diff options
-rw-r--r-- | components/net/image/base.rs | 19 | ||||
-rw-r--r-- | tests/ref/acid2_ref_broken.png | bin | 2174 -> 1951 bytes | |||
-rw-r--r-- | tests/ref/alpha_png_a.html | 13 | ||||
-rw-r--r-- | tests/ref/alpha_png_a.png | bin | 0 -> 16430 bytes | |||
-rw-r--r-- | tests/ref/alpha_png_b.html | 13 | ||||
-rw-r--r-- | tests/ref/alpha_png_b.png | bin | 0 -> 16643 bytes | |||
-rw-r--r-- | tests/ref/basic.list | 1 |
7 files changed, 44 insertions, 2 deletions
diff --git a/components/net/image/base.rs b/components/net/image/base.rs index deda4ee8556..37f53f509a4 100644 --- a/components/net/image/base.rs +++ b/components/net/image/base.rs @@ -27,6 +27,20 @@ fn byte_swap(data: &mut [u8]) { } } +// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this. +fn byte_swap_and_premultiply(data: &mut [u8]) { + let length = data.len(); + for i in range_step(0, length, 4) { + let r = data[i + 2]; + let g = data[i + 1]; + let b = data[i + 0]; + let a = data[i + 3]; + data[i + 0] = ((r as u32) * (a as u32) / 255) as u8; + data[i + 1] = ((g as u32) * (a as u32) / 255) as u8; + data[i + 2] = ((b as u32) * (a as u32) / 255) as u8; + } +} + pub fn load_from_memory(buffer: &[u8]) -> Option<Image> { if buffer.len() == 0 { return None; @@ -36,8 +50,9 @@ pub fn load_from_memory(buffer: &[u8]) -> Option<Image> { match png::load_png_from_memory(buffer) { Ok(mut png_image) => { match png_image.pixels { - png::RGB8(ref mut data) | png::RGBA8(ref mut data) => { - byte_swap(data.as_mut_slice()); + png::RGB8(ref mut data) => byte_swap(data.as_mut_slice()), + png::RGBA8(ref mut data) => { + byte_swap_and_premultiply(data.as_mut_slice()) } _ => {} } diff --git a/tests/ref/acid2_ref_broken.png b/tests/ref/acid2_ref_broken.png Binary files differindex 91c803bdadc..e46e613846c 100644 --- a/tests/ref/acid2_ref_broken.png +++ b/tests/ref/acid2_ref_broken.png diff --git a/tests/ref/alpha_png_a.html b/tests/ref/alpha_png_a.html new file mode 100644 index 00000000000..10abc613fb6 --- /dev/null +++ b/tests/ref/alpha_png_a.html @@ -0,0 +1,13 @@ +<!DOCTYPE html> +<html> +<head> +<title>Kaboom</title> +<style> +body { + background: #00ff00; +} +</style> +</head> +<body><img src=alpha_png_a.png></body> +</html> + diff --git a/tests/ref/alpha_png_a.png b/tests/ref/alpha_png_a.png Binary files differnew file mode 100644 index 00000000000..bfce3165adf --- /dev/null +++ b/tests/ref/alpha_png_a.png diff --git a/tests/ref/alpha_png_b.html b/tests/ref/alpha_png_b.html new file mode 100644 index 00000000000..d948302cee0 --- /dev/null +++ b/tests/ref/alpha_png_b.html @@ -0,0 +1,13 @@ +<!DOCTYPE html> +<html> +<head> +<title>Kaboom</title> +<style> +body { + background: #00ff00; +} +</style> +</head> +<body><img src=alpha_png_b.png></body> +</html> + diff --git a/tests/ref/alpha_png_b.png b/tests/ref/alpha_png_b.png Binary files differnew file mode 100644 index 00000000000..65c0375770c --- /dev/null +++ b/tests/ref/alpha_png_b.png diff --git a/tests/ref/basic.list b/tests/ref/basic.list index b785ddbd4c1..0aa73936d87 100644 --- a/tests/ref/basic.list +++ b/tests/ref/basic.list @@ -111,3 +111,4 @@ flaky_gpu,flaky_linux == acid2_noscroll.html acid2_ref_broken.html == float_right_intrinsic_width_a.html float_right_intrinsic_width_ref.html == fixed_width_overrides_child_intrinsic_width_a.html fixed_width_overrides_child_intrinsic_width_ref.html == abs_float_pref_width_a.html abs_float_pref_width_ref.html +== alpha_png_a.html alpha_png_b.html |