Debian Codegraph @

lib/Imager/Test.pm

raw
1 package Imager::Test;
2 use 5.006;
3 use strict;
4 use Imager;
5 use Test::More;
6 use Test::Builder;
7 require Exporter;
8 use Carp qw(croak carp);
9 use Config;
10
11 our $VERSION = "1.007";
12
13 our @ISA = qw(Exporter);
14 our @EXPORT_OK =
15 qw(
16 diff_text_with_nul
17 test_image_raw
18 test_image_16
19 test_image
20 test_image_double
21 test_image_mono
22 test_image_gray
23 test_image_gray_16
24 test_image_named
25 is_color1
26 is_color3
27 is_color4
28 is_color_close3
29 is_fcolor1
30 is_fcolor3
31 is_fcolor4
32 color_cmp
33 is_image
34 is_imaged
35 is_image_similar
36 isnt_image
37 image_bounds_checks
38 mask_tests
39 test_colorf_gpix
40 test_color_gpix
41 test_colorf_glin
42 can_test_threads
43 std_font_tests
44 std_font_test_count
45 );
46
47 sub diff_text_with_nul {
48 my ($desc, $text1, $text2, @params) = @_;
49
50 my $builder = Test::Builder->new;
51
52 print "# $desc\n";
53 my $imbase = Imager->new(xsize => 100, ysize => 100);
54 my $imcopy = Imager->new(xsize => 100, ysize => 100);
55
56 $builder->ok($imbase->string(x => 5, 'y' => 50, size => 20,
57 string => $text1,
58 @params), "$desc - draw text1");
59 $builder->ok($imcopy->string(x => 5, 'y' => 50, size => 20,
60 string => $text2,
61 @params), "$desc - draw text2");
62 $builder->isnt_num(Imager::i_img_diff($imbase->{IMG}, $imcopy->{IMG}), 0,
63 "$desc - check result different");
64 }
65
66 sub is_color3($$$$$) {
67 my ($color, $red, $green, $blue, $comment) = @_;
68
69 my $builder = Test::Builder->new;
70
71 unless (defined $color) {
72 $builder->ok(0, $comment);
73 $builder->diag("color is undef");
74 return;
75 }
76 unless ($color->can('rgba')) {
77 $builder->ok(0, $comment);
78 $builder->diag("color is not a color object");
79 return;
80 }
81
82 my ($cr, $cg, $cb) = $color->rgba;
83 unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue, $comment)) {
84 print <<END_DIAG;
85 Color mismatch:
86 Red: $red vs $cr
87 Green: $green vs $cg
88 Blue: $blue vs $cb
89 END_DIAG
90 return;
91 }
92
93 return 1;
94 }
95
96 sub is_color_close3($$$$$$) {
97 my ($color, $red, $green, $blue, $tolerance, $comment) = @_;
98
99 my $builder = Test::Builder->new;
100
101 unless (defined $color) {
102 $builder->ok(0, $comment);
103 $builder->diag("color is undef");
104 return;
105 }
106 unless ($color->can('rgba')) {
107 $builder->ok(0, $comment);
108 $builder->diag("color is not a color object");
109 return;
110 }
111
112 my ($cr, $cg, $cb) = $color->rgba;
113 unless ($builder->ok(abs($cr - $red) <= $tolerance
114 && abs($cg - $green) <= $tolerance
115 && abs($cb - $blue) <= $tolerance, $comment)) {
116 $builder->diag(<<END_DIAG);
117 Color out of tolerance ($tolerance):
118 Red: expected $red vs received $cr
119 Green: expected $green vs received $cg
120 Blue: expected $blue vs received $cb
121 END_DIAG
122 return;
123 }
124
125 return 1;
126 }
127
128 sub is_color4($$$$$$) {
129 my ($color, $red, $green, $blue, $alpha, $comment) = @_;
130
131 my $builder = Test::Builder->new;
132
133 unless (defined $color) {
134 $builder->ok(0, $comment);
135 $builder->diag("color is undef");
136 return;
137 }
138 unless ($color->can('rgba')) {
139 $builder->ok(0, $comment);
140 $builder->diag("color is not a color object");
141 return;
142 }
143
144 my ($cr, $cg, $cb, $ca) = $color->rgba;
145 unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue
146 && $ca == $alpha, $comment)) {
147 $builder->diag(<<END_DIAG);
148 Color mismatch:
149 Red: $cr vs $red
150 Green: $cg vs $green
151 Blue: $cb vs $blue
152 Alpha: $ca vs $alpha
153 END_DIAG
154 return;
155 }
156
157 return 1;
158 }
159
160 sub is_fcolor4($$$$$$;$) {
161 my ($color, $red, $green, $blue, $alpha, $comment_or_diff, $comment_or_undef) = @_;
162 my ($comment, $mindiff);
163 if (defined $comment_or_undef) {
164 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
165 }
166 else {
167 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
168 }
169
170 my $builder = Test::Builder->new;
171
172 unless (defined $color) {
173 $builder->ok(0, $comment);
174 $builder->diag("color is undef");
175 return;
176 }
177 unless ($color->can('rgba')) {
178 $builder->ok(0, $comment);
179 $builder->diag("color is not a color object");
180 return;
181 }
182
183 my ($cr, $cg, $cb, $ca) = $color->rgba;
184 unless ($builder->ok(abs($cr - $red) <= $mindiff
185 && abs($cg - $green) <= $mindiff
186 && abs($cb - $blue) <= $mindiff
187 && abs($ca - $alpha) <= $mindiff, $comment)) {
188 $builder->diag(<<END_DIAG);
189 Color mismatch:
190 Red: $cr vs $red
191 Green: $cg vs $green
192 Blue: $cb vs $blue
193 Alpha: $ca vs $alpha
194 END_DIAG
195 return;
196 }
197
198 return 1;
199 }
200
201 sub is_fcolor1($$$;$) {
202 my ($color, $grey, $comment_or_diff, $comment_or_undef) = @_;
203 my ($comment, $mindiff);
204 if (defined $comment_or_undef) {
205 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
206 }
207 else {
208 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
209 }
210
211 my $builder = Test::Builder->new;
212
213 unless (defined $color) {
214 $builder->ok(0, $comment);
215 $builder->diag("color is undef");
216 return;
217 }
218 unless ($color->can('rgba')) {
219 $builder->ok(0, $comment);
220 $builder->diag("color is not a color object");
221 return;
222 }
223
224 my ($cgrey) = $color->rgba;
225 unless ($builder->ok(abs($cgrey - $grey) <= $mindiff, $comment)) {
226 print <<END_DIAG;
227 Color mismatch:
228 Gray: $cgrey vs $grey
229 END_DIAG
230 return;
231 }
232
233 return 1;
234 }
235
236 sub is_fcolor3($$$$$;$) {
237 my ($color, $red, $green, $blue, $comment_or_diff, $comment_or_undef) = @_;
238 my ($comment, $mindiff);
239 if (defined $comment_or_undef) {
240 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
241 }
242 else {
243 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
244 }
245
246 my $builder = Test::Builder->new;
247
248 unless (defined $color) {
249 $builder->ok(0, $comment);
250 $builder->diag("color is undef");
251 return;
252 }
253 unless ($color->can('rgba')) {
254 $builder->ok(0, $comment);
255 $builder->diag("color is not a color object");
256 return;
257 }
258
259 my ($cr, $cg, $cb) = $color->rgba;
260 unless ($builder->ok(abs($cr - $red) <= $mindiff
261 && abs($cg - $green) <= $mindiff
262 && abs($cb - $blue) <= $mindiff, $comment)) {
263 $builder->diag(<<END_DIAG);
264 Color mismatch:
265 Red: $cr vs $red
266 Green: $cg vs $green
267 Blue: $cb vs $blue
268 END_DIAG
269 return;
270 }
271
272 return 1;
273 }
274
275 sub is_color1($$$) {
276 my ($color, $grey, $comment) = @_;
277
278 my $builder = Test::Builder->new;
279
280 unless (defined $color) {
281 $builder->ok(0, $comment);
282 $builder->diag("color is undef");
283 return;
284 }
285 unless ($color->can('rgba')) {
286 $builder->ok(0, $comment);
287 $builder->diag("color is not a color object");
288 return;
289 }
290
291 my ($cgrey) = $color->rgba;
292 unless ($builder->ok($cgrey == $grey, $comment)) {
293 $builder->diag(<<END_DIAG);
294 Color mismatch:
295 Grey: $grey vs $cgrey
296 END_DIAG
297 return;
298 }
299
300 return 1;
301 }
302
303 sub test_image_raw {
304 my $green=Imager::i_color_new(0,255,0,255);
305 my $blue=Imager::i_color_new(0,0,255,255);
306 my $red=Imager::i_color_new(255,0,0,255);
307
308 my $img=Imager::ImgRaw::new(150,150,3);
309
310 Imager::i_box_filled($img,70,25,130,125,$green);
311 Imager::i_box_filled($img,20,25,80,125,$blue);
312 Imager::i_arc($img,75,75,30,0,361,$red);
313 Imager::i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
314
315 $img;
316 }
317
318 sub test_image {
319 my $green = Imager::Color->new(0, 255, 0, 255);
320 my $blue = Imager::Color->new(0, 0, 255, 255);
321 my $red = Imager::Color->new(255, 0, 0, 255);
322 my $img = Imager->new(xsize => 150, ysize => 150);
323 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
324 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
325 $img->arc(x => 75, y => 75, r => 30, color => $red);
326 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
327
328 $img;
329 }
330
331 sub test_image_16 {
332 my $green = Imager::Color->new(0, 255, 0, 255);
333 my $blue = Imager::Color->new(0, 0, 255, 255);
334 my $red = Imager::Color->new(255, 0, 0, 255);
335 my $img = Imager->new(xsize => 150, ysize => 150, bits => 16);
336 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
337 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
338 $img->arc(x => 75, y => 75, r => 30, color => $red);
339 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
340
341 $img;
342 }
343
344 sub test_image_double {
345 my $green = Imager::Color->new(0, 255, 0, 255);
346 my $blue = Imager::Color->new(0, 0, 255, 255);
347 my $red = Imager::Color->new(255, 0, 0, 255);
348 my $img = Imager->new(xsize => 150, ysize => 150, bits => 'double');
349 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
350 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
351 $img->arc(x => 75, y => 75, r => 30, color => $red);
352 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
353
354 $img;
355 }
356
357 sub test_image_gray {
358 my $g50 = Imager::Color->new(128, 128, 128);
359 my $g30 = Imager::Color->new(76, 76, 76);
360 my $g70 = Imager::Color->new(178, 178, 178);
361 my $img = Imager->new(xsize => 150, ysize => 150, channels => 1);
362 $img->box(filled => 1, color => $g50, box => [ 70, 24, 130, 124 ]);
363 $img->box(filled => 1, color => $g30, box => [ 20, 26, 80, 126 ]);
364 $img->arc(x => 75, y => 75, r => 30, color => $g70);
365 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
366
367 return $img;
368 }
369
370 sub test_image_gray_16 {
371 my $g50 = Imager::Color->new(128, 128, 128);
372 my $g30 = Imager::Color->new(76, 76, 76);
373 my $g70 = Imager::Color->new(178, 178, 178);
374 my $img = Imager->new(xsize => 150, ysize => 150, channels => 1, bits => 16);
375 $img->box(filled => 1, color => $g50, box => [ 70, 24, 130, 124 ]);
376 $img->box(filled => 1, color => $g30, box => [ 20, 26, 80, 126 ]);
377 $img->arc(x => 75, y => 75, r => 30, color => $g70);
378 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
379
380 return $img;
381 }
382
383 sub test_image_mono {
384 require Imager::Fill;
385 my $fh = Imager::Fill->new(hatch => 'check1x1');
386 my $img = Imager->new(xsize => 150, ysize => 150, type => "paletted");
387 my $black = Imager::Color->new(0, 0, 0);
388 my $white = Imager::Color->new(255, 255, 255);
389 $img->addcolors(colors => [ $black, $white ]);
390 $img->box(fill => $fh, box => [ 70, 24, 130, 124 ]);
391 $img->box(filled => 1, color => $white, box => [ 20, 26, 80, 126 ]);
392 $img->arc(x => 75, y => 75, r => 30, color => $black, aa => 0);
393
394 return $img;
395 }
396
397 my %name_to_sub =
398 (
399 basic => \&test_image,
400 basic16 => \&test_image_16,
401 basic_double => \&test_image_double,
402 gray => \&test_image_gray,
403 gray16 => \&test_image_gray_16,
404 mono => \&test_image_mono,
405 );
406
407 sub test_image_named {
408 my $name = shift
409 or croak("No name supplied to test_image_named()");
410 my $sub = $name_to_sub{$name}
411 or croak("Unknown name $name supplied to test_image_named()");
412
413 return $sub->();
414 }
415
416 sub _low_image_diff_check {
417 my ($left, $right, $comment) = @_;
418
419 my $builder = Test::Builder->new;
420
421 unless (defined $left) {
422 $builder->ok(0, $comment);
423 $builder->diag("left is undef");
424 return;
425 }
426 unless (defined $right) {
427 $builder->ok(0, $comment);
428 $builder->diag("right is undef");
429 return;
430 }
431 unless ($left->{IMG}) {
432 $builder->ok(0, $comment);
433 $builder->diag("left image has no low level object");
434 return;
435 }
436 unless ($right->{IMG}) {
437 $builder->ok(0, $comment);
438 $builder->diag("right image has no low level object");
439 return;
440 }
441 unless ($left->getwidth == $right->getwidth) {
442 $builder->ok(0, $comment);
443 $builder->diag("left width " . $left->getwidth . " vs right width "
444 . $right->getwidth);
445 return;
446 }
447 unless ($left->getheight == $right->getheight) {
448 $builder->ok(0, $comment);
449 $builder->diag("left height " . $left->getheight . " vs right height "
450 . $right->getheight);
451 return;
452 }
453 unless ($left->getchannels == $right->getchannels) {
454 $builder->ok(0, $comment);
455 $builder->diag("left channels " . $left->getchannels . " vs right channels "
456 . $right->getchannels);
457 return;
458 }
459
460 return 1;
461 }
462
463 sub is_image_similar($$$$) {
464 my ($left, $right, $limit, $comment) = @_;
465
466 {
467 local $Test::Builder::Level = $Test::Builder::Level + 1;
468
469 _low_image_diff_check($left, $right, $comment)
470 or return;
471 }
472
473 my $builder = Test::Builder->new;
474
475 my $diff = Imager::i_img_diff($left->{IMG}, $right->{IMG});
476 if ($diff > $limit) {
477 $builder->ok(0, $comment);
478 $builder->diag("image data difference > $limit - $diff");
479
480 if ($limit == 0) {
481 # find the first mismatch
482 PIXELS:
483 for my $y (0 .. $left->getheight()-1) {
484 for my $x (0.. $left->getwidth()-1) {
485 my @lsamples = $left->getsamples(x => $x, y => $y, width => 1);
486 my @rsamples = $right->getsamples(x => $x, y => $y, width => 1);
487 if ("@lsamples" ne "@rsamples") {
488 $builder->diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
489 last PIXELS;
490 }
491 }
492 }
493 }
494
495 return;
496 }
497
498 return $builder->ok(1, $comment);
499 }
500
501 sub is_image($$$) {
502 my ($left, $right, $comment) = @_;
503
504 local $Test::Builder::Level = $Test::Builder::Level + 1;
505
506 return is_image_similar($left, $right, 0, $comment);
507 }
508
509 sub is_imaged($$$;$) {
510 my $epsilon = Imager::i_img_epsilonf();
511 if (@_ > 3) {
512 ($epsilon) = splice @_, 2, 1;
513 }
514
515 my ($left, $right, $comment) = @_;
516
517 {
518 local $Test::Builder::Level = $Test::Builder::Level + 1;
519
520 _low_image_diff_check($left, $right, $comment)
521 or return;
522 }
523
524 my $builder = Test::Builder->new;
525
526 my $same = Imager::i_img_samef($left->{IMG}, $right->{IMG}, $epsilon, $comment);
527 if (!$same) {
528 $builder->ok(0, $comment);
529 $builder->diag("images different");
530
531 # find the first mismatch
532 PIXELS:
533 for my $y (0 .. $left->getheight()-1) {
534 for my $x (0.. $left->getwidth()-1) {
535 my @lsamples = $left->getsamples(x => $x, y => $y, width => 1, type => "float");
536 my @rsamples = $right->getsamples(x => $x, y => $y, width => 1, type => "float");
537 if ("@lsamples" ne "@rsamples") {
538 $builder->diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
539 last PIXELS;
540 }
541 }
542 }
543
544 return;
545 }
546
547 return $builder->ok(1, $comment);
548 }
549
550 sub isnt_image {
551 my ($left, $right, $comment) = @_;
552
553 my $builder = Test::Builder->new;
554
555 my $diff = Imager::i_img_diff($left->{IMG}, $right->{IMG});
556
557 return $builder->ok($diff, "$comment");
558 }
559
560 sub image_bounds_checks {
561 my $im = shift;
562
563 my $builder = Test::Builder->new;
564
565 $builder->ok(!$im->getpixel(x => -1, y => 0), 'bounds check get (-1, 0)');
566 $builder->ok(!$im->getpixel(x => 10, y => 0), 'bounds check get (10, 0)');
567 $builder->ok(!$im->getpixel(x => 0, y => -1), 'bounds check get (0, -1)');
568 $builder->ok(!$im->getpixel(x => 0, y => 10), 'bounds check get (0, 10)');
569 $builder->ok(!$im->getpixel(x => -1, y => 0), 'bounds check get (-1, 0) float');
570 $builder->ok(!$im->getpixel(x => 10, y => 0), 'bounds check get (10, 0) float');
571 $builder->ok(!$im->getpixel(x => 0, y => -1), 'bounds check get (0, -1) float');
572 $builder->ok(!$im->getpixel(x => 0, y => 10), 'bounds check get (0, 10) float');
573 my $black = Imager::Color->new(0, 0, 0);
574 require Imager::Color::Float;
575 my $blackf = Imager::Color::Float->new(0, 0, 0);
576 $builder->ok($im->setpixel(x => -1, y => 0, color => $black) == 0,
577 'bounds check set (-1, 0)');
578 $builder->ok($im->setpixel(x => 10, y => 0, color => $black) == 0,
579 'bounds check set (10, 0)');
580 $builder->ok($im->setpixel(x => 0, y => -1, color => $black) == 0,
581 'bounds check set (0, -1)');
582 $builder->ok($im->setpixel(x => 0, y => 10, color => $black) == 0,
583 'bounds check set (0, 10)');
584 $builder->ok($im->setpixel(x => -1, y => 0, color => $blackf) == 0,
585 'bounds check set (-1, 0) float');
586 $builder->ok($im->setpixel(x => 10, y => 0, color => $blackf) == 0,
587 'bounds check set (10, 0) float');
588 $builder->ok($im->setpixel(x => 0, y => -1, color => $blackf) == 0,
589 'bounds check set (0, -1) float');
590 $builder->ok($im->setpixel(x => 0, y => 10, color => $blackf) == 0,
591 'bounds check set (0, 10) float');
592 }
593
594 sub test_colorf_gpix {
595 my ($im, $x, $y, $expected, $epsilon, $comment) = @_;
596
597 my $builder = Test::Builder->new;
598
599 defined $comment or $comment = '';
600
601 my $c = Imager::i_gpixf($im, $x, $y);
602 unless ($c) {
603 $builder->ok(0, "$comment - retrieve color at ($x,$y)");
604 return;
605 }
606 unless ($builder->ok(colorf_cmp($c, $expected, $epsilon) == 0,
607 "$comment - got right color ($x, $y)")) {
608 my @c = $c->rgba;
609 my @exp = $expected->rgba;
610 $builder->diag(<<EOS);
611 # got: ($c[0], $c[1], $c[2])
612 # expected: ($exp[0], $exp[1], $exp[2])
613 EOS
614 }
615 1;
616 }
617
618 sub test_color_gpix {
619 my ($im, $x, $y, $expected, $comment) = @_;
620
621 my $builder = Test::Builder->new;
622
623 defined $comment or $comment = '';
624 my $c = Imager::i_get_pixel($im, $x, $y);
625 unless ($c) {
626 $builder->ok(0, "$comment - retrieve color at ($x,$y)");
627 return;
628 }
629 unless ($builder->ok(color_cmp($c, $expected) == 0,
630 "got right color ($x, $y)")) {
631 my @c = $c->rgba;
632 my @exp = $expected->rgba;
633 $builder->diag(<<EOS);
634 # got: ($c[0], $c[1], $c[2])
635 # expected: ($exp[0], $exp[1], $exp[2])
636 EOS
637 return;
638 }
639
640 return 1;
641 }
642
643 sub test_colorf_glin {
644 my ($im, $x, $y, $pels, $comment) = @_;
645
646 my $builder = Test::Builder->new;
647
648 my @got = Imager::i_glinf($im, $x, $x+@$pels, $y);
649 @got == @$pels
650 or return $builder->is_num(scalar(@got), scalar(@$pels), "$comment - pixels retrieved");
651
652 return $builder->ok(!grep(colorf_cmp($pels->[$_], $got[$_], 0.005), 0..$#got),
653 "$comment - check colors ($x, $y)");
654 }
655
656 sub colorf_cmp {
657 my ($c1, $c2, $epsilon) = @_;
658
659 defined $epsilon or $epsilon = 0;
660
661 my @s1 = $c1->rgba;
662 my @s2 = $c2->rgba;
663
664 # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
665 return abs($s1[0]-$s2[0]) >= $epsilon && $s1[0] <=> $s2[0]
666 || abs($s1[1]-$s2[1]) >= $epsilon && $s1[1] <=> $s2[1]
667 || abs($s1[2]-$s2[2]) >= $epsilon && $s1[2] <=> $s2[2];
668 }
669
670 sub color_cmp {
671 my ($c1, $c2) = @_;
672
673 my @s1 = $c1->rgba;
674 my @s2 = $c2->rgba;
675
676 return $s1[0] <=> $s2[0]
677 || $s1[1] <=> $s2[1]
678 || $s1[2] <=> $s2[2];
679 }
680
681 # these test the action of the channel mask on the image supplied
682 # which should be an OO image.
683 sub mask_tests {
684 my ($im, $epsilon) = @_;
685
686 no if $] >= 5.014, warnings => 'Imager::channelmask';
687 my $builder = Test::Builder->new;
688
689 defined $epsilon or $epsilon = 0;
690
691 # we want to check all four of ppix() and plin(), ppix() and plinf()
692 # basic test procedure:
693 # first using default/all 1s mask, set to white
694 # make sure we got white
695 # set mask to skip a channel, set to grey
696 # make sure only the right channels set
697
698 print "# channel mask tests\n";
699 # 8-bit color tests
700 my $white = Imager::NC(255, 255, 255);
701 my $grey = Imager::NC(128, 128, 128);
702 my $white_grey = Imager::NC(128, 255, 128);
703
704 print "# with ppix\n";
705 $builder->ok($im->setmask(mask=>~0), "set to default mask");
706 $builder->ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
707 test_color_gpix($im->{IMG}, 0, 0, $white, "ppix");
708 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
709 $builder->ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
710 test_color_gpix($im->{IMG}, 0, 0, $white_grey, "ppix masked");
711
712 print "# with plin\n";
713 $builder->ok($im->setmask(mask=>~0), "set to default mask");
714 $builder->ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]),
715 "set to white all channels");
716 test_color_gpix($im->{IMG}, 0, 1, $white, "plin");
717 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
718 $builder->ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]),
719 "set to grey, no channel 2");
720 test_color_gpix($im->{IMG}, 0, 1, $white_grey, "plin masked");
721
722 # float color tests
723 my $whitef = Imager::NCF(1.0, 1.0, 1.0);
724 my $greyf = Imager::NCF(0.5, 0.5, 0.5);
725 my $white_greyf = Imager::NCF(0.5, 1.0, 0.5);
726
727 print "# with ppixf\n";
728 $builder->ok($im->setmask(mask=>~0), "set to default mask");
729 $builder->ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
730 test_colorf_gpix($im->{IMG}, 0, 2, $whitef, $epsilon, "ppixf");
731 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
732 $builder->ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
733 test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf, $epsilon, "ppixf masked");
734
735 print "# with plinf\n";
736 $builder->ok($im->setmask(mask=>~0), "set to default mask");
737 $builder->ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]),
738 "set to white all channels");
739 test_colorf_gpix($im->{IMG}, 0, 3, $whitef, $epsilon, "plinf");
740 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
741 $builder->ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]),
742 "set to grey, no channel 2");
743 test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf, $epsilon, "plinf masked");
744
745 }
746
747 sub std_font_test_count {
748 return 21;
749 }
750
751 sub std_font_tests {
752 my ($opts) = @_;
753
754 my $font = $opts->{font}
755 or carp "Missing font parameter";
756
757 my $name_font = $opts->{glyph_name_font} || $font;
758
759 my $has_chars = $opts->{has_chars} || [ 1, '', 1 ];
760
761 my $glyph_names = $opts->{glyph_names} || [ "A", undef, "A" ];
762
763 SKIP:
764 { # check magic is handled correctly
765 # https://rt.cpan.org/Ticket/Display.html?id=83438
766 skip("no native UTF8 support in this version of perl", 11)
767 unless $] >= 5.006;
768 skip("overloading handling of magic is broken in this version of perl", 11)
769 unless $] >= 5.008;
770 Imager->log("utf8 magic tests\n");
771 my $over = bless {}, "Imager::Test::OverUtf8";
772 my $text = "A".chr(0x2010)."A";
773 my $white = Imager::Color->new("#FFF");
774 my $base_draw = Imager->new(xsize => 80, ysize => 20);
775 ok($base_draw->string(font => $font,
776 text => $text,
777 x => 2,
778 y => 18,
779 size => 15,
780 color => $white,
781 aa => 1),
782 "magic: make a base image");
783 my $test_draw = Imager->new(xsize => 80, ysize => 20);
784 ok($test_draw->string(font => $font,
785 text => $over,
786 x => 2,
787 y => 18,
788 size => 15,
789 color => $white,
790 aa => 1),
791 "magic: draw with overload");
792 is_image($base_draw, $test_draw, "check they match");
793 if ($opts->{files}) {
794 $test_draw->write(file => "testout/utf8tdr.ppm");
795 $base_draw->write(file => "testout/utf8bdr.ppm");
796 }
797
798 my $base_cp = Imager->new(xsize => 80, ysize => 20);
799 $base_cp->box(filled => 1, color => "#808080");
800 my $test_cp = $base_cp->copy;
801 ok($base_cp->string(font => $font,
802 text => $text,
803 y => 2,
804 y => 18,
805 size => 16,
806 channel => 2,
807 aa => 1),
808 "magic: make a base image (channel)");
809 Imager->log("magic: draw to channel with overload\n");
810 ok($test_cp->string(font => $font,
811 text => $over,
812 y => 2,
813 y => 18,
814 size => 16,
815 channel => 2,
816 aa => 1),
817 "magic: draw with overload (channel)");
818 is_image($test_cp, $base_cp, "check they match");
819 if ($opts->{files}) {
820 $test_cp->write(file => "testout/utf8tcp.ppm");
821 $base_cp->write(file => "testout/utf8bcp.ppm");
822 }
823
824 SKIP:
825 {
826 Imager->log("magic: has_chars\n");
827 $font->can("has_chars")
828 or skip "No has_chars aupport", 2;
829 is_deeply([ $font->has_chars(string => $text) ], $has_chars,
830 "magic: has_chars with normal utf8 text");
831 is_deeply([ $font->has_chars(string => $over) ], $has_chars,
832 "magic: has_chars with magic utf8 text");
833 }
834
835 Imager->log("magic: bounding_box\n");
836 my @base_bb = $font->bounding_box(string => $text, size => 30);
837 is_deeply([ $font->bounding_box(string => $over, size => 30) ],
838 \@base_bb,
839 "check bounding box magic");
840
841 SKIP:
842 {
843 $font->can_glyph_names
844 or skip "No glyph_names", 2;
845 Imager->log("magic: glyph_names\n");
846 my @text_names = $name_font->glyph_names(string => $text, reliable_only => 0);
847 is_deeply(\@text_names, $glyph_names,
848 "magic: glyph_names with normal utf8 text");
849 my @over_names = $name_font->glyph_names(string => $over, reliable_only => 0);
850 is_deeply(\@over_names, $glyph_names,
851 "magic: glyph_names with magic utf8 text");
852 }
853 }
854
855 { # invalid UTF8 handling at the OO level
856 my $im = Imager->new(xsize => 80, ysize => 20);
857 my $bad_utf8 = pack("C", 0xC0);
858 Imager->_set_error("");
859 ok(!$im->string(font => $font, size => 1, text => $bad_utf8, utf8 => 1,
860 y => 18, x => 2),
861 "drawing invalid utf8 should fail");
862 is($im->errstr, "invalid UTF8 character", "check error message");
863 Imager->_set_error("");
864 ok(!$im->string(font => $font, size => 1, text => $bad_utf8, utf8 => 1,
865 y => 18, x => 2, channel => 1),
866 "drawing invalid utf8 should fail (channel)");
867 is($im->errstr, "invalid UTF8 character", "check error message");
868 Imager->_set_error("");
869 ok(!$font->bounding_box(string => $bad_utf8, size => 30, utf8 => 1),
870 "bounding_box() bad utf8 should fail");
871 is(Imager->errstr, "invalid UTF8 character", "check error message");
872 SKIP:
873 {
874 $font->can_glyph_names
875 or skip "No glyph_names support", 2;
876 Imager->_set_error("");
877 is_deeply([ $font->glyph_names(string => $bad_utf8, utf8 => 1) ],
878 [ ],
879 "glyph_names returns empty list for bad string");
880 is(Imager->errstr, "invalid UTF8 character", "check error message");
881 }
882 SKIP:
883 {
884 $font->can("has_chars")
885 or skip "No has_chars support", 2;
886 Imager->_set_error("");
887 is_deeply([ $font->has_chars(string => $bad_utf8, utf8 => 1) ],
888 [ ],
889 "has_chars returns empty list for bad string");
890 is(Imager->errstr, "invalid UTF8 character", "check error message");
891 }
892 }
893 }
894
895 package Imager::Test::OverUtf8;
896 use overload '""' => sub { "A".chr(0x2010)."A" };
897
898
899 1;
900
901 __END__
902
903 =head1 NAME
904
905 Imager::Test - common functions used in testing Imager
906
907 =head1 SYNOPSIS
908
909 use Imager::Test 'diff_text_with_nul';
910 diff_text_with_nul($test_name, $text1, $text2, @string_options);
911
912 =head1 DESCRIPTION
913
914 This is a repository of functions used in testing Imager.
915
916 Some functions will only be useful in testing Imager itself, while
917 others should be useful in testing modules that use Imager.
918
919 No functions are exported by default.
920
921 =head1 FUNCTIONS
922
923 =head2 Test functions
924
925 =for stopwords OO
926
927 =over
928
929 =item is_color1($color, $grey, $comment)
930
931 Tests if the first channel of $color matches $grey.
932
933 =item is_color3($color, $red, $green, $blue, $comment)
934
935 Tests if $color matches the given ($red, $green, $blue)
936
937 =item is_color4($color, $red, $green, $blue, $alpha, $comment)
938
939 Tests if $color matches the given ($red, $green, $blue, $alpha)
940
941 =item is_fcolor1($fcolor, $grey, $comment)
942
943 =item is_fcolor1($fcolor, $grey, $epsilon, $comment)
944
945 Tests if $fcolor's first channel is within $epsilon of ($grey). For
946 the first form $epsilon is taken as 0.001.
947
948 =item is_fcolor3($fcolor, $red, $green, $blue, $comment)
949
950 =item is_fcolor3($fcolor, $red, $green, $blue, $epsilon, $comment)
951
952 Tests if $fcolor's channels are within $epsilon of ($red, $green,
953 $blue). For the first form $epsilon is taken as 0.001.
954
955 =item is_fcolor4($fcolor, $red, $green, $blue, $alpha, $comment)
956
957 =item is_fcolor4($fcolor, $red, $green, $blue, $alpha, $epsilon, $comment)
958
959 Tests if $fcolor's channels are within $epsilon of ($red, $green,
960 $blue, $alpha). For the first form $epsilon is taken as 0.001.
961
962 =item is_image($im1, $im2, $comment)
963
964 Tests if the 2 images have the same content. Both images must be
965 defined, have the same width, height, channels and the same color in
966 each pixel. The color comparison is done at 8-bits per pixel. The
967 color representation such as direct vs paletted, bits per sample are
968 not checked. Equivalent to is_image_similar($im1, $im2, 0, $comment).
969
970 =item is_imaged($im, $im2, $comment)
971
972 =item is_imaged($im, $im2, $epsilon, $comment)
973
974 Tests if the two images have the same content at the double/sample
975 level. C<$epsilon> defaults to the platform DBL_EPSILON multiplied by
976 four.
977
978 =item is_image_similar($im1, $im2, $maxdiff, $comment)
979
980 Tests if the 2 images have similar content. Both images must be
981 defined, have the same width, height and channels. The sum of the
982 squares of the differences of each sample are calculated and must be
983 less than or equal to I<$maxdiff> for the test to pass. The color
984 comparison is done at 8-bits per pixel. The color representation such
985 as direct vs paletted, bits per sample are not checked.
986
987 =item isnt_image($im1, $im2, $comment)
988
989 Tests that the two images are different. For regressions tests where
990 something (like text output of "0") produced no change, but should
991 have produced a change.
992
993 =item test_colorf_gpix($im, $x, $y, $expected, $epsilon, $comment)
994
995 Retrieves the pixel ($x,$y) from the low-level image $im and compares
996 it to the floating point color $expected, with a tolerance of epsilon.
997
998 =item test_color_gpix($im, $x, $y, $expected, $comment)
999
1000 Retrieves the pixel ($x,$y) from the low-level image $im and compares
1001 it to the floating point color $expected.
1002
1003 =item test_colorf_glin($im, $x, $y, $pels, $comment)
1004
1005 Retrieves the floating point pixels ($x, $y)-[$x+@$pels, $y] from the
1006 low level image $im and compares them against @$pels.
1007
1008 =item is_color_close3($color, $red, $green, $blue, $tolerance, $comment)
1009
1010 Tests if $color's first three channels are within $tolerance of ($red,
1011 $green, $blue).
1012
1013 =back
1014
1015 =head2 Test suite functions
1016
1017 Functions that perform one or more tests, typically used to test
1018 various parts of Imager's implementation.
1019
1020 =over
1021
1022 =item image_bounds_checks($im)
1023
1024 Attempts to write to various pixel positions outside the edge of the
1025 image to ensure that it fails in those locations.
1026
1027 Any new image type should pass these tests. Does 16 separate tests.
1028
1029 =item mask_tests($im, $epsilon)
1030
1031 Perform a standard set of mask tests on the OO image $im. Does 24
1032 separate tests.
1033
1034 =item diff_text_with_nul($test_name, $text1, $text2, @options)
1035
1036 Creates 2 test images and writes $text1 to the first image and $text2
1037 to the second image with the string() method. Each call adds 3
1038 C<ok>/C<not ok> to the output of the test script.
1039
1040 Extra options that should be supplied include the font and either a
1041 color or channel parameter.
1042
1043 This was explicitly created for regression tests on #21770.
1044
1045 =item std_font_tests({ font => $font })
1046
1047 Perform standard font interface tests.
1048
1049 =item std_font_test_count()
1050
1051 The number of tests performed by std_font_tests().
1052
1053 =back
1054
1055 =head2 Helper functions
1056
1057 =over
1058
1059 =item test_image_raw()
1060
1061 Returns a 150x150x3 Imager::ImgRaw test image.
1062
1063 =item test_image()
1064
1065 Returns a 150x150x3 8-bit/sample OO test image. Name: C<basic>.
1066
1067 =item test_image_16()
1068
1069 Returns a 150x150x3 16-bit/sample OO test image. Name: C<basic16>
1070
1071 =item test_image_double()
1072
1073 Returns a 150x150x3 double/sample OO test image. Name: C<basic_double>.
1074
1075 =item test_image_gray()
1076
1077 Returns a 150x150 single channel OO test image. Name: C<gray>.
1078
1079 =item test_image_gray_16()
1080
1081 Returns a 150x150 16-bit/sample single channel OO test image. Name:
1082 C<gray16>.
1083
1084 =item test_image_mono()
1085
1086 Returns a 150x150 bilevel image that passes the is_bilevel() test.
1087 Name: C<mono>.
1088
1089 =item test_image_named($name)
1090
1091 Return one of the other test images above based on name.
1092
1093 =item color_cmp($c1, $c2)
1094
1095 Performs an ordering of 3-channel colors (like <=>).
1096
1097 =item colorf_cmp($c1, $c2)
1098
1099 Performs an ordering of 3-channel floating point colors (like <=>).
1100
1101 =back
1102
1103 =head1 AUTHOR
1104
1105 Tony Cook <[email protected]>
1106
1107 =cut