Debian Codegraph @

pTk/mTk/additions/imgPNG.c

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