Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 95 lines (70 sloc) 2.868 kB
bd74a36 @mberends add example calling GD image processing library
mberends authored
1 # Call the GD graphics library that is also used by Perl 5's GD module.
2
3 use NativeCall; # from project 'zavolaj'
4
5 # -------- foreign function definitions in alphabetical order ----------
6 sub gdImageCreate( Int $width, Int $height )
7 returns OpaquePointer
8 is native('libgd') { ... }
9
10 sub gdImageColorAllocate( OpaquePointer $image, Int $red, Int $green, Int $blue )
11 returns Int
12 is native('libgd') { ... }
13
14 sub gdImageLine( OpaquePointer $image, Int $x1, Int $y1, Int $x2, Int $y2, Int $colour )
15 is native('libgd') { ... }
16
17 sub gdImagePng( OpaquePointer $image, OpaquePointer $file )
18 is native('libgd') { ... }
19
20 sub gdImageJpeg( OpaquePointer $image, OpaquePointer $file, Int $quality )
21 is native('libgd') { ... }
22
23 sub gdImageDestroy( OpaquePointer $image )
24 is native('libgd') { ... }
25
26 sub fopen( Str $filename, Str $mode )
27 returns OpaquePointer
28 is native() { ... }
29
30 sub fclose( OpaquePointer $filepointer )
31 is native() { ... }
32
33 # Translation of the example at http://www.libgd.org/Basics from C to
34 # Perl 6. The only reason for having a MAIN subroutine is to have it
35 # look as similar as possible.
36 sub MAIN {
37 # Declare the image
38 my $im;
39 # Declare output files
40 my ($pngout, $jpegout);
41 # Declare color indexes
42 my Int $black;
43 my Int $white;
44 # Allocate the image: 64 pixels across by 64 pixels tall
45 $im = gdImageCreate(64, 64);
46 # Allocate the color black (red, green and blue all minimum).
47 # Since this is the first color in a new image, it will
48 # be the background color.
49 $black = gdImageColorAllocate($im, 0, 0, 0);
50
51 # Allocate the color white (red, green and blue all maximum).
52 $white = gdImageColorAllocate($im, 255, 255, 255);
53
54 # Draw a line from the upper left to the lower right,
55 # using white color index.
56 gdImageLine($im, 0, 0, 63, 63, $white);
57
58 # Open a file for writing. "wb" means "write binary", important
59 # under MSDOS, harmless under Unix.
60 $pngout = fopen("test.png", "wb");
61
62 # Do the same for a JPEG-format file.
63 $jpegout = fopen("test.jpg", "wb");
64
65 # Output the image to the disk file in PNG format.
66 gdImagePng($im, $pngout);
67
68 # Output the same image in JPEG format, using the default
69 # JPEG quality setting.
70 gdImageJpeg($im, $jpegout, -1);
71
72 # Close the files.
73 fclose($pngout);
74 fclose($jpegout);
75
76 # Destroy the image in memory.
77 gdImageDestroy($im);
78 }
79
80 =begin pod
81
82 =head1 PREREQUISITES
83
84 The GD library must be installed installed. On Debian systems such
85 as Ubuntu, install libgd-xpm-dev (or libgd-noxpm-dev) because Zavolaj
86 cannot find the variant without the -dev suffix. This applies to most
87 libraries called by Zavolaj, and probably is caused by an implementation
88 detail within Parrot.
89
90 =head1 SEE ALSO
91
92 Official GD project home page: http://www.libgd.org
93
94 =end pod
Something went wrong with that request. Please try again.