use std::io::Cursor;
use std::fmt::Display;
use std::fmt;
-use serde::Deserialize;
+use std::borrow::BorrowMut;
+use num_traits::Zero;
+use num_traits::cast::AsPrimitive;
use percent_encoding::percent_decode_str;
-use actix_web::{get, web, App, HttpServer, HttpResponse, Responder, ResponseError, Result};
+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::ImageOutputFormat::Png;
use image::imageops::{FilterType, resize};
-#[derive(Deserialize)]
-struct Data {
- data: String
-}
#[derive(Debug)]
struct ImageError(image::ImageError);
}
}
-struct StringToRgb<'a> {
- iter: Box<dyn Iterator<Item = char> + 'a>
+struct ToRgbIter<'a, T> where T: AsPrimitive<u8> {
+ iter: Box<&'a mut dyn Iterator<Item = T>>
}
-impl<'a> StringToRgb<'a> {
- fn new(text: &'a str) -> StringToRgb {
- StringToRgb { iter: Box::new(text.chars()) }
+impl<'a, T: AsPrimitive<u8>> ToRgbIter<'a, T> {
+ fn new(data: &'a mut (dyn Iterator<Item = T>)) -> ToRgbIter<T> {
+ ToRgbIter { iter: Box::new(data) }
}
}
-impl Iterator for StringToRgb<'_> {
+impl<T: AsPrimitive<u8> + Zero> Iterator for ToRgbIter<'_, T> {
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
+ if let Some(r) = self.iter.next() {
+ let g = self.iter.next().unwrap_or(T::zero());
+ let b = self.iter.next().unwrap_or(T::zero());
+ println!("{} {} {}", r.as_(), g.as_(), b.as_());
+ //Some(Rgb([r.unwrap_or(0) as u8, g.unwrap_or(0) as u8, b.unwrap_or(0) as u8]))
+ Some(Rgb([r.as_(), g.as_(), b.as_()]))
}
else {
- Some(Rgb([r.unwrap_or('\0') as u8, g.unwrap_or('\0') as u8, b.unwrap_or('\0') as u8]))
+ None
}
}
}
}
#[get("/hello/{name}")]
-async fn greet(name: web::Path<String>) -> impl Responder {
+async fn greet(name: web::Path<String>, req: HttpRequest) -> impl Responder {
+ println!("{:?}", req);
format!("Hello {name}!")
}
#[get("/gen/{data}")]
-async fn img_gen(data: web::Path<Data>) -> Result<impl Responder> {
+async fn img_gen(req: HttpRequest) -> Result<impl Responder> {
let mut cursor = Cursor::new(Vec::new());
let mut img: RgbImage = ImageBuffer::new(32, 32);
let mut pixels = img.pixels_mut();
+ let data = req.uri().path().split("/").skip(2).next().unwrap();
+
+ //println!("{:#?}\n{:#?}", req.uri().path().split("/").skip(2).next(), req.match_info().get("data"));
- for sp in StringToRgb::new(&percent_decode_str(&data.data).decode_utf8_lossy()) {
+ println!("{:?}", data);
+ //for sp in ToRgbIter::new(percent_decode(&RawOsString::from_string(data.into_inner()).as_raw_bytes()).into_iter().borrow_mut()) {
+ 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))?;
Ok(HttpResponse::build(StatusCode::OK)
.content_type("image/png")