1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
110
111
112
113 }
114 }
115 }