Instance tables refactor part 1: wrap graphical data in the new Instances<T> struct (#2230)

* Port VectorData to Instances<VectorData>

* Port ImageFrame<P> and TextureFrame to Instances<ImageFrame<P>> and Instances<TextureFrame>

* Avoid mutation with the TransformMut trait

* Port GraphicGroup to Instances<GraphicGroup>

* It compiles!

* Organize debugging

* Document upgrading

* Fix Brush node

* Restore TransformMut in lieu of TransformSet trait

* Fix tests

* Final code review
This commit is contained in:
Keavon Chambers
2025-01-28 23:51:12 -08:00
committed by GitHub
parent 408f9bffa1
commit eb0ff20d3c
43 changed files with 1855 additions and 1221 deletions

View File

@@ -1,15 +1,15 @@
#![allow(clippy::too_many_arguments)]
#[cfg(feature = "alloc")]
use super::curve::{Curve, CurveManipulatorGroup, ValueMapperNode};
use crate::raster::curve::{Curve, CurveManipulatorGroup, ValueMapperNode};
#[cfg(feature = "alloc")]
use super::ImageFrame;
use super::{Channel, Color, Pixel};
use crate::raster::image::{ImageFrame, ImageFrameTable};
use crate::raster::{Channel, Color, Pixel};
use crate::registry::types::{Angle, Percentage, SignedPercentage};
use crate::transform::Footprint;
use crate::vector::style::GradientStops;
use crate::vector::VectorData;
use crate::GraphicGroup;
use crate::vector::VectorDataTable;
use crate::{GraphicElement, GraphicGroupTable};
use dyn_any::DynAny;
@@ -294,10 +294,10 @@ async fn luminance<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -328,10 +328,10 @@ async fn extract_channel<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -361,10 +361,10 @@ async fn make_opaque<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -395,10 +395,10 @@ async fn levels<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -472,10 +472,10 @@ async fn black_and_white<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -554,10 +554,10 @@ async fn hue_saturation<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -598,10 +598,10 @@ async fn invert<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -630,10 +630,10 @@ async fn threshold<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -666,7 +666,6 @@ async fn threshold<F: 'n + Send, T: Adjust<Color>>(
trait Blend<P: Pixel> {
fn blend(&self, under: &Self, blend_fn: impl Fn(P, P) -> P) -> Self;
}
impl Blend<Color> for Color {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
blend_fn(*self, *under)
@@ -681,24 +680,28 @@ impl Blend<Color> for Option<Color> {
}
}
}
impl Blend<Color> for ImageFrame<Color> {
impl Blend<Color> for ImageFrameTable<Color> {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
let data = self.image.data.iter().zip(under.image.data.iter()).map(|(a, b)| blend_fn(*a, *b)).collect();
let mut result = self.clone();
ImageFrame {
image: super::Image {
data,
width: self.image.width,
height: self.image.height,
base64_string: None,
},
transform: self.transform,
alpha_blending: self.alpha_blending,
for (over, under) in result.instances_mut().zip(under.instances()) {
let data = over.image.data.iter().zip(under.image.data.iter()).map(|(a, b)| blend_fn(*a, *b)).collect();
*over = ImageFrame {
image: super::Image {
data,
width: over.image.width,
height: over.image.height,
base64_string: None,
},
transform: over.transform,
alpha_blending: over.alpha_blending,
};
}
result
}
}
impl Blend<Color> for GradientStops {
fn blend(&self, under: &Self, blend_fn: impl Fn(Color, Color) -> Color) -> Self {
let mut combined_stops = self.0.iter().map(|(position, _)| position).chain(under.0.iter().map(|(position, _)| position)).collect::<Vec<_>>();
@@ -730,20 +733,20 @@ async fn blend<F: 'n + Send + Copy, T: Blend<Color> + Send>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
over: impl Node<F, Output = T>,
#[expose]
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
under: impl Node<F, Output = T>,
@@ -753,7 +756,7 @@ async fn blend<F: 'n + Send + Copy, T: Blend<Color> + Send>(
let over = over.eval(footprint).await;
let under = under.eval(footprint).await;
Blend::blend(&over, &under, |a, b| blend_colors(a, b, blend_mode, opacity / 100.))
over.blend(&under, |a, b| blend_colors(a, b, blend_mode, opacity / 100.))
}
#[node_macro::node(category(""))]
@@ -800,8 +803,8 @@ pub fn apply_blend_mode(foreground: Color, background: Color, blend_mode: BlendM
}
}
trait Adjust<C> {
fn adjust(&mut self, map_fn: impl Fn(&C) -> C);
trait Adjust<P> {
fn adjust(&mut self, map_fn: impl Fn(&P) -> P);
}
impl Adjust<Color> for Color {
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
@@ -822,10 +825,17 @@ impl Adjust<Color> for GradientStops {
}
}
}
impl<C: Pixel> Adjust<C> for ImageFrame<C> {
fn adjust(&mut self, map_fn: impl Fn(&C) -> C) {
for c in self.image.data.iter_mut() {
*c = map_fn(c);
impl<P: Pixel> Adjust<P> for ImageFrameTable<P>
where
P: dyn_any::StaticType,
P::Static: Pixel,
GraphicElement: From<ImageFrame<P>>,
{
fn adjust(&mut self, map_fn: impl Fn(&P) -> P) {
for instance in self.instances_mut() {
for c in instance.image.data.iter_mut() {
*c = map_fn(c);
}
}
}
}
@@ -857,10 +867,10 @@ async fn gradient_map<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -896,10 +906,10 @@ async fn vibrance<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -1196,10 +1206,10 @@ async fn channel_mixer<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -1357,10 +1367,10 @@ async fn selective_color<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -1490,19 +1500,30 @@ impl MultiplyAlpha for Color {
*self = Color::from_rgbaf32_unchecked(self.r(), self.g(), self.b(), (self.a() * factor as f32).clamp(0., 1.))
}
}
impl MultiplyAlpha for VectorData {
impl MultiplyAlpha for VectorDataTable {
fn multiply_alpha(&mut self, factor: f64) {
self.alpha_blending.opacity *= factor as f32;
for instance in self.instances_mut() {
instance.alpha_blending.opacity *= factor as f32;
}
}
}
impl MultiplyAlpha for GraphicGroup {
impl MultiplyAlpha for GraphicGroupTable {
fn multiply_alpha(&mut self, factor: f64) {
self.alpha_blending.opacity *= factor as f32;
for instance in self.instances_mut() {
instance.alpha_blending.opacity *= factor as f32;
}
}
}
impl<P: Pixel> MultiplyAlpha for ImageFrame<P> {
impl<P: Pixel> MultiplyAlpha for ImageFrameTable<P>
where
P: dyn_any::StaticType,
P::Static: Pixel,
GraphicElement: From<ImageFrame<P>>,
{
fn multiply_alpha(&mut self, factor: f64) {
self.alpha_blending.opacity *= factor as f32;
for instance in self.instances_mut() {
instance.alpha_blending.opacity *= factor as f32;
}
}
}
@@ -1523,10 +1544,10 @@ async fn posterize<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -1566,10 +1587,10 @@ async fn exposure<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
input: impl Node<F, Output = T>,
@@ -1649,10 +1670,10 @@ async fn color_overlay<F: 'n + Send, T: Adjust<Color>>(
footprint: F,
#[implementations(
() -> Color,
() -> ImageFrame<Color>,
() -> ImageFrameTable<Color>,
() -> GradientStops,
Footprint -> Color,
Footprint -> ImageFrame<Color>,
Footprint -> ImageFrameTable<Color>,
Footprint -> GradientStops,
)]
image: impl Node<F, Output = T>,
@@ -1675,33 +1696,34 @@ async fn color_overlay<F: 'n + Send, T: Adjust<Color>>(
input
}
#[cfg(feature = "alloc")]
pub use index_node::IndexNode;
// #[cfg(feature = "alloc")]
// pub use index_node::IndexNode;
#[cfg(feature = "alloc")]
mod index_node {
use crate::raster::{Color, ImageFrame};
// #[cfg(feature = "alloc")]
// mod index_node {
// use crate::raster::{Color, ImageFrame};
#[node_macro::node(category(""))]
pub fn index<T: Default + Clone>(
_: (),
#[implementations(Vec<ImageFrame<Color>>, Vec<Color>)]
#[widget(ParsedWidgetOverride::Hidden)]
input: Vec<T>,
index: u32,
) -> T {
if (index as usize) < input.len() {
input[index as usize].clone()
} else {
warn!("The number of segments is {} but the requested segment is {}!", input.len(), index);
Default::default()
}
}
}
// #[node_macro::node(category(""))]
// pub fn index<T: Default + Clone>(
// _: (),
// #[implementations(Vec<ImageFrame<Color>>, Vec<Color>)]
// #[widget(ParsedWidgetOverride::Hidden)]
// input: Vec<T>,
// index: u32,
// ) -> T {
// if (index as usize) < input.len() {
// input[index as usize].clone()
// } else {
// warn!("The number of segments is {} but the requested segment is {}!", input.len(), index);
// Default::default()
// }
// }
// }
#[cfg(test)]
mod test {
use crate::raster::{BlendMode, Image, ImageFrame};
use crate::raster::image::{ImageFrame, ImageFrameTable};
use crate::raster::{BlendMode, Image};
use crate::{Color, Node};
use std::pin::Pin;
@@ -1730,7 +1752,8 @@ mod test {
// 100% of the output should come from the multiplied value
let opacity = 100_f64;
let result = super::color_overlay((), &FutureWrapperNode(image), overlay_color, BlendMode::Multiply, opacity).await;
let result = super::color_overlay((), &FutureWrapperNode(ImageFrameTable::new(image.clone())), overlay_color, BlendMode::Multiply, opacity).await;
let result = result.one_item();
// The output should just be the original green and alpha channels (as we multiply them by 1 and other channels by 0)
assert_eq!(result.image.data[0], Color::from_rgbaf32_unchecked(0., image_color.g(), 0., image_color.a()));

View File

@@ -5,8 +5,8 @@ use std::sync::Mutex;
use dyn_any::DynAny;
use crate::raster::image::ImageFrame;
use crate::raster::Image;
use crate::raster::ImageFrame;
use crate::vector::brush_stroke::BrushStroke;
use crate::vector::brush_stroke::BrushStyle;
use crate::Color;

View File

@@ -1,6 +1,7 @@
use super::discrete_srgb::float_to_srgb_u8;
use super::Color;
use crate::AlphaBlending;
use crate::instances::Instances;
use crate::{AlphaBlending, GraphicElement};
use alloc::vec::Vec;
use core::hash::{Hash, Hasher};
use dyn_any::StaticType;
@@ -216,7 +217,26 @@ impl<P: Pixel> IntoIterator for Image<P> {
}
}
#[derive(Clone, Debug, PartialEq, Default, specta::Type)]
// TODO: Eventually remove this migration document upgrade code
pub fn migrate_image_frame<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result<ImageFrameTable<Color>, D::Error> {
use serde::Deserialize;
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
enum EitherFormat {
ImageFrame(ImageFrame<Color>),
ImageFrameTable(ImageFrameTable<Color>),
}
Ok(match EitherFormat::deserialize(deserializer)? {
EitherFormat::ImageFrame(image_frame) => ImageFrameTable::<Color>::new(image_frame),
EitherFormat::ImageFrameTable(image_frame_table) => image_frame_table,
})
}
pub type ImageFrameTable<P> = Instances<ImageFrame<P>>;
#[derive(Clone, Debug, PartialEq, specta::Type)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ImageFrame<P: Pixel> {
pub image: Image<P>,
@@ -233,6 +253,17 @@ pub struct ImageFrame<P: Pixel> {
pub alpha_blending: AlphaBlending,
}
impl<P: Pixel> Default for ImageFrame<P> {
fn default() -> Self {
Self {
image: Image::empty(),
alpha_blending: AlphaBlending::new(),
// Different from DAffine2::default() which is IDENTITY
transform: DAffine2::ZERO,
}
}
}
impl<P: Debug + Copy + Pixel> Sample for ImageFrame<P> {
type Pixel = P;
@@ -248,6 +279,22 @@ impl<P: Debug + Copy + Pixel> Sample for ImageFrame<P> {
}
}
impl<P: Debug + Copy + Pixel + dyn_any::StaticType> Sample for ImageFrameTable<P>
where
GraphicElement: From<ImageFrame<P>>,
P::Static: Pixel,
{
type Pixel = P;
// TODO: Improve sampling logic
#[inline(always)]
fn sample(&self, pos: DVec2, area: DVec2) -> Option<Self::Pixel> {
let image = self.one_item();
Sample::sample(image, pos, area)
}
}
impl<P: Copy + Pixel> Bitmap for ImageFrame<P> {
type Pixel = P;
@@ -264,12 +311,50 @@ impl<P: Copy + Pixel> Bitmap for ImageFrame<P> {
}
}
impl<P: Copy + Pixel + dyn_any::StaticType> Bitmap for ImageFrameTable<P>
where
P::Static: Pixel,
GraphicElement: From<ImageFrame<P>>,
{
type Pixel = P;
fn width(&self) -> u32 {
let image = self.one_item();
image.width()
}
fn height(&self) -> u32 {
let image = self.one_item();
image.height()
}
fn get_pixel(&self, x: u32, y: u32) -> Option<Self::Pixel> {
let image = self.one_item();
image.get_pixel(x, y)
}
}
impl<P: Copy + Pixel> BitmapMut for ImageFrame<P> {
fn get_pixel_mut(&mut self, x: u32, y: u32) -> Option<&mut Self::Pixel> {
self.image.get_pixel_mut(x, y)
}
}
impl<P: Copy + Pixel + dyn_any::StaticType> BitmapMut for ImageFrameTable<P>
where
GraphicElement: From<ImageFrame<P>>,
P::Static: Pixel,
{
fn get_pixel_mut(&mut self, x: u32, y: u32) -> Option<&mut Self::Pixel> {
let image = self.one_item_mut();
BitmapMut::get_pixel_mut(image, x, y)
}
}
unsafe impl<P: dyn_any::StaticTypeSized + Pixel> StaticType for ImageFrame<P>
where
P::Static: Pixel,
@@ -278,22 +363,6 @@ where
}
impl<P: Copy + Pixel> ImageFrame<P> {
pub const fn empty() -> Self {
Self {
image: Image::empty(),
transform: DAffine2::ZERO,
alpha_blending: AlphaBlending::new(),
}
}
pub const fn identity() -> Self {
Self {
image: Image::empty(),
transform: DAffine2::IDENTITY,
alpha_blending: AlphaBlending::new(),
}
}
pub fn get_mut(&mut self, x: usize, y: usize) -> &mut P {
&mut self.image.data[y * (self.image.width as usize) + x]
}