3 Imager::Draw - Draw primitives to images
11 $blue = Imager::Color->new( 0, 0, 255 );
12 $fill = Imager::Fill->new(hatch=>'stipple');
14 $img->line(color=>$blue, x1=>10, x2=>100,
15 y1=>20, y2=>50, aa=>1, endp=>1 );
17 $img->polyline(points=>[[$x0,$y0], [$x1,$y1], [$x2,$y2]],
19 $img->polyline(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], aa=>1);
21 $img->box(color=> $blue, xmin=> 10, ymin=>30,
22 xmax=>200, ymax=>300, filled=>1);
23 $img->box(fill=>$fill);
25 $img->arc(color=>$blue, r=>20, x=>200, y=>100,
28 $img->circle(color=>$blue, r=>50, x=>200, y=>100);
30 $img->polygon(points=>[[$x0,$y0], [$x1,$y1], [$x2,$y2]],
33 $img->polygon(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2]);
35 $img->flood_fill(x=>50, y=>50, color=>$color);
37 $img->setpixel(x=>50, y=>70, color=>$color);
39 $img->setpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40], color=>$color);
41 my $color = $img->getpixel(x=>50, y=>70);
43 my @colors = $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]);
46 my $font = Imager::Font->new(...) or die;
47 $img->string(x => 50, y => 70,
49 string => "Hello, World!",
54 # bottom right-hand corner of the image
55 $img->align_string(x => $img->getwidth() - 1,
56 y => $img->getheight() - 1,
64 my @colors = $img->getscanline(y=>50, x=>10, width=>20);
66 $img->setscanline(y=>60, x=>20, pixels=>\@colors);
68 my @samples = $img->getsamples(y=>50, x=>10, width=>20,
73 It is possible to draw with graphics primitives onto images. Such
74 primitives include boxes, arcs, circles, polygons and lines. The
75 coordinate system in Imager has the origin C<(0,0)> in the upper left
76 corner of an image with co-ordinates increasing to the right and
77 bottom. For non anti-aliasing operation all coordinates are rounded
78 towards the nearest integer. For anti-aliased operations floating
79 point coordinates are used.
81 Drawing is assumed to take place in a coordinate system of infinite
82 resolution. This is the typical convention and really only matters when
83 it is necessary to check for off-by-one cases. Typically it's useful to
84 think of C<(10, 20)> as C<(10.00, 20.00)> and consider the consequences.
86 =head2 Color Parameters
88 X<color parameters>The C<color> parameter for any of the drawing
89 methods can be an L<Imager::Color> object, a simple scalar that
90 Imager::Color can understand, a hashref of parameters that
91 Imager::Color->new understands, or an arrayref of red, green, blue
94 $image->box(..., color=>'red');
95 $image->line(..., color=>'#FF0000');
96 $image->flood_fill(..., color=>[ 255, 0, 255 ]);
98 While supplying colors as names, array references or CSS color
99 specifiers is convenient, for maximum performance you should supply
100 the color as an L<Imager::Color> object:
102 my @colors = map Imager::Color->new($_), qw/red green blue/
103 for my $i (1..1000) {
104 $image->box(..., color => $colors[rand @colors]);
107 =head2 Fill Parameters
109 X<fill parameters>All filled primitives, i.e. C<arc()>, C<box()>,
110 C<circle()>, C<polygon()> and the C<flood_fill()> method can take a
111 C<fill> parameter instead of a C<color> parameter which can either be
112 an Imager::Fill object, or a reference to a hash containing the
113 parameters used to create the fill, for example:
115 $image->box(..., fill=>{ hatch => 'check1x1' });
116 my $fillimage = Imager->new;
117 $fillimage->read(file=>$somefile) or die;
118 $image->flood_fill(..., fill=>{ image=>$fillimage });
120 Currently you can create opaque or transparent plain color fills,
121 hatched fills, image based fills and fountain fills. See
122 L<Imager::Fill> for more information.
124 =head2 List of primitives
130 $img->line(color=>$green, x1=>10, x2=>100,
131 y1=>20, y2=>50, aa=>1, endp=>1 );
133 X<line method>Draws a line from (x1,y1) to (x2,y2). The endpoint
134 (x2,y2) is drawn by default. If C<endp> of 0 is specified then the
135 endpoint will not be drawn. If C<aa> is set then the line will be
136 drawn anti-aliased. The C<antialias> parameter is still available for
137 backwards compatibility.
145 C<x1>, C<y1> - starting point of the line. Required.
149 C<x2>, C<y2> - end point of the line. Required.
153 C<color> - the color of the line. See L</"Color Parameters">. Default:
158 C<endp> - if zero the end point of the line is not drawn. Default: 1
159 - the end point is drawn. This is useful to set to 0 when drawing a
160 series of connected lines.
164 C<aa> - if true the line is drawn anti-aliased. Default: 0.
170 $img->polyline(points=>[[$x0,$y0],[$x1,$y1],[$x2,$y2]],color=>$red);
171 $img->polyline(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], aa=>1);
173 X<polyline method>C<polyline> is used to draw multiple lines between a
174 series of points. The point set can either be specified as an
175 arrayref to an array of array references (where each such array
176 represents a point). The other way is to specify two array
179 The C<antialias> parameter is still available for backwards compatibility.
185 points - a reference to an array of references to arrays containing
186 the co-ordinates of the points in the line, for example:
188 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
189 $img->polyline(points => \@points);
193 x, y - each is an array of x or y ordinates. This is an alternative
194 to supplying the C<points> parameter.
196 # same as the above points example
197 my @x = ( 0, 100, 100, 0 );
198 my @y = ( 0, 0, 100, 100 );
199 $img->polyline(x => \@x, y => \@y);
203 C<color> - the color of the line. See L</"Color Parameters">.
208 C<aa> - if true the line is drawn anti-aliased. Default: 0. Can also
209 be supplied as C<antialias> for backward compatibility.
215 $blue = Imager::Color->new( 0, 0, 255 );
216 $img->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax=>300,
219 X<box method>If any of the edges of the box are omitted it will snap
220 to the outer edge of the image in that direction. If C<filled> is
221 omitted the box is drawn as an outline. Instead of a color it is
222 possible to use a C<fill> pattern:
224 $fill = Imager::Fill->new(hatch=>'stipple');
225 $img->box(fill=>$fill); # fill entire image with a given fill pattern
227 $img->box(xmin=>10, ymin=>30, xmax=>150, ymax=>60,
228 fill => { hatch=>'cross2' });
230 Also if a color is omitted a color with (255,255,255,255) is used
231 instead. [NOTE: This may change to use C<$img-E<gt>fgcolor()> in the future].
233 Box does not support fractional coordinates yet.
241 C<xmin> - left side of the box. Default: 0 (left edge of the image)
245 C<ymin> - top side of the box. Default: 0 (top edge of the image)
249 C<xmax> - right side of the box. Default: C<< $img->getwidth-1
250 >>. (right edge of the image)
254 C<ymax> - bottom side of the box. Default: C<< $img->getheight-1
255 >>. (bottom edge of the image)
257 Note: C<xmax> and C<ymax> are I<inclusive> - the number of pixels
258 drawn for a filled box is C<(xmax-xmin+1) * (ymax-ymin+1)>.
262 C<box> - a reference to an array of (left, top, right, bottom)
263 co-ordinates. This is an alternative to supplying C<xmin>, C<ymin>,
264 C<xmax>, C<ymax> and overrides their values.
268 C<color> - the color of the line. See L</"Color Parameters">.
269 Default: white. This is ignored if the filled parameter
273 C<filled> - if non-zero the box is filled with I<color> instead of
274 outlined. Default: an outline is drawn.
278 C<fill> - the fill for the box. If this is supplied then the box will be
279 filled. See L</"Fill Parameters">.
285 $img->arc(color=>$red, r=>20, x=>200, y=>100, d1=>10, d2=>20 );
287 This creates a filled red arc with a 'center' at (200, 100) and spans
288 10 degrees and the slice has a radius of 20.
290 It's also possible to supply a C<fill> parameter.
292 To draw just an arc outline - just the curve, not the radius lines,
297 $img->arc(color=>$red, r=>20, x=>200, y=>100, d1=>10, d2=>20, filled=>0 );
303 C<x>, C<y> - center of the filled arc. Default: center of the image.
307 C<r> - radius of the arc. Default: 1/3 of min(image height, image width).
311 C<d1> - starting angle of the arc, in degrees. Default: 0
315 C<d2> - ending angle of the arc, in degrees. Default: 361.
319 C<color> - the color of the filled arc. See L</"Color Parameters">.
320 Default: white. Overridden by C<fill>.
324 C<fill> - the fill for the filled arc. See L</"Fill Parameters">
328 C<aa> - if true the filled arc is drawn anti-aliased. Default: false.
330 Anti-aliased arc() is experimental for now, I'm not entirely happy
331 with the results in some cases.
335 C<filled> - set to 0 to draw only an outline.
339 # arc going through angle zero:
340 $img->arc(d1=>320, d2=>40, x=>100, y=>100, r=>50, color=>'blue');
343 $img->arc(d1=>135, d2=>45, x=>100, y=>150, r=>50,
344 fill=>{ solid=>'red', combine=>'diff' });
346 # draw an anti-aliased circle outline
347 $img->arc(x => 100, y => 150, r => 150, filled => 0,
348 color => '#F00', aa => 1);
350 # draw an anti-aliased arc
351 $img->arc(x => 100, y => 150, r => 90, filled => 0,
352 color => '#0f0', aa => 1, d1 => 90, d2 => 180);
356 $img->circle(color=>$green, r=>50, x=>200, y=>100, aa=>1, filled=>1);
358 This creates an anti-aliased green circle with its center at (200, 100)
359 and has a radius of 50. It's also possible to supply a C<fill> parameter
360 instead of a color parameter.
362 $img->circle(r => 50, x=> 150, y => 150, fill=>{ hatch => 'stipple' });
364 To draw a circular outline, set C<filled> to 0:
366 $img->circle(color=>$green, r=>50, x=>200, y=>100, aa=>1, filled=>0);
372 C<x>, C<y> - center of the filled circle. Default: center of the image.
376 C<r> - radius of the circle. Default: 1/3 of min(image height, image width).
380 C<color> - the color of the filled circle. See L</"Color Parameters">.
381 Default: white. Overridden by C<fill>.
385 C<fill> - the fill for the filled circle. See L</"Fill Parameters">
389 C<aa> - if true the filled circle is drawn anti-aliased. Default: false.
393 C<filled> - set to 0 to just draw an outline.
399 $img->polygon(points=>[[$x0,$y0],[$x1,$y1],[$x2,$y2]],color=>$red);
400 $img->polygon(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], fill=>$fill);
402 Polygon is used to draw a filled polygon. Currently the polygon is
403 always drawn anti-aliased, although that will change in the future.
404 Like other anti-aliased drawing functions its coordinates can be
405 specified with floating point values. As with other filled shapes
406 it's possible to use a C<fill> instead of a color.
412 C<points> - a reference to an array of references to arrays containing
413 the co-ordinates of the points in the line, for example:
415 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
416 $img->polygon(points => \@points);
420 C<x>, C<y> - each is an array of x or y ordinates. This is an alternative
421 to supplying the C<points> parameter.
423 # same as the above points example
424 my @x = ( 0, 100, 100, 0 );
425 my @y = ( 0, 0, 100, 100 );
426 $img->polygon(x => \@x, y => \@y);
430 C<color> - the color of the filled polygon. See L</"Color Parameters">.
431 Default: black. Overridden by C<fill>.
435 C<fill> - the fill for the filled circle. See L</"Fill Parameters">
441 X<flood_fill>You can fill a region that all has the same color using
442 the flood_fill() method, for example:
444 $img->flood_fill(x=>50, y=>50, color=>$color);
446 will fill all regions the same color connected to the point (50, 50).
448 Alternatively you can fill a region limited by a given border color:
450 # stop at the red border
451 $im->flood_fill(x=>50, y=>50, color=>$color, border=>"red");
453 You can also fill with a complex fill:
455 $img->flood_fill(x=>50, y=>50, fill=>{ hatch=>'cross1x1' });
463 C<x>, C<y> - the start point of the fill.
467 C<color> - the color of the filled area. See L</"Color Parameters">.
468 Default: white. Overridden by C<fill>.
472 C<fill> - the fill for the filled area. See L</"Fill Parameters">
476 C<border> - the border color of the region to be filled. If this
477 parameter is supplied flood_fill() will stop when it finds this color.
478 If this is not supplied then a normal fill is done. C<border> can be
479 supplied as a L</"Color Parameters">.
485 $img->setpixel(x=>50, y=>70, color=>$color);
486 $img->setpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40], color=>$color);
488 setpixel() is used to set one or more individual pixels.
490 You can supply a single set of co-ordinates as scalar C<x> and C<y>
491 parameters, or set either to an arrayref of ordinates.
493 If one array is shorter than another the final value in the shorter
494 will be duplicated until they match in length.
496 If only one of C<x> or C<y> is an array reference then setpixel() will
497 behave as if the non-reference value were an array reference
498 containing only that value.
502 my $count = $img->setpixel(x => 1, y => [ 0 .. 3 ], color => $color);
506 my $count = $img->setpixel(x => [ 1 ], y => [ 0 .. 3 ], color => $color);
508 and since the final element in the shorter array is duplicated, this
511 my $count = $img->setpixel(x => [ 1, 1, 1, 1 ], y => [ 0 .. 3 ],
520 x, y - either integers giving the co-ordinates of the pixel to set or
521 array references containing a set of pixels to be set.
525 color - the color of the pixels drawn. See L</"Color Parameters">.
530 When called with an array reference in either C<x> or C<y>, returns
531 the number of pixels successfully set, or false if none.
533 When called with scalars for x and y, return $img on success, false on
536 Possible errors conditions include:
540 =item * the image supplied is empty
542 =item * a reference to an empty array was supplied for C<x> or C<y>
544 =item * C<x> or C<y> wasn't supplied
546 =item * C<color> isn't a valid color, and can't be converted to a
551 On any of these errors, setpixel() returns an empty list and sets
556 my $color = $img->getpixel(x=>50, y=>70); my @colors =
557 $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]); my $colors_ref =
558 $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]);
560 getpixel() is used to retrieve one or more individual pixels.
562 You can supply a single set of co-ordinates as scalar C<x> and C<y>
563 parameters, or set each to an arrayref of ordinates.
565 If one array is shorter than another the final value in the shorter
566 will be duplicated until they match in length.
568 If only one of C<x> or C<y> is an array reference then getpixel() will
569 behave as if the non-reference value were an array reference
570 containing only that value.
574 my @colors = $img->getpixel(x => 0, y => [ 0 .. 3 ]);
578 my @colors = $img->getpixel(x => [ 0 ], y => [ 0 .. 3 ]);
580 and since the final element in the shorter array is duplicated, this
583 my @colors = $img->getpixel(x => [ 0, 0, 0, 0 ], y => [ 0 .. 3 ]);
585 To receive floating point colors from getpixel(), set the C<type>
586 parameter to 'float'.
594 x, y - either integers giving the co-ordinates of the pixel to set or
595 array references containing a set of pixels to be set.
599 type - the type of color object to return, either C<'8bit'> for
600 Imager::Color objects or C<'float'> for Imager::Color::Float objects.
605 When called with an array reference for either or C<x> or C<y>,
606 getpixel() will return a list of colors in list context, and an
607 arrayref in scalar context.
609 If a supplied co-ordinate is outside the image then C<undef> is
610 returned for the pixel.
612 Possible errors conditions include:
616 =item * the image supplied is empty
618 =item * a reference to an empty array was supplied for C<x> or C<y>
620 =item * C<x> or C<y> wasn't supplied
622 =item * C<type> isn't a valid value.
626 For any of these errors getpixel() returns an empty list.
630 my $font = Imager::Font->new(file=>"foo.ttf");
631 $img->string(x => 50, y => 70,
632 string => "Hello, World!",
638 Draws text on the image.
646 C<x>, C<y> - the point to draw the text from. If C<align> is 0 this
647 is the top left of the string. If C<align> is 1 (the default) then
648 this is the left of the string on the baseline. Required.
652 C<string> - the text to draw. Required unless you supply the C<text>
657 C<font> - an L<Imager::Font> object representing the font to draw the
662 C<aa> - if non-zero the output will be anti-aliased. Default: the value
663 set in Imager::Font->new() or 0 if not set.
667 C<align> - if non-zero the point supplied in (x,y) will be on the
668 base-line, if zero then (x,y) will be at the top-left of the string.
670 i.e. if drawing the string C<"yA"> and align is 0 the point (x,y) will
671 aligned with the top of the A. If align is 1 (the default) it will be
672 aligned with the baseline of the font, typically bottom of the A,
673 depending on the font used.
675 Default: the value set in Imager::Font->new, or 1 if not set.
679 C<channel> - if present, the text will be written to the specified
680 channel of the image and the color parameter will be ignore.
684 C<color> - the color to draw the text in. Default: the color supplied to
685 Imager::Font->new, or red if none.
689 C<size> - the point size to draw the text at. Default: the size
690 supplied to Imager::Font->new, or 15.
694 C<sizew> - the width scaling to draw the text at. Default: the value
699 C<utf8> - for drivers that support it, treat the string as UTF-8
700 encoded. For versions of perl that support Unicode (5.6 and later),
701 this will be enabled automatically if the C<string> parameter is
702 already a UTF-8 string. See L<Imager::Font/"UTF-8"> for more
707 C<vlayout> - for drivers that support it, draw the text vertically.
708 Note: I haven't found a font that has the appropriate metrics yet.
712 C<text> - alias for the C<string> parameter.
716 On error, string() returns false and you can use $img->errstr to get
717 the reason for the error.
721 Draws text aligned around a point on the image.
723 # "Hello" centered at 100, 100 in the image.
724 my ($left, $top, $right, $bottom) =
725 $img->align_string(string=>"Hello",
727 halign=>'center', valign=>'center',
736 C<x>, C<y> - the point to draw the text from. If C<align> is 0 this
737 is the top left of the string. If C<align> is 1 (the default) then
738 this is the left of the string on the baseline. Required.
742 C<string> - the text to draw. Required unless you supply the C<text>
747 C<font> - an L<Imager::Font> object representing the font to draw the
752 C<aa> - if non-zero the output will be anti-aliased
756 C<valign> - vertical alignment of the text against (x,y)
762 C<top> - Point is at the top of the text.
766 C<bottom> - Point is at the bottom of the text.
770 C<baseline> - Point is on the baseline of the text. This is the default.
774 C<center> - Point is vertically centered within the text.
780 C<halign> - horizontal alignment of the text against (x,y)
786 C<left> - The point is at the left of the text. This is the default.
790 C<start> - The point is at the start point of the text.
794 C<center> - The point is horizontally centered within the text.
798 C<right> - The point is at the right end of the text.
802 C<end> - The point is at the end point of the text.
808 C<channel> - if present, the text will be written to the specified
809 channel of the image and the color parameter will be ignore.
813 C<color> - the color to draw the text in. Default: the color supplied to
814 Imager::Font->new, or red if none.
818 C<size> - the point size to draw the text at. Default: the size supplied
819 to Imager::Font->new, or 15.
823 C<sizew> - the width scaling to draw the text at. Default: the value of
828 C<utf8> - for drivers that support it, treat the string as UTF-8
829 encoded. For versions of perl that support Unicode (5.6 and later),
830 this will be enabled automatically if the C<string> parameter is
831 already a UTF-8 string. See L<Imager::Font/"UTF-8"> for more
836 C<vlayout> - for drivers that support it, draw the text vertically.
837 Note: I haven't found a font that has the appropriate metrics yet.
841 C<text> - alias for the C<string> parameter.
845 On success returns a list of bounds of the drawn text, in the order
846 left, top, right, bottom.
848 On error, align_string() returns an empty list and you can use
849 C<< $img->errstr >> to get the reason for the error.
853 Set all or part of a horizontal line of pixels to an image. This
854 method is most useful in conjunction with L</getscanline()>.
856 The parameters you can pass are:
862 C<y> - vertical position of the scan line. This parameter is required.
866 C<x> - position to start on the scan line. Default: 0
870 C<pixels> - either a reference to an array containing Imager::Color
871 objects, an reference to an array containing Imager::Color::Float
872 objects or a scalar containing packed color data.
874 If C<type> is C<index> then this can either be a reference to an array
875 of palette color indexes or a scalar containing packed indexes.
877 See L</"Packed Color Data"> for information on the format of packed
882 C<type> - the type of pixel data supplied. If you supply an array
883 reference of object then this is determined automatically. If you
884 supply packed color data this defaults to C<'8bit'>, if your data is
885 packed floating point color data then set this to C<'float'>.
887 You can use C<float> or C<8bit> samples with any image.
889 If this is 'index' then pixels should be either an array of palette
890 color indexes or a packed string of color indexes.
894 Returns the number of pixels set.
896 Each of the following sets 5 pixels from (5, 10) through (9, 10) to
897 blue, red, blue, red, blue:
899 my $red_color = Imager::Color->new(255, 0, 0);
900 my $blue_color = Imager::Color->new(0, 0, 255);
902 $image->setscanline(y=>10, x=>5, pixels=>
903 [ ($blue_color, $red_color) x 2, $blue_color ]);
905 # use floating point color instead, for 16-bit plus images
906 my $red_colorf = Imager::Color::Float->new(1.0, 0, 0);
907 my $blue_colorf = Imager::Color::Float->new(0, 0, 1.0);
909 $image->setscanline(y=>10, x=>5, pixels=>
910 [ ($blue_colorf, $red_colorf) x 2, $blue_colorf ]);
913 $image->setscanline(y=>10, x=>5, pixels=>
914 pack("C*", ((0, 0, 255, 255), (255, 0, 0, 255)) x 2,
917 # packed floating point samples
918 $image->setscanline(y=>10, x=>5, type=>'float', pixels=>
919 pack("d*", ((0, 0, 1.0, 1.0), (1.0, 0, 0, 1.0)) x 2,
923 Copy even rows from one image to another:
925 for (my $y = 0; $y < $im2->getheight; $y+=2) {
926 $im1->setscanline(y=>$y,
927 pixels=>scalar($im2->getscanline(y=>$y)));
931 Set the blue channel to 0 for all pixels in an image. This could be
932 done with convert too:
934 for my $y (0..$im->getheight-1) {
935 my $row = $im->getscanline(y=>$y);
936 $row =~ s/(..).(.)/$1\0$2/gs;
937 $im->setscanline(y=>$y, pixels=>$row);
942 Read all or part of a horizontal line of pixels from an image. This
943 method is most useful in conjunction with L</setscanline()>.
945 The parameters you can pass are:
951 C<y> - vertical position of the scan line. This parameter is required.
955 C<x> - position to start on the scan line. Default: 0
959 C<width> - number of pixels to read. Default: $img->getwidth - x
963 C<type> - the type of pixel data to return. Default: C<8bit>.
965 Permitted values are C<8bit> and C<float> and C<index>.
969 In list context this method will return a list of Imager::Color
970 objects when I<type> is C<8bit>, or a list of Imager::Color::Float
971 objects when I<type> if C<float>, or a list of integers when I<type>
974 In scalar context this returns a packed 8-bit pixels when I<type> is
975 C<8bit>, or a list of packed floating point pixels when I<type> is
976 C<float>, or packed palette color indexes when I<type> is C<index>.
978 The values of samples for which the image does not have channels is
979 undefined. For example, for a single channel image the values of
980 channels 1 through 3 are undefined.
982 Check image for a given color:
985 YLOOP: for my $y (0..$img->getheight-1) {
986 my @colors = $img->getscanline(y=>$y);
987 for my $color (@colors) {
988 my ($red, $green, $blue, $alpha) = $color->rgba;
989 if ($red == $test_red && $green == $test_green && $blue == $test_blue
990 && $alpha == $test_alpha) {
997 Or do it using packed data:
1000 my $test_packed = pack("CCCC", $test_red, $test_green, $test_blue,
1002 YLOOP: for my $y (0..$img->getheight-1) {
1003 my $colors = $img->getscanline(y=>$y);
1004 while (length $colors) {
1005 if (substr($colors, 0, 4, '') eq $test_packed) {
1012 Some of the examples for L</setscanline()> for more examples.
1016 Read specified channels from all or part of a horizontal line of
1017 pixels from an image.
1019 The parameters you can pass are:
1025 C<y> - vertical position of the scan line. This parameter is required.
1029 C<x> - position to start on the scan line. Default: 0
1033 C<width> - number of pixels to read. Default: C<< $img->getwidth - x >>
1037 C<type> - the type of sample data to return. Default: C<8bit>.
1039 Permitted values are C<8bit> and C<float>.
1041 As of Imager 0.61 this can be C<16bit> only for 16 bit images.
1045 C<channels> - a reference to an array of channels to return, where 0
1046 is the first channel. Default: C<< [ 0 .. $self->getchannels()-1 ] >>
1050 C<target> - if an array reference is supplied in target then the samples
1051 will be stored here instead of being returned.
1055 C<offset> - the offset within the array referenced by I<target>
1059 In list context this will return a list of integers between 0 and 255
1060 inclusive when I<type> is C<8bit>, or a list of floating point numbers
1061 between 0.0 and 1.0 inclusive when I<type> is C<float>.
1063 In scalar context this will return a string of packed bytes, as with
1064 C< pack("C*", ...) > when I<type> is C<8bit> or a string of packed
1065 doubles as with C< pack("d*", ...) > when I<type> is C<float>.
1067 If the I<target> option is supplied then only a count of samples is
1070 Example: Check if any pixels in an image have a non-zero alpha
1074 for my $y (0 .. $img->getheight()-1) {
1075 my $alpha = $img->getsamples(y=>$y, channels=>[0]);
1076 if ($alpha =~ /[^\0]/) {
1082 Example: Convert a 2 channel gray image into a 4 channel RGBA image:
1084 # this could be done with convert() instead
1085 my $out = Imager->new(xsize => $src->getwidth(),
1086 ysize => $src->getheight(),
1088 for my $y ( 0 .. $src->getheight()-1 ) {
1089 my $data = $src->getsamples(y=>$y, channels=>[ 0, 0, 0, 1 ]);
1090 $out->setscanline(y=>$y, pixels=>$data);
1093 Retrieve 16-bit samples:
1095 if ($img->bits == 16) {
1097 $img->getsamples(x => 0, y => $y, target => \@samples, type => '16bit');
1102 This allows writing of samples back to some images. Currently this is
1103 only supported for 16-bit/sample images.
1111 C<y> - vertical position of the scan line. This parameter is required.
1115 C<x> - position to start on the scan line. Default: 0
1119 C<width> - number of pixels to write. Default: C<< $img->getwidth - x >>.
1120 The minimum of this and the number of pixels represented by the
1121 samples provided will be written.
1125 C<type> - the type of sample data to write. This parameter is required.
1127 This can be C<8bit>, C<float> or for 16-bit images only, C<16bit>.
1131 C<channels> - a reference to an array of channels to return, where 0 is
1132 the first channel. Default: C<< [ 0 .. $self->getchannels()-1 ] >>
1136 C<data> - for a type of C<8bit> or C<float> this can be a reference to
1137 an array of samples or a scalar containing packed samples. If C<data>
1138 is a scalar it may only contain characters from \x00 to \xFF.
1140 For a type of C<16bit> this can only be a reference to an array of
1147 C<offset> - the starting offset within the array referenced by
1148 I<data>. If C<data> is a scalar containing packed samples this offset
1153 Returns the number of samples written.
1155 $targ->setsamples(y => $y, data => \@data);
1157 $targ->setsamples(y => $y, data => \@data, offset => $src->getchannels);
1159 Copy from one image to another:
1161 my $targ = Imager->new(xsize => $src->getwidth,
1162 ysize => $src->getheight, channels => $src->getchannels);
1163 for my $y (0 .. $targ->getheight()-1) {
1164 my $row = $src->getsamples(y => $y)
1165 or die $src->errstr;
1166 $targ->setsamples(y => $y, data => $row)
1167 or die $targ->errstr;;
1170 Compose an image from separate source channels:
1172 my @src = ...; # images to work from, up to 4
1173 my $targ = Imager->new(xsize => $src[0]->getwidth,
1174 ysize => $src[0]->getheight, channels => scalar(@src));
1175 for my $y (0 .. $targ->getheight()-1) {
1176 for my $ch (0 .. $#src) {
1177 my $row = $src[$ch]->getsamples(y => $y, channels => [ 0 ]);
1178 $targ->setsamples(y => $y, data => $row, channels => [ $ch ] );
1184 =head1 Packed Color Data
1186 The getscanline() and setscanline() methods can work with pixels
1187 packed into scalars. This is useful to remove the cost of creating
1188 color objects, but should only be used when performance is an issue.
1190 The getsamples() and setsamples() methods can work with samples packed
1193 Packed data can either be 1 byte per sample or 1 double per sample.
1195 Each pixel returned by getscanline() or supplied to setscanline()
1196 contains 4 samples, even if the image has fewer then 4 channels. The
1197 values of the extra samples as returned by getscanline() is not
1198 specified. The extra samples passed to setscanline() are ignored.
1200 To produce packed 1 byte/sample pixels, use the pack C<C> template:
1202 my $packed_8bit_pixel = pack("CCCC", $red, $blue, $green, $alpha);
1204 To produce packed double/sample pixels, use the pack C<d> template:
1206 my $packed_float_pixel = pack("dddd", $red, $blue, $green, $alpha);
1208 If you use a I<type> parameter of C<index> then the values are palette
1209 color indexes, not sample values:
1211 my $im = Imager->new(xsize => 100, ysize => 100, type => 'paletted');
1212 my $black_index = $im->addcolors(colors => [ 'black' ]);
1213 my $red_index = $im->addcolors(colors => [ 'red' ]);
1215 my $packed_index_data = pack("C*", $black_index, $red_index);
1216 $im->setscanline(y => $y, pixels => $packed_index_data, type => 'index');
1218 =head1 Combine Types
1220 Some methods accept a C<combine> parameter, this can be any of the
1227 The fill pixel replaces the target pixel.
1231 The fill pixels alpha value is used to combine it with the target pixel.
1237 Each channel of fill and target is multiplied, and the result is
1238 combined using the alpha channel of the fill pixel.
1242 If the alpha of the fill pixel is greater than a random number, the
1243 fill pixel is alpha combined with the target pixel.
1247 The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
1251 The channels of the fill are subtracted from the target, clamped to be
1252 >= 0, and alpha combined with the target.
1256 The channels of the fill are subtracted from the target and the
1257 absolute value taken this is alpha combined with the target.
1261 The higher value is taken from each channel of the fill and target
1262 pixels, which is then alpha combined with the target.
1266 The higher value is taken from each channel of the fill and target
1267 pixels, which is then alpha combined with the target.
1271 The combination of the saturation and value of the target is combined
1272 with the hue of the fill pixel, and is then alpha combined with the
1277 The combination of the hue and value of the target is combined
1278 with the saturation of the fill pixel, and is then alpha combined with the
1283 The combination of the hue and value of the target is combined
1284 with the value of the fill pixel, and is then alpha combined with the
1289 The combination of the value of the target is combined with the hue
1290 and saturation of the fill pixel, and is then alpha combined with the
1299 Returns a list of possible combine types.
1305 box() does not support anti-aliasing yet. Default color is not
1310 Tony Cook <tonyc@cpan.org>, Arnar M. Hrafnkelsson.
1314 L<Imager>(3), L<Imager::Cookbook>(3)