diff --git a/document/migrations/nodes/src/raster_nodes/mod.rs b/document/migrations/nodes/src/raster_nodes/mod.rs index a75e50a945..e4d8c1e944 100644 --- a/document/migrations/nodes/src/raster_nodes/mod.rs +++ b/document/migrations/nodes/src/raster_nodes/mod.rs @@ -6,5 +6,6 @@ mod mandelbrot_v2; mod mix_colors_from_mix_node; mod noise_pattern_v1; mod noise_pattern_v2; +mod passthrough_from_extend_image_to_bounds; mod pattern_square; mod renames; diff --git a/document/migrations/nodes/src/raster_nodes/passthrough_from_extend_image_to_bounds.rs b/document/migrations/nodes/src/raster_nodes/passthrough_from_extend_image_to_bounds.rs new file mode 100644 index 0000000000..b9c3c8dee0 --- /dev/null +++ b/document/migrations/nodes/src/raster_nodes/passthrough_from_extend_image_to_bounds.rs @@ -0,0 +1,36 @@ +use migration_core::{Inputs, migration}; + +/// 'Extend Image to Bounds' is gone, and the transparent pixels it padded an image with drew nothing, so it passes its image on and drops its bounds. +#[migration("raster_nodes::std_nodes::ExtendImageToBoundsNode"@1 => "graphene_core::ops::PassthroughNode"@2)] +pub fn passthrough_from_extend_image_to_bounds(inputs: &mut Inputs) { + inputs.truncate(1); +} + +#[cfg(test)] +mod tests { + use super::*; + use migration_core::{decl, literal}; + use migration_runner::testing::{Fixture, SourceContract, check_commutation, check_undo}; + + fn contract() -> SourceContract { + SourceContract::new(2, |random, _slot| literal(random.below(10) as f64)) + } + + #[test] + fn rewrites_history() { + check_commutation(&PassthroughFromExtendImageToBounds, &contract()); + check_undo(&PassthroughFromExtendImageToBounds, &contract()); + } + + #[test] + fn keeps_the_image_and_drops_the_bounds() { + let (image, bounds) = (literal(1.), literal(2.)); + + let mut fixture = Fixture::new(&PassthroughFromExtendImageToBounds); + let extend = fixture.add(decl!("raster_nodes::std_nodes::ExtendImageToBoundsNode"@1), vec![image.clone(), bounds]); + let migrated = fixture.migrate(&PassthroughFromExtendImageToBounds).unwrap(); + + assert_eq!(fixture.declaration_of(&migrated, extend), Some(decl!("graphene_core::ops::PassthroughNode"@2))); + assert_eq!(fixture.inputs(&migrated, extend), [image]); + } +} diff --git a/editor/src/messages/portfolio/document_migration.rs b/editor/src/messages/portfolio/document_migration.rs index 8eb7a08e5e..aea4c33bf9 100644 --- a/editor/src/messages/portfolio/document_migration.rs +++ b/editor/src/messages/portfolio/document_migration.rs @@ -121,6 +121,10 @@ const NODE_REPLACEMENTS: &[NodeReplacement<'static>] = &[ "graphene_std::raster::SampleImageNode", "graphene_std::raster::SampleNode", "raster_nodes::std_nodes::SampleImageNode", + // The deleted Extend Image to Bounds passes its image through, since the transparent pixels it padded with drew nothing + "graphene_raster_nodes::std_nodes::ExtendImageToBoundsNode", + "graphene_std::raster::ExtendImageToBoundsNode", + "raster_nodes::std_nodes::ExtendImageToBoundsNode", ], }, NodeReplacement { @@ -569,10 +573,6 @@ const NODE_REPLACEMENTS: &[NodeReplacement<'static>] = &[ "graphene_core::raster::ExposureNode", ], }, - NodeReplacement { - node: graphene_std::raster_nodes::std_nodes::extend_image_to_bounds::IDENTIFIER, - aliases: &["graphene_raster_nodes::std_nodes::ExtendImageToBoundsNode", "graphene_std::raster::ExtendImageToBoundsNode"], - }, NodeReplacement { node: graphene_std::raster_nodes::adjustments::gamma_correction::IDENTIFIER, aliases: &["graphene_raster_nodes::adjustments::GammaCorrectionNode", "graphene_core::raster::adjustments::GammaCorrectionNode"], @@ -3252,9 +3252,17 @@ fn migrate_node(node_id: &NodeId, node: &DocumentNode, network_path: &[NodeId], document.network_interface.set_input(&InputConnector::node_at_index(*node_id, 1), old_inputs[1].clone(), network_path); } - // Sample Image nodes, now passthroughs, drop their old title + // Sample Image and Extend Image to Bounds nodes, now passthroughs, drop their old titles and any inputs past the first, like Extend Image to Bounds's bounds if reference == DefinitionIdentifier::ProtoNode(graphene_std::ops::passthrough::IDENTIFIER) { reset_default_display_name(document, node_id, network_path, "Sample Image"); + reset_default_display_name(document, node_id, network_path, "Extend Image to Bounds"); + + if inputs_count > 1 { + let mut node_template = resolve_document_node_type(&reference)?.default_node_template(); + document.network_interface.replace_implementation(node_id, network_path, &mut node_template); + let old_inputs = document.network_interface.replace_inputs(node_id, network_path, &mut node_template)?; + document.network_interface.set_input(&InputConnector::node_at_index(*node_id, 0), old_inputs[0].clone(), network_path); + } } // Serialize became As String, whose input converts a wired value to text, while a stored value keeps the JSON text Serialize made of it @@ -6323,6 +6331,35 @@ mod tests { } } + #[test] + fn extend_image_to_bounds_opens_as_a_passthrough_of_its_image() { + use crate::messages::portfolio::document_storage_io::load_legacy_document; + use glam::DAffine2; + use graph_craft::application_io::resource::HashMapResourceStorage; + + let (image, extend) = (NodeId(1), NodeId(2)); + let image_template = resolve_document_node_type(&DefinitionIdentifier::ProtoNode(graphene_std::raster_nodes::std_nodes::image::IDENTIFIER)) + .expect("the Image definition should exist") + .default_node_template(); + let extend_template = NodeTemplate { + implementation: NodeTemplateImplementation::ProtoNode(ProtoNodeIdentifier::new("raster_nodes::std_nodes::ExtendImageToBoundsNode")), + inputs: vec![NodeInput::value(TaggedValue::None, false), NodeInput::value(TaggedValue::DAffine2(DAffine2::IDENTITY), false)], + display_name: "Extend Image to Bounds".to_string(), + ..Default::default() + }; + + let mut document = DocumentMessageHandler::default(); + document.network_interface.insert_node(image, image_template, &[]); + document.network_interface.insert_node(extend, extend_template, &[]); + document.network_interface.set_input(&InputConnector::node_at_index(extend, 0), NodeInput::node(image, 0), &[]); + + let opened = load_legacy_document(document.serialize_document(), &HashMapResourceStorage::new(), false).expect("the document should open"); + let node = opened.network_interface.document_node(&extend, &[]).expect("the node should still exist"); + assert_eq!(node.implementation, DocumentNodeImplementation::ProtoNode(graphene_std::ops::passthrough::IDENTIFIER)); + assert!(matches!(node.inputs[..], [NodeInput::Node { node_id, output_index: 0, .. }] if node_id == image), "{:?}", node.inputs); + assert_ne!(opened.network_interface.display_name(&extend, &[]), "Extend Image to Bounds"); + } + #[test] fn make_opaques_from_before_their_strength_open_fully_opaque() { use crate::messages::portfolio::document_storage_io::load_legacy_document; diff --git a/node-graph/nodes/raster/src/std_nodes.rs b/node-graph/nodes/raster/src/std_nodes.rs index 6ed45b86dd..d436b40154 100644 --- a/node-graph/nodes/raster/src/std_nodes.rs +++ b/node-graph/nodes/raster/src/std_nodes.rs @@ -1,15 +1,10 @@ use crate::adjustments::{CellularDistanceFunction, CellularReturnType, DomainWarpType, FractalType, MandelbrotExterior, MandelbrotFractal, MandelbrotInterior, NoiseType}; -use core_types::ATTR_TRANSFORM; -use core_types::color::Color; use core_types::context::Ctx; use core_types::list::Item; -use core_types::math::bbox::Bbox; -use core_types::transform::Transform; use dyn_any::DynAny; -use glam::{DAffine2, DVec2}; +use glam::DVec2; use graphene_resource::Resource; use graphic_types::generator::{Generator, MandelbrotSettings, NoiseSettings}; -use raster_kernels::evaluate_raster_effects; use raster_types::{CPU, Raster}; use raster_types::{Image, ImageError}; use std::fmt::Debug; @@ -27,67 +22,6 @@ impl From for Error { } } -#[node_macro::node(category(""))] -fn extend_image_to_bounds(_: impl Ctx, image: Item>, bounds: Item) -> Result>, (Item>, ImageError)> { - let image = evaluate_raster_effects(image); - let bounds = *bounds.element(); - - let image_transform: DAffine2 = image.attribute_cloned_or_default(ATTR_TRANSFORM); - let image_aabb = Bbox::unit().affine_transform(image_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 Ok(image); - } - - let (image_width, image_height) = (image.element().width, image.element().height); - if image_width == 0 || image_height == 0 { - return empty_image(bounds, Color::TRANSPARENT).map_err(|error| (image, error)); - } - if image_transform.matrix2.determinant() == 0. { - return Ok(image); - } - - 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) * image_transform.inverse(); - 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_end = bounds_in_image_space.end.ceil().max(orig_image_scale); - let new_scale = new_end - new_start; - - // Copy over original image into enlarged image. - let mut new_image = match Image::try_new(new_scale.x as u32, new_scale.y as u32, Color::TRANSPARENT) { - Ok(new_image) => new_image, - Err(error) => return Err((image, error)), - }; - let (image, mut attributes) = image.into_parts(); - 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); - } - - // 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 new_texture_to_layer_space = image_transform * DAffine2::from_scale(1. / orig_image_scale) * DAffine2::from_translation(new_start) * DAffine2::from_scale(new_scale); - - attributes.insert(ATTR_TRANSFORM, new_texture_to_layer_space); - Ok(Item::from_parts(Raster::new_cpu(new_image), attributes)) -} - -/// Creates an image filled with one color, sized in pixels by the transform's scale and placed by the transform. -fn empty_image(transform: DAffine2, color: Color) -> Result>, ImageError> { - 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 image = Image::try_new(width, height, color)?; - - Ok(Item::new_from_element(Raster::new_cpu(image)).with_attribute(ATTR_TRANSFORM, transform)) -} - /// Displays an image from a file. /// /// Reads PNG, JPG, GIF, WEBP, TIFF, BMP, TGA, ICO, HDR, and EXR files. Light brighter than white, as HDR and EXR files can hold, is clipped to white.