blib/lib/Imager/Test.pm

filter: Statementstmt 63.0% 335 / 531 Branchbran 43.6% 55 / 126 Conditioncond 31.8% 21 / 66 MC/DCmcdc 0.0% 0 / 31 Subroutinesub 87.8% 36 / 41 Podpod 100% 30 / 30 totaltotal 57.8% 477 / 825 CC 94 SCAR 33.3
CC
94
Cov
57%
std_font_tests
182.0
is_color_close3
42.0
_low_image_diff_check
23.4
CRAP 27.8
« blib/lib/Imager.pm ↑ Summary blib/lib/Imager/Font/Image.pm »
1 package Imager::Test;
2 46 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 46 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 46 ▶use Imager;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 46 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 46 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 46 require Exporter;
8 46 ▶use Carp qw(croak carp);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 46 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 46 our $VERSION = "1.008";
12  
13 46 our @ISA = qw(Exporter);
14 46 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 std_image_tests
46 );
47  
48 0 ▶sub diff_text_with_nul {
Subroutine diff_text_with_nul: not called CC=1 SCAR=6.9
Pod documented
49 0 my ($desc, $text1, $text2, @params) = @_;
50  
51 0 my $builder = Test::Builder->new;
52  
53 0 print "# $desc\n";
54 0 my $imbase = Imager->new(xsize => 100, ysize => 100);
55 0 my $imcopy = Imager->new(xsize => 100, ysize => 100);
56  
57 0 $builder->ok($imbase->string(x => 5, 'y' => 50, size => 20,
58 string => $text1,
59 @params), "$desc - draw text1");
60 0 $builder->ok($imcopy->string(x => 5, 'y' => 50, size => 20,
61 string => $text2,
62 @params), "$desc - draw text2");
63 0 $builder->isnt_num(Imager::i_img_diff($imbase->{IMG}, $imcopy->{IMG}), 0,
64 "$desc - check result different");
65 }
66  
67 210 ▶sub is_color3($$$$$) {
Subroutine is_color3: called CC=6 SCAR=25.0
Pod documented
68 210 my ($color, $red, $green, $blue, $comment) = @_;
69  
70 210 my $builder = Test::Builder->new;
71  
72 210 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 210 210
73 0 $builder->ok(0, $comment);
74 0 $builder->diag("color is undef");
75 0 return;
76 }
77 210 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 210 210
78 0 $builder->ok(0, $comment);
79 0 $builder->diag("color is not a color object");
80 0 return;
81 }
82  
83 210 my ($cr, $cg, $cb) = $color->rgba;
84 210 ▶ unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue, $comment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue, $comment)) 0 210 210
Condition33%2/6
$cr == $red && $cg == $green && $cb == $blue
!ll&&!rl&&r
00210
$cr == $red and $cg == $green
!ll&&!rl&&r
00210
MC/DC0%0/3
$cr == $red && $cg == $green && $cb == $blue
$cr == $red $cg == $green $cb == $blue
Truth table25%1/4
A and B && C
A= $cr == $redB= $cg == $greenC= $cb == $blue
ABCresult
0XX0
10X0
1100
1111
85 0 print <<END_DIAG;
86 Color mismatch:
87 Red: $red vs $cr
88 Green: $green vs $cg
89 Blue: $blue vs $cb
90 END_DIAG
91 0 return;
92 }
93  
94 210 return 1;
95 }
96  
97 0 ▶sub is_color_close3($$$$$$) {
Subroutine is_color_close3: not called CC=6 SCAR=37.4
Pod documented
98 0 my ($color, $red, $green, $blue, $tolerance, $comment) = @_;
99  
100 0 my $builder = Test::Builder->new;
101  
102 0 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 0 0
103 0 $builder->ok(0, $comment);
104 0 $builder->diag("color is undef");
105 0 return;
106 }
107 0 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 0 0
108 0 $builder->ok(0, $comment);
109 0 $builder->diag("color is not a color object");
110 0 return;
111 }
112  
113 0 my ($cr, $cg, $cb) = $color->rgba;
114 0 ▶ unless ($builder->ok(abs($cr - $red) <= $tolerance
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(abs $cr - $red <= $tolerance && abs $cg - $green <= $tolerance && abs $cb - $blue <= $tolerance, $comment)) 0 0 0
Condition0%0/6
abs $cr - $red <= $tolerance && abs $cg - $green <= $tolerance && abs $cb - $blue <= $tolerance
!ll&&!rl&&r
000
abs $cr - $red <= $tolerance and abs $cg - $green <= $tolerance
!ll&&!rl&&r
000
MC/DC0%0/3
abs $cr - $red <= $tolerance && abs $cg - $green <= $tolerance && abs $cb - $blue <= $tolerance
abs $cr - $red <= $tolerance abs $cg - $green <= $tolerance abs $cb - $blue <= $tolerance
Truth table0%0/4
A and B && C
A= abs $cr - $red <= $toleranceB= abs $cg - $green <= $toleranceC= abs $cb - $blue <= $tolerance
ABCresult
0XX0
10X0
1100
1111
115 && abs($cg - $green) <= $tolerance
116 && abs($cb - $blue) <= $tolerance, $comment)) {
117 0 $builder->diag(<<END_DIAG);
118 Color out of tolerance ($tolerance):
119 Red: expected $red vs received $cr
120 Green: expected $green vs received $cg
121 Blue: expected $blue vs received $cb
122 END_DIAG
123 0 return;
124 }
125  
126 0 return 1;
127 }
128  
129 116 ▶sub is_color4($$$$$$) {
Subroutine is_color4: called CC=7 SCAR=27.7
Pod documented
130 116 my ($color, $red, $green, $blue, $alpha, $comment) = @_;
131  
132 116 my $builder = Test::Builder->new;
133  
134 116 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 116 116
135 0 $builder->ok(0, $comment);
136 0 $builder->diag("color is undef");
137 0 return;
138 }
139 116 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 116 116
140 0 $builder->ok(0, $comment);
141 0 $builder->diag("color is not a color object");
142 0 return;
143 }
144  
145 116 my ($cr, $cg, $cb, $ca) = $color->rgba;
146 116 ▶ unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok($cr == $red && $cg == $green && $cb == $blue && $ca == $alpha, $comment)) 0 116 116
Condition33%3/9
$cr == $red && $cg == $green && $cb == $blue && $ca == $alpha
!ll&&!rl&&r
00116
$cr == $red and $cg == $green and $cb == $blue
!ll&&!rl&&r
00116
$cr == $red and $cg == $green
!ll&&!rl&&r
00116
MC/DC0%0/4
$cr == $red && $cg == $green && $cb == $blue && $ca == $alpha
$cr == $red $cg == $green $cb == $blue $ca == $alpha
Truth table20%1/5
A and B and C && D
A= $cr == $redB= $cg == $greenC= $cb == $blueD= $ca == $alpha
ABCDresult
0XXX0
10XX0
110X0
11100
11111
147 && $ca == $alpha, $comment)) {
148 0 $builder->diag(<<END_DIAG);
149 Color mismatch:
150 Red: $cr vs $red
151 Green: $cg vs $green
152 Blue: $cb vs $blue
153 Alpha: $ca vs $alpha
154 END_DIAG
155 0 return;
156 }
157  
158 116 return 1;
159 }
160  
161 68 ▶sub is_fcolor4($$$$$$;$) {
Subroutine is_fcolor4: called CC=8 SCAR=26.9
Pod documented
162 68 my ($color, $red, $green, $blue, $alpha, $comment_or_diff, $comment_or_undef) = @_;
163 68 my ($comment, $mindiff);
164 68 ▶ if (defined $comment_or_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment_or_undef) { } 2 66 68
165 2 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
166 }
167 else {
168 66 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
169 }
170  
171 68 my $builder = Test::Builder->new;
172  
173 68 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 68 68
174 0 $builder->ok(0, $comment);
175 0 $builder->diag("color is undef");
176 0 return;
177 }
178 68 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 68 68
179 0 $builder->ok(0, $comment);
180 0 $builder->diag("color is not a color object");
181 0 return;
182 }
183  
184 68 my ($cr, $cg, $cb, $ca) = $color->rgba;
185 68 ▶ unless ($builder->ok(abs($cr - $red) <= $mindiff
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff && abs $ca - $alpha <= $mindiff, $comment)) 0 68 68
Condition33%3/9
abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff && abs $ca - $alpha <= $mindiff
!ll&&!rl&&r
0068
abs $cr - $red <= $mindiff and abs $cg - $green <= $mindiff and abs $cb - $blue <= $mindiff
!ll&&!rl&&r
0068
abs $cr - $red <= $mindiff and abs $cg - $green <= $mindiff
!ll&&!rl&&r
0068
MC/DC0%0/4
abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff && abs $ca - $alpha <= $mindiff
abs $cr - $red <= $mindiff abs $cg - $green <= $mindiff abs $cb - $blue <= $mindiff abs $ca - $alpha <= $mindiff
Truth table20%1/5
A and B and C && D
A= abs $cr - $red <= $mindiffB= abs $cg - $green <= $mindiffC= abs $cb - $blue <= $mindiffD= abs $ca - $alpha <= $mindiff
ABCDresult
0XXX0
10XX0
110X0
11100
11111
186 && abs($cg - $green) <= $mindiff
187 && abs($cb - $blue) <= $mindiff
188 && abs($ca - $alpha) <= $mindiff, $comment)) {
189 0 $builder->diag(<<END_DIAG);
190 Color mismatch:
191 Red: $cr vs $red
192 Green: $cg vs $green
193 Blue: $cb vs $blue
194 Alpha: $ca vs $alpha
195 END_DIAG
196 0 return;
197 }
198  
199 68 return 1;
200 }
201  
202 1 ▶sub is_fcolor1($$$;$) {
Subroutine is_fcolor1: called CC=5 SCAR=20.5
Pod documented
203 1 my ($color, $grey, $comment_or_diff, $comment_or_undef) = @_;
204 1 my ($comment, $mindiff);
205 1 ▶ if (defined $comment_or_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment_or_undef) { } 1 0 1
206 1 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
207 }
208 else {
209 0 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
210 }
211  
212 1 my $builder = Test::Builder->new;
213  
214 1 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 1 1
215 0 $builder->ok(0, $comment);
216 0 $builder->diag("color is undef");
217 0 return;
218 }
219 1 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 1 1
220 0 $builder->ok(0, $comment);
221 0 $builder->diag("color is not a color object");
222 0 return;
223 }
224  
225 1 my ($cgrey) = $color->rgba;
226 1 ▶ unless ($builder->ok(abs($cgrey - $grey) <= $mindiff, $comment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(abs $cgrey - $grey <= $mindiff, $comment)) 0 1 1
227 0 print <<END_DIAG;
228 Color mismatch:
229 Gray: $cgrey vs $grey
230 END_DIAG
231 0 return;
232 }
233  
234 1 return 1;
235 }
236  
237 25 ▶sub is_fcolor3($$$$$;$) {
Subroutine is_fcolor3: called CC=7 SCAR=24.5
Pod documented
238 25 my ($color, $red, $green, $blue, $comment_or_diff, $comment_or_undef) = @_;
239 25 my ($comment, $mindiff);
240 25 ▶ if (defined $comment_or_undef) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comment_or_undef) { } 9 16 25
241 9 ( $mindiff, $comment ) = ( $comment_or_diff, $comment_or_undef )
242 }
243 else {
244 16 ( $mindiff, $comment ) = ( 0.001, $comment_or_diff )
245 }
246  
247 25 my $builder = Test::Builder->new;
248  
249 25 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 25 25
250 0 $builder->ok(0, $comment);
251 0 $builder->diag("color is undef");
252 0 return;
253 }
254 25 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 25 25
255 0 $builder->ok(0, $comment);
256 0 $builder->diag("color is not a color object");
257 0 return;
258 }
259  
260 25 my ($cr, $cg, $cb) = $color->rgba;
261 25 ▶ unless ($builder->ok(abs($cr - $red) <= $mindiff
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff, $comment)) 0 25 25
Condition33%2/6
abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff
!ll&&!rl&&r
0025
abs $cr - $red <= $mindiff and abs $cg - $green <= $mindiff
!ll&&!rl&&r
0025
MC/DC0%0/3
abs $cr - $red <= $mindiff && abs $cg - $green <= $mindiff && abs $cb - $blue <= $mindiff
abs $cr - $red <= $mindiff abs $cg - $green <= $mindiff abs $cb - $blue <= $mindiff
Truth table25%1/4
A and B && C
A= abs $cr - $red <= $mindiffB= abs $cg - $green <= $mindiffC= abs $cb - $blue <= $mindiff
ABCresult
0XX0
10X0
1100
1111
262 && abs($cg - $green) <= $mindiff
263 && abs($cb - $blue) <= $mindiff, $comment)) {
264 0 $builder->diag(<<END_DIAG);
265 Color mismatch:
266 Red: $cr vs $red
267 Green: $cg vs $green
268 Blue: $cb vs $blue
269 END_DIAG
270 0 return;
271 }
272  
273 25 return 1;
274 }
275  
276 1 ▶sub is_color1($$$) {
Subroutine is_color1: called CC=4 SCAR=18.4
Pod documented
277 1 my ($color, $grey, $comment) = @_;
278  
279 1 my $builder = Test::Builder->new;
280  
281 1 ▶ unless (defined $color) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $color) 0 1 1
282 0 $builder->ok(0, $comment);
283 0 $builder->diag("color is undef");
284 0 return;
285 }
286 1 ▶ unless ($color->can('rgba')) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($color->can("rgba")) 0 1 1
287 0 $builder->ok(0, $comment);
288 0 $builder->diag("color is not a color object");
289 0 return;
290 }
291  
292 1 my ($cgrey) = $color->rgba;
293 1 ▶ unless ($builder->ok($cgrey == $grey, $comment)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok($cgrey == $grey, $comment)) 0 1 1
294 0 $builder->diag(<<END_DIAG);
295 Color mismatch:
296 Grey: $grey vs $cgrey
297 END_DIAG
298 0 return;
299 }
300  
301 1 return 1;
302 }
303  
304 2 ▶sub test_image_raw {
Subroutine test_image_raw: called CC=1 SCAR=0.0
Pod documented
305 2 my $green=Imager::i_color_new(0,255,0,255);
306 2 my $blue=Imager::i_color_new(0,0,255,255);
307 2 my $red=Imager::i_color_new(255,0,0,255);
308
309 2 my $img=Imager::ImgRaw::new(150,150,3);
310
311 2 Imager::i_box_filled($img,70,25,130,125,$green);
312 2 Imager::i_box_filled($img,20,25,80,125,$blue);
313 2 Imager::i_arc($img,75,75,30,0,361,$red);
314 2 Imager::i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
315  
316 2 $img;
317 }
318  
319 37 ▶sub test_image {
Subroutine test_image: called CC=1 SCAR=0.0
Pod documented
320 37 my $green = Imager::Color->new(0, 255, 0, 255);
321 37 my $blue = Imager::Color->new(0, 0, 255, 255);
322 37 my $red = Imager::Color->new(255, 0, 0, 255);
323 37 my $img = Imager->new(xsize => 150, ysize => 150);
324 37 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
325 37 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
326 37 $img->arc(x => 75, y => 75, r => 30, color => $red);
327 37 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
328  
329 37 $img;
330 }
331  
332 7 ▶sub test_image_16 {
Subroutine test_image_16: called CC=1 SCAR=0.0
Pod documented
333 7 my $green = Imager::Color->new(0, 255, 0, 255);
334 7 my $blue = Imager::Color->new(0, 0, 255, 255);
335 7 my $red = Imager::Color->new(255, 0, 0, 255);
336 7 my $img = Imager->new(xsize => 150, ysize => 150, bits => 16);
337 7 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
338 7 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
339 7 $img->arc(x => 75, y => 75, r => 30, color => $red);
340 7 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
341  
342 7 $img;
343 }
344  
345 4 ▶sub test_image_double {
Subroutine test_image_double: called CC=1 SCAR=0.0
Pod documented
346 4 my $green = Imager::Color->new(0, 255, 0, 255);
347 4 my $blue = Imager::Color->new(0, 0, 255, 255);
348 4 my $red = Imager::Color->new(255, 0, 0, 255);
349 4 my $img = Imager->new(xsize => 150, ysize => 150, bits => 'double');
350 4 $img->box(filled => 1, color => $green, box => [ 70, 24, 130, 124 ]);
351 4 $img->box(filled => 1, color => $blue, box => [ 20, 26, 80, 126 ]);
352 4 $img->arc(x => 75, y => 75, r => 30, color => $red);
353 4 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
354  
355 4 $img;
356 }
357  
358 3 ▶sub test_image_gray {
Subroutine test_image_gray: called CC=1 SCAR=0.0
Pod documented
359 3 my $g50 = Imager::Color->new(128, 128, 128);
360 3 my $g30 = Imager::Color->new(76, 76, 76);
361 3 my $g70 = Imager::Color->new(178, 178, 178);
362 3 my $img = Imager->new(xsize => 150, ysize => 150, channels => 1);
363 3 $img->box(filled => 1, color => $g50, box => [ 70, 24, 130, 124 ]);
364 3 $img->box(filled => 1, color => $g30, box => [ 20, 26, 80, 126 ]);
365 3 $img->arc(x => 75, y => 75, r => 30, color => $g70);
366 3 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
367  
368 3 return $img;
369 }
370  
371 4 ▶sub test_image_gray_16 {
Subroutine test_image_gray_16: called CC=1 SCAR=0.0
Pod documented
372 4 my $g50 = Imager::Color->new(128, 128, 128);
373 4 my $g30 = Imager::Color->new(76, 76, 76);
374 4 my $g70 = Imager::Color->new(178, 178, 178);
375 4 my $img = Imager->new(xsize => 150, ysize => 150, channels => 1, bits => 16);
376 4 $img->box(filled => 1, color => $g50, box => [ 70, 24, 130, 124 ]);
377 4 $img->box(filled => 1, color => $g30, box => [ 20, 26, 80, 126 ]);
378 4 $img->arc(x => 75, y => 75, r => 30, color => $g70);
379 4 $img->filter(type => 'conv', coef => [ 0.1, 0.2, 0.4, 0.2, 0.1 ]);
380  
381 4 return $img;
382 }
383  
384 4 ▶sub test_image_mono {
Subroutine test_image_mono: called CC=1 SCAR=0.0
Pod documented
385 4 require Imager::Fill;
386 4 my $fh = Imager::Fill->new(hatch => 'check1x1');
387 4 my $img = Imager->new(xsize => 150, ysize => 150, type => "paletted");
388 4 my $black = Imager::Color->new(0, 0, 0);
389 4 my $white = Imager::Color->new(255, 255, 255);
390 4 $img->addcolors(colors => [ $black, $white ]);
391 4 $img->box(fill => $fh, box => [ 70, 24, 130, 124 ]);
392 4 $img->box(filled => 1, color => $white, box => [ 20, 26, 80, 126 ]);
393 4 $img->arc(x => 75, y => 75, r => 30, color => $black, aa => 0);
394  
395 4 return $img;
396 }
397  
398 46 my %name_to_sub =
399 (
400 basic => \&test_image,
401 basic16 => \&test_image_16,
402 basic_double => \&test_image_double,
403 gray => \&test_image_gray,
404 gray16 => \&test_image_gray_16,
405 mono => \&test_image_mono,
406 );
407  
408 11 ▶sub test_image_named {
Subroutine test_image_named: called CC=3 SCAR=11.7
Pod documented
409 11 ▶ my $name = shift
Branch: 1 branch
BranchTrueFalseTotal
my $name = shift() or croak("No name supplied to test_image_named()") 11 0 11
410 or croak("No name supplied to test_image_named()");
411 11 ▶ my $sub = $name_to_sub{$name}
Branch: 1 branch
BranchTrueFalseTotal
my $sub = $name_to_sub{$name} or croak("Unknown name $name supplied to test_image_named()") 11 0 11
412 or croak("Unknown name $name supplied to test_image_named()");
413  
414 11 return $sub->();
415 }
416  
417 275 ▶sub _low_image_diff_check {
Subroutine _low_image_diff_check: called CC=8 SCAR=31.5
418 275 my ($left, $right, $comment) = @_;
419  
420 275 my $builder = Test::Builder->new;
421  
422 275 ▶ unless (defined $left) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $left) 0 275 275
423 0 $builder->ok(0, $comment);
424 0 $builder->diag("left is undef");
425 0 return;
426 }
427 275 ▶ unless (defined $right) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $right) 0 275 275
428 0 $builder->ok(0, $comment);
429 0 $builder->diag("right is undef");
430 0 return;
431 }
432 275 ▶ unless ($left->{IMG}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($left->{"IMG"}) 0 275 275
433 0 $builder->ok(0, $comment);
434 0 $builder->diag("left image has no low level object");
435 0 return;
436 }
437 275 ▶ unless ($right->{IMG}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($right->{"IMG"}) 0 275 275
438 0 $builder->ok(0, $comment);
439 0 $builder->diag("right image has no low level object");
440 0 return;
441 }
442 275 ▶ unless ($left->getwidth == $right->getwidth) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($left->getwidth == $right->getwidth) 0 275 275
443 0 $builder->ok(0, $comment);
444 0 $builder->diag("left width " . $left->getwidth . " vs right width "
445 . $right->getwidth);
446 0 return;
447 }
448 275 ▶ unless ($left->getheight == $right->getheight) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($left->getheight == $right->getheight) 0 275 275
449 0 $builder->ok(0, $comment);
450 0 $builder->diag("left height " . $left->getheight . " vs right height "
451 . $right->getheight);
452 0 return;
453 }
454 275 ▶ unless ($left->getchannels == $right->getchannels) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($left->getchannels == $right->getchannels) 0 275 275
455 0 $builder->ok(0, $comment);
456 0 $builder->diag("left channels " . $left->getchannels . " vs right channels "
457 . $right->getchannels);
458 0 return;
459 }
460  
461 275 return 1;
462 }
463  
464 246 ▶sub is_image_similar($$$$) {
Subroutine is_image_similar: called CC=7 SCAR=29.6
Pod documented
465 246 my ($left, $right, $limit, $comment) = @_;
466  
467 {
468 246 local $Test::Builder::Level = $Test::Builder::Level + 1;
469  
470 246 ▶ _low_image_diff_check($left, $right, $comment)
Branch: 1 branch
BranchTrueFalseTotal
_low_image_diff_check($left, $right, $comment) or return 246 0 246
471 or return;
472 }
473  
474 246 my $builder = Test::Builder->new;
475  
476 246 my $diff = Imager::i_img_diff($left->{IMG}, $right->{IMG});
477 246 ▶ if ($diff > $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($diff > $limit) 0 246 246
478 0 $builder->ok(0, $comment);
479 0 $builder->diag("image data difference > $limit - $diff");
480
481 0 ▶ if ($limit == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($limit == 0) 0 0 0
482 # find the first mismatch
483 PIXELS:
484 0 for my $y (0 .. $left->getheight()-1) {
485 0 for my $x (0.. $left->getwidth()-1) {
486 0 my @lsamples = $left->getsamples(x => $x, y => $y, width => 1);
487 0 my @rsamples = $right->getsamples(x => $x, y => $y, width => 1);
488 0 ▶ if ("@lsamples" ne "@rsamples") {
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @lsamples) ne join($", @rsamples)) 0 0 0
489 0 $builder->diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
490 0 last PIXELS;
491 }
492 }
493 }
494 }
495  
496 0 return;
497 }
498
499 246 return $builder->ok(1, $comment);
500 }
501  
502 241 ▶sub is_image($$$) {
Subroutine is_image: called CC=1 SCAR=0.0
Pod documented
503 241 my ($left, $right, $comment) = @_;
504  
505 241 local $Test::Builder::Level = $Test::Builder::Level + 1;
506  
507 241 return is_image_similar($left, $right, 0, $comment);
508 }
509  
510 29 ▶sub is_imaged($$$;$) {
Subroutine is_imaged: called CC=7 SCAR=27.2
Pod documented
511 29 my $epsilon = Imager::i_img_epsilonf();
512 29 ▶ if (@_ > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ > 3) 0 29 29
513 0 ($epsilon) = splice @_, 2, 1;
514 }
515  
516 29 my ($left, $right, $comment) = @_;
517  
518 {
519 29 local $Test::Builder::Level = $Test::Builder::Level + 1;
520  
521 29 ▶ _low_image_diff_check($left, $right, $comment)
Branch: 1 branch
BranchTrueFalseTotal
_low_image_diff_check($left, $right, $comment) or return 29 0 29
522 or return;
523 }
524  
525 29 my $builder = Test::Builder->new;
526  
527 29 my $same = Imager::i_img_samef($left->{IMG}, $right->{IMG}, $epsilon, $comment);
528 29 ▶ if (!$same) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($same) 0 29 29
529 0 $builder->ok(0, $comment);
530 0 $builder->diag("images different");
531  
532 # find the first mismatch
533 PIXELS:
534 0 for my $y (0 .. $left->getheight()-1) {
535 0 for my $x (0.. $left->getwidth()-1) {
536 0 my @lsamples = $left->getsamples(x => $x, y => $y, width => 1, type => "float");
537 0 my @rsamples = $right->getsamples(x => $x, y => $y, width => 1, type => "float");
538 0 ▶ if ("@lsamples" ne "@rsamples") {
Branch: 1 branch
BranchTrueFalseTotal
if (join($", @lsamples) ne join($", @rsamples)) 0 0 0
539 0 $builder->diag("first mismatch at ($x, $y) - @lsamples vs @rsamples");
540 0 last PIXELS;
541 }
542 }
543 }
544  
545 0 return;
546 }
547
548 29 return $builder->ok(1, $comment);
549 }
550  
551 3 ▶sub isnt_image {
Subroutine isnt_image: called CC=1 SCAR=0.0
Pod documented
552 3 my ($left, $right, $comment) = @_;
553  
554 3 my $builder = Test::Builder->new;
555  
556 3 my $diff = Imager::i_img_diff($left->{IMG}, $right->{IMG});
557  
558 3 return $builder->ok($diff, "$comment");
559 }
560  
561 4 ▶sub image_bounds_checks {
Subroutine image_bounds_checks: called CC=1 SCAR=0.0
Pod documented
562 4 my $im = shift;
563  
564 4 my $builder = Test::Builder->new;
565  
566 4 $builder->ok(!$im->getpixel(x => -1, y => 0), 'bounds check get (-1, 0)');
567 4 $builder->ok(!$im->getpixel(x => 10, y => 0), 'bounds check get (10, 0)');
568 4 $builder->ok(!$im->getpixel(x => 0, y => -1), 'bounds check get (0, -1)');
569 4 $builder->ok(!$im->getpixel(x => 0, y => 10), 'bounds check get (0, 10)');
570 4 $builder->ok(!$im->getpixel(x => -1, y => 0), 'bounds check get (-1, 0) float');
571 4 $builder->ok(!$im->getpixel(x => 10, y => 0), 'bounds check get (10, 0) float');
572 4 $builder->ok(!$im->getpixel(x => 0, y => -1), 'bounds check get (0, -1) float');
573 4 $builder->ok(!$im->getpixel(x => 0, y => 10), 'bounds check get (0, 10) float');
574 4 my $black = Imager::Color->new(0, 0, 0);
575 4 require Imager::Color::Float;
576 4 my $blackf = Imager::Color::Float->new(0, 0, 0);
577 4 $builder->ok($im->setpixel(x => -1, y => 0, color => $black) == 0,
578 'bounds check set (-1, 0)');
579 4 $builder->ok($im->setpixel(x => 10, y => 0, color => $black) == 0,
580 'bounds check set (10, 0)');
581 4 $builder->ok($im->setpixel(x => 0, y => -1, color => $black) == 0,
582 'bounds check set (0, -1)');
583 4 $builder->ok($im->setpixel(x => 0, y => 10, color => $black) == 0,
584 'bounds check set (0, 10)');
585 4 $builder->ok($im->setpixel(x => -1, y => 0, color => $blackf) == 0,
586 'bounds check set (-1, 0) float');
587 4 $builder->ok($im->setpixel(x => 10, y => 0, color => $blackf) == 0,
588 'bounds check set (10, 0) float');
589 4 $builder->ok($im->setpixel(x => 0, y => -1, color => $blackf) == 0,
590 'bounds check set (0, -1) float');
591 4 $builder->ok($im->setpixel(x => 0, y => 10, color => $blackf) == 0,
592 'bounds check set (0, 10) float');
593 }
594  
595 21 ▶sub test_colorf_gpix {
Subroutine test_colorf_gpix: called CC=4 SCAR=16.0
Pod documented
596 21 my ($im, $x, $y, $expected, $epsilon, $comment) = @_;
597  
598 21 my $builder = Test::Builder->new;
599
600 21 ▶ defined $comment or $comment = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $comment or $comment = "" 17 4 21
601  
602 21 my $c = Imager::i_gpixf($im, $x, $y);
603 21 ▶ unless ($c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 21 21
604 0 $builder->ok(0, "$comment - retrieve color at ($x,$y)");
605 0 return;
606 }
607 21 ▶ unless ($builder->ok(colorf_cmp($c, $expected, $epsilon) == 0,
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(colorf_cmp($c, $expected, $epsilon) == 0, "$comment - got right color ($x, $y)")) 0 21 21
608 "$comment - got right color ($x, $y)")) {
609 0 my @c = $c->rgba;
610 0 my @exp = $expected->rgba;
611 0 $builder->diag(<<EOS);
612 # got: ($c[0], $c[1], $c[2])
613 # expected: ($exp[0], $exp[1], $exp[2])
614 EOS
615 }
616 21 1;
617 }
618  
619 12 ▶sub test_color_gpix {
Subroutine test_color_gpix: called CC=4 SCAR=17.4
Pod documented
620 12 my ($im, $x, $y, $expected, $comment) = @_;
621  
622 12 my $builder = Test::Builder->new;
623
624 12 ▶ defined $comment or $comment = '';
Branch: 1 branch
BranchTrueFalseTotal
defined $comment or $comment = "" 12 0 12
625 12 my $c = Imager::i_get_pixel($im, $x, $y);
626 12 ▶ unless ($c) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 12 12
627 0 $builder->ok(0, "$comment - retrieve color at ($x,$y)");
628 0 return;
629 }
630 12 ▶ unless ($builder->ok(color_cmp($c, $expected) == 0,
Branch: 1 branch
BranchTrueFalseTotal
unless ($builder->ok(color_cmp($c, $expected) == 0, "got right color ($x, $y)")) 0 12 12
631 "got right color ($x, $y)")) {
632 0 my @c = $c->rgba;
633 0 my @exp = $expected->rgba;
634 0 $builder->diag(<<EOS);
635 # got: ($c[0], $c[1], $c[2])
636 # expected: ($exp[0], $exp[1], $exp[2])
637 EOS
638 0 return;
639 }
640  
641 12 return 1;
642 }
643  
644 6 ▶sub test_colorf_glin {
Subroutine test_colorf_glin: called CC=2 SCAR=7.0
Pod documented
645 6 my ($im, $x, $y, $pels, $comment) = @_;
646  
647 6 my $builder = Test::Builder->new;
648
649 6 my @got = Imager::i_glinf($im, $x, $x+@$pels, $y);
650 6 ▶ @got == @$pels
Branch: 1 branch
BranchTrueFalseTotal
@got == @$pels or return $builder->is_num(scalar @got, scalar @$pels, "$comment - pixels retrieved") 6 0 6
651 or return $builder->is_num(scalar(@got), scalar(@$pels), "$comment - pixels retrieved");
652
653 6 return $builder->ok(!grep(colorf_cmp($pels->[$_], $got[$_], 0.005), 0..$#got),
654 "$comment - check colors ($x, $y)");
655 }
656  
657 621 ▶sub colorf_cmp {
Subroutine colorf_cmp: called CC=7 SCAR=21.5
Pod documented
658 621 my ($c1, $c2, $epsilon) = @_;
659  
660 621 ▶ defined $epsilon or $epsilon = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $epsilon or $epsilon = 0 617 4 621
661  
662 621 my @s1 = $c1->rgba;
663 621 my @s2 = $c2->rgba;
664  
665 # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
666 621 ▶ return abs($s1[0]-$s2[0]) >= $epsilon && $s1[0] <=> $s2[0]
Condition53%8/15
abs $s1[0] - $s2[0] >= $epsilon && $s1[0] <=> $s2[0] || abs $s1[1] - $s2[1] >= $epsilon && $s1[1] <=> $s2[1] || abs $s1[2] - $s2[2] >= $epsilon && $s1[2] <=> $s2[2]
l!l&&r!l&&!r
00621
abs $s1[0] - $s2[0] >= $epsilon and $s1[0] <=> $s2[0] or abs $s1[1] - $s2[1] >= $epsilon and $s1[1] <=> $s2[1]
l!l&&r!l&&!r
00621
abs $s1[0] - $s2[0] >= $epsilon and $s1[0] <=> $s2[0]
!ll&&!rl&&r
609120
abs $s1[1] - $s2[1] >= $epsilon and $s1[1] <=> $s2[1]
!ll&&!rl&&r
609120
abs $s1[2] - $s2[2] >= $epsilon and $s1[2] <=> $s2[2]
!ll&&!rl&&r
609120
MC/DC0%0/6
abs $s1[0] - $s2[0] >= $epsilon && $s1[0] <=> $s2[0] || abs $s1[1] - $s2[1] >= $epsilon && $s1[1] <=> $s2[1] || abs $s1[2] - $s2[2] >= $epsilon && $s1[2] <=> $s2[2]
abs $s1[0] - $s2[0] >= $epsilon $s1[0] <=> $s2[0] abs $s1[1] - $s2[1] >= $epsilon $s1[1] <=> $s2[1] abs $s1[2] - $s2[2] >= $epsilon $s1[2] <=> $s2[2]
Truth table13%2/15
A and B or C and D || E and F
A= abs $s1[0] - $s2[0] >= $epsilonB= $s1[0] <=> $s2[0]C= abs $s1[1] - $s2[1] >= $epsilonD= $s1[1] <=> $s2[1]E= abs $s1[2] - $s2[2] >= $epsilonF= $s1[2] <=> $s2[2]
ABCDEFresult
11XXXX1
0X11XX1
1011XX1
0X0X111
0X10111
100X111
1010111
0X0X0X0
0X0X100
0X100X0
0X10100
100X0X0
100X100
10100X0
1010100
667 || abs($s1[1]-$s2[1]) >= $epsilon && $s1[1] <=> $s2[1]
668 || abs($s1[2]-$s2[2]) >= $epsilon && $s1[2] <=> $s2[2];
669 }
670  
671 18 ▶sub color_cmp {
Subroutine color_cmp: called CC=3 SCAR=12.7
Pod documented
672 18 my ($c1, $c2) = @_;
673  
674 18 my @s1 = $c1->rgba;
675 18 my @s2 = $c2->rgba;
676  
677 18 ▶ return $s1[0] <=> $s2[0]
Condition33%2/6
$s1[0] <=> $s2[0] || $s1[1] <=> $s2[1] || $s1[2] <=> $s2[2]
l!l&&r!l&&!r
0018
$s1[0] <=> $s2[0] or $s1[1] <=> $s2[1]
l!l&&r!l&&!r
0018
MC/DC0%0/3
$s1[0] <=> $s2[0] || $s1[1] <=> $s2[1] || $s1[2] <=> $s2[2]
$s1[0] <=> $s2[0] $s1[1] <=> $s2[1] $s1[2] <=> $s2[2]
Truth table25%1/4
A or B || C
A= $s1[0] <=> $s2[0]B= $s1[1] <=> $s2[1]C= $s1[2] <=> $s2[2]
ABCresult
1XX1
01X1
0011
0000
678 || $s1[1] <=> $s2[1]
679 || $s1[2] <=> $s2[2];
680 }
681  
682 # these test the action of the channel mask on the image supplied
683 # which should be an OO image.
684 3 ▶sub mask_tests {
Subroutine mask_tests: called CC=2 SCAR=6.9
Pod documented
685 3 my ($im, $epsilon) = @_;
686  
687 46 ▶ no if $] >= 5.014, warnings => 'Imager::channelmask';
Subroutine BEGIN: called CC=1 SCAR=0.0
688 3 my $builder = Test::Builder->new;
689  
690 3 ▶ defined $epsilon or $epsilon = 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $epsilon or $epsilon = 0 2 1 3
691  
692 # we want to check all four of ppix() and plin(), ppix() and plinf()
693 # basic test procedure:
694 # first using default/all 1s mask, set to white
695 # make sure we got white
696 # set mask to skip a channel, set to grey
697 # make sure only the right channels set
698  
699 3 print "# channel mask tests\n";
700 # 8-bit color tests
701 3 my $white = Imager::NC(255, 255, 255);
702 3 my $grey = Imager::NC(128, 128, 128);
703 3 my $white_grey = Imager::NC(128, 255, 128);
704  
705 3 print "# with ppix\n";
706 3 $builder->ok($im->setmask(mask=>~0), "set to default mask");
707 3 $builder->ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
708 3 test_color_gpix($im->{IMG}, 0, 0, $white, "ppix");
709 3 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
710 3 $builder->ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
711 3 test_color_gpix($im->{IMG}, 0, 0, $white_grey, "ppix masked");
712  
713 3 print "# with plin\n";
714 3 $builder->ok($im->setmask(mask=>~0), "set to default mask");
715 3 $builder->ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]),
716 "set to white all channels");
717 3 test_color_gpix($im->{IMG}, 0, 1, $white, "plin");
718 3 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
719 3 $builder->ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]),
720 "set to grey, no channel 2");
721 3 test_color_gpix($im->{IMG}, 0, 1, $white_grey, "plin masked");
722  
723 # float color tests
724 3 my $whitef = Imager::NCF(1.0, 1.0, 1.0);
725 3 my $greyf = Imager::NCF(0.5, 0.5, 0.5);
726 3 my $white_greyf = Imager::NCF(0.5, 1.0, 0.5);
727  
728 3 print "# with ppixf\n";
729 3 $builder->ok($im->setmask(mask=>~0), "set to default mask");
730 3 $builder->ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
731 3 test_colorf_gpix($im->{IMG}, 0, 2, $whitef, $epsilon, "ppixf");
732 3 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
733 3 $builder->ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
734 3 test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf, $epsilon, "ppixf masked");
735  
736 3 print "# with plinf\n";
737 3 $builder->ok($im->setmask(mask=>~0), "set to default mask");
738 3 $builder->ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]),
739 "set to white all channels");
740 3 test_colorf_gpix($im->{IMG}, 0, 3, $whitef, $epsilon, "plinf");
741 3 $builder->ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
742 3 $builder->ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]),
743 "set to grey, no channel 2");
744 3 test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf, $epsilon, "plinf masked");
745  
746 }
747  
748 0 ▶sub std_font_test_count {
Subroutine std_font_test_count: not called CC=1 SCAR=6.9
Pod documented
749 0 return 21;
750 }
751  
752 0 ▶sub std_font_tests {
Subroutine std_font_tests: not called CC=13 SCAR=52.0
Pod documented
753 0 my ($opts) = @_;
754  
755 my $font = $opts->{font}
756 0 ▶ or carp "Missing font parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $font = $opts->{"font"} or carp("Missing font parameter") 0 0 0
757  
758 0 ▶ my $name_font = $opts->{glyph_name_font} || $font;
Condition0%0/3
$opts->{'glyph_name_font'} || $font
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'glyph_name_font'} || $font
$opts->{'glyph_name_font'} $font
Truth table0%0/3
A || B
A= $opts->{'glyph_name_font'}B= $font
ABresult
1X1
011
000
759  
760 0 ▶ my $has_chars = $opts->{has_chars} || [ 1, '', 1 ];
Condition0%0/2
$opts->{'has_chars'} || [1, '', 1]
l!l
00
MC/DC0%0/1
$opts->{'has_chars'} || [1, '', 1]
$opts->{'has_chars'}
Truth table0%0/2
A
A= $opts->{'has_chars'}
Aresult
11
00
761  
762 0 ▶ my $glyph_names = $opts->{glyph_names} || [ "A", undef, "A" ];
Condition0%0/2
$opts->{'glyph_names'} || ['A', undef, 'A']
l!l
00
MC/DC0%0/1
$opts->{'glyph_names'} || ['A', undef, 'A']
$opts->{'glyph_names'}
Truth table0%0/2
A
A= $opts->{'glyph_names'}
Aresult
11
00
763  
764 SKIP:
765 { # check magic is handled correctly
766 # https://rt.cpan.org/Ticket/Display.html?id=83438
767 0 ▶ skip("no native UTF8 support in this version of perl", 11)
Branch: 1 branch
BranchTrueFalseTotal
unless $] >= "5.006" 0 0 0
768 unless $] >= 5.006;
769 0 ▶ skip("overloading handling of magic is broken in this version of perl", 11)
Branch: 1 branch
BranchTrueFalseTotal
unless $] >= "5.008" 0 0 0
770 unless $] >= 5.008;
771 0 Imager->log("utf8 magic tests\n");
772 0 my $over = bless {}, "Imager::Test::OverUtf8";
773 0 my $text = "A".chr(0x2010)."A";
774 0 my $white = Imager::Color->new("#FFF");
775 0 my $base_draw = Imager->new(xsize => 80, ysize => 20);
776 0 ok($base_draw->string(font => $font,
777 text => $text,
778 x => 2,
779 y => 18,
780 size => 15,
781 color => $white,
782 aa => 1),
783 "magic: make a base image");
784 0 my $test_draw = Imager->new(xsize => 80, ysize => 20);
785 0 ok($test_draw->string(font => $font,
786 text => $over,
787 x => 2,
788 y => 18,
789 size => 15,
790 color => $white,
791 aa => 1),
792 "magic: draw with overload");
793 0 is_image($base_draw, $test_draw, "check they match");
794 0 ▶ if ($opts->{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"files"}) 0 0 0
795 0 $test_draw->write(file => "testout/utf8tdr.ppm");
796 0 $base_draw->write(file => "testout/utf8bdr.ppm");
797 }
798  
799 0 my $base_cp = Imager->new(xsize => 80, ysize => 20);
800 0 $base_cp->box(filled => 1, color => "#808080");
801 0 my $test_cp = $base_cp->copy;
802 0 ok($base_cp->string(font => $font,
803 text => $text,
804 y => 2,
805 y => 18,
806 size => 16,
807 channel => 2,
808 aa => 1),
809 "magic: make a base image (channel)");
810 0 Imager->log("magic: draw to channel with overload\n");
811 0 ok($test_cp->string(font => $font,
812 text => $over,
813 y => 2,
814 y => 18,
815 size => 16,
816 channel => 2,
817 aa => 1),
818 "magic: draw with overload (channel)");
819 0 is_image($test_cp, $base_cp, "check they match");
820 0 ▶ if ($opts->{files}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts->{"files"}) 0 0 0
821 0 $test_cp->write(file => "testout/utf8tcp.ppm");
822 0 $base_cp->write(file => "testout/utf8bcp.ppm");
823 }
824  
825 SKIP:
826 {
827 0 Imager->log("magic: has_chars\n");
828 0 ▶ $font->can("has_chars")
Branch: 1 branch
BranchTrueFalseTotal
$font->can("has_chars") or skip("No has_chars aupport", 2) 0 0 0
829 or skip "No has_chars aupport", 2;
830 0 is_deeply([ $font->has_chars(string => $text) ], $has_chars,
831 "magic: has_chars with normal utf8 text");
832 0 is_deeply([ $font->has_chars(string => $over) ], $has_chars,
833 "magic: has_chars with magic utf8 text");
834 }
835  
836 0 Imager->log("magic: bounding_box\n");
837 0 my @base_bb = $font->bounding_box(string => $text, size => 30);
838 0 is_deeply([ $font->bounding_box(string => $over, size => 30) ],
839 \@base_bb,
840 "check bounding box magic");
841  
842 SKIP:
843 {
844 0 ▶ $font->can_glyph_names
Branch: 1 branch
BranchTrueFalseTotal
$font->can_glyph_names or skip("No glyph_names", 2) 0 0 0
845 or skip "No glyph_names", 2;
846 0 Imager->log("magic: glyph_names\n");
847 0 my @text_names = $name_font->glyph_names(string => $text, reliable_only => 0);
848 0 is_deeply(\@text_names, $glyph_names,
849 "magic: glyph_names with normal utf8 text");
850 0 my @over_names = $name_font->glyph_names(string => $over, reliable_only => 0);
851 0 is_deeply(\@over_names, $glyph_names,
852 "magic: glyph_names with magic utf8 text");
853 }
854 }
855  
856 { # invalid UTF8 handling at the OO level
857 0 my $im = Imager->new(xsize => 80, ysize => 20);
858 0 my $bad_utf8 = pack("C", 0xC0);
859 0 Imager->_set_error("");
860 0 ok(!$im->string(font => $font, size => 1, text => $bad_utf8, utf8 => 1,
861 y => 18, x => 2),
862 "drawing invalid utf8 should fail");
863 0 is($im->errstr, "invalid UTF8 character", "check error message");
864 0 Imager->_set_error("");
865 0 ok(!$im->string(font => $font, size => 1, text => $bad_utf8, utf8 => 1,
866 y => 18, x => 2, channel => 1),
867 "drawing invalid utf8 should fail (channel)");
868 0 is($im->errstr, "invalid UTF8 character", "check error message");
869 0 Imager->_set_error("");
870 0 ok(!$font->bounding_box(string => $bad_utf8, size => 30, utf8 => 1),
871 "bounding_box() bad utf8 should fail");
872 0 is(Imager->errstr, "invalid UTF8 character", "check error message");
873 SKIP:
874 {
875 0 ▶ $font->can_glyph_names
Branch: 1 branch
BranchTrueFalseTotal
$font->can_glyph_names or skip("No glyph_names support", 2) 0 0 0
876 or skip "No glyph_names support", 2;
877 0 Imager->_set_error("");
878 0 is_deeply([ $font->glyph_names(string => $bad_utf8, utf8 => 1) ],
879 [ ],
880 "glyph_names returns empty list for bad string");
881 0 is(Imager->errstr, "invalid UTF8 character", "check error message");
882 }
883 SKIP:
884 {
885 0 ▶ $font->can("has_chars")
Branch: 1 branch
BranchTrueFalseTotal
$font->can("has_chars") or skip("No has_chars support", 2) 0 0 0
886 or skip "No has_chars support", 2;
887 0 Imager->_set_error("");
888 0 is_deeply([ $font->has_chars(string => $bad_utf8, utf8 => 1) ],
889 [ ],
890 "has_chars returns empty list for bad string");
891 0 is(Imager->errstr, "invalid UTF8 character", "check error message");
892 }
893 }
894 }
895  
896 1 ▶sub std_image_tests {
Subroutine std_image_tests: called CC=9 SCAR=22.0
Pod documented
897 1 my ($opts) = @_;
898  
899 my $bits = $opts->{bits}
900 1 ▶ or croak "Missing bits parameter";
Branch: 1 branch
BranchTrueFalseTotal
my $bits = $opts->{"bits"} or croak("Missing bits parameter") 1 0 1
901  
902 1 ▶ my $models = $opts->{models} || [ qw(gray graya rgb rgba) ];
Condition50%1/2
$opts->{'models'} || ['gray', 'graya', 'rgb', 'rgba']
l!l
01
MC/DC0%0/1
$opts->{'models'} || ['gray', 'graya', 'rgb', 'rgba']
$opts->{'models'}
Truth table50%1/2
A
A= $opts->{'models'}
Aresult
11
00
903  
904 1 my @ten_zeros = (0) x 10;
905  
906 1 my @test_colors =
907 (
908 [ 255, 128, 0, 255 ],
909 [ 192, 128, 64, 128 ],
910 );
911  
912 1 for my $model (@$models) {
913 4 print "# model $model\n";
914 4 my $im = Imager->new(xsize => 10, ysize => 10, model => $model, bits => $bits);
915 4 my $alpha_ch = $im->alphachannel;
916 4 my $col_channels = $im->colorchannels;
917 4 my $channel_count = $col_channels;
918 4 ▶ ++$channel_count if $alpha_ch;
Branch: 1 branch
BranchTrueFalseTotal
if $alpha_ch 2 2 4
919 4 my @colors;
920 my @samples;
921 4 my @samples_alpha;
922 4 my @alphas;
923 4 for my $color (@test_colors) {
924 8 my @ch = @{$color}[0 .. $col_channels-1 ];
925 8 push @samples, @ch;
926 8 push @samples_alpha, @ch;
927 8 ▶ if ($alpha_ch) {
Branch: 1 branch
BranchTrueFalseTotal
if ($alpha_ch) 4 4 8
928 4 my $alpha = $color->[3];
929 4 push @ch, $alpha;
930 4 push @alphas, $alpha;
931 4 push @samples_alpha, $alpha;
932 }
933 8 push @colors, \@ch;
934 }
935 4 my @channels = ( 0 .. $channel_count-1 );
936 ok($im->setpixel(x => 0, y => 0, color => { channels => $colors[0] }),
937 "set a normal spread of values at (0,0)")
938 0 ▶ or diag "$model: set first pixel to (" . join(", ", @{$colors[0]}) . "): ".$im->errstr;
Branch: 1 branch
BranchTrueFalseTotal
&ok(scalar $im->setpixel("x", 0, "y", 0, "color", {"channels", $colors[0]}), "set a normal spread of values at (0,0)") or diag("${model}: set first pixel to (" . join(", ", @{$colors[0];}) . '): ' . $im->errstr) 4 0 4
939 4 ok($im->setpixel(x => 1, y => 0, color => { channels => $colors[1] }),
940 "set a normal spread of values at (1,0)");
941  
942 4 for my $type (qw(8bit float 16bit)) {
943 {
944 # getsamples, direct return
945 # one too many channels
946 12 ok(!$im->getsamples(type => $type, y => 0,
947 channels => [ $channel_count ]),
948 "fetch invalid channel type $type");
949 12 like($im->errstr, qr/getsamples: channel $channel_count out of range for $channel_count channel image/,
950 "check message channel $channel_count type $type");
951  
952 12 ok(!$im->getsamples(type => $type, y => 0, channels => [ -1 ]),
953 "fetch no channels type $type");
954 12 like($im->errstr, qr/getsamples: channel -1 out of range for $channel_count channel image/,
955 "check message for negative channel type $type");
956  
957 12 ok(!$im->getsamples(type => $type, y => -1),
958 "fetch from row -1 type $type");
959 12 like($im->errstr, qr/getsamples: y outside of image/,
960 "check message for y -1 type $type");
961  
962 12 ok(!$im->getsamples(type => $type, y => 10),
963 "fetch from row 10 type $type");
964 12 like($im->errstr, qr/getsamples: y outside of image/,
965 "check message for y 10 type $type");
966  
967 12 ok(!$im->getsamples(type => $type, x => -1, y => 0),
968 "fetch from x = -1 type $type");
969 12 like($im->errstr, qr/getsamples: left outside of image/,
970 "check message for x -1 type $type");
971  
972 12 ok(!$im->getsamples(type => $type, x => 11, width => 2, y => 0),
973 "fetch from x = 11");
974 12 like($im->errstr, qr/getsamples: left outside of image/,
975 "check message for x 11 type $type");
976  
977 12 ok(!$im->getsamples(type => $type, x => 5, width => 0, y => 0),
978 "fetch from x = 5, width 0");
979 12 like($im->errstr, qr/getsamples: left not left of right/,
980 "check message for x = 5, width 0 type $type");
981 }
982  
983 {
984 # getsamples, target return
985 # one too many channels
986 12 my @t;
987 12 ok(!$im->getsamples(type => $type, target => \@t, y => 0,
988 channels => [ $channel_count ]),
989 "fetch invalid channel type $type");
990 12 like($im->errstr, qr/getsamples: channel $channel_count out of range for $channel_count channel image/,
991 "check message channel $channel_count type $type");
992  
993 12 ok(!$im->getsamples(type => $type, target => \@t, y => 0,
994 channels => [ -1 ]),
995 "fetch no channels type $type");
996 12 like($im->errstr, qr/getsamples: channel -1 out of range for $channel_count channel image/,
997 "check message for negative channel type $type");
998  
999 12 ok(!$im->getsamples(type => $type, target => \@t, y => -1),
1000 "fetch from row -1 type $type");
1001 12 like($im->errstr, qr/getsamples: y outside of image/,
1002 "check message for y -1 type $type");
1003  
1004 12 ok(!$im->getsamples(type => $type, target => \@t, y => 10),
1005 "fetch from row 10 type $type");
1006 12 like($im->errstr, qr/getsamples: y outside of image/,
1007 "check message for y 10 type $type");
1008  
1009 12 ok(!$im->getsamples(type => $type, target => \@t, x => -1, y => 0),
1010 "fetch from x = -1 type $type");
1011 12 like($im->errstr, qr/getsamples: left outside of image/,
1012 "check message for x -1 type $type");
1013  
1014 12 ok(!$im->getsamples(type => $type, target => \@t, x => 11, width => 2, y => 0),
1015 "fetch from x = 11 type $type");
1016 12 like($im->errstr, qr/getsamples: left outside of image/,
1017 "check message for x 11 type $type");
1018  
1019 12 ok(!$im->getsamples(type => $type, target => \@t, x => 5, width => 0, y => 0),
1020 "fetch from x = 5, width 0 type $type");
1021 12 like($im->errstr, qr/getsamples: left not left of right/,
1022 "check message for x = 5, width 0 type $type");
1023 }
1024 }
1025 }
1026 }
1027  
1028 package Imager::Test::OverUtf8;
1029 0 ▶use overload '""' => sub { "A".chr(0x2010)."A" };
Subroutine BEGIN: called CC=1 SCAR=0.2
Subroutine __ANON__: not called CC=1 SCAR=0.2
1030  
1031  
1032 46 1;
1033  
1034 __END__
« blib/lib/Imager.pm ↑ Summary blib/lib/Imager/Font/Image.pm »