Skip to content

My Python Generated GIFs' integrity is not preserved when I use UNIGIF in Unity #13

Description

@Ludovic915

Hello,

I made python scripts that draw PM2.5 Pollution forecast heat maps as GIFs, example:

Image

When I first implemented UNIGIF to read and display those GIFs in Unity the C# compiler came up with the following error:

Image

So I tweaked the UnigifDecoder.cs script with AI to bypass that error:

` private static List<byte[]> GetColorTableAndSetBgColor(
GifData gifData,
ImageBlock imgBlock,
int transparentIndex,
out Color32 bgColor)
{
List<byte[]> colorTable = null;

    // 1️⃣ Prefer LOCAL color table only if it is valid
    if (imgBlock.m_localColorTableFlag &&
        imgBlock.m_localColorTable != null &&
        imgBlock.m_localColorTable.Count > 0)
    {
        colorTable = imgBlock.m_localColorTable;
    }
    // 2️⃣ Fallback to GLOBAL color table
    else if (gifData.m_globalColorTableFlag &&
            gifData.m_globalColorTable != null &&
            gifData.m_globalColorTable.Count > 0)
    {
        colorTable = gifData.m_globalColorTable;
    }

    // 3️⃣ Final fallback: try whatever exists
    if (colorTable == null)
    {
        if (gifData.m_globalColorTable != null && gifData.m_globalColorTable.Count > 0)
            colorTable = gifData.m_globalColorTable;
        else if (imgBlock.m_localColorTable != null && imgBlock.m_localColorTable.Count > 0)
            colorTable = imgBlock.m_localColorTable;
    }

    // 4️⃣ Background color handling
    if (colorTable != null && colorTable.Count > 0)
    {
        int bgIndex = gifData.m_bgColorIndex;

        // Clamp background index safely
        if (bgIndex < 0 || bgIndex >= colorTable.Count)
            bgIndex = 0;

        byte[] bgRgb = colorTable[bgIndex];

        byte alpha = (transparentIndex == bgIndex)
            ? (byte)0
            : (byte)255;

        bgColor = new Color32(
            bgRgb[0],
            bgRgb[1],
            bgRgb[2],
            alpha
        );
    }
    else
    {
        // Absolute fallback (rare but safe)
        bgColor = new Color32(0, 0, 0, 255);
    }

    return colorTable;
}`

... and:

`private static void SetTexturePixelRow(GifData gifData,
Texture2D tex,
int y,
ImageBlock imgBlock,
byte[] decodedData,
ref int dataIndex,
List<byte[]> colorTable,
Color32 bgColor,
int transparentIndex,
bool filledTexture)
{
// Convert from top-left origin to bottom-left (Unity)
int row = tex.height - 1 - y;

for (int x = 0; x < tex.width; x++)
{
    // Check if the pixel is outside the image block
    if (row < imgBlock.m_imageTopPosition ||
        row >= imgBlock.m_imageTopPosition + imgBlock.m_imageHeight ||
        x < imgBlock.m_imageLeftPosition ||
        x >= imgBlock.m_imageLeftPosition + imgBlock.m_imageWidth)
    {
        if (!filledTexture)
        {
            tex.SetPixel(x, y, bgColor);
        }
        continue;
    }

    // Check if decoded data is exhausted
    if (dataIndex >= decodedData.Length)
    {
        if (!filledTexture)
        {
            tex.SetPixel(x, y, bgColor);
        }
        dataIndex++;
        continue;
    }

    // --- Safe palette lookup ---
    byte colorIndex = decodedData[dataIndex];
    int safeIndex;

    if (colorTable != null && colorIndex < colorTable.Count)
    {
        // Index is valid
        safeIndex = colorIndex;
    }
    else
    {
        // Browser-style recovery: try background index, else 0
        if (gifData.m_bgColorIndex >= 0 && gifData.m_bgColorIndex < colorTable.Count)
        {
            safeIndex = gifData.m_bgColorIndex;
        }
        else
        {
            safeIndex = 0;
        }
    }

    byte[] rgb = colorTable[safeIndex];

    // Handle transparency
    byte alpha = (transparentIndex >= 0 && colorIndex == transparentIndex) ? (byte)0 : (byte)255;

    // Set pixel if filledTexture is false or pixel is not fully transparent
    if (!filledTexture || alpha != 0)
    {
        tex.SetPixel(x, y, new Color32(rgb[0], rgb[1], rgb[2], alpha));
    }

    dataIndex++;
}

}
`
But the GIF rendu in unity is degraded: https://youtu.be/GW0eJrVa9VI

Any idea of how I can fix this to replicate the exact GIF in unity?

B.R,

Ludovic

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions