Winit software renderer: Fix transparent background

Despite the documentation of softbuffer claiming that the high bits
should be 0, it seems to be interpreted as an alpha value when the
window is actually transparent.
And the values are considered as premultiplied alpha

Also use a Premultiplied aplha for the buffer so we can even have
transparent window with the software renderer
This commit is contained in:
Olivier Goffart 2023-05-03 13:50:28 +02:00 committed by Olivier Goffart
parent abbba8cbe8
commit 98747cd385
2 changed files with 36 additions and 15 deletions

View file

@ -502,6 +502,21 @@ impl TargetPixel for crate::graphics::image::Rgb8Pixel {
}
}
impl TargetPixel for PremultipliedRgbaColor {
fn blend(&mut self, color: PremultipliedRgbaColor) {
let a = (u8::MAX - color.alpha) as u16;
self.red = (self.red as u16 * a / 255) as u8 + color.red;
self.green = (self.green as u16 * a / 255) as u8 + color.green;
self.blue = (self.blue as u16 * a / 255) as u8 + color.blue;
self.alpha = (self.alpha as u16 + color.alpha as u16
- (self.alpha as u16 * color.alpha as u16) / 255) as u8;
}
fn from_rgb(r: u8, g: u8, b: u8) -> Self {
Self { red: r, green: g, blue: b, alpha: 255 }
}
}
/// A 16bit pixel that has 5 red bits, 6 green bits and 5 blue bits
#[repr(transparent)]
#[derive(Copy, Clone, Debug, PartialEq, Eq, Default)]