1. /*
  2. * @(#)JPEGHuffmanTable.java 1.13 03/12/19
  3. *
  4. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. /**********************************************************************
  8. **********************************************************************
  9. **********************************************************************
  10. *** COPYRIGHT (c) 1997-1998 Eastman Kodak Company. ***
  11. *** As an unpublished work pursuant to Title 17 of the United ***
  12. *** States Code. All rights reserved. ***
  13. **********************************************************************
  14. **********************************************************************
  15. **********************************************************************/
  16. package javax.imageio.plugins.jpeg;
  17. /**
  18. * A class encapsulating a single JPEG Huffman table. Fields are
  19. * provided for the "standard" tables from taken from Annex K of the
  20. * JPEG specification. These are the tables used as defaults.
  21. * This class differs from the old unsupported
  22. * com.sun.image.codec.jpeg.JPEGHuffmanTable in two ways:
  23. * <ol>
  24. * <li>The lengths arrays for this class do not contain an ignored 0th
  25. * entry. The lengths array in a JPEG stream does not contain an extra
  26. * entry, and this class is used to represent the contents of a JPEG
  27. * stream.
  28. * </li>
  29. * <li>The old class incorrectly referred to Huffman "symbols", while
  30. * expecting an array of values to be encoded. This class has a
  31. * <code>getValues</code> method instead of a <code>getSymbols</code>
  32. * method.
  33. * </li>
  34. * </ol>
  35. *
  36. * <p>
  37. * For more information about the operation of the built-in JPEG plug-ins,
  38. * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
  39. * metadata format specification and usage notes</A>.
  40. *
  41. * @version 0.5
  42. */
  43. public class JPEGHuffmanTable {
  44. /**
  45. * The maximum number of symbol lengths
  46. * (max symbol length in bits = 16).
  47. */
  48. private static final int HUFF_MAX_LEN = 16;
  49. /** The maximum number of values. */
  50. private static final int HUFF_MAX_VALS = 256;
  51. /** lengths[k-1] = number of symbols with length k bits. */
  52. private short lengths[] = null;
  53. /** Values in order of increasing length of the corresponding symbols. */
  54. private short values[] = null;
  55. /** The standard DC luminance Huffman table. */
  56. public static final JPEGHuffmanTable StdDCLuminance =
  57. new JPEGHuffmanTable();
  58. static {
  59. short lengths[] = {
  60. 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 };
  61. short values[] = {
  62. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  63. StdDCLuminance.lengths = lengths;
  64. StdDCLuminance.values = values;
  65. StdDCLuminance.checkTable();
  66. }
  67. /** The standard DC chrominance Huffman table. */
  68. public static final JPEGHuffmanTable StdDCChrominance =
  69. new JPEGHuffmanTable();
  70. static {
  71. short lengths[] = {
  72. 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0 };
  73. short values[] = {
  74. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
  75. StdDCChrominance.lengths = lengths;
  76. StdDCChrominance.values = values;
  77. StdDCChrominance.checkTable();
  78. }
  79. /** The standard AC luminance Huffman table. */
  80. public static final JPEGHuffmanTable StdACLuminance =
  81. new JPEGHuffmanTable();
  82. static {
  83. short lengths[] = {
  84. 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d };
  85. short values[] = {
  86. 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12,
  87. 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07,
  88. 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08,
  89. 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0,
  90. 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16,
  91. 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28,
  92. 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39,
  93. 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49,
  94. 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59,
  95. 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69,
  96. 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79,
  97. 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89,
  98. 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
  99. 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7,
  100. 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6,
  101. 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5,
  102. 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4,
  103. 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2,
  104. 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea,
  105. 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  106. 0xf9, 0xfa };
  107. StdACLuminance.lengths = lengths;
  108. StdACLuminance.values = values;
  109. StdACLuminance.checkTable();
  110. }
  111. /** The standard AC chrominance Huffman table. */
  112. public static final JPEGHuffmanTable StdACChrominance =
  113. new JPEGHuffmanTable();
  114. static {
  115. short lengths[] = {
  116. 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77 };
  117. short values[] = {
  118. 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21,
  119. 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71,
  120. 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91,
  121. 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0,
  122. 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34,
  123. 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26,
  124. 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38,
  125. 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48,
  126. 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58,
  127. 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68,
  128. 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78,
  129. 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
  130. 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96,
  131. 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5,
  132. 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4,
  133. 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3,
  134. 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2,
  135. 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda,
  136. 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9,
  137. 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8,
  138. 0xf9, 0xfa };
  139. StdACChrominance.lengths = lengths;
  140. StdACChrominance.values = values;
  141. StdACChrominance.checkTable();
  142. }
  143. /**
  144. * Private constructor used to construct the Standard Huffman tables
  145. */
  146. private JPEGHuffmanTable() {}
  147. /**
  148. * Creates a Huffman table and initializes it. The input arrays
  149. * are copied. The arrays must describe a possible Huffman table.
  150. * For example, 3 codes cannot be expressed with a single bit.
  151. *
  152. * @param lengths an array of <code>short</code>s where
  153. * <code>lengths[k]</code> is equal to the number of values with
  154. * corresponding codes of length <code>k + 1</code> bits.
  155. * @param values an array of <code>short</code>s containing the
  156. * values in order of increasing code length.
  157. *
  158. * @exception IllegalArgumentException if <code>lengths</code> or
  159. * <code>values</code> are <code>null</code>, the length of
  160. * <code>lengths</code> is greater than 16, the length of
  161. * <code>values</code> is greater than 256, if any value in
  162. * <code>lengths</code> or <code>values</code> is less than zero,
  163. * or if the arrays do not describe a valid Huffman table.
  164. */
  165. public JPEGHuffmanTable(short[] lengths, short[] values) {
  166. if (lengths == null) {
  167. throw new IllegalArgumentException("lengths array is null!");
  168. }
  169. if (values == null) {
  170. throw new IllegalArgumentException("values array is null!");
  171. }
  172. if (lengths.length > HUFF_MAX_LEN) {
  173. throw new IllegalArgumentException("lengths array is too long!");
  174. }
  175. if (values.length > HUFF_MAX_VALS) {
  176. throw new IllegalArgumentException("values array is too long");
  177. }
  178. for (int i = 1; i < lengths.length; i++) {
  179. if (lengths[i] < 0) {
  180. throw new IllegalArgumentException
  181. ("Values in lengths array must be non-negative.");
  182. }
  183. }
  184. for (int i = 0; i < values.length; i++) {
  185. if (values[i] < 0) {
  186. throw new IllegalArgumentException
  187. ("Values in values array must be non-negative.");
  188. }
  189. }
  190. this.lengths = (short[])lengths.clone();
  191. this.values = (short[])values.clone();
  192. checkTable();
  193. }
  194. /**
  195. * This checks that the table they gave us isn't 'illegal' It
  196. * checks that the symbol length counts are possible, and that
  197. * they gave us at least enough values for the symbol length
  198. * counts. Eventually this might check that there aren't duplicate
  199. * values.
  200. */
  201. private void checkTable() {
  202. int numVals = 2;
  203. int sum = 0;
  204. for (int i = 0; i < lengths.length; i++) {
  205. sum += lengths[i];
  206. numVals -= lengths[i];
  207. numVals *= 2;
  208. }
  209. if (numVals < 0) {
  210. throw new IllegalArgumentException
  211. ("Invalid Huffman table provided, lengths are incorrect.");
  212. }
  213. if (sum != values.length) {
  214. throw new IllegalArgumentException
  215. ("Invalid Huffman table provided, sum of lengths != values.");
  216. }
  217. }
  218. /**
  219. * Return an array of <code>short</code>s containing the number of
  220. * values for each length in the Huffman table. The returned
  221. * array is a copy.
  222. *
  223. * @return a <code>short</code> array where <code>array[k-1]</code>
  224. * is equal to the number of values in the table of length
  225. * <code>k</code>.
  226. *
  227. * @see #getValues
  228. */
  229. public short[] getLengths() {
  230. return (short[])lengths.clone();
  231. }
  232. /**
  233. * Return an array of <code>short</code>s containing the
  234. * values arranged by increasing length of their corresponding
  235. * codes. The interpretation of
  236. * the array is dependent on the values returned from
  237. * <code>getLengths</code>. The returned array is a copy.
  238. *
  239. * @return a <code>short</code> array of values.
  240. *
  241. * @see #getLengths
  242. */
  243. public short[] getValues() {
  244. return (short[])values.clone();
  245. }
  246. public String toString() {
  247. StringBuffer sb = new StringBuffer();
  248. sb.append("JPEGHuffmanTable:\nlengths:");
  249. for (int i = 0; i< lengths.length; i++) {
  250. sb.append(' ').append(lengths[i]);
  251. }
  252. sb.append("\nvalues:");
  253. for (int i = 0; i< values.length; i++) {
  254. sb.append(' ').append(values[i]);
  255. }
  256. return sb.toString();
  257. }
  258. }