Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion node-graph/libraries/graphic-types/src/adjustment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub enum Adjustment {
/// Percent.
opacity: f64,
},
Solarize,
}

impl Adjustment {
Expand All @@ -107,6 +108,7 @@ impl Adjustment {
Self::PhotoFilter { .. } => EffectKind::PhotoFilter,
Self::ColorLookup { .. } => EffectKind::ColorLookup,
Self::ColorOverlay { .. } => EffectKind::PaintOverlay,
Self::Solarize => EffectKind::Solarize,
}
}

Expand All @@ -123,6 +125,7 @@ impl Adjustment {
| Self::Posterize { .. }
| Self::Exposure { .. }
| Self::ColorBalance(_)
| Self::Solarize
)
}

Expand All @@ -134,7 +137,7 @@ impl Adjustment {
visitor.parameter("gamma", gamma, 2.2);
visitor.parameter("inverse", inverse, false);
}
Self::MakeOpaque | Self::Invert => {}
Self::MakeOpaque | Self::Invert | Self::Solarize => {}
Self::BrightnessContrast {
brightness,
contrast,
Expand Down
10 changes: 9 additions & 1 deletion node-graph/libraries/graphic-types/src/effect_layers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,15 @@ fn pieces(index: usize, operation: &EffectOperation) -> Vec<(EffectPiece, BlendM
}
EffectOperation::PaintOverlay { blend_mode, .. } => vec![piece(0, Within, None, *blend_mode)],
EffectOperation::Adjustment(Adjustment::ColorOverlay { color, blend_mode, .. }) => vec![piece(0, Within, Some(*color), *blend_mode)],
EffectOperation::Blur { .. } | EffectOperation::Mask { .. } | EffectOperation::Feather { .. } | EffectOperation::ConvolutionKernel { .. } | EffectOperation::Adjustment(_) => Vec::new(),
EffectOperation::Blur { .. }
| EffectOperation::Mask { .. }
| EffectOperation::Feather { .. }
| EffectOperation::ConvolutionKernel { .. }
| EffectOperation::Emboss { .. }
| EffectOperation::FindEdges
| EffectOperation::NeighborhoodMinimum { .. }
| EffectOperation::NeighborhoodMaximum { .. }
| EffectOperation::Adjustment(_) => Vec::new(),
};
pieces.into_iter().flatten().collect()
}
Expand Down
98 changes: 88 additions & 10 deletions node-graph/libraries/graphic-types/src/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ pub enum EffectKind {
Feather,
/// Sums each pixel's neighbors weighted by a kernel of numbers.
ConvolutionKernel,
/// Grays the content, showing its edges in relief as if lit from one side.
Emboss,
/// Outlines the content's edges in dark over white.
FindEdges,
/// Spreads each pixel's darkest neighboring colors.
NeighborhoodMinimum,
/// Spreads each pixel's lightest neighboring colors.
NeighborhoodMaximum,
// The adjustments, which change each color of the content on its own, as their nodes of the same names do
Desaturate,
GammaCorrection,
Expand All @@ -71,6 +79,7 @@ pub enum EffectKind {
ColorBalance,
PhotoFilter,
ColorLookup,
Solarize,
}

impl std::fmt::Display for EffectKind {
Expand All @@ -88,6 +97,10 @@ impl std::fmt::Display for EffectKind {
Self::Mask => "Mask",
Self::Feather => "Feather",
Self::ConvolutionKernel => "Convolution Kernel",
Self::Emboss => "Emboss",
Self::FindEdges => "Find Edges",
Self::NeighborhoodMinimum => "Minimum Filter",
Self::NeighborhoodMaximum => "Maximum Filter",
Self::Desaturate => "Desaturate",
Self::GammaCorrection => "Gamma Correction",
Self::MakeOpaque => "Make Opaque",
Expand All @@ -107,6 +120,7 @@ impl std::fmt::Display for EffectKind {
Self::ColorBalance => "Color Balance",
Self::PhotoFilter => "Photo Filter",
Self::ColorLookup => "Color Lookup",
Self::Solarize => "Solarize",
})
}
}
Expand Down Expand Up @@ -901,6 +915,23 @@ pub enum EffectOperation {
/// On a scale of 0 to 255.
offset: f64,
},
Emboss {
/// The direction the light comes from, in degrees counterclockwise from the right.
angle: f64,
/// How far apart the two samples are, the difference between them showing as relief.
height: f64,
/// Percent.
amount: f64,
},
FindEdges,
NeighborhoodMinimum {
/// How many pixels the square neighborhood reaches out from its center.
radius: f64,
},
NeighborhoodMaximum {
/// How many pixels the square neighborhood reaches out from its center.
radius: f64,
},
Adjustment(Adjustment),
}

