public void Set(string stream)
        {
            //Debug.Log("GLOBAL COLOR TABLE: " + stream);
            ColorList = new Dictionary <int, GifColor>();
            if (stream.Length % 6 != 0)
            {
                Debug.LogError("GLOBAL COLOR TABLE LENGTH DOESNT MOD 6");
            }
            NumberOfColors = stream.Length / 6;
            //Debug.LogError("GLOBAL COLOR TABLE COLORS: " + NumberOfColors);

            GlobalColorTableString = stream;

            //Debug.LogError("GLOBAL COLOR TABLE: " + GlobalColorTableString);

            //create our color table here using a dictionary
            int  index = 0;
            bool check = false;

            for (int i = 0; i < GlobalColorTableString.Length; i += 6)
            {
                string colorString = GlobalColorTableString.Substring(i, 6);
                if (colorString == "000000" && !check)
                {
                    Debug.LogError("FIRST 000000 IN GLOBAL COLOR TABLE: " + (i / 6) + "/" + (GlobalColorTableString.Length / 6));
                    check = true;
                }
                GifColor color = new GifColor();
                color.Set(colorString);
                ColorList.Add(index, color);
                index++;
            }
            //Debug.LogError("ADDED " + index + " COLORS TO OUR COLOR TABLE");
        }
    private GifColor[] readColorTable(byte[] bytes, int offset, int size)
    {
        GifColor[] colorTable = new GifColor[size];

        for (int i = 0; i < size; i++)
        {
            int      startIndex = offset + i * 3;
            GifColor color      = new GifColor(bytes[startIndex], bytes[startIndex + 1], bytes[startIndex + 2], 1);

            colorTable[i] = color;
        }

        return(colorTable);
    }
Example #3
0
    private GifColor[] readColorTable(byte[] bytes, int offset, int size)
    {
        GifColor[] colorTable = new GifColor[size];

        for (int i = 0; i < size; i++)
        {
            int      startIndex = offset + i * 3;
            GifColor color      = new GifColor(bytes[startIndex], bytes[startIndex + 1], bytes[startIndex + 2], 1);

            colorTable[i] = color;
        }
        // Debug.Log($"This gif has {size / 3} colors on its color table.");

        return(colorTable);
    }
 public static GifColor[] ReadColorTable(Stream stream, int size)
 {
     int length = 3 * size;
     byte[] bytes = new byte[length];
     stream.ReadAll(bytes, 0, length);
     GifColor[] colorTable = new GifColor[size];
     for (int i = 0; i < size; i++)
     {
         byte r = bytes[3 * i];
         byte g = bytes[3 * i + 1];
         byte b = bytes[3 * i + 2];
         colorTable[i] = new GifColor(r, g, b);
     }
     return colorTable;
 }
