KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > image > codec > jpeg > JPEGQTable


1 /*
2  * @(#)JPEGQTable.java 1.9 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 com.sun.image.codec.jpeg;
19
20
21
22 /** Class to encapsulate the JPEG quantization tables.
23  * <p>
24  * Note that the classes in the com.sun.image.codec.jpeg package are not
25  * part of the core Java APIs. They are a part of Sun's JDK and JRE
26  * distributions. Although other licensees may choose to distribute these
27  * classes, developers cannot depend on their availability in non-Sun
28  * implementations. We expect that equivalent functionality will eventually
29  * be available in a core API or standard extension.
30  * <p>
31  */

32 public class JPEGQTable {
33
34     /** Quantization step for each coefficient in zig-zag order */
35     private int quantval[];
36
37     /** The number of coefficients in a DCT block */
38     private static final byte QTABLESIZE = 64;
39     
40     /**
41      * This is the sample luminance quantization table given in the
42      * JPEG spec section K.1, expressed in zigzag order. The spec says
43      * that the values given produce "good" quality, and when divided
44      * by 2, "very good" quality.
45      */

46     public static final JPEGQTable StdLuminance = new JPEGQTable();
47     static {
48         int [] lumVals = {
49             16, 11, 12, 14, 12, 10, 16, 14,
50             13, 14, 18, 17, 16, 19, 24, 40,
51             26, 24, 22, 22, 24, 49, 35, 37,
52             29, 40, 58, 51, 61, 60, 57, 51,
53             56, 55, 64, 72, 92, 78, 64, 68,
54             87, 69, 55, 56, 80, 109, 81, 87,
55             95, 98, 103, 104, 103, 62, 77, 113,
56             121, 112, 100, 120, 92, 101, 103, 99
57         };
58         
59         StdLuminance.quantval = lumVals;
60     }
61
62     /**
63      * This is the sample luminance quantization table given in the
64      * JPEG spec section K.1, expressed in zigzag order. The spec says
65      * that the values given produce "good" quality, and when divided
66      * by 2, "very good" quality.
67      */

68     public static final JPEGQTable StdChrominance = new JPEGQTable();
69     static {
70         int [] chromVals = {
71             17, 18, 18, 24, 21, 24, 47, 26,
72             26, 47, 99, 66, 56, 66, 99, 99,
73             99, 99, 99, 99, 99, 99, 99, 99,
74             99, 99, 99, 99, 99, 99, 99, 99,
75             99, 99, 99, 99, 99, 99, 99, 99,
76             99, 99, 99, 99, 99, 99, 99, 99,
77             99, 99, 99, 99, 99, 99, 99, 99,
78             99, 99, 99, 99, 99, 99, 99, 99
79         };
80         StdChrominance.quantval = chromVals;
81     }
82         
83
84     /**
85      * Constructs an empty quantization table. This is used to create
86      * the Std Q-Tables.
87      */

88     private JPEGQTable() {
89         quantval = new int[QTABLESIZE];
90     }
91         
92     /**
93      * Constructs an quantization table from the array that was
94      * passed. The coefficents must be in zig-zag order. The array
95      * must be of length 64.
96      * @param table the quantization table (this is copied).
97      */

98     public JPEGQTable( int table[] ) {
99         if ( table.length != QTABLESIZE ) {
100             throw new IllegalArgumentException JavaDoc
101                 ("Quantization table is the wrong size.");
102         } else {
103             quantval = new int[QTABLESIZE];
104             System.arraycopy( table, 0, quantval, 0, QTABLESIZE );
105         }
106     }
107         
108
109     /**
110      * Returns the current quantization table as an array of ints in
111      * zig zag order.
112      * @return A copy of the contained quantization table.
113      */

114     public int[] getTable() {
115         int[] table = new int[QTABLESIZE];
116         System.arraycopy( quantval, 0, table, 0, QTABLESIZE );
117         return table;
118     }
119
120     /**
121      * Returns a new Quantization table where the values are
122      * multiplied by scaleFactor and then clamped to the range
123      * 1..32767 (or to 1..255 if forceBaseline is 'true'). <P>
124
125      * Values less than one tend to improve the quality level of the
126      * table, and values greater than one degrade the quality level of
127      * the table.
128
129      * @param scaleFactor the multiplication factor for the table
130      * @param forceBaseline if true the values will be clamped
131      * to the range [1 .. 255]
132      * @return A new Q-Table that is a linear multiple of this Q-Table
133      */

134     public JPEGQTable getScaledInstance(float scaleFactor,
135                                         boolean forceBaseline ) {
136         long max = (forceBaseline)?255L:32767L;
137         int []ret = new int[QTABLESIZE];
138
139         for (int i=0; i<QTABLESIZE; i++ ) {
140             long holder = (long)((quantval[i] * scaleFactor) + 0.5);
141
142             // limit to valid range
143
if (holder <= 0L) holder = 1L;
144
145             // Max quantizer for 12 bits
146
if (holder > max ) holder = max;
147             
148             ret[i] = (int)holder;
149         }
150         return new JPEGQTable(ret);
151     }
152 }
153
Popular Tags