protected void decodeBitmapData(final GifFrame frame,byte[] dstPixels){
  if (frame != null) {
    rawData.position(frame.bufferFrameStart);
  }
  final int nullCode=-1;
  final int npix=(frame == null) ? width * height : frame.iw * frame.ih;
  int available, clear, code_mask, code_size, end_of_information, in_code, old_code, bits, code, count, i, datum, data_size, first, top, bi, pi;
  if (dstPixels == null || dstPixels.length < npix) {
    dstPixels=new byte[npix];
  }
  if (prefix == null) {
    prefix=new short[MAX_STACK_SIZE];
  }
  if (suffix == null) {
    suffix=new byte[MAX_STACK_SIZE];
  }
  if (pixelStack == null) {
    pixelStack=new byte[MAX_STACK_SIZE + 1];
  }
  data_size=read();
  clear=1 << data_size;
  end_of_information=clear + 1;
  available=clear + 2;
  old_code=nullCode;
  code_size=data_size + 1;
  code_mask=(1 << code_size) - 1;
  for (code=0; code < clear; code++) {
    prefix[code]=0;
    suffix[code]=(byte)code;
  }
  datum=bits=count=first=top=pi=bi=0;
  for (i=0; i < npix; ) {
    if (top == 0) {
      if (bits < code_size) {
        if (count == 0) {
          count=readBlock();
          if (count <= 0) {
            break;
          }
          bi=0;
        }
        datum+=((block[bi]) & 0xff) << bits;
        bits+=8;
        bi++;
        count--;
        continue;
      }
      code=datum & code_mask;
      datum>>=code_size;
      bits-=code_size;
      if ((code > available) || (code == end_of_information)) {
        break;
      }
      if (code == clear) {
        code_size=data_size + 1;
        code_mask=(1 << code_size) - 1;
        available=clear + 2;
        old_code=nullCode;
        continue;
      }
      if (old_code == nullCode) {
        pixelStack[top++]=suffix[code];
        old_code=code;
        first=code;
        continue;
      }
      in_code=code;
      if (code == available) {
        pixelStack[top++]=(byte)first;
        code=old_code;
      }
      while (code > clear) {
        pixelStack[top++]=suffix[code];
        code=prefix[code];
      }
      first=(suffix[code]) & 0xff;
      if (available >= MAX_STACK_SIZE) {
        break;
      }
      pixelStack[top++]=(byte)first;
      prefix[available]=(short)old_code;
      suffix[available]=(byte)first;
      available++;
      if (((available & code_mask) == 0) && (available < MAX_STACK_SIZE)) {
        code_size++;
        code_mask+=available;
      }
      old_code=in_code;
    }
    top--;
    dstPixels[pi++]=pixelStack[top];
    i++;
  }
  for (i=pi; i < npix; i++) {
    dstPixels[i]=0;
  }
}

protected void decodeBitmapData ( final GifFrame frame , byte [ ] dstPixels ) { if ( frame != null ) { rawData . position ( frame . bufferFrameStart ) ; } final int nullCode = - 1 ; final int npix = ( frame == null ) ? width * height : frame . iw * frame . ih ; int available , clear , code_mask , code_size , end_of_information , in_code , old_code , bits , code , count , i , datum , data_size , first , top , bi , pi ; if ( dstPixels == null || dstPixels . length < npix ) { dstPixels = new byte [ npix ] ; } if ( prefix == null ) { prefix = new short [ MAX_STACK_SIZE ] ; } if ( suffix == null ) { suffix = new byte [ MAX_STACK_SIZE ] ; } if ( pixelStack == null ) { pixelStack = new byte [ MAX_STACK_SIZE + 1 ] ; } data_size = read ( ) ; clear = 1 << data_size ; end_of_information = clear + 1 ; available = clear + 2 ; old_code = nullCode ; code_size = data_size + 1 ; code_mask = ( 1 << code_size ) - 1 ; for ( code = 0 ; code < clear ; code ++ ) { prefix [ code ] = 0 ; suffix [ code ] = ( byte ) code ; } datum = bits = count = first = top = pi = bi = 0 ; for ( i = 0 ; i < npix ; ) { if ( top == 0 ) { if ( bits < code_size ) { if ( count == 0 ) { count = readBlock ( ) ; if ( count <= 0 ) { break ; } bi = 0 ; } datum += ( ( block [ bi ] ) & 0xff ) << bits ; bits += 8 ; bi ++ ; count -- ; continue ; } code = datum & code_mask ; datum >>= code_size ; bits -= code_size ; if ( ( code > available ) || ( code == end_of_information ) ) { break ; } if ( code == clear ) { code_size = data_size + 1 ; code_mask = ( 1 << code_size ) - 1 ; available = clear + 2 ; old_code = nullCode ; continue ; } if ( old_code == nullCode ) { pixelStack [ top ++ ] = suffix [ code ] ; old_code = code ; first = code ; continue ; } in_code = code ; if ( code == available ) { pixelStack [ top ++ ] = ( byte ) first ; code = old_code ; } while ( code > clear ) { pixelStack [ top ++ ] = suffix [ code ] ; code = prefix [ code ] ; } first = ( suffix [ code ] ) & 0xff ; if ( available >= MAX_STACK_SIZE ) { break ; } pixelStack [ top ++ ] = ( byte ) first ; prefix [ available ] = ( short ) old_code ; suffix [ available ] = ( byte ) first ; available ++ ; if ( ( ( available & code_mask ) == 0 ) && ( available < MAX_STACK_SIZE ) ) { code_size ++ ; code_mask += available ; } old_code = in_code ; } top -- ; dstPixels [ pi ++ ] = pixelStack [ top ] ; i ++ ; } for ( i = pi ; i < npix ; i ++ ) { dstPixels [ i ] = 0 ; } }
decodes lzw image data into pixel array . adapted from john cristy ' s bitmapmagick .
