Debian Codegraph @

pTk/mTk/additions/imgXPM.c

raw
1 /*
2 * tkImgXPM.c --
3 *
4 * A photo image file handler for XPM files.
5 *
6 * Written by:
7 * Jan Nijtmans
8 * CMG Oost-Nederland B.V.
9 * email: [email protected] (private)
10 * [email protected] (work)
11 * url: http://purl.oclc.org/net/nijtmans/
12 *
13 * (with some code stolen from the XPM image type and the GIF handler)
14 *
15 * <[email protected]> Mario Weilguni
16 * Jan 1997:
17 * - fixed a bug when reading images with invalid color tables
18 * (if a pixelchar is not in the colormap)
19 * - added FileWriteXPM, StringWriteXPM and WriteXPM.
20 * - modified FileReadXPM, ReadXPM and added StringReadXPM
21 * This makes the XPM reader complete :-)
22 *
23 *
24 * SCCS: @(#) tkImgXPM.c 0.1 96/11/22 13:56:24
25 */
26 #include "tk.h"
27 #include "tkVMacro.h"
28 #include "imgInt.h"
29 #include <X11/Xlib.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #ifndef MAC_TCL
35 #include <sys/types.h>
36 #endif
37
38 /* constants used only in this file */
39
40 #define XPM_MONO 1
41 #define XPM_GRAY_4 2
42 #define XPM_GRAY 3
43 #define XPM_COLOR 4
44 #define XPM_SYMBOLIC 5
45 #define XPM_UNKNOWN 6
46
47 #define MAX_BUFFER 4096
48
49 /*
50 * The format record for the XPM file format:
51 */
52
53 static int ChanMatchXPM _ANSI_ARGS_((Tcl_Channel chan,
54 Tcl_Obj *fileName, Tcl_Obj *format, int *widthPtr,
55 int *heightPtr, Tcl_Interp *interp));
56 static int ObjMatchXPM _ANSI_ARGS_((Tcl_Obj *dataObj,
57 Tcl_Obj *format, int *widthPtr, int *heightPt, Tcl_Interp *interp));
58 static int ChanReadXPM _ANSI_ARGS_((Tcl_Interp *interp,
59 Tcl_Channel chan, Tcl_Obj *fileName,
60 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
61 int destX, int destY, int width, int height,
62 int srcX, int srcY));
63 static int ObjReadXPM _ANSI_ARGS_((Tcl_Interp *interp,
64 Tcl_Obj *dataObj, Tcl_Obj *format,
65 Tk_PhotoHandle imageHandle, int destX, int destY,
66 int width, int height, int srcX, int srcY));
67 static int CommonReadXPM _ANSI_ARGS_((Tcl_Interp *interp, MFile *handle,
68 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
69 int destX, int destY, int width, int height,
70 int srcX, int srcY));
71 static int StringWriteXPM _ANSI_ARGS_((Tcl_Interp *interp,
72 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
73 static int FileWriteXPM _ANSI_ARGS_((Tcl_Interp *interp,
74 char *fileName, Tcl_Obj *format,
75 Tk_PhotoImageBlock *blockPtr));
76 static int CommonWriteXPM _ANSI_ARGS_((Tcl_Interp *interp, CONST char *fileName,
77 Tcl_DString *dataPtr, Tcl_Obj *format,
78 Tk_PhotoImageBlock *blockPtr));
79
80 Tk_PhotoImageFormat imgFmtXPM = {
81 "xpm", /* name */
82 ChanMatchXPM, /* fileMatchProc */
83 ObjMatchXPM, /* stringMatchProc */
84 ChanReadXPM, /* fileReadProc */
85 ObjReadXPM, /* stringReadProc */
86 FileWriteXPM, /* fileWriteProc */
87 StringWriteXPM /* stringWriteProc */
88 };
89
90 /*
91 * Prototypes for local procedures defined in this file:
92 */
93
94 static int ReadXPMFileHeader _ANSI_ARGS_((MFile *handle,
95 int *widthPtr, int *heightPtr, int *numColors, int *byteSize));
96 static char * GetType _ANSI_ARGS_((char *colorDefn, int *type_ret));
97 static char * GetColor _ANSI_ARGS_((char *colorDefn, char *colorName, int *type_ret));
98 static char * Gets _ANSI_ARGS_((MFile *handle, char *buffer, int size));
99
100 /*
101 *----------------------------------------------------------------------
102 * Gets
103 *
104 * Allows other routines to get their data from channels, files as well
105 * as Tcl_DStrings.
106 *
107 * Results:
108 * same as fgets: NULL pointer on any error, otherwise
109 * it returns buffer
110 *
111 * Side effects:
112 * The access position of the file changes OR
113 * The access pointer dataPtr->p is changed
114 *
115 *----------------------------------------------------------------------
116 */
117
118 static char *
119 Gets(handle, buffer, size)
120 MFile *handle;
121 char *buffer;
122 int size;
123 {
124 char *p;
125
126 /* read data from MFile */
127 p = buffer;
128 while ((ImgRead(handle, p, 1) == 1)) {
129 if (--size <= 0) {
130 *p = 0; return buffer;
131 }
132 if (*p++ == '\n') {
133 *p = 0;
134 return buffer;
135 }
136 }
137 *p = 0;
138 return (p != buffer) ? buffer :(char *) NULL;
139 }
140
141
142 /*
143 *----------------------------------------------------------------------
144 *
145 * ObjMatchXPM --
146 *
147 * This procedure is invoked by the photo image type to see if
148 * a datastring contains image data in XPM format.
149 *
150 * Results:
151 * The return value is >0 if the first characters in data look
152 * like XPM data, and 0 otherwise.
153 *
154 * Side effects:
155 * none
156 *
157 *----------------------------------------------------------------------
158 */
159 static int
160 ObjMatchXPM(dataObj, format, widthPtr, heightPtr, interp)
161 Tcl_Obj *dataObj; /* The data supplied by the image */
162 Tcl_Obj *format; /* User-specified format object, or NULL. */
163 int *widthPtr, *heightPtr; /* The dimensions of the image are
164 * returned here if the file is a valid
165 * raw XPM file. */
166 Tcl_Interp *interp;
167 {
168 int numColors, byteSize;
169 MFile handle;
170
171 ImgFixObjMatchProc(&interp, &dataObj, &format, &widthPtr, &heightPtr);
172
173 handle.data = ImgGetStringFromObj(dataObj, &handle.length);
174 handle.state = IMG_STRING;
175
176 return ReadXPMFileHeader(&handle, widthPtr, heightPtr, &numColors, &byteSize);
177 }
178
179
180 /*
181 *----------------------------------------------------------------------
182 *
183 * ChanMatchXPM --
184 *
185 * This procedure is invoked by the photo image type to see if
186 * a file contains image data in XPM format.
187 *
188 * Results:
189 * The return value is >0 if the first characters in channel "chan"
190 * look like XPM data, and 0 otherwise.
191 *
192 * Side effects:
193 * The access position in chan may change.
194 *
195 *----------------------------------------------------------------------
196 */
197
198 static int
199 ChanMatchXPM(chan, fileName, format, widthPtr, heightPtr, interp)
200 Tcl_Channel chan; /* The image channel, open for reading. */
201 Tcl_Obj *fileName; /* The name of the image file. */
202 Tcl_Obj *format; /* User-specified format object, or NULL. */
203 int *widthPtr, *heightPtr; /* The dimensions of the image are
204 * returned here if the file is a valid
205 * raw XPM file. */
206 Tcl_Interp *interp;
207 {
208 int numColors, byteSize;
209 MFile handle;
210
211 ImgFixChanMatchProc(&interp, &chan, &fileName, &format, &widthPtr, &heightPtr);
212
213 handle.data = (char *) chan;
214 handle.state = IMG_CHAN;
215
216 return ReadXPMFileHeader(&handle, widthPtr, heightPtr, &numColors, &byteSize);
217 }
218
219
220 /*
221 *----------------------------------------------------------------------
222 *
223 * CommonReadXPM --
224 *
225 * This procedure is called by the photo image type to read
226 * XPM format data from a file or string and write it into a
227 * given photo image.
228 *
229 * Results:
230 * A standard TCL completion code. If TCL_ERROR is returned
231 * then an error message is left in interp->result.
232 *
233 * Side effects:
234 * The access position in file f is changed (if read from file)
235 * and new data is added to the image given by imageHandle.
236 *
237 *----------------------------------------------------------------------
238 */
239
240 typedef struct myblock {
241 Tk_PhotoImageBlock pub;
242 int dummy; /* extra space for offset[3], if not included already
243 in Tk_PhotoImageBlock */
244 } myblock;
245
246 static int
247 CommonReadXPM(interp, handle, format, imageHandle, destX, destY,
248 width, height, srcX, srcY)
249 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
250 MFile *handle; /* The image channel, open for reading. */
251 Tcl_Obj *format; /* User-specified format object, or NULL. */
252 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
253 int destX, destY; /* Coordinates of top-left pixel in
254 * photo image to be written to. */
255 int width, height; /* Dimensions of block of photo image to
256 * be written to. */
257 int srcX, srcY; /* Coordinates of top-left pixel to be used
258 * in image being read. */
259 {
260 int fileWidth = 0, fileHeight = 0, numColors = 0, byteSize = 0;
261 int h, type;
262 unsigned char *pixelPtr;
263 myblock block;
264 Tcl_HashTable colorTable;
265 Tk_Window tkwin = Tk_MainWindow(interp);
266 Display *display = Tk_Display(tkwin);
267 Colormap colormap = Tk_Colormap(tkwin);
268 int depth = Tk_Depth(tkwin);
269 char *p;
270 char buffer[MAX_BUFFER];
271 int i, isMono;
272 int color1;
273 unsigned int data;
274 Tcl_HashEntry *hPtr;
275
276 Tcl_InitHashTable(&colorTable, TCL_ONE_WORD_KEYS);
277
278 switch ((Tk_Visual(tkwin))->class) {
279 case StaticGray:
280 case GrayScale:
281 isMono = 1;
282 break;
283 default:
284 isMono = 0;
285 }
286
287 type = ReadXPMFileHeader(handle, &fileWidth, &fileHeight, &numColors, &byteSize);
288 if (type == 0) {
289 Tcl_AppendResult(interp, "couldn't read raw XPM header", NULL);
290 return TCL_ERROR;
291 }
292 if ((fileWidth <= 0) || (fileHeight <= 0)) {
293 Tcl_AppendResult(interp, "XPM image file has dimension(s) <= 0",
294 (char *) NULL);
295 return TCL_ERROR;
296 }
297 if ((byteSize < 1) || (byteSize > 4)) {
298 Tcl_AppendResult(interp, "XPM image file has invalid byte size ",
299 "(should be 1, 2, 3 or 4)", (char *) NULL);
300 return TCL_ERROR;
301 }
302
303 if ((srcX + width) > fileWidth) {
304 width = fileWidth - srcX;
305 }
306 if ((srcY + height) > fileHeight) {
307 height = fileHeight - srcY;
308 }
309 if ((width <= 0) || (height <= 0)
310 || (srcX >= fileWidth) || (srcY >= fileHeight)) {
311 return TCL_OK;
312 }
313
314 for (i=0; i<numColors; i++) {
315 char * colorDefn; /* the color definition line */
316 char * colorName; /* temp place to hold the color name
317 * defined for one type of visual */
318 char * useName; /* the color name used for this
319 * color. If there are many names
320 * defined, choose the name that is
321 * "best" for the target visual
322 */
323 XColor color;
324 int found;
325
326 p = Gets(handle, buffer,MAX_BUFFER);
327 while (((p = strchr(p,'\"')) == NULL)) {
328 p = Gets(handle, buffer,MAX_BUFFER);
329 if (p == NULL) {
330 return TCL_ERROR;
331 }
332 p = buffer;
333 }
334 colorDefn = p + byteSize + 1;
335 colorName = (char*)ckalloc(strlen(colorDefn)+1);
336 useName = (char*)ckalloc(strlen(colorDefn)+1);
337 found = 0;
338 color1 = 0;
339 data = 0;
340
341 while (colorDefn && *colorDefn) {
342 int type;
343
344 if ((colorDefn=GetColor(colorDefn, colorName, &type)) == NULL) {
345 break;
346 }
347 if (colorName[0] == '\0') {
348 continue;
349 }
350
351 switch (type) {
352 case XPM_MONO:
353 if (isMono && depth == 1) {
354 strcpy(useName, colorName);
355 found = 1; goto gotcolor;
356 }
357 break;
358 case XPM_GRAY_4:
359 if (isMono && depth == 4) {
360 strcpy(useName, colorName);
361 found = 1; goto gotcolor;
362 }
363 break;
364 case XPM_GRAY:
365 if (isMono && depth > 4) {
366 strcpy(useName, colorName);
367 found = 1; goto gotcolor;
368 }
369 break;
370 case XPM_COLOR:
371 if (!isMono) {
372 strcpy(useName, colorName);
373 found = 1; goto gotcolor;
374 }
375 break;
376 }
377 if (type != XPM_SYMBOLIC && type != XPM_UNKNOWN) {
378 if (!found) { /* use this color as default */
379 strcpy(useName, colorName);
380 found = 1;
381 }
382 }
383 }
384
385 gotcolor:
386
387 memcpy(&color1, p+1, byteSize);
388 p = useName;
389 while ((*p != 0) && (*p != '"') && (*p != ' ') && (*p != 't')) {
390 p++;
391 }
392 *p = 0;
393
394 data = 0;
395 if (strncasecmp(useName, "none",5)) {
396 if (XParseColor(display, colormap, useName, &color) == 0) {
397 color.red = color.green = color.blue = 0;
398 }
399 ((unsigned char *) &data)[0] = color.red>>8;
400 ((unsigned char *) &data)[1] = color.green>>8;
401 ((unsigned char *) &data)[2] = color.blue>>8;
402 ((unsigned char *) &data)[3] = 255;
403 }
404
405 hPtr = Tcl_CreateHashEntry(&colorTable, (char *) color1, &found);
406 Tcl_SetHashValue(hPtr, (char *) data);
407
408 ckfree(colorName);
409 ckfree(useName);
410 }
411
412 Tk_PhotoGetImage(imageHandle, &block.pub);
413 /* in case Tk_PhotoGetImage doesn't set this */
414 block.pub.offset[0] = 0;
415 block.pub.offset[1] = 1;
416 block.pub.offset[2] = 2;
417 block.pub.offset[3] = (block.pub.pixelSize > 3) ? 3 : 0;
418 block.pub.width = width;
419 block.pub.pitch = block.pub.pixelSize * fileWidth;
420 block.pub.height = 1;
421 block.pub.pixelPtr = (unsigned char *) ckalloc((unsigned) block.pub.pixelSize * width);
422
423 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
424
425 i = srcY;
426 while (i-- > 0) {
427 p = Gets(handle, buffer,MAX_BUFFER);
428 while (((p = strchr(p,'\"')) == NULL)) {
429 p = Gets(handle, buffer,MAX_BUFFER);
430 if (p == NULL) {
431 return TCL_ERROR;
432 }
433 p = buffer;
434 }
435 }
436
437
438 for (h = height; h > 0; h--) {
439 p = Gets(handle, buffer,MAX_BUFFER);
440 while (((p = strchr(p,'\"')) == NULL)) {
441 p = Gets(handle, buffer,MAX_BUFFER);
442 if (p == NULL) {
443 sprintf(buffer, "%d", h);
444 Tcl_AppendResult(interp, "XPM image file is truncated; still ", buffer, " line(s) need to be read",
445 (char *) NULL);
446 return TCL_ERROR;
447 }
448 p = buffer;
449 }
450 p += byteSize * srcX + 1;
451 pixelPtr = block.pub.pixelPtr;
452
453 for (i = 0; i < width; ) {
454 unsigned int col;
455
456 memcpy((char *) &color1, p, byteSize);
457 hPtr = Tcl_FindHashEntry(&colorTable, (char *) color1);
458
459 /*
460 * if hPtr == NULL, we have an invalid color entry in the XPM
461 * file. We use transparant as default color
462 */
463 if (hPtr != (Tcl_HashEntry *)NULL)
464 col = (int)Tcl_GetHashValue(hPtr);
465 else
466 col = (int)0;
467
468 /*
469 * we've found a non-transparent pixel, let's search the next
470 * transparent pixel and copy this block to the image
471 */
472 if (col) {
473 int len = 0, j;
474
475 j = i;
476 pixelPtr = block.pub.pixelPtr;
477 do {
478 memcpy(pixelPtr, &col, block.pub.pixelSize);
479 pixelPtr += block.pub.pixelSize;
480 i++;
481 len++;
482 p += byteSize;
483
484 if (i < width) {
485 memcpy((char *) &color1, p, byteSize);
486 hPtr = Tcl_FindHashEntry(&colorTable, (char *) color1);
487 if (hPtr != (Tcl_HashEntry *)NULL)
488 col = (int)Tcl_GetHashValue(hPtr);
489 else
490 col = (int)0;
491 }
492 } while ((i < width) && col);
493 Tk_PhotoPutBlock(imageHandle, &block.pub, destX+j, destY, len, 1, TK_PHOTO_COMPOSITE_SET);
494 } else {
495 p += byteSize;
496 i++;
497 }
498 }
499 destY++;
500 }
501
502 Tcl_DeleteHashTable(&colorTable);
503
504 ckfree((char *) block.pub.pixelPtr);
505 return TCL_OK;
506 }
507
508
509 /*
510 *----------------------------------------------------------------------
511 *
512 * ChanReadXPM --
513 *
514 * This procedure is called by the photo image type to read
515 * XPM format data from a channel and write it into a given
516 * photo image.
517 *
518 * Results:
519 * A standard TCL completion code. If TCL_ERROR is returned
520 * then an error message is left in interp->result.
521 *
522 * Side effects:
523 * The access position in channel chan is changed, and new
524 * data is added to the image given by imageHandle.
525 *
526 *----------------------------------------------------------------------
527 */
528
529 static int
530 ChanReadXPM(interp, chan, fileName, format, imageHandle, destX, destY,
531 width, height, srcX, srcY)
532 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
533 Tcl_Channel chan; /* The image channel, open for reading. */
534 Tcl_Obj *fileName; /* The name of the image file. */
535 Tcl_Obj *format; /* User-specified format object, or NULL. */
536 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
537 int destX, destY; /* Coordinates of top-left pixel in
538 * photo image to be written to. */
539 int width, height; /* Dimensions of block of photo image to
540 * be written to. */
541 int srcX, srcY; /* Coordinates of top-left pixel to be used
542 * in image being read. */
543 {
544 MFile handle;
545
546 handle.data = (char *) chan;
547 handle.state = IMG_CHAN;
548
549 return CommonReadXPM(interp, &handle, format, imageHandle,
550 destX, destY, width, height, srcX, srcY);
551 }
552
553
554 /*
555 *----------------------------------------------------------------------
556 *
557 * ObjReadXPM --
558 *
559 * This procedure is called by the photo image type to read
560 * XPM format data from a string and write it into a given
561 * photo image.
562 *
563 * Results:
564 * A standard TCL completion code. If TCL_ERROR is returned
565 * then an error message is left in interp->result.
566 *
567 * Side effects:
568 * New data is added to the image given by imageHandle.
569 *
570 *----------------------------------------------------------------------
571 */
572
573 static int
574 ObjReadXPM(interp, data, format, imageHandle, destX, destY,
575 width, height, srcX, srcY)
576 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
577 Tcl_Obj *data;
578 Tcl_Obj *format; /* User-specified format object, or NULL. */
579 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
580 int destX, destY; /* Coordinates of top-left pixel in
581 * photo image to be written to. */
582 int width, height; /* Dimensions of block of photo image to
583 * be written to. */
584 int srcX, srcY; /* Coordinates of top-left pixel to be used
585 * in image being read. */
586 {
587 MFile handle;
588
589 handle.data = ImgGetStringFromObj(data, &handle.length);
590 handle.state = IMG_STRING;
591
592 return CommonReadXPM(interp, &handle, format, imageHandle,
593 destX, destY, width, height, srcX, srcY);
594 }
595
596
597 /*
598 *----------------------------------------------------------------------
599 *
600 * ReadXPMFileHeader --
601 *
602 * This procedure reads the XPM header from the beginning of a
603 * XPM file and returns information from the header.
604 *
605 * Results:
606 * The return value is 1 if file "f" appears to start with a valid
607 * XPM header, and 0 otherwise. If the header is valid,
608 * then *widthPtr and *heightPtr are modified to hold the
609 * dimensions of the image and *numColors holds the number of
610 * colors and byteSize the number of bytes used for 1 pixel.
611 *
612 * Side effects:
613 * The access position in f advances.
614 *
615 *----------------------------------------------------------------------
616 */
617
618 #define UCHAR(c) ((unsigned char) (c))
619
620 static int
621 ReadXPMFileHeader(handle, widthPtr, heightPtr, numColors, byteSize)
622 MFile *handle; /* handle to read the header from */
623 int *widthPtr, *heightPtr; /* The dimensions of the image are
624 * returned here. */
625 int *numColors; /* the number of colors is returned here */
626 int *byteSize; /* number of bytes per pixel */
627 {
628 char buffer[MAX_BUFFER];
629 char *p;
630
631 p = Gets(handle, buffer,MAX_BUFFER);
632 if (p == NULL) {
633 return 0;
634 }
635 p = buffer;
636 while (*p && isspace(UCHAR(*p))) {
637 p++;
638 }
639 if (strncmp("/* XPM", p, 6) != 0) {
640 return 0;
641 }
642 while ((p = strchr(p,'{')) == NULL) {
643 p = Gets(handle, buffer,MAX_BUFFER);
644 if (p == NULL) {
645 return 0;
646 }
647 p = buffer;
648 }
649 while ((p = strchr(p,'"')) == NULL) {
650 p = Gets(handle, buffer,MAX_BUFFER);
651 if (p == NULL) {
652 return 0;
653 }
654 p = buffer;
655 }
656 p++;
657 while (p && *p && isspace(UCHAR(*p))) {
658 p++;
659 }
660 *widthPtr = strtoul(p, &p, 0);
661 if (p == NULL) {
662 return 0;
663 }
664 while (p && *p && isspace(UCHAR(*p))) {
665 p++;
666 }
667 *heightPtr = strtoul(p, &p, 0);
668 if (p == NULL) {
669 return 0;
670 }
671 while (p && *p && isspace(UCHAR(*p))) {
672 p++;
673 }
674 *numColors = strtoul(p, &p, 0);
675 if (p == NULL) {
676 return 0;
677 }
678 while (p && *p && isspace(UCHAR(*p))) {
679 p++;
680 }
681 *byteSize = strtoul(p, &p, 0);
682 if (p == NULL) {
683 return 0;
684 }
685 return 1;
686 }
687
688 static char * GetType(colorDefn, type_ret)
689 char * colorDefn;
690 int * type_ret;
691 {
692 char * p = colorDefn;
693
694 /* skip white spaces */
695 while (*p && isspace(UCHAR(*p))) {
696 p ++;
697 }
698
699 /* parse the type */
700 if (p[0] != '\0' && p[0] == 'm' &&
701 p[1] != '\0' && isspace(UCHAR(p[1]))) {
702 *type_ret = XPM_MONO;
703 p += 2;
704 }
705 else if (p[0] != '\0' && p[0] == 'g' &&
706 p[1] != '\0' && p[1] == '4' &&
707 p[2] != '\0' && isspace(UCHAR(p[2]))) {
708 *type_ret = XPM_GRAY_4;
709 p += 3;
710 }
711 else if (p[0] != '\0' && p[0] == 'g' &&
712 p[1] != '\0' && isspace(UCHAR(p[1]))) {
713 *type_ret = XPM_GRAY;
714 p += 2;
715 }
716 else if (p[0] != '\0' && p[0] == 'c' &&
717 p[1] != '\0' && isspace(UCHAR(p[1]))) {
718 *type_ret = XPM_COLOR;
719 p += 2;
720 }
721 else if (p[0] != '\0' && p[0] == 's' &&
722 p[1] != '\0' && isspace(UCHAR(p[1]))) {
723 *type_ret = XPM_SYMBOLIC;
724 p += 2;
725 }
726 else {
727 *type_ret = XPM_UNKNOWN;
728 return NULL;
729 }
730
731 return p;
732 }
733
734 /* colorName is guaranteed to be big enough */
735 static char * GetColor(colorDefn, colorName, type_ret)
736 char * colorDefn;
737 char * colorName; /* if found, name is copied to this array */
738 int * type_ret;
739 {
740 int type;
741 char * p;
742
743 if (!colorDefn) {
744 return NULL;
745 }
746
747 if ((colorDefn = GetType(colorDefn, &type)) == NULL) {
748 /* unknown type */
749 return NULL;
750 }
751 else {
752 *type_ret = type;
753 }
754
755 /* skip white spaces */
756 while (*colorDefn && isspace(UCHAR(*colorDefn))) {
757 colorDefn ++;
758 }
759
760 p = colorName;
761
762 while (1) {
763 int dummy;
764
765 while (*colorDefn && !isspace(UCHAR(*colorDefn))) {
766 *p++ = *colorDefn++;
767 }
768
769 if (!*colorDefn) {
770 break;
771 }
772
773 if (GetType(colorDefn, &dummy) == NULL) {
774 /* the next string should also be considered as a part of a color
775 * name */
776
777 while (*colorDefn && isspace(UCHAR(*colorDefn))) {
778 *p++ = *colorDefn++;
779 }
780 } else {
781 break;
782 }
783 if (!*colorDefn) {
784 break;
785 }
786 }
787
788 /* Mark the end of the colorName */
789 *p = '\0';
790
791 return colorDefn;
792 }
793
794
795 /*
796 *----------------------------------------------------------------------
797 *
798 * FileWriteXPM
799 *
800 * Writes a XPM image to a file. Just calls WriteXPM
801 * with appropriate arguments.
802 *
803 * Results:
804 * Returns the return value of WriteXPM
805 *
806 * Side effects:
807 * A file is (hopefully) created on success.
808 *
809 *----------------------------------------------------------------------
810 */
811 static int
812 FileWriteXPM(interp, fileName, format, blockPtr)
813 Tcl_Interp *interp;
814 char *fileName;
815 Tcl_Obj *format;
816 Tk_PhotoImageBlock *blockPtr;
817 {
818 return CommonWriteXPM(interp, fileName, (Tcl_DString *)NULL, format, blockPtr);
819 }
820
821
822 /*
823 *----------------------------------------------------------------------
824 *
825 * StringWriteXPM
826 *
827 * Writes a XPM image to a string. Just calls WriteXPM
828 * with appropriate arguments.
829 *
830 * Results:
831 * Returns the return value of WriteXPM
832 *
833 * Side effects:
834 * The Tcl_DString dataPtr is modified on success.
835 *
836 *----------------------------------------------------------------------
837 */
838 static int
839 StringWriteXPM(interp, format, blockPtr)
840 Tcl_Interp *interp;
841 Tcl_Obj *format;
842 Tk_PhotoImageBlock *blockPtr;
843 {
844 int result;
845 Tcl_DString data;
846 Tcl_DString *dataPtr;
847 ImgFixStringWriteProc(&data, &interp, &dataPtr, &format, &blockPtr);
848 result = CommonWriteXPM(interp, "unknown", dataPtr, format, blockPtr);
849 if ((result == TCL_OK) && (dataPtr == &data)) {
850 Tcl_DStringResult(interp, dataPtr);
851 }
852 return result;
853 }
854
855
856 /*
857 * Yes, I know these macros are dangerous. But it should work fine
858 */
859 #define WRITE(buf) { if (!dataPtr) Tcl_Write(chan, buf, -1); else Tcl_DStringAppend(dataPtr, buf, -1);}
860
861 /*
862 *----------------------------------------------------------------------
863 *
864 * CommonWriteXPM
865 *
866 * This procedure writes a XPM image to the file filename
867 * (if filename != NULL) or to dataPtr.
868 *
869 * Results:
870 * Returns TCL_OK on success, or TCL_ERROR on error.
871 * Possible failures are:
872 * 1. cannot access file (permissions or path not found)
873 * 2. TkPhotoGetMask fails to retrieve the region mask
874 * for the image (should not happen)
875 *
876 * Side effects:
877 * varies (see StringWriteXPM and FileWriteXPM)
878 *
879 *----------------------------------------------------------------------
880 */
881 static int
882 CommonWriteXPM(interp, fileName, dataPtr, format, blockPtr)
883 Tcl_Interp *interp;
884 CONST char *fileName;
885 Tcl_DString *dataPtr;
886 Tcl_Obj *format;
887 Tk_PhotoImageBlock *blockPtr;
888 {
889 int x, y, i;
890 int found;
891 Tcl_Channel chan = (Tcl_Channel) NULL;
892 Tcl_HashTable colors;
893 Tcl_HashEntry *entry;
894 Tcl_HashSearch search;
895 unsigned char *pp;
896 int ncolors, chars_per_pixel;
897 int greenOffset, blueOffset, alphaOffset;
898 union {
899 ClientData value;
900 char component[3];
901 } col;
902 union {
903 ClientData value;
904 char component[5];
905 } temp;
906 char buffer[256], *p;
907
908 /*
909 * xpm_chars[] must be 64 chars long
910 */
911 static char xpm_chars[] =
912 ".#abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
913
914 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
915 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
916 alphaOffset = blockPtr->offset[0];
917 if (alphaOffset < blockPtr->offset[1]) alphaOffset = blockPtr->offset[1];
918 if (alphaOffset < blockPtr->offset[2]) alphaOffset = blockPtr->offset[2];
919 if (++alphaOffset < blockPtr->pixelSize) {
920 alphaOffset -= blockPtr->offset[0];
921 } else {
922 alphaOffset = 0;
923 }
924
925 /* open the output file (if needed) */
926 if (!dataPtr) {
927 chan = Tcl_OpenFileChannel(interp, (char *) fileName, "w", 0644);
928 if (!chan) {
929 return TCL_ERROR;
930 }
931 }
932
933 /* compute image name */
934
935 p = strrchr(fileName, '/');
936 if (p) {
937 fileName = p+1;
938 }
939 p = strrchr(fileName, '\\');
940 if (p) {
941 fileName = p+1;
942 }
943 p = strrchr(fileName, ':');
944 if (p) {
945 fileName = p+1;
946 }
947 p = strchr(fileName, '.');
948 if (p) {
949 *p = '\0';
950 }
951 sprintf(buffer, "/* XPM */\nstatic char * %s[] = {\n", fileName);
952 if (p) {
953 *p = '.';
954 }
955 WRITE(buffer);
956
957 /*
958 * Compute size of colortable
959 */
960 Tcl_InitHashTable(&colors, TCL_ONE_WORD_KEYS);
961 ncolors = 0;
962 col.value = 0;
963 for (y = 0; y < blockPtr->height; y++) {
964 pp = blockPtr->pixelPtr + y * blockPtr->pitch + blockPtr->offset[0];
965 for (x = blockPtr->width; x >0; x--) {
966 if (!alphaOffset || pp[alphaOffset]) {
967 col.component[0] = pp[0];
968 col.component[1] = pp[greenOffset];
969 col.component[2] = pp[blueOffset];
970 if (Tcl_FindHashEntry(&colors, (char *) col.value) == NULL) {
971 ncolors++;
972 entry = Tcl_CreateHashEntry(&colors, (char *) col.value,
973 &found);
974 }
975 }
976 pp += blockPtr->pixelSize;
977 }
978 }
979
980 /* compute number of characters per pixel */
981 chars_per_pixel = 1;
982 i = ncolors;
983 while(i > 64) {
984 chars_per_pixel++;
985 i /= 64;
986 }
987
988 /* write image info into XPM */
989 sprintf(buffer, "\"%d %d %d %d\",\n", blockPtr->width, blockPtr->height,
990 ncolors+(alphaOffset != 0), chars_per_pixel);
991 WRITE(buffer);
992
993 /* write transparent color id if transparency is available*/
994 if (alphaOffset) {
995 strcpy(temp.component, " ");
996 temp.component[chars_per_pixel] = 0;
997 sprintf(buffer, "\"%s s None c None\",\n", temp.component);
998 WRITE(buffer);
999 }
1000
1001 /* write colormap strings */
1002 entry = Tcl_FirstHashEntry(&colors, &search);
1003 y = 0;
1004 temp.component[chars_per_pixel] = 0;
1005 while(entry) {
1006 /* compute a color identifier for color #y */
1007 for (i = 0, x = y++; i < chars_per_pixel; i++, x /= 64)
1008 temp.component[i] = xpm_chars[x & 63];
1009
1010 /*
1011 * and put it in the hashtable
1012 * this is a little bit tricky
1013 */
1014 Tcl_SetHashValue(entry, (char *) temp.value);
1015 pp = (unsigned char *)&entry->key.oneWordValue;
1016 sprintf(buffer, "\"%s c #%02x%02x%02x\",\n",
1017 temp.component, pp[0], pp[1], pp[2]);
1018 WRITE(buffer);
1019 entry = Tcl_NextHashEntry(&search);
1020 }
1021
1022 /* write image itself */
1023 pp = blockPtr->pixelPtr + blockPtr->offset[0];
1024 buffer[chars_per_pixel] = 0;
1025 for (y = 0; y < blockPtr->height; y++) {
1026 WRITE("\"");
1027 for (x = 0; x < blockPtr->width; x++) {
1028 if (!alphaOffset || pp[alphaOffset]) {
1029 col.component[0] = pp[0];
1030 col.component[1] = pp[greenOffset];
1031 col.component[2] = pp[blueOffset];
1032 entry = Tcl_FindHashEntry(&colors, (char *) col.value);
1033 temp.value = Tcl_GetHashValue(entry);
1034 memcpy(buffer, temp.component, chars_per_pixel);
1035 } else {
1036 /* make transparent pixel */
1037 memcpy(buffer, " ", chars_per_pixel);
1038 }
1039 pp += blockPtr->pixelSize;
1040 WRITE(buffer);
1041 }
1042 if (y == blockPtr->height - 1) {
1043 WRITE("\"};");
1044 } else {
1045 WRITE("\",\n");
1046 }
1047 }
1048
1049 /* Delete the hash table */
1050 Tcl_DeleteHashTable(&colors);
1051
1052 /* close the file */
1053 if (chan) {
1054 Tcl_Close(interp, chan);
1055 }
1056 return TCL_OK;
1057 }
1058