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.readAndVerifyBytes; 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.startsWith; 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 App2Segment extends AppnSegment implements Comparable<App2Segment> { 032 private final byte[] iccBytes; 033 public final int curMarker; 034 public final int numMarkers; 035 036 public App2Segment(final int marker, final byte[] segmentData) 037 throws ImageReadException, IOException { 038 this(marker, segmentData.length, new ByteArrayInputStream(segmentData)); 039 } 040 041 public App2Segment(final int marker, int markerLength, final InputStream is2) 042 throws ImageReadException, IOException { 043 super(marker, markerLength, is2); 044 045 if (startsWith(getSegmentData(), 046 JpegConstants.ICC_PROFILE_LABEL)) { 047 final InputStream is = new ByteArrayInputStream(getSegmentData()); 048 049 readAndVerifyBytes(is, JpegConstants.ICC_PROFILE_LABEL, 050 "Not a Valid App2 Segment: missing ICC Profile label"); 051 052 curMarker = readByte("curMarker", is, "Not a valid App2 Marker"); 053 numMarkers = readByte("numMarkers", is, "Not a valid App2 Marker"); 054 055 markerLength -= JpegConstants.ICC_PROFILE_LABEL.size(); 056 markerLength -= (1 + 1); 057 058 iccBytes = readBytes("App2 Data", is, markerLength, "Invalid App2 Segment: insufficient data"); 059 } else { 060 // debugByteArray("Unknown APP2 Segment Type", bytes); 061 curMarker = -1; 062 numMarkers = -1; 063 iccBytes = null; 064 } 065 } 066 067 @Override 068 public boolean equals(final Object obj) { 069 if (obj instanceof App2Segment) { 070 final App2Segment other = (App2Segment) obj; 071 return curMarker == other.curMarker; 072 } 073 return false; 074 } 075 076 @Override 077 public int hashCode() { 078 return curMarker; 079 } 080 081 @Override 082 public int compareTo(final App2Segment other) { 083 return curMarker - other.curMarker; 084 } 085 086 /** 087 * @return the iccBytes 088 */ 089 public byte[] getIccBytes() { 090 return iccBytes.clone(); 091 } 092}