File: imgPNG.c.diff

package info (click to toggle)
libtk-img 1%3A1.3-15etch3
  • links: PTS
  • area: main
  • in suites: etch
  • size: 20,144 kB
  • ctags: 14,513
  • sloc: ansic: 110,000; sh: 65,326; tcl: 2,844; asm: 996; cpp: 560; pascal: 536; makefile: 436
file content (485 lines) | stat: -rw-r--r-- 13,286 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
*** ../img1.2.3/imgPNG.c	Sat Jan 15 11:13:45 2000
--- imgPNG.c	Sun Aug 13 06:11:31 2000
***************
*** 12,18 ****
  /* Date   : 2/13/97        */
  /* Original implementation : Joel Crisp     */
  
- #include <stdio.h>
  #include <string.h>
  #include <stdlib.h>
  
--- 12,17 ----
***************
*** 40,45 ****
--- 39,62 ----
  
  #define COMPRESS_THRESHOLD 1024
  
+ typedef struct png_text_struct_compat
+ {
+    int  compression;       /* compression value:
+                              -1: tEXt, none
+                               0: zTXt, deflate
+                               1: iTXt, none
+                               2: iTXt, deflate  */
+    png_charp key;          /* keyword, 1-79 character description of "text" */
+    png_charp text;         /* comment, may be an empty string (ie "")
+                               or a NULL pointer */
+    png_size_t text_length; /* length of the text string */
+    png_size_t itxt_length; /* length of the itxt string */
+    png_charp lang;         /* language code, 0-79 characters
+                               or a NULL pointer */
+    png_charp lang_key;     /* keyword translated UTF-8 string, 0 or more
+                               chars or a NULL pointer */
+ } png_text_compat;
+ 
  /*
   * The format record for the PNG file format:
   */
***************
*** 61,66 ****
--- 78,84 ----
  	Tcl_DString *dataPtr, Tcl_Obj *format,
  	Tk_PhotoImageBlock *blockPtr));
  
+ 
  Tk_PhotoImageFormat imgFmtPNG = {
      "png",					/* name */
      (Tk_ImageFileMatchProc *) ChnMatchPNG,	/* fileMatchProc */
***************
*** 114,120 ****
      png_uint_32 (* get_IHDR) _ANSI_ARGS_((png_structp, png_infop,
  	   png_uint_32*, png_uint_32*, int*, int*, int*, int*, int*));
      png_uint_32 (* get_valid) _ANSI_ARGS_((png_structp, png_infop, png_uint_32));
-     void (* init_io) _ANSI_ARGS_((png_structp, FILE *));
      void (* read_image) _ANSI_ARGS_((png_structp, png_bytepp));
      void (* read_info) _ANSI_ARGS_((png_structp, png_infop));
      void (* read_update_info) _ANSI_ARGS_((png_structp, png_infop));
--- 132,137 ----
***************
*** 137,142 ****
--- 154,161 ----
      void (* set_gAMA) PNGARG((png_structp png_ptr, png_infop, double));
      void (* set_gamma) _ANSI_ARGS_((png_structp, double, double));
      void (* set_sRGB_gAMA_and_cHRM) _ANSI_ARGS_((png_structp, png_infop, int));
+     void (* write_iTXt) _ANSI_ARGS_((png_structp, int, png_charp, png_charp,
+ 	    png_charp, png_charp));
  } png = {0};
  
  static char *symbols[] = {
***************
*** 152,158 ****
      "png_get_rowbytes",
      "png_get_IHDR",
      "png_get_valid",
-     "png_init_io",
      "png_read_image",
      "png_read_info",
      "png_read_update_info",
--- 171,176 ----
***************
*** 175,186 ****
      "png_set_gAMA",
      "png_set_gamma",
      "png_set_sRGB_gAMA_and_cHRM",
      (char *) NULL
  };
  
  typedef struct cleanup_info {
      Tcl_Interp *interp;
!     char **data;
  } cleanup_info;
  
  static void
--- 193,205 ----
      "png_set_gAMA",
      "png_set_gamma",
      "png_set_sRGB_gAMA_and_cHRM",
+     "png_write_iTXt", /* Only used to check if libpng has iTXt support at runtime */
      (char *) NULL
  };
  
  typedef struct cleanup_info {
      Tcl_Interp *interp;
!     jmp_buf jmpbuf;
  } cleanup_info;
  
  static void
