001: /*
002: * $RCSfile: FileLoadRIF.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.3 $
009: * $Date: 2006/06/17 00:02:28 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import java.awt.RenderingHints;
015: import java.awt.image.RenderedImage;
016: import java.awt.image.renderable.ParameterBlock;
017: import java.awt.image.renderable.RenderedImageFactory;
018: import java.io.FileNotFoundException;
019: import java.io.InputStream;
020: import java.io.IOException;
021: import java.lang.reflect.AccessibleObject;
022: import java.lang.reflect.Method;
023: import javax.media.jai.JAI;
024: import javax.media.jai.OperationRegistry;
025: import javax.media.jai.OpImage;
026: import javax.media.jai.RenderedImageAdapter;
027: import javax.media.jai.registry.RIFRegistry;
028: import javax.media.jai.util.ImagingListener;
029: import com.sun.media.jai.codec.FileSeekableStream;
030: import com.sun.media.jai.codec.ImageDecodeParam;
031: import com.sun.media.jai.codec.SeekableStream;
032: import com.sun.media.jai.util.ImageUtil;
033:
034: /*
035: * Package-scope class which merely adds a finalize() method to close
036: * the associated stream and a dispose() method to forward the dispose()
037: * call if possible.
038: */
039: class StreamImage extends RenderedImageAdapter {
040: private InputStream stream;
041:
042: /*
043: * Create the object and cache the stream.
044: */
045: public StreamImage(RenderedImage image, InputStream stream) {
046: super (image);
047: this .stream = stream;
048: if (image instanceof OpImage) {
049: // Set the properties related to TileCache key as used in
050: // RenderedOp.
051: setProperty("tile_cache_key", image);
052: Object tileCache = ((OpImage) image).getTileCache();
053: setProperty(
054: "tile_cache",
055: tileCache == null ? java.awt.Image.UndefinedProperty
056: : tileCache);
057: }
058: }
059:
060: public void dispose() {
061: // Use relection to invoke dispose();
062: RenderedImage trueSrc = getWrappedImage();
063: Method disposeMethod = null;
064: try {
065: Class cls = trueSrc.getClass();
066: disposeMethod = cls.getMethod("dispose", null);
067: if (!disposeMethod.isAccessible()) {
068: AccessibleObject.setAccessible(
069: new AccessibleObject[] { disposeMethod }, true);
070: }
071: disposeMethod.invoke(trueSrc, null);
072: } catch (Exception e) {
073: // Ignore it.
074: }
075: }
076:
077: /*
078: * Close the stream.
079: */
080: protected void finalize() throws Throwable {
081: stream.close();
082: super .finalize();
083: }
084: }
085:
086: /**
087: * @see javax.media.jai.operator.FileDescriptor
088: *
089: * @since EA3
090: *
091: */
092: public class FileLoadRIF implements RenderedImageFactory {
093:
094: /** Constructor. */
095: public FileLoadRIF() {
096: }
097:
098: /**
099: * Creates an image from a String containing a file name.
100: */
101: public RenderedImage create(ParameterBlock args,
102: RenderingHints hints) {
103: ImagingListener listener = ImageUtil.getImagingListener(hints);
104:
105: try {
106: // Create a SeekableStream from the file name (first parameter).
107: String fileName = (String) args.getObjectParameter(0);
108:
109: SeekableStream src = null;
110: try {
111: src = new FileSeekableStream(fileName);
112: } catch (FileNotFoundException fnfe) {
113: // Try to get the file as an InputStream resource. This would
114: // happen when the application and image file are packaged in
115: // a JAR file
116: InputStream is = this .getClass().getClassLoader()
117: .getResourceAsStream(fileName);
118: if (is != null)
119: src = SeekableStream.wrapInputStream(is, true);
120: }
121:
122: ImageDecodeParam param = null;
123: if (args.getNumParameters() > 1) {
124: param = (ImageDecodeParam) args.getObjectParameter(1);
125: }
126:
127: ParameterBlock newArgs = new ParameterBlock();
128: newArgs.add(src);
129: newArgs.add(param);
130:
131: RenderingHints.Key key = JAI.KEY_OPERATION_BOUND;
132: int bound = OpImage.OP_IO_BOUND;
133: if (hints == null) {
134: hints = new RenderingHints(key, new Integer(bound));
135: } else if (!hints.containsKey(key)) {
136: hints = (RenderingHints) hints.clone();
137: hints.put(key, new Integer(bound));
138: }
139:
140: // Get the registry from the hints, if any.
141: // Don't check for null hints as it cannot be null here.
142: OperationRegistry registry = (OperationRegistry) hints
143: .get(JAI.KEY_OPERATION_REGISTRY);
144:
145: // Create the image using the most preferred RIF for "stream".
146: RenderedImage image = RIFRegistry.create(registry,
147: "stream", newArgs, hints);
148:
149: return image == null ? null : new StreamImage(image, src);
150:
151: } catch (FileNotFoundException e) {
152: String message = JaiI18N.getString("FileLoadRIF0")
153: + args.getObjectParameter(0);
154: listener.errorOccurred(message, e, this , false);
155: // e.printStackTrace();
156: return null;
157: } catch (Exception e) {
158: String message = JaiI18N.getString("FileLoadRIF1");
159: listener.errorOccurred(message, e, this , false);
160: // e.printStackTrace();
161: return null;
162: }
163: }
164: }
|