-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathREADME
222 lines (168 loc) · 8.06 KB
/
README
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
NAME
Image::Scale - Fast, high-quality fixed-point image resizing
SYNOPSIS
use Image::Scale;
# Resize to 150 width and save to a file
my $img = Image::Scale->new('image.jpg') || die "Invalid JPEG file";
$img->resize_gd( { width => 150 } );
$img->save_jpeg('resized.jpg');
# Easily resize artwork embedded within an audio file
# You can use L<Audio::Scan> to obtain offset/length information
my $img = Image::Scale->new( 'track.mp3', { offset => 2200, length => 34123 } );
$img->resize_gd_fixed_point( { width => 75, height => 75, keep_aspect => 1 } );
my $data = $img->as_png();
DESCRIPTION
This module implements several resizing algorithms with a focus on low
overhead, speed and minimal features. Algorithms available are:
GD's copyResampled (floating-point)
GD's copyResampled fixed-point (useful on embedded devices/NAS devices)
GraphicsMagick's assortment of resize filters (floating-point)
GraphicsMagick's Triangle filter in fixed-point
Supported image formats include JPEG, GIF, PNG, and BMP for input, and
JPEG and PNG for output.
This module came about because we needed to improve the very slow
performance of floating-point resizing algorithms on platforms without a
floating-point unit, such as ARM devices like the SheevaPlug, and the
Sparc-based ReadyNAS Duo. Previously it would take many seconds to
resize using GD on the ReadyNAS but the conversion to fixed-point with a
little assembly code brings this down to the range of well under 1
second.
Normal platforms will also see improvement, by removing all of the GD
overhead this version of copyResampled is around 3 times faster while
also using less memory.
The fixed-point versions have an accuracy to around 4 decimal places so
the quality of floating-point vs. fixed is essentially identical.
METHODS
new( $PATH or \$DATA, [ \%OPTIONS ] )
Initialize a new Image::Scale object from PATH, which may be any valid
JPEG, GIF, PNG, or BMP file.
Raw image data may also be passed in as a scalar reference. Using a file
path is recommended when possible as this is more efficient and requires
less memory.
new() reads the image header, and will return undef if the header is
invalid, so be sure to check for this.
Optionally you can also pass in additional options in a hashref:
offset
length
To access an image embedded within another file, such as an audio file,
you can specify a byte offset and length.
width()
Returns the width of the original source image.
height()
Returns the height of the original source image.
resize( \%OPTIONS )
resize() uses the default resize algorithm, which is
resize_gd_fixed_point. See below for details on the available options.
resize_gd( \%OPTIONS )
resize_gd_fixed_point( \%OPTIONS )
resize_gm( \%OPTIONS )
resize_gm_fixed_point( \%OPTIONS )
The 4 resize methods available are:
resize_gd - This is GD's copyResampled algorithm (floating-point)
resize_gd_fixed_point - copyResampled (converted to fixed-point)
resize_gm - GraphicsMagick, see below for filter options
resize_gm_fixed_point - GraphicsMagick, only the Triangle filter is available in fixed-point mode
Options are specified in a hashref:
width
height
At least one of width or height are required. If only one is supplied
the image will retain the original aspect ratio.
filter
For use with resize_gm() only. Choose from the following filters, sorted
in order from least to most CPU time. This does not necessarily mean
least to best quality, though! Be sure to do your own comparisons for
quality.
Point
Box
Triangle
Hermite
Hanning
Hamming
Blackman
Gaussian
Quadratic
Cubic
Catrom
Mitchell
Lanczos
Bessel
Sinc
If no filter is specified the default is Lanczos if downsizing, and
Mitchell for upsizing or if the image has an alpha channel.
keep_aspect => 1
Only useful when both width and height are specified. This option will
keep the original aspect ratio of the source as well as center the image
when resizing into a different aspect ratio. For best results, images
altered in this way should be saved as PNG which will automatically add
the necessary transparency around the image.
bgcolor => 0xffffff
When using keep_aspect, you can use bgcolor to define the background
color of the padded portion of the image. Usually this should only be
used if saving as JPEG because PNG will default to transparent. If this
value is set and the image is saved as PNG, the PNG will not be
transparent. The default bgcolor value is 0x000000 (black).
ignore_exif => 1
By default, if a JPEG image contains an EXIF tag with orientation info,
the image will be rotated accordingly during resizing. To disable this
feature, set ignore_exif to 1.
memory_limit => $limit_in_bytes
To avoid excess memory growth when resizing images that may be very
large, you can specify this option. If the resize_*() method would
result in a total memory allocation greater than $limit_in_bytes, the
method will die. Be sure to wrap the resize call in an eval when using
this option.
save_jpeg( $PATH, [ $QUALITY ] )
Saves the resized image as a JPEG to PATH. If a quality is not
specified, the quality defaults to 90.
as_jpeg( [ $QUALITY ] )
Returns the resized JPEG image as scalar data. If a quality is not
specified, the quality defaults to 90.
save_png( $PATH )
Saves the resized image as a PNG to PATH. Transparency is preserved when
saving to PNG.
as_png()
Returns the resized PNG image as scalar data.
jpeg_version()
png_version()
gif_version()
Returns the version of the image library used. Returns undef if support
for that image format was not built.
PERFORMANCE
These numbers were gathered on my 2.4ghz MacBook Pro.
JPEG image, 1425x1425 -> 200x200 (libjpeg v8 with scaling)
GD copyResampled 21.9/s
resize_gm( { filter => 'Triangle' } ) 65.7/s
resize_gd_fixed_point 67.9/s
resize_gd 69.4/s
resize_gm_fixed_point 74.5/s
PNG image, 512x768 -> 200x133 (libpng 1.4.3)
GD copyResampled 14.7/s
resize_gm( { filter => 'Triangle' } ) 26.2/s
resize_gm_fixed_point 27.7/s
resize_gd 29.9/s
resize_gd_fixed_point 31.9/s
Here are some numbers from a machine without floating-point support.
(Marvell SheevaPlug 1.2ghz ARM9, JPEG 1425x1425 -> 200x200, libjpeg 6b
with scaling)
GD copyResampled 1.08/s
resize_gd 2.16/s
resize_gm( { filter => 'Triangle' } ) 2.85/s
resize_gd_fixed_point 7.98/s
resize_gm_fixed_point 9.44/s
And finally, from an even slower machine, the 240mhz Netgear ReadyNAS
Duo which has extremely poor floating-point performance. (JPEG 1425x1425
-> 200x200, libjpeg 6b with scaling)
resize_gd 0.029/s (34.5 s/iter)
resize_gm( { filter => 'Triangle' } ) 0.033/s (30.4 s/iter)
resize_gd_fixed_point 1.92/s (0.522 s/iter)
resize_gm_fixed_point 2.07/s (0.483 s/iter) (63x faster than floating-point!)
SEE ALSO
TODO
AUTHOR
Andy Grundman, <[email protected]>
COPYRIGHT AND LICENSE
Copyright (C) 2010 Andy Grundman
This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.