001: /*
002: * $RCSfile: TileEncoderImpl.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.image.Raster;
014: import java.awt.image.SampleModel;
015: import java.io.IOException;
016: import java.io.OutputStream;
017: import javax.media.jai.JAI;
018: import javax.media.jai.ParameterListDescriptor;
019: import javax.media.jai.tilecodec.TileCodecDescriptor;
020: import com.sun.media.jai.tilecodec.TileCodecUtils;
021:
022: /**
023: * A partial implementation of the <code>TileEncoder</code> interface
024: * useful for subclassing.
025: *
026: * @since JAI 1.1
027: */
028: public abstract class TileEncoderImpl implements TileEncoder {
029:
030: /**
031: * The name of the format.
032: */
033: protected String formatName;
034:
035: /**
036: * The <code>OutputStream</code> to write the encoded data to.
037: */
038: protected OutputStream outputStream;
039:
040: /**
041: * The <code>TileCodecParameterList</code> object containing the
042: * encoding parameters.
043: */
044: protected TileCodecParameterList paramList;
045:
046: /**
047: * Constructs an <code>TileEncoderImpl</code>. An
048: * <code>IllegalArgumentException</code> will be thrown if
049: * <code>param</code>'s <code>getParameterListDescriptor()</code> method
050: * does not return the same descriptor as that from the associated
051: * <code>TileCodecDescriptor</code>'s
052: * <code>getParameterListDescriptor</code> method for the "tileEncoder"
053: * registry mode.
054: *
055: * <p> If param is null, then the default parameter list for encoding
056: * as defined by the associated <code>TileCodecDescriptor</code>'s
057: * <code>getDefaultParameters()</code> method will be used for encoding.
058: * If this too is null, an <code>IllegalArgumentException</code> will
059: * be thrown if the <code>ParameterListDescriptor</code> associated
060: * with the associated <code>TileCodecDescriptor</code> for the
061: * "tileEncoder" registry mode, reports that the number of parameters
062: * for this format is non-zero.
063: *
064: * @param formatName The name of the format.
065: * @param output The <code>OutputStream</code> to write encoded data to.
066: * @param param The object containing the tile encoding parameters.
067: *
068: * @throws IllegalArgumentException if formatName is null.
069: * @throws IllegalArgumentException if output is null.
070: * @throws IllegalArgumentException if param's getFormatName() method does
071: * not return the same formatName as the one specified to this method.
072: * @throws IllegalArgumentException if the ParameterListDescriptors
073: * associated with the param and the associated TileCodecDescriptor are
074: * not equal.
075: * @throws IllegalArgumentException if param does not have "tileEncoder"
076: * as one of the valid modes that it supports.
077: */
078: public TileEncoderImpl(String formatName, OutputStream output,
079: TileCodecParameterList param) {
080:
081: // Cause a IllegalArgumentException to be thrown if formatName, output
082: // is null
083: if (formatName == null) {
084: throw new IllegalArgumentException(JaiI18N
085: .getString("TileCodecDescriptorImpl0"));
086: }
087:
088: if (output == null) {
089: throw new IllegalArgumentException(JaiI18N
090: .getString("TileEncoderImpl0"));
091: }
092:
093: TileCodecDescriptor tcd = TileCodecUtils
094: .getTileCodecDescriptor("tileEncoder", formatName);
095:
096: // If param is null, get the default parameter list.
097: if (param == null)
098: param = tcd.getDefaultParameters("tileEncoder");
099:
100: if (param != null) {
101:
102: // Check whether the formatName from the param is the same as the
103: // one supplied to this method.
104: if (param.getFormatName().equalsIgnoreCase(formatName) == false) {
105: throw new IllegalArgumentException(JaiI18N
106: .getString("TileEncoderImpl1"));
107: }
108:
109: // Check whether the supplied parameterList supports the
110: // "tileDecoder" mode.
111: if (param.isValidForMode("tileEncoder") == false) {
112: throw new IllegalArgumentException(JaiI18N
113: .getString("TileEncoderImpl2"));
114: }
115:
116: // Check whether the ParameterListDescriptors are the same.
117: if (param.getParameterListDescriptor().equals(
118: tcd.getParameterListDescriptor("tileEncoder")) == false)
119: throw new IllegalArgumentException(JaiI18N
120: .getString("TileCodec0"));
121:
122: } else {
123:
124: // If the supplied parameterList is null and the default one is
125: // null too, then check whether this format supports no parameters
126: ParameterListDescriptor pld = tcd
127: .getParameterListDescriptor("tileEncoder");
128:
129: // If the PLD is not null and says that there are supposed to
130: // be some parameters (numParameters returns non-zero value)
131: // throw an IllegalArgumentException
132: if (pld != null && pld.getNumParameters() != 0) {
133: throw new IllegalArgumentException(JaiI18N
134: .getString("TileDecoderImpl6"));
135: }
136: }
137:
138: this .formatName = formatName;
139: this .outputStream = output;
140: this .paramList = param;
141: }
142:
143: /**
144: * Returns the format name of the encoding scheme.
145: */
146: public String getFormatName() {
147: return formatName;
148: }
149:
150: /**
151: * Returns the current parameters as an instance of the
152: * <code>TileCodecParameterList</code> interface.
153: */
154: public TileCodecParameterList getEncodeParameterList() {
155: return paramList;
156: }
157:
158: /**
159: * Returns the <code>OutputStream</code> to which the encoded data will
160: * be written.
161: */
162: public OutputStream getOutputStream() {
163: return outputStream;
164: }
165:
166: }
|