53849d9188a138ac9c58cc8c9d22bec8a25c39d2
4 use std
::borrow
::BorrowMut
;
6 use num_traits
::cast
::AsPrimitive
;
7 use percent_encoding
::percent_decode_str
;
8 use actix_web
::{get
, web
, App
, HttpServer
, HttpRequest
, HttpResponse
, Responder
, ResponseError
, Result
};
9 use actix_web
::body
::BoxBody
;
10 use actix_web
::http
::StatusCode
;
11 use image
::{ImageBuffer
, ColorType
, Rgb
, RgbImage
, write_buffer_with_format
};
12 use image
::ImageOutputFormat
::Png
;
13 use image
::imageops
::{FilterType
, resize
};
17 struct ImageError(image
::ImageError
);
19 impl Display
for ImageError
{
20 fn fmt(&self, f
: &mut fmt
::Formatter
<'_
>) -> fmt
::Result
{
21 write
!(f
, "{}", &self.0)
25 impl ResponseError
for ImageError
{
26 fn status_code(&self) -> StatusCode
{
27 StatusCode
::INTERNAL_SERVER_ERROR
30 fn error_response(&self) -> HttpResponse
<BoxBody
> {
31 HttpResponse
::InternalServerError().fin
ish
()
35 struct ToRgbIter
<'a
, T
> where T
: AsPrimitive
<u8> {
36 iter
: Box
<&'a
mut dyn Iterator
<Item
= T
>>
39 impl<'a
, T
: AsPrimitive
<u8>> ToRgbIter
<'a
, T
> {
40 fn new(data
: &'a
mut (dyn Iterator
<Item
= T
>)) -> ToRgbIter
<T
> {
41 ToRgbIter
{ iter
: Box
::new(data
) }
45 impl<T
: AsPrimitive
<u8> + Zero
> Iterator
for ToRgbIter
<'_
, T
> {
47 fn next(&mut self) -> Option
<Rgb
<u8>> {
48 if let Some(r
) = self.iter
.next() {
49 let g
= self.iter
.next().unwrap
_or
(T
::zero());
50 let b
= self.iter
.next().unwrap
_or
(T
::zero());
51 println
!("{} {} {}", r
.as_(), g
.as_(), b
.as_());
52 Some(Rgb([r
.as_(), g
.as_(), b
.as_()]))
60 fn to_imageresult
<T
> (result
: Result
<T
, image
::ImageError
>) -> Result
<T
, ImageError
> {
63 Err(e
) => Err(ImageError(e
))
67 #[get("/hello/{name}")]
68 async
fn greet(name
: web
::Path
<String
>, req
: HttpRequest
) -> impl Responder
{
69 println
!("{:?}", req
);
70 format
!("Hello {name}!")
73 #[get("/gen/0/{data}")]
74 async
fn img_gen(req
: HttpRequest
) -> Result
<impl Responder
> {
75 let mut cursor
= Cursor
::new(Vec
::new());
76 let mut img
: RgbImage
= ImageBuffer
::new(32, 32);
77 let mut pixels
= img
.pixels_mut();
78 let data
= req
.ur
i().path().split("/").skip(3).next().unwrap
();
80 println
!("{:?}", data
);
81 for sp
in ToRgbIter
::new(percent_decode_str(&data
).into
_iter
().borrow_mut()) {
82 let mut dp
= pixels
.next().unwrap
();
87 let img
= resize(&img
, 512, 512, FilterType
::Nearest
);
89 to_imageresult(write_buffer_with_format(&mut cursor
, &img
, 512, 512, ColorType
::Rgb8
, Png
))?
;
90 Ok(HttpResponse
::build(StatusCode
::OK
)
91 .content_type("image/png")
92 .body(cursor
.into
_inner
()))
95 #[actix_web::main] // or #[tokio::main]
96 async
fn main() -> std
::io
::Result
<()> {
98 App
::new().service(greet
).service(img_gen
)
100 .bind(("127.0.0.1", 8080))?