1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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 }