]> git.imager.perl.org - imager.git/blame - lib/Imager/Draw.pod
document setpixel return values
[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
TC
76corner of an image with co-ordinates increasing to the right and
77bottom. For non antialiasing operation all coordinates are rounded
78towards the nearest integer. For antialiased 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
83it is necessary to check for off-by-one cases. Typically it's usefull to
84think of C<(10, 20)> as C<(10.00, 20.00)> and consider the consiquences.
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
08e6cbea
TC
98=head2 Fill Parameters
99
100X<fill parameters>All filled primitives, i.e. C<arc()>, C<box()>,
101C<circle()>, C<polygon()> and the C<flood_fill()> method can take a
102C<fill> parameter instead of a C<color> parameter which can either be
103an Imager::Fill object, or a reference to a hash containing the
ed9e5812
TC
104parameters used to create the fill, for example:
105
106 $image->box(..., fill=>{ hatch => 'check1x1' });
107 my $fillimage = Imager->new;
108 $fillimage->read(file=>$somefile) or die;
109 $image->flood_fill(..., fill=>{ image=>$fillimage });
7144e056
AMH
110
111Currently you can create opaque or transparent plain color fills,
112hatched fills, image based fills and fountain fills. See
113L<Imager::Fill> for more information.
114
115=head2 List of primitives
116
117=over
118
119=item line
120
121 $img->line(color=>$green, x1=>10, x2=>100,
aa833c97 122 y1=>20, y2=>50, aa=>1, endp=>1 );
7144e056 123
08e6cbea
TC
124X<line method>Draws a line from (x1,y1) to (x2,y2). The endpoint
125(x2,y2) is drawn by default. If endp of 0 is specified then the
126endpoint will not be drawn. If C<aa> is set then the line will be
127drawn antialiased. The I<antialias> parameter is still available for
128backwards compatibility.
129
130Parameters:
131
132=over
133
134=item *
135
136x1, y1 - starting point of the line. Required.
137
138=item *
139
140x2, y2 - end point of the line. Required.
141
142=item *
143
144color - the color of the line. See L<"Color Parameters">. Default:
145black.
146
147=item *
148
149endp - if zero the end point of the line is not drawn. Default: 1 -
150the end point is drawn. This is useful to set to 0 when drawning a
151series of connected lines.
152
153=item *
154
155aa - if true the line is drawn anti-aliased. Default: 0.
156
157=back
7144e056
AMH
158
159=item polyline
160
161 $img->polyline(points=>[[$x0,$y0],[$x1,$y1],[$x2,$y2]],color=>$red);
162 $img->polyline(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], aa=>1);
163
08e6cbea
TC
164X<polyline method>Polyline is used to draw multilple lines between a
165series of points. The point set can either be specified as an
166arrayref to an array of array references (where each such array
167represents a point). The other way is to specify two array
168references.
7144e056
AMH
169
170The I<antialias> parameter is still available for backwards compatibility.
171
08e6cbea
TC
172=over
173
174=item *
175
176points - a reference to an array of references to arrays containing
177the co-ordinates of the points in the line, for example:
178
179 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
180 $img->polyline(points => \@points);
181
182=item *
183
184x, y - each is an array of x or y ordinates. This is an alternative
185to supplying the C<points> parameter.
186
187 # same as the above points example
188 my @x = ( 0, 100, 100, 0 );
189 my @y = ( 0, 0, 100, 100 );
190 $img->polyline(x => \@x, y => \@y);
191
192=item *
193
194color - the color of the line. See L<"Color Parameters">. Default:
195black.
196
197=item *
198
199aa - if true the line is drawn anti-aliased. Default: 0. Can also be
200supplied as C<antialias> for backward compatibility.
201
202=back
203
7144e056
AMH
204=item box
205
206 $blue = Imager::Color->new( 0, 0, 255 );
ed9e5812
TC
207 $img->box(color => $blue, xmin=>10, ymin=>30, xmax=>200, ymax=>300,
208 filled=>1);
7144e056 209
08e6cbea
TC
210X<box method>If any of the edges of the box are ommited it will snap
211to the outer edge of the image in that direction. If C<filled> is
212ommited the box is drawn as an outline. Instead of a color it is
213possible to use a C<fill> pattern:
7144e056
AMH
214
215 $fill = Imager::Fill->new(hatch=>'stipple');
216 $img->box(fill=>$fill); # fill entire image with a given fill pattern
217
218 $img->box(xmin=>10, ymin=>30, xmax=>150, ymax=>60,
219 fill => { hatch=>'cross2' });
220
221Also if a color is omitted a color with (255,255,255,255) is used
55b287f5 222instead. [NOTE: This may change to use C<$img-E<gt>fgcolor()> in the future].
7144e056
AMH
223
224Box does not support fractional coordinates yet.
225
08e6cbea
TC
226Parameters:
227
228=over
229
230=item *
231
232xmin - left side of the box. Default: 0 (left edge of the image)
233
234=item *
235
236ymin - top side of the box. Default: 0 (top edge of the image)
237
238=item *
239
240xmax - right side of the box. Default: $img->getwidth-1. (right edge
241of the image)
242
243=item *
244
245ymax - bottom side of the box. Default: $img->getheight-1. (bottom
246edge of the image)
247
248Note: xmax and ymax are I<inclusive> - the number of pixels drawn for
249a filled box is (xmax-xmin+1) * (ymax-ymin+1).
250
251=item *
252
253box - a reference to an array of (left, top, right, bottom)
254co-ordinates. This is an alternative to supplying xmin, ymin, xmax,
255ymax and overrides their values.
256
257=item *
258
259color - the color of the line. See L<"Color Parameters">. Default:
260white. This is ignored if the filled parameter
261
262=item *
263
264filled - if non-zero the box is filled with I<color> instead of
265outlined. Default: an outline is drawn.
266
267=item *
268
269fill - the fill for the box. If this is supplied then the box will be
270filled. See L<"Fill Parameters">.
271
272=back
273
7144e056
AMH
274=item arc
275
9ecefd41 276 $img->arc(color=>$red, r=>20, x=>200, y=>100, d1=>10, d2=>20 );
7144e056
AMH
277
278This creates a filled red arc with a 'center' at (200, 100) and spans
27910 degrees and the slice has a radius of 20. [NOTE: arc has a BUG in
280it right now for large differences in angles.]
281It's also possible to supply a C<fill> parameter.
282
08e6cbea
TC
283Parameters:
284
285=over
286
287=item *
288
289x, y - center of the filled arc. Default: center of the image.
290
291=item *
292
293r - radius of the arc. Default: 1/3 of min(image height, image width).
294
295=item *
296
297d1 - starting angle of the arc, in degrees. Default: 0
298
299=item *
300
301d2 - ending angle of the arc, in degrees. Default: 361.
302
303=item *
304
305color - the color of the filled arc. See L<"Color Parameters">.
306Default: white. Overridden by C<fill>.
307
308=item *
309
310fill - the fill for the filled arc. See L<"Fill Parameters">
311
312=item *
313
314aa - if true the filled arc is drawn anti-aliased. Default: false.
315
316Anti-aliased arc() is experimental for now, I'm not entirely happy
317with the results in some cases.
318
319=back
320
321 # arc going through angle zero:
322 $img->arc(d1=>320, d2=>40, x=>100, y=>100, r=>50, color=>'blue');
323
324 # complex fill arc
325 $img->arc(d1=>135, d2=>45, x=>100, y=>150, r=>50,
326 fill=>{ solid=>'red', combine=>'diff' });
327
7144e056
AMH
328=item circle
329
08e6cbea 330 $img->circle(color=>$green, r=>50, x=>200, y=>100, aa=>1, filled=>1);
7144e056
AMH
331
332This creates an antialiased green circle with its center at (200, 100)
333and has a radius of 50. It's also possible to supply a C<fill> parameter
334instead of a color parameter.
335
08e6cbea
TC
336 $img->circle(r => 50, x=> 150, y => 150, fill=>{ hatch => 'stipple' });
337
7144e056
AMH
338The circle is always filled but that might change, so always pass a
339filled=>1 parameter if you want it to be filled.
340
08e6cbea
TC
341=over
342
343=item *
344
345x, y - center of the filled circle. Default: center of the image.
346
347=item *
348
349r - radius of the circle. Default: 1/3 of min(image height, image width).
350
351=item *
352
353color - the color of the filled circle. See L<"Color Parameters">.
354Default: white. Overridden by C<fill>.
355
356=item *
357
358fill - the fill for the filled circle. See L<"Fill Parameters">
359
360=item *
361
362aa - if true the filled circle is drawn anti-aliased. Default: false.
363
364=back
7144e056
AMH
365
366=item polygon
367
368 $img->polygon(points=>[[$x0,$y0],[$x1,$y1],[$x2,$y2]],color=>$red);
369 $img->polygon(x=>[$x0,$x1,$x2], y=>[$y0,$y1,$y2], fill=>$fill);
370
371Polygon is used to draw a filled polygon. Currently the polygon is
372always drawn antialiased, although that will change in the future.
373Like other antialiased drawing functions its coordinates can be
374specified with floating point values. As with other filled shapes
375it's possible to use a C<fill> instead of a color.
376
08e6cbea
TC
377=over
378
379=item *
380
381points - a reference to an array of references to arrays containing
382the co-ordinates of the points in the line, for example:
383
384 my @points = ( [ 0, 0 ], [ 100, 0 ], [ 100, 100 ], [ 0, 100 ] );
385 $img->polygon(points => \@points);
386
387=item *
388
389x, y - each is an array of x or y ordinates. This is an alternative
390to supplying the C<points> parameter.
391
392 # same as the above points example
393 my @x = ( 0, 100, 100, 0 );
394 my @y = ( 0, 0, 100, 100 );
395 $img->polygon(x => \@x, y => \@y);
396
397=item *
398
399color - the color of the filled polygon. See L<"Color Parameters">.
400Default: black. Overridden by C<fill>.
401
402=item *
403
404fill - the fill for the filled circle. See L<"Fill Parameters">
405
406=back
407
13fc481e 408=item flood_fill
7144e056 409
3efb0915
TC
410X<flood_fill>You can fill a region that all has the same color using
411the flood_fill() method, for example:
7144e056
AMH
412
413 $img->flood_fill(x=>50, y=>50, color=>$color);
414
415will fill all regions the same color connected to the point (50, 50).
416
3efb0915
TC
417Alternatively you can fill a region limited by a given border color:
418
419 # stop at the red border
420 $im->flood_fill(x=>50, y=>50, color=>$color, border=>"red");
421
ed9e5812
TC
422You can also fill with a complex fill:
423
424 $img->flood_fill(x=>50, y=>50, fill=>{ hatch=>'cross1x1' });
425
08e6cbea
TC
426Parameters:
427
428=over
429
430=item *
431
432x, y - the start point of the fill.
433
434=item *
435
436color - the color of the filled area. See L<"Color Parameters">.
437Default: white. Overridden by C<fill>.
438
439=item *
440
441fill - the fill for the filled area. See L<"Fill Parameters">
442
3efb0915
TC
443=item *
444
445border - the border color of the region to be filled. If this
446parameter is supplied flood_fill() will stop when it finds this color.
447If this is not supplied then a normal fill is done. C<border> can be
448supplied as a L<"Color Parameter">.
449
08e6cbea
TC
450=back
451
452=item setpixel
591b5954
TC
453
454 $img->setpixel(x=>50, y=>70, color=>$color);
455 $img->setpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40], color=>$color);
08e6cbea
TC
456
457setpixel() is used to set one or more individual pixels.
458
459Parameters:
460
461=over
462
463=item *
464
465x, y - either integers giving the co-ordinates of the pixel to set or
466array references containing a set of pixels to be set.
467
468=item *
469
470color - the color of the pixels drawn. See L<"Color Parameters">.
471Default: white.
472
473=back
474
f2ad77de
TC
475When called with array parameters, returns the number of pixels
476successfully set, or false if none.
477
478When called with scalars for x and y, return $img on success, false on
479failure.
480
08e6cbea
TC
481=item getpixel
482
591b5954
TC
483 my $color = $img->getpixel(x=>50, y=>70);
484 my @colors = $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]);
08e6cbea 485 my $colors_ref = $img->getpixel(x=>[ 50, 60, 70 ], y=>[20, 30, 40]);
591b5954 486
08e6cbea 487getpixel() is used to retrieve one or more individual pixels.
591b5954
TC
488
489For either method you can supply a single set of co-ordinates as
490scalar x and y parameters, or set each to an arrayref of ordinates.
491
492When called with arrays, getpixel() will return a list of colors in
493list context, and an arrayref in scalar context.
494
495To receive floating point colors from getpixel, set the C<type>
496parameter to 'float'.
7144e056 497
08e6cbea
TC
498Parameters:
499
500=over
501
502=item *
503
504x, y - either integers giving the co-ordinates of the pixel to set or
505array references containing a set of pixels to be set.
506
507=item *
508
509type - the type of color object to return, either C<'8bit'> for
510Imager::Color objects or C<'float'> for Imager::Color::Float objects.
511Default: C<'8bit'>.
512
513=back
514
515=item string
516
517 my $font = Imager::Font->new(file=>"foo.ttf");
518 $img->string(x => 50, y => 70,
519 string => "Hello, World!",
520 font => $font,
521 size => 30,
522 aa => 1,
523 color => 'white');
524
525Draws text on the image.
526
527Parameters:
528
529=over
530
531=item *
532
533x, y - the point to draw the text from. If C<align> is 0 this is the
534top left of the string. If C<align> is 1 (the default) then this is
535the left of the string on the baseline. Required.
536
537=item *
538
539string - the text to draw. Required unless you supply the C<text>
540parameter.
541
542=item *
543
544font - an L<Imager::Font> object representing the font to draw the
545text with. Required.
546
547=item *
548
549aa - if non-zero the output will be anti-aliased. Default: the value
550set in Imager::Font->new() or 0 if not set.
551
552=item *
553
554align - if non-zero the point supplied in (x,y) will be on the
555base-line, if zero then (x,y) will be at the top-left of the string.
556
557ie. if drawing the string "yA" and align is 0 the point (x,y) will
558aligned with the top of the A. If align is 1 (the default) it will be
559aligned with the baseline of the font, typically bottom of the A,
560depending on the font used.
561
562Default: the value set in Imager::Font->new, or 1 if not set.
563
564=item *
565
566channel - if present, the text will be written to the specified
567channel of the image and the color parameter will be ignore.
568
569=item *
570
571color - the color to draw the text in. Default: the color supplied to
572Imager::Font->new, or red if none.
573
574=item *
575
576size - the point size to draw the text at. Default: the size supplied
577to Imager::Font->new, or 15.
578
579=item *
580
581sizew - the width scaling to draw the text at. Default: the value of
582C<size>.
583
584=item *
585
586utf8 - for drivers that support it, treat the string as UTF8 encoded.
587For versions of perl that support Unicode (5.6 and later), this will
588be enabled automatically if the C<string> parameter is already a UTF8
589string. See L<Imager::Font/"UTF8"> for more information.
590
591=item *
592
593vlayout - for drivers that support it, draw the text vertically.
594Note: I haven't found a font that has the appropriate metrics yet.
595
596=item *
597
598text - alias for the C<string> parameter.
599
600=back
601
602On error, string() returns false and you can use $img->errstr to get
603the reason for the error.
604
605=item align_string
606
607Draws text aligned around a point on the image.
608
609 # "Hello" centered at 100, 100 in the image.
610 my ($left, $top, $right, $bottom) =
611 $img->align_string(string=>"Hello",
612 x=>100, y=>100,
613 halign=>'center', valign=>'center',
614 font=>$font);
615
616Parameters:
617
618=over
619
620=item *
621
622x, y - the point to draw the text from. If C<align> is 0 this is the
623top left of the string. If C<align> is 1 (the default) then this is
624the left of the string on the baseline. Required.
625
626=item *
627
628string - the text to draw. Required unless you supply the C<text> parameter.
629
630=item *
631
632font - an L<Imager::Font> object representing the font to draw the
633text with. Required.
634
635=item *
636
637aa - if non-zero the output will be anti-aliased
638
639=item *
640
641valign - vertical alignment of the text against (x,y)
642
643=over
644
645=item *
646
647top - Point is at the top of the text.
648
649=item *
650
651bottom - Point is at the bottom of the text.
652
653=item *
654
655baseline - Point is on the baseline of the text. This is the default.
656
657=item *
658
659center - Point is vertically centered within the text.
660
661=back
662
663=item *
664
665halign - horizontal alignment of the text against (x,y)
666
667=over
668
669=item *
670
671left - The point is at the left of the text. This is the default.
672
673=item *
674
675start - The point is at the start point of the text.
676
677=item *
678
679center - The point is horizontally centered within the text.
680
681=item *
682
683right - The point is at the right end of the text.
684
685=item *
686
687end - The point is at the end point of the text.
688
689=back
690
691=item *
692
693channel - if present, the text will be written to the specified
694channel of the image and the color parameter will be ignore.
695
696=item *
697
698color - the color to draw the text in. Default: the color supplied to
699Imager::Font->new, or red if none.
700
701=item *
702
703size - the point size to draw the text at. Default: the size supplied
704to Imager::Font->new, or 15.
705
706=item *
707
708sizew - the width scaling to draw the text at. Default: the value of
709C<size>.
710
711=item *
712
713utf8 - for drivers that support it, treat the string as UTF8 encoded.
714For versions of perl that support Unicode (5.6 and later), this will
715be enabled automatically if the C<string> parameter is already a UTF8
716string. See L<Imager::Font/"UTF8"> for more information.
717
718=item *
719
720vlayout - for drivers that support it, draw the text vertically.
721Note: I haven't found a font that has the appropriate metrics yet.
722
723=item *
724
725text - alias for the C<string> parameter.
726
727=back
728
729On success returns a list of bounds of the drawn text, in the order
730left, top, right, bottom.
731
732On error, align_string() returns an empty list and you can use
733$img->errstr to get the reason for the error.
734
ca4d914e
TC
735=item setscanline
736
737Set all or part of a horizontal line of pixels to an image. This
738method is most useful in conjuction with L</getscanline>.
739
740The parameters you can pass are:
741
742=over
743
744=item *
745
746y - vertical position of the scanline. This parameter is required.
747
748=item *
749
750x - position to start on the scanline. Default: 0
751
752=item *
753
754pixels - either a reference to an array containing Imager::Color
755objects, an reference to an array containing Imager::Color::Float
756objects or a scalar containing packed color data.
757
4cda4e76
TC
758If C<type> is C<index> then this can either be a reference to an array
759of palette color indexes or a scalar containing packed indexes.
760
ca4d914e
TC
761See L</"Packed Color Data"> for information on the format of packed
762color data.
763
764=item *
765
766type - the type of pixel data supplied. If you supply an array
767reference of object then this is determined automatically. If you
768supply packed color data this defaults to '8bit', if your data is
769packed floating point color data then set this to 'float'.
770
771You can use float or 8bit samples with any image.
772
4cda4e76
TC
773If this is 'index' then pixels should be either an array of palette
774color indexes or a packed string of color indexes.
775
ca4d914e
TC
776=back
777
778Returns the number of pixels set.
779
780Each of the following sets 5 pixels from (5, 10) through (9, 10) to
781blue, red, blue, red, blue:
782
783 my $red_color = Imager::Color->new(255, 0, 0);
784 my $blue_color = Imager::Color->new(0, 0, 255);
785
786 $image->setscanline(y=>10, x=>5, pixels=>
787 [ ($blue_color, $red_color) x 2, $blue_color ]);
788
789 # use floating point color instead, for 16-bit plus images
790 my $red_colorf = Imager::Color::Float->new(1.0, 0, 0);
791 my $blue_colorf = Imager::Color::Float->new(0, 0, 1.0);
792
793 $image->setscanline(y=>10, x=>5, pixels=>
794 [ ($blue_colorf, $red_colorf) x 2, $blue_colorf ]);
795
796 # packed 8-bit data
797 $image->setscanline(y=>10, x=>5, pixels=>
798 pack("C*", ((0, 0, 255, 255), (255, 0, 0, 255)) x 2,
799 (0, 0, 255, 255)));
800
801 # packed floating point samples
802 $image->setscanline(y=>10, x=>5, type=>'float', pixels=>
803 pack("d*", ((0, 0, 1.0, 1.0), (1.0, 0, 0, 1.0)) x 2,
804 (0, 0, 1.0, 1.0)));
805
806
807Copy even rows from one image to another:
808
809 for (my $y = 0; $y < $im2->getheight; $y+=2) {
810 $im1->setscanline(y=>$y,
811 pixels=>scalar($im2->getscanline(y=>$y)));
812 }
813
814
815Set the blue channel to 0 for all pixels in an image. This could be
816done with convert too:
817
818 for my $y (0..$im->getheight-1) {
819 my $row = $im->getscanline(y=>$y);
820 $row =~ s/(..).(.)/$1\0$2/gs;
821 $im->setscanline(y=>$y, pixels=>$row);
822 }
823
824=item getscanline
825
826Read all or part of a horizonatal line of pixels from an image. This
827method is most useful in conjunction with L</setscanline>.
828
829The parameters you can pass are:
830
831=over
832
833=item *
834
835y - vertical position of the scanline. This parameter is required.
836
837=item *
838
839x - position to start on the scanline. Default: 0
840
841=item *
842
843width - number of pixels to read. Default: $img->getwidth - x
844
845=item *
846
847type - the type of pixel data to return. Default: C<8bit>.
848
4cda4e76 849Permited values are C<8bit> and C<float> and C<index>.
ca4d914e
TC
850
851=back
852
853In list context this method will return a list of Imager::Color
854objects when I<type> is C<8bit>, or a list of Imager::Color::Float
4cda4e76
TC
855objects when I<type> if C<float>, or a list of integers when I<type>
856is C<index>.
ca4d914e
TC
857
858In scalar context this returns a packed 8-bit pixels when I<type> is
859C<8bit>, or a list of packed floating point pixels when I<type> is
4cda4e76 860C<float>, or packed palette color indexes when I<type> is C<index>.
ca4d914e
TC
861
862The values of samples for which the image does not have channels is
863undefined. For example, for a single channel image the values of
864channels 1 through 3 are undefined.
865
866Check image for a given color:
867
868 my $found;
869 YLOOP: for my $y (0..$img->getheight-1) {
870 my @colors = $img->getscanline(y=>$y);
871 for my $color (@colors) {
872 my ($red, $green, $blue, $alpha) = $color->rgba;
873 if ($red == $test_red && $green == $test_green && $blue == $test_blue
874 && $alpha == $test_alpha) {
875 ++$found;
876 last YLOOP;
877 }
878 }
879 }
880
881Or do it using packed data:
882
883 my $found;
884 my $test_packed = pack("CCCC", $test_red, $test_green, $test_blue,
885 $test_alpha);
886 YLOOP: for my $y (0..$img->getheight-1) {
887 my $colors = $img->getscanline(y=>$y);
888 while (length $colors) {
889 if (substr($colors, 0, 4, '') eq $test_packed) {
890 ++$found;
891 last YLOOP;
892 }
893 }
894 }
895
896Some of the examples for L</setscanline> for more examples.
897
898=item getsamples
899
900Read specified channels from all or part of a horizontal line of
901pixels from an image.
902
903The parameters you can pass are:
904
905=over
906
907=item *
908
909y - vertical position of the scanline. This parameter is required.
910
911=item *
912
913x - position to start on the scanline. Default: 0
914
915=item *
916
917width - number of pixels to read. Default: $img->getwidth - x
918
919=item *
920
921type - the type of sample data to return. Default: C<8bit>.
922
923Permited values are C<8bit> and C<float>.
924
925=item *
926
927channels - a reference to an array of channels to return, where 0 is
928the first channel. Default: C< [ 0 .. $self->getchannels()-1 ] >
929
930=back
931
932In list context this will return a list of integers between 0 and 255
933inclusive when I<type> is C<8bit>, or a list of floating point numbers
934between 0.0 and 1.0 inclusive when I<type> is C<float>.
935
936In scalar context this will return a string of packed bytes, as with
937C< pack("C*", ...) > when I<type> is C<8bit> or a string of packed
938doubles as with C< pack("d*", ...) > when I<type> is C<float>.
939
940Example: Check if any pixels in an image have a non-zero alpha
941channel:
942
943 my $has_coverage;
944 for my $y (0 .. $img->getheight()-1) {
945 my $alpha = $img->getsamples(y=>$y, channels=>[0]);
946 if ($alpha =~ /[^\0]/) {
947 ++$has_coverage;
948 last;
949 }
950 }
951
952Example: Convert a 2 channel grey image into a 4 channel RGBA image:
953
954 # this could be done with convert() instead
955 my $out = Imager->new(xsize => $src->getwidth(),
956 ysize => $src->getheight(),
957 channels => 4);
958 for my $y ( 0 .. $src->getheight()-1 ) {
959 my $data = $src->getsamples(y=>$y, channels=>[ 0, 0, 0, 1 ]);
960 $out->setscanline(y=>$y, pixels=>$data);
961 }
962
963=back
964
965=head1 Packed Color Data
966
967The getscanline() and setscanline() functions can work with pixels
968packed into scalars. This is useful to remove the cost of creating
969color objects, but should only be used when performance is an issue.
970
971Packed data can either be 1 byte per sample or 1 double per sample.
972
973Each pixel returned by getscanline() or supplied to setscanline()
974contains 4 samples, even if the image has fewer then 4 channels. The
975values of the extra samples as returned by getscanline() is not
976specified. The extra samples passed to setscanline() are ignored.
977
978To produce packed 1 byte/sample pixels, use the pack C<C> template:
979
980 my $packed_8bit_pixel = pack("CCCC", $red, $blue, $green, $alpha);
981
982To produce packed double/sample pixels, use the pack C<d> template:
983
984 my $packed_float_pixel = pack("dddd", $red, $blue, $green, $alpha);
985
4cda4e76
TC
986If you use a I<type> parameter of C<index> then the values are palette
987color indexes, not sample values:
988
989 my $im = Imager->new(xsize => 100, ysize => 100, type => 'paletted');
990 my $black_index = $im->addcolors(colors => [ 'black' ]);
991 my $red_index = $im->addcolors(colors => [ 'red' ]);
992 # 2 pixels
993 my $packed_index_data = pack("C*", $black_index, $red_index);
994 $im->setscanline(y => $y, pixels => $packed_index_data, type => 'index');
995
bac4fcee
AMH
996=head1 BUGS
997
998box, arc, do not support antialiasing yet. Arc, is only filled as of
999yet. Default color is not unified yet.
1000
08e6cbea
TC
1001=head1 AUTHOR
1002
1003Tony Cook <tony@imager.perl.org>, Arnar M. Hrafnkelsson.
1004
ed9e5812
TC
1005=head1 SEE ALSO
1006
08e6cbea
TC
1007L<Imager>(3), L<Imager::Cookbook>(3)
1008
1009=head1 REVISION
1010
1011$Revision$
ed9e5812 1012
f75c1aeb 1013=cut