gdtclft.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379
  1. /*************************************************************************
  2. * Copyright (c) 2011 AT&T Intellectual Property
  3. * All rights reserved. This program and the accompanying materials
  4. * are made available under the terms of the Eclipse Public License v1.0
  5. * which accompanies this distribution, and is available at
  6. * https://www.eclipse.org/legal/epl-v10.html
  7. *
  8. * Contributors: Details at https://graphviz.org
  9. *************************************************************************/
  10. #include "config.h"
  11. #include "gd.h"
  12. #include <assert.h>
  13. #include <errno.h>
  14. #include <limits.h>
  15. #include <math.h>
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <tcl.h>
  20. #include <util/agxbuf.h>
  21. #include <util/startswith.h>
  22. #include <util/streq.h>
  23. #ifdef _WIN32
  24. #include <windows.h>
  25. #endif
  26. static Tcl_UpdateStringProc GdPtrTypeUpdate;
  27. static Tcl_SetFromAnyProc GdPtrTypeSet;
  28. static Tcl_ObjType GdPtrType = {.name = "gd",
  29. .updateStringProc = GdPtrTypeUpdate,
  30. .setFromAnyProc = GdPtrTypeSet};
  31. #define IMGPTR(O) (O->internalRep.otherValuePtr)
  32. /* The only two symbols exported */
  33. #ifdef GVDLL
  34. __declspec(dllexport)
  35. #endif
  36. Tcl_AppInitProc Gdtclft_Init;
  37. #ifdef GVDLL
  38. __declspec(dllexport)
  39. #endif
  40. Tcl_AppInitProc Gdtclft_SafeInit;
  41. typedef int(GdDataFunction)(Tcl_Interp *interp, int argc,
  42. Tcl_Obj *const objv[]);
  43. typedef int(GdImgFunction)(Tcl_Interp *interp, gdImagePtr gdImg, int argc,
  44. const int args[]);
  45. static GdDataFunction tclGdCreateCmd, tclGdDestroyCmd, tclGdWriteCmd,
  46. tclGdColorCmd, tclGdInterlaceCmd, tclGdSetCmd, tclGdLineCmd, tclGdRectCmd,
  47. tclGdArcCmd, tclGdFillCmd, tclGdSizeCmd, tclGdTextCmd, tclGdCopyCmd,
  48. tclGdGetCmd, tclGdWriteBufCmd, tclGdBrushCmd, tclGdStyleCmd, tclGdTileCmd,
  49. tclGdPolygonCmd;
  50. static GdImgFunction tclGdColorNewCmd, tclGdColorExactCmd, tclGdColorClosestCmd,
  51. tclGdColorResolveCmd, tclGdColorFreeCmd, tclGdColorTranspCmd,
  52. tclGdColorGetCmd;
  53. typedef struct {
  54. const char *cmd;
  55. GdDataFunction *f;
  56. unsigned int minargs, maxargs;
  57. unsigned int subcmds;
  58. unsigned int ishandle;
  59. unsigned int unsafearg;
  60. const char *usage;
  61. } cmdDataOptions;
  62. typedef struct {
  63. const char *cmd;
  64. GdImgFunction *f;
  65. unsigned int minargs, maxargs;
  66. const char *usage;
  67. } cmdImgOptions;
  68. static cmdDataOptions subcmdVec[] = {
  69. {"create", tclGdCreateCmd, 2, 3, 0, 0, 0, "width heighti ?true?"},
  70. {"createTrueColor", tclGdCreateCmd, 2, 2, 0, 0, 2, "width height"},
  71. {"createFromGD", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  72. #ifdef HAVE_LIBZ
  73. {"createFromGD2", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  74. #endif
  75. #ifdef HAVE_GD_GIF
  76. {"createFromGIF", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  77. #endif
  78. #ifdef HAVE_GD_JPEG
  79. {"createFromJPEG", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  80. #endif
  81. #ifdef HAVE_GD_PNG
  82. {"createFromPNG", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  83. #endif
  84. {"createFromWBMP", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  85. #ifdef HAVE_GD_XPM
  86. {"createFromXBM", tclGdCreateCmd, 1, 1, 0, 0, 2, "filehandle"},
  87. #endif
  88. {"destroy", tclGdDestroyCmd, 1, 1, 0, 1, 0, "gdhandle"},
  89. {"writeGD", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  90. #ifdef HAVE_LIBZ
  91. {"writeGD2", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  92. #endif
  93. #ifdef HAVE_GD_GIF
  94. {"writeGIF", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  95. #endif
  96. #ifdef HAVE_GD_JPEG
  97. {"writeJPEG", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  98. #endif
  99. #ifdef HAVE_GD_PNG
  100. {"writePNG", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  101. #endif
  102. {"writeWBMP", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  103. #ifdef HAVE_GD_XPM
  104. {"writeXBM", tclGdWriteCmd, 2, 2, 0, 1, 3, "gdhandle filehandle"},
  105. #endif
  106. #ifdef HAVE_GD_PNG
  107. {"writePNGvar", tclGdWriteBufCmd, 2, 2, 0, 1, 0, "gdhandle var"},
  108. #endif
  109. {"interlace", tclGdInterlaceCmd, 1, 2, 0, 1, 0, "gdhandle ?on-off?"},
  110. {"color", tclGdColorCmd, 2, 5, 1, 1, 0, "option values..."},
  111. {"brush", tclGdBrushCmd, 2, 2, 0, 2, 0, "gdhandle brushhandle"},
  112. {"style", tclGdStyleCmd, 2, 999, 0, 1, 0, "gdhandle color..."},
  113. {"tile", tclGdTileCmd, 2, 2, 0, 2, 0, "gdhandle tilehandle"},
  114. {"set", tclGdSetCmd, 4, 4, 0, 1, 0, "gdhandle color x y"},
  115. {"line", tclGdLineCmd, 6, 6, 0, 1, 0, "gdhandle color x1 y1 x2 y2"},
  116. {"rectangle", tclGdRectCmd, 6, 6, 0, 1, 0, "gdhandle color x1 y1 x2 y2"},
  117. {"fillrectangle", tclGdRectCmd, 6, 6, 0, 1, 0,
  118. "gdhandle color x1 y1 x2 y2"},
  119. {"arc", tclGdArcCmd, 8, 8, 0, 1, 0,
  120. "gdhandle color cx cy width height start end"},
  121. {"fillarc", tclGdArcCmd, 8, 8, 0, 1, 0,
  122. "gdhandle color cx cy width height start end"},
  123. {"openarc", tclGdArcCmd, 8, 8, 0, 1, 0,
  124. "gdhandle color cx cy width height start end"},
  125. {"chord", tclGdArcCmd, 8, 8, 0, 1, 0,
  126. "gdhandle color cx cy width height start end"},
  127. {"fillchord", tclGdArcCmd, 8, 8, 0, 1, 0,
  128. "gdhandle color cx cy width height start end"},
  129. {"openchord", tclGdArcCmd, 8, 8, 0, 1, 0,
  130. "gdhandle color cx cy width height start end"},
  131. {"pie", tclGdArcCmd, 8, 8, 0, 1, 0,
  132. "gdhandle color cx cy width height start end"},
  133. {"fillpie", tclGdArcCmd, 8, 8, 0, 1, 0,
  134. "gdhandle color cx cy width height start end"},
  135. {"openpie", tclGdArcCmd, 8, 8, 0, 1, 0,
  136. "gdhandle color cx cy width height start end"},
  137. {"polygon", tclGdPolygonCmd, 2, 999, 0, 1, 0,
  138. "gdhandle color x1 y1 x2 y2 x3 y3 ..."},
  139. {"fillpolygon", tclGdPolygonCmd, 3, 999, 0, 1, 0,
  140. "gdhandle color x1 y1 x2 y2 x3 y3 ..."},
  141. {"fill", tclGdFillCmd, 4, 5, 0, 1, 0, "gdhandle color x y ?bordercolor?"},
  142. /*
  143. * we allow null gd handles to the text command to allow program to get size
  144. * of text string, so the text command provides its own handle processing
  145. * and checking
  146. */
  147. {"text", tclGdTextCmd, 8, 8, 0, 0, 4,
  148. "gdhandle color fontname size angle x y string"},
  149. {"copy", tclGdCopyCmd, 8, 10, 0, 2, 0,
  150. "desthandle srchandle destx desty srcx srcy destw desth ?srcw srch?"},
  151. {"get", tclGdGetCmd, 3, 3, 0, 1, 0, "gdhandle x y"},
  152. {"size", tclGdSizeCmd, 1, 1, 0, 1, 0, "gdhandle"},
  153. };
  154. static cmdImgOptions colorCmdVec[] = {
  155. {"new", tclGdColorNewCmd, 5, 5, "red green blue"},
  156. {"exact", tclGdColorExactCmd, 5, 5, "red green blue"},
  157. {"closest", tclGdColorClosestCmd, 5, 5, "red green blue"},
  158. {"resolve", tclGdColorResolveCmd, 5, 5, "red green blue"},
  159. {"free", tclGdColorFreeCmd, 3, 3, "color"},
  160. {"transparent", tclGdColorTranspCmd, 2, 3, "?color?"},
  161. {"get", tclGdColorGetCmd, 2, 3, "?color?"}};
  162. /*
  163. * Helper function to interpret color_idx values.
  164. */
  165. static int tclGd_GetColor(Tcl_Interp *interp, Tcl_Obj *obj, int *color) {
  166. int nlist, retval = TCL_OK;
  167. Tcl_Obj **theList;
  168. char *firsttag, *secondtag;
  169. /* Assume it's an integer, check other cases on failure. */
  170. if (Tcl_GetIntFromObj(interp, obj, color) == TCL_OK)
  171. return TCL_OK;
  172. else {
  173. Tcl_ResetResult(interp);
  174. if (Tcl_ListObjGetElements(interp, obj, &nlist, &theList) != TCL_OK)
  175. return TCL_ERROR;
  176. if (nlist < 1 || nlist > 2)
  177. retval = TCL_ERROR;
  178. else {
  179. firsttag = Tcl_GetString(theList[0]);
  180. switch (firsttag[0]) {
  181. case 'b':
  182. *color = gdBrushed;
  183. if (nlist == 2) {
  184. secondtag = Tcl_GetString(theList[1]);
  185. if (secondtag[0] == 's') {
  186. *color = gdStyledBrushed;
  187. } else {
  188. retval = TCL_ERROR;
  189. }
  190. }
  191. break;
  192. case 's':
  193. *color = gdStyled;
  194. if (nlist == 2) {
  195. secondtag = Tcl_GetString(theList[1]);
  196. if (secondtag[0] == 'b') {
  197. *color = gdStyledBrushed;
  198. } else {
  199. retval = TCL_ERROR;
  200. }
  201. }
  202. break;
  203. case 't':
  204. *color = gdTiled;
  205. break;
  206. default:
  207. retval = TCL_ERROR;
  208. }
  209. }
  210. }
  211. if (retval == TCL_ERROR)
  212. Tcl_SetResult(interp, "Malformed special color value", TCL_STATIC);
  213. return retval;
  214. }
  215. /*
  216. * GD composite command:
  217. *
  218. * gd create <width> <height>
  219. * Return a handle to a new gdImage that is width X height.
  220. * gd createTrueColor <width> <height>
  221. * Return a handle to a new trueColor gdImage that is width X
  222. * height. gd createFromGD <filehandle> gd createFromGD2 <filehandle> gd
  223. * createFromGIF <filehandle> gd createFromJPEG <filehandle> gd createFromPNG
  224. * <filehandle> gd createFromWBMP <filehandle> gd createFromXBM <filehandle>
  225. * Return a handle to a new gdImage created by reading an
  226. * image from the file of the indicated format
  227. * open on filehandle.
  228. *
  229. * gd destroy <gdhandle>
  230. * Destroy the gdImage referred to by gdhandle.
  231. *
  232. * gd writeGD <gdhandle> <filehandle>
  233. * gd writeGD2 <gdhandle> <filehandle>
  234. * gd writeGIF <gdhandle> <filehandle>
  235. * gd writeJPEG <gdhandle> <filehandle>
  236. * gd writePNG <gdhandle> <filehandle>
  237. * gd writeWBMP <gdhandle> <filehandle>
  238. * gd writeXBM <gdhandle> <filehandle>
  239. * Write the image in gdhandle to filehandle in the
  240. * format indicated.
  241. *
  242. * gd color new <gdhandle> <red> <green> <blue>
  243. * Allocate a new color with the given RGB values. Returns the
  244. * color_idx, or -1 on failure (256 colors already allocated).
  245. * gd color exact <gdhandle> <red> <green> <blue>
  246. * Find a color_idx in the image that exactly matches the given RGB
  247. * color. Returns the color_idx, or -1 if no exact match. gd color closest
  248. * <gdhandle> <red> <green> <blue> Find a color in the image that is closest to
  249. * the given RGB color. Guaranteed to return a color idx. gd color resolve
  250. * <gdhandle> <red> <green> <blue> Return the index of the best possible effort
  251. * to get a color. Guaranteed to return a color idx. Equivalent to: if {[set
  252. * idx [gd color exact $gd $r $g $b]] == -1} { if {[set idx [gd color neW $Gd $r
  253. * $g $b]] == -1} { set idx [gd color closest $gd $r $g $b]
  254. * }
  255. * }
  256. * gd color free <gdhandle> <color_idx>
  257. * Free the color at the given color_idx for reuse.
  258. * gd color transparent <gdhandle> <color_idx>
  259. * Mark the color_idx as the transparent background color.
  260. * gd color get <gdhandle> [<color_idx>]
  261. * Return the RGB value at <color_idx>, or {} if it is not
  262. * allocated. If <color_idx> is not specified, return a list of {color_idx R G
  263. * B} values for all allocated colors. gd color gettransparent <gdhandle> Return
  264. * the color_idx of the transparent color.
  265. *
  266. * gd brush <gdhandle> <brushhandle>
  267. * Set the brush image to be used for brushed lines. Transparent
  268. * pixels in the brush will not change the image when the brush
  269. * is applied.
  270. * gd style <gdhandle> <color_idx> ...
  271. * Set the line style to the list of color indices. This is
  272. * interpreted in one of two ways. For a simple styled line, each color is
  273. * applied to points along the line in turn. The transparent color
  274. * value may be used to leave gaps in the line. For a styled,
  275. * brushed line, a 0 (or the transparent color_idx) means not to fill the pixel,
  276. * and a non-zero value means to apply the brush.
  277. * gd tile <gdhandle> <tilehandle>
  278. * Set the tile image to be used for tiled fills. Transparent
  279. * pixels in the tile will not change the underlying image during tiling.
  280. *
  281. * In all drawing functions, the color_idx is a number, or may be one of the
  282. * strings styled, brushed, tiled, "styled brushed" or "brushed styled". The
  283. * style, brush, or tile currently in effect will be used. Brushing and
  284. * styling apply to lines, tiling to filled areas.
  285. *
  286. * gd set <gdhandle> <color_idx> <x> <y>
  287. * Set the pixel at (x,y) to color <color_idx>.
  288. * gd line <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  289. * Draw a line in color <color_idx> from (x1,y1) to (x2,y2).
  290. * gd rectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  291. * gd fillrectangle <gdhandle> <color_idx> <x1> <y1> <x2> <y2>
  292. * Draw the outline of (resp. fill) a rectangle in color
  293. * <color_idx> with corners at (x1,y1) and (x2,y2). gd arc <gdhandle>
  294. * <color_idx> <cx> <cy> <width> <height> <start> <end> gd fillarc <gdhandle>
  295. * <color_idx> <cx> <cy> <width> <height> <start> <end> Draw an arc, or filled
  296. * segment, in color <color_idx>, centered at (cx,cy) in a rectangle width x
  297. * height, starting at start degrees and ending at end degrees.
  298. * Start must be > end. gd polygon <gdhandle> <color_idx> <x1> <y1> ... gd
  299. * fillpolygon <gdhandle> <color_idx> <x1> <y1> ... Draw the outline of, or
  300. * fill, a polygon specified by the x, y coordinate list.
  301. *
  302. * gd fill <gdhandle> <color_idx> <x> <y>
  303. * gd fill <gdhandle> <color_idx> <x> <y> <borderindex>
  304. * Fill with color <color_idx>, starting from (x,y) within a region
  305. * of pixels all the color of the pixel at (x,y) (resp., within a
  306. * border colored borderindex).
  307. *
  308. * gd size <gdhandle>
  309. * Returns a list {width height} of the image.
  310. *
  311. * gd text <gdhandle> <color_idx> <fontname> <size> <angle> <x> <y> <string>
  312. * Draw text using <fontname> in color <color_idx>,
  313. * with pointsize <size>, rotation in radians <angle>, with lower left
  314. * corner at (x,y). String may contain UTF8 sequences like: "&#192;"
  315. * Returns 4 corner coords of bounding rectangle.
  316. * Use gdhandle = {} to get boundary without rendering.
  317. * Use negative of color_idx to disable antialiasing.
  318. *
  319. * The file <fontname>.ttf must be found in the builtin DEFAULT_FONTPATH
  320. * or in the fontpath specified in a GDFONTPATH environment variable.
  321. *
  322. * gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <w> <h>
  323. * gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> \
  324. * <destw> <desth> <srcw> <srch>
  325. * Copy a subimage from srchandle(srcx, srcy) to
  326. * desthandle(destx, desty), size w x h. Or, resize the subimage
  327. * in copying from srcw x srch to destw x desth.
  328. *
  329. */
  330. static int gdCmd(ClientData clientData, Tcl_Interp *interp, int argc,
  331. Tcl_Obj *const objv[]) {
  332. unsigned int argi;
  333. size_t subi;
  334. /* Check for subcommand. */
  335. if (argc < 2) {
  336. Tcl_SetResult(interp, "wrong # args: should be \"gd option ...\"",
  337. TCL_STATIC);
  338. return TCL_ERROR;
  339. }
  340. /* Find the subcommand. */
  341. for (subi = 0; subi < sizeof(subcmdVec) / sizeof(subcmdVec[0]); subi++) {
  342. if (streq(subcmdVec[subi].cmd, Tcl_GetString(objv[1]))) {
  343. /* Check arg count. */
  344. if ((unsigned)argc - 2 < subcmdVec[subi].minargs ||
  345. (unsigned)argc - 2 > subcmdVec[subi].maxargs) {
  346. Tcl_WrongNumArgs(interp, 2, objv, subcmdVec[subi].usage);
  347. return TCL_ERROR;
  348. }
  349. /* Check for valid handle(s). */
  350. if (subcmdVec[subi].ishandle > 0) {
  351. /* Check each handle to see if it's a valid handle. */
  352. if (2 + subcmdVec[subi].subcmds + subcmdVec[subi].ishandle >
  353. (unsigned)argc) {
  354. Tcl_SetResult(interp, "GD handle(s) not specified", TCL_STATIC);
  355. return TCL_ERROR;
  356. }
  357. for (argi = 2 + subcmdVec[subi].subcmds;
  358. argi < (2 + subcmdVec[subi].subcmds + subcmdVec[subi].ishandle);
  359. argi++) {
  360. if (objv[argi]->typePtr != &GdPtrType &&
  361. GdPtrTypeSet(interp, objv[argi]) != TCL_OK)
  362. return TCL_ERROR;
  363. }
  364. }
  365. /*
  366. * If we are operating in a safe interpreter, check,
  367. * if this command is suspect -- and only let existing
  368. * filehandles through, if so.
  369. */
  370. if (clientData != NULL && subcmdVec[subi].unsafearg != 0) {
  371. const char *fname = Tcl_GetString(objv[subcmdVec[subi].unsafearg]);
  372. if (!Tcl_IsChannelExisting(fname)) {
  373. Tcl_AppendResult(interp, "Access to ", fname,
  374. " not allowed in safe interpreter", NULL);
  375. return TCL_ERROR;
  376. }
  377. }
  378. /* Call the subcommand function. */
  379. return (*subcmdVec[subi].f)(interp, argc, objv);
  380. }
  381. }
  382. /* If we get here, the option doesn't match. */
  383. Tcl_AppendResult(interp, "bad option \"", Tcl_GetString(objv[1]),
  384. "\": should be ", 0);
  385. for (subi = 0; subi < sizeof(subcmdVec) / sizeof(subcmdVec[0]); subi++)
  386. Tcl_AppendResult(interp, (subi > 0 ? ", " : ""), subcmdVec[subi].cmd, 0);
  387. return TCL_ERROR;
  388. }
  389. static int tclGdCreateCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  390. int w, h;
  391. gdImagePtr im = NULL;
  392. char *cmd;
  393. Tcl_Obj *result;
  394. int fileByName;
  395. cmd = Tcl_GetString(objv[1]);
  396. if (streq(cmd, "create")) {
  397. int trueColor = 0;
  398. if (Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK)
  399. return TCL_ERROR;
  400. if (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)
  401. return TCL_ERROR;
  402. /* An optional argument may specify true for "TrueColor" */
  403. if (argc == 5 &&
  404. Tcl_GetBooleanFromObj(interp, objv[4], &trueColor) == TCL_ERROR)
  405. return TCL_ERROR;
  406. if (trueColor)
  407. im = gdImageCreateTrueColor(w, h);
  408. else
  409. im = gdImageCreate(w, h);
  410. if (im == NULL) {
  411. char buf[255];
  412. snprintf(buf, sizeof(buf), "GD unable to allocate %d X %d image", w, h);
  413. Tcl_SetResult(interp, buf, TCL_VOLATILE);
  414. return TCL_ERROR;
  415. }
  416. } else if (streq(cmd, "createTrueColor")) {
  417. if (Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK)
  418. return TCL_ERROR;
  419. if (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)
  420. return TCL_ERROR;
  421. im = gdImageCreateTrueColor(w, h);
  422. if (im == NULL) {
  423. char buf[255];
  424. snprintf(buf, sizeof(buf), "GD unable to allocate %d X %d image", w, h);
  425. Tcl_SetResult(interp, buf, TCL_VOLATILE);
  426. return TCL_ERROR;
  427. }
  428. } else {
  429. char *arg2 = Tcl_GetString(objv[2]);
  430. fileByName = 0; /* first try to get file from open channel */
  431. FILE *filePtr = NULL;
  432. #if !defined(_WIN32)
  433. ClientData clientdata;
  434. if (Tcl_GetOpenFile(interp, arg2, 0, 1, &clientdata) == TCL_OK) {
  435. filePtr = (FILE *)clientdata;
  436. }
  437. #endif
  438. if (filePtr == NULL) {
  439. /* Not a channel, or Tcl_GetOpenFile() not supported.
  440. * See if we can open directly.
  441. */
  442. if ((filePtr = fopen(arg2, "rb")) == NULL) {
  443. return TCL_ERROR;
  444. }
  445. fileByName++;
  446. Tcl_ResetResult(interp);
  447. }
  448. /* Read file */
  449. if (streq(&cmd[10], "GD")) {
  450. im = gdImageCreateFromGd(filePtr);
  451. #ifdef HAVE_LIBZ
  452. } else if (streq(&cmd[10], "GD2")) {
  453. im = gdImageCreateFromGd2(filePtr);
  454. #endif
  455. #ifdef HAVE_GD_GIF
  456. } else if (streq(&cmd[10], "GIF")) {
  457. im = gdImageCreateFromGif(filePtr);
  458. #endif
  459. #ifdef HAVE_GD_JPEG
  460. } else if (streq(&cmd[10], "JPEG")) {
  461. im = gdImageCreateFromJpeg(filePtr);
  462. #endif
  463. #ifdef HAVE_GD_PNG
  464. } else if (streq(&cmd[10], "PNG")) {
  465. im = gdImageCreateFromPng(filePtr);
  466. #endif
  467. } else if (streq(&cmd[10], "WBMP")) {
  468. im = gdImageCreateFromWBMP(filePtr);
  469. #ifdef HAVE_GD_XPM
  470. } else if (streq(&cmd[10], "XBM")) {
  471. im = gdImageCreateFromXbm(filePtr);
  472. #endif
  473. } else {
  474. Tcl_AppendResult(interp, cmd + 10, "unrecognizable format requested",
  475. NULL);
  476. if (fileByName) {
  477. fclose(filePtr);
  478. }
  479. return TCL_ERROR;
  480. }
  481. if (fileByName) {
  482. fclose(filePtr);
  483. }
  484. if (im == NULL) {
  485. Tcl_AppendResult(interp, "GD unable to read image file '", arg2, "` as ",
  486. cmd + 10, NULL);
  487. return TCL_ERROR;
  488. }
  489. }
  490. result = Tcl_NewObj();
  491. IMGPTR(result) = im;
  492. result->typePtr = &GdPtrType;
  493. result->bytes = NULL;
  494. Tcl_SetObjResult(interp, result);
  495. return TCL_OK;
  496. }
  497. static int tclGdDestroyCmd(Tcl_Interp *interp, int argc,
  498. Tcl_Obj *const objv[]) {
  499. (void)interp;
  500. (void)argc;
  501. gdImagePtr im;
  502. /* Get the image pointer and destroy it */
  503. im = IMGPTR(objv[2]);
  504. gdImageDestroy(im);
  505. return TCL_OK;
  506. }
  507. static int tclGdWriteCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  508. gdImagePtr im;
  509. const char *cmd, *fname;
  510. int fileByName;
  511. int arg4;
  512. cmd = Tcl_GetString(objv[1]);
  513. if (cmd[5] == 'J' || cmd[5] == 'W') {
  514. /* JPEG and WBMP expect an extra (integer) argument */
  515. if (argc < 5) {
  516. if (cmd[5] == 'J')
  517. arg4 = -1; /* default quality-level */
  518. else {
  519. Tcl_SetResult(interp, "WBMP saving requires the foreground pixel value",
  520. TCL_STATIC);
  521. return TCL_ERROR;
  522. }
  523. } else if (Tcl_GetIntFromObj(interp, objv[4], &arg4) != TCL_OK)
  524. return TCL_ERROR;
  525. if (cmd[5] == 'J' && argc > 4 && (arg4 > 100 || arg4 < 1)) {
  526. Tcl_SetObjResult(interp, objv[4]);
  527. Tcl_AppendResult(interp,
  528. ": JPEG image quality, if specified, must be an integer "
  529. "from 1 to 100, or -1 for default",
  530. NULL);
  531. return TCL_ERROR;
  532. }
  533. /* XXX no error-checking for the WBMP case here */
  534. }
  535. /* Get the image pointer. */
  536. im = IMGPTR(objv[2]);
  537. fname = Tcl_GetString(objv[3]);
  538. /* Get the file reference. */
  539. fileByName = 0; /* first try to get file from open channel */
  540. FILE *filePtr = NULL;
  541. #if !defined(_WIN32)
  542. ClientData clientdata;
  543. if (Tcl_GetOpenFile(interp, fname, 1, 1, &clientdata) == TCL_OK) {
  544. filePtr = (FILE *)clientdata;
  545. }
  546. #endif
  547. if (filePtr == NULL) {
  548. /* Not a channel, or Tcl_GetOpenFile() not supported.
  549. * See if we can open directly.
  550. */
  551. fileByName++;
  552. if ((filePtr = fopen(fname, "wb")) == NULL) {
  553. Tcl_AppendResult(interp, "could not open :", fname,
  554. "': ", strerror(errno), NULL);
  555. return TCL_ERROR;
  556. }
  557. Tcl_ResetResult(interp);
  558. }
  559. /*
  560. * Write IM to OUTFILE as a JFIF-formatted JPEG image, using quality
  561. * JPEG_QUALITY. If JPEG_QUALITY is in the range 0-100, increasing values
  562. * represent higher quality but also larger image size. If JPEG_QUALITY is
  563. * negative, the IJG JPEG library's default quality is used (which
  564. * should be near optimal for many applications). See the IJG JPEG
  565. * library documentation for more details. */
  566. /* Do it. */
  567. if (streq(&cmd[5], "GD")) {
  568. gdImageGd(im, filePtr);
  569. } else if (streq(&cmd[5], "GD2")) {
  570. #ifdef HAVE_LIBZ
  571. #define GD2_CHUNKSIZE 128
  572. #define GD2_COMPRESSED 2
  573. gdImageGd2(im, filePtr, GD2_CHUNKSIZE, GD2_COMPRESSED);
  574. #endif
  575. #ifdef HAVE_GD_GIF
  576. } else if (streq(&cmd[5], "GIF")) {
  577. gdImageGif(im, filePtr);
  578. #endif
  579. #ifdef HAVE_GD_JPEG
  580. } else if (streq(&cmd[5], "JPEG")) {
  581. #define JPEG_QUALITY -1
  582. gdImageJpeg(im, filePtr, JPEG_QUALITY);
  583. #endif
  584. #ifdef HAVE_GD_PNG
  585. } else if (streq(&cmd[5], "PNG")) {
  586. gdImagePng(im, filePtr);
  587. #endif
  588. } else if (streq(&cmd[5], "WBMP")) {
  589. /* Assume the color closest to black is the foreground
  590. color for the B&W wbmp image. */
  591. int foreground = gdImageColorClosest(im, 0, 0, 0);
  592. gdImageWBMP(im, foreground, filePtr);
  593. } else {
  594. /* cannot happen - but would result in an empty output file */
  595. }
  596. if (fileByName) {
  597. fclose(filePtr);
  598. } else {
  599. fflush(filePtr);
  600. }
  601. return TCL_OK;
  602. }
  603. static int tclGdInterlaceCmd(Tcl_Interp *interp, int argc,
  604. Tcl_Obj *const objv[]) {
  605. gdImagePtr im;
  606. int on_off;
  607. /* Get the image pointer. */
  608. im = IMGPTR(objv[2]);
  609. if (argc == 4) {
  610. /* Get the on_off values. */
  611. if (Tcl_GetBooleanFromObj(interp, objv[3], &on_off) != TCL_OK)
  612. return TCL_ERROR;
  613. /* Do it. */
  614. gdImageInterlace(im, on_off);
  615. } else {
  616. /* Get the current state. */
  617. on_off = gdImageGetInterlaced(im);
  618. }
  619. Tcl_SetObjResult(interp, Tcl_NewBooleanObj(on_off));
  620. return TCL_OK;
  621. }
  622. static int tclGdColorCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  623. gdImagePtr im;
  624. int subi, nsub, i, args[3];
  625. nsub = sizeof(colorCmdVec) / sizeof(colorCmdVec[0]);
  626. if (argc >= 3) {
  627. /* Find the subcommand. */
  628. for (subi = 0; subi < nsub; subi++) {
  629. if (streq(colorCmdVec[subi].cmd, Tcl_GetString(objv[2]))) {
  630. /* Check arg count. */
  631. if ((unsigned)argc - 2 < colorCmdVec[subi].minargs ||
  632. (unsigned)argc - 2 > colorCmdVec[subi].maxargs) {
  633. Tcl_WrongNumArgs(interp, 3, objv, colorCmdVec[subi].usage);
  634. return TCL_ERROR;
  635. }
  636. /* Get the image pointer. */
  637. im = IMGPTR(objv[3]);
  638. /* Parse off integer arguments.
  639. * 1st 4 are gd color <opt> <handle>
  640. */
  641. for (i = 0; i < argc - 4; i++) {
  642. if (Tcl_GetIntFromObj(interp, objv[i + 4], &args[i]) != TCL_OK) {
  643. /* gd text uses -ve colors to turn off anti-aliasing */
  644. if (args[i] < -255 || args[i] > 255) {
  645. Tcl_SetResult(interp, "argument out of range 0-255", TCL_STATIC);
  646. return TCL_ERROR;
  647. }
  648. }
  649. }
  650. /* Call the subcommand function. */
  651. return colorCmdVec[subi].f(interp, im, argc - 4, args);
  652. }
  653. }
  654. }
  655. /* If we get here, the option doesn't match. */
  656. if (argc > 2) {
  657. Tcl_AppendResult(interp, "bad option \"", Tcl_GetString(objv[2]),
  658. "\": ", 0);
  659. } else {
  660. Tcl_AppendResult(interp, "wrong # args: ", 0);
  661. }
  662. Tcl_AppendResult(interp, "should be ", 0);
  663. for (subi = 0; subi < nsub; subi++)
  664. Tcl_AppendResult(interp, subi > 0 ? ", " : "", colorCmdVec[subi].cmd, 0);
  665. return TCL_ERROR;
  666. }
  667. static int tclGdColorNewCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  668. const int args[]) {
  669. (void)argc;
  670. int color;
  671. color = gdImageColorAllocate(im, args[0], args[1], args[2]);
  672. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  673. return TCL_OK;
  674. }
  675. static int tclGdColorExactCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  676. const int args[]) {
  677. (void)argc;
  678. int color;
  679. color = gdImageColorExact(im, args[0], args[1], args[2]);
  680. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  681. return TCL_OK;
  682. }
  683. static int tclGdColorClosestCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  684. const int args[]) {
  685. (void)argc;
  686. int color;
  687. color = gdImageColorClosest(im, args[0], args[1], args[2]);
  688. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  689. return TCL_OK;
  690. }
  691. static int tclGdColorResolveCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  692. const int args[]) {
  693. (void)argc;
  694. int color;
  695. color = gdImageColorResolve(im, args[0], args[1], args[2]);
  696. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  697. return TCL_OK;
  698. }
  699. static int tclGdColorFreeCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  700. const int args[]) {
  701. (void)interp;
  702. (void)argc;
  703. gdImageColorDeallocate(im, args[0]);
  704. return TCL_OK;
  705. }
  706. static int tclGdColorTranspCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  707. const int args[]) {
  708. int color;
  709. if (argc > 0) {
  710. color = args[0];
  711. gdImageColorTransparent(im, color);
  712. } else {
  713. color = gdImageGetTransparent(im);
  714. }
  715. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  716. return TCL_OK;
  717. }
  718. static int tclGdColorGetCmd(Tcl_Interp *interp, gdImagePtr im, int argc,
  719. const int args[]) {
  720. int i, ncolors;
  721. Tcl_Obj *tuple[4], *result;
  722. ncolors = gdImageColorsTotal(im);
  723. /* IF one arg, return the single color, else return list of all colors. */
  724. if (argc == 1) {
  725. i = args[0];
  726. if (i >= ncolors || im->open[i]) {
  727. Tcl_SetResult(interp, "No such color", TCL_STATIC);
  728. return TCL_ERROR;
  729. }
  730. tuple[0] = Tcl_NewIntObj(i);
  731. tuple[1] = Tcl_NewIntObj(gdImageRed(im, i));
  732. tuple[2] = Tcl_NewIntObj(gdImageGreen(im, i));
  733. tuple[3] = Tcl_NewIntObj(gdImageBlue(im, i));
  734. Tcl_SetObjResult(interp, Tcl_NewListObj(4, tuple));
  735. } else {
  736. result = Tcl_NewListObj(0, NULL);
  737. for (i = 0; i < ncolors; i++) {
  738. if (im->open[i])
  739. continue;
  740. tuple[0] = Tcl_NewIntObj(i);
  741. tuple[1] = Tcl_NewIntObj(gdImageRed(im, i));
  742. tuple[2] = Tcl_NewIntObj(gdImageGreen(im, i));
  743. tuple[3] = Tcl_NewIntObj(gdImageBlue(im, i));
  744. Tcl_ListObjAppendElement(NULL, result, Tcl_NewListObj(4, tuple));
  745. }
  746. Tcl_SetObjResult(interp, result);
  747. }
  748. return TCL_OK;
  749. }
  750. static int tclGdBrushCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  751. (void)interp;
  752. (void)argc;
  753. gdImagePtr im, imbrush;
  754. /* Get the image pointers. */
  755. im = IMGPTR(objv[2]);
  756. imbrush = IMGPTR(objv[3]);
  757. /* Do it. */
  758. gdImageSetBrush(im, imbrush);
  759. return TCL_OK;
  760. }
  761. static int tclGdTileCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  762. (void)interp;
  763. (void)argc;
  764. gdImagePtr im, tile;
  765. /* Get the image pointers. */
  766. im = IMGPTR(objv[2]);
  767. tile = IMGPTR(objv[3]);
  768. /* Do it. */
  769. gdImageSetTile(im, tile);
  770. return TCL_OK;
  771. }
  772. static int tclGdStyleCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  773. gdImagePtr im;
  774. int ncolor, *colors = NULL, i;
  775. Tcl_Obj **colorObjv =
  776. (Tcl_Obj **)(&objv[3]); /* By default, colors are listed in objv. */
  777. int retval = TCL_OK;
  778. /* Get the image pointer. */
  779. im = IMGPTR(objv[2]);
  780. /* Figure out how many colors in the style list and allocate memory. */
  781. ncolor = argc - 3;
  782. /* If only one argument, treat it as a list. */
  783. if (ncolor == 1)
  784. if (Tcl_ListObjGetElements(interp, objv[3], &ncolor, &colorObjv) != TCL_OK)
  785. return TCL_ERROR;
  786. colors = (int *)Tcl_Alloc(ncolor * sizeof(int));
  787. /* Get the color values. */
  788. for (i = 0; i < ncolor; i++)
  789. if (Tcl_GetIntFromObj(interp, colorObjv[i], &colors[i]) != TCL_OK) {
  790. retval = TCL_ERROR;
  791. break;
  792. }
  793. /* Call the Style function if no error. */
  794. if (retval == TCL_OK)
  795. gdImageSetStyle(im, colors, ncolor);
  796. /* Free the colors. */
  797. if (colors != NULL)
  798. Tcl_Free((char *)colors);
  799. return retval;
  800. }
  801. static int tclGdSetCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  802. (void)argc;
  803. gdImagePtr im;
  804. int color, x, y;
  805. /* Get the image pointer. */
  806. im = IMGPTR(objv[2]);
  807. /* Get the color, x, y values. */
  808. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  809. return TCL_ERROR;
  810. if (Tcl_GetIntFromObj(interp, objv[4], &x) != TCL_OK)
  811. return TCL_ERROR;
  812. if (Tcl_GetIntFromObj(interp, objv[5], &y) != TCL_OK)
  813. return TCL_ERROR;
  814. /* Call the Set function. */
  815. gdImageSetPixel(im, x, y, color);
  816. return TCL_OK;
  817. }
  818. static int tclGdLineCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  819. (void)argc;
  820. gdImagePtr im;
  821. int color, x1, y1, x2, y2;
  822. /* Get the image pointer. */
  823. im = IMGPTR(objv[2]);
  824. /* Get the color, x, y values. */
  825. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  826. return TCL_ERROR;
  827. if (Tcl_GetIntFromObj(interp, objv[4], &x1) != TCL_OK)
  828. return TCL_ERROR;
  829. if (Tcl_GetIntFromObj(interp, objv[5], &y1) != TCL_OK)
  830. return TCL_ERROR;
  831. if (Tcl_GetIntFromObj(interp, objv[6], &x2) != TCL_OK)
  832. return TCL_ERROR;
  833. if (Tcl_GetIntFromObj(interp, objv[7], &y2) != TCL_OK)
  834. return TCL_ERROR;
  835. /* Call the appropriate Line function. */
  836. gdImageLine(im, x1, y1, x2, y2, color);
  837. return TCL_OK;
  838. }
  839. static int tclGdRectCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  840. (void)argc;
  841. gdImagePtr im;
  842. int color, x1, y1, x2, y2;
  843. const char *cmd;
  844. /* Get the image pointer. */
  845. im = IMGPTR(objv[2]);
  846. /* Get the color, x, y values. */
  847. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  848. return TCL_ERROR;
  849. if (Tcl_GetIntFromObj(interp, objv[4], &x1) != TCL_OK)
  850. return TCL_ERROR;
  851. if (Tcl_GetIntFromObj(interp, objv[5], &y1) != TCL_OK)
  852. return TCL_ERROR;
  853. if (Tcl_GetIntFromObj(interp, objv[6], &x2) != TCL_OK)
  854. return TCL_ERROR;
  855. if (Tcl_GetIntFromObj(interp, objv[7], &y2) != TCL_OK)
  856. return TCL_ERROR;
  857. /* Call the appropriate rectangle function. */
  858. cmd = Tcl_GetString(objv[1]);
  859. if (cmd[0] == 'r')
  860. gdImageRectangle(im, x1, y1, x2, y2, color);
  861. else
  862. gdImageFilledRectangle(im, x1, y1, x2, y2, color);
  863. return TCL_OK;
  864. }
  865. static int tclGdArcCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  866. (void)argc;
  867. gdImagePtr im;
  868. int color, cx, cy, width, height, start, end;
  869. const char *cmd;
  870. /* Get the image pointer. */
  871. im = IMGPTR(objv[2]);
  872. /* Get the color, x, y values. */
  873. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  874. return TCL_ERROR;
  875. if (Tcl_GetIntFromObj(interp, objv[4], &cx) != TCL_OK)
  876. return TCL_ERROR;
  877. if (Tcl_GetIntFromObj(interp, objv[5], &cy) != TCL_OK)
  878. return TCL_ERROR;
  879. if (Tcl_GetIntFromObj(interp, objv[6], &width) != TCL_OK)
  880. return TCL_ERROR;
  881. if (Tcl_GetIntFromObj(interp, objv[7], &height) != TCL_OK)
  882. return TCL_ERROR;
  883. if (Tcl_GetIntFromObj(interp, objv[8], &start) != TCL_OK)
  884. return TCL_ERROR;
  885. if (Tcl_GetIntFromObj(interp, objv[9], &end) != TCL_OK)
  886. return TCL_ERROR;
  887. /* Call the appropriate arc function. */
  888. cmd = Tcl_GetString(objv[1]);
  889. if (cmd[0] == 'a') /* arc */
  890. gdImageArc(im, cx, cy, width, height, start, end, color);
  891. /* This one is not really useful as gd renderers it the same as fillpie */
  892. /* It would be more useful if gd provided fill between arc and chord */
  893. else if (cmd[0] == 'f' && cmd[4] == 'a') /* fill arc */
  894. gdImageFilledArc(im, cx, cy, width, height, start, end, color, gdArc);
  895. /* this one is a kludge */
  896. else if (cmd[0] == 'o' && cmd[4] == 'a') { /* open arc */
  897. gdImageArc(im, cx, cy, width, height, start, end, color);
  898. gdImageFilledArc(im, cx, cy, width, height, start, end, color,
  899. gdChord | gdNoFill);
  900. } else if (cmd[0] == 'c') /* chord */
  901. gdImageFilledArc(im, cx, cy, width, height, start, end, color,
  902. gdChord | gdNoFill);
  903. else if (cmd[0] == 'f' && cmd[4] == 'c') /* fill chord */
  904. gdImageFilledArc(im, cx, cy, width, height, start, end, color, gdChord);
  905. else if (cmd[0] == 'o' && cmd[4] == 'c') /* open chord */
  906. gdImageFilledArc(im, cx, cy, width, height, start, end, color,
  907. gdChord | gdEdged | gdNoFill);
  908. else if (cmd[0] == 'p' ||
  909. (cmd[0] == 'f' && cmd[4] == 'p')) /* pie or fill pie */
  910. gdImageFilledArc(im, cx, cy, width, height, start, end, color, gdPie);
  911. else if (cmd[0] == 'o' && cmd[4] == 'p') /* open pie */
  912. gdImageFilledArc(im, cx, cy, width, height, start, end, color,
  913. gdPie | gdEdged | gdNoFill);
  914. return TCL_OK;
  915. }
  916. static int tclGdPolygonCmd(Tcl_Interp *interp, int argc,
  917. Tcl_Obj *const objv[]) {
  918. gdImagePtr im;
  919. int color, npoints, i;
  920. Tcl_Obj **pointObjv = (Tcl_Obj **)(&objv[4]);
  921. gdPointPtr points = NULL;
  922. int retval = TCL_OK;
  923. char *cmd;
  924. /* Get the image pointer. */
  925. im = IMGPTR(objv[2]);
  926. /* Get the color, x, y values. */
  927. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  928. return TCL_ERROR;
  929. /* Figure out how many points in the list and allocate memory. */
  930. npoints = argc - 4;
  931. /* If only one argument, treat it as a list. */
  932. if (npoints == 1)
  933. if (Tcl_ListObjGetElements(interp, objv[4], &npoints, &pointObjv) != TCL_OK)
  934. return TCL_ERROR;
  935. /* Error check size of point list. */
  936. if (npoints % 2 != 0) {
  937. Tcl_SetResult(interp, "Number of coordinates must be even", TCL_STATIC);
  938. retval = TCL_ERROR;
  939. goto out;
  940. }
  941. /* Divide by 2 to get number of points, and final error check. */
  942. npoints /= 2;
  943. if (npoints < 3) {
  944. Tcl_SetResult(interp, "Must specify at least 3 points.", TCL_STATIC);
  945. retval = TCL_ERROR;
  946. goto out;
  947. }
  948. points = (gdPointPtr)Tcl_Alloc(npoints * sizeof(gdPoint));
  949. /* Get the point values. */
  950. for (i = 0; i < npoints; i++)
  951. if (Tcl_GetIntFromObj(interp, pointObjv[i * 2], &points[i].x) != TCL_OK ||
  952. Tcl_GetIntFromObj(interp, pointObjv[i * 2 + 1], &points[i].y) !=
  953. TCL_OK) {
  954. retval = TCL_ERROR;
  955. goto out;
  956. }
  957. /* Call the appropriate polygon function. */
  958. cmd = Tcl_GetString(objv[1]);
  959. if (cmd[0] == 'p')
  960. gdImagePolygon(im, points, npoints, color);
  961. else
  962. gdImageFilledPolygon(im, points, npoints, color);
  963. out:
  964. /* Free the points. */
  965. if (points != NULL)
  966. Tcl_Free((char *)points);
  967. /* return TCL_OK; */
  968. return retval;
  969. }
  970. static int tclGdFillCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  971. gdImagePtr im;
  972. int color, x, y, border;
  973. /* Get the image pointer. */
  974. im = IMGPTR(objv[2]);
  975. /* Get the color, x, y and possibly bordercolor values. */
  976. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
  977. return TCL_ERROR;
  978. if (Tcl_GetIntFromObj(interp, objv[4], &x) != TCL_OK)
  979. return TCL_ERROR;
  980. if (Tcl_GetIntFromObj(interp, objv[5], &y) != TCL_OK)
  981. return TCL_ERROR;
  982. /* Call the appropriate fill function. */
  983. if (argc - 2 == 5) {
  984. if (Tcl_GetIntFromObj(interp, objv[6], &border) != TCL_OK)
  985. return TCL_ERROR;
  986. gdImageFillToBorder(im, x, y, border, color);
  987. } else {
  988. gdImageFill(im, x, y, color);
  989. }
  990. return TCL_OK;
  991. }
  992. static int tclGdCopyCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  993. gdImagePtr imdest, imsrc;
  994. int destx, desty, srcx, srcy, destw, desth, srcw, srch;
  995. /* Get the image pointer. */
  996. imdest = IMGPTR(objv[2]);
  997. imsrc = IMGPTR(objv[3]);
  998. /* Get the x, y, etc. values. */
  999. if (Tcl_GetIntFromObj(interp, objv[4], &destx) != TCL_OK)
  1000. return TCL_ERROR;
  1001. if (Tcl_GetIntFromObj(interp, objv[5], &desty) != TCL_OK)
  1002. return TCL_ERROR;
  1003. if (Tcl_GetIntFromObj(interp, objv[6], &srcx) != TCL_OK)
  1004. return TCL_ERROR;
  1005. if (Tcl_GetIntFromObj(interp, objv[7], &srcy) != TCL_OK)
  1006. return TCL_ERROR;
  1007. if (Tcl_GetIntFromObj(interp, objv[8], &destw) != TCL_OK)
  1008. return TCL_ERROR;
  1009. if (Tcl_GetIntFromObj(interp, objv[9], &desth) != TCL_OK)
  1010. return TCL_ERROR;
  1011. /* Call the appropriate copy function. */
  1012. if (argc - 2 == 10) {
  1013. if (Tcl_GetIntFromObj(interp, objv[10], &srcw) != TCL_OK)
  1014. return TCL_ERROR;
  1015. if (Tcl_GetIntFromObj(interp, objv[11], &srch) != TCL_OK)
  1016. return TCL_ERROR;
  1017. gdImageCopyResized(imdest, imsrc, destx, desty, srcx, srcy, destw, desth,
  1018. srcw, srch);
  1019. } else
  1020. gdImageCopy(imdest, imsrc, destx, desty, srcx, srcy, destw, desth);
  1021. return TCL_OK;
  1022. }
  1023. static int tclGdGetCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  1024. (void)argc;
  1025. gdImagePtr im;
  1026. int color, x, y;
  1027. /* Get the image pointer. */
  1028. im = IMGPTR(objv[2]);
  1029. /* Get the x, y values. */
  1030. if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK)
  1031. return TCL_ERROR;
  1032. if (Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK)
  1033. return TCL_ERROR;
  1034. /* Call the Get function. */
  1035. color = gdImageGetPixel(im, x, y);
  1036. Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
  1037. return TCL_OK;
  1038. }
  1039. static int tclGdSizeCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  1040. (void)argc;
  1041. gdImagePtr im;
  1042. Tcl_Obj *answers[2];
  1043. /* Get the image pointer. */
  1044. im = IMGPTR(objv[2]);
  1045. answers[0] = Tcl_NewIntObj(gdImageSX(im));
  1046. answers[1] = Tcl_NewIntObj(gdImageSY(im));
  1047. Tcl_SetObjResult(interp, Tcl_NewListObj(2, answers));
  1048. return TCL_OK;
  1049. }
  1050. static int tclGdTextCmd(Tcl_Interp *interp, int argc, Tcl_Obj *const objv[]) {
  1051. /* gd gdhandle color fontname size angle x y string */
  1052. gdImagePtr im;
  1053. int color, x, y;
  1054. double ptsize, angle;
  1055. char *error, *fontname;
  1056. int i, brect[8], len;
  1057. char *str;
  1058. Tcl_Obj *orect[8];
  1059. /* Get the image pointer. (an invalid or null arg[2] will result in string
  1060. size calculation but no rendering */
  1061. if (argc == 2 || (objv[2]->typePtr != &GdPtrType &&
  1062. GdPtrTypeSet(NULL, objv[2]) != TCL_OK)) {
  1063. im = NULL;
  1064. } else {
  1065. im = IMGPTR(objv[2]);
  1066. }
  1067. /* Get the color, values. */
  1068. if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK) {
  1069. return TCL_ERROR;
  1070. }
  1071. /* Get point size */
  1072. if (Tcl_GetDoubleFromObj(interp, objv[5], &ptsize) != TCL_OK) {
  1073. return TCL_ERROR;
  1074. }
  1075. /* Get rotation (radians) */
  1076. if (Tcl_GetDoubleFromObj(interp, objv[6], &angle) != TCL_OK) {
  1077. return TCL_ERROR;
  1078. }
  1079. /* get x, y position */
  1080. if (Tcl_GetIntFromObj(interp, objv[7], &x) != TCL_OK) {
  1081. return TCL_ERROR;
  1082. }
  1083. if (Tcl_GetIntFromObj(interp, objv[8], &y) != TCL_OK) {
  1084. return TCL_ERROR;
  1085. }
  1086. str = Tcl_GetStringFromObj(objv[9], &len);
  1087. fontname = Tcl_GetString(objv[4]);
  1088. gdFTUseFontConfig(1);
  1089. error = gdImageStringFT(im, brect, color, fontname, ptsize, angle, x, y, str);
  1090. if (error) {
  1091. Tcl_SetResult(interp, error, TCL_VOLATILE);
  1092. return TCL_ERROR;
  1093. }
  1094. for (i = 0; i < 8; i++) {
  1095. orect[i] = Tcl_NewIntObj(brect[i]);
  1096. }
  1097. Tcl_SetObjResult(interp, Tcl_NewListObj(8, orect));
  1098. return TCL_OK;
  1099. }
  1100. /*
  1101. * Initialize the package.
  1102. */
  1103. int Gdtclft_Init(Tcl_Interp *interp) {
  1104. #ifdef USE_TCL_STUBS
  1105. if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
  1106. return TCL_ERROR;
  1107. }
  1108. #else
  1109. if (Tcl_PkgRequire(interp, "Tcl", TCL_VERSION, 0) == NULL) {
  1110. return TCL_ERROR;
  1111. }
  1112. #endif
  1113. // inter-release Graphviz versions have a number including '~dev.' that does
  1114. // not comply with TCL version number rules, so replace this with 'b'
  1115. char adjusted_version[sizeof(PACKAGE_VERSION)] = PACKAGE_VERSION;
  1116. char *tilde_dev = strstr(adjusted_version, "~dev.");
  1117. if (tilde_dev != NULL) {
  1118. *tilde_dev = 'b';
  1119. memmove(tilde_dev + 1, tilde_dev + strlen("~dev."),
  1120. strlen(tilde_dev + strlen("~dev.")) + 1);
  1121. }
  1122. if (Tcl_PkgProvide(interp, "Gdtclft", adjusted_version) != TCL_OK) {
  1123. return TCL_ERROR;
  1124. }
  1125. Tcl_CreateObjCommand(interp, "gd", gdCmd, NULL, (Tcl_CmdDeleteProc *)NULL);
  1126. return TCL_OK;
  1127. }
  1128. int Gdtclft_SafeInit(Tcl_Interp *interp) {
  1129. Tcl_CmdInfo info;
  1130. if (Gdtclft_Init(interp) != TCL_OK ||
  1131. Tcl_GetCommandInfo(interp, "gd", &info) != 1)
  1132. return TCL_ERROR;
  1133. info.objClientData = (char *)info.objClientData + 1; /* Non-NULL */
  1134. if (Tcl_SetCommandInfo(interp, "gd", &info) != 1)
  1135. return TCL_ERROR;
  1136. return TCL_OK;
  1137. }
  1138. #ifndef __CYGWIN__
  1139. #ifdef __WIN32__
  1140. /* Define DLL entry point, standard macro */
  1141. /*
  1142. *----------------------------------------------------------------------
  1143. *
  1144. * DllEntryPoint --
  1145. *
  1146. * This wrapper function is used by Windows to invoke the
  1147. * initialization code for the DLL. If we are compiling
  1148. * with Visual C++, this routine will be renamed to DllMain.
  1149. * routine.
  1150. *
  1151. * Results:
  1152. * Returns TRUE;
  1153. *
  1154. * Side effects:
  1155. * None.
  1156. *
  1157. *----------------------------------------------------------------------
  1158. *
  1159. * @param hInst Library instance handle
  1160. * @param reason Reason this function is being called
  1161. * @param reserved Not used
  1162. */
  1163. BOOL APIENTRY DllEntryPoint(HINSTANCE hInst, DWORD reason, LPVOID reserved);
  1164. BOOL APIENTRY DllEntryPoint(HINSTANCE hInst, DWORD reason, LPVOID reserved) {
  1165. return TRUE;
  1166. }
  1167. #endif
  1168. #endif
  1169. #ifdef HAVE_GD_PNG
  1170. static int BufferSinkFunc(void *context, const char *buffer, int len) {
  1171. agxbuf *p = context;
  1172. if (len > 0) {
  1173. agxbput_n(p, buffer, (size_t)len);
  1174. }
  1175. return len;
  1176. }
  1177. static int tclGdWriteBufCmd(Tcl_Interp *interp, int argc,
  1178. Tcl_Obj *const objv[]) {
  1179. (void)argc;
  1180. agxbuf buffer = {0};
  1181. gdSink buffsink = {.sink = BufferSinkFunc, .context = &buffer};
  1182. /* Get the image pointer. */
  1183. gdImagePtr im = IMGPTR(objv[2]);
  1184. gdImagePngToSink(im, &buffsink);
  1185. const size_t buffer_length = agxblen(&buffer);
  1186. void *const result = agxbuse(&buffer);
  1187. assert(buffer_length <= INT_MAX);
  1188. Tcl_Obj *output = Tcl_NewByteArrayObj(result, (int)buffer_length);
  1189. agxbfree(&buffer);
  1190. if (output == NULL)
  1191. return TCL_ERROR;
  1192. else
  1193. Tcl_IncrRefCount(output);
  1194. if (Tcl_ObjSetVar2(interp, objv[3], NULL, output, 0) == NULL)
  1195. return TCL_ERROR;
  1196. else
  1197. return TCL_OK;
  1198. }
  1199. static void GdPtrTypeUpdate(struct Tcl_Obj *O) {
  1200. size_t len = strlen(GdPtrType.name) + (sizeof(void *) + 1) * 2 + 1;
  1201. O->bytes = Tcl_Alloc(len);
  1202. O->length = snprintf(O->bytes, len, "%s%p", GdPtrType.name, IMGPTR(O));
  1203. }
  1204. static int GdPtrTypeSet(Tcl_Interp *I, struct Tcl_Obj *O) {
  1205. if (O->bytes == NULL || O->bytes[0] == '\0' ||
  1206. !startswith(O->bytes, GdPtrType.name) ||
  1207. sscanf(O->bytes + strlen(GdPtrType.name), "%p", &IMGPTR(O)) != 1) {
  1208. if (I != NULL)
  1209. Tcl_AppendResult(I, O->bytes, " is not a ", GdPtrType.name, "-handle",
  1210. NULL);
  1211. return TCL_ERROR;
  1212. }
  1213. O->typePtr = &GdPtrType;
  1214. return TCL_OK;
  1215. }
  1216. #endif