From 538eafc78bd6a103201a287e925f43e5b8a62f79 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Fri, 3 Jun 2022 00:44:59 +0100 Subject: [PATCH] Fix bug with wrong GetIndexToUpscale() return value Only assign known good values of the index to "indexLast" as otherwise we could end up returning an invalid index value from it if "indexBest" was never set and the last index wasn't a valid one, as it happened in wxBitmapBundleImplSet containing an original bitmap and a x2 scaled copy of it, which shouldn't be used for further scaling. Add a unit test checking that this bug is fixed now. --- src/common/bmpbndl.cpp | 8 +++++++- tests/graphics/bmpbundle.cpp | 9 +++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/common/bmpbndl.cpp b/src/common/bmpbndl.cpp index 0f83b4da24..3316c0b14e 100644 --- a/src/common/bmpbndl.cpp +++ b/src/common/bmpbndl.cpp @@ -803,12 +803,18 @@ size_t wxBitmapBundleImpl::GetIndexToUpscale(const wxSize& size) const size_t indexLast = 0; const wxSize sizeDef = GetDefaultSize(); - for ( size_t i = 0;; indexLast = i) + for ( size_t i = 0;; ) { + // Save it before it's updated by GetNextAvailableScale(). + size_t indexPrev = i; + const double scaleThis = GetNextAvailableScale(i); if ( scaleThis == 0.0 ) break; + // Only update it now, knowing that this index could have been used. + indexLast = indexPrev; + const double scale = size.y / (sizeDef.y*scaleThis); if (wxRound(scale) == scale) indexBest = indexLast; diff --git a/tests/graphics/bmpbundle.cpp b/tests/graphics/bmpbundle.cpp index c40e50124a..c90e022e6d 100644 --- a/tests/graphics/bmpbundle.cpp +++ b/tests/graphics/bmpbundle.cpp @@ -50,6 +50,15 @@ TEST_CASE("BitmapBundle::FromBitmaps", "[bmpbundle]") CHECK( b.GetBitmap(wxSize(24, 24)).GetSize() == wxSize(24, 24) ); } +TEST_CASE("BitmapBundle::GetBitmap", "[bmpbundle]") +{ + const wxBitmapBundle b = wxBitmapBundle::FromBitmap(wxBitmap(16, 16)); + + CHECK( b.GetBitmap(wxSize(16, 16)).GetSize() == wxSize(16, 16) ); + CHECK( b.GetBitmap(wxSize(32, 32)).GetSize() == wxSize(32, 32) ); + CHECK( b.GetBitmap(wxSize(24, 24)).GetSize() == wxSize(24, 24) ); +} + // Helper functions for the test below. namespace {