Example #5
0
    public static GifColor[] ReadColorTable(Stream stream, int size)
    {
        var length = 3 * size;
        var bytes  = new byte[length];

        stream.ReadAll(bytes, 0, length);
        var colorTable = new GifColor[size];

        for (var i = 0; i < size; i++)
        {
            var r = bytes[3 * i];
            var g = bytes[3 * i + 1];
            var b = bytes[3 * i + 2];
            colorTable[i] = new GifColor(r, g, b);
        }

        return(colorTable);
    }
    private void createAnimator(GifData gifData)
    {
        List <Sprite>     sprites        = new List <Sprite>();
        GifAnimatorScript animatorScript = gameObject.AddComponent <GifAnimatorScript>();

        Color[] previousFrame    = new Color[gifData.canvasWidth * gifData.canvasHeight];
        Color[] currentFrame     = new Color[gifData.canvasWidth * gifData.canvasHeight];
        Color[] transparentFrame = new Color[gifData.canvasWidth * gifData.canvasHeight];

        // Create sprites
        for (int i = 0; i < gifData.graphicsControlExtensions.Count; i++)
        {
            GifGraphicsControlExtension graphicsControlExt = gifData.graphicsControlExtensions[i];
            GifImageDescriptor          imageDescriptor    = graphicsControlExt.imageDescriptor;
            GifImageData imageData         = imageDescriptor.imageData;
            int          top               = imageDescriptor.imageTop;
            int          left              = imageDescriptor.imageLeft;
            int          disposalMethod    = graphicsControlExt.disposalMethod;
            Texture2D    texture           = new Texture2D(gifData.canvasWidth, gifData.canvasHeight);
            int          transparencyIndex = graphicsControlExt.transparentColorFlag ? graphicsControlExt.transparentColorIndex : -1;

            // Determine base pixels
            if (i == 0)
            {
                texture.SetPixels(transparentFrame);
            }
            else
            {
                if (disposalMethod == 1)
                {
                    texture.SetPixels(previousFrame);
                }
                else if (disposalMethod == 2)
                {
                    texture.SetPixels(transparentFrame);
                }
                else if (disposalMethod == 3)
                {
                    throw new NotImplementedException("Disposal method 3 is not implemented.");
                }
            }

            // Set pixels from image data
            for (int j = 0; j < imageDescriptor.imageWidth; j++)
            {
                for (int k = 0; k < imageDescriptor.imageHeight; k++)
                {
                    int x           = left + j;
                    int y           = (gifData.canvasHeight - 1) - (top + k);
                    int colorIndex  = imageData.colorIndices[j + k * imageDescriptor.imageWidth];
                    int pixelOffset = x + y * gifData.canvasWidth;

                    if (colorIndex != transparencyIndex)
                    {
                        GifColor gifColor = imageData.getColor(colorIndex);

                        currentFrame[pixelOffset] = new Color(gifColor.r / 255f, gifColor.g / 255f, gifColor.b / 255f);
                    }
                }
            }

            // Set texture pixels and create sprite
            texture.SetPixels(currentFrame);
            texture.Apply();
            texture.filterMode = FilterMode.Point;
            sprites.Add(Sprite.Create(texture, new Rect(0f, 0f, gifData.canvasWidth, gifData.canvasHeight), new Vector2(1f, 1f)));

            // Store current frame as previous before continuing, and reset current frame
            currentFrame.CopyTo(previousFrame, 0);
            if (disposalMethod == 0 || disposalMethod == 2)
            {
                currentFrame = new Color[currentFrame.Length];
            }
        }

        // Setup animator script
        animatorScript.sprites = sprites;
    }
