1Imager::TransformationsU(s3e)r Contributed Perl DocumentaItmiaogner::Transformations(3)
2
3
4

NAME

6       Imager::Transformations - Simple transformations of one image into
7       another.
8

SYNOPSIS

10         use Imager;
11
12         $newimg = $img->copy();
13
14         $newimg = $img->scale(xpixels=>400, qtype => 'mixing');
15         $newimg = $img->scale(xpixels=>400, ypixels=>400);
16         $newimg = $img->scale(xpixels=>400, ypixels=>400, type=>'min');
17         $newimg = $img->scale(scalefactor=>0.25);
18
19         $newimg = $img->scaleX(pixels=>400);
20         $newimg = $img->scaleX(scalefactor=>0.25);
21         $newimg = $img->scaleY(pixels=>400);
22         $newimg = $img->scaleY(scalefactor=>0.25);
23
24         $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
25         $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
26
27         $dest->paste(left=>40,top=>20,img=>$logo);
28
29         $img->rubthrough(src=>$srcimage,tx=>30, ty=>50);
30         $img->rubthrough(src=>$srcimage,tx=>30, ty=>50,
31                          src_minx=>20, src_miny=>30,
32                          src_maxx=>20, src_maxy=>30);
33
34         $img->compose(src => $src, tx => 30, ty => 20, combine => 'color');
35         $img->compose(src => $src, tx => 30, ty => 20, combine => 'color');
36                       mask => $mask, opacity => 0.5);
37
38         $img->flip(dir=>"h");       # horizontal flip
39         $img->flip(dir=>"vh");      # vertical and horizontal flip
40         $newimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
41
42         my $rot20 = $img->rotate(degrees=>20);
43         my $rotpi4 = $img->rotate(radians=>3.14159265/4);
44
45         # Convert image to gray
46         $new = $img->convert(preset=>'grey');
47
48         # Swap red/green channel
49         $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
50                                        [ 1, 0, 0 ],
51                                        [ 0, 0, 1 ] ]);
52
53         # limit the range of red channel from 0..255 to 0..127
54         @map = map { int( $_/2 } 0..255;
55         $img->map( red=>\@map );
56
57         # Apply a Gamma of 1.4
58         my $gamma = 1.4;
59         my @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
60         $img->map(all=>\@map);  # inplace conversion
61

DESCRIPTION

63       The methods described in Imager::Transformations fall into two cate‐
64       gories.  Either they take an existing image and modify it in place, or
65       they return a modified copy.
66
67       Functions that modify inplace are "flip()", "paste()", "rubthrough()"
68       and "compose()".  If the original is to be left intact it's possible to
69       make a copy and alter the copy:
70
71         $flipped = $img->copy()->flip(dir=>'h');
72
73       Image copying/resizing/cropping/rotating
74
75       A list of the transformations that do not alter the source image fol‐
76       lows:
77
78       copy
79           To create a copy of an image use the "copy()" method.  This is use‐
80           full if you want to keep an original after doing something that
81           changes the image.
82
83             $newimg = $orig->copy();
84
85       scale
86           To scale an image so porportions are maintained use the
87           "$img->scale()" method.  if you give either a xpixels or ypixels
88           parameter they will determine the width or height respectively.  If
89           both are given the one resulting in a larger image is used, unless
90           you set the "type" parameter to 'min'.  example: $img is 700 pixels
91           wide and 500 pixels tall.
92
93             $newimg = $img->scale(xpixels=>400); # 400x285
94             $newimg = $img->scale(ypixels=>400); # 560x400
95
96             $newimg = $img->scale(xpixels=>400,ypixels=>400); # 560x400
97             $newimg = $img->scale(xpixels=>400,ypixels=>400,type=>'min'); # 400x285
98
99             $newimg = $img->scale(xpixels=>400, ypixels=>400),type=>'nonprop'); # 400x400
100
101             $newimg = $img->scale(scalefactor=>0.25); 175x125
102             $newimg = $img->scale(); # 350x250
103
104           If you want to create low quality previews of images you can pass
105           "qtype=>'preview'" to scale and it will use nearest neighbor sam‐
106           pling instead of filtering. It is much faster but also generates
107           worse looking images - especially if the original has a lot of
108           sharp variations and the scaled image is by more than 3-5 times
109           smaller than the original.
110
111           *   xpixels, ypixels - desired size of the scaled image.  The
112               "type" parameter controls whether the larger or smaller of the
113               two possible sizes is chosen, or if the image is scaled
114               non-proportionally.
115
116           *   constrain - an Image::Math::Constrain object defining the way
117               in which the image size should be constrained.
118
119           *   scalefactor - if none of xpixels, ypixels, xscalefactor,
120               yscalefactor or constrain is supplied then this is used as the
121               ratio to scale by.  Default: 0.5.
122
123           *   xscalefactor, yscalefactor - if both are supplied then the
124               image is scaled as per these parameters, whether this is pro‐
125               portionally or not.  New in Imager 0.54.
126
127           *   type - controls whether the larger or smaller of the two possi‐
128               ble sizes is chosen, possible values are:
129
130               *   min - the smaller of the 2 sizes are chosen.
131
132               *   max - the larger of the 2 sizes.  This is the default.
133
134               *   nonprop - non-proportional scaling.  New in Imager 0.54.
135
136               scale() will fail if "type" is set to some other value.
137
138               For example, if the original image is 400 pixels wide by 200
139               pixels high and "xpixels" is set to 300, and "ypixels" is set
140               to 160.  When "type" is 'min' the resulting image is 300 x 150,
141               when "type" is 'max' the resulting image is 320 x 150.
142
143               "type" is only used if both "xpixels" and "ypixels" are sup‐
144               plied.
145
146           *   qtype - defines the quality of scaling performed.  Possible
147               values are:
148
149               *   "normal" - high quality scaling.  This is the default.
150
151               *   "preview" - lower quality.  When scaling down this will
152                   skip input pixels, eg. scaling by 0.5 will skip every other
153                   pixel.  When scaling up this will duplicate pixels.
154
155               *   "mixing" - implements the mixing algorithm implemented by
156                   pnmscale.  This retains more detail when scaling down than
157                   "normal".  When scaling down this proportionally accumu‐
158                   lates sample data from the pixels, resulting in a propor‐
159                   tional mix of all of the pixels.  When scaling up this will
160                   mix pixels when the sampling grid crosses a pixel boundary
161                   but will otherwise copy pixel values.
162
163               scale() will fail if "qtype" is set to some other value.
164
165               "preview" is faster than "mixing" which is much faster than
166               "normal".
167
168           To scale an image on a given axis without maintaining proportions,
169           it is best to call the scaleX() and scaleY() methods with the
170           required dimensions. eg.
171
172             my $scaled = $img->scaleX(pixels=>400)->scaleY(pixels=>200);
173
174           From Imager 0.54 you can scale without maintaining proportions
175           either by supplying both the xscalefactor and yscalefactor argu‐
176           ments:
177
178             my $scaled = $img->scale(xscalefactor => 0.5, yscalefactor => 0.67);
179
180           or by supplying "xpixels" and "ypixels" and setting "type" to "non‐
181           prop":
182
183             my $scaled = $im->scale(xpixels => 200, ypixels => 200, type => 'nonprop');
184
185           Returns a new scaled image on success.  The source image is not
186           modified.
187
188           Returns false on failure, check the errstr() method for the reason
189           for failure.
190
191           A mandatory warning is produced if scale() is called in void con‐
192           text.
193
194             # setup
195             my $image = Imager->new;
196             $image->read(file => 'somefile.jpg')
197               or die $image->errstr;
198
199             # all full quality unless indicated otherwise
200             # half the size:
201             my $half = $image->scale;
202
203             # double the size
204             my $double = $image->scale(scalefactor => 2.0);
205
206             # so a 400 x 400 box fits in the resulting image:
207             my $fit400x400inside = $image->scale(xpixels => 400, ypixels => 400);
208             my $fit400x400inside2 = $image->scale(xpixels => 400, ypixels => 400,
209                                                   type=>'max');
210
211             # fit inside a 400 x 400 box
212             my $inside400x400 = $image->scale(xpixels => 400, ypixels => 400,
213                                         type=>'min');
214
215             # make it 400 pixels wide or high
216             my $width400 = $image->scale(xpixels => 400);
217             my $height400 = $image->scale(ypixels => 400);
218
219             # low quality scales:
220             # to half size
221             my $low = $image->scale(qtype => 'preview');
222
223             # mixing method scale
224             my $mixed = $image->scale(qtype => 'mixing', scalefactor => 0.1);
225
226             # using an Image::Math::Constrain object
227             use Image::Math::Constrain;
228             my $constrain = Image::Math::Constrain->new(800, 600);
229             my $scaled = $image->scale(constrain => $constrain);
230
231             # same as Image::Math::Constrain version
232             my $scaled2 = $image->scale(xpixels => 800, ypixels => 600, type => 'min');
233
234       scaleX
235           scaleX() will scale along the X dimension, return a new image with
236           the new width:
237
238             my $newimg = $img->scaleX(pixels=>400); # 400x500
239             $newimg = $img->scaleX(scalefactor=>0.25) # 175x500
240
241           *   scalefactor - the amount to scale the X axis.  Ignored if "pix‐
242               els" is provided.  Default: 0.5.
243
244           *   pixels - the new width of the image.
245
246           Returns a new scaled image on success.  The source image is not
247           modified.
248
249           Returns false on failure, check the errstr() method for the reason
250           for failure.
251
252           A mandatory warning is produced if scaleX() is called in void con‐
253           text.
254
255       scaleY
256           scaleY() will scale along the Y dimension, return a new image with
257           the new height:
258
259             $newimg = $img->scaleY(pixels=>400); # 700x400
260             $newimg = $img->scaleY(scalefactor=>0.25) # 700x125
261
262           *   scalefactor - the amount to scale the Y axis.  Ignored if "pix‐
263               els" is provided.  Default: 0.5.
264
265           *   pixels - the new height of the image.
266
267           Returns a new scaled image on success.  The source image is not
268           modified.
269
270           Returns false on failure, check the errstr() method for the reason
271           for failure.
272
273           A mandatory warning is produced if scaleY() is called in void con‐
274           text.
275
276       scale_calculate
277           Performs the same calculations that the scale() method does to cal‐
278           culate the scaling factors from the parameters you pass.
279
280           scale_calculate() can be called as an object method, or as a class
281           method.
282
283           Takes the following parameters over scale():
284
285           *   width, height - the image width and height to base the scaling
286               on.  Required if scale_calculate() is called as a class method.
287               If called as an object method these default to the image width
288               and height respectively.
289
290           You might use scale_calculate() as a class method when generating
291           an IMG tag, for example.
292
293           Returns an empty list on failure.
294
295           Returns a list containing horizontal scale factor, vertical scale
296           factor, new width, new height, on success.
297
298             my ($x_scale, $y_scale, $new_width, $new_height) =
299                   Imager->scale_calculate(width => 1024, height => 768,
300                                           ypixels => 180, type => 'min');
301
302             my ($x_scale, $y_scale, $new_width, $new_height) =
303                   $img->scale_calculate(xpixels => 200, type => 'min');
304
305       crop
306           Another way to resize an image is to crop it.  The parameters to
307           crop are the edges of the area that you want in the returned image,
308           where the right and bottom edges are non-inclusive.  If a parameter
309           is omitted a default is used instead.
310
311           crop() returns the cropped image and does not modify the source
312           image.
313
314           The possible parameters are:
315
316           *   "left" - the left edge of the area to be cropped.  Default: 0
317
318           *   "top" - the top edge of the area to be cropped.  Default: 0
319
320           *   "right" - the right edge of the area to be cropped.  Default:
321               right edge of image.
322
323           *   "bottom" - the bottom edge of the area to be cropped.  Default:
324               bottom edge of image.
325
326           *   "width" - width of the crop area.  Ignored if both "left" and
327               "right" are supplied.  Centered on the image if neither "left"
328               nor "right" are supplied.
329
330           *   "height" - height of the crop area.  Ignored if both "top" and
331               "bottom" are supplied.  Centered on the image if neither "top"
332               nor "bottom" are supplied.
333
334           For example:
335
336             # these produce the same image
337             $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
338             $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
339             $newimg = $img->crop(right=>100, bottom=>100, width=>50, height=>90);
340
341             # and the following produce the same image
342             $newimg = $img->crop(left=>50, right=>100);
343             $newimg = $img->crop(left=>50, right=>100, top=>0,
344                                  bottom=>$img->getheight);
345
346             # grab the top left corner of the image
347             $newimg = $img->crop(right=>50, bottom=>50);
348
349           You can also specify width and height parameters which will produce
350           a new image cropped from the center of the input image, with the
351           given width and height.
352
353             $newimg = $img->crop(width=>50, height=>50);
354
355           If you supply "left", "width" and "right" values, the "right" value
356           will be ignored.  If you supply "top", "height" and "bottom" val‐
357           ues, the "bottom" value will be ignored.
358
359           The edges of the cropped area default to the edges of the source
360           image, for example:
361
362             # a vertical bar from the middle from top to bottom
363             $newimg = $img->crop(width=>50);
364
365             # the right half
366             $newimg = $img->crop(left=>$img->getwidth() / 2);
367
368           If the resulting image would have zero width or height then crop()
369           returns false and $img->errstr is an appropriate error message.
370
371           A mandatory warning is produced if crop() is called in void con‐
372           text.
373
374       rotate
375           Use the rotate() method to rotate an image.  This method will
376           return a new, rotated image.
377
378           To rotate by an exact amount in degrees or radians, use the
379           'degrees' or 'radians' parameter:
380
381             my $rot20 = $img->rotate(degrees=>20);
382             my $rotpi4 = $img->rotate(radians=>3.14159265/4);
383
384           Exact image rotation uses the same underlying transformation engine
385           as the matrix_transform() method (see Imager::Engines).
386
387           You can also supply a "back" argument which acts as a background
388           color for the areas of the image with no samples available (outside
389           the rectangle of the source image.)  This can be either an
390           Imager::Color or Imager::Color::Float object.  This is not mixed
391           transparent pixels in the middle of the source image, it is only
392           used for pixels where there is no corresponding pixel in the source
393           image.
394
395           To rotate in steps of 90 degrees, use the 'right' parameter:
396
397             my $rotated = $img->rotate(right=>270);
398
399           Rotations are clockwise for positive values.
400
401           Parameters:
402
403           *   right - rotate by an exact multiple of 90 degrees, specified in
404               degreess.
405
406           *   radians - rotate by an angle specified in radians.
407
408           *   degrees - rotate by an angle specified in degrees.
409
410           *   back - for "radians" and "degrees" this is the color used for
411               the areas not covered by the original image.  For example, the
412               corners of an image rotated by 45 degrees.
413
414               This can be either an Imager::Color object, an
415               Imager::Color::Float object or any parameter that Imager can
416               convert to a color object, see "Color Parameters" in
417               Imager::Draw for details.
418
419               This is not mixed transparent pixels in the middle of the
420               source image, it is only used for pixels where there is no cor‐
421               responding pixel in the source image.
422
423               Default: transparent black.
424
425             # rotate 45 degrees clockwise,
426             my $rotated = $img->rotate(degrees => 45);
427
428             # rotate 10 degrees counter-clockwise
429             # set pixels not sourced from the original to red
430             my $rotated = $img->rotate(degrees => -10, back => 'red');
431
432       Image pasting/flipping
433
434       A list of the transformations that alter the source image follows:
435
436       paste
437           To copy an image to onto another image use the "paste()" method.
438
439             $dest->paste(left=>40, top=>20, src=>$logo);
440
441           That copies the entire $logo image onto the $dest image so that the
442           upper left corner of the $logo image is at (40,20).
443
444           Parameters:
445
446           *   src, img - the source image.  src added for compatibility with
447               rubthrough().
448
449           *   left, top - position in output of the top left of the pasted
450               image.  Default: (0,0)
451
452           *   src_minx, src_miny - the top left corner in the source image to
453               start the paste from.  Default: (0, 0)
454
455           *   src_maxx, src_maxy - the bottom right in the source image of
456               the sub image to paste.  This position is non inclusive.
457               Default: bottom right corner of the source image.
458
459           *   width, height - if the corresponding src_maxx or src_maxy is
460               not defined then width or height is used for the width or
461               height of the sub image to be pasted.
462
463             # copy the 20x20 pixel image from (20,20) in $src_image to (10,10) in $img
464             $img->paste(src=>$src_image,
465                         left => 10, top => 10,
466                         src_minx => 20, src_miny => 20,
467                         src_maxx => 40, src_maxx => 40);
468
469           If the source image has an alpha channel and the target doesn't,
470           then the source is treated as if composed onto a black background.
471
472           If the source image is color and the target is grayscale, the the
473           source is treated as if run through " convert(preset="'gray') >.
474
475       rubthrough
476           A more complicated way of blending images is where one image is put
477           'over' the other with a certain amount of opaqueness.  The method
478           that does this is rubthrough.
479
480             $img->rubthrough(src=>$overlay,
481                              tx=>30,       ty=>50,
482                              src_minx=>20, src_miny=>30,
483                              src_maxx=>20, src_maxy=>30);
484
485           That will take the sub image defined by $overlay and
486           [src_minx,src_maxx)[src_miny,src_maxy) and overlay it on top of
487           $img with the upper left corner at (30,50).  You can rub 2 or 4
488           channel images onto a 3 channel image, or a 2 channel image onto a
489           1 channel image.  The last channel is used as an alpha channel.  To
490           add an alpha channel to an image see convert().
491
492           Parameters:
493
494           *   tx, ty - location in the the target image ($self) to render the
495               top left corner of the source.
496
497           *   src_minx, src_miny - the top left corner in the source to
498               transfer to the target image.  Default: (0, 0).
499
500           *   src_maxx, src_maxy - the bottom right in the source image of
501               the sub image to overlay.  This position is non inclusive.
502               Default: bottom right corner of the source image.
503
504             # overlay all of $source onto $targ
505             $targ->rubthrough(tx => 20, ty => 25, src => $source);
506
507             # overlay the top left corner of $source onto $targ
508             $targ->rubthrough(tx => 20, ty => 25, src => $source,
509                               src_maxx => 20, src_maxy => 20);
510
511             # overlay the bottom right corner of $source onto $targ
512             $targ->rubthrough(tx => 20, ty => 30, src => $src,
513                               src_minx => $src->getwidth() - 20,
514                               src_miny => $src->getheight() - 20);
515
516           rubthrough() returns true on success.  On failure check $tar‐
517           get->errstr for the reason for failure.
518
519       compose
520           Draws the source image over the target image, with the src alpha
521           channel modified by the optional mask and the opacity.
522
523             $img->compose(src=>$overlay,
524                           tx=>30,       ty=>50,
525                           src_minx=>20, src_miny=>30,
526                           src_maxx=>20, src_maxy=>30,
527                           mask => $mask, opacity => 0.5);
528
529           That will take the sub image defined by $overlay and
530           [src_minx,src_maxx)[src_miny,src_maxy) and overlay it on top of
531           $img with the upper left corner at (30,50).  You can rub 2 or 4
532           channel images onto a 3 channel image, or a 2 channel image onto a
533           1 channel image.
534
535           Parameters:
536
537           *   src - the source image to draw onto the target.  Required.
538
539           *   tx, ty - location in the the target image ($self) to render the
540               top left corner of the source.  These can also be supplied as
541               "left" and "right".  Default: (0, 0).
542
543           *   src_minx, src_miny - the top left corner in the source to
544               transfer to the target image.  Default: (0, 0).
545
546           *   src_maxx, src_maxy - the bottom right in the source image of
547               the sub image to overlay.  This position is non inclusive.
548               Default: bottom right corner of the source image.
549
550           *   mask - a mask image.  The first channel of this image is used
551               to modify the alpha channel of the source image.  This can me
552               used to mask out portions of the source image.  Where the first
553               channel is zero none of the source image will be used, where
554               the first channel is max the full alpha of the source image
555               will be used, as further modified by the opacity.
556
557           *   opacity - further modifies the alpha channel of the source
558               image, in the range 0.0 to 1.0.  Default: 1.0.
559
560           *   combine - the method to combine the source pixels with the tar‐
561               get.  See the combine option documentation in Imager::Fill.
562               Default: normal.
563
564           Calling compose() with no mask, combine set to "normal", opacity
565           set to 1.0 is equivalent to calling rubthrough().
566
567           compose() is intended to be produce similar effects to layers in
568           interactive paint software.
569
570             # overlay all of $source onto $targ
571             $targ->compose(tx => 20, ty => 25, src => $source);
572
573             # overlay the top left corner of $source onto $targ
574             $targ->compose(tx => 20, ty => 25, src => $source,
575                               src_maxx => 20, src_maxy => 20);
576
577             # overlay the bottom right corner of $source onto $targ
578             $targ->compose(tx => 20, ty => 30, src => $src,
579                               src_minx => $src->getwidth() - 20,
580                               src_miny => $src->getheight() - 20);
581
582           compose() returns true on success.  On failure check $tar‐
583           get->errstr for the reason for failure.
584
585       flip
586           An inplace horizontal or vertical flip is possible by calling the
587           "flip()" method.  If the original is to be preserved it's possible
588           to make a copy first.  The only parameter it takes is the "dir"
589           parameter which can take the values "h", "v", "vh" and "hv".
590
591             $img->flip(dir=>"h");       # horizontal flip
592             $img->flip(dir=>"vh");      # vertical and horizontal flip
593             $nimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
594
595           flip() returns true on success.  On failure check $img->errstr for
596           the reason for failure.
597
598       Color transformations
599
600       convert
601           You can use the convert method to transform the color space of an
602           image using a matrix.  For ease of use some presets are provided.
603
604           The convert method can be used to:
605
606           *   convert an RGB or RGBA image to grayscale.
607
608           *   convert a grayscale image to RGB.
609
610           *   extract a single channel from an image.
611
612           *   set a given channel to a particular value (or from another
613               channel)
614
615           The currently defined presets are:
616
617           gray
618           grey
619               converts an RGBA image into a grayscale image with alpha chan‐
620               nel, or an RGB image into a grayscale image without an alpha
621               channel.
622
623               This weights the RGB channels at 22.2%, 70.7% and 7.1% respec‐
624               tively.
625
626           noalpha
627               removes the alpha channel from a 2 or 4 channel image.  An
628               identity for other images.
629
630           red
631           channel0
632               extracts the first channel of the image into a single channel
633               image
634
635           green
636           channel1
637               extracts the second channel of the image into a single channel
638               image
639
640           blue
641           channel2
642               extracts the third channel of the image into a single channel
643               image
644
645           alpha
646               extracts the alpha channel of the image into a single channel
647               image.
648
649               If the image has 1 or 3 channels (assumed to be grayscale of
650               RGB) then the resulting image will be all white.
651
652           rgb converts a grayscale image to RGB, preserving the alpha channel
653               if any
654
655           addalpha
656               adds an alpha channel to a grayscale or RGB image.  Preserves
657               an existing alpha channel for a 2 or 4 channel image.
658
659           For example, to convert an RGB image into a greyscale image:
660
661             $new = $img->convert(preset=>'grey'); # or gray
662
663           or to convert a grayscale image to an RGB image:
664
665             $new = $img->convert(preset=>'rgb');
666
667           The presets aren't necessary simple constants in the code, some are
668           generated based on the number of channels in the input image.
669
670           If you want to perform some other colour transformation, you can
671           use the 'matrix' parameter.
672
673           For each output pixel the following matrix multiplication is done:
674
675             ⎪ channel[0] ⎪   ⎪ $c00, ...,  $c0k ⎪   ⎪ inchannel[0] ⎪
676             ⎪    ...     ⎪ = ⎪       ...        ⎪ x ⎪     ...      ⎪
677             ⎪ channel[k] ⎪   ⎪ $ck0, ...,  $ckk ⎪   ⎪ inchannel[k] ⎪
678                                                                     1
679           Where C<k = $img-E<gt>getchannels()-1>.
680
681           So if you want to swap the red and green channels on a 3 channel
682           image:
683
684             $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
685                                            [ 1, 0, 0 ],
686                                            [ 0, 0, 1 ] ]);
687
688           or to convert a 3 channel image to greyscale using equal weight‐
689           ings:
690
691             $new = $img->convert(matrix=>[ [ 0.333, 0.333, 0.334 ] ])
692
693           Convert a 2 channel image (grayscale with alpha) to an RGBA image
694           with the grey converted to the specified RGB color:
695
696             # set (RGB) scaled on the grey scale portion and copy the alpha
697             # channel as is
698             my $colored = $gray->convert(matrix=>[ [ ($red/255),   0 ],
699                                                    [ ($green/255), 0 ],
700                                                    [ ($blue/255),  0 ],
701                                                    [ 0,            1 ],
702                                                  ]);
703
704           To convert a 3 channel image to a 4 channel image with a 50 percent
705           alpha channel:
706
707             my $withalpha = $rgb->convert(matrix =>[ [ 1, 0, 0, 0 ],
708                                                      [ 0, 1, 0, 0 ],
709                                                      [ 0, 0, 1, 0 ],
710                                                      [ 0, 0, 0, 0.5 ],
711                                                    ]);
712
713       Color Mappings
714
715       map You can use the map method to map the values of each channel of an
716           image independently using a list of lookup tables.  It's important
717           to realize that the modification is made inplace.  The function
718           simply returns the input image again or undef on failure.
719
720           Each channel is mapped independently through a lookup table with
721           256 entries.  The elements in the table should not be less than 0
722           and not greater than 255.  If they are out of the 0..255 range they
723           are clamped to the range.  If a table does not contain 256 entries
724           it is silently ignored.
725
726           Single channels can mapped by specifying their name and the mapping
727           table.  The channel names are "red", "green", "blue", "alpha".
728
729             @map = map { int( $_/2 } 0..255;
730             $img->map( red=>\@map );
731
732           It is also possible to specify a single map that is applied to all
733           channels, alpha channel included.  For example this applies a gamma
734           correction with a gamma of 1.4 to the input image.
735
736             $gamma = 1.4;
737             @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
738             $img->map(all=> \@map);
739
740           The "all" map is used as a default channel, if no other map is
741           specified for a channel then the "all" map is used instead.  If we
742           had not wanted to apply gamma to the alpha channel we would have
743           used:
744
745             $img->map(all=> \@map, alpha=>[]);
746
747           Since "[]" contains fewer than 256 element the gamma channel is
748           unaffected.
749
750           It is also possible to simply specify an array of maps that are
751           applied to the images in the rgba order.  For example to apply maps
752           to the "red" and "blue" channels one would use:
753
754             $img->map(maps=>[\@redmap, [], \@bluemap]);
755

SEE ALSO

757       Imager, Imager::Engines
758

AUTHOR

760       Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson
761

REVISION

763       $Revision: 1431 $
764
765
766
767perl v5.8.8                       2008-03-28        Imager::Transformations(3)
Impressum