Move bitmap resizing from wxDefaultArtProvider to base class
No real changes, this is just a refactoring in preparation for the next commit. This commit is best viewed with --color-moved git option.
This commit is contained in:
@@ -257,6 +257,11 @@ protected:
|
||||
return wxNullIconBundle;
|
||||
}
|
||||
|
||||
// Helper for resizing the bitmaps to the requested size without scaling
|
||||
// them up nor resizing (16, 15) bitmaps to (16, 16) -- or doing anything
|
||||
// at all if the bitmap is already of the required size.
|
||||
static void RescaleOrResizeIfNeeded(wxBitmap& bmp, const wxSize& sizeNeeded);
|
||||
|
||||
private:
|
||||
static void CommonAddingProvider();
|
||||
static wxIconBundle DoGetIconBundle(const wxArtID& id,
|
||||
|
||||
@@ -308,6 +308,49 @@ void wxArtProvider::RescaleBitmap(wxBitmap& bmp, const wxSize& sizeNeeded)
|
||||
}
|
||||
#endif // WXWIN_COMPATIBILITY_3_0
|
||||
|
||||
void
|
||||
wxArtProvider::RescaleOrResizeIfNeeded(wxBitmap& bmp, const wxSize& sizeNeeded)
|
||||
{
|
||||
#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
|
||||
if ( sizeNeeded == wxDefaultSize )
|
||||
return;
|
||||
|
||||
int bmp_w = bmp.GetWidth();
|
||||
int bmp_h = bmp.GetHeight();
|
||||
|
||||
if ( bmp_w == sizeNeeded.x && bmp_h == sizeNeeded.y )
|
||||
return;
|
||||
|
||||
if (bmp_w == 16 && bmp_h == 15 && sizeNeeded == wxSize(16, 16))
|
||||
{
|
||||
// Do nothing in this special but quite common case, because scaling
|
||||
// with only a pixel difference will look horrible.
|
||||
}
|
||||
else if ((bmp_h < sizeNeeded.x) && (bmp_w < sizeNeeded.y))
|
||||
{
|
||||
// the caller wants default size, which is larger than
|
||||
// the image we have; to avoid degrading it visually by
|
||||
// scaling it up, paste it into transparent image instead:
|
||||
wxPoint offset((sizeNeeded.x - bmp_w)/2, (sizeNeeded.y - bmp_h)/2);
|
||||
wxImage img = bmp.ConvertToImage();
|
||||
img.Resize(sizeNeeded, offset);
|
||||
bmp = wxBitmap(img);
|
||||
}
|
||||
else // scale (down or mixed, but not up)
|
||||
{
|
||||
wxImage img = bmp.ConvertToImage();
|
||||
bmp = wxBitmap
|
||||
(
|
||||
img.Scale(sizeNeeded.x, sizeNeeded.y,
|
||||
wxIMAGE_QUALITY_HIGH)
|
||||
);
|
||||
}
|
||||
#else
|
||||
wxUnusedVar(bmp);
|
||||
wxUnusedVar(sizeNeeded);
|
||||
#endif // wxUSE_IMAGE
|
||||
}
|
||||
|
||||
/*static*/ wxBitmap wxArtProvider::GetBitmap(const wxArtID& id,
|
||||
const wxArtClient& client,
|
||||
const wxSize& size)
|
||||
|
||||
@@ -213,50 +213,15 @@ wxBitmap wxDefaultArtProvider::CreateBitmap(const wxArtID& id,
|
||||
{
|
||||
wxBitmap bmp = wxDefaultArtProvider_CreateBitmap(id);
|
||||
|
||||
#if wxUSE_IMAGE && (!defined(__WXMSW__) || wxUSE_WXDIB)
|
||||
if (bmp.IsOk())
|
||||
{
|
||||
// fit into transparent image with desired size hint from the client
|
||||
if (reqSize == wxDefaultSize)
|
||||
{
|
||||
// find out if there is a desired size for this client
|
||||
wxSize bestSize = GetSizeHint(client);
|
||||
if (bestSize != wxDefaultSize)
|
||||
{
|
||||
int bmp_w = bmp.GetWidth();
|
||||
int bmp_h = bmp.GetHeight();
|
||||
|
||||
if (bmp_w == 16 && bmp_h == 15 && bestSize == wxSize(16, 16))
|
||||
{
|
||||
// Do nothing in this special but quite common case, because scaling
|
||||
// with only a pixel difference will look horrible.
|
||||
}
|
||||
else if ((bmp_h < bestSize.x) && (bmp_w < bestSize.y))
|
||||
{
|
||||
// the caller wants default size, which is larger than
|
||||
// the image we have; to avoid degrading it visually by
|
||||
// scaling it up, paste it into transparent image instead:
|
||||
wxPoint offset((bestSize.x - bmp_w)/2, (bestSize.y - bmp_h)/2);
|
||||
wxImage img = bmp.ConvertToImage();
|
||||
img.Resize(bestSize, offset);
|
||||
bmp = wxBitmap(img);
|
||||
}
|
||||
else // scale (down or mixed, but not up)
|
||||
{
|
||||
wxImage img = bmp.ConvertToImage();
|
||||
bmp = wxBitmap
|
||||
(
|
||||
img.Scale(bestSize.x, bestSize.y,
|
||||
wxIMAGE_QUALITY_HIGH)
|
||||
);
|
||||
}
|
||||
}
|
||||
RescaleOrResizeIfNeeded(bmp, GetSizeHint(client));
|
||||
}
|
||||
}
|
||||
#else
|
||||
wxUnusedVar(client);
|
||||
wxUnusedVar(reqSize);
|
||||
#endif // wxUSE_IMAGE
|
||||
|
||||
return bmp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user