summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-10-07 15:58:36 -0400
committerJesse Luehrs <doy@tozt.net>2014-10-07 15:58:36 -0400
commit3b5bc86786a56b9437d1e6ac5a3de786110d731e (patch)
tree40c1b7648cfc412be4f424c9905c42069f6de17b
parentc58bbc3a8368dc96f1a07e2ee3bf0825c9e2af34 (diff)
downloadimage-pnm-3b5bc86786a56b9437d1e6ac5a3de786110d731e.tar.gz
image-pnm-3b5bc86786a56b9437d1e6ac5a3de786110d731e.zip
implement P5
-rw-r--r--lib/Image/PNM.pm44
-rw-r--r--t/P1.t7
-rw-r--r--t/P2.t7
-rw-r--r--t/P3.t7
-rw-r--r--t/P4.t7
-rw-r--r--t/P5.t70
-rw-r--r--t/data/P5.pgmbin0 -> 98 bytes
7 files changed, 142 insertions, 0 deletions
diff --git a/lib/Image/PNM.pm b/lib/Image/PNM.pm
index b927b44..4deca51 100644
--- a/lib/Image/PNM.pm
+++ b/lib/Image/PNM.pm
@@ -168,6 +168,29 @@ HEADER
return $data;
}
+sub _as_string_P5 {
+ my $self = shift;
+
+ my $data = <<HEADER;
+P5
+$self->{w} $self->{h}
+$self->{max}
+HEADER
+
+ for my $row (@{ $self->{pixels} }) {
+ $data .= pack("C*", map {
+ if (ref($_)) {
+ $self->_to_greyscale(@$_)
+ }
+ else {
+ $_
+ }
+ } @$row);
+ }
+
+ return $data;
+}
+
sub _parse_string {
my $self = shift;
my ($string) = @_;
@@ -301,6 +324,27 @@ sub _parse_pnm_P4 {
}
}
+sub _parse_pnm_P5 {
+ my ($self) = shift;
+ my ($next_line) = @_;
+
+ chomp (my $max = $next_line->());
+ die "Invalid max color value: $max"
+ unless $max =~ /^[0-9]+$/ && $max > 0;
+ $self->{max} = $max;
+
+ my $next_word = $self->_make_next_bitfield($next_line, 0);
+
+ $self->{pixels} = [];
+ for my $i (1..$self->{h}) {
+ my $row = [];
+ for my $j (1..$self->{w}) {
+ push @$row, $next_word->();
+ }
+ push @{ $self->{pixels} }, $row;
+ }
+}
+
sub _make_next_word {
my $self = shift;
my ($next_line, $ws) = @_;
diff --git a/t/P1.t b/t/P1.t
index 030634b..325f87e 100644
--- a/t/P1.t
+++ b/t/P1.t
@@ -60,4 +60,11 @@ P4
\x00\x30\x48\x84\xfc\x84\x84\x00
IMAGE
+is($image->as_string('P5') . "\n", <<IMAGE);
+P5
+6 8
+1
+\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x01\x01\x01\x00\x01\x01\x00\x01\x00\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x00\x00\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01
+IMAGE
+
done_testing;
diff --git a/t/P2.t b/t/P2.t
index 6ff8a29..88efe59 100644
--- a/t/P2.t
+++ b/t/P2.t
@@ -60,4 +60,11 @@ P4
\x00\x30\x48\x84\xfc\x84\x84\x00
IMAGE
+is($image->as_string('P5') . "\n", <<IMAGE);
+P5
+6 8
+255
+\xff\xff\xff\xff\xff\xff\xff\xff\x4e\x4e\xff\xff\xff\x00\xff\xff\x00\xff\x00\xff\xff\xff\xff\x00\x00\x36\x36\x36\x36\x00\x00\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff
+IMAGE
+
done_testing;
diff --git a/t/P3.t b/t/P3.t
index 925b2d7..3a60aa0 100644
--- a/t/P3.t
+++ b/t/P3.t
@@ -60,4 +60,11 @@ P4
\x00\x30\x48\x84\xfc\x84\x84\x00
IMAGE
+is($image->as_string('P5') . "\n", <<IMAGE);
+P5
+6 8
+255
+\xff\xff\xff\xff\xff\xff\xff\xff\x4e\x4e\xff\xff\xff\x00\xff\xff\x00\xff\x00\xff\xff\xff\xff\x00\x00\x36\x36\x36\x36\x00\x00\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff
+IMAGE
+
done_testing;
diff --git a/t/P4.t b/t/P4.t
index 9655658..7c68665 100644
--- a/t/P4.t
+++ b/t/P4.t
@@ -60,4 +60,11 @@ P4
\x00\x30\x48\x84\xfc\x84\x84\x00
IMAGE
+is($image->as_string('P5') . "\n", <<IMAGE);
+P5
+6 8
+1
+\x01\x01\x01\x01\x01\x01\x01\x01\x00\x00\x01\x01\x01\x00\x01\x01\x00\x01\x00\x01\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x00\x00\x01\x01\x01\x01\x00\x01\x01\x01\x01\x01\x01
+IMAGE
+
done_testing;
diff --git a/t/P5.t b/t/P5.t
new file mode 100644
index 0000000..bbd0cbf
--- /dev/null
+++ b/t/P5.t
@@ -0,0 +1,70 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Image::PNM;
+
+my $image = Image::PNM->new('t/data/P5.pgm');
+
+is($image->width, 6);
+is($image->height, 8);
+is($image->max_pixel_value, 255);
+is_deeply($image->raw_pixel(1, 2), [78, 78, 78]);
+is_deeply($image->pixel(0, 0), [1, 1, 1]);
+
+is($image->as_string('P1'), <<IMAGE);
+P1
+6 8
+0 0 0 0 0 0
+0 0 1 1 0 0
+0 1 0 0 1 0
+1 0 0 0 0 1
+1 1 1 1 1 1
+1 0 0 0 0 1
+1 0 0 0 0 1
+0 0 0 0 0 0
+IMAGE
+
+is($image->as_string('P2'), <<IMAGE);
+P2
+6 8
+255
+255 255 255 255 255 255
+255 255 78 78 255 255
+255 0 255 255 0 255
+0 255 255 255 255 0
+0 54 54 54 54 0
+0 255 255 255 255 0
+0 255 255 255 255 0
+255 255 255 255 255 255
+IMAGE
+
+is($image->as_string('P3'), <<IMAGE);
+P3
+6 8
+255
+255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
+255 255 255 255 255 255 78 78 78 78 78 78 255 255 255 255 255 255
+255 255 255 0 0 0 255 255 255 255 255 255 0 0 0 255 255 255
+0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0
+0 0 0 54 54 54 54 54 54 54 54 54 54 54 54 0 0 0
+0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0
+0 0 0 255 255 255 255 255 255 255 255 255 255 255 255 0 0 0
+255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255
+IMAGE
+
+is($image->as_string('P4') . "\n", <<IMAGE);
+P4
+6 8
+\x00\x30\x48\x84\xfc\x84\x84\x00
+IMAGE
+
+is($image->as_string('P5') . "\n", <<IMAGE);
+P5
+6 8
+255
+\xff\xff\xff\xff\xff\xff\xff\xff\x4e\x4e\xff\xff\xff\x00\xff\xff\x00\xff\x00\xff\xff\xff\xff\x00\x00\x36\x36\x36\x36\x00\x00\xff\xff\xff\xff\x00\x00\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff
+IMAGE
+
+done_testing;
diff --git a/t/data/P5.pgm b/t/data/P5.pgm
new file mode 100644
index 0000000..31285e0
--- /dev/null
+++ b/t/data/P5.pgm
Binary files differ