From 3b5bc86786a56b9437d1e6ac5a3de786110d731e Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Tue, 7 Oct 2014 15:58:36 -0400 Subject: implement P5 --- lib/Image/PNM.pm | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) (limited to 'lib/Image') 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 = <{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) = @_; -- cgit v1.2.3-54-g00ecf