GIF Decoding of Color Tables

Started by blue-j, April 13, 2024, 12:30:39 AM

Previous topic - Next topic

blue-j

I am trying to decode any Global Color Table (GCT) and/or Local Color Tables present in GIFs.  As far as I can tell, it is not possible to do in a config entry...?

In the source code, for the GCT, the position and length are calculated in order to be skipped to the next block:

    my $flags = Get8u(\$s, 4);
    if ($flags & 0x80) { # does this image contain a color table?
        # calculate color table size
        $length = 3 * (2 << ($flags & 0x07));
        $raf->Read($buff, $length) == $length or return 0; # skip color table
        Write($outfile, $buff) or $err = 1 if $outfile;
    }


I am unsure, but perhaps something along these lines would expose the list of RGB values?

# Check if the image has a color table
if ($flags & 0x80) {
    # Calculate the number of colors in the color table
    my $num_colors = 2 ** (($flags & 0x07) + 1);
   
    # Calculate the length of the color table in bytes
    my $length = 3 * $num_colors;
   
    # Read the color table
    my $buff;
    $raf->Read($buff, $length) == $length or return 0;

    # Process the color table to extract RGB values
    my @colors;
    for (my $i = 0; $i < $length; $i += 3) {
        my ($r, $g, $b) = unpack('C3', substr($buff, $i, 3));
        push @colors, { R => $r, G => $g, B => $b };
    }

    $et->FoundTag('GlobalColorTable', \@colors);
}

It looks to me like the source code doesn't mention LCTs?  It has the same structure - there's a flag to say whether it exists, and then the table is a sequence of RGB triplets.

On a related side note:

GIF:BackgroundColor should be GIF:BackgroundColorIndex
GIF:TransparentColor should be GIF:TransparentColorIndex

Thees values give the position in the GCT for those colors (zero indexed list of entries).


The GIF spec is a bear to read, but this is a nice resource:
https://matthewflickinger.com/lab/whatsinagif/bits_and_bytes.asp

and someone put the GIF 89a spec into HTML for us:
https://web.archive.org/web/20160304075538/http://qalle.net/gif89a.php

Any guidance on this would be very much appreciated.  It's over my head really.

- J