Skip to content

Commit d6b68ca

Browse files
committed
WinUIBackend: Kinda fix broken Image alpha blending
1 parent d31ca5c commit d6b68ca

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

Sources/WinUIBackend/WinUIBackend.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -828,12 +828,23 @@ public final class WinUIBackend: AppBackend {
828828
let buffer = try! bitmap.pixelBuffer.buffer!
829829
memcpy(buffer, rgbaData, min(Int(bitmap.pixelBuffer.length), rgbaData.count))
830830

831-
// Convert RGBA to BGRA in-place.
831+
// Convert RGBA to BGRA in-place, and apply janky transparency fix until we
832+
// figure out how to fix WinUI image blending (non-black transparent pixels
833+
// just don't seem to get blended at all, or at least pixels that are white
834+
// enough, haven't tested many colours).
832835
for i in 0..<(width * height) {
833836
let offset = i * 4
834-
let tmp = buffer[offset]
835-
buffer[offset] = buffer[offset + 2]
836-
buffer[offset + 2] = tmp
837+
if buffer[offset + 3] == 0 {
838+
// If transparent, make the pixel black (this is the janky blending fix).
839+
buffer[offset] = 0
840+
buffer[offset + 1] = 0
841+
buffer[offset + 2] = 0
842+
} else {
843+
// Swap R and B (RGBA to BGRA)
844+
let tmp = buffer[offset]
845+
buffer[offset] = buffer[offset + 2]
846+
buffer[offset + 2] = tmp
847+
}
837848
}
838849

839850
imageView.source = bitmap

0 commit comments

Comments
 (0)