// Retrieve layout information from the hints. The tile size hints
// are ignored if a SampleModel hint is supplied. Set the hints
// observed if any hints are used.
SampleModel sm = null;
ColorModel cm = null;
RenderingHints hintsObserved = null;
if(hints != null) {
// Get the ImageLayout.
ImageLayout layout = (ImageLayout)hints.get(JAI.KEY_IMAGE_LAYOUT);
if(layout != null) {
// Initialize the observed hint variables.
hintsObserved = new RenderingHints(null);
ImageLayout layoutObserved = new ImageLayout();
// Get the SampleModel.
if(layout.isValid(ImageLayout.SAMPLE_MODEL_MASK)) {
sm = layout.getSampleModel(null);
if(sm.getWidth() != tileWidth ||
sm.getHeight() != tileHeight) {
sm = sm.createCompatibleSampleModel(tileWidth,
tileHeight);
}
if(layoutObserved != null) {
layoutObserved.setSampleModel(sm);
}
}
// Get the ColorModel.
if(layout.isValid(ImageLayout.COLOR_MODEL_MASK)) {
cm = layout.getColorModel(null);
if(layoutObserved != null) {
layoutObserved.setColorModel(cm);
}
}
// Get the tile dimensions.
if(layout.isValid(ImageLayout.TILE_WIDTH_MASK)) {
tileWidth = layout.getTileWidth(null);
if(layoutObserved != null) {
layoutObserved.setTileWidth(tileWidth);
}
} else if(sm != null) {
tileWidth = sm.getWidth();
}
if(layout.isValid(ImageLayout.TILE_HEIGHT_MASK)) {
tileHeight = layout.getTileHeight(null);
if(layoutObserved != null) {
layoutObserved.setTileHeight(tileHeight);
}
} else if(sm != null) {
tileHeight = sm.getHeight();
}
// Set the observed hints layout.
hintsObserved.put(JAI.KEY_IMAGE_LAYOUT, layoutObserved);
} // layout != null
} // hints != null
// Ensure that the SampleModel is compatible with the tile size.
if(sm != null &&
(sm.getWidth() != tileWidth || sm.getHeight() != tileHeight)) {
sm = sm.createCompatibleSampleModel(tileWidth, tileHeight);
}
// Attempt to derive compatible SampleModel/ColorModel combination.
if(cm != null && (sm == null ||
!JDKWorkarounds.areCompatibleDataModels(sm, cm))) {
// If the ColorModel is non-null and the SampleModel is null
// or incompatible then create a new SampleModel.
sm = cm.createCompatibleSampleModel(tileWidth, tileHeight);
} else if(cm == null && sm != null) {
// If the ColorModel is null but the SampleModel is not,
// try to guess a reasonable ColorModel.
cm = PlanarImage.createColorModel(sm);
// If the ColorModel is still null, set it to the RGB default
// ColorModel if the latter is compatible with the SampleModel.
ColorModel cmRGB = ColorModel.getRGBdefault();
if(cm == null &&
JDKWorkarounds.areCompatibleDataModels(sm, cmRGB)) {
cm = cmRGB;
}
}