From ab4b34d423260bc3b5d27f9f0b65769fdf834712 Mon Sep 17 00:00:00 2001 From: hirschenberger Date: Wed, 11 Mar 2015 14:52:16 +0100 Subject: [PATCH 1/2] Fix #5176 by premultiplying the alpha channel to the color channels This is GIF specific. It's also done when the image is PNG but PNG is handled separately with the PNG crate, whereas GIFs are handled by the stb-image crate and the distinction between alpha and non-alpha-supporting images was missing. --- components/net/image/base.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 + } +} From 2a91c317c042fd0af3011fd49e56f2a790f3e0e3 Mon Sep 17 00:00:00 2001 From: hirschenberger Date: Thu, 12 Mar 2015 11:02:29 +0100 Subject: [PATCH 2/2] Add reftest for issue #5176 --- tests/ref/alpha_gif_a.gif | Bin 0 -> 562 bytes tests/ref/alpha_gif_a.html | 13 +++++++++++++ tests/ref/alpha_gif_b.gif | Bin 0 -> 589 bytes tests/ref/alpha_gif_b.html | 13 +++++++++++++ tests/ref/basic.list | 1 + 5 files changed, 27 insertions(+) create mode 100644 tests/ref/alpha_gif_a.gif create mode 100644 tests/ref/alpha_gif_a.html create mode 100644 tests/ref/alpha_gif_b.gif create mode 100644 tests/ref/alpha_gif_b.html diff --git a/tests/ref/alpha_gif_a.gif b/tests/ref/alpha_gif_a.gif new file mode 100644 index 0000000000000000000000000000000000000000..298fe8f26273399874b9a68f92f0f7de2279834d GIT binary patch literal 562 zcmV-20?qwLNk%w1VE_RD0q_6-00930001HR1OWg50RSuj000000RRC20{(=LsmtvT zqnxzbi?iOm`wxcVNS5Y_rs~SJ?hD8AOxN~}=lag~{tpZahs2`sh)gP%%%<}RjY_A~ zs`ZM^YPa03_X`e-$K-YS={|^`_I7nD%c!-#&xX9S( z_y`#(IZ0V*d5M{+xyjk-`3V{-I!an`U)E>J4;(@dyAW^yUW|_`wJW_ zJWO0{e2ko|yv*F}{0to}JxyJ0eT|*1z0KY2{S6*2K2Bb4evY25zRuq6{th26KTlt8 ze~+K9zt7+A{|_*rz=4DU7(9qDp~8g>8#;UlF`~qY6f0W1h%uwa{*4?vdi)47q{xvZ zOPV~1GNsCuEL*yK2{We5nKWzKyoocX&Ye7a`uqtrsL-KAiyA$OG^x_1Oq)7=3N@kh3pcLZxpeE=y^A-m-o1SL`uz(y zu;9Uj3mZO+II-fzj2kC~%Rzm7e-_U+ue zd;bnTy!i3t%bP!sKE3+&?AyD44?n*A`Sk1CzmGq^{{8$5`}_Y7V1NP+NLT>?J9C*S AM*si- literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..3da985ea5a33944bf6040755ade01f4d753c5a1b GIT binary patch literal 589 zcmV-T0_CX>@2HM@dak z03rDV0SW*D04x9i0000100963{)CUI%k2-NoV41Dv);V>4~F7Mmgb43>dLn63&-+I z*Y=I)`p)(tOe&Ymrt=AnN~hGS^@`1Ex7@Dx3l59N_^!OfrJ7WJcux%!i5YQ zI(!H*qQr?5D_XpWF{8%*jT}3A{0K6n$dM#VnmmazrOK5oTe^G+Gp5X$G;7+ti8H6p zojiN`{0TIu(4j<&8a;|MsnVrPn>u|8HLBF9RI6IOiZ!d&tz5f${R%d$*s)~Gnmvm) zt=hG0+q!)VH?G{dbnDu^i#M;{y?p!n{R=p-;K76o8$OIUvEs#y8#{gsIkM!*lq*}l zj5)LB&73=X{tP;_=+UH0n?8*?wd&QZTf2S@JGSiEv}@bGjXSsQ-MoAI{tZ01@ZrRZ z8$XUbx$@=An>&9FJ-YPi)T>*+jy=2f?cBS2{|-L9`0?b+n?H{}z54a++q-`cKfe6= b^y}Ndk3YZu{rn62`~MGMfC3IkSOEY#3u!kG literal 0 HcmV?d00001 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