stimmer / DueVGA

Arduino Due VGA library
94 stars 31 forks source link

GIF to animation.data convertor (makeanim.py) issues #16

Open CSharpLover opened 8 years ago

CSharpLover commented 8 years ago

Hello, I'm using DueVGA library and it is great. I'm making a simple GUI homemade computer. Currently it only supports mice, but when Due USBHost Library got support usb hubs, it will support both mice and keyboard. I wanted to make a simple gif animator program, so i looked at SDCard example in extras. It has a convertor (makeanim.py) that converts gif files to pixel data that Arduino program understands. But it has problems in some gifs like you said (Not all animated GIFs will work properly due to a bug in the Python Imaging Library) and this bug is happening in some of my necessary gifs. So i made the similar version of this convertor in C#. It is working without these bugs, but I couldn't modify palette because I couldn't understand full of the code in python script. So there is some color quality problems... (Colors has much less quality in my convertor) When I finish it, i will make a pr but now i need to fix its problem. I will post its code in second post, how can i fix its color problem?

Thanks, again, very good library.

CSharpLover commented 8 years ago

Code:

class Program
{
    static int index = 1;

    static Image[] GetFrames(Image originalImg)
    {
        int numberOfFrames = originalImg.GetFrameCount(FrameDimension.Time);

        Image[] frames = new Image[numberOfFrames];

        for (int i = 0; i < numberOfFrames; i++)
        {
            originalImg.SelectActiveFrame(FrameDimension.Time, i);

            frames[i] = ((Image)originalImg.Clone());
        }

        return frames;
    }

    static byte[] GetImageBytes(Image image)
    {
        Bitmap bmp = new Bitmap(image);

        byte[] bytes = new byte[(bmp.Width * bmp.Height)];

        for (int x = 0; x < bmp.Width; x++)
        {
            for (int y = 0; y < bmp.Height; y++)
            {
                Color pixel = bmp.GetPixel(x, y);

                bytes[x + y * bmp.Width] = (byte) ((pixel.R >> 5) | (pixel.G >> 2) | pixel.B);
            }
        }
        return bytes;
    }

    static void Main(string[] args)
    {
        byte[] GifFile = File.ReadAllBytes(args[0]);

        Image Temporary = Image.FromStream(new MemoryStream(GifFile));

        MemoryStream SR = new MemoryStream();
        Temporary.Save(SR, ImageFormat.Gif);

        Bitmap TempBitmap = new Bitmap(SR);

        Bitmap EightBitImage = TempBitmap.Clone(new Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height), PixelFormat.Format8bppIndexed);

        if (File.Exists(args[1]))
        {
            File.Delete(args[1]);
        }

        BinaryWriter Writer = new BinaryWriter(File.Open(args[1], FileMode.Create));

        Console.WriteLine("Preparing GIF Frames (This can take a while) for Converting...");

        foreach (Image I in GetFrames(EightBitImage))
        {
            Console.WriteLine("Converting Frame: {0}", index++);

            byte[] imagearray = GetImageBytes(I);

            foreach (byte b in imagearray)
            {
                Writer.Write(b);
            }
        }

        Writer.Flush();
        Writer.Dispose();

        Console.WriteLine("GIF is successfully converted.");
    }
}

Hastebin:

http://hastebin.com/quxecagevi.avrasm