From dbb201d59ead46aec1d280fe753dcb36fdd767a0 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 7 Oct 2014 16:10:11 -0400 Subject: implement P6 --- lib/Image/PNM.pm | 43 +++++++++++++++++++++++++++++++ t/P1.t | 7 +++++ t/P2.t | 7 +++++ t/P3.t | 7 +++++ t/P4.t | 7 +++++ t/P5.t | 7 +++++ t/P6.t | 77 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ t/data/P6.ppm | Bin 0 -> 194 bytes 8 files changed, 155 insertions(+) create mode 100644 t/P6.t create mode 100644 t/data/P6.ppm diff --git a/lib/Image/PNM.pm b/lib/Image/PNM.pm index 4deca51..42121da 100644 --- a/lib/Image/PNM.pm +++ b/lib/Image/PNM.pm @@ -191,6 +191,24 @@ HEADER return $data; } +sub _as_string_P6 { + my $self = shift; + + my $data = <{w} $self->{h} +$self->{max} +HEADER + + for my $row (@{ $self->{pixels} }) { + $data .= pack("C*", map { + ref($_) ? @$_ : ($_, $_, $_) + } @$row); + } + + return $data; +} + sub _parse_string { my $self = shift; my ($string) = @_; @@ -345,6 +363,31 @@ sub _parse_pnm_P5 { } } +sub _parse_pnm_P6 { + 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->(), + $next_word->(), + $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 325f87e..adf267e 100644 --- a/t/P1.t +++ b/t/P1.t @@ -67,4 +67,11 @@ P5 \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 +is($image->as_string('P6') . "\n", <as_string('P6') . "\n", <as_string('P6') . "\n", <as_string('P6') . "\n", <as_string('P6') . "\n", <new('t/data/P6.ppm'); + +is($image->width, 6); +is($image->height, 8); +is($image->max_pixel_value, 255); +is_deeply($image->raw_pixel(1, 2), [0, 84, 255]); +is_deeply($image->pixel(4, 1), [1, 0, 0]); + +is($image->as_string('P1'), <as_string('P2'), <as_string('P3'), <as_string('P4') . "\n", <as_string('P5') . "\n", <as_string('P6') . "\n", <