Debian Codegraph @

lib/Imager/Transformations.pod

raw
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, qtype => 'mixing');
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->scaleX(pixels=>400);
17 $newimg = $img->scaleX(scalefactor=>0.25);
18 $newimg = $img->scaleY(pixels=>400);
19 $newimg = $img->scaleY(scalefactor=>0.25);
20
21 $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
22 $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
23
24 $dest->paste(left=>40,top=>20,img=>$logo);
25
26 $img->rubthrough(src=>$srcimage,tx=>30, ty=>50);
27 $img->rubthrough(src=>$srcimage,tx=>30, ty=>50,
28 src_minx=>20, src_miny=>30,
29 src_maxx=>20, src_maxy=>30);
30
31 $img->compose(src => $src, tx => 30, ty => 20, combine => 'color');
32 $img->compose(src => $src, tx => 30, ty => 20, combine => 'color');
33 mask => $mask, opacity => 0.5);
34
35 $img->flip(dir=>"h"); # horizontal flip
36 $img->flip(dir=>"vh"); # vertical and horizontal flip
37 $newimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
38
39 my $rot20 = $img->rotate(degrees=>20);
40 my $rotpi4 = $img->rotate(radians=>3.14159265/4);
41
42
43 # Convert image to gray
44 $new = $img->convert(preset=>'grey');
45
46 # Swap red/green channel
47 $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
48 [ 1, 0, 0 ],
49 [ 0, 0, 1 ] ]);
50
51 # build an image using channels from multiple input images
52 $new = $img->combine(src => [ $im1, $im2, $im3 ]);
53 $new = $img->combine(src => [ $im1, $im2, $im3 ],
54 channels => [ 2, 1, 0 ]);
55
56 # limit the range of red channel from 0..255 to 0..127
57 @map = map { int( $_/2 } 0..255;
58 $img->map( red=>\@map );
59
60 # Apply a Gamma of 1.4
61 my $gamma = 1.4;
62 my @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
63 $img->map(all=>\@map); # inplace conversion
64
65 =head1 DESCRIPTION
66
67 The methods described in Imager::Transformations fall into two categories.
68 Either they take an existing image and modify it in place, or they
69 return a modified copy.
70
71 Functions that modify inplace are C<flip()>, C<paste()>,
72 C<rubthrough()> and C<compose()>. If the original is to be left
73 intact it's possible to make a copy and alter the copy:
74
75 $flipped = $img->copy()->flip(dir=>'h');
76
77 =head2 Image copying/resizing/cropping/rotating
78
79 A list of the transformations that do not alter the source image follows:
80
81 =over
82
83 =item copy()
84
85 To create a copy of an image use the C<copy()> method. This is useful
86 if you want to keep an original after doing something that changes the image.
87
88 $newimg = $orig->copy();
89
90 =item scale()
91
92 X<scale> To scale an image so proportions are maintained use the
93 C<$img-E<gt>scale()> method. if you give either a C<xpixels> or
94 C<ypixels> parameter they will determine the width or height
95 respectively. If both are given the one resulting in a larger image
96 is used, unless you set the C<type> parameter to C<'min'>. example:
97 C<$img> is 700 pixels wide and 500 pixels tall.
98
99 $newimg = $img->scale(xpixels=>400); # 400x285
100 $newimg = $img->scale(ypixels=>400); # 560x400
101
102 $newimg = $img->scale(xpixels=>400,ypixels=>400); # 560x400
103 $newimg = $img->scale(xpixels=>400,ypixels=>400,type=>'min'); # 400x285
104
105 $newimg = $img->scale(xpixels=>400, ypixels=>400),type=>'nonprop'); # 400x400
106
107 $newimg = $img->scale(scalefactor=>0.25); 175x125
108 $newimg = $img->scale(); # 350x250
109
110 If you want to create low quality previews of images you can pass
111 C<qtype=E<gt>'preview'> to scale and it will use nearest neighbor
112 sampling instead of filtering. It is much faster but also generates
113 worse looking images - especially if the original has a lot of sharp
114 variations and the scaled image is by more than 3-5 times smaller than
115 the original.
116
117 =over
118
119 =item *
120
121 C<xpixels>, C<ypixels> - desired size of the scaled image. The
122 C<type> parameter controls whether the larger or smaller of the two
123 possible sizes is chosen, or if the image is scaled
124 non-proportionally.
125
126 =item *
127
128 C<constrain> - an Image::Math::Constrain object defining the way in
129 which the image size should be constrained.
130
131 =item *
132
133 C<scalefactor> - if none of C<xpixels>, C<ypixels>, C<xscalefactor>,
134 C<yscalefactor> or C<constrain> is supplied then this is used as the
135 ratio to scale by. Default: 0.5.
136
137 =item *
138
139 C<xscalefactor>, C<yscalefactor> - if both are supplied then the image is
140 scaled as per these parameters, whether this is proportionally or not.
141 New in Imager 0.54.
142
143 =item *
144
145 C<type> - when both C<xpixels> and C<ypixels> are supplied and do not
146 scale the image proportionally C<type> controls whether the larger or
147 smaller of the two possible sizes is chosen, or whether the resulting
148 image is stretched.
149
150 Possible values are:
151
152 =over
153
154 =item *
155
156 C<min> - the smaller of the two possible sizes is chosen.
157
158 =item *
159
160 C<max> - the larger of the two possible sizes is chosen. This is the
161 default.
162
163 =item *
164
165 C<nonprop> - allow non-proportional scaling.
166
167 =back
168
169 scale() will fail if C<type> is set to some other value.
170
171 For example, if the original image is 400 pixels wide by 200 pixels
172 high and C<xpixels> is set to 300, and C<ypixels> is set to 160:
173
174 # $image is 400x200
175 my $scaled = $image->scale(xpixels => 300, ypixels => 160, type => $type);
176
177 then scaling to 300x160 would distort the message, making it look
178 stretched vertically.
179
180 Scaling to 300 x 150, based on C<xpixels> or 320 x 160 based on
181 C<ypixels> prevents any stretching, preserving a ratio of two to one
182 for width to height.
183
184 When C<type> is C<min> the result is the smaller 300 x 150 image, when
185 C<type> is C<max> the result is the larger 320 x 160 image.
186 C<nonprop> doesn't attempt to scale proportionally at all, producing a
187 300 x 160 image.
188
189 =item *
190
191 C<qtype> - defines the quality of scaling performed. Possible values are:
192
193 =over
194
195 =item *
196
197 C<normal> - high quality scaling. This is the default.
198
199 =item *
200
201 C<preview> - lower quality. When scaling down this will skip input
202 pixels, eg. scaling by 0.5 will skip every other pixel. When scaling
203 up this will duplicate pixels.
204
205 =item *
206
207 C<mixing> - implements the mixing algorithm implemented by
208 F<pnmscale>. This retains more detail when scaling down than
209 C<normal>. When scaling down this proportionally accumulates sample
210 data from the pixels, resulting in a proportional mix of all of the
211 pixels. When scaling up this will mix pixels when the sampling grid
212 crosses a pixel boundary but will otherwise copy pixel values.
213
214 =back
215
216 scale() will fail if C<qtype> is set to some other value.
217
218 C<preview> is faster than C<mixing> which is much faster than C<normal>.
219
220 =back
221
222 To scale an image on a given axis without maintaining proportions, it
223 is best to call the scaleX() and scaleY() methods with the required
224 dimensions. eg.
225
226 my $scaled = $img->scaleX(pixels=>400)->scaleY(pixels=>200);
227
228 From Imager 0.54 you can scale without maintaining proportions either
229 by supplying both the C<xscalefactor> and C<yscalefactor> arguments:
230
231 my $scaled = $img->scale(xscalefactor => 0.5, yscalefactor => 0.67);
232
233 or by supplying C<xpixels> and C<ypixels> and setting C<type> to
234 <nonprop>:
235
236 my $scaled = $im->scale(xpixels => 200, ypixels => 200, type => 'nonprop');
237
238 Returns a new scaled image on success. The source image is not
239 modified.
240
241 Returns false on failure, check the errstr() method for the reason for
242 failure.
243
244 A mandatory warning is produced if scale() is called in void context.
245
246 # setup
247 my $image = Imager->new;
248 $image->read(file => 'somefile.jpg')
249 or die $image->errstr;
250
251 # all full quality unless indicated otherwise
252 # half the size:
253 my $half = $image->scale;
254
255 # double the size
256 my $double = $image->scale(scalefactor => 2.0);
257
258 # so a 400 x 400 box fits in the resulting image:
259 my $fit400x400inside = $image->scale(xpixels => 400, ypixels => 400);
260 my $fit400x400inside2 = $image->scale(xpixels => 400, ypixels => 400,
261 type=>'max');
262
263 # fit inside a 400 x 400 box
264 my $inside400x400 = $image->scale(xpixels => 400, ypixels => 400,
265 type=>'min');
266
267 # make it 400 pixels wide or high
268 my $width400 = $image->scale(xpixels => 400);
269 my $height400 = $image->scale(ypixels => 400);
270
271 # low quality scales:
272 # to half size
273 my $low = $image->scale(qtype => 'preview');
274
275 # mixing method scale
276 my $mixed = $image->scale(qtype => 'mixing', scalefactor => 0.1);
277
278 # using an Image::Math::Constrain object
279 use Image::Math::Constrain;
280 my $constrain = Image::Math::Constrain->new(800, 600);
281 my $scaled = $image->scale(constrain => $constrain);
282
283 # same as Image::Math::Constrain version
284 my $scaled2 = $image->scale(xpixels => 800, ypixels => 600, type => 'min');
285
286 =item scaleX()
287
288 scaleX() will scale along the X dimension, return a new image with the
289 new width:
290
291 my $newimg = $img->scaleX(pixels=>400); # 400x500
292 $newimg = $img->scaleX(scalefactor=>0.25) # 175x500
293
294 =over
295
296 =item *
297
298 C<scalefactor> - the amount to scale the X axis. Ignored if C<pixels> is
299 provided. Default: 0.5.
300
301 =item *
302
303 C<pixels> - the new width of the image.
304
305 =back
306
307 Returns a new scaled image on success. The source image is not
308 modified.
309
310 Returns false on failure, check the errstr() method for the reason for
311 failure.
312
313 A mandatory warning is produced if scaleX() is called in void context.
314
315 =item scaleY()
316
317 scaleY() will scale along the Y dimension, return a new image with the
318 new height:
319
320 $newimg = $img->scaleY(pixels=>400); # 700x400
321 $newimg = $img->scaleY(scalefactor=>0.25) # 700x125
322
323 =over
324
325 =item *
326
327 C<scalefactor> - the amount to scale the Y axis. Ignored if C<pixels> is
328 provided. Default: 0.5.
329
330 =item *
331
332 C<pixels> - the new height of the image.
333
334 =back
335
336 Returns a new scaled image on success. The source image is not
337 modified.
338
339 Returns false on failure, check the errstr() method for the reason for
340 failure.
341
342 A mandatory warning is produced if scaleY() is called in void context.
343
344 =item scale_calculate()
345
346 Performs the same calculations that the scale() method does to
347 calculate the scaling factors from the parameters you pass.
348
349 scale_calculate() can be called as an object method, or as a class
350 method.
351
352 Takes the following parameters over scale():
353
354 =over
355
356 =item *
357
358 C<width>, C<height> - the image width and height to base the scaling on.
359 Required if scale_calculate() is called as a class method. If called
360 as an object method these default to the image width and height
361 respectively.
362
363 =back
364
365 You might use scale_calculate() as a class method when generating an
366 HTML C<IMG> tag, for example.
367
368 Returns an empty list on failure.
369
370 Returns a list containing horizontal scale factor, vertical scale
371 factor, new width, new height, on success.
372
373 my ($x_scale, $y_scale, $new_width, $new_height) =
374 Imager->scale_calculate(width => 1024, height => 768,
375 ypixels => 180, type => 'min');
376
377 my ($x_scale, $y_scale, $new_width, $new_height) =
378 $img->scale_calculate(xpixels => 200, type => 'min');
379
380 =item crop()
381
382 =for stopwords resize
383
384 Another way to resize an image is to crop it. The parameters to
385 crop are the edges of the area that you want in the returned image,
386 where the right and bottom edges are non-inclusive. If a parameter is
387 omitted a default is used instead.
388
389 crop() returns the cropped image and does not modify the source image.
390
391 The possible parameters are:
392
393 =over
394
395 =item *
396
397 C<left> - the left edge of the area to be cropped. Default: 0
398
399 =item *
400
401 C<top> - the top edge of the area to be cropped. Default: 0
402
403 =item *
404
405 C<right> - the right edge of the area to be cropped. Default: right
406 edge of image.
407
408 =item *
409
410 C<bottom> - the bottom edge of the area to be cropped. Default:
411 bottom edge of image.
412
413 =item *
414
415 C<width> - width of the crop area. Ignored if both C<left> and C<right> are
416 supplied. Centered on the image if neither C<left> nor C<right> are
417 supplied.
418
419 =item *
420
421 C<height> - height of the crop area. Ignored if both C<top> and
422 C<bottom> are supplied. Centered on the image if neither C<top> nor
423 C<bottom> are supplied.
424
425 =back
426
427 For example:
428
429 # these produce the same image
430 $newimg = $img->crop(left=>50, right=>100, top=>10, bottom=>100);
431 $newimg = $img->crop(left=>50, top=>10, width=>50, height=>90);
432 $newimg = $img->crop(right=>100, bottom=>100, width=>50, height=>90);
433
434 # and the following produce the same image
435 $newimg = $img->crop(left=>50, right=>100);
436 $newimg = $img->crop(left=>50, right=>100, top=>0,
437 bottom=>$img->getheight);
438
439 # grab the top left corner of the image
440 $newimg = $img->crop(right=>50, bottom=>50);
441
442 You can also specify width and height parameters which will produce a
443 new image cropped from the center of the input image, with the given
444 width and height.
445
446 $newimg = $img->crop(width=>50, height=>50);
447
448 If you supply C<left>, C<width> and C<right> values, the C<right>
449 value will be ignored. If you supply C<top>, C<height> and C<bottom>
450 values, the C<bottom> value will be ignored.
451
452 The edges of the cropped area default to the edges of the source
453 image, for example:
454
455 # a vertical bar from the middle from top to bottom
456 $newimg = $img->crop(width=>50);
457
458 # the right half
459 $newimg = $img->crop(left=>$img->getwidth() / 2);
460
461 If the resulting image would have zero width or height then crop()
462 returns false and $img->errstr is an appropriate error message.
463
464 A mandatory warning is produced if crop() is called in void context.
465
466 =item rotate()
467
468 Use the rotate() method to rotate an image. This method will return a
469 new, rotated image.
470
471 To rotate by an exact amount in degrees or radians, use the 'degrees'
472 or 'radians' parameter:
473
474 my $rot20 = $img->rotate(degrees=>20);
475 my $rotpi4 = $img->rotate(radians=>3.14159265/4);
476
477 Exact image rotation uses the same underlying transformation engine as
478 the matrix_transform() method (see Imager::Engines).
479
480 You can also supply a C<back> argument which acts as a background
481 color for the areas of the image with no samples available (outside
482 the rectangle of the source image.) This can be either an
483 Imager::Color or Imager::Color::Float object. This is B<not> mixed
484 transparent pixels in the middle of the source image, it is B<only>
485 used for pixels where there is no corresponding pixel in the source
486 image.
487
488 To rotate in steps of 90 degrees, use the 'right' parameter:
489
490 my $rotated = $img->rotate(right=>270);
491
492 Rotations are clockwise for positive values.
493
494 Parameters:
495
496 =over
497
498 =item *
499
500 C<right> - rotate by an exact multiple of 90 degrees, specified in
501 degrees.
502
503 =item *
504
505 C<radians> - rotate by an angle specified in radians.
506
507 =item *
508
509 C<degrees> - rotate by an angle specified in degrees.
510
511 =item *
512
513 C<back> - for C<radians> and C<degrees> this is the color used for the
514 areas not covered by the original image. For example, the corners of
515 an image rotated by 45 degrees.
516
517 This can be either an Imager::Color object, an Imager::Color::Float
518 object or any parameter that Imager can convert to a color object, see
519 L<Imager::Draw/Color Parameters> for details.
520
521 This is B<not> mixed transparent pixels in the middle of the source
522 image, it is B<only> used for pixels where there is no corresponding
523 pixel in the source image.
524
525 Default: transparent black.
526
527 =back
528
529 # rotate 45 degrees clockwise,
530 my $rotated = $img->rotate(degrees => 45);
531
532 # rotate 10 degrees counter-clockwise
533 # set pixels not sourced from the original to red
534 my $rotated = $img->rotate(degrees => -10, back => 'red');
535
536 =item trim()
537
538 Returns a cropped version of the original image cropped of border
539 pixels based on either transparent pixels, or pixels that match any of
540 a set of provided colors.
541
542 If you just want the number of pixels to be cropped on each side you
543 can use the L</trim_rect()> method.
544
545 If the supplied parameters would result in the entire image is
546 cropped, then a single pixel image is returned from the top left of
547 the source image.
548
549 To trim just on fully transparent pixels:
550
551 my $trimmed = $img->trim();
552
553 To trim pixels with less than 1% coverage:
554
555 my $trimmed = $img->trim(alpha => 0.01);
556
557 To trim based on automatically determined colors and fully transparent
558 pixels:
559
560 my $trimmed = $img->trim(auto => 1);
561
562 To trim a black border:
563
564 my $trimmed = $img->trim(colors => [ "#000" ]);
565
566 Parameters:
567
568 =over
569
570 =item *
571
572 C<alpha> - any pixels with less than or equal coverage to this value
573 will be treated as part of the border to be cropped. This is always
574 expressed as a fraction, so C<alpha => 1.0> will treat all pixels as
575 part of the border. Default: 0 (fully transparent pixels are part of
576 the border).
577
578 =item *
579
580 C<auto> - automatically select colors to crop on. Possible values:
581
582 =over
583
584 =item *
585
586 C<1> - a "best" mechanism is selected, this is currently the C<center>
587 method, but it subject to change.
588
589 =item *
590
591 C<center>, C<centre> - the pixels at the center of each side of the
592 image are used.
593
594 =back
595
596 =item *
597
598 C<tolerance> - used by the C<auto> mechanism to control the range of
599 pixel colors to be accepted as part of the border. Default: 0.01.
600
601 =item *
602
603 C<colors> - either a L<Imager::TrimColorList> object or a reference to
604 an array of color entries to be considered part of the border. Code like:
605
606 my $trimmed = $img->trim(colors => [ LIST ]);
607
608 results in a temporary Imager::TrimColorList being produced like:
609
610 my $trimmed = $img->trim(colors => Imager::TrimColorList->new( LIST ));
611
612 =back
613
614 C<colors> and C<auto> exclude each other, only one or the other can be
615 supplied.
616
617 =item trim_rect()
618
619 Returns a list of the number of columns and rows of that would be
620 removed from each side if trim() was called.
621
622 my ($left, $top, $right, $bottom) = $img->trim_rect();
623
624 If the entire image matches the trim parameters then C<$left> will be
625 the width of the image and C<$top> will be the height of the image.
626
627 Accepts the same parameters as L</trim()>.
628
629 =back
630
631 =head2 Image pasting/flipping
632
633 A list of the transformations that alter the source image follows:
634
635 =over
636
637 =item paste()
638
639 X<paste> To copy an image to onto another image use the C<paste()>
640 method.
641
642 $dest->paste(left=>40, top=>20, src=>$logo);
643
644 That copies the entire C<$logo> image onto the C<$dest> image so that the
645 upper left corner of the C<$logo> image is at (40,20).
646
647 Parameters:
648
649 =over
650
651 =item *
652
653 C<src>, C<img> - the source image. C<src> added for compatibility with
654 rubthrough().
655
656 =item *
657
658 C<left>, C<top> - position in output of the top left of the pasted image.
659 Default: (0,0)
660
661 =item *
662
663 C<src_minx>, C<src_miny> - the top left corner in the source image to start
664 the paste from. Default: (0, 0)
665
666 =item *
667
668 C<src_maxx>, C<src_maxy> - the bottom right in the source image of the sub
669 image to paste. This position is B<non> inclusive. Default: bottom
670 right corner of the source image.
671
672 =item *
673
674 C<width>, C<height> - if the corresponding src_maxx or src_maxy is not
675 defined then width or height is used for the width or height of the
676 sub image to be pasted.
677
678 =back
679
680 # copy the 20x20 pixel image from (20,20) in $src_image to (10,10) in $img
681 $img->paste(src=>$src_image,
682 left => 10, top => 10,
683 src_minx => 20, src_miny => 20,
684 src_maxx => 40, src_maxx => 40);
685
686 If the source image has an alpha channel and the target doesn't, then
687 the source is treated as if composed onto a black background.
688
689 If the source image is color and the target is gray scale, the
690 source is treated as if run through C<< convert(preset=>'gray') >>.
691
692 =item rubthrough()
693
694 A more complicated way of blending images is where one image is
695 put 'over' the other with a certain amount of opaqueness. The
696 method that does this is rubthrough().
697
698 $img->rubthrough(src=>$overlay,
699 tx=>30, ty=>50,
700 src_minx=>20, src_miny=>30,
701 src_maxx=>20, src_maxy=>30);
702
703 That will take the sub image defined by I<$overlay> and
704 I<[src_minx,src_maxx)[src_miny,src_maxy)> and overlay it on top of
705 I<$img> with the upper left corner at (30,50). You can rub 2 or 4
706 channel images onto a 3 channel image, or a 2 channel image onto a 1
707 channel image. The last channel is used as an alpha channel. To add
708 an alpha channel to an image see I<convert()>.
709
710 Parameters:
711
712 =over
713
714 =item *
715
716 C<tx>, C<ty> - location in the target image ($self) to render the
717 top left corner of the source.
718
719 =item *
720
721 C<src_minx>, C<src_miny> - the top left corner in the source to transfer to
722 the target image. Default: (0, 0).
723
724 =item *
725
726 C<src_maxx>, C<src_maxy> - the bottom right in the source image of the sub
727 image to overlay. This position is B<non> inclusive. Default: bottom
728 right corner of the source image.
729
730 =back
731
732 # overlay all of $source onto $targ
733 $targ->rubthrough(tx => 20, ty => 25, src => $source);
734
735 # overlay the top left corner of $source onto $targ
736 $targ->rubthrough(tx => 20, ty => 25, src => $source,
737 src_maxx => 20, src_maxy => 20);
738
739 # overlay the bottom right corner of $source onto $targ
740 $targ->rubthrough(tx => 20, ty => 30, src => $src,
741 src_minx => $src->getwidth() - 20,
742 src_miny => $src->getheight() - 20);
743
744 rubthrough() returns true on success. On failure check
745 C<< $target->errstr >> for the reason for failure.
746
747 =item compose()
748
749 Draws the source image over the target image, with the source alpha
750 channel modified by the optional mask and the opacity.
751
752 $img->compose(src=>$overlay,
753 tx=>30, ty=>50,
754 src_minx=>20, src_miny=>30,
755 src_maxx=>20, src_maxy=>30,
756 mask => $mask, opacity => 0.5);
757
758 That will take the sub image defined by I<$overlay> and
759 I<[src_minx,src_maxx)[src_miny,src_maxy)> and overlay it on top of
760 I<$img> with the upper left corner at (30,50). You can rub 2 or 4
761 channel images onto a 3 channel image, or a 2 channel image onto a 1
762 channel image.
763
764 Parameters:
765
766 =over
767
768 =item *
769
770 C<src> - the source image to draw onto the target. Required.
771
772 =item *
773
774 C<tx>, C<ty> - location in the target image ($self) to render the top
775 left corner of the source. These can also be supplied as C<left> and
776 C<right>. Default: (0, 0).
777
778 =item *
779
780 C<src_minx>, C<src_miny> - the top left corner in the source to transfer to
781 the target image. Default: (0, 0).
782
783 =item *
784
785 C<src_maxx>, C<src_maxy> - the bottom right in the source image of the sub
786 image to overlay. This position is B<non> inclusive. Default: bottom
787 right corner of the source image.
788
789 =item *
790
791 C<mask> - a mask image. The first channel of this image is used to
792 modify the alpha channel of the source image. This can be used to
793 mask out portions of the source image. Where the first channel is
794 zero none of the source image will be used, where the first channel is
795 maximum the full alpha of the source image will be used, as further
796 modified by the opacity.
797
798 =item *
799
800 opacity - further modifies the alpha channel of the source image, in
801 the range 0.0 to 1.0. Default: 1.0.
802
803 =item *
804
805 combine - the method to combine the source pixels with the target.
806 See the combine option documentation in Imager::Fill. Default:
807 normal.
808
809 =back
810
811 Calling compose() with no mask, combine set to C<normal>, opacity set
812 to C<1.0> is equivalent to calling rubthrough().
813
814 compose() is intended to be produce similar effects to layers in
815 interactive paint software.
816
817 # overlay all of $source onto $targ
818 $targ->compose(tx => 20, ty => 25, src => $source);
819
820 # overlay the top left corner of $source onto $targ
821 $targ->compose(tx => 20, ty => 25, src => $source,
822 src_maxx => 20, src_maxy => 20);
823
824 # overlay the bottom right corner of $source onto $targ
825 $targ->compose(tx => 20, ty => 30, src => $src,
826 src_minx => $src->getwidth() - 20,
827 src_miny => $src->getheight() - 20);
828
829 compose() returns true on success. On failure check $target->errstr
830 for the reason for failure.
831
832 =item flip()
833
834 An inplace horizontal or vertical flip is possible by calling the
835 C<flip()> method. If the original is to be preserved it's possible to
836 make a copy first. The only parameter it takes is the C<dir>
837 parameter which can take the values C<h>, C<v>, C<vh> and C<hv>.
838
839 $img->flip(dir=>"h"); # horizontal flip
840 $img->flip(dir=>"vh"); # vertical and horizontal flip
841 $nimg = $img->copy->flip(dir=>"v"); # make a copy and flip it vertically
842
843 flip() returns true on success. On failure check $img->errstr for the
844 reason for failure.
845
846 =back
847
848 =head2 Color transformations
849
850 =over
851
852 =item convert()
853
854 You can use the convert method to transform the color space of an
855 image using a matrix. For ease of use some presets are provided.
856
857 The convert method can be used to:
858
859 =over
860
861 =item *
862
863 convert an RGB or RGBA image to gray scale.
864
865 =item *
866
867 convert a gray scale image to RGB.
868
869 =item *
870
871 extract a single channel from an image.
872
873 =item *
874
875 set a given channel to a particular value (or from another channel)
876
877 =back
878
879 The currently defined presets are:
880
881 =over
882
883 =item *
884
885 C<gray>, C<grey> - converts an RGBA image into a gray scale image with
886 alpha channel, or an RGB image into a gray scale image without an
887 alpha channel.
888
889 This weights the RGB channels at 22.2%, 70.7% and 7.1% respectively.
890
891 =item *
892
893 C<noalpha> - removes the alpha channel from a 2 or 4 channel image.
894 An identity for other images. Warning: this removes the alpha channel
895 without applying it.
896
897 =item *
898
899 C<red>, C<channel0> - extracts the first channel of the image into a
900 single channel image
901
902 =item *
903
904 C<green>, C<channel1> - extracts the second channel of the image into
905 a single channel image
906
907 =item *
908
909 C<blue>, C<channel2> - extracts the third channel of the image into a
910 single channel image
911
912 =item *
913
914 C<alpha> - extracts the alpha channel of the image into a single
915 channel image.
916
917 If the image has 1 or 3 channels (assumed to be gray scale or RGB) then
918 the resulting image will be all white.
919
920 =item *
921
922 C<rgb>
923
924 converts a gray scale image to RGB, preserving the alpha channel if any
925
926 =item *
927
928 C<addalpha> - adds an alpha channel to a gray scale or RGB image.
929 Preserves an existing alpha channel for a 2 or 4 channel image.
930
931 =back
932
933 For example, to convert an RGB image into a gray scale image:
934
935 $new = $img->convert(preset=>'grey'); # or gray
936
937 or to convert a gray scale image to an RGB image:
938
939 $new = $img->convert(preset=>'rgb');
940
941 The presets aren't necessary simple constants in the code, some are
942 generated based on the number of channels in the input image.
943
944 If you want to perform some other color transformation, you can use
945 the 'matrix' parameter.
946
947 For each output pixel the following matrix multiplication is done:
948
949 | channel[0] | | $c00, ..., $c0k | | inchannel[0] |
950 | ... | = | ... | x | ... |
951 | channel[k] | | $ck0, ..., $ckk | | inchannel[k] |
952 1
953 Where C<k = $img-E<gt>getchannels()-1>.
954
955 So if you want to swap the red and green channels on a 3 channel image:
956
957 $new = $img->convert(matrix=>[ [ 0, 1, 0 ],
958 [ 1, 0, 0 ],
959 [ 0, 0, 1 ] ]);
960
961 or to convert a 3 channel image to gray scale using equal weightings:
962
963 $new = $img->convert(matrix=>[ [ 0.333, 0.333, 0.334 ] ])
964
965 Convert a 2 channel image (gray scale with alpha) to an RGBA image with
966 the gray converted to the specified RGB color:
967
968 # set (RGB) scaled on the grey scale portion and copy the alpha
969 # channel as is
970 my $colored = $gray->convert(matrix=>[ [ ($red/255), 0 ],
971 [ ($green/255), 0 ],
972 [ ($blue/255), 0 ],
973 [ 0, 1 ],
974 ]);
975
976 To convert a 3 channel image to a 4 channel image with a 50 percent
977 alpha channel:
978
979 my $withalpha = $rgb->convert(matrix =>[ [ 1, 0, 0, 0 ],
980 [ 0, 1, 0, 0 ],
981 [ 0, 0, 1, 0 ],
982 [ 0, 0, 0, 0.5 ],
983 ]);
984
985 =item combine()
986 X<combine>
987
988 Combine channels from one or more input images into a new image.
989
990 Parameters:
991
992 =over
993
994 =item *
995
996 C<src> - a reference to an array of input images. There must be at least
997 one input image. A given image may appear more than once in C<src>.
998
999 =item *
1000
1001 C<channels> - a reference to an array of channels corresponding to the
1002 source images. If C<channels> is not supplied then the first channel
1003 from each input image is used. If the array referenced by C<channels>
1004 is shorter than that referenced by C<src> then the first channel is
1005 used from the extra images.
1006
1007 =back
1008
1009 # make an rgb image from red, green, and blue images
1010 my $rgb = Imager->combine(src => [ $red, $green, $blue ]);
1011
1012 # convert a BGR image into RGB
1013 my $rgb = Imager->combine(src => [ $bgr, $bgr, $bgr ],
1014 channels => [ 2, 1, 0 ]);
1015
1016 # add an alpha channel from another image
1017 my $rgba = Imager->combine(src => [ $rgb, $rgb, $rgb, $alpha ],
1018 channels => [ 0, 1, 2, 0 ]);
1019
1020 =back
1021
1022 =head2 Color Mappings
1023
1024 =over
1025
1026 =item map()
1027
1028 You can use the map method to map the values of each channel of an
1029 image independently using a list of look-up tables. It's important to
1030 realize that the modification is made inplace. The function simply
1031 returns the input image again or undef on failure.
1032
1033 Each channel is mapped independently through a look-up table with 256
1034 entries. The elements in the table should not be less than 0 and not
1035 greater than 255. If they are out of the 0..255 range they are
1036 clamped to the range. If a table does not contain 256 entries it is
1037 silently ignored.
1038
1039 Single channels can mapped by specifying their name and the mapping
1040 table. The channel names are C<red>, C<green>, C<blue>, C<alpha>.
1041
1042 @map = map { int( $_/2 } 0..255;
1043 $img->map( red=>\@map );
1044
1045 It is also possible to specify a single map that is applied to all
1046 channels, alpha channel included. For example this applies a gamma
1047 correction with a gamma of 1.4 to the input image.
1048
1049 $gamma = 1.4;
1050 @map = map { int( 0.5 + 255*($_/255)**$gamma ) } 0..255;
1051 $img->map(all=> \@map);
1052
1053 The C<all> map is used as a default channel, if no other map is
1054 specified for a channel then the C<all> map is used instead. If we
1055 had not wanted to apply gamma to the alpha channel we would have used:
1056
1057 $img->map(all=> \@map, alpha=>[]);
1058
1059 Since C<[]> contains fewer than 256 element the gamma channel is
1060 unaffected.
1061
1062 It is also possible to simply specify an array of maps that are
1063 applied to the images in the RGBA order. For example to apply
1064 maps to the C<red> and C<blue> channels one would use:
1065
1066 $img->map(maps=>[\@redmap, [], \@bluemap]);
1067
1068 =back
1069
1070 =head1 SEE ALSO
1071
1072 L<Imager>, L<Imager::Engines>
1073
1074 =head1 AUTHOR
1075
1076 Tony Cook <[email protected]>, Arnar M. Hrafnkelsson
1077
1078 =head1 REVISION
1079
1080 $Revision$
1081
1082 =cut