public IIOMetadataNode getStandardChromaNode() {
IIOMetadataNode chroma_node = new IIOMetadataNode("Chroma");
IIOMetadataNode node = null; // scratch node
TIFFField f;
// Set the PhotometricInterpretation and the palette color flag.
int photometricInterpretation = -1;
boolean isPaletteColor = false;
f = getTIFFField(BaselineTIFFTagSet.TAG_PHOTOMETRIC_INTERPRETATION);
if (f != null) {
photometricInterpretation = f.getAsInt(0);
isPaletteColor =
photometricInterpretation ==
BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_PALETTE_COLOR;
}
// Determine the number of channels.
int numChannels = -1;
if(isPaletteColor) {
numChannels = 3;
} else {
f = getTIFFField(BaselineTIFFTagSet.TAG_SAMPLES_PER_PIXEL);
if (f != null) {
numChannels = f.getAsInt(0);
} else { // f == null
f = getTIFFField(BaselineTIFFTagSet.TAG_BITS_PER_SAMPLE);
if(f != null) {
numChannels = f.getCount();
}
}
}
if(photometricInterpretation != -1) {
if (photometricInterpretation >= 0 &&
photometricInterpretation < colorSpaceNames.length) {
node = new IIOMetadataNode("ColorSpaceType");
String csName;
if(photometricInterpretation ==
BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_CMYK &&
numChannels == 3) {
csName = "CMY";
} else {
csName = colorSpaceNames[photometricInterpretation];
}
node.setAttribute("name", csName);
chroma_node.appendChild(node);
}
node = new IIOMetadataNode("BlackIsZero");
node.setAttribute("value",
(photometricInterpretation ==
BaselineTIFFTagSet.PHOTOMETRIC_INTERPRETATION_WHITE_IS_ZERO)
? "FALSE" : "TRUE");
chroma_node.appendChild(node);
}
if(numChannels != -1) {
node = new IIOMetadataNode("NumChannels");
node.setAttribute("value", Integer.toString(numChannels));
chroma_node.appendChild(node);
}
f = getTIFFField(BaselineTIFFTagSet.TAG_COLOR_MAP);
if (f != null) {
// NOTE: The presence of hasAlpha is vestigial: there is
// no way in TIFF to represent an alpha component in a palette
// color image. See bug 5086341.
boolean hasAlpha = false;
node = new IIOMetadataNode("Palette");
int len = f.getCount()/(hasAlpha ? 4 : 3);
for (int i = 0; i < len; i++) {
IIOMetadataNode entry =
new IIOMetadataNode("PaletteEntry");
entry.setAttribute("index", Integer.toString(i));
int r = (f.getAsInt(i)*255)/65535;
int g = (f.getAsInt(len + i)*255)/65535;
int b = (f.getAsInt(2*len + i)*255)/65535;
entry.setAttribute("red", Integer.toString(r));
entry.setAttribute("green", Integer.toString(g));
entry.setAttribute("blue", Integer.toString(b));
if (hasAlpha) {