4 use Test::More tests=>36;
5 BEGIN { require "t/testtools.pl"; }
6 init_log("testout/t108tga.log",1);
9 my $img = create_test_image();
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, "");
17 # 'webmap' is noticably faster than the default
18 my $im8 = Imager::i_img_to_pal($img, { make_colors=>'webmap',
19 translate=>'errdiff'});
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, "");
27 # use a fixed palette so we get reproducible results for the compressed
30 my @bit4 = map { NC($_) }
31 qw(605844 966600 0148b2 00f800 bf0a33 5e009e
32 2ead1b 0000f8 004b01 fd0000 0e1695 000002);
34 my @bit1 = (NC(0, 0, 0), NC(176, 160, 144));
36 my $im4 = Imager::i_img_to_pal($img, { colors=>\@bit4,
37 make_colors=>'none' });
39 my $im1 = Imager::i_img_to_pal($img, { colors=>\@bit1,
41 translate=>'errdiff' });
43 write_test($im4, "testout/t108_4bit.tga", 0, 1, "");
44 write_test($im1, "testout/t108_1bit.tga", 0, 1, "This is a comment!");
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);
51 # the following might have slight differences
53 $base_diff = i_img_diff($img, $im8) * 2;
55 print "# base difference $base_diff\n";
57 my $imoo = Imager->new;
58 ok($imoo->read(file=>'testout/t108_24bit.tga'),
60 or print "# ",$imoo->errstr,"\n";
62 ok($imoo->write(file=>'testout/t108_oo.tga'),
64 or print "# ",$imoo->errstr,"\n";
66 my ($type) = $imoo->tags(name=>'i_format');
67 is($type, 'tga', "check i_format tag");
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
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");
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");
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");
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");
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");
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);
118 my ($im, $filename, $wierdpack, $compress, $idstring) = @_;
121 if (open FH, "> $filename") {
123 my $IO = Imager::io_new_fd(fileno(FH));
124 ok(Imager::i_writetga_wiol($im, $IO, $wierdpack, $compress, $idstring),
126 or print "# ",Imager->_error_as_msg(),"\n";
130 fail("write $filename: open failed: $!");
136 my ($filename, $im, %tags) = @_;
139 if (open FH, "< $filename") {
141 my $IO = Imager::io_new_fd(fileno(FH));
142 my $im_read = Imager::i_readtga_wiol($IO,-1);
144 my $diff = i_img_diff($im, $im_read);
145 cmp_ok($diff, '<=', $base_diff,
146 "check read image vs original");
148 fail("read $filename ".Imager->_error_as_msg());
153 fail("read $filename, open failure: $!");
159 sub create_test_image {
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);
165 my $img = Imager::ImgRaw::new(150,150,3);
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]);