home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / lib / site / GD.pm < prev    next >
Encoding:
Perl POD Document  |  1997-08-10  |  29.0 KB  |  1,066 lines

  1. package GD;
  2.  
  3. # Copyright 1995 Lincoln D. Stein.  See accompanying README file for
  4. # usage information
  5.  
  6. require 5.002;
  7. require Exporter;
  8. require DynaLoader;
  9. require AutoLoader;
  10.  
  11. @ISA = qw(Exporter DynaLoader);
  12. # Items to export into callers namespace by default. Note: do not export
  13. # names by default without a very good reason. Use EXPORT_OK instead.
  14. # Do not simply export all your public functions/methods/constants.
  15. @EXPORT = qw(
  16.     gdBrushed
  17.     gdDashSize
  18.     gdMaxColors
  19.     gdStyled
  20.     gdStyledBrushed
  21.     gdTiled
  22.     gdTransparent
  23.     gdSmallFont
  24.     gdLargeFont
  25.     gdMediumBoldFont
  26.     gdTinyFont
  27. );
  28. sub AUTOLOAD {
  29.     # This AUTOLOAD is used to 'autoload' constants from the constant()
  30.     # XS function.  If a constant is not found then control is passed
  31.     # to the AUTOLOAD in AutoLoader.
  32.  
  33.     my($constname);
  34.     ($constname = $AUTOLOAD) =~ s/.*:://;
  35.     $val = constant($constname, @_ ? $_[0] : 0);
  36.     if ($! != 0) {
  37.     if ($! =~ /Invalid/) {
  38.         $AutoLoader::AUTOLOAD = $AUTOLOAD;
  39.         goto &AutoLoader::AUTOLOAD;
  40.     }
  41.     else {
  42.         ($pack,$file,$line) = caller;
  43.         die "Your vendor has not defined GD macro $pack\:\:$constname, used at $file line $line.\n";
  44.     }
  45.     }
  46.     eval "sub $AUTOLOAD { $val }";
  47.     goto &$AUTOLOAD;
  48. }
  49.  
  50. bootstrap GD;
  51.  
  52. # Preloaded methods go here.
  53. sub GD::gdSmallFont {
  54.     return &GD::Font::Small;
  55. }
  56.  
  57. sub GD::gdLargeFont {
  58.     return &GD::Font::Large;
  59. }
  60.  
  61. sub GD::gdMediumBoldFont {
  62.     return &GD::Font::MediumBold;
  63. }
  64.  
  65. sub GD::gdTinyFont {
  66.     return &GD::Font::Tiny;
  67. }
  68.  
  69. # This is a C callback
  70. sub GD::fileno {
  71.     my($fh) = @_;
  72.     my($package) = caller;
  73.     return fileno "$package\:\:$fh";
  74. }
  75.  
  76. ### The polygon object ###
  77. # create a new polygon
  78. sub GD::Polygon::new {
  79.     my $class = shift;
  80.     return bless { 'length'=>0,'points'=>[] },$class;
  81. }
  82.  
  83. # automatic destruction of the polygon
  84. sub GD::Polygon::DESTROY {
  85.     my $self = shift;
  86.     undef $self->{'points'};
  87. }
  88.  
  89. # add an x,y vertex to the polygon
  90. sub GD::Polygon::addPt {
  91.     my($self,$x,$y) = @_;
  92.     push(@{$self->{'points'}},[$x,$y]);
  93.     $self->{'length'}++;
  94. }
  95.  
  96. # get a vertex
  97. sub GD::Polygon::getPt {
  98.     my($self,$index) = @_;
  99.     return () unless ($index>=0) && ($index<$self->{'length'});
  100.     return @{$self->{'points'}->[$index]};
  101. }
  102.  
  103. # change the value of a vertex
  104. sub GD::Polygon::setPt {
  105.     my($self,$index,$x,$y) = @_;
  106.     unless (($index>=0) && ($index<$self->{'length'})) {
  107.     warn "Attempt to set an undefined polygon vertex";
  108.     return undef;
  109.     }
  110.     @{$self->{'points'}->[$index]} = ($x,$y);
  111.     1;
  112. }
  113.  
  114. # return the total number of vertices
  115. sub GD::Polygon::length {
  116.     my $self = shift;
  117.     return $self->{'length'};
  118. }
  119.  
  120. # return the array of vertices.
  121. # each vertex is an two-member (x,y) array
  122. sub GD::Polygon::vertices {
  123.     my $self = shift;
  124.     return @{$self->{'points'}};
  125. }
  126.  
  127. # return the bounding box of the polygon
  128. # (smallest rectangle that contains it)
  129. sub GD::Polygon::bounds {
  130.     my $self = shift;
  131.     my($top,$bottom,$left,$right) = @_;
  132.     $top =    99999999;
  133.     $bottom =-99999999;
  134.     $left =   99999999;
  135.     $right = -99999999;
  136.     my $v;
  137.     foreach $v ($self->vertices) {
  138.     $left = $v->[0] if $left > $v->[0];
  139.     $right = $v->[0] if $right < $v->[0];
  140.     $top = $v->[1] if $top > $v->[1];
  141.     $bottom = $v->[1] if $bottom < $v->[1];
  142.     }
  143.     return ($left,$top,$right,$bottom);
  144. }
  145.  
  146. # delete a vertex, returning it, just for fun
  147. sub GD::Polygon::delete {
  148.     my($self,$index) = @_;
  149.     my($vertex) = splice(@{$self->{'points'}},$index,1);
  150.     return @$vertex;
  151. }
  152.  
  153. # translate the polygon in space by deltaX and deltaY
  154. sub GD::Polygon::offset {
  155.     my($self,$dh,$dv) = @_;
  156.     my $size = $self->length;
  157.     my($i);
  158.     for ($i=0;$i<$size;$i++) {
  159.     my($x,$y)=$self->getPt($i);
  160.     $self->setPt($i,$x+$dh,$y+$dv);
  161.     }
  162. }
  163.  
  164. # map the polygon from sourceRect to destRect,
  165. # translating and resizing it if necessary
  166. sub GD::Polygon::map {
  167.     my($self,$srcL,$srcT,$srcR,$srcB,$destL,$destT,$destR,$destB) = @_;
  168.     my($factorV) = ($destB-$destT)/($srcB-$srcT);
  169.     my($factorH) = ($destR-$destL)/($srcR-$srcL);
  170.     my($vertices) = $self->length;
  171.     my($i);
  172.     for ($i=0;$i<$vertices;$i++) {
  173.     my($x,$y) = $self->getPt($i);
  174.     $x = int($destL + ($x - $srcL) * $factorH);
  175.     $y = int($destT + ($y - $srcT) * $factorV);
  176.     $self->setPt($i,$x,$y);
  177.     }
  178. }
  179.  
  180. # Autoload methods go after __END__, and are processed by the autosplit program.
  181. 1;
  182. __END__
  183.  
  184. =head1 NAME
  185.  
  186. GD.pm - Interface to Gd Graphics Library
  187.  
  188. =head1 DESCRIPTION
  189.  
  190. B<GD.pm> is a port of Thomas Boutell's gd graphics library (see
  191. below).  GD allows you to create color drawings using a large number of
  192. graphics primitives, and emit the drawings as GIF files.
  193.  
  194. GD defines the following three classes:
  195.  
  196. =over 5
  197.  
  198. =item C<GD::Image>
  199.  
  200. An image class, which holds the image data and accepts graphic
  201. primitive method calls.
  202.  
  203. =item C<GD::Font>
  204.  
  205. A font class, which holds static font information and used for text
  206. rendering.
  207.  
  208. =item C<GD::Polygon>
  209.  
  210. A simple polygon object, used for storing lists of vertices prior to
  211. rendering a polygon into an image.
  212.  
  213. =back
  214.  
  215. A Simple Example:
  216.  
  217.     #!/usr/local/bin/perl
  218.  
  219.     use GD;
  220.     
  221.     # create a new image
  222.     $im = new GD::Image(100,100);
  223.  
  224.     # allocate some colors
  225.     $white = $im->colorAllocate(255,255,255);
  226.     $black = $im->colorAllocate(0,0,0);       
  227.     $red = $im->colorAllocate(255,0,0);      
  228.     $blue = $im->colorAllocate(0,0,255);
  229.  
  230.     # make the background transparent and interlaced
  231.     $im->transparent($white);
  232.     $im->interlaced('true');
  233.  
  234.     # Put a black frame around the picture
  235.     $im->rectangle(0,0,99,99,$black);
  236.  
  237.     # Draw a blue oval
  238.     $im->arc(50,50,95,75,0,360,$blue);
  239.  
  240.     # And fill it with red
  241.     $im->fill(50,50,$red);
  242.  
  243.     # Convert the image to GIF and print it on standard output
  244.     print $im->gif;
  245.  
  246. Notes:
  247.  
  248. =over 5
  249.  
  250. =item 1.
  251. To create a new, empty image, send a new() message to GD::Image, passing
  252. it the width and height of the image you want to create.  An image
  253. object will be returned.  Other class methods allow you to initialize
  254. an image from a preexisting GIF, GD or XBM file.
  255.  
  256. =item 2.
  257. Next you will ordinarily add colors to the image's color table.
  258. colors are added using a colorAllocate() method call.  The three
  259. parameters in each call are the red, green and blue (rgb) triples for
  260. the desired color.  The method returns the index of that color in the
  261. image's color table.  You should store these indexes for later use.
  262.  
  263. =item 3.
  264. Now you can do some drawing!  The various graphics primitives are
  265. described below.  In this example, we do some text drawing, create an
  266. oval, and create and draw a polygon.
  267.  
  268. =item 4.
  269. Polygons are created with a new() message to GD::Polygon.  You can add
  270. points to the returned polygon one at a time using the addPt() method.
  271. The polygon can then be passed to an image for rendering.
  272.  
  273. =item 5.
  274. When you're done drawing, you can convert the image into GIF format by
  275. sending it a gif() message.  It will return a (potentially large)
  276. scalar value containing the binary data for the image.  Ordinarily you
  277. will print it out at this point or write it to a file.
  278.  
  279. =back
  280.  
  281. =head1 Method Calls
  282.  
  283.  
  284. =head2 Creating and Saving Images
  285.  
  286. =over 5
  287.  
  288. =item C<new>
  289.  
  290. C<GD::Image::new(width,height)> I<class method>
  291.  
  292. To create a new, blank image, send a new() message to the GD::Image
  293. class.  For example:
  294.  
  295.     $myImage = new GD::Image(100,100) || die;
  296.  
  297. This will create an image that is 100 x 100 pixels wide.  If you don't
  298. specify the dimensions, a default of 64 x 64 will be chosen. If
  299. something goes wrong (e.g. insufficient memory), this call will
  300. return undef.
  301.  
  302. =item C<newFromGif>
  303.  
  304. C<GD::Image::newFromGif(FILEHANDLE)> I<class method>
  305.  
  306. This will create an image from a GIF file read in through the provided
  307. filehandle.  The filehandle must previously have been opened on a
  308. valid GIF file or pipe.  If successful, this call will return an
  309. initialized image which you can then manipulate as you please.  If it
  310. fails, which usually happens if the thing at the other end of the
  311. filehandle is not a valid GIF file, the call returns undef.  Notice
  312. that the call doesn't automatically close the filehandle for you.
  313.  
  314. To get information about the size and color usage of the information,
  315. you can call the image query methods described below.
  316.  
  317.     Example usage:
  318.  
  319.     open (GIF,"barnswallow.gif") || die;
  320.     $myImage = newFromGif GD::Image(GIF) || die;
  321.     close GIF;
  322.  
  323. =item C<newFromXbm>
  324.  
  325. C<GD::Image::newFromXbm(FILEHANDLE)> I<class method>
  326.  
  327. This works in exactly the same way as C<newFromGif>, but reads the
  328. contents of an X Bitmap file:
  329.  
  330.     open (XBM,"coredump.xbm") || die;
  331.     $myImage = newFromXbm GD::Image(XBM) || die;
  332.     close XBM;
  333.  
  334. =item C<newFromGd>
  335.  
  336. C<GD::Image::newFromGd(FILEHANDLE)> I<class method>
  337.  
  338. This works in exactly the same way as C<newFromGif>, but reads the
  339. contents of a GD file.  GD is Tom Boutell's disk-based storage format,
  340. intended for the rare case when you need to read and write the image
  341. to disk quickly.  It's not intended for regular use, because, unlike
  342. GIF or JPEG, no image compression is performed and these files can
  343. become B<BIG>.
  344.  
  345.     open (GDF,"godzilla.gd") || die;
  346.     $myImage = newFromGd GD::Image(GDF) || die;
  347.     close GDF;
  348.  
  349. =item C<gif>
  350.  
  351. C<GD::Image::gif> I<object method>
  352.  
  353. This returns the image data in GIF format.  You can then print it,
  354. pipe it to a display program, or write it to a file.  Example:
  355.  
  356.     $gif_data = $myImage->gif;
  357.     open (DISPLAY,"| display -") || die;
  358.     print DISPLAY $gif_data;
  359.     close DISPLAY;
  360.  
  361. =item C<gd>
  362.  
  363. C<GD::Image::gd> I<object method>
  364.  
  365. This returns the image data in GD format.  You can then print it,
  366. pipe it to a display program, or write it to a file.  Example:
  367.  
  368.     print MYOUTFILE $myImage->gd;
  369.  
  370. =back
  371.  
  372. =head2 Color Control
  373.  
  374. =over 5
  375.  
  376. =item C<colorAllocate>
  377.  
  378. C<GD::Image::colorAllocate(red,green,blue)> I<object method>
  379.  
  380. This allocates a color with the specified red, green and blue
  381. components and returns its index in the color table, if specified.
  382. The first color allocated in this way becomes the image's background
  383. color.  (255,255,255) is white (all pixels on).  (0,0,0) is black (all
  384. pixels off).  (255,0,0) is fully saturated red.  (127,127,127) is 50%
  385. gray.  You can find plenty of examples in /usr/X11/lib/X11/rgb.txt.
  386.  
  387. If no colors are allocated, then this function returns -1.
  388.  
  389. Example:
  390.  
  391.     $white = $myImage->colorAllocate(0,0,0); #background color
  392.     $black = $myImage->colorAllocate(255,255,255);
  393.     $peachpuff = $myImage->colorAllocate(255,218,185);
  394.  
  395. =item C<colorDeallocate>
  396.  
  397. C<GD::Image::colorDeallocate(colorIndex)> I<object method> 
  398.  
  399. This marks the color at the specified index as being ripe for
  400. reallocation.  The next time colorAllocate is used, this entry will be
  401. replaced.  You can call this method several times to deallocate
  402. multiple colors.  There's no function result from this call.
  403.  
  404. Example:
  405.  
  406.     $myImage->colorDeallocate($peachpuff);
  407.     $peachy = $myImage->colorAllocate(255,210,185);
  408.  
  409. =item C<colorClosest>
  410.  
  411. C<GD::Image::colorClosest(red,green,blue)> I<object method>
  412.  
  413. This returns the index of the color closest in the color table to the
  414. red green and blue components specified.  If no colors have yet been
  415. allocated, then this call returns -1.
  416.  
  417. Example:
  418.  
  419.     $apricot = $myImage->colorClosest(255,200,180);
  420.  
  421. =item C<colorExact>
  422.  
  423. C<GD::Image::colorExact(red,green,blue)> I<object method>
  424.  
  425. This returns the index of a color that exactly matches the specified
  426. red green and blue components.  If such a color is not in the color
  427. table, this call returns -1.
  428.  
  429.     $rosey = $myImage->colorExact(255,100,80);
  430.     warn "Everything's coming up roses.\n" if $rosey >= 0;
  431.  
  432. =item C<colorsTotal>
  433.  
  434. C<GD::Image::colorsTotal)> I<object method>
  435.  
  436. This returns the total number of colors allocated in the object.
  437.  
  438.     $maxColors = $myImage->colorsTotal;
  439.  
  440. =item C<getPixel>
  441.  
  442. C<GD::Image::getPixel(x,y)> I<object method>
  443.  
  444. This returns the color table index underneath the specified
  445. point.  It can be combined with rgb()
  446. to obtain the rgb color underneath the pixel.
  447.  
  448. Example:
  449.  
  450.         $index = $myImage->getPixel(20,100);
  451.         ($r,$g,$b) = $myImage->rgb($index);
  452.  
  453. =item C<rgb>
  454.  
  455. C<GD::Image::rgb(colorIndex)> I<object method>
  456.  
  457. This returns a list containing the red, green and blue components of
  458. the specified color index.
  459.  
  460. Example:
  461.  
  462.     @RGB = $myImage->rgb($peachy);
  463.  
  464. =item C<transparent>
  465.  
  466. C<GD::Image::transparent(colorIndex)> I<object method>
  467.  
  468. This marks the color at the specified index as being transparent.
  469. Portions of the image drawn in this color will be invisible.  This is
  470. useful for creating paintbrushes of odd shapes, as well as for
  471. making GIF backgrounds transparent for displaying on the Web.  Only
  472. one color can be transparent at any time. To disable transparency, 
  473. specify -1 for the index.  
  474.  
  475. If you call this method without any parameters, it will return the
  476. current index of the transparent color, or -1 if none.
  477.  
  478. Example:
  479.  
  480.     open(GIF,"test.gif");
  481.     $im = newFromGif GD::Image(GIF);
  482.     $white = $im->colorClosest(255,255,255); # find white
  483.     $im->transparent($white);
  484.     print $im->gif;
  485.  
  486. =back
  487.  
  488. =head2 Special Colors
  489.  
  490. GD implements a number of special colors that can be used to achieve
  491. special effects.  They are constants defined in the GD::
  492. namespace, but automatically exported into your namespace when the GD
  493. module is loaded.
  494.  
  495. =over 5
  496.  
  497. =item C<setBrush>
  498.  
  499. =item C<gdBrushed>
  500.  
  501. C<GD::Image::setBrush( )> and C<GD::gdBrushed>
  502.  
  503. You can draw lines and shapes using a brush pattern.  Brushes are 
  504. just images that you can create and manipulate in the usual way. When
  505. you draw with them, their contents are used for the color and shape of
  506. the lines.
  507.  
  508. To make a brushed line, you must create or load the brush first, then
  509. assign it to the image using C<setBrush>.  You can then draw in that
  510. with that brush using the C<gdBrushed> special color.  It's often 
  511. useful to set the background of the brush to transparent so that 
  512. the non-colored parts don't overwrite other parts of your image.
  513.  
  514. Example:
  515.  
  516.     # Create a brush at an angle
  517.     $diagonal_brush = new GD::Image(5,5);
  518.     $white = $diagonal_brush->allocateColor(255,255,255);
  519.     $black = $diagonal_brush->allocateColor(0,0,0);
  520.     $diagonal_brush->transparent($white);
  521.     $diagonal_brush->line(0,4,4,0,$black); # NE diagonal
  522.  
  523.     # Set the brush
  524.     $myImage->setBrush($diagonal_brush);
  525.     
  526.     # Draw a circle using the brush
  527.     $myImage->arc(50,50,25,25,0,360,gdBrushed);
  528.  
  529. =item C<setStyle>
  530.  
  531. =item C<gdStyled>
  532.  
  533. C<GD::Image::setStyle(@colors)> and C<GD::gdStyled>
  534.  
  535. Styled lines consist of an arbitrary series of repeated colors and are
  536. useful for generating dotted and dashed lines.  To create a styled
  537. line, use C<setStyle> to specify a repeating series of colors.  It
  538. accepts an array consisting of one or more color indexes.  Then
  539. draw using the C<gdStyled> special color.  Another special color,
  540. C<gdTransparent> can be used to introduce holes in the line, as the
  541. example shows.
  542.  
  543. Example:
  544.  
  545.     # Set a style consisting of 4 pixels of yellow,
  546.     # 4 pixels of blue, and a 2 pixel gap
  547.     $myImage->setStyle($yellow,$yellow,$yellow,$yellow,
  548.                $blue,$blue,$blue,$blue,
  549.                gdTransparent,gdTransparent);
  550.     $myImage->arc(50,50,25,25,0,360,gdStyled);
  551.  
  552. To combine the C<gdStyled> and C<gdBrushed> behaviors, you can specify
  553. C<gdStyledBrushed>.  In this case, a pixel from the current brush
  554. pattern is rendered wherever the color specified in setStyle() is
  555. neither gdTransparent nor 0.
  556.  
  557. =item C<gdTiled>
  558.  
  559. Draw filled shapes and flood fills using a pattern.  The pattern is
  560. just another image.  The image will be tiled multiple times in order
  561. to fill the required space, creating wallpaper effects.  You must call
  562. C<setTile> in order to define the particular tile pattern you'll use
  563. for drawing when you specify the gdTiled color.
  564. details.
  565.  
  566. =item C<gdStyled>
  567.  
  568. The gdStyled color is used for creating dashed and dotted lines.  A
  569. styled line can contain any series of colors and is created using the
  570. C<setStyled> command.
  571.  
  572. =back
  573.  
  574. =head2 Drawing Commands
  575.  
  576. =over 5
  577.  
  578. =item C<setPixel>
  579.  
  580. C<GD::Image::setPixel(x,y,color)> I<object method> 
  581.  
  582. This sets the pixel at (x,y) to the specified color index.  No value
  583. is returned from this method.  The coordinate system starts at the
  584. upper left at (0,0) and gets larger as you go down and to the right.
  585. You can use a real color, or one of the special colors gdBrushed, 
  586. gdStyled and gdStyledBrushed can be specified.
  587.  
  588. Example:
  589.  
  590.     # This assumes $peach already allocated
  591.     $myImage->setPixel(50,50,$peach);
  592.  
  593. =item C<line>
  594.  
  595. C<GD::Image::line(x1,y1,x2,y2,color)> I<object method>
  596.  
  597. This draws a line from (x1,y1) to (x2,y2) of the specified color.  You
  598. can use a real color, or one of the special colors gdBrushed, 
  599. gdStyled and gdStyledBrushed.
  600.  
  601. Example:
  602.  
  603.     # Draw a diagonal line using the currently defind
  604.     # paintbrush pattern.
  605.     $myImage->line(0,0,150,150,gdBrushed);
  606.  
  607. =item C<dashedLine>
  608.  
  609. C<GD::Image::dashedLine(x1,y1,x2,y2,color)> I<object method>
  610.  
  611. This draws a dashed line from (x1,y1) to (x2,y2) in the specified
  612. color.  A more powerful way to generate arbitrary dashed and dotted
  613. lines is to use the setStyle() method described below and to draw with
  614. the special color gdStyled.
  615.  
  616. Example:
  617.  
  618.     $myImage->dashedLine(0,0,150,150,$blue);
  619.  
  620. =item C<rectangle>
  621.  
  622. C<GD::Image::rectangle(x1,y1,x2,y2,color)> I<object method>
  623.  
  624. This draws a rectangle with the specified color.  (x1,y1) and (x2,y2)
  625. are the upper left and lower right corners respectively.  Both real 
  626. color indexes and the special colors gdBrushed, gdStyled and 
  627. gdStyledBrushed are accepted.
  628.  
  629. Example:
  630.  
  631.     $myImage->rectangle(10,10,100,100,$rose);
  632.  
  633. =item C<filledRectangle>
  634.  
  635. C<GD::Image::filledRectangle(x1,y1,x2,y2,color)> I<object method>
  636.  
  637. This draws a rectangle filed with the specified color.  You can use a
  638. real color, or the special fill color gdTiled to fill the polygon
  639. with a pattern.
  640.  
  641. Example:
  642.  
  643.     # read in a fill pattern and set it
  644.     open(GIF,"happyface.gif") || die;
  645.     $tile = newFromGif GD::Image(GIF);
  646.     $myImage->setTile($tile); 
  647.  
  648.     # draw the rectangle, filling it with the pattern
  649.     $myImage->filledRectangle(10,10,150,200,gdTiled);
  650.  
  651. =item C<polygon>
  652.  
  653. C<GD::Image::polygon(polygon,color)> I<object method> 
  654.  
  655. This draws a polygon with the specified color.  The polygon must be
  656. created first (see below).  The polygon must have at least three
  657. vertices.  If the last vertex doesn't close the polygon, the method
  658. will close it for you.  Both real color indexes and the special 
  659. colors gdBrushed, gdStyled and gdStyledBrushed can be specified.
  660.  
  661. Example:
  662.  
  663.     $poly = new GD::Polygon;
  664.     $poly->addPt(50,0);
  665.     $poly->addPt(99,99);
  666.     $poly->addPt(0,99);
  667.     $myImage->polygon($poly,$blue);
  668.  
  669. =item C<filledPolygon>
  670.  
  671. C<GD::Image::filledPolygon(poly,color)> I<object method>
  672.  
  673. This draws a polygon filled with the specified color.  You can use a
  674. real color, or the special fill color gdTiled to fill the polygon
  675. with a pattern.
  676.  
  677. Example:
  678.  
  679.     # make a polygon
  680.     $poly = new GD::Polygon;
  681.     $poly->addPt(50,0);
  682.     $poly->addPt(99,99);
  683.     $poly->addPt(0,99);
  684.  
  685.     # draw the polygon, filling it with a color
  686.     $myImage->filledPolygon($poly,$peachpuff);
  687.  
  688. =item C<arc>
  689.  
  690. C<GD::Image::arc(cx,cy,width,height,start,end,color)> I<object method>
  691.  
  692. This draws arcs and ellipses.  (cx,cy) are the center of the arc, and
  693. (width,height) specify the width and height, respectively.  The
  694. portion of the ellipse covered by the arc are controlled by start and
  695. end, both of which are given in degrees from 0 to 360.  Zero is at the
  696. top of the ellipse, and angles increase clockwise.  To specify a
  697. complete ellipse, use 0 and 360 as the starting and ending angles.  To
  698. draw a circle, use the same value for width and height.
  699.  
  700. You can specify a normal color or one of the special colors gdBrushed,
  701. gdStyled, or gdStyledBrushed.
  702.  
  703. Example:
  704.  
  705.     # draw a semicircle centered at 100,100
  706.     $myImage->arc(100,100,50,50,0,180,$blue);
  707.  
  708. =item C<fill>
  709.  
  710. C<GD::Image::fill(x,y,color)> I<object method>
  711.  
  712. This method flood-fills regions with the specified color.  The color
  713. will spread through the image, starting at point (x,y), until it is
  714. stopped by a pixel of a different color from the starting pixel (this
  715. is similar to the "paintbucket" in many popular drawing toys).  You
  716. can specify a normal color, or the special color gdTiled, to flood-fill
  717. with patterns.
  718.  
  719. Example:
  720.  
  721.     # Draw a rectangle, and then make its interior blue
  722.     $myImage->rectangle(10,10,100,100,$black);
  723.     $myImage->fill(50,50,$blue);
  724.  
  725. =item C<GD::Image::fillToBorder(x,y,bordercolor,color)> I<object method>
  726.  
  727. Like C<fill>, this method flood-fills regions with the specified color,
  728. starting at position (x,y).
  729. However, instead of stopping when it hits a pixel of a different color
  730. than the starting pixel, flooding will only stop when it hits the
  731. color specified by bordercolor.  You must specify a normal indexed
  732. color for the bordercolor.  However, you are free to use the gdTiled
  733. color for the fill.
  734.  
  735. Example:
  736.  
  737.     # This has the same effect as the previous example
  738.     $myImage->rectangle(10,10,100,100,$black);
  739.     $myImage->fillToBorder(50,50,$black,$blue);
  740.  
  741. =back
  742.  
  743. =head2 Image Copying Commands
  744.  
  745. Two methods are provided for copying a rectangular region from one
  746. image to another.  One method copies a region without resizing it.
  747. The other allows you to stretch the region during the copy operation.
  748.  
  749. With either of these methods it is important to know that the routines
  750. will attempt to flesh out the destination image's color table to match
  751. the colors that are being copied from the source.  If the
  752. destination's color table is already full, then the routines will
  753. attempt to find the best match, with varying results.
  754.  
  755. =over 5
  756.  
  757. =item C<copy>
  758.  
  759. C<GD::Image::copy(sourceImage,dstX,dstY,srcX,srcY,width,height)> I<object method>
  760.  
  761. This is the simpler of the two copy operations, copying the specified
  762. region from the source image to the destination image (the one
  763. performing the method call).  (srcX,srcY) specify the upper left
  764. corner of a rectangle in the source image, and (width,height) give the
  765. width and height of the region to copy.  (dstX,dstY) control where in
  766. the destination image to stamp the copy.  You can use the same image
  767. for both the source and the destination, but the source and
  768. destination regions must not overlap or strange things will happen.
  769.  
  770. Example:
  771.  
  772.     $myImage = new GD::Image(100,100);
  773.     ... various drawing stuff ...
  774.     $srcImage = new GD::Image(50,50);
  775.     ... more drawing stuff ...
  776.     # copy a 25x25 pixel region from $srcImage to
  777.     # the rectangle starting at (10,10) in $myImage
  778.     $myImage->copy($srcImage,10,10,0,0,25,25);
  779.  
  780. =item C<copyResized>
  781.  
  782. C<GD::Image::copyResized(sourceImage,dstX,dstY,srcX,srcY,destW,destH,srcW,srcH)> I<object method>
  783.  
  784. This method is similar to copy() but allows you to choose different
  785. sizes for the source and destination rectangles.  The source and
  786. destination rectangle's are specified independently by (srcW,srcH) and
  787. (destW,destH) respectively.  copyResized() will stretch or shrink the
  788. image to accomodate the size requirements.
  789.  
  790. Example:
  791.  
  792.     $myImage = new GD::Image(100,100);
  793.     ... various drawing stuff ...
  794.     $srcImage = new GD::Image(50,50);
  795.     ... more drawing stuff ...
  796.     # copy a 25x25 pixel region from $srcImage to
  797.     # a larger rectangle starting at (10,10) in $myImage
  798.     $myImage->copyResized($srcImage,10,10,0,0,50,50,25,25);
  799.  
  800. =back
  801.  
  802. =head2 Character and String Drawing
  803.  
  804. Gd allows you to draw characters and strings, either in normal
  805. horizontal orientation or rotated 90 degrees.  These routines use a
  806. GD::Font object, described in more detail below.  There are four
  807. built-in fonts, available in global variables gdLargeFont,
  808. gdMediumBoldFont, gdSmallFont and gdTinyFont.  Currently there is no
  809. way of dynamically creating your own fonts.
  810.  
  811. =over 5
  812.  
  813. =item C<string>
  814.  
  815. C<GD::Image::string(font,x,y,string,color)> I<Object Method>
  816.  
  817. This method draws a string startin at position (x,y) in the specified
  818. font and color.  Your choices of fonts are gdSmallFont, gdMediumBoldFont,
  819. gdTinyFont and gdLargeFont.
  820.  
  821. Example:
  822.  
  823.     $myImage->string(gdSmallFont,2,10,"Peachy Keen",$peach);
  824.  
  825. =item C<stringUp>
  826.  
  827. C<GD::Image::stringUp(font,x,y,string,color)> I<Object Method>
  828.  
  829. Just like the previous call, but draws the text rotated
  830. counterclockwise 90 degrees.
  831.  
  832. =item C<char>
  833.  
  834. =item C<charUp>
  835.  
  836. C<GD::Image::char(font,x,y,char,color)> I<Object Method>
  837. C<GD::Image::charUp(font,x,y,char,color)> I<Object Method>
  838.  
  839. These methods draw single characters at position (x,y) in the
  840. specified font and color.  They're carry-overs from the C interface,
  841. where there is a distinction between characters and strings.  Perl is
  842. insensible to such subtle distinctions.
  843.  
  844. =back
  845.  
  846. =head2 Miscellaneous Image Methods
  847.  
  848. =over 5
  849.  
  850. =item C<interlaced>
  851.  
  852. C<GD::Image::interlaced( )> C<GD::Image::interlaced(1)> I<Object method>
  853.  
  854. This method sets or queries the image's interlaced setting.  Interlace
  855. produces a cool venetian blinds effect on certain viewers.  Provide a
  856. true parameter to set the interlace attribute.  Provide undef to
  857. disable it.  Call the method without parameters to find out the
  858. current setting.
  859.  
  860. =item c<getBounds>
  861.  
  862. C<GD::Image::getBounds( )> I<Object method>
  863.  
  864. This method will return a two-member list containing the width and
  865. height of the image.  You query but not not change the size of the
  866. image once it's created.
  867.  
  868.  
  869. =back
  870.  
  871. =head2 Polygon Methods
  872.  
  873. A few primitive polygon creation and manipulation methods are
  874. provided.  They aren't part of the Gd library, but I thought they
  875. might be handy to have around (they're borrowed from my qd.pl
  876. Quickdraw library).
  877.  
  878. =over 5
  879.  
  880. =item c<new>
  881.  
  882. C<GD::Polygon::new> I<class method>
  883.  
  884. Create an empty polygon with no vertices.
  885.  
  886.     $poly = new GD::Polygon;
  887.  
  888. =item C<addPt>
  889.  
  890. C<GD::Polygon::addPt(x,y)> I<object method>
  891.  
  892. Add point (x,y) to the polygon.
  893.  
  894.     $poly->addPt(0,0);
  895.     $poly->addPt(0,50);
  896.     $poly->addPt(25,25);
  897.     $myImage->fillPoly($poly,$blue);
  898.  
  899. =item C<getPt>
  900.  
  901. C<GD::Polygon::getPt(index)> I<object method>
  902.  
  903. Retrieve the point at the specified vertex.
  904.  
  905.     ($x,$y) = $poly->getPt(2);
  906.  
  907. =item C<setPt>
  908.  
  909. C<GD::Polygon::setPt(index,x,y)> I<object method>
  910.  
  911. Change the value of an already existing vertex.  It is an error to set
  912. a vertex that isn't already defined.
  913.  
  914.     $poly->setPt(2,100,100);
  915.  
  916. =item C<deletePt>
  917.  
  918. C<GD::Polygon:deletePt(index)> I<object method>
  919.  
  920. Delete the specified vertex, returning its value.
  921.  
  922.     ($x,$y) = $poly->deletePt(1); 
  923.  
  924. =item C<length>
  925.  
  926. C<GD::Polygon::length> I<object method>
  927.  
  928. Return the number of vertices in the polygon.
  929.  
  930.     $points = $poly->length;
  931.  
  932. =item C<vertices>
  933.  
  934. C<GD::Polygon::vertices> I<object method>
  935.  
  936. Return a list of all the verticies in the polygon object.  Each
  937. membver of the list is a reference to an (x,y) array.
  938.  
  939.     @vertices = $poly->vertices;
  940.     foreach $v (@vertices)
  941.        print join(",",@$v),"\n";
  942.     }
  943.  
  944. =item C<bounds>
  945.  
  946. C<GD::Polygon::bounds> I<object method>
  947.  
  948. Return the smallest rectangle that completely encloses the polygon.
  949. The return value is an array containing the (left,top,right,bottom) of
  950. the rectangle.
  951.  
  952.     ($left,$top,$right,$bottom) = $poly->bounds;
  953.  
  954. =item C<offset>
  955.  
  956. C<GD::Polygon::offset(dx,dy)> I<object method>
  957.  
  958. Offset all the vertices of the polygon by the specified horizontal
  959. (dh) and vertical (dy) amounts.  Positive numbers move the polygon
  960. down and to the right.
  961.  
  962.     $poly->offset(10,30);
  963.  
  964. =item C<map>
  965.  
  966. C<GD::Polygon::map(srcL,srcT,srcR,srcB,destL,dstT,dstR,dstB)> I<object method>
  967.  
  968. Map the polygon from a source rectangle to an equivalent position in a
  969. destination rectangle, moving it and resizing it as necessary.  See
  970. polys.pl for an example of how this works.  Both the source and
  971. destination rectangles are given in (left,top,right,bottom)
  972. coordinates.  For convenience, you can use the polygon's own bounding
  973. box as the source rectangle.
  974.  
  975.     # Make the polygon really tall
  976.     $poly->map($poly->bounds,0,0,50,200);
  977.  
  978. =back
  979.  
  980. =head2 Font Utilities
  981.  
  982. Gd's support for fonts is minimal.  Basically you have access to
  983. gdSmallFont and gdLargeFont for drawing, and not much else.  However,
  984. for future compatibility, I've made the fonts into perl objects of
  985. type GD::Font that you can query and, perhaps someday manipulate.
  986.  
  987. =over 5
  988.  
  989. =item C<gdSmallFont>
  990.  
  991. C<GD::Font::gdSmallFont> I<constant>
  992.  
  993. This is the basic small font, "borrowed" from a well known public
  994. domain 6x12 font.
  995.  
  996. =item C<gdLargeFont>
  997.  
  998. C<GD::Font::gdLargeFont> I<constant>
  999.  
  1000. This is the basic large font, "borrowed" from a well known public
  1001. domain 8x16 font.
  1002.  
  1003. =item C<gdMediumBoldFont>
  1004.  
  1005. C<GD::Font::gdMediumBoldFont> I<constant>
  1006.  
  1007. This is a bold font intermediate in size between the small and large
  1008. fonts, borrowed from a public domain 7x13 font;
  1009.  
  1010. =item C<gdTinyFont>
  1011.  
  1012. C<GD::Font::gdTinyFont> I<constant>
  1013.  
  1014. This is a tiny, almost unreadable font, 5x8 pixels wide.
  1015.  
  1016. =item C<nchars>
  1017.  
  1018. C<GD::Font::nchars>    I<object method>
  1019.  
  1020. This returns the number of characters in the font.
  1021.  
  1022.     print "The large font contains ",gdLargeFont->nchars," characters\n";
  1023.  
  1024. =item C<offset>
  1025.  
  1026. C<GD::Font::offset>     I<object method>
  1027.  
  1028. This returns the ASCII value of the first character in the font
  1029.  
  1030. =item C<width>
  1031.  
  1032. =item C<height>
  1033.  
  1034. C<GD::Font::width> C<GD::Font::height>    I<object methods>
  1035.  
  1036. These return the width and height of the font.
  1037.  
  1038.     ($w,$h) = (gdLargeFont->width,gdLargeFont->height);
  1039.  
  1040. =back
  1041.  
  1042. =head1 Obtaining the C-language version of gd
  1043.  
  1044. libgd, the C-language version of gd, can be obtained at URL
  1045. http://www.boutell.com/gd/gd.html.  Directions for installing and
  1046. using it can be found at that site.  Please do not contact me for help
  1047. with libgd.
  1048.  
  1049. =head1 Copyright Information
  1050.  
  1051. The GD.pm interface is copyright 1995, Lincoln D. Stein.  You are free
  1052. to use it for any purpose, commercial or noncommercial, provided that
  1053. if you redistribute the source code this statement of copyright
  1054. remains attached. The gd library is covered separately under a 1994
  1055. copyright by Quest Protein Database Center, Cold Spring Harbor Labs
  1056. and Thomas Boutell.  For usage information see the gd documentation at
  1057. URL
  1058.  
  1059.     http://www.boutell.com/gd/gd.html
  1060.  
  1061. The latest versions of GD.pm are available at
  1062.  
  1063.   http://www.genome.wi.mit.edu/ftp/pub/software/WWW/GD.html
  1064.   ftp://ftp-genome.wi.mit.edu/pub/software/WWW/GD.pm.tar.gz
  1065.  
  1066.