Add wxBitmap(wxImage, wxDC) ctor to all ports

This ctor was previously present only in wxMSW, make it available in all
ports to allow the same code to compile everywhere.

In most of them wxDC argument is simply ignored, but in wxGTK and wxOSX
it is used to assign the appropriate scale factor for the new bitmap.

Enable previously wxMSW-only unit test checking for this.
This commit is contained in:
Vadim Zeitlin 2022-04-12 17:44:59 +01:00
parent b185186ebf
commit 24970061fa
12 changed files with 74 additions and 17 deletions

View File

@ -32,7 +32,10 @@ public:
wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE);
wxBitmap(const char* const* bits);
#if wxUSE_IMAGE
wxBitmap(const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0);
wxBitmap(const wxImage& image, int depth = -1, double scale = 1.0)
{ InitFromImage(image, depth, scale); }
wxBitmap(const wxImage& image, const wxDC& WXUNUSED(dc))
{ InitFromImage(image, -1, 1.0); }
#endif
bool Create(const wxIDirectFBSurfacePtr& surface);
@ -86,6 +89,8 @@ protected:
virtual wxGDIRefData *CreateGDIRefData() const;
virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const;
void InitFromImage(const wxImage& image, int depth, double scale);
bool CreateWithFormat(int width, int height, int dfbFormat);
wxDECLARE_DYNAMIC_CLASS(wxBitmap);

View File

@ -75,6 +75,7 @@ public:
wxBitmap( const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE );
#if wxUSE_IMAGE
wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH, double scale = 1.0);
wxBitmap(const wxImage& image, const wxDC& dc);
#endif // wxUSE_IMAGE
wxBitmap(GdkPixbuf* pixbuf, int depth = 0);
explicit wxBitmap(const wxCursor& cursor);
@ -147,11 +148,12 @@ public:
bool HasAlpha() const;
protected:
#ifndef __WXGTK3__
#if wxUSE_IMAGE
void InitFromImage(const wxImage& image, int depth, double scale);
#ifndef __WXGTK3__
bool CreateFromImage(const wxImage& image, int depth);
#endif // wxUSE_IMAGE
#endif
#endif // wxUSE_IMAGE
virtual wxGDIRefData* CreateGDIRefData() const wxOVERRIDE;
virtual wxGDIRefData* CloneGDIRefData(const wxGDIRefData* data) const wxOVERRIDE;

View File

@ -69,6 +69,7 @@ public:
wxBitmap( const char* const* bits );
wxBitmap( const wxString &filename, wxBitmapType type = wxBITMAP_DEFAULT_TYPE );
wxBitmap( const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0 ) { (void)CreateFromImage(image, depth); }
wxBitmap( const wxImage& image, const wxDC& WXUNUSED(dc) ) { (void)CreateFromImage(image); }
explicit wxBitmap(const wxCursor& cursor);
virtual ~wxBitmap();

View File

@ -114,6 +114,7 @@ public:
// Convert from wxImage:
wxBitmap(const wxImage& image, int depth = -1, double scale = 1.0);
wxBitmap(const wxImage& image, const wxDC& dc);
// Convert from wxIcon
wxBitmap(const wxIcon& icon) { CopyFromIcon(icon); }
@ -236,6 +237,9 @@ protected:
virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE;
virtual bool DoCreate(const wxSize& sz, double scale, int depth) wxOVERRIDE;
private:
void InitFromImage(const wxImage& image, int depth, double scale);
};
#endif // _WX_BITMAP_H_

View File

@ -24,6 +24,7 @@ public:
wxBitmap(const char* const* bits);
wxBitmap(const wxString &filename, wxBitmapType type = wxBITMAP_TYPE_XPM);
wxBitmap(const wxImage& image, int depth = wxBITMAP_SCREEN_DEPTH, double scale = 1.0);
wxBitmap(const wxImage& image, const wxDC& dc);
// Convert from wxIcon / wxCursor
wxBitmap(const wxIcon& icon) { CopyFromIcon(icon); }
@ -77,6 +78,9 @@ protected:
virtual wxGDIRefData *CreateGDIRefData() const wxOVERRIDE;
virtual wxGDIRefData *CloneGDIRefData(const wxGDIRefData *data) const wxOVERRIDE;
private:
void InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale));
wxDECLARE_DYNAMIC_CLASS(wxBitmap);
};

View File

@ -84,6 +84,7 @@ public:
#if wxUSE_IMAGE
wxBitmap( const wxImage& image, int depth = -1, double WXUNUSED(scale) = 1.0 ) { (void)CreateFromImage(image, depth); }
wxBitmap( const wxImage& image, const wxDC& WXUNUSED(dc) ) { (void)CreateFromImage(image); }
wxImage ConvertToImage() const;
bool CreateFromImage(const wxImage& image, int depth = -1);
#endif // wxUSE_IMAGE

View File

