BITMAP_BASE: Add const image data accessor

This allows a const BITMAP_BASE to expose const image
data. This is currently done with const_casts, which is
"OK", only as long as the source image is not declared
const, in which case it's undefined behaviour.

Also immediately dereference the pointer to a reference
if it's not checked to make the non-null requirements
explicit at the point of access.
This commit is contained in:
John Beard 2019-04-24 10:50:18 +01:00
parent 5e556f0c6a
commit 15bba27406
4 changed files with 23 additions and 19 deletions

View File

@ -422,10 +422,11 @@ void CAIRO_GAL_BASE::DrawBitmap( const BITMAP_BASE& aBitmap )
unsigned char* pix_buffer = cairo_image_surface_get_data( image );
// The pixel buffer of the initial bitmap:
auto bm_pix_buffer = const_cast<BITMAP_BASE&>( aBitmap ).GetImageData();
uint32_t mask_color = ( bm_pix_buffer->GetMaskRed() << 16 ) +
( bm_pix_buffer->GetMaskGreen() << 8 ) +
( bm_pix_buffer->GetMaskBlue() );
const wxImage& bm_pix_buffer = *aBitmap.GetImageData();
uint32_t mask_color = ( bm_pix_buffer.GetMaskRed() << 16 ) +
( bm_pix_buffer.GetMaskGreen() << 8 ) +
( bm_pix_buffer.GetMaskBlue() );
// Copy the source bitmap to the cairo bitmap buffer.
// In cairo bitmap buffer, a ARGB32 bitmap is an ARGB pixel packed into a uint_32
@ -435,13 +436,13 @@ void CAIRO_GAL_BASE::DrawBitmap( const BITMAP_BASE& aBitmap )
for( int col = 0; col < w; col++ )
{
// Build the RGB24 pixel:
uint32_t pixel = bm_pix_buffer->GetRed( col, row ) << 16;
pixel += bm_pix_buffer->GetGreen( col, row ) << 8;
pixel += bm_pix_buffer->GetBlue( col, row );
uint32_t pixel = bm_pix_buffer.GetRed( col, row ) << 16;
pixel += bm_pix_buffer.GetGreen( col, row ) << 8;
pixel += bm_pix_buffer.GetBlue( col, row );
if( bm_pix_buffer->HasAlpha() )
pixel += bm_pix_buffer->GetAlpha( col, row ) << 24;
else if( bm_pix_buffer->HasMask() && pixel == mask_color )
if( bm_pix_buffer.HasAlpha() )
pixel += bm_pix_buffer.GetAlpha( col, row ) << 24;
else if( bm_pix_buffer.HasMask() && pixel == mask_color )
pixel += ( wxALPHA_TRANSPARENT << 24 );
else
pixel += ( wxALPHA_OPAQUE << 24 );

View File

@ -143,7 +143,7 @@ GLuint GL_BITMAP_CACHE::cacheBitmap( const BITMAP_BASE* aBitmap )
// make_unique initializes this to 0, so extra pixels are transparent
auto buf = std::make_unique<uint8_t[]>( ( bmp.w + extra_w ) * bmp.h * 4 );
auto imgData = const_cast<BITMAP_BASE*>( aBitmap )->GetImageData();
const wxImage& imgData = *aBitmap->GetImageData();
for( int y = 0; y < bmp.h; y++ )
{
@ -151,14 +151,14 @@ GLuint GL_BITMAP_CACHE::cacheBitmap( const BITMAP_BASE* aBitmap )
{
uint8_t *p = buf.get() + ( ( bmp.w + extra_w ) * y + x ) * 4;
p[0] = imgData->GetRed( x, y );
p[1] = imgData->GetGreen( x, y );
p[2] = imgData->GetBlue( x, y );
p[0] = imgData.GetRed( x, y );
p[1] = imgData.GetGreen( x, y );
p[2] = imgData.GetBlue( x, y );
if( imgData->HasAlpha() )
p[3] = imgData->GetAlpha( x, y );
else if( imgData->HasMask() && p[0] == imgData->GetMaskRed() &&
p[1] == imgData->GetMaskGreen() && p[2] == imgData->GetMaskBlue() )
if( imgData.HasAlpha() )
p[3] = imgData.GetAlpha( x, y );
else if( imgData.HasMask() && p[0] == imgData.GetMaskRed() &&
p[1] == imgData.GetMaskGreen() && p[2] == imgData.GetMaskBlue() )
p[3] = wxALPHA_TRANSPARENT;
else
p[3] = wxALPHA_OPAQUE;

View File

@ -2000,7 +2000,7 @@ void SCH_LEGACY_PLUGIN::saveBitmap( SCH_BITMAP* aBitmap )
{
wxCHECK_RET( aBitmap != NULL, "SCH_BITMAP* is NULL" );
wxImage* image = aBitmap->GetImage()->GetImageData();
const wxImage* image = aBitmap->GetImage()->GetImageData();
wxCHECK_RET( image != NULL, "wxImage* is NULL" );

View File

@ -74,7 +74,10 @@ public:
*/
double GetPixelScaleFactor() const { return m_pixelScaleFactor; }
void SetPixelScaleFactor( double aSF ) { m_pixelScaleFactor = aSF; }
wxImage* GetImageData() { return m_image; }
const wxImage* GetImageData() const { return m_image; }
void SetImage( wxImage* aImage )
{
delete m_image;