1 From e76a8a537dbf8c47024f5863f9f18ed55ad686d3 Mon Sep 17 00:00:00 2001
2 From: michal-n <michal-n@users.sourceforge.net>
3 Date: Sat, 17 Dec 2011 15:24:12 +0100
4 Subject: [PATCH] There was no need to panic...
5 The patch from Paul contained errant reference to 'png_color16'.
6 The correct form is 'png_color_16'. So, patch reapplied.
9 gutils/gimagewritepng.c | 100 +++++++++++++++++++++++-----------------------
10 1 files changed, 50 insertions(+), 50 deletions(-)
12 diff --git a/gutils/gimagewritepng.c b/gutils/gimagewritepng.c
13 index 43fa097..a88ec63 100644
14 --- a/gutils/gimagewritepng.c
15 +++ b/gutils/gimagewritepng.c
16 @@ -236,6 +236,7 @@ return(false);
19 if ( base->trans!=-1 ) {
20 + trans_color = galloc(sizeof(png_color_16));
21 trans_color->red = COLOR_RED(base->trans);
22 trans_color->green = COLOR_GREEN(base->trans);
23 trans_color->blue = COLOR_BLUE(base->trans);
24 @@ -258,6 +259,7 @@ return(false);
25 _png_write_end(png_ptr, info_ptr);
27 if ( trans_alpha!=NULL ) gfree(trans_alpha);
28 + if ( trans_color!=NULL ) gfree(trans_color);
29 if ( palette!=NULL ) gfree(palette);
30 _png_destroy_write_struct(&png_ptr, &info_ptr);
32 @@ -296,7 +298,7 @@ static void user_error_fn(png_structp png_ptr, png_const_charp error_msg) {
33 #if (PNG_LIBPNG_VER < 10500)
34 longjmp(png_ptr->jmpbuf,1);
36 - _png_longjmp (png_ptr, 1);
37 + png_longjmp (png_ptr, 1);
41 @@ -310,6 +312,12 @@ int GImageWrite_Png(GImage *gi, FILE *fp, int progressive) {
48 + png_bytep trans_alpha = NULL;
49 + png_color_16p trans_color = NULL;
50 + png_colorp palette = NULL;
52 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
53 (void *)NULL, user_error_fn, user_warning_fn);
54 @@ -336,65 +344,60 @@ return(false);
56 png_init_io(png_ptr, fp);
58 - info_ptr->width = base->width;
59 - info_ptr->height = base->height;
60 - info_ptr->bit_depth = 8;
61 - info_ptr->valid = 0;
62 - info_ptr->interlace_type = progressive;
63 - if ( base->trans!=-1 ) {
64 - info_ptr->num_trans = 1;
65 - info_ptr->valid |= PNG_INFO_tRNS;
67 + num_palette = base->clut==NULL?2:base->clut->clut_len;
68 + if ( base->image_type==it_index || base->image_type==it_bitmap ) {
69 + color_type = PNG_COLOR_TYPE_PALETTE;
70 + if ( num_palette<=2 )
72 + else if ( num_palette<=4 )
74 + else if ( num_palette<=16 )
77 + color_type = PNG_COLOR_TYPE_RGB;
78 + if ( base->image_type == it_rgba )
79 + color_type = PNG_COLOR_TYPE_RGB_ALPHA;
82 + png_set_IHDR(png_ptr, info_ptr, base->width, base->height,
83 + bit_depth, color_type, progressive,
84 + PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
85 if ( base->image_type==it_index || base->image_type==it_bitmap ) {
86 - info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
87 - info_ptr->valid |= PNG_INFO_PLTE;
88 - info_ptr->num_palette = base->clut==NULL?2:base->clut->clut_len;
89 - info_ptr->palette = (png_color *) galloc(info_ptr->num_palette*sizeof(png_color));
90 + palette = (png_color *) galloc(num_palette*sizeof(png_color));
91 if ( base->clut==NULL ) {
92 - info_ptr->palette[0].red = info_ptr->palette[0].green = info_ptr->palette[0].blue = 0;
93 - info_ptr->palette[1].red = info_ptr->palette[1].green = info_ptr->palette[1].blue = 0xff;
94 + palette[0].red = palette[0].green = palette[0].blue = 0;
95 + palette[1].red = palette[1].green = palette[1].blue = 0xff;
97 - for ( i=0; i<info_ptr->num_palette; ++i ) {
98 + for ( i=0; i<num_palette; ++i ) {
99 long col = base->clut->clut[i];
100 - info_ptr->palette[i].red = COLOR_RED(col);
101 - info_ptr->palette[i].green = COLOR_GREEN(col);
102 - info_ptr->palette[i].blue = COLOR_BLUE(col);
103 + palette[i].red = COLOR_RED(col);
104 + palette[i].green = COLOR_GREEN(col);
105 + palette[i].blue = COLOR_BLUE(col);
108 - if ( info_ptr->num_palette<=2 )
109 - info_ptr->bit_depth=1;
110 - else if ( info_ptr->num_palette<=4 )
111 - info_ptr->bit_depth=2;
112 - else if ( info_ptr->num_palette<=16 )
113 - info_ptr->bit_depth=4;
114 - if ( info_ptr->num_palette<=16 )
115 + png_set_PLTE(png_ptr, info_ptr, palette, num_palette);
116 + if ( num_palette<=16 )
117 png_set_packing(png_ptr);
119 if ( base->trans!=-1 ) {
120 -#if ( PNG_LIBPNG_VER_MAJOR > 1 || PNG_LIBPNG_VER_MINOR > 2 )
121 - info_ptr->trans_alpha = galloc(1);
122 - info_ptr->trans_alpha[0] = base->trans;
124 - info_ptr->trans = galloc(1);
125 - info_ptr->trans[0] = base->trans;
127 + trans_alpha = galloc(1);
128 + trans_alpha[0] = base->trans;
131 - info_ptr->color_type = PNG_COLOR_TYPE_RGB;
132 if ( base->trans!=-1 ) {
133 -#if ( PNG_LIBPNG_VER_MAJOR > 1 || PNG_LIBPNG_VER_MINOR > 2 )
134 - info_ptr->trans_color.red = COLOR_RED(base->trans);
135 - info_ptr->trans_color.green = COLOR_GREEN(base->trans);
136 - info_ptr->trans_color.blue = COLOR_BLUE(base->trans);
138 - info_ptr->trans_values.red = COLOR_RED(base->trans);
139 - info_ptr->trans_values.green = COLOR_GREEN(base->trans);
140 - info_ptr->trans_values.blue = COLOR_BLUE(base->trans);
142 + trans_color = galloc(sizeof(png_color_16));
143 + trans_color->red = COLOR_RED(base->trans);
144 + trans_color->green = COLOR_GREEN(base->trans);
145 + trans_color->blue = COLOR_BLUE(base->trans);
148 + if ( base->trans!=-1 ) {
149 + png_set_tRNS(png_ptr, info_ptr, trans_alpha, 1, trans_color);
151 png_write_info(png_ptr, info_ptr);
153 - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB)
154 + if (color_type == PNG_COLOR_TYPE_RGB)
155 png_set_filler(png_ptr, '\0', PNG_FILLER_BEFORE);
157 rows = galloc(base->height*sizeof(png_byte *));
158 @@ -405,12 +408,9 @@ return(false);
160 png_write_end(png_ptr, info_ptr);
162 -#if ( PNG_LIBPNG_VER_MAJOR > 1 || PNG_LIBPNG_VER_MINOR > 2 )
163 - if ( info_ptr->trans_alpha!=NULL ) gfree(info_ptr->trans_alpha);
165 - if ( info_ptr->trans!=NULL ) gfree(info_ptr->trans);
167 - if ( info_ptr->palette!=NULL ) gfree(info_ptr->palette);
168 + if ( trans_alpha!=NULL ) gfree(trans_alpha);
169 + if ( trans_color!=NULL ) gfree(trans_color);
170 + if ( palette!=NULL ) gfree(palette);
171 png_destroy_write_struct(&png_ptr, &info_ptr);