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