1 /*
2  * $RCSfile: EncodeDescriptor.java,v $
3  *
4  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
5  *
6  * Use is subject to license terms.
7  *
8  * $Revision: 1.1 $
9  * $Date: 2005/02/11 04:57:34 $
10  * $State: Exp $
11  */
12 package com.lightcrafts.mediax.jai.operator;
13 
14 import com.lightcrafts.media.jai.codec.ImageCodec;
15 import com.lightcrafts.media.jai.codec.ImageEncodeParam;
16 import java.awt.RenderingHints;
17 import java.awt.image.RenderedImage;
18 import java.awt.image.renderable.ParameterBlock;
19 import java.io.OutputStream;
20 import com.lightcrafts.mediax.jai.JAI;
21 import com.lightcrafts.mediax.jai.OperationDescriptorImpl;
22 import com.lightcrafts.mediax.jai.ParameterBlockJAI;
23 import com.lightcrafts.mediax.jai.RenderedOp;
24 import com.lightcrafts.mediax.jai.registry.RenderedRegistryMode;
25 
26 /**
27  * An <code>OperationDescriptor</code> describing the "Encode" operation.
28  *
29  * The "Encode" operation writes an image to a given <code>OutputStream</code>
30  * in a specified format using the supplied encoding parameters.
31  *
32  * <p> The third parameter contains an instance of
33  * <code>ImageEncodeParam</code> to be used during the decoding.  It
34  * may be set to <code>null</code> in order to perform default
35  * encoding, or equivalently may be omitted.  If
36  * non-<code>null</code>, it must be of the correct class type for the
37  * selected format.
38  *
39  * <p><b> The classes in the <code>com.lightcrafts.media.jai.codec</code>
40  * package are not a committed part of the JAI API.  Future releases
41  * of JAI will make use of new classes in their place.  This
42  * class will change accordingly.</b>
43  *
44  * <p><table border=1>
45  * <caption>Resource List</caption>
46  * <tr><th>Name</th>        <th>Value</th></tr>
47  * <tr><td>GlobalName</td>  <td>encode</td></tr>
48  * <tr><td>LocalName</td>   <td>encode</td></tr>
49  * <tr><td>Vendor</td>      <td>com.lightcrafts.media.jai</td></tr>
50  * <tr><td>Description</td> <td>Stores an image to an OutputStream.</td></tr>
51  * <tr><td>DocURL</td>      <td>http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/EncodeDescriptor.html</td></tr>
52  * <tr><td>Version</td>     <td>1.0</td></tr>
53  * <tr><td>arg0Desc</td>    <td>The OutputStream to write to.</td></tr>
54  * <tr><td>arg1Desc</td>    <td>The format of the created file.</td></tr>
55  * <tr><td>arg2Desc</td>    <td>The encoding parameters.</td></tr>
56  * </table></p>
57  *
58  * <p><table border=1>
59  * <caption>Parameter List</caption>
60  * <tr><th>Name</th>          <th>Class Type</th>
61  *                            <th>Default Value</th></tr>
62  * <tr><td>stream</td>        <td>java.io.OutputStream</td>
63  *                            <td>NO_PARAMETER_DEFAULT</td>
64  * <tr><td>format</td>        <td>java.lang.String</td>
65  *                            <td>"tiff"</td>
66  * <tr><td>param</td>         <td>com.lightcrafts.media.jai.codec.ImageEncodeParam</td>
67  *                            <td>null</td>
68  * </table></p>
69  *
70  * @see com.lightcrafts.mediax.jai.OperationDescriptor
71  */
72 public class EncodeDescriptor extends OperationDescriptorImpl {
73 
74     /**
75      * The resource strings that provide the general documentation and
76      * specify the parameter list for the "Encode" operation.
77      */
78     private static final String[][] resources = {
79         {"GlobalName",  "Encode"},
80         {"LocalName",   "Encode"},
81         {"Vendor",      "com.lightcrafts.media.jai"},
82         {"Description", JaiI18N.getString("EncodeDescriptor0")},
83         {"DocURL",      "http://java.sun.com/products/java-media/jai/forDevelopers/jai-apidocs/javax/media/jai/operator/EncodeDescriptor.html"},
84         {"Version",     JaiI18N.getString("DescriptorVersion")},
85         {"arg0Desc",    JaiI18N.getString("EncodeDescriptor1")},
86         {"arg1Desc",    JaiI18N.getString("EncodeDescriptor2")},
87         {"arg2Desc",    JaiI18N.getString("EncodeDescriptor3")}
88     };
89 
90     /** The parameter names for the "Encode" operation. */
91     private static final String[] paramNames = {
92         "stream", "format", "param"
93     };
94 
95     /** The parameter class types for the "Encode" operation. */
96     private static final Class[] paramClasses = {
97         java.io.OutputStream.class,
98         java.lang.String.class,
99         com.lightcrafts.media.jai.codec.ImageEncodeParam.class
100     };
101 
102     /** The parameter default values for the "Encode" operation. */
103     private static final Object[] paramDefaults = {
104         NO_PARAMETER_DEFAULT, "tiff", null
105     };
106 
107     private static final String[] supportedModes = {
108 	"rendered"
109     };
110 
111     /** Constructor. */
EncodeDescriptor()112     public EncodeDescriptor() {
113         super(resources, supportedModes, 1,
114 		paramNames, paramClasses, paramDefaults, null);
115     }
116 
117     /**
118      * Validates the input source and parameters.
119      *
120      * <p> In addition to the standard checks performed by the
121      * superclass method, this method checks that the format name is
122      * recognized and is capable of encoding the source image using
123      * the encoding parameter "param", if non-<code>null</code>.
124      */
validateArguments(String modeName, ParameterBlock args, StringBuffer msg)125     public boolean validateArguments(String modeName,
126 				     ParameterBlock args,
127                                      StringBuffer msg) {
128 
129 	if (!modeName.equalsIgnoreCase("rendered"))
130 	    return true;
131 
132         // Fool the superclass method if length < 3
133         if (args.getNumParameters() < 3) {
134             args = (ParameterBlock)args.clone();
135             args.set(null, 2);
136         }
137 
138         if (!super.validateArguments(modeName, args, msg)) {
139             return false;
140         }
141 
142         // Retrieve the format.
143         String format = (String)args.getObjectParameter(1);
144 
145         // Retrieve the associated ImageCodec.
146         ImageCodec codec = ImageCodec.getCodec(format);
147 
148         // Check for null codec.
149         if (codec == null) {
150             msg.append(getName() + " " +
151                        JaiI18N.getString("EncodeDescriptor4"));
152             return false;
153         }
154 
155         // Retrieve the ImageEncodeParam object.
156         ImageEncodeParam param =
157             (ImageEncodeParam)args.getObjectParameter(2);
158 
159 	RenderedImage src = args.getRenderedSource(0);
160 
161         // Verify that the image can be encoded with the given parameters.
162         if (!codec.canEncodeImage(src, param)) {
163             msg.append(getName() + " " +
164                        JaiI18N.getString("EncodeDescriptor5"));
165             return false;
166         }
167 
168         return true;
169     }
170 
171     /**
172      * Returns true indicating that the operation should be rendered
173      * immediately during a call to <code>JAI.create()</code>.
174      *
175      * @see com.lightcrafts.mediax.jai.OperationDescriptor
176      */
isImmediate()177     public boolean isImmediate() {
178         return true;
179     }
180 
181 
182     /**
183      * Stores an image to an OutputStream.
184      *
185      * <p>Creates a <code>ParameterBlockJAI</code> from all
186      * supplied arguments except <code>hints</code> and invokes
187      * {@link JAI#create(String,ParameterBlock,RenderingHints)}.
188      *
189      * @see JAI
190      * @see ParameterBlockJAI
191      * @see RenderedOp
192      *
193      * @param source0 <code>RenderedImage</code> source 0.
194      * @param stream The OutputStream to write to.
195      * @param format The format of the created file.
196      * May be <code>null</code>.
197      * @param param The encoding parameters.
198      * May be <code>null</code>.
199      * @param hints The <code>RenderingHints</code> to use.
200      * May be <code>null</code>.
201      * @return The <code>RenderedOp</code> destination.
202      * @throws IllegalArgumentException if <code>source0</code> is <code>null</code>.
203      * @throws IllegalArgumentException if <code>stream</code> is <code>null</code>.
204      */
create(RenderedImage source0, OutputStream stream, String format, ImageEncodeParam param, RenderingHints hints)205     public static RenderedOp create(RenderedImage source0,
206                                     OutputStream stream,
207                                     String format,
208                                     ImageEncodeParam param,
209                                     RenderingHints hints)  {
210         ParameterBlockJAI pb =
211             new ParameterBlockJAI("Encode",
212                                   RenderedRegistryMode.MODE_NAME);
213 
214         pb.setSource("source0", source0);
215 
216         pb.setParameter("stream", stream);
217         pb.setParameter("format", format);
218         pb.setParameter("param", param);
219 
220         return JAI.create("Encode", pb, hints);
221     }
222 }
223