/** *************************************************************
*
* Read in the GIF to generate color cube for the array. If
* this is just an GIF this function will initialize the LUT
* with the colors of the imageas long as there are fewer than
* 256 colors (the MAX for a GIF)
*
* @param file The input image file
* @returns boolean Indicates initialization success
*
*/
public boolean init( String file )
{
Hashtable ht = null;
Image proto = null;
Canvas can = null;
MediaTracker t = null;
PixelGrabber pg = null;
Toolkit tk = null;
int n=0;
try
{
ht = new Hashtable();
can = new Canvas();
tk = can.getToolkit();
proto = tk.getImage( file );
t = new MediaTracker(this);
t.addImage( proto, 0 );
t.waitForAll();
int w = proto.getWidth(null);
int h = proto.getHeight(null);
int pix[] = new int[w*h];
pg = new PixelGrabber(proto, 0, 0, w, h, pix, 0, w);
while(!(pg.grabPixels()))
{
Thread.sleep(100);
}
// Insert into hash table to nuke duplicates
while (n<(pix.length))
{
Integer iobj = new Integer(pix[n++]);
ht.put(iobj, iobj);
}
t.removeImage( proto );
proto.flush();
t = null;
proto = null;
tk = null;
can = null;
// Create byte array.
n = ht.size();
if ( n>MAXCOLORS )
throw new Exception( "CIUNetscapeColorCube::init> too many colors" );
//
// Since we need 8 bits for an index into this LUT, the
// GIF we create will expect 256 possible values... therefore,
// we allocate these 256 but only fill the 1st 216. The rest
// are black.
//
m_aColors = new byte[ 256*3 ];
m_cColors = n;
// Fill byte array
Enumeration enum = ht.keys();
n=0;
while(enum.hasMoreElements() && (n<216))
{
int i = ((Integer)(enum.nextElement())).intValue();
byte r = (byte)(( i >> 16) & 0xFF);
byte g = (byte)(( i >> 8 ) & 0xFF);
byte b = (byte)( i & 0xFF);
m_aColors[ n*3 ] = r;
m_aColors[ n*3+1 ] = g;
m_aColors[ n*3+2 ] = b;
++n;
}
}
catch(Exception e)
{
System.err.println("CIUNetscapeColorCube::init> Exception: " + e );
e.printStackTrace();
return false;
}
return true;
}