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.readAndVerifyBytes;
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.startsWith;
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 App2Segment extends AppnSegment implements Comparable<App2Segment> {
32 private final byte[] iccBytes;
33 public final int curMarker;
34 public final int numMarkers;
35
36 public App2Segment(final int marker, final byte[] segmentData) throws ImagingException, IOException {
37 this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
38 }
39
40 public App2Segment(final int marker, int markerLength, final InputStream is2) throws ImagingException, IOException {
41 super(marker, markerLength, is2);
42
43 if (startsWith(getSegmentData(), JpegConstants.ICC_PROFILE_LABEL)) {
44 final InputStream is = new ByteArrayInputStream(getSegmentData());
45
46 readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL, "Not a Valid App2 Segment: missing ICC Profile label");
47
48 curMarker = readByte("curMarker", is, "Not a valid App2 Marker");
49 numMarkers = readByte("numMarkers", is, "Not a valid App2 Marker");
50
51 markerLength -= JpegConstants.ICC_PROFILE_LABEL.size();
52 markerLength -= 1 + 1;
53
54 iccBytes = readBytes("App2 Data", is, markerLength, "Invalid App2 Segment: insufficient data");
55 } else {
56
57 curMarker = -1;
58 numMarkers = -1;
59 iccBytes = null;
60 }
61 }
62
63 @Override
64 public int compareTo(final App2Segment other) {
65 return curMarker - other.curMarker;
66 }
67
68 @Override
69 public boolean equals(final Object obj) {
70 if (obj instanceof App2Segment) {
71 final App2Segment other = (App2Segment) obj;
72 return curMarker == other.curMarker;
73 }
74 return false;
75 }
76
77
78
79
80 public byte[] getIccBytes() {
81 return iccBytes != null ? iccBytes.clone() : null;
82 }
83
84 @Override
85 public int hashCode() {
86 return curMarker;
87 }
88 }