diff --git a/src/engine/image.cpp b/src/engine/image.cpp index 01b504f05f8..bd31929de50 100644 --- a/src/engine/image.cpp +++ b/src/engine/image.cpp @@ -21,7 +21,6 @@ #include "image.h" #include -#include #include #include #include @@ -507,7 +506,11 @@ namespace fheroes2 if ( !empty() ) { const size_t totalSize = static_cast( _width ) * _height; memset( image(), value, totalSize ); - memset( transform(), static_cast( 0 ), totalSize ); + + if ( !_singleLayer ) { + // For double-layer image: set the transform layer not to skip all data. + memset( transform(), static_cast( 0 ), totalSize ); + } } } @@ -523,7 +526,8 @@ namespace fheroes2 return; } - const size_t size = static_cast( width_ ) * height_ * 2; + // Allocate memory only for the used layers. + const size_t size = static_cast( width_ ) * height_ * ( _singleLayer ? 1 : 2 ); _data.reset( new uint8_t[size] ); @@ -536,8 +540,11 @@ namespace fheroes2 if ( !empty() ) { const size_t totalSize = static_cast( _width ) * _height; memset( image(), static_cast( 0 ), totalSize ); - // Set the transform layer to skip all data. - memset( transform(), static_cast( 1 ), totalSize ); + + if ( !_singleLayer ) { + // Set the transform layer to skip all data. + memset( transform(), static_cast( 1 ), totalSize ); + } } } @@ -549,7 +556,8 @@ namespace fheroes2 return; } - const size_t size = static_cast( image._width ) * image._height * 2; + // Allocate memory and copy data only for the used layers. + const size_t size = static_cast( image._width ) * image._height * ( _singleLayer ? 1 : 2 ); _singleLayer = image._singleLayer; diff --git a/src/engine/image.h b/src/engine/image.h index d626cdd7789..bb22f5c2e97 100644 --- a/src/engine/image.h +++ b/src/engine/image.h @@ -19,6 +19,7 @@ ***************************************************************************/ #pragma once +#include #include #include #include @@ -63,11 +64,17 @@ namespace fheroes2 uint8_t * transform() { + // Why do you want to get transform layer from the single-layer image? + assert( !_singleLayer ); + return _data.get() + width() * height(); } const uint8_t * transform() const { + // Why do you want to get transform layer from the single-layer image? + assert( !_singleLayer ); + return _data.get() + width() * height(); } @@ -76,15 +83,17 @@ namespace fheroes2 return !_data; } - void reset(); // makes image fully transparent (transform layer is set to 1) + // Set all data in the image layer to 0 and make double-layer images fully transparent (transform layer is set to 1). + void reset(); - void clear(); // makes the image empty + // Make the image empty. + void clear(); - // Fill 'image' layer with given value, setting 'transform' layer to 0. + // Fill 'image' layer with given value, setting the double-layer images 'transform' layer to 0. void fill( const uint8_t value ); - // This is an optional indicator for image processing functions. - // The whole image still consists of 2 layers but transform layer might be ignored in computations + // This is an indicator for image processing functions. + // The single-layer image can not contain transform layer so this layer is never accessed for such images. bool singleLayer() const { return _singleLayer;