From 12759d4a9127b4d61fbd9b72edc49c1b47baa62e Mon Sep 17 00:00:00 2001 From: MegaBrutal Date: Sun, 25 Dec 2022 00:30:00 +0100 Subject: [PATCH] Implement /pgen/5 --- src/main.rs | 56 ++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 40 insertions(+), 16 deletions(-) diff --git a/src/main.rs b/src/main.rs index 25a4e4a..2d58e6e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -268,6 +268,24 @@ fn sine_encode(mut img: RgbImage, wave_offset: (u32, u32), amplitude: f32, freq_ Ok(img) } +fn offset_encode(mut img: RgbImage, offset: (u32, u32), run: u32, color: Rgb, data: &mut dyn Iterator) -> Result { + let (offset_x, offset_y) = offset; + let mut x = offset_x; + let mut data = data.peekable(); + while (x < img.width()) && (data.peek().is_some()) { + let v = data.next().unwrap_or(0); + let y = offset_y + v as u32; + let max_x = if (x + run) < img.width() { x + run } else { img.width() }; + while x < max_x { + if y < img.height() { + img.put_pixel(x, offset_y + v as u32, color); + } + x += 1; + } + } + Ok(img) +} + fn apply_to_canvas<'a, F>(f: F, canvas_option: &'a mut Option, dimensions: Option<(u32, u32)>, data: &str) -> Result<&'a mut Canvas, CanvasError> where F: Fn(Canvas, &mut dyn Iterator) -> Result { @@ -378,22 +396,7 @@ async fn img_gen5(req: HttpRequest, path: web::Path<(u32, u32, u32, u32, u32, u3 let color = extract_color!(req, 9)?; let data = &mut percent_decode_str(extract_data!(req, 10)); let cursor = make_png(dim_x, dim_y, scale, data, - |mut img, data| { - let mut x = offset_x; - let mut data = data.peekable(); - while (x < img.width()) && (data.peek().is_some()) { - let v = data.next().unwrap_or(0); - let y = offset_y + v as u32; - let max_x = if (x + run) < img.width() { x + run } else { img.width() }; - while x < max_x { - if y < img.height() { - img.put_pixel(x, offset_y + v as u32, color); - } - x += 1; - } - } - Ok(img) - })?; + |img, data| offset_encode(img, (offset_x, offset_y), run, color, data))?; Ok(response_image!(cursor)) } @@ -475,6 +478,26 @@ async fn img_pgen4(req: HttpRequest, path: web::Path<(u32, u32, u32, u32, u32, f Ok(response_image!(cursor)) } +#[get("/pgen/5/{dim_x}/{dim_y}/{scale}/{offset_x}/{offset_y}/{run}/{color}/{data}")] +async fn img_pgen5(req: HttpRequest, path: web::Path<(u32, u32, u32, u32, u32, u32)>, + canvas1: web::Data>>) -> Result +{ + let (dim_x, dim_y, scale, offset_x, offset_y, run) = path.into_inner(); + let color = extract_color!(req, 9)?; + let data = extract_data!(req, 10); + let img: RgbImage = ({ + let canvas_option = &mut *canvas1.lock().unwrap(); + Ok(apply_to_canvas( + |mut canvas, data| { + canvas.img = offset_encode(canvas.img, (offset_x, offset_y), run, color, data)?; + Ok(canvas) + }, + &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone()) + } as Result)?; + let cursor = image_to_cursor(img, scale)?; + Ok(response_image!(cursor)) +} + #[get("/pdrop/{canvas_id}")] async fn pdrop(canvas_id: web::Path, canvas0: web::Data>>, @@ -520,6 +543,7 @@ async fn main() -> std::io::Result<()> { .service(img_pgen2) .service(img_pgen3) .service(img_pgen4) + .service(img_pgen5) .service(pdrop) .app_data(web::Data::new(canvas0.clone())) .app_data(web::Data::new(canvas1.clone())) -- 2.34.1