Magellan Linux

Annotation of /trunk/gimp/patches/gimp-2.6.11-png15.patch

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1596 - (hide annotations) (download)
Tue Dec 6 00:38:56 2011 UTC (12 years, 6 months ago) by niro
File size: 38242 byte(s)
-more patches
1 niro 1596 From 0b404f55ca2e8b6ad3967146dbe78c88302cb1bf Mon Sep 17 00:00:00 2001
2     From: Nils Philippsen <nils@redhat.com>
3     Date: Thu, 17 Nov 2011 15:55:46 +0100
4     Subject: [PATCH] patch: libpng
5    
6     Squashed commit of the following:
7    
8     commit 4ffa25b23f392b3b05ba8d6a2eb51e81697527b6
9     Author: Mukund Sivaraman <muks@banu.com>
10     Date: Tue Oct 11 10:22:24 2011 +0530
11    
12     file-png: Remove redundant assignment
13     (cherry picked from commit f40453be3c90b6e9157c5b6f25c278948e5f90b4)
14    
15     commit 2d3ffbe665de920819a8c4dd926f066950274a2e
16     Author: Mukund Sivaraman <muks@banu.com>
17     Date: Tue Oct 11 10:22:00 2011 +0530
18    
19     file-png: Move comment to correct function
20     (cherry picked from commit e0298effdf758f5beb9c01dfe161d33448f4130c)
21    
22     commit 5d0e8a0d51cc3067e9e365425dcecef59234369f
23     Author: Mukund Sivaraman <muks@banu.com>
24     Date: Tue Sep 27 19:22:50 2011 +0530
25    
26     file-png: Add comments about the struct checks
27     (cherry picked from commit 5769acd3db0c726914c2a399e59b71eb4d8a9d7d)
28    
29     commit 1ef55c0057ecf4ac8cd20ef940d819dce7c6810f
30     Author: Mukund Sivaraman <muks@banu.com>
31     Date: Wed Sep 21 17:16:54 2011 +0530
32    
33     file-png: Return correct type of error value
34     (cherry picked from commit ff2b9ee727586b431e17f283825be3444b5ef496)
35    
36     commit 45f1586ff0159e450111264962b082d71595d320
37     Author: Mukund Sivaraman <muks@banu.com>
38     Date: Wed Sep 21 17:08:53 2011 +0530
39    
40     file-png: Check that PNG structs are created properly
41    
42     If there's a version mismatch between compiled version (header) and
43     installed shared libpng library, structs are not created and NULL is
44     returned.
45     (cherry picked from commit 1ac6c7b85c461b29fc84c9ed5446e7ae98102e00)
46    
47     commit 38b07f3759478536c3e0570ab570ebdf43e4f9b7
48     Author: Mukund Sivaraman <muks@banu.com>
49     Date: Fri May 6 17:51:09 2011 +0530
50    
51     file-png: Indent some code
52     (cherry picked from commit 5b999ab3f9c5e1bb5937111bb1d2fb4222381b98)
53    
54     commit 3a6c9bf27d91d42bcd97e508758c622cbb960277
55     Author: Mukund Sivaraman <muks@banu.com>
56     Date: Thu May 5 20:47:53 2011 +0530
57    
58     file-png: Update comment for png_set_IHDR()
59     (cherry picked from commit ae654a7aaea25a1ae445b702f57792562e1d3763)
60    
61     commit f463360d5e1235d98ff1b4a6eb4293d4062c8127
62     Author: Mukund Sivaraman <muks@banu.com>
63     Date: Thu May 5 20:36:45 2011 +0530
64    
65     file-png: Call png_set_*() functions after png_set_IHDR()
66     (cherry picked from commit fbbeb8d629eb78728396025432bb8be2078461fe)
67    
68     commit 76a768f10ad9fa3f50ea55b772f55f76a50b78b0
69     Author: Mukund Sivaraman <muks@banu.com>
70     Date: Thu May 5 20:15:02 2011 +0530
71    
72     file-png: Move setting the comment text after IHDR is set
73     (cherry picked from commit dbecc7b7053e0b8429eb00ab0a732d252c688381)
74    
75     Conflicts:
76    
77     plug-ins/common/file-png.c
78    
79     commit 41b09a75861a7bf401a373ffe2d9e0f9769e81c9
80     Author: Mukund Sivaraman <muks@banu.com>
81     Date: Thu May 5 18:53:11 2011 +0530
82    
83     file-png: Specify the interlace type explicitly
84     (cherry picked from commit 4fa2bbabf9959dc8bebc45fe263e737198c30e99)
85    
86     commit 3d74018f4c1ade9b8f9a06ef8751af940985e476
87     Author: Mukund Sivaraman <muks@banu.com>
88     Date: Tue Apr 26 15:18:27 2011 +0530
89    
90     file-png: Get num_colors properly before passing it on
91     (cherry picked from commit 5b5d88a2f1d9f0e37d07b24148f76a14d0f2789b)
92    
93     commit 3bed4f1a982a3b204adda568b82d2aedaadc3324
94     Author: Mukund Sivaraman <muks@banu.com>
95     Date: Tue Apr 26 08:06:27 2011 +0530
96    
97     file-png: Don't access inside PNG structs directly (contd.)
98     (cherry picked from commit 92684a25e95ce3da7954140b6f88f2cbfe3cd62d)
99    
100     commit e55ab29c628e32d849211be90d1520d7cd5bbd65
101     Author: Mukund Sivaraman <muks@banu.com>
102     Date: Mon Apr 25 20:12:54 2011 +0530
103    
104     file-png: Conditionally declare text_length
105     (cherry picked from commit a1c7606d51541ba0a99f556de90d3f79d1a2ca00)
106    
107     commit 4862b920e0b7df479f42cc4b4c056cc63d4300a1
108     Author: Mukund Sivaraman <muks@banu.com>
109     Date: Mon Apr 25 20:09:15 2011 +0530
110    
111     file-png: Don't access inside PNG structs directly
112     (cherry picked from commit f610aa4375213d556bc6881ce00fecbb67d50aec)
113    
114     commit a97f84e1d5c4ac07802a0cde73bf8a61b85a2bb8
115     Author: Mukund Sivaraman <muks@banu.com>
116     Date: Fri May 6 19:33:20 2011 +0530
117    
118     file-mng: Indent some code
119     (cherry picked from commit 904d3452b9087bcdc6366cf98b4b2d04b1fc6a37)
120    
121     commit d0aeaa0dae65cb3b505ab48bac7b0f48d8a650e8
122     Author: Mukund Sivaraman <muks@banu.com>
123     Date: Fri May 6 16:28:25 2011 +0530
124    
125     file-mng: Call png_set_*() functions after png_set_IHDR()
126     (cherry picked from commit 17c5e114c3df3aeabe5316f7a85da67f21f65961)
127    
128     commit b401448ab1817fcc6e536e4b0f4a5492c8469312
129     Author: Mukund Sivaraman <muks@banu.com>
130     Date: Fri May 6 16:17:56 2011 +0530
131    
132     file-mng: Specify the interlace type explicitly
133     (cherry picked from commit cbc2ffc7c2cd2103c81064d56b135fd66987422c)
134    
135     commit 640d357c3e62238727d6e0612ed543ac82acb8f3
136     Author: Mukund Sivaraman <muks@banu.com>
137     Date: Tue Apr 26 15:19:43 2011 +0530
138    
139     file-mng: Rename more variables
140     (cherry picked from commit 8868a044ea266618e06c984e5ac500db59f0c1cf)
141    
142     commit 067140cf06c241dc7e57e8261072b4ca3715ee4b
143     Author: Mukund Sivaraman <muks@banu.com>
144     Date: Tue Apr 26 15:18:45 2011 +0530
145    
146     file-mng: Don't access inside PNG structs directly
147     (cherry picked from commit c6a60d79c451f9f13825443fc1836f2fbd492c51)
148    
149     commit e6927634b249ff9c8da1f6ce541a2a663b6d637b
150     Author: Mukund Sivaraman <muks@banu.com>
151     Date: Tue Apr 26 15:00:07 2011 +0530
152    
153     file-mng: Rename variables
154     (cherry picked from commit 838ba0b78e6c2a1d60a00da52c89f6bc5ddd5385)
155     ---
156     plug-ins/common/file-mng.c | 158 ++++++++++++++--------
157     plug-ins/common/file-png.c | 324 +++++++++++++++++++++++++++-----------------
158     2 files changed, 304 insertions(+), 178 deletions(-)
159    
160     diff --git a/plug-ins/common/file-mng.c b/plug-ins/common/file-mng.c
161     index 29dd155..8054504 100644
162     --- a/plug-ins/common/file-mng.c
163     +++ b/plug-ins/common/file-mng.c
164     @@ -136,7 +136,6 @@ struct mng_data_t
165     gint32 default_dispose;
166     };
167    
168     -
169     /* Values of the instance of the above struct when the plug-in is
170     * first invoked. */
171    
172     @@ -160,6 +159,21 @@ static struct mng_data_t mng_data =
173     };
174    
175    
176     +/* These are not saved or restored. */
177     +
178     +struct mng_globals_t
179     +{
180     + gboolean has_trns;
181     + png_bytep trans;
182     + int num_trans;
183     + gboolean has_plte;
184     + png_colorp palette;
185     + int num_palette;
186     +};
187     +
188     +static struct mng_globals_t mngg;
189     +
190     +
191     /* The output FILE pointer which is used by libmng;
192     * passed around as user data. */
193     struct mnglib_userdata_t
194     @@ -196,7 +210,8 @@ static gboolean respin_cmap (png_structp png_ptr,
195     png_infop png_info_ptr,
196     guchar *remap,
197     gint32 image_id,
198     - GimpDrawable *drawable);
199     + GimpDrawable *drawable,
200     + int *bit_depth);
201    
202     static gboolean mng_save_image (const gchar *filename,
203     gint32 image_id,
204     @@ -414,6 +429,18 @@ ia_has_transparent_pixels (guchar *pixels,
205     return FALSE;
206     }
207    
208     +static int
209     +get_bit_depth_for_palette (int num_palette)
210     +{
211     + if (num_palette <= 2)
212     + return 1;
213     + else if (num_palette <= 4)
214     + return 2;
215     + else if (num_palette <= 16)
216     + return 4;
217     + else
218     + return 8;
219     +}
220    
221     /* Spins the color map (palette) putting the transparent color at
222     * index 0 if there is space. If there isn't any space, warn the user
223     @@ -422,11 +449,12 @@ ia_has_transparent_pixels (guchar *pixels,
224     */
225    
226     static gboolean
227     -respin_cmap (png_structp png_ptr,
228     - png_infop png_info_ptr,
229     +respin_cmap (png_structp pp,
230     + png_infop info,
231     guchar *remap,
232     gint32 image_id,
233     - GimpDrawable *drawable)
234     + GimpDrawable *drawable,
235     + int *bit_depth)
236     {
237     static guchar trans[] = { 0 };
238     guchar *before;
239     @@ -464,10 +492,13 @@ respin_cmap (png_structp png_ptr,
240    
241     if (transparent != -1)
242     {
243     - png_color palette[256] = { {0, 0, 0} };
244     + static png_color palette[256] = { {0, 0, 0} };
245     gint i;
246    
247     - png_set_tRNS (png_ptr, png_info_ptr, (png_bytep) trans, 1, NULL);
248     + /* Set tRNS chunk values for writing later. */
249     + mngg.has_trns = TRUE;
250     + mngg.trans = trans;
251     + mngg.num_trans = 1;
252    
253     /* Transform all pixels with a value = transparent to
254     * 0 and vice versa to compensate for re-ordering in palette
255     @@ -489,7 +520,12 @@ respin_cmap (png_structp png_ptr,
256     palette[i].blue = before[3 * remap[i] + 2];
257     }
258    
259     - png_set_PLTE (png_ptr, png_info_ptr, (png_colorp) palette, colors);
260     + /* Set PLTE chunk values for writing later. */
261     + mngg.has_plte = TRUE;
262     + mngg.palette = palette;
263     + mngg.num_palette = colors;
264     +
265     + *bit_depth = get_bit_depth_for_palette (colors);
266    
267     return TRUE;
268     }
269     @@ -500,7 +536,10 @@ respin_cmap (png_structp png_ptr,
270     }
271     }
272    
273     - png_set_PLTE (png_ptr, png_info_ptr, (png_colorp) before, colors);
274     + mngg.has_plte = TRUE;
275     + mngg.palette = (png_colorp) before;
276     + mngg.num_palette = colors;
277     + *bit_depth = get_bit_depth_for_palette (colors);
278    
279     return FALSE;
280     }
281     @@ -777,7 +816,6 @@ mng_save_image (const gchar *filename,
282    
283     for (i = (num_layers - 1); i >= 0; i--)
284     {
285     - gint num_colors;
286     GimpImageType layer_drawable_type;
287     GimpDrawable *layer_drawable;
288     gint layer_offset_x, layer_offset_y;
289     @@ -795,8 +833,8 @@ mng_save_image (const gchar *filename,
290     gchar frame_mode;
291     int frame_delay;
292     gchar *temp_file_name;
293     - png_structp png_ptr;
294     - png_infop png_info_ptr;
295     + png_structp pp;
296     + png_infop info;
297     FILE *infile, *outfile;
298     int num_passes;
299     int tile_height;
300     @@ -804,6 +842,8 @@ mng_save_image (const gchar *filename,
301     int pass, j, k, begin, end, num;
302     guchar *fixed;
303     guchar layer_remap[256];
304     + int color_type;
305     + int bit_depth;
306    
307     layer_name = gimp_drawable_get_name (layers[i]);
308     layer_chunks_type = parse_chunks_type_from_layer_name (layer_name);
309     @@ -948,9 +988,9 @@ mng_save_image (const gchar *filename,
310     goto err3;
311     }
312    
313     - png_ptr = png_create_write_struct (PNG_LIBPNG_VER_STRING,
314     + pp = png_create_write_struct (PNG_LIBPNG_VER_STRING,
315     NULL, NULL, NULL);
316     - if (NULL == png_ptr)
317     + if (NULL == pp)
318     {
319     g_warning ("Unable to png_create_write_struct() in mng_save_image()");
320     fclose (outfile);
321     @@ -958,89 +998,97 @@ mng_save_image (const gchar *filename,
322     goto err3;
323     }
324    
325     - png_info_ptr = png_create_info_struct (png_ptr);
326     - if (NULL == png_info_ptr)
327     + info = png_create_info_struct (pp);
328     + if (NULL == info)
329     {
330     g_warning
331     ("Unable to png_create_info_struct() in mng_save_image()");
332     - png_destroy_write_struct (&png_ptr, NULL);
333     + png_destroy_write_struct (&pp, NULL);
334     fclose (outfile);
335     g_unlink (temp_file_name);
336     goto err3;
337     }
338    
339     - if (setjmp (png_ptr->jmpbuf) != 0)
340     + if (setjmp (png_jmpbuf (pp)) != 0)
341     {
342     g_warning ("HRM saving PNG in mng_save_image()");
343     - png_destroy_write_struct (&png_ptr, &png_info_ptr);
344     + png_destroy_write_struct (&pp, &info);
345     fclose (outfile);
346     g_unlink (temp_file_name);
347     goto err3;
348     }
349    
350     - png_init_io (png_ptr, outfile);
351     - png_set_compression_level (png_ptr, mng_data.compression_level);
352     + png_init_io (pp, outfile);
353    
354     - png_info_ptr->width = layer_cols;
355     - png_info_ptr->height = layer_rows;
356     - png_info_ptr->interlace_type = (mng_data.interlaced == 0 ? 0 : 1);
357     - png_info_ptr->bit_depth = 8;
358     + bit_depth = 8;
359    
360     switch (layer_drawable_type)
361     {
362     case GIMP_RGB_IMAGE:
363     - png_info_ptr->color_type = PNG_COLOR_TYPE_RGB;
364     + color_type = PNG_COLOR_TYPE_RGB;
365     break;
366     case GIMP_RGBA_IMAGE:
367     - png_info_ptr->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
368     + color_type = PNG_COLOR_TYPE_RGB_ALPHA;
369     break;
370     case GIMP_GRAY_IMAGE:
371     - png_info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
372     + color_type = PNG_COLOR_TYPE_GRAY;
373     break;
374     case GIMP_GRAYA_IMAGE:
375     - png_info_ptr->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
376     + color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
377     break;
378     case GIMP_INDEXED_IMAGE:
379     - png_info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
380     - png_info_ptr->valid |= PNG_INFO_PLTE;
381     - png_info_ptr->palette =
382     - (png_colorp) gimp_image_get_colormap (image_id, &num_colors);
383     - png_info_ptr->num_palette = num_colors;
384     + color_type = PNG_COLOR_TYPE_PALETTE;
385     + mngg.has_plte = TRUE;
386     + mngg.palette = (png_colorp)
387     + gimp_image_get_colormap (image_id, &mngg.num_palette);
388     + bit_depth = get_bit_depth_for_palette (mngg.num_palette);
389     break;
390     case GIMP_INDEXEDA_IMAGE:
391     - png_info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
392     + color_type = PNG_COLOR_TYPE_PALETTE;
393     layer_has_unique_palette =
394     - respin_cmap (png_ptr, png_info_ptr, layer_remap,
395     - image_id, layer_drawable);
396     + respin_cmap (pp, info, layer_remap,
397     + image_id, layer_drawable,
398     + &bit_depth);
399     break;
400     default:
401     g_warning ("This can't be!\n");
402     - png_destroy_write_struct (&png_ptr, &png_info_ptr);
403     + png_destroy_write_struct (&pp, &info);
404     fclose (outfile);
405     g_unlink (temp_file_name);
406     goto err3;
407     }
408    
409     - if ((png_info_ptr->valid & PNG_INFO_PLTE) == PNG_INFO_PLTE)
410     + /* Note: png_set_IHDR() must be called before any other
411     + png_set_*() functions. */
412     + png_set_IHDR (pp, info, layer_cols, layer_rows,
413     + bit_depth,
414     + color_type,
415     + mng_data.interlaced ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
416     + PNG_COMPRESSION_TYPE_BASE,
417     + PNG_FILTER_TYPE_BASE);
418     +
419     + if (mngg.has_trns)
420     + {
421     + png_set_tRNS (pp, info, mngg.trans, mngg.num_trans, NULL);
422     + }
423     +
424     + if (mngg.has_plte)
425     {
426     - if (png_info_ptr->num_palette <= 2)
427     - png_info_ptr->bit_depth = 1;
428     - else if (png_info_ptr->num_palette <= 4)
429     - png_info_ptr->bit_depth = 2;
430     - else if (png_info_ptr->num_palette <= 16)
431     - png_info_ptr->bit_depth = 4;
432     + png_set_PLTE (pp, info, mngg.palette, mngg.num_palette);
433     }
434    
435     - png_write_info (png_ptr, png_info_ptr);
436     + png_set_compression_level (pp, mng_data.compression_level);
437     +
438     + png_write_info (pp, info);
439    
440     if (mng_data.interlaced != 0)
441     - num_passes = png_set_interlace_handling (png_ptr);
442     + num_passes = png_set_interlace_handling (pp);
443     else
444     num_passes = 1;
445    
446     - if ((png_info_ptr->color_type == PNG_COLOR_TYPE_PALETTE) &&
447     - (png_info_ptr->bit_depth < 8))
448     - png_set_packing (png_ptr);
449     + if ((color_type == PNG_COLOR_TYPE_PALETTE) &&
450     + (bit_depth < 8))
451     + png_set_packing (pp);
452    
453     tile_height = gimp_tile_height ();
454     layer_pixel = g_new (guchar, tile_height * layer_cols * layer_bpp);
455     @@ -1065,7 +1113,7 @@ mng_save_image (const gchar *filename,
456     gimp_pixel_rgn_get_rect (&layer_pixel_rgn, layer_pixel, 0,
457     begin, layer_cols, num);
458    
459     - if ((png_info_ptr->valid & PNG_INFO_tRNS) == PNG_INFO_tRNS)
460     + if (png_get_valid (pp, info, PNG_INFO_tRNS))
461     {
462     for (j = 0; j < num; j++)
463     {
464     @@ -1077,7 +1125,7 @@ mng_save_image (const gchar *filename,
465     }
466     }
467     else
468     - if (((png_info_ptr->valid & PNG_INFO_PLTE) == PNG_INFO_PLTE)
469     + if (png_get_valid (pp, info, PNG_INFO_PLTE)
470     && (layer_bpp == 2))
471     {
472     for (j = 0; j < num; j++)
473     @@ -1089,12 +1137,12 @@ mng_save_image (const gchar *filename,
474     }
475     }
476    
477     - png_write_rows (png_ptr, layer_pixels, num);
478     + png_write_rows (pp, layer_pixels, num);
479     }
480     }
481    
482     - png_write_end (png_ptr, png_info_ptr);
483     - png_destroy_write_struct (&png_ptr, &png_info_ptr);
484     + png_write_end (pp, info);
485     + png_destroy_write_struct (&pp, &info);
486    
487     g_free (layer_pixels);
488     g_free (layer_pixel);
489     diff --git a/plug-ins/common/file-png.c b/plug-ins/common/file-png.c
490     index d42afff..8fa8983 100644
491     --- a/plug-ins/common/file-png.c
492     +++ b/plug-ins/common/file-png.c
493     @@ -106,6 +106,17 @@ typedef struct
494     }
495     PngSaveGui;
496    
497     +/* These are not saved or restored. */
498     +typedef struct
499     +{
500     + gboolean has_trns;
501     + png_bytep trans;
502     + int num_trans;
503     + gboolean has_plte;
504     + png_colorp palette;
505     + int num_palette;
506     +}
507     +PngGlobals;
508    
509     /*
510     * Local functions...
511     @@ -127,7 +138,7 @@ static gboolean save_image (const gchar *filename,
512     gint32 orig_image_ID,
513     GError **error);
514    
515     -static void respin_cmap (png_structp pp,
516     +static int respin_cmap (png_structp pp,
517     png_infop info,
518     guchar *remap,
519     gint32 image_ID,
520     @@ -175,6 +186,7 @@ static const PngSaveVals defaults =
521     };
522    
523     static PngSaveVals pngvals;
524     +static PngGlobals pngg;
525    
526    
527     /*
528     @@ -653,13 +665,25 @@ on_read_error (png_structp png_ptr, png_const_charp error_msg)
529     error_data->drawable->width, num);
530     }
531    
532     - longjmp (png_ptr->jmpbuf, 1);
533     + longjmp (png_jmpbuf (png_ptr), 1);
534     +}
535     +
536     +static int
537     +get_bit_depth_for_palette (int num_palette)
538     +{
539     + if (num_palette <= 2)
540     + return 1;
541     + else if (num_palette <= 4)
542     + return 2;
543     + else if (num_palette <= 16)
544     + return 4;
545     + else
546     + return 8;
547     }
548    
549     /*
550     * 'load_image()' - Load a PNG image into a new image window.
551     */
552     -
553     static gint32
554     load_image (const gchar *filename,
555     gboolean interactive,
556     @@ -695,9 +719,20 @@ load_image (const gchar *filename,
557     gint num_texts;
558    
559     pp = png_create_read_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
560     + if (!pp)
561     + {
562     + /* this could happen if the compile time and run-time libpng
563     + versions do not match. */
564     +
565     + g_set_error (error, 0, 0,
566     + _("Error creating PNG read struct while saving '%s'."),
567     + gimp_filename_to_utf8 (filename));
568     + return -1;
569     + }
570     +
571     info = png_create_info_struct (pp);
572    
573     - if (setjmp (pp->jmpbuf))
574     + if (setjmp (png_jmpbuf (pp)))
575     {
576     g_set_error (error, G_FILE_ERROR, G_FILE_ERROR_FAILED,
577     _("Error while reading '%s'. File corrupted?"),
578     @@ -705,10 +740,6 @@ load_image (const gchar *filename,
579     return image;
580     }
581    
582     - /* initialise image here, thus avoiding compiler warnings */
583     -
584     - image = -1;
585     -
586     /*
587     * Open the file and initialize the PNG read "engine"...
588     */
589     @@ -738,17 +769,19 @@ load_image (const gchar *filename,
590     * Latest attempt, this should be my best yet :)
591     */
592    
593     - if (info->bit_depth == 16)
594     + if (png_get_bit_depth (pp, info) == 16)
595     {
596     png_set_strip_16 (pp);
597     }
598    
599     - if (info->color_type == PNG_COLOR_TYPE_GRAY && info->bit_depth < 8)
600     + if (png_get_color_type (pp, info) == PNG_COLOR_TYPE_GRAY &&
601     + png_get_bit_depth (pp, info) < 8)
602     {
603     png_set_expand (pp);
604     }
605    
606     - if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8)
607     + if (png_get_color_type (pp, info) == PNG_COLOR_TYPE_PALETTE &&
608     + png_get_bit_depth (pp, info) < 8)
609     {
610     png_set_packing (pp);
611     }
612     @@ -757,8 +790,8 @@ load_image (const gchar *filename,
613     * Expand G+tRNS to GA, RGB+tRNS to RGBA
614     */
615    
616     - if (info->color_type != PNG_COLOR_TYPE_PALETTE &&
617     - (info->valid & PNG_INFO_tRNS))
618     + if (png_get_color_type (pp, info) != PNG_COLOR_TYPE_PALETTE &&
619     + png_get_valid (pp, info, PNG_INFO_tRNS))
620     {
621     png_set_expand (pp);
622     }
623     @@ -775,7 +808,7 @@ load_image (const gchar *filename,
624     */
625    
626     if (png_get_valid (pp, info, PNG_INFO_tRNS) &&
627     - info->color_type == PNG_COLOR_TYPE_PALETTE)
628     + png_get_color_type (pp, info) == PNG_COLOR_TYPE_PALETTE)
629     {
630     png_get_tRNS (pp, info, &alpha_ptr, &num, NULL);
631     /* Copy the existing alpha values from the tRNS chunk */
632     @@ -797,7 +830,7 @@ load_image (const gchar *filename,
633    
634     png_read_update_info (pp, info);
635    
636     - switch (info->color_type)
637     + switch (png_get_color_type (pp, info))
638     {
639     case PNG_COLOR_TYPE_RGB: /* RGB */
640     bpp = 3;
641     @@ -836,7 +869,9 @@ load_image (const gchar *filename,
642     return -1;
643     }
644    
645     - image = gimp_image_new (info->width, info->height, image_type);
646     + image = gimp_image_new (png_get_image_width (pp, info),
647     + png_get_image_height (pp, info),
648     + image_type);
649     if (image == -1)
650     {
651     g_set_error (error, 0, 0,
652     @@ -849,7 +884,9 @@ load_image (const gchar *filename,
653     * Create the "background" layer to hold the image...
654     */
655    
656     - layer = gimp_layer_new (image, _("Background"), info->width, info->height,
657     + layer = gimp_layer_new (image, _("Background"),
658     + png_get_image_width (pp, info),
659     + png_get_image_height (pp, info),
660     layer_type, 100, GIMP_NORMAL_MODE);
661     gimp_image_add_layer (image, layer, 0);
662    
663     @@ -883,7 +920,8 @@ load_image (const gchar *filename,
664    
665     gimp_layer_set_offsets (layer, offset_x, offset_y);
666    
667     - if ((abs (offset_x) > info->width) || (abs (offset_y) > info->height))
668     + if ((abs (offset_x) > png_get_image_width (pp, info)) ||
669     + (abs (offset_y) > png_get_image_height (pp, info)))
670     {
671     if (interactive)
672     g_message (_("The PNG file specifies an offset that caused "
673     @@ -938,23 +976,27 @@ load_image (const gchar *filename,
674    
675     empty = 0; /* by default assume no full transparent palette entries */
676    
677     - if (info->color_type & PNG_COLOR_MASK_PALETTE)
678     + if (png_get_color_type (pp, info) & PNG_COLOR_MASK_PALETTE)
679     {
680     + png_colorp palette;
681     + int num_palette;
682     +
683     + png_get_PLTE (pp, info, &palette, &num_palette);
684     if (png_get_valid (pp, info, PNG_INFO_tRNS))
685     {
686     for (empty = 0; empty < 256 && alpha[empty] == 0; ++empty)
687     /* Calculates number of fully transparent "empty" entries */;
688    
689     /* keep at least one entry */
690     - empty = MIN (empty, info->num_palette - 1);
691     + empty = MIN (empty, num_palette - 1);
692    
693     - gimp_image_set_colormap (image, (guchar *) (info->palette + empty),
694     - info->num_palette - empty);
695     + gimp_image_set_colormap (image, (guchar *) (palette + empty),
696     + num_palette - empty);
697     }
698     else
699     {
700     - gimp_image_set_colormap (image, (guchar *) info->palette,
701     - info->num_palette);
702     + gimp_image_set_colormap (image, (guchar *) palette,
703     + num_palette);
704     }
705     }
706    
707     @@ -972,18 +1014,19 @@ load_image (const gchar *filename,
708     */
709    
710     tile_height = gimp_tile_height ();
711     - pixel = g_new0 (guchar, tile_height * info->width * bpp);
712     + pixel = g_new0 (guchar, tile_height * png_get_image_width (pp, info) * bpp);
713     pixels = g_new (guchar *, tile_height);
714    
715     for (i = 0; i < tile_height; i++)
716     - pixels[i] = pixel + info->width * info->channels * i;
717     + pixels[i] = pixel + png_get_image_width (pp, info) *
718     + png_get_channels (pp, info) * i;
719    
720     /* Install our own error handler to handle incomplete PNG files better */
721     error_data.drawable = drawable;
722     error_data.pixel = pixel;
723     error_data.tile_height = tile_height;
724     - error_data.width = info->width;
725     - error_data.height = info->height;
726     + error_data.width = png_get_image_width (pp, info);
727     + error_data.height = png_get_image_height (pp, info);
728     error_data.bpp = bpp;
729     error_data.pixel_rgn = &pixel_rgn;
730    
731     @@ -996,10 +1039,11 @@ load_image (const gchar *filename,
732     */
733    
734     for (begin = 0, end = tile_height;
735     - begin < info->height; begin += tile_height, end += tile_height)
736     + begin < png_get_image_height (pp, info);
737     + begin += tile_height, end += tile_height)
738     {
739     - if (end > info->height)
740     - end = info->height;
741     + if (end > png_get_image_height (pp, info))
742     + end = png_get_image_height (pp, info);
743    
744     num = end - begin;
745    
746     @@ -1016,11 +1060,13 @@ load_image (const gchar *filename,
747     gimp_pixel_rgn_set_rect (&pixel_rgn, pixel, 0, begin,
748     drawable->width, num);
749    
750     - memset (pixel, 0, tile_height * info->width * bpp);
751     + memset (pixel, 0,
752     + tile_height * png_get_image_width (pp, info) * bpp);
753    
754     - gimp_progress_update (((gdouble) pass +
755     - (gdouble) end / (gdouble) info->height) /
756     - (gdouble) num_passes);
757     + gimp_progress_update
758     + (((gdouble) pass +
759     + (gdouble) end / (gdouble) png_get_image_height (pp, info)) /
760     + (gdouble) num_passes);
761     }
762     }
763    
764     @@ -1189,7 +1235,6 @@ save_image (const gchar *filename,
765     GimpPixelRgn pixel_rgn; /* Pixel region for layer */
766     png_structp pp; /* PNG read pointer */
767     png_infop info; /* PNG info pointer */
768     - gint num_colors; /* Number of colors in colormap */
769     gint offx, offy; /* Drawable offsets from origin */
770     guchar **pixels, /* Pixel rows */
771     *fixed, /* Fixed-up pixel data */
772     @@ -1200,56 +1245,28 @@ save_image (const gchar *filename,
773     guchar red, green, blue; /* Used for palette background */
774     time_t cutime; /* Time since epoch */
775     struct tm *gmt; /* GMT broken down */
776     + int color_type;
777     + int bit_depth;
778    
779     guchar remap[256]; /* Re-mapping for the palette */
780    
781     png_textp text = NULL;
782    
783     - if (pngvals.comment)
784     + pp = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
785     + if (!pp)
786     {
787     - GimpParasite *parasite;
788     - gsize text_length = 0;
789     -
790     - parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
791     - if (parasite)
792     - {
793     - gchar *comment = g_strndup (gimp_parasite_data (parasite),
794     - gimp_parasite_data_size (parasite));
795     + /* this could happen if the compile time and run-time libpng
796     + versions do not match. */
797    
798     - gimp_parasite_free (parasite);
799     -
800     - text = g_new0 (png_text, 1);
801     - text->key = "Comment";
802     -
803     -#ifdef PNG_iTXt_SUPPORTED
804     -
805     - text->compression = PNG_ITXT_COMPRESSION_NONE;
806     - text->text = comment;
807     - text->itxt_length = strlen (comment);
808     -
809     -#else
810     -
811     - text->compression = PNG_TEXT_COMPRESSION_NONE;
812     - text->text = g_convert (comment, -1,
813     - "ISO-8859-1", "UTF-8",
814     - NULL, &text_length,
815     - NULL);
816     - text->text_length = text_length;
817     -
818     -#endif
819     -
820     - if (!text->text)
821     - {
822     - g_free (text);
823     - text = NULL;
824     - }
825     - }
826     + g_set_error (error, 0, 0,
827     + _("Error creating PNG write struct while saving '%s'."),
828     + gimp_filename_to_utf8 (filename));
829     + return FALSE;
830     }
831    
832     - pp = png_create_write_struct (PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
833     info = png_create_info_struct (pp);
834    
835     - if (setjmp (pp->jmpbuf))
836     + if (setjmp (png_jmpbuf (pp)))
837     {
838     g_set_error (error, 0, 0,
839     _("Error while saving '%s'. Could not save image."),
840     @@ -1257,9 +1274,6 @@ save_image (const gchar *filename,
841     return FALSE;
842     }
843    
844     - if (text)
845     - png_set_text (pp, info, text, 1);
846     -
847     /*
848     * Open the file and initialize the PNG write "engine"...
849     */
850     @@ -1286,17 +1300,6 @@ save_image (const gchar *filename,
851     type = gimp_drawable_type (drawable_ID);
852    
853     /*
854     - * Set the image dimensions, bit depth, interlacing and compression
855     - */
856     -
857     - png_set_compression_level (pp, pngvals.compression_level);
858     -
859     - info->width = drawable->width;
860     - info->height = drawable->height;
861     - info->bit_depth = 8;
862     - info->interlace_type = pngvals.interlaced;
863     -
864     - /*
865     * Initialise remap[]
866     */
867     for (i = 0; i < 256; i++)
868     @@ -1306,42 +1309,44 @@ save_image (const gchar *filename,
869     * Set color type and remember bytes per pixel count
870     */
871    
872     + bit_depth = 8;
873     +
874     switch (type)
875     {
876     case GIMP_RGB_IMAGE:
877     - info->color_type = PNG_COLOR_TYPE_RGB;
878     + color_type = PNG_COLOR_TYPE_RGB;
879     bpp = 3;
880     break;
881    
882     case GIMP_RGBA_IMAGE:
883     - info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
884     + color_type = PNG_COLOR_TYPE_RGB_ALPHA;
885     bpp = 4;
886     break;
887    
888     case GIMP_GRAY_IMAGE:
889     - info->color_type = PNG_COLOR_TYPE_GRAY;
890     + color_type = PNG_COLOR_TYPE_GRAY;
891     bpp = 1;
892     break;
893    
894     case GIMP_GRAYA_IMAGE:
895     - info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
896     + color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
897     bpp = 2;
898     break;
899    
900     case GIMP_INDEXED_IMAGE:
901     bpp = 1;
902     - info->color_type = PNG_COLOR_TYPE_PALETTE;
903     - info->valid |= PNG_INFO_PLTE;
904     - info->palette =
905     - (png_colorp) gimp_image_get_colormap (image_ID, &num_colors);
906     - info->num_palette = num_colors;
907     + color_type = PNG_COLOR_TYPE_PALETTE;
908     + pngg.has_plte = TRUE;
909     + pngg.palette = (png_colorp) gimp_image_get_colormap (image_ID,
910     + &pngg.num_palette);
911     + bit_depth = get_bit_depth_for_palette (pngg.num_palette);
912     break;
913    
914     case GIMP_INDEXEDA_IMAGE:
915     bpp = 2;
916     - info->color_type = PNG_COLOR_TYPE_PALETTE;
917     + color_type = PNG_COLOR_TYPE_PALETTE;
918     /* fix up transparency */
919     - respin_cmap (pp, info, remap, image_ID, drawable);
920     + bit_depth = respin_cmap (pp, info, remap, image_ID, drawable);
921     break;
922    
923     default:
924     @@ -1349,21 +1354,29 @@ save_image (const gchar *filename,
925     return FALSE;
926     }
927    
928     - /*
929     - * Fix bit depths for (possibly) smaller colormap images
930     - */
931     + /* Note: png_set_IHDR() must be called before any other png_set_*()
932     + functions. */
933     + png_set_IHDR (pp, info, drawable->width, drawable->height,
934     + bit_depth,
935     + color_type,
936     + pngvals.interlaced ? PNG_INTERLACE_ADAM7 : PNG_INTERLACE_NONE,
937     + PNG_COMPRESSION_TYPE_BASE,
938     + PNG_FILTER_TYPE_BASE);
939     +
940     + if (pngg.has_trns)
941     + {
942     + png_set_tRNS (pp, info, pngg.trans, pngg.num_trans, NULL);
943     + }
944    
945     - if (info->valid & PNG_INFO_PLTE)
946     + if (pngg.has_plte)
947     {
948     - if (info->num_palette <= 2)
949     - info->bit_depth = 1;
950     - else if (info->num_palette <= 4)
951     - info->bit_depth = 2;
952     - else if (info->num_palette <= 16)
953     - info->bit_depth = 4;
954     - /* otherwise the default is fine */
955     + png_set_PLTE (pp, info, pngg.palette, pngg.num_palette);
956     }
957    
958     + /* Set the compression level */
959     +
960     + png_set_compression_level (pp, pngvals.compression_level);
961     +
962     /* All this stuff is optional extras, if the user is aiming for smallest
963     possible file size she can turn them all off */
964    
965     @@ -1462,6 +1475,52 @@ save_image (const gchar *filename,
966     }
967     #endif
968    
969     + if (pngvals.comment)
970     + {
971     + GimpParasite *parasite;
972     +#ifndef PNG_iTXt_SUPPORTED
973     + gsize text_length = 0;
974     +#endif /* PNG_iTXt_SUPPORTED */
975     +
976     + parasite = gimp_image_parasite_find (orig_image_ID, "gimp-comment");
977     + if (parasite)
978     + {
979     + gchar *comment = g_strndup (gimp_parasite_data (parasite),
980     + gimp_parasite_data_size (parasite));
981     +
982     + gimp_parasite_free (parasite);
983     +
984     + text = g_new0 (png_text, 1);
985     + text->key = "Comment";
986     +
987     +#ifdef PNG_iTXt_SUPPORTED
988     +
989     + text->compression = PNG_ITXT_COMPRESSION_NONE;
990     + text->text = comment;
991     + text->itxt_length = strlen (comment);
992     +
993     +#else
994     +
995     + text->compression = PNG_TEXT_COMPRESSION_NONE;
996     + text->text = g_convert (comment, -1,
997     + "ISO-8859-1", "UTF-8",
998     + NULL, &text_length,
999     + NULL);
1000     + text->text_length = text_length;
1001     +
1002     +#endif
1003     +
1004     + if (!text->text)
1005     + {
1006     + g_free (text);
1007     + text = NULL;
1008     + }
1009     + }
1010     + }
1011     +
1012     + if (text)
1013     + png_set_text (pp, info, text, 1);
1014     +
1015     png_write_info (pp, info);
1016    
1017     /*
1018     @@ -1477,7 +1536,8 @@ save_image (const gchar *filename,
1019     * Convert unpacked pixels to packed if necessary
1020     */
1021    
1022     - if (info->color_type == PNG_COLOR_TYPE_PALETTE && info->bit_depth < 8)
1023     + if (color_type == PNG_COLOR_TYPE_PALETTE &&
1024     + bit_depth < 8)
1025     png_set_packing (pp);
1026    
1027     /*
1028     @@ -1507,7 +1567,9 @@ save_image (const gchar *filename,
1029    
1030     gimp_pixel_rgn_get_rect (&pixel_rgn, pixel, 0, begin,
1031     drawable->width, num);
1032     - /*if we are with a RGBA image and have to pre-multiply the alpha channel */
1033     +
1034     + /* If we are with a RGBA image and have to pre-multiply the
1035     + alpha channel */
1036     if (bpp == 4 && ! pngvals.save_transp_pixels)
1037     {
1038     for (i = 0; i < num; ++i)
1039     @@ -1529,7 +1591,7 @@ save_image (const gchar *filename,
1040    
1041     /* If we're dealing with a paletted image with
1042     * transparency set, write out the remapped palette */
1043     - if (info->valid & PNG_INFO_tRNS)
1044     + if (png_get_valid (pp, info, PNG_INFO_tRNS))
1045     {
1046     guchar inverse_remap[256];
1047    
1048     @@ -1547,9 +1609,11 @@ save_image (const gchar *filename,
1049     }
1050     }
1051     }
1052     +
1053     /* Otherwise if we have a paletted image and transparency
1054     * couldn't be set, we ignore the alpha channel */
1055     - else if (info->valid & PNG_INFO_PLTE && bpp == 2)
1056     + else if (png_get_valid (pp, info, PNG_INFO_PLTE) &&
1057     + bpp == 2)
1058     {
1059     for (i = 0; i < num; ++i)
1060     {
1061     @@ -1564,7 +1628,7 @@ save_image (const gchar *filename,
1062     png_write_rows (pp, pixels, num);
1063    
1064     gimp_progress_update (((double) pass + (double) end /
1065     - (double) info->height) /
1066     + (double) drawable->height) /
1067     (double) num_passes);
1068     }
1069     }
1070     @@ -1694,14 +1758,14 @@ find_unused_ia_color (GimpDrawable *drawable,
1071     }
1072    
1073    
1074     -static void
1075     +static int
1076     respin_cmap (png_structp pp,
1077     png_infop info,
1078     guchar *remap,
1079     gint32 image_ID,
1080     GimpDrawable *drawable)
1081     {
1082     - static const guchar trans[] = { 0 };
1083     + static guchar trans[] = { 0 };
1084    
1085     gint colors;
1086     guchar *before;
1087     @@ -1728,10 +1792,13 @@ respin_cmap (png_structp pp,
1088     * index - do like gif2png and swap
1089     * index 0 and index transparent */
1090     {
1091     - png_color palette[256];
1092     + static png_color palette[256];
1093     gint i;
1094    
1095     - png_set_tRNS (pp, info, (png_bytep) trans, 1, NULL);
1096     + /* Set tRNS chunk values for writing later. */
1097     + pngg.has_trns = TRUE;
1098     + pngg.trans = trans;
1099     + pngg.num_trans = 1;
1100    
1101     /* Transform all pixels with a value = transparent to
1102     * 0 and vice versa to compensate for re-ordering in palette
1103     @@ -1752,7 +1819,10 @@ respin_cmap (png_structp pp,
1104     palette[i].blue = before[3 * remap[i] + 2];
1105     }
1106    
1107     - png_set_PLTE (pp, info, palette, colors);
1108     + /* Set PLTE chunk values for writing later. */
1109     + pngg.has_plte = TRUE;
1110     + pngg.palette = palette;
1111     + pngg.num_palette = colors;
1112     }
1113     else
1114     {
1115     @@ -1760,14 +1830,22 @@ respin_cmap (png_structp pp,
1116     * transparency & just use the full palette */
1117     g_message (_("Couldn't losslessly save transparency, "
1118     "saving opacity instead."));
1119     - png_set_PLTE (pp, info, (png_colorp) before, colors);
1120     +
1121     + /* Set PLTE chunk values for writing later. */
1122     + pngg.has_plte = TRUE;
1123     + pngg.palette = (png_colorp) before;
1124     + pngg.num_palette = colors;
1125     }
1126     }
1127     else
1128     {
1129     - png_set_PLTE (pp, info, (png_colorp) before, colors);
1130     + /* Set PLTE chunk values for writing later. */
1131     + pngg.has_plte = TRUE;
1132     + pngg.palette = (png_colorp) before;
1133     + pngg.num_palette = colors;
1134     }
1135    
1136     + return get_bit_depth_for_palette (colors);
1137     }
1138    
1139     static gboolean
1140     --
1141     1.7.7.1
1142