#!/usr/bin/perl -Tw ### # some nifty ImageMagick benchmarking # - comparison between Sample and Scale ### # 2003.02.28 altblue ##### use strict; use Image::Magick (); use Benchmark::Timer (); use vars qw(%config); BEGIN { %config = ( 'thWidth' => 650, # pixels 'thHeight' => 650, # pixels 'thExt' => '_tn.jpg', # file sufix for thumbnails 'imOpt' => { # output image options dither => 'False', matte => 'False', colorspace => 'RGB', compression=> 'JPEG', quality => 60, scene => 1, subrange => 1, subimage => 1, }, 'imType' => qr{\.(?i:jpe?g|gif|png)$}, ); }; { my $ifile = shift || die "usage: $0 \n"; my $th = $ifile; for ($th) { s#.*/##; s/$config{imType}/$config{thExt}/ or die "invalid image file!\n"; } my $t = Benchmark::Timer->new; my $ret; for (1..10) { $t->start('Sampling'); { (my $ofile = $th) =~ s/$config{thExt}$/sample$&/; $ret = mkThumb($ifile, $ofile, 1); warn "$ret\n" if $ret; } $t->stop('Sampling'); $t->start('Scaling'); { (my $ofile = $th) =~ s/$config{thExt}$/scale$&/; $ret = mkThumb($ifile, $ofile); warn "$ret\n" if $ret; } $t->stop('Scaling'); } $t->report; } #================================= #----------------- sub mkThumb { my ($file, $thumb, $sample) = @_; my $image = Image::Magick->new; my $ret = $image->Read($file); return $ret if "$ret"; if($image->Get('delay')) { pop @{$image} while scalar @{$image}>1; } my ($w,$h) = calcGeometry($image->Get('width','height')); $ret = $image->Comment('N0i.Net'); return $ret if "$ret"; $ret = $image->Set(%{$config{imOpt}}); if (defined $sample) { $ret = $image->Sample(width=>$w, height=>$h); } else { $ret = $image->Scale(width=>$w, height=>$h); } return $ret if "$ret"; $ret = $image->Write($thumb); return $ret if "$ret"; } #================================= #----------------- sub calcGeometry { my ($w,$h) = @_; if($w>$h) { $h = $h/($w/$config{thWidth}); $w = $config{thWidth}; } else { $w = $w/($h/$config{thHeight}); $h = $config{thHeight}; } return int($w), int($h); }