@ -367,6 +367,23 @@ public:
*/
wxBitmap(const wxImage& img, int depth = wxBITMAP_SCREEN_DEPTH);
/**
Creates a bitmap compatible with the given DC from the given image.
This constructor initializes the bitmap with the data of the given
image, which must be valid, but inherits the scaling factor from the
given device context instead of simply using the default factor of 1.
@param img
Platform-independent wxImage object.
@param dc
DC from which the scaling factor is inherited
@since 3.1.7 (previously this constructor overload was only available
in wxMSW port)
*/
wxBitmap(const wxImage& img, const wxDC& dc);
/**
Creates bitmap corresponding to the given cursor.

View File

@ -417,7 +417,7 @@ bool wxBitmap::CreateWithFormat(int width, int height, int dfbFormat)
}
#if wxUSE_IMAGE
wxBitmap::wxBitmap(const wxImage& imageOrig, int depth, double WXUNUSED(scale))
void wxBitmap::InitFromImage(const wxImage& imageOrig, int depth, double WXUNUSED(scale))
{
wxCHECK_RET( imageOrig.IsOk(), wxT("invalid image") );

View File

@ -16,15 +16,12 @@
#include "wx/image.h"
#include "wx/colour.h"
#include "wx/cursor.h"
#include "wx/dc.h"
#endif
#include "wx/math.h"
#include "wx/rawbmp.h"
#ifdef __WXGTK3__
#include "wx/dc.h"
#endif
#include "wx/gtk/private/object.h"
#include "wx/gtk/private.h"
@ -593,7 +590,7 @@ static void CopyImageData(
#if wxUSE_IMAGE
#ifdef __WXGTK3__
wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
void wxBitmap::InitFromImage(const wxImage& image, int depth, double scale)
{
wxCHECK_RET(image.IsOk(), "invalid image");
@ -640,7 +637,7 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
}
}
#else
wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale))
void wxBitmap::InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale))
{
wxCHECK_RET(image.IsOk(), "invalid image");
@ -786,6 +783,16 @@ bool wxBitmap::CreateFromImageAsPixbuf(const wxImage& image)
}
#endif
wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
{
InitFromImage(image, depth, scale);
}
wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc)
{
InitFromImage(image, -1, dc.GetContentScaleFactor());
}
wxImage wxBitmap::ConvertToImage() const
{
#ifdef __WXGTK3__

View File

@ -1134,7 +1134,7 @@ bool wxBitmap::Create(const void* data, wxBitmapType type, int width, int height
#if wxUSE_IMAGE
wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
void wxBitmap::InitFromImage(const wxImage& image, int depth, double scale)
{
wxCHECK_RET( image.IsOk(), wxT("invalid image") );
@ -1235,6 +1235,16 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
}
}
wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
{
InitFromImage(image, depth, scale);
}
wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc)
{
InitFromImage(image, -1, dc.GetContentScaleFactor());
}
wxImage wxBitmap::ConvertToImage() const
{
wxImage image;

View File

@ -14,6 +14,7 @@
#include <QtWidgets/QLabel>
#ifndef WX_PRECOMP
#include "wx/dc.h"
#include "wx/icon.h"
#include "wx/image.h"
#endif // WX_PRECOMP
@ -209,7 +210,7 @@ wxBitmap::wxBitmap(const wxString &filename, wxBitmapType type )
LoadFile(filename, type);
}
wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale) )
void wxBitmap::InitFromImage(const wxImage& image, int depth, double WXUNUSED(scale) )
{
Qt::ImageConversionFlags flags = 0;
if (depth == 1)
@ -217,6 +218,16 @@ wxBitmap::wxBitmap(const wxImage& image, int depth, double WXUNUSED(scale) )
m_refData = new wxBitmapRefData(QPixmap::fromImage(ConvertImage(image), flags));
}
wxBitmap::wxBitmap(const wxImage& image, int depth, double scale)
{
InitFromImage(image, depth, scale);
}
wxBitmap::wxBitmap(const wxImage& image, const wxDC& dc)
{
InitFromImage(image, -1, dc.GetContentScaleFactor());
}
wxBitmap::wxBitmap(const wxCursor& cursor)
{
// note that pixmap could be invalid if is not a pixmap cursor

View File

@ -1699,12 +1699,9 @@ TEST_CASE("Bitmap::DC", "[bitmap][dc]")
wxBitmap bmp(10, 10, dc);
CHECK( bmp.IsOk() );
// wxBitmap ctor from wxImage and wxDC is not available in the other ports.
#ifdef __WXMSW__
wxImage image(10, 10);
wxBitmap bmpFromImage(image, dc);
CHECK( bmpFromImage.IsOk() );
#endif // __WXMSW__
#endif // wxUSE_SVG
}
@ -1727,7 +1724,6 @@ TEST_CASE("Bitmap::ScaleFactor", "[bitmap][dc][scale]")
CHECK( bmp2.GetScaleFactor() == 2 );
CHECK( bmp2.GetSize() == wxSize(8, 8) );
#ifdef __WXMSW__
// A compatible bitmap created from wxImage and this DC should also inherit
// the same scale factor, but its size should be still the same as that of
// the image.
@ -1735,7 +1731,6 @@ TEST_CASE("Bitmap::ScaleFactor", "[bitmap][dc][scale]")
wxBitmap bmp3(img, dc);
CHECK( bmp3.GetScaleFactor() == 2 );
CHECK( bmp3.GetSize() == wxSize(16, 16) );
#endif // __WXMSW__
}
#endif // ports with scaled bitmaps support