summaryrefslogtreecommitdiffstats
path: root/lib/Image/PNM.pm
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Image/PNM.pm')
-rw-r--r--lib/Image/PNM.pm37
1 files changed, 37 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) = @_;