Expand Down Expand Up @@ -938,6 +969,10 @@ impl EffectOperation {
Self::Mask { .. } => EffectKind::Mask,
Self::Feather { .. } => EffectKind::Feather,
Self::ConvolutionKernel { .. } => EffectKind::ConvolutionKernel,
Self::Emboss { .. } => EffectKind::Emboss,
Self::FindEdges => EffectKind::FindEdges,
Self::NeighborhoodMinimum { .. } => EffectKind::NeighborhoodMinimum,
Self::NeighborhoodMaximum { .. } => EffectKind::NeighborhoodMaximum,
Self::Adjustment(adjustment) => adjustment.kind(),
}
}
Expand All @@ -951,6 +986,7 @@ impl EffectOperation {
let convolution = Convolution::custom(&weights, *columns as usize, *scale, *offset);
(!weights.is_empty() && !convolution.is_identity()).then_some(convolution)
}
Self::Emboss { angle, height, amount } => Some(Convolution::emboss(*angle, *height, *amount)),
_ => None,
}
}
Expand All @@ -975,7 +1011,8 @@ impl EffectOperation {
Self::Mask { density, .. } => *density <= 0.,
Self::Feather { distance } => *distance <= 0.,
Self::ConvolutionKernel { .. } => self.convolution().is_none(),
Self::Adjustment(_) => false,
Self::Emboss { .. } | Self::FindEdges | Self::Adjustment(_) => false,
Self::NeighborhoodMinimum { radius } | Self::NeighborhoodMaximum { radius } => *radius <= 0.,
}
}

Expand All @@ -988,7 +1025,7 @@ impl EffectOperation {
pub fn reads_pixels_around(&self) -> bool {
match self {
Self::Blur { .. } => true,
Self::ConvolutionKernel { .. } => true,
Self::ConvolutionKernel { .. } | Self::Emboss { .. } | Self::FindEdges | Self::NeighborhoodMinimum { .. } | Self::NeighborhoodMaximum { .. } => true,
// Cast from the content's shape, reading a mask's own image or a feather's map, or changing each pixel alone
Self::DropShadow { .. }
| Self::InnerShadow { .. }
Expand All @@ -1002,13 +1039,13 @@ impl EffectOperation {
}
}

/// How far past the content this operation paints, which is nowhere for an operation that changes nothing, a mask, a feather, or a convolution kernel, which keeps the content's coverage.
/// How far past the content this operation paints, which is nowhere for an operation that changes nothing, a mask, a feather, or a filter that keeps or shrinks the content's coverage.
pub fn painted_reach(&self) -> f64 {
if self.changes_nothing() || matches!(self, Self::Mask { .. } | Self::Feather { .. } | Self::ConvolutionKernel { .. }) {
0.
} else {
self.reach()
}
let keeps_coverage = matches!(
self,
Self::Mask { .. } | Self::Feather { .. } | Self::ConvolutionKernel { .. } | Self::Emboss { .. } | Self::FindEdges | Self::NeighborhoodMaximum { .. }
);
if self.changes_nothing() || keeps_coverage { 0. } else { self.reach() }
}

/// How far past the content this operation paints or reads from, which the region it's worked out over covers, and nowhere for an operation that changes nothing.
Expand Down Expand Up @@ -1039,7 +1076,10 @@ impl EffectOperation {
Self::Mask { feather, .. } => 3. * mask_feather_sigma(*feather),
// A feather paints nothing past the content, but finds edges up to this far past it, which a render cut short there would miss
Self::Feather { distance } => distance.max(0.) + 1.,
Self::ConvolutionKernel { .. } => self.convolution().map_or(0., |convolution| convolution.read_reach()),
Self::ConvolutionKernel { .. } | Self::Emboss { .. } => self.convolution().map_or(0., |convolution| convolution.read_reach()),
// The Sobel taps' diagonal, which a rotation can turn along an axis, plus two pixels for rounding and copies between pixels
Self::FindEdges => std::f64::consts::SQRT_2 + 2.,
Self::NeighborhoodMinimum { radius } | Self::NeighborhoodMaximum { radius } => radius.max(0.).ceil() + 1.,
Self::PaintOverlay { .. } | Self::Adjustment(_) => 0.,
}
}
Expand Down Expand Up @@ -1119,7 +1159,9 @@ impl EffectOperation {
} => [depth, size, soften, angle, altitude, slope_range, texture_depth].into_iter().for_each(finite),
Self::Mask { density, feather, .. } => [density, feather].into_iter().for_each(finite),
Self::Feather { distance } => finite(distance),
Self::PaintOverlay { .. } | Self::ConvolutionKernel { .. } | Self::Adjustment(_) => {}
Self::Emboss { angle, height, amount } => [angle, height, amount].into_iter().for_each(finite),
Self::NeighborhoodMinimum { radius } | Self::NeighborhoodMaximum { radius } => finite(radius),
Self::PaintOverlay { .. } | Self::ConvolutionKernel { .. } | Self::FindEdges | Self::Adjustment(_) => {}
}
self
}
Expand Down Expand Up @@ -1227,6 +1269,10 @@ impl EffectOperation {
scale: 0.,
offset: 0.,
},
EffectKind::Emboss => Self::Emboss { angle: 0., height: 0., amount: 0. },
EffectKind::FindEdges => Self::FindEdges,
EffectKind::NeighborhoodMinimum => Self::NeighborhoodMinimum { radius: 0. },
EffectKind::NeighborhoodMaximum => Self::NeighborhoodMaximum { radius: 0. },
EffectKind::Desaturate => Self::Adjustment(Adjustment::Desaturate { method: Default::default() }),
EffectKind::GammaCorrection => Self::Adjustment(Adjustment::GammaCorrection { gamma: 0., inverse: false }),
EffectKind::MakeOpaque => Self::Adjustment(Adjustment::MakeOpaque),
Expand Down Expand Up @@ -1262,6 +1308,7 @@ impl EffectOperation {
preserve_luminosity: false,
}),
EffectKind::ColorLookup => Self::Adjustment(Adjustment::ColorLookup { lut_file: Default::default() }),
EffectKind::Solarize => Self::Adjustment(Adjustment::Solarize),
}
}

