} else if(il.getSampleModel(null) != null) {
// Target SampleModel is available.
// Get SampleModel from "il"; guaranteed to be non-null.
SampleModel sm = il.getSampleModel(null);
// Get ColorModel from "layout".
ColorModel cmLayout = layout.getColorModel(null);
// First attempt to set the ColorModel to that specified
// in the original layout, if any.
if(cmLayout != null) {
// ColorModel is set in original layout.
if(JDKWorkarounds.areCompatibleDataModels(sm, cmLayout)) {
// ColorModel is compatible with target SampleModel.
il.setColorModel(cmLayout);
} else if(layout.getSampleModel(null) == null) {
// SampleModel not set in original layout so
// ColorModel must be incompatible with source
// SampleModel: create a compatible SampleModel.
// Set the ColorModel to that desired.
il.setColorModel(cmLayout);
// Derive a new SampleModel from the desired ColorModel
SampleModel derivedSM =
cmLayout.createCompatibleSampleModel(
il.getTileWidth(null),
il.getTileHeight(null));
// Set the SampleModel to that derived from the CM.
il.setSampleModel(derivedSM);
}
}
// If ColorModel not set from ImageLayout, attempt to set
// using a ColorModelFactory and if that fails, attempt to
// use the ColorModel of the source.
if(!il.isValid(ImageLayout.COLOR_MODEL_MASK) &&
!setColorModelFromFactory(sm, sources, config, il)) {
// Get ColorModel from "im", i.e., the source.
ColorModel cmSource = im.getColorModel();
if(cmSource != null &&
JDKWorkarounds.areCompatibleDataModels(sm, cmSource)) {
// Set to source ColorModel.
if (cmSource != null &&
cmSource instanceof IndexColorModel &&
config != null &&
config.containsKey(
JAI.KEY_REPLACE_INDEX_COLOR_MODEL) &&
((Boolean)config.get(JAI.KEY_REPLACE_INDEX_COLOR_MODEL)).booleanValue()) {
ColorModel newCM =
PlanarImage.getDefaultColorModel(
sm.getDataType(),
cmSource.getNumComponents());
SampleModel newSM;
if (newCM != null) {
newSM =
newCM.createCompatibleSampleModel(
il.getTileWidth(null),
il.getTileHeight(null));
} else {
newSM =
RasterFactory.createPixelInterleavedSampleModel(
sm.getDataType(),
il.getTileWidth(null),
il.getTileHeight(null),
cmSource.getNumComponents());
}
il.setSampleModel(newSM);
if (newCM != null)
il.setColorModel(newCM);
} else {
il.setColorModel(cmSource);
}
}
}
} else if(il.getSampleModel(null) == null) { // null SampleModel
// Set to whatever is available.
il.setColorModel(layout.getColorModel(im));
}
// end of block if(im != null)
} else if(il != null) {
// Can only get here if im == null && layout != null.
// Make sure that il is a clone of layout.
il = (ImageLayout)layout.clone();
// If the ColorModel is set but the SampleModel is not,
// derive a SampleModel from the ColorModel.
if(il.getColorModel(null) != null &&
il.getSampleModel(null) == null) {
// Set SampleModel dimensions.
int smWidth = il.getTileWidth(null);
if(smWidth == 0) {
smWidth = 512;
}
int smHeight = il.getTileHeight(null);
if(smHeight == 0) {
smHeight = 512;
}
// Derive a new SampleModel from the desired ColorModel
SampleModel derivedSM =
il.getColorModel(null).createCompatibleSampleModel(smWidth,
smHeight);
// Set the SampleModel to that derived from the CM.
il.setSampleModel(derivedSM);
}
} // end of block if(il != null)
// If no ColorModel is set, then first attempt to set a ColorModel
// using the ColorModelFactory; otherwise set a default ColorModel
// if either the configuration mapping is null, it does not contain
// a mapping of the key KEY_DEFAULT_COLOR_MODEL_ENABLED, or this key
// is mapped to Boolean.TRUE.
if(il != null &&
!il.isValid(ImageLayout.COLOR_MODEL_MASK) &&
il.getSampleModel(null) != null &&
!setColorModelFromFactory(il.getSampleModel(null),
sources, config, il)) {
ColorModel cm = null;
SampleModel srcSM = il.getSampleModel(null);
// If it is not a byte image, then probably all the above did not
// manage to set a ColorModel and ImageUtil.getCompatibleColorModel
// will be used to get the ColorModel. However we want to ensure
// that the destination ColorModel is expanded if the ColorModel
// of the source is an IndexColorModel and we've been asked to
// do IndexColorModel expansion
if (im != null &&
im.getColorModel() != null &&
im.getColorModel() instanceof IndexColorModel &&
config != null &&
config.containsKey(JAI.KEY_REPLACE_INDEX_COLOR_MODEL) &&
((Boolean)config.get(JAI.KEY_REPLACE_INDEX_COLOR_MODEL)).booleanValue()) {
IndexColorModel icm = (IndexColorModel)im.getColorModel();
// We need to change the ColorModel to a non IndexColorModel
// so that operations such as geometric can take place
// correctly. If the ColorModel is changed the SampleModel
// needs to be changed too, so that they are compatible
cm = PlanarImage.getDefaultColorModel(srcSM.getDataType(),
icm.getNumComponents());
SampleModel newSM;
if (cm != null) {
newSM = cm.createCompatibleSampleModel(srcSM.getWidth(),
srcSM.getHeight());
} else {
newSM = RasterFactory.createPixelInterleavedSampleModel(