Debian Codegraph @

pTk/mTk/additions/imgXBM.c

raw
1 /*
2 * imgXBM.c --
3 *
4 * A photo image file handler for XBM 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 */
14 #include "tk.h"
15 #include "tkVMacro.h"
16 #include "imgInt.h"
17 #include <string.h>
18 #include <ctype.h>
19 #include <stdlib.h>
20
21 /* constants used only in this file */
22
23 #define MAX_BUFFER 4096
24
25 /*
26 * The following data structure is used to describe the state of
27 * parsing a bitmap file or string. It is used for communication
28 * between TkGetBitmapData and NextBitmapWord.
29 */
30
31 #define MAX_WORD_LENGTH 100
32 typedef struct ParseInfo {
33 MFile handle;
34 char word[MAX_WORD_LENGTH+2];
35 /* Current word of bitmap data, NULL
36 * terminated. */
37 int wordLength; /* Number of non-NULL bytes in word. */
38 } ParseInfo;
39
40 /*
41 * The format record for the XBM file format:
42 */
43
44 static int ChnMatchXBM _ANSI_ARGS_((Tcl_Channel chan,
45 Tcl_Obj *fileName, Tcl_Obj *format, int *widthPtr,
46 int *heightPtr, Tcl_Interp *interp));
47 static int ObjMatchXBM _ANSI_ARGS_((Tcl_Obj *data,
48 Tcl_Obj *format, int *widthPtr, int *heightPtr, Tcl_Interp *interp));
49 static int ChnReadXBM _ANSI_ARGS_((Tcl_Interp *interp,
50 Tcl_Channel chan, Tcl_Obj *fileName,
51 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
52 int destX, int destY, int width, int height,
53 int srcX, int srcY));
54 static int ObjReadXBM _ANSI_ARGS_((Tcl_Interp *interp,
55 Tcl_Obj *dataObj, Tcl_Obj *format,
56 Tk_PhotoHandle imageHandle, int destX, int destY,
57 int width, int height, int srcX, int srcY));
58 static int ChnWriteXBM _ANSI_ARGS_((Tcl_Interp *interp,
59 char *fileName, Tcl_Obj *format,
60 Tk_PhotoImageBlock *blockPtr));
61 static int StringWriteXBM _ANSI_ARGS_((Tcl_Interp *interp,
62 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
63
64 static int CommonReadXBM _ANSI_ARGS_((Tcl_Interp *interp,
65 ParseInfo *parseInfo,
66 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
67 int destX, int destY, int width, int height,
68 int srcX, int srcY));
69 static int CommonWriteXBM _ANSI_ARGS_((Tcl_Interp *interp,
70 CONST char *fileName, Tcl_DString *dataPtr,
71 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
72
73 Tk_PhotoImageFormat imgFmtXBM = {
74 "xbm", /* name */
75 ChnMatchXBM, /* fileMatchProc */
76 ObjMatchXBM, /* stringMatchProc */
77 ChnReadXBM, /* fileReadProc */
78 ObjReadXBM, /* stringReadProc */
79 ChnWriteXBM, /* fileWriteProc */
80 StringWriteXBM /* stringWriteProc */
81 };
82
83 /*
84 * Prototypes for local procedures defined in this file:
85 */
86
87 static int ReadXBMFileHeader _ANSI_ARGS_((ParseInfo *parseInfo,
88 int *widthPtr, int *heightPtr));
89 static int NextBitmapWord _ANSI_ARGS_((ParseInfo *parseInfoPtr));
90
91 /*
92 *----------------------------------------------------------------------
93 *
94 * ObjMatchXBM --
95 *
96 * This procedure is invoked by the photo image type to see if
97 * a datastring contains image data in XBM format.
98 *
99 * Results:
100 * The return value is >0 if the first characters in data look
101 * like XBM data, and 0 otherwise.
102 *
103 * Side effects:
104 * none
105 *
106 *----------------------------------------------------------------------
107 */
108 static int
109 ObjMatchXBM(data, format, widthPtr, heightPtr, interp)
110 Tcl_Obj *data; /* The data supplied by the image */
111 Tcl_Obj *format; /* User-specified format string, or NULL. */
112 int *widthPtr, *heightPtr; /* The dimensions of the image are
113 * returned here if the file is a valid
114 * raw XBM file. */
115 Tcl_Interp *interp;
116 {
117 ParseInfo parseInfo;
118
119 ImgFixObjMatchProc(&interp, &data, &format, &widthPtr, &heightPtr);
120
121 parseInfo.handle.data = ImgGetStringFromObj(data, &parseInfo.handle.length);
122 parseInfo.handle.state = IMG_STRING;
123
124 return ReadXBMFileHeader(&parseInfo, widthPtr, heightPtr);
125 }
126
127
128 /*
129 *----------------------------------------------------------------------
130 *
131 * ChnMatchXBM --
132 *
133 * This procedure is invoked by the photo image type to see if
134 * a channel contains image data in XBM format.
135 *
136 * Results:
137 * The return value is >0 if the first characters in channel "chan"
138 * look like XBM data, and 0 otherwise.
139 *
140 * Side effects:
141 * The access position in chan may change.
142 *
143 *----------------------------------------------------------------------
144 */
145
146 static int
147 ChnMatchXBM(chan, fileName, format, widthPtr, heightPtr, interp)
148 Tcl_Channel chan; /* The image channel, open for reading. */
149 Tcl_Obj *fileName; /* The name of the image file. */
150 Tcl_Obj *format; /* User-specified format object, or NULL. */
151 int *widthPtr, *heightPtr; /* The dimensions of the image are
152 * returned here if the file is a valid
153 * raw XBM file. */
154 Tcl_Interp *interp;
155 {
156 ParseInfo parseInfo;
157
158 ImgFixChanMatchProc(&interp, &chan, &fileName, &format, &widthPtr, &heightPtr);
159
160 parseInfo.handle.data = (char *) chan;
161 parseInfo.handle.state = IMG_CHAN;
162
163 return ReadXBMFileHeader(&parseInfo, widthPtr, heightPtr);
164 }
165
166
167 /*
168 *----------------------------------------------------------------------
169 *
170 * CommonReadXBM --
171 *
172 * This procedure is called by the photo image type to read
173 * XBM format data from a file or string and write it into a
174 * given photo image.
175 *
176 * Results:
177 * A standard TCL completion code. If TCL_ERROR is returned
178 * then an error message is left in interp->result.
179 *
180 * Side effects:
181 * The access position in file f is changed (if read from file)
182 * and new data is added to the image given by imageHandle.
183 *
184 *----------------------------------------------------------------------
185 */
186
187 typedef struct myblock {
188 Tk_PhotoImageBlock ck;
189 int dummy; /* extra space for offset[3], if not included already
190 in Tk_PhotoImageBlock */
191 } myblock;
192
193 #define block bl.ck
194
195 static int
196 CommonReadXBM(interp, parseInfo, format, imageHandle, destX, destY,
197 width, height, srcX, srcY)
198 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
199 ParseInfo *parseInfo;
200 Tcl_Obj *format; /* User-specified format string, or NULL. */
201 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
202 int destX, destY; /* Coordinates of top-left pixel in
203 * photo image to be written to. */
204 int width, height; /* Dimensions of block of photo image to
205 * be written to. */
206 int srcX, srcY; /* Coordinates of top-left pixel to be used
207 * in image being read. */
208 {
209 myblock bl;
210 int fileWidth, fileHeight;
211 int numBytes, row, col, value, i;
212 unsigned char *data, *pixelPtr;
213 char *end;
214
215 ReadXBMFileHeader(parseInfo, &fileWidth, &fileHeight);
216
217 if ((srcX + width) > fileWidth) {
218 width = fileWidth - srcX;
219 }
220 if ((srcY + height) > fileHeight) {
221 height = fileHeight - srcY;
222 }
223 if ((width <= 0) || (height <= 0)
224 || (srcX >= fileWidth) || (srcY >= fileHeight)) {
225 return TCL_OK;
226 }
227
228 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
229
230 numBytes = ((fileWidth+7)/8)*32;
231 block.width = fileWidth;
232 block.height = 1;
233 block.pixelSize = 4;
234 block.offset[0] = 0;
235 block.offset[1] = 1;
236 block.offset[2] = 2;
237 block.offset[3] = 3;
238
239 data = (unsigned char *) ckalloc((unsigned) numBytes);
240 block.pixelPtr = data + srcX*4;
241 for (row = 0; row < srcY + height; row++) {
242 pixelPtr = data;
243 for (col = 0; col<(numBytes/32); col++) {
244 if (NextBitmapWord(parseInfo) != TCL_OK) {
245 ckfree((char *) data);
246 return TCL_ERROR;
247 }
248 value = (int) strtol(parseInfo->word, &end, 0);
249 if (end == parseInfo->word) {
250 ckfree((char *) data);
251 return TCL_ERROR;
252 }
253 for (i=0; i<8; i++) {
254 *pixelPtr++ = 0;
255 *pixelPtr++ = 0;
256 *pixelPtr++ = 0;
257 *pixelPtr++ = (value & 0x1)? 255:0;
258 value >>= 1;
259 }
260 }
261 if (row >= srcY) {
262 ImgPhotoPutBlock(imageHandle, &block, destX, destY++, width, 1);
263 }
264 }
265 ckfree((char *) data);
266 return TCL_OK;
267 }
268
269
270 /*
271 *----------------------------------------------------------------------
272 *
273 * ChnReadXBM --
274 *
275 * This procedure is called by the photo image type to read
276 * XBM format data from a channel and write it into a given
277 * photo image.
278 *
279 * Results:
280 * A standard TCL completion code. If TCL_ERROR is returned
281 * then an error message is left in interp->result.
282 *
283 * Side effects:
284 * The access position in channel chan is changed, and new data is
285 * added to the image given by imageHandle.
286 *
287 *----------------------------------------------------------------------
288 */
289
290 static int
291 ChnReadXBM(interp, chan, fileName, format, imageHandle, destX, destY,
292 width, height, srcX, srcY)
293 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
294 Tcl_Channel chan; /* The image channel, open for reading. */
295 Tcl_Obj *fileName; /* The name of the image file. */
296 Tcl_Obj *format; /* User-specified format object, or NULL. */
297 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
298 int destX, destY; /* Coordinates of top-left pixel in
299 * photo image to be written to. */
300 int width, height; /* Dimensions of block of photo image to
301 * be written to. */
302 int srcX, srcY; /* Coordinates of top-left pixel to be used
303 * in image being read. */
304 {
305 ParseInfo parseInfo;
306
307 parseInfo.handle.data = (char *) chan;
308 parseInfo.handle.state = IMG_CHAN;
309
310 return CommonReadXBM(interp, &parseInfo, format, imageHandle,
311 destX, destY, width, height, srcX, srcY);
312 }
313
314 /*
315 *----------------------------------------------------------------------
316 *
317 * ObjReadXBM --
318 *
319 * This procedure is called by the photo image type to read
320 * XBM format data from a string and write it into a given
321 * photo image.
322 *
323 * Results:
324 * A standard TCL completion code. If TCL_ERROR is returned
325 * then an error message is left in interp->result.
326 *
327 * Side effects:
328 * New data is added to the image given by imageHandle.
329 *
330 *----------------------------------------------------------------------
331 */
332
333 static int
334 ObjReadXBM(interp, data, format, imageHandle, destX, destY,
335 width, height, srcX, srcY)
336 Tcl_Interp *interp; /* Interpreter to use for reporting errors. */
337 Tcl_Obj *data;
338 Tcl_Obj *format; /* User-specified format string, or NULL. */
339 Tk_PhotoHandle imageHandle; /* The photo image to write into. */
340 int destX, destY; /* Coordinates of top-left pixel in
341 * photo image to be written to. */
342 int width, height; /* Dimensions of block of photo image to
343 * be written to. */
344 int srcX, srcY; /* Coordinates of top-left pixel to be used
345 * in image being read. */
346 {
347 ParseInfo parseInfo;
348 parseInfo.handle.data = ImgGetStringFromObj(data, &parseInfo.handle.length);
349 parseInfo.handle.state = IMG_STRING;
350
351 return CommonReadXBM(interp, &parseInfo, format, imageHandle,
352 destX, destY, width, height, srcX, srcY);
353 }
354
355 /*
356 *----------------------------------------------------------------------
357 *
358 * ReadXBMFileHeader --
359 *
360 * This procedure reads the XBM header from the beginning of a
361 * XBM file and returns information from the header.
362 *
363 * Results:
364 * The return value is 1 if file "f" appears to start with a valid
365 * XBM header, and 0 otherwise. If the header is valid,
366 * then *widthPtr and *heightPtr are modified to hold the
367 * dimensions of the image and *numColors holds the number of
368 * colors and byteSize the number of bytes used for 1 pixel.
369 *
370 * Side effects:
371 * The access position in f advances.
372 *
373 *----------------------------------------------------------------------
374 */
375
376 #define UCHAR(c) ((unsigned char) (c))
377
378 /*
379 *----------------------------------------------------------------------
380 *
381 * NextBitmapWord --
382 *
383 * This procedure retrieves the next word of information (stuff
384 * between commas or white space) from a bitmap description.
385 *
386 * Results:
387 * Returns TCL_OK if all went well. In this case the next word,
388 * and its length, will be availble in *parseInfoPtr. If the end
389 * of the bitmap description was reached then TCL_ERROR is returned.
390 *
391 * Side effects:
392 * None.
393 *
394 *----------------------------------------------------------------------
395 */
396
397 static int
398 NextBitmapWord(parseInfoPtr)
399 ParseInfo *parseInfoPtr; /* Describes what we're reading
400 * and where we are in it. */
401 {
402 char *dst, buf;
403 int num;
404
405 parseInfoPtr->wordLength = 0;
406 dst = parseInfoPtr->word;
407
408 for (num=ImgRead(&parseInfoPtr->handle,&buf,1); isspace(UCHAR(buf)) || (buf == ',');
409 num=ImgRead(&parseInfoPtr->handle,&buf,1)) {
410 if (num == 0 || buf == EOF) {
411 return TCL_ERROR;
412 }
413 }
414 for ( ; !isspace(UCHAR(buf)) && (buf != ',') && (num != 0);
415 num=ImgRead(&parseInfoPtr->handle,&buf,1)) {
416 if (UCHAR(buf) < ' ' || UCHAR(buf) > '~') {
417 return TCL_ERROR;
418 }
419 *dst = buf;
420 dst++;
421 parseInfoPtr->wordLength++;
422 if (num == 0 || parseInfoPtr->wordLength > MAX_WORD_LENGTH) {
423 return TCL_ERROR;
424 }
425 }
426
427 if (parseInfoPtr->wordLength == 0) {
428 return TCL_ERROR;
429 }
430 parseInfoPtr->word[parseInfoPtr->wordLength] = 0;
431 return TCL_OK;
432 }
433
434 static int
435 ReadXBMFileHeader(pi, widthPtr, heightPtr)
436 ParseInfo *pi;
437 int *widthPtr, *heightPtr; /* The dimensions of the image are
438 * returned here. */
439 {
440 int width, height, hotX, hotY;
441 char *end;
442
443 /*
444 * Parse the lines that define the dimensions of the bitmap,
445 * plus the first line that defines the bitmap data (it declares
446 * the name of a data variable but doesn't include any actual
447 * data). These lines look something like the following:
448 *
449 * #define foo_width 16
450 * #define foo_height 16
451 * #define foo_x_hot 3
452 * #define foo_y_hot 3
453 * static char foo_bits[] = {
454 *
455 * The x_hot and y_hot lines may or may not be present. It's
456 * important to check for "char" in the last line, in order to
457 * reject old X10-style bitmaps that used shorts.
458 */
459
460 width = 0;
461 height = 0;
462 hotX = -1;
463 hotY = -1;
464 while (1) {
465 if (NextBitmapWord(pi) != TCL_OK) {
466 return 0;
467 }
468 if ((pi->wordLength >= 6) && (pi->word[pi->wordLength-6] == '_')
469 && (strcmp(pi->word+pi->wordLength-6, "_width") == 0)) {
470 if (NextBitmapWord(pi) != TCL_OK) {
471 return 0;
472 }
473 width = strtol(pi->word, &end, 0);
474 if ((end == pi->word) || (*end != 0)) {
475 return 0;
476 }
477 } else if ((pi->wordLength >= 7) && (pi->word[pi->wordLength-7] == '_')
478 && (strcmp(pi->word+pi->wordLength-7, "_height") == 0)) {
479 if (NextBitmapWord(pi) != TCL_OK) {
480 return 0;
481 }
482 height = strtol(pi->word, &end, 0);
483 if ((end == pi->word) || (*end != 0)) {
484 return 0;
485 }
486 } else if ((pi->wordLength >= 6) && (pi->word[pi->wordLength-6] == '_')
487 && (strcmp(pi->word+pi->wordLength-6, "_x_hot") == 0)) {
488 if (NextBitmapWord(pi) != TCL_OK) {
489 return 0;
490 }
491 hotX = strtol(pi->word, &end, 0);
492 if ((end == pi->word) || (*end != 0)) {
493 return 0;
494 }
495 } else if ((pi->wordLength >= 6) && (pi->word[pi->wordLength-6] == '_')
496 && (strcmp(pi->word+pi->wordLength-6, "_y_hot") == 0)) {
497 if (NextBitmapWord(pi) != TCL_OK) {
498 return 0;
499 }
500 hotY = strtol(pi->word, &end, 0);
501 if ((end == pi->word) || (*end != 0)) {
502 return 0;
503 }
504 } else if ((pi->word[0] == 'c') && (strcmp(pi->word, "char") == 0)) {
505 while (1) {
506 if (NextBitmapWord(pi) != TCL_OK) {
507 return 0;
508 }
509 if ((pi->word[0] == '{') && (pi->word[1] == 0)) {
510 goto getData;
511 }
512 }
513 } else if ((pi->word[0] == '{') && (pi->word[1] == 0)) {
514 return 0;
515 }
516 }
517
518 getData:
519 *widthPtr = width;
520 *heightPtr = height;
521 return 1;
522 }
523
524
525 /*
526 *----------------------------------------------------------------------
527 *
528 * ChnWriteXBM
529 *
530 * Writes a XBM image to a file. Just calls CommonWriteXBM
531 * with appropriate arguments.
532 *
533 * Results:
534 * Returns the return value of CommonWriteXBM
535 *
536 * Side effects:
537 * A file is (hopefully) created on success.
538 *
539 *----------------------------------------------------------------------
540 */
541 static int
542 ChnWriteXBM(interp, fileName, format, blockPtr)
543 Tcl_Interp *interp;
544 char *fileName;
545 Tcl_Obj *format;
546 Tk_PhotoImageBlock *blockPtr;
547 {
548 return CommonWriteXBM(interp, fileName, (Tcl_DString *)NULL, format, blockPtr);
549 }
550
551
552 /*
553 *----------------------------------------------------------------------
554 *
555 * StringWriteXBM
556 *
557 * Writes a XBM image to a string. Just calls CommonWriteXBM
558 * with appropriate arguments.
559 *
560 * Results:
561 * Returns the return value of CommonWriteXBM
562 *
563 * Side effects:
564 * The Tcl_DString dataPtr is modified on success.
565 *
566 *----------------------------------------------------------------------
567 */
568 static int
569 StringWriteXBM(interp, format, blockPtr)
570 Tcl_Interp *interp;
571 Tcl_Obj *format;
572 Tk_PhotoImageBlock *blockPtr;
573 {
574 int result;
575 Tcl_DString data;
576 Tcl_DString *dataPtr;
577
578 ImgFixStringWriteProc(&data, &interp, &dataPtr, &format, &blockPtr);
579
580 result = CommonWriteXBM(interp, (CONST char *) NULL, dataPtr, format, blockPtr);
581
582 if ((result == TCL_OK) && (dataPtr == &data)) {
583 Tcl_DStringResult(interp, dataPtr);
584 }
585 return result;
586 }
587
588
589 /*
590 * Yes, I know these macros are dangerous. But it should work fine
591 */
592 #define WRITE(buf) { if (chan) Tcl_Write(chan, buf, -1); else Tcl_DStringAppend(dataPtr, buf, -1);}
593
594 /*
595 *----------------------------------------------------------------------
596 *
597 * CommonWriteXBM
598 *
599 * This procedure writes a XBM image to the file filename
600 * (if filename != NULL) or to dataPtr.
601 *
602 * Results:
603 * Returns TCL_OK on success, or TCL_ERROR on error.
604 *
605 * Side effects:
606 * varies (see StringWriteXBM and ChnWriteXBM)
607 *
608 *----------------------------------------------------------------------
609 */
610 static int
611 CommonWriteXBM(interp, fileName, dataPtr, format, blockPtr)
612 Tcl_Interp *interp;
613 CONST char *fileName;
614 Tcl_DString *dataPtr;
615 Tcl_Obj *format;
616 Tk_PhotoImageBlock *blockPtr;
617 {
618 Tcl_Channel chan = (Tcl_Channel) NULL;
619 char buffer[256];
620 unsigned char *pp;
621 int x, y, value, mask;
622 int sep = ' ';
623 int alphaOffset;
624 char *p = NULL;
625 static CONST char header[] =
626 "#define %s_width %d\n\
627 #define %s_height %d\n\
628 static char %s_bits[] = {\n";
629
630 alphaOffset = blockPtr->offset[0];
631 if (alphaOffset < blockPtr->offset[1]) alphaOffset = blockPtr->offset[1];
632 if (alphaOffset < blockPtr->offset[2]) alphaOffset = blockPtr->offset[2];
633 if (++alphaOffset < blockPtr->pixelSize) {
634 alphaOffset -= blockPtr->offset[0];
635 } else {
636 alphaOffset = 0;
637 }
638
639
640 /* open the output file (if needed) */
641 if (fileName) {
642 chan = Tcl_OpenFileChannel(interp, (char *) fileName, "w", 0644);
643 if (!chan) {
644 return TCL_ERROR;
645 }
646 }
647
648 /* compute image name */
649 if (chan) {
650 p = strrchr(fileName, '/');
651 if (p) {
652 fileName = p+1;
653 }
654 p = strrchr(fileName, '\\');
655 if (p) {
656 fileName = p+1;
657 }
658 p = strrchr(fileName, ':');
659 if (p) {
660 fileName = p+1;
661 }
662 p = strchr(fileName, '.');
663 if (p) {
664 *(char *)p = 0;
665 }
666 } else {
667 fileName = "unknown";
668 }
669
670 sprintf(buffer, header, fileName, blockPtr->width, fileName,
671 blockPtr->height, fileName);
672 if (p) {
673 *p = '.';
674 }
675 WRITE(buffer);
676
677 /* write image itself */
678 pp = blockPtr->pixelPtr + blockPtr->offset[0];
679 sep = ' ';
680 for (y = 0; y < blockPtr->height; y++) {
681 value = 0;
682 mask = 1;
683 for (x = 0; x < blockPtr->width; x++) {
684 if (!alphaOffset || pp[alphaOffset]) {
685 value |= mask;
686 } else {
687 /* make transparent pixel */
688 }
689 pp += blockPtr->pixelSize;
690 mask <<= 1;
691 if (mask >= 256)
692 {
693 sprintf(buffer,"%c 0x%02x",sep,value);
694 WRITE(buffer);
695 value = 0;
696 mask = 1;
697 sep = ',';
698 }
699 }
700 if (mask != 1) {
701 sprintf(buffer,"%c 0x%02x",sep, value);
702 WRITE(buffer);
703 }
704
705 if (y == blockPtr->height - 1) {
706 WRITE("};\n");
707 } else {
708 WRITE(",\n");
709 sep = ' ';
710 }
711 }
712
713 /* close the channel */
714 if (chan) {
715 Tcl_Close(interp, chan);
716 }
717 return TCL_OK;
718 }