Debian Codegraph @

t/png.t

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 use strict;
2
3 use File::Path ();
4 use File::Spec::Functions;
5 use FindBin ();
6 use Test::More;
7 require Test::NoWarnings;
8
9 use Image::Scale;
10
11 ### PNG tests pass with both 1.4.3 and 1.2.44, don't need to skip any
12
13 my $png_version = Image::Scale->png_version();
14
15 if ($png_version) {
•16 plan tests => 4;
17 }
18 else {
19 plan skip_all => 'Image::Scale not built with libpng support';
20 }
21
22 my $tmpdir = catdir( $FindBin::Bin, 'tmp' );
23 if ( -d $tmpdir ) {
24 File::Path::rmtree($tmpdir);
25 }
26 mkdir $tmpdir;
27
28 # XXX palette_bkgd
29
30 # corrupt files from PNG test suite
31 # x00n0g01 - empty 0x0 grayscale file
32 # xcrn0g04 - added cr bytes
33 {
34 Test::NoWarnings::clear_warnings();
35
36 my $im = Image::Scale->new( _f("x00n0g01.png") );
37
38 # Test that $im is undef when new fails
39 ok( !defined $im, 'new() returns undef on error ok' );
40
41 # Test that the correct warning was output
42 my @warnings = Test::NoWarnings::warnings();
43 like( $warnings[0]->getMessage, qr/zero in IHDR/, 'PNG corrupt warning 1 output ok' );
44 }
45
46 {
47 Test::NoWarnings::clear_warnings();
48
49 my $im = Image::Scale->new( _f("xcrn0g04.png") );
50
51 # This file won't be seen as PNG, so generates a generic unknown warning
52 my @warnings = Test::NoWarnings::warnings();
53 like( $warnings[0]->getMessage, qr/Image::Scale unknown file type/, 'PNG corrupt header ok' );
54 }
55
56 # XXX test for valid header but error during image_png_load()
57
58 # 1-height image that would previously try to resize to 0-height
59 {
60 my $dataref = _load( _f("height1.png") );
61
62 my $outfile = _tmp("height1_resize_gd_fixed_point_w100.png");
63 my $im = Image::Scale->new($dataref);
64 $im->resize_gd_fixed_point( { width => 100 } );
65 $im->save_png($outfile);
66
67 is( _compare( _load($outfile), "height1_resize_gd_fixed_point_w100.png" ), 1, "PNG 1-height resize ok" );
68 }
69
70 diag("libpng version: $png_version");
71
72 END {
73 File::Path::rmtree($tmpdir);
74 }
75
76 sub _f {
77 return catfile( $FindBin::Bin, 'images', 'png', shift );
78 }
79
80 sub _tmp {
81 return catfile( $tmpdir, shift );
82 }
83
84 sub _load {
85 my $path = shift;
86
87 open my $fh, '<', $path or die "Cannot open $path";
88 binmode $fh;
89 my $data = do { local $/; <$fh> };
90 close $fh;
91
92 return \$data;
93 }
94
95 sub _compare {
96 my ( $test, $path ) = @_;
97
98 my $ref = _load( catfile( $FindBin::Bin, 'ref', 'png', $path ) );
99
100 return $$ref eq $$test;
101 }