001: /*
002: * $RCSfile: FileStoreRIF.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.2 $
009: * $Date: 2005/12/02 01:51:26 $
010: * $State: Exp $
011: */
012: package com.sun.media.jai.opimage;
013:
014: import com.sun.media.jai.codec.ImageEncodeParam;
015: import java.awt.RenderingHints;
016: import java.awt.image.RenderedImage;
017: import java.awt.image.renderable.ParameterBlock;
018: import java.awt.image.renderable.RenderedImageFactory;
019: import java.io.BufferedOutputStream;
020: import java.io.FileOutputStream;
021: import java.io.FileNotFoundException;
022: import java.io.RandomAccessFile;
023: import java.io.IOException;
024: import java.io.OutputStream;
025: import javax.media.jai.JAI;
026: import javax.media.jai.OperationRegistry;
027: import javax.media.jai.PlanarImage;
028: import javax.media.jai.RenderedImageAdapter;
029: import javax.media.jai.registry.RIFRegistry;
030: import javax.media.jai.util.ImagingListener;
031: import com.sun.media.jai.codec.SeekableOutputStream;
032: import com.sun.media.jai.util.ImageUtil;
033:
034: /**
035: * @see javax.media.jai.operator.FileDescriptor
036: *
037: * @since EA4
038: *
039: */
040: public class FileStoreRIF implements RenderedImageFactory {
041: /** The default file format. */
042: private static String DEFAULT_FORMAT = "tiff";
043:
044: /** Constructor. */
045: public FileStoreRIF() {
046: }
047:
048: /*
049: * Private class which merely adds a finalize() method to close
050: * the associated stream.
051: */
052: private class FileStoreImage extends RenderedImageAdapter {
053: private OutputStream stream;
054:
055: /*
056: * Create the object and cache the stream.
057: */
058: public FileStoreImage(RenderedImage image, OutputStream stream) {
059: super (image);
060: this .stream = stream;
061: }
062:
063: /*
064: * Close the stream.
065: */
066: public void dispose() {
067: try {
068: stream.close();
069: } catch (IOException e) {
070: // Ignore it ...
071: }
072: super .dispose();
073: }
074: }
075:
076: /**
077: * Stores an image to a file.
078: */
079: public RenderedImage create(ParameterBlock paramBlock,
080: RenderingHints renderHints) {
081: ImagingListener listener = ImageUtil
082: .getImagingListener(renderHints);
083:
084: // Retrieve the file path.
085: String fileName = (String) paramBlock.getObjectParameter(0);
086:
087: // Retrieve the file format preference.
088: String format = (String) paramBlock.getObjectParameter(1);
089:
090: // TODO: If format is null get format name from file extension.
091:
092: // If the format is still null use the default format.
093: if (format == null) {
094: format = DEFAULT_FORMAT;
095: }
096:
097: // Retrieve the ImageEncodeParam (which may be null).
098: ImageEncodeParam param = null;
099: if (paramBlock.getNumParameters() > 2) {
100: param = (ImageEncodeParam) paramBlock.getObjectParameter(2);
101: }
102:
103: // Create a FileOutputStream from the file name.
104: OutputStream stream = null;
105: try {
106: if (param == null) {
107: // Use a BufferedOutputStream for greater efficiency
108: // since no compression is occurring.
109: stream = new BufferedOutputStream(new FileOutputStream(
110: fileName));
111: } else {
112: // Use SeekableOutputStream to avoid temp cache file
113: // in case of compression.
114: stream = new SeekableOutputStream(new RandomAccessFile(
115: fileName, "rw"));
116: }
117: } catch (FileNotFoundException e) {
118: String message = JaiI18N.getString("FileLoadRIF0")
119: + fileName;
120: listener.errorOccurred(message, e, this , false);
121: // e.printStackTrace();
122: return null;
123: } catch (SecurityException e) {
124: String message = JaiI18N.getString("FileStoreRIF0");
125: listener.errorOccurred(message, e, this , false);
126: // e.printStackTrace();
127: return null;
128: }
129:
130: // Add the operation to the DAG.
131: ParameterBlock pb = new ParameterBlock();
132: pb.addSource(paramBlock.getSource(0));
133: pb.add(stream).add(format).add(param);
134:
135: // Get the default registry.
136: OperationRegistry registry = (renderHints == null) ? null
137: : (OperationRegistry) renderHints
138: .get(JAI.KEY_OPERATION_REGISTRY);
139:
140: PlanarImage im = new FileStoreImage(RIFRegistry.create(
141: registry, "encode", pb, renderHints), stream);
142:
143: return im;
144: }
145: }
|