DataBufferByte buffer=(DataBufferByte)raster.getDataBuffer();
byte[] imgdata=(byte[])buffer.getData();
BitSwapOutputStream bsos = new BitSwapOutputStream(baos);
ModHuffmanOutputStream mhos = new ModModREADOutputStream(bsos,width);
RLEBit1OutputStream rlos = new RLEBit1OutputStream(mhos);
int len=width>>3; // eight pixel per byte
int end=8-(width&0x07); // how many bits of last byte represent image data
int off=0;
if(end==8){ // image row ends at byte boundary
for(int y=0;y<height;y++){
rlos.setStartCodeWord(0x0001); // white run first; White is Zero: 1s in image => 0s in compressed data
mhos.writeEOL(); // T.6: we don't write EOL code, we just set up buffers here
rlos.write(imgdata,off,len);
rlos.flush();
off+=len;
}
}else{
for(int y=0;y<height;y++){
rlos.setStartCodeWord(0x0001); // white run first; White is Zero: 1s in image => 0s in compressed data
mhos.writeEOL(); // T.6: we don't write EOL code, we just set up buffers here
rlos.write(imgdata,off,len);
rlos.writeBits(imgdata[off+len],7,end); // write end of line pixel
rlos.flush();
off+=len+1;
}
}
if(mhos instanceof ModModREADOutputStream){ // T.6: write EOFB
((ModModREADOutputStream)mhos).writeEOFB();
}
rlos.close();
}