diff options
Diffstat (limited to 'libjava/classpath/javax/imageio/plugins')
5 files changed, 1078 insertions, 0 deletions
diff --git a/libjava/classpath/javax/imageio/plugins/bmp/BMPImageWriteParam.java b/libjava/classpath/javax/imageio/plugins/bmp/BMPImageWriteParam.java new file mode 100644 index 000000000..cc0e0d34c --- /dev/null +++ b/libjava/classpath/javax/imageio/plugins/bmp/BMPImageWriteParam.java @@ -0,0 +1,144 @@ +/* BMPImageWriteParam.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + +This file is part of GNU Classpath. + +GNU Classpath is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2, or (at your option) +any later version. + +GNU Classpath is distributed in the hope that it will be useful, but +WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +General Public License for more details. + +You should have received a copy of the GNU General Public License +along with GNU Classpath; see the file COPYING. If not, write to the +Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA +02110-1301 USA. + +Linking this library statically or dynamically with other modules is +making a combined work based on this library. Thus, the terms and +conditions of the GNU General Public License cover the whole +combination. + +As a special exception, the copyright holders of this library give you +permission to link this library with independent modules to produce an +executable, regardless of the license terms of these independent +modules, and to copy and distribute the resulting executable under +terms of your choice, provided that you also meet, for each linked +independent module, the terms and conditions of the license of that +module. An independent module is a module which is not derived from +or based on this library. If you modify this library, you may extend +this exception to your version of the library, but you are not +obligated to do so. If you do not wish to do so, delete this +exception statement from your version. */ + + +package javax.imageio.plugins.bmp; + +import java.util.Locale; + +import javax.imageio.ImageWriteParam; + +/** + * A class to encode images in the BMP format. + * By default, the data layout is bottom-up, such that the pixels are stored in + * bottom-up order. + * + * The compression scheme can be specified by using setCompressionType() + * appropriate type string. The compression scheme specified will be honored + * if it is compatible with the type of image being written. If the + * compression scheme is not compatible with the type of image being written, + * then an IOException will be thrown by the BMP image writer. If the + * compression type is not set, then getCompressionType() will return null. + * In this case the BMP image writer will select a compression type that + * supports encoding of the given image without loss of the color resolution. + * + * The compression type strings and the image type each supports are: + * Uncompressed RLE: BI_RGB, image type: <= 8-bits/sample. + * 8-bit Run Length Encoding: BI_RLE8, image type: <= 8-bits/sample + * 4-bit Run Length Encoding: BI_RLE4, image type: <= 4-bits/sample + * Packed data: BI_BITFIELDS, image type: 16 or 32 bits/sample + * + * @author Lillian Angel (langel at redhat dot com) + */ +public class BMPImageWriteParam + extends ImageWriteParam +{ + + /** + * This boolean is true if the data will be written in a topdown manner. + */ + private boolean topDown; + + /** + * Compression type strings. + */ + String rgb = "BI_RGB"; + String rle8 = "BI_RLE8"; + String rle4 = "BI_RLE4"; + String bitfields = "BI_BITFIELDS"; + + /** + * Constants to represent image types. + */ + static final int BI_RGB = 0; + static final int BI_RLE8 = 1; + static final int BI_RLE4 = 2; + static final int BI_BITFIELDS = 3; + + /** + * Constructs an <code>BMPImageWriteParam</code> object with default values + * and a <code>null Locale</code>. + */ + public BMPImageWriteParam() + { + this(null); + } + + /** + * Constructs a <code>BMPImageWriteParam</code> set to use a given + * <code>Locale</code> and with default values for all parameters. + * + * @param locale - a <code>Locale</code> to be used to localize compression + * type names and quality descriptions, or <code>null</code>. + */ + public BMPImageWriteParam(Locale locale) + { + super(locale); + topDown = false; + canWriteCompressed = true; + + compressionTypes = new String[4]; + compressionTypes[BI_RGB] = rgb; + compressionTypes[BI_RLE8] = rle8; + compressionTypes[BI_RLE4] = rle4; + compressionTypes[BI_BITFIELDS] = bitfields; + + compressionType = compressionTypes[BI_RGB]; + } + + /** + * If set, the data will be written out in a top-down manner, the first + * scanline being written first. + * + * @param topDown - whether the data are written in top-down order. + */ + public void setTopDown(boolean topDown) + { + this.topDown = topDown; + } + + /** + * Returns the value of the <code>topDown</code> parameter. The default is + * false. + * + * @return whether the data are written in top-down order. + */ + public boolean isTopDown() + { + return topDown; + } +} diff --git a/libjava/classpath/javax/imageio/plugins/jpeg/JPEGHuffmanTable.java b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGHuffmanTable.java new file mode 100644 index 000000000..be718d4fc --- /dev/null +++ b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGHuffmanTable.java @@ -0,0 +1,282 @@ +/* JPEGHuffmanTable.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package javax.imageio.plugins.jpeg; + +import gnu.java.lang.CPStringBuilder; + +/** + * The JPEGHuffmanTable class represents a Huffman table read from a + * JPEG image file. The standard JPEG AC and DC chrominance and + * luminance values are provided as static fields. + */ +public class JPEGHuffmanTable +{ + /** + * Huffman code lengths. + */ + private short[] lengths; + + /** + * Huffman values. + */ + private short[] values; + + // The private constructors are used for these final fields to avoid + // unnecessary copying. + /** + * The standard JPEG AC chrominance Huffman table. + */ + public static final JPEGHuffmanTable StdACChrominance = + new JPEGHuffmanTable(new short[] { 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, + 4, 4, 0, 1, 2, 0x77 }, + new short[] { 0x00, 0x01, 0x02, 0x03, 0x11, + 0x04, 0x05, 0x21, 0x31, 0x06, + 0x12, 0x41, 0x51, 0x07, 0x61, + 0x71, 0x13, 0x22, 0x32, 0x81, + 0x08, 0x14, 0x42, 0x91, 0xa1, + 0xb1, 0xc1, 0x09, 0x23, 0x33, + 0x52, 0xf0, 0x15, 0x62, 0x72, + 0xd1, 0x0a, 0x16, 0x24, 0x34, + 0xe1, 0x25, 0xf1, 0x17, 0x18, + 0x19, 0x1a, 0x26, 0x27, 0x28, + 0x29, 0x2a, 0x35, 0x36, 0x37, + 0x38, 0x39, 0x3a, 0x43, 0x44, + 0x45, 0x46, 0x47, 0x48, 0x49, + 0x4a, 0x53, 0x54, 0x55, 0x56, + 0x57, 0x58, 0x59, 0x5a, 0x63, + 0x64, 0x65, 0x66, 0x67, 0x68, + 0x69, 0x6a, 0x73, 0x74, 0x75, + 0x76, 0x77, 0x78, 0x79, 0x7a, + 0x82, 0x83, 0x84, 0x85, 0x86, + 0x87, 0x88, 0x89, 0x8a, 0x92, + 0x93, 0x94, 0x95, 0x96, 0x97, + 0x98, 0x99, 0x9a, 0xa2, 0xa3, + 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, + 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, + 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, + 0xba, 0xc2, 0xc3, 0xc4, 0xc5, + 0xc6, 0xc7, 0xc8, 0xc9, 0xca, + 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, + 0xd7, 0xd8, 0xd9, 0xda, 0xe2, + 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, + 0xe8, 0xe9, 0xea, 0xf2, 0xf3, + 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa }, false); + + /** + * The standard JPEG AC luminance Huffman table. + */ + public static final JPEGHuffmanTable StdACLuminance = + new JPEGHuffmanTable(new short[] { 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, + 4, 4, 0, 0, 1, 0x7d }, + new short[] { 0x01, 0x02, 0x03, 0x00, 0x04, + 0x11, 0x05, 0x12, 0x21, 0x31, + 0x41, 0x06, 0x13, 0x51, 0x61, + 0x07, 0x22, 0x71, 0x14, 0x32, + 0x81, 0x91, 0xa1, 0x08, 0x23, + 0x42, 0xb1, 0xc1, 0x15, 0x52, + 0xd1, 0xf0, 0x24, 0x33, 0x62, + 0x72, 0x82, 0x09, 0x0a, 0x16, + 0x17, 0x18, 0x19, 0x1a, 0x25, + 0x26, 0x27, 0x28, 0x29, 0x2a, + 0x34, 0x35, 0x36, 0x37, 0x38, + 0x39, 0x3a, 0x43, 0x44, 0x45, + 0x46, 0x47, 0x48, 0x49, 0x4a, + 0x53, 0x54, 0x55, 0x56, 0x57, + 0x58, 0x59, 0x5a, 0x63, 0x64, + 0x65, 0x66, 0x67, 0x68, 0x69, + 0x6a, 0x73, 0x74, 0x75, 0x76, + 0x77, 0x78, 0x79, 0x7a, 0x83, + 0x84, 0x85, 0x86, 0x87, 0x88, + 0x89, 0x8a, 0x92, 0x93, 0x94, + 0x95, 0x96, 0x97, 0x98, 0x99, + 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, + 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, + 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, + 0xb7, 0xb8, 0xb9, 0xba, 0xc2, + 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, + 0xc8, 0xc9, 0xca, 0xd2, 0xd3, + 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, + 0xd9, 0xda, 0xe1, 0xe2, 0xe3, + 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, + 0xe9, 0xea, 0xf1, 0xf2, 0xf3, + 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, + 0xf9, 0xfa }, false); + + /** + * The standard JPEG DC chrominance Huffman table. + */ + public static final JPEGHuffmanTable StdDCChrominance = + new JPEGHuffmanTable(new short[] { 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, + 1, 0, 0, 0, 0, 0 }, + new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11 }, false); + + /** + * The standard JPEG DC luminance Huffman table. + */ + public static final JPEGHuffmanTable StdDCLuminance = + new JPEGHuffmanTable(new short[] { 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, + 0, 0, 0, 0, 0, 0 }, + new short[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, + 10, 11 }, false); + + /** + * Construct and initialize a Huffman table. Copies are created of + * the array arguments. lengths[index] stores the number of Huffman + * values with Huffman codes of length index + 1. The values array + * stores the Huffman values in order of increasing code length. + * + * @param lengths an array of Huffman code lengths + * @param values a sorted array of Huffman values + * @throws IllegalArgumentException if either parameter is null, if + * lengths.length > 16 or values.length > 256, if any value in + * length or values is negative, or if the parameters do not + * describe a valid Huffman table + */ + public JPEGHuffmanTable(short[] lengths, short[] values) + { + // Create copies of the lengths and values arguments. + this(checkLengths(lengths), checkValues(values, lengths), true); + } + + /** + * Private constructor that avoids unnecessary copying and argument + * checking. + * + * @param lengths an array of Huffman code lengths + * @param values a sorted array of Huffman values + * @param copy true if copies should be created of the given arrays + */ + private JPEGHuffmanTable(short[] lengths, short[] values, boolean copy) + { + this.lengths = copy ? (short[]) lengths.clone() : lengths; + this.values = copy ? (short[]) values.clone() : values; + } + + private static short[] checkLengths(short[] lengths) + { + if (lengths == null || lengths.length > 16) + throw new IllegalArgumentException("invalid length array"); + + for (int i = 0; i < lengths.length; i++) + { + if (lengths[i] < 0) + throw new IllegalArgumentException("negative length"); + } + + int sum = 0; + for (int i = 0; i < lengths.length; i++) + { + if (lengths[i] > ((1 << (i + 1)) - 1)) + throw new IllegalArgumentException("invalid number of codes" + + " for code length " + (i + 1)); + sum += lengths[i]; + } + + return lengths; + } + + private static short[] checkValues(short[] values, short[] lengths) + { + if (values == null || values.length > 256) + throw new IllegalArgumentException("invalid values array"); + + for (int i = 0; i < values.length; i++) + { + if (values[i] < 0) + throw new IllegalArgumentException("negative value"); + } + // lengths is known-valid by this point. + int sum = 0; + for (int i = 0; i < lengths.length; i++) + sum += lengths[i]; + + if (values.length != sum) + throw new IllegalArgumentException("invalid number of values" + + " for number of codes"); + + return values; + } + + /** + * Retrieve a copy of the array of Huffman code lengths. If the + * returned array is called lengthcount, there are + * lengthcount[index] codes of length index + 1. + * + * @return a copy of the array of Huffman code lengths + */ + public short[] getLengths() + { + return (short[]) lengths.clone(); + } + + /** + * Retrieve a copy of the array of Huffman values, sorted in order + * of increasing code length. + * + * @return a copy of the array of Huffman values + */ + public short[] getValues() + { + return (short[]) values.clone(); + } + + /** + * Create a string representation of this JPEG Huffman table. + * + * @return a string representation of this JPEG Huffman table. + */ + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(); + + buffer.append("JPEGHuffmanTable:\nlengths:"); + + for (int i = 0; i < lengths.length; i++) + buffer.append(" " + lengths[i]); + + buffer.append("\nvalues:"); + + for (int i = 0; i < values.length; i++) + buffer.append(" " + values[i]); + + return buffer.toString(); + } +} diff --git a/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageReadParam.java b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageReadParam.java new file mode 100644 index 000000000..b570922f9 --- /dev/null +++ b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageReadParam.java @@ -0,0 +1,161 @@ +/* JPEGImageReadParam.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package javax.imageio.plugins.jpeg; + +import javax.imageio.ImageReadParam; + +/** + * The JPEGImageReadParam class is only used to set JPEG decoding + * tables for streams that do not provide their own tables. If a + * stream does not provide tables and a custom JPEGImageReadParam is + * not provided, then the standard JPEG tables are used from the + * JPEGQTable and JPEGHuffmanTable classes. If a stream does provide + * decoding tables then JPEGImageReadParam will be ignored. + * JPEGImageReadParam cannot be used to retrieve the tables from a + * stream. Instead, use IIOMetadata for this purpose. + * + * A JPEGImageReadParam instance is retrieved from the built-in JPEG + * ImageReader using the getDefaultImageReadParam method. + */ +public class JPEGImageReadParam + extends ImageReadParam +{ + private JPEGQTable[] qTables; + private JPEGHuffmanTable[] DCHuffmanTables; + private JPEGHuffmanTable[] ACHuffmanTables; + + /** + * Construct a JPEGImageReadParam. + */ + public JPEGImageReadParam() + { + super(); + } + + /** + * Check if the decoding tables are set. + * + * @return true if the decoding tables are set, false otherwise + */ + public boolean areTablesSet() + { + // If qTables is not null then all tables are set. + return (qTables != null); + } + + /** + * Set the quantization and Huffman tables that will be used to + * decode the stream. Copies are created of the array arguments. + * The number of Huffman tables must be the same in both Huffman + * table arrays. No argument may be null and no array may be longer + * than four elements. + * + * @param qTables JPEG quantization tables + * @param DCHuffmanTables JPEG DC Huffman tables + * @param ACHuffmanTables JPEG AC Huffman tables + * + * @throws IllegalArgumentException if any argument is null, if any + * of the arrays are longer than four elements, or if the Huffman + * table arrays do not have the same number of elements + */ + public void setDecodeTables(JPEGQTable[] qTables, + JPEGHuffmanTable[] DCHuffmanTables, + JPEGHuffmanTable[] ACHuffmanTables) + { + if (qTables == null || DCHuffmanTables == null || ACHuffmanTables == null) + throw new IllegalArgumentException("null argument"); + + if (qTables.length > 4 || DCHuffmanTables.length > 4 + || ACHuffmanTables.length > 4) + throw new IllegalArgumentException("argument has too many elements"); + + if (DCHuffmanTables.length != ACHuffmanTables.length) + throw new IllegalArgumentException("Huffman table arrays differ in length"); + + // Do a shallow copy. JPEGQTable's data is not directly + // modifyable since JPEGQTable.getTable returns a copy. Therefore + // it is safe to have multiple references to a single JPEGQTable. + // Likewise for JPEGHuffmanTable. + this.qTables = (JPEGQTable[]) qTables.clone(); + this.DCHuffmanTables = (JPEGHuffmanTable[]) DCHuffmanTables.clone(); + this.ACHuffmanTables = (JPEGHuffmanTable[]) ACHuffmanTables.clone(); + } + + /** + * Clear the quantization and Huffman decoding tables. + */ + public void unsetDecodeTables() + { + qTables = null; + DCHuffmanTables = null; + ACHuffmanTables = null; + } + + /** + * Retrieve the quantization tables. + * + * @returns an array of JPEG quantization tables + */ + public JPEGQTable[] getQTables() + { + return qTables == null ? qTables : (JPEGQTable[]) qTables.clone(); + } + + /** + * Retrieve the DC Huffman tables. + * + * @return an array of JPEG DC Huffman tables + */ + public JPEGHuffmanTable[] getDCHuffmanTables() + { + return DCHuffmanTables == null ? DCHuffmanTables + : (JPEGHuffmanTable[]) DCHuffmanTables.clone(); + } + + /** + * Retrieve the AC Huffman tables. + * + * @return an array of JPEG AC Huffman tables + */ + public JPEGHuffmanTable[] getACHuffmanTables() + { + return ACHuffmanTables == null ? ACHuffmanTables + : (JPEGHuffmanTable[]) ACHuffmanTables.clone(); + } +} diff --git a/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageWriteParam.java b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageWriteParam.java new file mode 100644 index 000000000..1e7989e82 --- /dev/null +++ b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGImageWriteParam.java @@ -0,0 +1,293 @@ +/* JPEGImageWriteParam.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package javax.imageio.plugins.jpeg; + +import java.util.Locale; +import java.util.PropertyResourceBundle; +import java.util.ResourceBundle; +import javax.imageio.ImageWriteParam; + +/** + * The JPEGImageWriteParam class can be used to specify tables and + * settings used in the JPEG encoding process. Encoding tables are + * taken from the metadata associated with the output stream, and + * failing that (if the metadata tables are null) from an instance of + * JPEGImageWriteParam. The default metadata uses the standard JPEG + * tables from the JPEGQTable and JPEGHuffmanTable classes. Non-null + * metadata tables override JPEGImageWriteParam tables. Compression + * settings range from 1.0, best compression, through 0.75, default + * compression, to 0.0, worst compression. + * + * A JPEGImageWriteParam instance is retrieved from the built-in JPEG + * ImageWriter using the getDefaultImageWriteParam method. + */ +public class JPEGImageWriteParam + extends ImageWriteParam +{ + private JPEGQTable[] qTables; + private JPEGHuffmanTable[] DCHuffmanTables; + private JPEGHuffmanTable[] ACHuffmanTables; + private boolean optimize; + private String[] compressionQualityDescriptions; + private float[] compressionQualityValues; + + /** + * Localized messages are stored in separate files. + */ + private ResourceBundle messages; + + /** + * Construct a JPEGImageWriteParam with the following state: tiling + * is not supported, progressive mode is supported, initial + * progressive mode is MODE_DISABLED, compression is supported, one + * compression type named "JPEG" is supported and the default + * compression quality is 0.75f. Compression type names and + * compression quality descriptions are localized to the given + * locale. + * + * @param locale the locale used for message localization + */ + public JPEGImageWriteParam(Locale locale) + { + super(locale); + + // Get localized compression type and compression quality + // description strings for the given locale. + messages = PropertyResourceBundle.getBundle + ("javax/imageio/plugins/jpeg/MessagesBundle", locale); + + // Initialize inherited ImageWriter fields. + canWriteTiles = false; + canWriteProgressive = true; + progressiveMode = MODE_DISABLED; + canWriteCompressed = true; + compressionTypes = new String[] + { + messages.getString("compression.types.jpeg") + }; + compressionType = compressionTypes[0]; + compressionQuality = 0.75f; + } + + /** + * Reset the compression quality to 0.75f. + */ + public void unsetCompression() + { + compressionQuality = 0.75f; + } + + /** + * Check if compression algorithm is lossless. JPEGImageWriteParam + * overrides this ImageWriteParam method to always return false + * since JPEG compression is inherently lossy. + * + * @return false + */ + public boolean isCompressionLossless() + { + return false; + } + + /** + * Retrieve an array of compression quality descriptions. These + * messages are localized using the locale provided upon + * construction. Each compression quality description in the + * returned array corresponds to the compression quality value at + * the same index in the array returned by + * getCompressionQualityValues. + * + * @return an array of strings each of which describes a compression + * quality value + */ + public String[] getCompressionQualityDescriptions() + { + // Make sure exceptions are thrown when this image write param is + // in the wrong state. + super.getCompressionQualityDescriptions(); + + if (compressionQualityDescriptions == null) + { + compressionQualityDescriptions = new String[] + { + messages.getString("compression.minimum"), + messages.getString("compression.default"), + messages.getString("compression.maximum") + }; + } + + return compressionQualityDescriptions; + } + + /** + * Retrieve an array of compression quality values, ordered from + * lowest quality to highest quality. + * + * @return an array of compressions quality values + */ + public float[] getCompressionQualityValues() + { + // Make sure exceptions are thrown when this image write param is + // in the wrong state. + super.getCompressionQualityValues(); + + if (compressionQualityValues == null) + compressionQualityValues = new float[] { 0.05f, 0.75f, 0.95f }; + + return compressionQualityValues; + } + + /** + * Check if the encoding tables are set. + * + * @return true if the encoding tables are set, false otherwise + */ + public boolean areTablesSet() + { + // If qTables is not null then all tables are set. + return (qTables != null); + } + + /** + * Set the quantization and Huffman tables that will be used to + * encode the stream. Copies are created of the array arguments. + * The number of Huffman tables must be the same in both Huffman + * table arrays. No argument may be null and no array may be longer + * than four elements. + * + * @param qTables JPEG quantization tables + * @param DCHuffmanTables JPEG DC Huffman tables + * @param ACHuffmanTables JPEG AC Huffman tables + * + * @throws IllegalArgumentException if any argument is null, if any + * of the arrays are longer than four elements, or if the Huffman + * table arrays do not have the same number of elements + */ + public void setEncodeTables(JPEGQTable[] qTables, + JPEGHuffmanTable[] DCHuffmanTables, + JPEGHuffmanTable[] ACHuffmanTables) + { + if (qTables == null || DCHuffmanTables == null || ACHuffmanTables == null) + throw new IllegalArgumentException("null argument"); + + if (qTables.length > 4 || DCHuffmanTables.length > 4 + || ACHuffmanTables.length > 4) + throw new IllegalArgumentException("argument has too many elements"); + + if (DCHuffmanTables.length != ACHuffmanTables.length) + throw new IllegalArgumentException("Huffman table arrays differ in length"); + + // Do a shallow copy. JPEGQTable's data is not directly + // modifyable since JPEGQTable.getTable returns a copy. Therefore + // it is safe to have multiple references to a single JPEGQTable. + // Likewise for JPEGHuffmanTable. + this.qTables = (JPEGQTable[]) qTables.clone(); + this.DCHuffmanTables = (JPEGHuffmanTable[]) DCHuffmanTables.clone(); + this.ACHuffmanTables = (JPEGHuffmanTable[]) ACHuffmanTables.clone(); + } + + /** + * Clear the quantization and Huffman encoding tables. + */ + public void unsetEncodeTables() + { + qTables = null; + DCHuffmanTables = null; + ACHuffmanTables = null; + } + + /** + * Retrieve the quantization tables. + * + * @returns an array of JPEG quantization tables + */ + public JPEGQTable[] getQTables() + { + return qTables == null ? qTables : (JPEGQTable[]) qTables.clone(); + } + + /** + * Retrieve the DC Huffman tables. + * + * @return an array of JPEG DC Huffman tables + */ + public JPEGHuffmanTable[] getDCHuffmanTables() + { + return DCHuffmanTables == null ? DCHuffmanTables + : (JPEGHuffmanTable[]) DCHuffmanTables.clone(); + } + + /** + * Retrieve the AC Huffman tables. + * + * @return an array of JPEG AC Huffman tables + */ + public JPEGHuffmanTable[] getACHuffmanTables() + { + return ACHuffmanTables == null ? ACHuffmanTables + : (JPEGHuffmanTable[]) ACHuffmanTables.clone(); + } + + /** + * Specify whether or not Huffman tables written to the output + * stream should be optimized. Every image encoded with this flag + * set will contain a Huffman table, and the generated Huffman + * tables will override those specified in the metadata. + * + * @param optimize true to generate optimized Huffman tables, false + * otherwise + */ + public void setOptimizeHuffmanTables(boolean optimize) + { + this.optimize = optimize; + } + + /** + * Check whether or not Huffman tables written to the output stream + * will be optimized. Unless otherwise set using + * setOptimizeHuffmanTables, this returns false. + * + * @return true Huffman tables written to the output stream will be + * optimized, false otherwise + */ + public boolean getOptimizeHuffmanTables() + { + return optimize; + } +} diff --git a/libjava/classpath/javax/imageio/plugins/jpeg/JPEGQTable.java b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGQTable.java new file mode 100644 index 000000000..259d0aa64 --- /dev/null +++ b/libjava/classpath/javax/imageio/plugins/jpeg/JPEGQTable.java @@ -0,0 +1,198 @@ +/* JPEGQTable.java -- + Copyright (C) 2006 Free Software Foundation, Inc. + + This file is part of GNU Classpath. + + GNU Classpath is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2, or (at your option) + any later version. + + GNU Classpath is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + General Public License for more details. + + You should have received a copy of the GNU General Public License + along with GNU Classpath; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + 02110-1301 USA. + + Linking this library statically or dynamically with other modules is + making a combined work based on this library. Thus, the terms and + conditions of the GNU General Public License cover the whole + combination. + + As a special exception, the copyright holders of this library give you + permission to link this library with independent modules to produce an + executable, regardless of the license terms of these independent + modules, and to copy and distribute the resulting executable under + terms of your choice, provided that you also meet, for each linked + independent module, the terms and conditions of the license of that + module. An independent module is a module which is not derived from + or based on this library. If you modify this library, you may extend + this exception to your version of the library, but you are not + obligated to do so. If you do not wish to do so, delete this + exception statement from your version. */ + + +package javax.imageio.plugins.jpeg; + +import gnu.java.lang.CPStringBuilder; + +/** + * The JPEGQTable class represents a quantization table that can be + * used to encode or decode a JPEG stream. The standard JPEG + * luminance and chrominance quantization tables are provided as + * static fields. Table entries are stored in natural order, not + * zig-zag order. + */ +public class JPEGQTable +{ + /** + * The table entries, stored in natural order. + */ + private int[] table; + + /** + * The standard JPEG luminance quantization table. Values are + * stored in natural order. + */ + public static final JPEGQTable K1Luminance = new JPEGQTable(new int[] + { + 16, 11, 10, 16, 24, 40, 51, 61, + 12, 12, 14, 19, 26, 58, 60, 55, + 14, 13, 16, 24, 40, 57, 69, 56, + 14, 17, 22, 29, 51, 87, 80, 62, + 18, 22, 37, 56, 68, 109, 103, 77, + 24, 35, 55, 64, 81, 104, 113, 92, + 49, 64, 78, 87, 103, 121, 120, 101, + 72, 92, 95, 98, 112, 100, 103, 99 + }, false); + + /** + * The standard JPEG luminance quantization table, scaled by + * one-half. Values are stored in natural order. + */ + public static final JPEGQTable K1Div2Luminance = + K1Luminance.getScaledInstance(0.5f, true); + + /** + * The standard JPEG chrominance quantization table. Values are + * stored in natural order. + */ + public static final JPEGQTable K2Chrominance = new JPEGQTable(new int[] + { + 17, 18, 24, 47, 99, 99, 99, 99, + 18, 21, 26, 66, 99, 99, 99, 99, + 24, 26, 56, 99, 99, 99, 99, 99, + 47, 66, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99, + 99, 99, 99, 99, 99, 99, 99, 99 + }, false); + + /** + * The standard JPEG chrominance quantization table, scaled by + * one-half. Values are stored in natural order. + */ + public static final JPEGQTable K2Div2Chrominance = + K2Chrominance.getScaledInstance(0.5f, true); + + /** + * Construct a new JPEG quantization table. A copy is created of + * the table argument. + * + * @param table the 64-element value table, stored in natural order + * + * @throws IllegalArgumentException if the table is null or if + * table's length is not equal to 64. + */ + public JPEGQTable(int[] table) + { + this(checkTable(table), true); + } + + /** + * Private constructor that avoids unnecessary copying and argument + * checking. + * + * @param table the 64-element value table, stored in natural order + * @param copy true if a copy should be created of the given table + */ + private JPEGQTable(int[] table, boolean copy) + { + this.table = copy ? (int[]) table.clone() : table; + } + + private static int[] checkTable(int[] table) + { + if (table == null || table.length != 64) + throw new IllegalArgumentException("invalid JPEG quantization table"); + + return table; + } + + /** + * Retrieve a copy of the quantization values for this table. + * + * @return a copy of the quantization value array + */ + public int[] getTable() + { + return (int[]) table.clone(); + } + + /** + * Retrieve a copy of this JPEG quantization table with every value + * scaled by the given scale factor, and clamped from 1 to 255 + * baseline or from 1 to 32767 otherwise. + * + * @param scaleFactor the factor by which to scale this table + * @param forceBaseline clamp scaled values to a maximum of 255 if + * true, 32767 if false + * + * @return a new scaled JPEG quantization table + */ + public JPEGQTable getScaledInstance(float scaleFactor, + boolean forceBaseline) + { + int[] scaledTable = getTable(); + int max = forceBaseline ? 255 : 32767; + + for (int i = 0; i < scaledTable.length; i++) + { + scaledTable[i] = Math.round (scaleFactor * (float) scaledTable[i]); + if (scaledTable[i] < 1) + scaledTable[i] = 1; + else if (scaledTable[i] > max) + scaledTable[i] = max; + } + + // Do not copy scaledTable. It is already a copy because we used + // getTable to retrieve it. + return new JPEGQTable(scaledTable, false); + } + + /** + * Create a string representing this JPEG quantization table. + */ + public String toString() + { + CPStringBuilder buffer = new CPStringBuilder(); + + buffer.append("JPEGQTable:\n"); + for (int i = 0; i < 8; i++) + { + buffer.append(" "); + for (int j = 0; j < 8; j++) + { + buffer.append(table[i * 8 + j] + " "); + } + buffer.append("\n"); + } + + return buffer.toString(); + } +} |