Implement the Brush without relying on a stamp texture

Test Plan: Test the BrushNode in the editor

Reviewers: Keavon

Reviewed By: Keavon

Pull Request: https://github.com/GraphiteEditor/Graphite/pull/1184
This commit is contained in:
Dennis Kobert
2023-04-29 01:31:14 +02:00
committed by Keavon Chambers
parent 5d9c0cb4d5
commit 1020eb6835
31 changed files with 221 additions and 178 deletions

View File

@@ -1,8 +1,8 @@
use std::marker::PhantomData;
use glam::{DAffine2, DVec2};
use graphene_core::raster::{Color, Image, ImageFrame, RasterMut};
use graphene_core::transform::TransformMut;
use graphene_core::raster::{Alpha, Color, Pixel, Sample};
use graphene_core::transform::{Transform, TransformMut};
use graphene_core::vector::VectorData;
use graphene_core::Node;
use node_macro::node_fn;
@@ -73,8 +73,49 @@ fn vector_points(vector: VectorData) -> Vec<DVec2> {
vector.subpaths.iter().flat_map(|subpath| subpath.manipulator_groups().iter().map(|group| group.anchor)).collect()
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BrushStampGenerator<P: Pixel + Alpha> {
color: P,
feather_exponent: f32,
transform: DAffine2,
}
impl<P: Pixel + Alpha> Transform for BrushStampGenerator<P> {
fn transform(&self) -> DAffine2 {
self.transform
}
}
impl<P: Pixel + Alpha> TransformMut for BrushStampGenerator<P> {
fn transform_mut(&mut self) -> &mut DAffine2 {
&mut self.transform
}
}
impl<P: Pixel + Alpha> Sample for BrushStampGenerator<P> {
type Pixel = P;
#[inline]
fn sample(&self, position: DVec2, area: DVec2) -> Option<P> {
let position = self.transform.inverse().transform_point2(position);
let area = self.transform.inverse().transform_vector2(area);
let center = DVec2::splat(0.5);
let distance = (position + area / 2. - center).length() as f32 * 2.;
let result = if distance < 1. {
1. - distance.powf(self.feather_exponent)
} else {
return None;
};
use graphene_core::raster::Channel;
Some(self.color.multiplied_alpha(P::AlphaChannel::from_f32(result)))
}
}
#[derive(Clone, Debug, PartialEq)]
pub struct BrushTextureNode<ColorNode, Hardness, Flow> {
pub struct BrushStampGeneratorNode<ColorNode, Hardness, Flow> {
pub color: ColorNode,
pub hardness: Hardness,
pub flow: Flow,
@@ -92,17 +133,14 @@ fn erase(input: (Color, Color), flow: f64) -> Color {
Color::from_unassociated_alpha(input.r(), input.g(), input.b(), alpha)
}
#[node_fn(BrushTextureNode)]
fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> ImageFrame<Color> {
#[node_fn(BrushStampGeneratorNode)]
fn brush_stamp_generator_node(diameter: f64, color: Color, hardness: f64, flow: f64) -> BrushStampGenerator<Color> {
// Diameter
let radius = diameter / 2.;
// TODO: Remove the 4px padding after figuring out why the brush stamp gets randomly offset by 1px up/down/left/right when clicking with the Brush tool
let dimension = diameter.ceil() as u32 + 4;
let center = DVec2::splat(radius + (dimension as f64 - diameter) / 2.);
// Hardness
let hardness = hardness / 100.;
let feather_exponent = 1. / (1. - hardness);
let feather_exponent = 1. / (1. - hardness) as f32;
// Flow
let flow = flow / 100.;
@@ -110,33 +148,8 @@ fn brush_texture(diameter: f64, color: Color, hardness: f64, flow: f64) -> Image
// Color
let color = color.apply_opacity(flow as f32);
// Initial transparent image
let mut image = Image::new(dimension, dimension, Color::TRANSPARENT);
for y in 0..dimension {
for x in 0..dimension {
let summation = MULTISAMPLE_GRID.iter().fold(0., |acc, (offset_x, offset_y)| {
let position = DVec2::new(x as f64 + offset_x, y as f64 + offset_y);
let distance = (position - center).length();
if distance < radius {
acc + (1. - (distance / radius).powf(feather_exponent)).clamp(0., 1.)
} else {
acc
}
});
let pixel_fill = summation / MULTISAMPLE_GRID.len() as f64;
let pixel = image.get_pixel_mut(x, y).unwrap();
*pixel = color.apply_opacity(pixel_fill as f32);
}
}
ImageFrame {
image,
transform: DAffine2::from_scale_angle_translation(DVec2::splat(dimension as f64), 0., -DVec2::splat(radius)),
}
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(diameter), 0., -DVec2::splat(radius));
BrushStampGenerator { color, feather_exponent, transform }
}
#[derive(Clone, Debug, PartialEq)]
@@ -183,19 +196,17 @@ mod test {
#[test]
fn test_brush_texture() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
let brush_texture_node = BrushStampGeneratorNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(100.), ClonedNode::new(100.));
let size = 20.;
let image = brush_texture_node.eval(size);
assert_eq!(image.image.width, size.ceil() as u32 + 4);
assert_eq!(image.image.height, size.ceil() as u32 + 4);
assert_eq!(image.transform, DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil() + 4.), 0., -DVec2::splat(size / 2.)));
assert_eq!(image.transform(), DAffine2::from_scale_angle_translation(DVec2::splat(size.ceil()), 0., -DVec2::splat(size / 2.)));
// center pixel should be BLACK
assert_eq!(image.image.get_pixel(11, 11), Some(Color::BLACK));
assert_eq!(image.sample(DVec2::splat(0.), DVec2::ONE), Some(Color::BLACK));
}
#[test]
fn test_brush() {
let brush_texture_node = BrushTextureNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
let brush_texture_node = BrushStampGeneratorNode::new(ClonedNode::new(Color::BLACK), ClonedNode::new(1.0), ClonedNode::new(1.0));
let image = brush_texture_node.eval(20.);
let trace = vec![DVec2::new(0.0, 0.0), DVec2::new(10.0, 0.0)];
let trace = ClonedNode::new(trace.into_iter());
@@ -203,7 +214,6 @@ mod test {
let frames = MapNode::new(ValueNode::new(translate_node));
let frames = trace.then(frames).eval(()).collect::<Vec<_>>();
assert_eq!(frames.len(), 2);
assert_eq!(frames[0].image.width, 24);
let background_bounds = ReduceNode::new(ClonedNode::new(None), ValueNode::new(MergeBoundingBoxNode::new()));
let background_bounds = background_bounds.eval(frames.clone().into_iter());
let background_bounds = ClonedNode::new(background_bounds.unwrap().to_transform());
@@ -211,8 +221,8 @@ mod test {
let blend_node = graphene_core::raster::BlendNode::new(ClonedNode::new(BlendMode::Normal), ClonedNode::new(1.0));
let final_image = ReduceNode::new(background_image, ValueNode::new(BlendImageTupleNode::new(ValueNode::new(blend_node))));
let final_image = final_image.eval(frames.into_iter());
assert_eq!(final_image.image.height, 24);
assert_eq!(final_image.image.width, 34);
assert_eq!(final_image.image.height, 20);
assert_eq!(final_image.image.width, 30);
drop(final_image);
}
}

View File

@@ -28,12 +28,12 @@ where
if let Some((_, cached_value, keep)) = self.cache.iter().find(|(h, _, _)| *h == hash) {
keep.store(true, std::sync::atomic::Ordering::Relaxed);
return cached_value;
cached_value
} else {
trace!("Cache miss");
let output = self.node.eval(input);
let index = self.cache.push((hash, output, AtomicBool::new(true)));
return &self.cache[index].1;
&self.cache[index].1
}
}
@@ -70,7 +70,7 @@ where
fn serialize(&self) -> Option<String> {
let output = self.output.lock().unwrap();
(&*output).as_ref().map(|output| serde_json::to_string(output).ok()).flatten()
(*output).as_ref().and_then(|output| serde_json::to_string(output).ok())
}
}
@@ -110,7 +110,7 @@ impl<'i, T: 'i + Hash> Node<'i, Option<T>> for LetNode<T> {
}
trace!("Cache miss");
let index = self.cache.push((hash, input));
return &self.cache[index].1;
&self.cache[index].1
}
None => &self.cache.iter().last().expect("Let node was not initialized").1,
}

