Patches to add support for iTXt blocks.
Subject: | 0001-Refactor-PNG-tXTt-and-zTXt-handling-to-make-it-easie.patch |
From 8ab52fc36307ca785d88ca71614a27ba3860b728 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <nick@ccl4.org>
Date: Fri, 30 Jun 2017 08:19:38 +0200
Subject: [PATCH 1/2] Refactor PNG tXTt and zTXt handling to make it easier to
add iTXt.
---
lib/Image/Info/PNG.pm | 27 +++++++++++++++------------
1 file changed, 15 insertions(+), 12 deletions(-)
diff --git a/lib/Image/Info/PNG.pm b/lib/Image/Info/PNG.pm
index c4a2208..4d38338 100644
--- a/lib/Image/Info/PNG.pm
+++ b/lib/Image/Info/PNG.pm
@@ -132,20 +132,23 @@ sub process_file
}
$info->push_info(0, "resolution" => $res)
}
- elsif ($type eq "tEXt") {
+ elsif ($type eq "tEXt" || $type eq "zTXt") {
my($key, $val) = split(/\0/, $data, 2);
- # XXX should make sure $key is not in conflict with any
- # other key we might generate
- $info->push_info(0, $key, $val);
- }
- elsif ($type eq "zTXt" && $have_zlib) {
- my($key, $val) = split(/\0/, $data, 2);
+ if ($type eq "zTXt") {
my($method,$ctext) = split(//, $val, 2);
- if ($method eq "\0") {
- $info->push_info(0, $key, Compress::Zlib::uncompress($ctext));
- } else {
- $info->push_info(0, "Chunk-$type" => $data);
- }
+ if ($have_zlib && $method eq "\0") {
+ $val = Compress::Zlib::uncompress($ctext);
+ } else {
+ undef $val;
+ }
+ }
+ if (defined $val) {
+ # XXX should make sure $key is not in conflict with any
+ # other key we might generate
+ $info->push_info(0, $key, $val);
+ } else {
+ $info->push_info(0, "Chunk-$type" => $data);
+ }
}
elsif ($type eq "tIME" && $len == 7) {
$info->push_info(0, "LastModificationTime",
--
1.9.1
Subject: | 0002-Support-iTXt-chunks-in-PNGs.patch |
From 01def19ef346f0dbe85b96d0584685e876ee0f59 Mon Sep 17 00:00:00 2001
From: Nicholas Clark <nick@ccl4.org>
Date: Fri, 30 Jun 2017 09:52:04 +0200
Subject: [PATCH 2/2] Support iTXt chunks in PNGs.
---
img/itxt.png | Bin 0 -> 97 bytes
img/itxt2.png | Bin 0 -> 112 bytes
img/itxt3.png | Bin 0 -> 114 bytes
lib/Image/Info/PNG.pm | 35 ++++++++++++++++++++++++++++++++---
t/png.t | 27 ++++++++++++++++++++++++++-
5 files changed, 58 insertions(+), 4 deletions(-)
create mode 100644 img/itxt.png
create mode 100644 img/itxt2.png
create mode 100644 img/itxt3.png
diff --git a/img/itxt.png b/img/itxt.png
new file mode 100644
index 0000000000000000000000000000000000000000..3df03100ee687d1c9e8e6e27c26cc8c9b1872493
GIT binary patch
literal 97
zcmeAS@N?(olHy`uVBq!ia0vp^j3CSbBp9sfW`_bPE>9Q75RT~N1R$G<ficO!KLf}T
p$_$Apan8@pP0cF-a#Qmd7(UFOu<Fa!l>b03gQu&X%Q~loCICce7DfO7
literal 0
HcmV?d00001
diff --git a/img/itxt2.png b/img/itxt2.png
new file mode 100644
index 0000000000000000000000000000000000000000..873243ecc53f8e15a5c7808ef3777371d4beaee8
GIT binary patch
literal 112
zcmeAS@N?(olHy`uVBq!ia0vp^j3CSbBp9sfW`_bPE>9Q75RT~N1R$G<ficO!KLf~8
z%nXSran8@pP0cF->3P(!mw{s@w}<YjKn|_5p1N!d!mpV3uiJh4A5bxar>mdKI;Vst
E0G8+)bpQYW
literal 0
HcmV?d00001
diff --git a/img/itxt3.png b/img/itxt3.png
new file mode 100644
index 0000000000000000000000000000000000000000..99c1d97b9a029a5ec5361c0668e8d7c5c338445d
GIT binary patch
literal 114
zcmeAS@N?(olHy`uVBq!ia0vp^j3CSbBp9sfW`_bPE>9Q75RT~N1R$G<ficO!KLf~8
z&J2ktan8@pP0cF->PgLGVBnZpy(QVLAhW0_tMKFEw!I8Si`ljLmdAk%VeoYIb6Mw<
G&;$V1?Hbwu
literal 0
HcmV?d00001
diff --git a/lib/Image/Info/PNG.pm b/lib/Image/Info/PNG.pm
index 4d38338..2726e3b 100644
--- a/lib/Image/Info/PNG.pm
+++ b/lib/Image/Info/PNG.pm
@@ -29,6 +29,13 @@ eval {
$have_zlib++;
};
+# Test for Encode (for reading iTXt chunks)
+my $have_encode = 0;
+eval {
+ require Encode;
+ $have_encode++;
+};
+
sub my_read
{
my($source, $len) = @_;
@@ -132,16 +139,38 @@ sub process_file
}
$info->push_info(0, "resolution" => $res)
}
- elsif ($type eq "tEXt" || $type eq "zTXt") {
+ elsif ($type eq "tEXt" || $type eq "zTXt" || $type eq "iTXt") {
my($key, $val) = split(/\0/, $data, 2);
- if ($type eq "zTXt") {
- my($method,$ctext) = split(//, $val, 2);
+ my($method,$ctext,$is_i);
+ if ($type eq "iTXt") {
+ ++$is_i;
+ (my $compressed, $method, my $lang, my $trans, $ctext)
+ = unpack "CaZ*Z*a*", $val;
+ unless ($compressed) {
+ undef $method;
+ $val = $ctext;
+ }
+ }
+ elsif ($type eq "zTXt") {
+ ($method,$ctext) = split(//, $val, 2);
+ }
+
+ if (defined $method) {
if ($have_zlib && $method eq "\0") {
$val = Compress::Zlib::uncompress($ctext);
} else {
undef $val;
}
}
+
+ if ($is_i) {
+ if ($have_encode) {
+ $val = Encode::decode("UTF-8", $val);
+ } else {
+ undef $val;
+ }
+ }
+
if (defined $val) {
# XXX should make sure $key is not in conflict with any
# other key we might generate
diff --git a/t/png.t b/t/png.t
index bedbf60..a9da2df 100755
--- a/t/png.t
+++ b/t/png.t
@@ -7,7 +7,7 @@ use strict;
BEGIN
{
- plan tests => 19;
+ plan tests => 22;
chdir 't' if -d 't';
use lib '../lib';
use_ok ("Image::Info") or die($@);
@@ -60,3 +60,28 @@ SKIP:
die ("Couldn't read ztxt.png: $!");
is ($i->{comment}, "some image comment\n", 'ztxt comment');
}
+
+#############################################################################
+# itxt tests
+SKIP:
+ {
+ skip 'Need Encode for this itxt test', 2
+ if !eval { require Encode; 1 };
+
+ # test files generated with pngcrush, which actually honours the spec:
+ $i = image_info("../img/itxt.png") ||
+ die ("Couldn't read itxt.png: $!");
+ is ($i->{Comment}, "\x{1F42A}", 'itxt comment');
+ # keywords such as Comment are case sensitive.
+
+ skip 'Need Compress::Zlib for this itxt test', 2
+ if !eval { require Compress::Zlib; 1 };
+
+ $i = image_info("../img/itxt2.png") ||
+ die ("Couldn't read itxt2.png: $!");
+ is ($i->{Comment}, "Perl Rules", 'compressed itxt comment');
+
+ $i = image_info("../img/itxt3.png") ||
+ die ("Couldn't read itxt3.png: $!");
+ is ($i->{Comment}, "\x{2E18}Success\x{203D}", 'compressed itxt comment');
+ }
--
1.9.1