summaryrefslogtreecommitdiff
path: root/extra/cinepaint/cinepaint-libpng15.patch
blob: e22edafaee92b60267818c9d85af1fa119f360b6 (plain)
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
diff -Naur cinepaint.old/plug-ins/png/png.c cinepaint/plug-ins/png/png.c
--- cinepaint.old/plug-ins/png/png.c	2012-01-23 12:39:42.000000000 -0500
+++ cinepaint/plug-ins/png/png.c	2012-01-23 12:39:50.000000000 -0500
@@ -390,6 +390,16 @@
   gchar		*progress;	/* Title for progress display... */
   guchar	alpha[256],	/* Index -> Alpha */
   		*alpha_ptr;	/* Temporary pointer */
+  png_byte     color_type;
+  png_byte     bit_depth;
+  png_colorp palette;
+  int num_palette;
+  png_charpp iccp_name;
+  int compression_type;
+  png_bytepp iccp_profile;
+  png_uint_32 iccp_proflen;
+  png_uint_32 width, height;
+  png_byte channels;
 
  /*
   * PNG 0.89 and newer have a sane, forwards compatible constructor.
@@ -405,7 +415,7 @@
   info = (png_infop)calloc(sizeof(png_info), 1);
 #endif /* PNG_LIBPNG_VER > 88 */
 
-  if (setjmp (pp->jmpbuf))
+  if (setjmp (png_jmpbuf(pp)))
   {
     g_message ("%s\nPNG error. File corrupted?", filename);
     return image;
@@ -442,21 +452,26 @@
   */
 
   png_read_info(pp, info);
+  color_type = png_get_color_type(pp, info);
+  width=png_get_image_width(pp, info);
+  height=png_get_image_height(pp, info);
 
  /*
   * Latest attempt, this should be my best yet :)
   */
 
+  bit_depth = png_get_bit_depth(pp, info);
+
 #ifndef WORDS_BIGENDIAN
-  if(info->bit_depth == 16)
+  if(bit_depth == 16)
 	  png_set_swap(pp);
 #endif
 
-  if (info->color_type == PNG_COLOR_TYPE_GRAY && info->bit_depth < 8) {
+  if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) {
     png_set_expand(pp);
   }
 
-  if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8) {
+  if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth < 8) {
     png_set_packing(pp);
   }
 
@@ -464,8 +479,8 @@
   * Expand G+tRNS to GA, RGB+tRNS to RGBA
   */
 
