Implement /pgen/5
authorMegaBrutal <code+git@megabrutal.com>
Sat, 24 Dec 2022 23:30:00 +0000 (00:30 +0100)
committerMegaBrutal <code+git@megabrutal.com>
Sat, 24 Dec 2022 23:30:00 +0000 (00:30 +0100)
src/main.rs

index 25a4e4a5112b89e8736d4c85762365c57bdb9266..2d58e6ebfef4b350fb4b850275b9e892bda2661c 100644 (file)
@@ -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<u8>, data: &mut dyn Iterator<Item = u8>) -> Result<RgbImage, ImageError> {
+    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<Canvas>, dimensions: Option<(u32, u32)>, data: &str) -> Result<&'a mut Canvas, CanvasError>
     where F: Fn(Canvas, &mut dyn Iterator<Item = u8>) -> Result<Canvas, CanvasError>
 {
@@ -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<Arc<Mutex<Canvas1>>>) -> Result<impl Responder>
+{
+    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<RgbImage, CanvasError>)?;
+    let cursor = image_to_cursor(img, scale)?;
+    Ok(response_image!(cursor))
+}
+
 #[get("/pdrop/{canvas_id}")]
 async fn pdrop(canvas_id: web::Path<u8>,
     canvas0: web::Data<Arc<Mutex<Canvas0>>>,
@@ -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()))