From aceef51fab02351ab15fe38bae4d3a9b2c152e08 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 15 Jan 2015 16:01:45 -0500 Subject: [PATCH 1/2] Fix obsolete Vec::from_fn, Vec::from_elem, slice::raw::buf_as_slice. --- src/lib.rs | 34 ++++++++++++++++------------------ 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 3b54695..b5d52d1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,12 +54,11 @@ pub extern fn read_data(png_ptr: *mut ffi::png_struct, data: *mut u8, length: si let io_ptr = ffi::RUST_png_get_io_ptr(png_ptr); let image_data: &mut ImageData = mem::transmute(io_ptr); let len = length as uint; - slice::raw::mut_buf_as_slice(data, len, |buf| { - let end_pos = std::cmp::min(image_data.data.len()-image_data.offset, len); - let src = image_data.data.slice(image_data.offset, image_data.offset+end_pos); - ptr::copy_memory(buf.as_mut_ptr(), src.as_ptr(), src.len()); - image_data.offset += end_pos; - }); + let buf = slice::from_raw_mut_buf(&data, len); + let end_pos = std::cmp::min(image_data.data.len()-image_data.offset, len); + let src = image_data.data.slice(image_data.offset, image_data.offset+end_pos); + ptr::copy_memory(buf.as_mut_ptr(), src.as_ptr(), src.len()); + image_data.offset += end_pos; } } @@ -142,9 +141,9 @@ pub fn load_png_from_memory(image: &[u8]) -> Result { let mut image_data = Vec::from_elem((width * height * pixel_width) as uint, 0u8); let image_buf = image_data.as_mut_ptr(); - let mut row_pointers: Vec<*mut u8> = Vec::from_fn(height as uint, |idx| { + let mut row_pointers: Vec<*mut u8> = (0..height).map(|idx| { image_buf.offset((((width * pixel_width) as uint) * idx) as int) - }); + }).collect(); ffi::RUST_png_read_image(png_ptr, row_pointers.as_mut_ptr()); @@ -162,12 +161,11 @@ pub extern fn write_data(png_ptr: *mut ffi::png_struct, data: *mut u8, length: s unsafe { let io_ptr = ffi::RUST_png_get_io_ptr(png_ptr); let writer: &mut &mut io::Writer = mem::transmute(io_ptr); - slice::raw::buf_as_slice(&*data, length as uint, |buf| { - match writer.write(buf) { - Err(e) => panic!("{}", e.desc), - _ => {} - } - }); + let buf = slice::from_raw_buf(&*data, length as uint); + match writer.write(buf) { + Err(e) => panic!("{}", e.desc), + _ => {} + } } } @@ -224,9 +222,9 @@ pub fn store_png(img: &mut Image, path: &Path) -> Result<(),String> { ffi::RUST_png_set_IHDR(png_ptr, info_ptr, img.width, img.height, bit_depth, color_type, ffi::INTERLACE_NONE, ffi::COMPRESSION_TYPE_DEFAULT, ffi::FILTER_NONE); - let mut row_pointers: Vec<*mut u8> = Vec::from_fn(img.height as uint, |idx| { + let mut row_pointers: Vec<*mut u8> = (0..img.height).map(|idx| { image_buf.offset((((img.width * pixel_width) as uint) * idx) as int) - }); + }).collect(); ffi::RUST_png_set_rows(png_ptr, info_ptr, row_pointers.as_mut_ptr()); ffi::RUST_png_write_png(png_ptr, info_ptr, ffi::TRANSFORM_IDENTITY, ptr::null_mut()); @@ -255,7 +253,7 @@ mod test { Err(e) => panic!(e.desc), }; - let mut buf = Vec::from_elem(1024, 0u8); + let mut buf = repeat(0u8).take(1024).collect(); let count = reader.read(buf.slice_mut(0, 1024)).unwrap(); assert!(count >= 8); unsafe { @@ -332,7 +330,7 @@ mod test { let mut img = Image { width: 10, height: 10, - pixels: RGB8(Vec::from_elem(10 * 10 * 3, 100u8)), + pixels: RGB8(repeat(100).take(10 * 10 * 3).collect()), }; let res = store_png(&mut img, &Path::new("test/store.png")); assert!(res.is_ok()); From 2804379427ced963466d19132b816bb06a8a4006 Mon Sep 17 00:00:00 2001 From: Josh Matthews Date: Thu, 15 Jan 2015 22:58:32 -0500 Subject: [PATCH 2/2] Fix remaining build errors and some usize/isize warnings. --- src/lib.rs | 31 +++++++++++++++++-------------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index b5d52d1..463e617 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -16,6 +16,7 @@ use libc::{c_int, size_t}; use std::mem; use std::io; use std::io::File; +use std::iter::repeat; use std::ptr; use std::slice; @@ -40,7 +41,7 @@ pub struct Image { // to the data vector. struct ImageData<'a> { data: &'a [u8], - offset: uint, + offset: usize, } pub fn is_png(image: &[u8]) -> bool { @@ -53,7 +54,7 @@ pub extern fn read_data(png_ptr: *mut ffi::png_struct, data: *mut u8, length: si unsafe { let io_ptr = ffi::RUST_png_get_io_ptr(png_ptr); let image_data: &mut ImageData = mem::transmute(io_ptr); - let len = length as uint; + let len = length as usize; let buf = slice::from_raw_mut_buf(&data, len); let end_pos = std::cmp::min(image_data.data.len()-image_data.offset, len); let src = image_data.data.slice(image_data.offset, image_data.offset+end_pos); @@ -102,8 +103,8 @@ pub fn load_png_from_memory(image: &[u8]) -> Result { ffi::RUST_png_set_read_fn(png_ptr, mem::transmute(&mut image_data), read_data); ffi::RUST_png_read_info(png_ptr, info_ptr); - let width = ffi::RUST_png_get_image_width(png_ptr, info_ptr); - let height = ffi::RUST_png_get_image_height(png_ptr, info_ptr); + 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); // convert palette and grayscale to rgb @@ -133,16 +134,16 @@ pub fn load_png_from_memory(image: &[u8]) -> Result { let (color_type, pixel_width) = match (updated_color_type as c_int, updated_bit_depth) { (ffi::COLOR_TYPE_RGB, 8) | (ffi::COLOR_TYPE_RGBA, 8) | - (ffi::COLOR_TYPE_PALETTE, 8) => (PixelsByColorType::RGBA8, 4), - (ffi::COLOR_TYPE_GRAY, 8) => (PixelsByColorType::K8, 1), - (ffi::COLOR_TYPE_GA, 8) => (PixelsByColorType::KA8, 2), + (ffi::COLOR_TYPE_PALETTE, 8) => (PixelsByColorType::RGBA8 as fn(Vec) -> PixelsByColorType, 4us), + (ffi::COLOR_TYPE_GRAY, 8) => (PixelsByColorType::K8 as fn(Vec) -> PixelsByColorType, 1us), + (ffi::COLOR_TYPE_GA, 8) => (PixelsByColorType::KA8 as fn(Vec) -> PixelsByColorType, 2us), _ => panic!("color type not supported"), }; - let mut image_data = Vec::from_elem((width * height * pixel_width) as uint, 0u8); + let mut image_data: Vec = repeat(0u8).take(width * height * pixel_width).collect(); let image_buf = image_data.as_mut_ptr(); let mut row_pointers: Vec<*mut u8> = (0..height).map(|idx| { - image_buf.offset((((width * pixel_width) as uint) * idx) as int) + image_buf.offset((width * pixel_width * idx) as isize) }).collect(); ffi::RUST_png_read_image(png_ptr, row_pointers.as_mut_ptr()); @@ -150,8 +151,8 @@ pub fn load_png_from_memory(image: &[u8]) -> Result { ffi::RUST_png_destroy_read_struct(&mut png_ptr, &mut info_ptr, ptr::null_mut()); Ok(Image { - width: width, - height: height, + width: width as u32, + height: height as u32, pixels: color_type(image_data), }) } @@ -161,7 +162,8 @@ pub extern fn write_data(png_ptr: *mut ffi::png_struct, data: *mut u8, length: s unsafe { let io_ptr = ffi::RUST_png_get_io_ptr(png_ptr); let writer: &mut &mut io::Writer = mem::transmute(io_ptr); - let buf = slice::from_raw_buf(&*data, length as uint); + let data = data as *const _; + let buf = slice::from_raw_buf(&data, length as usize); match writer.write(buf) { Err(e) => panic!("{}", e.desc), _ => {} @@ -222,8 +224,8 @@ pub fn store_png(img: &mut Image, path: &Path) -> Result<(),String> { ffi::RUST_png_set_IHDR(png_ptr, info_ptr, img.width, img.height, bit_depth, color_type, ffi::INTERLACE_NONE, ffi::COMPRESSION_TYPE_DEFAULT, ffi::FILTER_NONE); - let mut row_pointers: Vec<*mut u8> = (0..img.height).map(|idx| { - image_buf.offset((((img.width * pixel_width) as uint) * idx) as int) + let mut row_pointers: Vec<*mut u8> = (0..img.height as usize).map(|idx| { + image_buf.offset((((img.width * pixel_width) as usize) * idx) as isize) }).collect(); ffi::RUST_png_set_rows(png_ptr, info_ptr, row_pointers.as_mut_ptr()); @@ -241,6 +243,7 @@ mod test { use self::test::fmt_bench_samples; use std::io; use std::io::File; + use std::iter::repeat; use super::{ffi, load_png, load_png_from_memory, store_png}; use super::{RGB8, RGBA8, K8, KA8, Image};