mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2025-08-31 10:17:21 +00:00
Clean up code by using Iterator::collect() when constructing instance tables (#2918)
* instances: `Iterator::collect()` instances * instances: adjust nodes to use iterators * fix warnings on master * Bump MSRV * Port the remaining usages --------- Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
parent
032f9bdf72
commit
890da6a3c3
18 changed files with 964 additions and 985 deletions
|
@ -2,7 +2,7 @@
|
||||||
name = "graphite-editor"
|
name = "graphite-editor"
|
||||||
publish = false
|
publish = false
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
rust-version = "1.85"
|
rust-version = "1.88"
|
||||||
authors = ["Graphite Authors <contact@graphite.rs>"]
|
authors = ["Graphite Authors <contact@graphite.rs>"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
readme = "../README.md"
|
readme = "../README.md"
|
||||||
|
|
|
@ -6804,13 +6804,6 @@ impl From<DocumentNodePersistentMetadataPropertiesRow> for DocumentNodePersisten
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(serde::Serialize, serde::Deserialize)]
|
|
||||||
enum NodePersistentMetadataVersions {
|
|
||||||
DocumentNodePersistentMetadataPropertiesRow(DocumentNodePersistentMetadataPropertiesRow),
|
|
||||||
NodePersistentMetadataInputNames(DocumentNodePersistentMetadataInputNames),
|
|
||||||
NodePersistentMetadata(DocumentNodePersistentMetadata),
|
|
||||||
}
|
|
||||||
|
|
||||||
fn deserialize_node_persistent_metadata<'de, D>(deserializer: D) -> Result<DocumentNodePersistentMetadata, D::Error>
|
fn deserialize_node_persistent_metadata<'de, D>(deserializer: D) -> Result<DocumentNodePersistentMetadata, D::Error>
|
||||||
where
|
where
|
||||||
D: serde::Deserializer<'de>,
|
D: serde::Deserializer<'de>,
|
||||||
|
|
|
@ -1000,7 +1000,7 @@ impl ShapeState {
|
||||||
} else {
|
} else {
|
||||||
// Push both in and out handles into the correct position
|
// Push both in and out handles into the correct position
|
||||||
for ((handle, sign), other_anchor) in handles.iter().zip([1., -1.]).zip(&anchor_positions) {
|
for ((handle, sign), other_anchor) in handles.iter().zip([1., -1.]).zip(&anchor_positions) {
|
||||||
let Some(anchor_vector) = other_anchor.map(|position| (position - anchor_position)) else {
|
let Some(anchor_vector) = other_anchor.map(|position| position - anchor_position) else {
|
||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
name = "graphite-wasm"
|
name = "graphite-wasm"
|
||||||
publish = false
|
publish = false
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
rust-version = "1.85"
|
rust-version = "1.88"
|
||||||
authors = ["Graphite Authors <contact@graphite.rs>"]
|
authors = ["Graphite Authors <contact@graphite.rs>"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
readme = "../../README.md"
|
readme = "../../README.md"
|
||||||
|
@ -13,7 +13,7 @@ license = "Apache-2.0"
|
||||||
[features]
|
[features]
|
||||||
default = ["gpu"]
|
default = ["gpu"]
|
||||||
gpu = ["editor/gpu"]
|
gpu = ["editor/gpu"]
|
||||||
tauri = [ "editor/tauri"]
|
tauri = ["editor/tauri"]
|
||||||
|
|
||||||
[lib]
|
[lib]
|
||||||
crate-type = ["cdylib", "rlib"]
|
crate-type = ["cdylib", "rlib"]
|
||||||
|
|
|
@ -169,7 +169,7 @@ where
|
||||||
A::Item: Clone,
|
A::Item: Clone,
|
||||||
B::Item: Clone,
|
B::Item: Clone,
|
||||||
{
|
{
|
||||||
a.flat_map(move |i| (b.clone().map(move |j| (i.clone(), j))))
|
a.flat_map(move |i| b.clone().map(move |j| (i.clone(), j)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.
|
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.
|
||||||
|
|
|
@ -1086,7 +1086,7 @@ fn compute_dual(minor_graph: &MinorGraph) -> Result<DualGraph, BooleanError> {
|
||||||
let outer_face_key = if count != 1 {
|
let outer_face_key = if count != 1 {
|
||||||
#[cfg(feature = "logging")]
|
#[cfg(feature = "logging")]
|
||||||
eprintln!("Found multiple outer faces: {areas:?}, falling back to area calculation");
|
eprintln!("Found multiple outer faces: {areas:?}, falling back to area calculation");
|
||||||
let (key, _) = *areas.iter().max_by_key(|(_, area)| ((area.abs() * 1000.) as u64)).unwrap();
|
let (key, _) = *areas.iter().max_by_key(|(_, area)| (area.abs() * 1000.) as u64).unwrap();
|
||||||
*key
|
*key
|
||||||
} else {
|
} else {
|
||||||
*windings
|
*windings
|
||||||
|
|
|
@ -27,6 +27,24 @@ impl<T> Instances<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn new_instance(instance: Instance<T>) -> Self {
|
||||||
|
Self {
|
||||||
|
instance: vec![instance.instance],
|
||||||
|
transform: vec![instance.transform],
|
||||||
|
alpha_blending: vec![instance.alpha_blending],
|
||||||
|
source_node_id: vec![instance.source_node_id],
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn with_capacity(capacity: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
instance: Vec::with_capacity(capacity),
|
||||||
|
transform: Vec::with_capacity(capacity),
|
||||||
|
alpha_blending: Vec::with_capacity(capacity),
|
||||||
|
source_node_id: Vec::with_capacity(capacity),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub fn push(&mut self, instance: Instance<T>) {
|
pub fn push(&mut self, instance: Instance<T>) {
|
||||||
self.instance.push(instance.instance);
|
self.instance.push(instance.instance);
|
||||||
self.transform.push(instance.transform);
|
self.transform.push(instance.transform);
|
||||||
|
@ -161,6 +179,18 @@ unsafe impl<T: StaticType + 'static> StaticType for Instances<T> {
|
||||||
type Static = Instances<T>;
|
type Static = Instances<T>;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<T> FromIterator<Instance<T>> for Instances<T> {
|
||||||
|
fn from_iter<I: IntoIterator<Item = Instance<T>>>(iter: I) -> Self {
|
||||||
|
let iter = iter.into_iter();
|
||||||
|
let (lower, _) = iter.size_hint();
|
||||||
|
let mut instances = Self::with_capacity(lower);
|
||||||
|
for instance in iter {
|
||||||
|
instances.push(instance);
|
||||||
|
}
|
||||||
|
instances
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn one_daffine2_default() -> Vec<DAffine2> {
|
fn one_daffine2_default() -> Vec<DAffine2> {
|
||||||
vec![DAffine2::IDENTITY]
|
vec![DAffine2::IDENTITY]
|
||||||
}
|
}
|
||||||
|
|
|
@ -182,7 +182,7 @@ where
|
||||||
A::Item: Clone,
|
A::Item: Clone,
|
||||||
B::Item: Clone,
|
B::Item: Clone,
|
||||||
{
|
{
|
||||||
a.flat_map(move |i| (b.clone().map(move |j| (i.clone(), j))))
|
a.flat_map(move |i| b.clone().map(move |j| (i.clone(), j)))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.
|
/// A square (represented by its top left corner position and width/height of `square_size`) that is currently a candidate for targetting by the dart throwing process.
|
||||||
|
|
|
@ -337,7 +337,7 @@ impl VectorData {
|
||||||
/// Returns the number of linear segments connected to the given point.
|
/// Returns the number of linear segments connected to the given point.
|
||||||
pub fn connected_linear_segments(&self, point_id: PointId) -> usize {
|
pub fn connected_linear_segments(&self, point_id: PointId) -> usize {
|
||||||
self.segment_bezier_iter()
|
self.segment_bezier_iter()
|
||||||
.filter(|(_, bez, start, end)| ((*start == point_id || *end == point_id) && matches!(bez.handles, BezierHandles::Linear)))
|
.filter(|(_, bez, start, end)| (*start == point_id || *end == point_id) && matches!(bez.handles, BezierHandles::Linear))
|
||||||
.count()
|
.count()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
File diff suppressed because it is too large
Load diff
|
@ -80,8 +80,7 @@ fn union<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, Vector
|
||||||
// Reverse vector data so that the result style is the style of the first vector data
|
// Reverse vector data so that the result style is the style of the first vector data
|
||||||
let mut vector_data_reversed = vector_data.rev();
|
let mut vector_data_reversed = vector_data.rev();
|
||||||
|
|
||||||
let mut result_vector_data_table = VectorDataTable::default();
|
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data_reversed.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
||||||
result_vector_data_table.push(vector_data_reversed.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
|
||||||
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
||||||
|
|
||||||
// Loop over all vector data and union it with the result
|
// Loop over all vector data and union it with the result
|
||||||
|
@ -113,8 +112,7 @@ fn union<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, Vector
|
||||||
fn subtract<'a>(vector_data: impl Iterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
|
fn subtract<'a>(vector_data: impl Iterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
|
||||||
let mut vector_data = vector_data.into_iter();
|
let mut vector_data = vector_data.into_iter();
|
||||||
|
|
||||||
let mut result_vector_data_table = VectorDataTable::default();
|
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
||||||
result_vector_data_table.push(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
|
||||||
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
||||||
|
|
||||||
let mut next_vector_data = vector_data.next();
|
let mut next_vector_data = vector_data.next();
|
||||||
|
@ -145,8 +143,7 @@ fn subtract<'a>(vector_data: impl Iterator<Item = InstanceRef<'a, VectorData>>)
|
||||||
fn intersect<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
|
fn intersect<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, VectorData>>) -> VectorDataTable {
|
||||||
let mut vector_data = vector_data.rev();
|
let mut vector_data = vector_data.rev();
|
||||||
|
|
||||||
let mut result_vector_data_table = VectorDataTable::default();
|
let mut result_vector_data_table = VectorDataTable::new_instance(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
||||||
result_vector_data_table.push(vector_data.next().map(|x| x.to_instance_cloned()).unwrap_or_default());
|
|
||||||
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
let mut first_instance = result_vector_data_table.instance_mut_iter().next().expect("Expected the one instance we just pushed");
|
||||||
|
|
||||||
let default = Instance::default();
|
let default = Instance::default();
|
||||||
|
@ -225,71 +222,67 @@ fn difference<'a>(vector_data: impl DoubleEndedIterator<Item = InstanceRef<'a, V
|
||||||
}
|
}
|
||||||
|
|
||||||
fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTable {
|
fn flatten_vector_data(graphic_group_table: &GraphicGroupTable) -> VectorDataTable {
|
||||||
let mut result_table = VectorDataTable::default();
|
graphic_group_table
|
||||||
|
.instance_ref_iter()
|
||||||
|
.flat_map(|element| {
|
||||||
|
match element.instance.clone() {
|
||||||
|
GraphicElement::VectorData(vector_data) => {
|
||||||
|
// Apply the parent group's transform to each element of vector data
|
||||||
|
vector_data
|
||||||
|
.instance_iter()
|
||||||
|
.map(|mut sub_vector_data| {
|
||||||
|
sub_vector_data.transform = *element.transform * sub_vector_data.transform;
|
||||||
|
|
||||||
for element in graphic_group_table.instance_ref_iter() {
|
sub_vector_data
|
||||||
match element.instance.clone() {
|
})
|
||||||
GraphicElement::VectorData(vector_data) => {
|
.collect::<Vec<_>>()
|
||||||
// Apply the parent group's transform to each element of vector data
|
}
|
||||||
for mut sub_vector_data in vector_data.instance_iter() {
|
GraphicElement::RasterDataCPU(image) => {
|
||||||
sub_vector_data.transform = *element.transform * sub_vector_data.transform;
|
let make_instance = |transform| {
|
||||||
|
// Convert the image frame into a rectangular subpath with the image's transform
|
||||||
|
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
||||||
|
subpath.apply_transform(transform);
|
||||||
|
|
||||||
result_table.push(sub_vector_data);
|
// Create a vector data table row from the rectangular subpath, with a default black fill
|
||||||
|
let mut instance = VectorData::from_subpath(subpath);
|
||||||
|
instance.style.set_fill(Fill::Solid(Color::BLACK));
|
||||||
|
|
||||||
|
Instance { instance, ..Default::default() }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Apply the parent group's transform to each element of raster data
|
||||||
|
image.instance_ref_iter().map(|instance| make_instance(*element.transform * *instance.transform)).collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
GraphicElement::RasterDataGPU(image) => {
|
||||||
|
let make_instance = |transform| {
|
||||||
|
// Convert the image frame into a rectangular subpath with the image's transform
|
||||||
|
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
||||||
|
subpath.apply_transform(transform);
|
||||||
|
|
||||||
|
// Create a vector data table row from the rectangular subpath, with a default black fill
|
||||||
|
let mut instance = VectorData::from_subpath(subpath);
|
||||||
|
instance.style.set_fill(Fill::Solid(Color::BLACK));
|
||||||
|
|
||||||
|
Instance { instance, ..Default::default() }
|
||||||
|
};
|
||||||
|
|
||||||
|
// Apply the parent group's transform to each element of raster data
|
||||||
|
image.instance_ref_iter().map(|instance| make_instance(*element.transform * *instance.transform)).collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
GraphicElement::GraphicGroup(mut graphic_group) => {
|
||||||
|
// Apply the parent group's transform to each element of inner group
|
||||||
|
for sub_element in graphic_group.instance_mut_iter() {
|
||||||
|
*sub_element.transform = *element.transform * *sub_element.transform;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recursively flatten the inner group into vector data
|
||||||
|
let unioned = boolean_operation_on_vector_data_table(flatten_vector_data(&graphic_group).instance_ref_iter(), BooleanOperation::Union);
|
||||||
|
|
||||||
|
unioned.instance_iter().collect::<Vec<_>>()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
GraphicElement::RasterDataCPU(image) => {
|
})
|
||||||
let make_instance = |transform| {
|
.collect()
|
||||||
// Convert the image frame into a rectangular subpath with the image's transform
|
|
||||||
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
|
||||||
subpath.apply_transform(transform);
|
|
||||||
|
|
||||||
// Create a vector data table row from the rectangular subpath, with a default black fill
|
|
||||||
let mut instance = VectorData::from_subpath(subpath);
|
|
||||||
instance.style.set_fill(Fill::Solid(Color::BLACK));
|
|
||||||
|
|
||||||
Instance { instance, ..Default::default() }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Apply the parent group's transform to each element of raster data
|
|
||||||
for instance in image.instance_ref_iter() {
|
|
||||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GraphicElement::RasterDataGPU(image) => {
|
|
||||||
let make_instance = |transform| {
|
|
||||||
// Convert the image frame into a rectangular subpath with the image's transform
|
|
||||||
let mut subpath = Subpath::new_rect(DVec2::ZERO, DVec2::ONE);
|
|
||||||
subpath.apply_transform(transform);
|
|
||||||
|
|
||||||
// Create a vector data table row from the rectangular subpath, with a default black fill
|
|
||||||
let mut instance = VectorData::from_subpath(subpath);
|
|
||||||
instance.style.set_fill(Fill::Solid(Color::BLACK));
|
|
||||||
|
|
||||||
Instance { instance, ..Default::default() }
|
|
||||||
};
|
|
||||||
|
|
||||||
// Apply the parent group's transform to each element of raster data
|
|
||||||
for instance in image.instance_ref_iter() {
|
|
||||||
result_table.push(make_instance(*element.transform * *instance.transform));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
GraphicElement::GraphicGroup(mut graphic_group) => {
|
|
||||||
// Apply the parent group's transform to each element of inner group
|
|
||||||
for sub_element in graphic_group.instance_mut_iter() {
|
|
||||||
*sub_element.transform = *element.transform * *sub_element.transform;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Recursively flatten the inner group into vector data
|
|
||||||
let unioned = boolean_operation_on_vector_data_table(flatten_vector_data(&graphic_group).instance_ref_iter(), BooleanOperation::Union);
|
|
||||||
|
|
||||||
for element in unioned.instance_iter() {
|
|
||||||
result_table.push(element);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
result_table
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn to_path(vector: &VectorData, transform: DAffine2) -> Vec<path_bool::PathSegment> {
|
fn to_path(vector: &VectorData, transform: DAffine2) -> Vec<path_bool::PathSegment> {
|
||||||
|
|
|
@ -8,34 +8,33 @@ use std::cmp::{max, min};
|
||||||
|
|
||||||
#[node_macro::node(category("Raster: Filter"))]
|
#[node_macro::node(category("Raster: Filter"))]
|
||||||
async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<CPU>, strength: Percentage) -> RasterDataTable<CPU> {
|
async fn dehaze(_: impl Ctx, image_frame: RasterDataTable<CPU>, strength: Percentage) -> RasterDataTable<CPU> {
|
||||||
let mut result_table = RasterDataTable::default();
|
image_frame
|
||||||
|
.instance_iter()
|
||||||
|
.map(|mut image_frame_instance| {
|
||||||
|
let image = image_frame_instance.instance;
|
||||||
|
// Prepare the image data for processing
|
||||||
|
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();
|
||||||
|
|
||||||
for mut image_frame_instance in image_frame.instance_iter() {
|
// Run the dehaze algorithm
|
||||||
let image = image_frame_instance.instance;
|
let dehazed_dynamic_image = dehaze_image(dynamic_image, strength / 100.);
|
||||||
// Prepare the image data for processing
|
|
||||||
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
|
// Prepare the image data for returning
|
||||||
let dehazed_dynamic_image = dehaze_image(dynamic_image, strength / 100.);
|
let buffer = dehazed_dynamic_image.to_rgba32f().into_raw();
|
||||||
|
let color_vec = bytemuck::cast_vec(buffer);
|
||||||
|
let dehazed_image = Image {
|
||||||
|
width: image.width,
|
||||||
|
height: image.height,
|
||||||
|
data: color_vec,
|
||||||
|
base64_string: None,
|
||||||
|
};
|
||||||
|
|
||||||
// Prepare the image data for returning
|
image_frame_instance.instance = Raster::new_cpu(dehazed_image);
|
||||||
let buffer = dehazed_dynamic_image.to_rgba32f().into_raw();
|
image_frame_instance.source_node_id = None;
|
||||||
let color_vec = bytemuck::cast_vec(buffer);
|
image_frame_instance
|
||||||
let dehazed_image = Image {
|
})
|
||||||
width: image.width,
|
.collect()
|
||||||
height: image.height,
|
|
||||||
data: color_vec,
|
|
||||||
base64_string: None,
|
|
||||||
};
|
|
||||||
|
|
||||||
image_frame_instance.instance = Raster::new_cpu(dehazed_image);
|
|
||||||
image_frame_instance.source_node_id = None;
|
|
||||||
result_table.push(image_frame_instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
result_table
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
|
@ -20,27 +20,26 @@ async fn blur(
|
||||||
/// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software.
|
/// Opt to incorrectly apply the filter with color calculations in gamma space for compatibility with the results from other software.
|
||||||
gamma: bool,
|
gamma: bool,
|
||||||
) -> RasterDataTable<CPU> {
|
) -> RasterDataTable<CPU> {
|
||||||
let mut result_table = RasterDataTable::default();
|
image_frame
|
||||||
|
.instance_iter()
|
||||||
|
.map(|mut image_instance| {
|
||||||
|
let image = image_instance.instance.clone();
|
||||||
|
|
||||||
for mut image_instance in image_frame.instance_iter() {
|
// Run blur algorithm
|
||||||
let image = image_instance.instance.clone();
|
let blurred_image = if radius < 0.1 {
|
||||||
|
// Minimum blur radius
|
||||||
|
image.clone()
|
||||||
|
} else if box_blur {
|
||||||
|
Raster::new_cpu(box_blur_algorithm(image.into_data(), radius, gamma))
|
||||||
|
} else {
|
||||||
|
Raster::new_cpu(gaussian_blur_algorithm(image.into_data(), radius, gamma))
|
||||||
|
};
|
||||||
|
|
||||||
// Run blur algorithm
|
image_instance.instance = blurred_image;
|
||||||
let blurred_image = if radius < 0.1 {
|
image_instance.source_node_id = None;
|
||||||
// Minimum blur radius
|
image_instance
|
||||||
image.clone()
|
})
|
||||||
} else if box_blur {
|
.collect()
|
||||||
Raster::new_cpu(box_blur_algorithm(image.into_data(), radius, gamma))
|
|
||||||
} else {
|
|
||||||
Raster::new_cpu(gaussian_blur_algorithm(image.into_data(), radius, gamma))
|
|
||||||
};
|
|
||||||
|
|
||||||
image_instance.instance = blurred_image;
|
|
||||||
image_instance.source_node_id = None;
|
|
||||||
result_table.push(image_instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
result_table
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 1D gaussian kernel
|
// 1D gaussian kernel
|
||||||
|
|
|
@ -31,69 +31,68 @@ impl From<std::io::Error> for Error {
|
||||||
|
|
||||||
#[node_macro::node(category("Debug: Raster"))]
|
#[node_macro::node(category("Debug: Raster"))]
|
||||||
pub fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDataTable<CPU>) -> RasterDataTable<CPU> {
|
pub fn sample_image(ctx: impl ExtractFootprint + Clone + Send, image_frame: RasterDataTable<CPU>) -> RasterDataTable<CPU> {
|
||||||
let mut result_table = RasterDataTable::default();
|
image_frame
|
||||||
|
.instance_iter()
|
||||||
|
.filter_map(|mut image_frame_instance| {
|
||||||
|
let image_frame_transform = image_frame_instance.transform;
|
||||||
|
let image = image_frame_instance.instance;
|
||||||
|
|
||||||
for mut image_frame_instance in image_frame.instance_iter() {
|
// Resize the image using the image crate
|
||||||
let image_frame_transform = image_frame_instance.transform;
|
let data = bytemuck::cast_vec(image.data.clone());
|
||||||
let image = image_frame_instance.instance;
|
|
||||||
|
|
||||||
// Resize the image using the image crate
|
let footprint = ctx.footprint();
|
||||||
let data = bytemuck::cast_vec(image.data.clone());
|
let viewport_bounds = footprint.viewport_bounds_in_local_space();
|
||||||
|
let image_bounds = Bbox::from_transform(image_frame_transform).to_axis_aligned_bbox();
|
||||||
|
let intersection = viewport_bounds.intersect(&image_bounds);
|
||||||
|
let image_size = DAffine2::from_scale(DVec2::new(image.width as f64, image.height as f64));
|
||||||
|
let size = intersection.size();
|
||||||
|
let size_px = image_size.transform_vector2(size).as_uvec2();
|
||||||
|
|
||||||
let footprint = ctx.footprint();
|
// If the image would not be visible, add nothing.
|
||||||
let viewport_bounds = footprint.viewport_bounds_in_local_space();
|
if size.x <= 0. || size.y <= 0. {
|
||||||
let image_bounds = Bbox::from_transform(image_frame_transform).to_axis_aligned_bbox();
|
return None;
|
||||||
let intersection = viewport_bounds.intersect(&image_bounds);
|
}
|
||||||
let image_size = DAffine2::from_scale(DVec2::new(image.width as f64, image.height as f64));
|
|
||||||
let size = intersection.size();
|
|
||||||
let size_px = image_size.transform_vector2(size).as_uvec2();
|
|
||||||
|
|
||||||
// If the image would not be visible, add nothing.
|
let image_buffer = ::image::Rgba32FImage::from_raw(image.width, image.height, data).expect("Failed to convert internal image format into image-rs data type.");
|
||||||
if size.x <= 0. || size.y <= 0. {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let image_buffer = ::image::Rgba32FImage::from_raw(image.width, image.height, data).expect("Failed to convert internal image format into image-rs data type.");
|
let dynamic_image: ::image::DynamicImage = image_buffer.into();
|
||||||
|
let offset = (intersection.start - image_bounds.start).max(DVec2::ZERO);
|
||||||
|
let offset_px = image_size.transform_vector2(offset).as_uvec2();
|
||||||
|
let cropped = dynamic_image.crop_imm(offset_px.x, offset_px.y, size_px.x, size_px.y);
|
||||||
|
|
||||||
let dynamic_image: ::image::DynamicImage = image_buffer.into();
|
let viewport_resolution_x = footprint.transform.transform_vector2(DVec2::X * size.x).length();
|
||||||
let offset = (intersection.start - image_bounds.start).max(DVec2::ZERO);
|
let viewport_resolution_y = footprint.transform.transform_vector2(DVec2::Y * size.y).length();
|
||||||
let offset_px = image_size.transform_vector2(offset).as_uvec2();
|
let mut new_width = size_px.x;
|
||||||
let cropped = dynamic_image.crop_imm(offset_px.x, offset_px.y, size_px.x, size_px.y);
|
let mut new_height = size_px.y;
|
||||||
|
|
||||||
let viewport_resolution_x = footprint.transform.transform_vector2(DVec2::X * size.x).length();
|
// Only downscale the image for now
|
||||||
let viewport_resolution_y = footprint.transform.transform_vector2(DVec2::Y * size.y).length();
|
let resized = if new_width < image.width || new_height < image.height {
|
||||||
let mut new_width = size_px.x;
|
new_width = viewport_resolution_x as u32;
|
||||||
let mut new_height = size_px.y;
|
new_height = viewport_resolution_y as u32;
|
||||||
|
// TODO: choose filter based on quality requirements
|
||||||
|
cropped.resize_exact(new_width, new_height, ::image::imageops::Triangle)
|
||||||
|
} else {
|
||||||
|
cropped
|
||||||
|
};
|
||||||
|
let buffer = resized.to_rgba32f();
|
||||||
|
let buffer = buffer.into_raw();
|
||||||
|
let vec = bytemuck::cast_vec(buffer);
|
||||||
|
let image = Image {
|
||||||
|
width: new_width,
|
||||||
|
height: new_height,
|
||||||
|
data: vec,
|
||||||
|
base64_string: None,
|
||||||
|
};
|
||||||
|
// we need to adjust the offset if we truncate the offset calculation
|
||||||
|
|
||||||
// Only downscale the image for now
|
let new_transform = image_frame_transform * DAffine2::from_translation(offset) * DAffine2::from_scale(size);
|
||||||
let resized = if new_width < image.width || new_height < image.height {
|
|
||||||
new_width = viewport_resolution_x as u32;
|
|
||||||
new_height = viewport_resolution_y as u32;
|
|
||||||
// TODO: choose filter based on quality requirements
|
|
||||||
cropped.resize_exact(new_width, new_height, ::image::imageops::Triangle)
|
|
||||||
} else {
|
|
||||||
cropped
|
|
||||||
};
|
|
||||||
let buffer = resized.to_rgba32f();
|
|
||||||
let buffer = buffer.into_raw();
|
|
||||||
let vec = bytemuck::cast_vec(buffer);
|
|
||||||
let image = Image {
|
|
||||||
width: new_width,
|
|
||||||
height: new_height,
|
|
||||||
data: vec,
|
|
||||||
base64_string: None,
|
|
||||||
};
|
|
||||||
// we need to adjust the offset if we truncate the offset calculation
|
|
||||||
|
|
||||||
let new_transform = image_frame_transform * DAffine2::from_translation(offset) * DAffine2::from_scale(size);
|
image_frame_instance.transform = new_transform;
|
||||||
|
image_frame_instance.source_node_id = None;
|
||||||
image_frame_instance.transform = new_transform;
|
image_frame_instance.instance = Raster::new_cpu(image);
|
||||||
image_frame_instance.source_node_id = None;
|
Some(image_frame_instance)
|
||||||
image_frame_instance.instance = Raster::new_cpu(image);
|
})
|
||||||
result_table.push(image_frame_instance)
|
.collect()
|
||||||
}
|
|
||||||
|
|
||||||
result_table
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category("Raster: Channels"))]
|
#[node_macro::node(category("Raster: Channels"))]
|
||||||
|
@ -105,84 +104,85 @@ pub fn combine_channels(
|
||||||
#[expose] blue: RasterDataTable<CPU>,
|
#[expose] blue: RasterDataTable<CPU>,
|
||||||
#[expose] alpha: RasterDataTable<CPU>,
|
#[expose] alpha: RasterDataTable<CPU>,
|
||||||
) -> RasterDataTable<CPU> {
|
) -> RasterDataTable<CPU> {
|
||||||
let mut result_table = RasterDataTable::default();
|
|
||||||
|
|
||||||
let max_len = red.len().max(green.len()).max(blue.len()).max(alpha.len());
|
let max_len = red.len().max(green.len()).max(blue.len()).max(alpha.len());
|
||||||
let red = red.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
let red = red.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
||||||
let green = green.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
let green = green.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
||||||
let blue = blue.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
let blue = blue.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
||||||
let alpha = alpha.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
let alpha = alpha.instance_iter().map(Some).chain(std::iter::repeat(None)).take(max_len);
|
||||||
|
|
||||||
for (((red, green), blue), alpha) in red.zip(green).zip(blue).zip(alpha) {
|
red.zip(green)
|
||||||
// Turn any default zero-sized image instances into None
|
.zip(blue)
|
||||||
let red = red.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
.zip(alpha)
|
||||||
let green = green.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
.filter_map(|(((red, green), blue), alpha)| {
|
||||||
let blue = blue.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
// Turn any default zero-sized image instances into None
|
||||||
let alpha = alpha.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
let red = red.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
||||||
|
let green = green.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
||||||
|
let blue = blue.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
||||||
|
let alpha = alpha.filter(|i| i.instance.width > 0 && i.instance.height > 0);
|
||||||
|
|
||||||
// Get this instance's transform and alpha blending mode from the first non-empty channel
|
// Get this instance's transform and alpha blending mode from the first non-empty channel
|
||||||
let Some((transform, alpha_blending)) = [&red, &green, &blue, &alpha].iter().find_map(|i| i.as_ref()).map(|i| (i.transform, i.alpha_blending)) else {
|
let Some((transform, alpha_blending)) = [&red, &green, &blue, &alpha].iter().find_map(|i| i.as_ref()).map(|i| (i.transform, i.alpha_blending)) else {
|
||||||
continue;
|
return None;
|
||||||
};
|
};
|
||||||
|
|
||||||
// Get the common width and height of the channels, which must have equal dimensions
|
// Get the common width and height of the channels, which must have equal dimensions
|
||||||
let channel_dimensions = [
|
let channel_dimensions = [
|
||||||
red.as_ref().map(|r| (r.instance.width, r.instance.height)),
|
red.as_ref().map(|r| (r.instance.width, r.instance.height)),
|
||||||
green.as_ref().map(|g| (g.instance.width, g.instance.height)),
|
green.as_ref().map(|g| (g.instance.width, g.instance.height)),
|
||||||
blue.as_ref().map(|b| (b.instance.width, b.instance.height)),
|
blue.as_ref().map(|b| (b.instance.width, b.instance.height)),
|
||||||
alpha.as_ref().map(|a| (a.instance.width, a.instance.height)),
|
alpha.as_ref().map(|a| (a.instance.width, a.instance.height)),
|
||||||
];
|
];
|
||||||
if channel_dimensions.iter().all(Option::is_none)
|
if channel_dimensions.iter().all(Option::is_none)
|
||||||
|| channel_dimensions
|
|| channel_dimensions
|
||||||
.iter()
|
.iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
.any(|&(x, y)| channel_dimensions.iter().flatten().any(|&(other_x, other_y)| x != other_x || y != other_y))
|
.any(|&(x, y)| channel_dimensions.iter().flatten().any(|&(other_x, other_y)| x != other_x || y != other_y))
|
||||||
{
|
{
|
||||||
continue;
|
return None;
|
||||||
}
|
}
|
||||||
let Some(&(width, height)) = channel_dimensions.iter().flatten().next() else { continue };
|
let Some(&(width, height)) = channel_dimensions.iter().flatten().next() else {
|
||||||
|
return None;
|
||||||
|
};
|
||||||
|
|
||||||
// Create a new image for this instance output
|
// Create a new image for this instance output
|
||||||
let mut image = Image::new(width, height, Color::TRANSPARENT);
|
let mut image = Image::new(width, height, Color::TRANSPARENT);
|
||||||
|
|
||||||
// Iterate over all pixels in the image and set the color channels
|
// Iterate over all pixels in the image and set the color channels
|
||||||
for y in 0..image.height() {
|
for y in 0..image.height() {
|
||||||
for x in 0..image.width() {
|
for x in 0..image.width() {
|
||||||
let image_pixel = image.get_pixel_mut(x, y).unwrap();
|
let image_pixel = image.get_pixel_mut(x, y).unwrap();
|
||||||
|
|
||||||
if let Some(r) = red.as_ref().and_then(|r| r.instance.get_pixel(x, y)) {
|
if let Some(r) = red.as_ref().and_then(|r| r.instance.get_pixel(x, y)) {
|
||||||
image_pixel.set_red(r.l().cast_linear_channel());
|
image_pixel.set_red(r.l().cast_linear_channel());
|
||||||
} else {
|
} else {
|
||||||
image_pixel.set_red(Channel::from_linear(0.));
|
image_pixel.set_red(Channel::from_linear(0.));
|
||||||
}
|
}
|
||||||
if let Some(g) = green.as_ref().and_then(|g| g.instance.get_pixel(x, y)) {
|
if let Some(g) = green.as_ref().and_then(|g| g.instance.get_pixel(x, y)) {
|
||||||
image_pixel.set_green(g.l().cast_linear_channel());
|
image_pixel.set_green(g.l().cast_linear_channel());
|
||||||
} else {
|
} else {
|
||||||
image_pixel.set_green(Channel::from_linear(0.));
|
image_pixel.set_green(Channel::from_linear(0.));
|
||||||
}
|
}
|
||||||
if let Some(b) = blue.as_ref().and_then(|b| b.instance.get_pixel(x, y)) {
|
if let Some(b) = blue.as_ref().and_then(|b| b.instance.get_pixel(x, y)) {
|
||||||
image_pixel.set_blue(b.l().cast_linear_channel());
|
image_pixel.set_blue(b.l().cast_linear_channel());
|
||||||
} else {
|
} else {
|
||||||
image_pixel.set_blue(Channel::from_linear(0.));
|
image_pixel.set_blue(Channel::from_linear(0.));
|
||||||
}
|
}
|
||||||
if let Some(a) = alpha.as_ref().and_then(|a| a.instance.get_pixel(x, y)) {
|
if let Some(a) = alpha.as_ref().and_then(|a| a.instance.get_pixel(x, y)) {
|
||||||
image_pixel.set_alpha(a.l().cast_linear_channel());
|
image_pixel.set_alpha(a.l().cast_linear_channel());
|
||||||
} else {
|
} else {
|
||||||
image_pixel.set_alpha(Channel::from_linear(1.));
|
image_pixel.set_alpha(Channel::from_linear(1.));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
// Add this instance to the result table
|
Some(Instance {
|
||||||
result_table.push(Instance {
|
instance: Raster::new_cpu(image),
|
||||||
instance: Raster::new_cpu(image),
|
transform,
|
||||||
transform,
|
alpha_blending,
|
||||||
alpha_blending,
|
source_node_id: None,
|
||||||
source_node_id: None,
|
})
|
||||||
});
|
})
|
||||||
}
|
.collect()
|
||||||
|
|
||||||
result_table
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category("Raster"))]
|
#[node_macro::node(category("Raster"))]
|
||||||
|
@ -201,91 +201,85 @@ pub fn mask(
|
||||||
};
|
};
|
||||||
let stencil_size = DVec2::new(stencil_instance.instance.width as f64, stencil_instance.instance.height as f64);
|
let stencil_size = DVec2::new(stencil_instance.instance.width as f64, stencil_instance.instance.height as f64);
|
||||||
|
|
||||||
let mut result_table = RasterDataTable::default();
|
image
|
||||||
|
.instance_iter()
|
||||||
|
.filter_map(|mut image_instance| {
|
||||||
|
let image_size = DVec2::new(image_instance.instance.width as f64, image_instance.instance.height as f64);
|
||||||
|
let mask_size = stencil_instance.transform.decompose_scale();
|
||||||
|
|
||||||
for mut image_instance in image.instance_iter() {
|
if mask_size == DVec2::ZERO {
|
||||||
let image_size = DVec2::new(image_instance.instance.width as f64, image_instance.instance.height as f64);
|
return None;
|
||||||
let mask_size = stencil_instance.transform.decompose_scale();
|
|
||||||
|
|
||||||
if mask_size == DVec2::ZERO {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Transforms a point from the background image to the foreground image
|
|
||||||
let bg_to_fg = image_instance.transform * DAffine2::from_scale(1. / image_size);
|
|
||||||
let stencil_transform_inverse = stencil_instance.transform.inverse();
|
|
||||||
|
|
||||||
for y in 0..image_instance.instance.height {
|
|
||||||
for x in 0..image_instance.instance.width {
|
|
||||||
let image_point = DVec2::new(x as f64, y as f64);
|
|
||||||
let mask_point = bg_to_fg.transform_point2(image_point);
|
|
||||||
let local_mask_point = stencil_transform_inverse.transform_point2(mask_point);
|
|
||||||
let mask_point = stencil_instance.transform.transform_point2(local_mask_point.clamp(DVec2::ZERO, DVec2::ONE));
|
|
||||||
let mask_point = (DAffine2::from_scale(stencil_size) * stencil_instance.transform.inverse()).transform_point2(mask_point);
|
|
||||||
|
|
||||||
let image_pixel = image_instance.instance.data_mut().get_pixel_mut(x, y).unwrap();
|
|
||||||
let mask_pixel = stencil_instance.instance.sample(mask_point);
|
|
||||||
*image_pixel = image_pixel.multiplied_alpha(mask_pixel.l().cast_linear_channel());
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
result_table.push(image_instance);
|
// Transforms a point from the background image to the foreground image
|
||||||
}
|
let bg_to_fg = image_instance.transform * DAffine2::from_scale(1. / image_size);
|
||||||
|
let stencil_transform_inverse = stencil_instance.transform.inverse();
|
||||||
|
|
||||||
result_table
|
for y in 0..image_instance.instance.height {
|
||||||
|
for x in 0..image_instance.instance.width {
|
||||||
|
let image_point = DVec2::new(x as f64, y as f64);
|
||||||
|
let mask_point = bg_to_fg.transform_point2(image_point);
|
||||||
|
let local_mask_point = stencil_transform_inverse.transform_point2(mask_point);
|
||||||
|
let mask_point = stencil_instance.transform.transform_point2(local_mask_point.clamp(DVec2::ZERO, DVec2::ONE));
|
||||||
|
let mask_point = (DAffine2::from_scale(stencil_size) * stencil_instance.transform.inverse()).transform_point2(mask_point);
|
||||||
|
|
||||||
|
let image_pixel = image_instance.instance.data_mut().get_pixel_mut(x, y).unwrap();
|
||||||
|
let mask_pixel = stencil_instance.instance.sample(mask_point);
|
||||||
|
*image_pixel = image_pixel.multiplied_alpha(mask_pixel.l().cast_linear_channel());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(image_instance)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category(""))]
|
#[node_macro::node(category(""))]
|
||||||
pub fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<CPU>, bounds: DAffine2) -> RasterDataTable<CPU> {
|
pub fn extend_image_to_bounds(_: impl Ctx, image: RasterDataTable<CPU>, bounds: DAffine2) -> RasterDataTable<CPU> {
|
||||||
let mut result_table = RasterDataTable::default();
|
image
|
||||||
|
.instance_iter()
|
||||||
for mut image_instance in image.instance_iter() {
|
.map(|mut image_instance| {
|
||||||
let image_aabb = Bbox::unit().affine_transform(image_instance.transform).to_axis_aligned_bbox();
|
let image_aabb = Bbox::unit().affine_transform(image_instance.transform).to_axis_aligned_bbox();
|
||||||
let bounds_aabb = Bbox::unit().affine_transform(bounds.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) {
|
if image_aabb.contains(bounds_aabb.start) && image_aabb.contains(bounds_aabb.end) {
|
||||||
result_table.push(image_instance);
|
return image_instance;
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let image_data = &image_instance.instance.data;
|
|
||||||
let (image_width, image_height) = (image_instance.instance.width, image_instance.instance.height);
|
|
||||||
if image_width == 0 || image_height == 0 {
|
|
||||||
for image_instance in empty_image((), bounds, Color::TRANSPARENT).instance_iter() {
|
|
||||||
result_table.push(image_instance);
|
|
||||||
}
|
}
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
|
let image_data = &image_instance.instance.data;
|
||||||
let layer_to_image_space = DAffine2::from_scale(orig_image_scale) * image_instance.transform.inverse();
|
let (image_width, image_height) = (image_instance.instance.width, image_instance.instance.height);
|
||||||
let bounds_in_image_space = Bbox::unit().affine_transform(layer_to_image_space * bounds).to_axis_aligned_bbox();
|
if image_width == 0 || image_height == 0 {
|
||||||
|
return empty_image((), bounds, Color::TRANSPARENT).instance_iter().next().unwrap();
|
||||||
|
}
|
||||||
|
|
||||||
let new_start = bounds_in_image_space.start.floor().min(DVec2::ZERO);
|
let orig_image_scale = DVec2::new(image_width as f64, image_height as f64);
|
||||||
let new_end = bounds_in_image_space.end.ceil().max(orig_image_scale);
|
let layer_to_image_space = DAffine2::from_scale(orig_image_scale) * image_instance.transform.inverse();
|
||||||
let new_scale = new_end - new_start;
|
let bounds_in_image_space = Bbox::unit().affine_transform(layer_to_image_space * bounds).to_axis_aligned_bbox();
|
||||||
|
|
||||||
// Copy over original image into enlarged image.
|
let new_start = bounds_in_image_space.start.floor().min(DVec2::ZERO);
|
||||||
let mut new_image = Image::new(new_scale.x as u32, new_scale.y as u32, Color::TRANSPARENT);
|
let new_end = bounds_in_image_space.end.ceil().max(orig_image_scale);
|
||||||
let offset_in_new_image = (-new_start).as_uvec2();
|
let new_scale = new_end - new_start;
|
||||||
for y in 0..image_height {
|
|
||||||
let old_start = y * image_width;
|
|
||||||
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 new_row = &mut new_image.data[new_start as usize..(new_start + image_width) as usize];
|
|
||||||
new_row.copy_from_slice(old_row);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Compute new transform.
|
// Copy over original image into enlarged image.
|
||||||
// let layer_to_new_texture_space = (DAffine2::from_scale(1. / new_scale) * DAffine2::from_translation(new_start) * layer_to_image_space).inverse();
|
let mut new_image = Image::new(new_scale.x as u32, new_scale.y as u32, Color::TRANSPARENT);
|
||||||
let new_texture_to_layer_space = image_instance.transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale);
|
let offset_in_new_image = (-new_start).as_uvec2();
|
||||||
|
for y in 0..image_height {
|
||||||
|
let old_start = y * image_width;
|
||||||
|
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 new_row = &mut new_image.data[new_start as usize..(new_start + image_width) as usize];
|
||||||
|
new_row.copy_from_slice(old_row);
|
||||||
|
}
|
||||||
|
|
||||||
image_instance.instance = Raster::new_cpu(new_image);
|
// Compute new transform.
|
||||||
image_instance.transform = new_texture_to_layer_space;
|
// let layer_to_new_texture_space = (DAffine2::from_scale(1. / new_scale) * DAffine2::from_translation(new_start) * layer_to_image_space).inverse();
|
||||||
image_instance.source_node_id = None;
|
let new_texture_to_layer_space = image_instance.transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale);
|
||||||
result_table.push(image_instance);
|
|
||||||
}
|
|
||||||
|
|
||||||
result_table
|
image_instance.instance = Raster::new_cpu(new_image);
|
||||||
|
image_instance.transform = new_texture_to_layer_space;
|
||||||
|
image_instance.source_node_id = None;
|
||||||
|
image_instance
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category("Debug: Raster"))]
|
#[node_macro::node(category("Debug: Raster"))]
|
||||||
|
@ -392,14 +386,11 @@ pub fn noise_pattern(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut result = RasterDataTable::default();
|
return RasterDataTable::new_instance(Instance {
|
||||||
result.push(Instance {
|
|
||||||
instance: Raster::new_cpu(image),
|
instance: Raster::new_cpu(image),
|
||||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
});
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
noise.set_noise_type(Some(noise_type));
|
noise.set_noise_type(Some(noise_type));
|
||||||
|
@ -457,14 +448,11 @@ pub fn noise_pattern(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut result = RasterDataTable::default();
|
RasterDataTable::new_instance(Instance {
|
||||||
result.push(Instance {
|
|
||||||
instance: Raster::new_cpu(image),
|
instance: Raster::new_cpu(image),
|
||||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
})
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category("Raster: Pattern"))]
|
#[node_macro::node(category("Raster: Pattern"))]
|
||||||
|
@ -502,20 +490,16 @@ pub fn mandelbrot(ctx: impl ExtractFootprint + Send) -> RasterDataTable<CPU> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let image = Image {
|
RasterDataTable::new_instance(Instance {
|
||||||
width,
|
instance: Raster::new_cpu(Image {
|
||||||
height,
|
width,
|
||||||
data,
|
height,
|
||||||
..Default::default()
|
data,
|
||||||
};
|
..Default::default()
|
||||||
let mut result = RasterDataTable::default();
|
}),
|
||||||
result.push(Instance {
|
|
||||||
instance: Raster::new_cpu(image),
|
|
||||||
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
transform: DAffine2::from_translation(offset) * DAffine2::from_scale(size),
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
})
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline(always)]
|
#[inline(always)]
|
||||||
|
|
|
@ -292,15 +292,12 @@ where
|
||||||
|
|
||||||
let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap();
|
let rasterized = context.get_image_data(0., 0., resolution.x as f64, resolution.y as f64).unwrap();
|
||||||
|
|
||||||
let mut result = RasterDataTable::default();
|
|
||||||
let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32);
|
let image = Image::from_image_data(&rasterized.data().0, resolution.x as u32, resolution.y as u32);
|
||||||
result.push(Instance {
|
RasterDataTable::new_instance(Instance {
|
||||||
instance: Raster::new_cpu(image),
|
instance: Raster::new_cpu(image),
|
||||||
transform: footprint.transform,
|
transform: footprint.transform,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
});
|
})
|
||||||
|
|
||||||
result
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[node_macro::node(category(""))]
|
#[node_macro::node(category(""))]
|
||||||
|
|
|
@ -38,10 +38,10 @@ impl MaskType {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_to_defs(self, svg_defs: &mut String, uuid: u64, svg_string: String) {
|
fn write_to_defs(self, svg_defs: &mut String, uuid: u64, svg_string: String) {
|
||||||
let id = format!("mask-{}", uuid);
|
let id = format!("mask-{uuid}");
|
||||||
match self {
|
match self {
|
||||||
Self::Clip => write!(svg_defs, r##"<clipPath id="{id}">{}</clipPath>"##, svg_string).unwrap(),
|
Self::Clip => write!(svg_defs, r##"<clipPath id="{id}">{svg_string}</clipPath>"##).unwrap(),
|
||||||
Self::Mask => write!(svg_defs, r##"<mask id="{id}" mask-type="alpha">{}</mask>"##, svg_string).unwrap(),
|
Self::Mask => write!(svg_defs, r##"<mask id="{id}" mask-type="alpha">{svg_string}</mask>"##).unwrap(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -89,9 +89,9 @@ impl SvgRender {
|
||||||
.unwrap_or_default();
|
.unwrap_or_default();
|
||||||
|
|
||||||
let matrix = format_transform_matrix(transform);
|
let matrix = format_transform_matrix(transform);
|
||||||
let transform = if matrix.is_empty() { String::new() } else { format!(r#" transform="{}""#, matrix) };
|
let transform = if matrix.is_empty() { String::new() } else { format!(r#" transform="{matrix}""#) };
|
||||||
|
|
||||||
let svg_header = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" {}><defs>{defs}</defs><g{transform}>"#, view_box);
|
let svg_header = format!(r#"<svg xmlns="http://www.w3.org/2000/svg" {view_box}><defs>{defs}</defs><g{transform}>"#);
|
||||||
self.svg.insert(0, svg_header.into());
|
self.svg.insert(0, svg_header.into());
|
||||||
self.svg.push("</g></svg>".into());
|
self.svg.push("</g></svg>".into());
|
||||||
}
|
}
|
||||||
|
@ -267,7 +267,7 @@ impl GraphicElementRendered for GraphicGroupTable {
|
||||||
mask_state = None;
|
mask_state = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let id = format!("mask-{}", uuid);
|
let id = format!("mask-{uuid}");
|
||||||
let selector = format!("url(#{id})");
|
let selector = format!("url(#{id})");
|
||||||
|
|
||||||
attributes.push(mask_type.to_attribute(), selector);
|
attributes.push(mask_type.to_attribute(), selector);
|
||||||
|
@ -444,18 +444,18 @@ impl GraphicElementRendered for VectorDataTable {
|
||||||
let can_use_order = !instance.instance.style.fill().is_none() && mask_type == MaskType::Mask;
|
let can_use_order = !instance.instance.style.fill().is_none() && mask_type == MaskType::Mask;
|
||||||
if !can_use_order {
|
if !can_use_order {
|
||||||
let id = format!("alignment-{}", generate_uuid());
|
let id = format!("alignment-{}", generate_uuid());
|
||||||
let mut vector_row = VectorDataTable::default();
|
|
||||||
let mut fill_instance = instance.instance.clone();
|
|
||||||
|
|
||||||
|
let mut fill_instance = instance.instance.clone();
|
||||||
fill_instance.style.clear_stroke();
|
fill_instance.style.clear_stroke();
|
||||||
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
|
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
|
||||||
|
|
||||||
vector_row.push(Instance {
|
let vector_row = VectorDataTable::new_instance(Instance {
|
||||||
instance: fill_instance,
|
instance: fill_instance,
|
||||||
alpha_blending: *instance.alpha_blending,
|
alpha_blending: *instance.alpha_blending,
|
||||||
transform: *instance.transform,
|
transform: *instance.transform,
|
||||||
source_node_id: None,
|
source_node_id: None,
|
||||||
});
|
});
|
||||||
|
|
||||||
push_id = Some((id, mask_type, vector_row));
|
push_id = Some((id, mask_type, vector_row));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -477,7 +477,7 @@ impl GraphicElementRendered for VectorDataTable {
|
||||||
let (x, y) = quad.top_left().into();
|
let (x, y) = quad.top_left().into();
|
||||||
let (width, height) = (quad.bottom_right() - quad.top_left()).into();
|
let (width, height) = (quad.bottom_right() - quad.top_left()).into();
|
||||||
write!(defs, r##"{}"##, svg.svg_defs).unwrap();
|
write!(defs, r##"{}"##, svg.svg_defs).unwrap();
|
||||||
let rect = format!(r##"<rect x="{}" y="{}" width="{width}" height="{height}" fill="white" />"##, x, y);
|
let rect = format!(r##"<rect x="{x}" y="{y}" width="{width}" height="{height}" fill="white" />"##);
|
||||||
match mask_type {
|
match mask_type {
|
||||||
MaskType::Clip => write!(defs, r##"<clipPath id="{id}">{}</clipPath>"##, svg.svg.to_svg_string()).unwrap(),
|
MaskType::Clip => write!(defs, r##"<clipPath id="{id}">{}</clipPath>"##, svg.svg.to_svg_string()).unwrap(),
|
||||||
MaskType::Mask => write!(defs, r##"<mask id="{id}">{}{}</mask>"##, rect, svg.svg.to_svg_string()).unwrap(),
|
MaskType::Mask => write!(defs, r##"<mask id="{id}">{}{}</mask>"##, rect, svg.svg.to_svg_string()).unwrap(),
|
||||||
|
@ -564,13 +564,11 @@ impl GraphicElementRendered for VectorDataTable {
|
||||||
.stroke()
|
.stroke()
|
||||||
.is_some_and(|stroke| stroke.align == StrokeAlign::Outside && !instance.instance.style.fill().is_none());
|
.is_some_and(|stroke| stroke.align == StrokeAlign::Outside && !instance.instance.style.fill().is_none());
|
||||||
if can_draw_aligned_stroke && !reorder_for_outside {
|
if can_draw_aligned_stroke && !reorder_for_outside {
|
||||||
let mut vector_data = VectorDataTable::default();
|
|
||||||
|
|
||||||
let mut fill_instance = instance.instance.clone();
|
let mut fill_instance = instance.instance.clone();
|
||||||
fill_instance.style.clear_stroke();
|
fill_instance.style.clear_stroke();
|
||||||
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
|
fill_instance.style.set_fill(Fill::solid(Color::BLACK));
|
||||||
|
|
||||||
vector_data.push(Instance {
|
let vector_data = VectorDataTable::new_instance(Instance {
|
||||||
instance: fill_instance,
|
instance: fill_instance,
|
||||||
alpha_blending: *instance.alpha_blending,
|
alpha_blending: *instance.alpha_blending,
|
||||||
transform: *instance.transform,
|
transform: *instance.transform,
|
||||||
|
@ -639,7 +637,11 @@ impl GraphicElementRendered for VectorDataTable {
|
||||||
let bounds = instance.instance.nonzero_bounding_box();
|
let bounds = instance.instance.nonzero_bounding_box();
|
||||||
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
|
let bound_transform = DAffine2::from_scale_angle_translation(bounds[1] - bounds[0], 0., bounds[0]);
|
||||||
|
|
||||||
let inverse_parent_transform = (parent_transform.matrix2.determinant() != 0.).then(|| parent_transform.inverse()).unwrap_or_default();
|
let inverse_parent_transform = if parent_transform.matrix2.determinant() != 0. {
|
||||||
|
parent_transform.inverse()
|
||||||
|
} else {
|
||||||
|
Default::default()
|
||||||
|
};
|
||||||
let mod_points = inverse_parent_transform * multiplied_transform * bound_transform;
|
let mod_points = inverse_parent_transform * multiplied_transform * bound_transform;
|
||||||
|
|
||||||
let start = mod_points.transform_point2(gradient.start);
|
let start = mod_points.transform_point2(gradient.start);
|
||||||
|
@ -666,7 +668,11 @@ impl GraphicElementRendered for VectorDataTable {
|
||||||
});
|
});
|
||||||
// Vello does `element_transform * brush_transform` internally. We don't want element_transform to have any impact so we need to left multiply by the inverse.
|
// Vello does `element_transform * brush_transform` internally. We don't want element_transform to have any impact so we need to left multiply by the inverse.
|
||||||
// This makes the final internal brush transform equal to `parent_transform`, allowing you to stretch a gradient by transforming the parent folder.
|
// This makes the final internal brush transform equal to `parent_transform`, allowing you to stretch a gradient by transforming the parent folder.
|
||||||
let inverse_element_transform = (element_transform.matrix2.determinant() != 0.).then(|| element_transform.inverse()).unwrap_or_default();
|
let inverse_element_transform = if element_transform.matrix2.determinant() != 0. {
|
||||||
|
element_transform.inverse()
|
||||||
|
} else {
|
||||||
|
Default::default()
|
||||||
|
};
|
||||||
let brush_transform = kurbo::Affine::new((inverse_element_transform * parent_transform).to_cols_array());
|
let brush_transform = kurbo::Affine::new((inverse_element_transform * parent_transform).to_cols_array());
|
||||||
scene.fill(peniko::Fill::NonZero, kurbo::Affine::new(element_transform.to_cols_array()), &fill, Some(brush_transform), &path);
|
scene.fill(peniko::Fill::NonZero, kurbo::Affine::new(element_transform.to_cols_array()), &fill, Some(brush_transform), &path);
|
||||||
}
|
}
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
name = "node-macro"
|
name = "node-macro"
|
||||||
publish = false
|
publish = false
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
rust-version = "1.85"
|
rust-version = "1.88"
|
||||||
authors = ["Graphite Authors <contact@graphite.rs>"]
|
authors = ["Graphite Authors <contact@graphite.rs>"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
readme = "../../README.md"
|
readme = "../../README.md"
|
||||||
|
@ -26,4 +26,3 @@ proc-macro-error2 = "2"
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
graphene-core = { workspace = true }
|
graphene-core = { workspace = true }
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
name = "graphite-proc-macros"
|
name = "graphite-proc-macros"
|
||||||
publish = false
|
publish = false
|
||||||
version = "0.0.0"
|
version = "0.0.0"
|
||||||
rust-version = "1.85"
|
rust-version = "1.88"
|
||||||
authors = ["Graphite Authors <contact@graphite.rs>"]
|
authors = ["Graphite Authors <contact@graphite.rs>"]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
readme = "../README.md"
|
readme = "../README.md"
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue