]> git.imager.perl.org - imager.git/blob - t/t20fill.t
0.61 release
[imager.git] / t / t20fill.t
1 #!perl -w
2 use strict;
3 use Test::More tests => 56;
4
5 use Imager ':handy';
6 use Imager::Fill;
7 use Imager::Color::Float;
8 use Config;
9
10 Imager::init_log("testout/t20fill.log", 1);
11
12 my $blue = NC(0,0,255);
13 my $red = NC(255, 0, 0);
14 my $redf = Imager::Color::Float->new(1, 0, 0);
15 my $bluef = Imager::Color::Float->new(0, 0, 1);
16 my $rsolid = Imager::i_new_fill_solid($blue, 0);
17 ok($rsolid, "building solid fill");
18 my $raw1 = Imager::ImgRaw::new(100, 100, 3);
19 # use the normal filled box
20 Imager::i_box_filled($raw1, 0, 0, 99, 99, $blue);
21 my $raw2 = Imager::ImgRaw::new(100, 100, 3);
22 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rsolid);
23 ok(1, "drawing with solid fill");
24 my $diff = Imager::i_img_diff($raw1, $raw2);
25 ok($diff == 0, "solid fill doesn't match");
26 Imager::i_box_filled($raw1, 0, 0, 99, 99, $red);
27 my $rsolid2 = Imager::i_new_fill_solidf($redf, 0);
28 ok($rsolid2, "creating float solid fill");
29 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rsolid2);
30 $diff = Imager::i_img_diff($raw1, $raw2);
31 ok($diff == 0, "float solid fill doesn't match");
32
33 # ok solid still works, let's try a hatch
34 # hash1 is a 2x2 checkerboard
35 my $rhatcha = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 0, 0);
36 my $rhatchb = Imager::i_new_fill_hatch($blue, $red, 0, 1, undef, 2, 0);
37 ok($rhatcha && $rhatchb, "can't build hatched fill");
38
39 # the offset should make these match
40 Imager::i_box_cfill($raw1, 0, 0, 99, 99, $rhatcha);
41 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
42 ok(1, "filling with hatch");
43 $diff = Imager::i_img_diff($raw1, $raw2);
44 ok($diff == 0, "hatch images different");
45 $rhatchb = Imager::i_new_fill_hatch($blue, $red, 0, 1, undef, 4, 6);
46 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
47 $diff = Imager::i_img_diff($raw1, $raw2);
48 ok($diff == 0, "hatch images different");
49
50 # I guess I was tired when I originally did this - make sure it keeps
51 # acting the way it's meant to
52 # I had originally expected these to match with the red and blue swapped
53 $rhatchb = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 2, 2);
54 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
55 $diff = Imager::i_img_diff($raw1, $raw2);
56 ok($diff == 0, "hatch images different");
57
58 # this shouldn't match
59 $rhatchb = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 1, 1);
60 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
61 $diff = Imager::i_img_diff($raw1, $raw2);
62 ok($diff, "hatch images the same!");
63
64 # custom hatch
65 # the inverse of the 2x2 checkerboard
66 my $hatch = pack("C8", 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC);
67 my $rcustom = Imager::i_new_fill_hatch($blue, $red, 0, 0, $hatch, 0, 0);
68 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rcustom);
69 $diff = Imager::i_img_diff($raw1, $raw2);
70 ok(!$diff, "custom hatch mismatch");
71
72 {
73   # basic test of floating color hatch fills
74   # this will exercise the code that the gcc shipped with OS X 10.4
75   # forgets to generate
76   # the float version is called iff we're working with a non-8-bit image
77   # i_new_fill_hatchf() makes the same object as i_new_fill_hatch() but
78   # we test the other construction code path here
79   my $fraw1 = Imager::i_img_double_new(100, 100, 3);
80   my $fhatch1 = Imager::i_new_fill_hatchf($redf, $bluef, 0, 1, undef, 0, 0);
81   ok($fraw1, "making double image 1");
82   ok($fhatch1, "making float hatch 1");
83   Imager::i_box_cfill($fraw1, 0, 0, 99, 99, $fhatch1);
84   my $fraw2 = Imager::i_img_double_new(100, 100, 3);
85   my $fhatch2 = Imager::i_new_fill_hatchf($bluef, $redf, 0, 1, undef, 0, 2);
86   ok($fraw2, "making double image 2");
87   ok($fhatch2, "making float hatch 2");
88   Imager::i_box_cfill($fraw2, 0, 0, 99, 99, $fhatch2);
89
90   $diff = Imager::i_img_diff($fraw1, $fraw2);
91   ok(!$diff, "float custom hatch mismatch");
92   save($fraw1, "testout/t20hatchf1.ppm");
93   save($fraw2, "testout/t20hatchf2.ppm");
94 }
95
96 # test the oo interface
97 my $im1 = Imager->new(xsize=>100, ysize=>100);
98 my $im2 = Imager->new(xsize=>100, ysize=>100);
99
100 my $solid = Imager::Fill->new(solid=>'#FF0000');
101 ok($solid, "creating oo solid fill");
102 ok($solid->{fill}, "bad oo solid fill");
103 $im1->box(fill=>$solid);
104 $im2->box(filled=>1, color=>$red);
105 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
106 ok(!$diff, "oo solid fill");
107
108 my $hatcha = Imager::Fill->new(hatch=>'check2x2');
109 my $hatchb = Imager::Fill->new(hatch=>'check2x2', dx=>2);
110 $im1->box(fill=>$hatcha);
111 $im2->box(fill=>$hatchb);
112 # should be different
113 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
114 ok($diff, "offset checks the same!");
115 $hatchb = Imager::Fill->new(hatch=>'check2x2', dx=>2, dy=>2);
116 $im2->box(fill=>$hatchb);
117 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
118 ok(!$diff, "offset into similar check should be the same");
119
120 # test dymanic build of fill
121 $im2->box(fill=>{hatch=>'check2x2', dx=>2, fg=>NC(255,255,255), 
122                  bg=>NC(0,0,0)});
123 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
124 ok(!$diff, "offset and flipped should be the same");
125
126 # a simple demo
127 my $im = Imager->new(xsize=>200, ysize=>200);
128
129 $im->box(xmin=>10, ymin=>10, xmax=>190, ymax=>190,
130          fill=>{ hatch=>'check4x4',
131                  fg=>NC(128, 0, 0),
132                  bg=>NC(128, 64, 0) })
133   or print "# ",$im->errstr,"\n";
134 $im->arc(r=>80, d1=>45, d2=>75, 
135            fill=>{ hatch=>'stipple2',
136                    combine=>1,
137                    fg=>[ 0, 0, 0, 255 ],
138                    bg=>{ rgba=>[255,255,255,160] } })
139   or print "# ",$im->errstr,"\n";
140 $im->arc(r=>80, d1=>75, d2=>135,
141          fill=>{ fountain=>'radial', xa=>100, ya=>100, xb=>20, yb=>100 })
142   or print "# ",$im->errstr,"\n";
143 $im->write(file=>'testout/t20_sample.ppm');
144
145 # flood fill tests
146 my $rffimg = Imager::ImgRaw::new(100, 100, 3);
147 # build a H 
148 Imager::i_box_filled($rffimg, 10, 10, 20, 90, $blue);
149 Imager::i_box_filled($rffimg, 80, 10, 90, 90, $blue);
150 Imager::i_box_filled($rffimg, 20, 45, 80, 55, $blue);
151 my $black = Imager::Color->new(0, 0, 0);
152 Imager::i_flood_fill($rffimg, 15, 15, $red);
153 my $rffcmp = Imager::ImgRaw::new(100, 100, 3);
154 # build a H 
155 Imager::i_box_filled($rffcmp, 10, 10, 20, 90, $red);
156 Imager::i_box_filled($rffcmp, 80, 10, 90, 90, $red);
157 Imager::i_box_filled($rffcmp, 20, 45, 80, 55, $red);
158 $diff = Imager::i_img_diff($rffimg, $rffcmp);
159 ok(!$diff, "flood fill difference");
160
161 my $ffim = Imager->new(xsize=>100, ysize=>100);
162 my $yellow = Imager::Color->new(255, 255, 0);
163 $ffim->box(xmin=>10, ymin=>10, xmax=>20, ymax=>90, color=>$blue, filled=>1);
164 $ffim->box(xmin=>20, ymin=>45, xmax=>80, ymax=>55, color=>$blue, filled=>1);
165 $ffim->box(xmin=>80, ymin=>10, xmax=>90, ymax=>90, color=>$blue, filled=>1);
166 ok($ffim->flood_fill('x'=>50, 'y'=>50, color=>$red), "flood fill");
167 $diff = Imager::i_img_diff($rffcmp, $ffim->{IMG});
168 ok(!$diff, "oo flood fill difference");
169 $ffim->flood_fill('x'=>50, 'y'=>50,
170                   fill=> {
171                           hatch => 'check2x2',
172                           fg => '0000FF',
173                          });
174 #                  fill=>{
175 #                         fountain=>'radial',
176 #                         xa=>50, ya=>50,
177 #                         xb=>10, yb=>10,
178 #                        });
179 $ffim->write(file=>'testout/t20_ooflood.ppm');
180
181 my $copy = $ffim->copy;
182 ok($ffim->flood_fill('x' => 50, 'y' => 50,
183                      color => $red, border => '000000'),
184    "border solid flood fill");
185 is(Imager::i_img_diff($ffim->{IMG}, $rffcmp), 0, "compare");
186 ok($ffim->flood_fill('x' => 50, 'y' => 50,
187                      fill => { hatch => 'check2x2', fg => '0000FF', },
188                      border => '000000'),
189    "border cfill fill");
190 is(Imager::i_img_diff($ffim->{IMG}, $copy->{IMG}), 0,
191    "compare");
192
193 # test combining modes
194 my $fill = NC(192, 128, 128, 128);
195 my $target = NC(64, 32, 64);
196 my %comb_tests =
197   (
198    none=>{ result=>$fill },
199    normal=>{ result=>NC(128, 80, 96) },
200    multiply => { result=>NC(56, 24, 48) },
201    dissolve => { result=>[ $target, NC(128, 80, 96) ] },
202    add => { result=>NC(159, 96, 128) },
203    subtract => { result=>NC(31, 15, 31) }, # 31.87, 15.9, 31.87
204    diff => { result=>NC(96, 64, 64) },
205    lighten => { result=>NC(128, 80, 96) },
206    darken => { result=>$target },
207    # the following results are based on the results of the tests and
208    # are suspect for that reason (and were broken at one point <sigh>)
209    # but trying to work them out manually just makes my head hurt - TC
210    hue => { result=>NC(64, 32, 47) },
211    saturation => { result=>NC(63, 37, 64) },
212    value => { result=>NC(127, 64, 128) },
213    color => { result=>NC(64, 37, 52) },
214   );
215
216 for my $comb (Imager::Fill->combines) {
217   my $test = $comb_tests{$comb};
218   my $targim = Imager->new(xsize=>1, ysize=>1);
219   $targim->box(filled=>1, color=>$target);
220   my $fillobj = Imager::Fill->new(solid=>$fill, combine=>$comb);
221   $targim->box(fill=>$fillobj);
222   my $c = Imager::i_get_pixel($targim->{IMG}, 0, 0);
223   if ($test->{result} =~ /ARRAY/) {
224     ok(scalar grep(color_close($_, $c), @{$test->{result}}), 
225        "combine '$comb'")
226       or print "# got:",join(",", $c->rgba),"  allowed: ", 
227         join("|", map { join(",", $_->rgba) } @{$test->{result}}),"\n";
228   }
229   else {
230     ok(color_close($c, $test->{result}), "combine '$comb'")
231       or print "# got: ",join(",", $c->rgba),
232         "  allowed: ",join(",", $test->{result}->rgba),"\n";
233   }
234 }
235
236 ok($ffim->arc(r=>45, color=>$blue, aa=>1), "aa circle");
237 $ffim->write(file=>"testout/t20_aacircle.ppm");
238
239 # image based fills
240 my $green = NC(0, 255, 0);
241 my $fillim = Imager->new(xsize=>40, ysize=>40, channels=>4);
242 $fillim->box(filled=>1, xmin=>5, ymin=>5, xmax=>35, ymax=>35, 
243              color=>NC(0, 0, 255, 128));
244 $fillim->arc(filled=>1, r=>10, color=>$green, aa=>1);
245 my $ooim = Imager->new(xsize=>150, ysize=>150);
246 $ooim->box(filled=>1, color=>$green, xmin=>70, ymin=>25, xmax=>130, ymax=>125);
247 $ooim->box(filled=>1, color=>$blue, xmin=>20, ymin=>25, xmax=>80, ymax=>125);
248 $ooim->arc(r=>30, color=>$red, aa=>1);
249
250 my $oocopy = $ooim->copy();
251 ok($oocopy->arc(fill=>{image=>$fillim, 
252                        combine=>'normal',
253                        xoff=>5}, r=>40),
254    "image based fill");
255 $oocopy->write(file=>'testout/t20_image.ppm');
256
257 # a more complex version
258 use Imager::Matrix2d ':handy';
259 $oocopy = $ooim->copy;
260 ok($oocopy->arc(fill=>{
261                        image=>$fillim,
262                        combine=>'normal',
263                        matrix=>m2d_rotate(degrees=>30),
264                        xoff=>5
265                        }, r=>40),
266    "transformed image based fill");
267 $oocopy->write(file=>'testout/t20_image_xform.ppm');
268
269 ok(!$oocopy->arc(fill=>{ hatch=>"not really a hatch" }, r=>20),
270    "error handling of automatic fill conversion");
271 ok($oocopy->errstr =~ /Unknown hatch type/,
272    "error message for automatic fill conversion");
273
274 # previous box fills to float images, or using the fountain fill
275 # got into a loop here
276
277 SKIP:
278 {
279   skip("can't test without alarm()", 1) unless $Config{d_alarm};
280   local $SIG{ALRM} = sub { die; };
281
282   eval {
283     alarm(2);
284     ok($ooim->box(xmin=>20, ymin=>20, xmax=>80, ymax=>40,
285                   fill=>{ fountain=>'linear', xa=>20, ya=>20, xb=>80, 
286                           yb=>20 }), "linear box fill");
287     alarm 0;
288   };
289   $@ and ok(0, "linear box fill $@");
290 }
291
292 # test that passing in a non-array ref returns an error
293 {
294   my $fill = Imager::Fill->new(fountain=>'linear',
295                                xa => 20, ya=>20, xb=>20, yb=>40,
296                                segments=>"invalid");
297   ok(!$fill, "passing invalid segments produces an error");
298   cmp_ok(Imager->errstr, '=~', 'array reference',
299          "check the error message");
300 }
301
302 # test that colors in segments are converted
303 {
304   my @segs =
305     (
306      [ 0.0, 0.5, 1.0, '000000', '#FFF', 0, 0 ],
307     );
308   my $fill = Imager::Fill->new(fountain=>'linear',
309                                xa => 0, ya=>20, xb=>49, yb=>20,
310                                segments=>\@segs);
311   ok($fill, "check that color names are converted")
312     or print "# ",Imager->errstr,"\n";
313   my $im = Imager->new(xsize=>50, ysize=>50);
314   $im->box(fill=>$fill);
315   my $left = $im->getpixel('x'=>0, 'y'=>20);
316   ok(color_close($left, Imager::Color->new(0,0,0)),
317      "check black converted correctly");
318   my $right = $im->getpixel('x'=>49, 'y'=>20);
319   ok(color_close($right, Imager::Color->new(255,255,255)),
320      "check white converted correctly");
321
322   # check that invalid colors handled correctly
323   
324   my @segs2 =
325     (
326      [ 0.0, 0.5, 1.0, '000000', 'FxFxFx', 0, 0 ],
327     );
328   my $fill2 = Imager::Fill->new(fountain=>'linear',
329                                xa => 0, ya=>20, xb=>49, yb=>20,
330                                segments=>\@segs2);
331   ok(!$fill2, "check handling of invalid color names");
332   cmp_ok(Imager->errstr, '=~', 'No color named', "check error message");
333 }
334
335 sub color_close {
336   my ($c1, $c2) = @_;
337
338   my @c1 = $c1->rgba;
339   my @c2 = $c2->rgba;
340
341   for my $i (0..2) {
342     if (abs($c1[$i]-$c2[$i]) > 2) {
343       return 0;
344     }
345   }
346   return 1;
347 }
348
349 # for use during testing
350 sub save {
351   my ($im, $name) = @_;
352
353   open FH, "> $name" or die "Cannot create $name: $!";
354   binmode FH;
355   my $io = Imager::io_new_fd(fileno(FH));
356   Imager::i_writeppm_wiol($im, $io) or die "Cannot save to $name";
357   undef $io;
358   close FH;
359 }