Debian Codegraph @

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

raw
1 /*
2 * Copyright 2002-2017 Drew Noakes
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 if (colorType == null)
83 return null;
84 return colorType.getDescription();
85 }
86
87 @Nullable
88 public String getCompressionTypeDescription()
89 {
90 return getIndexedDescription(TAG_COMPRESSION_TYPE, "Deflate");
91 }
92
93 @Nullable
94 public String getFilterMethodDescription()
95 {
96 return getIndexedDescription(TAG_FILTER_METHOD, "Adaptive");
97 }
98
99 @Nullable
100 public String getInterlaceMethodDescription()
101 {
102 return getIndexedDescription(TAG_INTERLACE_METHOD, "No Interlace", "Adam7 Interlace");
103 }
104
105 @Nullable
106 public String getPaletteHasTransparencyDescription()
107 {
108 return getIndexedDescription(TAG_PALETTE_HAS_TRANSPARENCY, null, "Yes");
109 }
110
111 @Nullable
112 public String getIsSrgbColorSpaceDescription()
113 {
114 return getIndexedDescription(
115 TAG_SRGB_RENDERING_INTENT,
116 "Perceptual",
117 "Relative Colorimetric",
118 "Saturation",
119 "Absolute Colorimetric"
120 );
121 }
122
123 @Nullable
124 public String getUnitSpecifierDescription()
125 {
126 return getIndexedDescription(
127 TAG_UNIT_SPECIFIER,
128 "Unspecified",
129 "Metres"
130 );
131 }
132
133 @Nullable
134 public String getTextualDataDescription()
135 {
136 Object object = _directory.getObject(TAG_TEXTUAL_DATA);
137 if (object == null) {
138 return null;
139 }
140 @SuppressWarnings("unchecked")
141 List<KeyValuePair> keyValues = (List<KeyValuePair>)object;
142 StringBuilder sb = new StringBuilder();
143 for (KeyValuePair keyValue : keyValues) {
144 if (sb.length() != 0)
145 sb.append('\n');
146 sb.append(String.format("%s: %s", keyValue.getKey(), keyValue.getValue()));
147 }
148 return sb.toString();
149 }
150
151 @Nullable
152 public String getBackgroundColorDescription()
153 {
154 byte[] bytes = _directory.getByteArray(TAG_BACKGROUND_COLOR);
155 Integer colorType = _directory.getInteger(TAG_COLOR_TYPE);
156 if (bytes == null || colorType == null) {
157 return null;
158 }
159 SequentialReader reader = new SequentialByteArrayReader(bytes);
160 try {
161 // TODO do we need to normalise these based upon the bit depth?
162 switch (colorType) {
163 case 0:
164 case 4:
165 return String.format("Greyscale Level %d", reader.getUInt16());
166 case 2:
167 case 6:
168 return String.format("R %d, G %d, B %d", reader.getUInt16(), reader.getUInt16(), reader.getUInt16());
169 case 3:
170 return String.format("Palette Index %d", reader.getUInt8());
171 }
172 } catch (IOException ex) {
173 return null;
174 }
175 return null;
176 }
177 }