public void decode(ByteBuffer buffer,int stride,Format fmt) throws IOException {
  final int offset=buffer.position();
  final int lineSize=((width * bitdepth + 7) / 8) * bytesPerPixel;
  byte[] curLine=new byte[lineSize + 1];
  byte[] prevLine=new byte[lineSize + 1];
  byte[] palLine=(bitdepth < 8) ? new byte[width + 1] : null;
  byte[] data=JTranscZlibTemp.uncompress(idat.toByteArray(),(lineSize + 1) * height);
  for (int y=0; y < height; y++) {
    FastMemory.copy(data,y * (lineSize + 1),curLine,0,(lineSize + 1));
    unfilter(curLine,prevLine);
    buffer.position(offset + y * stride);
switch (colorType) {
case COLOR_TRUECOLOR:
switch (fmt) {
case ABGR:
        copyRGBtoABGR(buffer,curLine);
      break;
case RGBA:
    copyRGBtoRGBA(buffer,curLine);
  break;
case BGRA:
copyRGBtoBGRA(buffer,curLine);
break;
case RGB:
copy(buffer,curLine);
break;
default :
throw new UnsupportedOperationException("Unsupported format for this image");
}
break;
case COLOR_TRUEALPHA:
switch (fmt) {
case ABGR:
copyRGBAtoABGR(buffer,curLine);
break;
case RGBA:
copy(buffer,curLine);
break;
case BGRA:
copyRGBAtoBGRA(buffer,curLine);
break;
case RGB:
copyRGBAtoRGB(buffer,curLine);
break;
default :
throw new UnsupportedOperationException("Unsupported format for this image");
}
break;
case COLOR_GREYSCALE:
switch (fmt) {
case LUMINANCE:
case ALPHA:
copy(buffer,curLine);
break;
default :
throw new UnsupportedOperationException("Unsupported format for this image");
}
break;
case COLOR_GREYALPHA:
switch (fmt) {
case LUMINANCE_ALPHA:
copy(buffer,curLine);
break;
default :
throw new UnsupportedOperationException("Unsupported format for this image");
}
break;
case COLOR_INDEXED:
switch (bitdepth) {
case 8:
palLine=curLine;
break;
case 4:
expand4(curLine,palLine);
break;
case 2:
expand2(curLine,palLine);
break;
case 1:
expand1(curLine,palLine);
break;
default :
throw new UnsupportedOperationException("Unsupported bitdepth for this image");
}
switch (fmt) {
case ABGR:
copyPALtoABGR(buffer,palLine);
break;
case RGBA:
copyPALtoRGBA(buffer,palLine);
break;
case BGRA:
copyPALtoBGRA(buffer,palLine);
break;
default :
throw new UnsupportedOperationException("Unsupported format for this image");
}
break;
default :
throw new UnsupportedOperationException("Not yet implemented");
}
byte[] tmp=curLine;
curLine=prevLine;
prevLine=tmp;
}
}

public void decode ( ByteBuffer buffer , int stride , Format fmt ) throws IOException { final int offset = buffer . position ( ) ; final int lineSize = ( ( width * bitdepth + 7 ) / 8 ) * bytesPerPixel ; byte [ ] curLine = new byte [ lineSize + 1 ] ; byte [ ] prevLine = new byte [ lineSize + 1 ] ; byte [ ] palLine = ( bitdepth < 8 ) ? new byte [ width + 1 ] : null ; byte [ ] data = JTranscZlibTemp . uncompress ( idat . toByteArray ( ) , ( lineSize + 1 ) * height ) ; for ( int y = 0 ; y < height ; y ++ ) { FastMemory . copy ( data , y * ( lineSize + 1 ) , curLine , 0 , ( lineSize + 1 ) ) ; unfilter ( curLine , prevLine ) ; buffer . position ( offset + y * stride ) ; switch ( colorType ) { case COLOR_TRUECOLOR : switch ( fmt ) { case ABGR : copyRGBtoABGR ( buffer , curLine ) ; break ; case RGBA : copyRGBtoRGBA ( buffer , curLine ) ; break ; case BGRA : copyRGBtoBGRA ( buffer , curLine ) ; break ; case RGB : copy ( buffer , curLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported format for this image" ) ; } break ; case COLOR_TRUEALPHA : switch ( fmt ) { case ABGR : copyRGBAtoABGR ( buffer , curLine ) ; break ; case RGBA : copy ( buffer , curLine ) ; break ; case BGRA : copyRGBAtoBGRA ( buffer , curLine ) ; break ; case RGB : copyRGBAtoRGB ( buffer , curLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported format for this image" ) ; } break ; case COLOR_GREYSCALE : switch ( fmt ) { case LUMINANCE : case ALPHA : copy ( buffer , curLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported format for this image" ) ; } break ; case COLOR_GREYALPHA : switch ( fmt ) { case LUMINANCE_ALPHA : copy ( buffer , curLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported format for this image" ) ; } break ; case COLOR_INDEXED : switch ( bitdepth ) { case 8 : palLine = curLine ; break ; case 4 : expand4 ( curLine , palLine ) ; break ; case 2 : expand2 ( curLine , palLine ) ; break ; case 1 : expand1 ( curLine , palLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported bitdepth for this image" ) ; } switch ( fmt ) { case ABGR : copyPALtoABGR ( buffer , palLine ) ; break ; case RGBA : copyPALtoRGBA ( buffer , palLine ) ; break ; case BGRA : copyPALtoBGRA ( buffer , palLine ) ; break ; default : throw new UnsupportedOperationException ( "Unsupported format for this image" ) ; } break ; default : throw new UnsupportedOperationException ( "Not yet implemented" ) ; } byte [ ] tmp = curLine ; curLine = prevLine ; prevLine = tmp ; } }
decodes the image into the specified buffer . the first line is placed at the current position . after decode the buffer position is at the end of the last line .
