Skip to content

Commit

Permalink
Correct a couple spots where inaccurate integer math was used for col…
Browse files Browse the repository at this point in the history
…or mult
  • Loading branch information
coornio committed Jan 15, 2025
1 parent 0f45afe commit c1f0614
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Assistants/Typedefs.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ struct alignas(4) RGBA {
constexpr operator u32() const noexcept { return R << 24 | G << 16 | B << 8 | A; }
};

inline constexpr u8 ChannelPremul(const u8 color, const u8 alpha) noexcept {
return ((color * (alpha | alpha << 8)) + 0x8080) >> 16;
inline constexpr u8 IntColorMult(const u8 color1, const u8 color2) noexcept {
return ((color1 * (color2 | color2 << 8)) + 0x8080) >> 16;
}

template <typename T, typename E>
Expand Down
12 changes: 6 additions & 6 deletions src/Systems/CHIP8/Cores/MEGACHIP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -393,8 +393,8 @@ void MEGACHIP::initializeFontColors() noexcept {
}

RGBA MEGACHIP::blendPixel(RGBA src, RGBA dst) const noexcept {
src.A = (src.A * mTexture.opacity) >> 8 & 0xFF;
if (src.A == 0x00) [[unlikely]] { return dst; }
src.A = IntColorMult(src.A, mTexture.opacity);
if (src.A == 0x0) [[unlikely]] { return dst; }

RGBA out{
intBlendAlgo(src.R, dst.R),
Expand All @@ -405,9 +405,9 @@ RGBA MEGACHIP::blendPixel(RGBA src, RGBA dst) const noexcept {
if (src.A < 0xFF) {
const auto dW{ static_cast<u8>(~src.A) };

out.R = 0xFF & ChannelPremul(dst.R, dW) + ChannelPremul(out.R, src.A);
out.G = 0xFF & ChannelPremul(dst.G, dW) + ChannelPremul(out.G, src.A);
out.B = 0xFF & ChannelPremul(dst.B, dW) + ChannelPremul(out.B, src.A);
out.R = 0xFF & IntColorMult(dst.R, dW) + IntColorMult(out.R, src.A);
out.G = 0xFF & IntColorMult(dst.G, dW) + IntColorMult(out.G, src.A);
out.B = 0xFF & IntColorMult(dst.B, dW) + IntColorMult(out.B, src.A);
out.A = 0xFF & std::min(src.A + ((dst.A * dW) >> 8), 0xFF);
}

Expand All @@ -423,7 +423,7 @@ void MEGACHIP::setNewBlendAlgorithm(const s32 mode) noexcept {

case BlendMode::MULTIPLY:
intBlendAlgo = [](const u8 src, const u8 dst)
noexcept { return static_cast<u8>((src * dst) >> 8); };
noexcept { return IntColorMult(src, dst); };
break;

default:
Expand Down

0 comments on commit c1f0614

Please sign in to comment.