1/*
2 * Copyright (c) 2005, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25package com.sun.imageio.plugins.tiff;
26
27import java.io.IOException;
28import javax.imageio.ImageWriter;
29import javax.imageio.metadata.IIOMetadata;
30import javax.imageio.stream.ImageOutputStream;
31
32/**
33 * An abstract superclass for pluggable TIFF compressors.
34 */
35public abstract class TIFFCompressor {
36
37    /**
38     * The {@code ImageWriter} calling this
39     * {@code TIFFCompressor}.
40     */
41    protected ImageWriter writer;
42
43    /**
44     * The {@code IIOMetadata} object containing metadata for the
45     * current image.
46     */
47    protected IIOMetadata metadata;
48
49    /**
50     * The name of the compression type supported by this compressor.
51     */
52    protected String compressionType;
53
54    /**
55     * The value to be assigned to the TIFF <i>Compression</i> tag in the
56     * TIFF image metadata.
57     */
58    protected int compressionTagValue;
59
60    /**
61     * Whether the compression is lossless.
62     */
63    protected boolean isCompressionLossless;
64
65    /**
66     * The {@code ImageOutputStream} to be written.
67     */
68    protected ImageOutputStream stream;
69
70    /**
71     * Creates a compressor object for use in compressing TIFF data. This
72     * object may be passed to the
73     * {@link TIFFImageWriteParam#setTIFFCompressor(TIFFCompressor)}
74     * method to override the compressor of a supported compression type or
75     * to provide the implementation of the compression algorithm of an
76     * unsupported compression type.
77     *
78     * <p>The parameters {@code compressionTagValue} and
79     * {@code isCompressionLossless} are provided to accomodate
80     * compression types which are unknown. A compression type is
81     * "known" if it is either among those already supported by the
82     * TIFF writer (see {@link TIFFImageWriteParam}), or is listed in
83     * the TIFF 6.0 specification but not supported. If the compression
84     * type is unknown, the {@code compressionTagValue} and
85     * {@code isCompressionLossless} parameters are ignored.</p>
86     *
87     * @param compressionType The name of the compression type.
88     * @param compressionTagValue The value to be assigned to the TIFF
89     * <i>Compression</i> tag in the TIFF image metadata; ignored if
90     * {@code compressionType} is a known type.
91     * @param isCompressionLossless Whether the compression is lossless;
92     * ignored if {@code compressionType} is a known type.
93     *
94     * @throws NullPointerException if {@code compressionType} is
95     * {@code null}.
96     * @throws IllegalArgumentException if {@code compressionTagValue} is
97     * less {@code 1}.
98     */
99    public TIFFCompressor(String compressionType,
100                          int compressionTagValue,
101                          boolean isCompressionLossless) {
102        if(compressionType == null) {
103            throw new NullPointerException("compressionType == null");
104        } else if(compressionTagValue < 1) {
105            throw new IllegalArgumentException("compressionTagValue < 1");
106        }
107
108        // Set the compression type.
109        this.compressionType = compressionType;
110
111        // Determine whether this type is either defined in the TIFF 6.0
112        // specification or is already supported.
113        int compressionIndex = -1;
114        String[] compressionTypes = TIFFImageWriter.compressionTypes;
115        int len = compressionTypes.length;
116        for(int i = 0; i < len; i++) {
117            if(compressionTypes[i].equals(compressionType)) {
118                // Save the index of the supported type.
119                compressionIndex = i;
120                break;
121            }
122        }
123
124        if(compressionIndex != -1) {
125            // Known compression type.
126            this.compressionTagValue =
127                TIFFImageWriter.compressionNumbers[compressionIndex];
128            this.isCompressionLossless =
129                TIFFImageWriter.isCompressionLossless[compressionIndex];
130        } else {
131            // Unknown compression type.
132            this.compressionTagValue = compressionTagValue;
133            this.isCompressionLossless = isCompressionLossless;
134        }
135    }
136
137    /**
138     * Retrieve the name of the compression type supported by this compressor.
139     *
140     * @return The compression type name.
141     */
142    public String getCompressionType() {
143        return compressionType;
144    }
145
146    /**
147     * Retrieve the value to be assigned to the TIFF <i>Compression</i> tag
148     * in the TIFF image metadata.
149     *
150     * @return The <i>Compression</i> tag value.
151     */
152    public int getCompressionTagValue() {
153        return compressionTagValue;
154    }
155
156    /**
157     * Retrieves a value indicating whether the compression is lossless.
158     *
159     * @return Whether the compression is lossless.
160     */
161    public boolean isCompressionLossless() {
162        return isCompressionLossless;
163    }
164
165    /**
166     * Sets the {@code ImageOutputStream} to be written.
167     *
168     * @param stream an {@code ImageOutputStream} to be written.
169     *
170     * @see #getStream
171     */
172    public void setStream(ImageOutputStream stream) {
173        this.stream = stream;
174    }
175
176    /**
177     * Returns the {@code ImageOutputStream} that will be written.
178     *
179     * @return an {@code ImageOutputStream}.
180     *
181     * @see #setStream(ImageOutputStream)
182     */
183    public ImageOutputStream getStream() {
184        return stream;
185    }
186
187    /**
188     * Sets the value of the {@code writer} field.
189     *
190     * @param writer the current {@code ImageWriter}.
191     *
192     * @see #getWriter()
193     */
194    public void setWriter(ImageWriter writer) {
195        this.writer = writer;
196    }
197
198    /**
199     * Returns the current {@code ImageWriter}.
200     *
201     * @return an {@code ImageWriter}.
202     *
203     * @see #setWriter(ImageWriter)
204     */
205    public ImageWriter getWriter() {
206        return this.writer;
207    }
208
209    /**
210     * Sets the value of the {@code metadata} field.
211     *
212     * @param metadata the {@code IIOMetadata} object for the
213     * image being written.
214     *
215     * @see #getMetadata()
216     */
217    public void setMetadata(IIOMetadata metadata) {
218        this.metadata = metadata;
219    }
220
221    /**
222     * Returns the current {@code IIOMetadata} object.
223     *
224     * @return the {@code IIOMetadata} object for the image being
225     * written.
226     *
227     * @see #setMetadata(IIOMetadata)
228     */
229    public IIOMetadata getMetadata() {
230        return this.metadata;
231    }
232
233    /**
234     * Encodes the supplied image data, writing to the currently set
235     * {@code ImageOutputStream}.
236     *
237     * @param b an array of {@code byte}s containing the packed
238     * but uncompressed image data.
239     * @param off the starting offset of the data to be written in the
240     * array {@code b}.
241     * @param width the width of the rectangle of pixels to be written.
242     * @param height the height of the rectangle of pixels to be written.
243     * @param bitsPerSample an array of {@code int}s indicting
244     * the number of bits used to represent each image sample within
245     * a pixel.
246     * @param scanlineStride the number of bytes separating each
247     * row of the input data.
248     *
249     * @return the number of bytes written.
250     *
251     * @throws IOException if the supplied data cannot be encoded by
252     * this {@code TIFFCompressor}, or if any I/O error occurs
253     * during writing.
254     */
255    public abstract int encode(byte[] b, int off,
256                               int width, int height,
257                               int[] bitsPerSample,
258                               int scanlineStride) throws IOException;
259
260}
261