Debian Codegraph @

pTk/mTk/additions/imgInit.c

raw
1 /*
2 * imgInit.tcl
3 */
4
5 #if defined(__WIN32__)
6 # define WIN32_LEAN_AND_MEAN
7 # include <windows.h>
8 # undef WIN32_LEAN_AND_MEAN
9 # if defined(_MSC_VER)
10 # define EXPORT(a,b) __declspec(dllexport) a b
11 # else
12 # if defined(__BORLANDC__)
13 # define EXPORT(a,b) a _export b
14 # else
15 # define EXPORT(a,b) a b
16 # endif
17 # endif
18 #else
19 # define EXPORT(a,b) a b
20 #endif
21
22
23 #include "tk.h"
24 #include "tkVMacro.h"
25 #include "imgInt.h"
26 #include <string.h>
27 #include <stdlib.h>
28
29
30 #ifndef USE_TCL_STUBS
31 #undef Tcl_InitStubs
32 #define Tcl_InitStubs(a,b,c) Tcl_PkgRequire(a,"Tcl","8.0",1)
33 #endif
34
35 #ifndef USE_TK_STUBS
36 #undef Tk_InitStubs
37 #define Tk_InitStubs(a,b,c) Tcl_PkgRequire(a,"Tk","8.0",1)
38 #endif
39
40 /*
41 * Declarations for functions defined in this file.
42 */
43
44 static int char64 _ANSI_ARGS_((int c));
45
46 #ifdef ALLOW_B64
47 static int tob64 _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
48 int argc, Tcl_Obj *CONST objv[]));
49 static int fromb64 _ANSI_ARGS_((ClientData clientData, Tcl_Interp *interp,
50 int argc, Tcl_Obj *CONST objv[]));
51 #endif
52
53 #ifndef _LANG
54
55 extern Tk_ImageType imgPixmapImageType;
56
57 extern Tk_PhotoImageFormat imgFmtBMP;
58 extern Tk_PhotoImageFormat imgFmtGIF;
59 extern Tk_PhotoImageFormat imgFmtJPEG;
60 extern Tk_PhotoImageFormat imgFmtPNG;
61 extern Tk_PhotoImageFormat imgFmtPS;
62 extern Tk_PhotoImageFormat imgFmtPDF;
63 extern Tk_PhotoImageFormat imgFmtRAS;
64 extern Tk_PhotoImageFormat imgFmtRAW;
65 extern Tk_PhotoImageFormat imgFmtRGB;
66 extern Tk_PhotoImageFormat imgFmtTIFF;
67 extern Tk_PhotoImageFormat imgFmtXBM;
68 extern Tk_PhotoImageFormat imgFmtXPM;
69 extern Tk_PhotoImageFormat imgFmtWin;
70
71 static Tk_PhotoImageFormat *Formats[] = {
72 &imgFmtTIFF,
73 &imgFmtPS,
74 &imgFmtPDF,
75 &imgFmtXBM,
76 &imgFmtXPM,
77 &imgFmtBMP,
78 &imgFmtJPEG,
79 &imgFmtPNG,
80 &imgFmtGIF,
81 &imgFmtWin,
82 (Tk_PhotoImageFormat *) NULL};
83
84 /*
85 * The variable "initialized" contains flags indicating which
86 * version of Tcl or Perl we are running:
87 *
88 * IMG_TCL Tcl
89 * IMG_OBJS using Tcl_Obj's in stead of char* (Tk 8.3 or higher)
90 * IMG_PERL perl
91 *
92 * These flags will be determined at runtime (except the IMG_PERL
93 * flag, for now), so we can use the same dynamic library for all
94 * Tcl/Tk versions (and for Perl/Tk in the future).
95 */
96
97 static int initialized = 0;
98
99 /*
100 *--------------------------------------------------------------
101 *
102 * Img_Init , Img_SafeInit, Img_InitStandAlone --
103 * Create Img commands.
104 *
105 * Results:
106 * None
107 *
108 * Side effects:
109 * None
110 *
111 *--------------------------------------------------------------
112 */
113
114 EXPORT(int,Img_Init)(interp)
115 Tcl_Interp *interp;
116 {
117 Tk_PhotoImageFormat **formatPtr = Formats;
118 char *version;
119
120 if ((version = Tcl_InitStubs(interp, "8.0", 0)) == NULL) {
121 return TCL_ERROR;
122 }
123 if (Tk_InitStubs(interp, "8.0", 0) == NULL) {
124 return TCL_ERROR;
125 }
126
127 if (!initialized) {
128 if (!(initialized = ImgObjInit(interp))) {
129 return TCL_ERROR;
130 }
131 while(*formatPtr) {
132 Tk_CreatePhotoImageFormat(*formatPtr++);
133 }
134 #ifdef __WIN32__
135 CreateMutex(NULL, FALSE, "ImgDllMutex");
136 #endif
137
138 #ifndef TCL_MAC
139 Tk_CreateImageType(&imgPixmapImageType);
140 #endif
141 }
142 #ifdef ALLOW_B64 /* Undocumented feature */
143 Tcl_CreateObjCommand(interp,"img_to_base64", tob64, (ClientData) NULL, NULL);
144 Tcl_CreateObjCommand(interp,"img_from_base64", fromb64, (ClientData) NULL, NULL);
145 #endif
146 return Tcl_PkgProvide(interp,"Img", IMG_PATCH_LEVEL);
147 }
148
149 EXPORT(int,Img_SafeInit)(interp)
150 Tcl_Interp *interp;
151 {
152 return Img_Init(interp);
153 }
154
155 #endif /* _LANG */
156
157 /*
158 *--------------------------------------------------------------------------
159 * char64 --
160 *
161 * This procedure converts a base64 ascii character into its binary
162 * equivalent. This code is a slightly modified version of the
163 * char64 proc in N. Borenstein's metamail decoder.
164 *
165 * Results:
166 * The binary value, or an error code.
167 *
168 * Side effects:
169 * None.
170 *--------------------------------------------------------------------------
171 */
172
173 static int
174 char64(c)
175 int c;
176 {
177 switch(c) {
178 case 'A': return 0; case 'B': return 1; case 'C': return 2;
179 case 'D': return 3; case 'E': return 4; case 'F': return 5;
180 case 'G': return 6; case 'H': return 7; case 'I': return 8;
181 case 'J': return 9; case 'K': return 10; case 'L': return 11;
182 case 'M': return 12; case 'N': return 13; case 'O': return 14;
183 case 'P': return 15; case 'Q': return 16; case 'R': return 17;
184 case 'S': return 18; case 'T': return 19; case 'U': return 20;
185 case 'V': return 21; case 'W': return 22; case 'X': return 23;
186 case 'Y': return 24; case 'Z': return 25; case 'a': return 26;
187 case 'b': return 27; case 'c': return 28; case 'd': return 29;
188 case 'e': return 30; case 'f': return 31; case 'g': return 32;
189 case 'h': return 33; case 'i': return 34; case 'j': return 35;
190 case 'k': return 36; case 'l': return 37; case 'm': return 38;
191 case 'n': return 39; case 'o': return 40; case 'p': return 41;
192 case 'q': return 42; case 'r': return 43; case 's': return 44;
193 case 't': return 45; case 'u': return 46; case 'v': return 47;
194 case 'w': return 48; case 'x': return 49; case 'y': return 50;
195 case 'z': return 51; case '0': return 52; case '1': return 53;
196 case '2': return 54; case '3': return 55; case '4': return 56;
197 case '5': return 57; case '6': return 58; case '7': return 59;
198 case '8': return 60; case '9': return 61; case '+': return 62;
199 case '/': return 63;
200
201 case ' ': case '\t': case '\n': case '\r': case '\f': return IMG_SPACE;
202 case '=': return IMG_PAD;
203 case '\0': return IMG_DONE;
204 default: return IMG_BAD;
205 }
206 }
207
208 /*
209 *--------------------------------------------------------------------------
210 * ImgRead --
211 *
212 * This procedure returns a buffer from the stream input. This stream
213 * could be anything from a base-64 encoded string to a Channel.
214 *
215 * Results:
216 * The number of characters successfully read from the input
217 *
218 * Side effects:
219 * The MFile state could change.
220 *--------------------------------------------------------------------------
221 */
222
223 int
224 ImgRead(handle, dst, count)
225 MFile *handle; /* mmdecode "file" handle */
226 char *dst; /* where to put the result */
227 int count; /* number of bytes */
228 {
229 register int i, c;
230 switch (handle->state) {
231 case IMG_STRING:
232 if (count > handle->length) {
233 count = handle->length;
234 }
235 if (count) {
236 memcpy(dst, handle->data, count);
237 handle->length -= count;
238 handle->data += count;
239 }
240 return count;
241 case IMG_CHAN:
242 return Tcl_Read((Tcl_Channel) handle->data, dst, count);
243 }
244
245 for(i=0; i<count && (c=ImgGetc(handle)) != IMG_DONE; i++) {
246 *dst++ = c;
247 }
248 return i;
249 }
250 /*
251 *--------------------------------------------------------------------------
252 *
253 * ImgGetc --
254 *
255 * This procedure returns the next input byte from a stream. This stream
256 * could be anything from a base-64 encoded string to a Channel.
257 *
258 * Results:
259 * The next byte (or IMG_DONE) is returned.
260 *
261 * Side effects:
262 * The MFile state could change.
263 *
264 *--------------------------------------------------------------------------
265 */
266
267 int
268 ImgGetc(handle)
269 MFile *handle; /* Input stream handle */
270 {
271 int c;
272 int result = 0; /* Initialization needed only to prevent
273 * gcc compiler warning */
274 if (handle->state == IMG_DONE) {
275 return IMG_DONE;
276 }
277
278 if (handle->state == IMG_STRING) {
279 if (!handle->length--) {
280 handle->state = IMG_DONE;
281 return IMG_DONE;
282 }
283 return *handle->data++;
284 }
285
286 do {
287 if (!handle->length--) {
288 handle->state = IMG_DONE;
289 return IMG_DONE;
290 }
291 c = char64(*handle->data++);
292 } while (c == IMG_SPACE);
293
294 if (c > IMG_SPECIAL) {
295 handle->state = IMG_DONE;
296 return IMG_DONE;
297 }
298
299 switch (handle->state++) {
300 case 0:
301 handle->c = c<<2;
302 result = ImgGetc(handle);
303 break;
304 case 1:
305 result = handle->c | (c>>4);
306 handle->c = (c&0xF)<<4;
307 break;
308 case 2:
309 result = handle->c | (c>>2);
310 handle->c = (c&0x3)<<6;
311 break;
312 case 3:
313 result = handle->c | c;
314 handle->state = 0;
315 break;
316 }
317 return result;
318 }
319
320 /*
321 *-----------------------------------------------------------------------
322 * ImgWrite --
323 *
324 * This procedure is invoked to put imaged data into a stream
325 * using ImgPutc.
326 *
327 * Results:
328 * The return value is the number of characters "written"
329 *
330 * Side effects:
331 * The base64 handle will change state.
332 *
333 *-----------------------------------------------------------------------
334 */
335
336 int
337 ImgWrite(handle, src, count)
338 MFile *handle; /* mmencode "file" handle */
339 CONST char *src; /* where to get the data */
340 int count; /* number of bytes */
341 {
342 register int i;
343 int curcount, bufcount;
344
345 if (handle->state == IMG_CHAN) {
346 return Tcl_Write((Tcl_Channel) handle->data, (char *) src, count);
347 }
348 curcount = handle->data - Tcl_DStringValue(handle->buffer);
349 bufcount = curcount + count + count/3 + count/52 + 1024;
350
351 /* make sure that the DString contains enough space */
352 if (bufcount >= (Tcl_DStringLength(handle->buffer))) {
353 Tcl_DStringSetLength(handle->buffer, bufcount + 4096);
354 handle->data = Tcl_DStringValue(handle->buffer) + curcount;
355 }
356 /* write the data */
357 for (i=0; (i<count) && (ImgPutc(*src++, handle) != IMG_DONE); i++) {
358 /* empty loop body */
359 }
360 return i;
361 }
362 /*
363 *-----------------------------------------------------------------------
364 *
365 * ImgPutc --
366 *
367 * This procedure encodes and writes the next byte to a base64
368 * encoded string.
369 *
370 * Results:
371 * The written byte is returned.
372 *
373 * Side effects:
374 * the base64 handle will change state.
375 *
376 *-----------------------------------------------------------------------
377 */
378
379 static char base64_table[64] = {
380 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
381 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
382 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
383 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
384 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
385 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
386 'w', 'x', 'y', 'z', '0', '1', '2', '3',
387 '4', '5', '6', '7', '8', '9', '+', '/'
388 };
389
390 int
391 ImgPutc(c, handle)
392 register int c; /* character to be written */
393 register MFile *handle; /* handle containing decoder data and state */
394 {
395 /* In fact, here should be checked first if the dynamic
396 * string contains enough space for the next character.
397 * This would be very expensive to do for each character.
398 * Therefore we just allocate 1024 bytes immediately in
399 * the beginning and also take a 1024 bytes margin inside
400 * every ImgWrite. At least this check is done then only
401 * every 256 bytes, which is much faster. Because the GIF
402 * header is less than 1024 bytes and pixel data is
403 * written in 256 byte portions, this should be safe.
404 */
405
406 if (c == IMG_DONE) {
407 switch(handle->state) {
408 case 0:
409 break;
410 case 1:
411 *handle->data++ = base64_table[(handle->c<<4)&63];
412 *handle->data++ = '='; *handle->data++ = '='; break;
413 case 2:
414 *handle->data++ = base64_table[(handle->c<<2)&63];
415 *handle->data++ = '='; break;
416 default:
417 handle->state = IMG_DONE;
418 return IMG_DONE;
419 }
420 Tcl_DStringSetLength(handle->buffer,
421 (handle->data) - Tcl_DStringValue(handle->buffer));
422 handle->state = IMG_DONE;
423 return IMG_DONE;
424 }
425
426 if (handle->state == IMG_CHAN) {
427 char ch = (char) c;
428 return (Tcl_Write((Tcl_Channel) handle->data, &ch, 1)>0) ? c : IMG_DONE;
429 }
430
431 c &= 0xff;
432 switch (handle->state++) {
433 case 0:
434 *handle->data++ = base64_table[(c>>2)&63]; break;
435 case 1:
436 c |= handle->c << 8;
437 *handle->data++ = base64_table[(c>>4)&63]; break;
438 case 2:
439 handle->state = 0;
440 c |= handle->c << 8;
441 *handle->data++ = base64_table[(c>>6)&63];
442 *handle->data++ = base64_table[c&63]; break;
443 }
444 handle->c = c;
445 if (handle->length++ > 52) {
446 handle->length = 0;
447 *handle->data++ = '\n';
448 }
449 return c & 0xff;
450 };
451
452 /*
453 *-------------------------------------------------------------------------
454 * ImgWriteInit --
455 * This procedure initializes a base64 decoder handle for writing
456 *
457 * Results:
458 * none
459 *
460 * Side effects:
461 * the base64 handle is initialized
462 *
463 *-------------------------------------------------------------------------
464 */
465
466 void
467 ImgWriteInit(buffer, handle)
468 Tcl_DString *buffer;
469 MFile *handle; /* mmencode "file" handle */
470 {
471 Tcl_DStringSetLength(buffer, 200);
472 handle->buffer = buffer;
473 handle->data = Tcl_DStringValue(buffer);
474 handle->state = 0;
475 handle->length = 0;
476 }
477
478 /*
479 *-------------------------------------------------------------------------
480 * ImgReadInit --
481 * This procedure initializes a base64 decoder handle for reading.
482 *
483 * Results:
484 * none
485 *
486 * Side effects:
487 * the base64 handle is initialized
488 *
489 *-------------------------------------------------------------------------
490 */
491
492
493 int
494 ImgReadInit(data, c, handle)
495 Tcl_Obj *data; /* string containing initial mmencoded data */
496 int c;
497 MFile *handle; /* mmdecode "file" handle */
498 {
499 handle->data = ImgGetByteArrayFromObj(data, &handle->length);
500 if (*handle->data == c) {
501 handle->state = IMG_STRING;
502 return 1;
503 }
504 c = base64_table[(c>>2)&63];
505
506 while((handle->length) && (char64(*handle->data) == IMG_SPACE)) {
507 handle->data++;
508 handle->length--;
509 }
510 if (c != *handle->data) {
511 handle->state = IMG_DONE;
512 return 0;
513 }
514 handle->state = 0;
515 return 1;
516 }
517
518 /*
519 *-------------------------------------------------------------------------
520 * tob64 --
521 * This function converts the contents of a file into a base-64
522 * encoded string.
523 *
524 * Results:
525 * none
526 *
527 * Side effects:
528 * none
529 *
530 *-------------------------------------------------------------------------
531 */
532
533 #ifdef ALLOW_B64
534 int tob64(clientData, interp, argc, objv)
535 ClientData clientData;
536 Tcl_Interp *interp;
537 int argc;
538 Tcl_Obj *CONST objv[];
539 {
540 Tcl_DString dstring;
541 MFile handle;
542 Tcl_Channel chan;
543 char buffer[1024];
544 int len;
545
546 if (argc != 2) {
547 Tcl_WrongNumArgs(interp, 1, objv, "filename");
548 return TCL_ERROR;
549 }
550
551 chan = ImgOpenFileChannel(interp, Tcl_GetStringFromObj(objv[1], &len), 0);
552 if (!chan) {
553 return TCL_ERROR;
554 }
555
556 Tcl_DStringInit(&dstring);
557 ImgWriteInit(&dstring, &handle);
558
559 while ((len = Tcl_Read(chan, buffer, 1024)) == 1024) {
560 ImgWrite(&handle, buffer, 1024);
561 }
562 if (len > 0) {
563 ImgWrite(&handle, buffer, len);
564 }
565 if ((Tcl_Close(interp, chan) == TCL_ERROR) || (len < 0)) {
566 Tcl_DStringFree(&dstring);
567 Tcl_AppendResult(interp, Tcl_GetStringFromObj(objv[1], &len),
568 ": ", Tcl_PosixError(interp), (char *)NULL);
569 return TCL_ERROR;
570 }
571 ImgPutc(IMG_DONE, &handle);
572
573 Tcl_DStringResult(interp, &dstring);
574 return TCL_OK;
575 }
576
577 /*
578 *-------------------------------------------------------------------------
579 * fromb64 --
580 * This function converts a base-64 encoded string into binary form,
581 * which is written to a file.
582 *
583 * Results:
584 * none
585 *
586 * Side effects:
587 * none
588 *
589 *-------------------------------------------------------------------------
590 */
591
592 int fromb64(clientData, interp, argc, objv)
593 ClientData clientData;
594 Tcl_Interp *interp;
595 int argc;
596 Tcl_Obj *CONST objv[];
597 {
598 MFile handle;
599 Tcl_Channel chan;
600 char buffer[1024];
601 int len;
602
603 if (argc != 3) {
604 Tcl_WrongNumArgs(interp, 1, objv, "filename data");
605 return TCL_ERROR;
606 }
607
608 chan = ImgOpenFileChannel(interp, Tcl_GetStringFromObj(objv[1], &len), 0644);
609 if (!chan) {
610 return TCL_ERROR;
611 }
612
613 handle.data = Tcl_GetStringFromObj(objv[2], &handle.length);
614 handle.state = 0;
615
616 while ((len = ImgRead(&handle, buffer, 1024)) == 1024) {
617 if (Tcl_Write(chan, buffer, 1024) != 1024) {
618 goto writeerror;
619 }
620 }
621 if (len > 0) {
622 if (Tcl_Write(chan, buffer, len) != len) {
623 goto writeerror;
624 }
625 }
626 if (Tcl_Close(interp, chan) == TCL_ERROR) {
627 return TCL_ERROR;
628 }
629 return TCL_OK;
630
631 writeerror:
632 Tcl_AppendResult(interp, Tcl_GetStringFromObj(objv[1], &len), ": ",
633 Tcl_PosixError(interp), (char *)NULL);
634 return TCL_ERROR;
635 }
636 #endif