Skip to content

Commit 44164e7

Browse files
committed
Make exporting transparent JPGs use white not black
1 parent cacebbf commit 44164e7

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

editor/src/node_graph_executor.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ impl NodeGraphExecutor {
478478
use image::buffer::ConvertBuffer;
479479
use image::{ImageFormat, RgbImage, RgbaImage};
480480

481-
let Some(image) = RgbaImage::from_raw(width, height, data) else {
481+
let Some(mut image) = RgbaImage::from_raw(width, height, data) else {
482482
return Err("Failed to create image buffer for export".to_string());
483483
};
484484

@@ -493,6 +493,14 @@ impl NodeGraphExecutor {
493493
}
494494
}
495495
FileType::Jpg => {
496+
// Composite onto a white background since JPG doesn't support transparency
497+
for pixel in image.pixels_mut() {
498+
let [r, g, b, a] = pixel.0;
499+
let alpha = a as f32 / 255.;
500+
let blend = |channel: u8| (channel as f32 * alpha + 255. * (1. - alpha)).round() as u8;
501+
*pixel = image::Rgba([blend(r), blend(g), blend(b), 255]);
502+
}
503+
496504
let image: RgbImage = image.convert();
497505
let result = image.write_to(&mut cursor, ImageFormat::Jpeg);
498506
if let Err(err) = result {

0 commit comments

Comments
 (0)