***************
*** 188,202 ****
      png_structp png_ptr;
      png_const_charp error_msg;
  {
!     cleanup_info *info;
! 
!     info = (cleanup_info *) png.get_error_ptr(png_ptr);
!     if (info->data) {
! 	ckfree((char *) info->data);
!     }
!     Tcl_AppendResult(info->interp,
! 	    error_msg, (char *) NULL);
!     longjmp(*(jmp_buf *) png_ptr,1);
  }
  
  static void
--- 207,215 ----
      png_structp png_ptr;
      png_const_charp error_msg;
  {
!     cleanup_info *info = (cleanup_info *) png.get_error_ptr(png_ptr);
!     Tcl_AppendResult(info->interp, error_msg, (char *) NULL);
!     longjmp(info->jmpbuf,1);
  }
  
  static void
***************
*** 271,279 ****
      unsigned char buf[8];
  
      if ((ImgRead(handle, (char *) buf, 8) != 8)
! 	    || (strncmp("\211PNG\15\12\32\12", (char *) buf, 8) != 0)
  	    || (ImgRead(handle, (char *) buf, 8) != 8)
! 	    || (strncmp("IHDR", (char *) buf+4, 4) != 0)
  	    || (ImgRead(handle, (char *) buf, 8) != 8)) {
  	return 0;
      }
--- 284,292 ----
      unsigned char buf[8];
  
      if ((ImgRead(handle, (char *) buf, 8) != 8)
! 	    || (strncmp("\211\120\116\107\15\12\32\12", (char *) buf, 8) != 0)
  	    || (ImgRead(handle, (char *) buf, 8) != 8)
! 	    || (strncmp("\111\110\104\122", (char *) buf+4, 4) != 0)
  	    || (ImgRead(handle, (char *) buf, 8) != 8)) {
  	return 0;
      }
***************
*** 286,292 ****
  load_png_library(interp)
      Tcl_Interp *interp;
  {
!     if (ImgLoadLib(interp, PNG_LIB_NAME, &png.handle, symbols, 23)
  	    != TCL_OK) {
  	return TCL_ERROR;
      }
--- 299,305 ----
  load_png_library(interp)
      Tcl_Interp *interp;
  {
!     if (ImgLoadLib(interp, PNG_LIB_NAME, &png.handle, symbols, 22)
  	    != TCL_OK) {
  	return TCL_ERROR;
      }
***************
*** 316,322 ****
      handle.state = IMG_CHAN;
  
      cleanup.interp = interp;
-     cleanup.data = NULL;
  
      png_ptr=png.create_read_struct(PNG_LIBPNG_VER_STRING,
  	    (png_voidp) &cleanup,tk_png_error,tk_png_warning);
--- 329,334 ----
***************
*** 347,353 ****
      }
  
      cleanup.interp = interp;
-     cleanup.data = NULL;
  
      png_ptr=png.create_read_struct(PNG_LIBPNG_VER_STRING,
  	    (png_voidp) &cleanup,tk_png_error,tk_png_warning);
--- 359,364 ----
***************
*** 399,405 ****
  	return(TCL_ERROR);
      }
  
!     if (setjmp(*(jmp_buf *) png_ptr)) {
  	png.destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  	return TCL_ERROR;
      }
--- 410,419 ----
  	return(TCL_ERROR);
      }
  
!     if (setjmp((((cleanup_info *) png.get_error_ptr(png_ptr))->jmpbuf))) {
! 	if (png_data) {
! 	    ckfree((char *)png_data);
! 	}
  	png.destroy_read_struct(&png_ptr, &info_ptr, &end_info);
  	return TCL_ERROR;
      }
***************
*** 470,476 ****
      png_data= (char **) ckalloc(sizeof(char *) * info_height +
  	    info_height * block.pitch);
  
