From 28279c0686ef129444de39cf968a7f7cab4a4724 Mon Sep 17 00:00:00 2001 From: kimci86 Date: Tue, 12 Apr 2022 15:46:19 +0200 Subject: [PATCH 1/3] Fix incorrect cast in Cursor::loadFromPixels Unix implementation --- src/SFML/Window/Unix/CursorImpl.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/SFML/Window/Unix/CursorImpl.cpp b/src/SFML/Window/Unix/CursorImpl.cpp index d0d51300d..f2cfdf802 100644 --- a/src/SFML/Window/Unix/CursorImpl.cpp +++ b/src/SFML/Window/Unix/CursorImpl.cpp @@ -80,10 +80,10 @@ bool CursorImpl::loadFromPixelsARGB(const Uint8* pixels, Vector2u size, Vector2u const std::size_t numPixels = size.x * size.y; for (std::size_t pixelIndex = 0; pixelIndex < numPixels; ++pixelIndex) { - cursorImage->pixels[pixelIndex] = static_cast(pixels[pixelIndex * 4 + 2] + - (pixels[pixelIndex * 4 + 1] << 8) + - (pixels[pixelIndex * 4 + 0] << 16) + - (pixels[pixelIndex * 4 + 3] << 24)); + cursorImage->pixels[pixelIndex] = static_cast(pixels[pixelIndex * 4 + 2] + + (pixels[pixelIndex * 4 + 1] << 8) + + (pixels[pixelIndex * 4 + 0] << 16) + + (pixels[pixelIndex * 4 + 3] << 24)); } // Create the cursor. From 470822cfe48c6a0aa5eaf2c9cda26ac5c8aee47c Mon Sep 17 00:00:00 2001 From: Peter Chapman Date: Mon, 14 Feb 2022 10:43:08 +1300 Subject: [PATCH 2/3] Fixed incorrect value for fully transparent pixels --- include/SFML/Graphics/Image.hpp | 10 +++++++--- src/SFML/Graphics/Image.cpp | 19 +++++++++++++------ 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/include/SFML/Graphics/Image.hpp b/include/SFML/Graphics/Image.hpp index 1b78529ad..75650c8c1 100644 --- a/include/SFML/Graphics/Image.hpp +++ b/include/SFML/Graphics/Image.hpp @@ -203,9 +203,13 @@ public: /// kind of feature in real-time you'd better use sf::RenderTexture. /// /// If \a sourceRect is empty, the whole image is copied. - /// If \a applyAlpha is set to true, the transparency of - /// source pixels is applied. If it is false, the pixels are - /// copied unchanged with their alpha value. + /// If \a applyAlpha is set to true, alpha blending is + /// applied from the source pixels to the destination pixels + /// 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 destX X coordinate of the destination position diff --git a/src/SFML/Graphics/Image.cpp b/src/SFML/Graphics/Image.cpp index f53c75a00..bab87e6f1 100644 --- a/src/SFML/Graphics/Image.cpp +++ b/src/SFML/Graphics/Image.cpp @@ -240,12 +240,19 @@ void Image::copy(const Image& source, unsigned int destX, unsigned int destY, co const Uint8* src = srcPixels + j * 4; Uint8* dst = dstPixels + j * 4; - // Interpolate RGBA components using the alpha value of the source pixel - Uint8 alpha = src[3]; - dst[0] = static_cast((src[0] * alpha + dst[0] * (255 - alpha)) / 255); - dst[1] = static_cast((src[1] * alpha + dst[1] * (255 - alpha)) / 255); - dst[2] = static_cast((src[2] * alpha + dst[2] * (255 - alpha)) / 255); - dst[3] = static_cast(alpha + dst[3] * (255 - alpha) / 255); + // Interpolate RGBA components using the alpha values of the destination and source pixels + Uint8 src_alpha = src[3]; + Uint8 dst_alpha = dst[3]; + Uint8 out_alpha = static_cast(src_alpha + dst_alpha - src_alpha * dst_alpha / 255); + + dst[3] = out_alpha; + + if (out_alpha) + for (int k = 0; k < 3; k++) + dst[k] = static_cast((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; From f7c88ee7ef4e1c705531cd614efb7dcff1f873cb Mon Sep 17 00:00:00 2001 From: kimci86 Date: Tue, 12 Apr 2022 15:25:47 +0200 Subject: [PATCH 3/3] Fix font pages not being created with the desired smoothness --- include/SFML/Graphics/Font.hpp | 12 +++++++++++- src/SFML/Graphics/Font.cpp | 22 +++++++++++++++++----- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/include/SFML/Graphics/Font.hpp b/include/SFML/Graphics/Font.hpp index eceabd50b..344d36c1a 100644 --- a/include/SFML/Graphics/Font.hpp +++ b/include/SFML/Graphics/Font.hpp @@ -335,7 +335,7 @@ private: //////////////////////////////////////////////////////////// struct Page { - Page(); + explicit Page(bool smooth); GlyphTable glyphs; //!< Table mapping code points to their corresponding glyph Texture texture; //!< Texture containing the pixels of the glyphs @@ -349,6 +349,16 @@ private: //////////////////////////////////////////////////////////// void cleanup(); + //////////////////////////////////////////////////////////// + /// \brief Find or create the glyphs page corresponding to the given character size + /// + /// \param characterSize Reference character size + /// + /// \return The glyphs page corresponding to \a characterSize + /// + //////////////////////////////////////////////////////////// + Page& loadPage(unsigned int characterSize) const; + //////////////////////////////////////////////////////////// /// \brief Load a new glyph and store it in the cache /// diff --git a/src/SFML/Graphics/Font.cpp b/src/SFML/Graphics/Font.cpp index ad6412f0f..1dee95015 100644 --- a/src/SFML/Graphics/Font.cpp +++ b/src/SFML/Graphics/Font.cpp @@ -348,7 +348,7 @@ const Font::Info& Font::getInfo() const const Glyph& Font::getGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness) const { // Get the page corresponding to the character size - GlyphTable& glyphs = m_pages[characterSize].glyphs; + GlyphTable& glyphs = loadPage(characterSize).glyphs; // Build the key by combining the glyph index (based on code point), bold flag, and outline thickness Uint64 key = combine(outlineThickness, bold, FT_Get_Char_Index(static_cast(m_face), codePoint)); @@ -476,7 +476,7 @@ float Font::getUnderlineThickness(unsigned int characterSize) const //////////////////////////////////////////////////////////// const Texture& Font::getTexture(unsigned int characterSize) const { - return m_pages[characterSize].texture; + return loadPage(characterSize).texture; } //////////////////////////////////////////////////////////// @@ -567,6 +567,18 @@ void Font::cleanup() } +//////////////////////////////////////////////////////////// +Font::Page& Font::loadPage(unsigned int characterSize) const +{ + // TODO: Remove this method and use try_emplace instead when updating to C++17 + PageTable::iterator pageIterator = m_pages.find(characterSize); + if (pageIterator == m_pages.end()) + pageIterator = m_pages.insert(std::make_pair(characterSize, Page(m_isSmooth))).first; + + return pageIterator->second; +} + + //////////////////////////////////////////////////////////// Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, float outlineThickness) const { @@ -652,7 +664,7 @@ Glyph Font::loadGlyph(Uint32 codePoint, unsigned int characterSize, bool bold, f height += 2 * padding; // Get the glyphs page corresponding to the character size - Page& page = m_pages[characterSize]; + Page& page = loadPage(characterSize); // Find a good position for the new glyph into the texture glyph.textureRect = findGlyphRect(page, width, height); @@ -842,7 +854,7 @@ bool Font::setCurrentSize(unsigned int characterSize) const //////////////////////////////////////////////////////////// -Font::Page::Page() : +Font::Page::Page(bool smooth) : nextRow(3) { // Make sure that the texture is initialized by default @@ -856,7 +868,7 @@ nextRow(3) // Create the texture texture.loadFromImage(image); - texture.setSmooth(true); + texture.setSmooth(smooth); } } // namespace sf