diff options
author | Jesse Luehrs <doy@tozt.net> | 2014-10-07 16:51:57 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2014-10-07 16:51:57 -0400 |
commit | ed8e2b3f8b13f403f70bec487066bdd30c9a8cc8 (patch) | |
tree | efea672506f8a9cdda526ac6dbf779b772f94d63 | |
parent | 616b8e75b274c76a791960ae6343d9b3cc873d21 (diff) | |
download | image-pnm-ed8e2b3f8b13f403f70bec487066bdd30c9a8cc8.tar.gz image-pnm-ed8e2b3f8b13f403f70bec487066bdd30c9a8cc8.zip |
add support for modifying the image
-rw-r--r-- | lib/Image/PNM.pm | 63 | ||||
-rw-r--r-- | t/write.t | 51 |
2 files changed, 105 insertions, 9 deletions
diff --git a/lib/Image/PNM.pm b/lib/Image/PNM.pm index b089851..6239d23 100644 --- a/lib/Image/PNM.pm +++ b/lib/Image/PNM.pm @@ -69,25 +69,53 @@ sub as_string { return $self->$method; } -=method width +=method width($w) -Returns the width of the image in pixels. +Returns the width of the image in pixels. If C<$w> is given, sets the width of +the image to C<$w>. =cut sub width { my $self = shift; + my ($w) = @_; + if (defined($w)) { + for my $row (@{ $self->{pixels} }) { + if ($w > $self->{w}) { + push @$row, 0 + for 1..($w - $self->{w}); + } + else { + pop @$row + for 1..($self->{w} - $w); + } + } + $self->{w} = $w; + } return $self->{w}; } -=method height +=method height($h) -Returns the height of the image in pixels. +Returns the height of the image in pixels. If C<$h> is given, sets the height +of the image to C<$h>. =cut sub height { my $self = shift; + my ($h) = @_; + if (defined($h)) { + if ($h > $self->{h}) { + push @{ $self->{pixels} }, [ (0) x $self->{w} ] + for 1..($h - $self->{h}); + } + else { + pop @{ $self->{pixels} } + for 1..($self->{h} - $h); + } + $self->{h} = $h; + } return $self->{h}; } @@ -100,10 +128,17 @@ and they are interpreted as being scaled by this value. sub max_pixel_value { my $self = shift; + my ($max) = @_; + if (defined($max)) { + for my $row (@{ $self->{pixels} }) { + @$row = map { $_ * $self->{max} / $max } @$row; + } + $self->{max} = $max; + } return $self->{max}; } -=method pixel($row, $col) +=method pixel($row, $col, $new_value) Returns the value of a pixel at the given C<$row> and C<$col>. The value is returned as an arrayref of three RGB values, where each value ranges from @@ -113,13 +148,18 @@ C<0.0> to C<1.0>. sub pixel { my $self = shift; - my ($row, $col) = @_; + my ($row, $col, $new_value) = @_; - my $pixel = $self->raw_pixel($row, $col); + if (defined($new_value)) { + $new_value = ref($new_value) + ? [ map { $_ * $self->{max} } @$new_value ] + : $new_value * $self->{max}; + } + my $pixel = $self->raw_pixel($row, $col, $new_value); return [ map { $_ / $self->{max} } @$pixel ]; } -=method raw_pixel($row, $col) +=method raw_pixel($row, $col, $new_value) Returns the value of a pixel at the given C<$row> and C<$col>. The value is returned as an arrayref of three RGB values, where each value is an integer @@ -129,12 +169,17 @@ ranging from C<0> to C<< $image->max_pixel_value >>. sub raw_pixel { my $self = shift; - my ($row, $col) = @_; + my ($row, $col, $new_value) = @_; my $pixel = $self->{pixels}[$row][$col]; die "invalid pixel location ($row, $col)" unless defined $pixel; + if (defined($new_value)) { + $self->{pixels}[$row][$col] = $new_value; + $pixel = $new_value; + } + if (!ref $pixel) { $pixel = [ $pixel, $pixel, $pixel ]; } diff --git a/t/write.t b/t/write.t new file mode 100644 index 0000000..36f716e --- /dev/null +++ b/t/write.t @@ -0,0 +1,51 @@ +#!/usr/bin/env perl +use strict; +use warnings; +use Test::More; + +use Image::PNM; + +{ + my $image = Image::PNM->new; + $image->width(6); + $image->height(8); + $image->max_pixel_value(255); + + for my $col (0..5) { + for my $row (0..7) { + $image->pixel($row, $col, [1, 1, 1]); + } + } + $image->raw_pixel(1, 2, [0, 84, 255]); + $image->raw_pixel(1, 3, [0, 84, 255]); + $image->raw_pixel(2, 1, [0, 0, 0]); + $image->raw_pixel(2, 4, [0, 0, 0]); + $image->raw_pixel(3, 0, [0, 0, 0]); + $image->raw_pixel(3, 5, [0, 0, 0]); + $image->raw_pixel(4, 0, [0, 0, 0]); + $image->raw_pixel(4, 1, [255, 0, 0]); + $image->raw_pixel(4, 2, [255, 0, 0]); + $image->raw_pixel(4, 3, [255, 0, 0]); + $image->raw_pixel(4, 4, [255, 0, 0]); + $image->raw_pixel(4, 5, [0, 0, 0]); + $image->raw_pixel(5, 0, [0, 0, 0]); + $image->raw_pixel(5, 5, [0, 0, 0]); + $image->raw_pixel(6, 0, [0, 0, 0]); + $image->raw_pixel(6, 5, [0, 0, 0]); + + 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 0 84 255 0 84 255 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 255 0 0 255 0 0 255 0 0 255 0 0 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 +} + +done_testing; |