-     ((cleanup_info *) png.get_error_ptr(png_ptr))->data = png_data;
      for(I=0;I<info_height;I++) {
  	png_data[I]= ((char *) png_data) + (sizeof(char *) * info_height +
  		I * block.pitch);
--- 484,489 ----
***************
*** 482,490 ****
      ImgPhotoPutBlock(imageHandle,&block,destX,destY,width,height);
  
      ckfree((char *) png_data);
-     ((cleanup_info *) png.get_error_ptr(png_ptr))->data = NULL;
-     png_data=NULL;
- 
      png.destroy_read_struct(&png_ptr,&info_ptr,&end_info);
  
      return(TCL_OK);
--- 495,500 ----
***************
*** 496,544 ****
      Tcl_Obj *format;
      Tk_PhotoImageBlock *blockPtr;
  {
-     FILE *outfile = NULL;
      png_structp png_ptr;
      png_infop info_ptr;
!     Tcl_DString nameBuffer; 
!     char *fullname;
      int result;
      cleanup_info cleanup;
  
!     if ((fullname=Tcl_TranslateFileName(interp,filename,&nameBuffer))==NULL) {
  	return TCL_ERROR;
      }
  
!     if (!(outfile=fopen(fullname,"wb"))) {
! 	Tcl_AppendResult(interp, filename, ": ", Tcl_PosixError(interp),
! 		(char *)NULL);
! 	Tcl_DStringFree(&nameBuffer);
! 	return TCL_ERROR;
!     }
! 
!     Tcl_DStringFree(&nameBuffer);
  
      if (load_png_library(interp) != TCL_OK) {
  	return TCL_ERROR;
      }
  
      cleanup.interp = interp;
-     cleanup.data = (char **) NULL;
  
      png_ptr=png.create_write_struct(PNG_LIBPNG_VER_STRING,
  	    (png_voidp) &cleanup,tk_png_error,tk_png_warning);
!     if (!png_ptr) return TCL_ERROR; 
  
      info_ptr=png.create_info_struct(png_ptr);
      if (!info_ptr) {
  	png.destroy_write_struct(&png_ptr,NULL);
! 	fclose(outfile);
  	return TCL_ERROR;
      }
  
!     png.init_io(png_ptr,outfile);
  
      result = CommonWritePNG(interp, png_ptr, info_ptr, format, blockPtr);
!     fclose(outfile);
      return result;
  }
  
--- 506,551 ----
      Tcl_Obj *format;
      Tk_PhotoImageBlock *blockPtr;
  {
      png_structp png_ptr;
      png_infop info_ptr;
!     MFile handle;
      int result;
      cleanup_info cleanup;
+     Tcl_Channel chan = (Tcl_Channel) NULL;
  
!     chan = ImgOpenFileChannel(interp, filename, 0644);
!     if (!chan) {
  	return TCL_ERROR;
      }
  
!     handle.data = (char *) chan;
!     handle.state = IMG_CHAN;
  
      if (load_png_library(interp) != TCL_OK) {
+ 	Tcl_Close(NULL, chan);
  	return TCL_ERROR;
      }
  
      cleanup.interp = interp;
  
      png_ptr=png.create_write_struct(PNG_LIBPNG_VER_STRING,
  	    (png_voidp) &cleanup,tk_png_error,tk_png_warning);
!     if (!png_ptr) {
! 	Tcl_Close(NULL, chan);
! 	return TCL_ERROR;
!     }
  
      info_ptr=png.create_info_struct(png_ptr);
      if (!info_ptr) {
  	png.destroy_write_struct(&png_ptr,NULL);
! 	Tcl_Close(NULL, chan);
  	return TCL_ERROR;
      }
  
!     png.set_write_fn(png_ptr,(png_voidp) &handle, tk_png_write, (png_voidp) NULL);
  
      result = CommonWritePNG(interp, png_ptr, info_ptr, format, blockPtr);
!     Tcl_Close(NULL, chan);
      return result;
  }
  
***************
*** 562,571 ****
      }
  
      cleanup.interp = interp;
-     cleanup.data = (char **) NULL;
  
      png_ptr=png.create_write_struct(PNG_LIBPNG_VER_STRING,
! 	    (png_voidp) &cleanup,tk_png_error,tk_png_warning);
      if (!png_ptr) {
  	return TCL_ERROR;
      }
--- 569,577 ----
      }
  
      cleanup.interp = interp;
  
      png_ptr=png.create_write_struct(PNG_LIBPNG_VER_STRING,
! 	    (png_voidp) &cleanup, tk_png_error, tk_png_warning);
      if (!png_ptr) {
  	return TCL_ERROR;
      }
***************
*** 576,582 ****
  	return TCL_ERROR;
      }
  
!     png.set_write_fn(png_ptr,(png_voidp) &handle, tk_png_write, (png_voidp) NULL);
  
      ImgWriteInit(dataPtr, &handle);
  
--- 582,588 ----
  	return TCL_ERROR;
      }
  
