KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * @(#)JPEGQTable.java 1.12 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 /**********************************************************************
9  **********************************************************************
10  **********************************************************************
11  *** COPYRIGHT (c) 1997-1998 Eastman Kodak Company. ***
12  *** As an unpublished work pursuant to Title 17 of the United ***
13  *** States Code. All rights reserved. ***
14  **********************************************************************
15  **********************************************************************
16  **********************************************************************/

17
18 package javax.imageio.plugins.jpeg;
19
20 /**
21  * A class encapsulating a single JPEG quantization table. The
22  * elements appear in natural order (as opposed to zig-zag order).
23  * Static variables are provided for the "standard" tables taken from
24  * Annex K of the JPEG spec, as well as the default tables
25  * conventionally used for visually lossless encoding.
26  *
27  * <p>
28  * For more information about the operation of the built-in JPEG plug-ins,
29  * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
30  * metadata format specification and usage notes</A>.
31  *
32  * @version 0.5
33  */

34 public class JPEGQTable {
35
36     /** Table */
37     private int[] table;
38
39     /** The number of coefficients in a quantization table. */
40     private static final byte QTABLESIZE = 64;
41     
42     /**
43      * The sample luminance quantization table given in the JPEG
44      * specification, table K.1. According to the specification,
45      * these values produce "good" quality output.
46      *
47      * @see #K1Div2Luminance
48      */

49     public static final JPEGQTable JavaDoc K1Luminance = new JPEGQTable JavaDoc();
50     static {
51         int [] lumVals = {
52             16, 11, 10, 16, 24, 40, 51, 61,
53             12, 12, 14, 19, 26, 58, 60, 55,
54             14, 13, 16, 24, 40, 57, 69, 56,
55             14, 17, 22, 29, 51, 87, 80, 62,
56             18, 22, 37, 56, 68, 109, 103, 77,
57             24, 35, 55, 64, 81, 104, 113, 92,
58             49, 64, 78, 87, 103, 121, 120, 101,
59             72, 92, 95, 98, 112, 100, 103, 99
60         };
61         
62         K1Luminance.table = lumVals;
63     }
64
65     /**
66      * The sample luminance quantization table given in the JPEG
67      * specification, table K.1, with all elements divided by 2.
68      * According to the specification, these values produce "very
69      * good" quality output. This is the table usually used for
70      * "visually lossless" encoding, and is the default luminance
71      * table used if the default tables and quality settings are used.
72      *
73      * @see #K1Luminance
74      */

75     public static final JPEGQTable JavaDoc K1Div2Luminance =
76         K1Luminance.getScaledInstance(0.5f, true);
77
78     /**
79      * The sample chrominance quantization table given in the JPEG
80      * specification, table K.2. According to the specification,
81      * these values produce "good" quality output.
82      *
83      * @see #K2Div2Chrominance
84      */

85     public static final JPEGQTable JavaDoc K2Chrominance = new JPEGQTable JavaDoc();
86     static {
87         int [] chromVals = {
88             17, 18, 24, 47, 99, 99, 99, 99,
89             18, 21, 26, 66, 99, 99, 99, 99,
90             24, 26, 56, 99, 99, 99, 99, 99,
91             47, 66, 99, 99, 99, 99, 99, 99,
92             99, 99, 99, 99, 99, 99, 99, 99,
93             99, 99, 99, 99, 99, 99, 99, 99,
94             99, 99, 99, 99, 99, 99, 99, 99,
95             99, 99, 99, 99, 99, 99, 99, 99
96         };
97         K2Chrominance.table = chromVals;
98     }
99     
100     /**
101      * The sample chrominance quantization table given in the JPEG
102      * specification, table K.2, with all elements divided by 2.
103      * According to the specification, these values produce "very
104      * good" quality output. This is the table usually used for
105      * "visually lossless" encoding, and is the default chrominance
106      * table used if the default tables and quality settings are used.
107      *
108      * @see #K2Chrominance
109      */

110     public static final JPEGQTable JavaDoc K2Div2Chrominance =
111         K2Chrominance.getScaledInstance(0.5f, true);
112     
113     /**
114      * Constructs an empty quantization table. This is used to create
115      * the standard tables.
116      */

117     private JPEGQTable() {}
118         
119     /**
120      * Constructs a quantization table from the argument, which must
121      * contain 64 elements in natural order (not zig-zag order).
122      * A copy is made of the the input array.
123      *
124      * @param table the quantization table, as an <code>int</code>
125      * array.
126      *
127      * @exception IllegalArgumentException if <code>table</code> is
128      * <code>null</code> or <code>table.length</code> is not equal to
129      * 64.
130      */

131     public JPEGQTable(int[] table) {
132         if (table == null) {
133             throw new IllegalArgumentException JavaDoc("table == null!");
134         }
135         if (table.length != QTABLESIZE) {
136             throw new IllegalArgumentException JavaDoc
137                 ("Quantization table is the wrong size.");
138         }
139
140         this.table = (int[])table.clone();
141     }
142     
143     /**
144      * Returns a copy of the current quantization table as an array of
145      * ints in natural (not zig-zag) order.
146      *
147      * @return A copy of the current quantization table.
148      */

149     public int[] getTable() {
150         return (int[])table.clone();
151     }
152
153     /**
154      * Returns a new quantization table where the values are
155      * multiplied by <code>scaleFactor</code> and then clamped to the
156      * range 1..32767 (or to 1..255 if <code>forceBaseline</code> is
157      * <code>true</code>).
158      *
159      * <p> Values of <code>scaleFactorless</code> than 1 tend to
160      * improve the quality level of the table, and values greater than
161      * <code>1.0</code> degrade the quality level of the table.
162      
163      * @param scaleFactor the multiplicative factor for the table.
164      * @param forceBaseline if <code>true</code>, the values will be
165      * clamped to the range 1..255.
166      *
167      * @return a new quantization table that is a linear multiple of
168      * the current table.
169      */

170     public JPEGQTable JavaDoc getScaledInstance(float scaleFactor,
171                                         boolean forceBaseline) {
172         int max = (forceBaseline) ? 255 : 32767;
173         int[] ret = new int[QTABLESIZE];
174
175         for (int i = 0; i < QTABLESIZE; i++) {
176             float scaledValue = (float)Math.round(table[i]*scaleFactor);
177             int holder;
178             
179             // limit to valid range
180
if (scaledValue <= 1.0F) {
181                 holder = 1;
182             } else if (scaledValue >= max) {
183                 holder = max;
184             } else {
185                 holder = (int)scaledValue;
186             }
187             ret[i] = holder;
188         }
189
190         return new JPEGQTable JavaDoc(ret);
191     }
192
193     public String JavaDoc toString() {
194         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
195         sb.append("JPEGQTable:\n");
196         for (int i = 0; i< 8; i++) {
197             sb.append('\t');
198             for (int j = 0; j < 8; j++) {
199                 sb.append(table[i]).append(" ");
200             }
201             sb.append('\n');
202         }
203         return sb.toString();
204     }
205 }
206
Popular Tags