5.8 An Example Downloadable Module: GD.pm

5.8  An Example Downloadable Module: GD.pm

    In this section, we briefly discuss a downloadable module called GD.pm. It an be downloaded from www.cpan.org using the steps given in Section 5.7. It is a module that allows a programmer to draw colored graphical objects with a large number of drawing primitives in formats such as JPEG or PNG.

Some downloadable modules provide an object-oriented interface, others a functional interface, and others both. GD.pm provides only an object-oriented interface. To understand how the module can be used to program one should run

 

%perldoc GD.pm

 

after the module has been installed and read the documentation. Usually, the documentation contains a wealth of information.

The GD.pm module has a large number of methods. They are classified in its accompanying documentation into the following categories.

•    Creating and saving images

•    Creating and controlling colors

•    Creating special effects such as brushes and tiling

•    Drawing commands

•    Copying images

•    Drawing characters and strings

•    Drawing polygons

•    Dealing with fonts

The program reads a number from the terminal, chomps it to remove the trailing newline, makes it into a string using the sprintf function. The program draws this string into a graphical object that is later saved in two formats: JPEG and PNG. The JPEG image can be seen on a Web browser without any plug-ins. The program is given below.

 Program 5.20

#!/usr/bin/perl
#file counterGD.pl
use strict;
use GD;

my ($number, $counter, @digits, $length);
my ($im, $black, $white, $blue, $green, $i);

print "Please give me an integer to draw>>";
$counter = ;
chomp $counter;

@digits = split(//, "$counter");
print "digits = @digits\n";
$length = length($counter);
print "length = $length\n";
$im = new GD::Image($length*14,20);
$black = $im->colorAllocate(0, 0, 0);
$white = $im->colorAllocate(255, 255, 255);        
$blue = $im->colorAllocate(0,0,255);

for ($i=0; $i < $length; $i++)
{ $im->string(gdLargeFont, 2+$i*14, 0, $digits[$i], $white);
  if($i < $length-1)
  { $im->filledRectangle(12+$i*14, 0, 13+$i*14, 20, $blue);
  }
}

open PNG, ">$counter.png";
open JPEG, ">$counter.jpg";
print PNG $im->png;
print JPEG $im->jpeg;
close PNG;
close JPEG;

To understand the program well, one should read the documentation for GD.pm for the description of the methods available. To draw a graphic object, the new method is invoked.

 

$im = new GD::Image($length*14,20);

 

This creates an object of type GD::Image. The arguments are length and height of the image object in pixels. Then, a few colors are created or allocated. The colors are $black, $white, and $blue. colorAllocate takes three arguments: red, green and blue (RGB). Black has each of the three components 0, and white has each component the maximum value of 255.

Next, in the for loop, the image is drawn. string is a method that draws a string. The arguments are font description, x position, y position, the string to draw, and the color to draw with, respectively. Here, the current digit is drawn in white. A rectangle is drawn around the digits in blue. filledRectangle is a drawing method. Its arguments are , , , , and a color. It draws a rectangle whose lower left coordinates are , and upper right coordinates are , and fills it up with the specified color. Finally, two
files are opened. A JPEG image is drawn in one and a PNG image is drawn in another. The png method creates a description of the current image in PNG format. Similarly, the jpeg method writes the image in JPEG format. The file names have the digit in front followed by .jpg or .png as extension. An interaction with the program is given below.

 

Please give me an integer to draw>>1234567890123

digits = 1 2 3 4 5 6 7 8 9 0 1 2 3

length = 13

 

Two image files are produced: 1234567890123.jpg and 1234567890123.png. Both files contain the exact same image. The image produced viewed on a Linux machine with the Electric Eyes (ee) utility looks like the one in Figure 5.2.

 

Figure 5.2:  A Counter Image Produced Using GD.pm