Package org.apache.sanselan.formats.jpeg.segments

Source Code of org.apache.sanselan.formats.jpeg.segments.DQTSegment

/*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*       http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*  under the License.
*/

package org.apache.sanselan.formats.jpeg.segments;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.sanselan.ImageReadException;

public class DQTSegment extends Segment
{
    public final ArrayList quantizationTables = new ArrayList();

    public static class QuantizationTable
    {
        public final int precision;
        public final int destinationIdentifier;
        public final int[] elements;

        public QuantizationTable(int precision, int destinationIdentifier,
                int[] elements)
        {
            this.precision = precision;
            this.destinationIdentifier = destinationIdentifier;
            this.elements = elements;
        }
    }

    public DQTSegment(int marker, byte[] segmentData)
            throws ImageReadException, IOException
    {
        this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
    }

    public DQTSegment(int marker, int length, InputStream is)
            throws ImageReadException, IOException
    {
        super(marker, length);

        while (length > 0)
        {
            int precisionAndDestination = readByte(
                    "QuantizationTablePrecisionAndDestination", is,
                    "Not a Valid JPEG File");
            length--;
            int precision = (precisionAndDestination >> 4) & 0xf;
            int destinationIdentifier = precisionAndDestination & 0xf;

            int[] elements = new int[64];
            for (int i = 0; i < 64; i++)
            {
                if (precision == 0)
                {
                    elements[i] = 0xff & readByte("QuantizationTableElement",
                            is, "Not a Valid JPEG File");
                    length--;
                }
                else if (precision == 1)
                {
                    elements[i] = read2Bytes("QuantizationTableElement",
                            is, "Not a Valid JPEG File");
                    length -= 2;
                }
                else
                {
                    throw new ImageReadException("Quantization table precision '" +
                            precision + "' is invalid");
                }
            }

            quantizationTables.add(new QuantizationTable(
                    precision, destinationIdentifier, elements));
        }
    }

    public String getDescription()
    {
        return "DQT (" + getSegmentType() + ")";
    }
}
TOP

Related Classes of org.apache.sanselan.formats.jpeg.segments.DQTSegment

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.