diff --git a/cowgen/src/lib.rs b/cowgen/src/lib.rs index 53c0967..4812167 100644 --- a/cowgen/src/lib.rs +++ b/cowgen/src/lib.rs @@ -1,40 +1,27 @@ use ril::prelude::*; -pub struct Generate { - elements: Vec, - base: Image, -} - -impl Generate { - pub fn new(base: Vec, elements: Vec) -> Result { - Ok(Self { - base: Image::::from_bytes_inferred(&base)?, - elements, - }) - } - - pub fn produce(self) -> Result, CowError> { - let size = self.base.dimensions(); - let mut base_image = self.base.with_overlay_mode(OverlayMode::Merge); - for element in self.elements { - let mut pos = element.position; - let mut img: Image = element.into_image()?; - if pos.0 < 0 { - img.crop(-pos.0 as u32, 0, size.0, size.1); - pos.0 = 0; - } - if pos.1 < 0 { - img.crop(0, -pos.1 as u32, size.0, size.1); - pos.1 = 0; - } - - base_image.draw(&Paste::new(&img.convert()).with_position(pos.0 as u32, pos.1 as u32)); +pub fn produce(base: &Vec, elements: Vec) -> Result, CowError> { + let base = Image::::from_bytes_inferred(base)?; + let size = base.dimensions(); + let mut base_image = base.with_overlay_mode(OverlayMode::Merge); + for element in elements { + let mut pos = element.position; + let mut img: Image = element.into_image()?; + if pos.0 < 0 { + img.crop(-pos.0 as u32, 0, size.0, size.1); + pos.0 = 0; + } + if pos.1 < 0 { + img.crop(0, -pos.1 as u32, size.0, size.1); + pos.1 = 0; } - let mut buf = Vec::::new(); - base_image.encode(ImageFormat::Png, &mut buf).unwrap(); - Ok(buf) + base_image.draw(&Paste::new(&img.convert()).with_position(pos.0 as u32, pos.1 as u32)); } + let mut buf = Vec::::new(); + base_image.encode(ImageFormat::Png, &mut buf).unwrap(); + + Ok(buf) } pub struct Element { @@ -54,7 +41,7 @@ impl Element { fn into_image(self) -> Result { match self.media { - Media::Image(image) => Ok(Image::from_bytes_inferred(image)?.resized( + Media::Image(image) => Ok(Image::from_bytes_inferred(*image)?.resized( self.dimensions.0, self.dimensions.1, ResizeAlgorithm::Bicubic, @@ -83,7 +70,7 @@ impl Element { pub enum Media { Text(Text), - Image(Vec), + Image(Box>), } pub struct Text { diff --git a/cowgen/tests/overlay_text_and_image.rs b/cowgen/tests/overlay_text_and_image.rs index 8fc5c95..c2f9f01 100644 --- a/cowgen/tests/overlay_text_and_image.rs +++ b/cowgen/tests/overlay_text_and_image.rs @@ -7,28 +7,21 @@ use std::{ #[test] fn test_overlay_text_and_image() { println!("{}", std::env::current_dir().unwrap().to_str().unwrap()); - let out = Generate::new( - include_bytes!("assets/cow.png").to_vec(), + let base = Box::new(include_bytes!("assets/cow.png").to_vec()); + let font = Box::new(include_bytes!("assets/OpenSans-ExtraBold.ttf").to_vec()); + let superimpose = Box::new(include_bytes!("assets/mariah.jpg").to_vec()); + + let out = produce( + &base, vec![ Element::new( - Media::Text(Text::new( - "Hello", - "tests/assets/OpenSans-ExtraBold.ttf", - 20.0, - (0, 0, 0, 128), - )), + Media::Text(Text::new("Hello".to_string(), font, 20.0, (0, 0, 0, 128))), (0, 0), (64, 64), ), - Element::new( - Media::Image(include_bytes!("assets/mariah.jpg").to_vec()), - (400, 400), - (800, 200), - ), + Element::new(Media::Image(superimpose), (400, 400), (800, 200)), ], ) - .unwrap() - .produce() .unwrap(); let mut f = File::create("tests/out/one.png").unwrap(); f.write_all(&out).unwrap();