1 /*
2  * $RCSfile: JPEGTileEncoderFactory.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:56:57 $
10  * $State: Exp $
11  */package com.lightcrafts.media.jai.tilecodec ;
12 
13 import java.awt.image.SampleModel ;
14 import java.awt.image.DataBuffer ;
15 import java.io.OutputStream;
16 import java.util.Vector;
17 import com.lightcrafts.mediax.jai.JAI;
18 import com.lightcrafts.mediax.jai.ParameterListDescriptor;
19 import com.lightcrafts.mediax.jai.ParameterListDescriptorImpl;
20 import com.lightcrafts.mediax.jai.remote.NegotiableCapability;
21 import com.lightcrafts.mediax.jai.remote.NegotiableNumericRange;
22 import com.lightcrafts.mediax.jai.remote.NegotiableCollection;
23 import com.lightcrafts.mediax.jai.tilecodec.TileCodecParameterList ;
24 import com.lightcrafts.mediax.jai.tilecodec.TileEncoder ;
25 import com.lightcrafts.mediax.jai.tilecodec.TileEncoderFactory ;
26 
27 /**
28  * A factory for creating <code>JPEGTileEncoder</code>s.
29  *
30  * <p> This class stipulates that the capabilities of the
31  * <code>TileEncoder</code> be specified by implementing the
32  * <code>getEncodingCapability()</code> method.
33  *
34  * @see com.lightcrafts.mediax.jai.remote.NegotiableCapability
35  */
36 public class JPEGTileEncoderFactory implements TileEncoderFactory {
37 
38     /**
39      * Creates a <code>TileEncoder</code> capable of encoding a
40      * <code>Raster</code> with the specified <code>SampleModel</code>
41      * using the specified <code>TileCodecParameterList</code>
42      * containing the encoding parameters to the given <code>OutputStream</code>.
43      *
44      * <p> This method can return null if the <code>TileEncoder</code> is not
45      * capable of producing output for the given set of parameters.
46      * For example, if a <code>TileEncoder</code> is only capable of dealing
47      * with a <code>PixelInterleavedSampleModel</code>, and the supplied
48      * <code>SampleModel</code> is not an instance of
49      * <code>PixelInterleavedSampleModel</code>, null should be
50      * returned. The supplied <code>SampleModel</code> should be used to
51      * decide whether it can be encoded by this class, and is not needed
52      * to actually construct a <code>TileEncoder</code>.
53      *
54      * <p> If the supplied <code>TileCodecParameterList</code> is null,
55      * a default <code>TileCodecParameterList</code> from the
56      * <code>TileCodecDescriptor</code> will be used to create the encoder.
57      *
58      * <p>Exceptions thrown by the <code>TileEncoder</code>
59      * will be caught by this method and will not be propagated.
60      *
61      * @param output      The <code>OutputStream</code> to write the encoded
62      *                    data to.
63      * @param paramList   The <code>TileCodecParameterList</code> containing
64      *                    the encoding parameters.
65      * @param sampleModel The <code>SampleModel</code> of the encoded
66      *                    <code>Raster</code>s.
67      * @throws IllegalArgumentException if output is null.
68      */
createEncoder(OutputStream output, TileCodecParameterList paramList, SampleModel sampleModel)69     public TileEncoder createEncoder(OutputStream output,
70 				     TileCodecParameterList paramList,
71 				     SampleModel sampleModel) {
72 	if(output == null)
73 	    throw new IllegalArgumentException(JaiI18N.getString("TileEncoder0"));
74 	int nbands = sampleModel.getNumBands() ;
75 	if(nbands != 1 && nbands != 3 && nbands != 4)
76 	    throw new IllegalArgumentException(
77 		JaiI18N.getString("JPEGTileEncoder0")) ;
78 
79 	if(sampleModel.getDataType() != DataBuffer.TYPE_BYTE)
80 	    throw new IllegalArgumentException(
81 		JaiI18N.getString("JPEGTileEncoder1")) ;
82 
83 	return new JPEGTileEncoder(output, paramList) ;
84     }
85 
86     /**
87      * Returns the capabilities of this <code>TileEncoder</code> as a
88      * <code>NegotiableCapability</code>.
89      */
getEncodeCapability()90     public NegotiableCapability getEncodeCapability() {
91 
92 	Vector generators = new Vector();
93 	generators.add(JPEGTileEncoderFactory.class);
94 
95 	ParameterListDescriptor jpegPld =
96 	    JAI.getDefaultInstance().getOperationRegistry().getDescriptor("tileEncoder", "jpeg").getParameterListDescriptor("tileEncoder");
97 
98 	Class paramClasses[] = {
99 	    com.lightcrafts.mediax.jai.remote.NegotiableNumericRange.class,
100 	    com.lightcrafts.mediax.jai.remote.NegotiableCollection.class,
101 	    // XXX How should a negotiable be created to represent int arrays
102 	    // integer array,  horizontal subsampling
103 	    // integer array,  vertical subsampling
104 	    // integer array,  quantization table mapping
105 	    // integer array,  quantizationTable0
106 	    // integer array,  quantizationTable1
107 	    // integer array,  quantizationTable2
108 	    // integer array,  quantizationTable3
109 	    com.lightcrafts.mediax.jai.remote.NegotiableNumericRange.class,
110 	    com.lightcrafts.mediax.jai.remote.NegotiableCollection.class,
111 	    com.lightcrafts.mediax.jai.remote.NegotiableCollection.class,
112 	    com.lightcrafts.mediax.jai.remote.NegotiableCollection.class
113 	};
114 
115 	String paramNames[] = {
116 	    "quality",
117 	    "qualitySet",
118 	    "restartInterval",
119 	    "writeImageInfo",
120 	    "writeTableInfo",
121 	    "writeJFIFHeader"
122 	};
123 
124 	// A collection containing the valid values for a boolean valued
125 	// parameters
126 	Vector v = new Vector();
127 	v.add(new Boolean(true));
128 	v.add(new Boolean(false));
129 	NegotiableCollection negCollection = new NegotiableCollection(v);
130 
131 	NegotiableNumericRange nnr1 =
132 	    new NegotiableNumericRange(
133 				  jpegPld.getParamValueRange(paramNames[0]));
134 
135 	NegotiableNumericRange nnr2 =
136 	    new NegotiableNumericRange(
137 				  jpegPld.getParamValueRange(paramNames[2]));
138 
139 	// The default values
140 	Object defaults[] = {
141 	    nnr1,
142 	    negCollection,
143 	    nnr2,
144 	    negCollection,
145 	    negCollection,
146 	    negCollection
147 	};
148 
149 	NegotiableCapability encodeCap =
150 	    new NegotiableCapability("tileCodec",
151 				     "jpeg",
152 				     generators,
153 				     new ParameterListDescriptorImpl(
154 							  null, // descriptor
155 							  paramNames,
156 							  paramClasses,
157 							  defaults,
158 							  null), // validValues
159 				     false); // a non-preference
160 
161 	// Set the Negotiables representing the valid values on the capability
162 	encodeCap.setParameter(paramNames[0], nnr1);
163 	encodeCap.setParameter(paramNames[1], negCollection);
164 	encodeCap.setParameter(paramNames[2], nnr2);
165 	encodeCap.setParameter(paramNames[3], negCollection);
166 	encodeCap.setParameter(paramNames[4], negCollection);
167 	encodeCap.setParameter(paramNames[5], negCollection);
168 
169 	return encodeCap;
170     }
171 }
172