rotate.c -> rotate.im transition
[imager.git] / t / t64copyflip.t
CommitLineData
0d3b936e 1#!perl -w
59c6cd7f 2use strict;
999f4294 3use Test::More tests => 83;
390cd725 4use Imager;
5cfde30f 5use Imager::Test qw(is_color3 is_image is_imaged test_image_double test_image isnt_image);
390cd725 6
390cd725
AMH
7#$Imager::DEBUG=1;
8
40e78f96
TC
9-d "testout" or mkdir "testout";
10
390cd725
AMH
11Imager::init('log'=>'testout/t64copyflip.log');
12
59c6cd7f 13my $img=Imager->new() or die "unable to create image object\n";
390cd725
AMH
14
15$img->open(file=>'testimg/scale.ppm',type=>'pnm');
59c6cd7f
TC
16my $nimg = $img->copy();
17ok($nimg, "copy returned something");
390cd725
AMH
18
19# test if ->copy() works
20
59c6cd7f 21my $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
e41cfe8f 22is_image($img, $nimg, "copy matches source");
390cd725
AMH
23
24# test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
390cd725 25$nimg->flip(dir=>"h")->flip(dir=>"h");
e41cfe8f 26is_image($nimg, $img, "double horiz flipped matches original");
390cd725
AMH
27
28# test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
390cd725 29$nimg->flip(dir=>"v")->flip(dir=>"v");
e41cfe8f 30is_image($nimg, $img, "double vertically flipped image matches original");
390cd725
AMH
31
32
33# test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
390cd725 34$nimg->flip(dir=>"v")->flip(dir=>"h")->flip(dir=>"hv");;
e41cfe8f
TC
35is_image($img, $nimg, "check flip with hv matches flip v then flip h");
36
37{
38 my $imsrc = test_image_double;
39 my $imcp = $imsrc->copy;
40 is_imaged($imsrc, $imcp, "copy double image");
41 $imcp->flip(dir=>"v")->flip(dir=>"v");
42 is_imaged($imsrc, $imcp, "flip v twice");
43 $imcp->flip(dir=>"h")->flip(dir=>"h");
44 is_imaged($imsrc, $imcp, "flip h twice");
45 $imcp->flip(dir=>"h")->flip(dir=>"v")->flip(dir=>"hv");
46 is_imaged($imsrc, $imcp, "flip h,v,hv twice");
47}
390cd725 48
5cfde30f
TC
49{
50 my $impal = test_image()->to_paletted;
51 my $imcp = $impal->copy;
52 is($impal->type, "paletted", "check paletted test image is");
53 is($imcp->type, "paletted", "check copy test image is paletted");
54 ok($impal->flip(dir => "h"), "flip paletted h");
55 isnt_image($impal, $imcp, "check it changed");
56 ok($impal->flip(dir => "v"), "flip paletted v");
57 ok($impal->flip(dir => "hv"), "flip paletted hv");
58 is_image($impal, $imcp, "should be back to original image");
59 is($impal->type, "paletted", "and still paletted");
60}
61
59c6cd7f
TC
62rot_test($img, 90, 4);
63rot_test($img, 180, 2);
64rot_test($img, 270, 4);
65rot_test($img, 0, 1);
faa9b3e7
TC
66
67my $pimg = $img->to_paletted();
59c6cd7f
TC
68rot_test($pimg, 90, 4);
69rot_test($pimg, 180, 2);
70rot_test($pimg, 270, 4);
71rot_test($pimg, 0, 1);
faa9b3e7
TC
72
73my $timg = $img->rotate(right=>90)->rotate(right=>270);
59c6cd7f
TC
74is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
75 "check rotate 90 then 270 matches original");
faa9b3e7 76$timg = $img->rotate(right=>90)->rotate(right=>180)->rotate(right=>90);
59c6cd7f
TC
77is(Imager::i_img_diff($img->{IMG}, $timg->{IMG}), 0,
78 "check rotate 90 then 180 then 90 matches original");
faa9b3e7
TC
79
80# this could use more tests
59c6cd7f
TC
81my $rimg = $img->rotate(degrees=>10);
82ok($rimg, "rotation by 10 degrees gave us an image");
faa9b3e7
TC
83if (!$rimg->write(file=>"testout/t64_rot10.ppm")) {
84 print "# Cannot save: ",$rimg->errstr,"\n";
85}
86
0d3b936e 87# rotate with background
59c6cd7f
TC
88$rimg = $img->rotate(degrees=>10, back=>Imager::Color->new(builtin=>'red'));
89ok($rimg, "rotate with background gave us an image");
0d3b936e
TC
90if (!$rimg->write(file=>"testout/t64_rot10_back.ppm")) {
91 print "# Cannot save: ",$rimg->errstr,"\n";
92}
7f627571
TC
93
94{
95 # rotate with text background
96 my $rimg = $img->rotate(degrees => 45, back => '#FF00FF');
97 ok($rimg, "rotate with background as text gave us an image");
98
99 # check the color set correctly
100 my $c = $rimg->getpixel(x => 0, 'y' => 0);
101 is_deeply([ 255, 0, 255 ], [ ($c->rgba)[0, 1, 2] ],
102 "check background set correctly");
103
104 # check error handling for background color
105 $rimg = $img->rotate(degrees => 45, back => "some really unknown color");
106 ok(!$rimg, "should fail due to bad back color");
107 cmp_ok($img->errstr, '=~', "^No color named ", "check error message");
108}
999f4294
TC
109SKIP:
110{ # rotate in double mode
111 my $dimg = $img->to_rgb16;
112 my $rimg = $dimg->rotate(degrees => 10);
113 ok($rimg, "rotate 16-bit image gave us an image")
114 or skip("could not rotate", 3);
115 ok($rimg->write(file => "testout/t64_rotf10.ppm", pnm_write_wide_data => 1),
116 "save wide data rotated")
117 or diag($rimg->errstr);
118
119 # with a background color
120 my $rimgb = $dimg->rotate(degrees => 10, back => "#FF8000");
121 ok($rimgb, "rotate 16-bit image with back gave us an image")
122 or skip("could not rotate", 1);
123 ok($rimgb->write(file => "testout/t64_rotfb10.ppm", pnm_write_wide_data => 1),
124 "save wide data rotated")
125 or diag($rimgb->errstr);
126}
127{ # rotate in paletted mode
128 my $rimg = $pimg->rotate(degrees => 10);
129 ok($rimg, "rotated paletted image 10 degrees");
130 ok($rimg->write(file => "testout/t64_rotp10.ppm"),
131 "save paletted rotated")
132 or diag($rimg->errstr);
133}
0d3b936e 134
faa9b3e7
TC
135my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
136 0, 1, 0,
59c6cd7f
TC
137 0, 0, 1]);
138ok($trimg, "matrix_transform() returned an image");
faa9b3e7
TC
139$trimg->write(file=>"testout/t64_trans.ppm")
140 or print "# Cannot save: ",$trimg->errstr,"\n";
141
13fc481e 142$trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
0d3b936e
TC
143 0, 1, 0,
144 0, 0, 1],
59c6cd7f
TC
145 back=>Imager::Color->new(builtin=>'blue'));
146ok($trimg, "matrix_transform() with back returned an image");
147
0d3b936e
TC
148$trimg->write(file=>"testout/t64_trans_back.ppm")
149 or print "# Cannot save: ",$trimg->errstr,"\n";
150
faa9b3e7 151sub rot_test {
59c6cd7f 152 my ($src, $degrees, $count) = @_;
faa9b3e7
TC
153
154 my $cimg = $src->copy();
155 my $in;
156 for (1..$count) {
157 $in = $cimg;
158 $cimg = $cimg->rotate(right=>$degrees)
159 or last;
160 }
59c6cd7f
TC
161 SKIP:
162 {
163 ok($cimg, "got a rotated image")
164 or skip("no image to check", 4);
faa9b3e7 165 my $diff = Imager::i_img_diff($src->{IMG}, $cimg->{IMG});
59c6cd7f
TC
166 is($diff, 0, "check it matches source")
167 or skip("didn't match", 3);
faa9b3e7 168
59c6cd7f
TC
169 # check that other parameters match
170 is($src->type, $cimg->type, "type check");
171 is($src->bits, $cimg->bits, "bits check");
172 is($src->getchannels, $cimg->getchannels, "channels check");
173 }
390cd725
AMH
174}
175
34b3f7e6
TC
176{ # http://rt.cpan.org/NoAuth/Bug.html?id=9672
177 my $warning;
178 local $SIG{__WARN__} =
179 sub {
180 $warning = "@_";
181 my $printed = $warning;
182 $printed =~ s/\n$//;
183 $printed =~ s/\n/\n\#/g;
184 print "# ",$printed, "\n";
185 };
186 my $img = Imager->new(xsize=>10, ysize=>10);
187 $img->copy();
188 cmp_ok($warning, '=~', 'void', "correct warning");
189 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
190 $warning = '';
191 $img->rotate(degrees=>5);
192 cmp_ok($warning, '=~', 'void', "correct warning");
193 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
194 $warning = '';
195 $img->matrix_transform(matrix=>[1, 1, 1]);
196 cmp_ok($warning, '=~', 'void', "correct warning");
197 cmp_ok($warning, '=~', 't64copyflip\\.t', "correct file");
198}
199
e7ac18bd
TC
200{
201 # 29936 - matrix_transform() should use fabs() instead of abs()
202 # range checking sz
203
204 # this meant that when sz was < 1 (which it often is for these
205 # transformations), it treated the values out of range, producing a
206 # blank output image
207
208 my $src = Imager->new(xsize => 20, ysize => 20);
209 $src->box(filled => 1, color => 'FF0000');
210 my $out = $src->matrix_transform(matrix => [ 1, 0, 0,
211 0, 1, 0,
212 0, 0, 0.9999 ])
213 or print "# ", $src->errstr, "\n";
214 my $blank = Imager->new(xsize => 20, ysize => 20);
215 # they have to be different, surely that would be easy
216 my $diff = Imager::i_img_diff($out->{IMG}, $blank->{IMG});
217 ok($diff, "RT#29936 - check non-blank output");
218}
13c9a303
TC
219
220{
221 my $im = Imager->new(xsize => 10, ysize => 10, channels => 4);
222 $im->box(filled => 1, color => 'FF0000');
223 my $back = Imager::Color->new(0, 0, 0, 0);
224 my $rot = $im->rotate(degrees => 10, back => $back);
225 # drop the alpha and make sure there's only 2 colors used
226 my $work = $rot->convert(preset => 'noalpha');
227 my $im_pal = $work->to_paletted(make_colors => 'mediancut');
228 my @colors = $im_pal->getcolors;
8d14daab
TC
229 is(@colors, 2, "should be only 2 colors")
230 or do {
231 print "# ", join(",", $_->rgba), "\n" for @colors;
232 };
13c9a303
TC
233 @colors = sort { ($a->rgba)[0] <=> ($b->rgba)[0] } @colors;
234 is_color3($colors[0], 0, 0, 0, "check we got black");
235 is_color3($colors[1], 255, 0, 0, "and red");
236}