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