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