Permalink
Browse files

consistently use _valid_image() internally to ensure image validity

and test for it.
  • Loading branch information...
tonycoz committed Nov 12, 2012
1 parent 98542a4 commit afb6f324653c52ec09d535c703f575d485f9560f
Showing with 610 additions and 161 deletions.
  1. +218 −126 Imager.pm
  2. +4 −1 Imager.xs
  3. +1 −0 MANIFEST
  4. +1 −0 MANIFEST.SKIP
  5. +92 −0 lib/Imager/Font/Test.pm
  6. +46 −3 t/t01introvert.t
  7. +8 −1 t/t020masked.t
  8. +1 −1 t/t021sixteen.t
  9. +1 −1 t/t022double.t
  10. +39 −2 t/t023palette.t
  11. +13 −1 t/t1000files.t
  12. +20 −1 t/t21draw.t
  13. +18 −1 t/t31font.t
  14. +3 −3 t/t40scale.t
  15. +8 −2 t/t55trans.t
  16. +11 −1 t/t58trans2.t
  17. +15 −1 t/t61filters.t
  18. +16 −1 t/t62compose.t
  19. +2 −1 t/t63combine.t
  20. +30 −1 t/t64copyflip.t
  21. +7 −1 t/t65crop.t
  22. +15 −1 t/t66paste.t
  23. +7 −1 t/t67convert.t
  24. +8 −1 t/t68map.t
  25. +2 −2 t/t69rubthru.t
  26. +14 −1 t/t90cc.t
  27. +10 −6 t/t99thread.t
