- added the det() function to the transform2() engine.
[imager.git] / t / t58trans2.t
CommitLineData
e5744e01 1#!perl -w
64f9ab49 2use strict;
3309187a 3use Test::More tests => 37;
64f9ab49 4BEGIN { use_ok('Imager'); }
3f29de50 5use Imager::Test qw(is_color3);
02d1d628 6
7da842e9 7Imager::init('log'=>'testout/t58trans2.log');
02d1d628
AMH
8
9my $im1 = Imager->new();
10$im1->open(file=>'testimg/penguin-base.ppm', type=>'pnm')
11 || die "Cannot read image";
12my $im2 = Imager->new();
13$im2->open(file=>'testimg/scale.ppm',type=>'pnm')
14 || die "Cannot read testimg/scale.ppm";
15
16# error handling
17my $opts = { rpnexpr=>'x x 10 / sin 10 * y + get1' };
18my $im3 = Imager::transform2($opts);
e5744e01
TC
19ok(!$im3, "returned an image on error");
20ok(defined($Imager::ERRSTR), "No error message on failure");
02d1d628
AMH
21
22# image synthesis
23my $im4 = Imager::transform2({
24 width=>300, height=>300,
25 rpnexpr=>'x y cx cy distance !d y cy - x cx - atan2 !a @d 10 / @a + 3.1416 2 * % !a2 @a2 cy * 3.1416 / 1 @a2 sin 1 + 2 / hsv'});
e5744e01 26ok($im4, "synthesis failed");
02d1d628
AMH
27
28if ($im4) {
29 $im4->write(type=>'pnm', file=>'testout/t56a.ppm')
30 || die "Cannot write testout/t56a.ppm";
31}
32
33# image distortion
34my $im5 = Imager::transform2({
35 rpnexpr=>'x x 10 / sin 10 * y + getp1'
36}, $im1);
e5744e01 37ok($im5, "image distortion");
02d1d628
AMH
38if ($im5) {
39 $im5->write(type=>'pnm', file=>'testout/t56b.ppm')
40 || die "Cannot write testout/t56b.ppm";
41}
42
43# image combination
44$opts = {
45rpnexpr=>'x h / !rat x w2 % y h2 % getp2 !pat x y getp1 @rat * @pat 1 @rat - * +'
46};
47my $im6 = Imager::transform2($opts,$im1,$im2);
e5744e01 48ok($im6, "image combination");
02d1d628
AMH
49if ($im6) {
50 $im6->write(type=>'pnm', file=>'testout/t56c.ppm')
51 || die "Cannot write testout/t56c.ppm";
52}
faa9b3e7 53
e5744e01
TC
54# alpha
55$opts =
56 {
57 rpnexpr => '0 0 255 x y + w h + 2 - / 255 * rgba',
58 channels => 4,
59 width => 50,
60 height => 50,
61 };
62my $im8 = Imager::transform2($opts);
63ok($im8, "alpha output");
64my $c = $im8->getpixel(x=>0, 'y'=>0);
65is(($c->rgba)[3], 0, "zero alpha");
66$c = $im8->getpixel(x=>49, 'y'=>49);
67is(($c->rgba)[3], 255, "max alpha");
68
aed9d070
TC
69$opts = { rpnexpr => 'x 1 + log 50 * y 1 + log 50 * getp1' };
70my $im9 = Imager::transform2($opts, $im1);
71ok($im9, "log function");
72if ($im9) {
73 $im9->write(type=>'pnm', file=>'testout/t56-9.ppm');
74}
75
3f29de50
TC
76# op tests
77sub op_test($$$$$$);
78print "# op tests\n";
79op_test('7F0000', <<EOS, 0, 127, 0, 'value hsv getp1');
80120 1.0
810 0 getp1 value
82hsv
83EOS
84op_test("7F0000", <<EOS, 255, 0, 0, 'hue');
850 0 getp1 hue
861.0 1.0 hsv
87EOS
88op_test("7F0000", <<EOS, 0, 255, 0, 'sat');
89120 0 0 getp1 sat 1.0 hsv
90EOS
91op_test("4060A0", <<'EOS', 128, 128, 128, "add mult sub rgb red green blue");
920 0 getp1 !p @p red 2 * @p green 32 + @p blue 32 - rgb
93EOS
94op_test('806040', <<'EOS', 64, 64, 64, "div uminus");
950 0 getp1 !p @p red 2 / @p green 32 uminus add @p blue rgb
96EOS
97op_test('40087f', <<'EOS', 8, 64, 31, 'pow mod');
980 0 getp1 !p @p red 0.5 pow @p green 2 pow @p blue 32 mod rgb
99EOS
100op_test('202122', '0 0 getp1 4 *', 128, 132, 136, 'multp');
101op_test('404040', '0 0 getp1 1 2 3 rgb +', 65, 66, 67, 'addp');
102op_test('414243', '0 0 getp1 3 2 1 rgb -', 62, 64, 66, 'subp');
103op_test('808040', <<'EOS', 64, 64, 8, 'sin cos pi sqrt');
1040 0 getp1 !p pi 6 / sin @p red * 0.1 + pi 3 / cos @p green * 0.1 +
105@p blue sqrt rgb
106EOS
107op_test('008080', <<'EOS', 0, 0, 0, 'atan2');
1080 0 0 0 getp1 !p @p red 128 / @p green 128 / atan2 hsv
109EOS
110op_test('000000', <<'EOS', 150, 150, 150, 'distance');
1110 100 120 10 distance !d @d @d @d rgb
112EOS
113op_test('000000', <<'EOS', 100, 100, 100, 'int');
11450.75 int 2 * !i @i @i @i rgb
115EOS
116op_test('000100', <<'EOS', 128, 0, 0, 'if');
1170 0 getp1 !p @p red 0 128 if @p green 0 128 if 0 rgb
118EOS
119op_test('FF0000', <<'EOS', 0, 255, 0, 'ifp');
1200 0 0 getp1 0 255 0 rgb ifp
121EOS
122op_test('000000', <<'EOS', 1, 0, 1, 'le lt gt');
1230 1 le 1 0 lt 1 0 gt rgb
124EOS
125op_test('000000', <<'EOS', 0, 1, 0, 'ge eq ne');
1260 1 ge 0 0 eq 0 0 ne rgb
127EOS
128op_test('000000', <<'EOS', 0, 1, 1, 'and or not');
1291 0 and 1 0 or 0 not rgb
130EOS
131op_test('000000', <<'EOS', 255, 0, 255, 'abs');
132-255 abs 0 abs 255 abs rgb
133EOS
134op_test('000000', <<'EOS', 50, 82, 0, 'exp log');
1351 exp log 50 * 0.5 + 0.5 exp 50 * 0 rgb
136EOS
3309187a
TC
137op_test('800000', <<'EOS', 128, 0, 0, 'det');
1381 0 0 1 det 128 * 1 1 1 1 det 128 * 0 rgb
139EOS
3f29de50 140
faa9b3e7
TC
141use Imager::Transform;
142
143# some simple tests
e5744e01
TC
144print "# Imager::Transform\n";
145my @funcs = Imager::Transform->list;
146ok(@funcs, "funcs");
147
148my $tran = Imager::Transform->new($funcs[0]);
149ok($tran, "got tranform");
150ok($tran->describe() eq Imager::Transform->describe($funcs[0]),
151 "description");
faa9b3e7
TC
152# look for a function that takes inputs (at least one does)
153my @needsinputs = grep Imager::Transform->new($_)->inputs, @funcs;
154# make sure they're
155my @inputs = Imager::Transform->new($needsinputs[0])->inputs;
e5744e01 156ok($inputs[0]{desc}, "input description");
faa9b3e7 157# at some point I might want to test the actual transformations
bf94b653
TC
158
159# check lower level error handling
160my $im7 = Imager::transform2({rpnexpr=>'x y getp2', width=>100, height=>100});
e5744e01 161ok(!$im7, "expected failure on accessing invalid image");
bf94b653 162print "# ", Imager->errstr, "\n";
e5744e01 163ok(Imager->errstr =~ /not enough images/, "didn't get expected error");
3f29de50
TC
164
165sub op_test ($$$$$$) {
166 my ($in_color, $code, $r, $g, $b, $comment) = @_;
167
168 my $im = Imager->new(xsize => 1, ysize => 1);
169 $im->setpixel(x => 0, y => 0, color => $in_color);
170 SKIP:
171 {
172 my $out = Imager::transform2({ rpnexpr => $code }, $im);
173 unless ($out) {
3309187a 174 fail("$comment: could not compile $code - ".Imager->errstr);
3f29de50
TC
175 return;
176 }
177 my $found = $out->getpixel(x => 0, y => 0);
178 is_color3($found, $r, $g, $b, $comment);
179 }
180}