001: /*
002: * $RCSfile: RawImageReader.java,v $
003: *
004: *
005: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.1 $
042: * $Date: 2005/02/11 05:01:42 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageioimpl.plugins.raw;
046:
047: import java.awt.Point;
048: import java.awt.Rectangle;
049: import java.awt.Transparency;
050: import java.awt.color.ColorSpace;
051: import java.awt.image.BufferedImage;
052: import java.awt.image.ColorModel;
053: import java.awt.image.ComponentColorModel;
054: import java.awt.image.ComponentSampleModel;
055: import java.awt.image.DataBuffer;
056: import java.awt.image.DataBufferByte;
057: import java.awt.image.DataBufferInt;
058: import java.awt.image.DataBufferUShort;
059: import java.awt.image.IndexColorModel;
060: import java.awt.image.MultiPixelPackedSampleModel;
061: import java.awt.image.PixelInterleavedSampleModel;
062: import java.awt.image.Raster;
063: import java.awt.image.RenderedImage;
064: import java.awt.image.SampleModel;
065: import java.awt.image.WritableRaster;
066:
067: import javax.imageio.IIOException;
068: import javax.imageio.ImageReader;
069: import javax.imageio.ImageReadParam;
070: import javax.imageio.ImageTypeSpecifier;
071: import javax.imageio.metadata.IIOMetadata;
072: import javax.imageio.spi.ImageReaderSpi;
073: import javax.imageio.stream.ImageInputStream;
074:
075: import java.io.*;
076: import java.util.ArrayList;
077: import java.util.Hashtable;
078: import java.util.Iterator;
079: import java.util.StringTokenizer;
080:
081: import com.sun.media.imageio.stream.RawImageInputStream;
082:
083: /** This class is the Java Image IO plugin reader for Raw images.
084: * It may subsample the image, clip the image, select sub-bands,
085: * and shift the decoded image origin if the proper decoding parameter
086: * are set in the provided <code>PNMImageReadParam</code>.
087: */
088: public class RawImageReader extends ImageReader {
089: /** The input stream where reads from */
090: private RawImageInputStream iis = null;
091:
092: /** Wrapper for the protected method <code>computeRegions</code>. So it
093: * can be access from the classes which are not in <code>ImageReader</code>
094: * hierachy.
095: */
096: public static void computeRegionsWrapper(ImageReadParam param,
097: int srcWidth, int srcHeight, BufferedImage image,
098: Rectangle srcRegion, Rectangle destRegion) {
099: computeRegions(param, srcWidth, srcHeight, image, srcRegion,
100: destRegion);
101: }
102:
103: /** Constructs <code>RawImageReader</code> from the provided
104: * <code>ImageReaderSpi</code>.
105: */
106: public RawImageReader(ImageReaderSpi originator) {
107: super (originator);
108: }
109:
110: /** Overrides the method defined in the superclass.
111: * @throw ClassCastException If the provided <code>input</code> is not
112: * an instance of <code>RawImageInputImage</code>
113: */
114: public void setInput(Object input, boolean seekForwardOnly,
115: boolean ignoreMetadata) {
116: super .setInput(input, seekForwardOnly, ignoreMetadata);
117: iis = (RawImageInputStream) input; // Always works
118: }
119:
120: /** Overrides the method defined in the superclass. */
121: public int getNumImages(boolean allowSearch) throws IOException {
122: return iis.getNumImages();
123: }
124:
125: public int getWidth(int imageIndex) throws IOException {
126: checkIndex(imageIndex);
127: return iis.getImageDimension(imageIndex).width;
128: }
129:
130: public int getHeight(int imageIndex) throws IOException {
131: checkIndex(imageIndex);
132:
133: return iis.getImageDimension(imageIndex).height;
134: }
135:
136: public int getTileWidth(int imageIndex) throws IOException {
137: checkIndex(imageIndex);
138: return iis.getImageType().getSampleModel().getWidth();
139: }
140:
141: public int getTileHeight(int imageIndex) throws IOException {
142: checkIndex(imageIndex);
143: return iis.getImageType().getSampleModel().getHeight();
144: }
145:
146: private void checkIndex(int imageIndex) throws IOException {
147: if (imageIndex < 0 || imageIndex >= getNumImages(true)) {
148: throw new IndexOutOfBoundsException(I18N
149: .getString("RawImageReader0"));
150: }
151: }
152:
153: public Iterator getImageTypes(int imageIndex) throws IOException {
154: checkIndex(imageIndex);
155: ArrayList list = new ArrayList(1);
156: list.add(iis.getImageType());
157: return list.iterator();
158: }
159:
160: public ImageReadParam getDefaultReadParam() {
161: return new ImageReadParam();
162: }
163:
164: public IIOMetadata getImageMetadata(int imageIndex)
165: throws IOException {
166: return null;
167: }
168:
169: public IIOMetadata getStreamMetadata() throws IOException {
170: return null;
171: }
172:
173: public boolean isRandomAccessEasy(int imageIndex)
174: throws IOException {
175: checkIndex(imageIndex);
176: return true;
177: }
178:
179: public BufferedImage read(int imageIndex, ImageReadParam param)
180: throws IOException {
181: if (param == null)
182: param = getDefaultReadParam();
183: checkIndex(imageIndex);
184: clearAbortRequest();
185: processImageStarted(imageIndex);
186:
187: BufferedImage bi = param.getDestination();
188: RawRenderedImage image = new RawRenderedImage(iis, this , param,
189: imageIndex);
190: Point offset = param.getDestinationOffset();
191: WritableRaster raster;
192:
193: if (bi == null) {
194: ColorModel colorModel = image.getColorModel();
195: SampleModel sampleModel = image.getSampleModel();
196:
197: // If the destination type is specified, use the color model of it.
198: ImageTypeSpecifier type = param.getDestinationType();
199: if (type != null)
200: colorModel = type.getColorModel();
201:
202: raster = Raster.createWritableRaster(sampleModel
203: .createCompatibleSampleModel(image.getMinX()
204: + image.getWidth(), image.getMinY()
205: + image.getHeight()), new Point(0, 0));
206:
207: bi = new BufferedImage(colorModel, raster,
208: colorModel != null ? colorModel
209: .isAlphaPremultiplied() : false,
210: new Hashtable());
211: } else {
212: raster = bi.getWritableTile(0, 0);
213: }
214:
215: image.setDestImage(bi);
216:
217: image.readAsRaster(raster);
218: image.clearDestImage();
219:
220: if (abortRequested())
221: processReadAborted();
222: else
223: processImageComplete();
224: return bi;
225: }
226:
227: public RenderedImage readAsRenderedImage(int imageIndex,
228: ImageReadParam param) throws java.io.IOException {
229: if (param == null)
230: param = getDefaultReadParam();
231:
232: checkIndex(imageIndex);
233: clearAbortRequest();
234: processImageStarted(0);
235:
236: RenderedImage image = new RawRenderedImage(iis, this , param,
237: imageIndex);
238:
239: if (abortRequested())
240: processReadAborted();
241: else
242: processImageComplete();
243: return image;
244: }
245:
246: public Raster readRaster(int imageIndex, ImageReadParam param)
247: throws IOException {
248: BufferedImage bi = read(imageIndex, param);
249: return bi.getData();
250: }
251:
252: public boolean canReadRaster() {
253: return true;
254: }
255:
256: public void reset() {
257: super .reset();
258: iis = null;
259: }
260:
261: /** Wrapper for the protected method <code>processImageUpdate</code>
262: * So it can be access from the classes which are not in
263: * <code>ImageReader</code> hierachy.
264: */
265: public void processImageUpdateWrapper(BufferedImage theImage,
266: int minX, int minY, int width, int height, int periodX,
267: int periodY, int[] bands) {
268: processImageUpdate(theImage, minX, minY, width, height,
269: periodX, periodY, bands);
270: }
271:
272: /** Wrapper for the protected method <code>processImageProgress</code>
273: * So it can be access from the classes which are not in
274: * <code>ImageReader</code> hierachy.
275: */
276: public void processImageProgressWrapper(float percentageDone) {
277: processImageProgress(percentageDone);
278: }
279:
280: /** This method wraps the protected method <code>abortRequested</code>
281: * to allow the abortions be monitored by <code>J2KReadState</code>.
282: */
283: public boolean getAbortRequest() {
284: return abortRequested();
285: }
286: }
|