!     png.set_write_fn(png_ptr, (png_voidp) &handle, tk_png_write, (png_voidp) NULL);
  
      ImgWriteInit(dataPtr, &handle);
  
***************
*** 600,616 ****
      Tcl_Obj **tags = (Tcl_Obj **) NULL;
      int I, pass, number_passes, color_type;  
      int newPixelSize;
!     png_bytep row_pointers;
!     png_textp text = (png_textp) NULL;
  
      if (ImgListObjGetElements(interp, format, &tagcount, &tags) != TCL_OK) {
  	return TCL_ERROR;
      }
      tagcount = (tagcount > 1) ? (tagcount/2 - 1) : 0;
  
!     if (setjmp(*(jmp_buf *)png_ptr)) {
! 	if (text) {
! 	    ckfree((char *) text);
  	}
  	png.destroy_write_struct(&png_ptr,&info_ptr);
  	return TCL_ERROR;
--- 606,621 ----
      Tcl_Obj **tags = (Tcl_Obj **) NULL;
      int I, pass, number_passes, color_type;  
      int newPixelSize;
!     png_bytep row_pointers = (png_bytep) NULL;
  
      if (ImgListObjGetElements(interp, format, &tagcount, &tags) != TCL_OK) {
  	return TCL_ERROR;
      }
      tagcount = (tagcount > 1) ? (tagcount/2 - 1) : 0;
  
!     if (setjmp((((cleanup_info *) png.get_error_ptr(png_ptr))->jmpbuf))) {
! 	if (row_pointers) {
! 	    ckfree((char *) row_pointers);
  	}
  	png.destroy_write_struct(&png_ptr,&info_ptr);
  	return TCL_ERROR;
***************
*** 661,677 ****
      }
  
      if (tagcount > 0) {
! 	png_text text;
  	for(I=0;I<tagcount;I++) {
  	    int length;
- 	    text.compression = 0;
  	    text.key = Tcl_GetStringFromObj(tags[2*I+1], (int *) NULL);
  	    text.text = Tcl_GetStringFromObj(tags[2*I+2], &length);
  	    text.text_length = length;
  	    if (text.text_length>COMPRESS_THRESHOLD) { 
! 		text.compression = -1;
  	    }
! 	    png.set_text(png_ptr, info_ptr, &text, 1);
          }
      }
      png.write_info(png_ptr,info_ptr);
--- 666,684 ----
      }
  
      if (tagcount > 0) {
! 	png_text_compat text;
  	for(I=0;I<tagcount;I++) {
  	    int length;
  	    text.key = Tcl_GetStringFromObj(tags[2*I+1], (int *) NULL);
  	    text.text = Tcl_GetStringFromObj(tags[2*I+2], &length);
  	    text.text_length = length;
  	    if (text.text_length>COMPRESS_THRESHOLD) { 
! 		text.compression = PNG_TEXT_COMPRESSION_zTXt;
! 	    } else {
! 		text.compression = PNG_TEXT_COMPRESSION_NONE;
  	    }
! 	    text.lang = NULL;
! 	    png.set_text(png_ptr, info_ptr, (png_text *) &text, 1);
          }
      }
      png.write_info(png_ptr,info_ptr);
***************
*** 698,716 ****
  	    }
  	}
  	ckfree((char *) row_pointers);
      } else {
  	for (pass = 0; pass < number_passes; pass++) {
  	    for(I=0;I<blockPtr->height;I++) {
! 		row_pointers = (png_bytep) blockPtr->pixelPtr
! 			+ I * blockPtr->pitch + blockPtr->offset[0];
! 		png.write_row(png_ptr, row_pointers);
  	    }
  	}
      }
      png.write_end(png_ptr,NULL);
-     if (text) {
- 	ckfree((char *) text);
-     }
      png.destroy_write_struct(&png_ptr,&info_ptr);
  
      return(TCL_OK);
--- 705,720 ----
  	    }
  	}
  	ckfree((char *) row_pointers);
+ 	row_pointers = NULL;
      } else {
  	for (pass = 0; pass < number_passes; pass++) {
  	    for(I=0;I<blockPtr->height;I++) {
! 		png.write_row(png_ptr, (png_bytep) blockPtr->pixelPtr
! 			+ I * blockPtr->pitch + blockPtr->offset[0]);
  	    }
  	}
      }
      png.write_end(png_ptr,NULL);
      png.destroy_write_struct(&png_ptr,&info_ptr);
  
      return(TCL_OK);