Skip to content

Commit 96052d4

Browse files
committed
PngImageMetadata
1 parent 91ad362 commit 96052d4

File tree

3 files changed

+119
-6
lines changed

3 files changed

+119
-6
lines changed

src/conf/spotbugs-exclude-filter.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,16 @@
171171
<Method name="&lt;init&gt;" />
172172
<Bug pattern="EI_EXPOSE_REP2" />
173173
</Match>
174+
<Match>
175+
<Class name="org.apache.commons.imaging.formats.png.PngImageMetadata" />
176+
<Method name="getExif" />
177+
<Bug pattern="EI_EXPOSE_REP" />
178+
</Match>
179+
<Match>
180+
<Class name="org.apache.commons.imaging.formats.png.PngImageMetadata" />
181+
<Method name="&lt;init&gt;" />
182+
<Bug pattern="EI_EXPOSE_REP2" />
183+
</Match>
174184
<Match>
175185
<Class name="org.apache.commons.imaging.common.mylzw.BitsToByteInputStream" />
176186
<Method name="&lt;init&gt;" />
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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.formats.png;
18+
19+
import org.apache.commons.imaging.common.ImageMetadata;
20+
import org.apache.commons.imaging.formats.tiff.TiffImageMetadata;
21+
import org.apache.commons.imaging.internal.Debug;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
import java.util.Objects;
26+
27+
public class PngImageMetadata implements ImageMetadata {
28+
29+
private static final String NEWLINE = System.getProperty("line.separator");
30+
31+
private final ImageMetadata textualInformation;
32+
private final TiffImageMetadata exif;
33+
34+
public PngImageMetadata(ImageMetadata textualInformation) {
35+
this(textualInformation, null);
36+
}
37+
38+
public PngImageMetadata(ImageMetadata textualInformation, TiffImageMetadata exif) {
39+
this.textualInformation = Objects.requireNonNull(textualInformation);
40+
this.exif = exif;
41+
}
42+
43+
public ImageMetadata getTextualInformation() {
44+
return textualInformation;
45+
}
46+
47+
public TiffImageMetadata getExif() {
48+
return exif;
49+
}
50+
51+
@Override
52+
public String toString() {
53+
return toString(null);
54+
}
55+
56+
@Override
57+
public String toString(String prefix) {
58+
if (prefix == null) {
59+
prefix = "";
60+
}
61+
62+
final StringBuilder result = new StringBuilder();
63+
64+
result.append(prefix);
65+
result.append("Textual information:");
66+
result.append(NEWLINE);
67+
result.append(textualInformation.toString("\t"));
68+
69+
if (exif != null) {
70+
result.append(NEWLINE);
71+
result.append(prefix);
72+
result.append("Exif metadata:");
73+
result.append(NEWLINE);
74+
result.append(exif.toString("\t"));
75+
}
76+
77+
return result.toString();
78+
}
79+
80+
@Override
81+
public List<? extends ImageMetadataItem> getItems() {
82+
if (exif == null) {
83+
return textualInformation.getItems();
84+
}
85+
86+
ArrayList<ImageMetadataItem> result = new ArrayList<>();
87+
result.addAll(textualInformation.getItems());
88+
result.addAll(exif.getItems());
89+
return result;
90+
}
91+
92+
public void dump() {
93+
Debug.debug(this.toString());
94+
}
95+
}

src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -263,21 +263,29 @@ public Dimension getImageSize(final ByteSource byteSource, final PngImagingParam
263263
@Override
264264
public ImageMetadata getMetadata(final ByteSource byteSource, final PngImagingParameters params)
265265
throws ImageReadException, IOException {
266-
final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.tEXt, ChunkType.zTXt, }, false);
266+
final ChunkType[] chunkTypes = { ChunkType.tEXt, ChunkType.zTXt, ChunkType.iTXt, ChunkType.eXIf };
267+
final List<PngChunk> chunks = readChunks(byteSource, chunkTypes, false);
267268

268269
if (chunks.isEmpty()) {
269270
return null;
270271
}
271272

272-
final GenericImageMetadata result = new GenericImageMetadata();
273+
final GenericImageMetadata textual = new GenericImageMetadata();
274+
TiffImageMetadata exif = null;
273275

274276
for (final PngChunk chunk : chunks) {
275-
final PngTextChunk textChunk = (PngTextChunk) chunk;
276-
277-
result.add(textChunk.getKeyword(), textChunk.getText());
277+
if (chunk instanceof PngTextChunk) {
278+
final PngTextChunk textChunk = (PngTextChunk) chunk;
279+
textual.add(textChunk.getKeyword(), textChunk.getText());
280+
} else if (chunk.chunkType == ChunkType.eXIf.value) {
281+
if (exif != null) {
282+
throw new ImageReadException("Duplicate eXIf chunk");
283+
}
284+
exif = (TiffImageMetadata) new TiffImageParser().getMetadata(chunk.getBytes());
285+
}
278286
}
279287

280-
return result;
288+
return new PngImageMetadata(textual, exif);
281289
}
282290

283291
public TiffImageMetadata getExifMetadata(final ByteSource byteSource, TiffImagingParameters params)

0 commit comments

Comments
 (0)