Merge pull request #3162 from SimonSapin/png-enum

Update to png::Image::pixels as an enum.
This commit is contained in:
Jack Moffitt 2014-08-27 08:49:40 -06:00
commit b491f56e59
5 changed files with 37 additions and 34 deletions

View file

@ -895,8 +895,7 @@ impl IOCompositor {
let mut img = png::Image { let mut img = png::Image {
width: width as u32, width: width as u32,
height: height as u32, height: height as u32,
color_type: png::RGB8, pixels: png::RGB8(pixels),
pixels: pixels,
}; };
let res = png::store_png(&mut img, &path); let res = png::store_png(&mut img, &path);
assert!(res.is_ok()); assert!(res.is_ok());

View file

@ -15,7 +15,7 @@ use geom::size::Size2D;
use geom::side_offsets::SideOffsets2D; use geom::side_offsets::SideOffsets2D;
use libc::types::common::c99::uint16_t; use libc::types::common::c99::uint16_t;
use libc::size_t; use libc::size_t;
use png::{RGBA8, K8, KA8}; use png::{RGB8, RGBA8, K8, KA8};
use servo_net::image::base::Image; use servo_net::image::base::Image;
use servo_util::geometry::Au; use servo_util::geometry::Au;
use servo_util::opts::Opts; use servo_util::opts::Opts;
@ -100,17 +100,15 @@ impl<'a> RenderContext<'a> {
pub fn draw_image(&self, bounds: Rect<Au>, image: Arc<Box<Image>>) { pub fn draw_image(&self, bounds: Rect<Au>, image: Arc<Box<Image>>) {
let size = Size2D(image.width as i32, image.height as i32); let size = Size2D(image.width as i32, image.height as i32);
let pixel_width = match image.color_type { let (pixel_width, pixels) = match image.pixels {
RGBA8 => 4, RGBA8(ref pixels) => (4, pixels.as_slice()),
K8 => 1, RGB8(_) | K8(_) | KA8(_) => fail!("color type not supported"),
KA8 => 2,
_ => fail!("color type not supported"),
}; };
let stride = image.width * pixel_width; let stride = image.width * pixel_width;
self.draw_target.make_current(); self.draw_target.make_current();
let draw_target_ref = &self.draw_target; let draw_target_ref = &self.draw_target;
let azure_surface = draw_target_ref.create_source_surface_from_data(image.pixels.as_slice(), let azure_surface = draw_target_ref.create_source_surface_from_data(pixels,
size, size,
stride as i32, stride as i32,
B8G8R8A8); B8G8R8A8);

View file

@ -18,17 +18,12 @@ pub fn test_image_bin() -> Vec<u8> {
} }
// TODO(pcwalton): Speed up with SIMD, or better yet, find some way to not do this. // 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]) { fn byte_swap(data: &mut [u8]) {
match color_type { let length = data.len();
png::RGBA8 => { for i in range_step(0, length, 4) {
let length = data.len(); let r = data[i + 2];
for i in range_step(0, length, 4) { data[i + 2] = data[i + 0];
let r = data[i + 2]; data[i + 0] = r;
data[i + 2] = data[i + 0];
data[i + 0] = r;
}
}
_ => {}
} }
} }
@ -40,7 +35,12 @@ 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(mut png_image) => { Ok(mut png_image) => {
byte_swap(png_image.color_type, png_image.pixels.as_mut_slice()); match png_image.pixels {
png::RGB8(ref mut data) | png::RGBA8(ref mut data) => {
byte_swap(data.as_mut_slice());
}
_ => {}
}
Some(png_image) Some(png_image)
} }
Err(_err) => None, Err(_err) => None,
@ -53,12 +53,11 @@ pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
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(mut image) => { stb_image::ImageU8(mut image) => {
assert!(image.depth == 4); assert!(image.depth == 4);
byte_swap(png::RGBA8, image.data.as_mut_slice()); byte_swap(image.data.as_mut_slice());
Some(png::Image { Some(png::Image {
width: image.width as u32, width: image.width as u32,
height: image.height as u32, height: image.height as u32,
color_type: png::RGBA8, pixels: png::RGBA8(image.data)
pixels: image.data
}) })
} }
stb_image::ImageF32(_image) => fail!("HDR images not implemented"), stb_image::ImageF32(_image) => fail!("HDR images not implemented"),

@ -1 +1 @@
Subproject commit 869ec5657a628a72cbe0fcf9969e6a28eba4bf18 Subproject commit 0fae0f48754d1af9b33d208a87f9abc632dc2dc8

View file

@ -209,7 +209,7 @@ fn make_test(reftest: Reftest) -> TestDescAndFn {
} }
} }
fn capture(reftest: &Reftest, side: uint) -> png::Image { fn capture(reftest: &Reftest, side: uint) -> (u32, u32, Vec<u8>) {
let filename = format!("/tmp/servo-reftest-{:06u}-{:u}.png", reftest.id, side); let filename = format!("/tmp/servo-reftest-{:06u}-{:u}.png", reftest.id, side);
let mut args = reftest.servo_args.clone(); let mut args = reftest.servo_args.clone();
// GPU rendering is the default // GPU rendering is the default
@ -228,14 +228,22 @@ fn capture(reftest: &Reftest, side: uint) -> png::Image {
}; };
assert!(retval == ExitStatus(0)); assert!(retval == ExitStatus(0));
png::load_png(&from_str::<Path>(filename.as_slice()).unwrap()).unwrap() let image = png::load_png(&from_str::<Path>(filename.as_slice()).unwrap()).unwrap();
let rgba8_bytes = match image.pixels {
png::RGBA8(pixels) => pixels,
_ => fail!(),
};
(image.width, image.height, rgba8_bytes)
} }
fn check_reftest(reftest: Reftest) { fn check_reftest(reftest: Reftest) {
let left = capture(&reftest, 0); let (left_width, left_height, left_bytes) = capture(&reftest, 0);
let right = capture(&reftest, 1); let (right_width, right_height, right_bytes) = capture(&reftest, 1);
let pixels = left.pixels.iter().zip(right.pixels.iter()).map(|(&a, &b)| { assert_eq!(left_width, right_width);
assert_eq!(left_height, right_height);
let pixels = left_bytes.iter().zip(right_bytes.iter()).map(|(&a, &b)| {
if a as i8 - b as i8 == 0 { if a as i8 - b as i8 == 0 {
// White for correct // White for correct
0xFF 0xFF
@ -253,10 +261,9 @@ fn check_reftest(reftest: Reftest) {
let output = from_str::<Path>(output_str.as_slice()).unwrap(); let output = from_str::<Path>(output_str.as_slice()).unwrap();
let mut img = png::Image { let mut img = png::Image {
width: left.width, width: left_width,
height: left.height, height: left_height,
color_type: png::RGBA8, pixels: png::RGBA8(pixels),
pixels: pixels,
}; };
let res = png::store_png(&mut img, &output); let res = png::store_png(&mut img, &output);
assert!(res.is_ok()); assert!(res.is_ok());