feat: make Flutgrid implement image
This commit is contained in:
parent
3b835b4609
commit
386a5fd5ec
3 changed files with 891 additions and 2 deletions
869
Cargo.lock
generated
869
Cargo.lock
generated
File diff suppressed because it is too large
Load diff
|
|
@ -5,5 +5,6 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
axum = { version = "0.7.5" }
|
||||
image = "0.25.1"
|
||||
tokio = { version = "1.38", features = ["full"] }
|
||||
tokio-test = "*"
|
||||
|
|
|
|||
23
src/main.rs
23
src/main.rs
|
|
@ -3,12 +3,13 @@
|
|||
|
||||
use std::{
|
||||
cell::SyncUnsafeCell,
|
||||
io::{self, Error, ErrorKind},
|
||||
io::{self, Cursor, Error, ErrorKind},
|
||||
iter::once,
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use axum::{http::StatusCode, routing::get, Router};
|
||||
use image::{GenericImage, GenericImageView, Rgba};
|
||||
use tokio::{
|
||||
io::{AsyncReadExt, AsyncWriteExt, BufReader, BufWriter},
|
||||
net::TcpListener,
|
||||
|
|
@ -52,6 +53,21 @@ impl<T> FlutGrid<T> {
|
|||
}
|
||||
}
|
||||
|
||||
impl GenericImageView for FlutGrid<u32> {
|
||||
type Pixel = Rgba<u8>;
|
||||
|
||||
fn dimensions(&self) -> (u32, u32) {
|
||||
return (self.size_x as u32, self.size_y as u32);
|
||||
}
|
||||
|
||||
fn get_pixel(&self, x: u32, y: u32) -> Self::Pixel {
|
||||
match self.get(x as u16, y as u16) {
|
||||
None => todo!("out of bounds for images not supported"),
|
||||
Some(color) => return Rgba(color.to_be_bytes()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Grid<u16, T> for FlutGrid<T> {
|
||||
fn get(&self, x: u16, y: u16) -> Option<&T> {
|
||||
match self.index(x, y) {
|
||||
|
|
@ -266,6 +282,11 @@ async fn main() -> io::Result<()> {
|
|||
let web_listener = TcpListener::bind("0.0.0.0:7792").await?;
|
||||
println!("bound web listener");
|
||||
|
||||
let img_asuc = asuc.clone();
|
||||
tokio::spawn(async move {
|
||||
let img_grids = unsafe { img_asuc.get().as_ref().unwrap() };
|
||||
});
|
||||
|
||||
let app = Router::new().route("/", get(root));
|
||||
tokio::spawn(async move { axum::serve(web_listener, app).await });
|
||||
println!("web server started");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue