1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
#!perl -w
use strict;
use Imager;
use Imager::Test qw(is_image is_color3);
use Test::More tests => 103;
-d 'testout' or mkdir 'testout', 0777;
Imager::init_log('testout/10read.log', 2);
{
my $im_verb = Imager->new;
ok($im_verb->read(file => 'testimg/verb.rgb'), "read verbatim")
or print "# ", $im_verb->errstr, "\n";
is($im_verb->getchannels, 3, "check channels");
is($im_verb->getwidth, 20, "check width");
is($im_verb->getheight, 20, "check height");
is_color3($im_verb->getpixel(x => 0, 'y' => 0), 255, 0, 0, "check 0,0");
is_color3($im_verb->getpixel(x => 1, 'y' => 2), 255, 255, 0, "check 0,2");
is_color3($im_verb->getpixel(x => 2, 'y' => 4), 0, 255, 255, "check 2,5");
is($im_verb->tags(name => 'i_format'), 'sgi', "check i_format tag");
is($im_verb->tags(name => 'sgi_rle'), 0, "check sgi_rgb");
is($im_verb->tags(name => 'sgi_pixmin'), 0, "check pixmin");
is($im_verb->tags(name => 'sgi_pixmax'), 255, "check pixmax");
is($im_verb->tags(name => 'sgi_bpc'), 1, "check bpc");
is($im_verb->tags(name => 'i_comment'), 'test image',
"check name string");
my $im_rle = Imager->new;
ok($im_rle->read(file => 'testimg/rle.rgb'), "read rle")
or print "# ", $im_rle->errstr, "\n";
is($im_rle->tags(name => 'sgi_rle'), 1, "check sgi_rgb");
my $im_rleagr = Imager->new;
ok($im_rleagr->read(file => 'testimg/rleagr.rgb'), "read rleagr")
or print "# ", $im_rleagr->errstr, "\n";
my $im6 = Imager->new;
ok($im6->read(file => 'testimg/verb6.rgb'), "read verbatim 6-bit")
or print "# ", $im6->errstr, "\n";
is($im6->tags(name => 'sgi_pixmax'), 63, "check pixmax");
is_image($im_verb, $im_rle, "compare verbatim to rle");
is_image($im_verb, $im_rleagr, "compare verbatim to rleagr");
is_image($im_verb, $im6, "compare verbatim to verb 6-bit");
my $im_verb12 = Imager->new;
ok($im_verb12->read(file => 'testimg/verb12.rgb'), "read verbatim 12")
or print "# ", $im_verb12->errstr, "\n";
is($im_verb12->bits, 16, "check bits on verb12");
is($im_verb12->tags(name => 'sgi_pixmax'), 4095, "check pixmax");
my $im_verb16 = Imager->new;
ok($im_verb16->read(file => 'testimg/verb16.rgb'), "read verbatim 16")
or print "# ", $im_verb16->errstr, "\n";
is($im_verb16->bits, 16, "check bits on verb16");
is($im_verb16->tags(name => 'sgi_pixmax'), 65535, "check pixmax");
is_image($im_verb, $im_verb12, "compare verbatim to verb12");
is_image($im_verb, $im_verb16, "compare verbatim to verb16");
my $im_rle6 = Imager->new;
ok($im_rle6->read(file => 'testimg/rle6.rgb'), "read rle 6 bit");
is($im_rle6->tags(name => 'sgi_pixmax'), 63, 'check pixmax');
is_image($im_verb, $im_rle6, 'compare verbatim to rle6');
my $im_rle12 = Imager->new;
ok($im_rle12->read(file => 'testimg/rle12.rgb'), 'read rle 12 bit')
or print "# ", $im_rle12->errstr, "\n";
is($im_rle12->tags(name => 'sgi_pixmax'), 4095, 'check pixmax');
is_image($im_verb, $im_rle12, 'compare verbatim to rle12');
my $im_rle16 = Imager->new;
ok($im_rle16->read(file => 'testimg/rle16.rgb'), 'read rle 16 bit')
or print "# ", $im_rle16->errstr, "\n";
is($im_rle16->tags(name => 'sgi_pixmax'), 65535, 'check pixmax');
is($im_rle16->tags(name => 'sgi_bpc'), 2, "check bpc");
is_image($im_verb, $im_rle16, 'compare verbatim to rle16');
}
{
# short read tests, each is source file, limit, match, description
my @tests =
(
[
'verb.rgb', 100,
'SGI image: could not read header', 'header',
],
[
'verb.rgb', 512,
'SGI image: cannot read image data',
'verbatim image data'
],
[
'rle.rgb', 512,
'SGI image: short read reading RLE start table',
'rle start table'
],
[
'rle.rgb', 752,
'SGI image: short read reading RLE length table',
'rle length table'
],
[
'rle.rgb', 0x510,
"SGI image: cannot read RLE data",
'read rle data'
],
[
'rle.rgb', 0x50E,
"SGI image: cannot seek to RLE data",
'seek rle data'
],
[
'verb16.rgb', 512,
'SGI image: cannot read image data',
'read image data (16-bit)'
],
[
'rle16.rgb', 512,
'SGI image: short read reading RLE start table',
'rle start table (16-bit)',
],
[
'rle16.rgb', 0x42f,
'SGI image: cannot seek to RLE data',
'seek RLE data (16-bit)'
],
[
'rle16.rgb', 0x64A,
'SGI image: cannot read RLE data',
'read rle image data (16-bit)'
],
);
for my $test (@tests) {
my ($src, $size, $match, $desc) = @$test;
open SRC, "< testimg/$src"
or die "Cannot open testimg/$src: $!";
binmode SRC;
my $data;
read(SRC, $data, $size) == $size
or die "Could not read $size bytes from $src";
close SRC;
my $im = Imager->new;
ok(!$im->read(data => $data, type => 'sgi'),
"read: $desc");
is($im->errstr, $match, "error match: $desc");
}
}
{
# each entry is: source file, patches, expected error, description
my @tests =
(
[
'verb.rgb',
{ 0 => '00 00' },
'SGI image: invalid magic number',
'bad magic',
],
[
'verb.rgb',
{ 104 => '00 00 00 01' },
'SGI image: invalid value for colormap (1)',
'invalid colormap field',
],
[
'verb.rgb',
{ 3 => '03' },
'SGI image: invalid value for BPC (3)',
'invalid bpc field',
],
[
'verb.rgb',
{ 2 => '03' },
'SGI image: invalid storage type field',
'invalid storage type field',
],
[
'verb.rgb',
{ 4 => '00 04' },
'SGI image: invalid dimension field',
'invalid dimension field',
],
[
'rle.rgb',
{ 0x2f0 => '00 00 00 2b' },
'SGI image: ridiculous RLE line length 43',
'invalid rle length',
],
[
'rle.rgb',
{ 0x3E0 => '95' },
'SGI image: literal run overflows scanline',
'literal run overflow scanline',
],
[
'rle.rgb',
{ 0x3E0 => '87' },
'SGI image: literal run consumes more data than available',
'literal run consuming too much data',
],
[
'rle.rgb',
{ 0x3E0 => '15' },
'SGI image: RLE run overflows scanline',
'RLE run overflows scanline',
],
[
'rle.rgb',
{ 0x3E0 => '81 FF 12 00 01' },
'SGI image: RLE run has no data for pixel',
'RLE run has no data for pixel',
],
[
'rle.rgb',
{ 0x3E0 => '81 FF 12 00' },
'SGI image: incomplete RLE scanline',
'incomplete RLE scanline',
],
[
'rle.rgb',
{ 0x2F0 => '00 00 00 06' },
'SGI image: unused RLE data',
'unused RLE data',
],
[
'verb.rgb',
{ 0x0c => '00 00 00 FF 00 00 00 00' },
'SGI image: invalid pixmin >= pixmax',
'bad pixmin/pixmax',
],
[
'rle16.rgb',
{ 0x2f0 => '00 00 00 0B' },
'SGI image: invalid RLE length value for BPC=2',
'bad RLE table (length) (bpc=2)'
],
[
'rle16.rgb',
{ 0x2f0 => '00 00 00 53' },
'SGI image: ridiculous RLE line length 83',
'way too big RLE line length (16-bit)'
],
[
'rle16.rgb',
{ 0x426 => '00 95' },
'SGI image: literal run overflows scanline',
'literal overflow scanline (bpc=2)'
],
[
'rle16.rgb',
{ 0x426 => '00 93' },
'SGI image: literal run consumes more data than available',
'literal overflow data (bpc=2)'
],
[
'rle16.rgb',
{ 0x3EA => '00 15' },
'SGI image: RLE run overflows scanline',
'rle overflow scanline (bpc=2)'
],
[
'rle16.rgb',
{ 0x3EA => '00 15' },
'SGI image: RLE run overflows scanline',
'rle overflow scanline (bpc=2)'
],
[
'rle16.rgb',
{ 0x3EA => '00 83 ff ff ff ff ff ff 00 01' },
'SGI image: RLE run has no data for pixel',
'rle code no argument (bpc=2)'
],
[
'rle16.rgb',
{ 0x3EA => '00 14 ff ff 00 00' },
'SGI image: unused RLE data',
'unused RLE data (bpc=2)'
],
[
'rle16.rgb',
{ 0x3EA => '00 12 ff ff' },
'SGI image: incomplete RLE scanline',
'incomplete rle scanline (bpc=2)'
],
);
# invalid file tests - take our original files and patch them a
# little to make them invalid
my $test_index = 0;
for my $test (@tests) {
my ($filename, $patches, $error, $desc) = @$test;
my $data = load_patched_file("testimg/$filename", $patches);
my $im = Imager->new;
ok(!$im->read(data => $data, type=>'sgi'),
"$test_index - $desc:should fail to read");
is($im->errstr, $error, "$test_index - $desc:check message");
++$test_index;
}
}
sub load_patched_file {
my ($filename, $patches) = @_;
open IMDATA, "< $filename"
or die "Cannot open $filename: $!";
binmode IMDATA;
my $data = do { local $/; <IMDATA> };
for my $offset (keys %$patches) {
(my $hdata = $patches->{$offset}) =~ tr/ //d;
my $pdata = pack("H*", $hdata);
substr($data, $offset, length $pdata) = $pdata;
}
return $data;
}
|