001 /* JPEGQTable.java --
002 Copyright (C) 2006 Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING. If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library. Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module. An independent module is a module which is not derived from
033 or based on this library. If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so. If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039 package javax.imageio.plugins.jpeg;
040
041 import gnu.java.lang.CPStringBuilder;
042
043 /**
044 * The JPEGQTable class represents a quantization table that can be
045 * used to encode or decode a JPEG stream. The standard JPEG
046 * luminance and chrominance quantization tables are provided as
047 * static fields. Table entries are stored in natural order, not
048 * zig-zag order.
049 */
050 public class JPEGQTable
051 {
052 /**
053 * The table entries, stored in natural order.
054 */
055 private int[] table;
056
057 /**
058 * The standard JPEG luminance quantization table. Values are
059 * stored in natural order.
060 */
061 public static final JPEGQTable K1Luminance = new JPEGQTable(new int[]
062 {
063 16, 11, 10, 16, 24, 40, 51, 61,
064 12, 12, 14, 19, 26, 58, 60, 55,
065 14, 13, 16, 24, 40, 57, 69, 56,
066 14, 17, 22, 29, 51, 87, 80, 62,
067 18, 22, 37, 56, 68, 109, 103, 77,
068 24, 35, 55, 64, 81, 104, 113, 92,
069 49, 64, 78, 87, 103, 121, 120, 101,
070 72, 92, 95, 98, 112, 100, 103, 99
071 }, false);
072
073 /**
074 * The standard JPEG luminance quantization table, scaled by
075 * one-half. Values are stored in natural order.
076 */
077 public static final JPEGQTable K1Div2Luminance =
078 K1Luminance.getScaledInstance(0.5f, true);
079
080 /**
081 * The standard JPEG chrominance quantization table. Values are
082 * stored in natural order.
083 */
084 public static final JPEGQTable K2Chrominance = new JPEGQTable(new int[]
085 {
086 17, 18, 24, 47, 99, 99, 99, 99,
087 18, 21, 26, 66, 99, 99, 99, 99,
088 24, 26, 56, 99, 99, 99, 99, 99,
089 47, 66, 99, 99, 99, 99, 99, 99,
090 99, 99, 99, 99, 99, 99, 99, 99,
091 99, 99, 99, 99, 99, 99, 99, 99,
092 99, 99, 99, 99, 99, 99, 99, 99,
093 99, 99, 99, 99, 99, 99, 99, 99
094 }, false);
095
096 /**
097 * The standard JPEG chrominance quantization table, scaled by
098 * one-half. Values are stored in natural order.
099 */
100 public static final JPEGQTable K2Div2Chrominance =
101 K2Chrominance.getScaledInstance(0.5f, true);
102
103 /**
104 * Construct a new JPEG quantization table. A copy is created of
105 * the table argument.
106 *
107 * @param table the 64-element value table, stored in natural order
108 *
109 * @throws IllegalArgumentException if the table is null or if
110 * table's length is not equal to 64.
111 */
112 public JPEGQTable(int[] table)
113 {
114 this(checkTable(table), true);
115 }
116
117 /**
118 * Private constructor that avoids unnecessary copying and argument
119 * checking.
120 *
121 * @param table the 64-element value table, stored in natural order
122 * @param copy true if a copy should be created of the given table
123 */
124 private JPEGQTable(int[] table, boolean copy)
125 {
126 this.table = copy ? (int[]) table.clone() : table;
127 }
128
129 private static int[] checkTable(int[] table)
130 {
131 if (table == null || table.length != 64)
132 throw new IllegalArgumentException("invalid JPEG quantization table");
133
134 return table;
135 }
136
137 /**
138 * Retrieve a copy of the quantization values for this table.
139 *
140 * @return a copy of the quantization value array
141 */
142 public int[] getTable()
143 {
144 return (int[]) table.clone();
145 }
146
147 /**
148 * Retrieve a copy of this JPEG quantization table with every value
149 * scaled by the given scale factor, and clamped from 1 to 255
150 * baseline or from 1 to 32767 otherwise.
151 *
152 * @param scaleFactor the factor by which to scale this table
153 * @param forceBaseline clamp scaled values to a maximum of 255 if
154 * true, 32767 if false
155 *
156 * @return a new scaled JPEG quantization table
157 */
158 public JPEGQTable getScaledInstance(float scaleFactor,
159 boolean forceBaseline)
160 {
161 int[] scaledTable = getTable();
162 int max = forceBaseline ? 255 : 32767;
163
164 for (int i = 0; i < scaledTable.length; i++)
165 {
166 scaledTable[i] = Math.round (scaleFactor * (float) scaledTable[i]);
167 if (scaledTable[i] < 1)
168 scaledTable[i] = 1;
169 else if (scaledTable[i] > max)
170 scaledTable[i] = max;
171 }
172
173 // Do not copy scaledTable. It is already a copy because we used
174 // getTable to retrieve it.
175 return new JPEGQTable(scaledTable, false);
176 }
177
178 /**
179 * Create a string representing this JPEG quantization table.
180 */
181 public String toString()
182 {
183 CPStringBuilder buffer = new CPStringBuilder();
184
185 buffer.append("JPEGQTable:\n");
186 for (int i = 0; i < 8; i++)
187 {
188 buffer.append(" ");
189 for (int j = 0; j < 8; j++)
190 {
191 buffer.append(table[i * 8 + j] + " ");
192 }
193 buffer.append("\n");
194 }
195
196 return buffer.toString();
197 }
198 }