001: /*
002: * $RCSfile: TileDecoderImpl.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:56 $
010: * $State: Exp $
011: */package javax.media.jai.tilecodec;
012:
013: import java.awt.Point;
014: import java.awt.image.Raster;
015: import java.awt.image.SampleModel;
016: import java.io.InputStream;
017: import java.io.IOException;
018: import javax.media.jai.JAI;
019: import javax.media.jai.ParameterListDescriptor;
020: import javax.media.jai.tilecodec.TileCodecDescriptor;
021: import com.sun.media.jai.tilecodec.TileCodecUtils;
022:
023: /**
024: * A partial implementation of the <code>TileDecoder</code> interface
025: * useful for subclassing.
026: *
027: * @since JAI 1.1
028: */
029: public abstract class TileDecoderImpl implements TileDecoder {
030:
031: /**
032: * The name of the format.
033: */
034: protected String formatName;
035:
036: /**
037: * The <code>InputStream</code> containing the encoded data to decode.
038: */
039: protected InputStream inputStream;
040:
041: /**
042: * The <code>TileCodecParameterList</code> object containing the
043: * decoding parameters.
044: */
045: protected TileCodecParameterList paramList;
046:
047: /**
048: * Constructs a <code>TileDecoderImpl</code>. An
049: * <code>IllegalArgumentException</code> will be thrown if
050: * <code>param</code>'s <code>getParameterListDescriptor()</code>
051: * method does not return the same descriptor as that from
052: * the associated <code>TileCodecDescriptor</code>'s
053: * <code>getParameterListDescriptor</code> method for the "tileDecoder"
054: * registry mode.
055: *
056: * <p> If param is null, then the default parameter list for decoding
057: * as defined by the associated <code>TileCodecDescriptor</code>'s
058: * <code>getDefaultParameters()</code> method will be used for decoding.
059: * If this too is null, an <code>IllegalArgumentException</code> will
060: * be thrown if the <code>ParameterListDescriptor</code> associated
061: * with the associated <code>TileCodecDescriptor</code> for the
062: * "tileDecoder" registry mode, reports that the number of parameters
063: * for this format is non-zero.
064: *
065: * @param formatName The name of the format.
066: * @param input The <code>InputStream</code> to decode data from.
067: * @param param The object containing the tile decoding parameters.
068: *
069: * @throws IllegalArgumentException if formatName is null.
070: * @throws IllegalArgumentException if input is null.
071: * @throws IllegalArgumentException if param's getFormatName() method does
072: * not return the same formatName as the one specified to this method.
073: * @throws IllegalArgumentException if the ParameterListDescriptor
074: * associated with the param and the associated TileCodecDescriptor are
075: * not equal.
076: * @throws IllegalArgumentException if param does not have "tileDecoder"
077: * as one of the valid modes that it supports.
078: * @throws IllegalArgumentException if the associated TileCodecDescriptor's
079: * includesSampleModelInfo() returns false and a non-null value for the
080: * "sampleModel" parameter is not supplied in the supplied parameter list.
081: */
082: public TileDecoderImpl(String formatName, InputStream input,
083: TileCodecParameterList param) {
084:
085: // Cause IllegalArgumentException to be thrown if formatName,
086: // input is null
087: if (formatName == null) {
088: throw new IllegalArgumentException(JaiI18N
089: .getString("TileCodecDescriptorImpl0"));
090: }
091:
092: if (input == null) {
093: throw new IllegalArgumentException(JaiI18N
094: .getString("TileDecoderImpl0"));
095: }
096:
097: TileCodecDescriptor tcd = TileCodecUtils
098: .getTileCodecDescriptor("tileDecoder", formatName);
099:
100: // If param is null, get the default parameter list.
101: if (param == null)
102: param = tcd.getDefaultParameters("tileDecoder");
103:
104: if (param != null) {
105:
106: // Check whether the formatName from the param is the same as the
107: // one supplied to this method.
108: if (param.getFormatName().equalsIgnoreCase(formatName) == false) {
109: throw new IllegalArgumentException(JaiI18N
110: .getString("TileDecoderImpl1"));
111: }
112:
113: // Check whether the supplied parameterList supports the
114: // "tileDecoder" mode.
115: if (param.isValidForMode("tileDecoder") == false) {
116: throw new IllegalArgumentException(JaiI18N
117: .getString("TileDecoderImpl2"));
118: }
119:
120: // Check whether the ParameterListDescriptors are the same.
121: if (param.getParameterListDescriptor().equals(
122: tcd.getParameterListDescriptor("tileDecoder")) == false)
123: throw new IllegalArgumentException(JaiI18N
124: .getString("TileCodec0"));
125:
126: SampleModel sm = null;
127:
128: // Check whether a non-null samplemodel value is needed
129: if (tcd.includesSampleModelInfo() == false) {
130: try {
131: sm = (SampleModel) param
132: .getObjectParameter("sampleModel");
133: } catch (IllegalArgumentException iae) {
134: // There is no parameter named sampleModel defined on the
135: // supplied parameter list
136: throw new IllegalArgumentException(JaiI18N
137: .getString("TileDecoderImpl3"));
138: }
139:
140: if (sm == null
141: || sm == ParameterListDescriptor.NO_PARAMETER_DEFAULT) {
142:
143: if (tcd.getParameterListDescriptor("tileDecoder")
144: .getParamDefaultValue("sampleModel") == null) {
145: // If a non-null value was not set on the parameter list
146: // and wasn't available thru the descriptor either
147: throw new IllegalArgumentException(JaiI18N
148: .getString("TileDecoderImpl4"));
149: }
150: }
151: }
152:
153: } else {
154:
155: // If the supplied parameterList is null and the default one is
156: // null too, then check whether this format supports no parameters
157: ParameterListDescriptor pld = tcd
158: .getParameterListDescriptor("tileDecoder");
159:
160: // Check whether a non-null samplemodel value is needed
161: if (tcd.includesSampleModelInfo() == false) {
162: // SampleModel must be specified via the parameter list
163: throw new IllegalArgumentException(JaiI18N
164: .getString("TileDecoderImpl5"));
165: }
166:
167: // If the PLD is not null and says that there are supposed to
168: // be some parameters (numParameters returns non-zero value)
169: // throw an IllegalArgumentException
170: if (pld != null && pld.getNumParameters() != 0) {
171: throw new IllegalArgumentException(JaiI18N
172: .getString("TileDecoderImpl6"));
173: }
174: }
175:
176: this .formatName = formatName;
177: this .inputStream = input;
178: this .paramList = param;
179: }
180:
181: /**
182: * Returns the format name.
183: */
184: public String getFormatName() {
185: return formatName;
186: }
187:
188: /**
189: * Returns the current parameters as an instance of the
190: * <code>TileCodecParameterList</code> interface.
191: */
192: public TileCodecParameterList getDecodeParameterList() {
193: return paramList;
194: }
195:
196: /**
197: * Returns the <code>InputStream</code> associated with this
198: * <code>TileDecoder</code>.
199: */
200: public InputStream getInputStream() {
201: return inputStream;
202: }
203: }
|