Put red pixels from data
authorMegaBrutal <code+git@megabrutal.com>
Fri, 24 Jun 2022 20:55:45 +0000 (22:55 +0200)
committerMegaBrutal <code+git@megabrutal.com>
Fri, 24 Jun 2022 20:55:45 +0000 (22:55 +0200)
src/main.rs

index e4d6ac74d36e42e87ebc0658b40dcbe0b55ba141..bab5da743def470f5421a99bed554db0611a8b26 100644 (file)
@@ -41,9 +41,18 @@ async fn greet(name: web::Path<String>) -> impl Responder {
 #[get("/gen/{data}")]
 async fn img_gen(data: web::Path<String>) -> Result<impl Responder> {
     let mut cursor = Cursor::new(Vec::new());
-    let img: RgbImage = ImageBuffer::new(512, 512);
-    to_imageresult(write_buffer_with_format(&mut cursor, &img, 512, 512, ColorType::Rgb8, Png))?;
-    Ok(cursor.into_inner().customize().insert_header(("content-type", "image/png")))
+    let mut img: RgbImage = ImageBuffer::new(128, 128);
+    let mut pixels = img.pixels_mut();
+
+    for c in data.chars() {
+        let mut p = pixels.next().unwrap();
+        p.0 = [c as u8, 0, 0];
+    }
+
+    to_imageresult(write_buffer_with_format(&mut cursor, &img, 128, 128, ColorType::Rgb8, Png))?;
+    Ok(HttpResponse::build(StatusCode::OK)
+       .content_type("image/png")
+       .body(cursor.into_inner()))
 }
 
 #[actix_web::main] // or #[tokio::main]