001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.imageio.plugins.jpeg;
019:
020: import org.apache.harmony.x.imageio.plugins.jpeg.JPEGConsts;
021:
022: import javax.imageio.ImageWriteParam;
023: import java.util.Locale;
024:
025: public class JPEGImageWriteParam extends ImageWriteParam {
026: private static final float[] COMP_QUALITY_VALUES = { 0.05f, 0.75f,
027: 0.95f };
028: private static final String[] COMP_QUALITY_DESCRIPTIONS = {
029: "Minimum useful", "Visually lossless", "Maximum useful" };
030:
031: private JPEGQTable[] qTables;
032: private JPEGHuffmanTable[] dcHuffmanTables;
033: private JPEGHuffmanTable[] acHuffmanTables;
034:
035: private boolean optimizeHuffmanTables;
036:
037: public JPEGImageWriteParam(Locale locale) {
038: super (locale);
039:
040: canWriteProgressive = true;
041: progressiveMode = ImageWriteParam.MODE_DISABLED;
042:
043: canWriteCompressed = true;
044: compressionTypes = new String[] { "JPEG" };
045: compressionType = compressionTypes[0];
046: compressionQuality = JPEGConsts.DEFAULT_JPEG_COMPRESSION_QUALITY;
047: }
048:
049: public boolean areTablesSet() {
050: return qTables != null;
051: }
052:
053: public void setEncodeTables(JPEGQTable[] qTables,
054: JPEGHuffmanTable[] DCHuffmanTables,
055: JPEGHuffmanTable[] ACHuffmanTables) {
056: if (qTables == null || DCHuffmanTables == null
057: || ACHuffmanTables == null) {
058: throw new IllegalArgumentException(
059: "Invalid JPEG table arrays");
060: }
061: if (DCHuffmanTables.length != ACHuffmanTables.length) {
062: throw new IllegalArgumentException(
063: "Invalid JPEG table arrays");
064: }
065: if (qTables.length > 4 || DCHuffmanTables.length > 4) {
066: throw new IllegalArgumentException(
067: "Invalid JPEG table arrays");
068: }
069:
070: // Do the shallow copy, it should be enough
071: this .qTables = qTables.clone();
072: dcHuffmanTables = DCHuffmanTables.clone();
073: acHuffmanTables = ACHuffmanTables.clone();
074: }
075:
076: public void unsetEncodeTables() {
077: qTables = null;
078: dcHuffmanTables = null;
079: acHuffmanTables = null;
080: }
081:
082: public JPEGHuffmanTable[] getDCHuffmanTables() {
083: return dcHuffmanTables == null ? null : dcHuffmanTables.clone();
084: }
085:
086: public JPEGHuffmanTable[] getACHuffmanTables() {
087: return acHuffmanTables == null ? null : acHuffmanTables.clone();
088: }
089:
090: public JPEGQTable[] getQTables() {
091: return qTables == null ? null : qTables.clone();
092: }
093:
094: @Override
095: public String[] getCompressionQualityDescriptions() {
096: super .getCompressionQualityDescriptions();
097: return COMP_QUALITY_DESCRIPTIONS.clone();
098: }
099:
100: @Override
101: public float[] getCompressionQualityValues() {
102: super .getCompressionQualityValues();
103: return COMP_QUALITY_VALUES.clone();
104: }
105:
106: public void setOptimizeHuffmanTables(boolean optimize) {
107: optimizeHuffmanTables = optimize;
108: }
109:
110: public boolean getOptimizeHuffmanTables() {
111: return optimizeHuffmanTables;
112: }
113:
114: @Override
115: public boolean isCompressionLossless() {
116: if (getCompressionMode() != MODE_EXPLICIT) {
117: throw new IllegalStateException(
118: "Compression mode not MODE_EXPLICIT!");
119: }
120: return false;
121: }
122:
123: @Override
124: public void unsetCompression() {
125: if (getCompressionMode() != MODE_EXPLICIT) {
126: throw new IllegalStateException(
127: "Compression mode not MODE_EXPLICIT!");
128: }
129: compressionQuality = JPEGConsts.DEFAULT_JPEG_COMPRESSION_QUALITY;
130: }
131: }
|