switch(imageType) {
case TYPE_BILEVEL:
case TYPE_GRAY_4BIT:
sampleModel =
new MultiPixelPackedSampleModel(dataType,
tileWidth,
tileHeight,
sampleSize);
if(imageType == TYPE_BILEVEL) {
byte[] map = new byte[] {(byte)(isWhiteZero ? 255 : 0),
(byte)(isWhiteZero ? 0 : 255)};
colorModel = new IndexColorModel(1, 2, map, map, map);
} else {
colorModel =
ImageCodec.createGrayIndexColorModel(sampleModel,
!isWhiteZero);
}
break;
case TYPE_GRAY:
case TYPE_GRAY_ALPHA:
case TYPE_RGB:
case TYPE_RGB_ALPHA:
case TYPE_CMYK:
// Create a pixel interleaved SampleModel with decreasing
// band offsets.
int[] RGBOffsets = new int[numBands];
if(compression == COMP_JPEG_TTN2) {
for (int i=0; i<numBands; i++) {
RGBOffsets[i] = numBands - 1 - i;
}
} else {
for (int i=0; i<numBands; i++) {
RGBOffsets[i] = i;
}
}
sampleModel = createPixelInterleavedSampleModel(dataType,
tileWidth,
tileHeight,
numBands,
numBands*tileWidth,
RGBOffsets);
if(imageType == TYPE_GRAY || imageType == TYPE_RGB) {
colorModel =
ImageCodec.createComponentColorModel(sampleModel);
} else if (imageType == TYPE_CMYK) {
colorModel = ImageCodec.createComponentColorModel(sampleModel,
SimpleCMYKColorSpace.getInstance());
} else { // hasAlpha
// Transparency.OPAQUE signifies image data that is
// completely opaque, meaning that all pixels have an alpha
// value of 1.0. So the extra band gets ignored, which is
// what we want.
int transparency = Transparency.OPAQUE;
if(extraSamples == 1 || extraSamples == 2) {
// associated (premultiplied) alpha when == 1
// unassociated alpha when ==2
// Fix bug: 4699316
transparency = Transparency.TRANSLUCENT;
}
colorModel =
createAlphaComponentColorModel(dataType,
numBands,
extraSamples == 1,
transparency);
}
break;
case TYPE_GENERIC:
case TYPE_YCBCR_SUB:
// For this case we can't display the image, so we create a
// SampleModel with increasing bandOffsets, and keep the
// ColorModel as null, as there is no appropriate ColorModel.
int[] bandOffsets = new int[numBands];
for (int i=0; i<numBands; i++) {
bandOffsets[i] = i;
}
sampleModel =
createPixelInterleavedSampleModel(dataType,
tileWidth, tileHeight,
numBands, numBands * tileWidth,
bandOffsets);
colorModel = null;
break;
case TYPE_PALETTE:
// Get the colormap
TIFFField cfield = getField(dir, TIFFImageDecoder.TIFF_COLORMAP,
"Colormap");
colormap = cfield.getAsChars();
// Could be either 1 or 3 bands depending on whether we use
// IndexColorModel or not.
if (decodePaletteAsShorts) {
numBands = 3;
// If no SampleFormat tag was specified and if the
// sampleSize is less than or equal to 8, then the
// dataType was initially set to byte, but now we want to
// expand the palette as shorts, so the dataType should
// be ushort.
if (dataType == DataBuffer.TYPE_BYTE) {
dataType = DataBuffer.TYPE_USHORT;
}
// Data will have to be unpacked into a 3 band short image
// as we do not have a IndexColorModel that can deal with
// a colormodel whose entries are of short data type.
sampleModel =
RasterFactory.createPixelInterleavedSampleModel(dataType,
tileWidth,
tileHeight,
numBands);
colorModel = ImageCodec.createComponentColorModel(sampleModel);
} else {
numBands = 1;
if (sampleSize == 4) {
// Pixel data will not be unpacked, will use MPPSM to store
// packed data and IndexColorModel to do the unpacking.
sampleModel =
new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE,
tileWidth,
tileHeight,
sampleSize);
} else if (sampleSize == 8) {
sampleModel =