1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381
|
use strict;
use File::Path ();
use File::Spec::Functions;
use FindBin ();
use Test::More;
require Test::NoWarnings;
use Image::Scale;
my $jpeg_version = Image::Scale->jpeg_version();
if ($jpeg_version) {
plan tests => 112;
}
else {
plan skip_all => 'Image::Scale not built with libjpeg support';
}
my $tmpdir = catdir( $FindBin::Bin, 'tmp' );
if ( -d $tmpdir ) {
File::Path::rmtree($tmpdir);
}
mkdir $tmpdir;
my @types = qw(
rgb
rgb_progressive
gray
gray_progressive
cmyk
);
my @resizes = qw(
resize_gd
resize_gd_fixed_point
resize_gm
resize_gm_fixed_point
);
# width/height
for my $type ( @types ) {
my $im = Image::Scale->new( _f("${type}.jpg") );
is( $im->width, 313, "JPEG $type width ok" );
is( $im->height, 234, "JPEG $type height ok" );
}
# Test resized height/width
{
my $im = Image::Scale->new( _f("rgb.jpg") );
is( $im->resized_width, 0, "JPEG resized_width is 0 before resize" );
is( $im->resized_height, 0, "JPEG resized_height is 0 before resize" );
$im->resize_gd( { width => 50, height => 50 } );
is( $im->resized_width, 50, "JPEG resized_width ok" );
is( $im->resized_height, 50, "JPEG resized_height ok" );
}
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 44
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 44 unless $ENV{TEST_COMPARE};
# Normal width resize
for my $resize ( @resizes ) {
for my $type ( @types ) {
my $outfile = _tmp("${type}_${resize}_w100.jpg");
my $im = Image::Scale->new( _f("${type}.jpg") );
$im->$resize( { width => 100 } );
$im->save_jpeg($outfile);
my $data = $im->as_jpeg();
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "${type}_${resize}_w100.jpg" ), 1, "JPEG $type $resize 100 file ok" );
is( _compare( \$data, "${type}_${resize}_w100.jpg" ), 1, "JPEG $type $resize 100 scalar ok" );
}
}
# square, without keep_aspect
for my $resize ( @resizes ) {
for my $type ( @types ) {
my $outfile = _tmp("${type}_${resize}_50x50.jpg");
my $im = Image::Scale->new( _f("${type}.jpg") );
$im->$resize( { width => 50, height => 50 } );
$im->save_jpeg($outfile);
my $data = $im->as_jpeg();
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "${type}_${resize}_50x50.jpg" ), 1, "JPEG $type $resize 50x50 square file ok" );
is( _compare( \$data, "${type}_${resize}_50x50.jpg" ), 1, "JPEG $type $resize 50x50 square scalar ok" );
}
}
# keep_aspect with height padding
for my $resize ( @resizes ) {
my $outfile = _tmp("${resize}_50x50_keep_aspect_height.jpg");
my $im = Image::Scale->new( _f("rgb.jpg") );
$im->$resize( { width => 50, height => 50, keep_aspect => 1 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "${resize}_50x50_keep_aspect_height.jpg" ), 1, "JPEG $resize 50x50 keep_aspect file ok" );
}
# keep_aspect with width padding
for my $resize ( @resizes ) {
my $outfile = _tmp("${resize}_50x50_keep_aspect_width.jpg");
my $im = Image::Scale->new( _f("exif_90_ccw.jpg") );
$im->$resize( { width => 50, height => 50, ignore_exif => 1, keep_aspect => 1 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "${resize}_50x50_keep_aspect_width.jpg" ), 1, "JPEG $resize 50x50 keep_aspect file ok" );
}
}
# memory_limit
{
my $im = Image::Scale->new( _f("rgb.jpg") );
eval { $im->resize( { width => 50, memory_limit => 1000 } ) };
like( $@, qr/memory_limit exceeded/, 'JPEG memory_limit ok' );
}
# corrupt truncated file but will still resize with a gray area
SKIP:
{
Test::NoWarnings::clear_warnings();
my $outfile = _tmp("truncated_50.jpg");
my $im = Image::Scale->new( _f("truncated.jpg") );
$im->resize_gd_fixed_point( { width => 50 } );
$im->save_jpeg($outfile);
# Test that the correct warning was output
like( (Test::NoWarnings::warnings())[0]->getMessage, qr/premature end of/i, 'JPEG corrupt truncated warning output ok' );
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
is( _compare( _load($outfile), "truncated_50.jpg" ), 1, 'JPEG corrupt truncated resize_gd ok' );
}
# corrupt file resulting in a fatal error
{
Test::NoWarnings::clear_warnings();
my $im = Image::Scale->new( _f("corrupt.jpg") );
is( $im, undef, 'JPEG corrupt failed new() ok' );
# Test that the correct warning was output
like( (Test::NoWarnings::warnings())[0]->getMessage, qr/Image::Scale libjpeg error: Corrupt JPEG data/i, 'JPEG corrupt warning output ok' );
}
# keep_aspect bgcolor
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 2
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 2 unless $ENV{TEST_COMPARE};
for my $resize ( @resizes ) {
my $outfile = _tmp("bgcolor_${resize}.jpg");
my $im = Image::Scale->new( _f("rgb.jpg") );
$im->$resize( { width => 50, height => 50, keep_aspect => 1, bgcolor => 0x123456 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "bgcolor_${resize}.jpg" ), 1, "JPEG bgcolor $resize ok" );
}
}
# Exif rotation
# Exif with both width/height specified
# Exif with keep_aspect
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 42
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 42 unless $ENV{TEST_COMPARE};
my @rotations = qw(
mirror_horiz
180
mirror_vert
mirror_horiz_270_ccw
90_ccw
mirror_horiz_90_ccw
270_ccw
);
for my $resize ( @resizes ) {
for my $r ( @rotations ) {
my $outfile = _tmp("exif_${r}_${resize}_50.jpg");
my $im = Image::Scale->new( _f("exif_${r}.jpg") );
$im->$resize( { width => 50 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "exif_${r}_${resize}_50.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize width 50 ok" );
}
for my $r ( @rotations ) {
my $outfile = _tmp("exif_${r}_${resize}_50x50.jpg");
my $im = Image::Scale->new( _f("exif_${r}.jpg") );
$im->$resize( { width => 50, height => 50 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "exif_${r}_${resize}_50x50.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize 50x50 ok" );
}
for my $r ( @rotations ) {
my $outfile = _tmp("exif_${r}_${resize}_50x50_keep_aspect.jpg");
my $im = Image::Scale->new( _f("exif_${r}.jpg") );
$im->$resize( { width => 50, height => 50, keep_aspect => 1 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "exif_${r}_${resize}_50x50_keep_aspect.jpg" ), 1, "JPEG EXIF auto-rotation $r $resize 50x50 keep_aspect ok" );
}
}
}
# Exif with ignore_exif
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
my $outfile = _tmp("exif_ignore_50.jpg");
my $im = Image::Scale->new( _f("exif_90_ccw.jpg") );
$im->resize_gd_fixed_point( { width => 50, ignore_exif => 1 } );
$im->save_jpeg($outfile);
is( _compare( _load($outfile), "exif_ignore_50.jpg" ), 1, "JPEG EXIF ignore_exif ok" );
}
# multiple resize calls on same $im object, should throw away previous resize data
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 2
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 2 unless $ENV{TEST_COMPARE};
for my $resize ( @resizes ) {
my $outfile = _tmp("rgb_multiple_${resize}.jpg");
my $im = Image::Scale->new( _f("rgb.jpg") );
$im->$resize( { width => 50 } );
$im->$resize( { width => 75 } );
$im->save_jpeg($outfile);
# Only perform file comparisons for fixed-point methods, as floating-point
# is expected to differ on different platforms
next if $resize !~ /fixed/;
is( _compare( _load($outfile), "rgb_multiple_${resize}.jpg" ), 1, "JPEG multiple resize $resize ok" );
}
}
# resize from JPEG in scalar
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
my $dataref = _load( _f("rgb.jpg") );
my $outfile = _tmp("rgb_resize_gd_fixed_point_w100.jpg");
my $im = Image::Scale->new($dataref);
$im->resize_gd_fixed_point( { width => 100 } );
$im->save_jpeg($outfile);
is( _compare( _load($outfile), "rgb_resize_gd_fixed_point_w100.jpg" ), 1, "JPEG resize_gd_fixed_point from scalar ok" );
}
# resize multiple from JPEG scalar
SKIP:
{
skip "libjpeg version is $jpeg_version, skipping file comparison tests (they require v62)", 1
if $jpeg_version != 62;
# These break when using libjpeg-turbo v62, just skip them instead of adding turbo detection
skip "Skipping binary comparison tests (TEST_COMPARE=1 to test)", 1 unless $ENV{TEST_COMPARE};
my $dataref = _load( _f("rgb.jpg") );
my $outfile = _tmp("rgb_resize_gd_fixed_point_w100.jpg");
my $im = Image::Scale->new($dataref);
$im->resize_gd_fixed_point( { width => 150 } );
$im->resize_gd_fixed_point( { width => 100 } );
$im->save_jpeg($outfile);
is( _compare( _load($outfile), "rgb_resize_gd_fixed_point_w100.jpg" ), 1, "JPEG resize_gd_fixed_point multiple from scalar ok" );
}
# XXX fatal errors during compression, will this ever actually happen?
# XXX progressive JPEG with/without memory_limit
diag("libjpeg version: $jpeg_version");
END {
File::Path::rmtree($tmpdir);
}
sub _f {
return catfile( $FindBin::Bin, 'images', 'jpg', shift );
}
sub _tmp {
return catfile( $tmpdir, shift );
}
sub _load {
my $path = shift;
open my $fh, '<', $path or die "Cannot open $path";
binmode $fh;
my $data = do { local $/; <$fh> };
close $fh;
return \$data;
}
sub _compare {
my ( $test, $path ) = @_;
my $ref = _load( catfile( $FindBin::Bin, 'ref', 'jpg', $path ) );
return $$ref eq $$test;
}
|