use actix_web::{get, web, App, HttpServer, HttpRequest, HttpResponse, Responder, ResponseError, Result};
use actix_web::body::BoxBody;
use actix_web::http::StatusCode;
-use image::{ImageBuffer, ColorType, Rgb, RgbImage, write_buffer_with_format};
+use image::{ImageBuffer, ColorType, Pixel, Rgb, RgbImage, write_buffer_with_format};
use image::ImageOutputFormat::Png;
use image::imageops::{FilterType, resize};
format!("Hello {name}!")
}
-#[get("/gen/0/{data}")]
-async fn img_gen(req: HttpRequest) -> Result<impl Responder> {
- let mut cursor = Cursor::new(Vec::new());
- let mut img: RgbImage = ImageBuffer::new(32, 32);
+fn make_png(dim_x: u32, dim_y: u32, scale: u32, data: &str) -> Result<Cursor<Vec<u8>>, ImageError> {
+ let mut img: RgbImage = ImageBuffer::new(dim_x, dim_y);
let mut pixels = img.pixels_mut();
- let data = req.uri().path().split("/").skip(3).next().unwrap();
- println!("{:?}", data);
for sp in ToRgbIter::new(percent_decode_str(&data).into_iter().borrow_mut()) {
let mut dp = pixels.next().unwrap();
println!("{:?}", sp);
dp.0 = sp.0;
}
- let img = resize(&img, 512, 512, FilterType::Nearest);
- println!("Here!");
- to_imageresult(write_buffer_with_format(&mut cursor, &img, 512, 512, ColorType::Rgb8, Png))?;
+ let tdim_x = dim_x * scale;
+ let tdim_y = dim_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))?;
+ 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();
+ let cursor = make_png(32, 32, 16, &data)?;
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/png")
.body(cursor.into_inner()))
}
+#[get("/gen/1/{dim_x}/{dim_y}/{scale}/{data}")]
+async fn img_gen1(req: HttpRequest, dim_x: web::Path<u16>, dim_y: web::Path<u16>, scale: web::Path<u8>) -> Result<impl Responder> {
+ Ok(HttpResponse::build(StatusCode::OK))
+}
+
#[actix_web::main] // or #[tokio::main]
async fn main() -> std::io::Result<()> {
HttpServer::new(|| {
- App::new().service(greet).service(img_gen)
+ App::new()
+ .service(greet)
+ .service(img_gen0)
+ .service(img_gen1)
})
.bind(("127.0.0.1", 8080))?
.run()