Debian Codegraph @

t/200-file/400-basic.t

raw
1 #!perl -w
2
3
4 use strict;
5 use Imager;
6 use IO::Seekable;
7 use Test::More;
8 use File::Spec;
9 use Imager::Test qw(is_image);
10
11 sub diag_skip_image($$);
12 sub diag_skip_errno($);
13
14 my @cleanup = "t50basicoo.log";
15
16 -d "testout" or mkdir "testout";
17
18 Imager->open_log(log => "testout/t50basicoo.log");
19
20 # single image/file types
21 my @types = qw( jpeg png raw pnm gif tiff bmp tga );
22
23 # multiple image/file formats
24 my @mtypes = qw(tiff gif);
25
26 # formats we need to check if they were successfully configured
27 my %check_formats = map { $_ => 1 } qw(tiff png jpeg gif);
28
29 open my $fh, "<", "imfound.cfg"
30 or plan skip_all => "Could not open imfound.cfg: $!";
31 chomp(my @cfg_found = <$fh>);
32 my %cfg_found = map { $_ => 1 } @cfg_found;
33 close $fh;
34
35 my %hsh=%Imager::formats;
36
37 print "# avaliable formats:\n";
38 for(keys %hsh) { print "# $_\n"; }
39
40 #print Dumper(\%hsh);
41
42 my $img = Imager->new();
43
44 my %files;
45 @files{@types} = ({ file => "JPEG/testimg/209_yonge.jpg" },
46 { file => "testimg/test.png" },
47 { file => "testimg/test.raw", xsize=>150, ysize=>150, type=>'raw', interleave => 0},
48 { file => "testimg/penguin-base.ppm" },
49 { file => "GIF/testimg/expected.gif" },
50 { file => "TIFF/testimg/comp8.tif" },
51 { file => "testimg/winrgb24.bmp" },
52 { file => "testimg/test.tga" }, );
53 my %writeopts =
54 (
55 gif=> { make_colors=>'webmap', translate=>'closest', gifquant=>'gen',
56 gif_delay=>20 },
57 );
58
59 for my $type (@types) {
60 # only try it if we found it
61 next unless $hsh{$type};
62 # only try it if it was installed during this build
63 # pre-installed modules may have bugs
64 next if $check_formats{$type} && !$cfg_found{$type};
65 print "# type $type\n";
66 my %opts = %{$files{$type}};
67 my @a = map { "$_=>${opts{$_}}" } keys %opts;
68
69 SKIP:
70 {
71 print "#opening Format: $type, options: @a\n";
72 ok($img->read( %opts ), "$type: reading from file")
73 or diag_skip_image($img, "$type: reading base file failed");
74
75 my %mopts = %opts;
76 delete $mopts{file};
77
78 SKIP:
79 {
80 my $fh;
81 ok(open($fh, "<", $opts{file}), "$type: open $opts{file}")
82 or diag_skip_errno("$type: Cannot open $opts{file}");
83
84 binmode $fh;
85 my $fhimg = Imager->new;
86 ok($fhimg->read(fh=>$fh, %mopts), "$type: read from fh")
87 or diag_skip_image($fhimg, "$type: couldn't read from fh");
88 ok(seek($fh, 0, SEEK_SET), "$type: seek after read")
89 or diag_skip_errno("$type: couldn't seek back to start");
90 SKIP:
91 {
92 ok($fhimg->read(fh=>$fh, %mopts, type=>$type), "$type: read from fh after seek")
93 or diag_skip_image($fhimg, "$type: failed to read after seek");
94 is_image($img, $fhimg,
95 "$type: image comparison after fh read after seek");
96 }
97 ok(seek($fh, 0, SEEK_SET), "$type: seek after read prep to read from fd")
98 or diag_skip_errno("$type: couldn't seek");
99
100 SKIP:
101 {
102 # read from a fd
103 my $fdimg = Imager->new;
104 ok($fdimg->read(fd=>fileno($fh), %mopts, type=>$type), "read from fd")
105 or diag_skip_image($fdimg, "$type: couldn't read from fd");
106 is_image($img, $fdimg, "image comparistion after fd read");
107 }
108 ok($fh->close, "close fh after reads")
109 or diag_skip_errno("$type: close after read failed");
110 }
111
112 # read from a memory buffer
113 open my $dfh, "<", $opts{file}
114 or die "Cannot open $opts{file}: $!";
115 binmode $dfh;
116 my $data = do { local $/; <$dfh> };
117 close $dfh;
118 my $bimg = Imager->new;
119
120 SKIP:
121 {
122 ok($bimg->read(data=>$data, %mopts, type=>$type), "$type: read from buffer")
123 or diag_skip_image($bimg, "$type: read from buffer failed");
124 is_image($img, $bimg, "comparing buffer read image");
125 }
126
127 # read from callbacks, both with minimum and maximum reads
128 my $buf = $data;
129 my $seekpos = 0;
130 my $reader_min =
131 sub {
132 my ($size, $maxread) = @_;
133 my $out = substr($buf, $seekpos, $size);
134 $seekpos += length $out;
135 $out;
136 };
137 my $reader_max =
138 sub {
139 my ($size, $maxread) = @_;
140 my $out = substr($buf, $seekpos, $maxread);
141 $seekpos += length $out;
142 $out;
143 };
144 my $seeker =
145 sub {
146 my ($offset, $whence) = @_;
147 #print "io_seeker($offset, $whence)\n";
148 if ($whence == SEEK_SET) {
149 $seekpos = $offset;
150 }
151 elsif ($whence == SEEK_CUR) {
152 $seekpos += $offset;
153 }
154 else { # SEEK_END
155 $seekpos = length($buf) + $offset;
156 }
157 #print "-> $seekpos\n";
158 $seekpos;
159 };
160 my $cbimg = Imager->new;
161 SKIP:
162 {
163 ok($cbimg->read(callback=>$reader_min, seekcb=>$seeker, type=>$type, %mopts),
164 "$type: read from callback min")
165 or diag_skip_image("$type: read from callback min", $cbimg);
166 is_image($cbimg, $img, "$type: comparing mincb image");
167 }
168 SKIP:
169 {
170 $seekpos = 0;
171 ok($cbimg->read(callback=>$reader_max, seekcb=>$seeker, type=>$type, %mopts),
172 "$type: read from callback max")
173 or diag_skip_image("$type: read from callback max", $cbimg);
174 is_image($cbimg, $img, "$type: comparing maxcb image");
175 }
176 }
177 }
178
179 for my $type (@types) {
180 next unless $hsh{$type};
181
182 print "# write tests for $type\n";
183 # test writes
184 next unless $hsh{$type};
185 my $file = "testout/t50out.$type";
186 push @cleanup, "t50out.$type";
187 my $wimg = Imager->new;
188
189 SKIP:
190 {
191 ok($wimg->read(file=>"testimg/penguin-base.ppm"),
192 "$type: cannot read base file")
193 or diag_skip_image($wimg, "$type: reading base file");
194
195 # first to a file
196 print "# writing $type to a file\n";
197 my %extraopts;
198 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
199 ok($wimg->write(file=>$file, %extraopts),
200 "writing $type to a file $file");
201
202 my $fh_data;
203 SKIP:
204 {
205 print "# writing $type to a FH\n";
206 # to a FH
207 ok(open(my $fh, "+>", $file), "$type: create FH test file")
208 or diag_skip_errno("$type: Could not create $file");
209 binmode $fh;
210 ok($wimg->write(fh=>$fh, %extraopts, type=>$type),
211 "$type: writing to a FH")
212 or diag_skip_image($wimg, "$type: write to fh");
213 ok(seek($fh, 0, SEEK_END),
214 "$type: seek after writing to a FH")
215 or diag_skip_errno("$type: seek to end failed");
216 ok(print($fh "SUFFIX\n"), "write to FH after writing $type");
217 ok($fh->close, "closing FH after writing $type");
218
219 my $ifh;
220 ok(open($ifh, "< $file"), "opening data source")
221 or diag_skip_errno("$type: couldn't re-open file");
222 binmode $ifh;
223 $fh_data = do { local $/; <$ifh> };
224 close $ifh;
225
226 # writing to a buffer
227 print "# writing $type to a buffer\n";
228 my $buf = '';
229 ok($wimg->write(data=>\$buf, %extraopts, type=>$type),
230 "$type: writing to a buffer")
231 or diag_skip_image($wimg, "$type: writing to buffer failed");
232 $buf .= "SUFFIX\n";
233 if (open my $wfh, ">", "testout/t50_buf.$type") {
234 binmode $wfh;
235 print $wfh $buf;
236 close $wfh;
237 }
238 push @cleanup, "t50_buf.$type";
239 is($fh_data, $buf, "comparing file data to buffer");
240 }
241
242 SKIP:
243 {
244 my $buf = '';
245 my $seekpos = 0;
246 my $did_close;
247 my $writer =
248 sub {
249 my ($what) = @_;
250 if ($seekpos > length $buf) {
251 $buf .= "\0" x ($seekpos - length $buf);
252 }
253 substr($buf, $seekpos, length $what) = $what;
254 $seekpos += length $what;
255 $did_close = 0; # the close must be last
256 1;
257 };
258 my $reader_min =
259 sub {
260 my ($size, $maxread) = @_;
261 my $out = substr($buf, $seekpos, $size);
262 $seekpos += length $out;
263 $out;
264 };
265 my $reader_max =
266 sub {
267 my ($size, $maxread) = @_;
268 my $out = substr($buf, $seekpos, $maxread);
269 $seekpos += length $out;
270 $out;
271 };
272 use IO::Seekable;
273 my $seeker =
274 sub {
275 my ($offset, $whence) = @_;
276 #print "io_seeker($offset, $whence)\n";
277 if ($whence == SEEK_SET) {
278 $seekpos = $offset;
279 }
280 elsif ($whence == SEEK_CUR) {
281 $seekpos += $offset;
282 }
283 else { # SEEK_END
284 $seekpos = length($buf) + $offset;
285 }
286 #print "-> $seekpos\n";
287 $seekpos;
288 };
289
290 my $closer = sub { ++$did_close; };
291
292 print "# writing $type via callbacks (mb=1)\n";
293 ok($wimg->write(writecb=>$writer, seekcb=>$seeker, closecb=>$closer,
294 readcb=>$reader_min,
295 %extraopts, type=>$type, maxbuffer=>1),
296 "$type: writing to callback (mb=1)")
297 or diag_skip_image($wimg, "$type: writing to callback failed");
298
299 ok($did_close, "checking closecb called");
300 $buf .= "SUFFIX\n";
301 SKIP:
302 {
303 defined $fh_data
304 or skip "Couldn't read original file", 1;
305 is($fh_data, $buf, "comparing callback output to file data");
306 }
307 print "# writing $type via callbacks (no mb)\n";
308 $buf = '';
309 $did_close = 0;
310 $seekpos = 0;
311 # we don't use the closecb here - used to make sure we don't get
312 # a warning/error on an attempt to call an undef close sub
313 ok($wimg->write(writecb=>$writer, seekcb=>$seeker, readcb=>$reader_min,
314 %extraopts, type=>$type),
315 "writing $type to callback (no mb)")
316 or diag_skip_image($wimg, "$type: failed writing to callback (no mb)");
317 $buf .= "SUFFIX\n";
318 SKIP:
319 {
320 defined $fh_data
321 or skip "Couldn't read original file", 1;
322 is($fh_data, $buf, "comparing callback output to file data");
323 }
324 }
325 }
326 }
327
328 undef($img);
329
330 # multi image/file tests
331 print "# multi-image write tests\n";
332 for my $type (@mtypes) {
333 next unless $hsh{$type};
334 print "# $type\n";
335
336 my $file = "testout/t50out.$type";
337 my $wimg = Imager->new;
338
339 # if this doesn't work, we're so screwed up anyway
340 SKIP:
341 {
342 ok($wimg->read(file=>"testout/t50out.$type"),
343 "reading base file")
344 or diag_skip_image($wimg, "$type-multi: reading base file failed");
345
346 ok(my $wimg2 = $wimg->copy, "copying base image")
347 or diag_skip_image($wimg, "$type-multi: cannot copy");
348 ok($wimg2->flip(dir=>'h'), "flipping base image")
349 or diag_skip_image($wimg, "$type-multi: cannot flip");
350
351 my @out = ($wimg, $wimg2);
352 my %extraopts;
353 %extraopts = %{$writeopts{$type}} if $writeopts{$type};
354 push @cleanup, "t50_multi.$type";
355 ok(Imager->write_multi({ file=>"testout/t50_multi.$type", %extraopts },
356 @out),
357 "$type-multi: writing multiple to a file")
358 or diag_skip_image(Imager => "$type-multi: failed writing multiple to a file");
359
360 # make sure we get the same back
361 my @images = Imager->read_multi(file=>"testout/t50_multi.$type");
362 if (ok(@images == @out, "$type-multi: checking read image count")) {
363 for my $i (0 .. $#out) {
364 is_image($out[$i], $images[$i],
365 "$type-multi: comparing image $i");
366 }
367 }
368 }
369 }
370
371 done_testing();
372
373 Imager::malloc_state();
374
375 Imager->close_log;
376
377 END {
378 unless ($ENV{IMAGER_KEEP_FILES}) {
379 unlink map "testout/$_", @cleanup;
380 rmdir "testout";
381 }
382 }
383
384 sub diag_skip_image($$) {
385 my ($img, $msg) = @_;
386
387 diag "$msg: " . $img->errstr;
388 skip $msg, 1;
389 }
390
391 sub diag_skip_errno($) {
392 my ($msg) = @_;
393
394 diag "$msg: $!";
395 skip $msg, 1;
396 }