Debian Codegraph @

src/com/drew/metadata/png/PngDescriptor.java

raw symbols SCIP
1 /*
2 * Copyright 2002-2019 Drew Noakes and contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * More information about this project is available at:
17 *
18 * https://drewnoakes.com/code/exif/
19 * https://github.com/drewnoakes/metadata-extractor
20 */
21 package com.drew.metadata.png;
22
23 import com.drew.imaging.png.PngColorType;
24 import com.drew.lang.KeyValuePair;
25 import com.drew.lang.SequentialByteArrayReader;
26 import com.drew.lang.SequentialReader;
27 import com.drew.lang.annotations.NotNull;
28 import com.drew.lang.annotations.Nullable;
29 import com.drew.metadata.TagDescriptor;
30
31 import java.io.IOException;
32 import java.util.List;
33
34 import static com.drew.metadata.png.PngDirectory.*;
35
36 /**
37 * @author Drew Noakes https://drewnoakes.com
38 */
39 @SuppressWarnings("WeakerAccess")
40 public class PngDescriptor extends TagDescriptor<PngDirectory>
41 {
42 public PngDescriptor(@NotNull PngDirectory directory)
43 {
44 super(directory);
45 }
46
47 @Override
48 @Nullable
49 public String getDescription(int tagType)
50 {
51 switch (tagType) {
52 case TAG_COLOR_TYPE:
53 return getColorTypeDescription();
54 case TAG_COMPRESSION_TYPE:
55 return getCompressionTypeDescription();
56 case TAG_FILTER_METHOD:
57 return getFilterMethodDescription();
58 case TAG_INTERLACE_METHOD:
59 return getInterlaceMethodDescription();
60 case TAG_PALETTE_HAS_TRANSPARENCY:
61 return getPaletteHasTransparencyDescription();
62 case TAG_SRGB_RENDERING_INTENT:
63 return getIsSrgbColorSpaceDescription();
64 case TAG_TEXTUAL_DATA:
65 return getTextualDataDescription();
66 case TAG_BACKGROUND_COLOR:
67 return getBackgroundColorDescription();
68 case TAG_UNIT_SPECIFIER:
69 return getUnitSpecifierDescription();
70 default:
71 return super.getDescription(tagType);
72 }
73 }
74
75 @Nullable
76 public String getColorTypeDescription()
77 {
78 Integer value = _directory.getInteger(TAG_COLOR_TYPE);
79 if (value == null)
80 return null;
81 PngColorType colorType = PngColorType.fromNumericValue(value);
82 return colorType.getDescription();
83 }
84
85 @Nullable
86 public String getCompressionTypeDescription()
87 {
88 return getIndexedDescription(TAG_COMPRESSION_TYPE, "Deflate");
89 }
90
91 @Nullable
92 public String getFilterMethodDescription()
93 {
94 return getIndexedDescription(TAG_FILTER_METHOD, "Adaptive");
95 }
96
97 @Nullable
98 public String getInterlaceMethodDescription()
99 {
100 return getIndexedDescription(TAG_INTERLACE_METHOD, "No Interlace", "Adam7 Interlace");
101 }
102
103 @Nullable
104 public String getPaletteHasTransparencyDescription()
105 {
106 return getIndexedDescription(TAG_PALETTE_HAS_TRANSPARENCY, null, "Yes");
107 }
108
109 @Nullable
110 public String getIsSrgbColorSpaceDescription()
111 {
112 return getIndexedDescription(
113 TAG_SRGB_RENDERING_INTENT,
114 "Perceptual",
115 "Relative Colorimetric",
116 "Saturation",
117 "Absolute Colorimetric"
118 );
119 }
120
121 @Nullable
122 public String getUnitSpecifierDescription()
123 {
124 return getIndexedDescription(
125 TAG_UNIT_SPECIFIER,
126 "Unspecified",
127 "Metres"
128 );
129 }
130
131 @Nullable
132 public String getTextualDataDescription()
133 {
134 Object object = _directory.getObject(TAG_TEXTUAL_DATA);
135 if (object == null) {
136 return null;
137 }
138 @SuppressWarnings("unchecked")
139 List<KeyValuePair> keyValues = (List<KeyValuePair>)object;
140 StringBuilder sb = new StringBuilder();
141 for (KeyValuePair keyValue : keyValues) {
142 if (sb.length() != 0)
143 sb.append('\n');
144 sb.append(String.format("%s: %s", keyValue.getKey(), keyValue.getValue()));
145 }
146 return sb.toString();
147 }
148
149 @Nullable
150 public String getBackgroundColorDescription()
151 {
152 byte[] bytes = _directory.getByteArray(TAG_BACKGROUND_COLOR);
153 if (bytes == null) {
154 return null;
155 }
156 SequentialReader reader = new SequentialByteArrayReader(bytes);
157 try {
158 // TODO do we need to normalise these based upon the bit depth?
159 switch (bytes.length) {
160 case 1:
161 return String.format("Palette Index %d", reader.getUInt8());
162 case 2:
163 return String.format("Greyscale Level %d", reader.getUInt16());
164 case 6:
165 return String.format("R %d, G %d, B %d", reader.getUInt16(), reader.getUInt16(), reader.getUInt16());
166 }
167 } catch (IOException ex) {
168 return null;
169 }
170 return null;
171 }
172 }