Skip to content

Commit 41fc00e

Browse files
committed
Prevent premultiplication on image import
1 parent afa3282 commit 41fc00e

File tree

2 files changed

+29
-9
lines changed

2 files changed

+29
-9
lines changed

wgpu-hal/src/gles/queue.rs

+12-7
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,12 @@ impl super::Queue {
388388
const UNPACK_PREMULTIPLY_ALPHA_WEBGL: u32 = 0x9241;
389389

390390
unsafe {
391-
gl.pixel_store_i32(UNPACK_FLIP_Y_WEBGL, src.flip_y as i32);
392-
gl.pixel_store_i32(
393-
UNPACK_PREMULTIPLY_ALPHA_WEBGL,
394-
dst_premultiplication as i32,
395-
);
391+
if src.flip_y {
392+
gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, true);
393+
}
394+
if dst_premultiplication {
395+
gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, true);
396+
}
396397
}
397398

398399
unsafe { gl.bind_texture(dst_target, Some(dst)) };
@@ -492,8 +493,12 @@ impl super::Queue {
492493
}
493494

494495
unsafe {
495-
gl.pixel_store_i32(UNPACK_FLIP_Y_WEBGL, 0);
496-
gl.pixel_store_i32(UNPACK_PREMULTIPLY_ALPHA_WEBGL, 0);
496+
if src.flip_y {
497+
gl.pixel_store_bool(UNPACK_FLIP_Y_WEBGL, false);
498+
}
499+
if dst_premultiplication {
500+
gl.pixel_store_bool(UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
501+
}
497502
}
498503
}
499504
C::CopyTextureToTexture {

wgpu/tests/external_texture.rs

+17-2
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,24 @@ async fn image_bitmap_import() {
1818
let blob = web_sys::Blob::new_with_u8_array_sequence(&array).unwrap();
1919

2020
// Parse the image from the blob
21-
let image_bitmap_promise = web_sys::window()
21+
let image_bitmap_function: js_sys::Function = web_sys::window()
2222
.unwrap()
23-
.create_image_bitmap_with_blob(&blob)
23+
.get("createImageBitmap")
24+
.unwrap()
25+
.dyn_into()
26+
.unwrap();
27+
28+
let options_arg = js_sys::Object::new();
29+
js_sys::Reflect::set(
30+
&options_arg,
31+
&wasm_bindgen::JsValue::from_str("premultiplyAlpha"),
32+
&wasm_bindgen::JsValue::from_str("none"),
33+
)
34+
.unwrap();
35+
let image_bitmap_promise: js_sys::Promise = image_bitmap_function
36+
.call2(&wasm_bindgen::JsValue::UNDEFINED, &blob, &options_arg)
37+
.unwrap()
38+
.dyn_into()
2439
.unwrap();
2540

2641
// Wait for the parsing to be done

0 commit comments

Comments
 (0)