Ok(canvas_option.insert(f(canvas, percent_decode_str(data).into_iter().borrow_mut())?))
}
-fn make_png(dim_x: u32, dim_y: u32, scale: u32, data: &mut dyn Iterator<Item = u8>) -> Result<Cursor<Vec<u8>>, ImageError> {
+fn make_png<F>(dim_x: u32, dim_y: u32, scale: u32, data: &mut dyn Iterator<Item = u8>, encode: F) -> Result<Cursor<Vec<u8>>, ImageError>
+ where F: Fn(RgbImage, &mut dyn Iterator<Item = u8>) -> Result<RgbImage, ImageError>
+{
// Image must not be larger than 1 megapixel
if dim_x * dim_y * scale > 1048576 {
return Err(ImageError::dimension_error())
}
- let img: RgbImage = rgb_encode(ImageBuffer::new(dim_x, dim_y), data)?;
+ let img: RgbImage = encode(ImageBuffer::new(dim_x, dim_y), data)?;
let cursor = image_to_cursor(img, scale)?;
Ok(cursor)
}
#[get("/gen/0/{data}")]
async fn img_gen0(req: HttpRequest) -> Result<impl Responder> {
let data = extract_data!(req, 3);
- let cursor = make_png(32, 32, 16, percent_decode_str(data).into_iter().borrow_mut())?;
+ let cursor = make_png(32, 32, 16, percent_decode_str(data).into_iter().borrow_mut(), rgb_encode)?;
Ok(response_image!(cursor))
}
async fn img_gen1(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
let data = extract_data!(req, 6);
- let cursor = make_png(dim_x, dim_y, scale, percent_decode_str(data).into_iter().borrow_mut())?;
+ let cursor = make_png(dim_x, dim_y, scale, percent_decode_str(data).into_iter().borrow_mut(), rgb_encode)?;
Ok(response_image!(cursor))
}
async fn img_gen2(req: HttpRequest, path: web::Path<(u32, u32, u32)>) -> Result<impl Responder> {
let (dim_x, dim_y, scale) = path.into_inner();
let data = extract_header!(req, "user-agent");
- let cursor = make_png(dim_x, dim_y, scale, &mut data.bytes())?;
+ let cursor = make_png(dim_x, dim_y, scale, &mut data.bytes(), rgb_encode)?;
Ok(response_image!(cursor))
}
let wave_color = ToRgbIter::new(&mut percent_decode_str(wave_color)).next().unwrap_or(Rgb([255, 255, 255]));
debug!("Decoded color: {:?}", wave_color);
let signal = &mut percent_decode_str(extract_data!(req, 12));
- let resimg = sine_encode(ImageBuffer::new(dim_x, dim_y), (wave_offset_x, wave_offset_y), amplitude, freq_base, run, wave_color, signal)?;
- let cursor = image_to_cursor(resimg, scale)?;
+ let cursor = make_png(dim_x, dim_y, scale, signal,
+ |img, signal| sine_encode(img, (wave_offset_x, wave_offset_y), amplitude, freq_base, run, wave_color, signal))?;
Ok(response_image!(cursor))
}