1 http://pkgbuild.com/~ioni/libpng-1.5/amsn-0.98.4-libpng15.patch
3 --- utils/TkCximage/src/CxImage/ximapng.cpp
4 +++ utils/TkCximage/src/CxImage/ximapng.cpp
6 void CxImagePNG::ima_png_error(png_struct *png_ptr, char *message)
8 strcpy(info.szLastError,message);
9 - longjmp(png_ptr->jmpbuf, 1);
10 + longjmp(png_jmpbuf(png_ptr), 1);
12 ////////////////////////////////////////////////////////////////////////////////
13 #if CXIMAGE_SUPPORT_DECODE
15 /* Set error handling if you are using the setjmp/longjmp method (this is
16 * the normal method of doing things with libpng). REQUIRED unless you
17 * set up your own error handlers in the png_create_read_struct() earlier. */
18 - if (setjmp(png_ptr->jmpbuf)) {
19 + if (setjmp(png_jmpbuf(png_ptr))) {
20 /* Free all of the memory associated with the png_ptr and info_ptr */
21 delete [] row_pointers;
22 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
24 png_read_info(png_ptr, info_ptr);
26 if (info.nEscape == -1){
27 - head.biWidth = info_ptr->width;
28 - head.biHeight= info_ptr->height;
29 + head.biWidth = png_get_image_width(png_ptr, info_ptr);
30 + head.biHeight= png_get_image_height(png_ptr, info_ptr);
31 info.dwType = CXIMAGE_FORMAT_PNG;
32 - longjmp(png_ptr->jmpbuf, 1);
33 + longjmp(png_jmpbuf(png_ptr), 1);
36 /* calculate new number of channels */
38 - switch(info_ptr->color_type){
39 + switch(png_get_color_type(png_ptr, info_ptr)){
40 case PNG_COLOR_TYPE_GRAY:
41 case PNG_COLOR_TYPE_PALETTE:
46 strcpy(info.szLastError,"unknown PNG color type");
47 - longjmp(png_ptr->jmpbuf, 1);
48 + longjmp(png_jmpbuf(png_ptr), 1);
51 //find the right pixel depth used for cximage
52 - int pixel_depth = info_ptr->pixel_depth;
54 + int pixel_depth = png_get_pixel_depth(png_ptr, info_ptr);
55 if (channels == 1 && pixel_depth>8) pixel_depth=8;
58 + if (channels == 1) pixel_depth=8;
59 if (channels == 2) pixel_depth=8;
60 if (channels >= 3) pixel_depth=24;
62 - if (!Create(info_ptr->width, info_ptr->height, pixel_depth, CXIMAGE_FORMAT_PNG)){
63 - longjmp(png_ptr->jmpbuf, 1);
64 + if (!Create(png_get_image_width(png_ptr, info_ptr), png_get_image_height(png_ptr, info_ptr), pixel_depth, CXIMAGE_FORMAT_PNG)){
65 + longjmp(png_jmpbuf(png_ptr), 1);
69 - switch (info_ptr->phys_unit_type)
70 + png_uint_32 res_x, res_y;
72 + png_get_pHYs_dpi(png_ptr, info_ptr, &res_x, &res_y, &unit_type);
75 case PNG_RESOLUTION_UNKNOWN:
76 - SetXDPI(info_ptr->x_pixels_per_unit);
77 - SetYDPI(info_ptr->y_pixels_per_unit);
81 case PNG_RESOLUTION_METER:
82 - SetXDPI((long)floor(info_ptr->x_pixels_per_unit * 254.0 / 10000.0 + 0.5));
83 - SetYDPI((long)floor(info_ptr->y_pixels_per_unit * 254.0 / 10000.0 + 0.5));
84 + SetXDPI((long)floor(res_x * 254.0 / 10000.0 + 0.5));
85 + SetYDPI((long)floor(res_y * 254.0 / 10000.0 + 0.5));
89 - if (info_ptr->num_palette>0){
90 - SetPalette((rgb_color*)info_ptr->palette,info_ptr->num_palette);
91 - SetClrImportant(info_ptr->num_palette);
92 - } else if (info_ptr->bit_depth ==2) { //<DP> needed for 2 bpp grayscale PNGs
95 + png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette);
97 + SetPalette((rgb_color*)palette,num_palette);
98 + SetClrImportant(num_palette);
99 + } else if (png_get_bit_depth(png_ptr, info_ptr) ==2) { //<DP> needed for 2 bpp grayscale PNGs
100 SetPaletteColor(0,0,0,0);
101 SetPaletteColor(1,85,85,85);
102 SetPaletteColor(2,170,170,170);
103 SetPaletteColor(3,255,255,255);
104 } else SetGrayPalette(); //<DP> needed for grayscale PNGs
106 - int nshift = max(0,(info_ptr->bit_depth>>3)-1)<<3;
107 + int nshift = max(0,(png_get_bit_depth(png_ptr, info_ptr)>>3)-1)<<3;
109 - if (info_ptr->num_trans!=0){ //palette transparency
110 - if (info_ptr->num_trans==1){
111 - if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE){
112 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
113 - info.nBkgndIndex = info_ptr->trans_color.index;
115 - info.nBkgndIndex = info_ptr->trans_values.index;
117 +png_uint_32 png_get_tRNS (png_const_structp png_ptr, png_infop
118 +info_ptr, png_bytep *trans_alpha, int *num_trans, png_color_16p
120 + png_bytep trans_alpha;
122 + png_color_16p trans_color;
123 + png_get_tRNS(png_ptr, info_ptr, &trans_alpha, &num_trans, &trans_color);
124 + if (num_trans!=0){ //palette transparency
126 + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_PALETTE){
127 + info.nBkgndIndex = trans_color->index;
129 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
130 - info.nBkgndIndex = info_ptr->trans_color.gray>>nshift;
132 - info.nBkgndIndex = info_ptr->trans_values.gray>>nshift;
134 + info.nBkgndIndex = trans_color->gray>>nshift;
137 - if (info_ptr->num_trans>1){
139 RGBQUAD* pal=GetPalette();
142 - for (ip=0;ip<min(head.biClrUsed,(unsigned long)info_ptr->num_trans);ip++)
143 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
144 - pal[ip].rgbReserved=info_ptr->trans_alpha[ip];
146 - pal[ip].rgbReserved=info_ptr->trans[ip];
148 - for (ip=info_ptr->num_trans;ip<head.biClrUsed;ip++){
149 + for (ip=0;ip<min(head.biClrUsed,(unsigned long)num_trans);ip++)
150 + pal[ip].rgbReserved=trans_alpha[ip];
151 + for (ip=num_trans;ip<head.biClrUsed;ip++){
152 pal[ip].rgbReserved=255;
154 info.bAlphaPaletteEnabled=true;
157 png_color_16 *image_background;
158 if (png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &image_background)){
159 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
160 - info.nBkgndColor.rgbRed = (BYTE)(info_ptr->trans_color.red>>nshift);
161 - info.nBkgndColor.rgbGreen = (BYTE)(info_ptr->trans_color.green>>nshift);
162 - info.nBkgndColor.rgbBlue = (BYTE)(info_ptr->trans_color.blue>>nshift);
164 - info.nBkgndColor.rgbRed = (BYTE)(info_ptr->trans_values.red>>nshift);
165 - info.nBkgndColor.rgbGreen = (BYTE)(info_ptr->trans_values.green>>nshift);
166 - info.nBkgndColor.rgbBlue = (BYTE)(info_ptr->trans_values.blue>>nshift);
168 + info.nBkgndColor.rgbRed = (BYTE)(trans_color->red>>nshift);
169 + info.nBkgndColor.rgbGreen = (BYTE)(trans_color->green>>nshift);
170 + info.nBkgndColor.rgbBlue = (BYTE)(trans_color->blue>>nshift);
171 info.nBkgndColor.rgbReserved = 0;
172 info.nBkgndIndex = 0;
174 @@ -202,15 +201,15 @@
177 // <vho> - flip the RGB pixels to BGR (or RGBA to BGRA)
178 - if (info_ptr->color_type & PNG_COLOR_MASK_COLOR){
179 + if (png_get_color_type(png_ptr, info_ptr) & PNG_COLOR_MASK_COLOR){
180 png_set_bgr(png_ptr);
183 // <vho> - handle cancel
184 - if (info.nEscape) longjmp(png_ptr->jmpbuf, 1);
185 + if (info.nEscape) longjmp(png_jmpbuf(png_ptr), 1);
187 // row_bytes is the width x number of channels x (bit-depth / 8)
188 - row_pointers = new BYTE[info_ptr->rowbytes + 8];
189 + row_pointers = new BYTE[png_get_rowbytes(png_ptr, info_ptr) + 8];
191 // turn on interlace handling
192 int number_passes = png_set_interlace_handling(png_ptr);
197 - int chan_offset = info_ptr->bit_depth >> 3;
198 - int pixel_offset = info_ptr->pixel_depth >> 3;
199 + int bit_depth = png_get_bit_depth(png_ptr, info_ptr);
200 + int chan_offset = bit_depth >> 3;
201 + //int pixel_offset = info_ptr->pixel_depth >> 3;
202 + int pixel_offset = (bit_depth * channels) >> 3;
204 for (int pass=0; pass < number_passes; pass++) {
209 // <vho> - handle cancel
210 - if (info.nEscape) longjmp(png_ptr->jmpbuf, 1);
211 + if (info.nEscape) longjmp(png_jmpbuf(png_ptr), 1);
213 #if CXIMAGE_SUPPORT_ALPHA // <vho>
214 if (AlphaIsValid()) {
216 BYTE* prow= iter.GetRow(ay);
218 //recover data from previous scan
219 - if (info_ptr->interlace_type && pass>0 && pass!=7){
220 + if (png_get_interlace_type(png_ptr, info_ptr) && pass>0 && pass!=7){
221 for(ax=0;ax<head.biWidth;ax++){
222 long px = ax * pixel_offset;
224 @@ -278,10 +279,10 @@
225 #endif // CXIMAGE_SUPPORT_ALPHA // vho
227 //recover data from previous scan
228 - if (info_ptr->interlace_type && pass>0){
229 - iter.GetRow(row_pointers, info_ptr->rowbytes);
230 + if (png_get_interlace_type(png_ptr, info_ptr) && pass>0){
231 + iter.GetRow(row_pointers, png_get_rowbytes(png_ptr, info_ptr));
232 //re-expand buffer for images with bit depth > 8
233 - if (info_ptr->bit_depth > 8){
234 + if (png_get_bit_depth(png_ptr, info_ptr) > 8){
235 for(long ax=(head.biWidth*channels-1);ax>=0;ax--)
236 row_pointers[ax*chan_offset] = row_pointers[ax];
238 @@ -291,15 +292,15 @@
239 png_read_row(png_ptr, row_pointers, NULL);
241 //shrink 16 bit depth images down to 8 bits
242 - if (info_ptr->bit_depth > 8){
243 + if (png_get_bit_depth(png_ptr, info_ptr) > 8){
244 for(long ax=0;ax<(head.biWidth*channels);ax++)
245 row_pointers[ax] = row_pointers[ax*chan_offset];
249 - iter.SetRow(row_pointers, info_ptr->rowbytes);
250 + iter.SetRow(row_pointers, png_get_rowbytes(png_ptr, info_ptr));
251 //<DP> expand 2 bpp images only in the last pass
252 - if (info_ptr->bit_depth==2 && pass==(number_passes-1))
253 + if (png_get_bit_depth(png_ptr, info_ptr)==2 && pass==(number_passes-1))
254 expand2to4bpp(iter.GetRow());
258 BYTE trans[256]; //for transparency (don't move)
261 + png_uint_32 width, height;
262 + png_byte bit_depth;
263 + png_byte color_type;
264 + png_byte interlace_type;
270 /* Set error handling. REQUIRED if you aren't supplying your own
271 * error hadnling functions in the png_create_write_struct() call.
273 - if (setjmp(png_ptr->jmpbuf)){
274 + if (setjmp(png_jmpbuf(png_ptr))){
275 /* If we get here, we had a problem reading the file */
276 - if (info_ptr->palette) free(info_ptr->palette);
277 png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr);
278 cx_throw("Error saving PNG file");
280 @@ -376,20 +381,18 @@
281 png_set_write_fn(png_ptr,hFile,/*(png_rw_ptr)*/user_write_data,/*(png_flush_ptr)*/user_flush_data);
283 /* set the file information here */
284 - info_ptr->width = GetWidth();
285 - info_ptr->height = GetHeight();
286 - info_ptr->pixel_depth = (BYTE)GetBpp();
287 - info_ptr->channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1;
288 - info_ptr->bit_depth = (BYTE)(GetBpp()/info_ptr->channels);
289 - info_ptr->compression_type = info_ptr->filter_type = 0;
290 - info_ptr->valid = 0;
291 + width = GetWidth();
292 + height = GetHeight();
293 + //pixel_depth = (BYTE)GetBpp();
294 + channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1;
295 + bit_depth = (BYTE)(GetBpp()/channels);
297 switch(GetCodecOption(CXIMAGE_FORMAT_PNG)){
299 - info_ptr->interlace_type = PNG_INTERLACE_ADAM7;
300 + interlace_type = PNG_INTERLACE_ADAM7;
303 - info_ptr->interlace_type = PNG_INTERLACE_NONE;
304 + interlace_type = PNG_INTERLACE_NONE;
307 /* set compression level */
308 @@ -399,19 +402,19 @@
312 - info_ptr->color_type = PNG_COLOR_TYPE_GRAY;
313 + color_type = PNG_COLOR_TYPE_GRAY;
315 - info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;
316 + color_type = PNG_COLOR_TYPE_PALETTE;
319 - info_ptr->color_type = PNG_COLOR_TYPE_RGB;
320 + color_type = PNG_COLOR_TYPE_RGB;
322 #if CXIMAGE_SUPPORT_ALPHA
324 - info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;
325 - info_ptr->channels++;
326 - info_ptr->bit_depth = 8;
327 - info_ptr->pixel_depth += 8;
328 + color_type |= PNG_COLOR_MASK_ALPHA;
331 + //pixel_depth += 8;
335 @@ -428,29 +431,21 @@
337 png_set_pHYs(png_ptr, info_ptr, head.biXPelsPerMeter, head.biYPelsPerMeter, PNG_RESOLUTION_METER);
339 - png_set_IHDR(png_ptr, info_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth,
340 - info_ptr->color_type, info_ptr->interlace_type,
341 + png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth,
342 + color_type, interlace_type,
343 PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
345 //<DP> simple transparency
346 if (info.nBkgndIndex >= 0){
347 - info_ptr->num_trans = 1;
348 - info_ptr->valid |= PNG_INFO_tRNS;
349 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
350 - info_ptr->trans_alpha = trans;
351 - info_ptr->trans_color.index = (BYTE)info.nBkgndIndex;
352 - info_ptr->trans_color.red = tc.rgbRed;
353 - info_ptr->trans_color.green = tc.rgbGreen;
354 - info_ptr->trans_color.blue = tc.rgbBlue;
355 - info_ptr->trans_color.gray = info_ptr->trans_color.index;
357 - info_ptr->trans = trans;
358 - info_ptr->trans_values.index = (BYTE)info.nBkgndIndex;
359 - info_ptr->trans_values.red = tc.rgbRed;
360 - info_ptr->trans_values.green = tc.rgbGreen;
361 - info_ptr->trans_values.blue = tc.rgbBlue;
362 - info_ptr->trans_values.gray = info_ptr->trans_values.index;
364 + png_color_16 trans_color;
366 + trans_color.index = (BYTE)info.nBkgndIndex;
367 + trans_color.red = tc.rgbRed;
368 + trans_color.green = tc.rgbGreen;
369 + trans_color.blue = tc.rgbBlue;
370 + trans_color.gray = (BYTE)info.nBkgndIndex;
372 + png_set_tRNS(png_ptr, info_ptr, trans, 1, &trans_color);
374 // the transparency indexes start from 0 for non grayscale palette
375 if (!bGrayScale && head.biClrUsed && info.nBkgndIndex)
376 @@ -459,30 +454,27 @@
378 /* set the palette if there is one */
381 - info_ptr->valid |= PNG_INFO_PLTE;
384 int nc = GetClrImportant();
385 if (nc==0) nc = GetNumColors();
387 if (info.bAlphaPaletteEnabled){
388 for(WORD ip=0; ip<nc;ip++)
389 trans[ip]=GetPaletteColor((BYTE)ip).rgbReserved;
390 - info_ptr->num_trans = (WORD)nc;
391 - info_ptr->valid |= PNG_INFO_tRNS;
392 -#if PNG_LIBPNG_VER_MAJOR >= 1 && PNG_LIBPNG_VER_MINOR >= 4
393 - info_ptr->trans_alpha = trans;
395 - info_ptr->trans = trans;
397 + png_set_tRNS(png_ptr, info_ptr, trans, (WORD)nc, NULL);
400 // copy the palette colors
401 - info_ptr->palette = new png_color[nc];
402 - info_ptr->num_palette = (png_uint_16) nc;
403 - for (int i=0; i<nc; i++)
404 - GetPaletteColor(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue);
406 + png_color *palette;
408 + palette = (png_color *)png_malloc(png_ptr, nc*sizeof(palette[0]));
409 + for (int i=0; i<nc; i++)
410 + GetPaletteColor(i, &palette[i].red, &palette[i].green, &palette[i].blue);
411 + png_set_PLTE(png_ptr, info_ptr, palette, nc);
412 + png_data_freer(png_ptr, info_ptr,
413 + PNG_DESTROY_WILL_FREE_DATA,
418 #if CXIMAGE_SUPPORT_ALPHA // <vho>
421 #endif // CXIMAGE_SUPPORT_ALPHA // <vho>
423 - int row_size = max(info.dwEffWidth, info_ptr->width*info_ptr->channels*(info_ptr->bit_depth/8));
424 - info_ptr->rowbytes = row_size;
425 + int row_size = max(info.dwEffWidth, png_get_rowbytes(png_ptr, info_ptr));
426 + //info_ptr->rowbytes = row_size;
427 BYTE *row_pointers = new BYTE[row_size];
429 /* write the file information */
432 for (long ax=head.biWidth-1; ax>=0;ax--){
433 c = BlindGetPixelColor(ax,ay);
434 - int px = ax * info_ptr->channels;
435 + int px = ax * png_get_channels(png_ptr, info_ptr);
437 row_pointers[px++]=c.rgbRed;
438 row_pointers[px++]=c.rgbGreen;
440 #endif //CXIMAGE_SUPPORT_ALPHA // <vho>
442 iter.GetRow(row_pointers, row_size);
443 - if (info_ptr->color_type == PNG_COLOR_TYPE_RGB) //HACK BY OP
444 + if (png_get_color_type(png_ptr, info_ptr) == PNG_COLOR_TYPE_RGB) //HACK BY OP
445 RGBtoBGR(row_pointers, row_size);
446 png_write_row(png_ptr, row_pointers);
449 /* It is REQUIRED to call this to finish writing the rest of the file */
450 png_write_end(png_ptr, info_ptr);
452 - /* if you malloced the palette, free it here */
453 - if (info_ptr->palette){
454 - delete [] (info_ptr->palette);
455 - info_ptr->palette = NULL;
458 /* clean up after the write, and free any memory allocated */
459 png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr);
461 --- utils/TkCximage/src/CxImage/ximapng.h
462 +++ utils/TkCximage/src/CxImage/ximapng.h
465 static void PNGAPI user_error_fn(png_structp png_ptr,png_const_charp error_msg)
467 - strncpy((char*)png_ptr->error_ptr,error_msg,255);
468 - longjmp(png_ptr->jmpbuf, 1);
469 + strncpy((char*)png_get_error_ptr(png_ptr),error_msg,255);
470 + longjmp(png_jmpbuf(png_ptr), 1);