1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.imaging;
18
19 import java.io.PrintWriter;
20 import java.io.StringWriter;
21 import java.util.ArrayList;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.logging.Logger;
25
26
27
28
29
30 public class ImageInfo {
31
32 public enum ColorType {
33 BW("Black and White"),
34 GRAYSCALE("Grayscale"),
35 RGB("RGB"),
36 CMYK("CMYK"),
37 YCbCr("YCbCr"),
38 YCCK("YCCK"),
39 YCC("YCC"),
40 OTHER("Other"),
41 UNKNOWN("Unknown");
42
43 private final String description;
44
45 ColorType(final String description) {
46 this.description = description;
47 }
48
49 @Override
50 public String toString() {
51 return description;
52 }
53 }
54
55 public enum CompressionAlgorithm {
56 UNKNOWN("Unknown"),
57 NONE("None"),
58 LZW("LZW"),
59 PACKBITS("PackBits"),
60 JPEG("JPEG"),
61 RLE("RLE: Run-Length Encoding"),
62 ADAPTIVE_RLE("Adaptive RLE"),
63 PSD("Photoshop"),
64 PNG_FILTER("PNG Filter"),
65 CCITT_GROUP_3("CCITT Group 3 1-Dimensional Modified Huffman run-length encoding."),
66 CCITT_GROUP_4("CCITT Group 4"),
67 CCITT_1D("CCITT 1D");
68
69 private final String description;
70
71 CompressionAlgorithm(final String description) {
72 this.description = description;
73 }
74
75 @Override
76 public String toString() {
77 return description;
78 }
79 }
80
81 private static final Logger LOGGER = Logger.getLogger(ImageInfo.class.getName());
82
83 private final String formatDetails;
84
85 private final int bitsPerPixel;
86 private final List<String> comments;
87
88 private final ImageFormat format;
89 private final String formatName;
90 private final int height;
91 private final String mimeType;
92
93 private final int numberOfImages;
94 private final int physicalHeightDpi;
95 private final float physicalHeightInch;
96 private final int physicalWidthDpi;
97 private final float physicalWidthInch;
98 private final int width;
99 private final boolean progressive;
100 private final boolean transparent;
101
102 private final boolean usesPalette;
103
104 private final ColorType colorType;
105
106 private final CompressionAlgorithm compressionAlgorithm;
107
108 public ImageInfo(final String formatDetails, final int bitsPerPixel,
109 final List<String> comments, final ImageFormat format, final String formatName,
110 final int height, final String mimeType, final int numberOfImages,
111 final int physicalHeightDpi, final float physicalHeightInch,
112 final int physicalWidthDpi, final float physicalWidthInch, final int width,
113 final boolean progressive, final boolean transparent, final boolean usesPalette,
114 final ColorType colorType, final CompressionAlgorithm compressionAlgorithm) {
115 this.formatDetails = formatDetails;
116
117 this.bitsPerPixel = bitsPerPixel;
118 this.comments = comments == null ? Collections.emptyList() : Collections.unmodifiableList(comments);
119
120 this.format = format;
121 this.formatName = formatName;
122 this.height = height;
123 this.mimeType = mimeType;
124
125 this.numberOfImages = numberOfImages;
126 this.physicalHeightDpi = physicalHeightDpi;
127 this.physicalHeightInch = physicalHeightInch;
128 this.physicalWidthDpi = physicalWidthDpi;
129 this.physicalWidthInch = physicalWidthInch;
130 this.width = width;
131 this.progressive = progressive;
132
133 this.transparent = transparent;
134 this.usesPalette = usesPalette;
135
136 this.colorType = colorType;
137 this.compressionAlgorithm = compressionAlgorithm;
138 }
139
140
141
142
143
144
145 public int getBitsPerPixel() {
146 return bitsPerPixel;
147 }
148
149
150
151
152
153
154
155
156 public List<String> getComments() {
157 return new ArrayList<>(comments);
158 }
159
160
161
162
163
164
165
166
167
168 public ImageFormat getFormat() {
169 return format;
170 }
171
172
173
174
175
176
177
178 public String getFormatName() {
179 return formatName;
180 }
181
182
183
184
185
186
187
188 public int getHeight() {
189 return height;
190 }
191
192
193
194
195
196
197
198 public String getMimeType() {
199 return mimeType;
200 }
201
202
203
204
205
206
207
208
209
210 public int getNumberOfImages() {
211 return numberOfImages;
212 }
213
214
215
216
217
218
219
220
221
222
223 public int getPhysicalHeightDpi() {
224 return physicalHeightDpi;
225 }
226
227
228
229
230
231
232
233
234
235
236 public float getPhysicalHeightInch() {
237 return physicalHeightInch;
238 }
239
240
241
242
243
244
245
246
247
248
249 public int getPhysicalWidthDpi() {
250 return physicalWidthDpi;
251 }
252
253
254
255
256
257
258
259
260
261
262 public float getPhysicalWidthInch() {
263 return physicalWidthInch;
264 }
265
266
267
268
269
270
271
272 public int getWidth() {
273 return width;
274 }
275
276
277
278
279
280
281 public boolean isProgressive() {
282 return progressive;
283 }
284
285
286
287
288
289
290 public ColorType getColorType() {
291 return colorType;
292 }
293
294 public void dump() {
295 LOGGER.fine(toString());
296 }
297
298 @Override
299 public String toString() {
300 try {
301 final StringWriter sw = new StringWriter();
302 final PrintWriter pw = new PrintWriter(sw);
303
304 toString(pw, "");
305 pw.flush();
306
307 return sw.toString();
308 } catch (final Exception e) {
309 return "Image Data: Error";
310 }
311 }
312
313 public void toString(final PrintWriter pw, final String prefix) {
314 pw.println("Format Details: " + formatDetails);
315
316 pw.println("Bits Per Pixel: " + bitsPerPixel);
317 pw.println("Comments: " + comments.size());
318 for (int i = 0; i < comments.size(); i++) {
319 final String s = comments.get(i);
320 pw.println("\t" + i + ": '" + s + "'");
321
322 }
323 pw.println("Format: " + format.getName());
324 pw.println("Format Name: " + formatName);
325 pw.println("Compression Algorithm: " + compressionAlgorithm);
326 pw.println("Height: " + height);
327 pw.println("MimeType: " + mimeType);
328 pw.println("Number Of Images: " + numberOfImages);
329 pw.println("Physical Height Dpi: " + physicalHeightDpi);
330 pw.println("Physical Height Inch: " + physicalHeightInch);
331 pw.println("Physical Width Dpi: " + physicalWidthDpi);
332 pw.println("Physical Width Inch: " + physicalWidthInch);
333 pw.println("Width: " + width);
334 pw.println("Is Progressive: " + progressive);
335 pw.println("Is Transparent: " + transparent);
336
337 pw.println("Color Type: " + colorType.toString());
338 pw.println("Uses Palette: " + usesPalette);
339
340 pw.flush();
341
342 }
343
344
345
346
347
348
349 public String getFormatDetails() {
350 return formatDetails;
351 }
352
353
354
355
356
357
358 public boolean isTransparent() {
359 return transparent;
360 }
361
362
363
364
365
366
367 public boolean usesPalette() {
368 return usesPalette;
369 }
370
371
372
373
374
375
376 public CompressionAlgorithm getCompressionAlgorithm() {
377 return compressionAlgorithm;
378 }
379
380 }