    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 */
    017package org.apache.commons.imaging.formats.jpeg.segments;
    018
    019import static org.apache.commons.imaging.common.BinaryFunctions.read2Bytes;
    020import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
    021import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
    022import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
    023
    024import java.io.ByteArrayInputStream;
    025import java.io.IOException;
    026import java.io.InputStream;
    027
    028import org.apache.commons.imaging.ImagingException;
    029import org.apache.commons.imaging.formats.jpeg.JpegConstants;
    030
    031public final class JfifSegment extends AbstractSegment {
    032    public final int jfifMajorVersion;
    033    public final int jfifMinorVersion;
    034    public final int densityUnits;
    035    public final int xDensity;
    036    public final int yDensity;
    037
    038    public final int xThumbnail;
    039    public final int yThumbnail;
    040    public final int thumbnailSize;
    041
    042    public JfifSegment(final int marker, final byte[] segmentData) throws ImagingException, IOException {
    043        this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
    044    }
    045
    046    public JfifSegment(final int marker, final int markerLength, final InputStream is) throws ImagingException, IOException {
    047        super(marker, markerLength);
    048
    049        final byte[] signature = readBytes(is, JpegConstants.JFIF0_SIGNATURE.size());
    050        if (!JpegConstants.JFIF0_SIGNATURE.equals(signature) && !JpegConstants.JFIF0_SIGNATURE_ALTERNATIVE.equals(signature)) {
    051            throw new ImagingException("Not a Valid JPEG File: missing JFIF string");
    052        }
    053
    054        jfifMajorVersion = readByte("jfifMajorVersion", is, "Not a Valid JPEG File");
    055        jfifMinorVersion = readByte("jfifMinorVersion", is, "Not a Valid JPEG File");
    056        densityUnits = readByte("densityUnits", is, "Not a Valid JPEG File");
    057        xDensity = read2Bytes("xDensity", is, "Not a Valid JPEG File", getByteOrder());
    058        yDensity = read2Bytes("yDensity", is, "Not a Valid JPEG File", getByteOrder());
    059
    060        xThumbnail = readByte("xThumbnail", is, "Not a Valid JPEG File");
    061        yThumbnail = readByte("yThumbnail", is, "Not a Valid JPEG File");
    062        thumbnailSize = xThumbnail * yThumbnail;
    063        if (thumbnailSize > 0) {
    064            skipBytes(is, thumbnailSize, "Not a Valid JPEG File: missing thumbnail");
    065
    066        }
    067    }
    068
    069    @Override
    070    public String getDescription() {
    071        return "JFIF (" + getSegmentType() + ")";
    072    }
    073
    074}