| 1 | #!perl -w | ||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | 1 | ▶ | use Imager; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 4 | 1 | ▶ | use Test::More tests => 57; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 5 | 1 | ▶ | use Imager::Test qw(test_image test_image_16 is_image); | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 6 | 1 | ▶ | use IO::Seekable; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 7 | |||||||||||||||||||
| 8 | 1 | ▶ | -d 'testout' or mkdir 'testout', 0777; | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 9 | |||||||||||||||||||
| 10 | 1 | Imager::init_log('testout/20write.log', 2); | |||||||||||||||||
| 11 | |||||||||||||||||||
| 12 | { | ||||||||||||||||||
| 13 | 1 | my $im = test_image(); | |||||||||||||||||
| 14 | 1 | $im->line(x1 => 0, y1 => 0, x2 => 150, y2 => 150, color => 'FF0000'); | |||||||||||||||||
| 15 | 1 | ▶ | ok($im->write(file => 'testout/20verb.rgb'), "write 8-bit verbatim") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 16 | or print "# ", $im->errstr, "\n"; | ||||||||||||||||||
| 17 | 1 | my $im2 = Imager->new; | |||||||||||||||||
| 18 | 1 | ▶ | ok($im2->read(file => 'testout/20verb.rgb'), "read it back") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 19 | or print "# ", $im2->errstr, "\n"; | ||||||||||||||||||
| 20 | 1 | is_image($im, $im2, "compare"); | |||||||||||||||||
| 21 | 1 | is($im2->tags(name => 'sgi_rle'), 0, "check not rle"); | |||||||||||||||||
| 22 | 1 | is($im2->tags(name => 'sgi_bpc'), 1, "check bpc"); | |||||||||||||||||
| 23 | 1 | is($im2->tags(name => 'i_comment'), undef, "no namestr"); | |||||||||||||||||
| 24 | |||||||||||||||||||
| 25 | 1 | ▶ | ok($im->write(file => 'testout/20rle.rgb', | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 26 | sgi_rle => 1, | ||||||||||||||||||
| 27 | i_comment => "test"), "write 8-bit rle") | ||||||||||||||||||
| 28 | or print "# ", $im->errstr, "\n"; | ||||||||||||||||||
| 29 | 1 | my $im3 = Imager->new; | |||||||||||||||||
| 30 | 1 | ▶ | ok($im3->read(file => 'testout/20rle.rgb'), "read it back") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 31 | or print "# ", $im3->errstr, "\n"; | ||||||||||||||||||
| 32 | 1 | is_image($im, $im3, "compare"); | |||||||||||||||||
| 33 | 1 | is($im3->tags(name => 'sgi_rle'), 1, "check not rle"); | |||||||||||||||||
| 34 | 1 | is($im3->tags(name => 'sgi_bpc'), 1, "check bpc"); | |||||||||||||||||
| 35 | 1 | is($im3->tags(name => 'i_comment'), 'test', "check i_comment set"); | |||||||||||||||||
| 36 | } | ||||||||||||||||||
| 37 | |||||||||||||||||||
| 38 | { | ||||||||||||||||||
| 39 | 1 | my $im = test_image_16(); | |||||||||||||||||
| 40 | 1 | $im->line(x1 => 0, y1 => 0, x2 => 150, y2 => 150, color => 'FF0000'); | |||||||||||||||||
| 41 | 1 | ▶ | ok($im->write(file => 'testout/20verb16.rgb'), "write 16-bit verbatim") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 42 | or print "# ", $im->errstr, "\n"; | ||||||||||||||||||
| 43 | 1 | my $im2 = Imager->new; | |||||||||||||||||
| 44 | 1 | ▶ | ok($im2->read(file => 'testout/20verb16.rgb'), "read it back") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 45 | or print "# ", $im2->errstr, "\n"; | ||||||||||||||||||
| 46 | 1 | is_image($im, $im2, "compare"); | |||||||||||||||||
| 47 | 1 | is($im2->tags(name => 'sgi_rle'), 0, "check not rle"); | |||||||||||||||||
| 48 | 1 | is($im2->tags(name => 'sgi_bpc'), 2, "check bpc"); | |||||||||||||||||
| 49 | 1 | is($im2->tags(name => 'i_comment'), undef, "no namestr"); | |||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | 1 | ▶ | ok($im->write(file => 'testout/20rle16.rgb', | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 52 | sgi_rle => 1, | ||||||||||||||||||
| 53 | i_comment => "test"), "write 16-bit rle") | ||||||||||||||||||
| 54 | or print "# ", $im->errstr, "\n"; | ||||||||||||||||||
| 55 | 1 | my $im3 = Imager->new; | |||||||||||||||||
| 56 | 1 | ▶ | ok($im3->read(file => 'testout/20rle16.rgb'), "read it back") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 57 | or print "# ", $im3->errstr, "\n"; | ||||||||||||||||||
| 58 | 1 | is_image($im, $im3, "compare"); | |||||||||||||||||
| 59 | 1 | is($im3->tags(name => 'sgi_rle'), 1, "check not rle"); | |||||||||||||||||
| 60 | 1 | is($im3->tags(name => 'sgi_bpc'), 2, "check bpc"); | |||||||||||||||||
| 61 | 1 | is($im3->tags(name => 'i_comment'), 'test', "check i_comment set"); | |||||||||||||||||
| 62 | |||||||||||||||||||
| 63 | 1 | my $imbig = Imager->new(xsize => 300, ysize => 300, bits => 16); | |||||||||||||||||
| 64 | 1 | $imbig->paste(src => $im, tx => 0, ty => 0); | |||||||||||||||||
| 65 | 1 | $imbig->paste(src => $im, tx => 150, ty => 0); | |||||||||||||||||
| 66 | 1 | $imbig->paste(src => $im, tx => 0, ty => 150); | |||||||||||||||||
| 67 | 1 | $imbig->paste(src => $im, tx => 150, ty => 150); | |||||||||||||||||
| 68 | 1 | for my $t (0 .. 74) { | |||||||||||||||||
| 69 | 75 | $imbig->line(x1 => $t*4, y1 => 0, x2 => 3+$t*4, y2 => 299, | |||||||||||||||||
| 70 | color => [ 255 - $t, 0, 0 ]); | ||||||||||||||||||
| 71 | } | ||||||||||||||||||
| 72 | 1 | my $data; | |||||||||||||||||
| 73 | 1 | ok($imbig->write(data => \$data, type => 'sgi', sgi_rle => 1), | |||||||||||||||||
| 74 | "write larger image"); | ||||||||||||||||||
| 75 | 1 | cmp_ok(length($data), '>', 0x10000, "check output large enough for test"); | |||||||||||||||||
| 76 | 1 | print "# ", length $data, "\n"; | |||||||||||||||||
| 77 | 1 | my $imbigcmp = Imager->new; | |||||||||||||||||
| 78 | 1 | ok($imbigcmp->read(data => $data), "read larger image"); | |||||||||||||||||
| 79 | 1 | is_image($imbig, $imbigcmp, "check large image matches"); | |||||||||||||||||
| 80 | } | ||||||||||||||||||
| 81 | |||||||||||||||||||
| 82 | { | ||||||||||||||||||
| 83 | # grey scale check | ||||||||||||||||||
| 84 | 1 | my $im = test_image()->convert(preset=>'grey'); | |||||||||||||||||
| 85 | 1 | ▶ | ok($im->write(file => 'testout/20vgray8.bw'), "write 8-bit verbatim grey") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 86 | or print "# ", $im->errstr, "\n"; | ||||||||||||||||||
| 87 | 1 | my $im2 = Imager->new; | |||||||||||||||||
| 88 | 1 | ▶ | ok($im2->read(file => 'testout/20vgray8.bw'), "read it back") | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 89 | or print "# ", $im2->errstr, "\n"; | ||||||||||||||||||
| 90 | 1 | is_image($im, $im2, "compare"); | |||||||||||||||||
| 91 | 1 | is($im2->tags(name => 'i_format'), 'sgi', "check we saved as SGI"); | |||||||||||||||||
| 92 | 1 | is($im2->tags(name => 'sgi_rle'), 0, "check not rle"); | |||||||||||||||||
| 93 | 1 | is($im2->tags(name => 'sgi_bpc'), 1, "check bpc"); | |||||||||||||||||
| 94 | 1 | is($im2->tags(name => 'i_comment'), undef, "no namestr"); | |||||||||||||||||
| 95 | } | ||||||||||||||||||
| 96 | |||||||||||||||||||
| 97 | { | ||||||||||||||||||
| 98 | # write failure tests | ||||||||||||||||||
| 99 | 1 | my $rgb8 = test_image(); | |||||||||||||||||
| 100 | 1 | my $rgb16 = test_image_16(); | |||||||||||||||||
| 101 | 1 | my $rgb8rle = $rgb8->copy; | |||||||||||||||||
| 102 | 1 | $rgb8rle->settag(name => 'sgi_rle', value => 1); | |||||||||||||||||
| 103 | 1 | my $grey8 = $rgb8->convert(preset => 'grey'); | |||||||||||||||||
| 104 | 1 | my $grey16 = $rgb16->convert(preset => 'grey'); | |||||||||||||||||
| 105 | 1 | my $grey16rle = $grey16->copy; | |||||||||||||||||
| 106 | 1 | $grey16rle->settag(name => 'sgi_rle', value => 1); | |||||||||||||||||
| 107 | |||||||||||||||||||
| 108 | 1 | my @tests = | |||||||||||||||||
| 109 | ( | ||||||||||||||||||
| 110 | # each entry is: image, limit, expected msg, description | ||||||||||||||||||
| 111 | [ | ||||||||||||||||||
| 112 | $rgb8, 500, | ||||||||||||||||||
| 113 | 'SGI image: cannot write header', | ||||||||||||||||||
| 114 | 'writing header' | ||||||||||||||||||
| 115 | ], | ||||||||||||||||||
| 116 | [ | ||||||||||||||||||
| 117 | $rgb8, 1024, | ||||||||||||||||||
| 118 | 'SGI image: error writing image data', | ||||||||||||||||||
| 119 | '8-bit image data' | ||||||||||||||||||
| 120 | ], | ||||||||||||||||||
| 121 | [ | ||||||||||||||||||
| 122 | $grey8, 513, | ||||||||||||||||||
| 123 | 'SGI image: error writing image data', | ||||||||||||||||||
| 124 | '8-bit image data (grey)' | ||||||||||||||||||
| 125 | ], | ||||||||||||||||||
| 126 | [ | ||||||||||||||||||
| 127 | $rgb8rle, 513, | ||||||||||||||||||
| 128 | 'SGI image: error writing offsets/lengths', | ||||||||||||||||||
| 129 | 'rle tables, 8 bit', | ||||||||||||||||||
| 130 | ], | ||||||||||||||||||
| 131 | [ | ||||||||||||||||||
| 132 | $rgb8rle, 4112, | ||||||||||||||||||
| 133 | 'SGI image: error writing RLE data', | ||||||||||||||||||
| 134 | '8-bit rle data', | ||||||||||||||||||
| 135 | ], | ||||||||||||||||||
| 136 | [ | ||||||||||||||||||
| 137 | $rgb8rle, 14707, | ||||||||||||||||||
| 138 | 'SGI image: cannot write final RLE table', | ||||||||||||||||||
| 139 | '8-bit rewrite RLE table', | ||||||||||||||||||
| 140 | ], | ||||||||||||||||||
| 141 | [ | ||||||||||||||||||
| 142 | $rgb16, 513, | ||||||||||||||||||
| 143 | 'SGI image: error writing image data', | ||||||||||||||||||
| 144 | '16-bit image data', | ||||||||||||||||||
| 145 | ], | ||||||||||||||||||
| 146 | [ | ||||||||||||||||||
| 147 | $grey16rle, 513, | ||||||||||||||||||
| 148 | 'SGI image: error writing offsets/lengths', | ||||||||||||||||||
| 149 | 'rle tables, 16 bit', | ||||||||||||||||||
| 150 | ], | ||||||||||||||||||
| 151 | [ | ||||||||||||||||||
| 152 | $grey16rle, 1713, | ||||||||||||||||||
| 153 | 'SGI image: error writing RLE data', | ||||||||||||||||||
| 154 | '16-bit rle data', | ||||||||||||||||||
| 155 | ], | ||||||||||||||||||
| 156 | [ | ||||||||||||||||||
| 157 | $grey16rle, 10871, | ||||||||||||||||||
| 158 | 'SGI image: cannot write final RLE table', | ||||||||||||||||||
| 159 | '16-bit rewrite RLE table', | ||||||||||||||||||
| 160 | ], | ||||||||||||||||||
| 161 | ); | ||||||||||||||||||
| 162 | 1 | for my $test (@tests) { | |||||||||||||||||
| 163 | 10 | my ($im, $limit, $expected_msg, $desc) = @$test; | |||||||||||||||||
| 164 | 10 | my $io = limited_write_io($limit); | |||||||||||||||||
| 165 | 10 | ok(!$im->write(type => 'sgi', io => $io), | |||||||||||||||||
| 166 | "write should fail - $desc"); | ||||||||||||||||||
| 167 | 10 | is($im->errstr, "$expected_msg: limit reached", "check error - $desc"); | |||||||||||||||||
| 168 | } | ||||||||||||||||||
| 169 | } | ||||||||||||||||||
| 170 | |||||||||||||||||||
| 171 | |||||||||||||||||||
| 172 | { # check close failures are handled correctly | ||||||||||||||||||
| 173 | 1 | my $im = test_image(); | |||||||||||||||||
| 174 | 1 | ▶ | my $fail_close = sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 175 | 1 | Imager::i_push_error(0, "synthetic close failure"); | |||||||||||||||||
| 176 | 1 | return 0; | |||||||||||||||||
| 177 | 1 | }; | |||||||||||||||||
| 178 | 9 | ▶ | ok(!$im->write(type => "sgi", callback => sub { 1 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 179 | 1 | closecb => $fail_close), | |||||||||||||||||
| 180 | "check failing close fails"); | ||||||||||||||||||
| 181 | 1 | like($im->errstr, qr/synthetic close failure/, | |||||||||||||||||
| 182 | "check error message"); | ||||||||||||||||||
| 183 | } | ||||||||||||||||||
| 184 | |||||||||||||||||||
| 185 | 10 | ▶ | sub limited_write_io { | ||||||||||||||||
|
Subroutine
limited_write_io: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 186 | 10 | my ($limit) = @_; | |||||||||||||||||
| 187 | |||||||||||||||||||
| 188 | 10 | my ($writecb, $seekcb) = limited_write($limit); | |||||||||||||||||
| 189 | |||||||||||||||||||
| 190 | 10 | my $io = Imager::io_new_cb($writecb, undef, $seekcb, undef, 1); | |||||||||||||||||
| 191 | 10 | $io->set_buffered(0); | |||||||||||||||||
| 192 | |||||||||||||||||||
| 193 | 10 | return $io; | |||||||||||||||||
| 194 | } | ||||||||||||||||||
| 195 | |||||||||||||||||||
| 196 | 10 | ▶ | sub limited_write { | ||||||||||||||||
|
Subroutine
limited_write: called CC=1 SCAR=0.2
| |||||||||||||||||||
| 197 | 10 | my ($limit) = @_; | |||||||||||||||||
| 198 | |||||||||||||||||||
| 199 | 10 | my $pos = 0; | |||||||||||||||||
| 200 | 10 | my $written = 0; | |||||||||||||||||
| 201 | return | ||||||||||||||||||
| 202 | ( | ||||||||||||||||||
| 203 | # write callback | ||||||||||||||||||
| 204 | 626 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||
| 205 | 626 | my ($data) = @_; | |||||||||||||||||
| 206 | # limit total written so we can fail the offset table write for RLE | ||||||||||||||||||
| 207 | 626 | $written += length $data; | |||||||||||||||||
| 208 | 626 | ▶ | if ($written <= $limit) { | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 209 | 616 | $pos += length $data; | |||||||||||||||||
| 210 | 616 | print "# write of ", length $data, " bytes successful (", | |||||||||||||||||
| 211 | $limit - $written, " left)\n"; | ||||||||||||||||||
| 212 | 616 | return 1; | |||||||||||||||||
| 213 | } | ||||||||||||||||||
| 214 | else { | ||||||||||||||||||
| 215 | 10 | print "# write of ", length $data, " bytes failed\n"; | |||||||||||||||||
| 216 | 10 | Imager::i_push_error(0, "limit reached"); | |||||||||||||||||
| 217 | 10 | return; | |||||||||||||||||
| 218 | } | ||||||||||||||||||
| 219 | }, | ||||||||||||||||||
| 220 | # seek cb | ||||||||||||||||||
| 221 | 2 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=19.4
| |||||||||||||||||||
| 222 | 2 | my ($position, $whence) = @_; | |||||||||||||||||
| 223 | |||||||||||||||||||
| 224 | 2 | ▶ | if ($whence == SEEK_SET) { | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 225 | 2 | $pos = $position; | |||||||||||||||||
| 226 | 2 | print "# seek to $pos\n"; | |||||||||||||||||
| 227 | } | ||||||||||||||||||
| 228 | elsif ($whence == SEEK_END) { | ||||||||||||||||||
| 229 | 0 | die "SEEK_END not supported\n"; | |||||||||||||||||
| 230 | } | ||||||||||||||||||
| 231 | elsif ($whence == SEEK_CUR) { | ||||||||||||||||||
| 232 | 0 | die "SEEK_CUR not supported\n"; | |||||||||||||||||
| 233 | } | ||||||||||||||||||
| 234 | else { | ||||||||||||||||||
| 235 | 0 | die "Invalid seek whence $whence"; | |||||||||||||||||
| 236 | } | ||||||||||||||||||
| 237 | |||||||||||||||||||
| 238 | 2 | $pos; | |||||||||||||||||
| 239 | } | ||||||||||||||||||
| 240 | ) | ||||||||||||||||||
| 241 | 10 | } | |||||||||||||||||