]> git.imager.perl.org - imager.git/blob - t/t108tga.t
a19591f6ead99dd4ba39c96f16ede8c662fe1859
[imager.git] / t / t108tga.t
1 #!perl -w
2 use Imager qw(:all);
3 use strict;
4 use Test::More tests=>36;
5 BEGIN { require "t/testtools.pl"; }
6 init_log("testout/t108tga.log",1);
7
8
9 my $img = create_test_image();
10 my $base_diff = 0;
11
12 write_test($img, "testout/t108_24bit.tga", 0, 0, "");
13 write_test($img, "testout/t108_24bit_rle.tga", 0, 1, "");
14 write_test($img, "testout/t108_15bit.tga", 1, 1, "");
15 write_test($img, "testout/t108_15bit_rle.tga", 1, 1, "");
16
17 # 'webmap' is noticably faster than the default
18 my $im8 = Imager::i_img_to_pal($img, { make_colors=>'webmap',
19                                        translate=>'errdiff'});
20
21 write_test($im8, "testout/t108_8bit.tga", 0, 0, "");
22 write_test($im8, "testout/t108_8bit_rle.tga", 0, 1, "");
23 write_test($im8, "testout/t108_8_15bit.tga", 1, 0, "");
24 write_test($im8, "testout/t108_8_15bit_rle.tga", 1, 1, "");
25
26
27 # use a fixed palette so we get reproducible results for the compressed
28 # version
29
30 my @bit4 = map { NC($_) }
31   qw(605844 966600 0148b2 00f800 bf0a33 5e009e
32      2ead1b 0000f8 004b01 fd0000 0e1695 000002);
33
34 my @bit1 = (NC(0, 0, 0), NC(176, 160, 144));
35
36 my $im4 = Imager::i_img_to_pal($img, { colors=>\@bit4,
37                                        make_colors=>'none' });
38
39 my $im1 = Imager::i_img_to_pal($img, { colors=>\@bit1,
40                                        make_colors=>'none',
41                                        translate=>'errdiff' });
42
43 write_test($im4, "testout/t108_4bit.tga", 0, 1, "");
44 write_test($im1, "testout/t108_1bit.tga", 0, 1, "This is a comment!");
45
46 read_test("testout/t108_24bit.tga", $img);
47 read_test("testout/t108_8bit.tga",  $im8);
48 read_test("testout/t108_4bit.tga",  $im4);
49 read_test("testout/t108_1bit.tga",  $im1);
50
51 # the following might have slight differences
52
53 $base_diff = i_img_diff($img, $im8) * 2;
54
55 print "# base difference $base_diff\n";
56
57 my $imoo = Imager->new;
58 ok($imoo->read(file=>'testout/t108_24bit.tga'),
59    "OO read image")
60   or print "# ",$imoo->errstr,"\n";
61
62 ok($imoo->write(file=>'testout/t108_oo.tga'),
63    "OO write image")
64   or print "# ",$imoo->errstr,"\n";
65
66 my ($type) = $imoo->tags(name=>'i_format');
67 is($type, 'tga', "check i_format tag");
68
69 # in 0.44 and earlier, reading an image with an idstring of 128 or more
70 # bytes would result in an allocation error, if the platform char type
71 # was signed
72 $imoo = Imager->new;
73 ok($imoo->read(file=>'testimg/longid.tga'), "read long id image");
74 my ($id) = $imoo->tags(name=>'tga_idstring');
75 is($id, "X" x 128, "check tga_idstring tag");
76 my ($bitspp) = $imoo->tags(name=>'tga_bitspp');
77 is($bitspp, 24, "check tga_bitspp tag");
78 my ($compressed) = $imoo->tags(name=>'compressed');
79 is($compressed, 1, "check compressed tag");
80
81 { # check file limits are checked
82   my $limit_file = "testout/t108_24bit.tga";
83   ok(Imager->set_file_limits(reset=>1, width=>149), "set width limit 149");
84   my $im = Imager->new;
85   ok(!$im->read(file=>$limit_file),
86      "should fail read due to size limits");
87   print "# ",$im->errstr,"\n";
88   like($im->errstr, qr/image width/, "check message");
89   
90   ok(Imager->set_file_limits(reset=>1, height=>149), "set height limit 149");
91   ok(!$im->read(file=>$limit_file),
92      "should fail read due to size limits");
93   print "# ",$im->errstr,"\n";
94   like($im->errstr, qr/image height/, "check message");
95   
96   ok(Imager->set_file_limits(reset=>1, width=>150), "set width limit 150");
97   ok($im->read(file=>$limit_file),
98      "should succeed - just inside width limit");
99   ok(Imager->set_file_limits(reset=>1, height=>150), "set height limit 150");
100   ok($im->read(file=>$limit_file),
101      "should succeed - just inside height limit");
102   
103   # 150 x 150 x 3 channel image uses 67500 bytes
104   ok(Imager->set_file_limits(reset=>1, bytes=>67499),
105      "set bytes limit 67499");
106   ok(!$im->read(file=>$limit_file),
107      "should fail - too many bytes");
108   print "# ",$im->errstr,"\n";
109   like($im->errstr, qr/storage size/, "check error message");
110   ok(Imager->set_file_limits(reset=>1, bytes=>67500),
111      "set bytes limit 67500");
112   ok($im->read(file=>$limit_file),
113      "should succeed - just inside bytes limit");
114   Imager->set_file_limits(reset=>1);
115 }
116
117 sub write_test {
118   my ($im, $filename, $wierdpack, $compress, $idstring) = @_;
119   local *FH;
120
121   if (open FH, "> $filename") {
122     binmode FH;
123     my $IO = Imager::io_new_fd(fileno(FH));
124     ok(Imager::i_writetga_wiol($im, $IO, $wierdpack, $compress, $idstring),
125        "write $filename")
126       or print "# ",Imager->_error_as_msg(),"\n";
127     undef $IO;
128     close FH;
129   } else {
130     fail("write $filename: open failed: $!");
131   }
132 }
133
134
135 sub read_test {
136   my ($filename, $im, %tags) = @_;
137   local *FH;
138
139   if (open FH, "< $filename") {
140     binmode FH;
141     my $IO = Imager::io_new_fd(fileno(FH));
142     my $im_read = Imager::i_readtga_wiol($IO,-1);
143     if ($im_read) {
144       my $diff = i_img_diff($im, $im_read);
145       cmp_ok($diff, '<=', $base_diff,
146              "check read image vs original");
147     } else {
148       fail("read $filename ".Imager->_error_as_msg());
149     }
150     undef $IO;
151     close FH;
152   } else {
153     fail("read $filename, open failure: $!");
154   }
155 }
156
157
158
159 sub create_test_image {
160
161   my $green  = i_color_new(0,255,0,255);
162   my $blue   = i_color_new(0,0,255,255);
163   my $red    = i_color_new(255,0,0,255);
164
165   my $img    = Imager::ImgRaw::new(150,150,3);
166
167   i_box_filled($img, 70, 25, 130, 125, $green);
168   i_box_filled($img, 20, 25,  80, 125, $blue);
169   i_arc($img, 75, 75, 30, 0, 361, $red);
170   i_conv($img, [0.1, 0.2, 0.4, 0.2, 0.1]);
171
172   return $img;
173 }