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;
37 if (!length $value || ($value & ~$value)) {
38 $value =~ s/\\/\\\\/g;
43 $value =~ s/([^ -\x7E])/"\\x".sprintf("%02x", ord($1))/ge;
48 return $value; # a number
59 sub test_colorf_gpix {
60 my ($im, $x, $y, $expected, $epsilon) = @_;
61 my $c = Imager::i_gpixf($im, $x, $y);
62 ok($c, "got gpix ($x, $y)");
63 unless (ok(colorf_cmp($c, $expected, $epsilon) == 0,
64 "got right color ($x, $y)")) {
65 print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
66 print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
71 my ($im, $x, $y, $expected) = @_;
72 my $c = Imager::i_get_pixel($im, $x, $y);
73 ok($c, "got gpix ($x, $y)");
74 unless (ok(color_cmp($c, $expected) == 0,
75 "got right color ($x, $y)")) {
76 print "# got: (", join(",", ($c->rgba)[0,1,2]), ")\n";
77 print "# expected: (", join(",", ($expected->rgba)[0,1,2]), ")\n";
81 sub test_colorf_glin {
82 my ($im, $x, $y, @pels) = @_;
84 my @got = Imager::i_glinf($im, $x, $x+@pels, $y);
85 is(@got, @pels, "check number of pixels ($x, $y)");
86 ok(!grep(colorf_cmp($pels[$_], $got[$_], 0.005), 0..$#got),
87 "check colors ($x, $y)");
91 my ($c1, $c2, $epsilon) = @_;
93 defined $epsilon or $epsilon = 0;
98 # print "# (",join(",", @s1[0..2]),") <=> (",join(",", @s2[0..2]),")\n";
99 return abs($s1[0]-$s2[0]) >= $epsilon && $s1[0] <=> $s2[0]
100 || abs($s1[1]-$s2[1]) >= $epsilon && $s1[1] <=> $s2[1]
101 || abs($s1[2]-$s2[2]) >= $epsilon && $s1[2] <=> $s2[2];
110 return $s1[0] <=> $s2[0]
112 || $s1[2] <=> $s2[2];
115 # these test the action of the channel mask on the image supplied
116 # which should be an OO image.
118 my ($im, $epsilon) = @_;
120 defined $epsilon or $epsilon = 0;
122 # we want to check all four of ppix() and plin(), ppix() and plinf()
123 # basic test procedure:
124 # first using default/all 1s mask, set to white
125 # make sure we got white
126 # set mask to skip a channel, set to grey
127 # make sure only the right channels set
129 print "# channel mask tests\n";
131 my $white = NC(255, 255, 255);
132 my $grey = NC(128, 128, 128);
133 my $white_grey = NC(128, 255, 128);
135 print "# with ppix\n";
136 ok($im->setmask(mask=>~0), "set to default mask");
137 ok($im->setpixel(x=>0, 'y'=>0, color=>$white), "set to white all channels");
138 test_color_gpix($im->{IMG}, 0, 0, $white);
139 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
140 ok($im->setpixel(x=>0, 'y'=>0, color=>$grey), "set to grey, no channel 2");
141 test_color_gpix($im->{IMG}, 0, 0, $white_grey);
143 print "# with plin\n";
144 ok($im->setmask(mask=>~0), "set to default mask");
145 ok($im->setscanline(x=>0, 'y'=>1, pixels => [$white]),
146 "set to white all channels");
147 test_color_gpix($im->{IMG}, 0, 1, $white);
148 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
149 ok($im->setscanline(x=>0, 'y'=>1, pixels=>[$grey]),
150 "set to grey, no channel 2");
151 test_color_gpix($im->{IMG}, 0, 1, $white_grey);
154 my $whitef = NCF(1.0, 1.0, 1.0);
155 my $greyf = NCF(0.5, 0.5, 0.5);
156 my $white_greyf = NCF(0.5, 1.0, 0.5);
158 print "# with ppixf\n";
159 ok($im->setmask(mask=>~0), "set to default mask");
160 ok($im->setpixel(x=>0, 'y'=>2, color=>$whitef), "set to white all channels");
161 test_colorf_gpix($im->{IMG}, 0, 2, $whitef, $epsilon);
162 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
163 ok($im->setpixel(x=>0, 'y'=>2, color=>$greyf), "set to grey, no channel 2");
164 test_colorf_gpix($im->{IMG}, 0, 2, $white_greyf, $epsilon);
166 print "# with plinf\n";
167 ok($im->setmask(mask=>~0), "set to default mask");
168 ok($im->setscanline(x=>0, 'y'=>3, pixels => [$whitef]),
169 "set to white all channels");
170 test_colorf_gpix($im->{IMG}, 0, 3, $whitef, $epsilon);
171 ok($im->setmask(mask=>0xF-0x2), "set channel to exclude channel1");
172 ok($im->setscanline(x=>0, 'y'=>3, pixels=>[$greyf]),
173 "set to grey, no channel 2");
174 test_colorf_gpix($im->{IMG}, 0, 3, $white_greyf, $epsilon);
179 return Imager::Color::Float->new(@_);