]> git.imager.perl.org - imager.git/blob - t/t64copyflip.t
base for hand-edited test tga images
[imager.git] / t / t64copyflip.t
1 #!perl -w
2 BEGIN { $| = 1; print "1..43\n"; }
3 END {print "not ok 1\n" unless $loaded;}
4 use Imager;
5
6 $loaded = 1;
7
8 #$Imager::DEBUG=1;
9
10 Imager::init('log'=>'testout/t64copyflip.log');
11
12 $img=Imager->new() or die "unable to create image object\n";
13
14 $img->open(file=>'testimg/scale.ppm',type=>'pnm');
15 $nimg = $img->copy() or skip ( "\# warning ".$img->{'ERRSTR'}."\n" );
16 print "ok 1\n";
17
18 # test if ->copy() works
19
20 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
21 if ($diff > 0) { print "not ok 2 # copy and original differ!\n"; }
22 else { print "ok 2\n"; }
23
24
25 # test if ->flip(dir=>'h')->flip(dir=>'h') doesn't alter the image
26
27 $nimg->flip(dir=>"h")->flip(dir=>"h");
28 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
29 if ($diff > 0) { print "not ok 3 # double horizontal flip and original differ!\n"; }
30 else { print "ok 3\n"; }
31
32
33 # test if ->flip(dir=>'v')->flip(dir=>'v') doesn't alter the image
34
35 $nimg->flip(dir=>"v")->flip(dir=>"v");
36 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
37 if ($diff > 0) { print "not ok 4 # double vertical flip and original differ!\n"; }
38 else { print "ok 4\n"; }
39
40
41 # test if ->flip(dir=>'h')->flip(dir=>'v') is same as ->flip(dir=>'hv')
42
43 $nimg->flip(dir=>"v")->flip(dir=>"h")->flip(dir=>"hv");;
44 $diff = Imager::i_img_diff($img->{IMG}, $nimg->{IMG});
45 if ($diff > 0) {
46   print "not ok 5 # double flips and original differ!\n";
47   $nimg->write(file=>"testout/t64copyflip_error.ppm") or die $nimg->errstr();
48 }
49 else { print "ok 5\n"; }
50
51 rot_test(6, $img, 90, 4);
52 rot_test(10, $img, 180, 2);
53 rot_test(14, $img, 270, 4);
54 rot_test(18, $img, 0, 1);
55
56 my $pimg = $img->to_paletted();
57 rot_test(22, $pimg, 90, 4);
58 rot_test(26, $pimg, 180, 2);
59 rot_test(30, $pimg, 270, 4);
60 rot_test(34, $pimg, 0, 1);
61
62 my $timg = $img->rotate(right=>90)->rotate(right=>270);
63 Imager::i_img_diff($img->{IMG}, $timg->{IMG}) and print "not ";
64 print "ok 38\n";
65 $timg = $img->rotate(right=>90)->rotate(right=>180)->rotate(right=>90);
66 Imager::i_img_diff($img->{IMG}, $timg->{IMG}) and print "not ";
67 print "ok 39\n";
68
69 # this could use more tests
70 my $rimg = $img->rotate(degrees=>10)
71   or print "not ";
72 print "ok 40\n";
73 if (!$rimg->write(file=>"testout/t64_rot10.ppm")) {
74   print "# Cannot save: ",$rimg->errstr,"\n";
75 }
76
77 # rotate with background
78 $rimg = $img->rotate(degrees=>10, back=>Imager::Color->new(builtin=>'red'))
79   or print "not ";
80 print "ok 41\n";
81 if (!$rimg->write(file=>"testout/t64_rot10_back.ppm")) {
82   print "# Cannot save: ",$rimg->errstr,"\n";
83 }
84         
85
86 my $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
87                                              0,   1, 0,
88                                              0,   0, 1])
89   or print "not ";
90 print "ok 42\n";
91 $trimg->write(file=>"testout/t64_trans.ppm")
92   or print "# Cannot save: ",$trimg->errstr,"\n";
93
94 $trimg = $img->matrix_transform(matrix=>[ 1.2, 0, 0,
95                                              0,   1, 0,
96                                              0,   0, 1],
97                                    back=>Imager::Color->new(builtin=>'blue'))
98   or print "not ";
99 print "ok 43\n";
100 $trimg->write(file=>"testout/t64_trans_back.ppm")
101   or print "# Cannot save: ",$trimg->errstr,"\n";
102
103 sub rot_test {
104   my ($testnum, $src, $degrees, $count) = @_;
105
106   my $cimg = $src->copy();
107   my $in;
108   for (1..$count) {
109     $in = $cimg;
110     $cimg = $cimg->rotate(right=>$degrees)
111       or last;
112   }
113   if ($cimg) {
114     print "ok ",$testnum++,"\n";
115     my $diff = Imager::i_img_diff($src->{IMG}, $cimg->{IMG});
116     if ($diff) {
117       print "not ok ",$testnum++," # final image doesn't match input\n";
118       for (1..3) {
119         print "ok ",$testnum++," # skipped\n";
120       }
121     }
122     else {
123       # check that other parameters match
124       $src->type eq $cimg->type or print "not ";
125       print "ok ",$testnum++," # type\n";
126       $src->bits eq $cimg->bits or print "not ";
127       print "ok ",$testnum++," # bits\n";
128       $src->getchannels eq $cimg->getchannels or print "not ";
129       print "ok ",$testnum++," # channels \n";
130     }
131   }
132   else {
133     print "not ok ",$testnum++," # rotate returned undef:",$in->errstr,"\n";
134     for (1..4) {
135       print "ok ",$testnum++," # skipped\n";
136     }
137   }
138 }
139
140 sub skip {
141     print $_[0];
142     print "ok 2 # skip\n";
143     print "ok 3 # skip\n";
144     print "ok 4 # skip\n";
145     print "ok 5 # skip\n";
146     exit(0);
147 }
148