2 # t/t01introvert.t - tests internals of image formats
3 # to make sure we get expected values
5 # Change 1..1 below to 1..last_test_to_print .
6 # (It may become useful if the test is moved to ./t subdirectory.)
11 BEGIN { $| = 1; print "1..93\n"; }
12 END {print "not ok 1\n" unless $loaded;}
13 use Imager qw(:handy :all);
17 require "t/testtools.pl";
19 init_log("testout/t01introvert.log",1);
21 my $im_g = Imager::ImgRaw::new(100, 101, 1);
23 print Imager::i_img_getchannels($im_g) == 1
24 ? "ok 2\n" : "not ok 2 # 1 channel image channel count mismatch\n";
25 print Imager::i_img_getmask($im_g) & 1
26 ? "ok 3\n" : "not ok 3 # 1 channel image bad mask\n";
27 print Imager::i_img_virtual($im_g)
28 ? "not ok 4 # 1 channel image thinks it is virtual\n" : "ok 4\n";
29 print Imager::i_img_bits($im_g) == 8
30 ? "ok 5\n" : "not ok 5 # 1 channel image has bits != 8\n";
31 print Imager::i_img_type($im_g) == 0 # direct
32 ? "ok 6\n" : "not ok 6 # 1 channel image isn't direct\n";
34 my @ginfo = Imager::i_img_info($im_g);
35 print $ginfo[0] == 100
36 ? "ok 7\n" : "not ok 7 # 1 channel image width incorrect\n";
37 print $ginfo[1] == 101
38 ? "ok 8\n" : "not ok 8 # 1 channel image height incorrect\n";
40 undef $im_g; # can we check for release after this somehow?
42 my $im_rgb = Imager::ImgRaw::new(100, 101, 3);
44 print Imager::i_img_getchannels($im_rgb) == 3
45 ? "ok 9\n" : "not ok 9 # 3 channel image channel count mismatch\n";
46 print +(Imager::i_img_getmask($im_rgb) & 7) == 7
47 ? "ok 10\n" : "not ok 10 # 3 channel image bad mask\n";
48 print Imager::i_img_bits($im_rgb) == 8
49 ? "ok 11\n" : "not ok 11 # 3 channel image has bits != 8\n";
50 print Imager::i_img_type($im_rgb) == 0 # direct
51 ? "ok 12\n" : "not ok 12 # 3 channel image isn't direct\n";
55 my $im_pal = Imager::i_img_pal_new(100, 101, 3, 256);
57 print $im_pal ? "ok 13\n" : "not ok 13 # couldn't make paletted image\n";
58 print Imager::i_img_getchannels($im_pal) == 3
59 ? "ok 14\n" : "not ok 14 # pal img channel count mismatch\n";
60 print Imager::i_img_bits($im_pal) == 8
61 ? "ok 15\n" : "not ok 15 # pal img bits != 8\n";
62 print Imager::i_img_type($im_pal) == 1
63 ? "ok 16\n" : "not ok 16 # pal img isn't paletted\n";
65 my $red = NC(255, 0, 0);
66 my $green = NC(0, 255, 0);
67 my $blue = NC(0, 0, 255);
69 my $red_idx = check_add(17, $im_pal, $red, 0);
70 my $green_idx = check_add(21, $im_pal, $green, 1);
71 my $blue_idx = check_add(25, $im_pal, $blue, 2);
73 # basic writing of palette indicies
75 Imager::i_ppal($im_pal, 0, 0, ($red_idx) x 100) == 100
79 Imager::i_ppal($im_pal, 50, 0, ($blue_idx) x 50) == 50
83 # make sure we get it back
84 my @pals = Imager::i_gpal($im_pal, 0, 100, 0);
85 grep($_ != $red_idx, @pals[0..49]) and print "not ";
87 grep($_ != $blue_idx, @pals[50..99]) and print "not ";
89 Imager::i_gpal($im_pal, 0, 100, 0) eq "\0" x 50 . "\2" x 50 or print "not ";
91 my @samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
92 @samp == 300 or print "not ";
94 my @samp_exp = ((255, 0, 0) x 50, (0, 0, 255) x 50);
95 my $diff = array_ncmp(\@samp, \@samp_exp);
96 $diff == 0 or print "not ";
98 my $samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
99 length($samp) == 300 or print "not ";
101 $samp eq "\xFF\0\0" x 50 . "\0\0\xFF" x 50
105 # reading indicies as colors
106 my $c_red = Imager::i_get_pixel($im_pal, 0, 0)
109 color_cmp($red, $c_red) == 0
112 my $c_blue = Imager::i_get_pixel($im_pal, 50, 0)
115 color_cmp($blue, $c_blue) == 0
119 # drawing with colors
120 Imager::i_ppix($im_pal, 0, 0, $green) and print "not ";
122 # that was in the palette, should still be paletted
123 print Imager::i_img_type($im_pal) == 1
124 ? "ok 43\n" : "not ok 43 # pal img isn't paletted (but still should be)\n";
126 my $c_green = Imager::i_get_pixel($im_pal, 0, 0)
129 color_cmp($green, $c_green) == 0
133 Imager::i_colorcount($im_pal) == 3 or print "not ";
135 Imager::i_findcolor($im_pal, $green) == 1 or print "not ";
138 my $black = NC(0, 0, 0);
139 # this should convert the image to RGB
140 Imager::i_ppix($im_pal, 1, 0, $black) and print "not ";
142 print Imager::i_img_type($im_pal) == 0
143 ? "ok 49\n" : "not ok 49 # pal img shouldn't be paletted now\n";
147 colors => [$red, $green, $blue, $black],
150 my $im_pal2 = Imager::i_img_to_pal($im_pal, \%quant);
151 $im_pal2 or print "not ";
153 @{$quant{colors}} == 4 or print "not ";
155 Imager::i_gsamp($im_pal2, 0, 100, 0, 0, 1, 2)
156 eq "\0\xFF\0\0\0\0"."\xFF\0\0" x 48 . "\0\0\xFF" x 50
160 # test the OO interfaces
161 my $impal2 = Imager->new(type=>'pseudo', xsize=>200, ysize=>201)
164 $impal2->getchannels == 3 or print "not ";
166 $impal2->bits == 8 or print "not ";
168 $impal2->type eq 'paletted' or print "not ";
172 my $red_idx = $impal2->addcolors(colors=>[$red])
175 $red_idx == 0 or print "not ";
177 my $blue_idx = $impal2->addcolors(colors=>[$blue, $green])
180 $blue_idx == 1 or print "not ";
182 my $green_idx = $blue_idx + 1;
183 my $c = $impal2->getcolors(start=>$green_idx);
184 color_cmp($green, $c) == 0 or print "not ";
186 my @cols = $impal2->getcolors;
187 @cols == 3 or print "not ";
189 my @exp = ( $red, $blue, $green );
191 if (color_cmp($cols[$i], $exp[$i])) {
197 $impal2->colorcount == 3 or print "not ";
199 $impal2->maxcolors == 256 or print "not ";
201 $impal2->findcolor(color=>$blue) == 1 or print "not ";
203 $impal2->setcolors(start=>0, colors=>[ $blue, $red ]) or print "not ";
206 # make an rgb version
207 my $imrgb2 = $impal2->to_rgb8();
208 $imrgb2->type eq 'direct' or print "not ";
211 # and back again, specifying the palette
212 my @colors = ( $red, $blue, $green );
213 my $impal3 = $imrgb2->to_paletted(colors=>\@colors,
215 translate=>'closest')
218 dump_colors(@colors);
219 print "# in image\n";
220 dump_colors($impal3->getcolors);
221 $impal3->colorcount == 3 or print "not ";
223 $impal3->type eq 'paletted' or print "not ";
228 okn($num++, !Imager->new(xsize=>0, ysize=>1), "fail to create 0 height image");
229 matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
230 "0 height error message check");
231 okn($num++, !Imager->new(xsize=>1, ysize=>0), "fail to create 0 width image");
232 matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
233 "0 width error message check");
234 okn($num++, !Imager->new(xsize=>-1, ysize=>1), "fail to create -ve height image");
235 matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
236 "-ve width error message check");
237 okn($num++, !Imager->new(xsize=>1, ysize=>-1), "fail to create -ve width image");
238 matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
239 "-ve height error message check");
240 okn($num++, !Imager->new(xsize=>-1, ysize=>-1), "fail to create -ve width/height image");
241 matchn($num++, Imager->errstr, qr/Image sizes must be positive/,
242 "-ve width/height error message check");
244 okn($num++, !Imager->new(xsize=>1, ysize=>1, channels=>0),
245 "fail to create a zero channel image");
246 matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
247 "out of range channel message check");
248 okn($num++, !Imager->new(xsize=>1, ysize=>1, channels=>5),
249 "fail to create a five channel image");
250 matchn($num++, Imager->errstr, qr/channels must be between 1 and 4/,
251 "out of range channel message check");
254 # https://rt.cpan.org/Ticket/Display.html?id=8213
255 # check for handling of memory allocation of very large images
256 # only test this on 32-bit machines - on a 64-bit machine it may
257 # result in trying to allocate 4Gb of memory, which is unfriendly at
258 # least and may result in running out of memory, causing a different
261 if ($Config{intsize} == 4) {
262 my $uint_range = 256 ** $Config{intsize};
263 print "# range $uint_range\n";
264 my $dim1 = int(sqrt($uint_range))+1;
266 my $im_b = Imager->new(xsize=>$dim1, ysize=>$dim1, channels=>1);
267 isn($num++, $im_b, undef, "integer overflow check - 1 channel");
269 $im_b = Imager->new(xisze=>$dim1, ysize=>1, channels=>1);
270 okn($num++, $im_b, "but same width ok");
271 $im_b = Imager->new(xisze=>1, ysize=>$dim1, channels=>1);
272 okn($num++, $im_b, "but same height ok");
273 matchn($num++, Imager->errstr, qr/integer overflow/,
274 "check the error message");
276 # do a similar test with a 3 channel image, so we're sure we catch
277 # the same case where the third dimension causes the overflow
278 my $dim3 = int(sqrt($uint_range / 3))+1;
280 $im_b = Imager->new(xsize=>$dim3, ysize=>$dim3, channels=>3);
281 isn($num++, $im_b, undef, "integer overflow check - 3 channel");
283 $im_b = Imager->new(xisze=>$dim3, ysize=>1, channels=>3);
284 okn($num++, $im_b, "but same width ok");
285 $im_b = Imager->new(xisze=>1, ysize=>$dim3, channels=>3);
286 okn($num++, $im_b, "but same height ok");
288 matchn($num++, Imager->errstr, qr/integer overflow/,
289 "check the error message");
292 skipn($num, 8, "don't want to allocate 4Gb");
298 my ($base, $im, $color, $expected) = @_;
299 my $index = Imager::i_addcolors($im, $color)
301 print "ok ",$base++,"\n";
305 print "ok ",$base++,"\n";
306 my ($new) = Imager::i_getcolors($im, $index)
308 print "ok ",$base++,"\n";
309 color_cmp($new, $color) == 0
311 print "ok ",$base++,"\n";
320 return $l[0] <=> $r[0]
327 my $len = @$a1 < @$a2 ? @$a1 : @$a2;
328 for my $i (0..$len-1) {
329 my $diff = $a1->[$i] <=> $a2->[$i]
332 return @$a1 <=> @$a2;
337 print "# ", map(sprintf("%02X", $_), ($col->rgba)[0..2]),"\n";