FileDocCategorySizeDatePackage
JPEGHuffmanTable.javaAPI DocAndroid 1.5 API8688Wed May 06 22:41:54 BST 2009javax.imageio.plugins.jpeg

JPEGHuffmanTable

public class JPEGHuffmanTable extends Object
The JPEGHuffmanTable class represents a single JPEG Huffman table. It contains the standard tables from the JPEG specification.
since
Android 1.0

Fields Summary
public static final JPEGHuffmanTable
StdDCLuminance
The standard DC luminance Huffman table .
public static final JPEGHuffmanTable
StdDCChrominance
The standard DC chrominance Huffman table.
public static final JPEGHuffmanTable
StdACLuminance
The standard AC luminance Huffman table.
public static final JPEGHuffmanTable
StdACChrominance
The standard AC chrominance Huffman table.
private short[]
lengths
The lengths.
private short[]
values
The values.
Constructors Summary
JPEGHuffmanTable(short[] lengths, short[] values, boolean copy)
Instantiates a new jPEG huffman table.

param
lengths the lengths
param
values the values
param
copy the copy


                                                             
          
        // Construction of standard tables without checks
        // The third param is dummy
        // Could be also used for copying of the existing tables
        this.lengths = lengths;
        this.values = values;
    
public JPEGHuffmanTable(short[] lengths, short[] values)
Instantiates a new JPEGHuffmanTable.

param
lengths the array of shorts lengths.
param
values the array of shorts containing the values in order of increasing code length.

        if (lengths == null) {
            throw new IllegalArgumentException("lengths array is null!");
        }
        if (values == null) {
            throw new IllegalArgumentException("values array is null!");
        }
        if (lengths.length > 16) { // According to the spec
            throw new IllegalArgumentException("lengths array is too long!");
        }
        if (values.length > 256) { // According to the spec
            throw new IllegalArgumentException("values array is too long");
        }
        for (short length : lengths) {
            if (length < 0) {
                throw new IllegalArgumentException("Values in lengths array must be non-negative.");
            }
        }
        for (short value : values) {
            if (value < 0) {
                throw new IllegalArgumentException("Values in values array must be non-negative.");
            }
        }

        checkHuffmanTable(lengths, values);

        this.lengths = new short[lengths.length];
        this.values = new short[values.length];
        System.arraycopy(lengths, 0, this.lengths, 0, lengths.length);
        System.arraycopy(values, 0, this.values, 0, values.length);
    
Methods Summary
private static voidcheckHuffmanTable(short[] lengths, short[] values)
Check huffman table.

param
lengths the lengths.
param
values the values.

        int numLeaves = 0;
        int possibleLeaves = 2;
        for (short length : lengths) {
            numLeaves += length;
            possibleLeaves -= length;
            if (possibleLeaves < 0) {
                throw new IllegalArgumentException(
                        "Invalid Huffman table provided, lengths are incorrect.");
            }
            possibleLeaves <<= 1;
        }

        if (values.length != numLeaves) {
            throw new IllegalArgumentException(
                    "Invalid Huffman table provided, sum of lengths != values.");
        }
    
public short[]getLengths()
Gets an array of lengths in the Huffman table.

return
the array of short values representing the length values in the Huffman table.

        short newLengths[] = new short[lengths.length];
        System.arraycopy(lengths, 0, newLengths, 0, lengths.length);
        return newLengths;
    
public short[]getValues()
Gets an array of values represented by increasing length of their codes.

return
the array of values.

        short newValues[] = new short[values.length];
        System.arraycopy(values, 0, newValues, 0, values.length);
        return newValues;
    
public java.lang.StringtoString()
Returns the string representation of this JPEGHuffmanTable object.

return
the string representation of this JPEGHuffmanTable object.

        StringBuffer sb = new StringBuffer();

        sb.append("JPEGHuffmanTable:\nlengths:");
        for (short length : lengths) {
            sb.append(' ").append(length);
        }

        sb.append("\nvalues:");
        for (short value : values) {
            sb.append(' ").append(value);
        }

        return sb.toString();