colorModel instanceof IndexColorModel &&
dataType != DataBuffer.TYPE_BYTE) {
// Don't support (unsigned) short palette-color images.
throw new Error("TIFFImageEncoder6");
}
IndexColorModel icm = null;
int sizeOfColormap = 0;
char colormap[] = null;
// Set image type.
int imageType = TIFF_UNSUPPORTED;
int numExtraSamples = 0;
int extraSampleType = EXTRA_SAMPLE_UNSPECIFIED;
if(colorModel instanceof IndexColorModel) { // Bilevel or palette
icm = (IndexColorModel)colorModel;
int mapSize = icm.getMapSize();
if(sampleSize[0] == 1 && numBands == 1) { // Bilevel image
if (mapSize != 2) {
throw new IllegalArgumentException(
"TIFFImageEncoder7");
}
byte r[] = new byte[mapSize];
icm.getReds(r);
byte g[] = new byte[mapSize];
icm.getGreens(g);
byte b[] = new byte[mapSize];
icm.getBlues(b);
if ((r[0] & 0xff) == 0 &&
(r[1] & 0xff) == 255 &&
(g[0] & 0xff) == 0 &&
(g[1] & 0xff) == 255 &&
(b[0] & 0xff) == 0 &&
(b[1] & 0xff) == 255) {
imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
} else if ((r[0] & 0xff) == 255 &&
(r[1] & 0xff) == 0 &&
(g[0] & 0xff) == 255 &&
(g[1] & 0xff) == 0 &&
(b[0] & 0xff) == 255 &&
(b[1] & 0xff) == 0) {
imageType = TIFF_BILEVEL_WHITE_IS_ZERO;
} else {
imageType = TIFF_PALETTE;
}
} else if(numBands == 1) { // Non-bilevel image.
// Palette color image.
imageType = TIFF_PALETTE;
}
} else if(colorModel == null) {
if(sampleSize[0] == 1 && numBands == 1) { // bilevel
imageType = TIFF_BILEVEL_BLACK_IS_ZERO;
} else { // generic image
imageType = TIFF_GENERIC;
if(numBands > 1) {
numExtraSamples = numBands - 1;
}
}
} else { // colorModel is non-null but not an IndexColorModel
ColorSpace colorSpace = colorModel.getColorSpace();
switch(colorSpace.getType()) {
case ColorSpace.TYPE_CMYK:
imageType = TIFF_CMYK;
break;
case ColorSpace.TYPE_GRAY:
imageType = TIFF_GRAY;
break;
case ColorSpace.TYPE_Lab:
imageType = TIFF_CIELAB;
break;
case ColorSpace.TYPE_RGB:
if(compression == COMP_JPEG_TTN2 &&
encodeParam.getJPEGCompressRGBToYCbCr()) {
imageType = TIFF_YCBCR;
} else {
imageType = TIFF_RGB;
}
break;
case ColorSpace.TYPE_YCbCr:
imageType = TIFF_YCBCR;
break;
default:
imageType = TIFF_GENERIC; // generic
break;
}
if(imageType == TIFF_GENERIC) {
numExtraSamples = numBands - 1;
} else if(numBands > 1) {
numExtraSamples = numBands - colorSpace.getNumComponents();
}
if(numExtraSamples == 1 && colorModel.hasAlpha()) {
extraSampleType = colorModel.isAlphaPremultiplied() ?
EXTRA_SAMPLE_ASSOCIATED_ALPHA :
EXTRA_SAMPLE_UNASSOCIATED_ALPHA;
}
}
if(imageType == TIFF_UNSUPPORTED) {
throw new Error("TIFFImageEncoder8");
}
// Check JPEG compatibility.
if(compression == COMP_JPEG_TTN2) {
if(imageType == TIFF_PALETTE) {
throw new Error("TIFFImageEncoder11");
} else if(!(sampleSize[0] == 8 &&
(imageType == TIFF_GRAY ||
imageType == TIFF_RGB ||
imageType == TIFF_YCBCR))) {
throw new Error("TIFFImageEncoder9");
}
}
int photometricInterpretation = -1;
switch (imageType) {
case TIFF_BILEVEL_WHITE_IS_ZERO:
photometricInterpretation = 0;
break;
case TIFF_BILEVEL_BLACK_IS_ZERO:
photometricInterpretation = 1;
break;
case TIFF_GRAY:
case TIFF_GENERIC:
// Since the CS_GRAY colorspace is always of type black_is_zero
photometricInterpretation = 1;
break;
case TIFF_PALETTE:
photometricInterpretation = 3;
icm = (IndexColorModel)colorModel;
sizeOfColormap = icm.getMapSize();
byte r[] = new byte[sizeOfColormap];
icm.getReds(r);
byte g[] = new byte[sizeOfColormap];
icm.getGreens(g);
byte b[] = new byte[sizeOfColormap];
icm.getBlues(b);
int redIndex = 0, greenIndex = sizeOfColormap;
int blueIndex = 2 * sizeOfColormap;
colormap = new char[sizeOfColormap * 3];
for (int i=0; i<sizeOfColormap; i++) {