]> git.imager.perl.org - imager.git/blob - t/t108tga.t
new samples
[imager.git] / t / t108tga.t
1 #!perl -w
2 print "1..21\n";
3 use Imager qw(:all);
4 use strict;
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(1, $img, "testout/t108_24bit.tga", 0, 0, "");
13 write_test(2, $img, "testout/t108_24bit_rle.tga", 0, 1, "");
14 write_test(3, $img, "testout/t108_15bit.tga", 1, 1, "");
15 write_test(4, $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(5, $im8, "testout/t108_8bit.tga", 0, 0, "");
22 write_test(6, $im8, "testout/t108_8bit_rle.tga", 0, 1, "");
23 write_test(7, $im8, "testout/t108_8_15bit.tga", 1, 0, "");
24 write_test(8, $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(9, $im4, "testout/t108_4bit.tga", 0, 1, "");
44 write_test(10, $im1, "testout/t108_1bit.tga", 0, 1, "This is a comment!");
45
46 read_test(11, "testout/t108_24bit.tga", $img);
47 read_test(12, "testout/t108_8bit.tga",  $im8);
48 read_test(13, "testout/t108_4bit.tga",  $im4);
49 read_test(14, "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 if ($imoo->read(file=>'testout/t108_24bit.tga')) {
59   print "ok 15\n";
60 } else {
61   print "not ok 15 # ",$imoo->errstr,"\n";
62 }
63
64 if ($imoo->write(file=>'testout/t108_oo.tga')) {
65   print "ok 16\n";
66 } else {
67   print "not ok 16 # ",$imoo->errstr,"\n";
68 }
69
70 my ($type) = $imoo->tags(name=>'i_format');
71 isn(17, $type, 'tga', "check i_format tag");
72
73 # in 0.44 and earlier, reading an image with an idstring of 128 or more
74 # bytes would result in an allocation error, if the platform char type
75 # was signed
76 $imoo = Imager->new;
77 okn(18, $imoo->read(file=>'testimg/longid.tga'), "read long id image");
78 my ($id) = $imoo->tags(name=>'tga_idstring');
79 isn(19, $id, "X" x 128, "check tga_idstring tag");
80 my ($bitspp) = $imoo->tags(name=>'tga_bitspp');
81 isn(20, $bitspp, 24, "check tga_bitspp tag");
82 my ($compressed) = $imoo->tags(name=>'compressed');
83 isn(21, $compressed, 1, "check compressed tag");
84
85 sub write_test {
86   my ($test_num, $im, $filename, $wierdpack, $compress, $idstring) = @_;
87   local *FH;
88
89   if (open FH, "> $filename") {
90     binmode FH;
91     my $IO = Imager::io_new_fd(fileno(FH));
92     if (Imager::i_writetga_wiol($im, $IO, $wierdpack, $compress, $idstring)) {
93       print "ok $test_num\n";
94     } else {
95       print "not ok $test_num # ",Imager->_error_as_msg(),"\n";
96     }
97     undef $IO;
98     close FH;
99   } else {
100     print "not ok $test_num # $!\n";
101   }
102 }
103
104
105 sub read_test {
106   my ($test_num, $filename, $im, %tags) = @_;
107   local *FH;
108
109   if (open FH, "< $filename") {
110     binmode FH;
111     my $IO = Imager::io_new_fd(fileno(FH));
112     my $im_read = Imager::i_readtga_wiol($IO,-1);
113     if ($im_read) {
114       my $diff = i_img_diff($im, $im_read);
115       if ($diff > $base_diff) {
116         print "not ok $test_num # image mismatch $diff\n";
117       }
118       print "ok $test_num\n";
119     } else {
120       print "not ok $test_num # ",Imager->_error_as_msg(),"\n";
121     }
122     undef $IO;
123     close FH;
124   } else {
125     print "not ok $test_num # $!\n";
126   }
127 }
128
129
130
131 sub create_test_image {
132
133   my $green  = i_color_new(0,255,0,255);
134   my $blue   = i_color_new(0,0,255,255);
135   my $red    = i_color_new(255,0,0,255);
136
137   my $img    = Imager::ImgRaw::new(150,150,3);
138
139   i_box_filled($img, 70, 25, 130, 125, $green);
140   i_box_filled($img, 20, 25,  80, 125, $blue);
141   i_arc($img, 75, 75, 30, 0, 361, $red);
142   i_conv($img, [0.1, 0.2, 0.4, 0.2, 0.1]);
143
144   return $img;
145 }