01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Rustem V. Rafikov
19: * @version $Revision: 1.3 $
20: */package javax.imageio.plugins.jpeg;
21:
22: public class JPEGQTable {
23:
24: private final static int SIZE = 64;
25: private final static int BASELINE_MAX = 255;
26: private final static int MAX = 32767;
27:
28: private int[] theTable;
29:
30: /*
31: * K1 & K2 tables can be found in the JPEG format specification
32: * at http://www.w3.org/Graphics/JPEG/itu-t81.pdf
33: */
34:
35: private static final int[] K1LumTable = new int[] { 16, 11, 10, 16,
36: 24, 40, 51, 61, 12, 12, 14, 19, 26, 58, 60, 55, 14, 13, 16,
37: 24, 40, 57, 69, 56, 14, 17, 22, 29, 51, 87, 80, 62, 18, 22,
38: 37, 56, 68, 109, 103, 77, 24, 35, 55, 64, 81, 104, 113, 92,
39: 49, 64, 78, 87, 103, 121, 120, 101, 72, 92, 95, 98, 112,
40: 100, 103, 99 };
41:
42: private static final int[] K2ChrTable = new int[] { 17, 18, 24, 47,
43: 99, 99, 99, 99, 18, 21, 26, 66, 99, 99, 99, 99, 24, 26, 56,
44: 99, 99, 99, 99, 99, 47, 66, 99, 99, 99, 99, 99, 99, 99, 99,
45: 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99,
46: 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99 };
47:
48: public static final JPEGQTable K1Luminance = new JPEGQTable(
49: K1LumTable);
50: public static final JPEGQTable K1Div2Luminance = K1Luminance
51: .getScaledInstance(0.5f, true);
52: public static final JPEGQTable K2Chrominance = new JPEGQTable(
53: K2ChrTable);
54: public static final JPEGQTable K2Div2Chrominance = K2Chrominance
55: .getScaledInstance(0.5f, true);;
56:
57: public JPEGQTable(int[] table) {
58: if (table == null) {
59: throw new IllegalArgumentException(
60: "table should not be NULL");
61: }
62: if (table.length != SIZE) {
63: throw new IllegalArgumentException("illegal table size: "
64: + table.length);
65: }
66: theTable = table.clone();
67: }
68:
69: public int[] getTable() {
70: return theTable.clone();
71: }
72:
73: public JPEGQTable getScaledInstance(float scaleFactor,
74: boolean forceBaseline) {
75: int table[] = new int[SIZE];
76:
77: int maxValue = forceBaseline ? BASELINE_MAX : MAX;
78:
79: for (int i = 0; i < theTable.length; i++) {
80: int rounded = Math.round(theTable[i] * scaleFactor);
81: if (rounded < 1) {
82: rounded = 1;
83: }
84: if (rounded > maxValue) {
85: rounded = maxValue;
86: }
87: table[i] = rounded;
88: }
89: return new JPEGQTable(table);
90: }
91:
92: @Override
93: public String toString() {
94: //-- TODO more informative info
95: return "JPEGQTable";
96: }
97: }
|