From 15bba27406dd94a6db558438b2a52eff3b447efa Mon Sep 17 00:00:00 2001 From: John Beard Date: Wed, 24 Apr 2019 10:50:18 +0100 Subject: [PATCH] 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. --- common/gal/cairo/cairo_gal.cpp | 21 +++++++++++---------- common/gal/opengl/opengl_gal.cpp | 16 ++++++++-------- eeschema/sch_legacy_plugin.cpp | 2 +- include/bitmap_base.h | 3 +++ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/common/gal/cairo/cairo_gal.cpp b/common/gal/cairo/cairo_gal.cpp index bcec4cd34e..76a9da7e8d 100644 --- a/common/gal/cairo/cairo_gal.cpp +++ b/common/gal/cairo/cairo_gal.cpp @@ -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( 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 ); diff --git a/common/gal/opengl/opengl_gal.cpp b/common/gal/opengl/opengl_gal.cpp index fc34b4e495..5428ff6c0f 100644 --- a/common/gal/opengl/opengl_gal.cpp +++ b/common/gal/opengl/opengl_gal.cpp @@ -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( ( bmp.w + extra_w ) * bmp.h * 4 ); - auto imgData = const_cast( 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; diff --git a/eeschema/sch_legacy_plugin.cpp b/eeschema/sch_legacy_plugin.cpp index e028afcf2e..50e8c084ff 100644 --- a/eeschema/sch_legacy_plugin.cpp +++ b/eeschema/sch_legacy_plugin.cpp @@ -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" ); diff --git a/include/bitmap_base.h b/include/bitmap_base.h index 964f6ffff9..e34cb7b588 100644 --- a/include/bitmap_base.h +++ b/include/bitmap_base.h @@ -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;