From 17bc5b0025a0eb0a9dc92152e6244beb1dea1e92 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Wed, 14 Jul 2004 21:02:49 +0000 Subject: [PATCH] palette handling fixes don't free pixels passed to CreateSurfaceFrom --- stepmania/src/RageSurface.cpp | 26 ++++++++++++++++++++++---- stepmania/src/RageSurface.h | 5 +++++ stepmania/src/RageSurfaceUtils.cpp | 8 ++++---- 3 files changed, 31 insertions(+), 8 deletions(-) diff --git a/stepmania/src/RageSurface.cpp b/stepmania/src/RageSurface.cpp index a33afad3d4..ff24553afa 100644 --- a/stepmania/src/RageSurface.cpp +++ b/stepmania/src/RageSurface.cpp @@ -90,6 +90,18 @@ bool RageSurfaceFormat::MapRGBA( uint8_t r, uint8_t g, uint8_t b, uint8_t a, uin } bool RageSurfaceFormat::operator== ( const RageSurfaceFormat &rhs ) const +{ + if( !Equivalent(rhs) ) + return false; + + if( BytesPerPixel == 1 ) + if( memcmp( palette, rhs.palette, sizeof(RageSurfaceFormat) ) ) + return false; + + return true; +} + +bool RageSurfaceFormat::Equivalent( const RageSurfaceFormat &rhs ) const { #define COMP(a) if( a != rhs.a ) return false; COMP( BytesPerPixel ); @@ -97,8 +109,6 @@ bool RageSurfaceFormat::operator== ( const RageSurfaceFormat &rhs ) const COMP( Gmask ); COMP( Bmask ); COMP( Amask ); - if( BytesPerPixel == 1 ) - COMP( palette ); return true; } @@ -107,6 +117,7 @@ RageSurface::RageSurface() { format = &fmt; pixels = NULL; + pixels_owned = true; } RageSurface::RageSurface( const RageSurface &cpy ) @@ -117,8 +128,14 @@ RageSurface::RageSurface( const RageSurface &cpy ) h = cpy.h; pitch = cpy.pitch; flags = cpy.flags; - pixels = new uint8_t[ pitch*h ]; - memcpy( pixels, cpy.pixels, pitch*h ); + pixels_owned = true; + if( cpy.pixels ) + { + pixels = new uint8_t[ pitch*h ]; + memcpy( pixels, cpy.pixels, pitch*h ); + } + else + pixels = NULL; } RageSurface::~RageSurface() @@ -223,6 +240,7 @@ RageSurface *CreateSurfaceFrom( int width, int height, int BitsPerPixel, uint32_ pImg->flags = 0; pImg->pitch = pitch; pImg->pixels = pPixels; + pImg->pixels_owned = false; return pImg; } diff --git a/stepmania/src/RageSurface.h b/stepmania/src/RageSurface.h index 51f445270e..269c0f93ea 100644 --- a/stepmania/src/RageSurface.h +++ b/stepmania/src/RageSurface.h @@ -48,6 +48,10 @@ struct RageSurfaceFormat uint32_t MapNearestRGBA( uint8_t r, uint8_t g, uint8_t b, uint8_t a ) const; bool operator== ( const RageSurfaceFormat &rhs ) const; + + /* Like operator==, but ignores the palette (which is really a part of the + * surface, not the format). */ + bool Equivalent( const RageSurfaceFormat &rhs ) const; }; struct RageSurface @@ -56,6 +60,7 @@ struct RageSurface RageSurfaceFormat fmt; uint8_t *pixels; + bool pixels_owned; int32_t w, h, pitch; int32_t flags; diff --git a/stepmania/src/RageSurfaceUtils.cpp b/stepmania/src/RageSurfaceUtils.cpp index bb64a26c6c..e680a61d84 100644 --- a/stepmania/src/RageSurfaceUtils.cpp +++ b/stepmania/src/RageSurfaceUtils.cpp @@ -137,8 +137,8 @@ void RageSurfaceUtils::CopySurface( RageSurface *src, RageSurface *dest ) /* Copy the palette, if we have one. */ if( src->format->BitsPerPixel == 8 && dest->format->BitsPerPixel == 8 ) { - ASSERT( dest->format->palette ); - dest->fmt.palette = src->fmt.palette; + ASSERT( dest->fmt.palette ); + *dest->fmt.palette = *src->fmt.palette; } Blit( src, dest, -1, -1, false ); @@ -150,8 +150,8 @@ bool RageSurfaceUtils::ConvertSurface( RageSurface *src, RageSurface *&dst, { dst = CreateSurface( width, height, bpp, R, G, B, A ); - /* If the formats are the same, no conversion is needed. */ - if( width == src->w && height == src->h && src->format == dst->format ) + /* If the formats are the same, no conversion is needed. Ignore the palette. */ + if( width == src->w && height == src->h && src->format->Equivalent( *dst->format ) ) { delete dst; dst = NULL;