4 use Test::More tests => 57;
5 use Imager::Test qw(test_image test_image_16 is_image);
8 -d 'testout' or mkdir 'testout', 0777;
10 Imager::init_log('testout/20write.log', 2);
13 my $im = test_image();
14 $im->line(x1 => 0, y1 => 0, x2 => 150, y2 => 150, color => 'FF0000');
15 ok($im->write(file => 'testout/20verb.rgb'), "write 8-bit verbatim")
16 or print "# ", $im->errstr, "\n";
17 my $im2 = Imager->new;
18 ok($im2->read(file => 'testout/20verb.rgb'), "read it back")
19 or print "# ", $im2->errstr, "\n";
20 is_image($im, $im2, "compare");
21 is($im2->tags(name => 'sgi_rle'), 0, "check not rle");
22 is($im2->tags(name => 'sgi_bpc'), 1, "check bpc");
23 is($im2->tags(name => 'i_comment'), undef, "no namestr");
25 ok($im->write(file => 'testout/20rle.rgb',
27 i_comment => "test"), "write 8-bit rle")
28 or print "# ", $im->errstr, "\n";
29 my $im3 = Imager->new;
30 ok($im3->read(file => 'testout/20rle.rgb'), "read it back")
31 or print "# ", $im3->errstr, "\n";
32 is_image($im, $im3, "compare");
33 is($im3->tags(name => 'sgi_rle'), 1, "check not rle");
34 is($im3->tags(name => 'sgi_bpc'), 1, "check bpc");
35 is($im3->tags(name => 'i_comment'), 'test', "check i_comment set");
39 my $im = test_image_16();
40 $im->line(x1 => 0, y1 => 0, x2 => 150, y2 => 150, color => 'FF0000');
41 ok($im->write(file => 'testout/20verb16.rgb'), "write 16-bit verbatim")
42 or print "# ", $im->errstr, "\n";
43 my $im2 = Imager->new;
44 ok($im2->read(file => 'testout/20verb16.rgb'), "read it back")
45 or print "# ", $im2->errstr, "\n";
46 is_image($im, $im2, "compare");
47 is($im2->tags(name => 'sgi_rle'), 0, "check not rle");
48 is($im2->tags(name => 'sgi_bpc'), 2, "check bpc");
49 is($im2->tags(name => 'i_comment'), undef, "no namestr");
51 ok($im->write(file => 'testout/20rle16.rgb',
53 i_comment => "test"), "write 16-bit rle")
54 or print "# ", $im->errstr, "\n";
55 my $im3 = Imager->new;
56 ok($im3->read(file => 'testout/20rle16.rgb'), "read it back")
57 or print "# ", $im3->errstr, "\n";
58 is_image($im, $im3, "compare");
59 is($im3->tags(name => 'sgi_rle'), 1, "check not rle");
60 is($im3->tags(name => 'sgi_bpc'), 2, "check bpc");
61 is($im3->tags(name => 'i_comment'), 'test', "check i_comment set");
63 my $imbig = Imager->new(xsize => 300, ysize => 300, bits => 16);
64 $imbig->paste(src => $im, tx => 0, ty => 0);
65 $imbig->paste(src => $im, tx => 150, ty => 0);
66 $imbig->paste(src => $im, tx => 0, ty => 150);
67 $imbig->paste(src => $im, tx => 150, ty => 150);
69 $imbig->line(x1 => $t*4, y1 => 0, x2 => 3+$t*4, y2 => 299,
70 color => [ 255 - $t, 0, 0 ]);
73 ok($imbig->write(data => \$data, type => 'sgi', sgi_rle => 1),
74 "write larger image");
75 cmp_ok(length($data), '>', 0x10000, "check output large enough for test");
76 print "# ", length $data, "\n";
77 my $imbigcmp = Imager->new;
78 ok($imbigcmp->read(data => $data), "read larger image");
79 is_image($imbig, $imbigcmp, "check large image matches");
84 my $im = test_image()->convert(preset=>'grey');
85 ok($im->write(file => 'testout/20vgray8.bw'), "write 8-bit verbatim grey")
86 or print "# ", $im->errstr, "\n";
87 my $im2 = Imager->new;
88 ok($im2->read(file => 'testout/20vgray8.bw'), "read it back")
89 or print "# ", $im2->errstr, "\n";
90 is_image($im, $im2, "compare");
91 is($im2->tags(name => 'i_format'), 'sgi', "check we saved as SGI");
92 is($im2->tags(name => 'sgi_rle'), 0, "check not rle");
93 is($im2->tags(name => 'sgi_bpc'), 1, "check bpc");
94 is($im2->tags(name => 'i_comment'), undef, "no namestr");
99 my $rgb8 = test_image();
100 my $rgb16 = test_image_16();
101 my $rgb8rle = $rgb8->copy;
102 $rgb8rle->settag(name => 'sgi_rle', value => 1);
103 my $grey8 = $rgb8->convert(preset => 'grey');
104 my $grey16 = $rgb16->convert(preset => 'grey');
105 my $grey16rle = $grey16->copy;
106 $grey16rle->settag(name => 'sgi_rle', value => 1);
110 # each entry is: image, limit, expected msg, description
113 'SGI image: cannot write header',
118 'SGI image: error writing image data',
123 'SGI image: error writing image data',
124 '8-bit image data (grey)'
128 'SGI image: error writing offsets/lengths',
133 'SGI image: error writing RLE data',
138 'SGI image: cannot write final RLE table',
139 '8-bit rewrite RLE table',
143 'SGI image: error writing image data',
148 'SGI image: error writing offsets/lengths',
149 'rle tables, 16 bit',
153 'SGI image: error writing RLE data',
158 'SGI image: cannot write final RLE table',
159 '16-bit rewrite RLE table',
162 for my $test (@tests) {
163 my ($im, $limit, $expected_msg, $desc) = @$test;
164 my $io = limited_write_io($limit);
165 ok(!$im->write(type => 'sgi', io => $io),
166 "write should fail - $desc");
167 is($im->errstr, "$expected_msg: limit reached", "check error - $desc");
172 { # check close failures are handled correctly
173 my $im = test_image();
174 my $fail_close = sub {
175 Imager::i_push_error(0, "synthetic close failure");
178 ok(!$im->write(type => "sgi", callback => sub { 1 },
179 closecb => $fail_close),
180 "check failing close fails");
181 like($im->errstr, qr/synthetic close failure/,
182 "check error message");
185 sub limited_write_io {
188 my ($writecb, $seekcb) = limited_write($limit);
190 my $io = Imager::io_new_cb($writecb, undef, $seekcb, undef, 1);
191 $io->set_buffered(0);
206 # limit total written so we can fail the offset table write for RLE
207 $written += length $data;
208 if ($written <= $limit) {
209 $pos += length $data;
210 print "# write of ", length $data, " bytes successful (",
211 $limit - $written, " left)\n";
215 print "# write of ", length $data, " bytes failed\n";
216 Imager::i_push_error(0, "limit reached");
222 my ($position, $whence) = @_;
224 if ($whence == SEEK_SET) {
226 print "# seek to $pos\n";
228 elsif ($whence == SEEK_END) {
229 die "SEEK_END not supported\n";
231 elsif ($whence == SEEK_CUR) {
232 die "SEEK_CUR not supported\n";
235 die "Invalid seek whence $whence";