1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
/* * @(#)JPEGImageWriteParam.java 1.20 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package javax.imageio.plugins.jpeg; import java.util.Locale; import javax.imageio.ImageWriteParam; import com.sun.imageio.plugins.jpeg.JPEG; /** * This class adds the ability to set JPEG quantization and Huffman * tables when using the built-in JPEG writer plug-in, and to request that * optimized Huffman tables be computed for an image. An instance of * this class will be returned from the * <code>getDefaultImageWriteParam</code> methods of the built-in JPEG * <code>ImageWriter</code>. * <p> The principal purpose of these additions is to allow the * specification of tables to use in encoding abbreviated streams. * The built-in JPEG writer will also accept an ordinary * <code>ImageWriteParam</code>, in which case the writer will * construct the necessary tables internally. * * <p> In either case, the quality setting in an <code>ImageWriteParam</code> * has the same meaning as for the underlying library: 1.00 means a * quantization table of all 1's, 0.75 means the "standard", visually * lossless quantization table, and 0.00 means aquantization table of * all 255's. * * <p> While tables for abbreviated streams are often specified by * first writing an abbreviated stream containing only the tables, in * some applications the tables are fixed ahead of time. This class * allows the tables to be specified directly from client code. * * <p> Normally, the tables are specified in the * <code>IIOMetadata</code> objects passed in to the writer, and any * tables included in these objects are written to the stream. * If no tables are specified in the metadata, then an abbreviated * stream is written. If no tables are included in the metadata and * no tables are specified in a <code>JPEGImageWriteParam</code>, then * an abbreviated stream is encoded using the "standard" visually * lossless tables. This class is necessary for specifying tables * when an abbreviated stream must be written without writing any tables * to a stream first. In order to use this class, the metadata object * passed into the writer must contain no tables, and no stream metadata * must be provided. See {@link JPEGQTable <code>JPEGQTable</code>} and * {@link JPEGHuffmanTable <code>JPEGHuffmanTable</code>} for more * information on the default tables. * * <p> The default <code>JPEGImageWriteParam</code> returned by the * <code>getDefaultWriteParam</code> method of the writer contains no * tables. Default tables are included in the default * <code>IIOMetadata</code> objects returned by the writer. * * <p> If the metadata does contain tables, the tables given in a * <code>JPEGImageWriteParam</code> are ignored. Furthermore, once a * set of tables has been written, only tables in the metadata can * override them for subsequent writes, whether to the same stream or * a different one. In order to specify new tables using this class, * the {@link javax.imageio.ImageWriter#reset <code>reset</code>} * method of the writer must be called. * * <p> * For more information about the operation of the built-in JPEG plug-ins, * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG * metadata format specification and usage notes</A>. * * @version 0.5 */ public class JPEGImageWriteParam extends ImageWriteParam { private JPEGQTable[] qTables = null; private JPEGHuffmanTable[] DCHuffmanTables = null; private JPEGHuffmanTable[] ACHuffmanTables = null; private boolean optimizeHuffman = false; private String[] compressionNames = {"JPEG"}; private float[] qualityVals = { 0.00F, 0.30F, 0.75F, 1.00F }; private String[] qualityDescs = { "Low quality", // 0.00 -> 0.30 "Medium quality", // 0.30 -> 0.75 "Visually lossless" // 0.75 -> 1.00 }; /** * Constructs a <code>JPEGImageWriteParam</code>. Tiling is not * supported. Progressive encoding is supported. The default * progressive mode is MODE_DISABLED. A single form of compression, * named "JPEG", is supported. The default compression quality is * 0.75. * * @param locale a <code>Locale</code> to be used by the * superclass to localize compression type names and quality * descriptions, or <code>null</code>. */ public JPEGImageWriteParam(Locale locale) { super(locale); this.canWriteProgressive = true; this.progressiveMode = MODE_DISABLED; this.canWriteCompressed = true; this.compressionTypes = compressionNames; this.compressionType = compressionTypes[0]; this.compressionQuality = JPEG.DEFAULT_QUALITY; } /** * Removes any previous compression quality setting. * * <p> The default implementation resets the compression quality * to <code>0.75F</code>. * * @exception IllegalStateException if the compression mode is not * <code>MODE_EXPLICIT</code>. */ public void unsetCompression() { if (getCompressionMode() != MODE_EXPLICIT) { throw new IllegalStateException ("Compression mode not MODE_EXPLICIT!"); } this.compressionQuality = JPEG.DEFAULT_QUALITY; } /** * Returns <code>false</code> since the JPEG plug-in only supports * lossy compression. * * @return <code>false</code>. * * @exception IllegalStateException if the compression mode is not * <code>MODE_EXPLICIT</code>. */ public boolean isCompressionLossless() { if (getCompressionMode() != MODE_EXPLICIT) { throw new IllegalStateException ("Compression mode not MODE_EXPLICIT!"); } return false; } public String[] getCompressionQualityDescriptions() { if (getCompressionMode() != MODE_EXPLICIT) { throw new IllegalStateException ("Compression mode not MODE_EXPLICIT!"); } if ((getCompressionTypes() != null) && (getCompressionType() == null)) { throw new IllegalStateException("No compression type set!"); } return (String[])qualityDescs.clone(); } public float[] getCompressionQualityValues() { if (getCompressionMode() != MODE_EXPLICIT) { throw new IllegalStateException ("Compression mode not MODE_EXPLICIT!"); } if ((getCompressionTypes() != null) && (getCompressionType() == null)) { throw new IllegalStateException("No compression type set!"); } return (float[])qualityVals.clone(); } /** * Returns <code>true</code> if tables are currently set. * * @return <code>true</code> if tables are present. */ public boolean areTablesSet() { return (qTables != null); } /** * Sets the quantization and Huffman tables to use in encoding * abbreviated streams. There may be a maximum of 4 tables of * each type. These tables are ignored if tables are specified in * the metadata. All arguments must be non-<code>null</code>. * The two arrays of Huffman tables must have the same number of * elements. The table specifiers in the frame and scan headers * in the metadata are assumed to be equivalent to indices into * these arrays. The argument arrays are copied by this method. * * @param qTables An array of quantization table objects. * @param DCHuffmanTables An array of Huffman table objects. * @param ACHuffmanTables An array of Huffman table objects. * * @exception IllegalArgumentException if any of the arguments * is <code>null</code> or has more than 4 elements, or if the * numbers of DC and AC tables differ. * * @see #unsetEncodeTables */ public void setEncodeTables(JPEGQTable[] qTables, JPEGHuffmanTable[] DCHuffmanTables, JPEGHuffmanTable[] ACHuffmanTables) { if ((qTables == null) || (DCHuffmanTables == null) || (ACHuffmanTables == null) || (qTables.length > 4) || (DCHuffmanTables.length > 4) || (ACHuffmanTables.length > 4) || (DCHuffmanTables.length != ACHuffmanTables.length)) { throw new IllegalArgumentException("Invalid JPEG table arrays"); } this.qTables = (JPEGQTable[])qTables.clone(); this.DCHuffmanTables = (JPEGHuffmanTable[])DCHuffmanTables.clone(); this.ACHuffmanTables = (JPEGHuffmanTable[])ACHuffmanTables.clone(); } /** * Removes any quantization and Huffman tables that are currently * set. * * @see #setEncodeTables */ public void unsetEncodeTables() { this.qTables = null; this.DCHuffmanTables = null; this.ACHuffmanTables = null; } /** * Returns a copy of the array of quantization tables set on the * most recent call to <code>setEncodeTables</code>, or * <code>null</code> if tables are not currently set. * * @return an array of <code>JPEGQTable</code> objects, or * <code>null</code>. * * @see #setEncodeTables */ public JPEGQTable[] getQTables() { return (qTables != null) ? (JPEGQTable[])qTables.clone() : null; } /** * Returns a copy of the array of DC Huffman tables set on the * most recent call to <code>setEncodeTables</code>, or * <code>null</code> if tables are not currently set. * * @return an array of <code>JPEGHuffmanTable</code> objects, or * <code>null</code>. * * @see #setEncodeTables */ public JPEGHuffmanTable[] getDCHuffmanTables() { return (DCHuffmanTables != null) ? (JPEGHuffmanTable[])DCHuffmanTables.clone() : null; } /** * Returns a copy of the array of AC Huffman tables set on the * most recent call to <code>setEncodeTables</code>, or * <code>null</code> if tables are not currently set. * * @return an array of <code>JPEGHuffmanTable</code> objects, or * <code>null</code>. * * @see #setEncodeTables */ public JPEGHuffmanTable[] getACHuffmanTables() { return (ACHuffmanTables != null) ? (JPEGHuffmanTable[])ACHuffmanTables.clone() : null; } /** * Tells the writer to generate optimized Huffman tables * for the image as part of the writing process. The * default is <code>false</code>. If this flag is set * to <code>true</code>, it overrides any tables specified * in the metadata. Note that this means that any image * written with this flag set to <code>true</code> will * always contain Huffman tables. * * @param optimize A boolean indicating whether to generate * optimized Huffman tables when writing. * * @see #getOptimizeHuffmanTables */ public void setOptimizeHuffmanTables(boolean optimize) { optimizeHuffman = optimize; } /** * Returns the value passed into the most recent call * to <code>setOptimizeHuffmanTables</code>, or * <code>false</code> if <code>setOptimizeHuffmanTables</code> * has never been called. * * @return <code>true</code> if the writer will generate optimized * Huffman tables. * * @see #setOptimizeHuffmanTables */ public boolean getOptimizeHuffmanTables() { return optimizeHuffman; } }