8 use Imager::Color::Float;
12 Imager::init_log("testout/t20fill.log", 1);
14 my $blue = NC(0,0,255);
15 my $red = NC(255, 0, 0);
16 my $redf = Imager::Color::Float->new(1, 0, 0);
17 my $rsolid = Imager::i_new_fill_solid($blue, 0);
18 ok(1, $rsolid, "building solid fill");
19 my $raw1 = Imager::ImgRaw::new(100, 100, 3);
20 # use the normal filled box
21 Imager::i_box_filled($raw1, 0, 0, 99, 99, $blue);
22 my $raw2 = Imager::ImgRaw::new(100, 100, 3);
23 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rsolid);
24 ok(2, 1, "drawing with solid fill");
25 my $diff = Imager::i_img_diff($raw1, $raw2);
26 ok(3, $diff == 0, "solid fill doesn't match");
27 Imager::i_box_filled($raw1, 0, 0, 99, 99, $red);
28 my $rsolid2 = Imager::i_new_fill_solidf($redf, 0);
29 ok(4, $rsolid2, "creating float solid fill");
30 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rsolid2);
31 $diff = Imager::i_img_diff($raw1, $raw2);
32 ok(5, $diff == 0, "float solid fill doesn't match");
34 # ok solid still works, let's try a hatch
35 # hash1 is a 2x2 checkerboard
36 my $rhatcha = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 0, 0);
37 my $rhatchb = Imager::i_new_fill_hatch($blue, $red, 0, 1, undef, 2, 0);
38 ok(6, $rhatcha && $rhatchb, "can't build hatched fill");
40 # the offset should make these match
41 Imager::i_box_cfill($raw1, 0, 0, 99, 99, $rhatcha);
42 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
43 ok(7, 1, "filling with hatch");
44 $diff = Imager::i_img_diff($raw1, $raw2);
45 ok(8, $diff == 0, "hatch images different");
46 $rhatchb = Imager::i_new_fill_hatch($blue, $red, 0, 1, undef, 4, 6);
47 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
48 $diff = Imager::i_img_diff($raw1, $raw2);
49 ok(9, $diff == 0, "hatch images different");
51 # I guess I was tired when I originally did this - make sure it keeps
52 # acting the way it's meant to
53 # I had originally expected these to match with the red and blue swapped
54 $rhatchb = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 2, 2);
55 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
56 $diff = Imager::i_img_diff($raw1, $raw2);
57 ok(10, $diff == 0, "hatch images different");
59 # this shouldn't match
60 $rhatchb = Imager::i_new_fill_hatch($red, $blue, 0, 1, undef, 1, 1);
61 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rhatchb);
62 $diff = Imager::i_img_diff($raw1, $raw2);
63 ok(11, $diff, "hatch images the same!");
66 # the inverse of the 2x2 checkerboard
67 my $hatch = pack("C8", 0x33, 0x33, 0xCC, 0xCC, 0x33, 0x33, 0xCC, 0xCC);
68 my $rcustom = Imager::i_new_fill_hatch($blue, $red, 0, 0, $hatch, 0, 0);
69 Imager::i_box_cfill($raw2, 0, 0, 99, 99, $rcustom);
70 $diff = Imager::i_img_diff($raw1, $raw2);
71 ok(12, !$diff, "custom hatch mismatch");
73 # test the oo interface
74 my $im1 = Imager->new(xsize=>100, ysize=>100);
75 my $im2 = Imager->new(xsize=>100, ysize=>100);
77 my $solid = Imager::Fill->new(solid=>'#FF0000');
78 ok(13, $solid, "creating oo solid fill");
79 ok(14, $solid->{fill}, "bad oo solid fill");
80 $im1->box(fill=>$solid);
81 $im2->box(filled=>1, color=>$red);
82 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
83 ok(15, !$diff, "oo solid fill");
85 my $hatcha = Imager::Fill->new(hatch=>'check2x2');
86 my $hatchb = Imager::Fill->new(hatch=>'check2x2', dx=>2);
87 $im1->box(fill=>$hatcha);
88 $im2->box(fill=>$hatchb);
90 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
91 ok(16, $diff, "offset checks the same!");
92 $hatchb = Imager::Fill->new(hatch=>'check2x2', dx=>2, dy=>2);
93 $im2->box(fill=>$hatchb);
94 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
95 ok(17, !$diff, "offset into similar check should be the same");
97 # test dymanic build of fill
98 $im2->box(fill=>{hatch=>'check2x2', dx=>2, fg=>NC(255,255,255),
100 $diff = Imager::i_img_diff($im1->{IMG}, $im2->{IMG});
101 ok(18, !$diff, "offset and flipped should be the same");
104 my $im = Imager->new(xsize=>200, ysize=>200);
106 $im->box(xmin=>10, ymin=>10, xmax=>190, ymax=>190,
107 fill=>{ hatch=>'check4x4',
109 bg=>NC(128, 64, 0) });
110 $im->arc(r=>80, d1=>45, d2=>75,
111 fill=>{ hatch=>'stipple2',
113 fg=>[ 0, 0, 0, 255 ],
114 bg=>{ rgba=>[255,255,255,160] } });
115 $im->arc(r=>80, d1=>75, d2=>135,
116 fill=>{ fountain=>'radial', xa=>100, ya=>100, xb=>20, yb=>100 });
117 $im->write(file=>'testout/t20_sample.ppm');
120 my $rffimg = Imager::ImgRaw::new(100, 100, 3);
122 Imager::i_box_filled($rffimg, 10, 10, 20, 90, $blue);
123 Imager::i_box_filled($rffimg, 80, 10, 90, 90, $blue);
124 Imager::i_box_filled($rffimg, 20, 45, 80, 55, $blue);
125 my $black = Imager::Color->new(0, 0, 0);
126 Imager::i_flood_fill($rffimg, 15, 15, $red);
127 my $rffcmp = Imager::ImgRaw::new(100, 100, 3);
129 Imager::i_box_filled($rffcmp, 10, 10, 20, 90, $red);
130 Imager::i_box_filled($rffcmp, 80, 10, 90, 90, $red);
131 Imager::i_box_filled($rffcmp, 20, 45, 80, 55, $red);
132 $diff = Imager::i_img_diff($rffimg, $rffcmp);
133 ok(19, !$diff, "flood fill difference");
135 my $ffim = Imager->new(xsize=>100, ysize=>100);
136 my $yellow = Imager::Color->new(255, 255, 0);
137 $ffim->box(xmin=>10, ymin=>10, xmax=>20, ymax=>90, color=>$blue, filled=>1);
138 $ffim->box(xmin=>20, ymin=>45, xmax=>80, ymax=>55, color=>$blue, filled=>1);
139 $ffim->box(xmin=>80, ymin=>10, xmax=>90, ymax=>90, color=>$blue, filled=>1);
140 ok(20, $ffim->flood_fill(x=>50, 'y'=>50, color=>$red), "flood fill");
141 $diff = Imager::i_img_diff($rffcmp, $ffim->{IMG});
142 ok(21, !$diff, "oo flood fill difference");
143 $ffim->flood_fill(x=>50, 'y'=>50,
148 # fountain=>'radial',
152 $ffim->write(file=>'testout/t20_ooflood.ppm');
154 # test combining modes
155 my $fill = NC(192, 128, 128, 128);
156 my $target = NC(64, 32, 64);
159 none=>{ result=>$fill },
160 normal=>{ result=>NC(128, 80, 96) },
161 multiply => { result=>NC(56, 24, 48) },
162 dissolve => { result=>[ $target, NC(128, 80, 96) ] },
163 add => { result=>NC(159, 96, 128) },
164 subtract => { result=>NC(31, 15, 31) }, # 31.87, 15.9, 31.87
165 diff => { result=>NC(96, 64, 64) },
166 lighten => { result=>NC(128, 80, 96) },
167 darken => { result=>$target },
168 # the following results are based on the results of the tests and
169 # are suspect for that reason (and were broken at one point <sigh>)
170 # but trying to work them out manually just makes my head hurt - TC
171 hue => { result=>NC(64, 32, 55) },
172 saturation => { result=>NC(63, 37, 64) },
173 value => { result=>NC(127, 64, 128) },
174 color => { result=>NC(64, 37, 52) },
177 my $testnum = 22; # from 22 to 34
178 for my $comb (Imager::Fill->combines) {
179 my $test = $comb_tests{$comb};
180 my $targim = Imager->new(xsize=>1, ysize=>1);
181 $targim->box(filled=>1, color=>$target);
182 my $fillobj = Imager::Fill->new(solid=>$fill, combine=>$comb);
183 $targim->box(fill=>$fillobj);
184 my $c = Imager::i_get_pixel($targim->{IMG}, 0, 0);
185 if ($test->{result} =~ /ARRAY/) {
186 ok($testnum++, scalar grep(color_close($_, $c), @{$test->{result}}),
188 or print "# got:",join(",", $c->rgba)," allowed: ",
189 join("|", map { join(",", $_->rgba) } @{$test->{result}}),"\n";
192 ok($testnum++, color_close($c, $test->{result}), "combine '$comb'")
193 or print "# got: ",join(",", $c->rgba),
194 " allowed: ",join(",", $test->{result}->rgba),"\n";
199 my ($num, $test, $desc) = @_;
205 print "not ok $num # $desc\n";
217 if (abs($c1[$i]-$c2[$i]) > 2) {
224 # for use during testing
226 my ($im, $name) = @_;
228 open FH, "> $name" or die "Cannot create $name: $!";
230 my $io = Imager::io_new_fd(fileno(FH));
231 Imager::i_writeppm_wiol($im, $io) or die "Cannot save to $name";