001: /*
002: * $RCSfile: SourcelessOpImage.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.1 $
009: * $Date: 2005/02/11 04:57:21 $
010: * $State: Exp $
011: */
012: package javax.media.jai;
013:
014: import java.awt.Point;
015: import java.awt.Rectangle;
016: import java.awt.image.Raster;
017: import java.awt.image.SampleModel;
018: import java.awt.image.WritableRaster;
019: import java.util.Map;
020: import javax.media.jai.util.CaselessStringKey;
021:
022: /**
023: * An abstract base class for image operators that have no image
024: * sources.
025: *
026: * <p> <code>SourcelessOpImage</code> is intended as a convenient superclass
027: * for <code>OpImage</code>s that have no source image. Some examples are
028: * constant color images, file readers, protocol-based network readers,
029: * and mathematically-defined imagery such as fractals.
030: *
031: * <p> The <code>computeTile</code> method of this class will call the
032: * <code>computeRect(PlanarImage[], WritableRaster, Rectangle)</code>
033: * method of the subclass to perform the computation. The first
034: * argument will be <code>null</code> as there are no source images.
035: *
036: * @see OpImage
037: */
038: public abstract class SourcelessOpImage extends OpImage {
039:
040: private static ImageLayout layoutHelper(int minX, int minY,
041: int width, int height, SampleModel sampleModel,
042: ImageLayout il) {
043: ImageLayout layout = (il == null) ? new ImageLayout()
044: : (ImageLayout) il.clone();
045:
046: layout.setMinX(minX);
047: layout.setMinY(minY);
048: layout.setWidth(width);
049: layout.setHeight(height);
050: layout.setSampleModel(sampleModel);
051:
052: if (!layout.isValid(ImageLayout.TILE_GRID_X_OFFSET_MASK)) {
053: layout.setTileGridXOffset(layout.getMinX(null));
054: }
055: if (!layout.isValid(ImageLayout.TILE_GRID_Y_OFFSET_MASK)) {
056: layout.setTileGridYOffset(layout.getMinY(null));
057: }
058:
059: return layout;
060: }
061:
062: /**
063: * Constructs a <code>SourcelessOpImage</code>. The image bounds
064: * and <code>SampleModel</code> are set explicitly; other layout
065: * parameters may be set using the <code>layout</code> parameter.
066: * The min X, min Y, width, height, and <code>SampleModel</code>
067: * fields of the <code>layout</code> parameter are ignored.
068: *
069: * <p> If <code>sampleModel</code> is <code>null</code>, no
070: * exceptions will be thrown. However, the caller must be sure to
071: * set the <code>sampleModel</code> instance variable before
072: * construction terminates. This feature allows subclasses that
073: * require external computation such as file loading to defer the
074: * determination of their <code>SampleModel</code> until after the
075: * call to <code>super</code>.
076: *
077: * <p> Similarly, <code>minX</code>, <code>minY</code>,
078: * <code>width</code>, and <code>height</code> may be dummy values
079: * if care is taken to manually set all values that depend on
080: * them, namely the tile grid offset, tile size, and
081: * <code>SampleModel</code> width and height.
082: *
083: * <p> The tile dimensions, tile grid X and Y offsets, and
084: * <code>ColorModel</code> of the output will be set in the standard
085: * way by the <code>OpImage</code> constructor.
086: *
087: * @param configuration Configurable attributes of the image including
088: * configuration variables indexed by
089: * <code>RenderingHints.Key</code>s and image properties indexed
090: * by <code>String</code>s or <code>CaselessStringKey</code>s.
091: * This is simply forwarded to the superclass constructor.
092: * @param layout an <code>ImageLayout</code> describing the layout.
093: *
094: * @since JAI 1.1
095: */
096: public SourcelessOpImage(ImageLayout layout, Map configuration,
097: SampleModel sampleModel, int minX, int minY, int width,
098: int height) {
099: super (null, layoutHelper(minX, minY, width, height,
100: sampleModel, layout), configuration, false);
101: }
102:
103: /**
104: * Returns false as SourcelessOpImages often return Rasters
105: * via computeTile() tile that are internally cached. Some
106: * subclasses may want to override this method and return true.
107: */
108: public boolean computesUniqueTiles() {
109: return false;
110: }
111:
112: /**
113: * Computes a tile. Since the operation has no sources,
114: * there is no need to worry about cobbling.
115: *
116: * <p> Subclasses should implement the
117: * <code>computeRect(PlanarImage[], WritableRaster, Rectangle)</code>
118: * method to perform the actual computation.
119: *
120: * @param tileX The X index of the tile.
121: * @param tileY The Y index of the tile.
122: */
123: public Raster computeTile(int tileX, int tileY) {
124: /* Create a new WritableRaster to represent this tile. */
125: Point org = new Point(tileXToX(tileX), tileYToY(tileY));
126: WritableRaster dest = createWritableRaster(sampleModel, org);
127:
128: /* Clip output rectangle to image bounds. */
129: Rectangle rect = new Rectangle(org.x, org.y, sampleModel
130: .getWidth(), sampleModel.getHeight());
131: Rectangle destRect = rect.intersection(getBounds());
132: computeRect((PlanarImage[]) null, dest, destRect);
133: return dest;
134: }
135:
136: /**
137: * Throws an IllegalArgumentException since the image has no image
138: * sources.
139: *
140: * @param sourceRect ignored.
141: * @param sourceIndex ignored.
142: *
143: * @throws IllegalArgumentException since the image has no sources.
144: */
145: public Rectangle mapSourceRect(Rectangle sourceRect, int sourceIndex) {
146: throw new IllegalArgumentException(JaiI18N
147: .getString("SourcelessOpImage0"));
148: }
149:
150: /**
151: * Throws an IllegalArgumentException since the image has no image
152: * sources.
153: *
154: * @param destRect ignored.
155: * @param sourceIndex ignored.
156: *
157: * @throws IllegalArgumentException since the image has no sources.
158: */
159: public Rectangle mapDestRect(Rectangle destRect, int sourceIndex) {
160: throw new IllegalArgumentException(JaiI18N
161: .getString("SourcelessOpImage0"));
162: }
163: }
|