From 71afe63679dcecd90bc43445d2003b9c48b10895 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Wed, 19 Feb 2025 01:52:00 +0100 Subject: [PATCH] Add wxImage::Scale() and Rescale() overloads taking wxSize Use them in the code where this is more convenient than using individual components. No real changes, just make resizing the images a bit more convenient. --- include/wx/image.h | 6 ++++++ interface/wx/image.h | 12 ++++++++++++ samples/image/image.cpp | 4 +--- samples/splash/splash.cpp | 2 +- samples/toolbar/toolbar.cpp | 2 +- src/common/animatecmn.cpp | 2 +- src/common/bmpbase.cpp | 2 +- src/generic/dirctrlg.cpp | 2 +- src/propgrid/advprops.cpp | 2 +- src/ribbon/buttonbar.cpp | 2 +- src/ribbon/panel.cpp | 4 +--- src/richtext/richtextbuffer.cpp | 2 +- src/univ/topluniv.cpp | 2 +- src/xrc/xmlres.cpp | 3 ++- 14 files changed, 31 insertions(+), 16 deletions(-) diff --git a/include/wx/image.h b/include/wx/image.h index 0c4c885757..88e5bd3330 100644 --- a/include/wx/image.h +++ b/include/wx/image.h @@ -352,6 +352,9 @@ public: // return the new image with size width*height wxImage Scale( int width, int height, wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL ) const; + wxImage Scale(const wxSize& size, + wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL) const + { return Scale(size.GetWidth(), size.GetHeight(), quality); } // box averager and bicubic filters for up/down sampling wxImage ResampleNearest(int width, int height) const; @@ -370,6 +373,9 @@ public: wxImage& Rescale( int width, int height, wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL ) { return *this = Scale(width, height, quality); } + wxImage& Rescale( const wxSize& size, + wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL ) + { return *this = Scale(size, quality); } // resizes the image in place wxImage& Resize( const wxSize& size, const wxPoint& pos, diff --git a/interface/wx/image.h b/interface/wx/image.h index c394ad759e..8989d1b0fc 100644 --- a/interface/wx/image.h +++ b/interface/wx/image.h @@ -877,11 +877,17 @@ public: For a description of the @a quality parameter, see the Scale() function. Returns the (modified) image itself. + Overload taking wxSize is only available since wxWidgets 3.3.0. + @see Scale() */ wxImage& Rescale(int width, int height, wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL); + /// @overload + wxImage& Rescale(const wxSize& size, + wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL); + /** Changes the size of the image in-place without scaling it by adding either a border with the given colour or cropping as necessary. @@ -1008,11 +1014,17 @@ public: for 32-bit programs. For 64-bit programs the limit is 2^48 and so not relevant in practice. + The overload taking a wxSize is only available since wxWidgets 3.3.0. + @see Rescale() */ wxImage Scale(int width, int height, wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL) const; + /// @overload + wxImage Scale(const wxSize& size, + wxImageResizeQuality quality = wxIMAGE_QUALITY_NORMAL) const; + /** Returns a resized version of this image without scaling it by adding either a border with the given colour or cropping as necessary. diff --git a/samples/image/image.cpp b/samples/image/image.cpp index b4cfe8cd62..107752c06b 100644 --- a/samples/image/image.cpp +++ b/samples/image/image.cpp @@ -462,9 +462,7 @@ private: void OnResize(wxCommandEvent& WXUNUSED(event)) { wxImage img(m_bitmap.ConvertToImage()); - - const wxSize size = GetClientSize(); - img.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH); + img.Rescale(GetClientSize(), wxIMAGE_QUALITY_HIGH); m_bitmap = wxBitmap(img); UpdateStatusBar(); diff --git a/samples/splash/splash.cpp b/samples/splash/splash.cpp index 5bc25ff40b..08f1b47948 100644 --- a/samples/splash/splash.cpp +++ b/samples/splash/splash.cpp @@ -263,7 +263,7 @@ void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) // do not scale on already small screens if (!m_isPda) - image.Rescale( bitmap.GetWidth()/2, bitmap.GetHeight()/2 ); + image.Rescale(bitmap.GetSize()/2); bitmap = wxBitmap(image); wxSplashScreen *splash = new wxSplashScreen(bitmap, diff --git a/samples/toolbar/toolbar.cpp b/samples/toolbar/toolbar.cpp index ffaa4ea833..de8f87f7a4 100644 --- a/samples/toolbar/toolbar.cpp +++ b/samples/toolbar/toolbar.cpp @@ -584,7 +584,7 @@ void MyFrame::PopulateToolbar(wxToolBarBase* toolBar) // anything. wxImage image = m_image; if ( image.GetSize() != size ) - image.Rescale(size.x, size.y, wxIMAGE_QUALITY_HIGH); + image.Rescale(size, wxIMAGE_QUALITY_HIGH); // This is required under MSW in order to be able to draw // over the bitmap using wxDC. For full alpha support, diff --git a/src/common/animatecmn.cpp b/src/common/animatecmn.cpp index 282fc2e9da..5c34acb5fa 100644 --- a/src/common/animatecmn.cpp +++ b/src/common/animatecmn.cpp @@ -203,7 +203,7 @@ void wxAnimationCtrlBase::UpdateStaticImage() { // the user-provided bitmap is bigger than our control, strech it wxImage temp(bmpCurrent.ConvertToImage()); - temp.Rescale(sz.GetWidth(), sz.GetHeight(), wxIMAGE_QUALITY_HIGH); + temp.Rescale(sz, wxIMAGE_QUALITY_HIGH); m_bmpStaticReal = wxBitmap(temp); } } diff --git a/src/common/bmpbase.cpp b/src/common/bmpbase.cpp index d686c2ce1f..9067a27487 100644 --- a/src/common/bmpbase.cpp +++ b/src/common/bmpbase.cpp @@ -76,7 +76,7 @@ void wxBitmapHelpers::Rescale(wxBitmap& bmp, const wxSize& sizeNeeded) #if wxUSE_IMAGE wxImage img = bmp.ConvertToImage(); - img.Rescale(sizeNeeded.x, sizeNeeded.y); + img.Rescale(sizeNeeded); bmp = wxBitmap(img); #else // !wxUSE_IMAGE // Fallback method of scaling the bitmap diff --git a/src/generic/dirctrlg.cpp b/src/generic/dirctrlg.cpp index f3076baa18..ae855a738b 100644 --- a/src/generic/dirctrlg.cpp +++ b/src/generic/dirctrlg.cpp @@ -1497,7 +1497,7 @@ int wxFileIconsTable::GetIconID(const wxString& extension, const wxString& mime) #endif { // Double, using normal quality scaling. - img.Rescale(2*img.GetWidth(), 2*img.GetHeight()); + img.Rescale(2*img.GetSize()); // Then scale to the desired size. This gives the best quality, // and better than CreateAntialiasedBitmap. diff --git a/src/propgrid/advprops.cpp b/src/propgrid/advprops.cpp index 360e14d20c..1319f928e5 100644 --- a/src/propgrid/advprops.cpp +++ b/src/propgrid/advprops.cpp @@ -1964,7 +1964,7 @@ void wxImageFileProperty::OnCustomPaint( wxDC& dc, if ( !m_bitmap.IsOk() ) { wxImage imgScaled = m_image; - imgScaled.Rescale(rect.width, rect.height); + imgScaled.Rescale(rect.GetSize()); m_bitmap = wxBitmap(imgScaled, dc); } } diff --git a/src/ribbon/buttonbar.cpp b/src/ribbon/buttonbar.cpp index f997cd98f7..5407e9bd77 100644 --- a/src/ribbon/buttonbar.cpp +++ b/src/ribbon/buttonbar.cpp @@ -70,7 +70,7 @@ wxBitmap MakeResizedBitmap(const wxBitmap& original, wxSize size) scale = 2.0; wxImage img(original.ConvertToImage()); - img.Rescale(int(scale * size.GetWidth()), int(scale * size.GetHeight()), wxIMAGE_QUALITY_HIGH); + img.Rescale(scale * size, wxIMAGE_QUALITY_HIGH); return wxBitmap(img, -1, scale); } diff --git a/src/ribbon/panel.cpp b/src/ribbon/panel.cpp index ca0275979c..52fc66aeed 100644 --- a/src/ribbon/panel.cpp +++ b/src/ribbon/panel.cpp @@ -711,9 +711,7 @@ bool wxRibbonPanel::Realize() scale = 2.0; wxImage img(m_minimised_icon.ConvertToImage()); - img.Rescale(wxRound(scale * bitmap_size.GetWidth()), - wxRound(scale * bitmap_size.GetHeight()), - wxIMAGE_QUALITY_HIGH); + img.Rescale(scale * bitmap_size, wxIMAGE_QUALITY_HIGH); m_minimised_icon_resized = wxBitmap(img, -1, scale); } else diff --git a/src/richtext/richtextbuffer.cpp b/src/richtext/richtextbuffer.cpp index 7343c223b9..30a0bdc503 100644 --- a/src/richtext/richtextbuffer.cpp +++ b/src/richtext/richtextbuffer.cpp @@ -12731,7 +12731,7 @@ bool wxRichTextImage::LoadAndScaleImageCache(wxImage& image, const wxSize& sz, w wxImage img; if (image.GetWidth() <= upscaleThreshold || image.GetHeight() <= upscaleThreshold) { - img = image.Scale(image.GetWidth()*2, image.GetHeight()*2); + img = image.Scale(2*image.GetSize()); img.Rescale(width*scaleFactor, height*scaleFactor, wxIMAGE_QUALITY_HIGH); } else diff --git a/src/univ/topluniv.cpp b/src/univ/topluniv.cpp index 2e7d7953fe..ac22569303 100644 --- a/src/univ/topluniv.cpp +++ b/src/univ/topluniv.cpp @@ -337,7 +337,7 @@ void wxTopLevelWindow::SetIcons(const wxIconBundle& icons) else { wxImage img = bmp1.ConvertToImage(); - img.Rescale(size.x, size.y); + img.Rescale(size); m_titlebarIcon.CopyFromBitmap(wxBitmap(img)); } #endif // wxUSE_IMAGE diff --git a/src/xrc/xmlres.cpp b/src/xrc/xmlres.cpp index 73499bf1a1..222bd0e94f 100644 --- a/src/xrc/xmlres.cpp +++ b/src/xrc/xmlres.cpp @@ -1900,7 +1900,8 @@ wxBitmap LoadBitmapFromFS(wxXmlResourceHandlerImpl* impl, ); return wxNullBitmap; } - if (!(size == wxDefaultSize)) img.Rescale(size.x, size.y); + if (size != wxDefaultSize) + img.Rescale(size); return wxBitmap(img); }