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.ImageReadException; 029import org.apache.commons.imaging.formats.jpeg.JpegConstants; 030 031public class JfifSegment extends Segment { 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 @Override 043 public String getDescription() { 044 return "JFIF (" + getSegmentType() + ")"; 045 } 046 047 public JfifSegment(final int marker, final byte[] segmentData) 048 throws ImageReadException, IOException { 049 this(marker, segmentData.length, new ByteArrayInputStream(segmentData)); 050 } 051 052 public JfifSegment(final int marker, final int markerLength, final InputStream is) 053 throws ImageReadException, IOException { 054 super(marker, markerLength); 055 056 final byte[] signature = readBytes(is, JpegConstants.JFIF0_SIGNATURE.size()); 057 if (!JpegConstants.JFIF0_SIGNATURE.equals(signature) 058 && !JpegConstants.JFIF0_SIGNATURE_ALTERNATIVE.equals(signature)) { 059 throw new ImageReadException( 060 "Not a Valid JPEG File: missing JFIF string"); 061 } 062 063 jfifMajorVersion = readByte("JFIF_major_version", is, 064 "Not a Valid JPEG File"); 065 jfifMinorVersion = readByte("JFIF_minor_version", is, 066 "Not a Valid JPEG File"); 067 densityUnits = readByte("density_units", is, "Not a Valid JPEG File"); 068 xDensity = read2Bytes("x_density", is, "Not a Valid JPEG File", getByteOrder()); 069 yDensity = read2Bytes("y_density", is, "Not a Valid JPEG File", getByteOrder()); 070 071 xThumbnail = readByte("x_thumbnail", is, "Not a Valid JPEG File"); 072 yThumbnail = readByte("y_thumbnail", is, "Not a Valid JPEG File"); 073 thumbnailSize = xThumbnail * yThumbnail; 074 if (thumbnailSize > 0) { 075 skipBytes(is, thumbnailSize, 076 "Not a Valid JPEG File: missing thumbnail"); 077 078 } 079 } 080 081}