Debian Codegraph @

pTk/mTk/additions/imgTIFF.c

raw
1 /*
2 * imgTIFF.c --
3 *
4 * A photo image file handler for TIFF files.
5 *
6 * Uses the libtiff.so library, which is dynamically
7 * loaded only when used.
8 *
9 */
10
11 /* Author : Jan Nijtmans */
12 /* Date : 7/16/97 */
13
14 #include "imgInt.h"
15 #include <string.h>
16 #include <stdlib.h>
17 #if defined(__STDC__) || defined(HAS_STDARG)
18 #include <stdarg.h>
19 #else
20 #include <varargs.h>
21 #endif
22
23 extern int unlink _ANSI_ARGS_((CONST char *));
24
25 #ifdef __WIN32__
26 #define WIN32_LEAN_AND_MEAN
27 #include <windows.h>
28 #define WIN32_LEAN_AND_MEAN
29 #endif
30
31 #ifdef MAC_TCL
32 #include "libtiff:tiffio.h"
33 #else
34 #ifdef HAVE_TIFF_H
35 # include <tiffio.h>
36 #else
37 # include "libtiff/tiffio.h"
38 #endif
39 #endif
40
41 #ifdef __WIN32__
42 #define TIFF_LIB_NAME "tiff.dll"
43 #endif
44
45 #ifndef TIFF_LIB_NAME
46 #define TIFF_LIB_NAME "libtiff.so"
47 #endif
48
49 /*
50 * Prototypes for local procedures defined in this file:
51 */
52
53 static int ChnMatchTIFF _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Channel chan,
54 CONST char *fileName, Tcl_Obj *format, int *widthPtr, int *heightPtr));
55 static int ObjMatchTIFF _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj,
56 Tcl_Obj *format, int *widthPtr, int *heightPtr));
57 static int ChnReadTIFF _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Channel chan,
58 CONST char *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle,
59 int destX, int destY, int width, int height, int srcX, int srcY));
60 static int ObjReadTIFF _ANSI_ARGS_((Tcl_Interp *interp,
61 Tcl_Obj *dataObj, Tcl_Obj *format,
62 Tk_PhotoHandle imageHandle, int destX, int destY,
63 int width, int height, int srcX, int srcY));
64 static int ChnWriteTIFF _ANSI_ARGS_((Tcl_Interp *interp, char *filename,
65 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
66 static int StringWriteTIFF _ANSI_ARGS_((Tcl_Interp *interp,
67 Tcl_DString *dataPtr, Tcl_Obj *format,
68 Tk_PhotoImageBlock *blockPtr));
69
70 Tk_PhotoImageFormat imgFmtTIFF = {
71 "tiff", /* name */
72 (Tk_ImageFileMatchProc *) ChnMatchTIFF, /* fileMatchProc */
73 (Tk_ImageStringMatchProc *) ObjMatchTIFF, /* stringMatchProc */
74 (Tk_ImageFileReadProc *) ChnReadTIFF, /* fileReadProc */
75 (Tk_ImageStringReadProc *) ObjReadTIFF, /* stringReadProc */
76 (Tk_ImageFileWriteProc *) ChnWriteTIFF, /* fileWriteProc */
77 (Tk_ImageStringWriteProc *) StringWriteTIFF,/* stringWriteProc */
78 };
79
80 static struct TiffFunctions {
81 VOID *handle;
82 void (* Close) _ANSI_ARGS_((TIFF *));
83 int (* GetField) _ANSI_ARGS_(TCL_VARARGS(TIFF *, tif));
84 int (* GetFieldDefaulted) _ANSI_ARGS_(TCL_VARARGS(TIFF *,tif));
85 TIFF* (* Open) _ANSI_ARGS_((CONST char*, CONST char*));
86 int (* ReadEncodedStrip) _ANSI_ARGS_((TIFF*, tstrip_t, tdata_t, tsize_t));
87 int (* ReadRGBAImage) _ANSI_ARGS_((TIFF *, uint32, uint32, uint32*, int));
88 int (* ReadTile) _ANSI_ARGS_((TIFF *, uint32, uint32, uint32*, int));
89 int (* SetField) _ANSI_ARGS_(TCL_VARARGS(TIFF *, tif));
90 tsize_t (* TileSize) _ANSI_ARGS_((TIFF*));
91 int (* WriteEncodedStrip) _ANSI_ARGS_((TIFF*, tstrip_t, tdata_t, tsize_t));
92 void (* free) _ANSI_ARGS_((tdata_t));
93 tdata_t (* malloc) _ANSI_ARGS_((tsize_t));
94 tdata_t (* memcpy) _ANSI_ARGS_((tdata_t, tdata_t, tsize_t));
95 tdata_t (* realloc) _ANSI_ARGS_((tdata_t, tsize_t));
96 TIFFErrorHandler (* SetErrorHandler) _ANSI_ARGS_((TIFFErrorHandler));
97 TIFFErrorHandler (* SetWarningHandler) _ANSI_ARGS_((TIFFErrorHandler));
98 TIFF* (* ClientOpen) _ANSI_ARGS_((CONST char*, CONST char*, VOID *,
99 TIFFReadWriteProc, TIFFReadWriteProc, TIFFSeekProc,
100 TIFFCloseProc, TIFFSizeProc, TIFFMapFileProc, TIFFUnmapFileProc));
101 TIFFCodec* (*RegisterCODEC) _ANSI_ARGS_((uint16, CONST char*, VOID *));
102 void (* Error) _ANSI_ARGS_(TCL_VARARGS(CONST char *, arg1));
103 int (* PredictorInit) _ANSI_ARGS_((TIFF *));
104 void (* MergeFieldInfo) _ANSI_ARGS_((TIFF *, CONST VOID *, int));
105 int (* FlushData1) _ANSI_ARGS_((TIFF *));
106 void (* NoPostDecode) _ANSI_ARGS_((TIFF *, VOID*, tsize_t));
107 tsize_t (* TileRowSize) _ANSI_ARGS_((TIFF *));
108 tsize_t (* ScanlineSize) _ANSI_ARGS_((TIFF *));
109 void (* setByteArray) _ANSI_ARGS_((VOID **, VOID*, long));
110 int (* VSetField) _ANSI_ARGS_((TIFF *, ttag_t, va_list));
111 void (* SwabArrayOfShort) _ANSI_ARGS_((uint16*, unsigned long));
112 } tiff = {0};
113
114 static char *symbols[] = {
115 "TIFFClose",
116 "TIFFGetField",
117 "TIFFGetFieldDefaulted",
118 "TIFFOpen",
119 "TIFFReadEncodedStrip",
120 "TIFFReadRGBAImage",
121 "TIFFReadTile",
122 "TIFFSetField",
123 "TIFFTileSize",
124 "TIFFWriteEncodedStrip",
125 /* The following symbols are not crucial. If they cannot be
126 found, just don't use them. The ClientOpen function is
127 more difficult to emulate, but even that is possible. */
128 "_TIFFfree",
129 "_TIFFmalloc",
130 "_TIFFmemcpy",
131 "_TIFFrealloc",
132 "TIFFSetErrorHandler",
133 "TIFFSetWarningHandler",
134 "TIFFClientOpen",
135 "TIFFRegisterCODEC", /* not in libtiff.def */
136 "TIFFError",
137 "TIFFPredictorInit", /* not in libtiff.def */
138 "_TIFFMergeFieldInfo", /* not in libtiff.def */
139 "TIFFFlushData1", /* not in libtiff.def */
140 "_TIFFNoPostDecode", /* not in libtiff.def */
141 "TIFFTileRowSize",
142 "TIFFScanlineSize",
143 "_TIFFsetByteArray", /* not in libtiff.def */
144 "TIFFVSetField",
145 "TIFFSwabArrayOfShort",
146 (char *) NULL
147 };
148
149 /*
150 * Prototypes for local procedures defined in this file:
151 */
152
153 static int getint _ANSI_ARGS_((unsigned char *buf, TIFFDataType format,
154 int order));
155 static int CommonMatchTIFF _ANSI_ARGS_((MFile *handle, int *widhtPtr,
156 int *heightPtr));
157 static int CommonReadTIFF _ANSI_ARGS_((Tcl_Interp *interp, TIFF *tif,
158 Tcl_Obj *format, Tk_PhotoHandle imageHandle, int destX, int destY,
159 int width, int height, int srcX, int srcY));
160 static int CommonWriteTIFF _ANSI_ARGS_((Tcl_Interp *interp, TIFF *tif,
161 int comp, Tk_PhotoImageBlock *blockPtr));
162 static int ParseWriteFormat _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *format,
163 int *comp, char **mode));
164 static int load_tiff_library _ANSI_ARGS_((Tcl_Interp *interp));
165 static void _TIFFerr _ANSI_ARGS_((CONST char *, CONST char *, va_list));
166 static void _TIFFwarn _ANSI_ARGS_((CONST char *, CONST char *, va_list));
167 void ImgTIFFfree _ANSI_ARGS_((tdata_t data));
168 tdata_t ImgTIFFmalloc _ANSI_ARGS_((tsize_t size));
169 tdata_t ImgTIFFrealloc _ANSI_ARGS_((tdata_t data, tsize_t size));
170 tdata_t ImgTIFFmemcpy _ANSI_ARGS_((tdata_t, tdata_t, tsize_t));
171 void ImgTIFFError _ANSI_ARGS_(TCL_VARARGS(CONST char *, module));
172 int ImgTIFFPredictorInit _ANSI_ARGS_((TIFF *tif));
173 void ImgTIFFMergeFieldInfo _ANSI_ARGS_((TIFF* tif, CONST VOID *voidp, int i));
174 int ImgTIFFFlushData1 _ANSI_ARGS_((TIFF *tif));
175 void ImgTIFFNoPostDecode _ANSI_ARGS_((TIFF *, VOID *, tsize_t));
176 tsize_t ImgTIFFTileRowSize _ANSI_ARGS_((TIFF *));
177 tsize_t ImgTIFFScanlineSize _ANSI_ARGS_((TIFF *));
178 void ImgTIFFsetByteArray _ANSI_ARGS_((VOID **, VOID*, long));
179 int ImgTIFFSetField _ANSI_ARGS_(TCL_VARARGS(TIFF *, tif));
180 tsize_t ImgTIFFTileSize _ANSI_ARGS_((TIFF*));
181 void ImgTIFFSwabArrayOfShort _ANSI_ARGS_((uint16*, unsigned long));
182
183 /*
184 * External hooks to functions, so they can be called from
185 * imgTIFFzip.c and imgTIFFjpeg.c as well.
186 */
187
188 void ImgTIFFfree (data)
189 tdata_t data;
190 {
191 if (tiff.free) {
192 tiff.free(data);
193 } else {
194 ckfree((char *) data);
195 }
196 }
197
198 tdata_t ImgTIFFmalloc(size)
199 tsize_t size;
200 {
201 if (tiff.malloc) {
202 return tiff.malloc(size);
203 } else {
204 return ckalloc(size);
205 }
206 }
207
208 tdata_t ImgTIFFrealloc(data, size)
209 tdata_t data;
210 tsize_t size;
211 {
212 if (tiff.realloc) {
213 return tiff.realloc(data, size);
214 } else {
215 return ckrealloc(data, size);
216 }
217 }
218
219 tdata_t
220 ImgTIFFmemcpy(a,b,c)
221 tdata_t a;
222 tdata_t b;
223 tsize_t c;
224 {
225 return tiff.memcpy(a,b,c);
226 }
227
228 void
229 ImgTIFFError TCL_VARARGS_DEF(CONST char *, arg1)
230 {
231 va_list ap;
232 CONST char* module;
233 CONST char* fmt;
234
235 module = (CONST char*) TCL_VARARGS_START(CONST char *, arg1, ap);
236 fmt = va_arg(ap, CONST char *);
237 _TIFFerr(module, fmt, ap);
238 va_end(ap);
239 }
240
241 int
242 ImgTIFFPredictorInit(tif)
243 TIFF *tif;
244 {
245 return tiff.PredictorInit(tif);
246 }
247
248 void
249 ImgTIFFMergeFieldInfo(tif, voidp, i)
250 TIFF* tif;
251 CONST VOID *voidp;
252 int i;
253 {
254 tiff.MergeFieldInfo(tif, voidp, i);
255 }
256
257 int
258 ImgTIFFFlushData1(tif)
259 TIFF *tif;
260 {
261 return tiff.FlushData1(tif);
262 }
263
264 void
265 ImgTIFFNoPostDecode(tif,a,b)
266 TIFF * tif;
267 VOID *a;
268 tsize_t b;
269 {
270 tiff.NoPostDecode(tif, a, b);
271 }
272
273 tsize_t
274 ImgTIFFTileRowSize(tif)
275 TIFF * tif;
276 {
277 return tiff.TileRowSize(tif);
278 }
279
280 tsize_t
281 ImgTIFFScanlineSize(tif)
282 TIFF *tif;
283 {
284 return tiff.ScanlineSize(tif);
285 }
286
287 void
288 ImgTIFFsetByteArray(a,b,c)
289 VOID **a;
290 VOID *b;
291 long c;
292 {
293 tiff.setByteArray(a,b,c);
294 }
295
296 int
297 ImgTIFFSetField TCL_VARARGS_DEF(TIFF*, arg1)
298 {
299 va_list ap;
300 TIFF* tif;
301 ttag_t tag;
302 int result;
303
304 tif = (TIFF*) TCL_VARARGS_START(TIFF*, arg1, ap);
305 tag = va_arg(ap, ttag_t);
306 result = tiff.VSetField(tif, tag, ap);
307 va_end(ap);
308 return result;
309 }
310
311 tsize_t
312 ImgTIFFTileSize(tif)
313 TIFF* tif;
314 {
315 return tiff.TileSize(tif);
316 }
317
318 void
319 ImgTIFFSwabArrayOfShort(p, l)
320 uint16* p;
321 unsigned long l;
322 {
323 tiff.SwabArrayOfShort(p,l);
324 return;
325 }
326
327 /*
328 * The functions for the TIFF input handler
329 */
330
331 static int mapDummy _ANSI_ARGS_((thandle_t, tdata_t *, toff_t *));
332 static void unMapDummy _ANSI_ARGS_((thandle_t, tdata_t, toff_t));
333 static int closeDummy _ANSI_ARGS_((thandle_t));
334 static tsize_t writeDummy _ANSI_ARGS_((thandle_t, tdata_t, tsize_t));
335
336 static tsize_t readMFile _ANSI_ARGS_((thandle_t, tdata_t, tsize_t));
337 static toff_t seekMFile _ANSI_ARGS_((thandle_t, toff_t, int));
338 static toff_t sizeMFile _ANSI_ARGS_((thandle_t));
339
340 static tsize_t readString _ANSI_ARGS_((thandle_t, tdata_t, tsize_t));
341 static tsize_t writeString _ANSI_ARGS_((thandle_t, tdata_t, tsize_t));
342 static toff_t seekString _ANSI_ARGS_((thandle_t, toff_t, int));
343 static toff_t sizeString _ANSI_ARGS_((thandle_t));
344
345 static char *errorMessage = NULL;
346
347 static int getint(buf, format, order)
348 unsigned char *buf;
349 TIFFDataType format;
350 int order;
351 {
352 int result;
353
354 switch (format) {
355 case TIFF_BYTE:
356 result = buf[0]; break;
357 case TIFF_SHORT:
358 result = (buf[order]<<8) + buf[1-order]; break;
359 case TIFF_LONG:
360 if (order) {
361 result = (buf[3]<<24) + (buf[2]<<16) + (buf[1]<<8) + buf[0];
362 } else {
363 result = (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + buf[3];
364 }; break;
365 default:
366 result = -1;
367 }
368 return result;
369 }
370
371 static int
372 load_tiff_library(interp)
373 Tcl_Interp *interp;
374 {
375 static int initialized = 0;
376 if (errorMessage) {
377 ckfree(errorMessage);
378 errorMessage = NULL;
379 }
380 if (ImgLoadLib(interp, TIFF_LIB_NAME, &tiff.handle, symbols, 10)
381 != TCL_OK) {
382 return TCL_ERROR;
383 }
384 if (tiff.SetErrorHandler != NULL) {
385 tiff.SetErrorHandler(_TIFFerr);
386 }
387 if (tiff.SetWarningHandler != NULL) {
388 tiff.SetWarningHandler(_TIFFwarn);
389 }
390 if (!initialized) {
391 initialized = 1;
392 if (tiff.RegisterCODEC && tiff.Error && tiff.PredictorInit &&
393 tiff.MergeFieldInfo && tiff.FlushData1 && tiff.NoPostDecode &&
394 tiff.TileRowSize && tiff.ScanlineSize && tiff.setByteArray &&
395 tiff.VSetField && tiff.SwabArrayOfShort) {
396 tiff.RegisterCODEC(COMPRESSION_DEFLATE, "Deflate", ImgInitTIFFzip);
397 tiff.RegisterCODEC(COMPRESSION_JPEG, "JPEG", ImgInitTIFFjpeg);
398 tiff.RegisterCODEC(COMPRESSION_PIXARLOG, "PixarLog", ImgInitTIFFpixar);
399 }
400 }
401 return TCL_OK;
402 }
403
404 static void _TIFFerr(module, fmt, ap)
405 CONST char *module;
406 CONST char *fmt;
407 va_list ap;
408 {
409 char buf[2048];
410 char *cp = buf;
411
412 if (module != NULL) {
413 sprintf(cp, "%s: ", module);
414 cp += strlen(module) + 2;
415 }
416
417 vsprintf(cp, fmt, ap);
418 if (errorMessage) {
419 ckfree(errorMessage);
420 }
421 errorMessage = (char *) ckalloc(strlen(buf)+1);
422 strcpy(errorMessage, buf);
423 }
424
425 /* warnings are not processed in Tcl */
426 static void _TIFFwarn(module, fmt, ap)
427 CONST char *module;
428 CONST char *fmt;
429 va_list ap;
430 {
431 }
432
433 static int
434 mapDummy(fd, base, size)
435 thandle_t fd;
436 tdata_t *base;
437 toff_t *size;
438 {
439 return (toff_t) 0;
440 }
441
442 static void
443 unMapDummy(fd, base, size)
444 thandle_t fd;
445 tdata_t base;
446 toff_t size;
447 {
448 }
449
450 static int
451 closeDummy(fd)
452 thandle_t fd;
453 {
454 return 0;
455 }
456
457 static tsize_t
458 writeDummy(fd, data, size)
459 thandle_t fd;
460 tdata_t data;
461 tsize_t size;
462 {
463 return size;
464 }
465
466 static tsize_t
467 readMFile(fd, data, size)
468 thandle_t fd;
469 tdata_t data;
470 tsize_t size;
471 {
472 return (tsize_t) ImgRead((MFile *) fd, (char *) data, (int) size) ;
473 }
474
475 static toff_t
476 seekMFile(fd, off, whence)
477 thandle_t fd;
478 toff_t off;
479 int whence;
480 {
481 return Tcl_Seek((Tcl_Channel) ((MFile *) fd)->data, (int) off, whence);
482 }
483
484 static toff_t
485 sizeMFile(fd)
486 thandle_t fd;
487 {
488 int fsize;
489 return (fsize = Tcl_Seek((Tcl_Channel) ((MFile *) fd)->data,
490 (int) 0, SEEK_END)) < 0 ? 0 : (toff_t) fsize;
491 }
492
493 /*
494 * In the following functions "handle" is used differently for speed reasons:
495 *
496 * handle.buffer (writing only) dstring used for writing.
497 * handle.data pointer to first character
498 * handle.lenght size of data
499 * handle.state "file" position pointer.
500 *
501 * After a read, only the position pointer is adapted, not the other fields.
502 */
503
504 static tsize_t
505 readString(fd, data, size)
506 thandle_t fd;
507 tdata_t data;
508 tsize_t size;
509 {
510 register MFile *handle = (MFile *) fd;
511
512 if ((size + handle->state) > handle->length) {
513 size = handle->length - handle->state;
514 }
515 if (size) {
516 memcpy((char *) data, handle->data + handle->state, (size_t) size);
517 handle->state += size;
518 }
519 return size;
520 }
521
522 static tsize_t
523 writeString(fd, data, size)
524 thandle_t fd;
525 tdata_t data;
526 tsize_t size;
527 {
528 register MFile *handle = (MFile *) fd;
529
530 if (handle->state + size > handle->length) {
531 handle->length = handle->state + size;
532 Tcl_DStringSetLength(handle->buffer, handle->length);
533 handle->data = Tcl_DStringValue(handle->buffer);
534 }
535 memcpy(handle->data + handle->state, (char *) data, (size_t) size);
536 handle->state += size;
537 return size;
538 }
539
540 static toff_t
541 seekString(fd, off, whence)
542 thandle_t fd;
543 toff_t off;
544 int whence;
545 {
546 register MFile *handle = (MFile *) fd;
547
548 switch (whence) {
549 case SEEK_SET:
550 handle->state = (int) off;
551 break;
552 case SEEK_CUR:
553 handle->state += (int) off;
554 break;
555 case SEEK_END:
556 handle->state = handle->length + (int) off;
557 break;
558 }
559 if (handle->state < 0) {
560 handle->state = 0;
561 return -1;
562 }
563 return (toff_t) handle->state;
564 }
565
566 static toff_t
567 sizeString(fd)
568 thandle_t fd;
569 {
570 return ((MFile *) fd)->length;
571 }
572
573
574 /*
575 *----------------------------------------------------------------------
576 *
577 * ObjMatchTIFF --
578 *
579 * This procedure is invoked by the photo image type to see if
580 * a string contains image data in TIFF format.
581 *
582 * Results:
583 * The return value is 1 if the first characters in the string
584 * is like TIFF data, and 0 otherwise.
585 *
586 * Side effects:
587 * the size of the image is placed in widthPre and heightPtr.
588 *
589 *----------------------------------------------------------------------
590 */
591
592 static int
593 ObjMatchTIFF(interp, data, format, widthPtr, heightPtr)
594 Tcl_Interp *interp;
595 Tcl_Obj *data; /* the object containing the image data */
596 Tcl_Obj *format; /* the image format string */
597 int *widthPtr; /* where to put the string width */
598 int *heightPtr; /* where to put the string height */
599 {
600 MFile handle;
601
602 ImgFixObjMatchProc(&interp, &data, &format, &widthPtr, &heightPtr);
603
604 if (!ImgReadInit(data, '\111', &handle) &&
605 !ImgReadInit(data, '\115', &handle)) {
606 return 0;
607 }
608
609 return CommonMatchTIFF(&handle, widthPtr, heightPtr);
610 }
611
612 static int ChnMatchTIFF(interp, chan, fileName, format, widthPtr, heightPtr)
613 Tcl_Interp *interp;
614 Tcl_Channel chan;
615 CONST char *fileName;
616 Tcl_Obj *format;
617 int *widthPtr, *heightPtr;
618 {
619 MFile handle;
620
621 ImgFixChanMatchProc(&interp, &chan, &fileName, &format, &widthPtr, &heightPtr);
622
623 handle.data = (char *) chan;
624 handle.state = IMG_CHAN;
625
626 return CommonMatchTIFF(&handle, widthPtr, heightPtr);
627 }
628
629 static int CommonMatchTIFF(handle, widthPtr, heightPtr)
630 MFile *handle;
631 int *widthPtr, *heightPtr;
632 {
633 unsigned char buf[4096];
634 int i, j, order, w = 0, h = 0;
635
636 i = ImgRead(handle, (char *) buf, 8);
637 order = (buf[0] == '\111');
638 if ((i != 8) || (buf[0] != buf[1])
639 || ((buf[0] != '\111') && (buf[0] != '\115'))
640 || (getint(buf+2,TIFF_SHORT,order) != 42)) {
641 return 0;
642 }
643 i = getint(buf+4,TIFF_LONG,order);
644
645 while (i > 4104) {
646 i -= 4096;
647 ImgRead(handle, (char *) buf, 4096);
648 }
649 if (i>8) {
650 ImgRead(handle, (char *) buf, i-8);
651 }
652 ImgRead(handle, (char *) buf, 2);
653 i = getint(buf,TIFF_SHORT,order);
654 while (i--) {
655 ImgRead(handle, (char *) buf, 12);
656 if (buf[order]!=1) continue;
657 j = getint(buf+2,TIFF_SHORT,order);
658 j = getint(buf+8, (TIFFDataType) j, order);
659 if (buf[1-order]==0) {
660 w = j;
661 if (h>0) break;
662 } else if (buf[1-order]==1) {
663 h = j;
664 if (w>0) break;
665 }
666 }
667
668 if ((w <= 0) || (h <= 0)) {
669 return 0;
670 }
671 *widthPtr = w;
672 *heightPtr = h;
673 return 1;
674 }
675
676 static int ObjReadTIFF(interp, data, format, imageHandle,
677 destX, destY, width, height, srcX, srcY)
678 Tcl_Interp *interp;
679 Tcl_Obj *data; /* object containing the image */
680 Tcl_Obj *format;
681 Tk_PhotoHandle imageHandle;
682 int destX, destY;
683 int width, height;
684 int srcX, srcY;
685 {
686 TIFF *tif;
687 char tempFileName[256];
688 int count, result;
689 MFile handle;
690 char buffer[1024];
691 char *dataPtr = NULL;
692
693 if (load_tiff_library(interp) != TCL_OK) {
694 return TCL_ERROR;
695 }
696
697 if (!ImgReadInit(data, '\115', &handle)) {
698 ImgReadInit(data, '\111', &handle);
699 }
700
701 if (tiff.ClientOpen) {
702 tempFileName[0] = 0;
703 if (handle.state != IMG_STRING) {
704 dataPtr = ckalloc((handle.length*3)/4 + 2);
705 handle.length = ImgRead(&handle, dataPtr, handle.length);
706 handle.data = dataPtr;
707 }
708 handle.state = 0;
709 tif = tiff.ClientOpen("inline data", "r", (thandle_t) &handle,
710 readString, writeString, seekString, closeDummy,
711 sizeString, mapDummy, unMapDummy);
712 } else {
713 Tcl_Channel outchan;
714 tmpnam(tempFileName);
715 outchan = ImgOpenFileChannel(interp, tempFileName, 0644);
716 if (!outchan) {
717 return TCL_ERROR;
718 }
719
720 count = ImgRead(&handle, buffer, 1024);
721 while (count == 1024) {
722 Tcl_Write(outchan, buffer, count);
723 count = ImgRead(&handle, buffer, 1024);
724 }
725 if (count>0){
726 Tcl_Write(outchan, buffer, count);
727 }
728 if (Tcl_Close(interp, outchan) == TCL_ERROR) {
729 return TCL_ERROR;
730 }
731 tif = tiff.Open(tempFileName, "r");
732 }
733
734 if (tif != NULL) {
735 result = CommonReadTIFF(interp, tif, format, imageHandle,
736 destX, destY, width, height, srcX, srcY);
737 } else {
738 result = TCL_ERROR;
739 }
740 if (tempFileName[0]) {
741 unlink(tempFileName);
742 }
743 if (result == TCL_ERROR) {
744 Tcl_AppendResult(interp, errorMessage, (char *) NULL);
745 ckfree(errorMessage);
746 errorMessage = NULL;
747 }
748 if (dataPtr) {
749 ckfree(dataPtr);
750 }
751 return result;
752 }
753
754 static int ChnReadTIFF(interp, chan, fileName, format, imageHandle,
755 destX, destY, width, height, srcX, srcY)
756 Tcl_Interp *interp;
757 Tcl_Channel chan;
758 CONST char *fileName;
759 Tcl_Obj *format;
760 Tk_PhotoHandle imageHandle;
761 int destX, destY;
762 int width, height;
763 int srcX, srcY;
764 {
765 TIFF *tif;
766 char tempFileName[256];
767 int count, result;
768 char buffer[1024];
769
770 if (load_tiff_library(interp) != TCL_OK) {
771 return TCL_ERROR;
772 }
773
774 if (tiff.ClientOpen) {
775 MFile handle;
776 tempFileName[0] = 0;
777 handle.data = (char *) chan;
778 handle.state = IMG_CHAN;
779 tif = tiff.ClientOpen(fileName, "r", (thandle_t) &handle,
780 readMFile, writeDummy, seekMFile, closeDummy,
781 sizeMFile, mapDummy, unMapDummy);
782 } else {
783 Tcl_Channel outchan;
784 tmpnam(tempFileName);
785 outchan = ImgOpenFileChannel(interp, tempFileName, 0644);
786 if (!outchan) {
787 return TCL_ERROR;
788 }
789
790 count = Tcl_Read(chan, buffer, 1024);
791 while (count == 1024) {
792 Tcl_Write(outchan, buffer, count);
793 count = Tcl_Read(chan, buffer, 1024);
794 }
795 if (count>0){
796 Tcl_Write(outchan, buffer, count);
797 }
798 if (Tcl_Close(interp, outchan) == TCL_ERROR) {
799 return TCL_ERROR;
800 }
801
802 tif = tiff.Open(tempFileName, "r");
803 }
804 if (tif) {
805 result = CommonReadTIFF(interp, tif, format, imageHandle,
806 destX, destY, width, height, srcX, srcY);
807 } else {
808 result = TCL_ERROR;
809 }
810 if (tempFileName[0]) {
811 unlink(tempFileName);
812 }
813 if (result == TCL_ERROR) {
814 Tcl_AppendResult(interp, errorMessage, (char *) NULL);
815 ckfree(errorMessage);
816 errorMessage = 0;
817 }
818 return result;
819 }
820
821
822 typedef struct myblock {
823 Tk_PhotoImageBlock ck;
824 int dummy; /* extra space for offset[3], if not included already
825 in Tk_PhotoImageBlock */
826 } myblock;
827
828 #define block bl.ck
829
830 static int CommonReadTIFF(interp, tif, format, imageHandle,
831 destX, destY, width, height, srcX, srcY)
832 Tcl_Interp *interp;
833 TIFF *tif;
834 Tcl_Obj *format;
835 Tk_PhotoHandle imageHandle;
836 int destX, destY;
837 int width, height;
838 int srcX, srcY;
839 {
840 myblock bl;
841 unsigned char *pixelPtr = block.pixelPtr;
842 uint32 w, h;
843 size_t npixels;
844 uint32 *raster;
845
846 #ifdef WORDS_BIGENDIAN
847 block.offset[0] = 3;
848 block.offset[1] = 2;
849 block.offset[2] = 1;
850 block.offset[3] = 0;
851 #else
852 block.offset[0] = 0;
853 block.offset[1] = 1;
854 block.offset[2] = 2;
855 block.offset[3] = 3;
856 #endif
857 block.pixelSize = sizeof (uint32);
858
859 tiff.GetField(tif, TIFFTAG_IMAGEWIDTH, &w);
860 tiff.GetField(tif, TIFFTAG_IMAGELENGTH, &h);
861 npixels = w * h;
862 if (tiff.malloc == NULL) {
863 raster = (uint32 *) ckalloc(npixels * sizeof (uint32));
864 } else {
865 raster = (uint32 *) tiff.malloc(npixels * sizeof (uint32));
866 }
867 block.width = w;
868 block.height = h;
869 block.pitch = - (block.pixelSize * (int) w);
870 block.pixelPtr = ((unsigned char *) raster) + ((1-h) * block.pitch);
871 if (raster == NULL) {
872 printf("cannot malloc\n");
873 return TCL_ERROR;
874 }
875
876 if (!tiff.ReadRGBAImage(tif, w, h, raster, 0) || errorMessage) {
877 if (tiff.free == NULL) {
878 ckfree((char *)raster);
879 } else {
880 tiff.free((char *)raster);
881 }
882 if (errorMessage) {
883 Tcl_AppendResult(interp, errorMessage, (char *) NULL);
884 ckfree(errorMessage);
885 errorMessage = NULL;
886 }
887 return TCL_ERROR;
888 }
889
890 pixelPtr = block.pixelPtr += srcY * block.pitch
891 + srcX * block.pixelSize;
892 block.offset[3] = block.offset[0]; /* don't use transparency */
893 ImgPhotoPutBlock(imageHandle, &block, destX,
894 destY, width, height);
895
896 if (tiff.free == NULL) {
897 ckfree((char *)raster);
898 } else {
899 tiff.free((char *)raster);
900 }
901 tiff.Close(tif);
902 return TCL_OK;
903 }
904
905 static int StringWriteTIFF(interp, dataPtr, format, blockPtr)
906 Tcl_Interp *interp;
907 Tcl_DString *dataPtr;
908 Tcl_Obj *format;
909 Tk_PhotoImageBlock *blockPtr;
910 {
911 TIFF *tif;
912 int result, comp;
913 MFile handle;
914 char tempFileName[256];
915 Tcl_DString dstring;
916 char *mode;
917 Tcl_DString data;
918
919 if (load_tiff_library(interp) != TCL_OK) {
920 return TCL_ERROR;
921 }
922
923 ImgFixStringWriteProc(&data, &interp, &dataPtr, &format, &blockPtr);
924
925 if (ParseWriteFormat(interp, format, &comp, &mode) != TCL_OK) {
926 return TCL_ERROR;
927 }
928
929 if (tiff.ClientOpen) {
930 tempFileName[0] = 0;
931 Tcl_DStringInit(&dstring);
932 ImgWriteInit(&dstring, &handle);
933 tif = tiff.ClientOpen("inline data", mode, (thandle_t) &handle,
934 readString, writeString, seekString, closeDummy,
935 sizeString, mapDummy, unMapDummy);
936 } else {
937 tmpnam(tempFileName);
938 tif = tiff.Open(tempFileName,mode);
939 }
940
941 result = CommonWriteTIFF(interp, tif, comp, blockPtr);
942 tiff.Close(tif);
943
944 if (result != TCL_OK) {
945 if (tempFileName[0]) {
946 unlink(tempFileName);
947 }
948 Tcl_AppendResult(interp, errorMessage, (char *) NULL);
949 ckfree(errorMessage);
950 errorMessage = NULL;
951 return TCL_ERROR;
952 }
953
954 if (tempFileName[0]) {
955 Tcl_Channel inchan;
956 char buffer[1024];
957 inchan = ImgOpenFileChannel(interp, tempFileName, 0644);
958 if (!inchan) {
959 return TCL_ERROR;
960 }
961 ImgWriteInit(dataPtr, &handle);
962
963 result = Tcl_Read(inchan, buffer, 1024);
964 while ((result == TCL_OK) && !Tcl_Eof(inchan)) {
965 ImgWrite(&handle, buffer, result);
966 result = Tcl_Read(inchan, buffer, 1024);
967 }
968 if (result == TCL_OK) {
969 ImgWrite(&handle, buffer, result);
970 result = Tcl_Close(interp, inchan);
971 }
972 unlink(tempFileName);
973 } else {
974 int length = handle.length;
975 ImgWriteInit(dataPtr, &handle);
976 ImgWrite(&handle, Tcl_DStringValue(&dstring), length);
977 Tcl_DStringFree(&dstring);
978 }
979 ImgPutc(IMG_DONE, &handle);
980 if ((result == TCL_OK) && (dataPtr == &data)) {
981 Tcl_DStringResult(interp, dataPtr);
982 }
983 return result;
984 }
985
986 static int ChnWriteTIFF(interp, filename, format, blockPtr)
987 Tcl_Interp *interp;
988 char *filename;
989 Tcl_Obj *format;
990 Tk_PhotoImageBlock *blockPtr;
991 {
992 TIFF *tif;
993 int result, comp;
994 Tcl_DString nameBuffer;
995 char *fullname, *mode;
996
997 if (!(fullname=Tcl_TranslateFileName(interp, filename, &nameBuffer))) {
998 return TCL_ERROR;
999 }
1000
1001 if (load_tiff_library(interp) != TCL_OK) {
1002 Tcl_DStringFree(&nameBuffer);
1003 return TCL_ERROR;
1004 }
1005
1006 if (ParseWriteFormat(interp, format, &comp, &mode) != TCL_OK) {
1007 Tcl_DStringFree(&nameBuffer);
1008 return TCL_ERROR;
1009 }
1010
1011 if (!(tif = tiff.Open(fullname, mode))) {
1012 Tcl_AppendResult(interp, filename, ": ", Tcl_PosixError(interp),
1013 (char *)NULL);
1014 Tcl_DStringFree(&nameBuffer);
1015 return TCL_ERROR;
1016 }
1017
1018 Tcl_DStringFree(&nameBuffer);
1019
1020 result = CommonWriteTIFF(interp, tif, comp, blockPtr);
1021 tiff.Close(tif);
1022 return result;
1023 }
1024
1025 static int ParseWriteFormat(interp, format, comp, mode)
1026 Tcl_Interp *interp;
1027 Tcl_Obj *format;
1028 int *comp;
1029 char **mode;
1030 {
1031 static char *tiffWriteOptions[] = {"-compression", "-byteorder"};
1032 int objc, length, c, i, index;
1033 Tcl_Obj **objv;
1034 char *compression, *byteorder;
1035
1036 *comp = COMPRESSION_NONE;
1037 *mode = "w";
1038 if (ImgListObjGetElements(interp, format, &objc, &objv) != TCL_OK)
1039 return TCL_ERROR;
1040 if (objc) {
1041 compression = "none";
1042 byteorder = "";
1043 for (i=1; i<objc; i++) {
1044 if (Tcl_GetIndexFromObj(interp, objv[i], tiffWriteOptions,
1045 "format option", 0, &index)!=TCL_OK) {
1046 return TCL_ERROR;
1047 }
1048 if (++i >= objc) {
1049 Tcl_AppendResult(interp, "No value for option \"",
1050 Tcl_GetStringFromObj(objv[--i], (int *) NULL),
1051 "\"", (char *) NULL);
1052 return TCL_ERROR;
1053 }
1054 switch(index) {
1055 case 0:
1056 compression = Tcl_GetStringFromObj(objv[i], (int *) NULL); break;
1057 case 1:
1058 byteorder = Tcl_GetStringFromObj(objv[i], (int *) NULL); break;
1059 }
1060 }
1061 c = compression[0]; length = strlen(compression);
1062 if ((c == 'n') && (!strncmp(compression,"none",length))) {
1063 *comp = COMPRESSION_NONE;
1064 } else if ((c == 'd') && (!strncmp(compression,"deflate",length))) {
1065 *comp = COMPRESSION_DEFLATE;
1066 } else if ((c == 'j') && (!strncmp(compression,"jpeg",length))) {
1067 *comp = COMPRESSION_JPEG;
1068 } else if ((c == 'l') && (!strncmp(compression,"logluv",length))) {
1069 *comp = COMPRESSION_SGILOG;
1070 /* disabled, because of patented lzw-algorithm.
1071 } else if ((c == 'l') && (length>1) && (!strncmp(compression,"lzw",length))) {
1072 *comp = COMPRESSION_LZW;
1073 */
1074 } else if ((c == 'p') && (length>1) && (!strncmp(compression,"packbits",length))) {
1075 *comp = COMPRESSION_PACKBITS;
1076 } else if ((c == 'p') && (length>1) && (!strncmp(compression,"pixarlog",length))) {
1077 *comp = COMPRESSION_PIXARLOG;
1078 } else {
1079 Tcl_AppendResult(interp, "invalid compression mode \"",
1080 compression,"\": should be deflate, jpeg, logluv, lzw, ",
1081 "packbits, pixarlog, or none", (char *) NULL);
1082 return TCL_ERROR;
1083 }
1084 c = byteorder[0]; length = strlen(byteorder);
1085 if (c == 0) {
1086 *mode = "w";
1087 } else if ((c == 's') && (!strncmp(byteorder,"smallendian", length))) {
1088 *mode = "wl";
1089 } else if ((c == 'l') && (!strncmp(byteorder,"littleendian", length))) {
1090 *mode = "wl";
1091 } else if ((c == 'b') && (!strncmp(byteorder,"bigendian", length))) {
1092 *mode = "wb";
1093 } else if ((c == 'n') && (!strncmp(byteorder,"network", length))) {
1094 *mode = "wb";
1095 } else {
1096 Tcl_AppendResult(interp, "invalid byteorder \"",
1097 byteorder,"\": should be bigendian, littleendian",
1098 "network, smallendian, or {}", (char *) NULL);
1099 return TCL_ERROR;
1100 }
1101 }
1102 return TCL_OK;
1103 }
1104
1105 static int CommonWriteTIFF(interp, tif, comp, blockPtr)
1106 Tcl_Interp *interp;
1107 TIFF *tif;
1108 int comp;
1109 Tk_PhotoImageBlock *blockPtr;
1110 {
1111 int numsamples;
1112 unsigned char *data = NULL;
1113
1114 tiff.SetField(tif, TIFFTAG_IMAGEWIDTH, blockPtr->width);
1115 tiff.SetField(tif, TIFFTAG_IMAGELENGTH, blockPtr->height);
1116 tiff.SetField(tif, TIFFTAG_COMPRESSION, comp);
1117
1118 tiff.SetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
1119 tiff.SetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1120 tiff.SetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
1121 tiff.SetField(tif, TIFFTAG_ROWSPERSTRIP, blockPtr->height);
1122
1123 tiff.SetField(tif, TIFFTAG_RESOLUTIONUNIT, (int)2);
1124 tiff.SetField(tif, TIFFTAG_XRESOLUTION, (float)1200.0);
1125 tiff.SetField(tif, TIFFTAG_YRESOLUTION, (float)1200.0);
1126
1127 tiff.SetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
1128 if ((blockPtr->offset[0] == blockPtr->offset[1])
1129 && (blockPtr->offset[0] == blockPtr->offset[2])) {
1130 numsamples = 1;
1131 tiff.SetField(tif, TIFFTAG_SAMPLESPERPIXEL, 1);
1132 tiff.SetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK);
1133 } else {
1134 numsamples = 3;
1135 tiff.SetField(tif, TIFFTAG_SAMPLESPERPIXEL, 3);
1136 tiff.SetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
1137 }
1138
1139 if ((blockPtr->pitch == numsamples * blockPtr->width)
1140 && (blockPtr->pixelSize == numsamples)) {
1141 data = blockPtr->pixelPtr;
1142 } else {
1143 unsigned char *srcPtr, *dstPtr, *rowPtr;
1144 int greenOffset, blueOffset, alphaOffset, x, y;
1145 dstPtr = data = (unsigned char *) ckalloc(numsamples *
1146 blockPtr->width * blockPtr->height);
1147 rowPtr = blockPtr->pixelPtr + blockPtr->offset[0];
1148 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
1149 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
1150 alphaOffset = blockPtr->offset[0];
1151 if (alphaOffset < blockPtr->offset[2]) {
1152 alphaOffset = blockPtr->offset[2];
1153 }
1154 if (++alphaOffset < blockPtr->pixelSize) {
1155 alphaOffset -= blockPtr->offset[0];
1156 } else {
1157 alphaOffset = 0;
1158 }
1159 if (blueOffset || greenOffset) {
1160 for (y = blockPtr->height; y > 0; y--) {
1161 srcPtr = rowPtr;
1162 for (x = blockPtr->width; x>0; x--) {
1163 if (alphaOffset && !srcPtr[alphaOffset]) {
1164 *dstPtr++ = 0xd9;
1165 *dstPtr++ = 0xd9;
1166 *dstPtr++ = 0xd9;
1167 } else {
1168 *dstPtr++ = srcPtr[0];
1169 *dstPtr++ = srcPtr[greenOffset];
1170 *dstPtr++ = srcPtr[blueOffset];
1171 }
1172 srcPtr += blockPtr->pixelSize;
1173 }
1174 rowPtr += blockPtr->pitch;
1175 }
1176 } else {
1177 for (y = blockPtr->height; y > 0; y--) {
1178 srcPtr = rowPtr;
1179 for (x = blockPtr->width; x>0; x--) {
1180 *dstPtr++ = srcPtr[0];
1181 srcPtr += blockPtr->pixelSize;
1182 }
1183 rowPtr += blockPtr->pitch;
1184 }
1185 }
1186 }
1187
1188 tiff.WriteEncodedStrip(tif, 0, data,
1189 numsamples * blockPtr->width * blockPtr->height);
1190 if (data != blockPtr->pixelPtr) {
1191 ckfree((char *) data);
1192 }
1193
1194 return TCL_OK;
1195 }