View File

@@ -2,7 +2,7 @@ use dyn_any::{DynAny, StaticType};
use glam::{DAffine2, DVec2};
use graphene_core::raster::{Alpha, Channel, Image, ImageFrame, Luminance, Pixel, RasterMut, Sample};
use graphene_core::transform::Transform;
use graphene_core::value::{ClonedNode, ValueNode};
use graphene_core::Node;
use std::fmt::Debug;
@@ -240,6 +240,7 @@ fn mask_image<
// Transforms a point from the background image to the forground image
let bg_to_fg = image.transform() * DAffine2::from_scale(1. / image_size);
let area = bg_to_fg.transform_point2(DVec2::new(1., 1.)) - bg_to_fg.transform_point2(DVec2::ZERO);
for y in 0..image.height() {
for x in 0..image.width() {
let image_point = DVec2::new(x as f64, y as f64);
@@ -247,8 +248,8 @@ fn mask_image<
let local_mask_point = stencil.transform().inverse().transform_point2(mask_point);
mask_point = stencil.transform().transform_point2(local_mask_point.clamp(DVec2::ZERO, DVec2::ONE));
let image_pixel = image.get_pixel_mut(x as u32, y as u32).unwrap();
if let Some(mask_pixel) = stencil.sample(mask_point) {
let image_pixel = image.get_pixel_mut(x, y).unwrap();
if let Some(mask_pixel) = stencil.sample(mask_point, area) {
*image_pixel = image_pixel.multiplied_alpha(mask_pixel.l().to_channel());
}
}
@@ -258,20 +259,20 @@ fn mask_image<
}
#[derive(Debug, Clone, Copy)]
pub struct BlendImageTupleNode<P, MapFn> {
pub struct BlendImageTupleNode<P, Fg, MapFn> {
map_fn: MapFn,
_p: PhantomData<P>,
_fg: PhantomData<Fg>,
}
#[node_macro::node_fn(BlendImageTupleNode<_P>)]
fn blend_image_tuple<_P: Pixel + Debug, MapFn>(images: (ImageFrame<_P>, ImageFrame<_P>), map_fn: &'any_input MapFn) -> ImageFrame<_P>
#[node_macro::node_fn(BlendImageTupleNode<_P, _Fg>)]
fn blend_image_tuple<_P: Pixel + Debug, MapFn, _Fg: Sample<Pixel = _P> + Transform>(images: (ImageFrame<_P>, _Fg), map_fn: &'any_input MapFn) -> ImageFrame<_P>
where
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P> + 'input + Clone,
{
let (background, foreground) = images;
let node = BlendImageNode::new(ClonedNode::new(background), ValueNode::new(map_fn.clone()));
node.eval(foreground)
blend_image(foreground, background, map_fn)
}
#[derive(Debug, Clone, Copy)]
@@ -283,13 +284,20 @@ pub struct BlendImageNode<P, Background, MapFn> {
// TODO: Implement proper blending
#[node_macro::node_fn(BlendImageNode<_P>)]
fn blend_image<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(
fn blend_image_node<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(
foreground: Frame,
mut background: Background,
background: Background,
map_fn: &'any_input MapFn,
) -> Background
where
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P> + 'input,
{
blend_image(foreground, background, map_fn)
}
fn blend_image<_P: Clone, MapFn, Frame: Sample<Pixel = _P> + Transform, Background: RasterMut<Pixel = _P> + Transform>(foreground: Frame, mut background: Background, map_fn: &MapFn) -> Background
where
MapFn: for<'any_input> Node<'any_input, (_P, _P), Output = _P>,
{
let background_size = DVec2::new(background.width() as f64, background.height() as f64);
@@ -303,12 +311,13 @@ where
let start = (bg_aabb.start * background_size).max(DVec2::ZERO).as_uvec2();
let end = (bg_aabb.end * background_size).min(background_size).as_uvec2();
let area = bg_to_fg.transform_point2(DVec2::new(1., 1.)) - bg_to_fg.transform_point2(DVec2::ZERO);
for y in start.y..end.y {
for x in start.x..end.x {
let bg_point = DVec2::new(x as f64, y as f64);
let fg_point = bg_to_fg.transform_point2(bg_point);
if let Some(src_pixel) = foreground.sample(fg_point) {
if let Some(src_pixel) = foreground.sample(fg_point, area) {
if let Some(dst_pixel) = background.get_pixel_mut(x, y) {
*dst_pixel = map_fn.eval((src_pixel, dst_pixel.clone()));
}