Merge from mainline.
[official-gcc.git] / libjava / classpath / javax / imageio / plugins / jpeg / JPEGImageWriteParam.java
blob1e7989e82dbcfac4f0b3bbc7cd6cf4221a02bbb9
1 /* JPEGImageWriteParam.java --
2 Copyright (C) 2006 Free Software Foundation, Inc.
4 This file is part of GNU Classpath.
6 GNU Classpath is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Classpath is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Classpath; see the file COPYING. If not, write to the
18 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301 USA.
21 Linking this library statically or dynamically with other modules is
22 making a combined work based on this library. Thus, the terms and
23 conditions of the GNU General Public License cover the whole
24 combination.
26 As a special exception, the copyright holders of this library give you
27 permission to link this library with independent modules to produce an
28 executable, regardless of the license terms of these independent
29 modules, and to copy and distribute the resulting executable under
30 terms of your choice, provided that you also meet, for each linked
31 independent module, the terms and conditions of the license of that
32 module. An independent module is a module which is not derived from
33 or based on this library. If you modify this library, you may extend
34 this exception to your version of the library, but you are not
35 obligated to do so. If you do not wish to do so, delete this
36 exception statement from your version. */
39 package javax.imageio.plugins.jpeg;
41 import java.util.Locale;
42 import java.util.PropertyResourceBundle;
43 import java.util.ResourceBundle;
44 import javax.imageio.ImageWriteParam;
46 /**
47 * The JPEGImageWriteParam class can be used to specify tables and
48 * settings used in the JPEG encoding process. Encoding tables are
49 * taken from the metadata associated with the output stream, and
50 * failing that (if the metadata tables are null) from an instance of
51 * JPEGImageWriteParam. The default metadata uses the standard JPEG
52 * tables from the JPEGQTable and JPEGHuffmanTable classes. Non-null
53 * metadata tables override JPEGImageWriteParam tables. Compression
54 * settings range from 1.0, best compression, through 0.75, default
55 * compression, to 0.0, worst compression.
57 * A JPEGImageWriteParam instance is retrieved from the built-in JPEG
58 * ImageWriter using the getDefaultImageWriteParam method.
60 public class JPEGImageWriteParam
61 extends ImageWriteParam
63 private JPEGQTable[] qTables;
64 private JPEGHuffmanTable[] DCHuffmanTables;
65 private JPEGHuffmanTable[] ACHuffmanTables;
66 private boolean optimize;
67 private String[] compressionQualityDescriptions;
68 private float[] compressionQualityValues;
70 /**
71 * Localized messages are stored in separate files.
73 private ResourceBundle messages;
75 /**
76 * Construct a JPEGImageWriteParam with the following state: tiling
77 * is not supported, progressive mode is supported, initial
78 * progressive mode is MODE_DISABLED, compression is supported, one
79 * compression type named "JPEG" is supported and the default
80 * compression quality is 0.75f. Compression type names and
81 * compression quality descriptions are localized to the given
82 * locale.
84 * @param locale the locale used for message localization
86 public JPEGImageWriteParam(Locale locale)
88 super(locale);
90 // Get localized compression type and compression quality
91 // description strings for the given locale.
92 messages = PropertyResourceBundle.getBundle
93 ("javax/imageio/plugins/jpeg/MessagesBundle", locale);
95 // Initialize inherited ImageWriter fields.
96 canWriteTiles = false;
97 canWriteProgressive = true;
98 progressiveMode = MODE_DISABLED;
99 canWriteCompressed = true;
100 compressionTypes = new String[]
102 messages.getString("compression.types.jpeg")
104 compressionType = compressionTypes[0];
105 compressionQuality = 0.75f;
109 * Reset the compression quality to 0.75f.
111 public void unsetCompression()
113 compressionQuality = 0.75f;
117 * Check if compression algorithm is lossless. JPEGImageWriteParam
118 * overrides this ImageWriteParam method to always return false
119 * since JPEG compression is inherently lossy.
121 * @return false
123 public boolean isCompressionLossless()
125 return false;
129 * Retrieve an array of compression quality descriptions. These
130 * messages are localized using the locale provided upon
131 * construction. Each compression quality description in the
132 * returned array corresponds to the compression quality value at
133 * the same index in the array returned by
134 * getCompressionQualityValues.
136 * @return an array of strings each of which describes a compression
137 * quality value
139 public String[] getCompressionQualityDescriptions()
141 // Make sure exceptions are thrown when this image write param is
142 // in the wrong state.
143 super.getCompressionQualityDescriptions();
145 if (compressionQualityDescriptions == null)
147 compressionQualityDescriptions = new String[]
149 messages.getString("compression.minimum"),
150 messages.getString("compression.default"),
151 messages.getString("compression.maximum")
155 return compressionQualityDescriptions;
159 * Retrieve an array of compression quality values, ordered from
160 * lowest quality to highest quality.
162 * @return an array of compressions quality values
164 public float[] getCompressionQualityValues()
166 // Make sure exceptions are thrown when this image write param is
167 // in the wrong state.
168 super.getCompressionQualityValues();
170 if (compressionQualityValues == null)
171 compressionQualityValues = new float[] { 0.05f, 0.75f, 0.95f };
173 return compressionQualityValues;
177 * Check if the encoding tables are set.
179 * @return true if the encoding tables are set, false otherwise
181 public boolean areTablesSet()
183 // If qTables is not null then all tables are set.
184 return (qTables != null);
188 * Set the quantization and Huffman tables that will be used to
189 * encode the stream. Copies are created of the array arguments.
190 * The number of Huffman tables must be the same in both Huffman
191 * table arrays. No argument may be null and no array may be longer
192 * than four elements.
194 * @param qTables JPEG quantization tables
195 * @param DCHuffmanTables JPEG DC Huffman tables
196 * @param ACHuffmanTables JPEG AC Huffman tables
198 * @throws IllegalArgumentException if any argument is null, if any
199 * of the arrays are longer than four elements, or if the Huffman
200 * table arrays do not have the same number of elements
202 public void setEncodeTables(JPEGQTable[] qTables,
203 JPEGHuffmanTable[] DCHuffmanTables,
204 JPEGHuffmanTable[] ACHuffmanTables)
206 if (qTables == null || DCHuffmanTables == null || ACHuffmanTables == null)
207 throw new IllegalArgumentException("null argument");
209 if (qTables.length > 4 || DCHuffmanTables.length > 4
210 || ACHuffmanTables.length > 4)
211 throw new IllegalArgumentException("argument has too many elements");
213 if (DCHuffmanTables.length != ACHuffmanTables.length)
214 throw new IllegalArgumentException("Huffman table arrays differ in length");
216 // Do a shallow copy. JPEGQTable's data is not directly
217 // modifyable since JPEGQTable.getTable returns a copy. Therefore
218 // it is safe to have multiple references to a single JPEGQTable.
219 // Likewise for JPEGHuffmanTable.
220 this.qTables = (JPEGQTable[]) qTables.clone();
221 this.DCHuffmanTables = (JPEGHuffmanTable[]) DCHuffmanTables.clone();
222 this.ACHuffmanTables = (JPEGHuffmanTable[]) ACHuffmanTables.clone();
226 * Clear the quantization and Huffman encoding tables.
228 public void unsetEncodeTables()
230 qTables = null;
231 DCHuffmanTables = null;
232 ACHuffmanTables = null;
236 * Retrieve the quantization tables.
238 * @returns an array of JPEG quantization tables
240 public JPEGQTable[] getQTables()
242 return qTables == null ? qTables : (JPEGQTable[]) qTables.clone();
246 * Retrieve the DC Huffman tables.
248 * @return an array of JPEG DC Huffman tables
250 public JPEGHuffmanTable[] getDCHuffmanTables()
252 return DCHuffmanTables == null ? DCHuffmanTables
253 : (JPEGHuffmanTable[]) DCHuffmanTables.clone();
257 * Retrieve the AC Huffman tables.
259 * @return an array of JPEG AC Huffman tables
261 public JPEGHuffmanTable[] getACHuffmanTables()
263 return ACHuffmanTables == null ? ACHuffmanTables
264 : (JPEGHuffmanTable[]) ACHuffmanTables.clone();
268 * Specify whether or not Huffman tables written to the output
269 * stream should be optimized. Every image encoded with this flag
270 * set will contain a Huffman table, and the generated Huffman
271 * tables will override those specified in the metadata.
273 * @param optimize true to generate optimized Huffman tables, false
274 * otherwise
276 public void setOptimizeHuffmanTables(boolean optimize)
278 this.optimize = optimize;
282 * Check whether or not Huffman tables written to the output stream
283 * will be optimized. Unless otherwise set using
284 * setOptimizeHuffmanTables, this returns false.
286 * @return true Huffman tables written to the output stream will be
287 * optimized, false otherwise
289 public boolean getOptimizeHuffmanTables()
291 return optimize;