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 2021/03/15 02:45:19 UTC
[commons-imaging] branch master updated (58ef126 -> 1d9c368)
This is an automated email from the ASF dual-hosted git repository.
kinow pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/commons-imaging.git.
from 58ef126 Merge branch 'pr-114'
new abac6c0 [IMAGING-168] Encoding Support for IPTC metadata when reading an image metadata and writing an image metadata.
new 2b0311a [IMAGING-168]: Add unit test
new 3b275e5 [IMAGING-168] Changelog
new 1d9c368 Merge branch 'pr-124'
The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
Summary of changes:
src/changes/changes.xml | 3 +
.../imaging/formats/jpeg/iptc/IptcParser.java | 61 +++++++++++++++++++--
.../imaging/formats/jpeg/iptc/IptcParserTest.java | 22 ++++++++
...083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg | Bin 0 -> 24243 bytes
4 files changed, 82 insertions(+), 4 deletions(-)
create mode 100644 src/test/resources/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg
[commons-imaging] 03/04: [IMAGING-168] Changelog
Posted by ki...@apache.org.
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
commit 3b275e54c017cf1e145fcead787acfd486f95aa3
Author: Bruno P. Kinoshita <ki...@apache.org>
AuthorDate: Mon Mar 15 15:43:46 2021 +1300
[IMAGING-168] Changelog
---
src/changes/changes.xml | 3 +++
1 file changed, 3 insertions(+)
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 4ecb585..50adb11 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -45,6 +45,9 @@ The <action> type attribute can be add,update,fix,remove.
</properties>
<body>
<release version="1.0-alpha3" date="2020-??-??" description="Third 1.0 alpha release">
+ <action issue="IMAGING-168" dev="kinow" type="fix" due-to="Yavuz Kavus">
+ Encoding Support for IPTC metadata.
+ </action>
<action issue="IMAGING-283" dev="kinow" type="add" due-to="Wanja Gayk">
Add CIELAB and DIN99 conversion, reduce code duplication, and issues related to zero-division and precision.
</action>
[commons-imaging] 02/04: [IMAGING-168]: Add unit test
Posted by ki...@apache.org.
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
commit 2b0311a56fb68119dd8f22647e56e47dfb1f3f47
Author: Bruno P. Kinoshita <ki...@apache.org>
AuthorDate: Mon Mar 15 15:39:29 2021 +1300
[IMAGING-168]: Add unit test
---
.../imaging/formats/jpeg/iptc/IptcParserTest.java | 22 +++++++++++++++++++++
...083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg | Bin 0 -> 24243 bytes
2 files changed, 22 insertions(+)
diff --git a/src/test/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParserTest.java b/src/test/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParserTest.java
index a7ae28c..6fff24e 100644
--- a/src/test/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParserTest.java
+++ b/src/test/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParserTest.java
@@ -16,16 +16,19 @@
*/
package org.apache.commons.imaging.formats.jpeg.iptc;
+import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import java.io.File;
import java.io.IOException;
+import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.imaging.ImageReadException;
+import org.apache.commons.imaging.common.GenericImageMetadata.GenericImageMetadataItem;
import org.apache.commons.imaging.common.bytesource.ByteSourceFile;
import org.apache.commons.imaging.formats.jpeg.JpegImageMetadata;
import org.apache.commons.imaging.formats.jpeg.JpegImageParser;
@@ -81,4 +84,23 @@ public class IptcParserTest {
}
}
}
+
+ /**
+ * Tests for IptcParser encoding support. See IMAGING-168 and pullr equest #124 for more.
+ * @throws IOException when reading input
+ * @throws ImageReadException when parsing file
+ */
+ @Test
+ public void testEncodingSupport() throws IOException, ImageReadException {
+ // NOTE: We use the JpegParser, so it will send only the block/segment that IptcParser needs for the test image
+ File file = new File(IptcParserTest.class.getResource("/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg").getFile());
+ JpegImageParser parser = new JpegImageParser();
+ JpegImageMetadata metadata = (JpegImageMetadata) parser.getMetadata(file);
+ JpegPhotoshopMetadata photoshopMetadata = metadata.getPhotoshop();
+ @SuppressWarnings("unchecked")
+ List<GenericImageMetadataItem> items = (List<GenericImageMetadataItem>) photoshopMetadata.getItems();
+ GenericImageMetadataItem thanksInMandarin = items.get(3);
+ // converted the thank-you in chinese characters to unicode for comparison here
+ assertArrayEquals("\u8c22\u8c22".getBytes(StandardCharsets.UTF_8), thanksInMandarin.getText().getBytes());
+ }
}
diff --git a/src/test/resources/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg b/src/test/resources/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg
new file mode 100644
index 0000000..f00deca
Binary files /dev/null and b/src/test/resources/images/jpeg/iptc/IMAGING-168/111083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg differ
[commons-imaging] 01/04: [IMAGING-168] Encoding Support for IPTC
metadata when reading an image metadata and writing an image metadata.
Posted by ki...@apache.org.
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
commit abac6c05136548ebe83dd93c978daa4a8bf6bd8f
Author: Yavuz Kavus <ya...@gmail.com>
AuthorDate: Sun Mar 14 00:48:45 2021 +0300
[IMAGING-168] Encoding Support for IPTC metadata when reading an image metadata and writing an image metadata.
---
.../imaging/formats/jpeg/iptc/IptcParser.java | 61 ++++++++++++++++++++--
1 file changed, 57 insertions(+), 4 deletions(-)
diff --git a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
index 8814075..6cde3dd 100644
--- a/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
+++ b/src/main/java/org/apache/commons/imaging/formats/jpeg/iptc/IptcParser.java
@@ -29,6 +29,7 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteOrder;
+import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
@@ -36,6 +37,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
+import java.util.Objects;
import java.util.logging.Level;
import java.util.logging.Logger;
@@ -65,6 +67,10 @@ public class IptcParser extends BinaryFileParser {
*/
private static final List<Integer> PHOTOSHOP_IGNORED_BLOCK_TYPE = Arrays.asList(1084, 1085, 1086, 1087);
+ private static final Charset DEFAULT_CHARSET = StandardCharsets.ISO_8859_1;
+ private static final int ENV_TAG_CODED_CHARACTER_SET = 90;
+ private static final byte[] CHARACTER_ESCAPE_SEQUENCE = {'\u001B', '%', 'G'};
+
public IptcParser() {
setByteOrder(ByteOrder.BIG_ENDIAN);
}
@@ -143,6 +149,7 @@ public class IptcParser extends BinaryFileParser {
}
protected List<IptcRecord> parseIPTCBlock(final byte[] bytes) {
+ Charset charset = DEFAULT_CHARSET;
final List<IptcRecord> elements = new ArrayList<>();
int index = 0;
@@ -202,6 +209,11 @@ public class IptcParser extends BinaryFileParser {
// Debug.debug("recordSize", recordSize + " (0x"
// + Integer.toHexString(recordSize) + ")");
+ if (recordNumber == IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER && recordType == ENV_TAG_CODED_CHARACTER_SET) {
+ charset = findCharset(recordData);
+ continue;
+ }
+
if (recordNumber != IptcConstants.IPTC_APPLICATION_2_RECORD_NUMBER) {
continue;
}
@@ -237,7 +249,7 @@ public class IptcParser extends BinaryFileParser {
// continue;
// }
- final String value = new String(recordData, StandardCharsets.ISO_8859_1);
+ final String value = new String(recordData, charset);
final IptcType iptcType = IptcTypeLookup.getIptcType(recordType);
@@ -405,9 +417,25 @@ public class IptcParser extends BinaryFileParser {
public byte[] writeIPTCBlock(List<IptcRecord> elements)
throws ImageWriteException, IOException {
+ Charset charset = DEFAULT_CHARSET;
+ for (final IptcRecord element : elements) {
+ final byte[] recordData = element.getValue().getBytes(charset);
+ if (!new String(recordData, charset).equals(element.getValue())) {
+ charset = StandardCharsets.UTF_8;
+ break;
+ }
+ }
byte[] blockData;
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
try (BinaryOutputStream bos = new BinaryOutputStream(baos, getByteOrder())) {
+ if (charset != null && !charset.equals(DEFAULT_CHARSET)) {
+ bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
+ bos.write(IptcConstants.IPTC_ENVELOPE_RECORD_NUMBER);
+ bos.write(ENV_TAG_CODED_CHARACTER_SET);
+ byte[] codedCharset = charset.equals(StandardCharsets.UTF_8) ? CHARACTER_ESCAPE_SEQUENCE: charset.name().getBytes(StandardCharsets.ISO_8859_1);
+ bos.write2Bytes(codedCharset.length);
+ bos.write(codedCharset);
+ }
// first, right record version record
bos.write(IptcConstants.IPTC_RECORD_TAG_MARKER);
@@ -440,11 +468,13 @@ public class IptcParser extends BinaryFileParser {
}
bos.write(element.iptcType.getType());
- final byte[] recordData = element.getValue().getBytes(StandardCharsets.ISO_8859_1);
- if (!new String(recordData, StandardCharsets.ISO_8859_1).equals(element.getValue())) {
+ final byte[] recordData = element.getValue().getBytes(charset);
+ /*
+ if (!new String(recordData, charset).equals(element.getValue())) {
throw new ImageWriteException(
- "Invalid record value, not ISO-8859-1");
+ "Invalid record value, not " + charset.name());
}
+ */
bos.write2Bytes(recordData.length);
bos.write(recordData);
@@ -456,4 +486,27 @@ public class IptcParser extends BinaryFileParser {
return blockData;
}
+ private Charset findCharset(byte[] codedCharset) {
+ String codedCharsetString = new String(codedCharset, StandardCharsets.ISO_8859_1);
+ try {
+ if (Charset.isSupported(codedCharsetString)) {
+ return Charset.forName(codedCharsetString);
+ }
+ } catch (IllegalArgumentException e) { }
+ // check if encoding is a escape sequence
+ // normalize encoding byte sequence
+ byte[] codedCharsetNormalized = new byte[codedCharset.length];
+ int j = 0;
+ for (int i = 0; i < codedCharset.length; i++) {
+ if (codedCharset[i] != ' ') {
+ codedCharsetNormalized[j++] = codedCharset[i];
+ }
+ }
+
+ if (Objects.deepEquals(codedCharsetNormalized, CHARACTER_ESCAPE_SEQUENCE)) {
+ return StandardCharsets.UTF_8;
+ }
+ return DEFAULT_CHARSET;
+ }
+
}
[commons-imaging] 04/04: Merge branch 'pr-124'
Posted by ki...@apache.org.
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
commit 1d9c3682bb16f85b92a8fa5b2a5341c018a0da61
Merge: 58ef126 3b275e5
Author: Bruno P. Kinoshita <ki...@apache.org>
AuthorDate: Mon Mar 15 15:44:14 2021 +1300
Merge branch 'pr-124'
This closes #124
src/changes/changes.xml | 3 +
.../imaging/formats/jpeg/iptc/IptcParser.java | 61 +++++++++++++++++++--
.../imaging/formats/jpeg/iptc/IptcParserTest.java | 22 ++++++++
...083453-c07f1880-851e-11eb-8b61-2757f7d934bf.jpg | Bin 0 -> 24243 bytes
4 files changed, 82 insertions(+), 4 deletions(-)