Debian Codegraph @

PNG/imgPNG.c

raw symbols SCIP
1 /*
2 * tkImgPNG.c --
3 *
4 * A photo image file handler for PNG files.
5 *
6 * Uses the libpng_so library, which is dynamically
7 * loaded only when used.
8 *
9 */
10
11 /* Author : Jan Nijtmans */
12 /* Date : 2/13/97 */
13 /* Original implementation : Joel Crisp */
14
15 #include <string.h>
16 #include <stdlib.h>
17
18 #include "pTk/imgInt.h"
19 #include <pTk/tkImgPhoto.h>
20 #include "pTk/tkVMacro.h"
21
22 #undef EXTERN
23
24 #ifdef MAC_TCL
25 #include "libpng:png_h"
26 #else
27 #ifdef HAVE_IMG_H
28 # include <png.h>
29 #else
30 # include "png.h"
31 #endif
32 #endif
33
34 #ifdef __WIN32__
35 #define PNG_LIB_NAME "png.dll"
36 #endif
37
38 #ifndef PNG_LIB_NAME
39 #define PNG_LIB_NAME "libpng.so"
40 #endif
41
42 #define COMPRESS_THRESHOLD 1024
43
44 typedef struct png_text_struct_compat
45 {
46 int compression; /* compression value:
47 -1: tEXt, none
48 0: zTXt, deflate
49 1: iTXt, none
50 2: iTXt, deflate */
51 png_charp key; /* keyword, 1-79 character description of "text" */
52 png_charp text; /* comment, may be an empty string (ie "")
53 or a NULL pointer */
54 png_size_t text_length; /* length of the text string */
55 png_size_t itxt_length; /* length of the itxt string */
56 png_charp lang; /* language code, 0-79 characters
57 or a NULL pointer */
58 png_charp lang_key; /* keyword translated UTF-8 string, 0 or more
59 chars or a NULL pointer */
60 } png_text_compat;
61
62 /*
63 * The format record for the PNG file format:
64 */
65
66 /*
67 The extra Tcl_Interp * arg was added in different
68 place when tcl/tk adopted this code so we need two
69 signatures of a couple of functions - one for Tk800 and
70 one for modern Tk
71 */
72 #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION == 0
73 static int ChnMatchPNG _ANSI_ARGS_((Tcl_Interp *interp,Tcl_Channel chan, Tcl_Obj *fileName,
74 Tcl_Obj *format, int *widthPtr, int *heightPtr));
75 static int ObjMatchPNG _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj,
76 Tcl_Obj *format, int *widthPtr, int *heightPtr));
77 #else
78 static int ChnMatchPNG _ANSI_ARGS_((Tcl_Channel chan, Tcl_Obj *fileName,
79 Tcl_Obj *format, int *widthPtr, int *heightPtr,Tcl_Interp *interp));
80 static int ObjMatchPNG _ANSI_ARGS_((Tcl_Obj *dataObj,
81 Tcl_Obj *format, int *widthPtr, int *heightPtr,Tcl_Interp *interp));
82 #endif
83
84 static int ChnReadPNG _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Channel chan,
85 Tcl_Obj *fileName, Tcl_Obj *format, Tk_PhotoHandle imageHandle,
86 int destX, int destY, int width, int height, int srcX, int srcY));
87 static int ObjReadPNG _ANSI_ARGS_((Tcl_Interp *interp, Tcl_Obj *dataObj,
88 Tcl_Obj *format, Tk_PhotoHandle imageHandle,
89 int destX, int destY, int width, int height, int srcX, int srcY));
90 static int ChnWritePNG _ANSI_ARGS_((Tcl_Interp *interp, char *filename,
91 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
92 static int StringWritePNG _ANSI_ARGS_((Tcl_Interp *interp,
93 Tcl_Obj *format, Tk_PhotoImageBlock *blockPtr));
94
95
96 Tk_PhotoImageFormat imgFmtPNG = {
97 "png", /* name */
98 ChnMatchPNG, /* fileMatchProc */
99 ObjMatchPNG, /* stringMatchProc */
100 ChnReadPNG, /* fileReadProc */
101 ObjReadPNG, /* stringReadProc */
102 ChnWritePNG, /* fileWriteProc */
103 StringWritePNG, /* stringWriteProc */
104 };
105
106 /*
107 * Prototypes for local procedures defined in this file:
108 */
109
110 static int CommonMatchPNG _ANSI_ARGS_((MFile *handle, int *widthPtr,
111 int *heightPtr));
112 static int CommonReadPNG _ANSI_ARGS_((png_structp png_ptr, Tcl_Obj *format,
113 Tk_PhotoHandle imageHandle, int destX, int destY, int width,
114 int height, int srcX, int srcY));
115 static int CommonWritePNG _ANSI_ARGS_((Tcl_Interp *interp, png_structp png_ptr,
116 png_infop info_ptr, Tcl_Obj *format,
117 Tk_PhotoImageBlock *blockPtr));
118 static void tk_png_error _ANSI_ARGS_((png_structp, png_const_charp));
119 static void tk_png_warning _ANSI_ARGS_((png_structp, png_const_charp));
120
121 /*
122 * These functions are used for all Input/Output.
123 */
124
125 static void tk_png_read _ANSI_ARGS_((png_structp, png_bytep,
126 png_size_t));
127 static void tk_png_write _ANSI_ARGS_((png_structp, png_bytep,
128 png_size_t));
129 static void tk_png_flush _ANSI_ARGS_((png_structp));
130
131 #ifndef _LANG
132
133 static struct PngFunctions {
134 VOID *handle;
135 png_structp (* create_read_struct) _ANSI_ARGS_((png_const_charp,
136 png_voidp, png_error_ptr, png_error_ptr));
137 png_infop (* create_info_struct) _ANSI_ARGS_((png_structp));
138 png_structp (* create_write_struct) _ANSI_ARGS_((png_const_charp,
139 png_voidp, png_error_ptr, png_error_ptr));
140 void (* destroy_read_struct) _ANSI_ARGS_((png_structpp,
141 png_infopp, png_infopp));
142 void (* destroy_write_struct) _ANSI_ARGS_((png_structpp, png_infopp));
143 void (* error) _ANSI_ARGS_((png_structp, png_charp));
144 png_byte (* get_channels) _ANSI_ARGS_((png_structp, png_infop));
145 png_voidp (* get_error_ptr) _ANSI_ARGS_((png_structp));
146 png_voidp (* get_progressive_ptr) _ANSI_ARGS_((png_structp));
147 png_uint_32 (* get_rowbytes) _ANSI_ARGS_((png_structp, png_infop));
148 png_uint_32 (* get_IHDR) _ANSI_ARGS_((png_structp, png_infop,
149 png_uint_32*, png_uint_32*, int*, int*, int*, int*, int*));
150 png_uint_32 (* get_valid) _ANSI_ARGS_((png_structp, png_infop, png_uint_32));
151 void (* read_image) _ANSI_ARGS_((png_structp, png_bytepp));
152 void (* read_info) _ANSI_ARGS_((png_structp, png_infop));
153 void (* read_update_info) _ANSI_ARGS_((png_structp, png_infop));
154 int (* set_interlace_handling) _ANSI_ARGS_ ((png_structp));
155 void (* set_read_fn) _ANSI_ARGS_((png_structp, png_voidp, png_rw_ptr));
156 void (* set_text) _ANSI_ARGS_((png_structp, png_infop, png_textp, int));
157 void (* set_write_fn) _ANSI_ARGS_((png_structp, png_voidp,
158 png_rw_ptr, png_voidp));
159 void (* set_IHDR) _ANSI_ARGS_((png_structp, png_infop, png_uint_32,
160 png_uint_32, int, int, int, int, int));
161 void (* write_end) _ANSI_ARGS_((png_structp, png_infop));
162 void (* write_info) _ANSI_ARGS_((png_structp, png_infop));
163 void (* write_row) _ANSI_ARGS_((png_structp, png_bytep));
164 void (* set_expand) _ANSI_ARGS_((png_structp));
165 void (* set_filler) _ANSI_ARGS_((png_structp, png_uint_32, int));
166 void (* set_strip_16) _ANSI_ARGS_((png_structp));
167 png_uint_32 (* get_sRGB) _ANSI_ARGS_((png_structp, png_infop, int *));
168 void (* set_sRGB) _ANSI_ARGS_((png_structp, png_infop, int));
169 png_uint_32 (* get_gAMA) _ANSI_ARGS_((png_structp, png_infop, double *));
170 void (* set_gAMA) PNGARG((png_structp png_ptr, png_infop, double));
171 void (* set_gamma) _ANSI_ARGS_((png_structp, double, double));
172 void (* set_sRGB_gAMA_and_cHRM) _ANSI_ARGS_((png_structp, png_infop, int));
173 void (* write_iTXt) _ANSI_ARGS_((png_structp, int, png_charp, png_charp,
174 png_charp, png_charp));
175 } png = {0};
176
177 static char *symbols[] = {
178 "png_create_read_struct",
179 "png_create_info_struct",
180 "png_create_write_struct",
181 "png_destroy_read_struct",
182 "png_destroy_write_struct",
183 "png_error",
184 "png_get_channels",
185 "png_get_error_ptr",
186 "png_get_progressive_ptr",
187 "png_get_rowbytes",
188 "png_get_IHDR",
189 "png_get_valid",
190 "png_read_image",
191 "png_read_info",
192 "png_read_update_info",
193 "png_set_interlace_handling",
194 "png_set_read_fn",
195 "png_set_text",
196 "png_set_write_fn",
197 "png_set_IHDR",
198 "png_write_end",
199 "png_write_info",
200 "png_write_row",
201 /* The following symbols are not crucial. All of them
202 are checked at runtime. */
203 "png_set_expand",
204 "png_set_filler",
205 "png_set_strip_16",
206 "png_get_sRGB",
207 "png_set_sRGB",
208 "png_get_gAMA",
209 "png_set_gAMA",
210 "png_set_gamma",
211 "png_set_sRGB_gAMA_and_cHRM",
212 "png_write_iTXt", /* Only used to check if libpng has iTXt support at runtime */
213 (char *) NULL
214 };
215
216 #endif
217
218 typedef struct cleanup_info {
219 Tcl_Interp *interp;
220 jmp_buf jmpbuf;
221 } cleanup_info;
222
223 static void
224 tk_png_error(png_ptr, error_msg)
225 png_structp png_ptr;
226 png_const_charp error_msg;
227 {
228 cleanup_info *info = (cleanup_info *) png_get_error_ptr(png_ptr);
229 Tcl_AppendResult(info->interp, error_msg, (char *) NULL);
230 longjmp(info->jmpbuf,1);
231 }
232
233 static void
234 tk_png_warning(png_ptr, error_msg)
235 png_structp png_ptr;
236 png_const_charp error_msg;
237 {
238 return;
239 }
240
241 static void
242 tk_png_read(png_ptr, data, length)
243 png_structp png_ptr;
244 png_bytep data;
245 png_size_t length;
246 {
247 if (ImgRead((MFile *) png_get_progressive_ptr(png_ptr),
248 (char *) data, (size_t) length) != (int) length) {
249 png_error(png_ptr, "Read Error");
250 }
251 }
252
253 static void
254 tk_png_write(png_ptr, data, length)
255 png_structp png_ptr;
256 png_bytep data;
257 png_size_t length;
258 {
259 if (ImgWrite((MFile *) png_get_progressive_ptr(png_ptr),
260 (char *) data, (size_t) length) != (int) length) {
261 png_error(png_ptr, "Write Error");
262 }
263 }
264
265 static void
266 tk_png_flush(png_ptr)
267 png_structp png_ptr;
268 {
269 }
270
271 #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION == 0
272 static int ChnMatchPNG(interp, chan, fileName, format, widthPtr, heightPtr)
273 #else
274 static int ChnMatchPNG(chan, fileName, format, widthPtr, heightPtr,interp)
275 #endif
276 Tcl_Interp *interp;
277 Tcl_Channel chan;
278 Tcl_Obj *fileName;
279 Tcl_Obj *format;
280 int *widthPtr, *heightPtr;
281 {
282 MFile handle;
283
284 ImgFixChanMatchProc(&interp, &chan, &fileName, &format, &widthPtr, &heightPtr);
285
286 handle.data = (char *) chan;
287 handle.state = IMG_CHAN;
288
289 return CommonMatchPNG(&handle, widthPtr, heightPtr);
290 }
291
292 #if TK_MAJOR_VERSION == 8 && TK_MINOR_VERSION == 0
293 static int ObjMatchPNG(interp, data, format, widthPtr, heightPtr)
294 #else
295 static int ObjMatchPNG(data, format, widthPtr, heightPtr, interp)
296 #endif
297 Tcl_Interp *interp;
298 Tcl_Obj *data;
299 Tcl_Obj *format;
300 int *widthPtr, *heightPtr;
301 {
302 MFile handle;
303
304 ImgFixObjMatchProc(&interp, &data, &format, &widthPtr, &heightPtr);
305
306 if (!ImgReadInit(data,'\211',&handle)) {
307 return 0;
308 }
309 return CommonMatchPNG(&handle, widthPtr, heightPtr);
310 }
311
312 static int CommonMatchPNG(handle, widthPtr, heightPtr)
313 MFile *handle;
314 int *widthPtr, *heightPtr;
315 {
316 unsigned char buf[8];
317
318 if ((ImgRead(handle, (char *) buf, 8) != 8)
319 || (strncmp("\211\120\116\107\15\12\32\12", (char *) buf, 8) != 0)
320 || (ImgRead(handle, (char *) buf, 8) != 8)
321 || (strncmp("\111\110\104\122", (char *) buf+4, 4) != 0)
322 || (ImgRead(handle, (char *) buf, 8) != 8)) {
323 return 0;
324 }
325 *widthPtr = (buf[0]<<24) + (buf[1]<<16) + (buf[2]<<8) + buf[3];
326 *heightPtr = (buf[4]<<24) + (buf[5]<<16) + (buf[6]<<8) + buf[7];
327 return 1;
328 }
329
330
331 static int
332 load_png_library(interp)
333 Tcl_Interp *interp;
334 {
335 #ifndef _LANG
336 if (ImgLoadLib(interp, PNG_LIB_NAME, &png_handle, symbols, 22)
337 != TCL_OK) {
338 return TCL_ERROR;
339 }
340 #endif
341 return TCL_OK;
342 }
343
344 static int ChnReadPNG(interp, chan, fileName, format, imageHandle,
345 destX, destY, width, height, srcX, srcY)
346 Tcl_Interp *interp;
347 Tcl_Channel chan;
348 Tcl_Obj *fileName;
349 Tcl_Obj *format;
350 Tk_PhotoHandle imageHandle;
351 int destX, destY;
352 int width, height;
353 int srcX, srcY;
354 {
355 png_structp png_ptr;
356 MFile handle;
357 cleanup_info cleanup;
358
359 cleanup.interp = interp;
360
361 if (load_png_library(interp) != TCL_OK) {
362 return TCL_ERROR;
363 }
364
365 handle.data = (char *) chan;
366 handle.state = IMG_CHAN;
367
368 cleanup.interp = interp;
369
370 png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,
371 (png_voidp) &cleanup,tk_png_error,tk_png_warning);
372 if (!png_ptr) return(0);
373
374 png_set_read_fn(png_ptr, (png_voidp) &handle, tk_png_read);
375
376 return CommonReadPNG(png_ptr, format, imageHandle, destX, destY,
377 width, height, srcX, srcY);
378 }
379
380 static int ObjReadPNG(interp, dataObj, format, imageHandle,
381 destX, destY, width, height, srcX, srcY)
382 Tcl_Interp *interp;
383 Tcl_Obj *dataObj;
384 Tcl_Obj *format;
385 Tk_PhotoHandle imageHandle;
386 int destX, destY;
387 int width, height;
388 int srcX, srcY;
389 {
390 png_structp png_ptr;
391 MFile handle;
392 cleanup_info cleanup;
393
394 cleanup.interp = interp;
395
396 png_ptr=png_create_read_struct(PNG_LIBPNG_VER_STRING,
397 (png_voidp) &cleanup,tk_png_error,tk_png_warning);
398 if (!png_ptr) return TCL_ERROR;
399
400 ImgReadInit(dataObj,'\211',&handle);
401
402 png_set_read_fn(png_ptr,(png_voidp) &handle, tk_png_read);
403
404 return CommonReadPNG(png_ptr, format, imageHandle, destX, destY,
405 width, height, srcX, srcY);
406 }
407
408 typedef struct myblock {
409 Tk_PhotoImageBlock ck;
410 int dummy; /* extra space for offset[3], in case it is not
411 included already in Tk_PhotoImageBlock */
412 } myblock;
413
414 #define block bl.ck
415
416 static int CommonReadPNG(png_ptr, format, imageHandle, destX, destY,
417 width, height, srcX, srcY)
418 png_structp png_ptr;
419 Tcl_Obj *format;
420 Tk_PhotoHandle imageHandle;
421 int destX, destY;
422 #ifdef __GNUC__
423 volatile
424 #endif
425 int width, height;
426 int srcX, srcY;
427 {
428 png_infop info_ptr;
429 png_infop end_info;
430 char **
431 #ifdef __GNUC__
432 volatile
433 #endif
434 png_data = NULL;
435 myblock bl;
436 unsigned int I;
437 png_uint_32 info_width, info_height;
438 int bit_depth, color_type, interlace_type;
439 int intent;
440
441 info_ptr=png_create_info_struct(png_ptr);
442 if (!info_ptr) {
443 png_destroy_read_struct(&png_ptr,NULL,NULL);
444 return(TCL_ERROR);
445 }
446
447 end_info=png_create_info_struct(png_ptr);
448 if (!end_info) {
449 png_destroy_read_struct(&png_ptr,&info_ptr,NULL);
450 return(TCL_ERROR);
451 }
452
453 if (setjmp((((cleanup_info *) png_get_error_ptr(png_ptr))->jmpbuf))) {
454 if (png_data) {
455 ckfree((char *)png_data);
456 }
457 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
458 return TCL_ERROR;
459 }
460
461 png_read_info(png_ptr,info_ptr);
462
463 png_get_IHDR(png_ptr, info_ptr, &info_width, &info_height, &bit_depth,
464 &color_type, &interlace_type, (int *) NULL, (int *) NULL);
465
466 if ((srcX + width) > (int) info_width) {
467 width = info_width - srcX;
468 }
469 if ((srcY + height) > (int) info_height) {
470 height = info_height - srcY;
471 }
472 if ((width <= 0) || (height <= 0)
473 || (srcX >= (int) info_width)
474 || (srcY >= (int) info_height)) {
475 return TCL_OK;
476 }
477
478 Tk_PhotoExpand(imageHandle, destX + width, destY + height);
479
480 Tk_PhotoGetImage(imageHandle, &block);
481
482 if (png_set_strip_16 != NULL) {
483 png_set_strip_16(png_ptr);
484 } else if (bit_depth == 16) {
485 block.offset[1] = 2;
486 block.offset[2] = 4;
487 }
488
489 if (png_set_expand != NULL) {
490 png_set_expand(png_ptr);
491 }
492
493 #if defined(PNG_sRGB_SUPPORTED) || defined(PNG_gAMA_SUPPORTED)
494 #if defined(PNG_sRGB_SUPPORTED)
495 if (png_get_sRGB(png_ptr, info_ptr, &intent)) {
496 png_set_sRGB(png_ptr, info_ptr, intent);
497 } else {
498 #endif
499 #if defined(PNG_gAMA_SUPPORTED)
500 double gamma;
501 if (!png_get_gAMA(png_ptr, info_ptr, &gamma)) {
502 gamma = 0.45455;
503 }
504 png_set_gamma(png_ptr, 2.2, gamma);
505 #endif
506 #if defined(PNG_sRGB_SUPPORTED)
507 }
508 #endif
509 #endif
510
511 png_read_update_info(png_ptr,info_ptr);
512 block.pixelSize = png_get_channels(png_ptr, info_ptr);
513 block.pitch = png_get_rowbytes(png_ptr, info_ptr);
514
515 if ((color_type & PNG_COLOR_MASK_COLOR) == 0) {
516 /* grayscale image */
517 block.offset[1] = 0;
518 block.offset[2] = 0;
519 }
520 block.width = width;
521 block.height = height;
522
523 if ((color_type & PNG_COLOR_MASK_ALPHA)
524 || png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
525 /* with alpha channel */
526 block.offset[3] = block.pixelSize - 1;
527 } else {
528 /* without alpha channel */
529 block.offset[3] = 0;
530 }
531
532 png_data= (char **) ckalloc(sizeof(char *) * info_height +
533 info_height * block.pitch);
534
535 for(I=0;I<info_height;I++) {
536 png_data[I]= ((char *) png_data) + (sizeof(char *) * info_height +
537 I * block.pitch);
538 }
539 block.pixelPtr=(unsigned char *) (png_data[srcY]+srcX*block.pixelSize);
540
541 png_read_image(png_ptr,(png_bytepp) png_data);
542
543 ImgPhotoPutBlock(imageHandle,&block,destX,destY,width,height);
544
545 ckfree((char *) png_data);
546 png_destroy_read_struct(&png_ptr,&info_ptr,&end_info);
547
548 return(TCL_OK);
549 }
550
551 static int ChnWritePNG(interp, filename, format, blockPtr)
552 Tcl_Interp *interp;
553 char *filename;
554 Tcl_Obj *format;
555 Tk_PhotoImageBlock *blockPtr;
556 {
557 png_structp png_ptr;
558 png_infop info_ptr;
559 MFile handle;
560 int result;
561 cleanup_info cleanup;
562 Tcl_Channel chan = (Tcl_Channel) NULL;
563
564 chan = ImgOpenFileChannel(interp, filename, 0644);
565 if (!chan) {
566 return TCL_ERROR;
567 }
568
569 handle.data = (char *) chan;
570 handle.state = IMG_CHAN;
571
572 if (load_png_library(interp) != TCL_OK) {
573 Tcl_Close(NULL, chan);
574 return TCL_ERROR;
575 }
576
577 cleanup.interp = interp;
578
579 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
580 (png_voidp) &cleanup,tk_png_error,tk_png_warning);
581 if (!png_ptr) {
582 Tcl_Close(NULL, chan);
583 return TCL_ERROR;
584 }
585
586 info_ptr=png_create_info_struct(png_ptr);
587 if (!info_ptr) {
588 png_destroy_write_struct(&png_ptr,NULL);
589 Tcl_Close(NULL, chan);
590 return TCL_ERROR;
591 }
592
593 png_set_write_fn(png_ptr,(png_voidp) &handle, tk_png_write, tk_png_flush);
594
595 result = CommonWritePNG(interp, png_ptr, info_ptr, format, blockPtr);
596 Tcl_Close(NULL, chan);
597 return result;
598 }
599
600 static int StringWritePNG(interp, format, blockPtr)
601 Tcl_Interp *interp;
602 Tcl_Obj *format;
603 Tk_PhotoImageBlock *blockPtr;
604 {
605 png_structp png_ptr;
606 png_infop info_ptr;
607 MFile handle;
608 int result;
609 cleanup_info cleanup;
610 Tcl_DString data;
611 Tcl_DString *dataPtr;
612
613 ImgFixStringWriteProc(&data, &interp, &dataPtr, &format, &blockPtr);
614
615 cleanup.interp = interp;
616
617 png_ptr=png_create_write_struct(PNG_LIBPNG_VER_STRING,
618 (png_voidp) &cleanup, tk_png_error, tk_png_warning);
619 if (!png_ptr) {
620 return TCL_ERROR;
621 }
622
623 info_ptr = png_create_info_struct(png_ptr);
624 if (!info_ptr) {
625 png_destroy_write_struct(&png_ptr,NULL);
626 return TCL_ERROR;
627 }
628
629 png_set_write_fn(png_ptr, (png_voidp) &handle, tk_png_write, tk_png_flush);
630
631 ImgWriteInit(dataPtr, &handle);
632
633 result = CommonWritePNG(interp, png_ptr, info_ptr, format, blockPtr);
634 ImgPutc(IMG_DONE, &handle);
635 if ((result == TCL_OK) && (dataPtr == &data)) {
636 Tcl_DStringResult(interp, dataPtr);
637 }
638 return result;
639 }
640
641 static int CommonWritePNG(interp, png_ptr, info_ptr, format, blockPtr)
642 Tcl_Interp *interp;
643 png_structp png_ptr;
644 png_infop info_ptr;
645 Tcl_Obj *format;
646 Tk_PhotoImageBlock *blockPtr;
647 {
648 int greenOffset, blueOffset, alphaOffset;
649 int tagcount = 0;
650 Tcl_Obj **tags = (Tcl_Obj **) NULL;
651 int I, pass, number_passes, color_type;
652 int newPixelSize;
653 #ifdef __GNUC__
654 volatile
655 #endif
656 png_bytep row_pointers = (png_bytep) NULL;
657
658 if (ImgListObjGetElements(interp, format, &tagcount, &tags) != TCL_OK) {
659 return TCL_ERROR;
660 }
661 tagcount = (tagcount > 1) ? (tagcount/2 - 1) : 0;
662
663 if (setjmp((((cleanup_info *) png_get_error_ptr(png_ptr))->jmpbuf))) {
664 if (row_pointers) {
665 ckfree((char *) row_pointers);
666 }
667 png_destroy_write_struct(&png_ptr,&info_ptr);
668 return TCL_ERROR;
669 }
670 greenOffset = blockPtr->offset[1] - blockPtr->offset[0];
671 blueOffset = blockPtr->offset[2] - blockPtr->offset[0];
672 alphaOffset = blockPtr->offset[0];
673 if (alphaOffset < blockPtr->offset[2]) {
674 alphaOffset = blockPtr->offset[2];
675 }
676 if (++alphaOffset < blockPtr->pixelSize) {
677 alphaOffset -= blockPtr->offset[0];
678 } else {
679 alphaOffset = 0;
680 }
681
682 if (greenOffset || blueOffset) {
683 color_type = PNG_COLOR_TYPE_RGB;
684 newPixelSize = 3;
685 } else {
686 color_type = PNG_COLOR_TYPE_GRAY;
687 newPixelSize = 1;
688 }
689 if (alphaOffset) {
690 color_type |= PNG_COLOR_MASK_ALPHA;
691 newPixelSize++;
692 #if 0 /* The function png_set_filler doesn't seem to work; don't known why :-( */
693 } else if ((blockPtr->pixelSize==4) && (newPixelSize == 3)
694 && (png_set_filler != NULL)) {
695 /*
696 * The set_filler() function doesn't need to be called
697 * because the code below can handle all necessary
698 * re-allocation of memory. Only it is more economically
699 * to let the PNG library do that, which is only
700 * possible with v0.95 and higher.
701 */
702 png_set_filler(png_ptr, 0, PNG_FILLER_AFTER);
703 newPixelSize++;
704 #endif
705 }
706
707 png_set_IHDR(png_ptr, info_ptr, blockPtr->width, blockPtr->height, 8,
708 color_type, PNG_INTERLACE_ADAM7, PNG_COMPRESSION_TYPE_BASE,
709 PNG_FILTER_TYPE_BASE);
710
711 #if defined(PNG_gAMA_SUPPORTED)
712 png_set_gAMA(png_ptr, info_ptr, 1.0);
713 #endif
714
715 if (tagcount > 0) {
716 png_text_compat text;
717 for(I=0;I<tagcount;I++) {
718 int length;
719 text.key = Tcl_GetStringFromObj(tags[2*I+1], (int *) NULL);
720 text.text = Tcl_GetStringFromObj(tags[2*I+2], &length);
721 text.text_length = length;
722 if (text.text_length>COMPRESS_THRESHOLD) {
723 text.compression = PNG_TEXT_COMPRESSION_zTXt;
724 } else {
725 text.compression = PNG_TEXT_COMPRESSION_NONE;
726 }
727 text.lang = NULL;
728 png_set_text(png_ptr, info_ptr, (png_text *) &text, 1);
729 }
730 }
731 png_write_info(png_ptr,info_ptr);
732
733 number_passes = png_set_interlace_handling(png_ptr);
734
735 if (blockPtr->pixelSize != newPixelSize) {
736 int J, oldPixelSize;
737 png_bytep src, dst;
738 oldPixelSize = blockPtr->pixelSize;
739 row_pointers = (png_bytep)
740 ckalloc(blockPtr->width * newPixelSize);
741 for (pass = 0; pass < number_passes; pass++) {
742 for(I=0; I<blockPtr->height; I++) {
743 src = (png_bytep) blockPtr->pixelPtr
744 + I * blockPtr->pitch + blockPtr->offset[0];
745 dst = row_pointers;
746 for (J = blockPtr->width; J > 0; J--) {
747 memcpy(dst, src, newPixelSize);
748 src += oldPixelSize;
749 dst += newPixelSize;
750 }
751 png_write_row(png_ptr, row_pointers);
752 }
753 }
754 ckfree((char *) row_pointers);
755 row_pointers = NULL;
756 } else {
757 for (pass = 0; pass < number_passes; pass++) {
758 for(I=0;I<blockPtr->height;I++) {
759 png_write_row(png_ptr, (png_bytep) blockPtr->pixelPtr
760 + I * blockPtr->pitch + blockPtr->offset[0]);
761 }
762 }
763 }
764 png_write_end(png_ptr,NULL);
765 png_destroy_write_struct(&png_ptr,&info_ptr);
766
767 return(TCL_OK);
768 }