4 use Imager::Test qw(is_image is_color3);
5 use Test::More tests => 103;
7 -d 'testout' or mkdir 'testout', 0777;
9 Imager::init_log('testout/10read.log', 2);
12 my $im_verb = Imager->new;
13 ok($im_verb->read(file => 'testimg/verb.rgb'), "read verbatim")
14 or print "# ", $im_verb->errstr, "\n";
15 is($im_verb->getchannels, 3, "check channels");
16 is($im_verb->getwidth, 20, "check width");
17 is($im_verb->getheight, 20, "check height");
18 is_color3($im_verb->getpixel(x => 0, 'y' => 0), 255, 0, 0, "check 0,0");
19 is_color3($im_verb->getpixel(x => 1, 'y' => 2), 255, 255, 0, "check 0,2");
20 is_color3($im_verb->getpixel(x => 2, 'y' => 4), 0, 255, 255, "check 2,5");
21 is($im_verb->tags(name => 'i_format'), 'sgi', "check i_format tag");
22 is($im_verb->tags(name => 'sgi_rle'), 0, "check sgi_rgb");
23 is($im_verb->tags(name => 'sgi_pixmin'), 0, "check pixmin");
24 is($im_verb->tags(name => 'sgi_pixmax'), 255, "check pixmax");
25 is($im_verb->tags(name => 'sgi_bpc'), 1, "check bpc");
26 is($im_verb->tags(name => 'i_comment'), 'test image',
29 my $im_rle = Imager->new;
30 ok($im_rle->read(file => 'testimg/rle.rgb'), "read rle")
31 or print "# ", $im_rle->errstr, "\n";
32 is($im_rle->tags(name => 'sgi_rle'), 1, "check sgi_rgb");
34 my $im_rleagr = Imager->new;
35 ok($im_rleagr->read(file => 'testimg/rleagr.rgb'), "read rleagr")
36 or print "# ", $im_rleagr->errstr, "\n";
38 my $im6 = Imager->new;
39 ok($im6->read(file => 'testimg/verb6.rgb'), "read verbatim 6-bit")
40 or print "# ", $im6->errstr, "\n";
41 is($im6->tags(name => 'sgi_pixmax'), 63, "check pixmax");
43 is_image($im_verb, $im_rle, "compare verbatim to rle");
44 is_image($im_verb, $im_rleagr, "compare verbatim to rleagr");
45 is_image($im_verb, $im6, "compare verbatim to verb 6-bit");
47 my $im_verb12 = Imager->new;
48 ok($im_verb12->read(file => 'testimg/verb12.rgb'), "read verbatim 12")
49 or print "# ", $im_verb12->errstr, "\n";
50 is($im_verb12->bits, 16, "check bits on verb12");
51 is($im_verb12->tags(name => 'sgi_pixmax'), 4095, "check pixmax");
53 my $im_verb16 = Imager->new;
54 ok($im_verb16->read(file => 'testimg/verb16.rgb'), "read verbatim 16")
55 or print "# ", $im_verb16->errstr, "\n";
56 is($im_verb16->bits, 16, "check bits on verb16");
57 is($im_verb16->tags(name => 'sgi_pixmax'), 65535, "check pixmax");
59 is_image($im_verb, $im_verb12, "compare verbatim to verb12");
60 is_image($im_verb, $im_verb16, "compare verbatim to verb16");
62 my $im_rle6 = Imager->new;
63 ok($im_rle6->read(file => 'testimg/rle6.rgb'), "read rle 6 bit");
64 is($im_rle6->tags(name => 'sgi_pixmax'), 63, 'check pixmax');
65 is_image($im_verb, $im_rle6, 'compare verbatim to rle6');
67 my $im_rle12 = Imager->new;
68 ok($im_rle12->read(file => 'testimg/rle12.rgb'), 'read rle 12 bit')
69 or print "# ", $im_rle12->errstr, "\n";
70 is($im_rle12->tags(name => 'sgi_pixmax'), 4095, 'check pixmax');
71 is_image($im_verb, $im_rle12, 'compare verbatim to rle12');
73 my $im_rle16 = Imager->new;
74 ok($im_rle16->read(file => 'testimg/rle16.rgb'), 'read rle 16 bit')
75 or print "# ", $im_rle16->errstr, "\n";
76 is($im_rle16->tags(name => 'sgi_pixmax'), 65535, 'check pixmax');
77 is($im_rle16->tags(name => 'sgi_bpc'), 2, "check bpc");
78 is_image($im_verb, $im_rle16, 'compare verbatim to rle16');
82 # short read tests, each is source file, limit, match, description
87 'SGI image: could not read header', 'header',
91 'SGI image: cannot read image data',
96 'SGI image: short read reading RLE start table',
101 'SGI image: short read reading RLE length table',
106 "SGI image: cannot read RLE data",
111 "SGI image: cannot seek to RLE data",
116 'SGI image: cannot read image data',
117 'read image data (16-bit)'
121 'SGI image: short read reading RLE start table',
122 'rle start table (16-bit)',
126 'SGI image: cannot seek to RLE data',
127 'seek RLE data (16-bit)'
131 'SGI image: cannot read RLE data',
132 'read rle image data (16-bit)'
135 for my $test (@tests) {
136 my ($src, $size, $match, $desc) = @$test;
137 open SRC, "< testimg/$src"
138 or die "Cannot open testimg/$src: $!";
141 read(SRC, $data, $size) == $size
142 or die "Could not read $size bytes from $src";
144 my $im = Imager->new;
145 ok(!$im->read(data => $data, type => 'sgi'),
147 is($im->errstr, $match, "error match: $desc");
152 # each entry is: source file, patches, expected error, description
158 'SGI image: invalid magic number',
163 { 104 => '00 00 00 01' },
164 'SGI image: invalid value for colormap (1)',
165 'invalid colormap field',
170 'SGI image: invalid value for BPC (3)',
176 'SGI image: invalid storage type field',
177 'invalid storage type field',
182 'SGI image: invalid dimension field',
183 'invalid dimension field',
187 { 0x2f0 => '00 00 00 2b' },
188 'SGI image: ridiculous RLE line length 43',
189 'invalid rle length',
194 'SGI image: literal run overflows scanline',
195 'literal run overflow scanline',
200 'SGI image: literal run consumes more data than available',
201 'literal run consuming too much data',
206 'SGI image: RLE run overflows scanline',
207 'RLE run overflows scanline',
211 { 0x3E0 => '81 FF 12 00 01' },
212 'SGI image: RLE run has no data for pixel',
213 'RLE run has no data for pixel',
217 { 0x3E0 => '81 FF 12 00' },
218 'SGI image: incomplete RLE scanline',
219 'incomplete RLE scanline',
223 { 0x2F0 => '00 00 00 06' },
224 'SGI image: unused RLE data',
229 { 0x0c => '00 00 00 FF 00 00 00 00' },
230 'SGI image: invalid pixmin >= pixmax',
235 { 0x2f0 => '00 00 00 0B' },
236 'SGI image: invalid RLE length value for BPC=2',
237 'bad RLE table (length) (bpc=2)'
241 { 0x2f0 => '00 00 00 53' },
242 'SGI image: ridiculous RLE line length 83',
243 'way too big RLE line length (16-bit)'
247 { 0x426 => '00 95' },
248 'SGI image: literal run overflows scanline',
249 'literal overflow scanline (bpc=2)'
253 { 0x426 => '00 93' },
254 'SGI image: literal run consumes more data than available',
255 'literal overflow data (bpc=2)'
259 { 0x3EA => '00 15' },
260 'SGI image: RLE run overflows scanline',
261 'rle overflow scanline (bpc=2)'
265 { 0x3EA => '00 15' },
266 'SGI image: RLE run overflows scanline',
267 'rle overflow scanline (bpc=2)'
271 { 0x3EA => '00 83 ff ff ff ff ff ff 00 01' },
272 'SGI image: RLE run has no data for pixel',
273 'rle code no argument (bpc=2)'
277 { 0x3EA => '00 14 ff ff 00 00' },
278 'SGI image: unused RLE data',
279 'unused RLE data (bpc=2)'
283 { 0x3EA => '00 12 ff ff' },
284 'SGI image: incomplete RLE scanline',
285 'incomplete rle scanline (bpc=2)'
289 # invalid file tests - take our original files and patch them a
290 # little to make them invalid
292 for my $test (@tests) {
293 my ($filename, $patches, $error, $desc) = @$test;
295 my $data = load_patched_file("testimg/$filename", $patches);
296 my $im = Imager->new;
297 ok(!$im->read(data => $data, type=>'sgi'),
298 "$test_index - $desc:should fail to read");
299 is($im->errstr, $error, "$test_index - $desc:check message");
304 sub load_patched_file {
305 my ($filename, $patches) = @_;
307 open IMDATA, "< $filename"
308 or die "Cannot open $filename: $!";
310 my $data = do { local $/; <IMDATA> };
311 for my $offset (keys %$patches) {
312 (my $hdata = $patches->{$offset}) =~ tr/ //d;
313 my $pdata = pack("H*", $hdata);
314 substr($data, $offset, length $pdata) = $pdata;