001: /*
002: * $RCSfile: RectIterFactory.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:27 $
010: * $State: Exp $
011: */
012: package javax.media.jai.iterator;
013:
014: import java.awt.Rectangle;
015: import java.awt.image.ComponentSampleModel;
016: import java.awt.image.DataBuffer;
017: import java.awt.image.Raster;
018: import java.awt.image.WritableRaster;
019: import java.awt.image.RenderedImage;
020: import java.awt.image.SampleModel;
021: import java.awt.image.WritableRenderedImage;
022: import com.sun.media.jai.iterator.RectIterCSMByte; // import com.sun.media.jai.iterator.RectIterCSMShort;
023: // import com.sun.media.jai.iterator.RectIterCSMUShort;
024: // import com.sun.media.jai.iterator.RectIterCSMInt;
025: import com.sun.media.jai.iterator.RectIterCSMFloat; // import com.sun.media.jai.iterator.RectIterCSMDouble;
026: import com.sun.media.jai.iterator.RectIterFallback;
027: import com.sun.media.jai.iterator.WrapperRI;
028: import com.sun.media.jai.iterator.WrapperWRI;
029: import com.sun.media.jai.iterator.WritableRectIterCSMByte; // import com.sun.media.jai.iterator.WritableRectIterCSMShort;
030: // import com.sun.media.jai.iterator.WritableRectIterCSMUShort;
031: // import com.sun.media.jai.iterator.WritableRectIterCSMInt;
032: import com.sun.media.jai.iterator.WritableRectIterCSMFloat; // import com.sun.media.jai.iterator.WritableRectIterCSMDouble;
033: import com.sun.media.jai.iterator.WritableRectIterFallback;
034:
035: /**
036: * A factory class to instantiate instances of the RectIter and
037: * WritableRectIter interfaces on sources of type Raster,
038: * RenderedImage, and WritableRenderedImage.
039: *
040: * @see RectIter
041: * @see WritableRectIter
042: */
043: public class RectIterFactory {
044:
045: /** Prevent this class from ever being instantiated. */
046: private RectIterFactory() {
047: }
048:
049: /**
050: * Constructs and returns an instance of RectIter suitable
051: * for iterating over the given bounding rectangle within the
052: * given RenderedImage source. If the bounds parameter is null,
053: * the entire image will be used.
054: *
055: * @param im a read-only RenderedImage source.
056: * @param bounds the bounding Rectangle for the iterator, or null.
057: * @return a RectIter allowing read-only access to the source.
058: */
059: public static RectIter create(RenderedImage im, Rectangle bounds) {
060: if (bounds == null) {
061: bounds = new Rectangle(im.getMinX(), im.getMinY(), im
062: .getWidth(), im.getHeight());
063: }
064:
065: SampleModel sm = im.getSampleModel();
066: if (sm instanceof ComponentSampleModel) {
067: switch (sm.getDataType()) {
068: case DataBuffer.TYPE_BYTE:
069: return new RectIterCSMByte(im, bounds);
070: case DataBuffer.TYPE_SHORT:
071: // return new RectIterCSMShort(im, bounds);
072: break;
073: case DataBuffer.TYPE_USHORT:
074: // return new RectIterCSMUShort(im, bounds);
075: break;
076: case DataBuffer.TYPE_INT:
077: // return new RectIterCSMInt(im, bounds);
078: break;
079: case DataBuffer.TYPE_FLOAT:
080: return new RectIterCSMFloat(im, bounds);
081: case DataBuffer.TYPE_DOUBLE:
082: // return new RectIterCSMDouble(im, bounds);
083: break;
084: }
085: }
086:
087: return new RectIterFallback(im, bounds);
088: }
089:
090: /**
091: * Constructs and returns an instance of RectIter suitable
092: * for iterating over the given bounding rectangle within the
093: * given Raster source. If the bounds parameter is null,
094: * the entire Raster will be used.
095: *
096: * @param ras a read-only Raster source.
097: * @param bounds the bounding Rectangle for the iterator, or null.
098: * @return a RectIter allowing read-only access to the source.
099: */
100: public static RectIter create(Raster ras, Rectangle bounds) {
101: RenderedImage im = new WrapperRI(ras);
102: return create(im, bounds);
103: }
104:
105: /**
106: * Constructs and returns an instance of WritableRectIter suitable for
107: * iterating over the given bounding rectangle within the given
108: * WritableRenderedImage source. If the bounds parameter is null,
109: * the entire image will be used.
110: *
111: * @param im a WritableRenderedImage source.
112: * @param bounds the bounding Rectangle for the iterator, or null.
113: * @return a WritableRectIter allowing read/write access to the source.
114: */
115: public static WritableRectIter createWritable(
116: WritableRenderedImage im, Rectangle bounds) {
117: if (bounds == null) {
118: bounds = new Rectangle(im.getMinX(), im.getMinY(), im
119: .getWidth(), im.getHeight());
120: }
121:
122: SampleModel sm = im.getSampleModel();
123: if (sm instanceof ComponentSampleModel) {
124: switch (sm.getDataType()) {
125: case DataBuffer.TYPE_BYTE:
126: return new WritableRectIterCSMByte(im, bounds);
127: case DataBuffer.TYPE_SHORT:
128: // return new WritableRectIterCSMShort(im, bounds);
129: break;
130: case DataBuffer.TYPE_USHORT:
131: // return new WritableRectIterCSMUShort(im, bounds);
132: break;
133: case DataBuffer.TYPE_INT:
134: // return new WritableRectIterCSMInt(im, bounds);
135: break;
136: case DataBuffer.TYPE_FLOAT:
137: return new WritableRectIterCSMFloat(im, bounds);
138: case DataBuffer.TYPE_DOUBLE:
139: // return new WritableRectIterCSMDouble(im, bounds);
140: break;
141: }
142: }
143:
144: return new WritableRectIterFallback(im, bounds);
145: }
146:
147: /**
148: * Constructs and returns an instance of WritableRectIter suitable for
149: * iterating over the given bounding rectangle within the given
150: * WritableRaster source. If the bounds parameter is null,
151: * the entire Raster will be used.
152: *
153: * @param ras a WritableRaster source.
154: * @param bounds the bounding Rectangle for the iterator, or null.
155: * @return a WritableRectIter allowing read/write access to the source.
156: */
157: public static WritableRectIter createWritable(WritableRaster ras,
158: Rectangle bounds) {
159: WritableRenderedImage im = new WrapperWRI(ras);
160: return createWritable(im, bounds);
161: }
162: }
|