+ if ( iBitDepth == 16 )
+ iElements *= 2;
+
+ // save the image resolution if we have it
+ int resX, resY;
+ switch ( GetResolutionFromOptions(*image, &resX, &resY) )
+ {
+ case wxIMAGE_RESOLUTION_INCHES:
+ {
+ const double INCHES_IN_METER = 10000.0 / 254;
+ resX = int(resX * INCHES_IN_METER);
+ resY = int(resY * INCHES_IN_METER);
+ }
+ break;
+
+ case wxIMAGE_RESOLUTION_CM:
+ resX *= 100;
+ resY *= 100;
+ break;
+
+ case wxIMAGE_RESOLUTION_NONE:
+ break;
+
+ default:
+ wxFAIL_MSG( _T("unsupported image resolution units") );
+ }
+
+ if ( resX && resY )
+ png_set_pHYs( png_ptr, info_ptr, resX, resY, PNG_RESOLUTION_METER );
+
+ png_set_sBIT( png_ptr, info_ptr, &sig_bit );
+ png_write_info( png_ptr, info_ptr );
+ png_set_shift( png_ptr, &sig_bit );
+ png_set_packing( png_ptr );
+
+ unsigned char *
+ data = (unsigned char *)malloc( image->GetWidth() * iElements );
+ if ( !data )
+ {
+ png_destroy_write_struct( &png_ptr, (png_infopp)NULL );
+ return false;
+ }
+
+ unsigned char *
+ pAlpha = (unsigned char *)(bHasAlpha ? image->GetAlpha() : NULL);
+ int iHeight = image->GetHeight();
+ int iWidth = image->GetWidth();
+
+ unsigned char uchMaskRed = 0, uchMaskGreen = 0, uchMaskBlue = 0;
+
+ if ( bHasMask )
+ {
+ uchMaskRed = image->GetMaskRed();
+ uchMaskGreen = image->GetMaskGreen();
+ uchMaskBlue = image->GetMaskBlue();
+ }
+
+ unsigned char *pColors = image->GetData();
+
+ for (int y = 0; y != iHeight; ++y)
+ {
+ unsigned char *pData = data;
+ for (int x = 0; x != iWidth; x++)