1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     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
18package javax.imageio.plugins.jpeg;
19
20import org.apache.harmony.x.imageio.plugins.jpeg.JPEGConsts;
21
22import javax.imageio.ImageWriteParam;
23import java.util.Locale;
24
25/**
26 * The JPEGImageWriteParam class allows to set JPEG Huffman tables and
27 * quantization when using the JPEG writer plug-in.
28 *
29 * @since Android 1.0
30 */
31public class JPEGImageWriteParam extends ImageWriteParam {
32
33    /**
34     * The Constant COMP_QUALITY_VALUES.
35     */
36    private static final float[] COMP_QUALITY_VALUES = {
37            0.05f, 0.75f, 0.95f
38    };
39
40    /**
41     * The Constant COMP_QUALITY_DESCRIPTIONS.
42     */
43    private static final String[] COMP_QUALITY_DESCRIPTIONS = {
44            "Minimum useful", "Visually lossless", "Maximum useful"
45    };
46
47    /**
48     * The q tables.
49     */
50    private JPEGQTable[] qTables;
51
52    /**
53     * The dc huffman tables.
54     */
55    private JPEGHuffmanTable[] dcHuffmanTables;
56
57    /**
58     * The ac huffman tables.
59     */
60    private JPEGHuffmanTable[] acHuffmanTables;
61
62    /**
63     * The optimize huffman tables.
64     */
65    private boolean optimizeHuffmanTables;
66
67    /**
68     * Instantiates a new JPEGImageWriteParam object with the specified Locale.
69     *
70     * @param locale
71     *            the Locale.
72     */
73    public JPEGImageWriteParam(Locale locale) {
74        super(locale);
75
76        canWriteProgressive = true;
77        progressiveMode = ImageWriteParam.MODE_DISABLED;
78
79        canWriteCompressed = true;
80        compressionTypes = new String[] {
81            "JPEG"
82        };
83        compressionType = compressionTypes[0];
84        compressionQuality = JPEGConsts.DEFAULT_JPEG_COMPRESSION_QUALITY;
85    }
86
87    /**
88     * Returns true if tables are set, false otherwise.
89     *
90     * @return true, if tables are set, false otherwise.
91     */
92    public boolean areTablesSet() {
93        return qTables != null;
94    }
95
96    /**
97     * Sets the quantization and Huffman tables for using in encoding streams.
98     *
99     * @param qTables
100     *            the quantization tables.
101     * @param DCHuffmanTables
102     *            the standart DC Huffman tables.
103     * @param ACHuffmanTables
104     *            the standart AC huffman tables.
105     */
106    public void setEncodeTables(JPEGQTable[] qTables, JPEGHuffmanTable[] DCHuffmanTables,
107            JPEGHuffmanTable[] ACHuffmanTables) {
108        if (qTables == null || DCHuffmanTables == null || ACHuffmanTables == null) {
109            throw new IllegalArgumentException("Invalid JPEG table arrays");
110        }
111        if (DCHuffmanTables.length != ACHuffmanTables.length) {
112            throw new IllegalArgumentException("Invalid JPEG table arrays");
113        }
114        if (qTables.length > 4 || DCHuffmanTables.length > 4) {
115            throw new IllegalArgumentException("Invalid JPEG table arrays");
116        }
117
118        // Do the shallow copy, it should be enough
119        this.qTables = qTables.clone();
120        dcHuffmanTables = DCHuffmanTables.clone();
121        acHuffmanTables = ACHuffmanTables.clone();
122    }
123
124    /**
125     * Unset all encoded tables.
126     */
127    public void unsetEncodeTables() {
128        qTables = null;
129        dcHuffmanTables = null;
130        acHuffmanTables = null;
131    }
132
133    /**
134     * Gets the DC Huffman tables.
135     *
136     * @return the DC Huffman tables which are set, or null.
137     */
138    public JPEGHuffmanTable[] getDCHuffmanTables() {
139        return dcHuffmanTables == null ? null : dcHuffmanTables.clone();
140    }
141
142    /**
143     * Gets the AC Huffman tables.
144     *
145     * @return the AC Huffman tables which are set, or null.
146     */
147    public JPEGHuffmanTable[] getACHuffmanTables() {
148        return acHuffmanTables == null ? null : acHuffmanTables.clone();
149    }
150
151    /**
152     * Gets the quantization tables.
153     *
154     * @return the quantization tables, or null.
155     */
156    public JPEGQTable[] getQTables() {
157        return qTables == null ? null : qTables.clone();
158    }
159
160    @Override
161    public String[] getCompressionQualityDescriptions() {
162        super.getCompressionQualityDescriptions();
163        return COMP_QUALITY_DESCRIPTIONS.clone();
164    }
165
166    @Override
167    public float[] getCompressionQualityValues() {
168        super.getCompressionQualityValues();
169        return COMP_QUALITY_VALUES.clone();
170    }
171
172    /**
173     * Sets the flag indicated that the writer will generate optimized Huffman
174     * tables for the image as part of the writing process.
175     *
176     * @param optimize
177     *            the flag of optimizing huffman tables.
178     */
179    public void setOptimizeHuffmanTables(boolean optimize) {
180        optimizeHuffmanTables = optimize;
181    }
182
183    /**
184     * Returns true if the writer generates optimized Huffman tables, false
185     * otherwise.
186     *
187     * @return true, if the writer generates optimized Huffman tables, false
188     *         otherwise.
189     */
190    public boolean getOptimizeHuffmanTables() {
191        return optimizeHuffmanTables;
192    }
193
194    @Override
195    public boolean isCompressionLossless() {
196        if (getCompressionMode() != MODE_EXPLICIT) {
197            throw new IllegalStateException("Compression mode not MODE_EXPLICIT!");
198        }
199        return false;
200    }
201
202    @Override
203    public void unsetCompression() {
204        if (getCompressionMode() != MODE_EXPLICIT) {
205            throw new IllegalStateException("Compression mode not MODE_EXPLICIT!");
206        }
207        compressionQuality = JPEGConsts.DEFAULT_JPEG_COMPRESSION_QUALITY;
208    }
209}
210