public int encodeTilePixels(int[] pixels,Rectangle rect,byte[] bpixels){
  int rows=(int)rect.getHeight();
  int cols=(int)rect.getWidth();
  int index;
  int pixel=0;
  int k=0;
  int rowBytes=0;
switch (sampleSize[0]) {
case 1:
    index=0;
  rowBytes=(cols + 7) / 8;
for (int i=0; i < rows; i++) {
  for (int j=0; j < cols / 8; j++) {
    pixel=(pixels[index++] << 7) | (pixels[index++] << 6) | (pixels[index++] << 5)| (pixels[index++] << 4)| (pixels[index++] << 3)| (pixels[index++] << 2)| (pixels[index++] << 1)| pixels[index++];
    bpixels[k++]=(byte)pixel;
  }
  if (cols % 8 > 0) {
    pixel=0;
    for (int j=0; j < cols % 8; j++) {
      pixel|=(pixels[index++] << (7 - j));
    }
    bpixels[k++]=(byte)pixel;
  }
}
break;
case 4:
index=0;
rowBytes=(cols + 3) / 4;
for (int i=0; i < rows; i++) {
for (int j=0; j < cols / 2; j++) {
pixel=(pixels[index++] << 4) | pixels[index++];
bpixels[k++]=(byte)pixel;
}
if ((cols % 2) != 0) {
pixel=pixels[index++] << 4;
bpixels[k++]=(byte)pixel;
}
}
break;
case 8:
index=0;
rowBytes=cols * numBands;
for (int i=0; i < rows; i++) {
for (int j=0; j < cols * numBands; j++) {
bpixels[k++]=(byte)pixels[index++];
}
}
break;
case 16:
index=0;
rowBytes=cols * 2;
int l=0;
for (int i=0; i < rows; i++) {
for (int j=0; j < cols; j++) {
short value=(short)pixels[index++];
bpixels[l++]=(byte)((value & 0xff00) >> 8);
bpixels[l++]=(byte)(value & 0x00ff);
}
}
break;
}
return rows * rowBytes;
}

public int encodeTilePixels ( int [ ] pixels , Rectangle rect , byte [ ] bpixels ) { int rows = ( int ) rect . getHeight ( ) ; int cols = ( int ) rect . getWidth ( ) ; int index ; int pixel = 0 ; int k = 0 ; int rowBytes = 0 ; switch ( sampleSize [ 0 ] ) { case 1 : index = 0 ; rowBytes = ( cols + 7 ) / 8 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < cols / 8 ; j ++ ) { pixel = ( pixels [ index ++ ] << 7 ) | ( pixels [ index ++ ] << 6 ) | ( pixels [ index ++ ] << 5 ) | ( pixels [ index ++ ] << 4 ) | ( pixels [ index ++ ] << 3 ) | ( pixels [ index ++ ] << 2 ) | ( pixels [ index ++ ] << 1 ) | pixels [ index ++ ] ; bpixels [ k ++ ] = ( byte ) pixel ; } if ( cols % 8 > 0 ) { pixel = 0 ; for ( int j = 0 ; j < cols % 8 ; j ++ ) { pixel |= ( pixels [ index ++ ] << ( 7 - j ) ) ; } bpixels [ k ++ ] = ( byte ) pixel ; } } break ; case 4 : index = 0 ; rowBytes = ( cols + 3 ) / 4 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < cols / 2 ; j ++ ) { pixel = ( pixels [ index ++ ] << 4 ) | pixels [ index ++ ] ; bpixels [ k ++ ] = ( byte ) pixel ; } if ( ( cols % 2 ) != 0 ) { pixel = pixels [ index ++ ] << 4 ; bpixels [ k ++ ] = ( byte ) pixel ; } } break ; case 8 : index = 0 ; rowBytes = cols * numBands ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < cols * numBands ; j ++ ) { bpixels [ k ++ ] = ( byte ) pixels [ index ++ ] ; } } break ; case 16 : index = 0 ; rowBytes = cols * 2 ; int l = 0 ; for ( int i = 0 ; i < rows ; i ++ ) { for ( int j = 0 ; j < cols ; j ++ ) { short value = ( short ) pixels [ index ++ ] ; bpixels [ l ++ ] = ( byte ) ( ( value & 0xff00 ) > > 8 ) ; bpixels [ l ++ ] = ( byte ) ( value & 0x00ff ) ; } } break ; } return rows * rowBytes ; }
encode the tile into bpixels and return the byte size ( uncompressed packing algorithm ) . the padding has already been done , so we may safely assume that pixels is exactly rows by cols by numbands ints .
