View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.formats.jpeg.segments;
18  
19  import static org.apache.commons.imaging.common.BinaryFunctions.read2Bytes;
20  import static org.apache.commons.imaging.common.BinaryFunctions.readByte;
21  import static org.apache.commons.imaging.common.BinaryFunctions.readBytes;
22  import static org.apache.commons.imaging.common.BinaryFunctions.skipBytes;
23  
24  import java.io.ByteArrayInputStream;
25  import java.io.IOException;
26  import java.io.InputStream;
27  
28  import org.apache.commons.imaging.ImagingException;
29  import org.apache.commons.imaging.formats.jpeg.JpegConstants;
30  
31  public class JfifSegment extends AbstractSegment {
32      public final int jfifMajorVersion;
33      public final int jfifMinorVersion;
34      public final int densityUnits;
35      public final int xDensity;
36      public final int yDensity;
37  
38      public final int xThumbnail;
39      public final int yThumbnail;
40      public final int thumbnailSize;
41  
42      public JfifSegment(final int marker, final byte[] segmentData) throws ImagingException, IOException {
43          this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
44      }
45  
46      public JfifSegment(final int marker, final int markerLength, final InputStream is) throws ImagingException, IOException {
47          super(marker, markerLength);
48  
49          final byte[] signature = readBytes(is, JpegConstants.JFIF0_SIGNATURE.size());
50          if (!JpegConstants.JFIF0_SIGNATURE.equals(signature) && !JpegConstants.JFIF0_SIGNATURE_ALTERNATIVE.equals(signature)) {
51              throw new ImagingException("Not a Valid JPEG File: missing JFIF string");
52          }
53  
54          jfifMajorVersion = readByte("jfifMajorVersion", is, "Not a Valid JPEG File");
55          jfifMinorVersion = readByte("jfifMinorVersion", is, "Not a Valid JPEG File");
56          densityUnits = readByte("densityUnits", is, "Not a Valid JPEG File");
57          xDensity = read2Bytes("xDensity", is, "Not a Valid JPEG File", getByteOrder());
58          yDensity = read2Bytes("yDensity", is, "Not a Valid JPEG File", getByteOrder());
59  
60          xThumbnail = readByte("xThumbnail", is, "Not a Valid JPEG File");
61          yThumbnail = readByte("yThumbnail", is, "Not a Valid JPEG File");
62          thumbnailSize = xThumbnail * yThumbnail;
63          if (thumbnailSize > 0) {
64              skipBytes(is, thumbnailSize, "Not a Valid JPEG File: missing thumbnail");
65  
66          }
67      }
68  
69      @Override
70      public String getDescription() {
71          return "JFIF (" + getSegmentType() + ")";
72      }
73  
74  }