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

Don't interact with the transform layer if the image is a single layer #8149

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
125 changes: 95 additions & 30 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 @@ -503,7 +502,10 @@ 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 ) {
ihhub marked this conversation as resolved.
Show resolved Hide resolved
memset( transform(), static_cast<uint8_t>( 0 ), totalSize );
}
}
}

Expand Down Expand Up @@ -532,8 +534,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 @@ -556,7 +561,7 @@ namespace fheroes2

_singleLayer = image._singleLayer;

memcpy( _data.get(), image._data.get(), size * 2 );
memcpy( _data.get(), image._data.get(), _singleLayer ? size : size * 2 );
}

Sprite::Sprite( const int32_t width_, const int32_t height_, const int32_t x_ /* = 0 */, const int32_t y_ /* = 0 */ )
Expand Down Expand Up @@ -2683,7 +2688,18 @@ namespace fheroes2

const uint8_t * gamePalette = getGamePalette();

if ( in.singleLayer() && out.singleLayer() ) {
if ( in.singleLayer() ) {
if ( !out.singleLayer() ) {
// In this case we make the output image fully non-transparent in the given output area.

uint8_t * transformY = out.transform() + outY * widthOut + outX;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t * transformYEnd = transformY + heightRoiOut * widthOut;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved

for ( ; transformY != transformYEnd; transformY += widthOut ) {
memset( transformY, static_cast<uint8_t>( 0 ), widthRoiOut );
}
}

for ( int32_t y = 0; y < heightRoiOut; ++y, imageOutY += widthOut ) {
const double posY = static_cast<double>( y * heightRoiIn ) / heightRoiOut;
const int32_t startY = static_cast<int32_t>( posY );
Expand Down Expand Up @@ -2724,52 +2740,63 @@ namespace fheroes2
}
else {
const uint8_t * transformInY = in.transform() + offsetInY;
uint8_t * transformOutY = out.transform() + offsetOutY;
const bool isOutNotSingleLayer = !out.singleLayer();
uint8_t * transformOutY = isOutNotSingleLayer ? ( out.transform() + offsetOutY ) : nullptr;

for ( int32_t y = 0; y < heightRoiOut; ++y, imageOutY += widthOut, transformOutY += widthOut ) {
for ( int32_t y = 0; y < heightRoiOut; ++y, imageOutY += widthOut ) {
const double posY = static_cast<double>( y * heightRoiIn ) / heightRoiOut;
const int32_t startY = static_cast<int32_t>( posY );
const double coeffY = posY - startY;

uint8_t * imageOutX = imageOutY;
uint8_t * transformOutX = transformOutY;

for ( int32_t x = 0; x < widthRoiOut; ++x, ++imageOutX, ++transformOutX ) {
for ( int32_t x = 0; x < widthRoiOut; ++x, ++imageOutX ) {
const double posX = positionX[x];
const int32_t startX = static_cast<int32_t>( posX );
const int32_t offsetIn = startY * widthIn + startX;

const uint8_t * imageInX = imageInY + offsetIn;
const uint8_t * transformInX = transformInY + offsetIn;

if ( posX < widthIn - 1 && posY < heightRoiIn - 1 ) {
if ( *transformInX == 0 && *( transformInX + 1 ) == 0 && *( transformInX + widthRoiIn ) == 0 && *( transformInX + widthRoiIn + 1 ) == 0 ) {
const double coeffX = posX - startX;
const double coeff1 = ( 1 - coeffX ) * ( 1 - coeffY );
const double coeff2 = coeffX * ( 1 - coeffY );
const double coeff3 = ( 1 - coeffX ) * coeffY;
const double coeff4 = coeffX * coeffY;
if ( posX < widthIn - 1 && posY < heightRoiIn - 1 && *transformInX == 0 && *( transformInX + 1 ) == 0 && *( transformInX + widthRoiIn ) == 0
&& *( transformInX + widthRoiIn + 1 ) == 0 ) {
const double coeffX = posX - startX;
const double coeff1 = ( 1 - coeffX ) * ( 1 - coeffY );
const double coeff2 = coeffX * ( 1 - coeffY );
const double coeff3 = ( 1 - coeffX ) * coeffY;
const double coeff4 = coeffX * coeffY;

const uint8_t * id1 = gamePalette + static_cast<uint32_t>( *imageInX ) * 3;
const uint8_t * id2 = gamePalette + static_cast<uint32_t>( *( imageInX + 1 ) ) * 3;
const uint8_t * id3 = gamePalette + static_cast<uint32_t>( *( imageInX + widthIn ) ) * 3;
const uint8_t * id4 = gamePalette + static_cast<uint32_t>( *( imageInX + widthIn + 1 ) ) * 3;
const uint8_t * id1 = gamePalette + static_cast<uint32_t>( *imageInX ) * 3;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t * id2 = gamePalette + static_cast<uint32_t>( *( imageInX + 1 ) ) * 3;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t * id3 = gamePalette + static_cast<uint32_t>( *( imageInX + widthIn ) ) * 3;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t * id4 = gamePalette + static_cast<uint32_t>( *( imageInX + widthIn + 1 ) ) * 3;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved

const double red = *id1 * coeff1 + *id2 * coeff2 + *id3 * coeff3 + *id4 * coeff4 + 0.5;
const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + *( id3 + 1 ) * coeff3 + *( id4 + 1 ) * coeff4 + 0.5;
const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + *( id3 + 2 ) * coeff3 + *( id4 + 2 ) * coeff4 + 0.5;
const double red = *id1 * coeff1 + *id2 * coeff2 + *id3 * coeff3 + *id4 * coeff4 + 0.5;
const double green = *( id1 + 1 ) * coeff1 + *( id2 + 1 ) * coeff2 + *( id3 + 1 ) * coeff3 + *( id4 + 1 ) * coeff4 + 0.5;
const double blue = *( id1 + 2 ) * coeff1 + *( id2 + 2 ) * coeff2 + *( id3 + 2 ) * coeff3 + *( id4 + 2 ) * coeff4 + 0.5;

*imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) );
}
else {
*imageOutX = GetPALColorId( static_cast<uint8_t>( red ), static_cast<uint8_t>( green ), static_cast<uint8_t>( blue ) );
}
else {
if ( isOutNotSingleLayer || *transformInX == 0 ) {
// Output image is double-layer or single-layer with non-transparent current pixel.
*imageOutX = *imageInX;
}
else if ( *transformInX != 1 ) {
// Apply a transformation.
*imageOutX = *( transformTable + ( *transformInX ) * 256 + *imageOutX );
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
}
}
else {
*imageOutX = *imageInX;

if ( isOutNotSingleLayer ) {
*transformOutX = *transformInX;
++transformOutX;
}
}

*transformOutX = *transformInX;
if ( !isOutNotSingleLayer ) {
transformOutY += widthOut;
}
}
}
Expand All @@ -2784,7 +2811,18 @@ namespace fheroes2
positionX[x] = ( x * widthRoiIn ) / widthRoiOut;
}

if ( in.singleLayer() && out.singleLayer() ) {
if ( in.singleLayer() ) {
if ( !out.singleLayer() ) {
// In this case we make the output image fully non-transparent in the given output area.

uint8_t * transformY = out.transform() + outY * widthOut + outX;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
const uint8_t * transformYEnd = transformY + heightRoiOut * widthOut;
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved

for ( ; transformY != transformYEnd; transformY += widthOut ) {
memset( transformY, static_cast<uint8_t>( 0 ), widthRoiOut );
}
}

for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, ++idY ) {
uint8_t * imageOutX = imageOutY;

Expand All @@ -2797,7 +2835,34 @@ namespace fheroes2
}
}
}
else if ( out.singleLayer() ) {
const uint8_t * transformInY = in.transform() + offsetInY;

for ( ; imageOutY != imageOutYEnd; imageOutY += widthOut, ++idY ) {
uint8_t * imageOutX = imageOutY;

const int32_t offset = ( ( idY * heightRoiIn ) / heightRoiOut ) * widthIn;
const uint8_t * imageInX = imageInY + offset;
const uint8_t * transformInX = transformInY + offset;

for ( const int32_t posX : positionX ) {
const uint8_t * transformIn = transformInX + posX;
if ( *transformIn > 0 ) {
if ( *transformIn != 1 ) {
// Apply a transformation.
*imageOutX = *( transformTable + ( *transformIn ) * 256 + *imageOutX );
Districh-ru marked this conversation as resolved.
Show resolved Hide resolved
}
}
else {
*imageOutX = *( imageInX + posX );
}

++imageOutX;
}
}
}
else {
// Both 'in' and 'out' are double-layer.
const uint8_t * transformInY = in.transform() + offsetInY;
uint8_t * transformOutY = out.transform() + offsetOutY;

Expand Down
7 changes: 7 additions & 0 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 Down
5 changes: 2 additions & 3 deletions src/engine/screen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1349,10 +1349,9 @@ namespace fheroes2
}

Image::resize( info.gameWidth, info.gameHeight );
_screenSize = { info.screenWidth, info.screenHeight };
Image::reset();

// To detect some UI artifacts by invalid code let's put all transform data into pixel skipping mode.
std::fill( transform(), transform() + width() * height(), static_cast<uint8_t>( 1 ) );
_screenSize = { info.screenWidth, info.screenHeight };
}

Display & Display::instance()
Expand Down
1 change: 1 addition & 0 deletions src/fheroes2/game/game_credits.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ namespace
fheroes2::Blit( goblin, output, output.width() - goblin.width() * 2, output.height() - goblin.height() - 10, true );

fheroes2::Image resizedOutput( 640, 480 );
resizedOutput._disableTransformLayer();
fheroes2::Resize( output, 0, 0, output.width(), output.height(), resizedOutput, 0, 0, resizedOutput.width(), resizedOutput.height() );

return output;
Expand Down