Skip to content

Commit

Permalink
Merge pull request servo#57 from zarazek/master
Browse files Browse the repository at this point in the history
Handle 16-bit chennels by stripping them to 8-bit.
  • Loading branch information
jdm committed Jan 28, 2015
2 parents 9b2445d + a09c33b commit ade2143
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,5 @@ extern {
pub fn RUST_png_set_add_alpha(png_ptr: *mut png_struct, val: u32, flag: c_int);
pub fn RUST_png_set_filler(png_ptr: *mut png_struct, val: u32, flag: c_int);
pub fn RUST_png_set_interlace_handling(png_ptr: *mut png_struct);
pub fn RUST_png_set_strip_16(png_ptr: *mut png_struct);
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ pub fn load_png_from_memory(image: &[u8]) -> Result<Image,String> {
let width = ffi::RUST_png_get_image_width(png_ptr, info_ptr) as usize;
let height = ffi::RUST_png_get_image_height(png_ptr, info_ptr) as usize;
let color_type = ffi::RUST_png_get_color_type(png_ptr, info_ptr);
let bit_depth = ffi::RUST_png_get_bit_depth(png_ptr, info_ptr);

// convert palette and grayscale to rgb
match color_type as c_int {
Expand All @@ -118,6 +119,11 @@ pub fn load_png_from_memory(image: &[u8]) -> Result<Image,String> {
_ => {}
}

// convert 16-bit channels to 8-bit
if bit_depth == 16 {
ffi::RUST_png_set_strip_16(png_ptr);
}

// add alpha channels
ffi::RUST_png_set_add_alpha(png_ptr, 0xff, ffi::FILLER_AFTER);
if ffi::RUST_png_get_valid(png_ptr, info_ptr, ffi::INFO_tRNS as u32) != 0 {
Expand Down

0 comments on commit ade2143

Please sign in to comment.