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>
{
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))
}
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>>>,
.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()))