Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Little help to convert code in C# #3

Open
MagicFred888 opened this issue Dec 28, 2021 · 1 comment
Open

Little help to convert code in C# #3

MagicFred888 opened this issue Dec 28, 2021 · 1 comment

Comments

@MagicFred888
Copy link

Dear Julien,

I don't have java and would like to convert your code in C# (of course I will send you code as soon as done, maybe you can publish it to help other user)... However to make check simpler, would you please share on Github the log that I should get when trying to extract data from the two referenced files that you shared and if possible, result images in a zip file?

  1. Unnamed.hwt
  2. com.huawei.watchface.rgb

This will help me to check step by step if my code conversion give proper result...
Thanks in advance for your support :-))

@BlackHatDevX
Copy link

using System;
using System.IO;
using System.IO.Compression;
using System.Drawing;

class ImageExtractor
{
    private byte[] sign = { 85, 85, 85, 85 };
    private Color? currentColor = null;
    private int currentColorCount = 0;

    public void Read(string binFile, string outputDir)
    {
        Stream dataIn = null;
        ZipArchive zis = null;

        if (binFile.EndsWith(".hwt"))
        {
            bool foundEntry = false;
            zis = ZipFile.OpenRead(binFile);
            foreach (ZipArchiveEntry zipEntry in zis.Entries)
            {
                if (zipEntry.FullName == "com.huawei.watchface")
                {
                    dataIn = zipEntry.Open();
                    foundEntry = true;
                    break;
                }
            }

            if (!foundEntry)
            {
                Console.WriteLine($"The archive {binFile} doesn't contain com.huawei.watchface file");
                return;
            }
        }
        else
        {
            dataIn = File.OpenRead(binFile);
        }

        int signFlag = 0;
        long fileSize = new FileInfo(binFile).Length;

        for (long i = 0; i < fileSize; i++)
        {
            byte tmp = (byte)dataIn.ReadByte();
            if (sign[signFlag] == tmp)
            {
                signFlag += 1;
            }
            else
            {
                signFlag = 0;
            }

            if (signFlag >= 4)
            {
                Console.WriteLine($"sign position->{i}");
                break;
            }
        }

        dataIn.ReadByte(); // Skip 4 bytes
        outputDir = Path.GetFullPath(outputDir);
        Directory.CreateDirectory(outputDir);
        var imsList = new System.Collections.Generic.List<Image>();
        Console.WriteLine("Start parsing pictures");
        bool errFlag = false;

        while (!errFlag)
        {
            try
            {
                var img = ReadImg(dataIn);
                imsList.Add(img);
            }
            catch (Exception)
            {
                errFlag = true;
            }
        }

        Console.WriteLine("Start outputting pictures");

        for (int i = 0; i < imsList.Count; i++)
        {
            Console.WriteLine($"Start outputting picture {i}");
            string imgPath = Path.Combine(outputDir, $"{i}.png");
            imsList[i].Save(imgPath, System.Drawing.Imaging.ImageFormat.Png);
        }

        Console.WriteLine("End of output pictures");
        if (zis != null)
        {
            zis.Dispose();
        }
        dataIn.Close();
    }

    private Image ReadImg(Stream dataIn)
    {
        string header = $"{dataIn.ReadByte():x2}{dataIn.ReadByte():x2}{dataIn.ReadByte():x2}{dataIn.ReadByte():x2}";
        int width1 = dataIn.ReadByte();
        int width2 = dataIn.ReadByte();
        int height1 = dataIn.ReadByte();
        int height2 = dataIn.ReadByte();
        int width = width1 + (width2 << 8);
        int height = height1 + (height2 << 8);
        Console.WriteLine($"Width->{width}, Height->{height}");
        var img = new Bitmap(width, height);

        for (int i = 0; i < height; i++)
        {
            for (int j = 0; j < width; j++)
            {
                Color c = ReadColor(dataIn);
                img.SetPixel(j, i, c);
            }
        }

        return img;
    }

    private Color ReadColor(Stream dataIn)
    {
        if (currentColorCount > 0)
        {
            currentColorCount -= 1;
            return currentColor.Value;
        }
        else
        {
            int blue = dataIn.ReadByte();
            int green = dataIn.ReadByte();
            int red = dataIn.ReadByte();
            int alpha = dataIn.ReadByte();
            if (blue == 137 && green == 103 && red == 69 && alpha == 35)
            {
                blue = dataIn.ReadByte();
                green = dataIn.ReadByte();
                red = dataIn.ReadByte();
                alpha = dataIn.ReadByte();
                int l1 = dataIn.ReadByte();
                int l2 = dataIn.ReadByte();
                int l3 = dataIn.ReadByte();
                int l4 = dataIn.ReadByte();
                int count = (l4 << 24) + (l3 << 16) + (l2 << 8) + l1;
                currentColorCount = count - 1;
                currentColor = Color.FromArgb(alpha, red, green, blue);
                return currentColor.Value;
            }
            else
            {
                return Color.FromArgb(alpha, red, green, blue);
            }
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        if (args.Length < 1 || args[0] == "-?" || args[0] == "/?")
        {
            ShowHelp();
            Environment.Exit(0);
        }

        string inputFile = args[0];
        string outputDir = (args.Length < 2) ? inputFile + "_extra" : args[1];

        try
        {
            ImageExtractor extractor = new ImageExtractor();
            extractor.Read(inputFile, outputDir);
        }
        catch (Exception e)
        {
            Console.WriteLine("An error occurred: " + e.Message);
            Environment.Exit(-1);
        }

        Environment.Exit(0);
    }

    static void ShowHelp()
    {
        string argFormat = "{0,-10}";
        Console.WriteLine("Extract images from HWT or com.huawei.watchface file");
        Console.WriteLine("Simply drag & drop a file on executable\n");
        Console.WriteLine("Usage: HuaweiWatchFaceExtractor input [outputDir]");
        Console.WriteLine(string.Format("\t" + argFormat + "*.hwt or com.huawei.watchface file", ""));
        Console.WriteLine(string.Format("\t" + argFormat + "optional output directory for extracted images. Defaults to <input>_extra", ""));
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants