From: Tony Cook Date: Mon, 23 May 2011 10:50:56 +0000 (+1000) Subject: test and add error reporting to to_*() family methods X-Git-Tag: v0.84~26 X-Git-Url: http://git.imager.perl.org/imager.git/commitdiff_plain/3bcba6dfec282929b20df1374fb519989452a575 test and add error reporting to to_*() family methods --- diff --git a/Changes b/Changes index 3d233555..84d74b1e 100644 --- a/Changes +++ b/Changes @@ -14,6 +14,8 @@ Bug fixes: - fix META.yml testing and the generated META.yml https://rt.cpan.org/Ticket/Display.html?id=65235 + - test and add error reporting to to_*() family methods + Imager 0.83 - 21 May 2011 =========== diff --git a/Imager.pm b/Imager.pm index d27667a1..1fb9f3a3 100644 --- a/Imager.pm +++ b/Imager.pm @@ -968,24 +968,21 @@ sub to_paletted { return; } - my $result = Imager->new; - $result->{IMG} = i_img_to_pal($self->{IMG}, $opts); - - #print "Type ", i_img_type($result->{IMG}), "\n"; + $self->_valid_image + or return; - if ($result->{IMG}) { - return $result; - } - else { - $self->{ERRSTR} = $self->_error_as_msg; + my $result = Imager->new; + unless ($result->{IMG} = i_img_to_pal($self->{IMG}, $opts)) { + $self->_set_error(Imager->_error_as_msg); return; } + + return $result; } -# convert a paletted (or any image) to an 8-bit/channel RGB images +# convert a paletted (or any image) to an 8-bit/channel RGB image sub to_rgb8 { my $self = shift; - my $result; unless (defined wantarray) { my @caller = caller; @@ -993,30 +990,35 @@ sub to_rgb8 { return; } - if ($self->{IMG}) { - $result = Imager->new; - $result->{IMG} = i_img_to_rgb($self->{IMG}) - or undef $result; + $self->_valid_image + or return; + + my $result = Imager->new; + unless ($result->{IMG} = i_img_to_rgb($self->{IMG})) { + $self->_set_error(Imager->_error_as_msg()); + return; } return $result; } -# convert a paletted (or any image) to an 8-bit/channel RGB images +# convert a paletted (or any image) to a 16-bit/channel RGB image sub to_rgb16 { my $self = shift; - my $result; unless (defined wantarray) { my @caller = caller; - warn "to_rgb16() called in void context - to_rgb8() returns the converted image at $caller[1] line $caller[2]\n"; + warn "to_rgb16() called in void context - to_rgb16() returns the converted image at $caller[1] line $caller[2]\n"; return; } - if ($self->{IMG}) { - $result = Imager->new; - $result->{IMG} = i_img_to_rgb16($self->{IMG}) - or undef $result; + $self->_valid_image + or return; + + my $result = Imager->new; + unless ($result->{IMG} = i_img_to_rgb16($self->{IMG})) { + $self->_set_error(Imager->_error_as_msg()); + return; } return $result; diff --git a/t/t01introvert.t b/t/t01introvert.t index be1baf04..5b3e5975 100644 --- a/t/t01introvert.t +++ b/t/t01introvert.t @@ -3,7 +3,7 @@ # to make sure we get expected values use strict; -use Test::More tests => 230; +use Test::More tests => 233; BEGIN { use_ok(Imager => qw(:handy :all)) } @@ -132,7 +132,8 @@ is(Imager::i_img_type($im_pal), 0, "pal img shouldn't be paletted now"); # test the OO interfaces my $impal2 = Imager->new(type=>'pseudo', xsize=>200, ysize=>201); -ok($impal2, "make paletted via OO"); +ok($impal2, "make paletted via OO") + or diag(Imager->errstr); is($impal2->getchannels, 3, "check channels"); is($impal2->bits, 8, "check bits"); is($impal2->type, 'paletted', "check type"); @@ -167,7 +168,8 @@ is($impal2->getheight, 201, "check height"); "we can setcolors"); # make an rgb version - my $imrgb2 = $impal2->to_rgb8(); + my $imrgb2 = $impal2->to_rgb8() + or diag($impal2->errstr); is($imrgb2->type, 'direct', "converted is direct"); # and back again, specifying the palette @@ -183,6 +185,13 @@ is($impal2->getheight, 201, "check height"); is($impal3->type, 'paletted', "and is paletted"); } +{ # to_rgb on incomplete image + my $im = Imager->new; + ok($im, "make empty image"); + ok(!$im->to_rgb8, "convert to rgb8"); + is($im->errstr, "empty input image", "check message"); +} + { # basic checks, 8-bit direct images my $im = Imager->new(xsize => 2, ysize => 3); ok($im, 'create 8-bit direct image'); diff --git a/t/t021sixteen.t b/t/t021sixteen.t index 14682615..14f713b7 100644 --- a/t/t021sixteen.t +++ b/t/t021sixteen.t @@ -1,6 +1,6 @@ #!perl -w use strict; -use Test::More tests => 104; +use Test::More tests => 107; BEGIN { use_ok(Imager=>qw(:all :handy)) } @@ -206,6 +206,13 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/, is_image($im, $im16, "check image data matches"); } +{ # empty image handling + my $im = Imager->new; + ok($im, "make empty image"); + ok(!$im->to_rgb16, "convert empty image to 16-bit"); + is($im->errstr, "empty input image", "check message"); +} + { # bounds checks my $im = Imager->new(xsize => 10, ysize => 10, bits => 16); image_bounds_checks($im);