--- /dev/null
+fix building with newer libpng. patch by Ian Stakenvicius.
+
+https://bugs.gentoo.org/380127
+
+--- a/xbmc/lib/cximage-6.0/CxImage/ximapng.h
++++ b/xbmc/lib/cximage-6.0/CxImage/ximapng.h
+@@ -69,8 +69,13 @@
+ \r
+ static void PNGAPI user_error_fn(png_structp png_ptr,png_const_charp error_msg)\r
+ {\r
++#if PNG_LIBPNG_VER > 10399\r
++ strncpy((char*)png_get_error_ptr(png_ptr),error_msg,255);\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
+ strncpy((char*)png_ptr->error_ptr,error_msg,255);\r
+ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ }\r
+ };\r
+ \r
+--- a/xbmc/lib/cximage-6.0/CxImage/ximapng.cpp
++++ b/xbmc/lib/cximage-6.0/CxImage/ximapng.cpp
+@@ -15,7 +15,11 @@
+ void CxImagePNG::ima_png_error(png_struct *png_ptr, char *message)\r
+ {\r
+ strcpy(info.szLastError,message);\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
+ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ }\r
+ ////////////////////////////////////////////////////////////////////////////////\r
+ #if CXIMAGE_SUPPORT_DECODE\r
+@@ -62,7 +66,11 @@
+ /* Set error handling if you are using the setjmp/longjmp method (this is\r
+ * the normal method of doing things with libpng). REQUIRED unless you\r
+ * set up your own error handlers in the png_create_read_struct() earlier. */\r
++#if PNG_LIBPNG_VER > 10399\r
++ if (setjmp(png_jmpbuf(png_ptr))) {\r
++#else\r
+ if (setjmp(png_ptr->jmpbuf)) {\r
++#endif\r
+ /* Free all of the memory associated with the png_ptr and info_ptr */\r
+ delete [] row_pointers;\r
+ png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);\r
+@@ -70,16 +78,35 @@
+ /* read the file information */\r
+ png_read_info(png_ptr, info_ptr);\r
+ \r
++ png_uint_32 _width,_height;\r
++ int _bit_depth,_color_type,_interlace_type,_compression_type,_filter_type;\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_get_IHDR(png_ptr,info_ptr,&_width,&_height,&_bit_depth,&_color_type,\r
++ &_interlace_type,&_compression_type,&_filter_type);\r
++#else\r
++ _width=info_ptr->width;\r
++ _height=info_ptr->height;\r
++ _bit_depth=info_ptr->bit_depth;\r
++ _color_type=info_ptr->color_type;\r
++ _interlace_type=info_ptr->interlace_type;\r
++ _compression_type=info_ptr->compression_type;\r
++ _filter_type=info_ptr->filter_type;\r
++#endif\r
++\r
+ if (info.nEscape == -1){\r
+- head.biWidth = info_ptr->width;\r
+- head.biHeight= info_ptr->height;\r
++ head.biWidth = _width;\r
++ head.biHeight= _height;\r
+ info.dwType = CXIMAGE_FORMAT_PNG;\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
+ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ }\r
+ \r
+ /* calculate new number of channels */\r
+ int channels=0;\r
+- switch(info_ptr->color_type){\r
++ switch(_color_type){\r
+ case PNG_COLOR_TYPE_GRAY:\r
+ case PNG_COLOR_TYPE_PALETTE:\r
+ channels = 1;\r
+@@ -101,71 +128,108 @@
+ break;\r
+ default:\r
+ strcpy(info.szLastError,"unknown PNG color type");\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
+ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ }\r
+ \r
+ //find the right pixel depth used for cximage\r
++#if PNG_LIBPNG_VER > 10399\r
++ int pixel_depth = _bit_depth * png_get_channels(png_ptr,info_ptr);\r
++#else\r
+ int pixel_depth = info_ptr->pixel_depth;\r
++#endif\r
+ if (channels == 1 && pixel_depth>8) pixel_depth=8;\r
+ if (channels == 2) pixel_depth=8;\r
+ if (channels >= 3) pixel_depth=24;\r
+ \r
+- if (!Create(info_ptr->width, info_ptr->height, pixel_depth, CXIMAGE_FORMAT_PNG)){\r
++ if (!Create(_width, _height, pixel_depth, CXIMAGE_FORMAT_PNG)){\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
+ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ }\r
+ \r
+ /* get metrics */\r
+- switch (info_ptr->phys_unit_type)\r
++ png_uint_32 _x_pixels_per_unit,_y_pixels_per_unit;\r
++ int _phys_unit_type;\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_get_pHYs(png_ptr,info_ptr,&_x_pixels_per_unit,&_y_pixels_per_unit,&_phys_unit_type);\r
++#else\r
++ _x_pixels_per_unit=info_ptr->x_pixels_per_unit;\r
++ _y_pixels_per_unit=info_ptr->y_pixels_per_unit;\r
++ _phys_unit_type=info_ptr->phys_unit_type;\r
++#endif\r
++ switch (_phys_unit_type)\r
+ {\r
+ case PNG_RESOLUTION_UNKNOWN:\r
+- SetXDPI(info_ptr->x_pixels_per_unit);\r
+- SetYDPI(info_ptr->y_pixels_per_unit);\r
++ SetXDPI(_x_pixels_per_unit);\r
++ SetYDPI(_y_pixels_per_unit);\r
+ break;\r
+ case PNG_RESOLUTION_METER:\r
+- SetXDPI((long)floor(info_ptr->x_pixels_per_unit * 254.0 / 10000.0 + 0.5));\r
+- SetYDPI((long)floor(info_ptr->y_pixels_per_unit * 254.0 / 10000.0 + 0.5));\r
++ SetXDPI((long)floor(_x_pixels_per_unit * 254.0 / 10000.0 + 0.5));\r
++ SetYDPI((long)floor(_y_pixels_per_unit * 254.0 / 10000.0 + 0.5));\r
+ break;\r
+ }\r
+ \r
+- if (info_ptr->num_palette>0){\r
+- SetPalette((rgb_color*)info_ptr->palette,info_ptr->num_palette);\r
+- SetClrImportant(info_ptr->num_palette);\r
+- } else if (info_ptr->bit_depth ==2) { //<DP> needed for 2 bpp grayscale PNGs\r
++ int _num_palette;\r
++ png_colorp _palette;\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_get_PLTE(png_ptr,info_ptr,&_palette,&_num_palette);\r
++#else\r
++ _num_palette=info_ptr->num_palette;\r
++ _palette=info_ptr->palette;\r
++#endif\r
++ if (_num_palette>0){\r
++ SetPalette((rgb_color*)_palette,_num_palette);\r
++ SetClrImportant(_num_palette);\r
++ } else if (_bit_depth ==2) { //<DP> needed for 2 bpp grayscale PNGs\r
+ SetPaletteColor(0,0,0,0);\r
+ SetPaletteColor(1,85,85,85);\r
+ SetPaletteColor(2,170,170,170);\r
+ SetPaletteColor(3,255,255,255);\r
+ } else SetGrayPalette(); //<DP> needed for grayscale PNGs\r
+ \r
+- int nshift = max(0,(info_ptr->bit_depth>>3)-1)<<3;\r
++ int nshift = max(0,(_bit_depth>>3)-1)<<3;\r
+ \r
+- if (info_ptr->num_trans!=0){ //palette transparency\r
+- if (info_ptr->num_trans==1){\r
+- if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE){\r
++ png_bytep _trans_alpha;\r
++ int _num_trans;\r
++ png_color_16p _trans_color;\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_get_tRNS(png_ptr,info_ptr,&_trans_alpha,&_num_trans,&_trans_color);\r
++#else\r
++ _num_trans=info_ptr->num_trans;\r
++#endif\r
++ if (_num_trans!=0){ //palette transparency\r
++ if (_num_trans==1){\r
++ if (_color_type == PNG_COLOR_TYPE_PALETTE){\r
+ #if PNG_LIBPNG_VER > 10399\r
+- info.nBkgndIndex = info_ptr->trans_color.index;\r
++ info.nBkgndIndex = _trans_color->index;\r
+ #else\r
+ info.nBkgndIndex = info_ptr->trans_values.index;\r
+ #endif\r
+ } else{\r
+ #if PNG_LIBPNG_VER > 10399\r
+- info.nBkgndIndex = info_ptr->trans_color.gray>>nshift;\r
++ info.nBkgndIndex = _trans_color->gray>>nshift;\r
+ #else\r
+ info.nBkgndIndex = info_ptr->trans_values.gray>>nshift;\r
+ #endif\r
+ }\r
+ }\r
+- if (info_ptr->num_trans>1){\r
++ if (_num_trans>1){\r
+ RGBQUAD* pal=GetPalette();\r
+ if (pal){\r
+ DWORD ip;\r
+- for (ip=0;ip<min(head.biClrUsed,(unsigned long)info_ptr->num_trans);ip++)\r
++ for (ip=0;ip<min(head.biClrUsed,(unsigned long)_num_trans);ip++)\r
+ #if PNG_LIBPNG_VER > 10399\r
+- pal[ip].rgbReserved=info_ptr->trans_alpha[ip];\r
++ pal[ip].rgbReserved=_trans_alpha[ip];\r
+ #else\r
+ pal[ip].rgbReserved=info_ptr->trans[ip];\r
+ #endif\r
+- for (ip=info_ptr->num_trans;ip<head.biClrUsed;ip++){\r
++ for (ip=_num_trans;ip<head.biClrUsed;ip++){\r
+ pal[ip].rgbReserved=255;\r
+ }\r
+ info.bAlphaPaletteEnabled=true;\r
+@@ -174,14 +238,12 @@
+ }\r
+ \r
+ if (channels == 3){ //check RGB binary transparency\r
+- png_bytep trans;\r
+- int num_trans;\r
+- png_color_16 *image_background;\r
+- if (png_get_tRNS(png_ptr, info_ptr, &trans, &num_trans, &image_background)){\r
+-#if PNG_LIBPNG_VER > 10399\r
+- info.nBkgndColor.rgbRed = (BYTE)(info_ptr->trans_color.red>>nshift);\r
+- info.nBkgndColor.rgbGreen = (BYTE)(info_ptr->trans_color.green>>nshift);\r
+- info.nBkgndColor.rgbBlue = (BYTE)(info_ptr->trans_color.blue>>nshift);\r
++ /* seems unnecessary to call again, but the conditional must be important so... */\r
++ if (png_get_tRNS(png_ptr,info_ptr,&_trans_alpha,&_num_trans,&_trans_color)){\r
++#if PNG_LIBPNG_VER > 10399\r
++ info.nBkgndColor.rgbRed = (BYTE)(_trans_color->red>>nshift);\r
++ info.nBkgndColor.rgbGreen = (BYTE)(_trans_color->green>>nshift);\r
++ info.nBkgndColor.rgbBlue = (BYTE)(_trans_color->blue>>nshift);\r
+ #else\r
+ info.nBkgndColor.rgbRed = (BYTE)(info_ptr->trans_values.red>>nshift);\r
+ info.nBkgndColor.rgbGreen = (BYTE)(info_ptr->trans_values.green>>nshift);\r
+@@ -202,15 +264,24 @@
+ }\r
+ \r
+ // <vho> - flip the RGB pixels to BGR (or RGBA to BGRA)\r
+- if (info_ptr->color_type & PNG_COLOR_MASK_COLOR){\r
++ if (_color_type & PNG_COLOR_MASK_COLOR){\r
+ png_set_bgr(png_ptr);\r
+ }\r
+ \r
+ // <vho> - handle cancel\r
+- if (info.nEscape) longjmp(png_ptr->jmpbuf, 1);\r
++ if (info.nEscape)\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
++ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ \r
+ // row_bytes is the width x number of channels x (bit-depth / 8)\r
++#if PNG_LIBPNG_VER > 10399\r
++ row_pointers = new BYTE[png_get_rowbytes(png_ptr,info_ptr) + 8];\r
++#else\r
+ row_pointers = new BYTE[info_ptr->rowbytes + 8];\r
++#endif\r
+ \r
+ // turn on interlace handling\r
+ int number_passes = png_set_interlace_handling(png_ptr);\r
+@@ -221,8 +292,12 @@
+ SetCodecOption(0);\r
+ }\r
+ \r
+- int chan_offset = info_ptr->bit_depth >> 3;\r
++ int chan_offset = _bit_depth >> 3;\r
++#if PNG_LIBPNG_VER > 10399\r
++ int pixel_offset = (_bit_depth * png_get_channels(png_ptr,info_ptr)) >> 3;\r
++#else\r
+ int pixel_offset = info_ptr->pixel_depth >> 3;\r
++#endif\r
+ \r
+ for (int pass=0; pass < number_passes; pass++) {\r
+ iter.Upset();\r
+@@ -230,7 +305,12 @@
+ do {\r
+ \r
+ // <vho> - handle cancel\r
+- if (info.nEscape) longjmp(png_ptr->jmpbuf, 1);\r
++ if (info.nEscape)\r
++#if PNG_LIBPNG_VER > 10399\r
++ longjmp(png_jmpbuf(png_ptr), 1);\r
++#else\r
++ longjmp(png_ptr->jmpbuf, 1);\r
++#endif\r
+ \r
+ #if CXIMAGE_SUPPORT_ALPHA // <vho>\r
+ if (AlphaIsValid()) {\r
+@@ -241,7 +321,7 @@
+ BYTE* prow= iter.GetRow(ay);\r
+ \r
+ //recover data from previous scan\r
+- if (info_ptr->interlace_type && pass>0 && pass!=7){\r
++ if (_interlace_type && pass>0 && pass!=7){\r
+ for(ax=0;ax<head.biWidth;ax++){\r
+ long px = ax * pixel_offset;\r
+ if (channels == 2){\r
+@@ -278,10 +358,14 @@
+ #endif // CXIMAGE_SUPPORT_ALPHA // vho\r
+ {\r
+ //recover data from previous scan\r
+- if (info_ptr->interlace_type && pass>0){\r
++ if (_interlace_type && pass>0){\r
++#if PNG_LIBPNG_VER > 10399\r
++ iter.GetRow(row_pointers, png_get_rowbytes(png_ptr,info_ptr));\r
++#else\r
+ iter.GetRow(row_pointers, info_ptr->rowbytes);\r
++#endif\r
+ //re-expand buffer for images with bit depth > 8\r
+- if (info_ptr->bit_depth > 8){\r
++ if (_bit_depth > 8){\r
+ for(long ax=(head.biWidth*channels-1);ax>=0;ax--)\r
+ row_pointers[ax*chan_offset] = row_pointers[ax];\r
+ }\r
+@@ -291,15 +375,19 @@
+ png_read_row(png_ptr, row_pointers, NULL);\r
+ \r
+ //shrink 16 bit depth images down to 8 bits\r
+- if (info_ptr->bit_depth > 8){\r
++ if (_bit_depth > 8){\r
+ for(long ax=0;ax<(head.biWidth*channels);ax++)\r
+ row_pointers[ax] = row_pointers[ax*chan_offset];\r
+ }\r
+ \r
+ //copy the pixels\r
++#if PNG_LIBPNG_VER > 10399\r
++ iter.SetRow(row_pointers, png_get_rowbytes(png_ptr,info_ptr));\r
++#else\r
+ iter.SetRow(row_pointers, info_ptr->rowbytes);\r
++#endif\r
+ //<DP> expand 2 bpp images only in the last pass\r
+- if (info_ptr->bit_depth==2 && pass==(number_passes-1))\r
++ if (_bit_depth==2 && pass==(number_passes-1))\r
+ expand2to4bpp(iter.GetRow());\r
+ \r
+ //go on\r
+@@ -361,9 +449,13 @@
+ /* Set error handling. REQUIRED if you aren't supplying your own\r
+ * error hadnling functions in the png_create_write_struct() call.\r
+ */\r
++#if PNG_LIBPNG_VER > 10399\r
++ if (setjmp(png_jmpbuf(png_ptr))){\r
++#else\r
+ if (setjmp(png_ptr->jmpbuf)){\r
+ /* If we get here, we had a problem reading the file */\r
+ if (info_ptr->palette) free(info_ptr->palette);\r
++#endif\r
+ png_destroy_write_struct(&png_ptr, (png_infopp)&info_ptr);\r
+ cx_throw("Error saving PNG file");\r
+ }\r
+@@ -372,9 +464,23 @@
+ //png_init_io(png_ptr, hFile);\r
+ \r
+ // use custom I/O functions\r
+- png_set_write_fn(png_ptr,hFile,/*(png_rw_ptr)*/user_write_data,/*(png_flush_ptr)*/user_flush_data);\r
++ png_set_write_fn(png_ptr,hFile,/*(png_rw_ptr)*/user_write_data,/*(png_flush_ptr)*/user_flush_data);\r
+ \r
+ /* set the file information here */\r
++#if PNG_LIBPNG_VER > 10399\r
++ /* use variables to hold the values so it isnt necessary to png_get them later */\r
++ png_uint_32 _width,_height;\r
++ int _bit_depth,_color_type,_interlace_type,_compression_type,_filter_type;\r
++ png_byte _channels,_pixel_depth;\r
++\r
++ _width = GetWidth();\r
++ _height = GetHeight();\r
++ _pixel_depth = (BYTE)GetBpp();\r
++ _channels = (GetBpp()>8) ? (BYTE)3: (BYTE)1;\r
++ _bit_depth = (BYTE)(GetBpp()/_channels);\r
++ _compression_type = PNG_COMPRESSION_TYPE_DEFAULT;\r
++ _filter_type = PNG_FILTER_TYPE_DEFAULT;\r
++#else\r
+ info_ptr->width = GetWidth();\r
+ info_ptr->height = GetHeight();\r
+ info_ptr->pixel_depth = (BYTE)GetBpp();\r
+@@ -382,13 +488,22 @@
+ info_ptr->bit_depth = (BYTE)(GetBpp()/info_ptr->channels);\r
+ info_ptr->compression_type = info_ptr->filter_type = 0;\r
+ info_ptr->valid = 0;\r
++#endif\r
+ \r
+ switch(GetCodecOption(CXIMAGE_FORMAT_PNG)){\r
+ case 1:\r
++#if PNG_LIBPNG_VER > 10399\r
++ _interlace_type = PNG_INTERLACE_ADAM7;\r
++#else\r
+ info_ptr->interlace_type = PNG_INTERLACE_ADAM7;\r
++#endif\r
+ break;\r
+ default:\r
++#if PNG_LIBPNG_VER > 10399\r
++ _interlace_type = PNG_INTERLACE_NONE;\r
++#else\r
+ info_ptr->interlace_type = PNG_INTERLACE_NONE;\r
++#endif\r
+ }\r
+ \r
+ /* set compression level */\r
+@@ -398,22 +513,47 @@
+ \r
+ if (GetNumColors()){\r
+ if (bGrayScale){\r
++#if PNG_LIBPNG_VER > 10399\r
++ _color_type = PNG_COLOR_TYPE_GRAY;\r
++#else\r
+ info_ptr->color_type = PNG_COLOR_TYPE_GRAY;\r
++#endif\r
+ } else {\r
++#if PNG_LIBPNG_VER > 10399\r
++ _color_type = PNG_COLOR_TYPE_PALETTE;\r
++#else\r
+ info_ptr->color_type = PNG_COLOR_TYPE_PALETTE;\r
++#endif\r
+ }\r
+ } else {\r
++#if PNG_LIBPNG_VER > 10399\r
++ _color_type = PNG_COLOR_TYPE_RGB;\r
++#else\r
+ info_ptr->color_type = PNG_COLOR_TYPE_RGB;\r
++#endif\r
+ }\r
+ #if CXIMAGE_SUPPORT_ALPHA\r
+ if (AlphaIsValid()){\r
++#if PNG_LIBPNG_VER > 10399\r
++ _color_type |= PNG_COLOR_MASK_ALPHA;\r
++ _channels++;\r
++ _bit_depth = 8;\r
++ _pixel_depth += 8;\r
++#else\r
+ info_ptr->color_type |= PNG_COLOR_MASK_ALPHA;\r
+ info_ptr->channels++;\r
+ info_ptr->bit_depth = 8;\r
+ info_ptr->pixel_depth += 8;\r
++#endif\r
+ }\r
+ #endif\r
+ \r
++#if PNG_LIBPNG_VER > 10399\r
++ /* set the header here, since we're done modifying these values */\r
++ png_set_IHDR(png_ptr,info_ptr,_width,_height,_bit_depth,_color_type,_interlace_type,\r
++ _compression_type,_filter_type);\r
++#endif\r
++\r
+ /* set background */\r
+ png_color_16 image_background={ 0, 255, 255, 255, 0 };\r
+ RGBQUAD tc = GetTransColor();\r
+@@ -427,22 +567,24 @@
+ /* set metrics */\r
+ png_set_pHYs(png_ptr, info_ptr, head.biXPelsPerMeter, head.biYPelsPerMeter, PNG_RESOLUTION_METER);\r
+ \r
++#if PNG_LIBPNG_VER <= 10399\r
+ png_set_IHDR(png_ptr, info_ptr, info_ptr->width, info_ptr->height, info_ptr->bit_depth,\r
+ info_ptr->color_type, info_ptr->interlace_type,\r
+ PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);\r
++#endif\r
+ \r
+ //<DP> simple transparency\r
+ if (info.nBkgndIndex >= 0){\r
+- info_ptr->num_trans = 1;\r
+- info_ptr->valid |= PNG_INFO_tRNS;\r
+ #if PNG_LIBPNG_VER > 10399\r
+- info_ptr->trans_alpha = trans;\r
+- info_ptr->trans_color.index = (BYTE)info.nBkgndIndex;\r
+- info_ptr->trans_color.red = tc.rgbRed;\r
+- info_ptr->trans_color.green = tc.rgbGreen;\r
+- info_ptr->trans_color.blue = tc.rgbBlue;\r
+- info_ptr->trans_color.gray = info_ptr->trans_color.index;\r
++ png_color_16 _trans_color;\r
++ _trans_color.index = (BYTE)info.nBkgndIndex;\r
++ _trans_color.red = tc.rgbRed;\r
++ _trans_color.green = tc.rgbGreen;\r
++ _trans_color.blue = tc.rgbBlue;\r
++ _trans_color.gray = _trans_color.index;\r
+ #else\r
++ info_ptr->num_trans = 1;\r
++ info_ptr->valid |= PNG_INFO_tRNS;\r
+ info_ptr->trans = trans;\r
+ info_ptr->trans_values.index = (BYTE)info.nBkgndIndex;\r
+ info_ptr->trans_values.red = tc.rgbRed;\r
+@@ -454,34 +596,53 @@
+ // the transparency indexes start from 0 for non grayscale palette\r
+ if (!bGrayScale && head.biClrUsed && info.nBkgndIndex)\r
+ SwapIndex(0,(BYTE)info.nBkgndIndex);\r
++\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_set_tRNS(png_ptr,info_ptr,(png_bytep)trans,1,&_trans_color);\r
++#endif\r
+ }\r
+ \r
+ /* set the palette if there is one */\r
++#if PNG_LIBPNG_VER > 10399\r
++ png_colorp _palette;\r
++#endif\r
+ if (GetPalette()){\r
++#if PNG_LIBPNG_VER <= 10399\r
+ if (!bGrayScale){\r
+ info_ptr->valid |= PNG_INFO_PLTE;\r
+ }\r
++#endif\r
+ \r
+ int nc = GetClrImportant();\r
+ if (nc==0) nc = GetNumColors();\r
+ \r
++ // copy the palette colors\r
++#if PNG_LIBPNG_VER > 10399\r
++ _palette = new png_color[nc];\r
++#else\r
++ info_ptr->palette = new png_color[nc];\r
++ info_ptr->num_palette = (png_uint_16) nc;\r
++#endif\r
++ for (int i=0; i<nc; i++)\r
++#if PNG_LIBPNG_VER > 10399\r
++ GetPaletteColor(i, &_palette[i].red, &_palette[i].green, &_palette[i].blue);\r
++\r
++ png_set_PLTE(png_ptr,info_ptr,_palette,nc);\r
++#else\r
++ GetPaletteColor(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue);\r
++#endif\r
++\r
+ if (info.bAlphaPaletteEnabled){\r
+ for(WORD ip=0; ip<nc;ip++)\r
+ trans[ip]=GetPaletteColor((BYTE)ip).rgbReserved;\r
+- info_ptr->num_trans = (WORD)nc;\r
+- info_ptr->valid |= PNG_INFO_tRNS;\r
+ #if PNG_LIBPNG_VER > 10399\r
+- info_ptr->trans_alpha = trans;\r
++ png_set_tRNS(png_ptr,info_ptr,(png_bytep)trans,nc,NULL);\r
+ #else\r
++ info_ptr->num_trans = (WORD)nc;\r
++ info_ptr->valid |= PNG_INFO_tRNS;\r
+ info_ptr->trans = trans;\r
+ #endif\r
+ }\r
+-\r
+- // copy the palette colors\r
+- info_ptr->palette = new png_color[nc];\r
+- info_ptr->num_palette = (png_uint_16) nc;\r
+- for (int i=0; i<nc; i++)\r
+- GetPaletteColor(i, &info_ptr->palette[i].red, &info_ptr->palette[i].green, &info_ptr->palette[i].blue);\r
+ } \r
+ \r
+ #if CXIMAGE_SUPPORT_ALPHA // <vho>\r
+@@ -495,8 +656,12 @@
+ } } }\r
+ #endif // CXIMAGE_SUPPORT_ALPHA // <vho>\r
+ \r
++#if PNG_LIBPNG_VER > 10399\r
++ int row_size = max(info.dwEffWidth, (_width * _channels * _bit_depth / 8));\r
++#else\r
+ int row_size = max(info.dwEffWidth, info_ptr->width*info_ptr->channels*(info_ptr->bit_depth/8));\r
+ info_ptr->rowbytes = row_size;\r
++#endif\r
+ BYTE *row_pointers = new BYTE[row_size];\r
+ \r
+ /* write the file information */\r
+@@ -514,7 +679,11 @@
+ if (AlphaIsValid()){\r
+ for (long ax=head.biWidth-1; ax>=0;ax--){\r
+ c = BlindGetPixelColor(ax,ay);\r
++#if PNG_LIBPNG_VER > 10399\r
++ int px = ax * _channels;\r
++#else\r
+ int px = ax * info_ptr->channels;\r
++#endif\r
+ if (!bGrayScale){\r
+ row_pointers[px++]=c.rgbRed;\r
+ row_pointers[px++]=c.rgbGreen;\r
+@@ -529,7 +698,11 @@
+ #endif //CXIMAGE_SUPPORT_ALPHA // <vho>\r
+ {\r
+ iter.GetRow(row_pointers, row_size);\r
++#if PNG_LIBPNG_VER > 10399\r
++ if (_color_type == PNG_COLOR_TYPE_RGB) //HACK BY OP\r
++#else\r
+ if (info_ptr->color_type == PNG_COLOR_TYPE_RGB) //HACK BY OP\r
++#endif\r
+ RGBtoBGR(row_pointers, row_size);\r
+ png_write_row(png_ptr, row_pointers);\r
+ }\r
+@@ -546,9 +719,14 @@
+ png_write_end(png_ptr, info_ptr);\r
+ \r
+ /* if you malloced the palette, free it here */\r
++#if PNG_LIBPNG_VER > 10399\r
++ if (_palette){\r
++ delete [] (_palette);\r
++#else\r
+ if (info_ptr->palette){\r
+ delete [] (info_ptr->palette);\r
+ info_ptr->palette = NULL;\r
++#endif\r
+ }\r
+ \r
+ /* clean up after the write, and free any memory allocated */\r