gdtclft.3tcl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. .TH gdtclft 3tcl "Tcl Extensions"
  2. .SH NAME
  3. gdtclft \- render images in various bitmap formats (GD, GD2, GIF, JPEG, PNG, WBMP, XBM)
  4. .SH DESCRIPTION
  5. .PP
  6. .nf
  7. TCL GD EXTENSION
  8. Thomas Boutell's Gd package provides a convenient way to generate
  9. bitmap images with a C program. If you, like me, prefer Tcl for CGI
  10. applications, you'll want my TCL GD extension.
  11. A TCL INTERFACE TO THE GD PACKAGE
  12. Spencer W. Thomas
  13. Human Genome Center
  14. University of Michigan
  15. Ann Arbor, MI 48109
  16. [email protected]
  17. TrueType font support using the FreeType library was added by
  18. John Ellson ([email protected]).
  19. Latest sources available from:
  20. http://www.graphviz.org/ (included with graphviz sources)
  21. FreeBSD port maintained by Mikhail Teterin ([email protected]).
  22. Overview
  23. This package provides a simple Tcl interface to the gd (bitmap drawing)
  24. package. It includes an interface to all the gd functions
  25. and data structures from Tcl commands.
  26. Reference
  27. One Tcl command, 'gd', is added. All gd package actions are
  28. sub-commands (or "options" in Tcl terminology) of this command.
  29. Each active gd image is referred to with a "handle". The handle is a
  30. name of the form gd# (e.g., gd0) returned by the gd create options.
  31. Almost all the gd commands take a handle as the first argument (after
  32. the option). All the drawing commands take a color_idx as the next
  33. argument.
  34. gd create <width> <height> ?true?
  35. Return a handle to a new gdImage that is width X height.
  36. If "true" is specified, the new image is "TrueColor".
  37. gd createTrueColor <width> <height>
  38. Return a handle to a new trueColor gdImage that is width X height.
  39. gd createFromGD <file>
  40. gd createFromGD2 <file>
  41. gd createFromGIF <file>
  42. gd createFromJPEG <file>
  43. gd createFromPNG <file>
  44. gd createFromWBMP <file>
  45. gd createFromXBM <file>
  46. Return a handle to a new gdImage created by reading a
  47. image in the indicate format from the filename or open TCL filehandle
  48. (except for XPM, which only accepts filenames).
  49. gd destroy <gdhandle>
  50. Destroy the gdImage referred to by gdhandle.
  51. gd writeGD <gdhandle> <file>
  52. gd writeGD2 <gdhandle> <file>
  53. gd writeGIF <gdhandle> <file>
  54. gd writeJPEG <gdhandle> <file>
  55. gd writePNG <gdhandle> <file>
  56. gd writeWBMP <gdhandle> <file>
  57. gd writeXBM <gdhandle> <file>
  58. Write the image in gdhandle to filename or open TCL filehandle in the
  59. format indicated.
  60. gd writePNGvar <gdhandle> <varname>
  61. Write the image in gdhandle to Tcl variable "varname" as a binary
  62. coded PNG object.
  63. gd interlace <gdhandle> <on-off>
  64. Make the output image interlaced (if on-off is true) or not (if
  65. on-off is false).
  66. gd color new <gdhandle> <red> <green> <blue>
  67. Allocate a new color with the given RGB values. Returns the
  68. color_idx, or \-1 on failure (256 colors already allocated).
  69. gd color exact <gdhandle> <red> <green> <blue>
  70. Find a color_idx in the image that exactly matches the given RGB
  71. color. Returns the color_idx, or \-1 if no exact match.
  72. gd color closest <gdhandle> <red> <green> <blue>
  73. Find a color in the image that is closest to the given RGB color.
  74. Guaranteed to return a color idx.
  75. gd color resolve <gdhandle> <red> <green> <blue>
  76. Return the index of the best possible effort to get a color.
  77. Guaranteed to return a color idx. Equivalent to:
  78. if {[set idx [gd color exact $gd $r $g $b]] == \-1} {
  79. if {[set idx [gd color neW $Gd $r $g $b]] == \-1} {
  80. set idx [gd color closest $gd $r $g $b]
  81. }
  82. }
  83. gd color free <gdhandle> <color_idx>
  84. Free the color at the given color_idx for reuse.
  85. gd color transparent <gdhandle> [<color_idx>]
  86. Mark the color at <color_idx> as the transparent background color. Or,
  87. return the transparent color_idx if no color_idx specified.
  88. gd color get <gdhandle> [<color_idx>]
  89. Return the RGB value at <color_idx>, or {} if it is not allocated.
  90. If <color_idx> is not specified, return a list of {color_idx R G B}
  91. values for all allocated colors.
  92. gd brush <gdhandle> <brushhandle>
  93. Set the brush image to be used for brushed lines. Transparent
  94. pixels in the brush will not change the image when the brush is
  95. applied.
  96. gd style <gdhandle> <color_idx> ...
  97. Set the line style to the list of color indices. This is
  98. interpreted in one of two ways. For a simple styled line, each
  99. color is applied to points along the line in turn. The
  100. transparent color_idx value may be used to leave gaps in the line.
  101. For a styled, brushed line, a 0 (or the transparent color_idx)
  102. means not to fill the pixel, and a non-zero value means to
  103. apply the brush.
  104. gd tile <gdhandle> <tilehandle>
  105. Set the tile image to be used for tiled fills. Transparent
  106. pixels in the tile will not change the underlying image during
  107. tiling.
  108. In all drawing functions, the color_idx is a number, or may
  109. be one of the strings styled, brushed, tiled, "styled brushed"
  110. or "brushed styled". The style, brush, or tile currently in
  111. effect will be used. Brushing and styling apply to lines,
  112. tiling to filled areas.
  113. gd set <gdhandle> <color_idx> <x> <y>
  114. Set the pixel at (x,y) to color <color_idx>.
  115. gd line <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  116. gd rectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  117. gd fillrectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  118. Draw the outline of (resp. fill) a rectangle in color <color_idx>
  119. with corners at (x1,y1) and (x2,y2).
  120. gd arc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  121. gd fillarc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  122. gd openarc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  123. gd chord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  124. gd fillchord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  125. gd openchord <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  126. gd pie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  127. gd fillpie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  128. gd openpie <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
  129. All describe an arc based shape in color <color_idx>, centered at (cx,cy)
  130. in a rectangle width x height, starting at start degrees and ending
  131. at end degrees.
  132. arc - Just the curved line.
  133. fillarc - (Intended to be a fill between the curve and chord,
  134. but gd doesn't do that) - Same as pie.
  135. openarc - Outline shape with curve and chord.
  136. chord - Straight line chord between the ends of the curve,
  137. but without showing the curve.
  138. fillchord - Filled triangle between chord and center.
  139. openchord - Outline triangle between chord and center.
  140. pie - Filled pie segment between curve and center.
  141. fillpie - Same as pie.
  142. openpie - Outline pie segment between curve and center.
  143. gd polygon <gdhandle> <color_idx> <x1> <y1> ...
  144. gd fillpolygon <gdhandle> <color_idx> <x1> <y1> ...
  145. Draw the outline of, or fill, a polygon specified by the x, y
  146. coordinate list. There must be at least 3 points specified.
  147. gd fill <gdhandle> <color_idx> <x> <y>
  148. gd fill <gdhandle> <color_idx> <x> <y> <borderindex>
  149. Fill with color <color_idx>, starting from (x,y) within a region of
  150. pixels all the color of the pixel at (x,y) (resp., within a
  151. border colored borderindex).
  152. gd size <gdhandle>
  153. Returns a list {width height} of the image.
  154. gd text <gdhandle> <color_idx> <fontlist> <size> <angle> <x> <y> <string>
  155. Draw text using <fontlist> in color <color_idx>,
  156. with pointsize <size>, rotation in radians <angle>, with lower left
  157. corner at (x,y). String may contain UTF8 sequences like: "&#192;"
  158. Returns 4 corner coords of bounding rectangle.
  159. Use gdhandle = {} to get boundary without rendering.
  160. Use negative of color_idx to disable antialiasing.
  161. <fontlist> may contain either a full pathname of a font, including
  162. ".ttf" extension, or it may contain a space-separated list of
  163. alternate names for a font, without the ".ttf". e.g.
  164. "Times-Roman times"
  165. The file <name>.ttf corresponding to one of the alternate names
  166. must be found in the built-in DEFAULT_FONTPATH, or in the
  167. fontpath specified in a GDFONTPATH environment variable.
  168. gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <w> <h>
  169. Copy a subimage from srchandle(srcx, srcy)
  170. to desthandle(destx, desty), size w x h.
  171. gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> \\
  172. <destw> <desth> <srcw> <srch>
  173. Copy a subimage from srchandle(srcx, srcy)
  174. to desthandle(destx, desty), and resize the subimage
  175. from srcw by srch to destw by desth.
  176. Examples
  177. The sample program from the gd documentation can be written thusly:
  178. #!/bin/sh
  179. # next line is a comment in tcl \
  180. exec tclsh "$0" ${1+"$@"}
  181. package require Gdtclft
  182. ################################################################
  183. # Sample gdtcl program - from gdtclft man page
  184. #
  185. # Create a 64 x 64 image
  186. set im [gd create 64 64]
  187. # Get black and white as colors. Black is the background color because
  188. # it is allocated first from a new image.
  189. set black [gd color new $im 0 0 0]
  190. set white [gd color new $im 255 255 255]
  191. # Draw a line from upper left to lower right
  192. gd line $im $white 0 0 63 63
  193. # Open a file for writing (Tcl on Unix, at least, doesn't support 'wb' mode)
  194. set out [open test.png w]
  195. # Output the image to the disk file
  196. gd writePNG $im $out
  197. # Close the file
  198. close $out
  199. # Destroy the image in memory
  200. gd destroy $im
  201. GDDEMO
  202. Here's the gddemo.c program translated to tcl.
  203. #!/bin/sh
  204. # next line is a comment in tcl \
  205. exec tclsh "$0" ${1+"$@"}
  206. package require Gdtclft
  207. ################################################################
  208. #
  209. # gddemo in tcl
  210. #
  211. # open demoin.png or die
  212. if {[catch {open demoin.png r} in]} {
  213. puts stderr "Can't load source image; this demo is much";
  214. puts stderr "more impressive if demoin.png is available";
  215. exit
  216. }
  217. # Create output image 128 x 128
  218. set im_out [gd create 128 128]
  219. # First color is background
  220. set white [gd color new $im_out 255 255 255]
  221. # Set transparent
  222. gd color transparent $im_out $white
  223. # Load demoin.png and paste part of it into the output image.
  224. set im_in [gd createFromPNG $in]
  225. close $in
  226. # Copy and shrink
  227. gd copy $im_out $im_in 16 16 0 0 96 96 128 128
  228. # Get some colors
  229. set red [gd color new $im_out 255 0 0]
  230. set green [gd color new $im_out 0 255 0]
  231. set blue [gd color new $im_out 0 0 255]
  232. # Draw a rectangle
  233. gd line $im_out $green 8 8 120 8
  234. gd line $im_out $green 120 8 120 120
  235. gd line $im_out $green 120 120 8 120
  236. gd line $im_out $green 8 120 8 8
  237. # Text
  238. gd text $im_out $red arial 20 0 16 16 hi
  239. gd text $im_out $red arial 20 90 23 23 hi
  240. # Circle
  241. gd arc $im_out $blue 64 64 30 10 0 360
  242. # Arc
  243. gd arc $im_out $blue 64 64 20 20 45 135
  244. # Flood fill
  245. gd fill $im_out $blue 4 4
  246. # Polygon
  247. gd fillpolygon $im_out $green 32 0 0 64 64 64
  248. # Brush. A fairly wild example also involving a line style!
  249. if {$im_in != ""} {
  250. set brush [gd create 8 8];
  251. eval [concat gd copy $brush $im_in 0 0 0 0 [gd size $brush] [gd size $im_in]]
  252. gd brush $im_out $brush
  253. # Style so they won't overprint each other.
  254. gd style $im_out "0 0 0 0 0 0 0 1"
  255. gd line $im_out "styled brushed" 0 0 128 128
  256. }
  257. # Interlace the result for "fade in" in viewers that support it
  258. gd interlace $im_out true
  259. # Write PNG
  260. set out [open demoout.png w]
  261. gd writePNG $im_out $out
  262. close $out
  263. gd destroy $im_out
  264. GDSHOW
  265. A quick Tcl procedure to display a GD image using the xv program.
  266. ################################################################
  267. # gdshow -- use xv to display an image.
  268. #
  269. # Waits until xv quits to return.
  270. #
  271. proc gdshow {gd} {
  272. set f [open "|xv -" w]
  273. catch {gd writePNG $gd $f}
  274. catch {close $f} xx
  275. if {$xx != {}} {
  276. error "XV error: $xx"
  277. }
  278. }
  279. .SH SEE ALSO
  280. You will find Thomas Boutell's documentation for the underlying GD
  281. library quite useful, especially, if you are dealing with WBMP format.