stripRect.height = im.getHeight() - stripRect.y;
}
Rectangle encodedRect = stripRect.intersection(imageBounds);
// Get a strip of data.
Raster strip = im.getData(encodedRect);
// Get the data array.
byte[] bdata =
((DataBufferByte)strip.getDataBuffer()).getData();
// Get the scanline stride.
int rowStride = variant == PBM_RAW ?
((MultiPixelPackedSampleModel)strip.getSampleModel()).getScanlineStride() :
((ComponentSampleModel)strip.getSampleModel()).getScanlineStride();
if(rowStride == bytesPerRow && !isPBMInverted) {
// Write the entire strip at once.
output.write(bdata, 0, bdata.length);
} else {
// Write the strip row-by-row.
int offset = 0;
for(int i = 0; i < encodedRect.height; i++) {
if(isPBMInverted) {
for(int k = 0; k < bytesPerRow; k++) {
invertedData[k] =
(byte)(~(bdata[offset+k]&0xff));
}
output.write(invertedData, 0, bytesPerRow);
} else {
output.write(bdata, offset, bytesPerRow);
}
offset += rowStride;
}
}
// Increment the strip origin.
stripRect.y += tileHeight;
}
// Write all buffered bytes and return.
output.flush();
return;
}
// Buffer for up to 8 rows of pixels
int[] pixels = new int[8*width*numBands];
// Also allocate a buffer to hold the data to be written to the file,
// so we can use array writes.
byte[] bpixels = reds == null ?
new byte[8*width*numBands] : new byte[8*width*3];
// The index of the sample being written, used to
// place a line separator after every 16th sample in
// ASCII mode. Not used in raw mode.
int count = 0;
// Process 8 rows at a time so all but the last will have
// a multiple of 8 pixels. This simplifies PBM_RAW encoding.
int lastRow = minY + height;
for (int row = minY; row < lastRow; row += 8) {
int rows = Math.min(8, lastRow - row);
int size = rows*width*numBands;
// Grab the pixels
Raster src = im.getData(new Rectangle(minX, row, width, rows));
src.getPixels(minX, row, width, rows, pixels);
// Invert bits if necessary.
if(isPBMInverted) {
for(int k = 0; k < size; k++) {
pixels[k] ^= 0x00000001;