From b6963858fbc0a1fc82538b86d20245ace9db9449 Mon Sep 17 00:00:00 2001
From: Vadim Zeitlin <vadim@wxwidgets.org>
Date: Mon, 14 Nov 2011 13:35:48 +0000
Subject: [PATCH] Added wxIMAGE_OPTION_ORIGINAL_{WIDTH,HEIGHT} wxImage options.

These options allow to retrieve the original image size if the image was
scaled during load.

Closes #13662.

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@69759 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
---
 docs/changes.txt        |  1 +
 include/wx/image.h      |  3 +++
 interface/wx/image.h    |  8 ++++++++
 samples/image/image.cpp |  6 +++++-
 src/common/image.cpp    | 10 ++++++++++
 src/common/imagjpeg.cpp |  7 +++++++
 6 files changed, 34 insertions(+), 1 deletion(-)

diff --git a/docs/changes.txt b/docs/changes.txt
index 5048f67c67..71959ce5f5 100644
--- a/docs/changes.txt
+++ b/docs/changes.txt
@@ -490,6 +490,7 @@ All (GUI):
 - Add "checked" property for toolbar tool elements in XRC.
 - Allow customization of the locations where persistent settings are stored.
 - Restore support for reusing ids more than 254 times (Armel Asselin).
+- Added wxIMAGE_OPTION_ORIGINAL_{WIDTH,HEIGHT} (Catalin Raceanu).
 
 OSX:
 
diff --git a/include/wx/image.h b/include/wx/image.h
index e9fb5c5469..0122ed6f64 100644
--- a/include/wx/image.h
+++ b/include/wx/image.h
@@ -40,6 +40,9 @@
 #define wxIMAGE_OPTION_MAX_WIDTH             wxString(wxT("MaxWidth"))
 #define wxIMAGE_OPTION_MAX_HEIGHT            wxString(wxT("MaxHeight"))
 
+#define wxIMAGE_OPTION_ORIGINAL_WIDTH        wxString(wxT("OriginalWidth"))
+#define wxIMAGE_OPTION_ORIGINAL_HEIGHT       wxString(wxT("OriginalHeight"))
+
 // constants used with wxIMAGE_OPTION_RESOLUTIONUNIT
 //
 // NB: don't change these values, they correspond to libjpeg constants
diff --git a/interface/wx/image.h b/interface/wx/image.h
index aa83b98a7f..11bc7e782e 100644
--- a/interface/wx/image.h
+++ b/interface/wx/image.h
@@ -86,6 +86,8 @@ enum wxImagePNGType
 #define wxIMAGE_OPTION_RESOLUTIONUNIT                   wxString(wxT("ResolutionUnit"))
 #define wxIMAGE_OPTION_MAX_WIDTH                        wxString(wxT("MaxWidth"))
 #define wxIMAGE_OPTION_MAX_HEIGHT                       wxString(wxT("MaxHeight"))
+#define wxIMAGE_OPTION_ORIGINAL_WIDTH                   wxString(wxT("OriginalWidth"))
+#define wxIMAGE_OPTION_ORIGINAL_HEIGHT                  wxString(wxT("OriginalHeight"))
 
 #define wxIMAGE_OPTION_BMP_FORMAT                       wxString(wxT("wxBMP_FORMAT"))
 #define wxIMAGE_OPTION_CUR_HOTSPOT_X                    wxString(wxT("HotSpotX"))
@@ -1182,6 +1184,12 @@ public:
             handler, this is still what happens however). These options must be
             set before calling LoadFile() to have any effect.
 
+        @li @c wxIMAGE_OPTION_ORIGINAL_WIDTH and @c wxIMAGE_OPTION_ORIGINAL_HEIGHT:
+            These options will return the original size of the image if either
+            @c wxIMAGE_OPTION_MAX_WIDTH or @c wxIMAGE_OPTION_MAX_HEIGHT is
+            specified.
+            @since 2.9.3
+
         @li @c wxIMAGE_OPTION_QUALITY: JPEG quality used when saving. This is an
             integer in 0..100 range with 0 meaning very poor and 100 excellent
             (but very badly compressed). This option is currently ignored for
diff --git a/samples/image/image.cpp b/samples/image/image.cpp
index 5daf5736b6..9002c5b84f 100644
--- a/samples/image/image.cpp
+++ b/samples/image/image.cpp
@@ -944,10 +944,14 @@ void MyFrame::OnThumbnail( wxCommandEvent &WXUNUSED(event) )
         return;
     }
 
+    int origWidth = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_WIDTH );
+    int origHeight = image.GetOptionInt( wxIMAGE_OPTION_ORIGINAL_HEIGHT );
+
     const long loadTime = sw.Time();
 
     MyImageFrame * const frame = new MyImageFrame(this, filename, image);
-    wxLogStatus(frame, "Loaded \"%s\" in %ldms", filename, loadTime);
+    wxLogStatus(frame, "Loaded \"%s\" in %ldms; original size was (%d, %d)",
+                filename, loadTime, origWidth, origHeight);
 #else
     wxLogError( wxT("Couldn't create file selector dialog") );
     return;
diff --git a/src/common/image.cpp b/src/common/image.cpp
index 0cb1adf788..ebb8931854 100644
--- a/src/common/image.cpp
+++ b/src/common/image.cpp
@@ -2450,7 +2450,17 @@ bool wxImage::DoLoad(wxImageHandler& handler, wxInputStream& stream, int index)
         }
 
         if ( width != widthOrig || height != heightOrig )
+        {
+            // get the original size if it was set by the image handler
+            // but also in order to restore it after Rescale
+            int widthOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_WIDTH),
+                heightOrigOption = GetOptionInt(wxIMAGE_OPTION_ORIGINAL_HEIGHT);
+
             Rescale(width, height, wxIMAGE_QUALITY_HIGH);
+
+            SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH, widthOrigOption ? widthOrigOption : widthOrig);
+            SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT, heightOrigOption ? heightOrigOption : heightOrig);
+        }
     }
 
     // Set this after Rescale, which currently does not preserve it
diff --git a/src/common/imagjpeg.cpp b/src/common/imagjpeg.cpp
index 2a24dce990..88bcb4f313 100644
--- a/src/common/imagjpeg.cpp
+++ b/src/common/imagjpeg.cpp
@@ -334,6 +334,13 @@ bool wxJPEGHandler::LoadFile( wxImage *image, wxInputStream& stream, bool verbos
         image->SetOption(wxIMAGE_OPTION_RESOLUTIONUNIT, cinfo.density_unit);
     }
 
+    if ( cinfo.image_width != cinfo.output_width || cinfo.image_height != cinfo.output_height )
+    {
+        // save the original image size
+        image->SetOption(wxIMAGE_OPTION_ORIGINAL_WIDTH, cinfo.image_width);
+        image->SetOption(wxIMAGE_OPTION_ORIGINAL_HEIGHT, cinfo.image_height);
+    }
+
     jpeg_finish_decompress( &cinfo );
     jpeg_destroy_decompress( &cinfo );
     return true;
-- 
2.47.2