Debian Codegraph @

pTk/mTk/generic/tkImgBmap.c

raw symbols SCIP
1 /*
2 * tkImgBmap.c --
3 *
4 * This procedure implements images of type "bitmap" for Tk.
5 *
6 * Copyright (c) 1994 The Regents of the University of California.
7 * Copyright (c) 1994-1997 Sun Microsystems, Inc.
8 * Copyright (c) 1999 by Scriptics Corporation.
9 *
10 * See the file "license.terms" for information on usage and redistribution
11 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
12 *
13 * RCS: @(#) $Id: tkImgBmap.c,v 1.15 2002/08/05 04:30:39 dgp Exp $
14 */
15 #define NEED_REAL_STDIO
16 #include "tkInt.h"
17 #include "tkPort.h"
18
19 /*
20 * The following data structure represents the master for a bitmap
21 * image:
22 */
23
24 typedef struct BitmapMaster {
25 Tk_ImageMaster tkMaster; /* Tk's token for image master. NULL means
26 * the image is being deleted. */
27 Tcl_Interp *interp; /* Interpreter for application that is
28 * using image. */
29 Tcl_Command imageCmd; /* Token for image command (used to delete
30 * it when the image goes away). NULL means
31 * the image command has already been
32 * deleted. */
33 int width, height; /* Dimensions of image. */
34 char *data; /* Data comprising bitmap (suitable for
35 * input to XCreateBitmapFromData). May
36 * be NULL if no data. Malloc'ed. */
37 char *maskData; /* Data for bitmap's mask (suitable for
38 * input to XCreateBitmapFromData).
39 * Malloc'ed. */
40 Tk_Uid fgUid; /* Value of -foreground option (malloc'ed). */
41 Tk_Uid bgUid; /* Value of -background option (malloc'ed). */
42 char *fileString; /* Value of -file option (malloc'ed). */
43 char *dataString; /* Value of -data option (malloc'ed). */
44 char *maskFileString; /* Value of -maskfile option (malloc'ed). */
45 char *maskDataString; /* Value of -maskdata option (malloc'ed). */
46 struct BitmapInstance *instancePtr;
47 /* First in list of all instances associated
48 * with this master. */
49 } BitmapMaster;
50
51 /*
52 * The following data structure represents all of the instances of an
53 * image that lie within a particular window:
54 */
55
56 typedef struct BitmapInstance {
57 int refCount; /* Number of instances that share this
58 * data structure. */
59 BitmapMaster *masterPtr; /* Pointer to master for image. */
60 Tk_Window tkwin; /* Window in which the instances will be
61 * displayed. */
62 XColor *fg; /* Foreground color for displaying image. */
63 XColor *bg; /* Background color for displaying image. */
64 Pixmap bitmap; /* The bitmap to display. */
65 Pixmap mask; /* Mask: only display bitmap pixels where
66 * there are 1's here. */
67 GC gc; /* Graphics context for displaying bitmap.
68 * None means there was an error while
69 * setting up the instance, so it cannot
70 * be displayed. */
71 struct BitmapInstance *nextPtr;
72 /* Next in list of all instance structures
73 * associated with masterPtr (NULL means
74 * end of list). */
75 } BitmapInstance;
76
77 /*
78 * The type record for bitmap images:
79 */
80
81 static int GetByte _ANSI_ARGS_((Tcl_Channel chan));
82 static int ImgBmapCreate _ANSI_ARGS_((Tcl_Interp *interp,
83 char *name, int argc, Tcl_Obj *CONST objv[],
84 Tk_ImageType *typePtr, Tk_ImageMaster master,
85 ClientData *clientDataPtr));
86 static ClientData ImgBmapGet _ANSI_ARGS_((Tk_Window tkwin,
87 ClientData clientData));
88 static void ImgBmapDisplay _ANSI_ARGS_((ClientData clientData,
89 Display *display, Drawable drawable,
90 int imageX, int imageY, int width, int height,
91 int drawableX, int drawableY));
92 static void ImgBmapFree _ANSI_ARGS_((ClientData clientData,
93 Display *display));
94 static void ImgBmapDelete _ANSI_ARGS_((ClientData clientData));
95 static int ImgBmapPostscript _ANSI_ARGS_((ClientData clientData,
96 Tcl_Interp *interp, Tk_Window tkwin,
97 Tk_PostscriptInfo psinfo, int x, int y,
98 int width, int height, int prepass));
99
100 Tk_ImageType tkBitmapImageType = {
101 "bitmap", /* name */
102 ImgBmapCreate, /* createProc */
103 ImgBmapGet, /* getProc */
104 ImgBmapDisplay, /* displayProc */
105 ImgBmapFree, /* freeProc */
106 ImgBmapDelete, /* deleteProc */
107 ImgBmapPostscript, /* postscriptProc */
108 (Tk_ImageType *) NULL /* nextPtr */
109 };
110
111 /*
112 * Information used for parsing configuration specs:
113 */
114
115 static Tk_ConfigSpec configSpecs[] = {
116 {TK_CONFIG_UID, "-background", (char *) NULL, (char *) NULL,
117 "", Tk_Offset(BitmapMaster, bgUid), TK_CONFIG_NULL_OK},
118 {TK_CONFIG_STRING, "-data", (char *) NULL, (char *) NULL,
119 (char *) NULL, Tk_Offset(BitmapMaster, dataString), TK_CONFIG_NULL_OK},
120 {TK_CONFIG_STRING, "-file", (char *) NULL, (char *) NULL,
121 (char *) NULL, Tk_Offset(BitmapMaster, fileString), TK_CONFIG_NULL_OK},
122 {TK_CONFIG_UID, "-foreground", (char *) NULL, (char *) NULL,
123 "#000000", Tk_Offset(BitmapMaster, fgUid), 0},
124 {TK_CONFIG_STRING, "-maskdata", (char *) NULL, (char *) NULL,
125 (char *) NULL, Tk_Offset(BitmapMaster, maskDataString),
126 TK_CONFIG_NULL_OK},
127 {TK_CONFIG_STRING, "-maskfile", (char *) NULL, (char *) NULL,
128 (char *) NULL, Tk_Offset(BitmapMaster, maskFileString),
129 TK_CONFIG_NULL_OK},
130 {TK_CONFIG_END, (char *) NULL, (char *) NULL, (char *) NULL,
131 (char *) NULL, 0, 0}
132 };
133
134 /*
135 * The following data structure is used to describe the state of
136 * parsing a bitmap file or string. It is used for communication
137 * between TkGetBitmapData and NextBitmapWord.
138 */
139
140 #define MAX_WORD_LENGTH 100
141 typedef struct ParseInfo {
142 char *string; /* Next character of string data for bitmap,
143 * or NULL if bitmap is being read from
144 * file. */
145 Tcl_Channel chan; /* File containing bitmap data, or NULL
146 * if no file. */
147 char word[MAX_WORD_LENGTH+1];
148 /* Current word of bitmap data, NULL
149 * terminated. */
150 int wordLength; /* Number of non-NULL bytes in word. */
151 } ParseInfo;
152
153 /*
154 * Prototypes for procedures used only locally in this file:
155 */
156
157 static int ImgBmapCmd _ANSI_ARGS_((ClientData clientData,
158 Tcl_Interp *interp, int argc, Tcl_Obj *CONST objv[]));
159 static void ImgBmapCmdDeletedProc _ANSI_ARGS_((
160 ClientData clientData));
161 static void ImgBmapConfigureInstance _ANSI_ARGS_((
162 BitmapInstance *instancePtr));
163 static int ImgBmapConfigureMaster _ANSI_ARGS_((
164 BitmapMaster *masterPtr, int argc, Tcl_Obj *CONST objv[],
165 int flags));
166 static int NextBitmapWord _ANSI_ARGS_((ParseInfo *parseInfoPtr));
167
168 /*
169 *----------------------------------------------------------------------
170 *
171 * ImgBmapCreate --
172 *
173 * This procedure is called by the Tk image code to create "test"
174 * images.
175 *
176 * Results:
177 * A standard Tcl result.
178 *
179 * Side effects:
180 * The data structure for a new image is allocated.
181 *
182 *----------------------------------------------------------------------
183 */
184
185 /* ARGSUSED */
186 static int
187 ImgBmapCreate(interp, name, argc, objv, typePtr, master, clientDataPtr)
188 Tcl_Interp *interp; /* Interpreter for application containing
189 * image. */
190 char *name; /* Name to use for image. */
191 int argc; /* Number of arguments. */
192 Tcl_Obj *CONST objv[]; /* Argument objects for options (doesn't
193 * include image name or type). */
194 Tk_ImageType *typePtr; /* Pointer to our type record (not used). */
195 Tk_ImageMaster master; /* Token for image, to be used by us in
196 * later callbacks. */
197 ClientData *clientDataPtr; /* Store manager's token for image here;
198 * it will be returned in later callbacks. */
199 {
200 BitmapMaster *masterPtr;
201
202 masterPtr = (BitmapMaster *) ckalloc(sizeof(BitmapMaster));
203 masterPtr->tkMaster = master;
204 masterPtr->interp = interp;
205 masterPtr->imageCmd = Lang_CreateImage(interp, name, ImgBmapCmd,
206 (ClientData) masterPtr, ImgBmapCmdDeletedProc, typePtr);
207 masterPtr->width = masterPtr->height = 0;
208 masterPtr->data = NULL;
209 masterPtr->maskData = NULL;
210 masterPtr->fgUid = NULL;
211 masterPtr->bgUid = NULL;
212 masterPtr->fileString = NULL;
213 masterPtr->dataString = NULL;
214 masterPtr->maskFileString = NULL;
215 masterPtr->maskDataString = NULL;
216 masterPtr->instancePtr = NULL;
217 if (ImgBmapConfigureMaster(masterPtr, argc, objv, 0) != TCL_OK) {
218 ImgBmapDelete((ClientData) masterPtr);
219 return TCL_ERROR;
220 }
221 *clientDataPtr = (ClientData) masterPtr;
222 return TCL_OK;
223 }
224
225 /*
226 *----------------------------------------------------------------------
227 *
228 * ImgBmapConfigureMaster --
229 *
230 * This procedure is called when a bitmap image is created or
231 * reconfigured. It process configuration options and resets
232 * any instances of the image.
233 *
234 * Results:
235 * A standard Tcl return value. If TCL_ERROR is returned then
236 * an error message is left in the masterPtr->interp's result.
237 *
238 * Side effects:
239 * Existing instances of the image will be redisplayed to match
240 * the new configuration options.
241 *
242 *----------------------------------------------------------------------
243 */
244
245 static int
246 ImgBmapConfigureMaster(masterPtr, objc, objv, flags)
247 BitmapMaster *masterPtr; /* Pointer to data structure describing
248 * overall bitmap image to (reconfigure). */
249 int objc; /* Number of entries in objv. */
250 Tcl_Obj *CONST objv[]; /* Pairs of configuration options for image. */
251 int flags; /* Flags to pass to Tk_ConfigureWidget,
252 * such as TK_CONFIG_ARGV_ONLY. */
253 {
254 BitmapInstance *instancePtr;
255 int maskWidth, maskHeight, dummy1, dummy2;
256
257 if (Tk_ConfigureWidget(masterPtr->interp, Tk_MainWindow(masterPtr->interp),
258 configSpecs, objc, objv, (char *) masterPtr, flags)
259 != TCL_OK) {
260 return TCL_ERROR;
261 }
262
263 /*
264 * Parse the bitmap and/or mask to create binary data. Make sure that
265 * the bitmap and mask have the same dimensions.
266 */
267
268 if (masterPtr->data != NULL) {
269 ckfree(masterPtr->data);
270 masterPtr->data = NULL;
271 }
272 if ((masterPtr->fileString != NULL) || (masterPtr->dataString != NULL)) {
273 masterPtr->data = TkGetBitmapData(masterPtr->interp,
274 masterPtr->dataString, masterPtr->fileString,
275 &masterPtr->width, &masterPtr->height, &dummy1, &dummy2);
276 if (masterPtr->data == NULL) {
277 return TCL_ERROR;
278 }
279 }
280 if (masterPtr->maskData != NULL) {
281 ckfree(masterPtr->maskData);
282 masterPtr->maskData = NULL;
283 }
284 if ((masterPtr->maskFileString != NULL)
285 || (masterPtr->maskDataString != NULL)) {
286 if (masterPtr->data == NULL) {
287 Tcl_SetResult(masterPtr->interp, "can't have mask without bitmap",
288 TCL_STATIC);
289 return TCL_ERROR;
290 }
291 masterPtr->maskData = TkGetBitmapData(masterPtr->interp,
292 masterPtr->maskDataString, masterPtr->maskFileString,
293 &maskWidth, &maskHeight, &dummy1, &dummy2);
294 if (masterPtr->maskData == NULL) {
295 return TCL_ERROR;
296 }
297 if ((maskWidth != masterPtr->width)
298 || (maskHeight != masterPtr->height)) {
299 ckfree(masterPtr->maskData);
300 masterPtr->maskData = NULL;
301 Tcl_SetResult(masterPtr->interp,
302 "bitmap and mask have different sizes", TCL_STATIC);
303 return TCL_ERROR;
304 }
305 }
306
307 /*
308 * Cycle through all of the instances of this image, regenerating
309 * the information for each instance. Then force the image to be
310 * redisplayed everywhere that it is used.
311 */
312
313 for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
314 instancePtr = instancePtr->nextPtr) {
315 ImgBmapConfigureInstance(instancePtr);
316 }
317 Tk_ImageChanged(masterPtr->tkMaster, 0, 0, masterPtr->width,
318 masterPtr->height, masterPtr->width, masterPtr->height);
319 return TCL_OK;
320 }
321
322 /*
323 *----------------------------------------------------------------------
324 *
325 * ImgBmapConfigureInstance --
326 *
327 * This procedure is called to create displaying information for
328 * a bitmap image instance based on the configuration information
329 * in the master. It is invoked both when new instances are
330 * created and when the master is reconfigured.
331 *
332 * Results:
333 * None.
334 *
335 * Side effects:
336 * Generates errors via Tcl_BackgroundError if there are problems
337 * in setting up the instance.
338 *
339 *----------------------------------------------------------------------
340 */
341
342 static void
343 ImgBmapConfigureInstance(instancePtr)
344 BitmapInstance *instancePtr; /* Instance to reconfigure. */
345 {
346 BitmapMaster *masterPtr = instancePtr->masterPtr;
347 XColor *colorPtr;
348 XGCValues gcValues;
349 GC gc;
350 unsigned int mask;
351 Pixmap oldMask;
352
353 /*
354 * For each of the options in masterPtr, translate the string
355 * form into an internal form appropriate for instancePtr.
356 */
357
358 if ((masterPtr->bgUid != NULL) && (masterPtr->bgUid[0] != '\0')) {
359 colorPtr = Tk_GetColor(masterPtr->interp, instancePtr->tkwin,
360 masterPtr->bgUid);
361 if (colorPtr == NULL) {
362 goto error;
363 }
364 } else {
365 colorPtr = NULL;
366 }
367 if (instancePtr->bg != NULL) {
368 Tk_FreeColor(instancePtr->bg);
369 }
370 instancePtr->bg = colorPtr;
371
372 colorPtr = Tk_GetColor(masterPtr->interp, instancePtr->tkwin,
373 masterPtr->fgUid);
374 if (colorPtr == NULL) {
375 goto error;
376 }
377 if (instancePtr->fg != NULL) {
378 Tk_FreeColor(instancePtr->fg);
379 }
380 instancePtr->fg = colorPtr;
381
382 if (instancePtr->bitmap != None) {
383 Tk_FreePixmap(Tk_Display(instancePtr->tkwin), instancePtr->bitmap);
384 instancePtr->bitmap = None;
385 }
386 if (masterPtr->data != NULL) {
387 instancePtr->bitmap = XCreateBitmapFromData(
388 Tk_Display(instancePtr->tkwin),
389 RootWindowOfScreen(Tk_Screen(instancePtr->tkwin)),
390 masterPtr->data, (unsigned) masterPtr->width,
391 (unsigned) masterPtr->height);
392 }
393
394 /*
395 * Careful: We have to allocate a new mask Pixmap before deleting
396 * the old one. Otherwise, The XID allocator will always return
397 * the same XID for the new Pixmap as was used for the old Pixmap.
398 * And that will prevent the mask from changing in the GC below.
399 */
400 oldMask = instancePtr->mask;
401 instancePtr->mask = None;
402 if (masterPtr->maskData != NULL) {
403 instancePtr->mask = XCreateBitmapFromData(
404 Tk_Display(instancePtr->tkwin),
405 RootWindowOfScreen(Tk_Screen(instancePtr->tkwin)),
406 masterPtr->maskData, (unsigned) masterPtr->width,
407 (unsigned) masterPtr->height);
408 }
409 if (oldMask != None) {
410 Tk_FreePixmap(Tk_Display(instancePtr->tkwin), oldMask);
411 }
412
413 if (masterPtr->data != NULL) {
414 gcValues.foreground = instancePtr->fg->pixel;
415 gcValues.graphics_exposures = False;
416 mask = GCForeground|GCGraphicsExposures;
417 if (instancePtr->bg != NULL) {
418 gcValues.background = instancePtr->bg->pixel;
419 mask |= GCBackground;
420 if (instancePtr->mask != None) {
421 gcValues.clip_mask = instancePtr->mask;
422 mask |= GCClipMask;
423 }
424 } else {
425 gcValues.clip_mask = instancePtr->bitmap;
426 mask |= GCClipMask;
427 }
428 gc = Tk_GetGC(instancePtr->tkwin, mask, &gcValues);
429 } else {
430 gc = None;
431 }
432 if (instancePtr->gc != None) {
433 Tk_FreeGC(Tk_Display(instancePtr->tkwin), instancePtr->gc);
434 }
435 instancePtr->gc = gc;
436 return;
437
438 error:
439 /*
440 * An error occurred: clear the graphics context in the instance to
441 * make it clear that this instance cannot be displayed. Then report
442 * the error.
443 */
444
445 if (instancePtr->gc != None) {
446 Tk_FreeGC(Tk_Display(instancePtr->tkwin), instancePtr->gc);
447 }
448 instancePtr->gc = None;
449 Tcl_AddErrorInfo(masterPtr->interp, "\n (while configuring image \"");
450 Tcl_AddErrorInfo(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
451 Tcl_AddErrorInfo(masterPtr->interp, "\")");
452 Tcl_BackgroundError(masterPtr->interp);
453 }
454
455 /*
456 *----------------------------------------------------------------------
457 *
458 * TkGetBitmapData --
459 *
460 * Given a file name or ASCII string, this procedure parses the
461 * file or string contents to produce binary data for a bitmap.
462 *
463 * Results:
464 * If the bitmap description was parsed successfully then the
465 * return value is a malloc-ed array containing the bitmap data.
466 * The dimensions of the data are stored in *widthPtr and
467 * *heightPtr. *hotXPtr and *hotYPtr are set to the bitmap
468 * hotspot if one is defined, otherwise they are set to -1, -1.
469 * If an error occurred, NULL is returned and an error message is
470 * left in the interp's result.
471 *
472 * Side effects:
473 * A bitmap is created.
474 *
475 *----------------------------------------------------------------------
476 */
477
478 char *
479 TkGetBitmapData(interp, string, fileName, widthPtr, heightPtr,
480 hotXPtr, hotYPtr)
481 Tcl_Interp *interp; /* For reporting errors, or NULL. */
482 char *string; /* String describing bitmap. May
483 * be NULL. */
484 char *fileName; /* Name of file containing bitmap
485 * description. Used only if string
486 * is NULL. Must not be NULL if
487 * string is NULL. */
488 int *widthPtr, *heightPtr; /* Dimensions of bitmap get returned
489 * here. */
490 int *hotXPtr, *hotYPtr; /* Position of hot spot or -1,-1. */
491 {
492 int width, height, numBytes, hotX, hotY;
493 CONST char *expandedFileName;
494 char *p, *end;
495 ParseInfo pi;
496 char *data = NULL;
497 Tcl_DString buffer;
498
499 pi.string = string;
500 if (string == NULL) {
501 if ((interp != NULL) && Tcl_IsSafe(interp)) {
502 Tcl_AppendResult(interp, "can't get bitmap data from a file in a",
503 " safe interpreter", (char *) NULL);
504 return NULL;
505 }
506 expandedFileName = Tcl_TranslateFileName(interp, fileName, &buffer);
507 if (expandedFileName == NULL) {
508 return NULL;
509 }
510 pi.chan = Tcl_OpenFileChannel(interp, expandedFileName, "r", 0);
511 Tcl_DStringFree(&buffer);
512 if (pi.chan == NULL) {
513 if (interp != NULL) {
514 Tcl_ResetResult(interp);
515 Tcl_AppendResult(interp, "couldn't read bitmap file \"",
516 fileName, "\": ", Tcl_PosixError(interp),
517 (char *) NULL);
518 }
519 return NULL;
520 }
521
522 if (Tcl_SetChannelOption(interp, pi.chan, "-translation", "binary")
523 != TCL_OK) {
524 return NULL;
525 }
526 if (Tcl_SetChannelOption(interp, pi.chan, "-encoding", "binary")
527 != TCL_OK) {
528 return NULL;
529 }
530 } else {
531 pi.chan = NULL;
532 }
533
534 /*
535 * Parse the lines that define the dimensions of the bitmap,
536 * plus the first line that defines the bitmap data (it declares
537 * the name of a data variable but doesn't include any actual
538 * data). These lines look something like the following:
539 *
540 * #define foo_width 16
541 * #define foo_height 16
542 * #define foo_x_hot 3
543 * #define foo_y_hot 3
544 * static char foo_bits[] = {
545 *
546 * The x_hot and y_hot lines may or may not be present. It's
547 * important to check for "char" in the last line, in order to
548 * reject old X10-style bitmaps that used shorts.
549 */
550
551 width = 0;
552 height = 0;
553 hotX = -1;
554 hotY = -1;
555 while (1) {
556 if (NextBitmapWord(&pi) != TCL_OK) {
557 goto error;
558 }
559 if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
560 && (strcmp(pi.word+pi.wordLength-6, "_width") == 0)) {
561 if (NextBitmapWord(&pi) != TCL_OK) {
562 goto error;
563 }
564 width = strtol(pi.word, &end, 0);
565 if ((end == pi.word) || (*end != 0)) {
566 goto error;
567 }
568 } else if ((pi.wordLength >= 7) && (pi.word[pi.wordLength-7] == '_')
569 && (strcmp(pi.word+pi.wordLength-7, "_height") == 0)) {
570 if (NextBitmapWord(&pi) != TCL_OK) {
571 goto error;
572 }
573 height = strtol(pi.word, &end, 0);
574 if ((end == pi.word) || (*end != 0)) {
575 goto error;
576 }
577 } else if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
578 && (strcmp(pi.word+pi.wordLength-6, "_x_hot") == 0)) {
579 if (NextBitmapWord(&pi) != TCL_OK) {
580 goto error;
581 }
582 hotX = strtol(pi.word, &end, 0);
583 if ((end == pi.word) || (*end != 0)) {
584 goto error;
585 }
586 } else if ((pi.wordLength >= 6) && (pi.word[pi.wordLength-6] == '_')
587 && (strcmp(pi.word+pi.wordLength-6, "_y_hot") == 0)) {
588 if (NextBitmapWord(&pi) != TCL_OK) {
589 goto error;
590 }
591 hotY = strtol(pi.word, &end, 0);
592 if ((end == pi.word) || (*end != 0)) {
593 goto error;
594 }
595 } else if ((pi.word[0] == 'c') && (strcmp(pi.word, "char") == 0)) {
596 while (1) {
597 if (NextBitmapWord(&pi) != TCL_OK) {
598 goto error;
599 }
600 if ((pi.word[0] == '{') && (pi.word[1] == 0)) {
601 goto getData;
602 }
603 }
604 } else if ((pi.word[0] == '{') && (pi.word[1] == 0)) {
605 if (interp != NULL) {
606 Tcl_AppendResult(interp, "format error in bitmap data; ",
607 "looks like it's an obsolete X10 bitmap file",
608 (char *) NULL);
609 }
610 goto errorCleanup;
611 }
612 }
613
614 /*
615 * Now we've read everything but the data. Allocate an array
616 * and read in the data.
617 */
618
619 getData:
620 if ((width <= 0) || (height <= 0)) {
621 goto error;
622 }
623 numBytes = ((width+7)/8) * height;
624 data = (char *) ckalloc((unsigned) numBytes);
625 for (p = data; numBytes > 0; p++, numBytes--) {
626 if (NextBitmapWord(&pi) != TCL_OK) {
627 goto error;
628 }
629 *p = (char) strtol(pi.word, &end, 0);
630 if (end == pi.word) {
631 goto error;
632 }
633 }
634
635 /*
636 * All done. Clean up and return.
637 */
638
639 if (pi.chan != NULL) {
640 Tcl_Close(NULL, pi.chan);
641 }
642 *widthPtr = width;
643 *heightPtr = height;
644 *hotXPtr = hotX;
645 *hotYPtr = hotY;
646 return data;
647
648 error:
649 if (interp != NULL) {
650 Tcl_SetResult(interp, "format error in bitmap data", TCL_STATIC);
651 }
652
653 errorCleanup:
654 if (data != NULL) {
655 ckfree(data);
656 }
657 if (pi.chan != NULL) {
658 Tcl_Close(NULL, pi.chan);
659 }
660 return NULL;
661 }
662
663 /*
664 *----------------------------------------------------------------------
665 *
666 * NextBitmapWord --
667 *
668 * This procedure retrieves the next word of information (stuff
669 * between commas or white space) from a bitmap description.
670 *
671 * Results:
672 * Returns TCL_OK if all went well. In this case the next word,
673 * and its length, will be availble in *parseInfoPtr. If the end
674 * of the bitmap description was reached then TCL_ERROR is returned.
675 *
676 * Side effects:
677 * None.
678 *
679 *----------------------------------------------------------------------
680 */
681
682 static int
683 NextBitmapWord(parseInfoPtr)
684 ParseInfo *parseInfoPtr; /* Describes what we're reading
685 * and where we are in it. */
686 {
687 char *src, *dst;
688 int c;
689
690 parseInfoPtr->wordLength = 0;
691 dst = parseInfoPtr->word;
692 if (parseInfoPtr->string != NULL) {
693 for (src = parseInfoPtr->string; isspace(UCHAR(*src)) || (*src == ',');
694 src++) {
695 if (*src == 0) {
696 return TCL_ERROR;
697 }
698 }
699 for ( ; !isspace(UCHAR(*src)) && (*src != ',') && (*src != 0); src++) {
700 *dst = *src;
701 dst++;
702 parseInfoPtr->wordLength++;
703 if (parseInfoPtr->wordLength > MAX_WORD_LENGTH) {
704 return TCL_ERROR;
705 }
706 }
707 parseInfoPtr->string = src;
708 } else {
709 for (c = GetByte(parseInfoPtr->chan); isspace(UCHAR(c)) || (c == ',');
710 c = GetByte(parseInfoPtr->chan)) {
711 if (c == EOF) {
712 return TCL_ERROR;
713 }
714 }
715 for ( ; !isspace(UCHAR(c)) && (c != ',') && (c != EOF);
716 c = GetByte(parseInfoPtr->chan)) {
717 *dst = c;
718 dst++;
719 parseInfoPtr->wordLength++;
720 if (parseInfoPtr->wordLength > MAX_WORD_LENGTH) {
721 return TCL_ERROR;
722 }
723 }
724 }
725 if (parseInfoPtr->wordLength == 0) {
726 return TCL_ERROR;
727 }
728 parseInfoPtr->word[parseInfoPtr->wordLength] = 0;
729 return TCL_OK;
730 }
731
732 /*
733 *--------------------------------------------------------------
734 *
735 * ImgBmapCmd --
736 *
737 * This procedure is invoked to process the Tcl command
738 * that corresponds to an image managed by this module.
739 * See the user documentation for details on what it does.
740 *
741 * Results:
742 * A standard Tcl result.
743 *
744 * Side effects:
745 * See the user documentation.
746 *
747 *--------------------------------------------------------------
748 */
749
750 static int
751 ImgBmapCmd(clientData, interp, objc, objv)
752 ClientData clientData; /* Information about the image master. */
753 Tcl_Interp *interp; /* Current interpreter. */
754 int objc; /* Number of arguments. */
755 Tcl_Obj *CONST objv[]; /* Argument objects. */
756 {
757 static CONST char *bmapOptions[] = {"cget", "configure", (char *) NULL};
758 BitmapMaster *masterPtr = (BitmapMaster *) clientData;
759 int code, index;
760
761 if (objc < 2) {
762 Tcl_WrongNumArgs(interp, 1, objv, "option ?arg arg ...?");
763 return TCL_ERROR;
764 }
765 if (Tcl_GetIndexFromObj(interp, objv[1], bmapOptions, "option", 0,
766 &index) != TCL_OK) {
767 return TCL_ERROR;
768 }
769 switch (index) {
770 case 0: {
771 if (objc != 3) {
772 Tcl_WrongNumArgs(interp, 2, objv, "option");
773 return TCL_ERROR;
774 }
775 return Tk_ConfigureValue(interp, Tk_MainWindow(interp), configSpecs,
776 (char *) masterPtr, Tcl_GetString(objv[2]), 0);
777 }
778 case 1: {
779 if (objc == 2) {
780 code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
781 configSpecs, (char *) masterPtr, (char *) NULL, 0);
782 } else if (objc == 3) {
783 code = Tk_ConfigureInfo(interp, Tk_MainWindow(interp),
784 configSpecs, (char *) masterPtr,
785 Tcl_GetString(objv[2]), 0);
786 } else {
787 code = ImgBmapConfigureMaster(masterPtr, objc-2, objv+2,
788 TK_CONFIG_ARGV_ONLY);
789 }
790 return code;
791 }
792 default: {
793 panic("bad const entries to bmapOptions in ImgBmapCmd");
794 }
795 }
796 return TCL_OK;
797 }
798
799 /*
800 *----------------------------------------------------------------------
801 *
802 * ImgBmapGet --
803 *
804 * This procedure is called for each use of a bitmap image in a
805 * widget.
806 *
807 * Results:
808 * The return value is a token for the instance, which is passed
809 * back to us in calls to ImgBmapDisplay and ImgBmapFree.
810 *
811 * Side effects:
812 * A data structure is set up for the instance (or, an existing
813 * instance is re-used for the new one).
814 *
815 *----------------------------------------------------------------------
816 */
817
818 static ClientData
819 ImgBmapGet(tkwin, masterData)
820 Tk_Window tkwin; /* Window in which the instance will be
821 * used. */
822 ClientData masterData; /* Pointer to our master structure for the
823 * image. */
824 {
825 BitmapMaster *masterPtr = (BitmapMaster *) masterData;
826 BitmapInstance *instancePtr;
827
828 /*
829 * See if there is already an instance for this window. If so
830 * then just re-use it.
831 */
832
833 for (instancePtr = masterPtr->instancePtr; instancePtr != NULL;
834 instancePtr = instancePtr->nextPtr) {
835 if (instancePtr->tkwin == tkwin) {
836 instancePtr->refCount++;
837 return (ClientData) instancePtr;
838 }
839 }
840
841 /*
842 * The image isn't already in use in this window. Make a new
843 * instance of the image.
844 */
845
846 instancePtr = (BitmapInstance *) ckalloc(sizeof(BitmapInstance));
847 instancePtr->refCount = 1;
848 instancePtr->masterPtr = masterPtr;
849 instancePtr->tkwin = tkwin;
850 instancePtr->fg = NULL;
851 instancePtr->bg = NULL;
852 instancePtr->bitmap = None;
853 instancePtr->mask = None;
854 instancePtr->gc = None;
855 instancePtr->nextPtr = masterPtr->instancePtr;
856 masterPtr->instancePtr = instancePtr;
857 ImgBmapConfigureInstance(instancePtr);
858
859 /*
860 * If this is the first instance, must set the size of the image.
861 */
862
863 if (instancePtr->nextPtr == NULL) {
864 Tk_ImageChanged(masterPtr->tkMaster, 0, 0, 0, 0, masterPtr->width,
865 masterPtr->height);
866 }
867
868 return (ClientData) instancePtr;
869 }
870
871 /*
872 *----------------------------------------------------------------------
873 *
874 * ImgBmapDisplay --
875 *
876 * This procedure is invoked to draw a bitmap image.
877 *
878 * Results:
879 * None.
880 *
881 * Side effects:
882 * A portion of the image gets rendered in a pixmap or window.
883 *
884 *----------------------------------------------------------------------
885 */
886
887 static void
888 ImgBmapDisplay(clientData, display, drawable, imageX, imageY, width,
889 height, drawableX, drawableY)
890 ClientData clientData; /* Pointer to BitmapInstance structure for
891 * for instance to be displayed. */
892 Display *display; /* Display on which to draw image. */
893 Drawable drawable; /* Pixmap or window in which to draw image. */
894 int imageX, imageY; /* Upper-left corner of region within image
895 * to draw. */
896 int width, height; /* Dimensions of region within image to draw. */
897 int drawableX, drawableY; /* Coordinates within drawable that
898 * correspond to imageX and imageY. */
899 {
900 BitmapInstance *instancePtr = (BitmapInstance *) clientData;
901 int masking;
902
903 /*
904 * If there's no graphics context, it means that an error occurred
905 * while creating the image instance so it can't be displayed.
906 */
907
908 if (instancePtr->gc == None) {
909 return;
910 }
911
912 /*
913 * If masking is in effect, must modify the mask origin within
914 * the graphics context to line up with the image's origin.
915 * Then draw the image and reset the clip origin, if there's
916 * a mask.
917 */
918
919 masking = (instancePtr->mask != None) || (instancePtr->bg == NULL);
920 if (masking) {
921 XSetClipOrigin(display, instancePtr->gc, drawableX - imageX,
922 drawableY - imageY);
923 }
924 XCopyPlane(display, instancePtr->bitmap, drawable, instancePtr->gc,
925 imageX, imageY, (unsigned) width, (unsigned) height,
926 drawableX, drawableY, 1);
927 if (masking) {
928 XSetClipOrigin(display, instancePtr->gc, 0, 0);
929 }
930 }
931
932 /*
933 *----------------------------------------------------------------------
934 *
935 * ImgBmapFree --
936 *
937 * This procedure is called when a widget ceases to use a
938 * particular instance of an image.
939 *
940 * Results:
941 * None.
942 *
943 * Side effects:
944 * Internal data structures get cleaned up.
945 *
946 *----------------------------------------------------------------------
947 */
948
949 static void
950 ImgBmapFree(clientData, display)
951 ClientData clientData; /* Pointer to BitmapInstance structure for
952 * for instance to be displayed. */
953 Display *display; /* Display containing window that used image. */
954 {
955 BitmapInstance *instancePtr = (BitmapInstance *) clientData;
956 BitmapInstance *prevPtr;
957
958 instancePtr->refCount--;
959 if (instancePtr->refCount > 0) {
960 return;
961 }
962
963 /*
964 * There are no more uses of the image within this widget. Free
965 * the instance structure.
966 */
967
968 if (instancePtr->fg != NULL) {
969 Tk_FreeColor(instancePtr->fg);
970 }
971 if (instancePtr->bg != NULL) {
972 Tk_FreeColor(instancePtr->bg);
973 }
974 if (instancePtr->bitmap != None) {
975 Tk_FreePixmap(display, instancePtr->bitmap);
976 }
977 if (instancePtr->mask != None) {
978 Tk_FreePixmap(display, instancePtr->mask);
979 }
980 if (instancePtr->gc != None) {
981 Tk_FreeGC(display, instancePtr->gc);
982 }
983 if (instancePtr->masterPtr->instancePtr == instancePtr) {
984 instancePtr->masterPtr->instancePtr = instancePtr->nextPtr;
985 } else {
986 for (prevPtr = instancePtr->masterPtr->instancePtr;
987 prevPtr->nextPtr != instancePtr; prevPtr = prevPtr->nextPtr) {
988 /* Empty loop body */
989 }
990 prevPtr->nextPtr = instancePtr->nextPtr;
991 }
992 ckfree((char *) instancePtr);
993 }
994
995 /*
996 *----------------------------------------------------------------------
997 *
998 * ImgBmapDelete --
999 *
1000 * This procedure is called by the image code to delete the
1001 * master structure for an image.
1002 *
1003 * Results:
1004 * None.
1005 *
1006 * Side effects:
1007 * Resources associated with the image get freed.
1008 *
1009 *----------------------------------------------------------------------
1010 */
1011
1012 static void
1013 ImgBmapDelete(masterData)
1014 ClientData masterData; /* Pointer to BitmapMaster structure for
1015 * image. Must not have any more instances. */
1016 {
1017 BitmapMaster *masterPtr = (BitmapMaster *) masterData;
1018
1019 if (masterPtr->instancePtr != NULL) {
1020 panic("tried to delete bitmap image when instances still exist");
1021 }
1022 masterPtr->tkMaster = NULL;
1023 if (masterPtr->imageCmd != NULL) {
1024 Tcl_DeleteCommandFromToken(masterPtr->interp, masterPtr->imageCmd);
1025 }
1026 if (masterPtr->data != NULL) {
1027 ckfree(masterPtr->data);
1028 }
1029 if (masterPtr->maskData != NULL) {
1030 ckfree(masterPtr->maskData);
1031 }
1032 Tk_FreeOptions(configSpecs, (char *) masterPtr, (Display *) NULL, 0);
1033 ckfree((char *) masterPtr);
1034 }
1035
1036 /*
1037 *----------------------------------------------------------------------
1038 *
1039 * ImgBmapCmdDeletedProc --
1040 *
1041 * This procedure is invoked when the image command for an image
1042 * is deleted. It deletes the image.
1043 *
1044 * Results:
1045 * None.
1046 *
1047 * Side effects:
1048 * The image is deleted.
1049 *
1050 *----------------------------------------------------------------------
1051 */
1052
1053 static void
1054 ImgBmapCmdDeletedProc(clientData)
1055 ClientData clientData; /* Pointer to BitmapMaster structure for
1056 * image. */
1057 {
1058 BitmapMaster *masterPtr = (BitmapMaster *) clientData;
1059
1060 masterPtr->imageCmd = NULL;
1061 if (masterPtr->tkMaster != NULL) {
1062 Tk_DeleteImage(masterPtr->interp, Tk_NameOfImage(masterPtr->tkMaster));
1063 }
1064 }
1065
1066 /*
1067 *----------------------------------------------------------------------
1068 *
1069 * GetByte --
1070 *
1071 * Get the next byte from the open channel.
1072 *
1073 * Results:
1074 * The next byte or EOF.
1075 *
1076 * Side effects:
1077 * We read from the channel.
1078 *
1079 *----------------------------------------------------------------------
1080 */
1081
1082 static int
1083 GetByte(chan)
1084 Tcl_Channel chan; /* The channel we read from. */
1085 {
1086 char buffer;
1087 int size;
1088
1089 size = Tcl_Read(chan, &buffer, 1);
1090 if (size <= 0) {
1091 return EOF;
1092 } else {
1093 return buffer;
1094 }
1095 }
1096
1097
1098 /*
1099 *----------------------------------------------------------------------
1100 *
1101 * ImgBmapPsImagemask --
1102 *
1103 * This procedure generates postscript suitable for rendering a
1104 * single bitmap of an image. A single bitmap image might contain both
1105 * a foreground and a background bitmap. This routine is called once
1106 * for each such bitmap in a bitmap image.
1107 *
1108 * Prior to invoking this routine, the following setup has occurred:
1109 *
1110 * 1. The postscript foreground color has been set to the color
1111 * used to render the bitmap.
1112 *
1113 * 2. The origin of the postscript coordinate system is set to
1114 * the lower left corner of the bitmap.
1115 *
1116 * 3. The postscript coordinate system has been scaled so that
1117 * the entire bitmap is one unit squared.
1118 *
1119 * Some postscript implementations cannot handle bitmap strings
1120 * longer than about 60k characters. If the bitmap data is that big
1121 * or bigger, then we render it by splitting it into several smaller
1122 * bitmaps.
1123 *
1124 * Results:
1125 * Returns TCL_OK on success. Returns TCL_ERROR and leaves and error
1126 * message in interp->result if there is a problem.
1127 *
1128 * Side effects:
1129 * Postscript code is appended to interp->result.
1130 *
1131 *----------------------------------------------------------------------
1132 */
1133
1134 static int
1135 ImgBmapPsImagemask(interp, width, height, data)
1136 Tcl_Interp *interp; /* Append postscript to this interpreter */
1137 int width, height; /* Width and height of the bitmap in pixels */
1138 char *data; /* Data for the bitmap */
1139 {
1140 int i, j, nBytePerRow;
1141 char buffer[200];
1142
1143 /*
1144 * The bit order of bitmaps in Tk is the opposite of the bit order that
1145 * postscript uses. (In Tk, the least significant bit is on the right
1146 * side of the bitmap and in postscript the least significant bit is shown
1147 * on the left.) The following array is used to reverse the order of bits
1148 * within a byte so that the bits will be in the order postscript expects.
1149 */
1150 static unsigned char bit_reverse[] = {
1151 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
1152 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
1153 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
1154 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
1155 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
1156 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
1157 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
1158 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
1159 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
1160 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
1161 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
1162 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
1163 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
1164 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
1165 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
1166 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255,
1167 };
1168
1169 if (width*height > 60000) {
1170 Tcl_ResetResult(interp);
1171 Tcl_AppendResult(interp, "unable to generate postscript for bitmaps "
1172 "larger than 60000 pixels", 0);
1173 return TCL_ERROR;
1174 }
1175 sprintf(buffer, "0 0 moveto %d %d true [%d 0 0 %d 0 %d] {<\n",
1176 width, height, width, -height, height);
1177 Tcl_AppendResult(interp, buffer, 0);
1178 nBytePerRow = (width+7)/8;
1179 for(i=0; i<height; i++){
1180 for(j=0; j<nBytePerRow; j++){
1181 sprintf(buffer, " %02x", bit_reverse[0xff & data[i*nBytePerRow + j]]);
1182 Tcl_AppendResult(interp, buffer, 0);
1183 }
1184 Tcl_AppendResult(interp, "\n", 0);
1185 }
1186 Tcl_AppendResult(interp, ">} imagemask \n", 0);
1187 return TCL_OK;
1188 }
1189
1190 /*
1191 *----------------------------------------------------------------------
1192 *
1193 * ImgBmapPostscript --
1194 *
1195 * This procedure generates postscript for rendering a bitmap image.
1196 *
1197 * Results:
1198 * On success, this routine writes postscript code into interp->result
1199 * and returns TCL_OK TCL_ERROR is returned and an error
1200 * message is left in interp->result if anything goes wrong.
1201 *
1202 * Side effects:
1203 * None.
1204 *
1205 *----------------------------------------------------------------------
1206 */
1207
1208 static int
1209 ImgBmapPostscript(clientData, interp, tkwin, psinfo, x, y, width, height,
1210 prepass)
1211 ClientData clientData;
1212 Tcl_Interp *interp;
1213 Tk_Window tkwin;
1214 Tk_PostscriptInfo psinfo;
1215 int x, y, width, height, prepass;
1216 {
1217 BitmapMaster *masterPtr = (BitmapMaster *) clientData;
1218 char buffer[200];
1219
1220 if (prepass) {
1221 return TCL_OK;
1222 }
1223
1224 /*
1225 * There is nothing to do for bitmaps with zero width or height
1226 */
1227 if( width<=0 || height<=0 || masterPtr->width<=0 || masterPtr->height<=0 ){
1228 return TCL_OK;
1229 }
1230
1231 /*
1232 * Translate the origin of the coordinate system to be the lower-left
1233 * corner of the bitmap and adjust the scale of the coordinate system
1234 * so that entire bitmap covers one square unit of the page.
1235 * The calling function put a "gsave" into the postscript and
1236 * will add a "grestore" at after this routine returns, so it is safe
1237 * to make whatever changes are necessary here.
1238 */
1239 if( x!=0 || y!=0 ){
1240 sprintf(buffer, "%d %d moveto\n", x, y);
1241 Tcl_AppendResult(interp, buffer, 0);
1242 }
1243 if( width!=1 || height!=1 ){
1244 sprintf(buffer, "%d %d scale\n", width, height);
1245 Tcl_AppendResult(interp, buffer, 0);
1246 }
1247
1248 /*
1249 * Color the background, if there is one. This step is skipped if the
1250 * background is transparent. If the background is not transparent and
1251 * there is no background mask, then color the complete rectangle that
1252 * encloses the bitmap. If there is a background mask, then only apply
1253 * color to the bits specified by the mask.
1254 */
1255 if ((masterPtr->bgUid != NULL) && (masterPtr->bgUid[0] != '\0')) {
1256 XColor color;
1257 XParseColor(Tk_Display(tkwin), Tk_Colormap(tkwin), masterPtr->bgUid,
1258 &color);
1259 if (Tk_PostscriptColor(interp, psinfo, &color) != TCL_OK) {
1260 return TCL_ERROR;
1261 }
1262 if (masterPtr->maskData == NULL) {
1263 Tcl_AppendResult(interp,
1264 "0 0 moveto 1 0 rlineto 0 1 rlineto -1 0 rlineto "
1265 "closepath fill\n", 0
1266 );
1267 } else if (ImgBmapPsImagemask(interp, masterPtr->width,
1268 masterPtr->height, masterPtr->maskData) != TCL_OK) {
1269 return TCL_ERROR;
1270 }
1271 }
1272
1273 /*
1274 * Draw the bitmap foreground, assuming there is one.
1275 */
1276 if ( (masterPtr->fgUid != NULL) && (masterPtr->data != NULL) ) {
1277 XColor color;
1278 XParseColor(Tk_Display(tkwin), Tk_Colormap(tkwin), masterPtr->fgUid,
1279 &color);
1280 if (Tk_PostscriptColor(interp, psinfo, &color) != TCL_OK) {
1281 return TCL_ERROR;
1282 }
1283 if (ImgBmapPsImagemask(interp, masterPtr->width, masterPtr->height,
1284 masterPtr->data) != TCL_OK) {
1285 return TCL_ERROR;
1286 }
1287 }
1288 return TCL_OK;
1289 }
1290