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 java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  
23  import org.apache.commons.imaging.ImageReadException;
24  import org.apache.commons.imaging.formats.jpeg.JpegConstants;
25  
26  import static org.apache.commons.imaging.common.BinaryFunctions.*;
27  
28  public class App2Segment extends AppnSegment implements Comparable<App2Segment> {
29      private final byte[] iccBytes;
30      public final int curMarker;
31      public final int numMarkers;
32  
33      public App2Segment(final int marker, final byte[] segmentData)
34              throws ImageReadException, IOException {
35          this(marker, segmentData.length, new ByteArrayInputStream(segmentData));
36      }
37  
38      public App2Segment(final int marker, int markerLength, final InputStream is2)
39              throws ImageReadException, IOException {
40          super(marker, markerLength, is2);
41  
42          if (startsWith(getSegmentData(),
43                  JpegConstants.ICC_PROFILE_LABEL)) {
44              final InputStream is = new ByteArrayInputStream(getSegmentData());
45  
46              readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL,
47                      "Not a Valid App2 Segment: missing ICC Profile label");
48  
49              curMarker = readByte("curMarker", is, "Not a valid App2 Marker");
50              numMarkers = readByte("numMarkers", is, "Not a valid App2 Marker");
51  
52              markerLength -= JpegConstants.ICC_PROFILE_LABEL.size();
53              markerLength -= (1 + 1);
54  
55              iccBytes = readBytes("App2 Data", is, markerLength, "Invalid App2 Segment: insufficient data");
56          } else {
57              // debugByteArray("Unknown APP2 Segment Type", bytes);
58              curMarker = -1;
59              numMarkers = -1;
60              iccBytes = null;
61          }
62      }
63  
64      @Override
65      public boolean equals(final Object obj) {
66          if (obj instanceof App2Segment) {
67              final App2Segment other = (App2Segment) obj;
68              return curMarker == other.curMarker;
69          }
70          return false;
71      }
72  
73      @Override
74      public int hashCode() {
75          return curMarker;
76      }
77  
78      @Override
79      public int compareTo(final App2Segment other) {
80          return curMarker - other.curMarker;
81      }
82  
83      /**
84       * @return the iccBytes
85       */
86      public byte[] getIccBytes() {
87          return iccBytes; // TODO clone?
88      }
89  
90      // public String getDescription()
91      // {
92      // return "APPN (APP"
93      // + (marker - JpegImageParser.JPEG_APP0)
94      // + ") (" + getDescription() + ")";
95      // }
96  }