001: /*
002: * $RCSfile: EncodeDescriptor.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:34 $
010: * $State: Exp $
011: */
012: package javax.media.jai.operator;
013:
014: import com.sun.media.jai.codec.ImageCodec;
015: import com.sun.media.jai.codec.ImageEncodeParam;
016: import java.awt.RenderingHints;
017: import java.awt.image.RenderedImage;
018: import java.awt.image.renderable.ParameterBlock;
019: import java.io.File;
020: import java.io.IOException;
021: import java.io.OutputStream;
022: import javax.media.jai.JAI;
023: import javax.media.jai.OperationDescriptorImpl;
024: import javax.media.jai.ParameterBlockJAI;
025: import javax.media.jai.RenderedOp;
026: import javax.media.jai.registry.RenderedRegistryMode;
027:
028: /**
029: * An <code>OperationDescriptor</code> describing the "Encode" operation.
030: *
031: * The "Encode" operation writes an image to a given <code>OutputStream</code>
032: * in a specified format using the supplied encoding parameters.
033: *
034: * <p> The third parameter contains an instance of
035: * <code>ImageEncodeParam</code> to be used during the decoding. It
036: * may be set to <code>null</code> in order to perform default
037: * encoding, or equivalently may be omitted. If
038: * non-<code>null</code>, it must be of the correct class type for the
039: * selected format.
040: *
041: * <p><b> The classes in the <code>com.sun.media.jai.codec</code>
042: * package are not a committed part of the JAI API. Future releases
043: * of JAI will make use of new classes in their place. This
044: * class will change accordingly.</b>
045: *
046: * <p><table border=1>
047: * <caption>Resource List</caption>
048: * <tr><th>Name</th> <th>Value</th></tr>
049: * <tr><td>GlobalName</td> <td>encode</td></tr>
050: * <tr><td>LocalName</td> <td>encode</td></tr>
051: * <tr><td>Vendor</td> <td>com.sun.media.jai</td></tr>
052: * <tr><td>Description</td> <td>Stores an image to an OutputStream.</td></tr>
053: * <tr><td>DocURL</td> <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/EncodeDescriptor.html</td></tr>
054: * <tr><td>Version</td> <td>1.0</td></tr>
055: * <tr><td>arg0Desc</td> <td>The OutputStream to write to.</td></tr>
056: * <tr><td>arg1Desc</td> <td>The format of the created file.</td></tr>
057: * <tr><td>arg2Desc</td> <td>The encoding parameters.</td></tr>
058: * </table></p>
059: *
060: * <p><table border=1>
061: * <caption>Parameter List</caption>
062: * <tr><th>Name</th> <th>Class Type</th>
063: * <th>Default Value</th></tr>
064: * <tr><td>stream</td> <td>java.io.OutputStream</td>
065: * <td>NO_PARAMETER_DEFAULT</td>
066: * <tr><td>format</td> <td>java.lang.String</td>
067: * <td>"tiff"</td>
068: * <tr><td>param</td> <td>com.sun.media.jai.codec.ImageEncodeParam</td>
069: * <td>null</td>
070: * </table></p>
071: *
072: * @see javax.media.jai.OperationDescriptor
073: */
074: public class EncodeDescriptor extends OperationDescriptorImpl {
075:
076: /**
077: * The resource strings that provide the general documentation and
078: * specify the parameter list for the "Encode" operation.
079: */
080: private static final String[][] resources = {
081: { "GlobalName", "Encode" },
082: { "LocalName", "Encode" },
083: { "Vendor", "com.sun.media.jai" },
084: { "Description", JaiI18N.getString("EncodeDescriptor0") },
085: {
086: "DocURL",
087: "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/EncodeDescriptor.html" },
088: { "Version", JaiI18N.getString("DescriptorVersion") },
089: { "arg0Desc", JaiI18N.getString("EncodeDescriptor1") },
090: { "arg1Desc", JaiI18N.getString("EncodeDescriptor2") },
091: { "arg2Desc", JaiI18N.getString("EncodeDescriptor3") } };
092:
093: /** The parameter names for the "Encode" operation. */
094: private static final String[] paramNames = { "stream", "format",
095: "param" };
096:
097: /** The parameter class types for the "Encode" operation. */
098: private static final Class[] paramClasses = {
099: java.io.OutputStream.class, java.lang.String.class,
100: com.sun.media.jai.codec.ImageEncodeParam.class };
101:
102: /** The parameter default values for the "Encode" operation. */
103: private static final Object[] paramDefaults = {
104: NO_PARAMETER_DEFAULT, "tiff", null };
105:
106: private static final String[] supportedModes = { "rendered" };
107:
108: /** Constructor. */
109: public EncodeDescriptor() {
110: super (resources, supportedModes, 1, paramNames, paramClasses,
111: paramDefaults, null);
112: }
113:
114: /**
115: * Validates the input source and parameters.
116: *
117: * <p> In addition to the standard checks performed by the
118: * superclass method, this method checks that the format name is
119: * recognized and is capable of encoding the source image using
120: * the encoding parameter "param", if non-<code>null</code>.
121: */
122: public boolean validateArguments(String modeName,
123: ParameterBlock args, StringBuffer msg) {
124:
125: if (!modeName.equalsIgnoreCase("rendered"))
126: return true;
127:
128: // Fool the superclass method if length < 3
129: if (args.getNumParameters() < 3) {
130: args = (ParameterBlock) args.clone();
131: args.set(null, 2);
132: }
133:
134: if (!super .validateArguments(modeName, args, msg)) {
135: return false;
136: }
137:
138: // Retrieve the format.
139: String format = (String) args.getObjectParameter(1);
140:
141: // Retrieve the associated ImageCodec.
142: ImageCodec codec = ImageCodec.getCodec(format);
143:
144: // Check for null codec.
145: if (codec == null) {
146: msg.append(getName() + " "
147: + JaiI18N.getString("EncodeDescriptor4"));
148: return false;
149: }
150:
151: // Retrieve the ImageEncodeParam object.
152: ImageEncodeParam param = (ImageEncodeParam) args
153: .getObjectParameter(2);
154:
155: RenderedImage src = args.getRenderedSource(0);
156:
157: // Verify that the image can be encoded with the given parameters.
158: if (!codec.canEncodeImage(src, param)) {
159: msg.append(getName() + " "
160: + JaiI18N.getString("EncodeDescriptor5"));
161: return false;
162: }
163:
164: return true;
165: }
166:
167: /**
168: * Returns true indicating that the operation should be rendered
169: * immediately during a call to <code>JAI.create()</code>.
170: *
171: * @see javax.media.jai.OperationDescriptor
172: */
173: public boolean isImmediate() {
174: return true;
175: }
176:
177: /**
178: * Stores an image to an OutputStream.
179: *
180: * <p>Creates a <code>ParameterBlockJAI</code> from all
181: * supplied arguments except <code>hints</code> and invokes
182: * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
183: *
184: * @see JAI
185: * @see ParameterBlockJAI
186: * @see RenderedOp
187: *
188: * @param source0 <code>RenderedImage</code> source 0.
189: * @param stream The OutputStream to write to.
190: * @param format The format of the created file.
191: * May be <code>null</code>.
192: * @param param The encoding parameters.
193: * May be <code>null</code>.
194: * @param hints The <code>RenderingHints</code> to use.
195: * May be <code>null</code>.
196: * @return The <code>RenderedOp</code> destination.
197: * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
198: * @throws IllegalArgumentException if <code>stream</code> is <code>null</code>.
199: */
200: public static RenderedOp create(RenderedImage source0,
201: OutputStream stream, String format, ImageEncodeParam param,
202: RenderingHints hints) {
203: ParameterBlockJAI pb = new ParameterBlockJAI("Encode",
204: RenderedRegistryMode.MODE_NAME);
205:
206: pb.setSource("source0", source0);
207:
208: pb.setParameter("stream", stream);
209: pb.setParameter("format", format);
210: pb.setParameter("param", param);
211:
212: return JAI.create("Encode", pb, hints);
213: }
214: }
|