diff --git a/components/net/image/base.rs b/components/net/image/base.rs index 4acbd35cb75..46436ef8406 100644 --- a/components/net/image/base.rs +++ b/components/net/image/base.rs @@ -59,7 +59,12 @@ pub fn load_from_memory(buffer: &[u8]) -> Option { match stb_image::load_from_memory_with_depth(buffer, FORCE_DEPTH, true) { stb_image::LoadResult::ImageU8(mut image) => { assert!(image.depth == 4); - byte_swap(image.data.as_mut_slice()); + // handle gif separately because the alpha-channel has to be premultiplied + if is_gif(buffer) { + byte_swap_and_premultiply(image.data.as_mut_slice()); + } else { + byte_swap(image.data.as_mut_slice()); + } Some(png::Image { width: image.width as u32, height: image.height as u32, @@ -77,3 +82,10 @@ pub fn load_from_memory(buffer: &[u8]) -> Option { } } } + +fn is_gif(buffer: &[u8]) -> bool { + match buffer { + [b'G',b'I',b'F',b'8', n, b'a', ..] if n == b'7' || n == b'9' => true, + _ => false + } +} diff --git a/tests/ref/alpha_gif_a.gif b/tests/ref/alpha_gif_a.gif new file mode 100644 index 00000000000..298fe8f2627 Binary files /dev/null and b/tests/ref/alpha_gif_a.gif differ diff --git a/tests/ref/alpha_gif_a.html b/tests/ref/alpha_gif_a.html new file mode 100644 index 00000000000..f61c36ae435 --- /dev/null +++ b/tests/ref/alpha_gif_a.html @@ -0,0 +1,13 @@ + + + +Kaboom + + + + + diff --git a/tests/ref/alpha_gif_b.gif b/tests/ref/alpha_gif_b.gif new file mode 100644 index 00000000000..3da985ea5a3 Binary files /dev/null and b/tests/ref/alpha_gif_b.gif differ diff --git a/tests/ref/alpha_gif_b.html b/tests/ref/alpha_gif_b.html new file mode 100644 index 00000000000..97c1a67e715 --- /dev/null +++ b/tests/ref/alpha_gif_b.html @@ -0,0 +1,13 @@ + + + +Kaboom + + + + + diff --git a/tests/ref/basic.list b/tests/ref/basic.list index 4d26321575e..7737414c12f 100644 --- a/tests/ref/basic.list +++ b/tests/ref/basic.list @@ -13,6 +13,7 @@ fragment=top != ../html/acid2.html acid2_ref.html == acid1_a.html acid1_b.html == acid2_noscroll.html acid2_ref_broken.html == after_block_iteration.html after_block_iteration_ref.html +== alpha_gif_a.html alpha_gif_b.html == alpha_png_a.html alpha_png_b.html == anon_block_inherit_a.html anon_block_inherit_b.html flaky_cpu == append_style_a.html append_style_b.html