001/* JPEGImageReadParam.java --
002 Copyright (C)  2006  Free Software Foundation, Inc.
003
004 This file is part of GNU Classpath.
005
006 GNU Classpath is free software; you can redistribute it and/or modify
007 it under the terms of the GNU General Public License as published by
008 the Free Software Foundation; either version 2, or (at your option)
009 any later version.
010
011 GNU Classpath is distributed in the hope that it will be useful, but
012 WITHOUT ANY WARRANTY; without even the implied warranty of
013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
014 General Public License for more details.
015
016 You should have received a copy of the GNU General Public License
017 along with GNU Classpath; see the file COPYING.  If not, write to the
018 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
019 02110-1301 USA.
020
021 Linking this library statically or dynamically with other modules is
022 making a combined work based on this library.  Thus, the terms and
023 conditions of the GNU General Public License cover the whole
024 combination.
025
026 As a special exception, the copyright holders of this library give you
027 permission to link this library with independent modules to produce an
028 executable, regardless of the license terms of these independent
029 modules, and to copy and distribute the resulting executable under
030 terms of your choice, provided that you also meet, for each linked
031 independent module, the terms and conditions of the license of that
032 module.  An independent module is a module which is not derived from
033 or based on this library.  If you modify this library, you may extend
034 this exception to your version of the library, but you are not
035 obligated to do so.  If you do not wish to do so, delete this
036 exception statement from your version. */
037
038
039package javax.imageio.plugins.jpeg;
040
041import javax.imageio.ImageReadParam;
042
043/**
044 * The JPEGImageReadParam class is only used to set JPEG decoding
045 * tables for streams that do not provide their own tables.  If a
046 * stream does not provide tables and a custom JPEGImageReadParam is
047 * not provided, then the standard JPEG tables are used from the
048 * JPEGQTable and JPEGHuffmanTable classes.  If a stream does provide
049 * decoding tables then JPEGImageReadParam will be ignored.
050 * JPEGImageReadParam cannot be used to retrieve the tables from a
051 * stream.  Instead, use IIOMetadata for this purpose.
052 *
053 * A JPEGImageReadParam instance is retrieved from the built-in JPEG
054 * ImageReader using the getDefaultImageReadParam method.
055 */
056public class JPEGImageReadParam
057  extends ImageReadParam
058{
059  private JPEGQTable[] qTables;
060  private JPEGHuffmanTable[] DCHuffmanTables;
061  private JPEGHuffmanTable[] ACHuffmanTables;
062
063  /**
064   * Construct a JPEGImageReadParam.
065   */
066  public JPEGImageReadParam()
067  {
068    super();
069  }
070
071  /**
072   * Check if the decoding tables are set.
073   *
074   * @return true if the decoding tables are set, false otherwise
075   */
076  public boolean areTablesSet()
077  {
078    // If qTables is not null then all tables are set.
079    return (qTables != null);
080  }
081
082  /**
083   * Set the quantization and Huffman tables that will be used to
084   * decode the stream.  Copies are created of the array arguments.
085   * The number of Huffman tables must be the same in both Huffman
086   * table arrays.  No argument may be null and no array may be longer
087   * than four elements.
088   *
089   * @param qTables JPEG quantization tables
090   * @param DCHuffmanTables JPEG DC Huffman tables
091   * @param ACHuffmanTables JPEG AC Huffman tables
092   *
093   * @throws IllegalArgumentException if any argument is null, if any
094   * of the arrays are longer than four elements, or if the Huffman
095   * table arrays do not have the same number of elements
096   */
097  public void setDecodeTables(JPEGQTable[] qTables,
098                              JPEGHuffmanTable[] DCHuffmanTables,
099                              JPEGHuffmanTable[] ACHuffmanTables)
100  {
101    if (qTables == null || DCHuffmanTables == null || ACHuffmanTables == null)
102      throw new IllegalArgumentException("null argument");
103
104    if (qTables.length > 4 || DCHuffmanTables.length > 4
105        || ACHuffmanTables.length > 4)
106      throw new IllegalArgumentException("argument has too many elements");
107
108    if (DCHuffmanTables.length != ACHuffmanTables.length)
109      throw new IllegalArgumentException("Huffman table arrays differ in length");
110
111    // Do a shallow copy.  JPEGQTable's data is not directly
112    // modifyable since JPEGQTable.getTable returns a copy.  Therefore
113    // it is safe to have multiple references to a single JPEGQTable.
114    // Likewise for JPEGHuffmanTable.
115    this.qTables = (JPEGQTable[]) qTables.clone();
116    this.DCHuffmanTables = (JPEGHuffmanTable[]) DCHuffmanTables.clone();
117    this.ACHuffmanTables = (JPEGHuffmanTable[]) ACHuffmanTables.clone();
118  }
119
120  /**
121   * Clear the quantization and Huffman decoding tables.
122   */
123  public void unsetDecodeTables()
124  {
125    qTables = null;
126    DCHuffmanTables = null;
127    ACHuffmanTables = null;
128  }
129
130  /**
131   * Retrieve the quantization tables.
132   *
133   * @returns an array of JPEG quantization tables
134   */
135  public JPEGQTable[] getQTables()
136  {
137    return qTables == null ? qTables : (JPEGQTable[]) qTables.clone();
138  }
139
140  /**
141   * Retrieve the DC Huffman tables.
142   *
143   * @return an array of JPEG DC Huffman tables
144   */
145  public JPEGHuffmanTable[] getDCHuffmanTables()
146  {
147    return DCHuffmanTables == null ? DCHuffmanTables
148      : (JPEGHuffmanTable[]) DCHuffmanTables.clone();
149  }
150
151  /**
152   * Retrieve the AC Huffman tables.
153   *
154   * @return an array of JPEG AC Huffman tables
155   */
156  public JPEGHuffmanTable[] getACHuffmanTables()
157  {
158    return ACHuffmanTables == null ? ACHuffmanTables
159      : (JPEGHuffmanTable[]) ACHuffmanTables.clone();
160  }
161}