001: /*
002: * $RCSfile: TIFFCompressor.java,v $
003: *
004: *
005: * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved.
006: *
007: * Redistribution and use in source and binary forms, with or without
008: * modification, are permitted provided that the following conditions
009: * are met:
010: *
011: * - Redistribution of source code must retain the above copyright
012: * notice, this list of conditions and the following disclaimer.
013: *
014: * - Redistribution in binary form must reproduce the above copyright
015: * notice, this list of conditions and the following disclaimer in
016: * the documentation and/or other materials provided with the
017: * distribution.
018: *
019: * Neither the name of Sun Microsystems, Inc. or the names of
020: * contributors may be used to endorse or promote products derived
021: * from this software without specific prior written permission.
022: *
023: * This software is provided "AS IS," without a warranty of any
024: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
025: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
026: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027: * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
028: * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
029: * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030: * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
031: * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032: * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033: * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034: * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035: * POSSIBILITY OF SUCH DAMAGES.
036: *
037: * You acknowledge that this software is not designed or intended for
038: * use in the design, construction, operation or maintenance of any
039: * nuclear facility.
040: *
041: * $Revision: 1.1 $
042: * $Date: 2005/02/11 05:01:18 $
043: * $State: Exp $
044: */
045: package com.sun.media.imageio.plugins.tiff;
046:
047: import java.io.IOException;
048: import javax.imageio.ImageWriter;
049: import javax.imageio.metadata.IIOMetadata;
050: import javax.imageio.stream.ImageOutputStream;
051: import com.sun.media.imageioimpl.plugins.tiff.TIFFImageWriter;
052:
053: /**
054: * An abstract superclass for pluggable TIFF compressors.
055: */
056: public abstract class TIFFCompressor {
057:
058: /**
059: * The <code>ImageWriter</code> calling this
060: * <code>TIFFCompressor</code>.
061: */
062: protected ImageWriter writer;
063:
064: /**
065: * The <code>IIOMetadata</code> object containing metadata for the
066: * current image.
067: */
068: protected IIOMetadata metadata;
069:
070: /**
071: * The name of the compression type supported by this compressor.
072: */
073: protected String compressionType;
074:
075: /**
076: * The value to be assigned to the TIFF <i>Compression</i> tag in the
077: * TIFF image metadata.
078: */
079: protected int compressionTagValue;
080:
081: /**
082: * Whether the compression is lossless.
083: */
084: protected boolean isCompressionLossless;
085:
086: /**
087: * The <code>ImageOutputStream</code> to be written.
088: */
089: protected ImageOutputStream stream;
090:
091: /**
092: * Creates a compressor object for use in compressing TIFF data. This
093: * object may be passed to the
094: * {@link TIFFImageWriteParam#setTIFFCompressor(TIFFCompressor)}
095: * method to override the compressor of a supported compression type or
096: * to provide the implementation of the compression algorithm of an
097: * unsupported compression type.
098: *
099: * <p>The parameters <code>compressionTagValue</code> and
100: * <code>isCompressionLossless</code> are provided to accomodate
101: * compression types which are unknown. A compression type is
102: * "known" if it is either among those already supported by the
103: * TIFF writer (see {@link TIFFImageWriteParam}), or is listed in
104: * the TIFF 6.0 specification but not supported. If the compression
105: * type is unknown, the <code>compressionTagValue</code> and
106: * <code>isCompressionLossless</code> parameters are ignored.</p>
107: *
108: * @param compressionType The name of the compression type.
109: * @param compressionTagValue The value to be assigned to the TIFF
110: * <i>Compression</i> tag in the TIFF image metadata; ignored if
111: * <code>compressionType</code> is a known type.
112: * @param isCompressionLossless Whether the compression is lossless;
113: * ignored if <code>compressionType</code> is a known type.
114: *
115: * @throws IllegalArgumentException if <code>compressionType</code> is
116: * <code>null</code> or <code>compressionTagValue</code> is less than
117: * <code>1</code>.
118: */
119: public TIFFCompressor(String compressionType,
120: int compressionTagValue, boolean isCompressionLossless) {
121: if (compressionType == null) {
122: throw new IllegalArgumentException(
123: "compressionType == null");
124: } else if (compressionTagValue < 1) {
125: throw new IllegalArgumentException(
126: "compressionTagValue < 1");
127: }
128:
129: // Set the compression type.
130: this .compressionType = compressionType;
131:
132: // Determine whether this type is either defined in the TIFF 6.0
133: // specification or is already supported.
134: int compressionIndex = -1;
135: String[] compressionTypes = TIFFImageWriter.compressionTypes;
136: int len = compressionTypes.length;
137: for (int i = 0; i < len; i++) {
138: if (compressionTypes[i].equals(compressionType)) {
139: // Save the index of the supported type.
140: compressionIndex = i;
141: break;
142: }
143: }
144:
145: if (compressionIndex != -1) {
146: // Known compression type.
147: this .compressionTagValue = TIFFImageWriter.compressionNumbers[compressionIndex];
148: this .isCompressionLossless = TIFFImageWriter.isCompressionLossless[compressionIndex];
149: } else {
150: // Unknown compression type.
151: this .compressionTagValue = compressionTagValue;
152: this .isCompressionLossless = isCompressionLossless;
153: }
154: }
155:
156: /**
157: * Retrieve the name of the compression type supported by this compressor.
158: *
159: * @return The compression type name.
160: */
161: public String getCompressionType() {
162: return compressionType;
163: }
164:
165: /**
166: * Retrieve the value to be assigned to the TIFF <i>Compression</i> tag
167: * in the TIFF image metadata.
168: *
169: * @return The <i>Compression</i> tag value.
170: */
171: public int getCompressionTagValue() {
172: return compressionTagValue;
173: }
174:
175: /**
176: * Retrieves a value indicating whether the compression is lossless.
177: *
178: * @return Whether the compression is lossless.
179: */
180: public boolean isCompressionLossless() {
181: return isCompressionLossless;
182: }
183:
184: /**
185: * Sets the <code>ImageOutputStream</code> to be written.
186: *
187: * @param stream an <code>ImageOutputStream</code> to be written.
188: *
189: * @see #getStream
190: */
191: public void setStream(ImageOutputStream stream) {
192: this .stream = stream;
193: }
194:
195: /**
196: * Returns the <code>ImageOutputStream</code> that will be written.
197: *
198: * @return an <code>ImageOutputStream</code>.
199: *
200: * @see #setStream(ImageOutputStream)
201: */
202: public ImageOutputStream getStream() {
203: return stream;
204: }
205:
206: /**
207: * Sets the value of the <code>writer</code> field.
208: *
209: * @param writer the current <code>ImageWriter</code>.
210: *
211: * @see #getWriter()
212: */
213: public void setWriter(ImageWriter writer) {
214: this .writer = writer;
215: }
216:
217: /**
218: * Returns the current <code>ImageWriter</code>.
219: *
220: * @return an <code>ImageWriter</code>.
221: *
222: * @see #setWriter(ImageWriter)
223: */
224: public ImageWriter getWriter() {
225: return this .writer;
226: }
227:
228: /**
229: * Sets the value of the <code>metadata</code> field.
230: *
231: * @param metadata the <code>IIOMetadata</code> object for the
232: * image being written.
233: *
234: * @see #getMetadata()
235: */
236: public void setMetadata(IIOMetadata metadata) {
237: this .metadata = metadata;
238: }
239:
240: /**
241: * Returns the current <code>IIOMetadata</code> object.
242: *
243: * @return the <code>IIOMetadata</code> object for the image being
244: * written.
245: *
246: * @see #setMetadata(IIOMetadata)
247: */
248: public IIOMetadata getMetadata() {
249: return this .metadata;
250: }
251:
252: /**
253: * Encodes the supplied image data, writing to the currently set
254: * <code>ImageOutputStream</code>.
255: *
256: * @param b an array of <code>byte</code>s containing the packed
257: * but uncompressed image data.
258: * @param off the starting offset of the data to be written in the
259: * array <code>b</code>.
260: * @param width the width of the rectangle of pixels to be written.
261: * @param height the height of the rectangle of pixels to be written.
262: * @param bitsPerSample an array of <code>int</code>s indicting
263: * the number of bits used to represent each image sample within
264: * a pixel.
265: * @param scanlineStride the number of bytes separating each
266: * row of the input data.
267: *
268: * @return the number of bytes written.
269: *
270: * @throws IOException if the supplied data cannot be encoded by
271: * this <code>TIFFCompressor</code>, or if any I/O error occurs
272: * during writing.
273: */
274: public abstract int encode(byte[] b, int off, int width,
275: int height, int[] bitsPerSample, int scanlineStride)
276: throws IOException;
277:
278: }
|