#!/usr/bin/perl use strict; use Config; use FindBin; use constant RESIZER => 1; BEGIN { my $perlmajorversion = $Config{version}; $perlmajorversion =~ s/\.\d+$//; my $arch = $Config::Config{archname}; $arch =~ s/^i[3456]86-/i386-/; $arch =~ s/gnu-//; my @SlimINC = ( "$FindBin::Bin/../../server", "$FindBin::Bin/../../server/CPAN", "$FindBin::Bin/../../server/CPAN/arch/${perlmajorversion}/${arch}", ); if ( $^O =~ /linux/ && -d '/usr/squeezecenter' ) { # TinySC support @SlimINC = ( "/usr/squeezecenter", "/usr/squeezecenter/lib", "/usr/squeezecenter/CPAN", ); } unshift @INC, @SlimINC; } use Data::Dump qw(dump); use File::Spec::Functions; use Test::More tests => 44; use Slim::Utils::GDResizer; my $square_cover = _load('source/square-cover.jpg'); my $square_icon = _load('source/square-icon.png'); my $tall_cover = _load('source/tall-cover.jpg'); my $wide_cover = _load('source/wide-cover.jpg'); # pad/max mode { # Resize square -> square { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $square_cover, width => 50, height => 50, ); is( $format, 'jpg', 'max: square -> square format is JPEG' ); is( _compare($outref, 'ref/square-cover-50x50.jpg'), 1, 'max: square -> square compare ok' ); } # Resize square -> wide with forced jpeg and bgcolor padding { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $square_cover, width => 100, height => 50, format => 'jpg', bgcolor => '112233', ); is( $format, 'jpg', 'max: square -> wide format is forced JPEG' ); is( _compare($outref, 'ref/square-cover-100x50-112233.jpg'), 1, 'max: square -> wide compare ok' ); } # Resize square -> tall, default png format and transparency { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $square_cover, width => 50, height => 100, ); is( $format, 'png', 'max: square -> tall format is PNG' ); is( _compare($outref, 'ref/square-cover-50x100.png'), 1, 'max: square -> tall compare ok' ); } # Resize square PNG -> square PNG { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $square_icon, width => 100, height => 100, ); is( $format, 'png', 'max: square PNG -> square is PNG' ); is( _compare($outref, 'ref/square-icon-100x100.png'), 1, 'max: square PNG -> square compare ok' ); } # Resize tall JPG -> square PNG with transparent padding, result should be centered { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $tall_cover, width => 100, height => 100, ); is( $format, 'png', 'max: tall JPG -> square is PNG' ); is( _compare($outref, 'ref/tall-cover-100x100.png'), 1, 'max: tall JPG -> square compare ok' ); } # Resize wide JPG -> square PNG with transparent padding, result should be centered { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $wide_cover, width => 100, height => 100, ); is( $format, 'png', 'max: wide JPG -> square is PNG' ); is( _compare($outref, 'ref/wide-cover-100x100.png'), 1, 'max: wide JPG -> square compare ok' ); } # Resize wide JPG -> wide JPG, only width specified { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $wide_cover, width => 100, ); is( $format, 'jpg', 'max: wide JPG -> width only is JPG' ); is( _compare($outref, 'ref/wide-cover-100x12.jpg'), 1, 'max: wide JPG -> width only compare ok' ); } # Resize tall JPG -> tall JPG, only height specified { my ($outref, $format) = Slim::Utils::GDResizer->resize( original => $tall_cover, height => 100, ); is( $format, 'jpg', 'max: tall JPG -> height only is JPG' ); is( _compare($outref, 'ref/tall-cover-13x100.jpg'), 1, 'max: tall JPG -> height only compare ok' ); } # Test series: # 100x100_o # 64x64_m # 50x50_o # 41x41_m # 40x40_m { my @series = Slim::Utils::GDResizer->resizeSeries( original => $square_cover, series => [ { width => 50, mode => 'o' }, { width => 64, mode => 'm' }, { width => 100, mode => 'o' }, { width => 41, mode => 'm' }, { width => 40, mode => 'm' }, ], ); for my $s ( @series ) { my $width = $s->[2]; is( _compare($s->[0], "ref/series-${width}.jpg"), 1, "series: $width ok" ); } } } # Test resizing from a file path { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/square-cover.jpg'), width => 50, height => 50, ); is( $format, 'jpg', 'max: square -> square format is JPEG' ); is( _compare($outref, 'ref/square-cover-50x50.jpg'), 1, 'max: square -> square compare ok' ); } # Test resizing to original size { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/square-cover.jpg'), ); is( $format, 'jpg', 'no resize format is JPEG' ); is( _compare($outref, 'source/square-cover.jpg'), 1, 'no resize compare ok' ); } # Test resizing to original size but different format { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/square-cover.jpg'), format => 'png', ); is( $format, 'png', 'no resize -> PNG format is PNG' ); is( _compare($outref, 'ref/square-cover-512x512.png'), 1, 'no resize -> PNG compare ok' ); } # Test resizing from audio files # MP3 with ID3v2 { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/apic.mp3'), width => 50, ); is( $format, 'jpg', 'max: MP3 APIC -> square format is JPEG' ); is( _compare($outref, 'ref/apic-50x50.jpg'), 1, 'max: MP3 APIC -> square compare ok' ); } # MP3 with ID3v2, series { my @series = Slim::Utils::GDResizer->resizeSeries( file => _file('source/apic.mp3'), series => [ { width => 50, mode => 'o' }, { width => 64, mode => 'm' }, { width => 100, mode => 'o' }, { width => 41, mode => 'm' }, { width => 40, mode => 'm' }, ], ); for my $s ( @series ) { my $width = $s->[2]; is( _compare($s->[0], "ref/apic-series-${width}.jpg"), 1, "MP3 APIC series: $width ok" ); } } # FLAC { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/picture.flac'), width => 50, ); is( $format, 'jpg', 'max: FLAC picture -> square format is JPEG' ); is( _compare($outref, 'ref/flac-50x50.jpg'), 1, 'max: FLAC picture -> square compare ok' ); } # Ogg { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/coverart.ogg'), width => 50, ); is( $format, 'jpg', 'max: Ogg coverart -> square format is JPEG' ); is( _compare($outref, 'ref/ogg-50x50.jpg'), 1, 'max: Ogg coverart -> square compare ok' ); } # MP4 { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/covr.m4a'), width => 50, ); is( $format, 'png', 'max: MP4 COVR -> square format is PNG' ); is( _compare($outref, 'ref/mp4-50x50.png'), 1, 'max: MP4 COVR -> square compare ok' ); } # WMA { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => _file('source/picture.wma'), width => 50, ); is( $format, 'png', 'max: WM/Picture -> square format is PNG' ); is( _compare($outref, 'ref/wma-50x50.png'), 1, 'max: WM/Picture -> square compare ok' ); } # Bug 15944, resizing to same as source { my ($outref, $format) = Slim::Utils::GDResizer->resize( file => catfile( $FindBin::Bin, '..', 'data', 'images', 'source', 'bug15944.jpg' ), width => 300, height => 300, ); is( $format, 'jpg', 'bug15944 format is JPG' ); is( _compare($outref, 'source/bug15944.jpg'), 1, 'bug15944 compare ok' ); } sub _load { my $path = shift; open my $fh, '<', catfile( $FindBin::Bin, '..', 'data', 'images', $path ) or die "Can't open $path: $!\n"; my $data = do { local $/; <$fh> }; close $fh; return \$data; } sub _file { my $path = shift; return catfile( $FindBin::Bin, '..', 'data', 'images', $path ); } sub _compare { my ( $test, $path ) = @_; my $ref = _load($path); # XXX: This might break next time we update GD return $$ref eq $$test; } # For creating test images sub _out { my $ref = shift; if ( ref $ref ne 'ARRAY' ) { $ref = [ [ $ref ] ]; } my $i = 1; for my $img ( @{$ref} ) { open my $fh, '>', "out${i}.jpg"; print $fh ${ $img->[0] }; close $fh; $i++; } }