private static byte[] createColorTable(ColorModel colorModel,SampleModel sampleModel){
  byte[] colorTable;
  if (colorModel instanceof IndexColorModel) {
    IndexColorModel icm=(IndexColorModel)colorModel;
    int mapSize=icm.getMapSize();
    int ctSize=getGifPaletteSize(mapSize);
    byte[] reds=new byte[ctSize];
    byte[] greens=new byte[ctSize];
    byte[] blues=new byte[ctSize];
    icm.getReds(reds);
    icm.getGreens(greens);
    icm.getBlues(blues);
    for (int i=mapSize; i < ctSize; i++) {
      reds[i]=reds[0];
      greens[i]=greens[0];
      blues[i]=blues[0];
    }
    colorTable=new byte[3 * ctSize];
    int idx=0;
    for (int i=0; i < ctSize; i++) {
      colorTable[idx++]=reds[i];
      colorTable[idx++]=greens[i];
      colorTable[idx++]=blues[i];
    }
  }
 else   if (sampleModel.getNumBands() == 1) {
    int numBits=sampleModel.getSampleSize()[0];
    if (numBits > 8) {
      numBits=8;
    }
    int colorTableLength=3 * (1 << numBits);
    colorTable=new byte[colorTableLength];
    for (int i=0; i < colorTableLength; i++) {
      colorTable[i]=(byte)(i / 3);
    }
  }
 else {
    colorTable=null;
  }
  return colorTable;
}

private static byte [ ] createColorTable ( ColorModel colorModel , SampleModel sampleModel ) { byte [ ] colorTable ; if ( colorModel instanceof IndexColorModel ) { IndexColorModel icm = ( IndexColorModel ) colorModel ; int mapSize = icm . getMapSize ( ) ; int ctSize = getGifPaletteSize ( mapSize ) ; byte [ ] reds = new byte [ ctSize ] ; byte [ ] greens = new byte [ ctSize ] ; byte [ ] blues = new byte [ ctSize ] ; icm . getReds ( reds ) ; icm . getGreens ( greens ) ; icm . getBlues ( blues ) ; for ( int i = mapSize ; i < ctSize ; i ++ ) { reds [ i ] = reds [ 0 ] ; greens [ i ] = greens [ 0 ] ; blues [ i ] = blues [ 0 ] ; } colorTable = new byte [ 3 * ctSize ] ; int idx = 0 ; for ( int i = 0 ; i < ctSize ; i ++ ) { colorTable [ idx ++ ] = reds [ i ] ; colorTable [ idx ++ ] = greens [ i ] ; colorTable [ idx ++ ] = blues [ i ] ; } } else if ( sampleModel . getNumBands ( ) == 1 ) { int numBits = sampleModel . getSampleSize ( ) [ 0 ] ; if ( numBits > 8 ) { numBits = 8 ; } int colorTableLength = 3 * ( 1 << numBits ) ; colorTable = new byte [ colorTableLength ] ; for ( int i = 0 ; i < colorTableLength ; i ++ ) { colorTable [ i ] = ( byte ) ( i / 3 ) ; } } else { colorTable = null ; } return colorTable ; }
create a color table from the image colormodel and samplemodel .
