Showing the Colour Table of a GIF.

This script will display the colours in the colour table used by the first frame of a GIF.

                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                
                                                                

The script can be downloaded here - showpalette.zip. The image path is hard coded but this can easily be changed to read an image of your choice. To run the script the trial version of the csASPGif component must be registered. Click Here to download the component.

This is the GIF image.

Here is a listing of the script. Much of it is devoted to making the colours display in a square table and this is done by counting the rows and columns. The GIF could use a Global Colour Table or a Local Colour Table and this is taken care of with a <cfif> tag. If a multi-frame GIF is loaded only the first frame will be used.

<cfobject action="create" name="Gif" class="csASPGifTrial.Gif">
<cfset Gif.ReadFile(ExpandPath("./images/rabbit.gif"))>

<table>
<cfif Gif.ColorDepth EQ 1>
  <cfset Rows = 1>
  <cfset Columns = 2>
<cfelse>
  <cfif Gif.ColorDepth EQ 4>
    <cfset Rows = 1>
    <cfset Columns = 16>
  <cfelse>
    <cfset Rows = 16>
    <cfset Columns = 16>
  </cfif>
</cfif>
<cfset CurrentRow = 0>
<cfloop condition="CurrentRow NEQ Rows">
  <cfset CurrentColumn = 0>
  <cfoutput><tr></cfoutput>
  <cfloop condition="CurrentColumn NEQ Columns">
    <cfset Index = CurrentColumn + CurrentRow * 16>
    <cfif Gif.HasGCT EQ True>
    <cfoutput><td bgcolor=###Gif.GCTEntry(Index)#>
    </td></cfoutput>
    <cfelse>
    <cfoutput><td bgcolor=###Gif.LCTEntry(0, Index)#>
    </td></cfoutput>
    </cfif>
    <cfset CurrentColumn = CurrentColumn + 1>
  </cfloop>
  <cfset CurrentRow = CurrentRow + 1>
  <cfoutput></tr></cfoutput>
</cfloop>
</table>

There is an alternative method available in Cold Fusion MX which uses the PageContext Java Object and this can stream images directly to the browser without saving to a temporary file first. The is more information in the Cold Fusion section in the online manual.