Path: blob/master/src/java.desktop/share/classes/javax/imageio/plugins/jpeg/JPEGQTable.java
41159 views
/*1* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package javax.imageio.plugins.jpeg;2627import java.util.Arrays;2829/**30* A class encapsulating a single JPEG quantization table.31* The elements appear in natural order (as opposed to zig-zag order).32* Static variables are provided for the "standard" tables taken from33* Annex K of the JPEG specification, as well as the default tables34* conventionally used for visually lossless encoding.35* <p>36* For more information about the operation of the standard JPEG plug-in,37* see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG38* metadata format specification and usage notes</A>39*/4041public class JPEGQTable {4243private static final int[] k1 = {4416, 11, 10, 16, 24, 40, 51, 61,4512, 12, 14, 19, 26, 58, 60, 55,4614, 13, 16, 24, 40, 57, 69, 56,4714, 17, 22, 29, 51, 87, 80, 62,4818, 22, 37, 56, 68, 109, 103, 77,4924, 35, 55, 64, 81, 104, 113, 92,5049, 64, 78, 87, 103, 121, 120, 101,5172, 92, 95, 98, 112, 100, 103, 99,52};5354private static final int[] k1div2 = {558, 6, 5, 8, 12, 20, 26, 31,566, 6, 7, 10, 13, 29, 30, 28,577, 7, 8, 12, 20, 29, 35, 28,587, 9, 11, 15, 26, 44, 40, 31,599, 11, 19, 28, 34, 55, 52, 39,6012, 18, 28, 32, 41, 52, 57, 46,6125, 32, 39, 44, 52, 61, 60, 51,6236, 46, 48, 49, 56, 50, 52, 50,63};6465private static final int[] k2 = {6617, 18, 24, 47, 99, 99, 99, 99,6718, 21, 26, 66, 99, 99, 99, 99,6824, 26, 56, 99, 99, 99, 99, 99,6947, 66, 99, 99, 99, 99, 99, 99,7099, 99, 99, 99, 99, 99, 99, 99,7199, 99, 99, 99, 99, 99, 99, 99,7299, 99, 99, 99, 99, 99, 99, 99,7399, 99, 99, 99, 99, 99, 99, 99,74};7576private static final int[] k2div2 = {779, 9, 12, 24, 50, 50, 50, 50,789, 11, 13, 33, 50, 50, 50, 50,7912, 13, 28, 50, 50, 50, 50, 50,8024, 33, 50, 50, 50, 50, 50, 50,8150, 50, 50, 50, 50, 50, 50, 50,8250, 50, 50, 50, 50, 50, 50, 50,8350, 50, 50, 50, 50, 50, 50, 50,8450, 50, 50, 50, 50, 50, 50, 50,85};8687/**88* The sample luminance quantization table given in the JPEG89* specification, table K.1. According to the specification,90* these values produce "good" quality output.91* @see #K1Div2Luminance92*/93public static final JPEGQTable94K1Luminance = new JPEGQTable(k1, false);9596/**97* The sample luminance quantization table given in the JPEG98* specification, table K.1, with all elements divided by 2.99* According to the specification, these values produce "very good"100* quality output. This is the table usually used for "visually lossless"101* encoding, and is the default luminance table used if the default102* tables and quality settings are used.103* @see #K1Luminance104*/105public static final JPEGQTable106K1Div2Luminance = new JPEGQTable(k1div2, false);107108/**109* The sample chrominance quantization table given in the JPEG110* specification, table K.2. According to the specification,111* these values produce "good" quality output.112* @see #K2Div2Chrominance113*/114public static final JPEGQTable K2Chrominance =115new JPEGQTable(k2, false);116117/**118* The sample chrominance quantization table given in the JPEG119* specification, table K.1, with all elements divided by 2.120* According to the specification, these values produce "very good"121* quality output. This is the table usually used for "visually lossless"122* encoding, and is the default chrominance table used if the default123* tables and quality settings are used.124* @see #K2Chrominance125*/126public static final JPEGQTable K2Div2Chrominance =127new JPEGQTable(k2div2, false);128129private int[] qTable;130131private JPEGQTable(int[] table, boolean copy) {132qTable = (copy) ? Arrays.copyOf(table, table.length) : table;133}134135/**136* Constructs a quantization table from the argument, which must137* contain 64 elements in natural order (not zig-zag order).138* A copy is made of the input array.139* @param table the quantization table, as an {@code int} array.140* @throws IllegalArgumentException if {@code table} is141* {@code null} or {@code table.length} is not equal to 64.142*/143public JPEGQTable(int[] table) {144if (table == null) {145throw new IllegalArgumentException("table must not be null.");146}147if (table.length != 64) {148throw new IllegalArgumentException("table.length != 64");149}150qTable = Arrays.copyOf(table, table.length);151}152153/**154* Returns a copy of the current quantization table as an array155* of {@code int}s in natural (not zig-zag) order.156* @return A copy of the current quantization table.157*/158public int[] getTable() {159return Arrays.copyOf(qTable, qTable.length);160}161162/**163* Returns a new quantization table where the values are multiplied164* by {@code scaleFactor} and then clamped to the range 1..32767165* (or to 1..255 if {@code forceBaseline} is true).166* <p>167* Values of {@code scaleFactor} less than 1 tend to improve168* the quality level of the table, and values greater than 1.0169* degrade the quality level of the table.170* @param scaleFactor multiplication factor for the table.171* @param forceBaseline if {@code true},172* the values will be clamped to the range 1..255173* @return a new quantization table that is a linear multiple174* of the current table.175*/176public JPEGQTable getScaledInstance(float scaleFactor,177boolean forceBaseline) {178int max = (forceBaseline) ? 255 : 32767;179int[] scaledTable = new int[qTable.length];180for (int i=0; i<qTable.length; i++) {181int sv = (int)((qTable[i] * scaleFactor)+0.5f);182if (sv < 1) {183sv = 1;184}185if (sv > max) {186sv = max;187}188scaledTable[i] = sv;189}190return new JPEGQTable(scaledTable);191}192193/**194* Returns a {@code String} representing this quantization table.195* @return a {@code String} representing this quantization table.196*/197public String toString() {198String ls = System.getProperty("line.separator", "\n");199StringBuilder sb = new StringBuilder("JPEGQTable:"+ls);200for (int i=0; i < qTable.length; i++) {201if (i % 8 == 0) {202sb.append('\t');203}204sb.append(qTable[i]);205sb.append(((i % 8) == 7) ? ls : ' ');206}207return sb.toString();208}209}210211212