mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-17 07:18:04 +08:00
Incremental compilation and stable node IDs (#977)
* Generate stable node ids * checkpoint * Implement borrow tree * Add eval function on borrow tree * Refactor Node trait to fix lifetime issues * Compiler infinite loop * Impl compose pair * Transition to double lifetime on trait * Change node trait to use a generic arg for the input * Start adapting node_macro * Migrate more nodes to new macro * Fix raster tests * Port vector nodes * Make Node trait object safe * Fix FlatMapResultNode * Translate most of gstd * Fix DowncastBothNode * Refactor node trait once again to allow for HRTB for type erased nodes * Start working on type erased nodes * Try getting DowncastBothNode to work * Introduce Upcasting node + work on BorrowTree * Make enough 'static to get the code to compile * Transition DynamicExecutor to use borrow tree * Make Compose Node use HRTB's * Fix MapResultNode * Disable blur test * Add workaround for Composing type erased nodes * Convert more nodes in the node_registry * Convert more of the node_registry * Add update tree fn and hook up to frontend * Fix blur node * Implement CacheNode * Make frontend use graph compiler * Fix document_node_types type declaration for most nodes * Remove unused imports * Move comment down * Reuse nodes via borrow tree * Deprecate trait based value in favor of TaggedValue * Remove unsafe code in buffer creation * Fix blur node * Fix stable node id generation * Fix types for Image adjustment document nodes * Fix Imaginate Node * Remove unused imports * Remove log * Fix off by one error * Remove macro generated imaginate node entry * Create parameterized add node * Fix test case * Remove link from layer_panel.rs * Fix formatting
This commit is contained in:
committed by
Keavon Chambers
parent
12d6818d73
commit
004e87ca3e
@@ -1,4 +1,4 @@
|
||||
use core::fmt::Debug;
|
||||
use core::{fmt::Debug, marker::PhantomData};
|
||||
|
||||
use crate::Node;
|
||||
|
||||
@@ -14,78 +14,62 @@ fn grayscale_color_node(input: Color) -> Color {
|
||||
Color::from_rgbaf32_unchecked(avg, avg, avg, input.a())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MapNode<Iter: Iterator, MapFn: Node<Iter::Item>> {
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MapNode<MapFn> {
|
||||
map_fn: MapFn,
|
||||
_phantom: core::marker::PhantomData<Iter>,
|
||||
}
|
||||
|
||||
impl<Iter: Iterator, MapFn: Node<Iter::Item> + Clone> Clone for MapNode<Iter, MapFn> {
|
||||
#[node_macro::node_fn(MapNode)]
|
||||
fn map_node<_Iter: Iterator, MapFnNode>(input: _Iter, map_fn: &'any_input MapFnNode) -> MapFnIterator<'input, 'input, _Iter, MapFnNode>
|
||||
where
|
||||
MapFnNode: for<'any_input> Node<'any_input, _Iter::Item>,
|
||||
{
|
||||
MapFnIterator::new(input, map_fn)
|
||||
}
|
||||
|
||||
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
||||
pub struct MapFnIterator<'i, 's, Iter, MapFn> {
|
||||
iter: Iter,
|
||||
map_fn: &'s MapFn,
|
||||
_phantom: core::marker::PhantomData<&'i &'s ()>,
|
||||
}
|
||||
|
||||
impl<'i, 's: 'i, Iter: Debug, MapFn> Debug for MapFnIterator<'i, 's, Iter, MapFn> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("MapFnIterator").field("iter", &self.iter).field("map_fn", &"MapFn").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<'i, 's: 'i, Iter: Clone, MapFn> Clone for MapFnIterator<'i, 's, Iter, MapFn> {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
map_fn: self.map_fn.clone(),
|
||||
_phantom: self._phantom,
|
||||
iter: self.iter.clone(),
|
||||
map_fn: self.map_fn,
|
||||
_phantom: core::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<Iter: Iterator, MapFn: Node<Iter::Item> + Copy> Copy for MapNode<Iter, MapFn> {}
|
||||
impl<'i, 's: 'i, Iter: Copy, MapFn> Copy for MapFnIterator<'i, 's, Iter, MapFn> {}
|
||||
|
||||
impl<Iter: Iterator, MapFn: Node<Iter::Item>> MapNode<Iter, MapFn> {
|
||||
pub fn new(map_fn: MapFn) -> Self {
|
||||
impl<'i, 's: 'i, Iter, MapFn> MapFnIterator<'i, 's, Iter, MapFn> {
|
||||
pub fn new(iter: Iter, map_fn: &'s MapFn) -> Self {
|
||||
Self {
|
||||
iter,
|
||||
map_fn,
|
||||
_phantom: core::marker::PhantomData,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item = Item>, MapFn: Node<Item, Output = Out>, Item, Out> Node<Iter> for MapNode<Iter, MapFn> {
|
||||
type Output = MapFnIterator<Iter, MapFn>;
|
||||
|
||||
#[inline]
|
||||
fn eval(self, input: Iter) -> Self::Output {
|
||||
MapFnIterator::new(input, self.map_fn)
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item = Item>, MapFn: Node<Item, Output = Out> + Copy, Item, Out> Node<Iter> for &MapNode<Iter, MapFn> {
|
||||
type Output = MapFnIterator<Iter, MapFn>;
|
||||
|
||||
#[inline]
|
||||
fn eval(self, input: Iter) -> Self::Output {
|
||||
MapFnIterator::new(input, self.map_fn)
|
||||
}
|
||||
}
|
||||
|
||||
#[must_use = "iterators are lazy and do nothing unless consumed"]
|
||||
#[derive(Clone)]
|
||||
pub struct MapFnIterator<Iter, MapFn> {
|
||||
iter: Iter,
|
||||
map_fn: MapFn,
|
||||
}
|
||||
|
||||
impl<Iter: Debug, MapFn> Debug for MapFnIterator<Iter, MapFn> {
|
||||
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
|
||||
f.debug_struct("MapFnIterator").field("iter", &self.iter).field("map_fn", &"MapFn").finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Iter: Copy, MapFn: Copy> Copy for MapFnIterator<Iter, MapFn> {}
|
||||
|
||||
impl<Iter, MapFn> MapFnIterator<Iter, MapFn> {
|
||||
pub fn new(iter: Iter, map_fn: MapFn) -> Self {
|
||||
Self { iter, map_fn }
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, I: Iterator, F> Iterator for MapFnIterator<I, F>
|
||||
impl<'i, 's: 'i, I: Iterator + 's, F> Iterator for MapFnIterator<'i, 's, I, F>
|
||||
where
|
||||
F: Node<I::Item, Output = B> + Copy,
|
||||
F: Node<'i, I::Item> + 'i,
|
||||
Self: 'i,
|
||||
{
|
||||
type Item = B;
|
||||
type Item = F::Output;
|
||||
|
||||
#[inline]
|
||||
fn next(&mut self) -> Option<B> {
|
||||
fn next(&mut self) -> Option<F::Output> {
|
||||
self.iter.next().map(|x| self.map_fn.eval(x))
|
||||
}
|
||||
|
||||
@@ -95,19 +79,14 @@ where
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct WeightedAvgNode<Iter> {
|
||||
_phantom: core::marker::PhantomData<Iter>,
|
||||
}
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct WeightedAvgNode {}
|
||||
|
||||
impl<Iter> WeightedAvgNode<Iter> {
|
||||
pub fn new() -> Self {
|
||||
Self { _phantom: core::marker::PhantomData }
|
||||
}
|
||||
}
|
||||
|
||||
#[inline]
|
||||
fn weighted_avg_node<Iter: Iterator<Item = (Color, f32)> + Clone>(input: Iter) -> Color {
|
||||
#[node_macro::node_fn(WeightedAvgNode)]
|
||||
fn weighted_avg_node<_Iter: Iterator<Item = (Color, f32)>>(input: _Iter) -> Color
|
||||
where
|
||||
_Iter: Clone,
|
||||
{
|
||||
let total_weight: f32 = input.clone().map(|(_, weight)| weight).sum();
|
||||
let total_r: f32 = input.clone().map(|(color, weight)| color.r() * weight).sum();
|
||||
let total_g: f32 = input.clone().map(|(color, weight)| color.g() * weight).sum();
|
||||
@@ -116,28 +95,10 @@ fn weighted_avg_node<Iter: Iterator<Item = (Color, f32)> + Clone>(input: Iter) -
|
||||
Color::from_rgbaf32_unchecked(total_r / total_weight, total_g / total_weight, total_b / total_weight, total_a / total_weight)
|
||||
}
|
||||
|
||||
impl<Iter: Iterator<Item = (Color, f32)> + Clone> Node<Iter> for WeightedAvgNode<Iter> {
|
||||
type Output = Color;
|
||||
|
||||
#[inline]
|
||||
fn eval(self, input: Iter) -> Self::Output {
|
||||
weighted_avg_node(input)
|
||||
}
|
||||
}
|
||||
impl<Iter: Iterator<Item = (Color, f32)> + Clone> Node<Iter> for &WeightedAvgNode<Iter> {
|
||||
type Output = Color;
|
||||
|
||||
#[inline]
|
||||
fn eval(self, input: Iter) -> Self::Output {
|
||||
weighted_avg_node(input)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[derive(Debug)]
|
||||
pub struct GaussianNode<Sigma> {
|
||||
sigma: Sigma,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(GaussianNode)]
|
||||
fn gaussian_node(input: f32, sigma: f64) -> f32 {
|
||||
let sigma = sigma as f32;
|
||||
@@ -157,42 +118,47 @@ fn distance_node(input: (i32, i32)) -> f32 {
|
||||
pub struct ImageIndexIterNode;
|
||||
|
||||
#[node_macro::node_fn(ImageIndexIterNode)]
|
||||
fn image_index_iter_node(input: ImageSlice<'static>) -> core::ops::Range<u32> {
|
||||
fn image_index_iter_node(input: ImageSlice<'input>) -> core::ops::Range<u32> {
|
||||
0..(input.width * input.height)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct WindowNode<Radius, Image> {
|
||||
#[derive(Debug)]
|
||||
pub struct WindowNode<Radius: for<'i> Node<'i, (), Output = u32>, Image: for<'i> Node<'i, (), Output = ImageSlice<'i>>> {
|
||||
radius: Radius,
|
||||
image: Image,
|
||||
}
|
||||
|
||||
impl<Radius, Image> WindowNode<Radius, Image> {
|
||||
pub fn new(radius: Radius, image: Image) -> Self {
|
||||
impl<'input, S0: 'input, S1: 'input> Node<'input, u32> for WindowNode<S0, S1>
|
||||
where
|
||||
S0: for<'any_input> Node<'any_input, (), Output = u32>,
|
||||
S1: for<'any_input> Node<'any_input, (), Output = ImageSlice<'any_input>>,
|
||||
{
|
||||
type Output = ImageWindowIterator<'input>;
|
||||
#[inline]
|
||||
fn eval<'node: 'input>(&'node self, input: u32) -> Self::Output {
|
||||
let radius = self.radius.eval(());
|
||||
let image = self.image.eval(());
|
||||
{
|
||||
let iter = ImageWindowIterator::new(image, radius, input);
|
||||
iter
|
||||
}
|
||||
}
|
||||
}
|
||||
impl<S0, S1> WindowNode<S0, S1>
|
||||
where
|
||||
S0: for<'any_input> Node<'any_input, (), Output = u32>,
|
||||
S1: for<'any_input> Node<'any_input, (), Output = ImageSlice<'any_input>>,
|
||||
{
|
||||
pub const fn new(radius: S0, image: S1) -> Self {
|
||||
Self { radius, image }
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, Radius: Node<(), Output = u32>, Image: Node<(), Output = ImageSlice<'a>>> Node<u32> for WindowNode<Radius, Image> {
|
||||
type Output = ImageWindowIterator<'a>;
|
||||
#[inline]
|
||||
fn eval(self, input: u32) -> Self::Output {
|
||||
let radius = self.radius.eval(());
|
||||
let image = self.image.eval(());
|
||||
let iter = ImageWindowIterator::new(image, radius, input);
|
||||
iter
|
||||
}
|
||||
}
|
||||
impl<'a, 'b: 'a, Radius: Node<(), Output = u32> + Copy, Index: Node<(), Output = ImageSlice<'b>> + Copy> Node<u32> for &'a WindowNode<Radius, Index> {
|
||||
type Output = ImageWindowIterator<'a>;
|
||||
#[inline]
|
||||
fn eval(self, input: u32) -> Self::Output {
|
||||
let radius = self.radius.eval(());
|
||||
let image = self.image.eval(());
|
||||
let iter = ImageWindowIterator::new(image, radius, input);
|
||||
iter
|
||||
}
|
||||
}
|
||||
/*
|
||||
#[node_macro::node_fn(WindowNode)]
|
||||
fn window_node(input: u32, radius: u32, image: ImageSlice<'input>) -> ImageWindowIterator<'input> {
|
||||
let iter = ImageWindowIterator::new(image, radius, input);
|
||||
iter
|
||||
}*/
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ImageWindowIterator<'a> {
|
||||
@@ -245,124 +211,74 @@ impl<'a> Iterator for ImageWindowIterator<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct MapSndNode<MapFn> {
|
||||
#[derive(Debug)]
|
||||
pub struct MapSndNode<First, Second, MapFn> {
|
||||
map_fn: MapFn,
|
||||
_first: PhantomData<First>,
|
||||
_second: PhantomData<Second>,
|
||||
}
|
||||
|
||||
impl<MapFn> MapSndNode<MapFn> {
|
||||
pub fn new(map_fn: MapFn) -> Self {
|
||||
Self { map_fn }
|
||||
}
|
||||
}
|
||||
|
||||
impl<MapFn: Node<I>, I, F> Node<(F, I)> for MapSndNode<MapFn> {
|
||||
type Output = (F, MapFn::Output);
|
||||
#[inline]
|
||||
fn eval(self, input: (F, I)) -> Self::Output {
|
||||
(input.0, self.map_fn.eval(input.1))
|
||||
}
|
||||
}
|
||||
impl<MapFn: Node<I> + Copy, I, F> Node<(F, I)> for &MapSndNode<MapFn> {
|
||||
type Output = (F, MapFn::Output);
|
||||
#[inline]
|
||||
fn eval(self, input: (F, I)) -> Self::Output {
|
||||
(input.0, self.map_fn.eval(input.1))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BrightenColorNode<N: Node<(), Output = f32>>(N);
|
||||
|
||||
impl<N: Node<(), Output = f32>> Node<Color> for BrightenColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let brightness = self.0.eval(());
|
||||
let per_channel = |col: f32| (col + brightness / 255.).clamp(0., 1.);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
}
|
||||
impl<N: Node<(), Output = f32> + Copy> Node<Color> for &BrightenColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let brightness = self.0.eval(());
|
||||
let per_channel = |col: f32| (col + brightness / 255.).clamp(0., 1.);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Node<(), Output = f32> + Copy> BrightenColorNode<N> {
|
||||
pub fn new(node: N) -> Self {
|
||||
Self(node)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct GammaColorNode<N: Node<(), Output = f32>>(N);
|
||||
|
||||
impl<N: Node<(), Output = f32>> Node<Color> for GammaColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let gamma = self.0.eval(());
|
||||
let per_channel = |col: f32| col.powf(gamma);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
}
|
||||
impl<N: Node<(), Output = f32> + Copy> Node<Color> for &GammaColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let gamma = self.0.eval(());
|
||||
let per_channel = |col: f32| col.powf(gamma);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
}
|
||||
|
||||
impl<N: Node<(), Output = f32> + Copy> GammaColorNode<N> {
|
||||
pub fn new(node: N) -> Self {
|
||||
Self(node)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
pub struct HueShiftColorNode<N: Node<(), Output = f32>>(N);
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
impl<N: Node<(), Output = f32>> Node<Color> for HueShiftColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let hue_shift = self.0.eval(());
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
Color::from_hsla(hue + hue_shift / 360., saturation, lightness, alpha)
|
||||
}
|
||||
}
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
impl<N: Node<(), Output = f32> + Copy> Node<Color> for &HueShiftColorNode<N> {
|
||||
type Output = Color;
|
||||
fn eval(self, color: Color) -> Color {
|
||||
let hue_shift = self.0.eval(());
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
Color::from_hsla(hue + hue_shift / 360., saturation, lightness, alpha)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
impl<N: Node<(), Output = f32> + Copy> HueShiftColorNode<N> {
|
||||
pub fn new(node: N) -> Self {
|
||||
Self(node)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ForEachNode<MN>(pub MN);
|
||||
|
||||
impl<'n, I: Iterator<Item = S>, MN: 'n, S> Node<I> for &'n ForEachNode<MN>
|
||||
#[node_macro::node_fn(MapSndNode< _First, _Second>)]
|
||||
fn map_snd_node<MapFn, _First, _Second>(input: (_First, _Second), map_fn: &'any_input MapFn) -> (_First, <MapFn as Node<'input, _Second>>::Output)
|
||||
where
|
||||
&'n MN: Node<S, Output = ()>,
|
||||
MapFn: for<'any_input> Node<'any_input, _Second>,
|
||||
{
|
||||
type Output = ();
|
||||
fn eval(self, input: I) -> Self::Output {
|
||||
input.for_each(|x| (&self.0).eval(x))
|
||||
let (a, b) = input;
|
||||
(a, map_fn.eval(b))
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct BrightenColorNode<Brightness> {
|
||||
brightness: Brightness,
|
||||
}
|
||||
#[node_macro::node_fn(BrightenColorNode)]
|
||||
fn brighten_color_node(color: Color, brightness: f32) -> Color {
|
||||
let per_channel = |col: f32| (col + brightness / 255.).clamp(0., 1.);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GammaColorNode<Gamma> {
|
||||
gamma: Gamma,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(GammaColorNode)]
|
||||
fn gamma_color_node(color: Color, gamma: f32) -> Color {
|
||||
let per_channel = |col: f32| col.powf(gamma);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
pub use hue_shift::HueShiftColorNode;
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
mod hue_shift {
|
||||
use super::*;
|
||||
#[derive(Debug)]
|
||||
pub struct HueShiftColorNode<Angle> {
|
||||
angle: Angle,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(HueShiftColorNode)]
|
||||
fn hue_shift_color_node(color: Color, angle: f32) -> Color {
|
||||
let hue_shift = angle;
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
Color::from_hsla(hue + hue_shift / 360., saturation, lightness, alpha)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ForEachNode<Iter, MapNode> {
|
||||
map_node: MapNode,
|
||||
_iter: PhantomData<Iter>,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(ForEachNode<_Iter>)]
|
||||
fn map_node<_Iter: Iterator, MapNode>(input: _Iter, map_node: &'any_input MapNode) -> ()
|
||||
where
|
||||
MapNode: for<'any_input> Node<'any_input, _Iter::Item, Output = ()> + 'input,
|
||||
{
|
||||
input.for_each(|x| map_node.eval(x));
|
||||
}
|
||||
|
||||
use dyn_any::{DynAny, StaticType};
|
||||
@@ -396,47 +312,24 @@ impl<'a> IntoIterator for &'a ImageSlice<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct MapImageSliceNode<MapFn>(MapFn);
|
||||
#[derive(Debug)]
|
||||
pub struct ImageDimensionsNode;
|
||||
|
||||
impl<MapFn> MapImageSliceNode<MapFn> {
|
||||
pub fn new(map_fn: MapFn) -> Self {
|
||||
Self(map_fn)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, MapFn: Node<ImageSlice<'a>, Output = Vec<Color>>> Node<ImageSlice<'a>> for MapImageSliceNode<MapFn> {
|
||||
type Output = Image;
|
||||
fn eval(self, image: ImageSlice<'a>) -> Self::Output {
|
||||
let data = self.0.eval(image);
|
||||
Image {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, MapFn: Copy + Node<ImageSlice<'a>, Output = Vec<Color>>> Node<ImageSlice<'a>> for &MapImageSliceNode<MapFn> {
|
||||
type Output = Image;
|
||||
fn eval(self, image: ImageSlice<'a>) -> Self::Output {
|
||||
let data = self.0.eval(image);
|
||||
Image {
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
data,
|
||||
}
|
||||
}
|
||||
#[node_macro::node_fn(ImageDimensionsNode)]
|
||||
fn dimensions_node(input: ImageSlice<'input>) -> (u32, u32) {
|
||||
(input.width, input.height)
|
||||
}
|
||||
|
||||
#[cfg(feature = "alloc")]
|
||||
pub use image::{CollectNode, Image, ImageRefNode};
|
||||
pub use image::{CollectNode, Image, ImageRefNode, MapImageSliceNode};
|
||||
#[cfg(feature = "alloc")]
|
||||
mod image {
|
||||
use super::{Color, ImageSlice};
|
||||
use crate::Node;
|
||||
use alloc::vec::Vec;
|
||||
use dyn_any::{DynAny, StaticType};
|
||||
#[derive(Clone, Debug, PartialEq, DynAny, Default, specta::Type)]
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DynAny, Default, specta::Type, Hash)]
|
||||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
|
||||
pub struct Image {
|
||||
pub width: u32,
|
||||
@@ -477,82 +370,63 @@ mod image {
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct ImageRefNode;
|
||||
|
||||
impl ImageRefNode {
|
||||
pub fn new() -> Self {
|
||||
Self
|
||||
}
|
||||
#[node_macro::node_fn(ImageRefNode)]
|
||||
fn image_ref_node(image: &'input Image) -> ImageSlice<'input> {
|
||||
image.as_slice()
|
||||
}
|
||||
|
||||
impl<'a> Node<&'a Image> for ImageRefNode {
|
||||
type Output = ImageSlice<'a>;
|
||||
fn eval(self, image: &'a Image) -> Self::Output {
|
||||
image.as_slice()
|
||||
}
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct CollectNode {}
|
||||
|
||||
#[node_macro::node_fn(CollectNode)]
|
||||
fn collect_node<_Iter>(input: _Iter) -> Vec<_Iter::Item>
|
||||
where
|
||||
_Iter: Iterator,
|
||||
{
|
||||
input.collect()
|
||||
}
|
||||
|
||||
impl<'a> Node<&'a Image> for &ImageRefNode {
|
||||
type Output = ImageSlice<'a>;
|
||||
fn eval(self, image: &'a Image) -> Self::Output {
|
||||
image.as_slice()
|
||||
}
|
||||
#[derive(Debug)]
|
||||
pub struct MapImageSliceNode<Data> {
|
||||
data: Data,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct CollectNode;
|
||||
|
||||
use crate::Node;
|
||||
impl<Iter: Iterator> Node<Iter> for CollectNode {
|
||||
type Output = Vec<Iter::Item>;
|
||||
fn eval(self, iter: Iter) -> Self::Output {
|
||||
iter.collect()
|
||||
}
|
||||
}
|
||||
impl<Iter: Iterator> Node<Iter> for &CollectNode {
|
||||
type Output = Vec<Iter::Item>;
|
||||
fn eval(self, iter: Iter) -> Self::Output {
|
||||
iter.collect()
|
||||
#[node_macro::node_fn(MapImageSliceNode)]
|
||||
fn map_node(input: (u32, u32), data: Vec<Color>) -> Image {
|
||||
Image {
|
||||
width: input.0,
|
||||
height: input.1,
|
||||
data,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*pub struct MutWrapper<N>(pub N);
|
||||
|
||||
impl<'n, T: Clone, N> Node<&'n mut T> for &'n MutWrapper<N>
|
||||
where
|
||||
&'n N: Node<T, Output = T>,
|
||||
{
|
||||
type Output = ();
|
||||
fn eval(self, value: &'n mut T) {
|
||||
*value = (&self.0).eval(value.clone());
|
||||
}
|
||||
}*/
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::{
|
||||
ops::TypeNode,
|
||||
structural::{ComposeNode, Then},
|
||||
value::ValueNode,
|
||||
};
|
||||
use crate::{ops::CloneNode, structural::Then, value::ValueNode, Node};
|
||||
|
||||
use super::*;
|
||||
use alloc::vec::Vec;
|
||||
|
||||
#[test]
|
||||
fn map_node() {
|
||||
// let array = &mut [Color::from_rgbaf32(1.0, 0.0, 0.0, 1.0).unwrap()];
|
||||
(&GrayscaleColorNode).eval(Color::from_rgbf32_unchecked(1., 0., 0.));
|
||||
GrayscaleColorNode.eval(Color::from_rgbf32_unchecked(1., 0., 0.));
|
||||
/*let map = ForEachNode(MutWrapper(GrayscaleNode));
|
||||
(&map).eval(array.iter_mut());
|
||||
assert_eq!(array[0], Color::from_rgbaf32(0.33333334, 0.33333334, 0.33333334, 1.0).unwrap());*/
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn window_node() {
|
||||
let radius = ValueNode::new(1u32);
|
||||
static DATA: &[Color] = &[Color::from_rgbf32_unchecked(1., 0., 0.); 25];
|
||||
let image = ValueNode::<_>::new(ImageSlice { width: 5, height: 5, data: DATA });
|
||||
use alloc::vec;
|
||||
let radius = ValueNode::new(1u32).then(CloneNode::new());
|
||||
let image = ValueNode::<_>::new(Image {
|
||||
width: 5,
|
||||
height: 5,
|
||||
data: vec![Color::from_rgbf32_unchecked(1., 0., 0.); 25],
|
||||
});
|
||||
let image = image.then(ImageRefNode::new());
|
||||
let window = WindowNode::new(radius, image);
|
||||
//let window: TypeNode<_, u32, ImageWindowIterator<'static>> = TypeNode::new(window);
|
||||
let vec = window.eval(0);
|
||||
assert_eq!(vec.count(), 4);
|
||||
let vec = window.eval(5);
|
||||
@@ -561,29 +435,50 @@ mod test {
|
||||
assert_eq!(vec.count(), 9);
|
||||
}
|
||||
|
||||
// TODO: I can't be bothered to fix this test rn
|
||||
/*
|
||||
#[test]
|
||||
fn blur_node() {
|
||||
let radius = ValueNode::new(1u32);
|
||||
let sigma = ValueNode::new(3f64);
|
||||
static DATA: &[Color] = &[Color::from_rgbf32_unchecked(1., 0., 0.); 20];
|
||||
let image = ValueNode::<_>::new(ImageSlice { width: 10, height: 2, data: DATA });
|
||||
use alloc::vec;
|
||||
let radius = ValueNode::new(1u32).then(CloneNode::new());
|
||||
let sigma = ValueNode::new(3f64).then(CloneNode::new());
|
||||
let radius = ValueNode::new(1u32).then(CloneNode::new());
|
||||
let image = ValueNode::<_>::new(Image {
|
||||
width: 5,
|
||||
height: 5,
|
||||
data: vec![Color::from_rgbf32_unchecked(1., 0., 0.); 25],
|
||||
});
|
||||
let image = image.then(ImageRefNode::new());
|
||||
let window = WindowNode::new(radius, image);
|
||||
let window: TypeNode<_, u32, ImageWindowIterator<'static>> = TypeNode::new(window);
|
||||
let pos_to_dist = MapSndNode::new(DistanceNode);
|
||||
let distance = window.then(MapNode::new(pos_to_dist));
|
||||
let map_gaussian = MapSndNode::new(GaussianNode::new(sigma));
|
||||
let map_distances: MapNode<_, MapSndNode<_>> = MapNode::new(map_gaussian);
|
||||
let window: TypeNode<_, u32, ImageWindowIterator<'_>> = TypeNode::new(window);
|
||||
let distance = ValueNode::new(DistanceNode::new());
|
||||
let pos_to_dist = MapSndNode::new(distance);
|
||||
let type_erased = &window as &dyn for<'a> Node<'a, u32, Output = ImageWindowIterator<'a>>;
|
||||
type_erased.eval(0);
|
||||
let map_pos_to_dist = MapNode::new(ValueNode::new(pos_to_dist));
|
||||
|
||||
let type_erased = &map_pos_to_dist as &dyn for<'a> Node<'a, u32, Output = ImageWindowIterator<'a>>;
|
||||
type_erased.eval(0);
|
||||
|
||||
let distance = window.then(map_pos_to_dist);
|
||||
let map_gaussian = MapSndNode::new(ValueNode(GaussianNode::new(sigma)));
|
||||
let map_gaussian: TypeNode<_, (_, f32), (_, f32)> = TypeNode::new(map_gaussian);
|
||||
let map_gaussian = ValueNode(map_gaussian);
|
||||
let map_gaussian: TypeNode<_, (), &_> = TypeNode::new(map_gaussian);
|
||||
let map_distances = MapNode::new(map_gaussian);
|
||||
let map_distances: TypeNode<_, _, MapFnIterator<'_, '_, _, _>> = TypeNode::new(map_distances);
|
||||
let gaussian_iter = distance.then(map_distances);
|
||||
let avg = gaussian_iter.then(WeightedAvgNode::new());
|
||||
let avg: TypeNode<_, u32, Color> = TypeNode::new(avg);
|
||||
let blur_iter = MapNode::new(avg);
|
||||
let blur_iter = MapNode::new(ValueNode::new(avg));
|
||||
let blur = image.then(ImageIndexIterNode).then(blur_iter);
|
||||
let blur: TypeNode<_, (), MapFnIterator<_, _>> = TypeNode::new(blur);
|
||||
let collect = CollectNode {};
|
||||
let collect = CollectNode::new();
|
||||
let vec = collect.eval(0..10);
|
||||
assert_eq!(vec.len(), 10);
|
||||
let vec = ComposeNode::new(blur, collect);
|
||||
let vec: TypeNode<_, (), Vec<Color>> = TypeNode::new(vec);
|
||||
let _ = blur.eval(());
|
||||
let vec = blur.then(collect);
|
||||
let _image = vec.eval(());
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user