001 /*
002 * Copyright 2007 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.imageio.plugins.jpeg;
027
028 import java.util.Arrays;
029
030 /**
031 * A class encapsulating a single JPEG quantization table.
032 * The elements appear in natural order (as opposed to zig-zag order).
033 * Static variables are provided for the "standard" tables taken from
034 * Annex K of the JPEG specification, as well as the default tables
035 * conventionally used for visually lossless encoding.
036 * <p>
037 * For more information about the operation of the standard JPEG plug-in,
038 * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
039 * metadata format specification and usage notes</A>
040 */
041
042 public class JPEGQTable {
043
044 private static final int[] k1 = { 16, 11, 10, 16, 24, 40, 51, 61,
045 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16, 24, 40, 57, 69,
046 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22, 37, 56, 68,
047 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92, 49, 64, 78,
048 87, 103, 121, 120, 101, 72, 92, 95, 98, 112, 100, 103, 99, };
049
050 private static final int[] k1div2 = { 8, 6, 5, 8, 12, 20, 26, 31,
051 6, 6, 7, 10, 13, 29, 30, 28, 7, 7, 8, 12, 20, 29, 35, 28,
052 7, 9, 11, 15, 26, 44, 40, 31, 9, 11, 19, 28, 34, 55, 52,
053 39, 12, 18, 28, 32, 41, 52, 57, 46, 25, 32, 39, 44, 52, 61,
054 60, 51, 36, 46, 48, 49, 56, 50, 52, 50, };
055
056 private static final int[] k2 = { 17, 18, 24, 47, 99, 99, 99, 99,
057 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56, 99, 99, 99, 99,
058 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
059 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
060 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, };
061
062 private static final int[] k2div2 = { 9, 9, 12, 24, 50, 50, 50, 50,
063 9, 11, 13, 33, 50, 50, 50, 50, 12, 13, 28, 50, 50, 50, 50,
064 50, 24, 33, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
065 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50,
066 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, 50, };
067
068 /**
069 * The sample luminance quantization table given in the JPEG
070 * specification, table K.1. According to the specification,
071 * these values produce "good" quality output.
072 * @see #K1Div2Luminance
073 */
074 public static final JPEGQTable K1Luminance = new JPEGQTable(k1,
075 false);
076
077 /**
078 * The sample luminance quantization table given in the JPEG
079 * specification, table K.1, with all elements divided by 2.
080 * According to the specification, these values produce "very good"
081 * quality output. This is the table usually used for "visually lossless"
082 * encoding, and is the default luminance table used if the default
083 * tables and quality settings are used.
084 * @see #K1Luminance
085 */
086 public static final JPEGQTable K1Div2Luminance = new JPEGQTable(
087 k1div2, false);
088
089 /**
090 * The sample chrominance quantization table given in the JPEG
091 * specification, table K.2. According to the specification,
092 * these values produce "good" quality output.
093 * @see #K2Div2Chrominance
094 */
095 public static final JPEGQTable K2Chrominance = new JPEGQTable(k2,
096 false);
097
098 /**
099 * The sample chrominance quantization table given in the JPEG
100 * specification, table K.1, with all elements divided by 2.
101 * According to the specification, these values produce "very good"
102 * quality output. This is the table usually used for "visually lossless"
103 * encoding, and is the default chrominance table used if the default
104 * tables and quality settings are used.
105 * @see #K2Chrominance
106 */
107 public static final JPEGQTable K2Div2Chrominance = new JPEGQTable(
108 k2div2, false);
109
110 private int[] qTable;
111
112 private JPEGQTable(int[] table, boolean copy) {
113 qTable = (copy) ? Arrays.copyOf(table, table.length) : table;
114 }
115
116 /**
117 * Constructs a quantization table from the argument, which must
118 * contain 64 elements in natural order (not zig-zag order).
119 * A copy is made of the the input array.
120 * @param table the quantization table, as an <code>int</code> array.
121 * @throws IllegalArgumentException if <code>table</code> is
122 * <code>null</code> or <code>table.length</code> is not equal to 64.
123 */
124 public JPEGQTable(int[] table) {
125 if (table == null) {
126 throw new IllegalArgumentException(
127 "table must not be null.");
128 }
129 if (table.length != 64) {
130 throw new IllegalArgumentException("table.length != 64");
131 }
132 qTable = Arrays.copyOf(table, table.length);
133 }
134
135 /**
136 * Returns a copy of the current quantization table as an array
137 * of {@code int}s in natural (not zig-zag) order.
138 * @return A copy of the current quantization table.
139 */
140 public int[] getTable() {
141 return Arrays.copyOf(qTable, qTable.length);
142 }
143
144 /**
145 * Returns a new quantization table where the values are multiplied
146 * by <code>scaleFactor</code> and then clamped to the range 1..32767
147 * (or to 1..255 if <code>forceBaseline</code> is true).
148 * <p>
149 * Values of <code>scaleFactor</code> less than 1 tend to improve
150 * the quality level of the table, and values greater than 1.0
151 * degrade the quality level of the table.
152 * @param scaleFactor multiplication factor for the table.
153 * @param forceBaseline if <code>true</code>,
154 * the values will be clamped to the range 1..255
155 * @return a new quantization table that is a linear multiple
156 * of the current table.
157 */
158 public JPEGQTable getScaledInstance(float scaleFactor,
159 boolean forceBaseline) {
160 int max = (forceBaseline) ? 255 : 32767;
161 int[] scaledTable = new int[qTable.length];
162 for (int i = 0; i < qTable.length; i++) {
163 int sv = (int) ((qTable[i] * scaleFactor) + 0.5f);
164 if (sv < 1) {
165 sv = 1;
166 }
167 if (sv > max) {
168 sv = max;
169 }
170 scaledTable[i] = sv;
171 }
172 return new JPEGQTable(scaledTable);
173 }
174
175 /**
176 * Returns a {@code String} representing this quantization table.
177 * @return a {@code String} representing this quantization table.
178 */
179 public String toString() {
180 String ls = System.getProperty("line.separator", "\n");
181 StringBuilder sb = new StringBuilder("JPEGQTable:" + ls);
182 for (int i = 0; i < qTable.length; i++) {
183 if (i % 8 == 0) {
184 sb.append('\t');
185 }
186 sb.append(qTable[i]);
187 sb.append(((i % 8) == 7) ? ls : ' ');
188 }
189 return sb.toString();
190 }
191 }
|