View
344 Imager.pm

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -2122,7 +2122,7 @@ i_convert(src, avmain)
RETVAL
-void
+undef_int
i_map(im, pmaps)
Imager::ImgRaw im
PREINIT:
@@ -2160,6 +2160,9 @@ i_map(im, pmaps)
}
i_map(im, maps, mask);
myfree(maps);
+ RETVAL = 1;
+ OUTPUT:
+ RETVAL
View
@@ -185,6 +185,7 @@ lib/Imager/Font.pm
lib/Imager/Font/BBox.pm
lib/Imager/Font/FreeType2.pm
lib/Imager/Font/Image.pm
+lib/Imager/Font/Test.pm Font for testing (outputs boxes only)
lib/Imager/Font/Truetype.pm
lib/Imager/Font/Type1.pm Compatibility wrapper for Imager::Font::T1
lib/Imager/Font/Wrap.pm
View
@@ -134,3 +134,4 @@ Makefile\.old$
# sub-module build junk
\.bak$
+MYMETA.json
View
@@ -0,0 +1,92 @@
+package Imager::Font::Test;
+use strict;
+
+use base 'Imager::Font';
+
+sub new {
+ my ($class, %opts) = @_;
+
+ bless \%opts, shift;
+}
+
+sub _draw {
+ my ($self, %input) = @_;
+
+ my $text = $input{string};
+
+ my $ppn = int($input{size} * 0.5 + 0.5);
+ my $desc = int($input{size} * 0.3 + 0.5);
+ my $asc = $input{size} - $desc;
+ my $width = $ppn * length $text;
+ my $x = $input{x};
+ my $y = $input{'y'};
+ $input{align} and $y -= $asc;
+
+ $input{image}->box(color => $input{color}, xmin => $x, ymin => $y,
+ xmax => $x + $width-1, ymax => $y + $input{size} - 1);
+
+ return 1;
+}
+
+sub _bounding_box {
+ my ($self, %input) = @_;
+
+ my $text = $input{string};
+
+ my $ppn = int($input{size} * 0.5 + 0.5);
+ my $desc = int($input{size} * 0.3 + 0.5);
+ my $asc = $input{size} - $desc;
+
+ return ( 0, -$desc, $ppn * length $text, $asc, -$desc, $asc, $ppn * length $text, 0 );
+}
+
+sub has_chars {
+ my ($self, %input) = @_;
+
+ my $text = $input{string};
+ defined $text
+ or return Imager->_set_error("has_chars: No string parameter supplied");
+
+ return (1) x length $text;
+}
+
+sub face_name {
+ "test";
+}
+
+sub glyph_names {
+ my ($self, %input) = @_;
+
+ my $text = $input{string};
+ defined $text
+ or return Imager->_set_error("glyph_names: No string parameter supplied");
+
+ return (1) x length $text;
+}
+
+1;
+
+=head1 NAME'
+
+Imager::Font::Test - font driver producing consistent output for tests.
+
+=head1 SYNOPSIS
+
+ my $font = Imager::Font::Test->new;
+
+ # use $font where you use other fonts
+
+=head1 DESCRIPTION
+
+Imager::Font::Test is intended to produce consistent output without
+being subject to the inconsistent output produced by different
+versions of font libraries.
+
+The output is simple box for the whole string.
+
+=head1 AUTHOR
+
+Tony Cook <tonyc@cpan.org>
+
+=cut
+
View
@@ -3,7 +3,7 @@
# to make sure we get expected values
use strict;
-use Test::More tests => 433;
+use Test::More tests => 465;
BEGIN { use_ok(Imager => qw(:handy :all)) }
@@ -185,11 +185,36 @@ 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");
+ is($im->errstr, "to_rgb8: empty input image", "check message");
+ is($im->bits, undef, "can't call bits on an empty image");
+ is($im->errstr, "bits: empty input image", "check message");
+ is($im->type, undef, "can't call type on an empty image");
+ is($im->errstr, "type: empty input image", "check message");
+ is($im->virtual, undef, "can't call virtual on an empty image");
+ is($im->errstr, "virtual: empty input image", "check message");
+ is($im->is_bilevel, undef, "can't call virtual on an empty image");
+ is($im->errstr, "is_bilevel: empty input image", "check message");
+ ok(!$im->getscanline(y => 0), "can't call getscanline on an empty image");
+ is($im->errstr, "getscanline: empty input image", "check message");
+ ok(!$im->setscanline(y => 0, pixels => [ $red, $blue ]),
+ "can't call setscanline on an empty image");
+ is($im->errstr, "setscanline: empty input image", "check message");
+ ok(!$im->getsamples(y => 0), "can't call getsamples on an empty image");
+ is($im->errstr, "getsamples: empty input image", "check message");
+ is($im->getwidth, undef, "can't get width of empty image");
+ is($im->errstr, "getwidth: empty input image", "check message");
+ is($im->getheight, undef, "can't get height of empty image");
+ is($im->errstr, "getheight: empty input image", "check message");
+ is($im->getchannels, undef, "can't get channels of empty image");
+ is($im->errstr, "getchannels: empty input image", "check message");
+ is($im->getmask, undef, "can't get mask of empty image");
+ is($im->errstr, "getmask: empty input image", "check message");
+ is($im->setmask, undef, "can't set mask of empty image");
+ is($im->errstr, "setmask: empty input image", "check message");
}
{ # basic checks, 8-bit direct images
@@ -1064,6 +1089,24 @@ my $psamp_outside_error = "Image position outside of image";
}
}
+{
+ my $empty = Imager->new;
+ ok(!$empty->addtag(name => "foo", value => 1),
+ "can't addtag on an empty image");
+ is($empty->errstr, "addtag: empty input image",
+ "check error message");
+ ok(!$empty->settag(name => "foo", value => 1),
+ "can't settag on an empty image");
+ is($empty->errstr, "settag: empty input image",
+ "check error message");
+ ok(!$empty->deltag(name => "foo"), "can't deltag on an empty image");
+ is($empty->errstr, "deltag: empty input image",
+ "check error message");
+ ok(!$empty->tags(name => "foo"), "can't tags on an empty image");
+ is($empty->errstr, "tags: empty input image",
+ "check error message");
+}
+
Imager->close_log();
unless ($ENV{IMAGER_KEEP_FILES}) {
View
@@ -1,6 +1,6 @@
#!perl -w
use strict;
-use Test::More tests => 242;
+use Test::More tests => 244;
use Imager qw(:all :handy);
use Imager::Test qw(is_color3 is_fcolor3);
@@ -687,6 +687,13 @@ for my $masked (0, 1) { # psampf
"check values written");
}
+{
+ my $empty = Imager->new;
+ ok(!$empty->masked, "fail to make a masked image from an empty");
+ is($empty->errstr, "masked: empty input image",
+ "check error message");
+}
+
Imager->close_log();
unless ($ENV{IMAGER_KEEP_FILES}) {
View
@@ -210,7 +210,7 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
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");
+ is($im->errstr, "to_rgb16: empty input image", "check message");
}
{ # bounds checks
View
@@ -166,7 +166,7 @@ cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
my $im = Imager->new;
ok($im, "make empty image");
ok(!$im->to_rgb_double, "convert empty image to double");
- is($im->errstr, "empty input image", "check message");
+ is($im->errstr, "to_rgb_double: empty input image", "check message");
}
my $psamp_outside_error = "Image position outside of image";
View
@@ -1,7 +1,7 @@
#!perl -w
# some of this is tested in t01introvert.t too
use strict;
-use Test::More tests => 211;
+use Test::More tests => 226;
BEGIN { use_ok("Imager", ':handy'); }
use Imager::Test qw(image_bounds_checks test_image is_color3 isnt_image is_color4 is_fcolor3);
@@ -29,7 +29,9 @@ my $blacki = $img->addcolors(colors=>[ $black, $red, $green, $blue ]);
print "# blacki $blacki\n";
ok(defined $blacki && $blacki == 0, "we got the first color");
-ok($img->colorcount() == 4, "should have 4 colors");
+is($img->colorcount(), 4, "should have 4 colors");
+is($img->maxcolors, 256, "maxcolors always 256");
+
my ($redi, $greeni, $bluei) = 1..3;
my @all = $img->getcolors;
@@ -590,6 +592,41 @@ my $psamp_outside_error = "Image position outside of image";
0, 0, 1.0, "get a pixel in float form, make sure it's blue");
}
+{
+ my $empty = Imager->new;
+ ok(!$empty->to_paletted, "can't convert an empty image");
+ is($empty->errstr, "to_paletted: empty input image",
+ "check error message");
+
+ is($empty->addcolors(colors => [ $black ]), -1,
+ "can't addcolors() to an empty image");
+ is($empty->errstr, "addcolors: empty input image",
+ "check error message");
+
+ ok(!$empty->setcolors(colors => [ $black ]),
+ "can't setcolors() to an empty image");
+ is($empty->errstr, "setcolors: empty input image",
+ "check error message");
+
+ ok(!$empty->getcolors(),
+ "can't getcolors() from an empty image");
+ is($empty->errstr, "getcolors: empty input image",
+ "check error message");
+
+ is($empty->colorcount, -1, "can't colorcount() an empty image");
+ is($empty->errstr, "colorcount: empty input image",
+ "check error message");
+
+ is($empty->maxcolors, -1, "can't maxcolors() an empty image");
+ is($empty->errstr, "maxcolors: empty input image",
+ "check error message");
+
+ is($empty->findcolor(color => $blue), undef,
+ "can't findcolor an empty image");
+ is($empty->errstr, "findcolor: empty input image",
+ "check error message");
+}
+
Imager->close_log;
unless ($ENV{IMAGER_KEEP_FILES}) {
View
@@ -4,7 +4,7 @@
# the file format
use strict;
-use Test::More tests => 85;
+use Test::More tests => 89;
use Imager;
-d "testout" or mkdir "testout";
@@ -146,6 +146,18 @@ is(Imager->errstr, "check_file_limits: width must be a positive integer",
}
}
+{ # test empty image handling for write()/write_multi()
+ my $empty = Imager->new;
+ my $data;
+ ok(!$empty->write(data => \$data, type => "pnm"),
+ "fail to write an empty image");
+ is($empty->errstr, "write: empty input image", "check error message");
+ my $good = Imager->new(xsize => 1, ysize => 1);
+ ok(!Imager->write_multi({ data => \$data, type => "pnm" }, $good, $empty),
+ "fail to write_multi an empty image");
+ is(Imager->errstr, "write_multi: empty input image (image 2)");
+}
+
# check file type probe
probe_ok("49492A41", undef, "not quite tiff");
probe_ok("4D4D0041", undef, "not quite tiff");
View
@@ -1,6 +1,6 @@
#!perl -w
use strict;
-use Test::More tests => 244;
+use Test::More tests => 256;
use Imager ':all';
use Imager::Test qw(is_color3 is_image);
use constant PI => 3.14159265358979;
@@ -297,6 +297,25 @@ my $white = '#FFFFFF';
}
}
+{
+ my $empty = Imager->new;
+ ok(!$empty->box(), "can't draw box to empty image");
+ is($empty->errstr, "box: empty input image", "check error message");
+ ok(!$empty->arc(), "can't draw arc to empty image");
+ is($empty->errstr, "arc: empty input image", "check error message");
+ ok(!$empty->line(x1 => 0, y1 => 0, x2 => 10, y2 => 0),
+ "can't draw line to empty image");
+ is($empty->errstr, "line: empty input image", "check error message");
+ ok(!$empty->polyline(points => [ [ 0, 0 ], [ 10, 0 ] ]),
+ "can't draw polyline to empty image");
+ is($empty->errstr, "polyline: empty input image", "check error message");
+ ok(!$empty->polygon(points => [ [ 0, 0 ], [ 10, 0 ], [ 0, 10 ] ]),
+ "can't draw polygon to empty image");
+ is($empty->errstr, "polygon: empty input image", "check error message");
+ ok(!$empty->flood_fill(x => 0, y => 0), "can't flood fill to empty image");
+ is($empty->errstr, "flood_fill: empty input image", "check error message");
+}
+
malloc_state();
View
@@ -1,7 +1,7 @@
#!perl -w
use strict;
use Imager;
-use Test::More tests => 10;
+use Test::More tests => 14;
unshift @INC, "t";
@@ -40,3 +40,20 @@ SKIP:
or skip("Failed to load", 1);
ok($good->isa("GoodTestFont"), "and it's the right type");
}
+
+
+use Imager::Font::Test;
+
+# check string() and align_string() handle an empty image
+{
+ my $font = Imager::Font::Test->new;
+ my $empty = Imager->new;
+ ok(!$empty->string(text => "foo", x => 0, y => 10, size => 10, font => $font),
+ "can't draw text on an empty image");
+ is($empty->errstr, "string: empty input image",
+ "check error message");
+ ok(!$empty->align_string(text => "foo", x => 0, y => 10, size => 10, font => $font),
+ "can't draw text on an empty image");
+ is($empty->errstr, "align_string: empty input image",
+ "check error message");
+}
Oops, something went wrong.

0 comments on commit afb6f32

Please sign in to comment.