mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-10-01 18:08:11 +08:00
Convert dehaze, empty_image, extend_image_to_bounds, and image to record kernels
This commit is contained in:
@@ -11,7 +11,7 @@ use core_types::uuid::NodeId;
|
|||||||
use core_types::{ATTR_BLEND_MODE, ATTR_CLIPPING_MASK, ATTR_EDITOR_LAYER_PATH, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFORM};
|
use core_types::{ATTR_BLEND_MODE, ATTR_CLIPPING_MASK, ATTR_EDITOR_LAYER_PATH, ATTR_OPACITY, ATTR_OPACITY_FILL, ATTR_TRANSFORM};
|
||||||
use glam::{DAffine2, DVec2};
|
use glam::{DAffine2, DVec2};
|
||||||
use raster_nodes::blending_nodes::blend_colors;
|
use raster_nodes::blending_nodes::blend_colors;
|
||||||
use raster_nodes::std_nodes::{empty_image, extend_image_to_bounds};
|
use raster_nodes::std_nodes::{empty_image_core, extend_image_to_bounds_core};
|
||||||
use raster_types::BitmapMut;
|
use raster_types::BitmapMut;
|
||||||
use raster_types::Image;
|
use raster_types::Image;
|
||||||
use raster_types::{CPU, Raster};
|
use raster_types::{CPU, Raster};
|
||||||
@@ -134,7 +134,11 @@ where
|
|||||||
pub fn create_brush_texture(brush_style: &BrushStyle) -> Raster<CPU> {
|
pub fn create_brush_texture(brush_style: &BrushStyle) -> Raster<CPU> {
|
||||||
let stamp = brush_stamp_generator(&(), brush_style.diameter, brush_style.color, brush_style.hardness, brush_style.flow);
|
let stamp = brush_stamp_generator(&(), brush_style.diameter, brush_style.color, brush_style.hardness, brush_style.flow);
|
||||||
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(brush_style.diameter), 0., -DVec2::splat(brush_style.diameter / 2.));
|
let transform = DAffine2::from_scale_angle_translation(DVec2::splat(brush_style.diameter), 0., -DVec2::splat(brush_style.diameter / 2.));
|
||||||
let blank_texture = empty_image(&(), transform, List::new_from_element(Color::TRANSPARENT)).into_iter().next().unwrap_or_default();
|
let blank_texture = {
|
||||||
|
let mut item = Item::new_from_element(empty_image_core(transform, Color::TRANSPARENT));
|
||||||
|
item.set_attribute(ATTR_TRANSFORM, transform);
|
||||||
|
item
|
||||||
|
};
|
||||||
let image = blend_stamp_closure(stamp, blank_texture, |a, b| blend_colors(a, b, BlendMode::Normal, 1.));
|
let image = blend_stamp_closure(stamp, blank_texture, |a, b| blend_colors(a, b, BlendMode::Normal, 1.));
|
||||||
|
|
||||||
image.into_element()
|
image.into_element()
|
||||||
@@ -221,8 +225,14 @@ fn brush(
|
|||||||
let mut brush_plan = cache.compute_brush_plan(list_item, &draw_strokes);
|
let mut brush_plan = cache.compute_brush_plan(list_item, &draw_strokes);
|
||||||
|
|
||||||
// TODO: Find a way to handle more than one item
|
// TODO: Find a way to handle more than one item
|
||||||
let Some(mut actual_image) = extend_image_to_bounds(&(), List::new_from_item(brush_plan.background), background_bounds).into_iter().next() else {
|
let mut actual_image = {
|
||||||
return List::new();
|
let background = brush_plan.background;
|
||||||
|
let transform: DAffine2 = background.attribute_cloned_or_default(ATTR_TRANSFORM);
|
||||||
|
let (element, attributes) = background.into_parts();
|
||||||
|
let (element, transform) = extend_image_to_bounds_core(element, transform, background_bounds);
|
||||||
|
let mut item = Item::from_parts(element, attributes);
|
||||||
|
item.set_attribute(ATTR_TRANSFORM, transform);
|
||||||
|
item
|
||||||
};
|
};
|
||||||
|
|
||||||
let final_stroke_idx = brush_plan.strokes.len().saturating_sub(1);
|
let final_stroke_idx = brush_plan.strokes.len().saturating_sub(1);
|
||||||
@@ -254,9 +264,16 @@ fn brush(
|
|||||||
|
|
||||||
let blit_target = if idx == 0 {
|
let blit_target = if idx == 0 {
|
||||||
let target = core::mem::take(&mut brush_plan.first_stroke_texture);
|
let target = core::mem::take(&mut brush_plan.first_stroke_texture);
|
||||||
extend_image_to_bounds(&(), List::new_from_item(target), stroke_to_layer)
|
let transform: DAffine2 = target.attribute_cloned_or_default(ATTR_TRANSFORM);
|
||||||
|
let (element, attributes) = target.into_parts();
|
||||||
|
let (element, transform) = extend_image_to_bounds_core(element, transform, stroke_to_layer);
|
||||||
|
let mut item = Item::from_parts(element, attributes);
|
||||||
|
item.set_attribute(ATTR_TRANSFORM, transform);
|
||||||
|
List::new_from_item(item)
|
||||||
} else {
|
} else {
|
||||||
empty_image(&(), stroke_to_layer, List::new_from_element(Color::TRANSPARENT))
|
let mut item = Item::new_from_element(empty_image_core(stroke_to_layer, Color::TRANSPARENT));
|
||||||
|
item.set_attribute(ATTR_TRANSFORM, stroke_to_layer);
|
||||||
|
List::new_from_item(item)
|
||||||
};
|
};
|
||||||
|
|
||||||
let list = blit(&(), blit_target, brush_texture, positions, |a, b| blend_colors(a, b, BlendMode::Normal, 1.));
|
let list = blit(&(), blit_target, brush_texture, positions, |a, b| blend_colors(a, b, BlendMode::Normal, 1.));
|
||||||
|
|||||||
@@ -1,5 +1,4 @@
|
|||||||
use core_types::context::Ctx;
|
use core_types::context::Ctx;
|
||||||
use core_types::list::List;
|
|
||||||
use core_types::registry::types::Percentage;
|
use core_types::registry::types::Percentage;
|
||||||
use image::{DynamicImage, GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma, Rgba, RgbaImage};
|
use image::{DynamicImage, GenericImage, GenericImageView, GrayImage, ImageBuffer, Luma, Rgba, RgbaImage};
|
||||||
use ndarray::{Array2, ArrayBase, Dim, OwnedRepr};
|
use ndarray::{Array2, ArrayBase, Dim, OwnedRepr};
|
||||||
@@ -8,33 +7,27 @@ use raster_types::{CPU, Raster};
|
|||||||
use std::cmp::{max, min};
|
use std::cmp::{max, min};
|
||||||
|
|
||||||
#[node_macro::node(category("Raster: Filter"))]
|
#[node_macro::node(category("Raster: Filter"))]
|
||||||
fn dehaze(_: impl Ctx, image_frame: List<Raster<CPU>>, strength: Percentage) -> List<Raster<CPU>> {
|
fn dehaze(_: impl Ctx, image_frame: Raster<CPU>, strength: Percentage) -> Raster<CPU> {
|
||||||
image_frame
|
let image = image_frame;
|
||||||
.into_iter()
|
// Prepare the image data for processing
|
||||||
.map(|mut row| {
|
let image_data = bytemuck::cast_vec(image.data.clone());
|
||||||
let image = std::mem::replace(row.element_mut(), Raster::new_cpu(Image::default()));
|
let image_buffer = image::Rgba32FImage::from_raw(image.width, image.height, image_data).expect("Failed to convert internal image format into image-rs data type.");
|
||||||
// Prepare the image data for processing
|
let dynamic_image: DynamicImage = image_buffer.into();
|
||||||
let image_data = bytemuck::cast_vec(image.data.clone());
|
|
||||||
let image_buffer = image::Rgba32FImage::from_raw(image.width, image.height, image_data).expect("Failed to convert internal image format into image-rs data type.");
|
|
||||||
let dynamic_image: DynamicImage = image_buffer.into();
|
|
||||||
|
|
||||||
// Run the dehaze algorithm
|
// Run the dehaze algorithm
|
||||||
let dehazed_dynamic_image = dehaze_image(dynamic_image, strength / 100.);
|
let dehazed_dynamic_image = dehaze_image(dynamic_image, strength / 100.);
|
||||||
|
|
||||||
// Prepare the image data for returning
|
// Prepare the image data for returning
|
||||||
let buffer = dehazed_dynamic_image.to_rgba32f().into_raw();
|
let buffer = dehazed_dynamic_image.to_rgba32f().into_raw();
|
||||||
let color_vec = bytemuck::cast_vec(buffer);
|
let color_vec = bytemuck::cast_vec(buffer);
|
||||||
let dehazed_image = Image {
|
let dehazed_image = Image {
|
||||||
width: image.width,
|
width: image.width,
|
||||||
height: image.height,
|
height: image.height,
|
||||||
data: color_vec,
|
data: color_vec,
|
||||||
base64_string: None,
|
base64_string: None,
|
||||||
};
|
};
|
||||||
|
|
||||||
*row.element_mut() = Raster::new_cpu(dehazed_image);
|
Raster::new_cpu(dehazed_image)
|
||||||
row
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// There is no real point in modifying these values because they do not change the final result all that much.
|
// There is no real point in modifying these values because they do not change the final result all that much.
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
use crate::adjustments::{CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, NoiseType};
|
use crate::adjustments::{CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, NoiseType};
|
||||||
use core_types::ATTR_TRANSFORM;
|
use core_types::ATTR_TRANSFORM;
|
||||||
|
use core_types::attribute::{Attr, Transform as TransformAttr};
|
||||||
use core_types::color::Color;
|
use core_types::color::Color;
|
||||||
use core_types::color::{Alpha, AlphaMut, Channel, LinearChannel, Luminance, RGBMut};
|
use core_types::color::{Alpha, AlphaMut, Channel, LinearChannel, Luminance, RGBMut};
|
||||||
use core_types::context::{Ctx, ExtractFootprint};
|
use core_types::context::{Ctx, ExtractFootprint, ExtractIndex, InjectIndex};
|
||||||
use core_types::list::{Item, List};
|
use core_types::list::{Item, List};
|
||||||
use core_types::math::bbox::Bbox;
|
use core_types::math::bbox::Bbox;
|
||||||
use core_types::transform::Transform;
|
use core_types::transform::Transform;
|
||||||
@@ -225,75 +226,76 @@ pub fn mask(
|
|||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category(""))]
|
/// The per-lane extend, shared with the brush's plain callers.
|
||||||
pub fn extend_image_to_bounds(_: impl Ctx, image: List<Raster<CPU>>, bounds: DAffine2) -> List<Raster<CPU>> {
|
pub fn extend_image_to_bounds_core(image: Raster<CPU>, row_transform: DAffine2, bounds: DAffine2) -> (Raster<CPU>, DAffine2) {
|
||||||
image
|
let image_aabb = Bbox::unit().affine_transform(row_transform).to_axis_aligned_bbox();
|
||||||
.into_iter()
|
let bounds_aabb = Bbox::unit().affine_transform(bounds.transform()).to_axis_aligned_bbox();
|
||||||
.map(|mut row| {
|
if image_aabb.contains(bounds_aabb.start) && image_aabb.contains(bounds_aabb.end) {
|
||||||
let row_transform: DAffine2 = row.attribute_cloned_or_default(ATTR_TRANSFORM);
|
return (image, row_transform);
|
||||||
let image_aabb = Bbox::unit().affine_transform(row_transform).to_axis_aligned_bbox();
|
}
|
||||||
let bounds_aabb = Bbox::unit().affine_transform(bounds.transform()).to_axis_aligned_bbox();
|
|
||||||
if image_aabb.contains(bounds_aabb.start) && image_aabb.contains(bounds_aabb.end) {
|
|
||||||
return row;
|
|
||||||
}
|
|
||||||
|
|
||||||
let image_data = &row.element().data;
|
let (image_width, image_height) = (image.width, image.height);
|
||||||
let (image_width, image_height) = (row.element().width, row.element().height);
|
if image_width == 0 || image_height == 0 {
|
||||||
if image_width == 0 || image_height == 0 {
|
return (empty_image_core(bounds, Color::TRANSPARENT), bounds);
|
||||||
return empty_image(&(), bounds, List::new_from_element(Color::TRANSPARENT)).into_iter().next().unwrap();
|
}
|
||||||
}
|
let image_data = &image.data;
|
||||||
|
|
||||||
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
|
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
|
||||||
let layer_to_image_space = DAffine2::from_scale(orig_image_scale) * row_transform.inverse();
|
let layer_to_image_space = DAffine2::from_scale(orig_image_scale) * row_transform.inverse();
|
||||||
let bounds_in_image_space = Bbox::unit().affine_transform(layer_to_image_space * bounds).to_axis_aligned_bbox();
|
let bounds_in_image_space = Bbox::unit().affine_transform(layer_to_image_space * bounds).to_axis_aligned_bbox();
|
||||||
|
|
||||||
let new_start = bounds_in_image_space.start.floor().min(DVec2::ZERO);
|
let new_start = bounds_in_image_space.start.floor().min(DVec2::ZERO);
|
||||||
let new_end = bounds_in_image_space.end.ceil().max(orig_image_scale);
|
let new_end = bounds_in_image_space.end.ceil().max(orig_image_scale);
|
||||||
let new_scale = new_end - new_start;
|
let new_scale = new_end - new_start;
|
||||||
|
|
||||||
// Copy over original image into enlarged image.
|
// Copy over original image into enlarged image.
|
||||||
let mut new_image = Image::new(new_scale.x as u32, new_scale.y as u32, Color::TRANSPARENT);
|
let mut new_image = Image::new(new_scale.x as u32, new_scale.y as u32, Color::TRANSPARENT);
|
||||||
let offset_in_new_image = (-new_start).as_uvec2();
|
let offset_in_new_image = (-new_start).as_uvec2();
|
||||||
for y in 0..image_height {
|
for y in 0..image_height {
|
||||||
let old_start = y * image_width;
|
let old_start = y * image_width;
|
||||||
let new_start = (y + offset_in_new_image.y) * new_image.width + offset_in_new_image.x;
|
let new_start = (y + offset_in_new_image.y) * new_image.width + offset_in_new_image.x;
|
||||||
let old_row = &image_data[old_start as usize..(old_start + image_width) as usize];
|
let old_row = &image_data[old_start as usize..(old_start + image_width) as usize];
|
||||||
let new_row = &mut new_image.data[new_start as usize..(new_start + image_width) as usize];
|
let new_row = &mut new_image.data[new_start as usize..(new_start + image_width) as usize];
|
||||||
new_row.copy_from_slice(old_row);
|
new_row.copy_from_slice(old_row);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Compute new transform.
|
// Compute new transform.
|
||||||
// let layer_to_new_texture_space = (DAffine2::from_scale(1. / new_scale) * DAffine2::from_translation(new_start) * layer_to_image_space).inverse();
|
// let layer_to_new_texture_space = (DAffine2::from_scale(1. / new_scale) * DAffine2::from_translation(new_start) * layer_to_image_space).inverse();
|
||||||
let new_texture_to_layer_space = row_transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale);
|
let new_texture_to_layer_space = row_transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale);
|
||||||
|
|
||||||
*row.element_mut() = Raster::new_cpu(new_image);
|
(Raster::new_cpu(new_image), new_texture_to_layer_space)
|
||||||
row.set_attribute(ATTR_TRANSFORM, new_texture_to_layer_space);
|
|
||||||
row
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category("Debug"))]
|
#[node_macro::node(category(""))]
|
||||||
pub fn empty_image(_: impl Ctx, transform: DAffine2, color: List<Color>) -> List<Raster<CPU>> {
|
pub fn extend_image_to_bounds(_: impl Ctx, (image, transform): (Raster<CPU>, Attr<TransformAttr>), bounds: DAffine2) -> (Raster<CPU>, Attr<TransformAttr>) {
|
||||||
|
let (image, transform) = extend_image_to_bounds_core(image, *transform, bounds);
|
||||||
|
(image, Attr(transform))
|
||||||
|
}
|
||||||
|
|
||||||
|
/// The blank texture a transform spans, shared with the brush's plain callers.
|
||||||
|
pub fn empty_image_core(transform: DAffine2, color: Color) -> Raster<CPU> {
|
||||||
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
|
let width = transform.transform_vector2(DVec2::new(1., 0.)).length() as u32;
|
||||||
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
|
let height = transform.transform_vector2(DVec2::new(0., 1.)).length() as u32;
|
||||||
|
|
||||||
let color = color.element(0).copied().unwrap_or(Color::WHITE);
|
Raster::new_cpu(Image::new(width, height, color))
|
||||||
let image = Image::new(width, height, color);
|
}
|
||||||
|
|
||||||
let mut result_list = List::new_from_element(Raster::new_cpu(image));
|
#[node_macro::node(category("Debug"))]
|
||||||
result_list.set_attribute(ATTR_TRANSFORM, 0, transform);
|
pub fn empty_image(_: impl Ctx + ExtractIndex + InjectIndex + Copy, transform: DAffine2, color: IList<Color>) -> (Raster<CPU>, Attr<TransformAttr>) {
|
||||||
|
let color = match color.len() {
|
||||||
// Callers of empty_image can safely unwrap on returned `List`
|
0 => Color::WHITE,
|
||||||
result_list
|
_ => color.get(0),
|
||||||
|
};
|
||||||
|
(empty_image_core(transform, color), Attr(transform))
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category(""))]
|
#[node_macro::node(category(""))]
|
||||||
pub fn image(_: impl Ctx, resource: Resource) -> List<Raster<CPU>> {
|
pub fn image(_: impl Ctx, resource: Resource) -> Raster<CPU> {
|
||||||
let image_data = resource.as_ref();
|
let image_data = resource.as_ref();
|
||||||
|
|
||||||
|
// A zero-size raster renders as nothing, matching the legacy empty list.
|
||||||
let Some(image) = ::image::load_from_memory(image_data).ok() else {
|
let Some(image) = ::image::load_from_memory(image_data).ok() else {
|
||||||
return List::new();
|
return Raster::new_cpu(Image::default());
|
||||||
};
|
};
|
||||||
let image = image.to_rgba32f();
|
let image = image.to_rgba32f();
|
||||||
let image = Image {
|
let image = Image {
|
||||||
@@ -308,7 +310,7 @@ pub fn image(_: impl Ctx, resource: Resource) -> List<Raster<CPU>> {
|
|||||||
height: image.height(),
|
height: image.height(),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
List::new_from_element(Raster::new_cpu(image))
|
Raster::new_cpu(image)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates customizable procedural noise patterns.
|
/// Generates customizable procedural noise patterns.
|
||||||
|
|||||||
Reference in New Issue
Block a user