Skip to content

Commit 52c24a9

Browse files
authored
Do full image copy with one 'memcpy()' call (#9463)
1 parent 00fe5ee commit 52c24a9

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

src/engine/image.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/***************************************************************************
22
* fheroes2: https://github.com/ihhub/fheroes2 *
3-
* Copyright (C) 2020 - 2024 *
3+
* Copyright (C) 2020 - 2025 *
44
* *
55
* This program is free software; you can redistribute it and/or modify *
66
* it under the terms of the GNU General Public License as published by *
@@ -1291,8 +1291,29 @@ namespace fheroes2
12911291

12921292
void Copy( const Image & in, Image & out )
12931293
{
1294-
out.resize( in.width(), in.height() );
1295-
Copy( in, 0, 0, out, 0, 0, in.width(), in.height() );
1294+
if ( !out.singleLayer() && !in.singleLayer() ) {
1295+
// Both images have transform layer. Copy using the assignment operator.
1296+
out = in;
1297+
return;
1298+
}
1299+
1300+
const int32_t width = in.width();
1301+
const int32_t height = in.height();
1302+
1303+
out.resize( width, height );
1304+
1305+
// We do a full copy of an image.
1306+
const size_t size = static_cast<size_t>( width ) * height;
1307+
if ( out.singleLayer() ) {
1308+
// Copy only image layer. Input image can be single- or double-layer.
1309+
memcpy( out.image(), in.image(), size );
1310+
}
1311+
else {
1312+
assert( in.singleLayer() );
1313+
// Copy image layer and set transform to non-transparent mode.
1314+
memcpy( out.image(), in.image(), size );
1315+
memset( out.transform(), static_cast<uint8_t>( 0 ), size );
1316+
}
12961317
}
12971318

12981319
void Copy( const Image & in, int32_t inX, int32_t inY, Image & out, const Rect & outRoi )
@@ -1309,6 +1330,12 @@ namespace fheroes2
13091330
const int32_t widthIn = in.width();
13101331
const int32_t widthOut = out.width();
13111332

1333+
if ( inX == 0 && inY == 0 && outX == 0 && outY == 0 && width == widthIn && width == widthOut && height == in.height() && height == out.height() ) {
1334+
// Both images have identical width and height and a full copy is requested.
1335+
Copy( in, out );
1336+
return;
1337+
}
1338+
13121339
const int32_t offsetInY = inY * widthIn + inX;
13131340
const uint8_t * imageInY = in.image() + offsetInY;
13141341

0 commit comments

Comments
 (0)