001: /*
002: * $RCSfile: FormatCRIF.java,v $
003: *
004: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
005: *
006: * Use is subject to license terms.
007: *
008: * $Revision: 1.2 $
009: * $Date: 2005/05/12 18:24:32 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.RenderingHints;
015: import java.awt.image.ColorModel;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.SampleModel;
018: import java.awt.image.renderable.ParameterBlock;
019: import java.util.Map;
020: import javax.media.jai.CRIFImpl;
021: import javax.media.jai.ImageLayout;
022: import javax.media.jai.JAI;
023: import javax.media.jai.OpImage;
024: import javax.media.jai.NullOpImage;
025: import javax.media.jai.RasterFactory;
026: import com.sun.media.jai.util.JDKWorkarounds;
027:
028: /**
029: * A <code>CRIF</code> supporting the "Format" operation in the rendered
030: * and renderable image layers.
031: *
032: * @see javax.media.jai.operator.FormatDescriptor
033: * @see FormatOpImage
034: *
035: *
036: * @since EA4
037: */
038: public class FormatCRIF extends CRIFImpl {
039:
040: /** Constructor. */
041: public FormatCRIF() {
042: super ("format");
043: }
044:
045: /**
046: * Creates a new instance of <code>FormatOpImage</code> in the
047: * rendered layer.
048: *
049: * @param args The source image and data type
050: * @param hints Contains destination image layout.
051: */
052: public RenderedImage create(ParameterBlock args,
053: RenderingHints renderHints) {
054:
055: // Get the source image and the data type parameter.
056: RenderedImage src = args.getRenderedSource(0);
057: Integer datatype = (Integer) args.getObjectParameter(0);
058: int type = datatype.intValue();
059:
060: // Get ImageLayout from renderHints if any.
061: ImageLayout layout = RIFUtil.getImageLayoutHint(renderHints);
062:
063: // If there is no change return the source image directly.
064: if (layout == null
065: && type == src.getSampleModel().getDataType()) {
066: return src;
067: }
068:
069: // Create or clone the ImageLayout.
070: if (layout == null) {
071: layout = new ImageLayout(src);
072: } else {
073: layout = (ImageLayout) layout.clone();
074: }
075:
076: boolean isDataTypeChange = false;
077:
078: // Get prospective destination SampleModel.
079: SampleModel sampleModel = layout.getSampleModel(src);
080:
081: // Create a new SampleModel if the type is not as desired.
082: if (sampleModel.getDataType() != type) {
083: int tileWidth = layout.getTileWidth(src);
084: int tileHeight = layout.getTileHeight(src);
085: int numBands = src.getSampleModel().getNumBands();
086:
087: SampleModel csm = RasterFactory.createComponentSampleModel(
088: sampleModel, type, tileWidth, tileHeight, numBands);
089:
090: layout.setSampleModel(csm);
091: isDataTypeChange = true;
092: }
093:
094: // Check ColorModel.
095: ColorModel colorModel = layout.getColorModel(null);
096: if (colorModel != null
097: && !JDKWorkarounds.areCompatibleDataModels(layout
098: .getSampleModel(src), colorModel)) {
099: // Clear the mask bit if incompatible.
100: layout.unsetValid(ImageLayout.COLOR_MODEL_MASK);
101: }
102:
103: // Check whether anything but the ColorModel is changing.
104: if (layout.getSampleModel(src) == src.getSampleModel()
105: && layout.getMinX(src) == src.getMinX()
106: && layout.getMinY(src) == src.getMinY()
107: && layout.getWidth(src) == src.getWidth()
108: && layout.getHeight(src) == src.getHeight()
109: && layout.getTileWidth(src) == src.getTileWidth()
110: && layout.getTileHeight(src) == src.getTileHeight()
111: && layout.getTileGridXOffset(src) == src
112: .getTileGridXOffset()
113: && layout.getTileGridYOffset(src) == src
114: .getTileGridYOffset()) {
115:
116: if (layout.getColorModel(src) == src.getColorModel()) {
117: // Nothing changed: return the source directly.
118: return src;
119: } else {
120: // Remove TileCache hint from RenderingHints if present.
121: RenderingHints hints = renderHints;
122: if (hints != null
123: && hints.containsKey(JAI.KEY_TILE_CACHE)) {
124: hints = new RenderingHints((Map) renderHints);
125: hints.remove(JAI.KEY_TILE_CACHE);
126: }
127:
128: // Only the ColorModel is changing.
129: return new NullOpImage(src, layout, hints,
130: OpImage.OP_IO_BOUND);
131: }
132: }
133:
134: if (isDataTypeChange == true) {
135:
136: // Add JAI.KEY_REPLACE_INDEX_COLOR_MODEL hint to renderHints
137: if (renderHints == null) {
138: renderHints = new RenderingHints(
139: JAI.KEY_REPLACE_INDEX_COLOR_MODEL, Boolean.TRUE);
140:
141: } else if (!renderHints
142: .containsKey(JAI.KEY_REPLACE_INDEX_COLOR_MODEL)) {
143: // If the user specified a value for this hint, we don't
144: // want to change that
145: renderHints.put(JAI.KEY_REPLACE_INDEX_COLOR_MODEL,
146: Boolean.TRUE);
147: }
148: }
149:
150: return new CopyOpImage(src, renderHints, layout);
151: }
152: }
|