]> git.imager.perl.org - imager.git/blob - lib/Imager/Transformations.pod
b3f98f5fabbef1abeba9019d93259ecd0ceb3ce6
[imager.git] / lib / Imager / Transformations.pod
1 =head1 NAME
2
3 Imager::Transformations - Simple transformations of one image into another.
4
5 =head1 SYNOPSIS
6
7   use Imager;
8
9   $newimg = $img->copy();
10
11   $newimg = $img->scale(xpixels=>400);
12   $newimg = $img->scale(xpixels=>400, ypixels=>400);
13   $newimg = $img->scale(xpixels=>400, ypixels=>400, type=>min);
14   $newimg = $img->scale(scalefactor=>0.25);
15
16   $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100); 
17   $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
18
19   $dest->paste(left=>40,top=>20,img=>$logo);
20
21   $img->rubthrough(src=>$srcimage,tx=>30,ty=>50);
22
23
24   $img->flip(dir=>"h");       # horizontal flip
25   $img->flip(dir=>"vh");      # vertical and horizontal flip
26   $newimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
27
28   my $rot20 = $img->rotate(degrees=>20);
29   my $rotpi4 = $img->rotate(radians=>3.14159265/4);
30
31
32   # Convert image to gray
33   $new = $img->convert(preset=>'grey');          
34
35   # Swap red/green channel  
36   $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
37                                  [ 1, 0, 0 ],
38                                  [ 0, 0, 1 ] ]);
39
40   # limit the range of red channel from 0..255 to 0..127
41   @map = map { int( $_/2 } 0..255;
42   $img->map( red=>\@map );
43
44   # Apply a Gamma of 1.4
45   my $gamma = 1.4;
46   my @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
47   $img->map(all=>\@map);  # inplace conversion
48
49 =head1 DESCRIPTION
50
51 The methods described in Imager::Transformations fall into two categories.
52 Either they take an existing image and modify it in place, or they 
53 return a modified copy.
54
55 Functions that modify inplace are C<flip()>, C<paste()> and
56 C<rubthrough()>.  If the original is to be left intact it's possible
57 to make a copy and alter the copy:
58
59   $flipped = $img->copy()->flip(dir=>'h');
60
61 =head2 Image copying/resizing/cropping/rotating
62
63 A list of the transformations that do not alter the source image follows:
64
65 =over
66
67 =item copy
68
69 To create a copy of an image use the C<copy()> method.  This is usefull
70 if you want to keep an original after doing something that changes the image.
71
72   $newimg = $orig->copy();
73
74 =item scale
75
76 To scale an image so porportions are maintained use the
77 C<$img-E<gt>scale()> method.  if you give either a xpixels or ypixels
78 parameter they will determine the width or height respectively.  If
79 both are given the one resulting in a larger image is used.  example:
80 C<$img> is 700 pixels wide and 500 pixels tall.
81
82   $newimg = $img->scale(xpixels=>400); # 400x285
83   $newimg = $img->scale(ypixels=>400); # 560x400
84
85   $newimg = $img->scale(xpixels=>400,ypixels=>400); # 560x400
86   $newimg = $img->scale(xpixels=>400,ypixels=>400,type=>min); # 400x285
87
88   $newimg = $img->scale(scalefactor=>0.25); 175x125 
89   $newimg = $img->scale(); # 350x250
90
91 if you want to create low quality previews of images you can pass
92 C<qtype=E<gt>'preview'> to scale and it will use nearest neighbor
93 sampling instead of filtering. It is much faster but also generates
94 worse looking images - especially if the original has a lot of sharp
95 variations and the scaled image is by more than 3-5 times smaller than
96 the original.
97
98 If you need to scale images per axis it is best to do it simply by
99 calling scaleX and scaleY.  You can pass either 'scalefactor' or
100 'pixels' to both functions.
101
102 =item crop
103
104
105 Another way to resize an image size is to crop it.  The parameters
106 to crop are the edges of the area that you want in the returned image.
107 If a parameter is omited a default is used instead.
108
109   $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100); 
110   $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
111   $newimg = $img->crop(left=>50, right=>100); # top 
112
113 You can also specify width and height parameters which will produce a
114 new image cropped from the center of the input image, with the given
115 width and height.
116
117   $newimg = $img->crop(width=>50, height=>50);
118
119 The width and height parameters take precedence over the left/right
120 and top/bottom parameters respectively.
121
122 =item rotate
123
124
125 Use the rotate() method to rotate an image.  This method will return a
126 new, rotated image.
127
128 To rotate by an exact amount in degrees or radians, use the 'degrees'
129 or 'radians' parameter:
130
131   my $rot20 = $img->rotate(degrees=>20);
132   my $rotpi4 = $img->rotate(radians=>3.14159265/4);
133
134 Exact image rotation uses the same underlying transformation engine as
135 the matrix_transform() method.
136
137 To rotate in steps of 90 degrees, use the 'right' parameter:
138
139   my $rotated = $img->rotate(right=>270);
140
141 Rotations are clockwise for positive values.
142
143 =back
144
145
146 =head2 Image pasting/flipping/
147
148 A list of the transformations that alter the source image follows:
149
150 =over
151
152 =item paste
153
154
155 To copy an image to onto another image use the C<paste()> method.
156
157   $dest->paste(left=>40,top=>20,img=>$logo);
158
159 That copies the entire C<$logo> image onto the C<$dest> image so that the
160 upper left corner of the C<$logo> image is at (40,20).
161
162
163 =item rubthrough
164
165 A more complicated way of blending images is where one image is
166 put 'over' the other with a certain amount of opaqueness.  The
167 method that does this is rubthrough.
168
169   $img->rubthrough(src=>$srcimage,tx=>30,ty=>50);
170
171 That will take the image C<$srcimage> and overlay it with the upper
172 left corner at (30,50).  You can rub 2 or 4 channel images onto a 3
173 channel image, or a 2 channel image onto a 1 channel image.  The last
174 channel is used as an alpha channel.
175
176
177 =item flip
178
179 An inplace horizontal or vertical flip is possible by calling the
180 C<flip()> method.  If the original is to be preserved it's possible to
181 make a copy first.  The only parameter it takes is the C<dir>
182 parameter which can take the values C<h>, C<v>, C<vh> and C<hv>.
183
184   $img->flip(dir=>"h");       # horizontal flip
185   $img->flip(dir=>"vh");      # vertical and horizontal flip
186   $nimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
187
188 =back
189
190
191
192
193 =head2 Color transformations
194
195 You can use the convert method to transform the color space of an
196 image using a matrix.  For ease of use some presets are provided.
197
198 The convert method can be used to:
199
200 =over
201
202 =item *
203
204 convert an RGB or RGBA image to grayscale.
205
206 =item *
207
208 convert a grayscale image to RGB.
209
210 =item *
211
212 extract a single channel from an image.
213
214 =item *
215
216 set a given channel to a particular value (or from another channel)
217
218 =back
219
220 The currently defined presets are:
221
222 =over
223
224 =item gray
225
226 =item grey
227
228 converts an RGBA image into a grayscale image with alpha channel, or
229 an RGB image into a grayscale image without an alpha channel.
230
231 This weights the RGB channels at 22.2%, 70.7% and 7.1% respectively.
232
233 =item noalpha
234
235 removes the alpha channel from a 2 or 4 channel image.  An identity
236 for other images.
237
238 =item red
239
240 =item channel0
241
242 extracts the first channel of the image into a single channel image
243
244 =item green
245
246 =item channel1
247
248 extracts the second channel of the image into a single channel image
249
250 =item blue
251
252 =item channel2
253
254 extracts the third channel of the image into a single channel image
255
256 =item alpha
257
258 extracts the alpha channel of the image into a single channel image.
259
260 If the image has 1 or 3 channels (assumed to be grayscale of RGB) then
261 the resulting image will be all white.
262
263 =item rgb
264
265 converts a grayscale image to RGB, preserving the alpha channel if any
266
267 =item addalpha
268
269 adds an alpha channel to a grayscale or RGB image.  Preserves an
270 existing alpha channel for a 2 or 4 channel image.
271
272 =back
273
274 For example, to convert an RGB image into a greyscale image:
275
276   $new = $img->convert(preset=>'grey'); # or gray
277
278 or to convert a grayscale image to an RGB image:
279
280   $new = $img->convert(preset=>'rgb');
281
282 The presets aren't necessary simple constants in the code, some are
283 generated based on the number of channels in the input image.
284
285 If you want to perform some other colour transformation, you can use
286 the 'matrix' parameter.
287
288 For each output pixel the following matrix multiplication is done:
289
290   | channel[0] |   | $c00, ...,  $c0k |   | inchannel[0] |
291   |    ...     | = |       ...        | x |     ...      |
292   | channel[k] |   | $ck0, ...,  $ckk |   | inchannel[k] |
293                                                           1
294 Where C<k = $img-&gt;getchannels()-1>.
295
296 So if you want to swap the red and green channels on a 3 channel image:
297
298   $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
299                                  [ 1, 0, 0 ],
300                                  [ 0, 0, 1 ] ]);
301
302 or to convert a 3 channel image to greyscale using equal weightings:
303
304   $new = $img->convert(matrix=>[ [ 0.333, 0.333, 0.334 ] ])
305
306
307 =head2 Color Mappings
308
309 You can use the map method to map the values of each channel of an
310 image independently using a list of lookup tables.  It's important to
311 realize that the modification is made inplace.  The function simply
312 returns the input image again or undef on failure.
313
314 Each channel is mapped independently through a lookup table with 256
315 entries.  The elements in the table should not be less than 0 and not
316 greater than 255.  If they are out of the 0..255 range they are
317 clamped to the range.  If a table does not contain 256 entries it is
318 silently ignored.
319
320 Single channels can mapped by specifying their name and the mapping
321 table.  The channel names are C<red>, C<green>, C<blue>, C<alpha>.
322
323   @map = map { int( $_/2 } 0..255;
324   $img->map( red=>\@map );
325
326 It is also possible to specify a single map that is applied to all
327 channels, alpha channel included.  For example this applies a gamma
328 correction with a gamma of 1.4 to the input image.
329
330   $gamma = 1.4;
331   @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
332   $img->map(all=> \@map);
333
334 The C<all> map is used as a default channel, if no other map is
335 specified for a channel then the C<all> map is used instead.  If we
336 had not wanted to apply gamma to the alpha channel we would have used:
337
338   $img->map(all=> \@map, alpha=>[]);
339
340 Since C<[]> contains fewer than 256 element the gamma channel is
341 unaffected.
342
343 It is also possible to simply specify an array of maps that are
344 applied to the images in the rgba order.  For example to apply
345 maps to the C<red> and C<blue> channels one would use:
346
347   $img->map(maps=>[\@redmap, [], \@bluemap]);
348
349
350
351
352