5 use Test::More tests => 62;
6 Imager::init_log("testout/t61filters.log", 1);
7 # meant for testing the filters themselves
8 my $imbase = Imager->new;
9 $imbase->open(file=>'testout/t104.ppm') or die;
10 my $im_other = Imager->new(xsize=>150, ysize=>150);
11 $im_other->box(xmin=>30, ymin=>60, xmax=>120, ymax=>90, filled=>1);
13 test($imbase, {type=>'autolevels'}, 'testout/t61_autolev.ppm');
15 test($imbase, {type=>'contrast', intensity=>0.5},
16 'testout/t61_contrast.ppm');
18 # this one's kind of cool
19 test($imbase, {type=>'conv', coef=>[ -0.5, 1, -0.5, ], },
20 'testout/t61_conv.ppm');
22 test($imbase, {type=>'gaussian', stddev=>5 },
23 'testout/t61_gaussian.ppm');
25 test($imbase, { type=>'gradgen', dist=>1,
28 colors=> [ NC('#FF0000'), NC('#FFFF00'), NC('#00FFFF') ]},
29 'testout/t61_gradgen.ppm');
31 test($imbase, {type=>'mosaic', size=>8}, 'testout/t61_mosaic.ppm');
33 test($imbase, {type=>'hardinvert'}, 'testout/t61_hardinvert.ppm');
35 test($imbase, {type=>'noise'}, 'testout/t61_noise.ppm');
37 test($imbase, {type=>'radnoise'}, 'testout/t61_radnoise.ppm');
39 test($imbase, {type=>'turbnoise'}, 'testout/t61_turbnoise.ppm');
41 test($imbase, {type=>'bumpmap', bump=>$im_other, lightx=>30, lighty=>30},
42 'testout/t61_bumpmap.ppm');
44 test($imbase, {type=>'bumpmap_complex', bump=>$im_other}, 'testout/t61_bumpmap_complex.ppm');
46 test($imbase, {type=>'postlevels', levels=>3}, 'testout/t61_postlevels.ppm');
48 test($imbase, {type=>'watermark', wmark=>$im_other },
49 'testout/t61_watermark.ppm');
51 test($imbase, {type=>'fountain', xa=>75, ya=>75, xb=>85, yb=>30,
52 repeat=>'triangle', #ftype=>'radial',
53 super_sample=>'circle', ssample_param => 16,
55 'testout/t61_fountain.ppm');
58 my $f1 = Imager::Fountain->new;
59 $f1->add(end=>0.2, c0=>NC(255, 0,0), c1=>NC(255, 255,0));
60 $f1->add(start=>0.2, c0=>NC(255,255,0), c1=>NC(0,0,255,0));
61 test($imbase, { type=>'fountain', xa=>20, ya=>130, xb=>130, yb=>20,
65 'testout/t61_fountain2.ppm');
66 my $f2 = Imager::Fountain->new
67 ->add(end=>0.5, c0=>NC(255,0,0), c1=>NC(255,0,0), color=>'hueup')
68 ->add(start=>0.5, c0=>NC(255,0,0), c1=>NC(255,0,0), color=>'huedown');
71 test($imbase, { type=>'fountain', xa=>20, ya=>130, xb=>130, yb=>20,
73 'testout/t61_fount_hsv.ppm');
74 my $f3 = Imager::Fountain->read(gimp=>'testimg/gimpgrad');
75 ok($f3, "read gimpgrad");
76 test($imbase, { type=>'fountain', xa=>75, ya=>75, xb=>90, yb=>15,
77 segments=>$f3, super_sample=>'grid',
78 ftype=>'radial_square', combine=>'color' },
79 'testout/t61_fount_gimp.ppm');
80 test($imbase, { type=>'unsharpmask', stddev=>2.0 },
81 'testout/t61_unsharp.ppm');
82 test($imbase, {type=>'conv', coef=>[ -1, 3, -1, ], },
83 'testout/t61_conv_sharp.ppm');
85 # Regression test: the checking of the segment type was incorrect
86 # (the comparison was checking the wrong variable against the wrong value)
87 my $f4 = [ [ 0, 0.5, 1, NC(0,0,0), NC(255,255,255), 5, 0 ] ];
88 test($imbase, {type=>'fountain', xa=>75, ya=>75, xb=>90, yb=>15,
89 segments=>$f4, super_sample=>'grid',
90 ftype=>'linear', combine=>'color' },
91 'testout/t61_regress_fount.ppm');
92 my $im2 = $imbase->copy;
93 $im2->box(xmin=>20, ymin=>20, xmax=>40, ymax=>40, color=>'FF0000', filled=>1);
94 $im2->write(file=>'testout/t61_diff_base.ppm');
95 my $im3 = Imager->new(xsize=>150, ysize=>150, channels=>3);
96 $im3->box(xmin=>20, ymin=>20, xmax=>40, ymax=>40, color=>'FF0000', filled=>1);
97 my $diff = $imbase->difference(other=>$im2);
98 ok($diff, "got difference image");
101 skip(1, "missing comp or diff image") unless $im3 && $diff;
103 is(Imager::i_img_diff($im3->{IMG}, $diff->{IMG}), 0,
104 "compare test image and diff image");
107 # newer versions of gimp add a line to the gradient file
109 my $f5 = Imager::Fountain->read(gimp=>'testimg/newgimpgrad.ggr',
111 ok($f5, "read newer gimp gradient")
112 or print "# ",Imager->errstr,"\n";
113 is($name, "imager test gradient", "check name read correctly");
114 $f5 = Imager::Fountain->read(gimp=>'testimg/newgimpgrad.ggr');
115 ok($f5, "check we handle case of no name reference correctly")
116 or print "# ",Imager->errstr,"\n";
118 # test writing of gradients
119 ok($f2->write(gimp=>'testout/t61grad1.ggr'), "save a gradient")
120 or print "# ",Imager->errstr,"\n";
122 my $f6 = Imager::Fountain->read(gimp=>'testout/t61grad1.ggr',
124 ok($f6, "read what we wrote")
125 or print "# ",Imager->errstr,"\n";
126 ok(!defined $name, "we didn't set the name, so shouldn't get one");
129 ok($f2->write(gimp=>'testout/t61grad2.ggr', name=>'test gradient'),
130 "write gradient with a name")
131 or print "# ",Imager->errstr,"\n";
133 my $f7 = Imager::Fountain->read(gimp=>'testout/t61grad2.ggr', name=>\$name);
134 ok($f7, "read what we wrote")
135 or print "# ",Imager->errstr,"\n";
136 is($name, "test gradient", "check the name matches");
138 # we attempt to convert color names in segments to segments now
142 [ 0.0, 0.5, 1.0, '000000', '#FFF', 0, 0 ],
144 my $im = Imager->new(xsize=>50, ysize=>50);
145 ok($im->filter(type=>'fountain', segments => \@segs,
146 xa=>0, ya=>30, xb=>49, yb=>30),
147 "fountain with color names instead of objects in segments");
148 my $left = $im->getpixel('x'=>0, 'y'=>20);
149 ok(color_close($left, Imager::Color->new(0,0,0)),
150 "check black converted correctly");
151 my $right = $im->getpixel('x'=>49, 'y'=>20);
152 ok(color_close($right, Imager::Color->new(255,255,255)),
153 "check white converted correctly");
155 # check that invalid color names are handled correctly
158 [ 0.0, 0.5, 1.0, '000000', 'FxFxFx', 0, 0 ],
160 ok(!$im->filter(type=>'fountain', segments => \@segs2,
161 xa=>0, ya=>30, xb=>49, yb=>30),
162 "fountain with invalid color name");
163 cmp_ok($im->errstr, '=~', 'No color named', "check error message");
167 my $im = Imager->new(xsize=>100, ysize=>100);
168 # build the gradient the hard way - linear from black to white,
172 [ 0, 0.25, 0.5, 'black', 'white', 0, 0 ],
173 [ 0.5. 0.75, 1.0, 'white', 'black', 0, 0 ],
176 my $linear = $im->filter(type => "fountain",
178 repeat => 'sawtooth',
180 ya => $im->getheight / 2,
181 xb => $im->getwidth - 1,
182 yb => $im->getheight / 2);
183 ok($linear, "linear fountain sample");
185 my $revolution = $im->filter(type => "fountain",
186 ftype => 'revolution',
187 xa => $im->getwidth / 2,
188 ya => $im->getheight / 2,
189 xb => $im->getwidth / 2,
191 ok($revolution, "revolution fountain sample");
192 # out from the middle
193 my $radial = $im->filter(type => "fountain",
195 xa => $im->getwidth / 2,
196 ya => $im->getheight / 2,
197 xb => $im->getwidth / 2,
199 ok($radial, "radial fountain sample");
203 my ($in, $params, $out) = @_;
205 my $copy = $in->copy;
206 if (ok($copy->filter(%$params), $params->{type})) {
207 ok($copy->write(file=>$out), "write $params->{type}")
208 or print "# ",$copy->errstr,"\n";
213 skip("couldn't filter", 1);
225 if (abs($c1[$i]-$c2[$i]) > 2) {