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..71\n"; }
12 END {print "not ok 1\n" unless $loaded;}
13 use Imager qw(:handy :all);
17 init_log("testout/t01introvert.log",1);
19 my $im_g = Imager::ImgRaw::new(100, 101, 1);
21 print Imager::i_img_getchannels($im_g) == 1
22 ? "ok 2\n" : "not ok 2 # 1 channel image channel count mismatch\n";
23 print Imager::i_img_getmask($im_g) & 1
24 ? "ok 3\n" : "not ok 3 # 1 channel image bad mask\n";
25 print Imager::i_img_virtual($im_g)
26 ? "not ok 4 # 1 channel image thinks it is virtual\n" : "ok 4\n";
27 print Imager::i_img_bits($im_g) == 8
28 ? "ok 5\n" : "not ok 5 # 1 channel image has bits != 8\n";
29 print Imager::i_img_type($im_g) == 0 # direct
30 ? "ok 6\n" : "not ok 6 # 1 channel image isn't direct\n";
32 my @ginfo = Imager::i_img_info($im_g);
33 print $ginfo[0] == 100
34 ? "ok 7\n" : "not ok 7 # 1 channel image width incorrect\n";
35 print $ginfo[1] == 101
36 ? "ok 8\n" : "not ok 8 # 1 channel image height incorrect\n";
38 undef $im_g; # can we check for release after this somehow?
40 my $im_rgb = Imager::ImgRaw::new(100, 101, 3);
42 print Imager::i_img_getchannels($im_rgb) == 3
43 ? "ok 9\n" : "not ok 9 # 3 channel image channel count mismatch\n";
44 print +(Imager::i_img_getmask($im_rgb) & 7) == 7
45 ? "ok 10\n" : "not ok 10 # 3 channel image bad mask\n";
46 print Imager::i_img_bits($im_rgb) == 8
47 ? "ok 11\n" : "not ok 11 # 3 channel image has bits != 8\n";
48 print Imager::i_img_type($im_rgb) == 0 # direct
49 ? "ok 12\n" : "not ok 12 # 3 channel image isn't direct\n";
53 my $im_pal = Imager::i_img_pal_new(100, 101, 3, 256);
55 print $im_pal ? "ok 13\n" : "not ok 13 # couldn't make paletted image\n";
56 print Imager::i_img_getchannels($im_pal) == 3
57 ? "ok 14\n" : "not ok 14 # pal img channel count mismatch\n";
58 print Imager::i_img_bits($im_pal) == 8
59 ? "ok 15\n" : "not ok 15 # pal img bits != 8\n";
60 print Imager::i_img_type($im_pal) == 1
61 ? "ok 16\n" : "not ok 16 # pal img isn't paletted\n";
63 my $red = NC(255, 0, 0);
64 my $green = NC(0, 255, 0);
65 my $blue = NC(0, 0, 255);
67 my $red_idx = check_add(17, $im_pal, $red, 0);
68 my $green_idx = check_add(21, $im_pal, $green, 1);
69 my $blue_idx = check_add(25, $im_pal, $blue, 2);
71 # basic writing of palette indicies
73 Imager::i_ppal($im_pal, 0, 0, ($red_idx) x 100) == 100
77 Imager::i_ppal($im_pal, 50, 0, ($blue_idx) x 50) == 50
81 # make sure we get it back
82 my @pals = Imager::i_gpal($im_pal, 0, 100, 0);
83 grep($_ != $red_idx, @pals[0..49]) and print "not ";
85 grep($_ != $blue_idx, @pals[50..99]) and print "not ";
87 Imager::i_gpal($im_pal, 0, 100, 0) eq "\0" x 50 . "\2" x 50 or print "not ";
89 my @samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
90 @samp == 300 or print "not ";
92 my @samp_exp = ((255, 0, 0) x 50, (0, 0, 255) x 50);
93 my $diff = array_ncmp(\@samp, \@samp_exp);
94 $diff == 0 or print "not ";
96 my $samp = Imager::i_gsamp($im_pal, 0, 100, 0, 0, 1, 2);
97 length($samp) == 300 or print "not ";
99 $samp eq "\xFF\0\0" x 50 . "\0\0\xFF" x 50
103 # reading indicies as colors
104 my $c_red = Imager::i_get_pixel($im_pal, 0, 0)
107 color_cmp($red, $c_red) == 0
110 my $c_blue = Imager::i_get_pixel($im_pal, 50, 0)
113 color_cmp($blue, $c_blue) == 0
117 # drawing with colors
118 Imager::i_ppix($im_pal, 0, 0, $green) and print "not ";
120 # that was in the palette, should still be paletted
121 print Imager::i_img_type($im_pal) == 1
122 ? "ok 43\n" : "not ok 43 # pal img isn't paletted (but still should be)\n";
124 my $c_green = Imager::i_get_pixel($im_pal, 0, 0)
127 color_cmp($green, $c_green) == 0
131 Imager::i_colorcount($im_pal) == 3 or print "not ";
133 Imager::i_findcolor($im_pal, $green) == 1 or print "not ";
136 my $black = NC(0, 0, 0);
137 # this should convert the image to RGB
138 Imager::i_ppix($im_pal, 1, 0, $black) and print "not ";
140 print Imager::i_img_type($im_pal) == 0
141 ? "ok 49\n" : "not ok 49 # pal img shouldn't be paletted now\n";
145 colors => [$red, $green, $blue, $black],
148 my $im_pal2 = Imager::i_img_to_pal($im_pal, \%quant);
149 $im_pal2 or print "not ";
151 @{$quant{colors}} == 4 or print "not ";
153 Imager::i_gsamp($im_pal2, 0, 100, 0, 0, 1, 2)
154 eq "\0\xFF\0\0\0\0"."\xFF\0\0" x 48 . "\0\0\xFF" x 50
158 # test the OO interfaces
159 my $impal2 = Imager->new(type=>'pseudo', xsize=>200, ysize=>201)
162 $impal2->getchannels == 3 or print "not ";
164 $impal2->bits == 8 or print "not ";
166 $impal2->type eq 'paletted' or print "not ";
170 my $red_idx = $impal2->addcolors(colors=>[$red])
173 $red_idx == 0 or print "not ";
175 my $blue_idx = $impal2->addcolors(colors=>[$blue, $green])
178 $blue_idx == 1 or print "not ";
180 my $green_idx = $blue_idx + 1;
181 my $c = $impal2->getcolors(start=>$green_idx);
182 color_cmp($green, $c) == 0 or print "not ";
184 my @cols = $impal2->getcolors;
185 @cols == 3 or print "not ";
187 my @exp = ( $red, $blue, $green );
189 if (color_cmp($cols[$i], $exp[$i])) {
195 $impal2->colorcount == 3 or print "not ";
197 $impal2->maxcolors == 256 or print "not ";
199 $impal2->findcolor(color=>$blue) == 1 or print "not ";
201 $impal2->setcolors(start=>0, colors=>[ $blue, $red ]) or print "not ";
204 # make an rgb version
205 my $imrgb2 = $impal2->to_rgb8();
206 $imrgb2->type eq 'direct' or print "not ";
209 # and back again, specifying the palette
210 my @colors = ( $red, $blue, $green );
211 my $impal3 = $imrgb2->to_paletted(colors=>\@colors,
213 translate=>'closest')
216 dump_colors(@colors);
217 print "# in image\n";
218 dump_colors($impal3->getcolors);
219 $impal3->colorcount == 3 or print "not ";
221 $impal3->type eq 'paletted' or print "not ";
226 my ($base, $im, $color, $expected) = @_;
227 my $index = Imager::i_addcolors($im, $color)
229 print "ok ",$base++,"\n";
233 print "ok ",$base++,"\n";
234 my ($new) = Imager::i_getcolors($im, $index)
236 print "ok ",$base++,"\n";
237 color_cmp($new, $color) == 0
239 print "ok ",$base++,"\n";
248 return $l[0] <=> $r[0]
255 my $len = @$a1 < @$a2 ? @$a1 : @$a2;
256 for my $i (0..$len-1) {
257 my $diff = $a1->[$i] <=> $a2->[$i]
260 return @$a1 <=> @$a2;
265 print "# ", map(sprintf("%02X", $_), ($col->rgba)[0..2]),"\n";