| 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 => 46; |
| 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 |
my @types = qw( |
| 29 |
rgb |
| 30 |
rgba |
| 31 |
rgba_interlaced |
| 32 |
rgba16 |
| 33 |
gray |
| 34 |
gray_alpha |
| 35 |
gray_interlaced |
| 36 |
palette |
| 37 |
palette_alpha |
| 38 |
); |
| 39 |
|
| 40 |
# We don't need to test all resizes, JPEG can do that |
| 41 |
my @resizes = qw( |
| 42 |
resize_gd_fixed_point |
| 43 |
); |
| 44 |
|
| 45 |
# width/height |
| 46 |
for my $type ( @types ) { |
| 47 |
my $im = Image::Scale->new( _f("${type}.png") ); |
| 48 |
|
| 49 |
is( $im->width, 160, "PNG $type width ok" ); |
| 50 |
is( $im->height, 120, "PNG $type height ok" ); |
| 51 |
} |
| 52 |
|
| 53 |
# Normal width resize |
| 54 |
for my $resize ( @resizes ) { |
| 55 |
for my $type ( @types ) { |
| 56 |
my $outfile = _tmp("${type}_${resize}_w100.png"); |
| 57 |
|
| 58 |
my $im = Image::Scale->new( _f("${type}.png") ); |
| 59 |
$im->$resize( { width => 100 } ); |
| 60 |
$im->save_png($outfile); |
| 61 |
my $data = $im->as_png(); |
| 62 |
|
| 63 |
is( _compare( _load($outfile), "${type}_${resize}_w100.png" ), 1, "PNG $type $resize 100 file ok" ); |
| 64 |
is( _compare( \$data, "${type}_${resize}_w100.png" ), 1, "PNG $type $resize 100 scalar ok" ); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
# XXX palette_bkgd |
| 69 |
|
| 70 |
# corrupt files from PNG test suite |
| 71 |
# x00n0g01 - empty 0x0 grayscale file |
| 72 |
# xcrn0g04 - added cr bytes |
| 73 |
{ |
| 74 |
Test::NoWarnings::clear_warnings(); |
| 75 |
|
| 76 |
my $im = Image::Scale->new( _f("x00n0g01.png") ); |
| 77 |
|
| 78 |
# Test that $im is undef when new fails |
| 79 |
ok( !defined $im, 'new() returns undef on error ok' ); |
| 80 |
|
| 81 |
# Test that the correct warning was output |
| 82 |
my @warnings = Test::NoWarnings::warnings(); |
| 83 |
like( $warnings[0]->getMessage, qr/zero in IHDR/, 'PNG corrupt warning 1 output ok' ); |
| 84 |
} |
| 85 |
|
| 86 |
{ |
| 87 |
Test::NoWarnings::clear_warnings(); |
| 88 |
|
| 89 |
my $im = Image::Scale->new( _f("xcrn0g04.png") ); |
| 90 |
|
| 91 |
# This file won't be seen as PNG, so generates a generic unknown warning |
| 92 |
my @warnings = Test::NoWarnings::warnings(); |
| 93 |
like( $warnings[0]->getMessage, qr/Image::Scale unknown file type/, 'PNG corrupt header ok' ); |
| 94 |
} |
| 95 |
|
| 96 |
# XXX test for valid header but error during image_png_load() |
| 97 |
|
| 98 |
# multiple resize calls on same $im object, should throw away previous resize data |
| 99 |
{ |
| 100 |
for my $resize ( @resizes ) { |
| 101 |
my $outfile = _tmp("rgba_multiple_${resize}.png"); |
| 102 |
my $im = Image::Scale->new( _f("rgba.png") ); |
| 103 |
$im->$resize( { width => 50 } ); |
| 104 |
$im->$resize( { width => 75 } ); |
| 105 |
$im->save_png($outfile); |
| 106 |
|
| 107 |
is( _compare( _load($outfile), "rgba_multiple_${resize}.png" ), 1, "PNG multiple resize $resize ok" ); |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
# resize from PNG in scalar |
| 112 |
{ |
| 113 |
my $dataref = _load( _f("rgba.png") ); |
| 114 |
|
| 115 |
my $outfile = _tmp("rgba_resize_gd_fixed_point_w100.png"); |
| 116 |
my $im = Image::Scale->new($dataref); |
| 117 |
$im->resize_gd_fixed_point( { width => 100 } ); |
| 118 |
$im->save_png($outfile); |
| 119 |
|
| 120 |
is( _compare( _load($outfile), "rgba_resize_gd_fixed_point_w100.png" ), 1, "PNG resize_gd_fixed_point from scalar ok" ); |
| 121 |
} |
| 122 |
|
| 123 |
# resize multiple from PNG scalar |
| 124 |
{ |
| 125 |
my $dataref = _load( _f("rgba.png") ); |
| 126 |
|
| 127 |
my $outfile = _tmp("rgba_resize_gd_fixed_point_w100.png"); |
| 128 |
my $im = Image::Scale->new($dataref); |
| 129 |
$im->resize_gd_fixed_point( { width => 150 } ); |
| 130 |
$im->resize_gd_fixed_point( { width => 100 } ); |
| 131 |
$im->save_png($outfile); |
| 132 |
|
| 133 |
is( _compare( _load($outfile), "rgba_resize_gd_fixed_point_w100.png" ), 1, "PNG resize_gd_fixed_point multiple from scalar ok" ); |
| 134 |
} |
| 135 |
|
| 136 |
# offset image in MP3 ID3v2 tag |
| 137 |
{ |
| 138 |
my $outfile = _tmp("apic_gd_fixed_point_w50.png"); |
| 139 |
my $im = Image::Scale->new( |
| 140 |
_f('v2.4-apic-png-350-58618.mp3'), |
| 141 |
{ offset => 350, length => 58618 } |
| 142 |
); |
| 143 |
|
| 144 |
is( $im->width, 320, 'PNG from offset ID3 tag width ok' ); |
| 145 |
is( $im->height, 240, 'PNG from offset ID3 tag height ok' ); |
| 146 |
|
| 147 |
$im->resize_gd_fixed_point( { width => 50 } ); |
| 148 |
$im->save_png($outfile); |
| 149 |
|
| 150 |
is( _compare( _load($outfile), "apic_gd_fixed_point_w50.png" ), 1, "PNG resize_gd_fixed_point from offset ID3 tag ok" ); |
| 151 |
} |
| 152 |
|
| 153 |
# 1-height image that would previously try to resize to 0-height |
| 154 |
{ |
| 155 |
my $dataref = _load( _f("height1.png") ); |
| 156 |
|
| 157 |
my $outfile = _tmp("height1_resize_gd_fixed_point_w100.png"); |
| 158 |
my $im = Image::Scale->new($dataref); |
| 159 |
$im->resize_gd_fixed_point( { width => 100 } ); |
| 160 |
$im->save_png($outfile); |
| 161 |
|
| 162 |
is( _compare( _load($outfile), "height1_resize_gd_fixed_point_w100.png" ), 1, "PNG 1-height resize ok" ); |
| 163 |
} |
| 164 |
|
| 165 |
diag("libpng version: $png_version"); |
| 166 |
|
| 167 |
END { |
| 168 |
File::Path::rmtree($tmpdir); |
| 169 |
} |
| 170 |
|
| 171 |
sub _f { |
| 172 |
return catfile( $FindBin::Bin, 'images', 'png', shift ); |
| 173 |
} |
| 174 |
|
| 175 |
sub _tmp { |
| 176 |
return catfile( $tmpdir, shift ); |
| 177 |
} |
| 178 |
|
| 179 |
sub _load { |
| 180 |
my $path = shift; |
| 181 |
|
| 182 |
open my $fh, '<', $path or die "Cannot open $path"; |
| 183 |
binmode $fh; |
| 184 |
my $data = do { local $/; <$fh> }; |
| 185 |
close $fh; |
| 186 |
|
| 187 |
return \$data; |
| 188 |
} |
| 189 |
|
| 190 |
sub _compare { |
| 191 |
my ( $test, $path ) = @_; |
| 192 |
|
| 193 |
my $ref = _load( catfile( $FindBin::Bin, 'ref', 'png', $path ) ); |
| 194 |
|
| 195 |
return $$ref eq $$test; |
| 196 |
} |