boolean useDataBuffer = false;
if(compression != COMP_JPEG_TTN2) { // JPEG access Raster
if(checkContiguous) {
if(sampleSize[0] == 8) { // 8-bit
ComponentSampleModel csm =
(ComponentSampleModel)src.getSampleModel();
int[] bankIndices = csm.getBankIndices();
int[] bandOffsets = csm.getBandOffsets();
int pixelStride = csm.getPixelStride();
int lineStride = csm.getScanlineStride();
if(pixelStride != numBands ||
lineStride != bytesPerRow) {
useDataBuffer = false;
} else {
useDataBuffer = true;
for(int i = 0;
useDataBuffer && i < numBands;
i++) {
if(bankIndices[i] != 0 ||
bandOffsets[i] != i) {
useDataBuffer = false;
}
}
}
} else { // 1-bit
MultiPixelPackedSampleModel mpp =
(MultiPixelPackedSampleModel)src.getSampleModel();
if(mpp.getNumBands() == 1 &&
mpp.getDataBitOffset() == 0 &&
mpp.getPixelBitStride() == 1) {
useDataBuffer = true;
}
}
}
if(!useDataBuffer) {
if(dataType == DataBuffer.TYPE_FLOAT) {
fpixels = src.getPixels(col, row, tileWidth, rows,
fpixels);
} else {
pixels = src.getPixels(col, row, tileWidth, rows,
pixels);
}
}
}
int index;
int pixel = 0;
int k = 0;
switch(sampleSize[0]) {
case 1:
if(useDataBuffer) {
byte[] btmp =
((DataBufferByte)src.getDataBuffer()).getData();
MultiPixelPackedSampleModel mpp =
(MultiPixelPackedSampleModel)src.getSampleModel();
int lineStride = mpp.getScanlineStride();
int inOffset =
mpp.getOffset(col -
src.getSampleModelTranslateX(),
row -
src.getSampleModelTranslateY());
if(lineStride == (int)bytesPerRow) {
System.arraycopy(btmp, inOffset,
bpixels, 0,
(int)bytesPerRow*rows);
} else {
int outOffset = 0;
for(int j = 0; j < rows; j++) {
System.arraycopy(btmp, inOffset,
bpixels, outOffset,
(int)bytesPerRow);
inOffset += lineStride;
outOffset += (int)bytesPerRow;
}
}
} else {
index = 0;
// For each of the rows in a strip
for (int i=0; i<rows; i++) {
// Write number of pixels exactly divisible by 8
for (int j=0; j<tileWidth/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;
}
// Write the pixels remaining after division by 8
if (tileWidth%8 > 0) {
pixel = 0;
for (int j=0; j<tileWidth%8; j++) {
pixel |= (pixels[index++] << (7 - j));
}
bpixels[k++] = (byte)pixel;
}
}
}
if(compression == COMP_NONE) {
output.write(bpixels, 0, rows * ((tileWidth+7)/8));
} else if(compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
} else if(compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
}
break;
case 4:
index = 0;
// For each of the rows in a strip
for (int i=0; i<rows; i++) {
// Write the number of pixels that will fit into an
// even number of nibbles.
for (int j=0; j<tileWidth/2; j++) {
pixel = (pixels[index++] << 4) | pixels[index++];
bpixels[k++] = (byte)pixel;
}
// Last pixel for odd-length lines
if ((tileWidth % 2) == 1) {
pixel = pixels[index++] << 4;
bpixels[k++] = (byte)pixel;
}
}
if(compression == COMP_NONE) {
output.write(bpixels, 0, rows * ((tileWidth+1)/2));
} else if(compression == COMP_PACKBITS) {
int numCompressedBytes =
compressPackBits(bpixels, rows,
(int)bytesPerRow,
compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
} else if(compression == COMP_DEFLATE) {
int numCompressedBytes =
deflate(deflater, bpixels, compressBuf);
tileByteCounts[tileNum++] = numCompressedBytes;
output.write(compressBuf, 0, numCompressedBytes);
}
break;
case 8:
if(compression != COMP_JPEG_TTN2) {
if(useDataBuffer) {
byte[] btmp =
((DataBufferByte)src.getDataBuffer()).getData();
ComponentSampleModel csm =
(ComponentSampleModel)src.getSampleModel();
int inOffset =
csm.getOffset(col -
src.getSampleModelTranslateX(),
row -
src.getSampleModelTranslateY());
int lineStride = csm.getScanlineStride();
if(lineStride == (int)bytesPerRow) {
System.arraycopy(btmp,
inOffset,
bpixels, 0,
(int)bytesPerRow*rows);