Ok(cursor)
}
+fn image_to_cursor(img: RgbImage, scale: u32) -> Result<Cursor<Vec<u8>>, ImageError> {
+ let (dim_x, dim_y) = img.dimensions();
+ let (dim_x, dim_y) = (dim_x * scale, dim_y * scale);
+
+ // Image must not be larger than 1 megapixel
+ if dim_x * dim_y > 1048576 {
+ return Err(ImageError::dimension_error())
+ }
+
+ let mut cursor = Cursor::new(Vec::new());
+ let img = resize(&img, dim_x, dim_y, FilterType::Nearest);
+ to_imageresult(write_buffer_with_format(&mut cursor, &img, dim_x, dim_y, ColorType::Rgb8, Png))?;
+ Ok(cursor)
+}
+
+
#[get("/gen/0/{data}")]
async fn img_gen0(req: HttpRequest) -> Result<impl Responder> {
let data = req.uri().path().split("/").skip(3).next().unwrap();
#[get("/pgen/0/{data}")]
async fn img_pgen0(req: HttpRequest, canvas0: web::Data<Arc<Mutex<Canvas0>>>) -> Result<impl Responder> {
let data = req.uri().path().split("/").skip(3).next().unwrap();
- let mut cursor = Cursor::new(Vec::new());
let scale = 16;
let img: RgbImage = ({
let canvas_option = &mut *canvas0.lock().unwrap();
Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, None, data)?.img.clone())
} as Result<RgbImage, CanvasError>)?;
- let (tdim_x, tdim_y) = img.dimensions();
- let (tdim_x, tdim_y) = (tdim_x * scale, tdim_y * scale);
- let img = resize(&img, tdim_x, tdim_y, FilterType::Nearest);
- to_imageresult(write_buffer_with_format(&mut cursor, &img, tdim_x, tdim_y, ColorType::Rgb8, Png))?;
+ let cursor = image_to_cursor(img, scale)?;
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/png")
.body(cursor.into_inner()))
let canvas_option = &mut *canvas1.lock().unwrap();
Ok(apply_to_canvas( |c, d| to_canvasresult(rgb_encode_to_canvas(c, d)), &mut canvas_option.0, Some((dim_x, dim_y)), data)?.img.clone())
} as Result<RgbImage, CanvasError>)?;
- let (tdim_x, tdim_y) = img.dimensions();
- let (tdim_x, tdim_y) = (tdim_x * scale, tdim_y * scale);
- let img = resize(&img, tdim_x, tdim_y, FilterType::Nearest);
- let mut cursor = Cursor::new(Vec::new());
- to_imageresult(write_buffer_with_format(&mut cursor, &img, tdim_x, tdim_y, ColorType::Rgb8, Png))?;
+ let cursor = image_to_cursor(img, scale)?;
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/png")
.body(cursor.into_inner()))