Debian Codegraph @

t/100-base/010-introvert.t

raw
1 #!perl -w
2 # t/t01introvert.t - tests internals of image formats
3 # to make sure we get expected values
4
5 use strict;
6 use Test::More;
7
8 BEGIN { use_ok(Imager => qw(:handy :all)) }
9 use warnings;
10 use POSIX qw(INT_MIN INT_MAX UINT_MAX);
11
12 use Imager::Test qw(image_bounds_checks is_color3 is_color4 is_fcolor4 color_cmp mask_tests is_fcolor3);
13
14 -d "testout" or mkdir "testout";
15
16 Imager->open_log(log => "testout/t01introvert.log");
17
18 my $im_g = Imager::ImgRaw::new(100, 101, 1);
19
20 my $red = NC(255, 0, 0);
21 my $green = NC(0, 255, 0);
22 my $blue = NC(0, 0, 255);
23
24 use Imager::Color::Float;
25 my $f_black = Imager::Color::Float->new(0, 0, 0);
26 my $f_red = Imager::Color::Float->new(1.0, 0, 0);
27 my $f_green = Imager::Color::Float->new(0, 1.0, 0);
28 my $f_blue = Imager::Color::Float->new(0, 0, 1.0);
29
30 is(Imager::i_img_getchannels($im_g), 1, "1 channel image channel count");
31 ok(Imager::i_img_getmask($im_g) & 1, "1 channel image mask");
32 ok(!Imager::i_img_virtual($im_g), "1 channel image not virtual");
33 is(Imager::i_img_bits($im_g), 8, "1 channel image has 8 bits/sample");
34 is(Imager::i_img_type($im_g), 0, "1 channel image is direct");
35 is(Imager::i_img_get_width($im_g), 100, "100 pixels wide");
36 is(Imager::i_img_get_height($im_g), 101, "101 pixels high");
37
38 my @ginfo = Imager::i_img_info($im_g);
39 is($ginfo[0], 100, "1 channel image width");
40 is($ginfo[1], 101, "1 channel image height");
41
42 undef $im_g; # can we check for release after this somehow?
43
44 my $im_rgb = Imager::ImgRaw::new(100, 101, 3);
45
46 is(Imager::i_img_getchannels($im_rgb), 3, "3 channel image channel count");
47 is((Imager::i_img_getmask($im_rgb) & 7), 7, "3 channel image mask");
48 is(Imager::i_img_bits($im_rgb), 8, "3 channel image has 8 bits/sample");
49 is(Imager::i_img_type($im_rgb), 0, "3 channel image is direct");
50
51 undef $im_rgb;
52
53 my $im_pal = Imager::i_img_pal_new(100, 101, 3, 256);
54
55 ok($im_pal, "make paletted image");
56 is(Imager::i_img_getchannels($im_pal), 3, "pal img channel count");
57 is(Imager::i_img_bits($im_pal), 8, "pal img bits");
58 is(Imager::i_img_type($im_pal), 1, "pal img is paletted");
59
60 my $red_idx = check_add($im_pal, $red, 0);
61 my $green_idx = check_add($im_pal, $green, 1);
62 my $blue_idx = check_add($im_pal, $blue, 2);
63
64 # basic writing of palette indicies
65 # fill with red
66 is(Imager::i_ppal($im_pal, 0, 0, ($red_idx) x 100), 100,
67 "write red 100 times");
68 # and blue
69 is(Imager::i_ppal($im_pal, 50, 0, ($blue_idx) x 50), 50,
70 "write blue 50 times");
71
72 # make sure we get it back
73 my @pals = Imager::i_gpal($im_pal, 0, 100, 0);
74 ok(!grep($_ != $red_idx, @pals[0..49]), "check for red");
75 ok(!grep($_ != $blue_idx, @pals[50..99]), "check for blue");
76 is(Imager::i_gpal($im_pal, 0, 100, 0), "\0" x 50 . "\2" x 50,
77 "gpal in scalar context");
78 my @samp = Imager::i_gsamp($im_pal, 0, 100, 0, [ 0, 1, 2 ]);
79 is(@samp, 300, "gsamp count in list context");
80 my @samp_exp = ((255, 0, 0) x 50, (0, 0, 255) x 50);
81 is_deeply(\@samp, \@samp_exp, "gsamp list deep compare");
82 my $samp = Imager::i_gsamp($im_pal, 0, 100, 0, [ 0, 1, 2 ]);
83 is(length($samp), 300, "gsamp scalar length");
84 is($samp, "\xFF\0\0" x 50 . "\0\0\xFF" x 50, "gsamp scalar bytes");
85
86 # reading indicies as colors
87 my $c_red = Imager::i_get_pixel($im_pal, 0, 0);
88 ok($c_red, "got the red pixel");
89 is_color3($c_red, 255, 0, 0, "and it's red");
90 my $c_blue = Imager::i_get_pixel($im_pal, 50, 0);
91 ok($c_blue, "got the blue pixel");
92 is_color3($c_blue, 0, 0, 255, "and it's blue");
93
94 # drawing with colors
95 ok(Imager::i_ppix($im_pal, 0, 0, $green) == 0, "draw with color in palette");
96 # that was in the palette, should still be paletted
97 is(Imager::i_img_type($im_pal), 1, "image still paletted");
98
99 my $c_green = Imager::i_get_pixel($im_pal, 0, 0);
100 ok($c_green, "got green pixel");
101 is_color3($c_green, 0, 255, 0, "and it's green");
102
103 is(Imager::i_colorcount($im_pal), 3, "still 3 colors in palette");
104 is(Imager::i_findcolor($im_pal, $green), 1, "and green is the second");
105
106 my $black = NC(0, 0, 0);
107 # this should convert the image to RGB
108 ok(Imager::i_ppix($im_pal, 1, 0, $black) == 0, "draw with black (not in palette)");
109 is(Imager::i_img_type($im_pal), 0, "pal img shouldn't be paletted now");
110
111 {
112 my %quant =
113 (
114 colors => [$red, $green, $blue, $black],
115 make_colors => 'none',
116 );
117 my $im_pal2 = Imager::i_img_to_pal($im_pal, \%quant);
118 ok($im_pal2, "got an image from quantizing")
119 or diag _get_error();
120 is(@{$quant{colors}}, 4, "quant has the right number of colours");
121 is(Imager::i_colorcount($im_pal2), 4, "and so does the image");
122 my @colors = Imager::i_getcolors($im_pal2, 0, 4);
123 my ($first) = Imager::i_getcolors($im_pal2, 0);
124 my @first = $colors[0]->rgba;
125 is_color3($first, $first[0], $first[1], $first[2],
126 "check first color is first for multiple or single fetch");
127 is_color3($colors[0], 255, 0, 0, "still red");
128 is_color3($colors[1], 0, 255, 0, "still green");
129 is_color3($colors[2], 0, 0, 255, "still blue");
130 is_color3($colors[3], 0, 0, 0, "still black");
131 my @samples = Imager::i_gsamp($im_pal2, 0, 100, 0, [ 0, 1, 2 ]);
132 my @expect = unpack("C*", "\0\xFF\0\0\0\0"."\xFF\0\0" x 48 . "\0\0\xFF" x 50);
133 my $match_list = is_deeply(\@samples, \@expect, "colors are still correct");
134 my $samples = Imager::i_gsamp($im_pal2, 0, 100, 0, [ 0, 1, 2 ]);
135 my $match_scalar = is_deeply([ unpack("C*", $samples) ],
136 \@expect, "colors are still correct (scalar)");
137 unless ($match_list && $match_scalar) {
138 # this has been failing on a particular smoker, provide more
139 # diagnostic information
140 print STDERR "Pallete:\n";
141 print STDERR " $_: ", join(",", $colors[$_]->rgba), "\n" for 0..$#colors;
142 print STDERR "Samples (list): ", join(",", @samples), "\n";
143 print STDERR "Samples (scalar): ", join(",", unpack("C*", $samples)), "\n";
144 print STDERR "Indexes: ", join(",", Imager::i_gpal($im_pal2, 0, 100, 0)), "\n";
145 }
146 }
147
148 # test the OO interfaces
149 my $impal2 = Imager->new(type=>'pseudo', xsize=>200, ysize=>201);
150 ok($impal2, "make paletted via OO")
151 or diag(Imager->errstr);
152 is($impal2->getchannels, 3, "check channels");
153 is($impal2->bits, 8, "check bits");
154 is($impal2->type, 'paletted', "check type");
155 is($impal2->getwidth, 200, "check width");
156 is($impal2->getheight, 201, "check height");
157 is($impal2->colormodel, "rgb", "check color model (string)");
158 is($impal2->colormodel(numeric => 1), 3, "check color model (numeric)");
159 is($impal2->alphachannel, undef, "check alpha channel (has none)");
160 is($impal2->colorchannels, 3, "check colorchannels");
161
162 {
163 my $red_idx = $impal2->addcolors(colors=>[$red]);
164 ok($red_idx, "add red to OO");
165 is(0+$red_idx, 0, "and it's expected index for red");
166 my $blue_idx = $impal2->addcolors(colors=>[$blue, $green]);
167 ok($blue_idx, "add blue/green via OO");
168 is($blue_idx, 1, "and it's expected index for blue");
169 my $green_idx = $blue_idx + 1;
170 my $c = $impal2->getcolors(start=>$green_idx);
171 is_color3($c, 0, 255, 0, "found green where expected");
172 my @cols = $impal2->getcolors;
173 is(@cols, 3, "got 3 colors");
174 my @exp = ( $red, $blue, $green );
175 my $good = 1;
176 for my $i (0..2) {
177 if (color_cmp($cols[$i], $exp[$i])) {
178 $good = 0;
179 last;
180 }
181 }
182 ok($good, "all colors in palette as expected");
183 is($impal2->colorcount, 3, "and colorcount returns 3");
184 is($impal2->maxcolors, 256, "maxcolors as expected");
185 is($impal2->findcolor(color=>$blue), 1, "findcolors found blue");
186 ok($impal2->setcolors(start=>0, colors=>[ $blue, $red ]),
187 "we can setcolors");
188
189 # make an rgb version
190 my $imrgb2 = $impal2->to_rgb8()
191 or diag($impal2->errstr);
192 is($imrgb2->type, 'direct', "converted is direct");
193
194 # and back again, specifying the palette
195 my @colors = ( $red, $blue, $green );
196 my $impal3 = $imrgb2->to_paletted(colors=>\@colors,
197 make_colors=>'none',
198 translate=>'closest');
199 ok($impal3, "got a paletted image from conversion");
200 dump_colors(@colors);
201 print "# in image\n";
202 dump_colors($impal3->getcolors);
203 is($impal3->colorcount, 3, "new image has expected color table size");
204 is($impal3->type, 'paletted', "and is paletted");
205 }
206
207 {
208 my $im = Imager->new;
209 ok($im, "make empty image");
210 ok(!$im->to_rgb8, "convert to rgb8");
211 is($im->errstr, "to_rgb8: empty input image", "check message");
212 is($im->bits, undef, "can't call bits on an empty image");
213 is($im->errstr, "bits: empty input image", "check message");
214 is($im->type, undef, "can't call type on an empty image");
215 is($im->errstr, "type: empty input image", "check message");
216 is($im->virtual, undef, "can't call virtual on an empty image");
217 is($im->errstr, "virtual: empty input image", "check message");
218 is($im->is_bilevel, undef, "can't call virtual on an empty image");
219 is($im->errstr, "is_bilevel: empty input image", "check message");
220 ok(!$im->getscanline(y => 0), "can't call getscanline on an empty image");
221 is($im->errstr, "getscanline: empty input image", "check message");
222 ok(!$im->setscanline(y => 0, pixels => [ $red, $blue ]),
223 "can't call setscanline on an empty image");
224 is($im->errstr, "setscanline: empty input image", "check message");
225 ok(!$im->getsamples(y => 0), "can't call getsamples on an empty image");
226 is($im->errstr, "getsamples: empty input image", "check message");
227 is($im->getwidth, undef, "can't get width of empty image");
228 is($im->errstr, "getwidth: empty input image", "check message");
229 is($im->getheight, undef, "can't get height of empty image");
230 is($im->errstr, "getheight: empty input image", "check message");
231 is($im->getchannels, undef, "can't get channels of empty image");
232 is($im->errstr, "getchannels: empty input image", "check message");
233 is($im->getmask, undef, "can't get mask of empty image");
234 is($im->errstr, "getmask: empty input image", "check message");
235 {
236 no if $] >= 5.014, warnings => 'Imager::channelmask';
237 is($im->setmask, undef, "can't set mask of empty image");
238 is($im->errstr, "setmask: empty input image", "check message");
239 }
240 is($im->colorchannels, undef, "can't get colorchannels of empty image");
241 is($im->errstr, "colorchannels: empty input image", "check message");
242 is($im->alphachannel, undef, "can't get alphachannel of empty image");
243 is($im->errstr, "alphachannel: empty input image", "check message");
244 is($im->colormodel, undef, "can't get colormodel of empty image");
245 is($im->errstr, "colormodel: empty input image", "check message");
246 }
247
248 { # basic checks, 8-bit direct images
249 my $im = Imager->new(xsize => 2, ysize => 3);
250 ok($im, 'create 8-bit direct image');
251 is($im->bits, 8, '8 bits');
252 ok(!$im->virtual, 'not virtual');
253 is($im->type, 'direct', 'direct image');
254 ok(!$im->is_bilevel, 'not mono');
255 }
256
257 ok(!Imager->new(xsize=>0, ysize=>1), "fail to create 0 height image");
258 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
259 "0 height error message check");
260 ok(!Imager->new(xsize=>1, ysize=>0), "fail to create 0 width image");
261 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
262 "0 width error message check");
263 ok(!Imager->new(xsize=>-1, ysize=>1), "fail to create -ve height image");
264 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
265 "-ve width error message check");
266 ok(!Imager->new(xsize=>1, ysize=>-1), "fail to create -ve width image");
267 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
268 "-ve height error message check");
269 ok(!Imager->new(xsize=>-1, ysize=>-1), "fail to create -ve width/height image");
270 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
271 "-ve width/height error message check");
272
273 ok(!Imager->new(xsize=>1, ysize=>1, channels=>0),
274 "fail to create a zero channel image");
275 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
276 "out of range channel message check");
277 ok(!Imager->new(xsize=>1, ysize=>1, channels=>5),
278 "fail to create a five channel image");
279 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
280 "out of range channel message check");
281
282 {
283 # https://rt.cpan.org/Ticket/Display.html?id=8213
284 # check for handling of memory allocation of very large images
285 # only test this on 32-bit machines - on a 64-bit machine it may
286 # result in trying to allocate 4Gb of memory, which is unfriendly at
287 # least and may result in running out of memory, causing a different
288 # type of exit
289 SKIP:
290 {
291 use Config;
292 skip("don't want to allocate 4Gb", 8) unless $Config{ptrsize} == 4;
293
294 my $uint_range = 256 ** $Config{intsize};
295 print "# range $uint_range\n";
296 my $dim1 = int(sqrt($uint_range))+1;
297
298 my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1);
299 is($im_b, undef, "integer overflow check - 1 channel");
300
301 $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1);
302 ok($im_b, "but same width ok");
303 $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1);
304 ok($im_b, "but same height ok");
305 cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
306 "check the error message");
307
308 # do a similar test with a 3 channel image, so we're sure we catch
309 # the same case where the third dimension causes the overflow
310 my $dim3 = int(sqrt($uint_range / 3))+1;
311
312 $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3);
313 is($im_b, undef, "integer overflow check - 3 channel");
314
315 $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3);
316 ok($im_b, "but same width ok");
317 $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3);
318 ok($im_b, "but same height ok");
319
320 cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
321 "check the error message");
322 }
323 }
324
325 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
326 my $warning;
327 local $SIG{__WARN__} =
328 sub {
329 $warning = "@_";
330 my $printed = $warning;
331 $printed =~ s/\n$//;
332 $printed =~ s/\n/\n\#/g;
333 print "# ",$printed, "\n";
334 };
335 my $img = Imager->new(xsize=>10, ysize=>10);
336 $img->to_rgb8(); # doesn't really matter what the source is
337 cmp_ok($warning, '=~', 'void', "correct warning");
338 cmp_ok($warning, '=~', 'introvert\\.t', "correct file");
339 }
340
341 { # http://rt.cpan.org/NoAuth/Bug.html?id=11860
342 my $im = Imager->new(xsize=>2, ysize=>2);
343 $im->setpixel(x=>0, 'y'=>0, color=>$red);
344 $im->setpixel(x=>1, 'y'=>0, color=>$blue);
345
346 my @row = Imager::i_glin($im->{IMG}, 0, 2, 0);
347 is(@row, 2, "got 2 pixels from i_glin");
348 is_color3($row[0], 255, 0, 0, "red first");
349 is_color3($row[1], 0, 0, 255, "then blue");
350 }
351
352 { # general tag tests
353
354 # we don't care much about the image itself
355 my $im = Imager::ImgRaw::new(10, 10, 1);
356
357 ok(Imager::i_tags_addn($im, 'alpha', 0, 101), "i_tags_addn(...alpha, 0, 101)");
358 ok(Imager::i_tags_addn($im, undef, 99, 102), "i_tags_addn(...undef, 99, 102)");
359 is(Imager::i_tags_count($im), 2, "should have 2 tags");
360 ok(Imager::i_tags_addn($im, undef, 99, 103), "i_tags_addn(...undef, 99, 103)");
361 is(Imager::i_tags_count($im), 3, "should have 3 tags, despite the dupe");
362 is(Imager::i_tags_find($im, 'alpha', 0), '0 but true', "find alpha");
363 is(Imager::i_tags_findn($im, 99, 0), 1, "find 99");
364 is(Imager::i_tags_findn($im, 99, 2), 2, "find 99 again");
365 is(Imager::i_tags_get($im, 0), 101, "check first");
366 is(Imager::i_tags_get($im, 1), 102, "check second");
367 is(Imager::i_tags_get($im, 2), 103, "check third");
368
369 ok(Imager::i_tags_add($im, 'beta', 0, "hello", 0),
370 "add string with string key");
371 ok(Imager::i_tags_add($im, 'gamma', 0, "goodbye", 0),
372 "add another one");
373 ok(Imager::i_tags_add($im, undef, 199, "aloha", 0),
374 "add one keyed by number");
375 is(Imager::i_tags_find($im, 'beta', 0), 3, "find beta");
376 is(Imager::i_tags_find($im, 'gamma', 0), 4, "find gamma");
377 is(Imager::i_tags_findn($im, 199, 0), 5, "find 199");
378 ok(Imager::i_tags_delete($im, 2), "delete");
379 is(Imager::i_tags_find($im, 'beta', 0), 2, 'find beta after deletion');
380 ok(Imager::i_tags_delbyname($im, 'beta'), 'delete beta by name');
381 is(Imager::i_tags_find($im, 'beta', 0), undef, 'beta not there now');
382 is(Imager::i_tags_get_string($im, "gamma"), "goodbye",
383 'i_tags_get_string() on a string');
384 is(Imager::i_tags_get_string($im, 99), 102,
385 'i_tags_get_string() on a number entry');
386 ok(Imager::i_tags_delbycode($im, 99), 'delete by code');
387 is(Imager::i_tags_findn($im, 99, 0), undef, '99 not there now');
388 is(Imager::i_tags_count($im), 3, 'final count of 3');
389 }
390
391 {
392 print "# low-level scan line function tests\n";
393 my $im = Imager::ImgRaw::new(10, 10, 4);
394 Imager::i_ppix($im, 5, 0, $red);
395
396 # i_glin/i_glinf
397 my @colors = Imager::i_glin($im, 0, 10, 0);
398 is_deeply([ (0) x 20, (255, 0, 0, 255), (0) x 16 ],
399 [ map $_->rgba, @colors ],
400 "i_glin - list context");
401 my $colors = Imager::i_glin($im, 0, 10, 0);
402 is("00" x 20 . "FF0000FF" . "00" x 16,
403 uc unpack("H*", $colors), "i_glin - scalar context");
404 my @fcolors = Imager::i_glinf($im, 0, 10, 0);
405 is_deeply([ (0.0) x 20, (1.0, 0, 0, 1.0) , (0) x 16 ],
406 [ map $_->rgba, @fcolors ],
407 "i_glinf - list context");
408 my $fcolors = Imager::i_glinf($im, 0, 10, 0);
409 is_deeply([ (0.0) x 20, (1.0, 0, 0, 1.0) , (0) x 16 ],
410 [ unpack "d*", $fcolors ],
411 "i_glinf - scalar context");
412
413 # i_plin/i_plinf
414 my @plin_colors = (($black) x 4, $red, $blue, ($black) x 4);
415 is(Imager::i_plin($im, 0, 1, @plin_colors),
416 10, "i_plin - pass in a list");
417 # make sure we get it back
418 is_deeply([ map [ $_->rgba ], @plin_colors ],
419 [ map [ $_->rgba ], Imager::i_glin($im, 0, 10, 1) ],
420 "check i_plin wrote to the image");
421 my @scalar_plin =
422 (
423 (0,0,0,0) x 4,
424 (0, 255, 0, 255),
425 (0, 0, 255, 255),
426 (0, 0, 0, 0) x 4,
427 );
428 is(Imager::i_plin($im, 0, 2, pack("C*", @scalar_plin)),
429 10, "i_plin - pass in a scalar");
430 is_deeply(\@scalar_plin,
431 [ map $_->rgba , Imager::i_glin($im, 0, 10, 2) ],
432 "check i_plin scalar wrote to the image");
433
434 my @plinf_colors = # Note: only 9 pixels
435 (
436 ($f_blue) x 4,
437 $f_red,
438 ($f_black) x 3,
439 $f_black
440 );
441 is(Imager::i_plinf($im, 0, 3, @plinf_colors), 9,
442 "i_plinf - list");
443 is_deeply([ map $_->rgba, Imager::i_glinf($im, 0, 9, 3) ],
444 [ map $_->rgba, @plinf_colors ],
445 "check colors were written");
446 my @scalar_plinf =
447 (
448 ( 1.0, 1.0, 0, 1.0 ) x 3,
449 ( 0, 1.0, 1.0, 1.0 ) x 2,
450 ( 0, 0, 0, 0 ),
451 ( 1.0, 0, 1.0, 1.0 ),
452 );
453 is(Imager::i_plinf($im, 2, 4, pack("d*", @scalar_plinf)),
454 7, "i_plinf - scalar");
455 is_deeply(\@scalar_plinf,
456 [ map $_->rgba, Imager::i_glinf($im, 2, 9, 4) ],
457 "check colors were written");
458
459 is_deeply([ Imager::i_gsamp($im, 0, 10, 0, [ 0, 3 ]) ],
460 [ (0, 0) x 5, (255, 255), (0, 0) x 4 ],
461 "i_gsamp list context");
462 is("0000" x 5 . "FFFF" . "0000" x 4,
463 uc unpack("H*", Imager::i_gsamp($im, 0, 10, 0, [ 0, 3 ])),
464 "i_gsamp scalar context");
465 is_deeply([ Imager::i_gsampf($im, 2, 9, 4, [ 0, 2, 3 ]) ],
466 [ (1.0, 0, 1.0) x 3, (0, 1.0, 1.0) x 2, (0, 0, 0),
467 (1.0, 1.0, 1.0) ], "i_gsampf - list context");
468 is_deeply([ unpack("d*", Imager::i_gsampf($im, 2, 9, 4, [ 0, 2, 3 ])) ],
469 [ (1.0, 0, 1.0) x 3, (0, 1.0, 1.0) x 2, (0, 0, 0),
470 (1.0, 1.0, 1.0) ], "i_gsampf - scalar context");
471 print "# end low-level scan-line function tests\n";
472 }
473
474 my $psamp_outside_error = "Image position outside of image";
475 { # psamp
476 print "# psamp\n";
477 my $imraw = Imager::ImgRaw::new(10, 20, 3);
478 {
479 is(Imager::i_psamp($imraw, 0, 2, undef, [ 255, 128, 64 ]), 3,
480 "i_psamp def channels, 3 samples");
481 is_color3(Imager::i_get_pixel($imraw, 0, 2), 255, 128, 64,
482 "check color written");
483 Imager::i_img_setmask($imraw, 5);
484 is(Imager::i_psamp($imraw, 1, 3, undef, [ 64, 128, 192 ]), 3,
485 "i_psamp def channels, 3 samples, masked");
486 is_color3(Imager::i_get_pixel($imraw, 1, 3), 64, 0, 192,
487 "check color written");
488 is(Imager::i_psamp($imraw, 1, 7, [ 0, 1, 2 ], [ 64, 128, 192 ]), 3,
489 "i_psamp channels listed, 3 samples, masked");
490 is_color3(Imager::i_get_pixel($imraw, 1, 7), 64, 0, 192,
491 "check color written");
492 Imager::i_img_setmask($imraw, ~0);
493 is(Imager::i_psamp($imraw, 2, 4, [ 0, 1 ], [ 255, 128, 64, 32 ]), 4,
494 "i_psamp channels [0, 1], 4 samples");
495 is_color3(Imager::i_get_pixel($imraw, 2, 4), 255, 128, 0,
496 "check first color written");
497 is_color3(Imager::i_get_pixel($imraw, 3, 4), 64, 32, 0,
498 "check second color written");
499 is(Imager::i_psamp($imraw, 0, 5, [ 0, 1, 2 ], [ (128, 63, 32) x 10 ]), 30,
500 "write a full row");
501 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 5, [ 0, 1, 2 ]) ],
502 [ (128, 63, 32) x 10 ],
503 "check full row");
504 is(Imager::i_psamp($imraw, 8, 8, [ 0, 1, 2 ],
505 [ 255, 128, 32, 64, 32, 16, 32, 16, 8 ]),
506 6, "i_psamp channels [0, 1, 2], 9 samples, but room for 6");
507 is(Imager::i_psamp($imraw, 4, 6, undef, [ 0 .. 18 ], 1), 18,
508 "psamp with offset");
509 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 6, undef) ],
510 [ (0) x 12, 1 .. 18 ],
511 "check result");
512 is(Imager::i_psamp($imraw, 4, 11, undef, [ 0 .. 18 ], 1, 3), 9,
513 "psamp with offset and width");
514 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 11, undef) ],
515 [ (0) x 12, 1 .. 9, (0) x 9 ],
516 "check result");
517 }
518 { # errors we catch
519 is(Imager::i_psamp($imraw, 6, 8, [ 0, 1, 3 ], [ 255, 128, 32 ]),
520 undef, "i_psamp channels [0, 1, 3], 3 samples (invalid channel number)");
521 is(_get_error(), "No channel 3 in this image",
522 "check error message");
523 is(Imager::i_psamp($imraw, 6, 8, [ 0, 1, -1 ], [ 255, 128, 32 ]),
524 undef, "i_psamp channels [0, 1, -1], 3 samples (invalid channel number)");
525 is(_get_error(), "No channel -1 in this image",
526 "check error message");
527 is(Imager::i_psamp($imraw, 0, -1, undef, [ 0, 0, 0 ]), undef,
528 "negative y");
529 is(_get_error(), $psamp_outside_error,
530 "check error message");
531 is(Imager::i_psamp($imraw, 0, 20, undef, [ 0, 0, 0 ]), undef,
532 "y overflow");
533 is(_get_error(), $psamp_outside_error,
534 "check error message");
535 is(Imager::i_psamp($imraw, -1, 0, undef, [ 0, 0, 0 ]), undef,
536 "negative x");
537 is(_get_error(), $psamp_outside_error,
538 "check error message");
539 is(Imager::i_psamp($imraw, 10, 0, undef, [ 0, 0, 0 ]), undef,
540 "x overflow");
541 is(_get_error(), $psamp_outside_error,
542 "check error message");
543 }
544 { # test the im_sample_list typemap
545 ok(!eval { Imager::i_psamp($imraw, 9, 9, [ 0 ], undef); 1 },
546 "pass undef as the sample list");
547 like($@, qr/data must be a scalar or an arrayref/,
548 "check message");
549 ok(!eval { Imager::i_psamp($imraw, 9, 9, [ 0 ], { a => 1 }); 1 },
550 "hashref as the sample list");
551 like($@, qr/data must be a scalar or an arrayref/,
552 "check message");
553 ok(!eval { Imager::i_psamp($imraw, 9, 9, [ 0 ], []); 1 },
554 "empty sample list");
555 like($@, qr/i_psamp: no samples provided in data/,
556 "check message");
557 ok(!eval { Imager::i_psamp($imraw, 9, 9, [ 0 ], ""); 1 },
558 "empty scalar sample list");
559 like($@, qr/i_psamp: no samples provided in data/,
560 "check message");
561
562 # not the typemap
563 is(Imager::i_psamp($imraw, 0, 8, undef, [ (0) x 3 ], -1), undef,
564 "negative offset");
565 is(_get_error(), "offset must be non-negative",
566 "check message");
567
568 is(Imager::i_psamp($imraw, 0, 8, undef, [ (0) x 3 ], 4), undef,
569 "too high offset");
570 is(_get_error(), "offset greater than number of samples supplied",
571 "check message");
572 }
573 print "# end psamp tests\n";
574 }
575
576 { # psampf
577 print "# psampf\n";
578 my $imraw = Imager::ImgRaw::new(10, 20, 3);
579 {
580 is(Imager::i_psampf($imraw, 0, 2, undef, [ 1, 0.5, 0.25 ]), 3,
581 "i_psampf def channels, 3 samples");
582 is_color3(Imager::i_get_pixel($imraw, 0, 2), 255, 128, 64,
583 "check color written");
584 Imager::i_img_setmask($imraw, 5);
585 is(Imager::i_psampf($imraw, 1, 3, undef, [ 0.25, 0.5, 0.75 ]), 3,
586 "i_psampf def channels, 3 samples, masked");
587 is_color3(Imager::i_get_pixel($imraw, 1, 3), 64, 0, 191,
588 "check color written");
589 is(Imager::i_psampf($imraw, 1, 7, [ 0, 1, 2 ], [ 0.25, 0.5, 0.75 ]), 3,
590 "i_psampf channels listed, 3 samples, masked");
591 is_color3(Imager::i_get_pixel($imraw, 1, 7), 64, 0, 191,
592 "check color written");
593 Imager::i_img_setmask($imraw, ~0);
594 is(Imager::i_psampf($imraw, 2, 4, [ 0, 1 ], [ 1, 0.5, 0.25, 0.125 ]), 4,
595 "i_psampf channels [0, 1], 4 samples");
596 is_color3(Imager::i_get_pixel($imraw, 2, 4), 255, 128, 0,
597 "check first color written");
598 is_color3(Imager::i_get_pixel($imraw, 3, 4), 64, 32, 0,
599 "check second color written");
600 is(Imager::i_psampf($imraw, 0, 5, [ 0, 1, 2 ], [ (0.5, 0.25, 0.125) x 10 ]), 30,
601 "write a full row");
602 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 5, [ 0, 1, 2 ]) ],
603 [ (128, 64, 32) x 10 ],
604 "check full row");
605 is(Imager::i_psampf($imraw, 8, 8, [ 0, 1, 2 ],
606 [ 1.0, 0.5, 0.125, 0.25, 0.125, 0.0625, 0.125, 0, 1 ]),
607 6, "i_psampf channels [0, 1, 2], 9 samples, but room for 6");
608 is(Imager::i_psampf($imraw, 4, 6, undef, [ map $_/254.9, 0 .. 18 ], 1), 18,
609 "psampf with offset");
610 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 6, undef) ],
611 [ (0) x 12, 1 .. 18 ],
612 "check result");
613 is(Imager::i_psampf($imraw, 4, 11, undef, [ map $_/254.9, 0 .. 18 ], 1, 3), 9,
614 "psampf with offset and width");
615 is_deeply([ Imager::i_gsamp($imraw, 0, 10, 11, undef) ],
616 [ (0) x 12, 1 .. 9, (0) x 9 ],
617 "check result");
618 }
619 { # errors we catch
620 is(Imager::i_psampf($imraw, 6, 8, [ 0, 1, 3 ], [ 1, 0.5, 0.125 ]),
621 undef, "i_psampf channels [0, 1, 3], 3 samples (invalid channel number)");
622 is(_get_error(), "No channel 3 in this image",
623 "check error message");
624 is(Imager::i_psampf($imraw, 6, 8, [ 0, 1, -1 ], [ 1, 0.5, 0.125 ]),
625 undef, "i_psampf channels [0, 1, -1], 3 samples (invalid channel number)");
626 is(_get_error(), "No channel -1 in this image",
627 "check error message");
628 is(Imager::i_psampf($imraw, 0, -1, undef, [ 0, 0, 0 ]), undef,
629 "negative y");
630 is(_get_error(), $psamp_outside_error,
631 "check error message");
632 is(Imager::i_psampf($imraw, 0, 20, undef, [ 0, 0, 0 ]), undef,
633 "y overflow");
634 is(_get_error(), $psamp_outside_error,
635 "check error message");
636 is(Imager::i_psampf($imraw, -1, 0, undef, [ 0, 0, 0 ]), undef,
637 "negative x");
638 is(_get_error(), $psamp_outside_error,
639 "check error message");
640 is(Imager::i_psampf($imraw, 10, 0, undef, [ 0, 0, 0 ]), undef,
641 "x overflow");
642 is(_get_error(), $psamp_outside_error,
643 "check error message");
644 }
645 { # test the im_fsample_list typemap
646 ok(!eval { Imager::i_psampf($imraw, 9, 9, [ 0 ], undef); 1 },
647 "pass undef as the sample list");
648 like($@, qr/data must be a scalar or an arrayref/,
649 "check message");
650 ok(!eval { Imager::i_psampf($imraw, 9, 9, [ 0 ], { a => 1 }); 1 },
651 "hashref as the sample list");
652 like($@, qr/data must be a scalar or an arrayref/,
653 "check message");
654 ok(!eval { Imager::i_psampf($imraw, 9, 9, [ 0 ], []); 1 },
655 "empty sample list");
656 like($@, qr/i_psampf: no samples provided in data/,
657 "check message");
658 ok(!eval { Imager::i_psampf($imraw, 9, 9, [ 0 ], ""); 1 },
659 "empty scalar sample list");
660 like($@, qr/i_psampf: no samples provided in data/,
661 "check message");
662
663 # not the typemap
664 is(Imager::i_psampf($imraw, 0, 8, undef, [ (0) x 3 ], -1), undef,
665 "negative offset");
666 is(_get_error(), "offset must be non-negative",
667 "check message");
668
669 is(Imager::i_psampf($imraw, 0, 8, undef, [ (0) x 3 ], 4), undef,
670 "too high offset");
671 is(_get_error(), "offset greater than number of samples supplied",
672 "check message");
673 }
674 print "# end psampf tests\n";
675 }
676
677 {
678 print "# OO level scanline function tests\n";
679 my $im = Imager->new(xsize=>10, ysize=>10, channels=>4);
680 $im->setpixel(color=>$red, 'x'=>5, 'y'=>0);
681 ok(!$im->getscanline(), "getscanline() - supply nothing, get nothing");
682 is($im->errstr, "missing y parameter", "check message");
683 is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0) ],
684 [ ([ 0,0,0,0]) x 5, [ 255, 0, 0, 255 ], ([ 0,0,0,0]) x 4 ],
685 "getscanline, list context, default x, width");
686 is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0, 'x'=>3) ],
687 [ ([0,0,0,0]) x 2, [ 255, 0, 0, 255 ], ([0,0,0,0]) x 4 ],
688 "getscanline, list context, default width");
689 is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0, 'x'=>4, width=>4) ],
690 [ [0,0,0,0], [ 255, 0, 0, 255 ], ([0,0,0,0]) x 2 ],
691 "getscanline, list context, no defaults");
692 is(uc unpack("H*", $im->getscanline('y'=>0)),
693 "00000000" x 5 . "FF0000FF" . "00000000" x 4,
694 "getscanline, scalar context, default x, width");
695 is_deeply([ map [ $_->rgba ],
696 $im->getscanline('y'=>0, 'x'=>4, width=>4, type=>'float') ],
697 [ [0,0,0,0], [ 1.0, 0, 0, 1.0 ], ([0,0,0,0]) x 2 ],
698 "getscanline float, list context, no defaults");
699 is_deeply([ unpack "d*",
700 $im->getscanline('y'=>0, 'x'=>4, width=>4, type=>'float') ],
701 [ (0,0,0,0), ( 1.0, 0, 0, 1.0 ), (0,0,0,0) x 2 ],
702 "getscanline float, scalar context, no defaults");
703
704 ok(!$im->getscanline('y'=>0, type=>'invalid'),
705 "check invalid type checking");
706 like($im->errstr, qr/invalid type parameter/,
707 "check message for invalid type");
708
709 my @plin_colors = (($black) x 4, $red, $blue, ($green) x 4);
710 is($im->setscanline('y'=>1, pixels=>\@plin_colors), 10,
711 "setscanline - arrayref, default x");
712 is_deeply([ map [ $_->rgba ], @plin_colors ],
713 [ map [ $_->rgba ], $im->getscanline('y'=>1) ],
714 "check colors were written");
715
716 my @plin_colors2 = ( $green, $red, $blue, $red );
717 is($im->setscanline('y'=>2, 'x'=>3, pixels=>\@plin_colors2), 4,
718 "setscanline - arrayref");
719
720 # using map instead of x here due to a bug in some versions of Test::More
721 # fixed in the latest Test::More
722 is_deeply([ ( map [ 0,0,0,0 ], 1..3), (map [ $_->rgba ], @plin_colors2),
723 ( map [ 0,0,0,0 ], 1..3) ],
724 [ map [ $_->rgba ], $im->getscanline('y'=>2) ],
725 "check write to middle of line");
726
727 my $raw_colors = pack "H*", "FF00FFFF"."FF0000FF"."FFFFFFFF";
728 is($im->setscanline('y'=>3, 'x'=>2, pixels=>$raw_colors), 3,
729 "setscanline - scalar, default raw type")
730 or print "# ",$im->errstr,"\n";
731 is(uc unpack("H*", $im->getscanline('y'=>3, 'x'=>1, 'width'=>5)),
732 "00000000".uc(unpack "H*", $raw_colors)."00000000",
733 "check write");
734
735 # float colors
736 my @fcolors = ( $f_red, $f_blue, $f_black, $f_green );
737 is($im->setscanline('y'=>4, 'x'=>3, pixels=>\@fcolors), 4,
738 "setscanline - float arrayref");
739 is_deeply([ map [ $_->rgba ], @fcolors ],
740 [ map [ $_->rgba ], $im->getscanline('y'=>4, 'x'=>3, width=>4, type=>'float') ],
741 "check write");
742 # packed
743 my $packed_fcolors = pack "d*", map $_->rgba, @fcolors;
744 is($im->setscanline('y'=>5, 'x'=>4, pixels=>$packed_fcolors, type=>'float'), 4,
745 "setscanline - float scalar");
746 is_deeply([ map [ $_->rgba ], @fcolors ],
747 [ map [ $_->rgba ], $im->getscanline('y'=>5, 'x'=>4, width=>4, type=>'float') ],
748 "check write");
749
750 # get samples
751 is_deeply([ $im->getsamples('y'=>1, channels=>[ 0 ]) ],
752 [ map +($_->rgba)[0], @plin_colors ],
753 "get channel 0, list context, default x, width");
754 is_deeply([ unpack "C*", $im->getsamples('y'=>1, channels=>[0, 2]) ],
755 [ map { ($_->rgba)[0, 2] } @plin_colors ],
756 "get channel 0, 1, scalar context");
757 is_deeply([ $im->getsamples('y'=>4, 'x'=>3, width=>4, type=>'float',
758 channels=>[1,3]) ],
759 [ map { ($_->rgba)[1,3] } @fcolors ],
760 "get channels 1,3, list context, float samples");
761 is_deeply([ unpack "d*",
762 $im->getsamples('y'=>4, 'x'=>3, width=>4,
763 type=>'float', channels=>[3,2,1,0]) ],
764 [ map { ($_->rgba)[3,2,1,0] } @fcolors ],
765 "get channels 3..0 as scalar, float samples");
766
767 print "# end OO level scanline function tests\n";
768 }
769
770 { # RT 74882
771 # for the non-gsamp_bits case with a target parameter it was
772 # treating the target parameter as a hashref
773 {
774 my $im = Imager->new(xsize => 10, ysize => 10);
775 my $c1 = NC(0, 63, 255);
776 my $c2 = NC(255, 128, 255);
777 is($im->setscanline(y => 1, pixels => [ ( $c1, $c2 ) x 5 ]),
778 10, "set some test data")
779 or diag "setscanline: ", $im->errstr;
780 my @target;
781 is($im->getsamples(y => 1, x => 1, target => \@target, width => 3),
782 9, "getsamples to target");
783 is_deeply(\@target, [ 255, 128, 255, 0, 63, 255, 255, 128, 255 ],
784 "check result");
785 }
786 {
787 my $im = Imager->new(xsize => 10, ysize => 10, bits => "double");
788 my $c1 = NCF(0, 0.25, 1.0);
789 my $c2 = NCF(1.0, 0.5, 1.0);
790 is($im->setscanline(y => 1, pixels => [ ( $c1, $c2 ) x 5 ]),
791 10, "set some test data")
792 or diag "setscanline: ", $im->errstr;
793 my @target;
794 is($im->getsamples(y => 1, x => 1, target => \@target, width => 3, type => "float"),
795 9, "getsamples to target");
796 is_deeply(\@target, [ 1.0, 0.5, 1.0, 0, 0.25, 1.0, 1.0, 0.5, 1.0 ],
797 "check result");
798 }
799 }
800
801 { # to avoid confusion, i_glin/i_glinf modified to return 0 in unused
802 # channels at the perl level
803 my $im = Imager->new(xsize => 4, ysize => 4, channels => 2);
804 my $fill = Imager::Color->new(128, 255, 0, 0);
805 ok($im->box(filled => 1, color => $fill), 'fill it up');
806 my $data = $im->getscanline('y' => 0);
807 is(unpack("H*", $data), "80ff000080ff000080ff000080ff0000",
808 "check we get zeros");
809 my @colors = $im->getscanline('y' => 0);
810 is_color4($colors[0], 128, 255, 0, 0, "check object interface[0]");
811 is_color4($colors[1], 128, 255, 0, 0, "check object interface[1]");
812 is_color4($colors[2], 128, 255, 0, 0, "check object interface[2]");
813 is_color4($colors[3], 128, 255, 0, 0, "check object interface[3]");
814
815 my $dataf = $im->getscanline('y' => 0, type => 'float');
816 # the extra pack/unpack is to force double precision rather than long
817 # double, otherwise the test fails
818 is_deeply([ unpack("d*", $dataf) ],
819 [ unpack("d*", pack("d*", ( 128.0 / 255.0, 1.0, 0, 0, ) x 4)) ],
820 "check we get zeroes (double)");
821 my @fcolors = $im->getscanline('y' => 0, type => 'float');
822 is_fcolor4($fcolors[0], 128.0/255.0, 1.0, 0, 0, "check object interface[0]");
823 is_fcolor4($fcolors[1], 128.0/255.0, 1.0, 0, 0, "check object interface[1]");
824 is_fcolor4($fcolors[2], 128.0/255.0, 1.0, 0, 0, "check object interface[2]");
825 is_fcolor4($fcolors[3], 128.0/255.0, 1.0, 0, 0, "check object interface[3]");
826 }
827
828 { # check the channel mask function
829
830 my $im = Imager->new(xsize => 10, ysize=>10, bits=>8);
831 mask_tests($im, 0.005);
832 }
833
834 { # check bounds checking
835 my $im = Imager->new(xsize => 10, ysize => 10);
836
837 image_bounds_checks($im);
838 }
839
840 { # setsamples() interface to psamp()
841 my $im = Imager->new(xsize => 10, ysize => 10);
842 is($im->setsamples(y => 1, x => 2, data => [ 1 .. 6 ]), 6,
843 "simple put (array), default channels");
844 is_deeply([ $im->getsamples(y => 1, x => 0) ],
845 [ (0) x 6, 1 .. 6, (0) x 18 ], "check they were stored");
846 is($im->setsamples(y => 3, x => 3, data => pack("C*", 2 .. 10 )), 9,
847 "simple put (scalar), default channels")
848 or diag $im->errstr;
849 is_deeply([ $im->getsamples(y => 3, x => 0) ],
850 [ (0) x 9, 2 .. 10, (0) x 12 ], "check they were stored");
851 is($im->setsamples(y => 4, x => 4, data => [ map $_ / 254.5, 1 .. 6 ], type => 'float'),
852 6, "simple put (float array), default channels");
853 is_deeply([ $im->getsamples(y => 4, x => 0) ],
854 [ (0) x 12, 1 .. 6, (0) x 12 ], "check they were stored");
855
856 is($im->setsamples(y => 5, x => 3, data => pack("d*", map $_ / 254.5, 1 .. 6), type => 'float'),
857 6, "simple put (float scalar), default channels");
858 is_deeply([ $im->getsamples(y => 5, x => 0) ],
859 [ (0) x 9, 1 .. 6, (0) x 15 ], "check they were stored");
860
861 is($im->setsamples(y => 7, x => 3, data => [ 0 .. 18 ], offset => 1), 18,
862 "setsamples offset");
863 is_deeply([ $im->getsamples(y => 7) ],
864 [ (0) x 9, 1 .. 18, (0) x 3 ],
865 "check result");
866
867 is($im->setsamples(y => 8, x => 3, data => [ map $_ / 254.9, 0 .. 18 ],
868 offset => 1, type => 'float'),
869 18, "setsamples offset (float)");
870 is_deeply([ $im->getsamples(y => 8) ],
871 [ (0) x 9, 1 .. 18, (0) x 3 ],
872 "check result");
873
874 is_deeply([ $im->setsamples(y => 6, x => 10, data => [ (0) x 3 ]) ],
875 [], "check out of range result (8bit)");
876 is($im->errstr, $psamp_outside_error, "check error message");
877
878 is_deeply([ $im->setsamples(y => 6, x => 10, data => [ (0) x 3 ], type => "float") ],
879 [], "check out of range result (float)");
880 is($im->errstr, $psamp_outside_error, "check error message");
881
882 is_deeply([ $im->setsamples(y => 6, x => 2, channels => [0, 1, 3 ],
883 data => [ (0) x 3 ]) ],
884 [], "check bad channels (8bit)");
885 is($im->errstr, "No channel 3 in this image",
886 "check error message");
887
888 is_deeply([ $im->setsamples(y => 6, x => 2, channels => [0, 1, 3 ],
889 data => [ (0) x 3 ], type => "float") ],
890 [], "check bad channels (float)");
891 is($im->errstr, "No channel 3 in this image",
892 "check error message");
893
894 is($im->setsamples(y => 5, data => [ (0) x 3 ], type => "bad"),
895 undef, "setsamples with bad type");
896 is($im->errstr, "setsamples: type parameter invalid",
897 "check error message");
898 is($im->setsamples(y => 5),
899 undef, "setsamples with no data");
900 is($im->errstr, "setsamples: data parameter missing",
901 "check error message");
902
903 is($im->setsamples(y => 5, data => undef),
904 undef, "setsamples with undef data");
905 is($im->errstr, "setsamples: data parameter not defined",
906 "check error message");
907
908 my $imempty = Imager->new;
909 is($imempty->setsamples(y => 0, data => [ (0) x 3 ]), undef,
910 "setsamples to empty image");
911 is($imempty->errstr, "setsamples: empty input image",
912 "check error message");
913 }
914
915 { # getpixel parameters
916 my $im = Imager->new(xsize => 10, ysize => 10);
917 $im->box(filled => 1, xmax => 4, color => NC(255, 0, 0));
918 $im->box(filled => 1, xmin => 5, ymax => 4, color => NC(0, 255, 255));
919 $im->box(filled => 1, xmin => 5, ymin => 5, color => NC(255, 0, 255));
920 { # error handling
921 my $empty = Imager->new;
922 ok(!$empty->getpixel(x => 0, y => 0), "getpixel empty image");
923 is($empty->errstr, "getpixel: empty input image", "check message");
924
925 ok(!$im->getpixel(y => 0), "missing x");
926 is($im->errstr, "getpixel: missing x or y parameter", "check message");
927
928 $im->_set_error("something different");
929 ok(!$im->getpixel(x => 0), "missing y");
930 is($im->errstr, "getpixel: missing x or y parameter", "check message");
931
932 ok(!$im->getpixel(x => [], y => 0), "empty x array ref");
933 is($im->errstr, "getpixel: x is a reference to an empty array",
934 "check message");
935
936 ok(!$im->getpixel(x => 0, y => []), "empty y array ref");
937 is($im->errstr, "getpixel: y is a reference to an empty array",
938 "check message");
939
940 ok(!$im->getpixel(x => 0, y => 0, type => "bad"), "bad type (scalar path)");
941 is($im->errstr, "getpixel: type must be '8bit' or 'float'",
942 "check message");
943
944 $im->_set_error("something different");
945 ok(!$im->getpixel(x => [ 0 ], y => [ 0 ], type => "bad"),
946 "bad type (array path)");
947 is($im->errstr, "getpixel: type must be '8bit' or 'float'",
948 "check message");
949 }
950
951 # simple calls
952 is_color4($im->getpixel(x => 1, y => 0), 255, 0, 0, 0,
953 "getpixel(1, 0)");
954 is_color4($im->getpixel(x => 8, y => 1), 0, 255, 255, 0,
955 "getpixel(8, 1)");
956 is_color4($im->getpixel(x => 8, y => 7), 255, 0, 255, 0,
957 "getpixel(8, 7)");
958
959 {
960 # simple arrayrefs
961 my @colors = $im->getpixel(x => [ 0, 8, 7 ], y => [ 0, 7, 3 ]);
962 is(@colors, 3, "getpixel 2 3 element array refs");
963 is_color3($colors[0], 255, 0, 0, "check first color");
964 is_color3($colors[1], 255, 0, 255, "check second color");
965 is_color3($colors[2], 0, 255, 255, "check third color");
966 }
967
968 # array and scalar
969 {
970 my @colors = $im->getpixel(x => 5, y => [ 4, 5, 9 ]);
971 is(@colors, 3, "getpixel x scalar, y arrayref of 3");
972 is_color3($colors[0], 0, 255, 255, "check first color");
973 is_color3($colors[1], 255, 0, 255, "check second color");
974 is_color3($colors[2], 255, 0, 255, "check third color");
975 }
976
977 {
978 my @colors = $im->getpixel(x => [ 0, 4, 5 ], y => 2);
979 is(@colors, 3, "getpixel y scalar, x arrayref of 3");
980 is_color3($colors[0], 255, 0, 0, "check first color");
981 is_color3($colors[1], 255, 0, 0, "check second color");
982 is_color3($colors[2], 0, 255, 255, "check third color");
983 }
984
985 { # float
986 is_fcolor3($im->getpixel(x => 1, y => 0, type => 'float'),
987 1.0, 0, 0, "getpixel(1,0) float");
988 is_fcolor4($im->getpixel(x => 8, y => 1, type => 'float'),
989 0, 1.0, 1.0, 0, "getpixel(8,1) float");
990 is_fcolor4($im->getpixel(x => 8, y => 7, type => 'float'),
991 1.0, 0, 1.0, 0, "getpixel(8,7) float");
992
993 my @colors = $im->getpixel(x => [ 0, 8, 7 ], y => [ 0, 7, 3 ], type => 'float');
994 is(@colors, 3, "getpixel 2 3 element array refs (float)");
995 is_fcolor3($colors[0], 1, 0, 0, "check first color");
996 is_fcolor3($colors[1], 1, 0, 1, "check second color");
997 is_fcolor3($colors[2], 0, 1, 1, "check third color");
998 }
999
1000 { # out of bounds
1001 my @colors = $im->getpixel(x => [ 0, -1, 5, 10 ], y => 0);
1002 is(@colors, 4, "should be 4 entries")
1003 or diag $im->errstr;
1004 is_color3($colors[0], 255, 0, 0, "first red");
1005 is($colors[1], undef, "second undef");
1006 is_color3($colors[2], 0, 255, 255, "third cyan");
1007 is($colors[3], undef, "fourth undef");
1008 }
1009
1010 { # out of bounds
1011 my @colors = $im->getpixel(x => [ 0, -1, 5, 10 ], y => 0, type => "float");
1012 is(@colors, 4, "should be 4 entries")
1013 or diag $im->errstr;
1014 is_fcolor3($colors[0], 1.0, 0, 0, "first red");
1015 is($colors[1], undef, "second undef");
1016 is_fcolor3($colors[2], 0, 1.0, 1.0, "third cyan");
1017 is($colors[3], undef, "fourth undef");
1018 }
1019 }
1020
1021 { # setpixel
1022 my $im = Imager->new(xsize => 10, ysize => 10);
1023 { # errors
1024 my $empty = Imager->new;
1025 is_deeply([ $empty->setpixel(x => 0, y => 0, color => $red) ], [],
1026 "setpixel on empty image");
1027 is($empty->errstr, "setpixel: empty input image", "check message");
1028
1029 is_deeply([ $im->setpixel(y => 0, color => $red) ], [], "missing x");
1030 is($im->errstr, "setpixel: missing x or y parameter", "check message");
1031
1032 $im->_set_error("something different");
1033 is_deeply([$im->setpixel(x => 0, color => $red)], [], "missing y");
1034 is($im->errstr, "setpixel: missing x or y parameter", "check message");
1035
1036 is_deeply([ $im->setpixel(x => [], y => 0, color => $red) ], [],
1037 "empty x array ref");
1038 is($im->errstr, "setpixel: x is a reference to an empty array",
1039 "check message");
1040
1041 is_deeply([$im->setpixel(x => 0, y => [], color => $red)], [],
1042 "empty y array ref");
1043 is($im->errstr, "setpixel: y is a reference to an empty array",
1044 "check message");
1045
1046 is_deeply([ $im->setpixel(x => 0, y => 0, color => "not really a color") ], [],
1047 "color not a color");
1048 is($im->errstr, "setpixel: No color named not really a color found",
1049 "check message");
1050 }
1051
1052 # simple set
1053 is($im->setpixel(x => 0, y => 0, color => $red), 1,
1054 "simple setpixel")
1055 or diag "simple set float: ", $im->errstr;
1056 is_color3($im->getpixel(x => 0, y => 0), 255, 0, 0, "check stored pixel");
1057
1058 is($im->setpixel(x => 1, y => 2, color => $f_red), 1,
1059 "simple setpixel (float)")
1060 or diag "simple set float: ", $im->errstr;
1061 is_color3($im->getpixel(x => 1, y => 2), 255, 0, 0, "check stored pixel");
1062
1063 is($im->setpixel(x => -1, y => 0, color => $red), "0 but true",
1064 "simple setpixel outside of image");
1065 is($im->setpixel(x => 0, y => -1, color => $f_red), "0 but true",
1066 "simple setpixel (float) outside of image");
1067
1068 # simple arrayrefs
1069 is($im->setpixel( x => [ 0, 8, 7 ], y => [ 0, 7, 3 ], color => $blue),
1070 3, "setpixel with 3 element array refs");
1071 my @colors = $im->getpixel(x => [ 8, 7, 0 ], y => [ 7, 3, 0 ]);
1072 is_color3($colors[0], 0, 0, 255, "check first color");
1073 is_color3($colors[1], 0, 0, 255, "check second color");
1074 is_color3($colors[2], 0, 0, 255, "check third color");
1075
1076 # array and scalar
1077 {
1078 is($im->setpixel(x => 5, y => [ 4, 5, 9 ], color => $green), 3,
1079 "setpixel with x scalar, y arrayref of 3");
1080 my @colors = $im->getpixel(x => [ 5, 5, 5 ], y => [ 4, 5, 9 ]);
1081 is_color3($colors[0], 0, 255, 0, "check first color");
1082 is_color3($colors[1], 0, 255, 0, "check second color");
1083 is_color3($colors[2], 0, 255, 0, "check third color");
1084 }
1085
1086 {
1087 is($im->setpixel(x => [ 0, 4, 5 ], y => 2, color => $blue), 3,
1088 "setpixel with y scalar, x arrayref of 3");
1089 my @colors = $im->getpixel(x => [ 0, 4, 5 ], y => [ 2, 2, 2 ]);
1090 is_color3($colors[0], 0, 0, 255, "check first color");
1091 is_color3($colors[1], 0, 0, 255, "check second color");
1092 is_color3($colors[2], 0, 0, 255, "check third color");
1093 }
1094
1095 {
1096 is($im->setpixel(x => [ 0, -1, 10, 5, 0 ], y => [ 0, 1, 2, 3, 1 ], color => $blue), 3,
1097 "set array with two bad locations")
1098 or diag "set array bad locations: ", $im->errstr;
1099 my @colors = $im->getpixel(x => [ 0, 5, 0 ], y => [ 0, 3, 1 ]);
1100 is_color3($colors[0], 0, 0, 255, "check first color");
1101 is_color3($colors[1], 0, 0, 255, "check second color");
1102 is_color3($colors[2], 0, 0, 255, "check third color");
1103 }
1104 {
1105 is($im->setpixel(x => [ 0, -1, 10, 5, 0 ], y => [ 0, 1, 2, 3, 1 ], color => $f_green), 3,
1106 "set array with two bad locations (float)")
1107 or diag "set array bad locations (float): ", $im->errstr;
1108 my @colors = $im->getpixel(x => [ 0, 5, 0 ], y => [ 0, 3, 1 ]);
1109 is_color3($colors[0], 0, 255, 0, "check first color");
1110 is_color3($colors[1], 0, 255, 0, "check second color");
1111 is_color3($colors[2], 0, 255, 0, "check third color");
1112 }
1113 { # default color
1114 is($im->setpixel(x => 0, y => 9), 1, "setpixel() default color")
1115 or diag "setpixel default color: ", $im->errstr;
1116 is_color3($im->getpixel(x => 0, y => 9), 255, 255, 255,
1117 "check color set");
1118 }
1119 }
1120
1121 {
1122 my $empty = Imager->new;
1123 ok(!$empty->addtag(name => "foo", value => 1),
1124 "can't addtag on an empty image");
1125 is($empty->errstr, "addtag: empty input image",
1126 "check error message");
1127 ok(!$empty->settag(name => "foo", value => 1),
1128 "can't settag on an empty image");
1129 is($empty->errstr, "settag: empty input image",
1130 "check error message");
1131 ok(!$empty->deltag(name => "foo"), "can't deltag on an empty image");
1132 is($empty->errstr, "deltag: empty input image",
1133 "check error message");
1134 ok(!$empty->tags(name => "foo"), "can't tags on an empty image");
1135 is($empty->errstr, "tags: empty input image",
1136 "check error message");
1137 }
1138
1139 SKIP:
1140 {
1141 skip "No detailed warning registration before 5.014", 1
1142 if $] < 5.014;
1143 # warnings should be enabled
1144 my @warn;
1145 local $SIG{__WARN__} = sub { push @warn, "@_"; };
1146 my $im = Imager->new(xsize => 1, ysize => 1);
1147 $im->settag(code => 10, value => 10);
1148 is(scalar @warn, 1, "settag with code warns");
1149 like($warn[0], qr/settag: code parameter is deprecated/,
1150 "check message for settag");
1151 @warn = ();
1152 {
1153 no if $] >= 5.014, warnings => 'Imager::tagcodes';
1154 $im->settag(code => 10, value => 10);
1155 }
1156 is(scalar @warn, 0, "settag with code with warning disabled doesn't warn");
1157
1158 @warn = ();
1159 $im->addtag(code => 11, value => 11);
1160 is(scalar @warn, 1, "addtag with code warns");
1161 like($warn[0], qr/addtag: code parameter is deprecated/,
1162 "check message for addtag");
1163
1164 @warn = ();
1165 {
1166 no if $] >= 5.014, warnings => 'Imager::tagcodes';
1167 $im->addtag(code => 12, value => 12);
1168 }
1169 is(scalar @warn, 0, "addtag with code with warning disabled doesn't warn");
1170
1171 {
1172 my @tag_tests =
1173 (
1174 [ intmax => INT_MAX + 0 ],
1175 [ uintmax => UINT_MAX + 0 ],
1176 [ intmin => INT_MIN + 0 ],
1177 [ intmaxp1 => 1+INT_MAX ],
1178 [ uintmaxp1 => 1+UINT_MAX ],
1179 [ neg1 => -1 ],
1180 [ zero => 0 ],
1181 [ zerozero => "00" ],
1182 [ zeroone => "01" ],
1183 [ negzero => "-0" ],
1184 [ negzone => "-01" ],
1185 [ arabone => "\x{661}", "no unicode yet" ],
1186 );
1187 for my $test (@tag_tests) {
1188 my ($name, $val, $todo) = @$test;
1189 ok($im->addtag(name => $name, value => $val),
1190 "add $name = $val");
1191 my $got = $im->tags(name => $name);
1192 local $TODO = $todo;
1193 is($got, $val, "$name added correctly");
1194 }
1195 }
1196
1197
1198 # setmask
1199 @warn = ();
1200 $im->setmask(mask => 0xFF);
1201 is(scalar @warn, 1, "warned on setmask");
1202 like($warn[0], qr/setmask: image channel masks are deprecated/,
1203 "check setmask warning message");
1204 @warn = ();
1205 {
1206 no if $] >= 5.014, warnings => 'Imager::channelmask';
1207 $im->setmask(mask => 0xFF);
1208 }
1209 is(scalar @warn, 0, "setmask with warning disabled doesn't warn");
1210 }
1211
1212 {
1213 my @tests =
1214 (
1215 [ "gray", 1, undef ],
1216 [ "graya", 1, 1 ],
1217 [ "rgb", 3, undef ],
1218 [ "rgba", 3, 3 ],
1219 );
1220 for my $test (@tests) {
1221 my ($model, $color_channels, $alpha) = @$test;
1222 my $im = Imager->new(model => $model, xsize => 10, ysize => 10)
1223 or die "Cannot create $model image:", Imager->errstr;
1224 ok($im, "make a $model image via model");
1225 is($im->colormodel, $model, "check colormodel is $model");
1226 is($im->alphachannel, $alpha, "check alphachannel");
1227 is($im->colorchannels, $color_channels, "check colorchannels");
1228 }
1229 }
1230
1231 done_testing();
1232
1233 Imager->close_log();
1234
1235 unless ($ENV{IMAGER_KEEP_FILES}) {
1236 unlink "testout/t01introvert.log";
1237 }
1238
1239 sub check_add {
1240 my ($im, $color, $expected) = @_;
1241 my $index = Imager::i_addcolors($im, $color);
1242 ok($index, "got index");
1243 print "# $index\n";
1244 is(0+$index, $expected, "index matched expected");
1245 my ($new) = Imager::i_getcolors($im, $index);
1246 ok($new, "got the color");
1247 ok(color_cmp($new, $color) == 0, "color matched what was added");
1248
1249 $index;
1250 }
1251
1252 # sub array_ncmp {
1253 # my ($a1, $a2) = @_;
1254 # my $len = @$a1 < @$a2 ? @$a1 : @$a2;
1255 # for my $i (0..$len-1) {
1256 # my $diff = $a1->[$i] <=> $a2->[$i]
1257 # and return $diff;
1258 # }
1259 # return @$a1 <=> @$a2;
1260 # }
1261
1262 sub dump_colors {
1263 for my $col (@_) {
1264 print "# ", map(sprintf("%02X", $_), ($col->rgba)[0..2]),"\n";
1265 }
1266 }
1267
1268 sub _get_error {
1269 my @errors = Imager::i_errors();
1270 return join(": ", map $_->[0], @errors);
1271 }