]> git.saurik.com Git - wxWidgets.git/commitdiff
Added support for reading image resolutions from PNG images.
authorDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Wed, 16 Mar 2011 12:46:03 +0000 (12:46 +0000)
committerDimitri Schoolwerth <dimitri.schoolwerth@gmail.com>
Wed, 16 Mar 2011 12:46:03 +0000 (12:46 +0000)
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
src/common/imagpng.cpp

index 6d61b9b16f15f12e0a271a6458b17912a4d78183..211741fc5e15e39bbcb06fdc149ce0843b549b35 100644 (file)
@@ -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:
 
index 3472c5a1fc8259fd6dbe6214637d99f3bb03c9e0..c0b19395100aa8224ad965631bfbef145824ff1e 100644 (file)
@@ -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