001: /*
002: * $RCSfile: JPEGTileEncoderFactory.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:56:57 $
010: * $State: Exp $
011: */package com.sun.media.jai.tilecodec;
012:
013: import java.awt.image.SampleModel;
014: import java.awt.image.DataBuffer;
015: import java.io.OutputStream;
016: import java.util.Vector;
017: import javax.media.jai.JAI;
018: import javax.media.jai.ParameterListDescriptor;
019: import javax.media.jai.ParameterListDescriptorImpl;
020: import javax.media.jai.remote.NegotiableCapability;
021: import javax.media.jai.remote.NegotiableNumericRange;
022: import javax.media.jai.remote.NegotiableCollection;
023: import javax.media.jai.tilecodec.TileCodecParameterList;
024: import javax.media.jai.tilecodec.TileEncoder;
025: import javax.media.jai.tilecodec.TileEncoderFactory;
026:
027: /**
028: * A factory for creating <code>JPEGTileEncoder</code>s.
029: *
030: * <p> This class stipulates that the capabilities of the
031: * <code>TileEncoder</code> be specified by implementing the
032: * <code>getEncodingCapability()</code> method.
033: *
034: * @see javax.media.jai.remote.NegotiableCapability
035: */
036: public class JPEGTileEncoderFactory implements TileEncoderFactory {
037:
038: /**
039: * Creates a <code>TileEncoder</code> capable of encoding a
040: * <code>Raster</code> with the specified <code>SampleModel</code>
041: * using the specified <code>TileCodecParameterList</code>
042: * containing the encoding parameters to the given <code>OutputStream</code>.
043: *
044: * <p> This method can return null if the <code>TileEncoder</code> is not
045: * capable of producing output for the given set of parameters.
046: * For example, if a <code>TileEncoder</code> is only capable of dealing
047: * with a <code>PixelInterleavedSampleModel</code>, and the supplied
048: * <code>SampleModel</code> is not an instance of
049: * <code>PixelInterleavedSampleModel</code>, null should be
050: * returned. The supplied <code>SampleModel</code> should be used to
051: * decide whether it can be encoded by this class, and is not needed
052: * to actually construct a <code>TileEncoder</code>.
053: *
054: * <p> If the supplied <code>TileCodecParameterList</code> is null,
055: * a default <code>TileCodecParameterList</code> from the
056: * <code>TileCodecDescriptor</code> will be used to create the encoder.
057: *
058: * <p>Exceptions thrown by the <code>TileEncoder</code>
059: * will be caught by this method and will not be propagated.
060: *
061: * @param output The <code>OutputStream</code> to write the encoded
062: * data to.
063: * @param paramList The <code>TileCodecParameterList</code> containing
064: * the encoding parameters.
065: * @param sampleModel The <code>SampleModel</code> of the encoded
066: * <code>Raster</code>s.
067: * @throws IllegalArgumentException if output is null.
068: */
069: public TileEncoder createEncoder(OutputStream output,
070: TileCodecParameterList paramList, SampleModel sampleModel) {
071: if (output == null)
072: throw new IllegalArgumentException(JaiI18N
073: .getString("TileEncoder0"));
074: int nbands = sampleModel.getNumBands();
075: if (nbands != 1 && nbands != 3 && nbands != 4)
076: throw new IllegalArgumentException(JaiI18N
077: .getString("JPEGTileEncoder0"));
078:
079: if (sampleModel.getDataType() != DataBuffer.TYPE_BYTE)
080: throw new IllegalArgumentException(JaiI18N
081: .getString("JPEGTileEncoder1"));
082:
083: return new JPEGTileEncoder(output, paramList);
084: }
085:
086: /**
087: * Returns the capabilities of this <code>TileEncoder</code> as a
088: * <code>NegotiableCapability</code>.
089: */
090: public NegotiableCapability getEncodeCapability() {
091:
092: Vector generators = new Vector();
093: generators.add(JPEGTileEncoderFactory.class);
094:
095: ParameterListDescriptor jpegPld = JAI.getDefaultInstance()
096: .getOperationRegistry().getDescriptor("tileEncoder",
097: "jpeg").getParameterListDescriptor(
098: "tileEncoder");
099:
100: Class paramClasses[] = {
101: javax.media.jai.remote.NegotiableNumericRange.class,
102: javax.media.jai.remote.NegotiableCollection.class,
103: // XXX How should a negotiable be created to represent int arrays
104: // integer array, horizontal subsampling
105: // integer array, vertical subsampling
106: // integer array, quantization table mapping
107: // integer array, quantizationTable0
108: // integer array, quantizationTable1
109: // integer array, quantizationTable2
110: // integer array, quantizationTable3
111: javax.media.jai.remote.NegotiableNumericRange.class,
112: javax.media.jai.remote.NegotiableCollection.class,
113: javax.media.jai.remote.NegotiableCollection.class,
114: javax.media.jai.remote.NegotiableCollection.class };
115:
116: String paramNames[] = { "quality", "qualitySet",
117: "restartInterval", "writeImageInfo", "writeTableInfo",
118: "writeJFIFHeader" };
119:
120: // A collection containing the valid values for a boolean valued
121: // parameters
122: Vector v = new Vector();
123: v.add(new Boolean(true));
124: v.add(new Boolean(false));
125: NegotiableCollection negCollection = new NegotiableCollection(v);
126:
127: NegotiableNumericRange nnr1 = new NegotiableNumericRange(
128: jpegPld.getParamValueRange(paramNames[0]));
129:
130: NegotiableNumericRange nnr2 = new NegotiableNumericRange(
131: jpegPld.getParamValueRange(paramNames[2]));
132:
133: // The default values
134: Object defaults[] = { nnr1, negCollection, nnr2, negCollection,
135: negCollection, negCollection };
136:
137: NegotiableCapability encodeCap = new NegotiableCapability(
138: "tileCodec", "jpeg", generators,
139: new ParameterListDescriptorImpl(null, // descriptor
140: paramNames, paramClasses, defaults, null), // validValues
141: false); // a non-preference
142:
143: // Set the Negotiables representing the valid values on the capability
144: encodeCap.setParameter(paramNames[0], nnr1);
145: encodeCap.setParameter(paramNames[1], negCollection);
146: encodeCap.setParameter(paramNames[2], nnr2);
147: encodeCap.setParameter(paramNames[3], negCollection);
148: encodeCap.setParameter(paramNames[4], negCollection);
149: encodeCap.setParameter(paramNames[5], negCollection);
150:
151: return encodeCap;
152: }
153: }
|