1. /*
  2. * @(#)JPEGImageReadParam.java 1.10 03/01/23
  3. *
  4. * Copyright 2003 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package javax.imageio.plugins.jpeg;
  8. import javax.imageio.ImageReadParam;
  9. /**
  10. * This class adds the ability to set JPEG quantization and Huffman
  11. * tables when using the built-in JPEG reader plug-in. An instance of
  12. * this class will be returned from the
  13. * <code>getDefaultImageReadParam</code> methods of the built-in JPEG
  14. * <code>ImageReader</code>.
  15. *
  16. * <p> The sole purpose of these additions is to allow the
  17. * specification of tables for use in decoding abbreviated streams.
  18. * The built-in JPEG reader will also accept an ordinary
  19. * <code>ImageReadParam</code>, which is sufficient for decoding
  20. * non-abbreviated streams.
  21. *
  22. * <p> While tables for abbreviated streams are often obtained by
  23. * first reading another abbreviated stream containing only the
  24. * tables, in some applications the tables are fixed ahead of time.
  25. * This class allows the tables to be specified directly from client
  26. * code. If no tables are specified either in the stream or in a
  27. * <code>JPEGImageReadParam</code>, then the stream is presumed to use
  28. * the "standard" visually lossless tables. See {@link JPEGQTable
  29. * <code>JPEGQTable</code>} and {@link JPEGHuffmanTable
  30. * <code>JPEGHuffmanTable</code>} for more information on the default
  31. * tables.
  32. *
  33. * <p> The default <code>JPEGImageReadParam</code> returned by the
  34. * <code>getDefaultReadParam</code> method of the builtin JPEG reader
  35. * contains no tables. Default tables may be obtained from the table
  36. * classes {@link JPEGQTable <code>JPEGQTable</code>} and {@link
  37. * JPEGHuffmanTable <code>JPEGHuffmanTable</code>}.
  38. *
  39. * <p> If a stream does contain tables, the tables given in a
  40. * <code>JPEGImageReadParam</code> are ignored. Furthermore, if the
  41. * first image in a stream does contain tables and subsequent ones do
  42. * not, then the tables given in the first image are used for all the
  43. * abbreviated images. Once tables have been read from a stream, they
  44. * can be overridden only by tables subsequently read from the same
  45. * stream. In order to specify new tables, the {@link
  46. * javax.imageio.ImageReader#setInput <code>setInput</code>} method of
  47. * the reader must be called to change the stream.
  48. *
  49. * <p> Note that this class does not provide a means for obtaining the
  50. * tables found in a stream. These may be extracted from a stream by
  51. * consulting the <code>IIOMetadata</code> object returned by the
  52. * reader.
  53. *
  54. * <p>
  55. * For more information about the operation of the built-in JPEG plug-ins,
  56. * see the <A HREF="../../metadata/doc-files/jpeg_metadata.html">JPEG
  57. * metadata format specification and usage notes</A>.
  58. *
  59. * @version 0.5
  60. */
  61. public class JPEGImageReadParam extends ImageReadParam {
  62. private JPEGQTable[] qTables = null;
  63. private JPEGHuffmanTable[] DCHuffmanTables = null;
  64. private JPEGHuffmanTable[] ACHuffmanTables = null;
  65. /**
  66. * Constructs a <code>JPEGImageReadParam</code>.
  67. */
  68. public JPEGImageReadParam() {
  69. super();
  70. }
  71. /**
  72. * Returns <code>true</code> if tables are currently set.
  73. *
  74. * @return <code>true</code> if tables are present.
  75. */
  76. public boolean areTablesSet() {
  77. return (qTables != null);
  78. }
  79. /**
  80. * Sets the quantization and Huffman tables to use in decoding
  81. * abbreviated streams. There may be a maximum of 4 tables of
  82. * each type. These tables are ignored once tables are
  83. * encountered in the stream. All arguments must be
  84. * non-<code>null</code>. The two arrays of Huffman tables must
  85. * have the same number of elements. The table specifiers in the
  86. * frame and scan headers in the stream are assumed to be
  87. * equivalent to indices into these arrays. The argument arrays
  88. * are copied by this method.
  89. *
  90. * @param qTables an array of quantization table objects.
  91. * @param DCHuffmanTables an array of Huffman table objects.
  92. * @param ACHuffmanTables an array of Huffman table objects.
  93. *
  94. * @exception IllegalArgumentException if any of the arguments
  95. * is <code>null</code>, has more than 4 elements, or if the
  96. * numbers of DC and AC tables differ.
  97. *
  98. * @see #unsetDecodeTables
  99. */
  100. public void setDecodeTables(JPEGQTable[] qTables,
  101. JPEGHuffmanTable[] DCHuffmanTables,
  102. JPEGHuffmanTable[] ACHuffmanTables) {
  103. if ((qTables == null) ||
  104. (DCHuffmanTables == null) ||
  105. (ACHuffmanTables == null) ||
  106. (qTables.length > 4) ||
  107. (DCHuffmanTables.length > 4) ||
  108. (ACHuffmanTables.length > 4) ||
  109. (DCHuffmanTables.length != ACHuffmanTables.length)) {
  110. throw new IllegalArgumentException
  111. ("Invalid JPEG table arrays");
  112. }
  113. this.qTables = (JPEGQTable[])qTables.clone();
  114. this.DCHuffmanTables = (JPEGHuffmanTable[])DCHuffmanTables.clone();
  115. this.ACHuffmanTables = (JPEGHuffmanTable[])ACHuffmanTables.clone();
  116. }
  117. /**
  118. * Removes any quantization and Huffman tables that are currently
  119. * set.
  120. *
  121. * @see #setDecodeTables
  122. */
  123. public void unsetDecodeTables() {
  124. this.qTables = null;
  125. this.DCHuffmanTables = null;
  126. this.ACHuffmanTables = null;
  127. }
  128. /**
  129. * Returns a copy of the array of quantization tables set on the
  130. * most recent call to <code>setDecodeTables</code>, or
  131. * <code>null</code> if tables are not currently set.
  132. *
  133. * @return an array of <code>JPEGQTable</code> objects, or
  134. * <code>null</code>.
  135. *
  136. * @see #setDecodeTables
  137. */
  138. public JPEGQTable[] getQTables() {
  139. return (qTables != null) ? (JPEGQTable[])qTables.clone() : null;
  140. }
  141. /**
  142. * Returns a copy of the array of DC Huffman tables set on the
  143. * most recent call to <code>setDecodeTables</code>, or
  144. * <code>null</code> if tables are not currently set.
  145. *
  146. * @return an array of <code>JPEGHuffmanTable</code> objects, or
  147. * <code>null</code>.
  148. *
  149. * @see #setDecodeTables
  150. */
  151. public JPEGHuffmanTable[] getDCHuffmanTables() {
  152. return (DCHuffmanTables != null)
  153. ? (JPEGHuffmanTable[])DCHuffmanTables.clone()
  154. : null;
  155. }
  156. /**
  157. * Returns a copy of the array of AC Huffman tables set on the
  158. * most recent call to <code>setDecodeTables</code>, or
  159. * <code>null</code> if tables are not currently set.
  160. *
  161. * @return an array of <code>JPEGHuffmanTable</code> objects, or
  162. * <code>null</code>.
  163. *
  164. * @see #setDecodeTables
  165. */
  166. public JPEGHuffmanTable[] getACHuffmanTables() {
  167. return (ACHuffmanTables != null)
  168. ? (JPEGHuffmanTable[])ACHuffmanTables.clone()
  169. : null;
  170. }
  171. }