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 static org.apache.commons.imaging.common.BinaryFunctions.read4Bytes;
020
021import java.io.ByteArrayInputStream;
022import java.io.IOException;
023import java.io.InputStream;
024import java.nio.ByteOrder;
025import java.nio.charset.StandardCharsets;
026import java.util.logging.Logger;
027
028import org.apache.commons.imaging.ImageReadException;
029
030public enum IccTagDataTypes implements IccTagDataType {
031    DESC_TYPE(
032            "descType", 0x64657363) {
033        @Override
034        public void dump(final String prefix, final byte[] bytes)
035                throws ImageReadException, IOException {
036            try (InputStream bis = new ByteArrayInputStream(bytes)) {
037                read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
038
039                //            bis.setDebug(true);
040                read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
041                final int stringLength = read4Bytes("stringLength", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
042
043                //            bis.readByteArray("ignore", bytes.length -12, "none");
044                final String s = new String(bytes, 12, stringLength - 1, StandardCharsets.US_ASCII);
045                LOGGER.fine(prefix + "s: '" + s + "'");
046            }
047        }
048
049    },
050
051    DATA_TYPE(
052            "dataType", 0x64617461) {
053        @Override
054        public void dump(final String prefix, final byte[] bytes)
055                throws ImageReadException, IOException {
056            try (InputStream bis = new ByteArrayInputStream(bytes)) {
057                read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
058            }
059        }
060
061    },
062
063    MULTI_LOCALIZED_UNICODE_TYPE(
064            "multiLocalizedUnicodeType", (0x6D6C7563)) {
065        @Override
066        public void dump(final String prefix, final byte[] bytes)
067                throws ImageReadException, IOException {
068            try (InputStream bis = new ByteArrayInputStream(bytes)) {
069                read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
070            }
071        }
072
073    },
074
075    SIGNATURE_TYPE(
076            "signatureType", ((0x73696720))) {
077        @Override
078        public void dump(final String prefix, final byte[] bytes)
079                throws ImageReadException, IOException {
080            try (InputStream bis = new ByteArrayInputStream(bytes)) {
081                read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
082                read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
083                final int thesignature = read4Bytes("thesignature ", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
084                LOGGER.fine(prefix
085                        + "thesignature: "
086                        + Integer.toHexString(thesignature)
087                        + " ("
088                        + new String(new byte[]{
089                                (byte) (0xff & (thesignature >> 24)),
090                                (byte) (0xff & (thesignature >> 16)),
091                                (byte) (0xff & (thesignature >> 8)),
092                                (byte) (0xff & (thesignature >> 0)), }, StandardCharsets.US_ASCII)
093                        + ")");
094            }
095        }
096
097    },
098
099    TEXT_TYPE(
100            "textType", 0x74657874) {
101        @Override
102        public void dump(final String prefix, final byte[] bytes)
103                throws ImageReadException, IOException {
104            try (InputStream bis = new ByteArrayInputStream(bytes)) {
105                read4Bytes("type_signature", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
106                read4Bytes("ignore", bis, "ICC: corrupt tag data", ByteOrder.BIG_ENDIAN);
107                final String s = new String(bytes, 8, bytes.length - 8, StandardCharsets.US_ASCII);
108                LOGGER.fine(prefix + "s: '" + s + "'");
109            }
110        }
111
112    };
113
114    private static final Logger LOGGER = Logger.getLogger(IccTagDataTypes.class.getName());
115
116    public final String name;
117    public final int signature;
118
119    IccTagDataTypes(final String name, final int signature) {
120        this.name = name;
121        this.signature = signature;
122    }
123
124    @Override
125    public String getName() {
126        return name;
127    }
128
129    @Override
130    public int getSignature() {
131        return signature;
132    }
133}