001: /*
002: * @(#) $Header: /cvs/jai-operators/src/main/ca/forklabs/media/jai/RasterAdapter.java,v 1.3 2007/07/03 19:00:05 forklabs Exp $
003: *
004: * Copyright (C) 2007 Daniel Léonard
005: *
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU General Public License
008: * as published by the Free Software Foundation; either version 2
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: */
020:
021: package ca.forklabs.media.jai;
022:
023: import java.awt.Point;
024: import java.awt.image.BandedSampleModel;
025: import java.awt.image.ColorModel;
026: import java.awt.image.DataBuffer;
027: import java.awt.image.DataBufferByte;
028: import java.awt.image.DataBufferDouble;
029: import java.awt.image.DataBufferFloat;
030: import java.awt.image.DataBufferInt;
031: import java.awt.image.DataBufferShort;
032: import java.awt.image.DataBufferUShort;
033: import java.awt.image.Raster;
034: import java.awt.image.SampleModel;
035: import java.awt.image.WritableRaster;
036: import javax.media.jai.ImageLayout;
037: import javax.media.jai.PlanarImage;
038: import ca.forklabs.media.jai.Resources;
039:
040: /**
041: * Class {@code RasterAdapter} is a {@link PlanarImage} adapter over a
042: * {@link WritableRaster}. The resulting image has only one tile and the
043: * {@link ImageLayout} information, except for the {@link ColorModel} that is
044: * taken directly from the raster.
045: *
046: * @author <a href="mailto:forklabs at dev.java.net?subject=ca.forklabs.media.jai.RasterAdapter">Daniel Léonard</a>
047: * @version $Revision: 1.3 $
048: */
049: public class RasterAdapter extends PlanarImage {
050:
051: //---------------------------
052: // Instance variable
053: //---------------------------
054:
055: /** The raster. */
056: private WritableRaster writable_raster;
057:
058: //---------------------------
059: // Constructor
060: //---------------------------
061:
062: /**
063: * Constructor.
064: * @param raster the underlying raster.
065: * @exception NullPointerException if {@code raster} is {@code null}.
066: */
067: public RasterAdapter(WritableRaster raster) {
068: ImageLayout layout = this .setUpImageLayout(raster);
069: this .setImageLayout(layout);
070: this .setWritableRaster(raster);
071: }
072:
073: //---------------------------
074: // Accessor and mutator
075: //---------------------------
076:
077: /**
078: * Gets the raster.
079: * @return the raster.
080: */
081: protected WritableRaster getWritableRaster() {
082: return this .writable_raster;
083: }
084:
085: /**
086: * Changes the raster.
087: * @param raster the new raster.
088: */
089: protected void setWritableRaster(WritableRaster raster) {
090: this .writable_raster = raster;
091: }
092:
093: //---------------------------
094: // Implemented method from javax.media.jai.PlanarImage
095: //---------------------------
096:
097: /**
098: * Returns the underlying raster (as a {@link WritableRaster}). There is only
099: * one tile (0, 0).
100: * @param x the <em>x</em> position of the tile.
101: * @param y the <em>y</em> position of the tile.
102: * @return the tile.
103: * @exception IndexOutOfBoundsException if the requested tile is not
104: * <em>(0, 0)</em>.
105: */
106: @Override
107: public Raster getTile(int x, int y)
108: throws IndexOutOfBoundsException {
109: if (0 != x || 0 != y) {
110: String message = RasterAdapter.getBadTileErrorMessage(x, y);
111: throw new IndexOutOfBoundsException(message);
112: }
113: Raster raster = this .getWritableRaster();
114: return raster;
115: }
116:
117: //---------------------------
118: // Instance methods
119: //---------------------------
120:
121: /**
122: * Sets up an {@link ImageLayout} with information extracted from the given
123: * {@link Raster}.
124: * @param raster the raster behing this image.
125: * @return the new image layout.
126: */
127: protected ImageLayout setUpImageLayout(Raster raster) {
128: // TODO : set the tile properties so that there be only one tile ?
129: ImageLayout layout = new ImageLayout();
130:
131: layout.setMinX(raster.getMinX()).setMinY(raster.getMinY())
132: .setWidth(raster.getWidth()).setHeight(
133: raster.getHeight()).setSampleModel(
134: raster.getSampleModel());
135:
136: return layout;
137: }
138:
139: //---------------------------
140: // Class methods
141: //---------------------------
142:
143: /**
144: * Builds an image from a {@link DataBuffer}.
145: * @param buffer the data buffer.
146: * @param cols the number of columns.
147: * @param rows the number of rows.
148: * @return a new image.
149: */
150: public static RasterAdapter buildImage(DataBuffer buffer, int cols,
151: int rows) {
152: int type = buffer.getDataType();
153: int bands = buffer.getNumBanks();
154: SampleModel sample_model = new BandedSampleModel(type, cols,
155: rows, bands);
156: Point origin = new Point(0, 0);
157: WritableRaster raster = Raster.createWritableRaster(
158: sample_model, buffer, origin);
159: RasterAdapter image = new RasterAdapter(raster);
160: return image;
161: }
162:
163: /**
164: * Builds an image from byte data.
165: * @param pixels the pixel data.
166: * @param cols the number of columns in the image.
167: * @param rows the number of rows in the image.
168: * @return a new image.
169: * @see DataBufferByte#DataBufferByte(byte[][], int)
170: */
171: public static RasterAdapter buildByteImage(byte[][] pixels,
172: int cols, int rows) {
173: int size = cols * rows;
174: DataBuffer buffer = new DataBufferByte(pixels, size);
175: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
176: rows);
177: return image;
178: }
179:
180: /**
181: * Builds an image from short data.
182: * @param pixels the pixel data.
183: * @param cols the number of columns in the image.
184: * @param rows the number of rows in the image.
185: * @return a new image.
186: * @see DataBufferShort#DataBufferShort(short[][], int)
187: */
188: public static RasterAdapter buildShortImage(short[][] pixels,
189: int cols, int rows) {
190: int size = cols * rows;
191: DataBuffer buffer = new DataBufferShort(pixels, size);
192: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
193: rows);
194: return image;
195: }
196:
197: /**
198: * Builds an image from unsigned short data.
199: * @param pixels the pixel data.
200: * @param cols the number of columns in the image.
201: * @param rows the number of rows in the image.
202: * @return a new image.
203: * @see DataBufferUShort#DataBufferUShort(short[][], int)
204: */
205: public static RasterAdapter buildUShortImage(short[][] pixels,
206: int cols, int rows) {
207: int size = cols * rows;
208: DataBuffer buffer = new DataBufferUShort(pixels, size);
209: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
210: rows);
211: return image;
212: }
213:
214: /**
215: * Builds an image from int data.
216: * @param pixels the pixel data.
217: * @param cols the number of columns in the image.
218: * @param rows the number of rows in the image.
219: * @return a new image.
220: * @see DataBufferInt#DataBufferInt(int[][], int)
221: */
222: public static RasterAdapter buildIntImage(int[][] pixels, int cols,
223: int rows) {
224: int size = cols * rows;
225: DataBuffer buffer = new DataBufferInt(pixels, size);
226: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
227: rows);
228: return image;
229: }
230:
231: /**
232: * Builds an image from float data.
233: * @param pixels the pixel data.
234: * @param cols the number of columns in the image.
235: * @param rows the number of rows in the image.
236: * @return a new image.
237: * @see DataBufferFloat#DataBufferFloat(float[][], int)
238: */
239: public static RasterAdapter buildFloatImage(float[][] pixels,
240: int cols, int rows) {
241: int size = cols * rows;
242: DataBuffer buffer = new DataBufferFloat(pixels, size);
243: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
244: rows);
245: return image;
246: }
247:
248: /**
249: * Builds an image from double data.
250: * @param pixels the pixel data.
251: * @param cols the number of columns in the image.
252: * @param rows the number of rows in the image.
253: * @return a new image.
254: * @see DataBufferDouble#DataBufferDouble(double[][], int)
255: */
256: public static RasterAdapter buildDoubleImage(double[][] pixels,
257: int cols, int rows) {
258: int size = cols * rows;
259: DataBuffer buffer = new DataBufferDouble(pixels, size);
260: RasterAdapter image = RasterAdapter.buildImage(buffer, cols,
261: rows);
262: return image;
263: }
264:
265: /**
266: * Gets the formatted error message for a request to an inexistant tile.
267: * @param x the <em>x</em> coordinate of the tile.
268: * @param y the <em>y</em> coordinate of the tile.
269: * @return the message.
270: */
271: @SuppressWarnings("boxing")
272: protected static String getBadTileErrorMessage(int x, int y) {
273: String key = Resources.RASTER_ADAPTER_BAD_TILE;
274: String message = Resources.getLocalizedString(key, x, y);
275: return message;
276: }
277:
278: }
279:
280: /*
281: * $Log: RasterAdapter.java,v $
282: * Revision 1.3 2007/07/03 19:00:05 forklabs
283: * Added a note to investigate a better way to build the image layout.
284: *
285: * Revision 1.2 2007/06/04 21:20:12 forklabs
286: * Added new method to easily create images from memory.
287: *
288: * Revision 1.1 2007/05/03 18:31:28 forklabs
289: * Intial commit for RasterAdapter.
290: *
291: */
|