diff --git a/Common/Render/AtlasGen.cpp b/Common/Render/AtlasGen.cpp index f40d813db6..9a14da1c95 100644 --- a/Common/Render/AtlasGen.cpp +++ b/Common/Render/AtlasGen.cpp @@ -231,6 +231,29 @@ static void blurAlpha(const std::vector &src, std::vector &dst, in } } +static inline uint32_t Over_ABGR(uint32_t front, uint32_t back) { + const uint32_t fr = front & 0xFFu; + const uint32_t fg = (front >> 8) & 0xFFu; // green + const uint32_t fb = (front >> 16) & 0xFFu; + const uint32_t fa = (front >> 24) & 0xFFu; + + const uint32_t br = back & 0xFFu; + const uint32_t bg = (back >> 8) & 0xFFu; // green + const uint32_t bb = (back >> 16) & 0xFFu; + const uint32_t ba = (back >> 24) & 0xFFu; + + const uint32_t invA = 255u - fa; + + // multiply then divide by 255 with rounding equivalent to (x*invA + 127)/255 + uint32_t outr = fr + (uint32_t)((br * invA + 127u) / 255u); + uint32_t outg = fg + (uint32_t)((bg * invA + 127u) / 255u); + uint32_t outb = fb + (uint32_t)((bb * invA + 127u) / 255u); + uint32_t outa = fa + (uint32_t)((ba * invA + 127u) / 255u); + + // pack back to ABGR + return (outa << 24) | (outb << 16) | (outg << 8) | outr; +} + void AddDropShadow(Image &img, int shadowSize, float intensity) { int radius = std::max(1, (int)(shadowSize * img.scale)); @@ -254,7 +277,7 @@ void AddDropShadow(Image &img, int shadowSize, float intensity) { // Target buffer with transparent background std::vector newData(newW * newH, 0); - // Draw the computed shadow first (black, blurred alpha) + // Draw the computed shadow first (black, blurred alpha - automatically premultiplied). for (int y = 0; y < newH; y++) { for (int x = 0; x < newW; x++) { float a = blurred[y * newW + x]; @@ -269,10 +292,9 @@ void AddDropShadow(Image &img, int shadowSize, float intensity) { for (int x = 0; x < img.w; x++) { u32 c = img.dat[y * img.w + x]; if ((c >> 24) & 0xFF) { - float c_alpha = ((c >> 24) & 0xFF) * (1.0f / 255.0f); int nx = x + radius; int ny = y + radius; - newData[ny * newW + nx] = colorBlend(c, newData[ny * newW + nx], c_alpha); + newData[ny * newW + nx] = Over_ABGR(c, newData[ny * newW + nx]); } } } diff --git a/UI/UIAtlas.cpp b/UI/UIAtlas.cpp index 6f00f691ff..96321e5ff1 100644 --- a/UI/UIAtlas.cpp +++ b/UI/UIAtlas.cpp @@ -60,7 +60,7 @@ static const ImageMeta imageIDs[] = { {"I_SHOULDER", false}, {"I_SHOULDER_LINE", true}, {"I_DIR", false}, - {"I_DIR_LINE", false}, + {"I_DIR_LINE", true}, {"I_SQUARE_SHAPE", false}, {"I_SQUARE_SHAPE_LINE", true}, {"I_CHECKEDBOX", false}, @@ -260,6 +260,8 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiS if (SAVE_DEBUG_IMAGES) { pngSave(Path(std::string("../buttons_") + PNGNameFromID(shapeId)), img.data(), img.width(), img.height(), 4); } + + img.ConvertToPremultipliedAlpha(); } shapeCount = (int)usedShapes.size(); @@ -321,15 +323,13 @@ Draw::Texture *GenerateUIAtlas(Draw::DrawContext *draw, Atlas *atlas, float dpiS ERROR_LOG(Log::G3D, "Failed to load %s", name.c_str()); } else { pngsLoaded++; + img.ConvertToPremultipliedAlpha(); } } } INFO_LOG(Log::G3D, " - Loaded %d png images in %.2f ms", pngsLoaded, pngStart.ElapsedMs()); Instant addStart = Instant::Now(); - for (int i = 0; i < images.size(); i++) { - images[i].ConvertToPremultipliedAlpha(); - } for (int i = 0; i < images.size(); i++) { bucket.AddImage(std::move(images[i]), i); }