summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2014-10-07 14:11:36 -0400
committerJesse Luehrs <doy@tozt.net>2014-10-07 14:15:09 -0400
commitc2372470164611b5597ea63faf8dcfe33381df39 (patch)
tree4e2eace2403ab2bc9790d2fb6d0bd629ecae35b6
parent64157f5b6f4649f7ca845cb119535863dd221822 (diff)
downloadimage-pnm-c2372470164611b5597ea63faf8dcfe33381df39.tar.gz
image-pnm-c2372470164611b5597ea63faf8dcfe33381df39.zip
implement P2
-rw-r--r--lib/Image/PNM.pm37
-rw-r--r--t/P2.t30
-rw-r--r--t/data/P2.pgm52
3 files changed, 119 insertions, 0 deletions
diff --git a/lib/Image/PNM.pm b/lib/Image/PNM.pm
index 7302683..5a97439 100644
--- a/lib/Image/PNM.pm
+++ b/lib/Image/PNM.pm
@@ -88,6 +88,22 @@ HEADER
return $data;
}
+sub _as_string_P2 {
+ my $self = shift;
+
+ my $data = <<HEADER;
+P2
+$self->{w} $self->{h}
+$self->{max}
+HEADER
+
+ for my $row (@{ $self->{pixels} }) {
+ $data .= join(' ', @$row) . "\n";
+ }
+
+ return $data;
+}
+
sub _as_string_P3 {
my $self = shift;
@@ -173,6 +189,27 @@ sub _parse_pnm_P1 {
}
}
+sub _parse_pnm_P2 {
+ 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_word($next_line, 1);
+
+ $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 _parse_pnm_P3 {
my $self = shift;
my ($next_line) = @_;
diff --git a/t/P2.t b/t/P2.t
new file mode 100644
index 0000000..41c2d8e
--- /dev/null
+++ b/t/P2.t
@@ -0,0 +1,30 @@
+#!/usr/bin/env perl
+use strict;
+use warnings;
+use Test::More;
+
+use Image::PNM;
+
+my $image = Image::PNM->new('t/data/P2.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('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
+
+done_testing;
diff --git a/t/data/P2.pgm b/t/data/P2.pgm
new file mode 100644
index 0000000..0dbad2e
--- /dev/null
+++ b/t/data/P2.pgm
@@ -0,0 +1,52 @@
+P2
+# CREATOR: GIMP PNM Filter Version 1.1
+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