]>
Commit | Line | Data |
---|---|---|
74957262 | 1 | #!perl -w |
74957262 AMH |
2 | use Imager qw(:all); |
3 | use strict; | |
0389bf49 | 4 | use Test::More tests=>38; |
fe055ff6 | 5 | BEGIN { require "t/testtools.pl"; } |
74957262 AMH |
6 | init_log("testout/t108tga.log",1); |
7 | ||
8 | ||
9 | my $img = create_test_image(); | |
10 | my $base_diff = 0; | |
11 | ||
77157728 TC |
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, ""); | |
74957262 AMH |
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 | ||
77157728 TC |
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, ""); | |
74957262 AMH |
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 | ||
77157728 TC |
43 | write_test($im4, "testout/t108_4bit.tga", 0, 1, ""); |
44 | write_test($im1, "testout/t108_1bit.tga", 0, 1, "This is a comment!"); | |
74957262 | 45 | |
77157728 TC |
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); | |
74957262 AMH |
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; | |
77157728 TC |
58 | ok($imoo->read(file=>'testout/t108_24bit.tga'), |
59 | "OO read image") | |
60 | or print "# ",$imoo->errstr,"\n"; | |
74957262 | 61 | |
77157728 TC |
62 | ok($imoo->write(file=>'testout/t108_oo.tga'), |
63 | "OO write image") | |
64 | or print "# ",$imoo->errstr,"\n"; | |
74957262 | 65 | |
fe055ff6 | 66 | my ($type) = $imoo->tags(name=>'i_format'); |
77157728 | 67 | is($type, 'tga', "check i_format tag"); |
74957262 | 68 | |
b96be931 TC |
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; | |
77157728 | 73 | ok($imoo->read(file=>'testimg/longid.tga'), "read long id image"); |
b96be931 | 74 | my ($id) = $imoo->tags(name=>'tga_idstring'); |
77157728 | 75 | is($id, "X" x 128, "check tga_idstring tag"); |
b96be931 | 76 | my ($bitspp) = $imoo->tags(name=>'tga_bitspp'); |
77157728 | 77 | is($bitspp, 24, "check tga_bitspp tag"); |
b96be931 | 78 | my ($compressed) = $imoo->tags(name=>'compressed'); |
77157728 TC |
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 | } | |
74957262 | 116 | |
0389bf49 TC |
117 | { # Issue # 18397 |
118 | # the issue is for 4 channel images to jpeg, but 2 channel images have | |
119 | # a similar problem on tga | |
120 | my $im = Imager->new(xsize=>100, ysize=>100, channels => 2); | |
121 | my $data; | |
122 | ok(!$im->write(data => \$data, type=>'tga'), | |
123 | "check failure of writing a 2 channel image"); | |
124 | is($im->errstr, "Cannot store 2 channel image in targa format", | |
125 | "check the error message"); | |
126 | } | |
127 | ||
74957262 | 128 | sub write_test { |
77157728 | 129 | my ($im, $filename, $wierdpack, $compress, $idstring) = @_; |
74957262 AMH |
130 | local *FH; |
131 | ||
132 | if (open FH, "> $filename") { | |
133 | binmode FH; | |
134 | my $IO = Imager::io_new_fd(fileno(FH)); | |
77157728 TC |
135 | ok(Imager::i_writetga_wiol($im, $IO, $wierdpack, $compress, $idstring), |
136 | "write $filename") | |
137 | or print "# ",Imager->_error_as_msg(),"\n"; | |
74957262 AMH |
138 | undef $IO; |
139 | close FH; | |
140 | } else { | |
77157728 | 141 | fail("write $filename: open failed: $!"); |
74957262 AMH |
142 | } |
143 | } | |
144 | ||
145 | ||
146 | sub read_test { | |
77157728 | 147 | my ($filename, $im, %tags) = @_; |
74957262 AMH |
148 | local *FH; |
149 | ||
150 | if (open FH, "< $filename") { | |
151 | binmode FH; | |
152 | my $IO = Imager::io_new_fd(fileno(FH)); | |
153 | my $im_read = Imager::i_readtga_wiol($IO,-1); | |
154 | if ($im_read) { | |
155 | my $diff = i_img_diff($im, $im_read); | |
77157728 TC |
156 | cmp_ok($diff, '<=', $base_diff, |
157 | "check read image vs original"); | |
74957262 | 158 | } else { |
77157728 | 159 | fail("read $filename ".Imager->_error_as_msg()); |
74957262 AMH |
160 | } |
161 | undef $IO; | |
162 | close FH; | |
163 | } else { | |
77157728 | 164 | fail("read $filename, open failure: $!"); |
74957262 AMH |
165 | } |
166 | } | |
167 | ||
168 | ||
169 | ||
170 | sub create_test_image { | |
171 | ||
172 | my $green = i_color_new(0,255,0,255); | |
173 | my $blue = i_color_new(0,0,255,255); | |
174 | my $red = i_color_new(255,0,0,255); | |
175 | ||
176 | my $img = Imager::ImgRaw::new(150,150,3); | |
177 | ||
178 | i_box_filled($img, 70, 25, 130, 125, $green); | |
179 | i_box_filled($img, 20, 25, 80, 125, $blue); | |
180 | i_arc($img, 75, 75, 30, 0, 361, $red); | |
181 | i_conv($img, [0.1, 0.2, 0.4, 0.2, 0.1]); | |
182 | ||
183 | return $img; | |
184 | } |