Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not allocate memory to transform layer if image is already set as single-layer #8241

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
bfe72a2
Don't interact with the transform layer if the image is a single layer.
Districh-ru Dec 17, 2023
dc56af5
Add assertion to get transform layer only for double-layer images, fi…
Districh-ru Dec 24, 2023
c7d6c6e
Fix fheroes2::Resize() for the case it in and out images have differe…
Districh-ru Dec 24, 2023
12fb09f
Merge branch 'ihhub:master' into single-layer-do-not-access-transform
Districh-ru Dec 24, 2023
ca8874d
Add assertions to more places where transform layer is accessed
Districh-ru Dec 24, 2023
309266e
Do not allocate memory for transform() layer when image is single-layer
Districh-ru Dec 24, 2023
03c726e
Merge branch 'master' into single-layer-no-transform-memory-allocate
Districh-ru Dec 28, 2023
c05b8c6
Merge branch 'ihhub:master' into single-layer-do-not-access-transform
Districh-ru Jan 4, 2024
8782450
Apply IWYU suggestion
Districh-ru Jan 4, 2024
8377456
Revert "Apply IWYU suggestion"
Districh-ru Jan 4, 2024
67edb14
Update copyrights ... can it be automated? :)
Districh-ru Jan 4, 2024
87733e7
Merge branch 'single-layer-do-not-access-transform' into single-layer…
Districh-ru Jan 4, 2024
6a83307
Merge branch 'master' into single-layer-no-transform-memory-allocate
Districh-ru Jan 7, 2024
dbb5fb8
Update copyright years where needed
Districh-ru Jan 7, 2024
6e7fe0c
Merge branch 'ihhub:master' into single-layer-no-transform-memory-all…
Districh-ru Jan 14, 2024
12969d6
Merge branch 'ihhub:master'
Districh-ru Jan 20, 2024
400b5cf
Fix format
Districh-ru Jan 20, 2024
46268db
Fix Clang-Tidy issue
Districh-ru Jan 20, 2024
4981b22
Revert changes in game_credits.cpp
Districh-ru Jan 20, 2024
3d97053
Merge branch 'ihhub:master' into single-layer-no-transform-memory-all…
Districh-ru Jan 21, 2024
613acd9
Merge branch 'ihhub:master' into single-layer-no-transform-memory-all…
Districh-ru Apr 14, 2024
491e19a
Merge branch 'ihhub:master' into single-layer-no-transform-memory-all…
Districh-ru May 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/engine/image.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
#include "image.h"

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstddef>
#include <cstdlib>
Expand Down Expand Up @@ -507,7 +506,11 @@ namespace fheroes2
if ( !empty() ) {
const size_t totalSize = static_cast<size_t>( _width ) * _height;
memset( image(), value, totalSize );
memset( transform(), static_cast<uint8_t>( 0 ), totalSize );

if ( !_singleLayer ) {
// For double-layer image: set the transform layer not to skip all data.
memset( transform(), static_cast<uint8_t>( 0 ), totalSize );
}
}
}

Expand All @@ -523,7 +526,8 @@ namespace fheroes2
return;
}

const size_t size = static_cast<size_t>( width_ ) * height_ * 2;
// Allocate memory only for the used layers.
const size_t size = static_cast<size_t>( width_ ) * height_ * ( _singleLayer ? 1 : 2 );

_data.reset( new uint8_t[size] );

Expand All @@ -536,8 +540,11 @@ namespace fheroes2
if ( !empty() ) {
const size_t totalSize = static_cast<size_t>( _width ) * _height;
memset( image(), static_cast<uint8_t>( 0 ), totalSize );
// Set the transform layer to skip all data.
memset( transform(), static_cast<uint8_t>( 1 ), totalSize );

if ( !_singleLayer ) {
// Set the transform layer to skip all data.
memset( transform(), static_cast<uint8_t>( 1 ), totalSize );
}
}
}

Expand All @@ -549,7 +556,8 @@ namespace fheroes2
return;
}

const size_t size = static_cast<size_t>( image._width ) * image._height * 2;
// Allocate memory and copy data only for the used layers.
const size_t size = static_cast<size_t>( image._width ) * image._height * ( _singleLayer ? 1 : 2 );

_singleLayer = image._singleLayer;

Expand Down
19 changes: 14 additions & 5 deletions src/engine/image.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
***************************************************************************/
#pragma once

#include <cassert>
#include <cstdint>
#include <memory>
#include <utility>
Expand Down Expand Up @@ -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();
}

Expand All @@ -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;
Expand Down
Loading