servo: Merge #3162 - Update to png::Image::pixels as an enum (from SimonSapin:png-enum)

Source-Repo: https://github.com/servo/servo
Source-Revision: b491f56e591ed8f90fa1bda1331cbb3b0507a71b
This commit is contained in:
Simon Sapin 2014-08-27 08:49:40 -06:00
Родитель 3d051829b7
Коммит ae8b292e1b
4 изменённых файлов: 36 добавлений и 33 удалений

Просмотреть файл

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

Просмотреть файл

@ -15,7 +15,7 @@ use geom::size::Size2D;
use geom::side_offsets::SideOffsets2D;
use libc::types::common::c99::uint16_t;
use libc::size_t;
use png::{RGBA8, K8, KA8};
use png::{RGB8, RGBA8, K8, KA8};
use servo_net::image::base::Image;
use servo_util::geometry::Au;
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>>) {
let size = Size2D(image.width as i32, image.height as i32);
let pixel_width = match image.color_type {
RGBA8 => 4,
K8 => 1,
KA8 => 2,
_ => fail!("color type not supported"),
let (pixel_width, pixels) = match image.pixels {
RGBA8(ref pixels) => (4, pixels.as_slice()),
RGB8(_) | K8(_) | KA8(_) => fail!("color type not supported"),
};
let stride = image.width * pixel_width;
self.draw_target.make_current();
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,
stride as i32,
B8G8R8A8);

Просмотреть файл

@ -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.
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;
}
}
_ => {}
fn byte_swap(data: &mut [u8]) {
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;
}
}
@ -40,7 +35,12 @@ pub fn load_from_memory(buffer: &[u8]) -> Option<Image> {
if png::is_png(buffer) {
match png::load_png_from_memory(buffer) {
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)
}
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) {
stb_image::ImageU8(mut image) => {
assert!(image.depth == 4);
byte_swap(png::RGBA8, image.data.as_mut_slice());
byte_swap(image.data.as_mut_slice());
Some(png::Image {
width: image.width as u32,
height: image.height as u32,
color_type: png::RGBA8,
pixels: image.data
pixels: png::RGBA8(image.data)
})
}
stb_image::ImageF32(_image) => fail!("HDR images not implemented"),

Просмотреть файл

@ -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 mut args = reftest.servo_args.clone();
// GPU rendering is the default
@ -228,14 +228,22 @@ fn capture(reftest: &Reftest, side: uint) -> png::Image {
};
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) {
let left = capture(&reftest, 0);
let right = capture(&reftest, 1);
let (left_width, left_height, left_bytes) = capture(&reftest, 0);
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 {
// White for correct
0xFF
@ -253,10 +261,9 @@ fn check_reftest(reftest: Reftest) {
let output = from_str::<Path>(output_str.as_slice()).unwrap();
let mut img = png::Image {
width: left.width,
height: left.height,
color_type: png::RGBA8,
pixels: pixels,
width: left_width,
height: left_height,
pixels: png::RGBA8(pixels),
};
let res = png::store_png(&mut img, &output);
assert!(res.is_ok());