mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-04 05:18:19 +00:00
Implement image loding test
This commit is contained in:
parent
8a0eb0e266
commit
12b33da083
9 changed files with 665 additions and 30 deletions
|
@ -24,3 +24,4 @@ once_cell = {version= "1.10", optional = true}
|
|||
syn = {version = "1.0", default-features = false, features = ["parsing", "printing"]}
|
||||
proc-macro2 = {version = "1.0", default-features = false, features = ["proc-macro"]}
|
||||
quote = {version = "1.0", default-features = false }
|
||||
image = "*"
|
||||
|
|
|
@ -1,22 +1,23 @@
|
|||
pub mod value;
|
||||
#![feature(type_alias_impl_trait)]
|
||||
//pub mod value;
|
||||
pub use graphene_core::{generic, ops /*, structural*/};
|
||||
|
||||
//#[cfg(feature = "memoization")]
|
||||
//pub mod memo;
|
||||
#[cfg(feature = "memoization")]
|
||||
pub mod memo;
|
||||
|
||||
//pub mod raster;
|
||||
pub mod raster;
|
||||
|
||||
pub use graphene_core::*;
|
||||
|
||||
use dyn_any::DynAny;
|
||||
/*use dyn_any::DynAny;
|
||||
pub type DynNode<'n, T> = &'n (dyn Node<'n, Output = T> + 'n);
|
||||
pub type DynAnyNode<'n> = &'n (dyn Node<'n, Output = &'n dyn DynAny<'n>> + 'n);
|
||||
|
||||
pub trait DynamicInput<'n> {
|
||||
fn set_kwarg_by_name(&mut self, name: &str, value: DynAnyNode<'n>);
|
||||
fn set_arg_by_index(&mut self, index: usize, value: DynAnyNode<'n>);
|
||||
}
|
||||
|
||||
*/
|
||||
use quote::quote;
|
||||
use syn::{Expr, ExprPath, Type};
|
||||
|
||||
|
|
|
@ -8,6 +8,7 @@
|
|||
a * b
|
||||
}*/
|
||||
|
||||
/*
|
||||
mod mul {
|
||||
use dyn_any::downcast_ref;
|
||||
use graphene_std::{DynAnyNode, DynNode, DynamicInput, Node};
|
||||
|
@ -69,7 +70,7 @@ mod mul {
|
|||
}
|
||||
}
|
||||
// type SNode<'n> = dyn Node<'n, Output = &'n dyn DynAny<'n>>;
|
||||
|
||||
*/
|
||||
// struct NodeStore<'n>(borrow_stack::FixedSizeStack<'n, Box<SNode<'n>>>);
|
||||
|
||||
// impl<'n> NodeStore<'n> {
|
||||
|
|
|
@ -3,26 +3,26 @@ use once_cell::sync::OnceCell;
|
|||
use std::marker::PhantomData;
|
||||
|
||||
/// Caches the output of a given Node and acts as a proxy
|
||||
pub struct CacheNode<'n, CachedNode: Node<'n>> {
|
||||
pub struct CacheNode<'n, CachedNode: Node<'n, I>, I> {
|
||||
node: CachedNode,
|
||||
cache: OnceCell<CachedNode::Output>,
|
||||
_phantom: PhantomData<&'n ()>,
|
||||
}
|
||||
impl<'n, CashedNode: Node<'n>> Node<'n> for CacheNode<'n, CashedNode>
|
||||
impl<'n, CashedNode: Node<'n, I>, I> Node<'n, I> for CacheNode<'n, CashedNode, I>
|
||||
where
|
||||
CashedNode::Output: 'n,
|
||||
{
|
||||
type Output = &'n CashedNode::Output;
|
||||
fn eval(&'n self) -> Self::Output {
|
||||
self.cache.get_or_init(|| self.node.eval())
|
||||
fn eval(&'n self, input: I) -> Self::Output {
|
||||
self.cache.get_or_init(|| self.node.eval(input))
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, CachedNode: Node<'n>> CacheNode<'n, CachedNode> {
|
||||
impl<'n, CachedNode: Node<'n, I>, I> CacheNode<'n, CachedNode, I> {
|
||||
pub fn clear(&'n mut self) {
|
||||
self.cache = OnceCell::new();
|
||||
}
|
||||
pub fn new(node: CachedNode) -> CacheNode<'n, CachedNode> {
|
||||
pub fn new(node: CachedNode) -> CacheNode<'n, CachedNode, I> {
|
||||
CacheNode {
|
||||
node,
|
||||
cache: OnceCell::new(),
|
||||
|
@ -30,7 +30,7 @@ impl<'n, CachedNode: Node<'n>> CacheNode<'n, CachedNode> {
|
|||
}
|
||||
}
|
||||
}
|
||||
impl<'n, CachedNode: Node<'n>> Cache for CacheNode<'n, CachedNode> {
|
||||
impl<'n, CachedNode: Node<'n, I>, I> Cache for CacheNode<'n, CachedNode, I> {
|
||||
fn clear(&mut self) {
|
||||
self.cache = OnceCell::new();
|
||||
}
|
||||
|
|
|
@ -1,18 +1,197 @@
|
|||
use core::marker::PhantomData;
|
||||
use graphene_core::{value::RefNode, value::ValueNode, Node};
|
||||
use graphene_core::ops::FlatMapResultNode;
|
||||
use graphene_core::raster::color::Color;
|
||||
use graphene_core::structural::{ComposeNode, ConsNode};
|
||||
use graphene_core::{generic::FnNode, ops::MapResultNode, structural::After, value::ValueNode, Node};
|
||||
use image::Pixel;
|
||||
use std::path::Path;
|
||||
|
||||
pub struct MapNode<'n, IN: Node<'n, Output = I>, I: Iterator<Item = &'n S>, MAP: Fn(&dyn RefNode<Output = S>) -> MN, MN: Node<'n, Output = O> + 'n, S, O: 'n>(pub IN, pub MAP, PhantomData<&'n (I, S)>);
|
||||
pub struct MapNode<'n, MN: Node<'n, S>, I: IntoIterator<Item = S>, S>(pub MN, PhantomData<&'n (S, I)>);
|
||||
|
||||
impl<'n, IN: Node<'n, Output = I>, I: Iterator<Item = &'n S>, MAP: Fn(&dyn RefNode<Output = S>) -> MN, MN: Node<'n, Output = O>, S, O: 'static + Clone> Node<'n> for MapNode<'n, IN, I, MAP, MN, S, O> {
|
||||
type Output = Vec<O>;
|
||||
fn eval(&'n self) -> Self::Output {
|
||||
self.0
|
||||
.eval()
|
||||
.map(|x| {
|
||||
let map_node = self.1(x as &dyn RefNode<Output = S>);
|
||||
let result = map_node.eval();
|
||||
result.clone()
|
||||
})
|
||||
.collect()
|
||||
impl<'n, I: IntoIterator<Item = S>, MN: Node<'n, S>, S> Node<'n, I> for MapNode<'n, MN, I, S> {
|
||||
type Output = Vec<MN::Output>;
|
||||
fn eval(&'n self, input: I) -> Self::Output {
|
||||
input.into_iter().map(|x| self.0.eval(x)).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, I: IntoIterator<Item = S>, MN: Node<'n, S>, S> MapNode<'n, MN, I, S> {
|
||||
pub const fn new(mn: MN) -> Self {
|
||||
MapNode(mn, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct MapImageNode<'n, MN: Node<'n, Color, Output = Color>>(pub MN, PhantomData<&'n ()>);
|
||||
|
||||
impl<'n, MN: Node<'n, Color, Output = Color>> Node<'n, Image> for MapImageNode<'n, MN> {
|
||||
type Output = Image;
|
||||
fn eval(&'n self, input: Image) -> Self::Output {
|
||||
Image {
|
||||
width: input.width,
|
||||
height: input.height,
|
||||
data: input.data.iter().map(|x| self.0.eval(*x)).collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'n, MN: Node<'n, Color, Output = Color>> MapImageNode<'n, MN> {
|
||||
pub const fn new(mn: MN) -> Self {
|
||||
MapImageNode(mn, PhantomData)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
IO(std::io::Error),
|
||||
Image(image::ImageError),
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Error::IO(e)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait FileSystem {
|
||||
fn open<P: AsRef<Path>>(&self, path: P) -> Result<Box<dyn std::io::Read>, Error>;
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct StdFs;
|
||||
impl FileSystem for StdFs {
|
||||
fn open<P: AsRef<Path>>(&self, path: P) -> Result<Reader, Error> {
|
||||
Ok(Box::new(std::fs::File::open(path)?))
|
||||
}
|
||||
}
|
||||
type Reader = Box<dyn std::io::Read>;
|
||||
|
||||
pub struct FileNode<P: AsRef<Path>, FS: FileSystem>(PhantomData<(P, FS)>);
|
||||
impl<'n, P: AsRef<Path>, FS: FileSystem> Node<'n, (P, FS)> for FileNode<P, FS> {
|
||||
type Output = Result<Reader, Error>;
|
||||
|
||||
fn eval(&'n self, input: (P, FS)) -> Self::Output {
|
||||
let (path, fs) = input;
|
||||
fs.open(path)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct BufferNode;
|
||||
impl<'n, Reader: std::io::Read> Node<'n, Reader> for BufferNode {
|
||||
type Output = Result<Vec<u8>, Error>;
|
||||
|
||||
fn eval(&'n self, mut reader: Reader) -> Self::Output {
|
||||
let mut buffer = Vec::new();
|
||||
reader.read_to_end(&mut buffer)?;
|
||||
Ok(buffer)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct Image {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub data: Vec<Color>,
|
||||
}
|
||||
|
||||
impl IntoIterator for Image {
|
||||
type Item = Color;
|
||||
type IntoIter = std::vec::IntoIter<Color>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.data.into_iter()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoIterator for &'a Image {
|
||||
type Item = &'a Color;
|
||||
type IntoIter = std::slice::Iter<'a, Color>;
|
||||
fn into_iter(self) -> Self::IntoIter {
|
||||
self.data.iter()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn file_node<'n, P: AsRef<Path> + 'n>() -> impl Node<'n, P, Output = Result<Vec<u8>, Error>> {
|
||||
let fs = ValueNode(StdFs).clone();
|
||||
let fs = ConsNode(fs);
|
||||
let file: ComposeNode<P, _, FileNode<P, _>> = FileNode(PhantomData).after(fs);
|
||||
let buffer = FlatMapResultNode::new(BufferNode).after(file);
|
||||
buffer
|
||||
}
|
||||
type Ret<'n> = impl Node<'n, (), Output = u32>;
|
||||
|
||||
pub fn test_node<'n>() -> Ret<'n> {
|
||||
ValueNode(432).clone()
|
||||
}
|
||||
|
||||
pub fn image_node<'n, P: AsRef<Path> + 'n>() -> impl Node<'n, P, Output = Result<Image, Error>> {
|
||||
let file = file_node();
|
||||
let image_loader = FnNode::new(|data: Vec<u8>| image::load_from_memory(&data).map_err(Error::Image).map(|image| image.into_rgba32f()));
|
||||
let image: ComposeNode<'_, P, _, _> = FlatMapResultNode::new(image_loader).after(file);
|
||||
let convert_image = FnNode::new(|image: image::ImageBuffer<_, _>| {
|
||||
let data = image
|
||||
.enumerate_pixels()
|
||||
.map(|(_, _, pixel): (_, _, &image::Rgba<f32>)| {
|
||||
let c = pixel.channels();
|
||||
Color::from_rgbaf32(c[0], c[1], c[2], c[3]).unwrap()
|
||||
})
|
||||
.collect();
|
||||
Image {
|
||||
width: image.width(),
|
||||
height: image.height(),
|
||||
data,
|
||||
}
|
||||
});
|
||||
let image = MapResultNode::new(convert_image).after(image);
|
||||
image
|
||||
}
|
||||
|
||||
pub fn export_image_node<'n>() -> impl Node<'n, (Image, &'n str), Output = Result<(), Error>> {
|
||||
FnNode::new(|input: (Image, &str)| {
|
||||
let (image, path) = input;
|
||||
let mut new_image = image::ImageBuffer::new(image.width, image.height);
|
||||
for ((x, y, pixel), color) in new_image.enumerate_pixels_mut().zip((&image).into_iter()) {
|
||||
let color: Color = *color;
|
||||
assert!(x < image.width);
|
||||
assert!(y < image.height);
|
||||
*pixel = image::Rgba([color.r(), color.g(), color.b(), color.a()])
|
||||
}
|
||||
new_image.save(path).map_err(Error::Image)
|
||||
})
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::*;
|
||||
use graphene_core::raster::color::Color;
|
||||
use graphene_core::raster::GrayscaleNode;
|
||||
|
||||
#[test]
|
||||
fn map_node() {
|
||||
let array = [Color::from_rgbaf32(1.0, 0.0, 0.0, 1.0).unwrap()];
|
||||
let map = MapNode(GrayscaleNode, PhantomData);
|
||||
let values = map.eval(array.into_iter());
|
||||
assert_eq!(values[0], Color::from_rgbaf32(0.33333334, 0.33333334, 0.33333334, 1.0).unwrap());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn load_image() {
|
||||
/*let image = image_node();
|
||||
let gray = MapImageNode::new(GrayscaleNode);
|
||||
|
||||
let gray_scale_picture = MapResultNode::new(gray).after(image);
|
||||
let gray_scale_picture = gray_scale_picture.eval("image");
|
||||
*/
|
||||
let test_node = test_node();
|
||||
{
|
||||
let foo = test_node.eval(());
|
||||
std::mem::drop(foo);
|
||||
}
|
||||
let export = export_image_node();
|
||||
|
||||
/*let export = FnNode::new(|input: (&str, &str)| {
|
||||
let (input, output) = input;*/
|
||||
//let picture = gray_scale_picture.eval("/home/dennis/screenshot.png").unwrap().clone();
|
||||
//export.eval((picture, "screenshot.png"));
|
||||
/*});
|
||||
export.eval(("screenshot.png", "/home/dennis/screenshot.png"));*/
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue