Fixed incorrect value for fully transparent pixels

This commit is contained in:
Peter Chapman 2022-02-14 10:43:08 +13:00 committed by Lukas Dürrenberger
parent 28279c0686
commit 470822cfe4
2 changed files with 20 additions and 9 deletions

View File

@ -203,9 +203,13 @@ public:
/// kind of feature in real-time you'd better use sf::RenderTexture. /// kind of feature in real-time you'd better use sf::RenderTexture.
/// ///
/// If \a sourceRect is empty, the whole image is copied. /// If \a sourceRect is empty, the whole image is copied.
/// If \a applyAlpha is set to true, the transparency of /// If \a applyAlpha is set to true, alpha blending is
/// source pixels is applied. If it is false, the pixels are /// applied from the source pixels to the destination pixels
/// copied unchanged with their alpha value. /// using the \b over operator. If it is false, the source
/// pixels are copied unchanged with their alpha value.
///
/// See https://en.wikipedia.org/wiki/Alpha_compositing for
/// details on the \b over operator.
/// ///
/// \param source Source image to copy /// \param source Source image to copy
/// \param destX X coordinate of the destination position /// \param destX X coordinate of the destination position

View File

@ -240,12 +240,19 @@ void Image::copy(const Image& source, unsigned int destX, unsigned int destY, co
const Uint8* src = srcPixels + j * 4; const Uint8* src = srcPixels + j * 4;
Uint8* dst = dstPixels + j * 4; Uint8* dst = dstPixels + j * 4;
// Interpolate RGBA components using the alpha value of the source pixel // Interpolate RGBA components using the alpha values of the destination and source pixels
Uint8 alpha = src[3]; Uint8 src_alpha = src[3];
dst[0] = static_cast<Uint8>((src[0] * alpha + dst[0] * (255 - alpha)) / 255); Uint8 dst_alpha = dst[3];
dst[1] = static_cast<Uint8>((src[1] * alpha + dst[1] * (255 - alpha)) / 255); Uint8 out_alpha = static_cast<Uint8>(src_alpha + dst_alpha - src_alpha * dst_alpha / 255);
dst[2] = static_cast<Uint8>((src[2] * alpha + dst[2] * (255 - alpha)) / 255);
dst[3] = static_cast<Uint8>(alpha + dst[3] * (255 - alpha) / 255); dst[3] = out_alpha;
if (out_alpha)
for (int k = 0; k < 3; k++)
dst[k] = static_cast<Uint8>((src[k] * src_alpha + dst[k] * (out_alpha - src_alpha)) / out_alpha);
else
for (int k = 0; k < 3; k++)
dst[k] = src[k];
} }
srcPixels += srcStride; srcPixels += srcStride;