KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > imageio > plugins > jpeg > JPEGImageReadParam


1 /*
2  * @(#)JPEGImageReadParam.java 1.11 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package javax.imageio.plugins.jpeg;
9
10 import javax.imageio.ImageReadParam JavaDoc;
11
12 /**
13  * This class adds the ability to set JPEG quantization and Huffman
14  * tables when using the built-in JPEG reader plug-in. An instance of
15  * this class will be returned from the
16  * <code>getDefaultImageReadParam</code> methods of the built-in JPEG
17  * <code>ImageReader</code>.
18  *
19  * <p> The sole purpose of these additions is to allow the
20  * specification of tables for use in decoding abbreviated streams.
21  * The built-in JPEG reader will also accept an ordinary
22  * <code>ImageReadParam</code>, which is sufficient for decoding
23  * non-abbreviated streams.
24  *
25  * <p> While tables for abbreviated streams are often obtained by
26  * first reading another abbreviated stream containing only the
27  * tables, in some applications the tables are fixed ahead of time.
28  * This class allows the tables to be specified directly from client
29  * code. If no tables are specified either in the stream or in a
30  * <code>JPEGImageReadParam</code>, then the stream is presumed to use
31  * the "standard" visually lossless tables. See {@link JPEGQTable
32  * <code>JPEGQTable</code>} and {@link JPEGHuffmanTable
33  * <code>JPEGHuffmanTable</code>} for more information on the default
34  * tables.
35  *
36  * <p> The default <code>JPEGImageReadParam</code> returned by the
37  * <code>getDefaultReadParam</code> method of the builtin JPEG reader
38  * contains no tables. Default tables may be obtained from the table
39  * classes {@link JPEGQTable <code>JPEGQTable</code>} and {@link
40  * JPEGHuffmanTable <code>JPEGHuffmanTable</code>}.
41  *
42  * <p> If a stream does contain tables, the tables given in a
43  * <code>JPEGImageReadParam</code> are ignored. Furthermore, if the
44  * first image in a stream does contain tables and subsequent ones do
45  * not, then the tables given in the first image are used for all the
46  * abbreviated images. Once tables have been read from a stream, they
47  * can be overridden only by tables subsequently read from the same
48  * stream. In order to specify new tables, the {@link
49  * javax.imageio.ImageReader#setInput <code>setInput</code>} method of
50  * the reader must be called to change the stream.
51  *
52  * <p> Note that this class does not provide a means for obtaining the
53  * tables found in a stream. These may be extracted from a stream by
54  * consulting the <code>IIOMetadata</code> object returned by the
55  * reader.
56  *
57  * <p>
58  * For more information about the operation of the built-in JPEG plug-ins,
59  * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
60  * metadata format specification and usage notes</A>.
61  *
62  * @version 0.5
63  */

64 public class JPEGImageReadParam extends ImageReadParam JavaDoc {
65
66     private JPEGQTable JavaDoc[] qTables = null;
67     private JPEGHuffmanTable JavaDoc[] DCHuffmanTables = null;
68     private JPEGHuffmanTable JavaDoc[] ACHuffmanTables = null;
69
70     /**
71      * Constructs a <code>JPEGImageReadParam</code>.
72      */

73     public JPEGImageReadParam() {
74         super();
75     }
76     
77     /**
78      * Returns <code>true</code> if tables are currently set.
79      *
80      * @return <code>true</code> if tables are present.
81      */

82     public boolean areTablesSet() {
83         return (qTables != null);
84     }
85
86     /**
87      * Sets the quantization and Huffman tables to use in decoding
88      * abbreviated streams. There may be a maximum of 4 tables of
89      * each type. These tables are ignored once tables are
90      * encountered in the stream. All arguments must be
91      * non-<code>null</code>. The two arrays of Huffman tables must
92      * have the same number of elements. The table specifiers in the
93      * frame and scan headers in the stream are assumed to be
94      * equivalent to indices into these arrays. The argument arrays
95      * are copied by this method.
96      *
97      * @param qTables an array of quantization table objects.
98      * @param DCHuffmanTables an array of Huffman table objects.
99      * @param ACHuffmanTables an array of Huffman table objects.
100      *
101      * @exception IllegalArgumentException if any of the arguments
102      * is <code>null</code>, has more than 4 elements, or if the
103      * numbers of DC and AC tables differ.
104      *
105      * @see #unsetDecodeTables
106      */

107     public void setDecodeTables(JPEGQTable JavaDoc[] qTables,
108                                 JPEGHuffmanTable JavaDoc[] DCHuffmanTables,
109                                 JPEGHuffmanTable JavaDoc[] ACHuffmanTables) {
110         if ((qTables == null) ||
111             (DCHuffmanTables == null) ||
112             (ACHuffmanTables == null) ||
113             (qTables.length > 4) ||
114             (DCHuffmanTables.length > 4) ||
115             (ACHuffmanTables.length > 4) ||
116             (DCHuffmanTables.length != ACHuffmanTables.length)) {
117                 throw new IllegalArgumentException JavaDoc
118                     ("Invalid JPEG table arrays");
119         }
120         this.qTables = (JPEGQTable JavaDoc[])qTables.clone();
121         this.DCHuffmanTables = (JPEGHuffmanTable JavaDoc[])DCHuffmanTables.clone();
122         this.ACHuffmanTables = (JPEGHuffmanTable JavaDoc[])ACHuffmanTables.clone();
123     }
124
125     /**
126      * Removes any quantization and Huffman tables that are currently
127      * set.
128      *
129      * @see #setDecodeTables
130      */

131     public void unsetDecodeTables() {
132         this.qTables = null;
133         this.DCHuffmanTables = null;
134         this.ACHuffmanTables = null;
135     }
136
137     /**
138      * Returns a copy of the array of quantization tables set on the
139      * most recent call to <code>setDecodeTables</code>, or
140      * <code>null</code> if tables are not currently set.
141      *
142      * @return an array of <code>JPEGQTable</code> objects, or
143      * <code>null</code>.
144      *
145      * @see #setDecodeTables
146      */

147     public JPEGQTable JavaDoc[] getQTables() {
148         return (qTables != null) ? (JPEGQTable JavaDoc[])qTables.clone() : null;
149     }
150     
151     /**
152      * Returns a copy of the array of DC Huffman tables set on the
153      * most recent call to <code>setDecodeTables</code>, or
154      * <code>null</code> if tables are not currently set.
155      *
156      * @return an array of <code>JPEGHuffmanTable</code> objects, or
157      * <code>null</code>.
158      *
159      * @see #setDecodeTables
160      */

161     public JPEGHuffmanTable JavaDoc[] getDCHuffmanTables() {
162         return (DCHuffmanTables != null)
163             ? (JPEGHuffmanTable JavaDoc[])DCHuffmanTables.clone()
164             : null;
165     }
166
167     /**
168      * Returns a copy of the array of AC Huffman tables set on the
169      * most recent call to <code>setDecodeTables</code>, or
170      * <code>null</code> if tables are not currently set.
171      *
172      * @return an array of <code>JPEGHuffmanTable</code> objects, or
173      * <code>null</code>.
174      *
175      * @see #setDecodeTables
176      */

177     public JPEGHuffmanTable JavaDoc[] getACHuffmanTables() {
178         return (ACHuffmanTables != null)
179             ? (JPEGHuffmanTable JavaDoc[])ACHuffmanTables.clone()
180             : null;
181     }
182 }
183
Popular Tags