Path: blob/master/src/java.desktop/share/classes/javax/imageio/plugins/jpeg/JPEGImageReadParam.java
41159 views
/*1* Copyright (c) 2000, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.plugins.jpeg;2627import javax.imageio.ImageReadParam;2829/**30* This class adds the ability to set JPEG quantization and Huffman31* tables when using the built-in JPEG reader plug-in. An instance of32* this class will be returned from the33* {@code getDefaultImageReadParam} methods of the built-in JPEG34* {@code ImageReader}.35*36* <p> The sole purpose of these additions is to allow the37* specification of tables for use in decoding abbreviated streams.38* The built-in JPEG reader will also accept an ordinary39* {@code ImageReadParam}, which is sufficient for decoding40* non-abbreviated streams.41*42* <p> While tables for abbreviated streams are often obtained by43* first reading another abbreviated stream containing only the44* tables, in some applications the tables are fixed ahead of time.45* This class allows the tables to be specified directly from client46* code. If no tables are specified either in the stream or in a47* {@code JPEGImageReadParam}, then the stream is presumed to use48* the "standard" visually lossless tables. See {@link JPEGQTable JPEGQTable}49* and {@link JPEGHuffmanTable JPEGHuffmanTable} for more information50* on the default tables.51*52* <p> The default {@code JPEGImageReadParam} returned by the53* {@code getDefaultReadParam} method of the builtin JPEG reader54* contains no tables. Default tables may be obtained from the table55* classes {@link JPEGQTable JPEGQTable} and56* {@link JPEGHuffmanTable JPEGHuffmanTable}.57*58* <p> If a stream does contain tables, the tables given in a59* {@code JPEGImageReadParam} are ignored. Furthermore, if the60* first image in a stream does contain tables and subsequent ones do61* not, then the tables given in the first image are used for all the62* abbreviated images. Once tables have been read from a stream, they63* can be overridden only by tables subsequently read from the same64* stream. In order to specify new tables, the {@link65* javax.imageio.ImageReader#setInput setInput} method of66* the reader must be called to change the stream.67*68* <p> Note that this class does not provide a means for obtaining the69* tables found in a stream. These may be extracted from a stream by70* consulting the IIOMetadata object returned by the reader.71*72* <p>73* For more information about the operation of the built-in JPEG plug-ins,74* see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG75* metadata format specification and usage notes</A>.76*77*/78public class JPEGImageReadParam extends ImageReadParam {7980private JPEGQTable[] qTables = null;81private JPEGHuffmanTable[] DCHuffmanTables = null;82private JPEGHuffmanTable[] ACHuffmanTables = null;8384/**85* Constructs a {@code JPEGImageReadParam}.86*/87public JPEGImageReadParam() {88super();89}9091/**92* Returns {@code true} if tables are currently set.93*94* @return {@code true} if tables are present.95*/96public boolean areTablesSet() {97return (qTables != null);98}99100/**101* Sets the quantization and Huffman tables to use in decoding102* abbreviated streams. There may be a maximum of 4 tables of103* each type. These tables are ignored once tables are104* encountered in the stream. All arguments must be105* non-{@code null}. The two arrays of Huffman tables must106* have the same number of elements. The table specifiers in the107* frame and scan headers in the stream are assumed to be108* equivalent to indices into these arrays. The argument arrays109* are copied by this method.110*111* @param qTables an array of quantization table objects.112* @param DCHuffmanTables an array of Huffman table objects.113* @param ACHuffmanTables an array of Huffman table objects.114*115* @exception IllegalArgumentException if any of the arguments116* is {@code null}, has more than 4 elements, or if the117* numbers of DC and AC tables differ.118*119* @see #unsetDecodeTables120*/121public void setDecodeTables(JPEGQTable[] qTables,122JPEGHuffmanTable[] DCHuffmanTables,123JPEGHuffmanTable[] ACHuffmanTables) {124if ((qTables == null) ||125(DCHuffmanTables == null) ||126(ACHuffmanTables == null) ||127(qTables.length > 4) ||128(DCHuffmanTables.length > 4) ||129(ACHuffmanTables.length > 4) ||130(DCHuffmanTables.length != ACHuffmanTables.length)) {131throw new IllegalArgumentException132("Invalid JPEG table arrays");133}134this.qTables = qTables.clone();135this.DCHuffmanTables = DCHuffmanTables.clone();136this.ACHuffmanTables = ACHuffmanTables.clone();137}138139/**140* Removes any quantization and Huffman tables that are currently141* set.142*143* @see #setDecodeTables144*/145public void unsetDecodeTables() {146this.qTables = null;147this.DCHuffmanTables = null;148this.ACHuffmanTables = null;149}150151/**152* Returns a copy of the array of quantization tables set on the153* most recent call to {@code setDecodeTables}, or154* {@code null} if tables are not currently set.155*156* @return an array of {@code JPEGQTable} objects, or157* {@code null}.158*159* @see #setDecodeTables160*/161public JPEGQTable[] getQTables() {162return (qTables != null) ? qTables.clone() : null;163}164165/**166* Returns a copy of the array of DC Huffman tables set on the167* most recent call to {@code setDecodeTables}, or168* {@code null} if tables are not currently set.169*170* @return an array of {@code JPEGHuffmanTable} objects, or171* {@code null}.172*173* @see #setDecodeTables174*/175public JPEGHuffmanTable[] getDCHuffmanTables() {176return (DCHuffmanTables != null)177? DCHuffmanTables.clone()178: null;179}180181/**182* Returns a copy of the array of AC Huffman tables set on the183* most recent call to {@code setDecodeTables}, or184* {@code null} if tables are not currently set.185*186* @return an array of {@code JPEGHuffmanTable} objects, or187* {@code null}.188*189* @see #setDecodeTables190*/191public JPEGHuffmanTable[] getACHuffmanTables() {192return (ACHuffmanTables != null)193? ACHuffmanTables.clone()194: null;195}196}197198199