From f2c8079119e5887329d1cba5677498d9f6443f62 Mon Sep 17 00:00:00 2001 From: Dimitri Schoolwerth Date: Wed, 16 Mar 2011 12:46:03 +0000 Subject: [PATCH] Added support for reading image resolutions from PNG images. Patch by scottb. Closes #12893. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@67219 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- docs/changes.txt | 1 + src/common/imagpng.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/docs/changes.txt b/docs/changes.txt index 6d61b9b16f..211741fc5e 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -490,6 +490,7 @@ All (GUI): - Added wxFont::SetSymbolicSize() and related methods. - Fix SVG files generation in locales using decimal comma (snowleopard). - Fix setting tooltips for generic wxSpinCtrl (Catalin Raceanu). +- Added support for reading image resolutions from PNG images (scottb). GTK: diff --git a/src/common/imagpng.cpp b/src/common/imagpng.cpp index 3472c5a1fc..c0b1939510 100644 --- a/src/common/imagpng.cpp +++ b/src/common/imagpng.cpp @@ -602,6 +602,47 @@ wxPNGHandler::LoadFile(wxImage *image, } #endif // wxUSE_PALETTE + + // set the image resolution if it's available + png_uint_32 resX, resY; + int unitType; + if (png_get_pHYs(png_ptr, info_ptr, &resX, &resY, &unitType) + == PNG_INFO_pHYs) + { + wxImageResolution res = wxIMAGE_RESOLUTION_CM; + + switch (unitType) + { + default: + wxLogWarning(_("Unknown PNG resolution unit %d"), unitType); + // fall through + + case PNG_RESOLUTION_UNKNOWN: + image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, resX); + image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, resY); + + res = wxIMAGE_RESOLUTION_NONE; + break; + + case PNG_RESOLUTION_METER: + /* + Convert meters to centimeters. + Use a string to not lose precision (converting to cm and then + to inch would result in integer rounding error). + If an app wants an int, GetOptionInt will convert and round + down for them. + */ + image->SetOption(wxIMAGE_OPTION_RESOLUTIONX, + wxString::FromCDouble((double) resX / 100.0, 2)); + image->SetOption(wxIMAGE_OPTION_RESOLUTIONY, + wxString::FromCDouble((double) resY / 100.0, 2)); + break; + } + + image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, res); + } + + png_destroy_read_struct( &png_ptr, &info_ptr, (png_infopp) NULL ); // loaded successfully, now init wxImage with this data -- 2.45.2