You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ki...@apache.org on 2023/01/13 22:52:21 UTC

[commons-imaging] branch master updated: [IMAGING-342] Read PNG metadata from iTXt chunk

This is an automated email from the ASF dual-hosted git repository.

kinow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git


The following commit(s) were added to refs/heads/master by this push:
     new 32b857a3 [IMAGING-342] Read PNG metadata from iTXt chunk
     new 3542407e Merge branch 'pr-268'
32b857a3 is described below

commit 32b857a36cdf1a6a9d08f6d005e58ad8c805a90f
Author: Glavo <zj...@gmail.com>
AuthorDate: Fri Jan 13 05:52:51 2023 +0800

    [IMAGING-342] Read PNG metadata from iTXt chunk
---
 src/changes/changes.xml                            |   5 ++++-
 .../imaging/formats/png/PngImageParser.java        |   2 +-
 .../commons/imaging/formats/png/PngReadTest.java   |  24 +++++++++++++++++++++
 .../images/png/IMAGING-342/utf8-comment.png        | Bin 0 -> 107 bytes
 4 files changed, 29 insertions(+), 2 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index db580525..c2ee134d 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -46,9 +46,12 @@ The <action> type attribute can be add,update,fix,remove.
   <body>
     <release version="1.0" date="2022-??-??" description="1.0 release">
       <!-- FIX -->
-      <action issue="IMAGING-335" dev="ggregory" type="update" due-to="Dominik Stadler, Gary Gregory">
+      <action issue="IMAGING-335" dev="ggregory" type="fix" due-to="Dominik Stadler, Gary Gregory">
         NullPointerException in App2Segment.getIccBytes when parsing a broken JPEG file.
       </action>
+      <action issue="IMAGING-342" dev="kinow" type="fix" due-to="Glavo">
+        Read PNG metadata from iTXt chunk.
+      </action>
       <!-- UPDATE -->
       <action dev="kinow" type="update" due-to="Dependabot, Gary Gregory">
         Bump actions/cache from 3.0.4 to 3.0.10 #225, #228, #239, #240.
diff --git a/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java b/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
index 91a6f884..64c7d559 100644
--- a/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/png/PngImageParser.java
@@ -282,7 +282,7 @@ public class PngImageParser extends ImageParser<PngImagingParameters>  implement
     @Override
     public ImageMetadata getMetadata(final ByteSource byteSource, final PngImagingParameters params)
             throws ImageReadException, IOException {
-        final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.tEXt, ChunkType.zTXt, }, false);
+        final List<PngChunk> chunks = readChunks(byteSource, new ChunkType[] { ChunkType.tEXt, ChunkType.zTXt, ChunkType.iTXt }, false);
 
         if (chunks.isEmpty()) {
             return null;
diff --git a/src/test/java/org/apache/commons/imaging/formats/png/PngReadTest.java b/src/test/java/org/apache/commons/imaging/formats/png/PngReadTest.java
index 2630a97b..80d50cf8 100644
--- a/src/test/java/org/apache/commons/imaging/formats/png/PngReadTest.java
+++ b/src/test/java/org/apache/commons/imaging/formats/png/PngReadTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.commons.imaging.formats.png;
 
+import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertNotNull;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 
@@ -28,6 +29,7 @@ import java.util.List;
 import org.apache.commons.imaging.ImageInfo;
 import org.apache.commons.imaging.ImageReadException;
 import org.apache.commons.imaging.Imaging;
+import org.apache.commons.imaging.common.GenericImageMetadata;
 import org.apache.commons.imaging.common.ImageMetadata;
 import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
 import org.apache.commons.imaging.internal.Debug;
@@ -109,4 +111,26 @@ public class PngReadTest extends PngBaseTest {
         final PngImageParser parser = new PngImageParser();
         assertThrows(ImageReadException.class, () -> parser.getBufferedImage(new ByteSourceFile(new File(file)), new PngImagingParameters()));
     }
+
+    /**
+     * Test reading metadata from PNG file with UTF-8 characters in the text chunks.
+     *
+     * @see <a href="https://issues.apache.org/jira/browse/IMAGING-342">IMAGING-342</a>
+     * @throws IOException if it fails to read the test image
+     * @throws ImageReadException if it fails to read the test image
+     */
+    @Test
+    public void testReadMetadataFromItxtChunk() throws IOException, ImageReadException {
+        final String input = "/images/png/IMAGING-342/utf8-comment.png";
+        final String file = PngReadTest.class.getResource(input).getFile();
+        final PngImageParser parser = new PngImageParser();
+
+        ImageMetadata metadata = parser.getMetadata(new File(file));
+        List<?> items = metadata.getItems();
+        assertEquals(1, items.size());
+
+        GenericImageMetadata.GenericImageMetadataItem item = ((GenericImageMetadata.GenericImageMetadataItem) items.get(0));
+        assertEquals("Comment", item.getKeyword());
+        assertEquals("\u2192 UTF-8 Test", item.getText());
+    }
 }
diff --git a/src/test/resources/images/png/IMAGING-342/utf8-comment.png b/src/test/resources/images/png/IMAGING-342/utf8-comment.png
new file mode 100644
index 00000000..0d77cbf7
Binary files /dev/null and b/src/test/resources/images/png/IMAGING-342/utf8-comment.png differ