net: Color space convert 32-bit ARGB PNGs as well.

This commit is contained in:
Patrick Walton 2013-12-10 18:57:46 -08:00
parent fc80be982a
commit 85f1af42c3
4 changed files with 39 additions and 19 deletions

View file

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::vec; use std::iter::range_step;
use stb_image = stb_image::image; use stb_image = stb_image::image;
use png; use png;
@ -25,10 +25,28 @@ pub fn test_image_bin() -> ~[u8] {
TEST_IMAGE.into_owned() TEST_IMAGE.into_owned()
} }
// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this.
fn byte_swap(color_type: png::ColorType, data: &mut [u8]) {
match color_type {
png::RGBA8 => {
let length = data.len();
for i in range_step(0, length, 4) {
let r = data[i + 2];
data[i + 2] = data[i + 0];
data[i + 0] = r;
}
}
_ => {}
}
}
pub fn load_from_memory(buffer: &[u8]) -> Option<Image> { pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
if png::is_png(buffer) { if png::is_png(buffer) {
match png::load_png_from_memory(buffer) { match png::load_png_from_memory(buffer) {
Ok(png_image) => Some(png_image), Ok(mut png_image) => {
byte_swap(png_image.color_type, png_image.pixels);
Some(png_image)
}
Err(_err) => None, Err(_err) => None,
} }
} else { } else {
@ -37,24 +55,10 @@ pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
static FORCE_DEPTH: uint = 4; static FORCE_DEPTH: uint = 4;
match stb_image::load_from_memory_with_depth(buffer, FORCE_DEPTH, true) { match stb_image::load_from_memory_with_depth(buffer, FORCE_DEPTH, true) {
stb_image::ImageU8(image) => { stb_image::ImageU8(mut image) => {
assert!(image.depth == 4); assert!(image.depth == 4);
// Do color space conversion :( byte_swap(png::RGBA8, image.data);
let data = do vec::from_fn(image.width * image.height * 4) |i| { Some(Image(image.width as u32, image.height as u32, png::RGBA8, image.data))
let color = i % 4;
let pixel = i / 4;
match color {
0 => image.data[pixel * 4 + 2],
1 => image.data[pixel * 4 + 1],
2 => image.data[pixel * 4 + 0],
3 => 0xffu8,
_ => fail!()
}
};
assert!(image.data.len() == data.len());
Some(Image(image.width as u32, image.height as u32, png::RGBA8, data))
} }
stb_image::ImageF32(_image) => fail!(~"HDR images not implemented"), stb_image::ImageF32(_image) => fail!(~"HDR images not implemented"),
stb_image::Error => None stb_image::Error => None

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Labyrinth was also a good movie because David Bowie and Muppets are cool</title>
</head>
<body>
<img src="png_rgba_colorspace_a.png">
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 510 B

View file

@ -0,0 +1,8 @@
<html>
<head>
<title>Labyrinth was also a good movie because David Bowie and Muppets are cool</title>
</head>
<body>
<div style="background-color: red; width: 100px; height: 100px;"></div>
</body>
</html>