| 1 | package Image::Scale; |
| 2 | |
| 3 | use strict; |
| 4 | |
| 5 | use constant IMAGE_SCALE_TYPE_GD => 0; |
| 6 | use constant IMAGE_SCALE_TYPE_GD_FIXED => 1; |
| 7 | use constant IMAGE_SCALE_TYPE_GM => 2; |
| 8 | use constant IMAGE_SCALE_TYPE_GM_FIXED => 3; |
| 9 | |
| 10 | our $VERSION = '0.14'; |
| 11 | |
| 12 | require XSLoader; |
| 13 | XSLoader::load('Image::Scale', $VERSION); |
| 14 | |
| 15 | sub new { |
| 16 | my $class = shift; |
| 17 | my $self; |
| 18 | |
| 19 | my $file = shift || die "Image::Scale->new requires an image path\n"; |
| 20 | my $opts = shift || {}; |
| 21 | |
| 22 | if ( ref $file eq 'SCALAR' ) { |
| 23 | $self = bless { |
| 24 | data => $file, |
| 25 | %{$opts}, |
| 26 | }; |
| 27 | } |
| 28 | else { |
| 29 | if ( !-e $file ) { |
| 30 | die "Image::Scale couldn't find $file\n"; |
| 31 | } |
| 32 | |
| 33 | open my $fh, '<', $file || die "Image::Scale couldn't open $file: $!\n"; |
| 34 | binmode $fh; |
| 35 | |
| 36 | $self = bless { |
| 37 | file => $file, |
| 38 | _fh => $fh, |
| 39 | %{$opts}, |
| 40 | }; |
| 41 | } |
| 42 | |
| 43 | # XS init, determine the file type and image size |
| 44 | $self->{_image} = $self->__init(); |
| 45 | |
| 46 | # __init will return undef on any errors |
| 47 | return if !$self->{_image}; |
| 48 | |
| 49 | return $self; |
| 50 | } |
| 51 | |
| 52 | sub resize_gd { |
| 53 | shift->resize( { %{+shift}, type => IMAGE_SCALE_TYPE_GD } ); |
| 54 | } |
| 55 | |
| 56 | sub resize_gd_fixed_point { |
| 57 | shift->resize( { %{+shift}, type => IMAGE_SCALE_TYPE_GD_FIXED } ); |
| 58 | } |
| 59 | |
| 60 | sub resize_gm { |
| 61 | shift->resize( { %{+shift}, type => IMAGE_SCALE_TYPE_GM } ); |
| 62 | } |
| 63 | |
| 64 | sub resize_gm_fixed_point { |
| 65 | shift->resize( { %{+shift}, type => IMAGE_SCALE_TYPE_GM_FIXED } ); |
| 66 | } |
| 67 | |
| 68 | sub DESTROY { |
| 69 | my $self = shift; |
| 70 | |
| 71 | # XS cleanup |
| 72 | $self->__cleanup( $self->{_image} ) if $self->{_image}; |
| 73 | |
| 74 | close $self->{_fh} if $self->{_fh}; |
| 75 | } |
| 76 | |
| 77 | 1; |
| 78 | __END__ |
| 79 | |
| 80 | =head1 NAME |
| 81 | |
| 82 | Image::Scale - Fast, high-quality fixed-point image resizing |
| 83 | |
| 84 | =head1 SYNOPSIS |
| 85 | |
| 86 | use Image::Scale; |
| 87 | |
| 88 | # Resize to 150 width and save to a file |
| 89 | my $img = Image::Scale->new('image.jpg') || die "Invalid JPEG file"; |
| 90 | $img->resize_gd( { width => 150 } ); |
| 91 | $img->save_jpeg('resized.jpg'); |
| 92 | |
| 93 | # Easily resize artwork embedded within an audio file |
| 94 | # You can use L<Audio::Scan> to obtain offset/length information |
| 95 | my $img = Image::Scale->new( 'track.mp3', { offset => 2200, length => 34123 } ); |
| 96 | $img->resize_gd_fixed_point( { width => 75, height => 75, keep_aspect => 1 } ); |
| 97 | my $data = $img->as_png(); |
| 98 | |
| 99 | =head1 DESCRIPTION |
| 100 | |
| 101 | This module implements several resizing algorithms with a focus on low overhead, |
| 102 | speed and minimal features. Algorithms available are: |
| 103 | |
| 104 | GD's copyResampled (floating-point) |
| 105 | GD's copyResampled fixed-point (useful on embedded devices/NAS devices) |
| 106 | GraphicsMagick's assortment of resize filters (floating-point) |
| 107 | GraphicsMagick's Triangle filter in fixed-point |
| 108 | |
| 109 | Supported image formats include JPEG, GIF, PNG, and BMP for input, and |
| 110 | JPEG and PNG for output. |
| 111 | |
| 112 | This module came about because we needed to improve the very slow performance of |
| 113 | floating-point resizing algorithms on platforms without a floating-point |
| 114 | unit, such as ARM devices like the SheevaPlug, and the Sparc-based ReadyNAS Duo. |
| 115 | Previously it would take many seconds to resize using GD on the ReadyNAS but the |
| 116 | conversion to fixed-point with a little assembly code brings this down to the range of |
| 117 | well under 1 second. |
| 118 | |
| 119 | GD is also incredibly difficult to build on platforms such as Windows so we |
| 120 | needed a replacement. |
| 121 | |
| 122 | Normal platforms will also see improvement, by removing all of the GD overhead this |
| 123 | version of copyResampled is around 3 times faster while also using less memory. |
| 124 | |
| 125 | The fixed-point versions have an accuracy to around 4 decimal places so the quality |
| 126 | of floating-point vs. fixed is essentially identical. |
| 127 | |
| 128 | =head1 METHODS |
| 129 | |
| 130 | =head2 new( $PATH or \$DATA, [ \%OPTIONS ] ) |
| 131 | |
| 132 | Initialize a new Image::Scale object from PATH, which may be any valid JPEG, |
| 133 | GIF, PNG, or BMP file. |
| 134 | |
| 135 | Raw image data may also be passed in as a scalar reference. Using a file path |
| 136 | is recommended when possible as this is more efficient and requires less memory. |
| 137 | |
| 138 | new() reads the image header, and will return undef if the header is invalid, |
| 139 | so be sure to check for this. |
| 140 | |
| 141 | Optionally you can also pass in additional options in a hashref: |
| 142 | |
| 143 | offset |
| 144 | length |
| 145 | |
| 146 | To access an image embedded within another file, such as an audio file, you can |
| 147 | specify a byte offset and length. |
| 148 | |
| 149 | =head2 width() |
| 150 | |
| 151 | Returns the width of the original source image. |
| 152 | |
| 153 | =head2 height() |
| 154 | |
| 155 | Returns the height of the original source image. |
| 156 | |
| 157 | =head2 resized_width() |
| 158 | |
| 159 | Returns the resized width from the last call to resize_*(). Returns 0 if no |
| 160 | resize function has been called yet. |
| 161 | |
| 162 | =head2 resized_height() |
| 163 | |
| 164 | Returns the resized height from the last call to resize_*(). Returns 0 if no |
| 165 | resize function has been called yet. |
| 166 | |
| 167 | =head2 resize( \%OPTIONS ) |
| 168 | |
| 169 | resize() uses the default resize algorithm, which is resize_gd_fixed_point. See below |
| 170 | for details on the available options. |
| 171 | |
| 172 | =head2 resize_gd( \%OPTIONS ) |
| 173 | |
| 174 | =head2 resize_gd_fixed_point( \%OPTIONS ) |
| 175 | |
| 176 | =head2 resize_gm( \%OPTIONS ) |
| 177 | |
| 178 | =head2 resize_gm_fixed_point( \%OPTIONS ) |
| 179 | |
| 180 | The 4 resize methods available are: |
| 181 | |
| 182 | resize_gd - This is GD's copyResampled algorithm (floating-point) |
| 183 | resize_gd_fixed_point - copyResampled (converted to fixed-point) |
| 184 | resize_gm - GraphicsMagick, see below for filter options |
| 185 | resize_gm_fixed_point - GraphicsMagick, only the Triangle filter is available in fixed-point mode |
| 186 | |
| 187 | Options are specified in a hashref: |
| 188 | |
| 189 | width |
| 190 | height |
| 191 | |
| 192 | At least one of width or height are required. If only one is supplied the |
| 193 | image will retain the original aspect ratio. |
| 194 | |
| 195 | filter |
| 196 | |
| 197 | For use with resize_gm() only. Choose from the following filters, sorted in order |
| 198 | from least to most CPU time. This does not necessarily mean least to best quality, though! |
| 199 | Be sure to do your own comparisons for quality. |
| 200 | |
| 201 | Point |
| 202 | Box |
| 203 | Triangle |
| 204 | Hermite |
| 205 | Hanning |
| 206 | Hamming |
| 207 | Blackman |
| 208 | Gaussian |
| 209 | Quadratic |
| 210 | Cubic |
| 211 | Catrom |
| 212 | Mitchell |
| 213 | Lanczos |
| 214 | Bessel |
| 215 | Sinc |
| 216 | |
| 217 | If no filter is specified the default is Lanczos if downsizing, and Mitchell for upsizing or |
| 218 | if the image has an alpha channel. |
| 219 | |
| 220 | keep_aspect => 1 |
| 221 | |
| 222 | Only useful when both width and height are specified. This option will keep the |
| 223 | original aspect ratio of the source as well as center the image when resizing into |
| 224 | a different aspect ratio. For best results, images altered in this way should be |
| 225 | saved as PNG which will automatically add the necessary transparency around the image. |
| 226 | |
| 227 | bgcolor => 0xffffff |
| 228 | |
| 229 | When using keep_aspect, you can use bgcolor to define the background color of the padded |
| 230 | portion of the image. Usually this should only be used if saving as JPEG because PNG |
| 231 | will default to transparent. If this value is set and the image is saved as PNG, the |
| 232 | PNG will not be transparent. The default bgcolor value is 0x000000 (black). |
| 233 | |
| 234 | ignore_exif => 1 |
| 235 | |
| 236 | By default, if a JPEG image contains an EXIF tag with orientation info, the image will be |
| 237 | rotated accordingly during resizing. To disable this feature, set ignore_exif to 1. |
| 238 | |
| 239 | memory_limit => $limit_in_bytes |
| 240 | |
| 241 | To avoid excess memory growth when resizing images that may be very |
| 242 | large, you can specify this option. If the resize_*() method would result in a |
| 243 | total memory allocation greater than $limit_in_bytes, the method will die. |
| 244 | Be sure to wrap the resize call in an eval when using this option. |
| 245 | |
| 246 | =head2 save_jpeg( $PATH, [ $QUALITY ] ) |
| 247 | |
| 248 | Saves the resized image as a JPEG to PATH. If a quality is not specified, the |
| 249 | quality defaults to 90. |
| 250 | |
| 251 | =head2 as_jpeg( [ $QUALITY ] ) |
| 252 | |
| 253 | Returns the resized JPEG image as scalar data. If a quality is not specified, the |
| 254 | quality defaults to 90. |
| 255 | |
| 256 | =head2 save_png( $PATH ) |
| 257 | |
| 258 | Saves the resized image as a PNG to PATH. Transparency is preserved when saving to PNG. |
| 259 | |
| 260 | =head2 as_png() |
| 261 | |
| 262 | Returns the resized PNG image as scalar data. |
| 263 | |
| 264 | =head2 jpeg_version() |
| 265 | |
| 266 | =head2 png_version() |
| 267 | |
| 268 | =head2 gif_version() |
| 269 | |
| 270 | Returns the version of the image library used. Returns undef if support for that image |
| 271 | format was not built. |
| 272 | |
| 273 | =head1 PERFORMANCE |
| 274 | |
| 275 | These numbers were gathered on my 2.4ghz MacBook Pro with version 0.06. |
| 276 | |
| 277 | JPEG image, 1425x1425 -> 100x100 (libjpeg-turbo 1.0.0 with pre-scaling) |
| 278 | Note that GD does not support JPEG pre-scaling which results |
| 279 | in very poor performance and high memory usage. These numbers also include |
| 280 | returning the resized image as a JPEG. |
| 281 | |
| 282 | GD copyResampled 4.8/s |
| 283 | resize_gm( { filter => 'Triangle' } ) 127/s |
| 284 | resize_gd_fixed_point 128/s |
| 285 | resize_gd 131/s |
| 286 | resize_gm_fixed_point 133/s |
| 287 | |
| 288 | PNG image, 350x350 -> 100x100 (libpng 1.4.3) |
| 289 | libpng is quite slow, probably because they were forced to remove a lot of assembly |
| 290 | code recently. These numbers also include returning the resized image as a PNG. |
| 291 | |
| 292 | GD copyResampled 46.1/s |
| 293 | resize_gm( { filter => 'Triangle' } ) 61.4/s |
| 294 | resize_gm_fixed_point 64.9/s |
| 295 | resize_gd 76.0/s |
| 296 | resize_gd_fixed_point 77.6/s |
| 297 | |
| 298 | Here are some numbers from a machine without floating-point support (version 0.01). |
| 299 | (Marvell SheevaPlug 1.2ghz ARM9, JPEG 1425x1425 -> 200x200, libjpeg 6b with scaling) |
| 300 | |
| 301 | GD copyResampled 1.08/s |
| 302 | resize_gd 2.16/s |
| 303 | resize_gm( { filter => 'Triangle' } ) 2.85/s |
| 304 | resize_gd_fixed_point 7.98/s |
| 305 | resize_gm_fixed_point 9.44/s |
| 306 | |
| 307 | And finally, from an even slower machine, the 240mhz Netgear ReadyNAS Duo which |
| 308 | has extremely poor floating-point performance (version 0.01). |
| 309 | (JPEG 1425x1425 -> 200x200, libjpeg 6b with scaling) |
| 310 | |
| 311 | resize_gd 0.029/s (34.5 s/iter) |
| 312 | resize_gm( { filter => 'Triangle' } ) 0.033/s (30.4 s/iter) |
| 313 | resize_gd_fixed_point 1.92/s (0.522 s/iter) |
| 314 | resize_gm_fixed_point 2.07/s (0.483 s/iter) (63x faster than floating-point!) |
| 315 | |
| 316 | =head1 SEE ALSO |
| 317 | |
| 318 | L<GD>, |
| 319 | L<Image::Magick>, |
| 320 | L<Imager> |
| 321 | |
| 322 | =head1 AUTHOR |
| 323 | |
| 324 | Andy Grundman, E<lt>[email protected]<gt> |
| 325 | |
| 326 | =head1 COPYRIGHT AND LICENSE |
| 327 | |
| 328 | Copyright (C) 2010 Andy Grundman |
| 329 | |
| 330 | This program is free software; you can redistribute it and/or modify |
| 331 | it under the terms of the GNU General Public License as published by |
| 332 | the Free Software Foundation; either version 2 of the License, or |
| 333 | (at your option) any later version. |
| 334 | |
| 335 | =cut |