1 # this doesn't need a new namespace - I hope
10 my $green=i_color_new(0,255,0,255);
11 my $blue=i_color_new(0,0,255,255);
12 my $red=i_color_new(255,0,0,255);
14 my $img=Imager::ImgRaw::new(150,150,3);
16 i_box_filled($img,70,25,130,125,$green);
17 i_box_filled($img,20,25,80,125,$blue);
18 i_arc($img,75,75,30,0,361,$red);
19 i_conv($img,[0.1, 0.2, 0.4, 0.2, 0.1]);
26 my $img = Imager->new;
33 my ($testnum, $count, $why) = @_;
35 $why = '' unless defined $why;
37 print "ok $_ # skip $why\n" for $testnum ... $testnum+$count-1;
41 my ($count, $why) = @_;
43 skipn($TESTNUM, $count, $why);
48 my ($ok, $comment) = @_;
50 return okn($TESTNUM++, $ok, $comment);
54 my ($num, $ok, $comment) = @_;
56 defined $num or confess "No \$num supplied";
57 defined $comment or confess "No \$comment supplied";
59 print "ok $num # $comment\n";
62 print "not ok $num # $comment\n";
69 my ($file, $comment) = @_;
87 my ($module, $comment, @imports) = @_;
93 $module->import(\@imports);
95 unless (okx(!$@, $comment)) {
108 my ($num, $str, $re, $comment) = @_;
110 my $match = defined($str) && $str =~ $re;
111 okn($num, $match, $comment);
113 print "# The value: ",_sv_str($str),"\n";
114 print "# did not match: qr/$re/\n";
120 my ($str, $re, $comment) = @_;
122 matchn($TESTNUM++, $str, $re, $comment);
126 my ($num, $left, $right, $comment) = @_;
129 if (!defined $left && defined $right
130 || defined $left && !defined $right) {
133 elsif (!defined $left && !defined $right) {
136 # the right of the || produces a string of \0 if $left is a PV
138 elsif (!length $left || ($left & ~$left) ||
139 !length $right || ($right & ~$right)) {
140 $match = $left eq $right;
143 $match = $left == $right;
145 okn($num, $match, $comment);
147 print "# the following two values were not equal:\n";
148 print "# value: ",_sv_str($left),"\n";
149 print "# other: ",_sv_str($right),"\n";
156 my ($left, $right, $comment) = @_;
158 isn($TESTNUM++, $left, $right, $comment);
164 if (defined $value) {
165 if (!length $value || ($value & ~$value)) {
166 $value =~ s/\\/\\\\/g;
167 $value =~ s/\r/\\r/g;
168 $value =~ s/\n/\\n/g;
169 $value =~ s/\t/\\t/g;
170 $value =~ s/\"/\\"/g;
171 $value =~ s/([^ -\x7E])/"\\x".sprintf("%02x", ord($1))/ge;
176 return $value; # a number
187 sub test_colorf_gpix {
188 my ($im, $x, $y, $expected, $epsilon) = @_;
189 my $c = Imager::i_gpixf($im, $x, $y);
190 ok($c, "got gpix ($x, $y)");
191 unless (ok(colorf_cmp($c, $expected, $epsilon) == 0,
192 "got right color ($x, $y)")) {
193 print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
194 print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
198 sub test_color_gpix {
199 my ($im, $x, $y, $expected) = @_;
200 my $c = Imager::i_get_pixel($im, $x, $y);
201 ok($c, "got gpix ($x, $y)");
202 unless (ok(color_cmp($c, $expected) == 0,
203 "got right color ($x, $y)")) {
204 print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
205 print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
209 sub test_colorf_glin {
210 my ($im, $x, $y, @pels) = @_;
212 my @got = Imager::i_glinf($im, $x, $x+@pels, $y);
213 is(@got, @pels, "check number of pixels ($x, $y)");
214 ok(!grep(colorf_cmp($pels[$_], $got[$_], 0.005), 0..$#got),
215 "check colors ($x, $y)");
219 my ($c1, $c2, $epsilon) = @_;
221 defined $epsilon or $epsilon = 0;
226 # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
227 return abs($s1[0]-$s2[0]) >= $epsilon && $s1[0] <=> $s2[0]
228 || abs($s1[1]-$s2[1]) >= $epsilon && $s1[1] <=> $s2[1]
229 || abs($s1[2]-$s2[2]) >= $epsilon && $s1[2] <=> $s2[2];
238 return $s1[0] <=> $s2[0]
240 || $s1[2] <=> $s2[2];
243 # these test the action of the channel mask on the image supplied
244 # which should be an OO image.
246 my ($im, $epsilon) = @_;
248 defined $epsilon or $epsilon = 0;
250 # we want to check all four of ppix() and plin(), ppix() and plinf()
251 # basic test procedure:
252 # first using default/all 1s mask, set to white
253 # make sure we got white
254 # set mask to skip a channel, set to grey
255 # make sure only the right channels set
257 print "# channel mask tests\n";
259 my $white = NC(255, 255, 255);
260 my $grey = NC(128, 128, 128);
261 my $white_grey = NC(128, 255, 128);
263 print "# with ppix\n";
264 ok($im->setmask(mask=>~0), "set to default mask");
265 ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
266 test_color_gpix($im->{IMG}, 0, 0, $white);
267 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
268 ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
269 test_color_gpix($im->{IMG}, 0, 0, $white_grey);
271 print "# with plin\n";
272 ok($im->setmask(mask=>~0), "set to default mask");
273 ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]),
274 "set to white all channels");
275 test_color_gpix($im->{IMG}, 0, 1, $white);
276 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
277 ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]),
278 "set to grey, no channel 2");
279 test_color_gpix($im->{IMG}, 0, 1, $white_grey);
282 my $whitef = NCF(1.0, 1.0, 1.0);
283 my $greyf = NCF(0.5, 0.5, 0.5);
284 my $white_greyf = NCF(0.5, 1.0, 0.5);
286 print "# with ppixf\n";
287 ok($im->setmask(mask=>~0), "set to default mask");
288 ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
289 test_colorf_gpix($im->{IMG}, 0, 2, $whitef, $epsilon);
290 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
291 ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
292 test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf, $epsilon);
294 print "# with plinf\n";
295 ok($im->setmask(mask=>~0), "set to default mask");
296 ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]),
297 "set to white all channels");
298 test_colorf_gpix($im->{IMG}, 0, 3, $whitef, $epsilon);
299 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
300 ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]),
301 "set to grey, no channel 2");
302 test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf, $epsilon);
307 return Imager::Color::Float->new(@_);