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, RgbImage, write_buffer_with_format};
+use image::{ImageBuffer, ColorType, Rgb, RgbImage, write_buffer_with_format};
use image::ImageOutputFormat::Png;
#[derive(Debug)]
}
}
+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()) }
+ }
+}
+
+
+
+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();
+ if r.is_none() && g.is_none() && b.is_none() {
+ None
+ }
+ else {
+ Some(Rgb([r.unwrap_or('\0') as u8, g.unwrap_or('\0') as u8, b.unwrap_or('\0') as u8]))
+ }
+ }
+}
+
fn to_imageresult<T> (result: Result<T, image::ImageError>) -> Result<T, ImageError> {
match result {
Ok(v) => Ok(v),
let mut cursor = Cursor::new(Vec::new());
let mut img: RgbImage = ImageBuffer::new(128, 128);
let mut pixels = img.pixels_mut();
+ //let mut rgb = Box::new(StringToRgb::new(&data));
+ let mut rgb = StringToRgb::new(&data);
- for c in data.chars() {
- let mut p = pixels.next().unwrap();
- p.0 = [c as u8, 0, 0];
+ for sp in rgb {
+ let mut dp = pixels.next().unwrap();
+ dp.0 = sp.0;
}
to_imageresult(write_buffer_with_format(&mut cursor, &img, 128, 128, ColorType::Rgb8, Png))?;