Example #7
0
    private void createAnimator(GifData gifData)
    {
        List <Sprite>     sprites        = new List <Sprite>();
        GifAnimatorScript animatorScript = gameObject.AddComponent <GifAnimatorScript>();

        Color[] previousFrame    = new Color[gifData.canvasWidth * gifData.canvasHeight];
        Color[] currentFrame     = new Color[gifData.canvasWidth * gifData.canvasHeight];
        Color[] transparentFrame = new Color[gifData.canvasWidth * gifData.canvasHeight];

        // Create sprites
        for (int i = 0; i < gifData.graphicsControlExtensions.Count; i++)
        {
            GifGraphicsControlExtension graphicsControlExt = gifData.graphicsControlExtensions[i];
            GifImageDescriptor          imageDescriptor    = graphicsControlExt.imageDescriptor;
            GifImageData imageData         = imageDescriptor.imageData;
            int          top               = imageDescriptor.imageTop;
            int          left              = imageDescriptor.imageLeft;
            int          disposalMethod    = graphicsControlExt.disposalMethod;
            Texture2D    texture           = new Texture2D(gifData.canvasWidth, gifData.canvasHeight);
            int          transparencyIndex = graphicsControlExt.transparentColorFlag ? graphicsControlExt.transparentColorIndex : -1;

            // Determine base pixels
            if (i == 0)
            {
                texture.SetPixels(transparentFrame);
            }
            else
            {
                if (disposalMethod == 1)
                {
                    texture.SetPixels(previousFrame);
                }
                else if (disposalMethod == 2)
                {
                    texture.SetPixels(transparentFrame);
                }
                else if (disposalMethod == 3)
                {
                    throw new NotImplementedException("Disposal method 3 is not implemented.");
                }
            }

            // Set pixels from image data
            for (int j = 0; j < imageDescriptor.imageWidth; j++)
            {
                for (int k = 0; k < imageDescriptor.imageHeight; k++)
                {
                    int x           = left + j;
                    int y           = (gifData.canvasHeight - 1) - (top + k);
                    int colorIndex  = imageData.colorIndices[j + k * imageDescriptor.imageWidth];
                    int pixelOffset = x + y * gifData.canvasWidth;

                    if (colorIndex != transparencyIndex)
                    {
                        GifColor gifColor = imageData.getColor(colorIndex);

                        currentFrame[pixelOffset] = new Color(gifColor.r / 255f, gifColor.g / 255f, gifColor.b / 255f);
                    }
                }
            }

            //float bgMax = 50f / 255;
            //var transparent = currentFrame.Where(p => p.a == 0);
            //var bgMeanSum = currentFrame.Aggregate(new float[8], (curSum, color) =>
            //{
            //    curSum[4] += color.a;
            //    ++curSum[5];

            //    if (color.a > 0)
            //    {
            //        curSum[6] += color.a;
            //        ++curSum[7];
            //    }

            //    if (color.r >= bgMax || color.g >= bgMax || color.b >= bgMax)
            //        return curSum; // We are interested on dark colors

            //    curSum[0] += color.r;
            //    curSum[1] += color.g;
            //    curSum[2] += color.b;
            //    ++curSum[3];

            //    return curSum;
            //});

            //var bgMean = new Color32(
            //    (byte)(bgMeanSum[0] * 255 / bgMeanSum[3]),
            //    (byte)(bgMeanSum[1] * 255 / bgMeanSum[3]),
            //    (byte)(bgMeanSum[2] * 255 / bgMeanSum[3]),
            //    (byte)(bgMeanSum[4] * 255 / bgMeanSum[5]));

            //Debug.Log(
            //    $"Frame {i} has transparent colors?: {transparent.Any()} || Count: {transparent.Count()} || Background: {bgMean}" +
            //    Environment.NewLine +
            //    $"Solid transparency mean (must be 255): {(byte)(bgMeanSum[6] * 255 / bgMeanSum[7])}");

            // Set texture pixels and create sprite
            texture.SetPixels(currentFrame);
            texture.Apply();
            texture.filterMode = FilterMode.Point;
            sprites.Add(Sprite.Create(texture, new Rect(0f, 0f, gifData.canvasWidth, gifData.canvasHeight), new Vector2(1f, 1f)));

            // Store current frame as previous before continuing, and reset current frame
            currentFrame.CopyTo(previousFrame, 0);
            if (disposalMethod == 0 || disposalMethod == 2)
            {
                currentFrame = new Color[currentFrame.Length];
            }
        }

        // Setup animator script
        animatorScript.sprites = sprites;
    }
        public void Set(string stream)
        {
            //Debug.Log("GLOBAL COLOR TABLE: " + stream);
            ColorList = new Dictionary<int, GifColor>();
            if(stream.Length % 6 != 0)
            {
                Debug.LogError("GLOBAL COLOR TABLE LENGTH DOESNT MOD 6");
            }
            NumberOfColors = stream.Length / 6;
            //Debug.LogError("GLOBAL COLOR TABLE COLORS: " + NumberOfColors);

            GlobalColorTableString = stream;

            //Debug.LogError("GLOBAL COLOR TABLE: " + GlobalColorTableString);

            //create our color table here using a dictionary
            int index = 0;
            bool check = false;

            for(int i = 0; i < GlobalColorTableString.Length; i+=6)
            {
                string colorString = GlobalColorTableString.Substring(i, 6);
                if(colorString == "000000" && !check)
                {
                    Debug.LogError("FIRST 000000 IN GLOBAL COLOR TABLE: " + (i/6) + "/" + (GlobalColorTableString.Length/6));
                    check = true;
                }
                GifColor color = new GifColor();
                color.Set(colorString);
                ColorList.Add(index,color);
                index++;
            }
            //Debug.LogError("ADDED " + index + " COLORS TO OUR COLOR TABLE");
        }