// users can read RGB and then convert to indexed in Java.
ColorModel cm = image.getColorModel();
if (cm instanceof IndexColorModel) {
throw new IIOException("IndexColorModel not supported");
}
// Now check the ColorSpace type against outColorSpaceCode
// We may want to tweak the default
ColorSpace cs = cm.getColorSpace();
int csType = cs.getType();
convert = null;
switch (outColorSpaceCode) {
case JPEG.JCS_GRAYSCALE: // Its gray in the file
if (csType == ColorSpace.TYPE_RGB) { // We want RGB
// IJG can do this for us more efficiently
setOutColorSpace(structPointer, JPEG.JCS_RGB);
} else if (csType != ColorSpace.TYPE_GRAY) {
throw new IIOException("Incompatible color conversion");
}
break;
case JPEG.JCS_RGB: // IJG wants to go to RGB
if (csType == ColorSpace.TYPE_GRAY) { // We want gray
if (colorSpaceCode == JPEG.JCS_YCbCr) {
// If the jpeg space is YCbCr, IJG can do it
setOutColorSpace(structPointer, JPEG.JCS_GRAYSCALE);
}
} else if ((iccCS != null) &&
(cm.getNumComponents() == numComponents) &&
(cs != iccCS)) {
// We have an ICC profile but it isn't used in the dest
// image. So convert from the profile cs to the target cs
convert = new ColorConvertOp(iccCS, cs, null);
// Leave IJG conversion in place; we still need it
} else if ((iccCS == null) &&
(!cs.isCS_sRGB()) &&
(cm.getNumComponents() == numComponents)) {
// Target isn't sRGB, so convert from sRGB to the target
convert = new ColorConvertOp(JPEG.sRGB, cs, null);
} else if (csType != ColorSpace.TYPE_RGB) {
throw new IIOException("Incompatible color conversion");
}
break;
case JPEG.JCS_RGBA:
// No conversions available; image must be RGBA
if ((csType != ColorSpace.TYPE_RGB) ||
(cm.getNumComponents() != numComponents)) {
throw new IIOException("Incompatible color conversion");
}
break;
case JPEG.JCS_YCC:
if (JPEG.YCC == null) { // We can't do YCC at all
throw new IIOException("Incompatible color conversion");
}
if ((cs != JPEG.YCC) &&
(cm.getNumComponents() == numComponents)) {
convert = new ColorConvertOp(JPEG.YCC, cs, null);
}
break;
case JPEG.JCS_YCCA:
// No conversions available; image must be YCCA
if ((JPEG.YCC == null) || // We can't do YCC at all
(cs != JPEG.YCC) ||
(cm.getNumComponents() != numComponents)) {
throw new IIOException("Incompatible color conversion");
}
break;
default:
// Anything else we can't handle at all
throw new IIOException("Incompatible color conversion");
}
}