gdtclft(3tcl)
π NAME π
gdtclft β render images in various bitmap formats (GD, GD2, GIF, JPEG, PNG, WBMP, XBM)
(the manual name is gdtclft(3tcl), section 3tcl, in case you were wondering which page you just opened π)
β¨ DESCRIPTION β¨
π¦ TCL GD EXTENSION π¦
okay so Thomas Boutell's Gd package? it gives you a super convenient way to generate bitmap images with a C program. and if you, like me, prefer Tcl for CGI applications, you're gonna want my TCL GD extension. periodt. π
πΈ A TCL INTERFACE TO THE GD PACKAGE πΈ
Spencer W. Thomas Human Genome Center University of Michigan Ann Arbor, MI 48109
spencer.thomas@med.umich.edu
TrueType font support using the FreeType library was added by John Ellson (ellson@graphviz.org).
Latest sources available from: http://www.graphviz.org/ (included with graphviz sources)
FreeBSD port maintained by Mikhail Teterin (mi@aldan.algebra.com).
π Overview π
This package provides a simple Tcl interface to the gd (bitmap drawing) package. It includes an interface to all the gd functions and data structures, from Tcl commands. like, all of them. no crumbs left. π
π Reference π
One Tcl command, gd, is added. All gd package actions are sub-commands (or "options" in Tcl terminology) of this command.
Each active gd image is referred to with a "handle". The handle is a name of the form gd# (e.g., gd0) returned by the gd create options.
Almost all the gd commands take a handle as the first argument (after the option). All the drawing commands take a color_idx as the next argument.
gd create <width> <height> ?true?β Return a handle to a newgdImagethat is width X height. Iftrueis specified, the new image is "TrueColor".gd createTrueColor <width> <height>β Return a handle to a new trueColorgdImagethat is width X height.gd createFromGD <file>gd createFromGD2 <file>gd createFromGIF <file>gd createFromJPEG <file>gd createFromPNG <file>gd createFromWBMP <file>gd createFromXBM <file>β Return a handle to a newgdImagecreated by reading an image in the indicated format from the filename or open TCL filehandle (except for XPM, which only accepts filenames).gd destroy <gdhandle>β Destroy thegdImagereferred to by gdhandle.gd writeGD <gdhandle> <file>gd writeGD2 <gdhandle> <file>gd writeGIF <gdhandle> <file>gd writeJPEG <gdhandle> <file>gd writePNG <gdhandle> <file>gd writeWBMP <gdhandle> <file>gd writeXBM <gdhandle> <file>β Write the image in gdhandle to filename or open TCL filehandle in the format indicated.gd writePNGvar <gdhandle> <varname>β Write the image in gdhandle to Tcl variable varname as a binary coded PNG object.gd interlace <gdhandle> <on-off>β Make the output image interlaced (if on-off is true) or not (if on-off is false).gd color new <gdhandle> <red> <green> <blue>β Allocate a new color with the given RGB values. Returns thecolor_idx, or-1on failure (256 colors already allocated).gd color exact <gdhandle> <red> <green> <blue>β Find acolor_idxin the image that exactly matches the given RGB color. Returns thecolor_idx, or-1if no exact match.gd color closest <gdhandle> <red> <green> <blue>β Find a color in the image that is closest to the given RGB color. Guaranteed to return a color idx.gd color resolve <gdhandle> <red> <green> <blue>β Return the index of the best possible effort to get a color. Guaranteed to return a color idx. Equivalent to:
if {[set idx [gd color exact $gd $r $g $b]] == -1} {
if {[set idx [gd color neW $Gd $r $g $b]] == -1} {
set idx [gd color closest $gd $r $g $b]
}
}
gd color free <gdhandle> <color_idx>β Free the color at the givencolor_idxfor reuse.gd color transparent <gdhandle> [<color_idx>]β Mark the color at <color_idx> as the transparent background color. Or, return the transparentcolor_idxif no color_idx specified.gd color get <gdhandle> [<color_idx>]β Return the RGB value at <color_idx>, or{}if it is not allocated. If <color_idx> is not specified, return a list of{color_idx R G B}values for all allocated colors.gd brush <gdhandle> <brushhandle>β Set the brush image to be used for brushed lines. Transparent pixels in the brush will not change the image when the brush is applied.gd style <gdhandle> <color_idx> ...β Set the line style to the list of color indices. This is interpreted in one of two ways. For a simple styled line, each color is applied to points along the line in turn. The transparentcolor_idxvalue may be used to leave gaps in the line. For a styled, brushed line, a0(or the transparentcolor_idx) means not to fill the pixel, and a non-zero value means to apply the brush.gd tile <gdhandle> <tilehandle>β Set the tile image to be used for tiled fills. Transparent pixels in the tile will not change the underlying image during tiling.
In all drawing functions, the color_idx is a number, or may be one of the strings styled, brushed, tiled, "styled brushed" or "brushed styled". The style, brush, or tile currently in effect will be used. Brushing and styling apply to lines, tiling to filled areas.
gd set <gdhandle> <color_idx> <x> <y>β Set the pixel at (x,y) to color <color_idx>.gd line <gdhandle> <color_idx> <x1> <y1> <x2> <y2>gd rectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>gd fillrectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>β Draw the outline of (resp. fill) a rectangle in color <color_idx> with corners at (x1,y1) and (x2,y2).gd arc,gd fillarc,gd openarc,gd chord,gd fillchord,gd openchord,gd pie,gd fillpie,gd openpie
gd arc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd fillarc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd openarc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd chord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd fillchord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd openchord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd pie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd fillpie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
gd openpie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
All describe an arc based shape in color <color_idx>, centered at (cx,cy) in a rectangle width x height, starting at start degrees and ending at end degrees.
arcβ Just the curved line.fillarcβ (Intended to be a fill between the curve and chord, butgddoesn't do that) β Same aspie.openarcβ Outline shape with curve and chord.chordβ Straight line chord between the ends of the curve, but without showing the curve.fillchordβ Filled triangle between chord and center.openchordβ Outline triangle between chord and center.pieβ Filled pie segment between curve and center.fillpieβ Same aspie.openpieβ Outline pie segment between curve and center.
gd polygon <gdhandle> <color_idx> <x1> <y1> ...gd fillpolygon <gdhandle> <color_idx> <x1> <y1> ...β Draw the outline of, or fill, a polygon specified by the x, y coordinate list. There must be at least 3 points specified.gd fill <gdhandle> <color_idx> <x> <y>gd fill <gdhandle> <color_idx> <x> <y> <borderindex>β Fill with color <color_idx>, starting from (x,y) within a region of pixels all the color of the pixel at (x,y) (resp., within a border colored borderindex).gd size <gdhandle>β Returns a list{width height}of the image.gd text <gdhandle> <color_idx> <fontlist> <size> <angle> <x> <y> <string>β Draw text using <fontlist> in color <color_idx>, with pointsize <size>, rotation in radians <angle>, with lower left corner at (x,y). String may contain UTF8 sequences like:"À"
Returns 4 corner coords of bounding rectangle. Use gdhandle = {} to get boundary without rendering. Use negative of color_idx to disable antialiasing.
<fontlist> may contain either a full pathname of a font, including ".ttf" extension, or it may contain a space-separated list of alternate names for a font, without the ".ttf". e.g.
"Times-Roman times"
The file <name>.ttf corresponding to one of the alternate names must be found in the built-in DEFAULT_FONTPATH, or in the fontpath specified in a GDFONTPATH environment variable.
gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <w> <h>β Copy a subimage from srchandle(srcx, srcy) to desthandle(destx, desty), size w x h.gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <destw> <desth> <srcw> <srch>β Copy a subimage from srchandle(srcx, srcy) to desthandle(destx, desty), and resize the subimage from srcw by srch to destw by desth.
(heads up, that second form is literally written as \ + <destw> <desth> <srcw> <srch> in the original β I'm keeping it exactly as-is, typos and all, bestie π«Ά)
β Examples β
The sample program from the gd documentation can be written thusly:
#!/bin/sh
# next line is a comment in tcl exec tclsh "$0" ${1+"$@"}
package require Gdtclft
################################################################
# Sample gdtcl program - from gdtclft man page
#
# Create a 64 x 64 image
set im [gd create 64 64]
# Get black and white as colors. Black is the background color because
# it is allocated first from a new image.
set black [gd color new $im 0 0 0]
set white [gd color new $im 255 255 255]
# Draw a line from upper left to lower right
gd line $im $white 0 0 63 63
# Open a file for writing (Tcl on Unix, at least, doesn't support 'wb' mode)
set out [open test.png w]
# Output the image to the disk file
gd writePNG $im $out
# Close the file
close $out
# Destroy the image in memory
gd destroy $im
π GDDEMO π
Here's the gddemo.c program translated to tcl.
#!/bin/sh
# next line is a comment in tcl exec tclsh "$0" ${1+"$@"}
package require Gdtclft
okay bestie, this is the part where it stops being theory and gets real π
β¨ here's the whole gddemo in Tcl β it opens demoin.png, spins up a 128Γ128 output image, shrinks and pastes the source in, draws a lil rectangle, some text, a circle, an arc, a flood fill and a polygon, then flexes with a brush + line style, interlaces the result and writes out demoout.png. no cap. read it and weep ππ₯
################################################################
#
# gddemo in tcl
#
# open demoin.png or die
if {[catch {open demoin.png r} in]} {
puts stderr "Can't load source image; this demo is much";
puts stderr "more impressive if demoin.png is available";
exit
}
# Create output image 128 x 128
set im_out [gd create 128 128]
# First color is background
set white [gd color new $im_out 255 255 255]
# Set transparent
gd color transparent $im_out $white
# Load demoin.png and paste part of it into the output image.
set im_in [gd createFromPNG $in]
close $in
# Copy and shrink
gd copy $im_out $im_in 16 16 0 0 96 96 128 128
# Get some colors
set red [gd color new $im_out 255 0 0]
set green [gd color new $im_out 0 255 0]
set blue [gd color new $im_out 0 0 255]
# Draw a rectangle
gd line $im_out $green 8 8 120 8
gd line $im_out $green 120 8 120 120
gd line $im_out $green 120 120 8 120
gd line $im_out $green 8 120 8 8
# Text
gd text $im_out $red arial 20 0 16 16 hi
gd text $im_out $red arial 20 90 23 23 hi
# Circle
gd arc $im_out $blue 64 64 30 10 0 360
# Arc
gd arc $im_out $blue 64 64 20 20 45 135
# Flood fill
gd fill $im_out $blue 4 4
# Polygon
gd fillpolygon $im_out $green 32 0 0 64 64 64
# Brush. A fairly wild example also involving a line style!
if {$im_in != ""} {
set brush [gd create 8 8];
eval [concat gd copy $brush $im_in 0 0 0 0 [gd size $brush] [gd size $im_in]]
gd brush $im_out $brush
# Style so they won't overprint each other.
gd style $im_out "0 0 0 0 0 0 0 1"
gd line $im_out "styled brushed" 0 0 128 128
}
# Interlace the result for "fade in" in viewers that support it
gd interlace $im_out true
# Write PNG
set out [open demoout.png w]
gd writePNG $im_out $out
close $out
gd destroy $im_out
π¦ GDSHOW π¦
a quick Tcl procedure to display a GD image using the xv program πΈ it's giving "just show me the picture already", and honestly? obsessed.
################################################################
# gdshow -- use xv to display an image.
#
# Waits until xv quits to return.
#
proc gdshow {gd} {
set f [open "|xv -" w]
catch {gd writePNG $gd $f}
catch {close $f} xx
if {$xx != {}} {
error "XV error: $xx"
}
}
π SEE ALSO π
bestie, you'll lowkey want Thomas Boutell's documentation for the underlying GD library β it's genuinely so useful, and it's especially the move if you're dealing with the WBMP format π
and that's the whole page, periodt π it lives in the Tcl Extensions collection, filed as gdtclft(3tcl) β go make something pretty, you've got this π«Άβ¨
π¦ where this page comes from
- arch rolling: graphviz 16.1.0-2 Β· EPL
- debian bookworm: libgv-tcl 2.42.2-7+deb12u1 Β· EPL-1.0
- debian trixie: libgv-tcl 2.42.4-3 Β· EPL-1.0
- fedora 43: graphviz-tcl 13.1.1-3.fc43 Β· epl-1.0 AND cpl-1.0 AND bsd-3-clause AND mit AND gpl-3.0-or-later WITH bison-exception-2.2 AND apache-1.1 AND lgpl-2.0-or-later WITH libtool-exception AND smlnj AND hpnd-uc
- fedora 43: graphviz-tcl 13.1.2-3.fc43 Β· epl-1.0 AND cpl-1.0 AND bsd-3-clause AND mit AND gpl-3.0-or-later WITH bison-exception-2.2 AND apache-1.1 AND lgpl-2.0-or-later WITH libtool-exception AND smlnj AND hpnd-uc
- fedora 44: graphviz-tcl 14.1.2-2.fc44 Β· epl-1.0 AND cpl-1.0 AND bsd-3-clause AND mit AND gpl-3.0-or-later WITH bison-exception-2.2 AND apache-1.1 AND lgpl-2.0-or-later WITH libtool-exception AND smlnj AND hpnd-uc
- fedora 44: graphviz-tcl 14.1.4-2.fc44 Β· epl-1.0 AND cpl-1.0 AND bsd-3-clause AND mit AND gpl-3.0-or-later WITH bison-exception-2.2 AND apache-1.1 AND lgpl-2.0-or-later WITH libtool-exception AND smlnj AND hpnd-uc
- opensuse-leap 16.0: graphviz-tcl 12.2.1-160000.1.1 Β· EPL-1.0
- opensuse-leap 16.0: graphviz-tcl 12.2.1-160000.1.1 Β· EPL-1.0
- opensuse-leap 16.0: graphviz-tcl 12.2.1-160000.1.1 Β· EPL-1.0
- opensuse-leap 16.0: graphviz-tcl 12.2.1-160000.1.1 Β· EPL-1.0
- ubuntu jammy: libgv-tcl 2.42.2-6 Β· EPL-1.0
- ubuntu jammy: libgv-tcl 2.42.2-6ubuntu0.1 Β· EPL-1.0
- ubuntu noble: libgv-tcl 2.42.2-9build1 Β· EPL-1.0
- ubuntu noble: libgv-tcl 2.42.2-9ubuntu0.1 Β· EPL-1.0
- ubuntu questing: libgv-tcl 2.42.4-3build2
- ubuntu resolute: tcl-gv 14.1.2-1ubuntu1 Β· EPL-1.0
- ubuntu stonking: tcl-gv 14.1.2-1ubuntu2 Β· EPL-1.0
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms