use std::io::Cursor;
use std::fmt::Display;
use std::fmt;
+use serde::Deserialize;
+use percent_encoding::percent_decode_str;
use actix_web::{get, web, App, HttpServer, 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::ImageOutputFormat::Png;
+use image::imageops::{FilterType, resize};
+
+#[derive(Deserialize)]
+struct Data {
+ data: String
+}
#[derive(Debug)]
struct ImageError(image::ImageError);
}
struct StringToRgb<'a> {
- text: &'a str,
iter: Box<dyn Iterator<Item = char> + 'a>
}
impl<'a> StringToRgb<'a> {
fn new(text: &'a str) -> StringToRgb {
- StringToRgb { text, iter: Box::new(text.chars()) }
+ StringToRgb { iter: Box::new(text.chars()) }
}
}
-
-
impl Iterator for StringToRgb<'_> {
type Item = Rgb<u8>;
fn next(&mut self) -> Option<Rgb<u8>> {
let r = self.iter.next();
let g = self.iter.next();
let b = self.iter.next();
+ println!("{:?} {:?} {:?}", r, g, b);
if r.is_none() && g.is_none() && b.is_none() {
None
}
}
#[get("/gen/{data}")]
-async fn img_gen(data: web::Path<String>) -> Result<impl Responder> {
+async fn img_gen(data: web::Path<Data>) -> Result<impl Responder> {
let mut cursor = Cursor::new(Vec::new());
- let mut img: RgbImage = ImageBuffer::new(128, 128);
+ let mut img: RgbImage = ImageBuffer::new(32, 32);
let mut pixels = img.pixels_mut();
- //let mut rgb = Box::new(StringToRgb::new(&data));
- let mut rgb = StringToRgb::new(&data);
- for sp in rgb {
+ for sp in StringToRgb::new(&percent_decode_str(&data.data).decode_utf8_lossy()) {
let mut dp = pixels.next().unwrap();
+ println!("{:?}", sp);
dp.0 = sp.0;
}
- to_imageresult(write_buffer_with_format(&mut cursor, &img, 128, 128, ColorType::Rgb8, Png))?;
+ let img = resize(&img, 512, 512, FilterType::Nearest);
+ to_imageresult(write_buffer_with_format(&mut cursor, &img, 512, 512, ColorType::Rgb8, Png))?;
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/png")
.body(cursor.into_inner()))