1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
|
/*
* gdCmd.c - Interface gd to Tcl.
*
* Author: Spencer W. Thomas
* Information Technology and Networking
* University of Michigan
* Date: Fri Aug 19 1994
* Copyright (c) 1994, University of Michigan
*
* Updates by John Ellson (ellson@graphviz.org):
* - conversion to freetype text rendering
* - support for tcl8.0 Tcl_Objects
* - support for tcl8.1 Unicode
* - remove support for tcl8.0 and earlier
*
* Windows port by Bill Schongar (bills@lcdmultimedia.com)
*
* writePNGvar by David N. Welton (davidw@prosa.it)
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <tcl.h>
#include "gd.h"
#include "gdhandle.h"
#ifdef WIN32
#include <windows.h>
#endif
void *GDHandleTable;
/* global data */
typedef struct {
tblHeader_pt handleTbl;
} GdData;
static int tclGdCreateCmd(), tclGdDestroyCmd(), tclGdWriteCmd(),
tclGdColorCmd(), tclGdInterlaceCmd(), tclGdSetCmd(), tclGdLineCmd(),
tclGdRectCmd(), tclGdArcCmd(), tclGdFillCmd(), tclGdSizeCmd(),
tclGdTextCmd(), tclGdCopyCmd(), tclGdGetCmd(),
tclGdBrushCmd(), tclGdStyleCmd(), tclGdTileCmd(), tclGdPolygonCmd(),
tclGdColorNewCmd(), tclGdColorExactCmd(), tclGdColorClosestCmd(),
tclGdColorResolveCmd(), tclGdColorFreeCmd(), tclGdColorTranspCmd(),
tclGdColorGetCmd(), tclGdWriteBufCmd();
typedef struct {
char *cmd;
int (*f)();
int minargs, maxargs;
int subcmds;
int ishandle;
char *usage;
} cmdOptions;
typedef struct {
char *buf;
int buflen;
} BuffSinkContext;
static cmdOptions subcmdVec[] = {
{"create", tclGdCreateCmd, 2, 2, 0, 0,
"width height"},
{"createFromPNG", tclGdCreateCmd, 1, 1, 0, 0,
"filehandle"},
{"createFromGD", tclGdCreateCmd, 1, 1, 0, 0,
"filehandle"},
{"createFromXBM", tclGdCreateCmd, 1, 1, 0, 0,
"filehandle"},
{"destroy", tclGdDestroyCmd, 1, 1, 0, 1,
"gdhandle"},
{"writePNG", tclGdWriteCmd, 2, 2, 0, 1,
"gdhandle filehandle"},
{"writePNGvar", tclGdWriteBufCmd, 2, 2, 0, 1,
"gdhandle var"},
{"writeGD", tclGdWriteCmd, 2, 2, 0, 1,
"gdhandle filehandle"},
{"interlace", tclGdInterlaceCmd, 1, 2, 0, 1,
"gdhandle ?on-off?"},
{"color", tclGdColorCmd, 2, 5, 1, 1,
"option values..."},
{"brush", tclGdBrushCmd, 2, 2, 0, 2,
"gdhandle brushhandle"},
{"style", tclGdStyleCmd, 2, 999, 0, 1,
"gdhandle color..."},
{"tile", tclGdTileCmd, 2, 2, 0, 2,
"gdhandle tilehandle"},
{"set", tclGdSetCmd, 4, 4, 0, 1,
"gdhandle color x y"},
{"line", tclGdLineCmd, 6, 6, 0, 1,
"gdhandle color x1 y1 x2 y2"},
{"rectangle", tclGdRectCmd, 6, 6, 0, 1,
"gdhandle color x1 y1 x2 y2"},
{"fillrectangle", tclGdRectCmd, 6, 6, 0, 1,
"gdhandle color x1 y1 x2 y2"},
{"arc", tclGdArcCmd, 8, 8, 0, 1,
"gdhandle color cx cy width height start end"},
{"fillarc", tclGdArcCmd, 8, 8, 0, 1,
"gdhandle color cx cy width height start end"},
{"polygon", tclGdPolygonCmd, 2, 999, 0, 1,
"gdhandle color x1 y1 x2 y2 x3 y3 ..."},
{"fillpolygon", tclGdPolygonCmd, 3, 999, 0, 1,
"gdhandle color x1 y1 x2 y2 x3 y3 ..."},
{"fill", tclGdFillCmd, 4, 5, 0, 1,
"gdhandle color x y ?bordercolor?"},
/*
* we allow null gd handles to the text command to allow program to get size
* of text string, so the text command provides its own handle processing and checking
*/
{"text", tclGdTextCmd, 8, 8, 0, 0,
"gdhandle color fontpathname size angle x y string"},
{"copy", tclGdCopyCmd, 8, 10, 0, 2,
"desthandle srchandle destx desty srcx srcy destw desth ?srcw srch?"},
{"get", tclGdGetCmd, 3, 3, 0, 1,
"gdhandle x y"},
{"size", tclGdSizeCmd, 1, 1, 0, 1,
"gdhandle"},
};
static cmdOptions colorCmdVec[] = {
{"new", tclGdColorNewCmd, 5, 5, 1, 1,
"gdhandle red green blue"},
{"exact", tclGdColorExactCmd, 5, 5, 1, 1,
"gdhandle red green blue"},
{"closest", tclGdColorClosestCmd, 5, 5, 1, 1,
"gdhandle red green blue"},
{"resolve", tclGdColorResolveCmd, 5, 5, 1, 1,
"gdhandle red green blue"},
{"free", tclGdColorFreeCmd, 3, 3, 1, 1,
"gdhandle color"},
{"transparent", tclGdColorTranspCmd, 2, 3, 1, 1,
"gdhandle ?color?"},
{"get", tclGdColorGetCmd, 2, 3, 1, 1,
"gdhandle ?color?"}
};
/*
* Helper function to interpret color_idx values.
*/
static int
tclGd_GetColor(Tcl_Interp *interp, Tcl_Obj *obj, int *color)
{
int nlist, retval = TCL_OK;
Tcl_Obj **theList;
char *firsttag, *secondtag;
/* Assume it's an integer, check other cases on failure. */
if (Tcl_GetIntFromObj(interp, obj, color) == TCL_OK)
return TCL_OK;
else
{
Tcl_ResetResult(interp);
if (Tcl_ListObjGetElements(interp, obj, &nlist, &theList) != TCL_OK)
return TCL_ERROR;
if (nlist < 1 || nlist > 2)
retval = TCL_ERROR;
else
{
firsttag = Tcl_GetString(theList[0]);
switch (firsttag[0]) {
case 'b':
*color = gdBrushed;
if (nlist == 2) {
secondtag = Tcl_GetString(theList[1]);
if (secondtag[0] == 's') {
*color = gdStyledBrushed;
} else {
retval = TCL_ERROR;
}
}
break;
case 's':
*color = gdStyled;
if (nlist == 2) {
secondtag = Tcl_GetString(theList[1]);
if (secondtag[0] == 'b') {
*color = gdStyledBrushed;
} else {
retval = TCL_ERROR;
}
}
break;
case 't':
*color = gdTiled;
break;
default:
retval = TCL_ERROR;
}
}
}
if (retval == TCL_ERROR)
Tcl_SetResult(interp, "Malformed special color value", TCL_STATIC);
return retval;
}
/*
* GD composite command:
*
* gd create <width> <height>
* Return a handle to a new gdImage that is width X height.
* gd createFromPNG <filehandle>
* gd createFromGD <filehandle>
* gd createFromXBM <filehandle>
* Return a handle to a new gdImage created by reading a PNG
* (resp. GD or XBM) image from the file open on filehandle.
*
* gd destroy <gdhandle>
* Destroy the gdImage referred to by gdhandle.
*
* gd writePNG <gdhandle> <filehandle>
* gd writeGD <gdhandle> <filehandle>
* Write the image in gdhandle to filehandle as a PNG (resp. GD)
* file.
*
* gd color new <gdhandle> <red> <green> <blue>
* Allocate a new color with the given RGB values. Returns the
* color_idx, or -1 on failure (256 colors already allocated).
* gd color exact <gdhandle> <red> <green> <blue>
* Find a color_idx in the image that exactly matches the given RGB color.
* Returns the color_idx, or -1 if 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 given color_idx for reuse.
* gd color transparent <gdhandle> <color_idx>
* Mark the color_idx as the transparent background color.
* 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 color gettransparent <gdhandle>
* Return the color_idx of the transparent color.
*
* 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 transparent color
* value may be used to leave gaps in the line. For a styled, brushed
* line, a 0 (or the transparent color_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>
* Draw a line in color <color_idx> from (x1,y1) to (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 <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
* gd fillarc <gdhandle> <color_idx> <cx> <cy> <width> <height> <start> <end>
* Draw an arc, or filled segment, in color <color_idx>, centered at (cx,cy)
* in a rectangle width x height, starting at start degrees and ending
* at end degrees. Start must be > end.
* 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.
*
* 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> <fontpath> <size> <angle> <x> <y> <string>
* Draw text using the .ttf font in <fontpath> 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.
*
* gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> <w> <h>
* gd copy <desthandle> <srchandle> <destx> <desty> <srcx> <srcy> \
* <destw> <desth> <srcw> <srch>
* Copy a subimage from srchandle(srcx, srcy) to
* desthandle(destx, desty), size w x h. Or, resize the subimage
* in copying from srcw x srch to destw x desth.
*
*/
int
gdCmd(ClientData clientData, Tcl_Interp *interp,
int argc, Tcl_Obj *CONST objv[])
{
GdData *gdData = (GdData *)clientData;
int argi, subi;
char buf[100];
/* Check for subcommand. */
if (argc < 2) {
Tcl_SetResult(interp,
"wrong # args: should be \"gd option ...\"", TCL_STATIC);
return TCL_ERROR;
}
/* Find the subcommand. */
for (subi = 0; subi < (sizeof subcmdVec) / (sizeof subcmdVec[0]); subi++) {
if (strcmp(subcmdVec[subi].cmd, Tcl_GetString(objv[1])) == 0) {
/* Check arg count. */
if (argc - 2 < subcmdVec[subi].minargs ||
argc - 2 > subcmdVec[subi].maxargs) {
sprintf(buf, "wrong # args: should be \"gd %s %s\"",
subcmdVec[subi].cmd, subcmdVec[subi].usage);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_ERROR;
}
/* Check for valid handle(s). */
if (subcmdVec[subi].ishandle > 0) {
/* Are any handles allocated? */
if (gdData->handleTbl == NULL) {
sprintf(buf, "no such handle%s: ",
subcmdVec[subi].ishandle > 1 ? "s" : "");
Tcl_SetResult(interp, buf, TCL_VOLATILE);
for (argi = 2 + subcmdVec[subi].subcmds;
argi < 2 + subcmdVec[subi].subcmds +
subcmdVec[subi].ishandle;
argi++) {
Tcl_AppendResult(interp,
Tcl_GetString(objv[argi]), " ", 0);
}
return TCL_ERROR;
}
/* Check each handle to see if it's a valid handle. */
if (2+subcmdVec[subi].subcmds+subcmdVec[subi].ishandle > argc) {
Tcl_SetResult(interp, "GD handle(s) not specified", TCL_STATIC);
return TCL_ERROR;
}
for (argi = 2 + subcmdVec[subi].subcmds;
argi < (2 + subcmdVec[subi].subcmds +
subcmdVec[subi].ishandle);
argi++) {
if (! gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[argi])))
return TCL_ERROR;
}
}
/* Call the subcommand function. */
return (*subcmdVec[subi].f)(interp, gdData, argc, objv);
}
}
/* If we get here, the option doesn't match. */
Tcl_AppendResult(interp, "bad option \"",
Tcl_GetString(objv[1]), "\": should be ", 0);
for (subi = 0; subi < (sizeof subcmdVec) / (sizeof subcmdVec[0]); subi++)
Tcl_AppendResult(interp, (subi > 0 ? ", " : ""),
subcmdVec[subi].cmd, 0);
return TCL_ERROR;
}
static int
tclGdCreateCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
int w, h;
gdImagePtr im;
FILE *filePtr;
ClientData clientdata;
char *cmd, buf[50];
int fileByName;
cmd = Tcl_GetString(objv[1]);
if (strcmp(cmd, "create") == 0) {
if (Tcl_GetIntFromObj(interp, objv[2], &w) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[3], &h) != TCL_OK)
return TCL_ERROR;
im = gdImageCreate(w, h);
if (im == NULL)
{
sprintf(buf, "GD unable to allocate %d X %d image", w, h);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_ERROR;
}
} else {
fileByName = 0; /* first try to get file from open channel */
if (Tcl_GetOpenFile(interp, Tcl_GetString(objv[2]), 0, 1, &clientdata) == TCL_OK) {
filePtr = (FILE *)clientdata;
} else {
/* Not a channel, or Tcl_GetOpenFile() not supported.
* See if we can open directly.
*/
fileByName++;
if ((filePtr = fopen(Tcl_GetString(objv[2]),"rb")) == NULL) {
return TCL_ERROR;
}
Tcl_ResetResult(interp);
}
/* Read PNG, XBM, or GD file? */
if (cmd[10] == 'P') {
im = gdImageCreateFromPng(filePtr);
} else if (cmd[10] == 'X') {
im = gdImageCreateFromXbm(filePtr);
} else {
im = gdImageCreateFromGd(filePtr);
}
if (fileByName) {
fclose(filePtr);
}
if (im == NULL) {
Tcl_SetResult(interp,"GD unable to read image file", TCL_STATIC);
return TCL_ERROR;
}
}
*(gdImagePtr *)(gdHandleAlloc(gdData->handleTbl, buf)) = im;
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
}
static int
tclGdDestroyCmd(Tcl_Interp *interp, GdData *gdData, int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
void *hdl;
/* Get the handle, and the image pointer. */
hdl = (void *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
im = *(gdImagePtr *)hdl;
/* Release the handle, destroy the image. */
gdHandleFree(gdData->handleTbl, hdl);
gdImageDestroy(im);
return TCL_OK;
}
static int
tclGdWriteCmd(Tcl_Interp *interp, GdData *gdData, int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
FILE *filePtr;
ClientData clientdata;
char *cmd;
int fileByName;
cmd = Tcl_GetString(objv[1]);
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the file reference. */
fileByName = 0; /* first try to get file from open channel */
if (Tcl_GetOpenFile(interp, Tcl_GetString(objv[3]), 1, 1, &clientdata) == TCL_OK) {
filePtr = (FILE *)clientdata;
} else {
/* Not a channel, or Tcl_GetOpenFile() not supported.
* See if we can open directly.
*/
fileByName++;
if ((filePtr = fopen(Tcl_GetString(objv[3]),"wb")) == NULL) {
return TCL_ERROR;
}
Tcl_ResetResult(interp);
}
/* Do it. */
if (cmd[5] == 'P') {
gdImagePng(im, filePtr);
} else {
gdImageGd(im, filePtr);
}
if (fileByName) {
fclose(filePtr);
} else {
fflush(filePtr);
}
return TCL_OK;
}
static int
tclGdInterlaceCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int on_off;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
if (argc == 4) {
/* Get the on_off values. */
if (Tcl_GetBooleanFromObj(interp, objv[3], &on_off) != TCL_OK)
return TCL_ERROR;
/* Do it. */
gdImageInterlace(im, on_off);
} else {
/* Get the current state. */
on_off = gdImageGetInterlaced(im);
}
Tcl_SetObjResult(interp, Tcl_NewBooleanObj(on_off));
return TCL_OK;
}
static int
tclGdColorCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int subi, nsub, i, args[3];
nsub = (sizeof colorCmdVec) / (sizeof colorCmdVec[0]);
if (argc >= 3) {
/* Find the subcommand. */
for (subi = 0; subi < nsub; subi++)
{
if (strcmp(colorCmdVec[subi].cmd, Tcl_GetString(objv[2])) == 0)
{
/* Check arg count. */
if (argc - 2 < colorCmdVec[subi].minargs ||
argc - 2 > colorCmdVec[subi].maxargs)
{
Tcl_AppendResult(interp,
"wrong # args: should be \"gd color ",
colorCmdVec[subi].cmd, " ",
colorCmdVec[subi].usage, "\"", 0);
return TCL_ERROR;
}
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[3]));
/* Parse off integer arguments.
* 1st 4 are gd color <opt> <handle>
*/
for (i = 0; i < argc - 4; i++) {
if (Tcl_GetIntFromObj(interp, objv[i+4], &args[i]) != TCL_OK) {
#if 0
if (args[i] < 0 || args[i] > 255) {
#else
/* gd text uses -ve colors to turn off anti-aliasing */
if (args[i] < -255 || args[i] > 255) {
#endif
Tcl_SetResult(interp, "argument out of range 0-255", TCL_STATIC);
return TCL_ERROR;
}
}
}
/* Call the subcommand function. */
return (*colorCmdVec[subi].f)(interp, im, argc - 4, args);
}
}
}
/* If we get here, the option doesn't match. */
if (argc > 2) {
Tcl_AppendResult(interp, "bad option \"",
Tcl_GetString(objv[2]), "\": ", 0);
} else {
Tcl_AppendResult(interp, "wrong # args: ", 0);
}
Tcl_AppendResult(interp, "should be ", 0);
for (subi = 0; subi < nsub; subi++)
Tcl_AppendResult(interp, (subi > 0 ? ", " : ""),
colorCmdVec[subi].cmd, 0);
return TCL_ERROR;
}
static int
tclGdColorNewCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
int color;
color = gdImageColorAllocate(im, args[0], args[1], args[2]);
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdColorExactCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
int color;
color = gdImageColorExact(im, args[0], args[1], args[2]);
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdColorClosestCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
int color;
color = gdImageColorClosest(im, args[0], args[1], args[2]);
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdColorResolveCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
int color;
color = gdImageColorResolve(im, args[0], args[1], args[2]);
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdColorFreeCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
gdImageColorDeallocate(im, args[0]);
return TCL_OK;
}
static int
tclGdColorTranspCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
int color;
if (argc > 0) {
color = args[0];
gdImageColorTransparent(im, color);
}
else {
color = gdImageGetTransparent(im);
}
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdColorGetCmd(Tcl_Interp *interp, gdImagePtr im, int argc, int args[])
{
char buf[30];
int i;
/* IF one arg, return the single color, else return list of all colors. */
if (argc == 1)
{
i = args[0];
if (i >= gdImageColorsTotal(im) || im->open[i]) {
Tcl_SetResult(interp, "No such color", TCL_STATIC);
return TCL_ERROR;
}
sprintf(buf, "%d %d %d %d", i,
gdImageRed(im,i),
gdImageGreen(im,i),
gdImageBlue(im,i));
Tcl_SetResult(interp, buf, TCL_VOLATILE);
} else {
for (i = 0; i < gdImageColorsTotal(im); i++)
{
if (im->open[i])
continue;
sprintf(buf, "%d %d %d %d", i,
gdImageRed(im,i),
gdImageGreen(im,i),
gdImageBlue(im,i));
Tcl_AppendElement(interp, buf);
}
}
return TCL_OK;
}
static int
tclGdBrushCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im, imbrush;
/* Get the image pointers. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
imbrush = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[3]));
/* Do it. */
gdImageSetBrush(im, imbrush);
return TCL_OK;
}
static int
tclGdTileCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im, tile;
/* Get the image pointers. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
tile = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[3]));
/* Do it. */
gdImageSetTile(im, tile);
return TCL_OK;
}
static int
tclGdStyleCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int ncolor, *colors = NULL, i;
Tcl_Obj **colorObjv = (Tcl_Obj **)(&objv[3]); /* By default, colors are listed in objv. */
int retval = TCL_OK;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Figure out how many colors in the style list and allocate memory. */
ncolor = argc - 3;
/* If only one argument, treat it as a list. */
if (ncolor == 1)
if (Tcl_ListObjGetElements(interp, objv[3],
&ncolor, &colorObjv) != TCL_OK)
return TCL_ERROR;
colors = (int *)Tcl_Alloc(ncolor * sizeof(int));
if (colors == NULL)
{
Tcl_SetResult(interp, "Memory allocation failed", TCL_STATIC);
retval = TCL_ERROR;
goto out;
}
/* Get the color values. */
for (i = 0; i < ncolor; i++)
if (Tcl_GetIntFromObj(interp, colorObjv[i], &colors[i]) != TCL_OK)
{
retval = TCL_ERROR;
break;
}
/* Call the Style function if no error. */
if (retval == TCL_OK)
gdImageSetStyle(im, colors, ncolor);
out:
/* Free the colors. */
if (colors != NULL)
Tcl_Free((char *)colors);
return retval;
}
static int
tclGdSetCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, x, y;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &x) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &y) != TCL_OK)
return TCL_ERROR;
/* Call the Set function. */
gdImageSetPixel(im, x, y, color);
return TCL_OK;
}
static int
tclGdLineCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, x1, y1, x2, y2;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &x1) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &y1) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[6], &x2) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[7], &y2) != TCL_OK)
return TCL_ERROR;
/* Call the appropriate Line function. */
gdImageLine(im, x1, y1, x2, y2, color);
return TCL_OK;
}
static int
tclGdRectCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, x1, y1, x2, y2;
char *cmd;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &x1) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &y1) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[6], &x2) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[7], &y2) != TCL_OK)
return TCL_ERROR;
/* Call the appropriate rectangle function. */
cmd = Tcl_GetString(objv[1]);
if (cmd[0] == 'r')
gdImageRectangle(im, x1, y1, x2, y2, color);
else
gdImageFilledRectangle(im, x1, y1, x2, y2, color);
return TCL_OK;
}
static int
tclGdArcCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, cx, cy, width, height, start, end;
char *cmd;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &cx) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &cy) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[6], &width) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[7], &height) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[8], &start) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[9], &end) != TCL_OK)
return TCL_ERROR;
/* Call the appropriate arc function. */
cmd = Tcl_GetString(objv[1]);
if (cmd[0] == 'a')
gdImageArc(im, cx, cy, width, height, start, end, color);
else {
/* gdImageFilledArc(im, cx, cy, width, height, start, end, color); */
Tcl_SetResult(interp, "gdImageFilledArc not supported in gd1.2", TCL_STATIC);
return TCL_ERROR;
}
return TCL_OK;
}
static int
tclGdPolygonCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, npoints, i;
Tcl_Obj **pointObjv = (Tcl_Obj **)(&objv[4]);
gdPointPtr points = NULL;
int retval = TCL_OK;
char *cmd;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
/* Figure out how many points in the list and allocate memory. */
npoints = argc - 4;
/* If only one argument, treat it as a list. */
if (npoints == 1)
if (Tcl_ListObjGetElements(interp, objv[4],
&npoints, &pointObjv) != TCL_OK)
return TCL_ERROR;
/* Error check size of point list. */
if (npoints % 2 != 0)
{
Tcl_SetResult(interp, "Number of coordinates must be even", TCL_STATIC);
retval = TCL_ERROR;
goto out;
}
/* Divide by 2 to get number of points, and final error check. */
npoints /= 2;
if (npoints < 3)
{
Tcl_SetResult(interp, "Must specify at least 3 points.", TCL_STATIC);
retval = TCL_ERROR;
goto out;
}
points = (gdPointPtr)Tcl_Alloc(npoints * sizeof(gdPoint));
if (points == NULL)
{
Tcl_SetResult(interp, "Memory allocation failed", TCL_STATIC);
retval = TCL_ERROR;
goto out;
}
/* Get the point values. */
for (i = 0; i < npoints; i++)
if (Tcl_GetIntFromObj(interp, pointObjv[i*2], &points[i].x) != TCL_OK ||
Tcl_GetIntFromObj(interp, pointObjv[i*2+1], &points[i].y) != TCL_OK)
{
retval = TCL_ERROR;
break;
}
/* Call the appropriate polygon function. */
cmd = Tcl_GetString(objv[1]);
if (cmd[0] == 'p')
gdImagePolygon(im, points, npoints, color);
else
gdImageFilledPolygon(im, points, npoints, color);
out:
/* Free the points. */
if (points != NULL)
Tcl_Free((char *)points);
return TCL_OK;
}
static int
tclGdFillCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, x, y, border;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the color, x, y and possibly bordercolor values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &x) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &y) != TCL_OK)
return TCL_ERROR;
/* Call the appropriate fill function. */
if (argc - 2 == 5) {
if (Tcl_GetIntFromObj(interp, objv[6], &border) != TCL_OK)
return TCL_ERROR;
gdImageFillToBorder(im, x, y, border, color);
} else {
gdImageFill(im, x, y, color);
}
return TCL_OK;
}
static int
tclGdCopyCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr imdest, imsrc;
int destx, desty, srcx, srcy, destw, desth, srcw, srch;
/* Get the image pointer. */
imdest = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
imsrc = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[3]));
/* Get the x, y, etc. values. */
if (Tcl_GetIntFromObj(interp, objv[4], &destx) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[5], &desty) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[6], &srcx) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[7], &srcy) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[8], &destw) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[9], &desth) != TCL_OK)
return TCL_ERROR;
/* Call the appropriate copy function. */
if (argc - 2 == 10) {
if (Tcl_GetIntFromObj(interp, objv[10], &srcw) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[11], &srch) != TCL_OK)
return TCL_ERROR;
gdImageCopyResized(imdest, imsrc, destx, desty, srcx, srcy,
destw, desth, srcw, srch);
}
else
gdImageCopy(imdest, imsrc, destx, desty, srcx, srcy, destw, desth);
return TCL_OK;
}
static int
tclGdGetCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
int color, x, y;
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
/* Get the x, y values. */
if (Tcl_GetIntFromObj(interp, objv[3], &x) != TCL_OK)
return TCL_ERROR;
if (Tcl_GetIntFromObj(interp, objv[4], &y) != TCL_OK)
return TCL_ERROR;
/* Call the Get function. */
color = gdImageGetPixel(im, x, y);
Tcl_SetObjResult(interp, Tcl_NewIntObj(color));
return TCL_OK;
}
static int
tclGdSizeCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
char buf[30];
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
sprintf(buf, "%d %d", gdImageSX(im), gdImageSY(im));
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
}
static int
tclGdTextCmd(Tcl_Interp *interp, GdData *gdData,
int argc, Tcl_Obj *CONST objv[])
{
/* gd gdhandle color fontpathname size angle x y string */
gdImagePtr im;
int color, x, y;
double ptsize, angle;
char *error, buf[32], *font, *handle;
int i, brect[8], len;
unsigned char *str;
/* Get the image pointer. (an invalid or null arg[2] will result in string
size calculation but no rendering */
handle = Tcl_GetString(objv[2]);
if (! handle || *handle == '\0') {
im = (gdImagePtr)NULL;
}
else {
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl, handle);
}
/* Get the color, values. */
if (tclGd_GetColor(interp, objv[3], &color) != TCL_OK) {
return TCL_ERROR;
}
/* Get point size */
if (Tcl_GetDoubleFromObj(interp, objv[5], &ptsize) != TCL_OK) {
return TCL_ERROR;
}
/* Get rotation (radians) */
if (Tcl_GetDoubleFromObj(interp, objv[6], &angle) != TCL_OK) {
return TCL_ERROR;
}
/* get x, y position */
if (Tcl_GetIntFromObj(interp, objv[7], &x) != TCL_OK) {
return TCL_ERROR;
}
if (Tcl_GetIntFromObj(interp, objv[8], &y) != TCL_OK) {
return TCL_ERROR;
}
str = Tcl_GetStringFromObj(objv[9], &len);
font = Tcl_GetString(objv[4]);
error = gdImageStringTTF(im, brect, color, font, ptsize, angle, x, y, str);
if (error) {
Tcl_SetResult(interp, error, TCL_VOLATILE);
return TCL_ERROR;
}
for (i=0; i<8; i++) {
sprintf(buf, "%d", brect[i]);
Tcl_AppendElement(interp, buf);
}
return TCL_OK;
}
/*
* Initialize the package.
*/
#ifdef __WIN32__
EXPORT(int,Gdtclft_Init)(interp)
#else
int
Gdtclft_Init(Tcl_Interp *interp)
#endif
{
static GdData gdData;
#ifdef USE_TCL_STUBS
if (Tcl_InitStubs(interp, TCL_VERSION, 0) == NULL) {
return TCL_ERROR;
}
#else
if (Tcl_PkgRequired(interp, "Tcl", TCL_VERSION, 0) == NULL) {
return TCL_ERROR;
}
#endif
if (Tcl_PkgProvide(interp, "Gdtclft", VERSION) != TCL_OK) {
return TCL_ERROR;
}
GDHandleTable = gdData.handleTbl = gdHandleTblInit("gd", sizeof(gdImagePtr), 1);
if (gdData.handleTbl == NULL) {
Tcl_AppendResult(interp, "unable to create table for GD handles.", 0);
return TCL_ERROR;
}
Tcl_CreateObjCommand(interp, "gd", gdCmd, (ClientData)&gdData, (Tcl_CmdDeleteProc *)NULL);
return TCL_OK;
}
#ifdef __WIN32__
EXPORT(int,Gdtclft_SafeInit)(interp)
#else
int Gdtclft_SafeInit(Tcl_Interp *interp)
#endif
{
return Gdtclft_Init(interp);
}
#ifdef __WIN32__
/* Define DLL entry point, standard macro */
/*
*----------------------------------------------------------------------
*
* DllEntryPoint --
*
* This wrapper function is used by Windows to invoke the
* initialization code for the DLL. If we are compiling
* with Visual C++, this routine will be renamed to DllMain.
* routine.
*
* Results:
* Returns TRUE;
*
* Side effects:
* None.
*
*----------------------------------------------------------------------
*/
BOOL APIENTRY
DllEntryPoint(hInst, reason, reserved)
HINSTANCE hInst; /* Library instance handle. */
DWORD reason; /* Reason this function is being called. */
LPVOID reserved; /* Not used. */
{
return TRUE;
}
#endif
static int
BufferSinkFunc(void *context, const char *buffer, int len)
{
BuffSinkContext *p = context;
char *bufend;
if (p->buflen == 0)
{
p->buf = Tcl_Alloc(len + 1);
memcpy(p->buf, buffer, len);
p->buf[len] = '\0';
p->buflen = len;
} else {
p->buf = Tcl_Realloc(p->buf, len + p->buflen + 1);
bufend = p->buf + p->buflen;
memmove(bufend, buffer, len);
p->buf[len + p->buflen] = '\0';
p->buflen += len;
}
return len;
}
static int
tclGdWriteBufCmd(Tcl_Interp *interp, GdData *gdData, int argc, Tcl_Obj *CONST objv[])
{
gdImagePtr im;
Tcl_Obj *output;
char *cmd;
char *result = NULL;
BuffSinkContext bsc = {
NULL,
0
};
BuffSinkContext *res;
gdSink buffsink = {
BufferSinkFunc,
(void *)&bsc
};
cmd = Tcl_GetString(objv[1]);
/* Get the image pointer. */
im = *(gdImagePtr *)gdHandleXlate(interp, gdData->handleTbl,
Tcl_GetString(objv[2]));
gdImagePngToSink(im, &buffsink);
/* I'm not so hot with lots of pointer indirection, so this
makes things easier. - davidw*/
res = buffsink.context;
result = res->buf;
output = Tcl_NewByteArrayObj(result, res->buflen);
if (output == NULL)
return TCL_ERROR;
else
Tcl_IncrRefCount(output);
if (Tcl_ObjSetVar2(interp, objv[3], NULL, output, 0) == NULL)
return TCL_ERROR;
else
return TCL_OK;
}
|