    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.icc;
    018
    019import java.io.ByteArrayInputStream;
    020import java.io.IOException;
    021import java.io.InputStream;
    022import java.io.PrintWriter;
    023import java.io.StringWriter;
    024import java.nio.ByteOrder;
    025import java.nio.charset.StandardCharsets;
    026import java.util.Arrays;
    027import java.util.logging.Logger;
    028
    029import org.apache.commons.imaging.ImagingException;
    030import org.apache.commons.imaging.common.BinaryFunctions;
    031
    032public class IccTag {
    033
    034    static final int SHALLOW_SIZE = 40;
    035    private static final Logger LOGGER = Logger.getLogger(IccTag.class.getName());
    036
    037    public final int signature;
    038    public final int offset;
    039    public final int length;
    040    public final IccTagType fIccTagType;
    041    private byte[] data;
    042    private IccTagDataType itdt;
    043    private int dataTypeSignature;
    044
    045    // public final byte[] data;
    046
    047    public IccTag(final int signature, final int offset, final int length, final IccTagType fIccTagType) {
    048        this.signature = signature;
    049        this.offset = offset;
    050        this.length = length;
    051        this.fIccTagType = fIccTagType;
    052    }
    053
    054    public void dump(final PrintWriter pw, final String prefix) throws ImagingException, IOException {
    055        pw.println(prefix + "tag signature: " + Integer.toHexString(signature) + " (" + new String(new byte[] { (byte) (0xff & signature >> 24),
    056                (byte) (0xff & signature >> 16), (byte) (0xff & signature >> 8), (byte) (0xff & signature >> 0), }, StandardCharsets.US_ASCII) + ")");
    057
    058        if (data == null) {
    059            pw.println(prefix + "data: " + Arrays.toString((byte[]) null));
    060        } else {
    061            pw.println(prefix + "data: " + data.length);
    062
    063            pw.println(prefix + "data type signature: " + Integer.toHexString(dataTypeSignature) + " ("
    064                    + new String(new byte[] { (byte) (0xff & dataTypeSignature >> 24), (byte) (0xff & dataTypeSignature >> 16),
    065                            (byte) (0xff & dataTypeSignature >> 8), (byte) (0xff & dataTypeSignature >> 0), }, StandardCharsets.US_ASCII)
    066                    + ")");
    067
    068            if (itdt == null) {
    069                pw.println(prefix + "IccTagType : " + "unknown");
    070            } else {
    071                pw.println(prefix + "IccTagType : " + itdt.getName());
    072                itdt.dump(prefix, data);
    073            }
    074
    075        }
    076
    077        pw.println("");
    078        pw.flush();
    079
    080    }
    081
    082    public void dump(final String prefix) throws ImagingException, IOException {
    083        try (StringWriter sw = new StringWriter();
    084                PrintWriter pw = new PrintWriter(sw)) {
    085            dump(pw, prefix);
    086            pw.flush();
    087            sw.flush();
    088            LOGGER.fine(sw.toString());
    089        }
    090    }
    091
    092    private IccTagDataType getIccTagDataType(final int quad) {
    093        for (final IccTagDataType iccTagDataType : IccTagDataTypes.values()) {
    094            if (iccTagDataType.getSignature() == quad) {
    095                return iccTagDataType;
    096            }
    097        }
    098
    099        return null;
    100    }
    101
    102    public void setData(final byte[] bytes) throws IOException {
    103        data = bytes;
    104
    105        try (InputStream bis = new ByteArrayInputStream(bytes)) {
    106            dataTypeSignature = BinaryFunctions.read4Bytes("data type signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
    107
    108            itdt = getIccTagDataType(dataTypeSignature);
    109            // if (itdt != null)
    110            // {
    111            // System.out.println("\t\t\t" + "itdt: " + itdt.name);
    112            // }
    113        }
    114    }
    115}