}
}
+impl From<image::ImageError> for ImageError {
+ fn from(e: image::ImageError) -> Self {
+ Self(e)
+ }
+}
+
#[derive(Debug)]
enum CanvasError {
NotExists,
}
}
-fn to_imageresult<T> (result: Result<T, image::ImageError>) -> Result<T, ImageError> {
- match result {
- Ok(v) => Ok(v),
- Err(e) => Err(ImageError(e))
- }
-}
-
-fn to_canvasresult<T: std::fmt::Debug> (result: Result<T, ImageError>) -> Result<T, CanvasError> {
- match result {
- Ok(v) => Ok(v),
- Err(e) => Err(CanvasError::ImageError(e))
- }
-}
-
#[derive(Debug)]
struct Canvas {
pen: usize,
Canvas { pen: 0, img: ImageBuffer::new(width, height) }
}
- fn from_image(img: RgbImage) -> Canvas {
+ /*fn from_image(img: RgbImage) -> Canvas {
Canvas { pen: 0, img }
}
- /*fn replace(&self, img: RgbImage, pen: usize) -> Canvas {
+ fn replace(&self, img: RgbImage, pen: usize) -> Canvas {
self.img = img;
self.pen = pen;
self
format!("Hello {name}!\n{:?}", req.headers().get("user-agent"))
}
-fn rgb_encode_to_canvas(mut canvas: Canvas, data: &mut dyn Iterator<Item = u8>) -> Result<Canvas, ImageError> {
- let mut pixels = canvas.img.pixels_mut().skip(canvas.pen);
+fn rgb_encode_with_pen(img: &mut RgbImage, pen: usize, data: &mut dyn Iterator<Item = u8>) -> Result<usize, ImageError> {
+ let mut pixels = img.pixels_mut().skip(pen);
let mut counter = 0;
for sp in ToRgbIter::new(data) {
let mut dp = match pixels.next() {
Some(pixel) => pixel,
None => {
- pixels = canvas.img.pixels_mut().skip(0);
+ pixels = img.pixels_mut().skip(0);
pixels.next().ok_or_else(ImageError::dimension_error)?
}
};
counter += 1;
}
+ Ok(counter)
+}
+
+fn rgb_encode_to_canvas(mut canvas: Canvas, data: &mut dyn Iterator<Item = u8>) -> Result<Canvas, CanvasError> {
+ let counter = rgb_encode_with_pen(&mut canvas.img, canvas.pen, data)?;
Ok(canvas.advance_pen(counter))
}
-fn rgb_encode(img: RgbImage, data: &mut dyn Iterator<Item = u8>) -> Result<RgbImage, ImageError> {
- Ok(rgb_encode_to_canvas(Canvas::from_image(img), data)?.img)
+fn rgb_encode(mut img: RgbImage, data: &mut dyn Iterator<Item = u8>) -> Result<RgbImage, ImageError> {
+ rgb_encode_with_pen(&mut img, 0, data)?;
+ 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>
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))?;
+ write_buffer_with_format(&mut cursor, &img, dim_x, dim_y, ColorType::Rgb8, Png)?;
Ok(cursor)
}
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())
+ Ok(apply_to_canvas( |c, d| rgb_encode_to_canvas(c, d), &mut canvas_option.0, None, data)?.img.clone())
} as Result<RgbImage, CanvasError>)?;
let cursor = image_to_cursor(img, scale)?;
Ok(response_image!(cursor))
let data = req.uri().path().split('/').nth(6).unwrap();
let img: RgbImage = ({
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())
+ Ok(apply_to_canvas( |c, d| rgb_encode_to_canvas(c, d), &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))
};
let img: RgbImage = ({
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())
+ Ok(apply_to_canvas( |c, d| rgb_encode_to_canvas(c, d), &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))