aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorhirschenberger <falco.hirschenberger@gmail.com>2015-03-11 14:52:16 +0100
committerhirschenberger <falco.hirschenberger@gmail.com>2015-03-11 14:52:16 +0100
commitab4b34d423260bc3b5d27f9f0b65769fdf834712 (patch)
treeab2726a390fde0447f9223d929047847206edf33
parente581648c75a55a5939a16f4089295154e38dbc23 (diff)
downloadservo-ab4b34d423260bc3b5d27f9f0b65769fdf834712.tar.gz
servo-ab4b34d423260bc3b5d27f9f0b65769fdf834712.zip
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.
-rw-r--r--components/net/image/base.rs14
1 files changed, 13 insertions, 1 deletions
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<Image> {
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<Image> {
}
}
}
+
+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
+ }
+}