mirror of
https://github.com/GraphiteEditor/Graphite.git
synced 2026-09-16 06:38:03 +08:00
Image adjustment nodes restructure (#1013)
* Add macro for creation of Map image nodes * Move nodes to adjustments module * Add Saturation and Lightness to hue shift node * Fix raster node macro * Add Threshold Node * Convert all adjustment nodes to new format * Start implementing vibrance node * Remove package-lock.json * Code review --------- Co-authored-by: isiko404 <isihd.ko@gmail.com> Co-authored-by: Keavon Chambers <keavon@keavon.com>
This commit is contained in:
committed by
Keavon Chambers
parent
202b0ee6ed
commit
8e3480e952
@@ -5,14 +5,8 @@ use crate::Node;
|
||||
pub mod color;
|
||||
pub use self::color::Color;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct GrayscaleColorNode;
|
||||
|
||||
#[node_macro::node_fn(GrayscaleColorNode)]
|
||||
fn grayscale_color_node(input: Color) -> Color {
|
||||
let avg = (input.r() + input.g() + input.b()) / 3.0;
|
||||
Color::from_rgbaf32_unchecked(avg, avg, avg, input.a())
|
||||
}
|
||||
pub mod adjustments;
|
||||
pub use adjustments::*;
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
pub struct MapNode<MapFn> {
|
||||
@@ -237,36 +231,6 @@ fn brighten_color_node(color: Color, brightness: f32) -> Color {
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GammaColorNode<Gamma> {
|
||||
gamma: Gamma,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(GammaColorNode)]
|
||||
fn gamma_color_node(color: Color, gamma: f32) -> Color {
|
||||
let per_channel = |col: f32| col.powf(gamma);
|
||||
Color::from_rgbaf32_unchecked(per_channel(color.r()), per_channel(color.g()), per_channel(color.b()), color.a())
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
pub use hue_shift::HueShiftColorNode;
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
mod hue_shift {
|
||||
use super::*;
|
||||
#[derive(Debug)]
|
||||
pub struct HueShiftColorNode<Angle> {
|
||||
angle: Angle,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(HueShiftColorNode)]
|
||||
fn hue_shift_color_node(color: Color, angle: f32) -> Color {
|
||||
let hue_shift = angle;
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
Color::from_hsla(hue + hue_shift / 360., saturation, lightness, alpha)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ForEachNode<Iter, MapNode> {
|
||||
map_node: MapNode,
|
||||
@@ -410,7 +374,7 @@ mod test {
|
||||
#[test]
|
||||
fn map_node() {
|
||||
// let array = &mut [Color::from_rgbaf32(1.0, 0.0, 0.0, 1.0).unwrap()];
|
||||
GrayscaleColorNode.eval(Color::from_rgbf32_unchecked(1., 0., 0.));
|
||||
GrayscaleNode.eval(Color::from_rgbf32_unchecked(1., 0., 0.));
|
||||
/*let map = ForEachNode(MutWrapper(GrayscaleNode));
|
||||
(&map).eval(array.iter_mut());
|
||||
assert_eq!(array[0], Color::from_rgbaf32(0.33333334, 0.33333334, 0.33333334, 1.0).unwrap());*/
|
||||
|
||||
150
node-graph/gcore/src/raster/adjustments.rs
Normal file
150
node-graph/gcore/src/raster/adjustments.rs
Normal file
@@ -0,0 +1,150 @@
|
||||
use super::Color;
|
||||
use crate::Node;
|
||||
|
||||
use core::fmt::Debug;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default)]
|
||||
pub struct GrayscaleNode;
|
||||
|
||||
#[node_macro::node_fn(GrayscaleNode)]
|
||||
fn grayscale_color_node(input: Color) -> Color {
|
||||
let avg = (input.r() + input.g() + input.b()) / 3.0;
|
||||
map_rgb(input, |_| avg)
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct GammaNode<Gamma> {
|
||||
gamma: Gamma,
|
||||
}
|
||||
|
||||
// https://www.dfstudios.co.uk/articles/programming/image-programming-algorithms/image-processing-algorithms-part-6-gamma-correction/
|
||||
#[node_macro::node_fn(GammaNode)]
|
||||
fn gamma_color_node(color: Color, gamma: f64) -> Color {
|
||||
let inverse_gamma = 1. / gamma;
|
||||
let per_channel = |channel: f32| channel.powf(inverse_gamma as f32);
|
||||
map_rgb(color, per_channel)
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
pub use hue_shift::HueSaturationNode;
|
||||
|
||||
// TODO: Make this work on GPU so it can be removed from the wrapper module that excludes GPU (it doesn't work because of the modulo)
|
||||
#[cfg(not(target_arch = "spirv"))]
|
||||
mod hue_shift {
|
||||
use super::*;
|
||||
#[derive(Debug)]
|
||||
pub struct HueSaturationNode<Hue, Saturation, Lightness> {
|
||||
hue_shift: Hue,
|
||||
saturation_shift: Saturation,
|
||||
lightness_shift: Lightness,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(HueSaturationNode)]
|
||||
fn hue_shift_color_node(color: Color, hue_shift: f64, saturation_shift: f64, lightness_shift: f64) -> Color {
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
Color::from_hsla(
|
||||
(hue + hue_shift as f32 / 360.) % 1.,
|
||||
(saturation + saturation_shift as f32 / 100.).clamp(0., 1.),
|
||||
(lightness + lightness_shift as f32 / 100.).clamp(0., 1.),
|
||||
alpha,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct InvertRGBNode;
|
||||
|
||||
#[node_macro::node_fn(InvertRGBNode)]
|
||||
fn invert_image(color: Color) -> Color {
|
||||
map_rgb(color, |c| 1. - c)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ThresholdNode<Threshold> {
|
||||
threshold: Threshold,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(ThresholdNode)]
|
||||
fn threshold_node(color: Color, threshold: f64) -> Color {
|
||||
let avg = (color.r() + color.g() + color.b()) / 3.0;
|
||||
if avg >= threshold as f32 {
|
||||
Color::WHITE
|
||||
} else {
|
||||
Color::BLACK
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct VibranceNode<Vibrance> {
|
||||
vibrance: Vibrance,
|
||||
}
|
||||
|
||||
// TODO: The current results are incorrect, try implementing this from https://stackoverflow.com/questions/33966121/what-is-the-algorithm-for-vibrance-filters
|
||||
#[node_macro::node_fn(VibranceNode)]
|
||||
fn vibrance_node(color: Color, vibrance: f64) -> Color {
|
||||
let [hue, saturation, lightness, alpha] = color.to_hsla();
|
||||
let vibrance = vibrance as f32 / 100.;
|
||||
let saturation = saturation + vibrance * (1. - saturation);
|
||||
Color::from_hsla(hue, saturation, lightness, alpha)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct BrightnessContrastNode<Brightness, Contrast> {
|
||||
brightness: Brightness,
|
||||
contrast: Contrast,
|
||||
}
|
||||
|
||||
// From https://stackoverflow.com/questions/2976274/adjust-bitmap-image-brightness-contrast-using-c
|
||||
#[node_macro::node_fn(BrightnessContrastNode)]
|
||||
fn adjust_image_brightness_and_contrast(color: Color, brightness: f64, contrast: f64) -> Color {
|
||||
let (brightness, contrast) = (brightness as f32, contrast as f32);
|
||||
let factor = (259. * (contrast + 255.)) / (255. * (259. - contrast));
|
||||
let channel = |channel: f32| ((factor * (channel * 255. + brightness - 128.) + 128.) / 255.).clamp(0., 1.);
|
||||
map_rgb(color, channel)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct OpacityNode<O> {
|
||||
opacity_multiplier: O,
|
||||
}
|
||||
|
||||
#[node_macro::node_fn(OpacityNode)]
|
||||
fn image_opacity(color: Color, opacity_multiplier: f64) -> Color {
|
||||
let opacity_multiplier = opacity_multiplier as f32;
|
||||
Color::from_rgbaf32_unchecked(color.r(), color.g(), color.b(), color.a() * opacity_multiplier)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct PosterizeNode<P> {
|
||||
posterize_value: P,
|
||||
}
|
||||
|
||||
// Based on http://www.axiomx.com/posterize.htm
|
||||
#[node_macro::node_fn(PosterizeNode)]
|
||||
fn posterize(color: Color, posterize_value: f64) -> Color {
|
||||
let posterize_value = posterize_value as f32;
|
||||
let number_of_areas = posterize_value.recip();
|
||||
let size_of_areas = (posterize_value - 1.).recip();
|
||||
let channel = |channel: f32| (channel / number_of_areas).floor() * size_of_areas;
|
||||
map_rgb(color, channel)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct ExposureNode<E> {
|
||||
exposure: E,
|
||||
}
|
||||
|
||||
// Based on https://stackoverflow.com/questions/12166117/what-is-the-math-behind-exposure-adjustment-on-photoshop
|
||||
#[node_macro::node_fn(ExposureNode)]
|
||||
fn exposure(color: Color, exposure: f64) -> Color {
|
||||
let multiplier = 2_f32.powf(exposure as f32);
|
||||
let channel = |channel: f32| channel * multiplier;
|
||||
map_rgb(color, channel)
|
||||
}
|
||||
|
||||
pub fn map_rgba<F: Fn(f32) -> f32>(color: Color, f: F) -> Color {
|
||||
Color::from_rgbaf32_unchecked(f(color.r()), f(color.g()), f(color.b()), f(color.a()))
|
||||
}
|
||||
pub fn map_rgb<F: Fn(f32) -> f32>(color: Color, f: F) -> Color {
|
||||
Color::from_rgbaf32_unchecked(f(color.r()), f(color.g()), f(color.b()), color.a())
|
||||
}
|
||||
Reference in New Issue
Block a user