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.
This commit is contained in:
Vadim Zeitlin
2022-06-03 00:44:59 +01:00
parent f56b8a99a6
commit 538eafc78b
2 changed files with 16 additions and 1 deletions

View File

@@ -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;

View File

@@ -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
{