1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
extern crate image; extern crate itertools; extern crate rand; extern crate walkdir; pub(crate) trait CifarImageTrait { fn new(bytes: &[u8]) -> Result<super::image_pub::CifarImage, ::std::io::Error>; } impl CifarImageTrait for super::image_pub::CifarImage { fn new(bytes: &[u8]) -> Result<Self, ::std::io::Error> { use std::io::Read; use std::mem; use self::image::GenericImage; use self::itertools::multizip; use self::itertools::Itertools; let bytes: &mut &[u8] = &mut bytes.as_ref(); let label: u8 = unsafe { let label: &mut [u8; 1] = &mut mem::uninitialized(); bytes.read_exact(label)?; *label.get_unchecked(0) }; let img = unsafe { let mut img = image::DynamicImage::new_rgb8(32, 32); let red: &mut [u8; 1024] = &mut mem::uninitialized(); let green: &mut [u8; 1024] = &mut mem::uninitialized(); let blue: &mut [u8; 1024] = &mut mem::uninitialized(); bytes.read_exact(red)?; bytes.read_exact(green)?; bytes.read_exact(blue)?; multizip(( (0..32).cartesian_product(0..32), red.iter(), green.iter(), blue.iter(), )).for_each(|((y, x), r, g, b)| { let mut pixel: image::Rgba<u8> = mem::uninitialized(); pixel.data = [*r, *g, *b, 255]; img.unsafe_put_pixel(x, y, pixel); }); img }; Ok(super::image_pub::CifarImage { label: label, image: img, }) } }