access to poly_poly from perl as polypolygon()
[imager.git] / lib / Imager / Draw.pod
CommitLineData
7144e056
AMH
1=head1 NAME
2
3Imager::Draw - Draw primitives to images
4
5=head1 SYNOPSIS
6
7 use Imager;
8 use Imager::Fill;
9
10 $img = ...;
11 $blue = Imager::Color->new( 0, 0, 255 );
12 $fill = Imager::Fill->new(hatch=>'stipple');
13
14 $img->line(color=>$blue, x1=>10, x2=>100,
aa833c97 15 y1=>20, y2=>50, aa=>1, endp=>1 );
7144e056
AMH
16
17 $img->polyline(points=>[[$x0,$y0], [$x1,$y1], [$x2,$y2]],
18 color=>$blue);
19 $img->polyline(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], aa=>1);
20
21 $img->box(color=> $blue, xmin=> 10, ymin=>30,
22 xmax=>200, ymax=>300, filled=>1);
23 $img->box(fill=>$fill);
24
9ecefd41 25 $img->arc(color=>$blue, r=>20, x=>200, y=>100,
7144e056
AMH
26 d1=>10, d2=>20 );
27
9ecefd41 28 $img->circle(color=>$blue, r=>50, x=>200, y=>100);
7144e056
AMH
29
30 $img->polygon(points=>[[$x0,$y0], [$x1,$y1], [$x2,$y2]],
31 color=>$blue);
32
33 $img->polygon(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2]);
34
35 $img->flood_fill(x=>50, y=>50, color=>$color);
36
591b5954
TC
37 $img->setpixel(x=>50, y=>70, color=>$color);
38
39 $img->setpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40], color=>$color);
40
41 my $color = $img->getpixel(x=>50, y=>70);
42
43 my @colors = $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]);
44
08e6cbea
TC
45 # drawing text
46 my $font = Imager::Font->new(...) or die;
47 $img->string(x => 50, y => 70,
48 font => $font,
49 string => "Hello, World!",
50 color => 'red',
51 size => 30,
52 aa => 1);
53
54 # bottom right-hand corner of the image
55 $img->align_string(x => $img->getwidth() - 1,
56 y => $img->getheight() - 1,
57 halign => 'right',
cad360aa 58 valign => 'bottom',
08e6cbea
TC
59 string => 'Imager',
60 font => $font,
61 size => 12);
62
ca4d914e
TC
63 # low-level functions
64 my @colors = $img->getscanline(y=>50, x=>10, width=>20);
65
66 $img->setscanline(y=>60, x=>20, pixels=>\@colors);
67
68 my @samples = $img->getsamples(y=>50, x=>10, width=>20,
69 channels=>[ 2, 0 ]);
70
7144e056
AMH
71=head1 DESCRIPTION
72
73It is possible to draw with graphics primitives onto images. Such
74primitives include boxes, arcs, circles, polygons and lines. The
75coordinate system in Imager has the origin C<(0,0)> in the upper left
7117f25c 76corner of an image with co-ordinates increasing to the right and
5715f7c3
TC
77bottom. For non anti-aliasing operation all coordinates are rounded
78towards the nearest integer. For anti-aliased operations floating
7144e056
AMH
79point coordinates are used.
80
81Drawing is assumed to take place in a coordinate system of infinite
82resolution. This is the typical convention and really only matters when
7468f3fa 83it is necessary to check for off-by-one cases. Typically it's useful to
5715f7c3 84think of C<(10, 20)> as C<(10.00, 20.00)> and consider the consequences.
7144e056 85
08e6cbea
TC
86=head2 Color Parameters
87
88X<color parameters>The C<color> parameter for any of the drawing
89methods can be an L<Imager::Color> object, a simple scalar that
90Imager::Color can understand, a hashref of parameters that
91Imager::Color->new understands, or an arrayref of red, green, blue
92values, for example:
ed9e5812
TC
93
94 $image->box(..., color=>'red');
95 $image->line(..., color=>'#FF0000');
96 $image->flood_fill(..., color=>[ 255, 0, 255 ]);
7144e056 97
8289f78b
TC
98While supplying colors as names, array references or CSS color
99specifiers is convenient, for maximum performance you should supply
100the color as an L<Imager::Color> object:
101
102 my @colors = map Imager::Color->new($_), qw/red green blue/
103 for my $i (1..1000) {
104 $image->box(..., color => $colors[rand @colors]);
105 }
106
08e6cbea
TC
107=head2 Fill Parameters
108
109X<fill parameters>All filled primitives, i.e. C<arc()>, C<box()>,
110C<circle()>, C<polygon()> and the C<flood_fill()> method can take a
111C<fill> parameter instead of a C<color> parameter which can either be
112an Imager::Fill object, or a reference to a hash containing the
ed9e5812
TC
113parameters used to create the fill, for example:
114
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 });
7144e056
AMH
119
120Currently you can create opaque or transparent plain color fills,
121hatched fills, image based fills and fountain fills. See
122L<Imager::Fill> for more information.
123
124=head2 List of primitives
125
126=over
127
67d441b2 128=item line()
7144e056
AMH
129
130 $img->line(color=>$green, x1=>10, x2=>100,
aa833c97 131 y1=>20, y2=>50, aa=>1, endp=>1 );
7144e056 132
08e6cbea 133X<line method>Draws a line from (x1,y1) to (x2,y2). The endpoint
5715f7c3 134(x2,y2) is drawn by default. If C<endp> of 0 is specified then the
08e6cbea 135endpoint will not be drawn. If C<aa> is set then the line will be
5715f7c3 136drawn anti-aliased. The C<antialias> parameter is still available for
08e6cbea
TC
137backwards compatibility.
138
139Parameters:
140
141=over
142
143=item *
144
5715f7c3 145C<x1>, C<y1> - starting point of the line. Required.
08e6cbea
TC
146
147=item *
148
5715f7c3 149C<x2>, C<y2> - end point of the line. Required.
08e6cbea
TC
150
151=item *
152
67d441b2 153C<color> - the color of the line. See L</"Color Parameters">. Default:
08e6cbea
TC
154black.
155
156=item *
157
5715f7c3
TC
158C<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
08e6cbea
TC
160series of connected lines.
161
162=item *
163
5715f7c3 164C<aa> - if true the line is drawn anti-aliased. Default: 0.
08e6cbea
TC
165
166=back
7144e056 167
67d441b2 168=item polyline()
7144e056
AMH
169
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);
172
5715f7c3 173X<polyline method>C<polyline> is used to draw multiple lines between a
08e6cbea
TC
174series of points. The point set can either be specified as an
175arrayref to an array of array references (where each such array
176represents a point). The other way is to specify two array
177references.
7144e056 178
5715f7c3 179The C<antialias> parameter is still available for backwards compatibility.
7144e056 180
08e6cbea
TC
181=over
182
183=item *
184
185points - a reference to an array of references to arrays containing
186the co-ordinates of the points in the line, for example:
187
188 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
189 $img->polyline(points => \@points);
190
191=item *
192
193x, y - each is an array of x or y ordinates. This is an alternative
194to supplying the C<points> parameter.
195
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);
200
201=item *
202
67d441b2 203C<color> - the color of the line. See L</"Color Parameters">.
5715f7c3 204Default: black.
08e6cbea
TC
205
206=item *
207
5715f7c3
TC
208C<aa> - if true the line is drawn anti-aliased. Default: 0. Can also
209be supplied as C<antialias> for backward compatibility.
08e6cbea
TC
210
211=back
212
67d441b2 213=item box()
7144e056
AMH
214
215 $blue = Imager::Color->new( 0, 0, 255 );
ed9e5812
TC
216 $img->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax=>300,
217 filled=>1);
7144e056 218
5715f7c3 219X<box method>If any of the edges of the box are omitted it will snap
08e6cbea 220to the outer edge of the image in that direction. If C<filled> is
5715f7c3 221omitted the box is drawn as an outline. Instead of a color it is
08e6cbea 222possible to use a C<fill> pattern:
7144e056
AMH
223
224 $fill = Imager::Fill->new(hatch=>'stipple');
225 $img->box(fill=>$fill); # fill entire image with a given fill pattern
226
227 $img->box(xmin=>10, ymin=>30, xmax=>150, ymax=>60,
228 fill => { hatch=>'cross2' });
229
230Also if a color is omitted a color with (255,255,255,255) is used
55b287f5 231instead. [NOTE: This may change to use C<$img-E<gt>fgcolor()> in the future].
7144e056
AMH
232
233Box does not support fractional coordinates yet.
234
08e6cbea
TC
235Parameters:
236
237=over
238
239=item *
240
5715f7c3 241C<xmin> - left side of the box. Default: 0 (left edge of the image)
08e6cbea
TC
242
243=item *
244
5715f7c3 245C<ymin> - top side of the box. Default: 0 (top edge of the image)
08e6cbea
TC
246
247=item *
248
5715f7c3
TC
249C<xmax> - right side of the box. Default: C<< $img->getwidth-1
250>>. (right edge of the image)
08e6cbea
TC
251
252=item *
253
5715f7c3
TC
254C<ymax> - bottom side of the box. Default: C<< $img->getheight-1
255>>. (bottom edge of the image)
08e6cbea 256
5715f7c3
TC
257Note: C<xmax> and C<ymax> are I<inclusive> - the number of pixels
258drawn for a filled box is C<(xmax-xmin+1) * (ymax-ymin+1)>.
08e6cbea
TC
259
260=item *
261
5715f7c3
TC
262C<box> - a reference to an array of (left, top, right, bottom)
263co-ordinates. This is an alternative to supplying C<xmin>, C<ymin>,
264C<xmax>, C<ymax> and overrides their values.
08e6cbea
TC
265
266=item *
267
67d441b2 268C<color> - the color of the line. See L</"Color Parameters">.
5715f7c3 269Default: white. This is ignored if the filled parameter
08e6cbea
TC
270
271=item *
272
5715f7c3 273C<filled> - if non-zero the box is filled with I<color> instead of
08e6cbea
TC
274outlined. Default: an outline is drawn.
275
276=item *
277
5715f7c3 278C<fill> - the fill for the box. If this is supplied then the box will be
67d441b2 279filled. See L</"Fill Parameters">.
08e6cbea
TC
280
281=back
282
67d441b2 283=item arc()
7144e056 284
9ecefd41 285 $img->arc(color=>$red, r=>20, x=>200, y=>100, d1=>10, d2=>20 );
7144e056
AMH
286
287This creates a filled red arc with a 'center' at (200, 100) and spans
40068b33
TC
28810 degrees and the slice has a radius of 20.
289
7144e056
AMH
290It's also possible to supply a C<fill> parameter.
291
40068b33
TC
292To draw just an arc outline - just the curve, not the radius lines,
293set filled to 0:
294
08e6cbea
TC
295Parameters:
296
40068b33
TC
297 $img->arc(color=>$red, r=>20, x=>200, y=>100, d1=>10, d2=>20, filled=>0 );
298
08e6cbea
TC
299=over
300
301=item *
302
5715f7c3 303C<x>, C<y> - center of the filled arc. Default: center of the image.
08e6cbea
TC
304
305=item *
306
5715f7c3 307C<r> - radius of the arc. Default: 1/3 of min(image height, image width).
08e6cbea
TC
308
309=item *
310
5715f7c3 311C<d1> - starting angle of the arc, in degrees. Default: 0
08e6cbea
TC
312
313=item *
314
5715f7c3 315C<d2> - ending angle of the arc, in degrees. Default: 361.
08e6cbea
TC
316
317=item *
318
67d441b2 319C<color> - the color of the filled arc. See L</"Color Parameters">.
08e6cbea
TC
320Default: white. Overridden by C<fill>.
321
322=item *
323
67d441b2 324C<fill> - the fill for the filled arc. See L</"Fill Parameters">
08e6cbea
TC
325
326=item *
327
5715f7c3 328C<aa> - if true the filled arc is drawn anti-aliased. Default: false.
08e6cbea
TC
329
330Anti-aliased arc() is experimental for now, I'm not entirely happy
331with the results in some cases.
332
40068b33
TC
333=item *
334
5715f7c3 335C<filled> - set to 0 to draw only an outline.
40068b33 336
08e6cbea
TC
337=back
338
339 # arc going through angle zero:
340 $img->arc(d1=>320, d2=>40, x=>100, y=>100, r=>50, color=>'blue');
341
342 # complex fill arc
343 $img->arc(d1=>135, d2=>45, x=>100, y=>150, r=>50,
344 fill=>{ solid=>'red', combine=>'diff' });
345
80599c3f
TC
346 # draw an anti-aliased circle outline
347 $img->arc(x => 100, y => 150, r => 150, filled => 0,
348 color => '#F00', aa => 1);
349
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);
353
67d441b2 354=item circle()
7144e056 355
08e6cbea 356 $img->circle(color=>$green, r=>50, x=>200, y=>100, aa=>1, filled=>1);
7144e056 357
5715f7c3 358This creates an anti-aliased green circle with its center at (200, 100)
7144e056
AMH
359and has a radius of 50. It's also possible to supply a C<fill> parameter
360instead of a color parameter.
361
08e6cbea
TC
362 $img->circle(r => 50, x=> 150, y => 150, fill=>{ hatch => 'stipple' });
363
40068b33
TC
364To draw a circular outline, set C<filled> to 0:
365
366 $img->circle(color=>$green, r=>50, x=>200, y=>100, aa=>1, filled=>0);
7144e056 367
08e6cbea
TC
368=over
369
370=item *
371
5715f7c3 372C<x>, C<y> - center of the filled circle. Default: center of the image.
08e6cbea
TC
373
374=item *
375
5715f7c3 376C<r> - radius of the circle. Default: 1/3 of min(image height, image width).
08e6cbea
TC
377
378=item *
379
67d441b2 380C<color> - the color of the filled circle. See L</"Color Parameters">.
08e6cbea
TC
381Default: white. Overridden by C<fill>.
382
383=item *
384
67d441b2 385C<fill> - the fill for the filled circle. See L</"Fill Parameters">
08e6cbea
TC
386
387=item *
388
5715f7c3 389C<aa> - if true the filled circle is drawn anti-aliased. Default: false.
08e6cbea 390
40068b33
TC
391=item *
392
5715f7c3 393C<filled> - set to 0 to just draw an outline.
40068b33 394
08e6cbea 395=back
7144e056 396
5715f7c3 397=item polygon()
7144e056
AMH
398
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);
401
402Polygon is used to draw a filled polygon. Currently the polygon is
5715f7c3
TC
403always drawn anti-aliased, although that will change in the future.
404Like other anti-aliased drawing functions its coordinates can be
7144e056
AMH
405specified with floating point values. As with other filled shapes
406it's possible to use a C<fill> instead of a color.
407
08e6cbea
TC
408=over
409
410=item *
411
5715f7c3 412C<points> - a reference to an array of references to arrays containing
08e6cbea
TC
413the co-ordinates of the points in the line, for example:
414
415 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
416 $img->polygon(points => \@points);
417
418=item *
419
5715f7c3 420C<x>, C<y> - each is an array of x or y ordinates. This is an alternative
08e6cbea
TC
421to supplying the C<points> parameter.
422
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);
427
428=item *
429
67d441b2 430C<color> - the color of the filled polygon. See L</"Color Parameters">.
08e6cbea
TC
431Default: black. Overridden by C<fill>.
432
433=item *
434
67d441b2 435C<fill> - the fill for the filled circle. See L</"Fill Parameters">
08e6cbea
TC
436
437=back
438
5715f7c3 439=item flood_fill()
7144e056 440
3efb0915
TC
441X<flood_fill>You can fill a region that all has the same color using
442the flood_fill() method, for example:
7144e056
AMH
443
444 $img->flood_fill(x=>50, y=>50, color=>$color);
445
446will fill all regions the same color connected to the point (50, 50).
447
3efb0915
TC
448Alternatively you can fill a region limited by a given border color:
449
450 # stop at the red border
451 $im->flood_fill(x=>50, y=>50, color=>$color, border=>"red");
452
ed9e5812
TC
453You can also fill with a complex fill:
454
455 $img->flood_fill(x=>50, y=>50, fill=>{ hatch=>'cross1x1' });
456
08e6cbea
TC
457Parameters:
458
459=over
460
461=item *
462
5715f7c3 463C<x>, C<y> - the start point of the fill.
08e6cbea
TC
464
465=item *
466
67d441b2 467C<color> - the color of the filled area. See L</"Color Parameters">.
08e6cbea
TC
468Default: white. Overridden by C<fill>.
469
470=item *
471
67d441b2 472C<fill> - the fill for the filled area. See L</"Fill Parameters">
08e6cbea 473
3efb0915
TC
474=item *
475
5715f7c3 476C<border> - the border color of the region to be filled. If this
3efb0915
TC
477parameter is supplied flood_fill() will stop when it finds this color.
478If this is not supplied then a normal fill is done. C<border> can be
67d441b2 479supplied as a L</"Color Parameters">.
3efb0915 480
08e6cbea
TC
481=back
482
0d80f37e
TC
483=item polypolygon()
484
485C<FIXME>
486
5715f7c3 487=item setpixel()
591b5954
TC
488
489 $img->setpixel(x=>50, y=>70, color=>$color);
490 $img->setpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40], color=>$color);
08e6cbea
TC
491
492setpixel() is used to set one or more individual pixels.
493
2a27eeff
TC
494You can supply a single set of co-ordinates as scalar C<x> and C<y>
495parameters, or set either to an arrayref of ordinates.
496
497If one array is shorter than another the final value in the shorter
498will be duplicated until they match in length.
499
500If only one of C<x> or C<y> is an array reference then setpixel() will
501behave as if the non-reference value were an array reference
502containing only that value.
503
504eg.
505
506 my $count = $img->setpixel(x => 1, y => [ 0 .. 3 ], color => $color);
507
508behaves like:
509
510 my $count = $img->setpixel(x => [ 1 ], y => [ 0 .. 3 ], color => $color);
511
512and since the final element in the shorter array is duplicated, this
513behaves like:
514
515 my $count = $img->setpixel(x => [ 1, 1, 1, 1 ], y => [ 0 .. 3 ],
516 color => $color);
517
08e6cbea
TC
518Parameters:
519
520=over
521
522=item *
523
524x, y - either integers giving the co-ordinates of the pixel to set or
525array references containing a set of pixels to be set.
526
527=item *
528
67d441b2 529color - the color of the pixels drawn. See L</"Color Parameters">.
08e6cbea
TC
530Default: white.
531
532=back
533
5daeb11a
TC
534Returns the number of pixels drawn, if no pixels were drawn, but none
535of the errors below occur, returns C<"0 but true">.
f2ad77de 536
5daeb11a 537For other errors, setpixel() returns an empty list and sets errstr().
f2ad77de 538
2a27eeff
TC
539Possible errors conditions include:
540
541=over
542
543=item * the image supplied is empty
544
545=item * a reference to an empty array was supplied for C<x> or C<y>
546
547=item * C<x> or C<y> wasn't supplied
548
549=item * C<color> isn't a valid color, and can't be converted to a
550color.
551
552=back
553
5715f7c3 554=item getpixel()
08e6cbea 555
2a27eeff
TC
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]);
591b5954 559
08e6cbea 560getpixel() is used to retrieve one or more individual pixels.
591b5954 561
2a27eeff
TC
562You can supply a single set of co-ordinates as scalar C<x> and C<y>
563parameters, or set each to an arrayref of ordinates.
564
565If one array is shorter than another the final value in the shorter
566will be duplicated until they match in length.
567
568If only one of C<x> or C<y> is an array reference then getpixel() will
569behave as if the non-reference value were an array reference
570containing only that value.
571
572eg.
573
574 my @colors = $img->getpixel(x => 0, y => [ 0 .. 3 ]);
591b5954 575
2a27eeff
TC
576behaves like:
577
578 my @colors = $img->getpixel(x => [ 0 ], y => [ 0 .. 3 ]);
579
580and since the final element in the shorter array is duplicated, this
581behaves like:
582
583 my @colors = $img->getpixel(x => [ 0, 0, 0, 0 ], y => [ 0 .. 3 ]);
591b5954 584
5715f7c3 585To receive floating point colors from getpixel(), set the C<type>
591b5954 586parameter to 'float'.
7144e056 587
08e6cbea
TC
588Parameters:
589
590=over
591
592=item *
593
ab53641f 594C<x>, C<y> - either integers giving the co-ordinates of the pixel to set or
08e6cbea
TC
595array references containing a set of pixels to be set.
596
597=item *
598
ab53641f
TC
599C<type> - the type of color object to return, either C<'8bit'> for
600L<Imager::Color> objects or C<'float'> for L<Imager::Color::Float>
601objects. Default: C<'8bit'>.
08e6cbea
TC
602
603=back
604
2a27eeff
TC
605When called with an array reference for either or C<x> or C<y>,
606getpixel() will return a list of colors in list context, and an
607arrayref in scalar context.
608
609If a supplied co-ordinate is outside the image then C<undef> is
610returned for the pixel.
611
ab53641f
TC
612Each color is returned as an L<Imager::Color> object or as an
613L<Imager::Color::Float> object if C<type> is set to C<"float">.
614
2a27eeff
TC
615Possible errors conditions include:
616
617=over
618
619=item * the image supplied is empty
620
621=item * a reference to an empty array was supplied for C<x> or C<y>
622
623=item * C<x> or C<y> wasn't supplied
624
625=item * C<type> isn't a valid value.
626
627=back
628
629For any of these errors getpixel() returns an empty list.
630
67d441b2 631=item string()
08e6cbea
TC
632
633 my $font = Imager::Font->new(file=>"foo.ttf");
634 $img->string(x => 50, y => 70,
635 string => "Hello, World!",
636 font => $font,
637 size => 30,
638 aa => 1,
639 color => 'white');
640
641Draws text on the image.
642
643Parameters:
644
645=over
646
647=item *
648
5715f7c3
TC
649C<x>, C<y> - the point to draw the text from. If C<align> is 0 this
650is the top left of the string. If C<align> is 1 (the default) then
651this is the left of the string on the baseline. Required.
08e6cbea
TC
652
653=item *
654
5715f7c3 655C<string> - the text to draw. Required unless you supply the C<text>
08e6cbea
TC
656parameter.
657
658=item *
659
5715f7c3 660C<font> - an L<Imager::Font> object representing the font to draw the
08e6cbea
TC
661text with. Required.
662
663=item *
664
5715f7c3 665C<aa> - if non-zero the output will be anti-aliased. Default: the value
08e6cbea
TC
666set in Imager::Font->new() or 0 if not set.
667
668=item *
669
5715f7c3 670C<align> - if non-zero the point supplied in (x,y) will be on the
08e6cbea
TC
671base-line, if zero then (x,y) will be at the top-left of the string.
672
5715f7c3 673i.e. if drawing the string C<"yA"> and align is 0 the point (x,y) will
08e6cbea
TC
674aligned with the top of the A. If align is 1 (the default) it will be
675aligned with the baseline of the font, typically bottom of the A,
676depending on the font used.
677
678Default: the value set in Imager::Font->new, or 1 if not set.
679
680=item *
681
5715f7c3 682C<channel> - if present, the text will be written to the specified
08e6cbea
TC
683channel of the image and the color parameter will be ignore.
684
685=item *
686
5715f7c3 687C<color> - the color to draw the text in. Default: the color supplied to
08e6cbea
TC
688Imager::Font->new, or red if none.
689
690=item *
691
5715f7c3
TC
692C<size> - the point size to draw the text at. Default: the size
693supplied to Imager::Font->new, or 15.
08e6cbea
TC
694
695=item *
696
5715f7c3
TC
697C<sizew> - the width scaling to draw the text at. Default: the value
698of C<size>.
08e6cbea
TC
699
700=item *
701
5715f7c3
TC
702C<utf8> - for drivers that support it, treat the string as UTF-8
703encoded. For versions of perl that support Unicode (5.6 and later),
704this will be enabled automatically if the C<string> parameter is
67d441b2 705already a UTF-8 string. See L<Imager::Font/"UTF-8"> for more
5715f7c3 706information.
08e6cbea
TC
707
708=item *
709
5715f7c3 710C<vlayout> - for drivers that support it, draw the text vertically.
08e6cbea
TC
711Note: I haven't found a font that has the appropriate metrics yet.
712
713=item *
714
5715f7c3 715C<text> - alias for the C<string> parameter.
08e6cbea
TC
716
717=back
718
719On error, string() returns false and you can use $img->errstr to get
720the reason for the error.
721
67d441b2 722=item align_string()
08e6cbea
TC
723
724Draws text aligned around a point on the image.
725
726 # "Hello" centered at 100, 100 in the image.
727 my ($left, $top, $right, $bottom) =
728 $img->align_string(string=>"Hello",
729 x=>100, y=>100,
730 halign=>'center', valign=>'center',
731 font=>$font);
732
733Parameters:
734
735=over
736
737=item *
738
5715f7c3
TC
739C<x>, C<y> - the point to draw the text from. If C<align> is 0 this
740is the top left of the string. If C<align> is 1 (the default) then
741this is the left of the string on the baseline. Required.
08e6cbea
TC
742
743=item *
744
5715f7c3
TC
745C<string> - the text to draw. Required unless you supply the C<text>
746parameter.
08e6cbea
TC
747
748=item *
749
5715f7c3 750C<font> - an L<Imager::Font> object representing the font to draw the
08e6cbea
TC
751text with. Required.
752
753=item *
754
5715f7c3 755C<aa> - if non-zero the output will be anti-aliased
08e6cbea
TC
756
757=item *
758
5715f7c3 759C<valign> - vertical alignment of the text against (x,y)
08e6cbea
TC
760
761=over
762
763=item *
764
5715f7c3 765C<top> - Point is at the top of the text.
08e6cbea
TC
766
767=item *
768
5715f7c3 769C<bottom> - Point is at the bottom of the text.
08e6cbea
TC
770
771=item *
772
5715f7c3 773C<baseline> - Point is on the baseline of the text. This is the default.
08e6cbea
TC
774
775=item *
776
5715f7c3 777C<center> - Point is vertically centered within the text.
08e6cbea
TC
778
779=back
780
781=item *
782
5715f7c3 783C<halign> - horizontal alignment of the text against (x,y)
08e6cbea
TC
784
785=over
786
787=item *
788
5715f7c3 789C<left> - The point is at the left of the text. This is the default.
08e6cbea
TC
790
791=item *
792
5715f7c3 793C<start> - The point is at the start point of the text.
08e6cbea
TC
794
795=item *
796
5715f7c3 797C<center> - The point is horizontally centered within the text.
08e6cbea
TC
798
799=item *
800
5715f7c3 801C<right> - The point is at the right end of the text.
08e6cbea
TC
802
803=item *
804
5715f7c3 805C<end> - The point is at the end point of the text.
08e6cbea
TC
806
807=back
808
809=item *
810
5715f7c3 811C<channel> - if present, the text will be written to the specified
08e6cbea
TC
812channel of the image and the color parameter will be ignore.
813
814=item *
815
5715f7c3 816C<color> - the color to draw the text in. Default: the color supplied to
08e6cbea
TC
817Imager::Font->new, or red if none.
818
819=item *
820
5715f7c3 821C<size> - the point size to draw the text at. Default: the size supplied
08e6cbea
TC
822to Imager::Font->new, or 15.
823
824=item *
825
5715f7c3 826C<sizew> - the width scaling to draw the text at. Default: the value of
08e6cbea
TC
827C<size>.
828
829=item *
830
5715f7c3
TC
831C<utf8> - for drivers that support it, treat the string as UTF-8
832encoded. For versions of perl that support Unicode (5.6 and later),
833this will be enabled automatically if the C<string> parameter is
834already a UTF-8 string. See L<Imager::Font/"UTF-8"> for more
835information.
08e6cbea
TC
836
837=item *
838
5715f7c3 839C<vlayout> - for drivers that support it, draw the text vertically.
08e6cbea
TC
840Note: I haven't found a font that has the appropriate metrics yet.
841
842=item *
843
5715f7c3 844C<text> - alias for the C<string> parameter.
08e6cbea
TC
845
846=back
847
848On success returns a list of bounds of the drawn text, in the order
849left, top, right, bottom.
850
5715f7c3
TC
851On error, align_string() returns an empty list and you can use
852C<< $img->errstr >> to get the reason for the error.
08e6cbea 853
5715f7c3 854=item setscanline()
ca4d914e
TC
855
856Set all or part of a horizontal line of pixels to an image. This
67d441b2 857method is most useful in conjunction with L</getscanline()>.
ca4d914e
TC
858
859The parameters you can pass are:
860
861=over
862
863=item *
864
5715f7c3 865C<y> - vertical position of the scan line. This parameter is required.
ca4d914e
TC
866
867=item *
868
5715f7c3 869C<x> - position to start on the scan line. Default: 0
ca4d914e
TC
870
871=item *
872
5715f7c3 873C<pixels> - either a reference to an array containing Imager::Color
ca4d914e
TC
874objects, an reference to an array containing Imager::Color::Float
875objects or a scalar containing packed color data.
876
4cda4e76
TC
877If C<type> is C<index> then this can either be a reference to an array
878of palette color indexes or a scalar containing packed indexes.
879
ca4d914e
TC
880See L</"Packed Color Data"> for information on the format of packed
881color data.
882
883=item *
884
5715f7c3 885C<type> - the type of pixel data supplied. If you supply an array
78660b6c
TC
886reference then this is determined automatically. If you supply packed
887color data this defaults to C<'8bit'>, if your data is packed floating
888point color data then you need to set this to C<'float'>.
ca4d914e 889
5715f7c3 890You can use C<float> or C<8bit> samples with any image.
ca4d914e 891
78660b6c
TC
892If this is C<index> then C<pixels> should be either an array of
893palette color indexes or a packed string of color indexes.
4cda4e76 894
ca4d914e
TC
895=back
896
897Returns the number of pixels set.
898
899Each of the following sets 5 pixels from (5, 10) through (9, 10) to
900blue, red, blue, red, blue:
901
902 my $red_color = Imager::Color->new(255, 0, 0);
903 my $blue_color = Imager::Color->new(0, 0, 255);
904
905 $image->setscanline(y=>10, x=>5, pixels=>
906 [ ($blue_color, $red_color) x 2, $blue_color ]);
907
908 # use floating point color instead, for 16-bit plus images
909 my $red_colorf = Imager::Color::Float->new(1.0, 0, 0);
910 my $blue_colorf = Imager::Color::Float->new(0, 0, 1.0);
911
912 $image->setscanline(y=>10, x=>5, pixels=>
913 [ ($blue_colorf, $red_colorf) x 2, $blue_colorf ]);
914
915 # packed 8-bit data
916 $image->setscanline(y=>10, x=>5, pixels=>
917 pack("C*", ((0, 0, 255, 255), (255, 0, 0, 255)) x 2,
918 (0, 0, 255, 255)));
919
920 # packed floating point samples
921 $image->setscanline(y=>10, x=>5, type=>'float', pixels=>
922 pack("d*", ((0, 0, 1.0, 1.0), (1.0, 0, 0, 1.0)) x 2,
923 (0, 0, 1.0, 1.0)));
924
925
926Copy even rows from one image to another:
927
928 for (my $y = 0; $y < $im2->getheight; $y+=2) {
929 $im1->setscanline(y=>$y,
930 pixels=>scalar($im2->getscanline(y=>$y)));
931 }
932
933
934Set the blue channel to 0 for all pixels in an image. This could be
935done with convert too:
936
937 for my $y (0..$im->getheight-1) {
938 my $row = $im->getscanline(y=>$y);
939 $row =~ s/(..).(.)/$1\0$2/gs;
940 $im->setscanline(y=>$y, pixels=>$row);
941 }
942
5715f7c3 943=item getscanline()
ca4d914e 944
51c27309 945Read all or part of a horizontal line of pixels from an image. This
67d441b2 946method is most useful in conjunction with L</setscanline()>.
ca4d914e
TC
947
948The parameters you can pass are:
949
950=over
951
952=item *
953
5715f7c3 954C<y> - vertical position of the scan line. This parameter is required.
ca4d914e
TC
955
956=item *
957
5715f7c3 958C<x> - position to start on the scan line. Default: 0
ca4d914e
TC
959
960=item *
961
5715f7c3 962C<width> - number of pixels to read. Default: $img->getwidth - x
ca4d914e
TC
963
964=item *
965
5715f7c3 966C<type> - the type of pixel data to return. Default: C<8bit>.
ca4d914e 967
5715f7c3 968Permitted values are C<8bit> and C<float> and C<index>.
ca4d914e
TC
969
970=back
971
972In list context this method will return a list of Imager::Color
973objects when I<type> is C<8bit>, or a list of Imager::Color::Float
4cda4e76
TC
974objects when I<type> if C<float>, or a list of integers when I<type>
975is C<index>.
ca4d914e
TC
976
977In scalar context this returns a packed 8-bit pixels when I<type> is
978C<8bit>, or a list of packed floating point pixels when I<type> is
4cda4e76 979C<float>, or packed palette color indexes when I<type> is C<index>.
ca4d914e
TC
980
981The values of samples for which the image does not have channels is
982undefined. For example, for a single channel image the values of
983channels 1 through 3 are undefined.
984
985Check image for a given color:
986
987 my $found;
988 YLOOP: for my $y (0..$img->getheight-1) {
989 my @colors = $img->getscanline(y=>$y);
990 for my $color (@colors) {
991 my ($red, $green, $blue, $alpha) = $color->rgba;
992 if ($red == $test_red && $green == $test_green && $blue == $test_blue
993 && $alpha == $test_alpha) {
994 ++$found;
995 last YLOOP;
996 }
997 }
998 }
999
1000Or do it using packed data:
1001
1002 my $found;
1003 my $test_packed = pack("CCCC", $test_red, $test_green, $test_blue,
1004 $test_alpha);
1005 YLOOP: for my $y (0..$img->getheight-1) {
1006 my $colors = $img->getscanline(y=>$y);
1007 while (length $colors) {
1008 if (substr($colors, 0, 4, '') eq $test_packed) {
1009 ++$found;
1010 last YLOOP;
1011 }
1012 }
1013 }
1014
67d441b2 1015Some of the examples for L</setscanline()> for more examples.
ca4d914e 1016
5715f7c3 1017=item getsamples()
ca4d914e
TC
1018
1019Read specified channels from all or part of a horizontal line of
1020pixels from an image.
1021
1022The parameters you can pass are:
1023
1024=over
1025
1026=item *
1027
5715f7c3 1028C<y> - vertical position of the scan line. This parameter is required.
ca4d914e
TC
1029
1030=item *
1031
5715f7c3 1032C<x> - position to start on the scan line. Default: 0
ca4d914e
TC
1033
1034=item *
1035
5715f7c3 1036C<width> - number of pixels to read. Default: C<< $img->getwidth - x >>
ca4d914e
TC
1037
1038=item *
1039
5715f7c3 1040C<type> - the type of sample data to return. Default: C<8bit>.
ca4d914e 1041
5715f7c3 1042Permitted values are C<8bit> and C<float>.
ca4d914e 1043
bd8052a6
TC
1044As of Imager 0.61 this can be C<16bit> only for 16 bit images.
1045
ca4d914e
TC
1046=item *
1047
5715f7c3
TC
1048C<channels> - a reference to an array of channels to return, where 0
1049is the first channel. Default: C<< [ 0 .. $self->getchannels()-1 ] >>
ca4d914e 1050
bd8052a6
TC
1051=item *
1052
5715f7c3 1053C<target> - if an array reference is supplied in target then the samples
bd8052a6
TC
1054will be stored here instead of being returned.
1055
1056=item *
1057
5715f7c3 1058C<offset> - the offset within the array referenced by I<target>
bd8052a6 1059
ca4d914e
TC
1060=back
1061
1062In list context this will return a list of integers between 0 and 255
1063inclusive when I<type> is C<8bit>, or a list of floating point numbers
1064between 0.0 and 1.0 inclusive when I<type> is C<float>.
1065
1066In scalar context this will return a string of packed bytes, as with
1067C< pack("C*", ...) > when I<type> is C<8bit> or a string of packed
1068doubles as with C< pack("d*", ...) > when I<type> is C<float>.
1069
bd8052a6
TC
1070If the I<target> option is supplied then only a count of samples is
1071returned.
1072
ca4d914e
TC
1073Example: Check if any pixels in an image have a non-zero alpha
1074channel:
1075
1076 my $has_coverage;
1077 for my $y (0 .. $img->getheight()-1) {
1078 my $alpha = $img->getsamples(y=>$y, channels=>[0]);
1079 if ($alpha =~ /[^\0]/) {
1080 ++$has_coverage;
1081 last;
1082 }
1083 }
1084
5715f7c3 1085Example: Convert a 2 channel gray image into a 4 channel RGBA image:
ca4d914e
TC
1086
1087 # this could be done with convert() instead
1088 my $out = Imager->new(xsize => $src->getwidth(),
1089 ysize => $src->getheight(),
1090 channels => 4);
1091 for my $y ( 0 .. $src->getheight()-1 ) {
1092 my $data = $src->getsamples(y=>$y, channels=>[ 0, 0, 0, 1 ]);
1093 $out->setscanline(y=>$y, pixels=>$data);
1094 }
1095
bd8052a6
TC
1096Retrieve 16-bit samples:
1097
1098 if ($img->bits == 16) {
1099 my @samples;
1100 $img->getsamples(x => 0, y => $y, target => \@samples, type => '16bit');
1101 }
1102
5715f7c3 1103=item setsamples()
bd8052a6 1104
78660b6c 1105This allows writing of samples to an image.
bd8052a6
TC
1106
1107Parameters:
1108
1109=over
1110
1111=item *
1112
5715f7c3 1113C<y> - vertical position of the scan line. This parameter is required.
bd8052a6
TC
1114
1115=item *
1116
5715f7c3 1117C<x> - position to start on the scan line. Default: 0
bd8052a6
TC
1118
1119=item *
1120
5715f7c3
TC
1121C<width> - number of pixels to write. Default: C<< $img->getwidth - x >>.
1122The minimum of this and the number of pixels represented by the
1123samples provided will be written.
bd8052a6
TC
1124
1125=item *
1126
5715f7c3 1127C<type> - the type of sample data to write. This parameter is required.
bd8052a6 1128
0c78633e 1129This can be C<8bit>, C<float> or for 16-bit images only, C<16bit>.
bd8052a6
TC
1130
1131=item *
1132
5715f7c3 1133C<channels> - a reference to an array of channels to return, where 0 is
de3ca2fd 1134the first channel. Default: C<< [ 0 .. $self->getchannels()-1 ] >>
bd8052a6
TC
1135
1136=item *
1137
0c78633e
TC
1138C<data> - for a type of C<8bit> or C<float> this can be a reference to
1139an array of samples or a scalar containing packed samples. If C<data>
1140is a scalar it may only contain characters from \x00 to \xFF.
1141
1142For a type of C<16bit> this can only be a reference to an array of
1143samples to write.
1144
1145Required.
bd8052a6
TC
1146
1147=item *
1148
0c78633e
TC
1149C<offset> - the starting offset within the array referenced by
1150I<data>. If C<data> is a scalar containing packed samples this offset
1151is in samples.
bd8052a6
TC
1152
1153=back
1154
1155Returns the number of samples written.
1156
0c78633e
TC
1157 $targ->setsamples(y => $y, data => \@data);
1158
1159 $targ->setsamples(y => $y, data => \@data, offset => $src->getchannels);
1160
1161Copy from one image to another:
1162
1163 my $targ = Imager->new(xsize => $src->getwidth,
1164 ysize => $src->getheight, channels => $src->getchannels);
1165 for my $y (0 .. $targ->getheight()-1) {
1166 my $row = $src->getsamples(y => $y)
1167 or die $src->errstr;
1168 $targ->setsamples(y => $y, data => $row)
1169 or die $targ->errstr;;
1170 }
1171
1172Compose an image from separate source channels:
1173
1174 my @src = ...; # images to work from, up to 4
1175 my $targ = Imager->new(xsize => $src[0]->getwidth,
1176 ysize => $src[0]->getheight, channels => scalar(@src));
1177 for my $y (0 .. $targ->getheight()-1) {
1178 for my $ch (0 .. $#src) {
1179 my $row = $src[$ch]->getsamples(y => $y, channels => [ 0 ]);
1180 $targ->setsamples(y => $y, data => $row, channels => [ $ch ] );
1181 }
1182 }
1183
ca4d914e
TC
1184=back
1185
1186=head1 Packed Color Data
1187
0c78633e 1188The getscanline() and setscanline() methods can work with pixels
ca4d914e
TC
1189packed into scalars. This is useful to remove the cost of creating
1190color objects, but should only be used when performance is an issue.
1191
0c78633e
TC
1192The getsamples() and setsamples() methods can work with samples packed
1193into scalars.
1194
ca4d914e
TC
1195Packed data can either be 1 byte per sample or 1 double per sample.
1196
1197Each pixel returned by getscanline() or supplied to setscanline()
1198contains 4 samples, even if the image has fewer then 4 channels. The
1199values of the extra samples as returned by getscanline() is not
1200specified. The extra samples passed to setscanline() are ignored.
1201
1202To produce packed 1 byte/sample pixels, use the pack C<C> template:
1203
1204 my $packed_8bit_pixel = pack("CCCC", $red, $blue, $green, $alpha);
1205
1206To produce packed double/sample pixels, use the pack C<d> template:
1207
1208 my $packed_float_pixel = pack("dddd", $red, $blue, $green, $alpha);
1209
78660b6c
TC
1210Note that double/sample data is always stored using the C C<double>
1211type, never C<long double>, even if C<perl> is built with
1212C<-Duselongdouble>.
1213
4cda4e76
TC
1214If you use a I<type> parameter of C<index> then the values are palette
1215color indexes, not sample values:
1216
1217 my $im = Imager->new(xsize => 100, ysize => 100, type => 'paletted');
1218 my $black_index = $im->addcolors(colors => [ 'black' ]);
1219 my $red_index = $im->addcolors(colors => [ 'red' ]);
1220 # 2 pixels
1221 my $packed_index_data = pack("C*", $black_index, $red_index);
1222 $im->setscanline(y => $y, pixels => $packed_index_data, type => 'index');
1223
9b1ec2b8
TC
1224=head1 Combine Types
1225
1226Some methods accept a C<combine> parameter, this can be any of the
1227following:
1228
1229=over
1230
5715f7c3 1231=item C<none>
9b1ec2b8
TC
1232
1233The fill pixel replaces the target pixel.
1234
5715f7c3 1235=item C<normal>
9b1ec2b8
TC
1236
1237The fill pixels alpha value is used to combine it with the target pixel.
1238
5715f7c3 1239=item C<multiply>
9b1ec2b8 1240
5715f7c3 1241=item C<mult>
9b1ec2b8
TC
1242
1243Each channel of fill and target is multiplied, and the result is
1244combined using the alpha channel of the fill pixel.
1245
5715f7c3 1246=item C<dissolve>
9b1ec2b8
TC
1247
1248If the alpha of the fill pixel is greater than a random number, the
1249fill pixel is alpha combined with the target pixel.
1250
5715f7c3 1251=item C<add>
9b1ec2b8
TC
1252
1253The channels of the fill and target are added together, clamped to the range of the samples and alpha combined with the target.
1254
5715f7c3 1255=item C<subtract>
9b1ec2b8
TC
1256
1257The channels of the fill are subtracted from the target, clamped to be
1258>= 0, and alpha combined with the target.
1259
5715f7c3 1260=item C<diff>
9b1ec2b8
TC
1261
1262The channels of the fill are subtracted from the target and the
1263absolute value taken this is alpha combined with the target.
1264
5715f7c3 1265=item C<lighten>
9b1ec2b8
TC
1266
1267The higher value is taken from each channel of the fill and target
1268pixels, which is then alpha combined with the target.
1269
5715f7c3 1270=item C<darken>
9b1ec2b8
TC
1271
1272The higher value is taken from each channel of the fill and target
1273pixels, which is then alpha combined with the target.
1274
5715f7c3 1275=item C<hue>
9b1ec2b8
TC
1276
1277The combination of the saturation and value of the target is combined
1278with the hue of the fill pixel, and is then alpha combined with the
1279target.
1280
5715f7c3 1281=item C<sat>
9b1ec2b8
TC
1282
1283The combination of the hue and value of the target is combined
1284with the saturation of the fill pixel, and is then alpha combined with the
1285target.
1286
5715f7c3 1287=item C<value>
9b1ec2b8
TC
1288
1289The combination of the hue and value of the target is combined
1290with the value of the fill pixel, and is then alpha combined with the
1291target.
1292
5715f7c3 1293=item C<color>
9b1ec2b8
TC
1294
1295The combination of the value of the target is combined with the hue
1296and saturation of the fill pixel, and is then alpha combined with the
1297target.
1298
1299=back
1300
1301=over
1302
5715f7c3 1303=item combines()
9b1ec2b8
TC
1304
1305Returns a list of possible combine types.
1306
1307=back
1308
bac4fcee
AMH
1309=head1 BUGS
1310
5715f7c3
TC
1311box() does not support anti-aliasing yet. Default color is not
1312unified yet.
bac4fcee 1313
08e6cbea
TC
1314=head1 AUTHOR
1315
5b480b14 1316Tony Cook <tonyc@cpan.org>, Arnar M. Hrafnkelsson.
08e6cbea 1317
ed9e5812
TC
1318=head1 SEE ALSO
1319
08e6cbea
TC
1320L<Imager>(3), L<Imager::Cookbook>(3)
1321
1322=head1 REVISION
1323
1324$Revision$
ed9e5812 1325
f75c1aeb 1326=cut