-  if (info->color_type != PNG_COLOR_TYPE_PALETTE &&
-                       (info->valid & PNG_INFO_tRNS)) {
+  if (color_type != PNG_COLOR_TYPE_PALETTE &&
+      (png_get_valid(pp, info, PNG_INFO_tRNS))) {
     png_set_expand(pp);
   }
 
@@ -482,7 +497,7 @@
 
 #if PNG_LIBPNG_VER > 99
   if (png_get_valid(pp, info, PNG_INFO_tRNS) &&
-      info->color_type == PNG_COLOR_TYPE_PALETTE)
+      color_type == PNG_COLOR_TYPE_PALETTE)
   {
     png_get_tRNS(pp, info, &alpha_ptr, &num, NULL);
     /* Copy the existing alpha values from the tRNS chunk */
@@ -505,9 +520,9 @@
 
   png_read_update_info(pp, info);
   
-  if(info->bit_depth==16)
+  if(bit_depth==16)
   {
-	  switch (info->color_type)
+	  switch (color_type)
 	  {
 		  case PNG_COLOR_TYPE_RGB :		/* RGB */
 			  bpp        = 6;
@@ -545,7 +560,7 @@
   }
   else
   {
-	  switch (info->color_type)
+	  switch (color_type)
 	  {
 		  case PNG_COLOR_TYPE_RGB :		/* RGB */
 			  bpp        = 3;
@@ -582,7 +597,7 @@
 	  };
   }
 
-  image = gimp_image_new(info->width, info->height, image_type);
+  image = gimp_image_new(width, height, image_type);
   if (image == -1)
   {
     g_message("Can't allocate new image\n%s", filename);
@@ -595,7 +610,7 @@
   * Create the "background" layer to hold the image...
   */
 
-  layer = gimp_layer_new(image, _("Background"), info->width, info->height,
+  layer = gimp_layer_new(image, _("Background"), width, height,
                          layer_type, 100, NORMAL_MODE);
   gimp_image_add_layer(image, layer, 0);
 
@@ -627,20 +642,21 @@
 
   empty= 0; /* by default assume no full transparent palette entries */
 
-  if (info->color_type & PNG_COLOR_MASK_PALETTE) {
+  if (color_type & PNG_COLOR_MASK_PALETTE) {
 
 #if PNG_LIBPNG_VER > 99
+    png_get_PLTE(pp, info, &palette, &num_palette);
     if (png_get_valid(pp, info, PNG_INFO_tRNS)) {
       for (empty= 0; empty < 256 && alpha[empty] == 0; ++empty);
         /* Calculates number of fully transparent "empty" entries */
 
-      gimp_image_set_cmap(image, (guchar *) (info->palette + empty),
-                          info->num_palette - empty);
+      gimp_image_set_cmap(image, (guchar *) (palette + empty),
+                          num_palette - empty);
     } else {
-      gimp_image_set_cmap(image, (guchar *)info->palette, info->num_palette);
+      gimp_image_set_cmap(image, (guchar *)palette, num_palette);
     }
 #else
-    gimp_image_set_cmap(image, (guchar *)info->palette, info->num_palette);
+    gimp_image_set_cmap(image, (guchar *)palette, num_palette);
 #endif /* PNG_LIBPNG_VER > 99 */
 
   }
@@ -659,18 +675,19 @@
   */
 
   tile_height = gimp_tile_height ();
-  pixel       = g_new(guchar, tile_height * info->width * bpp);
+  pixel       = g_new(guchar, tile_height * width * bpp);
   pixels      = g_new(guchar *, tile_height);
+  channels=png_get_channels(pp, info);
 
-  if(info->bit_depth==16)
+  if(bit_depth==16)
   {
 	  for (i = 0; i < tile_height; i ++)
-		  pixels[i] = pixel + info->width * info->channels * i * 2;
+		  pixels[i] = pixel + width * channels * i * 2;
   }
   else
   {
 	  for (i = 0; i < tile_height; i ++)
-		  pixels[i] = pixel + info->width * info->channels * i;
+		  pixels[i] = pixel + width * channels * i;
   }
 
   for (pass = 0; pass < num_passes; pass ++)
@@ -680,11 +697,11 @@
 	   */
 
 	  for (begin = 0, end = tile_height;
-			  begin < info->height;
+			  begin < height;
 			  begin += tile_height, end += tile_height)
 	  {
-		  if (end > info->height)
-			  end = info->height;
+		  if (end > height)
+			  end = height;
 
 		  num = end - begin;
 
@@ -697,20 +714,21 @@
 		  gimp_pixel_rgn_set_rect(&pixel_rgn, pixel, 0, begin,
 				  drawable->width, num);
 
-		  gimp_progress_update(((double)pass + (double)end / (double)info->height) /
+		  gimp_progress_update(((double)pass + (double)end / (double)height) /
 				  (double)num_passes);
 	  };
   };
 
 #if defined(PNG_iCCP_SUPPORTED)
   /* set icc profile */
-  if (info->iccp_proflen > 0) {
-    gimp_image_set_icc_profile_by_mem (image, info->iccp_proflen,
-                                              info->iccp_profile,
-                                              ICC_IMAGE_PROFILE);
+  png_get_iCCP(pp, info,  (png_charpp)iccp_name, &compression_type, iccp_profile, &iccp_proflen);
+  if (iccp_proflen > 0) {
+    gimp_image_set_icc_profile_by_mem (image, iccp_proflen,
+				       (gchar *)iccp_profile,
+				       ICC_IMAGE_PROFILE);
     printf ("%s:%d %s() set embedded profile \"%s\"\n",
              __FILE__,__LINE__,__func__,
-                                              info->iccp_name);
+	    (char *)iccp_name);
   }
 #endif
 
@@ -808,6 +826,14 @@
                 blue;           /* Used for palette background */
   time_t	cutime;         /* Time since epoch */
   struct tm	*gmt;		/* GMT broken down */
+  png_byte     color_type;
+  png_byte     bit_depth;
+  int filter_method=0;
+  png_colorp palette;
+  int num_palette;
+  png_charpp iccp_name;
+  png_uint_32 height;
+  int compression_type;
 
  /*
   * PNG 0.89 and newer have a sane, forwards compatible constructor.
@@ -824,7 +849,7 @@
   info = (png_infop)calloc(sizeof(png_info), 1);
 #endif /* PNG_LIBPNG_VER > 88 */
 
-  if (setjmp (pp->jmpbuf))
+  if (setjmp (png_jmpbuf(pp)))
   {
     g_message ("%s\nPNG error. Couldn't save image", filename);
     return 0;
@@ -857,15 +882,6 @@
   drawable = gimp_drawable_get (drawable_ID);
   type     = gimp_drawable_type (drawable_ID);
 
- /*
-  * Set the image dimensions, bit depth, interlacing and compression
-  */
-
-  png_set_compression_level (pp, pngvals.compression_level);
-
-  info->width          = drawable->width;
-  info->height         = drawable->height;
-  info->interlace_type = pngvals.interlaced;
 
  /*
   * Set color type and remember bytes per pixel count 
@@ -874,71 +890,69 @@
   switch (type)
   {
     case RGB_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_RGB;
-	info->bit_depth      = 8;
+        color_type = PNG_COLOR_TYPE_RGB;
+	bit_depth      = 8;
         bpp              = 3;
         break;
     case RGBA_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-	info->bit_depth      = 8;
+        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+	bit_depth      = 8;
         bpp              = 4;
         break;
     case GRAY_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_GRAY;
-	info->bit_depth      = 8;
+        color_type = PNG_COLOR_TYPE_GRAY;
+	bit_depth      = 8;
         bpp              = 1;
         break;
     case GRAYA_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
-	info->bit_depth      = 8;
+        color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+	bit_depth      = 8;
         bpp              = 2;
         break;
     case INDEXED_IMAGE :
 	bpp		 = 1;
-	info->bit_depth      = 8;
-        info->color_type = PNG_COLOR_TYPE_PALETTE;
-	info->valid      |= PNG_INFO_PLTE;
-        info->palette= (png_colorp) gimp_image_get_cmap(image_ID, &num_colors);
-        info->num_palette= num_colors;
+	bit_depth      = 8;
+        color_type = PNG_COLOR_TYPE_PALETTE;
+	png_get_valid(pp, info, PNG_INFO_PLTE);
+	png_set_PLTE(pp, info, (png_colorp) gimp_image_get_cmap(image_ID, &num_colors), num_colors);
         break;
     case INDEXEDA_IMAGE :
 	bpp		 = 2;
-	info->bit_depth      = 8;
-	info->color_type = PNG_COLOR_TYPE_PALETTE;
+	bit_depth      = 8;
+	color_type = PNG_COLOR_TYPE_PALETTE;
 	respin_cmap (pp, info, image_ID); /* fix up transparency */
 	break;
     case U16_RGB_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_RGB;
-	info->bit_depth      = 16;
+        color_type = PNG_COLOR_TYPE_RGB;
+	bit_depth      = 16;
         bpp              = 6;
         break;
     case U16_RGBA_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
-	info->bit_depth      = 16;
+        color_type = PNG_COLOR_TYPE_RGB_ALPHA;
+	bit_depth      = 16;
         bpp              = 8;
         break;
     case U16_GRAY_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_GRAY;
-	info->bit_depth      = 16;
+        color_type = PNG_COLOR_TYPE_GRAY;
+	bit_depth      = 16;
         bpp              = 2;
         break;
     case U16_GRAYA_IMAGE :
-        info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
-	info->bit_depth      = 16;
+        color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
+	bit_depth      = 16;
         bpp              = 4;
         break;
     case U16_INDEXED_IMAGE :
 	bpp		 = 2;
-	info->bit_depth      = 16;
-        info->color_type = PNG_COLOR_TYPE_PALETTE;
-	info->valid      |= PNG_INFO_PLTE;
-        info->palette= (png_colorp) gimp_image_get_cmap(image_ID, &num_colors);
-        info->num_palette= num_colors;
+	bit_depth      = 16;
+        color_type = PNG_COLOR_TYPE_PALETTE;
+	png_get_valid(pp, info, PNG_INFO_PLTE);
+	png_set_PLTE(pp, info, (png_colorp) gimp_image_get_cmap(image_ID, &num_colors), num_colors);
         break;
     case U16_INDEXEDA_IMAGE :
 	bpp		 = 4;
-	info->bit_depth      = 16;
-	info->color_type = PNG_COLOR_TYPE_PALETTE;
+	bit_depth      = 16;
+	color_type = PNG_COLOR_TYPE_PALETTE;
 	respin_cmap (pp, info, image_ID); /* fix up transparency */
 	break;
     default:
@@ -950,30 +964,36 @@
   * Fix bit depths for (possibly) smaller colormap images
   */
   
-  if (info->valid & PNG_INFO_PLTE) {
-    if (info->num_palette <= 2)
-      info->bit_depth= 1;
-    else if (info->num_palette <= 4)
-      info->bit_depth= 2;
-    else if (info->num_palette <= 16)
-      info->bit_depth= 4;
+  if (png_get_valid(pp, info, PNG_INFO_PLTE)) {
+    png_get_PLTE(pp, info, &palette, &num_palette);
+    if (num_palette <= 2)
+      bit_depth= 1;
+    else if (num_palette <= 4)
+      bit_depth= 2;
+    else if (num_palette <= 16)
+      bit_depth= 4;
     /* otherwise the default is fine */
   }
 
+ png_set_compression_level (pp, pngvals.compression_level);
+ png_set_IHDR(pp, info, drawable->width, drawable->height,
+	      bit_depth, color_type, pngvals.interlaced, compression_type, filter_method);
+
+
   // write icc profile
 #if defined(PNG_iCCP_SUPPORTED)
   if (gimp_image_has_icc_profile (image_ID, ICC_IMAGE_PROFILE)) {
-    int size;
+    int  size;
     char *buffer;
     
     buffer = gimp_image_get_icc_profile_by_mem (image_ID, &size,
                                                 ICC_IMAGE_PROFILE);
     png_set_iCCP (pp, info,
            gimp_image_get_icc_profile_description (image_ID, ICC_IMAGE_PROFILE),
-                  0, buffer, size);
+                  0, (png_const_bytep)buffer, size);
     printf ("%s:%d %s() embedd icc profile \"%s\"\n",
              __FILE__,__LINE__,__func__,
-                                              info->iccp_name);
+	    (char *)iccp_name);
   }
 #endif
 
@@ -1039,13 +1059,13 @@
   * Convert unpacked pixels to packed if necessary
   */
 
-  if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8)
+  if (color_type == PNG_COLOR_TYPE_PALETTE && bit_depth < 8)
     png_set_packing(pp);
 
   /* Set swapping for 16 bit per sample images */
   
 #ifndef WORDS_BIGENDIAN
-  if (info->bit_depth == 16)
+  if (bit_depth == 16)
 	  png_set_swap(pp);
 #endif
   
@@ -1063,6 +1083,7 @@
 
   gimp_pixel_rgn_init(&pixel_rgn, drawable, 0, 0, drawable->width,
                       drawable->height, FALSE, FALSE);
+  height=png_get_image_height(pp, info);
 
   for (pass = 0; pass < num_passes; pass ++)
   {
@@ -1077,7 +1098,7 @@
 	num = end - begin;
 	
 	gimp_pixel_rgn_get_rect (&pixel_rgn, pixel, 0, begin, drawable->width, num);
-        if (info->valid & PNG_INFO_tRNS) {
+        if (png_get_valid(pp, info, PNG_INFO_tRNS)) {
           for (i = 0; i < num; ++i) {
 	    fixed= pixels[i];
             for (k = 0; k < drawable->width; ++k) {
@@ -1085,7 +1106,7 @@
             }
           }
        /* Forgot this case before, what if there are too many colors? */
-        } else if (info->valid & PNG_INFO_PLTE && bpp == 2) {
+        } else if (png_get_valid(pp, info, PNG_INFO_PLTE) && bpp == 2) {
           for (i = 0; i < num; ++i) {
 	    fixed= pixels[i];
             for (k = 0; k < drawable->width; ++k) {
@@ -1095,9 +1116,8 @@
         }
 	
 	png_write_rows (pp, pixels, num);
-	
 	gimp_progress_update (((double)pass + (double)end /
-                    (double)info->height) / (double)num_passes);
+                    (double)height) / (double)num_passes);
       };
   };