Expand Down Expand Up @@ -1440,6 +1487,14 @@ impl EffectOperation {
visitor.parameter("scale", scale, 1.);
visitor.parameter("offset", offset, 0.);
}
// For PSD interop, the defaults are those of the filters' dialogs
Self::Emboss { angle, height, amount } => {
visitor.parameter("angle", angle, 135.);
visitor.parameter("height", height, 3.);
visitor.parameter("amount", amount, 100.);
}
Self::FindEdges => {}
Self::NeighborhoodMinimum { radius } | Self::NeighborhoodMaximum { radius } => visitor.parameter("radius", radius, 1.),
Self::Adjustment(adjustment) => adjustment.visit_parameters(visitor),
}
}
Expand Down Expand Up @@ -1565,6 +1620,15 @@ mod tests {
opacity: 100.,
}),
EffectOperation::convolution_kernel("-1 0 1\n-2 0 2\n-1 0 1", Some(4.), 128.),
EffectOperation::Emboss {
angle: -30.,
height: 4.5,
amount: 250.,
},
EffectOperation::FindEdges,
EffectOperation::NeighborhoodMinimum { radius: 2.5 },
EffectOperation::NeighborhoodMaximum { radius: 7. },
EffectOperation::Adjustment(Adjustment::Solarize),
];

for operation in operations {
Expand Down Expand Up @@ -1809,6 +1873,7 @@ mod tests {
ColorBalance,
PhotoFilter,
ColorLookup,
Solarize,
PaintOverlay,
];
for kind in kinds {
Expand Down Expand Up @@ -1891,6 +1956,19 @@ mod tests {
// Its farthest copy a pixel away, a faint pixel's nearest visible color within the square around that, turned any way, and two pixels for rounding and copies between pixels
let reach = 1. + std::f64::consts::SQRT_2 + 2.;
assert_eq!((sharpen.working_reach(), sharpen.required_reach()), (reach, reach));

// Emboss, Find Edges, and Maximum keep or shrink the coverage too, while a Minimum grows it
let keeping = [
EffectOperation::Emboss {
angle: 135.,
height: 3.,
amount: 100.,
},
EffectOperation::FindEdges,
EffectOperation::NeighborhoodMaximum { radius: 3. },
];
assert!(keeping.iter().all(|operation| operation.painted_reach() == 0. && operation.working_reach() > 0.));
assert_eq!(EffectOperation::NeighborhoodMinimum { radius: 3. }.painted_reach(), 4.);
assert_eq!(Effects::from_operations([sharpen]).read_reach(DAffine2::IDENTITY), reach);
}

Expand Down
Loading
Loading