]> git.imager.perl.org - imager.git/blob - t/t01introvert.t
77e863f853c4ce8c7706d88d512794e2955e498e
[imager.git] / t / t01introvert.t
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 tests => 230;
7
8 BEGIN { use_ok(Imager => qw(:handy :all)) }
9
10 use Imager::Test qw(image_bounds_checks is_color3 is_color4 is_fcolor4 color_cmp mask_tests);
11
12 -d "testout" or mkdir "testout";
13
14 init_log("testout/t01introvert.log",1);
15
16 my $im_g = Imager::ImgRaw::new(100, 101, 1);
17
18 my $red = NC(255, 0, 0);
19 my $green = NC(0, 255, 0);
20 my $blue = NC(0, 0, 255);
21
22 use Imager::Color::Float;
23 my $f_black = Imager::Color::Float->new(0, 0, 0);
24 my $f_red = Imager::Color::Float->new(1.0, 0, 0);
25 my $f_green = Imager::Color::Float->new(0, 1.0, 0);
26 my $f_blue = Imager::Color::Float->new(0, 0, 1.0);
27
28 is(Imager::i_img_getchannels($im_g), 1, "1 channel image channel count");
29 ok(Imager::i_img_getmask($im_g) & 1, "1 channel image mask");
30 ok(!Imager::i_img_virtual($im_g), "1 channel image not virtual");
31 is(Imager::i_img_bits($im_g), 8, "1 channel image has 8 bits/sample");
32 is(Imager::i_img_type($im_g), 0, "1 channel image is direct");
33 is(Imager::i_img_get_width($im_g), 100, "100 pixels wide");
34 is(Imager::i_img_get_height($im_g), 101, "101 pixels high");
35
36 my @ginfo = Imager::i_img_info($im_g);
37 is($ginfo[0], 100, "1 channel image width");
38 is($ginfo[1], 101, "1 channel image height");
39
40 undef $im_g; # can we check for release after this somehow?
41
42 my $im_rgb = Imager::ImgRaw::new(100, 101, 3);
43
44 is(Imager::i_img_getchannels($im_rgb), 3, "3 channel image channel count");
45 is((Imager::i_img_getmask($im_rgb) & 7), 7, "3 channel image mask");
46 is(Imager::i_img_bits($im_rgb), 8, "3 channel image has 8 bits/sample");
47 is(Imager::i_img_type($im_rgb), 0, "3 channel image is direct");
48
49 undef $im_rgb;
50
51 my $im_pal = Imager::i_img_pal_new(100, 101, 3, 256);
52
53 ok($im_pal, "make paletted image");
54 is(Imager::i_img_getchannels($im_pal), 3, "pal img channel count");
55 is(Imager::i_img_bits($im_pal), 8, "pal img bits");
56 is(Imager::i_img_type($im_pal), 1, "pal img is paletted");
57
58 my $red_idx = check_add($im_pal, $red, 0);
59 my $green_idx = check_add($im_pal, $green, 1);
60 my $blue_idx = check_add($im_pal, $blue, 2);
61
62 # basic writing of palette indicies
63 # fill with red
64 is(Imager::i_ppal($im_pal, 0, 0, ($red_idx) x 100), 100, 
65    "write red 100 times");
66 # and blue
67 is(Imager::i_ppal($im_pal, 50, 0, ($blue_idx) x 50), 50,
68    "write blue 50 times");
69
70 # make sure we get it back
71 my @pals = Imager::i_gpal($im_pal, 0, 100, 0);
72 ok(!grep($_ != $red_idx, @pals[0..49]), "check for red");
73 ok(!grep($_ != $blue_idx, @pals[50..99]), "check for blue");
74 is(Imager::i_gpal($im_pal, 0, 100, 0), "\0" x 50 . "\2" x 50, 
75    "gpal in scalar context");
76 my @samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
77 is(@samp, 300, "gsamp count in list context");
78 my @samp_exp = ((255, 0, 0) x 50, (0, 0, 255) x 50);
79 is_deeply(\@samp, \@samp_exp, "gsamp list deep compare");
80 my $samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
81 is(length($samp), 300, "gsamp scalar length");
82 is($samp, "\xFF\0\0" x 50 . "\0\0\xFF" x 50, "gsamp scalar bytes");
83
84 # reading indicies as colors
85 my $c_red = Imager::i_get_pixel($im_pal, 0, 0);
86 ok($c_red, "got the red pixel");
87 is_color3($c_red, 255, 0, 0, "and it's red");
88 my $c_blue = Imager::i_get_pixel($im_pal, 50, 0);
89 ok($c_blue, "got the blue pixel");
90 is_color3($c_blue, 0, 0, 255, "and it's blue");
91
92 # drawing with colors
93 ok(Imager::i_ppix($im_pal, 0, 0, $green) == 0, "draw with color in palette");
94 # that was in the palette, should still be paletted
95 is(Imager::i_img_type($im_pal), 1, "image still paletted");
96
97 my $c_green = Imager::i_get_pixel($im_pal, 0, 0);
98 ok($c_green, "got green pixel");
99 is_color3($c_green, 0, 255, 0, "and it's green");
100
101 is(Imager::i_colorcount($im_pal), 3, "still 3 colors in palette");
102 is(Imager::i_findcolor($im_pal, $green), 1, "and green is the second");
103
104 my $black = NC(0, 0, 0);
105 # this should convert the image to RGB
106 ok(Imager::i_ppix($im_pal, 1, 0, $black) == 0, "draw with black (not in palette)");
107 is(Imager::i_img_type($im_pal), 0, "pal img shouldn't be paletted now");
108
109 {
110   my %quant =
111     (
112      colors => [$red, $green, $blue, $black],
113      make_colors => 'none',
114     );
115   my $im_pal2 = Imager::i_img_to_pal($im_pal, \%quant);
116   ok($im_pal2, "got an image from quantizing");
117   is(@{$quant{colors}}, 4, "quant has the right number of colours");
118   is(Imager::i_colorcount($im_pal2), 4, "and so does the image");
119   my @colors = Imager::i_getcolors($im_pal2, 0, 4);
120   my ($first) = Imager::i_getcolors($im_pal2, 0);
121   my @first = $colors[0]->rgba;
122   is_color3($first, $first[0], $first[1], $first[2],
123            "check first color is first for multiple or single fetch");
124   is_color3($colors[0], 255, 0, 0, "still red");
125   is_color3($colors[1], 0, 255, 0, "still green");
126   is_color3($colors[2], 0, 0, 255, "still blue");
127   is_color3($colors[3], 0, 0, 0, "still black");
128   is(Imager::i_gsamp($im_pal2, 0, 100, 0, 0, 1, 2),
129      "\0\xFF\0\0\0\0"."\xFF\0\0" x 48 . "\0\0\xFF" x 50,
130      "colors are still correct");
131 }
132
133 # test the OO interfaces
134 my $impal2 = Imager->new(type=>'pseudo', xsize=>200, ysize=>201);
135 ok($impal2, "make paletted via OO");
136 is($impal2->getchannels, 3, "check channels");
137 is($impal2->bits, 8, "check bits");
138 is($impal2->type, 'paletted', "check type");
139 is($impal2->getwidth, 200, "check width");
140 is($impal2->getheight, 201, "check height");
141
142 {
143   my $red_idx = $impal2->addcolors(colors=>[$red]);
144   ok($red_idx, "add red to OO");
145   is(0+$red_idx, 0, "and it's expected index for red");
146   my $blue_idx = $impal2->addcolors(colors=>[$blue, $green]);
147   ok($blue_idx, "add blue/green via OO");
148   is($blue_idx, 1, "and it's expected index for blue");
149   my $green_idx = $blue_idx + 1;
150   my $c = $impal2->getcolors(start=>$green_idx);
151   is_color3($c, 0, 255, 0, "found green where expected");
152   my @cols = $impal2->getcolors;
153   is(@cols, 3, "got 3 colors");
154   my @exp = ( $red, $blue, $green );
155   my $good = 1;
156   for my $i (0..2) {
157     if (color_cmp($cols[$i], $exp[$i])) {
158       $good = 0;
159       last;
160     }
161   }
162   ok($good, "all colors in palette as expected");
163   is($impal2->colorcount, 3, "and colorcount returns 3");
164   is($impal2->maxcolors, 256, "maxcolors as expected");
165   is($impal2->findcolor(color=>$blue), 1, "findcolors found blue");
166   ok($impal2->setcolors(start=>0, colors=>[ $blue, $red ]),
167      "we can setcolors");
168
169   # make an rgb version
170   my $imrgb2 = $impal2->to_rgb8();
171   is($imrgb2->type, 'direct', "converted is direct");
172
173   # and back again, specifying the palette
174   my @colors = ( $red, $blue, $green );
175   my $impal3 = $imrgb2->to_paletted(colors=>\@colors,
176                                     make_colors=>'none',
177                                     translate=>'closest');
178   ok($impal3, "got a paletted image from conversion");
179   dump_colors(@colors);
180   print "# in image\n";
181   dump_colors($impal3->getcolors);
182   is($impal3->colorcount, 3, "new image has expected color table size");
183   is($impal3->type, 'paletted', "and is paletted");
184 }
185
186 { # basic checks, 8-bit direct images
187   my $im = Imager->new(xsize => 2, ysize => 3);
188   ok($im, 'create 8-bit direct image');
189   is($im->bits, 8, '8 bits');
190   ok(!$im->virtual, 'not virtual');
191   is($im->type, 'direct', 'direct image');
192   ok(!$im->is_bilevel, 'not mono');
193 }
194
195 ok(!Imager->new(xsize=>0, ysize=>1), "fail to create 0 height image");
196 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
197        "0 height error message check");
198 ok(!Imager->new(xsize=>1, ysize=>0), "fail to create 0 width image");
199 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
200        "0 width error message check");
201 ok(!Imager->new(xsize=>-1, ysize=>1), "fail to create -ve height image");
202 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
203        "-ve width error message check");
204 ok(!Imager->new(xsize=>1, ysize=>-1), "fail to create -ve width image");
205 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
206        "-ve height error message check");
207 ok(!Imager->new(xsize=>-1, ysize=>-1), "fail to create -ve width/height image");
208 cmp_ok(Imager->errstr, '=~', qr/Image sizes must be positive/,
209        "-ve width/height error message check");
210
211 ok(!Imager->new(xsize=>1, ysize=>1, channels=>0),
212    "fail to create a zero channel image");
213 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
214        "out of range channel message check");
215 ok(!Imager->new(xsize=>1, ysize=>1, channels=>5),
216    "fail to create a five channel image");
217 cmp_ok(Imager->errstr, '=~', qr/channels must be between 1 and 4/,
218        "out of range channel message check");
219
220 {
221   # https://rt.cpan.org/Ticket/Display.html?id=8213
222   # check for handling of memory allocation of very large images
223   # only test this on 32-bit machines - on a 64-bit machine it may
224   # result in trying to allocate 4Gb of memory, which is unfriendly at
225   # least and may result in running out of memory, causing a different
226   # type of exit
227  SKIP:
228   {
229     use Config;
230     skip("don't want to allocate 4Gb", 8) unless $Config{intsize} == 4;
231
232     my $uint_range = 256 ** $Config{intsize};
233     print "# range $uint_range\n";
234     my $dim1 = int(sqrt($uint_range))+1;
235     
236     my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1);
237     is($im_b, undef, "integer overflow check - 1 channel");
238     
239     $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1);
240     ok($im_b, "but same width ok");
241     $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1);
242     ok($im_b, "but same height ok");
243     cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
244            "check the error message");
245
246     # do a similar test with a 3 channel image, so we're sure we catch
247     # the same case where the third dimension causes the overflow
248     my $dim3 = int(sqrt($uint_range / 3))+1;
249     
250     $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3);
251     is($im_b, undef, "integer overflow check - 3 channel");
252     
253     $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3);
254     ok($im_b, "but same width ok");
255     $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3);
256     ok($im_b, "but same height ok");
257
258     cmp_ok(Imager->errstr, '=~', qr/integer overflow/,
259            "check the error message");
260   }
261 }
262
263 { # http://rt.cpan.org/NoAuth/Bug.html?id=9672
264   my $warning;
265   local $SIG{__WARN__} = 
266     sub { 
267       $warning = "@_";
268       my $printed = $warning;
269       $printed =~ s/\n$//;
270       $printed =~ s/\n/\n\#/g; 
271       print "# ",$printed, "\n";
272     };
273   my $img = Imager->new(xsize=>10, ysize=>10);
274   $img->to_rgb8(); # doesn't really matter what the source is
275   cmp_ok($warning, '=~', 'void', "correct warning");
276   cmp_ok($warning, '=~', 't01introvert\\.t', "correct file");
277 }
278
279 { # http://rt.cpan.org/NoAuth/Bug.html?id=11860
280   my $im = Imager->new(xsize=>2, ysize=>2);
281   $im->setpixel(x=>0, 'y'=>0, color=>$red);
282   $im->setpixel(x=>1, 'y'=>0, color=>$blue);
283
284   my @row = Imager::i_glin($im->{IMG}, 0, 2, 0);
285   is(@row, 2, "got 2 pixels from i_glin");
286   is_color3($row[0], 255, 0, 0, "red first");
287   is_color3($row[1], 0, 0, 255, "then blue");
288 }
289
290 { # general tag tests
291   
292   # we don't care much about the image itself
293   my $im = Imager::ImgRaw::new(10, 10, 1);
294
295   ok(Imager::i_tags_addn($im, 'alpha', 0, 101), "i_tags_addn(...alpha, 0, 101)");
296   ok(Imager::i_tags_addn($im, undef, 99, 102), "i_tags_addn(...undef, 99, 102)");
297   is(Imager::i_tags_count($im), 2, "should have 2 tags");
298   ok(Imager::i_tags_addn($im, undef, 99, 103), "i_tags_addn(...undef, 99, 103)");
299   is(Imager::i_tags_count($im), 3, "should have 3 tags, despite the dupe");
300   is(Imager::i_tags_find($im, 'alpha', 0), '0 but true', "find alpha");
301   is(Imager::i_tags_findn($im, 99, 0), 1, "find 99");
302   is(Imager::i_tags_findn($im, 99, 2), 2, "find 99 again");
303   is(Imager::i_tags_get($im, 0), 101, "check first");
304   is(Imager::i_tags_get($im, 1), 102, "check second");
305   is(Imager::i_tags_get($im, 2), 103, "check third");
306
307   ok(Imager::i_tags_add($im, 'beta', 0, "hello", 0), 
308      "add string with string key");
309   ok(Imager::i_tags_add($im, 'gamma', 0, "goodbye", 0),
310      "add another one");
311   ok(Imager::i_tags_add($im, undef, 199, "aloha", 0),
312      "add one keyed by number");
313   is(Imager::i_tags_find($im, 'beta', 0), 3, "find beta");
314   is(Imager::i_tags_find($im, 'gamma', 0), 4, "find gamma");
315   is(Imager::i_tags_findn($im, 199, 0), 5, "find 199");
316   ok(Imager::i_tags_delete($im, 2), "delete");
317   is(Imager::i_tags_find($im, 'beta', 0), 2, 'find beta after deletion');
318   ok(Imager::i_tags_delbyname($im, 'beta'), 'delete beta by name');
319   is(Imager::i_tags_find($im, 'beta', 0), undef, 'beta not there now');
320   is(Imager::i_tags_get_string($im, "gamma"), "goodbye", 
321      'i_tags_get_string() on a string');
322   is(Imager::i_tags_get_string($im, 99), 102, 
323      'i_tags_get_string() on a number entry');
324   ok(Imager::i_tags_delbycode($im, 99), 'delete by code');
325   is(Imager::i_tags_findn($im, 99, 0), undef, '99 not there now');
326   is(Imager::i_tags_count($im), 3, 'final count of 3');
327 }
328
329
330   print "# low-level scan line function tests\n";
331   my $im = Imager::ImgRaw::new(10, 10, 4);
332   Imager::i_ppix($im, 5, 0, $red);
333
334   # i_glin/i_glinf
335   my @colors = Imager::i_glin($im, 0, 10, 0);
336   is_deeply([ (0) x 20, (255, 0, 0, 255), (0) x 16 ], 
337             [ map $_->rgba, @colors ],
338             "i_glin - list context");
339   my $colors = Imager::i_glin($im, 0, 10, 0);
340   is("00" x 20 . "FF0000FF" . "00" x 16, 
341      uc unpack("H*", $colors), "i_glin - scalar context");
342   my @fcolors = Imager::i_glinf($im, 0, 10, 0);
343   is_deeply([ (0.0) x 20, (1.0, 0, 0, 1.0) , (0) x 16 ],
344             [ map $_->rgba, @fcolors ],
345             "i_glinf - list context");
346   my $fcolors = Imager::i_glinf($im, 0, 10, 0);
347   is_deeply([ (0.0) x 20, (1.0, 0, 0, 1.0) , (0) x 16 ],
348             [ unpack "d*", $fcolors ],
349             "i_glinf - scalar context");
350
351   # i_plin/i_plinf
352   my @plin_colors = (($black) x 4, $red, $blue, ($black) x 4);
353   is(Imager::i_plin($im, 0, 1, @plin_colors),
354      10, "i_plin - pass in a list");
355   # make sure we get it back
356   is_deeply([ map [ $_->rgba ], @plin_colors ],
357             [ map [ $_->rgba ], Imager::i_glin($im, 0, 10, 1) ],
358             "check i_plin wrote to the image");
359   my @scalar_plin = 
360     (
361      (0,0,0,0) x 4, 
362      (0, 255, 0, 255),
363      (0, 0, 255, 255), 
364      (0, 0, 0, 0) x 4,
365     );
366   is(Imager::i_plin($im, 0, 2, pack("C*", @scalar_plin)),
367      10, "i_plin - pass in a scalar");
368   is_deeply(\@scalar_plin,
369             [ map $_->rgba , Imager::i_glin($im, 0, 10, 2) ],
370             "check i_plin scalar wrote to the image");
371
372   my @plinf_colors = # Note: only 9 pixels
373     ( 
374      ($f_blue) x 4, 
375      $f_red, 
376      ($f_black) x 3, 
377      $f_black
378     );
379   is(Imager::i_plinf($im, 0, 3, @plinf_colors), 9,
380      "i_plinf - list");
381   is_deeply([ map $_->rgba, Imager::i_glinf($im, 0, 9, 3) ],
382             [ map $_->rgba, @plinf_colors ],
383             "check colors were written");
384   my @scalar_plinf =
385     (
386      ( 1.0, 1.0,   0, 1.0 ) x 3,
387      (   0, 1.0, 1.0, 1.0 ) x 2,
388      (   0,   0,   0,   0 ),
389      ( 1.0,   0, 1.0, 1.0 ),
390     );
391   is(Imager::i_plinf($im, 2, 4, pack("d*", @scalar_plinf)),
392      7, "i_plinf - scalar");
393   is_deeply(\@scalar_plinf,
394             [ map $_->rgba, Imager::i_glinf($im, 2, 9, 4) ],
395             "check colors were written");
396
397   is_deeply([ Imager::i_gsamp($im, 0, 10, 0, (0, 3)) ],
398             [ (0, 0) x 5, (255, 255), (0, 0) x 4 ],
399             "i_gsamp list context");
400   is("0000" x 5 . "FFFF" . "0000" x 4,
401      uc unpack("H*", Imager::i_gsamp($im, 0, 10, 0, (0, 3))),
402      "i_gsamp scalar context");
403   is_deeply([ Imager::i_gsampf($im, 2, 9, 4, 0, 2, 3) ],
404             [ (1.0, 0, 1.0) x 3, (0, 1.0, 1.0) x 2, (0, 0, 0),
405               (1.0, 1.0, 1.0) ], "i_gsampf - list context");
406   is_deeply([ unpack("d*", Imager::i_gsampf($im, 2, 9, 4, 0, 2, 3)) ],
407             [ (1.0, 0, 1.0) x 3, (0, 1.0, 1.0) x 2, (0, 0, 0),
408               (1.0, 1.0, 1.0) ], "i_gsampf - scalar context");
409   print "# end low-level scan-line function tests\n";
410 }
411
412 {
413   print "# OO level scanline function tests\n";
414   my $im = Imager->new(xsize=>10, ysize=>10, channels=>4);
415   $im->setpixel(color=>$red, 'x'=>5, 'y'=>0);
416   ok(!$im->getscanline(), "getscanline() - supply nothing, get nothing");
417   is($im->errstr, "missing y parameter", "check message");
418   is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0) ],
419             [ ([ 0,0,0,0]) x 5, [ 255, 0, 0, 255 ], ([ 0,0,0,0]) x 4 ],
420             "getscanline, list context, default x, width");
421   is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0, 'x'=>3) ],
422             [ ([0,0,0,0]) x 2, [ 255, 0, 0, 255 ], ([0,0,0,0]) x 4 ],
423             "getscanline, list context, default width");
424   is_deeply([ map [ $_->rgba ], $im->getscanline('y'=>0, 'x'=>4, width=>4) ],
425             [ [0,0,0,0], [ 255, 0, 0, 255 ], ([0,0,0,0]) x 2 ],
426             "getscanline, list context, no defaults");
427   is(uc unpack("H*",  $im->getscanline('y'=>0)),
428      "00000000" x 5 .  "FF0000FF" . "00000000" x 4,
429      "getscanline, scalar context, default x, width");
430   is_deeply([ map [ $_->rgba ], 
431               $im->getscanline('y'=>0, 'x'=>4, width=>4, type=>'float') ],
432             [ [0,0,0,0], [ 1.0, 0, 0, 1.0 ], ([0,0,0,0]) x 2 ],
433             "getscanline float, list context, no defaults");
434   is_deeply([ unpack "d*",
435               $im->getscanline('y'=>0, 'x'=>4, width=>4, type=>'float') ],
436             [ (0,0,0,0), ( 1.0, 0, 0, 1.0 ), (0,0,0,0) x 2 ],
437             "getscanline float, scalar context, no defaults");
438
439   ok(!$im->getscanline('y'=>0, type=>'invalid'),
440      "check invalid type checking");
441   like($im->errstr, qr/invalid type parameter/, 
442        "check message for invalid type");
443
444   my @plin_colors = (($black) x 4, $red, $blue, ($green) x 4);
445   is($im->setscanline('y'=>1, pixels=>\@plin_colors), 10,
446      "setscanline - arrayref, default x");
447   is_deeply([ map [ $_->rgba ], @plin_colors ],
448             [ map [ $_->rgba ], $im->getscanline('y'=>1) ],
449             "check colors were written");
450
451   my @plin_colors2 = ( $green, $red, $blue, $red );
452   is($im->setscanline('y'=>2, 'x'=>3, pixels=>\@plin_colors2), 4,
453      "setscanline - arrayref");
454
455   # using map instead of x here due to a bug in some versions of Test::More
456   # fixed in the latest Test::More
457   is_deeply([ ( map [ 0,0,0,0 ], 1..3), (map [ $_->rgba ], @plin_colors2),
458               ( map [ 0,0,0,0 ], 1..3) ],
459             [ map [ $_->rgba ], $im->getscanline('y'=>2) ],
460             "check write to middle of line");
461   
462   my $raw_colors = pack "H*", "FF00FFFF"."FF0000FF"."FFFFFFFF";
463   is($im->setscanline('y'=>3, 'x'=>2, pixels=>$raw_colors), 3,
464      "setscanline - scalar, default raw type")
465     or print "# ",$im->errstr,"\n";
466   is(uc unpack("H*", $im->getscanline('y'=>3, 'x'=>1, 'width'=>5)),
467      "00000000".uc(unpack "H*", $raw_colors)."00000000",
468      "check write");
469
470   # float colors
471   my @fcolors = ( $f_red, $f_blue, $f_black, $f_green );
472   is($im->setscanline('y'=>4, 'x'=>3, pixels=>\@fcolors), 4,
473      "setscanline - float arrayref");
474   is_deeply([ map [ $_->rgba ], @fcolors ],
475             [ map [ $_->rgba ], $im->getscanline('y'=>4, 'x'=>3, width=>4, type=>'float') ],
476             "check write");
477   # packed
478   my $packed_fcolors = pack "d*", map $_->rgba, @fcolors;
479   is($im->setscanline('y'=>5, 'x'=>4, pixels=>$packed_fcolors, type=>'float'), 4,
480      "setscanline - float scalar");
481   is_deeply([ map [ $_->rgba ], @fcolors ],
482             [ map [ $_->rgba ], $im->getscanline('y'=>5, 'x'=>4, width=>4, type=>'float') ],
483             "check write");
484
485   # get samples
486   is_deeply([ $im->getsamples('y'=>1, channels=>[ 0 ]) ],
487             [ map +($_->rgba)[0], @plin_colors ],
488             "get channel 0, list context, default x, width");
489   is_deeply([ unpack "C*", $im->getsamples('y'=>1, channels=>[0, 2]) ],
490             [ map { ($_->rgba)[0, 2] } @plin_colors ],
491             "get channel 0, 1, scalar context");
492   is_deeply([ $im->getsamples('y'=>4, 'x'=>3, width=>4, type=>'float',
493                               channels=>[1,3]) ],
494             [ map { ($_->rgba)[1,3] } @fcolors ],
495             "get channels 1,3, list context, float samples");
496   is_deeply([ unpack "d*", 
497               $im->getsamples('y'=>4, 'x'=>3, width=>4,
498                               type=>'float', channels=>[3,2,1,0]) ],
499             [ map { ($_->rgba)[3,2,1,0] } @fcolors ],
500             "get channels 3..0 as scalar, float samples");
501   
502   print "# end OO level scanline function tests\n";
503 }
504
505 { # to avoid confusion, i_glin/i_glinf modified to return 0 in unused
506   # channels at the perl level
507   my $im = Imager->new(xsize => 4, ysize => 4, channels => 2);
508   my $fill = Imager::Color->new(128, 255, 0, 0);
509   ok($im->box(filled => 1, color => $fill), 'fill it up');
510   my $data = $im->getscanline('y' => 0);
511   is(unpack("H*", $data), "80ff000080ff000080ff000080ff0000",
512      "check we get zeros");
513   my @colors = $im->getscanline('y' => 0);
514   is_color4($colors[0], 128, 255, 0, 0, "check object interface[0]");
515   is_color4($colors[1], 128, 255, 0, 0, "check object interface[1]");
516   is_color4($colors[2], 128, 255, 0, 0, "check object interface[2]");
517   is_color4($colors[3], 128, 255, 0, 0, "check object interface[3]");
518   
519   my $dataf = $im->getscanline('y' => 0, type => 'float');
520   # the extra pack/unpack is to force double precision rather than long
521   # double, otherwise the test fails
522   is_deeply([ unpack("d*", $dataf) ],
523             [ unpack("d*", pack("d*", ( 128.0 / 255.0, 1.0, 0, 0, ) x 4)) ],
524             "check we get zeroes (double)");
525   my @fcolors = $im->getscanline('y' => 0, type => 'float');
526   is_fcolor4($fcolors[0], 128.0/255.0, 1.0, 0, 0, "check object interface[0]");
527   is_fcolor4($fcolors[1], 128.0/255.0, 1.0, 0, 0, "check object interface[1]");
528   is_fcolor4($fcolors[2], 128.0/255.0, 1.0, 0, 0, "check object interface[2]");
529   is_fcolor4($fcolors[3], 128.0/255.0, 1.0, 0, 0, "check object interface[3]");
530 }
531
532 { # check the channel mask function
533   
534   my $im = Imager->new(xsize => 10, ysize=>10, bits=>8);
535
536   mask_tests($im, 0.005);
537 }
538
539 { # check bounds checking
540   my $im = Imager->new(xsize => 10, ysize => 10);
541
542   image_bounds_checks($im);
543 }
544
545 sub check_add {
546   my ($im, $color, $expected) = @_;
547   my $index = Imager::i_addcolors($im, $color);
548   ok($index, "got index");
549   print "# $index\n";
550   is(0+$index, $expected, "index matched expected");
551   my ($new) = Imager::i_getcolors($im, $index);
552   ok($new, "got the color");
553   ok(color_cmp($new, $color) == 0, "color matched what was added");
554
555   $index;
556 }
557
558 # sub array_ncmp {
559 #   my ($a1, $a2) = @_;
560 #   my $len = @$a1 < @$a2 ? @$a1 : @$a2;
561 #   for my $i (0..$len-1) {
562 #     my $diff = $a1->[$i] <=> $a2->[$i] 
563 #       and return $diff;
564 #   }
565 #   return @$a1 <=> @$a2;
566 # }
567
568 sub dump_colors {
569   for my $col (@_) {
570     print "# ", map(sprintf("%02X", $_), ($col->rgba)[0..2]),"\n";
571   }
572 }