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