View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.imaging.icc;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.IOException;
21  import java.io.InputStream;
22  import java.io.PrintWriter;
23  import java.io.StringWriter;
24  import java.nio.ByteOrder;
25  import java.nio.charset.StandardCharsets;
26  import java.util.Arrays;
27  import java.util.logging.Logger;
28  
29  import org.apache.commons.imaging.ImagingException;
30  import org.apache.commons.imaging.common.BinaryFunctions;
31  
32  public class IccTag {
33  
34      static final int SHALLOW_SIZE = 40;
35      private static final Logger LOGGER = Logger.getLogger(IccTag.class.getName());
36  
37      public final int signature;
38      public final int offset;
39      public final int length;
40      public final IccTagType fIccTagType;
41      private byte[] data;
42      private IccTagDataType itdt;
43      private int dataTypeSignature;
44  
45      // public final byte[] data;
46  
47      public IccTag(final int signature, final int offset, final int length, final IccTagType fIccTagType) {
48          this.signature = signature;
49          this.offset = offset;
50          this.length = length;
51          this.fIccTagType = fIccTagType;
52      }
53  
54      public void dump(final PrintWriter pw, final String prefix) throws ImagingException, IOException {
55          pw.println(prefix + "tag signature: " + Integer.toHexString(signature) + " (" + new String(new byte[] { (byte) (0xff & signature >> 24),
56                  (byte) (0xff & signature >> 16), (byte) (0xff & signature >> 8), (byte) (0xff & signature >> 0), }, StandardCharsets.US_ASCII) + ")");
57  
58          if (data == null) {
59              pw.println(prefix + "data: " + Arrays.toString((byte[]) null));
60          } else {
61              pw.println(prefix + "data: " + data.length);
62  
63              pw.println(prefix + "data type signature: " + Integer.toHexString(dataTypeSignature) + " ("
64                      + new String(new byte[] { (byte) (0xff & dataTypeSignature >> 24), (byte) (0xff & dataTypeSignature >> 16),
65                              (byte) (0xff & dataTypeSignature >> 8), (byte) (0xff & dataTypeSignature >> 0), }, StandardCharsets.US_ASCII)
66                      + ")");
67  
68              if (itdt == null) {
69                  pw.println(prefix + "IccTagType : " + "unknown");
70              } else {
71                  pw.println(prefix + "IccTagType : " + itdt.getName());
72                  itdt.dump(prefix, data);
73              }
74  
75          }
76  
77          pw.println("");
78          pw.flush();
79  
80      }
81  
82      public void dump(final String prefix) throws ImagingException, IOException {
83          try (StringWriter sw = new StringWriter();
84                  PrintWriter pw = new PrintWriter(sw)) {
85              dump(pw, prefix);
86              pw.flush();
87              sw.flush();
88              LOGGER.fine(sw.toString());
89          }
90      }
91  
92      private IccTagDataType getIccTagDataType(final int quad) {
93          for (final IccTagDataType iccTagDataType : IccTagDataTypes.values()) {
94              if (iccTagDataType.getSignature() == quad) {
95                  return iccTagDataType;
96              }
97          }
98  
99          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 }