Debian Codegraph @

lib/Imager/Draw.pod

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