1:
37:
38:
39: package ;
40:
41:
46: public class JPEGHuffmanTable
47: {
48:
51: private short[] lengths;
52:
53:
56: private short[] values;
57:
58:
59:
60:
63: public static final JPEGHuffmanTable StdACChrominance =
64: new JPEGHuffmanTable(new short[] { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5,
65: 4, 4, 0, 1, 2, 0x77 },
66: new short[] { 0x00, 0x01, 0x02, 0x03, 0x11,
67: 0x04, 0x05, 0x21, 0x31, 0x06,
68: 0x12, 0x41, 0x51, 0x07, 0x61,
69: 0x71, 0x13, 0x22, 0x32, 0x81,
70: 0x08, 0x14, 0x42, 0x91, 0xa1,
71: 0xb1, 0xc1, 0x09, 0x23, 0x33,
72: 0x52, 0xf0, 0x15, 0x62, 0x72,
73: 0xd1, 0x0a, 0x16, 0x24, 0x34,
74: 0xe1, 0x25, 0xf1, 0x17, 0x18,
75: 0x19, 0x1a, 0x26, 0x27, 0x28,
76: 0x29, 0x2a, 0x35, 0x36, 0x37,
77: 0x38, 0x39, 0x3a, 0x43, 0x44,
78: 0x45, 0x46, 0x47, 0x48, 0x49,
79: 0x4a, 0x53, 0x54, 0x55, 0x56,
80: 0x57, 0x58, 0x59, 0x5a, 0x63,
81: 0x64, 0x65, 0x66, 0x67, 0x68,
82: 0x69, 0x6a, 0x73, 0x74, 0x75,
83: 0x76, 0x77, 0x78, 0x79, 0x7a,
84: 0x82, 0x83, 0x84, 0x85, 0x86,
85: 0x87, 0x88, 0x89, 0x8a, 0x92,
86: 0x93, 0x94, 0x95, 0x96, 0x97,
87: 0x98, 0x99, 0x9a, 0xa2, 0xa3,
88: 0xa4, 0xa5, 0xa6, 0xa7, 0xa8,
89: 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
90: 0xb5, 0xb6, 0xb7, 0xb8, 0xb9,
91: 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
92: 0xc6, 0xc7, 0xc8, 0xc9, 0xca,
93: 0xd2, 0xd3, 0xd4, 0xd5, 0xd6,
94: 0xd7, 0xd8, 0xd9, 0xda, 0xe2,
95: 0xe3, 0xe4, 0xe5, 0xe6, 0xe7,
96: 0xe8, 0xe9, 0xea, 0xf2, 0xf3,
97: 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
98: 0xf9, 0xfa }, false);
99:
100:
103: public static final JPEGHuffmanTable StdACLuminance =
104: new JPEGHuffmanTable(new short[] { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5,
105: 4, 4, 0, 0, 1, 0x7d },
106: new short[] { 0x01, 0x02, 0x03, 0x00, 0x04,
107: 0x11, 0x05, 0x12, 0x21, 0x31,
108: 0x41, 0x06, 0x13, 0x51, 0x61,
109: 0x07, 0x22, 0x71, 0x14, 0x32,
110: 0x81, 0x91, 0xa1, 0x08, 0x23,
111: 0x42, 0xb1, 0xc1, 0x15, 0x52,
112: 0xd1, 0xf0, 0x24, 0x33, 0x62,
113: 0x72, 0x82, 0x09, 0x0a, 0x16,
114: 0x17, 0x18, 0x19, 0x1a, 0x25,
115: 0x26, 0x27, 0x28, 0x29, 0x2a,
116: 0x34, 0x35, 0x36, 0x37, 0x38,
117: 0x39, 0x3a, 0x43, 0x44, 0x45,
118: 0x46, 0x47, 0x48, 0x49, 0x4a,
119: 0x53, 0x54, 0x55, 0x56, 0x57,
120: 0x58, 0x59, 0x5a, 0x63, 0x64,
121: 0x65, 0x66, 0x67, 0x68, 0x69,
122: 0x6a, 0x73, 0x74, 0x75, 0x76,
123: 0x77, 0x78, 0x79, 0x7a, 0x83,
124: 0x84, 0x85, 0x86, 0x87, 0x88,
125: 0x89, 0x8a, 0x92, 0x93, 0x94,
126: 0x95, 0x96, 0x97, 0x98, 0x99,
127: 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
128: 0xa6, 0xa7, 0xa8, 0xa9, 0xaa,
129: 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
130: 0xb7, 0xb8, 0xb9, 0xba, 0xc2,
131: 0xc3, 0xc4, 0xc5, 0xc6, 0xc7,
132: 0xc8, 0xc9, 0xca, 0xd2, 0xd3,
133: 0xd4, 0xd5, 0xd6, 0xd7, 0xd8,
134: 0xd9, 0xda, 0xe1, 0xe2, 0xe3,
135: 0xe4, 0xe5, 0xe6, 0xe7, 0xe8,
136: 0xe9, 0xea, 0xf1, 0xf2, 0xf3,
137: 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
138: 0xf9, 0xfa }, false);
139:
140:
143: public static final JPEGHuffmanTable StdDCChrominance =
144: new JPEGHuffmanTable(new short[] { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1,
145: 1, 0, 0, 0, 0, 0 },
146: new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
147: 10, 11 }, false);
148:
149:
152: public static final JPEGHuffmanTable StdDCLuminance =
153: new JPEGHuffmanTable(new short[] { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0,
154: 0, 0, 0, 0, 0, 0 },
155: new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,
156: 10, 11 }, false);
157:
158:
171: public JPEGHuffmanTable(short[] lengths, short[] values)
172: {
173:
174: this(checkLengths(lengths), checkValues(values, lengths), true);
175: }
176:
177:
185: private JPEGHuffmanTable(short[] lengths, short[] values, boolean copy)
186: {
187: this.lengths = copy ? (short[]) lengths.clone() : lengths;
188: this.values = copy ? (short[]) values.clone() : values;
189: }
190:
191: private static short[] checkLengths(short[] lengths)
192: {
193: if (lengths == null || lengths.length > 16)
194: throw new IllegalArgumentException("invalid length array");
195:
196: for (int i = 0; i < lengths.length; i++)
197: {
198: if (lengths[i] < 0)
199: throw new IllegalArgumentException("negative length");
200: }
201:
202: int sum = 0;
203: for (int i = 0; i < lengths.length; i++)
204: {
205: if (lengths[i] > ((1 << (i + 1)) - 1))
206: throw new IllegalArgumentException("invalid number of codes"
207: + " for code length " + (i + 1));
208: sum += lengths[i];
209: }
210:
211: return lengths;
212: }
213:
214: private static short[] checkValues(short[] values, short[] lengths)
215: {
216: if (values == null || values.length > 256)
217: throw new IllegalArgumentException("invalid values array");
218:
219: for (int i = 0; i < values.length; i++)
220: {
221: if (values[i] < 0)
222: throw new IllegalArgumentException("negative value");
223: }
224:
225: int sum = 0;
226: for (int i = 0; i < lengths.length; i++)
227: sum += lengths[i];
228:
229: if (values.length != sum)
230: throw new IllegalArgumentException("invalid number of values"
231: + " for number of codes");
232:
233: return values;
234: }
235:
236:
243: public short[] getLengths()
244: {
245: return (short[]) lengths.clone();
246: }
247:
248:
254: public short[] getValues()
255: {
256: return (short[]) values.clone();
257: }
258:
259:
264: public String toString()
265: {
266: StringBuffer buffer = new StringBuffer();
267:
268: buffer.append("JPEGHuffmanTable:\nlengths:");
269:
270: for (int i = 0; i < lengths.length; i++)
271: buffer.append(" " + lengths[i]);
272:
273: buffer.append("\nvalues:");
274:
275: for (int i = 0; i < values.length; i++)
276: buffer.append(" " + values[i]);
277:
278: return buffer.toString();
279: }
280: }