Skip to content

Commit ff600c4

Browse files
committed
raster-nodes: make it no_std with std feature
1 parent f1cdbf0 commit ff600c4

7 files changed

Lines changed: 159 additions & 103 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

node-graph/graster-nodes/Cargo.toml

Lines changed: 30 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,44 @@ authors = ["Graphite Authors <contact@graphite.rs>"]
77
license = "MIT OR Apache-2.0"
88

99
[features]
10-
default = ["serde"]
11-
serde = ["dep:serde"]
10+
default = ["std"]
11+
std = [
12+
"dep:graphene-core",
13+
"dep:dyn-any",
14+
"dep:image",
15+
"dep:ndarray",
16+
"dep:bezier-rs",
17+
"dep:rand",
18+
"dep:rand_chacha",
19+
"dep:fastnoise-lite",
20+
"dep:serde",
21+
"dep:specta",
22+
"dep:glam"
23+
]
1224

1325
[dependencies]
1426
# Local dependencies
15-
dyn-any = { workspace = true }
16-
graphene-core = { workspace = true }
27+
graphene-core-shaders = { workspace = true }
1728
node-macro = { workspace = true }
1829

30+
# Local std dependencies
31+
dyn-any = { workspace = true, optional = true }
32+
graphene-core = { workspace = true, optional = true }
33+
1934
# Workspace dependencies
20-
glam = { workspace = true }
21-
specta = { workspace = true }
22-
image = { workspace = true }
2335
bytemuck = { workspace = true }
24-
ndarray = { workspace = true }
25-
bezier-rs = { workspace = true }
26-
rand = { workspace = true }
27-
rand_chacha = { workspace = true }
28-
fastnoise-lite = { workspace = true }
36+
# glam is reexported from gcore-shaders in no_std mode
37+
glam = { workspace = true, optional = true }
2938

30-
# Optional workspace dependencies
31-
serde = { workspace = true, optional = true, features = ["derive"] }
39+
# Workspace std dependencies
40+
specta = { workspace = true, optional = true }
41+
image = { workspace = true, optional = true }
42+
ndarray = { workspace = true, optional = true }
43+
bezier-rs = { workspace = true, optional = true }
44+
rand = { workspace = true, optional = true }
45+
rand_chacha = { workspace = true, optional = true }
46+
fastnoise-lite = { workspace = true, optional = true }
47+
serde = { workspace = true, optional = true }
3248

3349
[dev-dependencies]
3450
tokio = { workspace = true }

node-graph/graster-nodes/src/adjust.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
use graphene_core::Color;
2-
use graphene_core::gradient::GradientStops;
3-
use graphene_core::raster_types::{CPU, RasterDataTable};
1+
use graphene_core_shaders::color::Color;
42

53
pub trait Adjust<P> {
64
fn adjust(&mut self, map_fn: impl Fn(&P) -> P);
@@ -17,18 +15,25 @@ impl Adjust<Color> for Option<Color> {
1715
}
1816
}
1917
}
20-
impl Adjust<Color> for GradientStops {
21-
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
22-
for (_pos, c) in self.iter_mut() {
23-
*c = map_fn(c);
18+
19+
#[cfg(feature = "std")]
20+
mod adjust_std {
21+
use super::*;
22+
use graphene_core::gradient::GradientStops;
23+
use graphene_core::raster_types::{CPU, RasterDataTable};
24+
impl Adjust<Color> for GradientStops {
25+
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
26+
for (_pos, c) in self.iter_mut() {
27+
*c = map_fn(c);
28+
}
2429
}
2530
}
26-
}
27-
impl Adjust<Color> for RasterDataTable<CPU> {
28-
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
29-
for instance in self.instance_mut_iter() {
30-
for c in instance.instance.data_mut().data.iter_mut() {
31-
*c = map_fn(c);
31+
impl Adjust<Color> for RasterDataTable<CPU> {
32+
fn adjust(&mut self, map_fn: impl Fn(&Color) -> Color) {
33+
for instance in self.instance_mut_iter() {
34+
for c in instance.instance.data_mut().data.iter_mut() {
35+
*c = map_fn(c);
36+
}
3237
}
3338
}
3439
}

node-graph/graster-nodes/src/adjustments.rs

Lines changed: 45 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
use crate::adjust::Adjust;
44
use crate::cubic_spline::CubicSplines;
5-
use dyn_any::DynAny;
6-
use graphene_core::color::Color;
7-
use graphene_core::context::Ctx;
5+
use core::fmt::Debug;
6+
#[cfg(feature = "std")]
87
use graphene_core::gradient::GradientStops;
8+
#[cfg(feature = "std")]
99
use graphene_core::raster_types::{CPU, RasterDataTable};
10-
use graphene_core::registry::types::{Angle, Percentage, SignedPercentage};
11-
use std::fmt::Debug;
10+
use graphene_core_shaders::color::Color;
11+
use graphene_core_shaders::context::Ctx;
12+
use graphene_core_shaders::registry::types::{Angle, Percentage, SignedPercentage};
1213

1314
// TODO: Implement the following:
1415
// Color Balance
@@ -25,7 +26,8 @@ use std::fmt::Debug;
2526
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27clrL%27%20%3D%20Color%20Lookup
2627
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Color%20Lookup%20(Photoshop%20CS6
2728

28-
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, DynAny, Hash, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
29+
#[derive(Debug, Default, Clone, Copy, Eq, PartialEq, Hash, node_macro::ChoiceType)]
30+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
2931
#[widget(Dropdown)]
3032
pub enum LuminanceCalculation {
3133
#[default]
@@ -37,7 +39,7 @@ pub enum LuminanceCalculation {
3739
MaximumChannels,
3840
}
3941

40-
#[node_macro::node(category("Raster: Adjustment"))]
42+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
4143
fn luminance<T: Adjust<Color>>(
4244
_: impl Ctx,
4345
#[implementations(
@@ -61,7 +63,7 @@ fn luminance<T: Adjust<Color>>(
6163
input
6264
}
6365

64-
#[node_macro::node(category("Raster"))]
66+
#[node_macro::node(category("Raster"), shader_node(PerPixelAdjust))]
6567
fn gamma_correction<T: Adjust<Color>>(
6668
_: impl Ctx,
6769
#[implementations(
@@ -81,7 +83,7 @@ fn gamma_correction<T: Adjust<Color>>(
8183
input
8284
}
8385

84-
#[node_macro::node(category("Raster: Channels"))]
86+
#[node_macro::node(category("Raster: Channels"), shader_node(PerPixelAdjust))]
8587
fn extract_channel<T: Adjust<Color>>(
8688
_: impl Ctx,
8789
#[implementations(
@@ -104,7 +106,7 @@ fn extract_channel<T: Adjust<Color>>(
104106
input
105107
}
106108

107-
#[node_macro::node(category("Raster: Channels"))]
109+
#[node_macro::node(category("Raster: Channels"), shader_node(PerPixelAdjust))]
108110
fn make_opaque<T: Adjust<Color>>(
109111
_: impl Ctx,
110112
#[implementations(
@@ -129,7 +131,7 @@ fn make_opaque<T: Adjust<Color>>(
129131
//
130132
// Some further analysis available at:
131133
// https://geraldbakker.nl/psnumbers/brightness-contrast.html
132-
#[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"))]
134+
#[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"), shader_node(PerPixelAdjust))]
133135
fn brightness_contrast<T: Adjust<Color>>(
134136
_: impl Ctx,
135137
#[implementations(
@@ -146,7 +148,7 @@ fn brightness_contrast<T: Adjust<Color>>(
146148
let brightness = brightness as f32 / 255.;
147149

148150
let contrast = contrast as f32 / 100.;
149-
let contrast = if contrast > 0. { (contrast * std::f32::consts::FRAC_PI_2 - 0.01).tan() } else { contrast };
151+
let contrast = if contrast > 0. { (contrast * core::f32::consts::FRAC_PI_2 - 0.01).tan() } else { contrast };
150152

151153
let offset = brightness * contrast + brightness - contrast / 2.;
152154

@@ -168,13 +170,13 @@ fn brightness_contrast<T: Adjust<Color>>(
168170
y: [0., 130. + brightness * 51., 233. + brightness * 10., 255.].map(|x| x / 255.),
169171
};
170172
let brightness_curve_solutions = brightness_curve_points.solve();
171-
let mut brightness_lut: [f32; WINDOW_SIZE] = std::array::from_fn(|i| {
173+
let mut brightness_lut: [f32; WINDOW_SIZE] = core::array::from_fn(|i| {
172174
let x = i as f32 / (WINDOW_SIZE as f32 - 1.);
173175
brightness_curve_points.interpolate(x, &brightness_curve_solutions)
174176
});
175177
// Special handling for when brightness is negative
176178
if brightness_is_negative {
177-
brightness_lut = std::array::from_fn(|i| {
179+
brightness_lut = core::array::from_fn(|i| {
178180
let mut x = i;
179181
while x > 1 && brightness_lut[x] > i as f32 / WINDOW_SIZE as f32 {
180182
x -= 1;
@@ -193,7 +195,7 @@ fn brightness_contrast<T: Adjust<Color>>(
193195
y: [0., 64. - contrast * 30., 192. + contrast * 30., 255.].map(|x| x / 255.),
194196
};
195197
let contrast_curve_solutions = contrast_curve_points.solve();
196-
let contrast_lut: [f32; WINDOW_SIZE] = std::array::from_fn(|i| {
198+
let contrast_lut: [f32; WINDOW_SIZE] = core::array::from_fn(|i| {
197199
let x = i as f32 / (WINDOW_SIZE as f32 - 1.);
198200
contrast_curve_points.interpolate(x, &contrast_curve_solutions)
199201
});
@@ -218,7 +220,7 @@ fn brightness_contrast<T: Adjust<Color>>(
218220
//
219221
// Some further analysis available at:
220222
// https://geraldbakker.nl/psnumbers/levels.html
221-
#[node_macro::node(category("Raster: Adjustment"))]
223+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
222224
fn levels<T: Adjust<Color>>(
223225
_: impl Ctx,
224226
#[implementations(
@@ -285,7 +287,7 @@ fn levels<T: Adjust<Color>>(
285287
// Algorithm from:
286288
// https://stackoverflow.com/a/55233732/775283
287289
// Works the same for gamma and linear color
288-
#[node_macro::node(name("Black & White"), category("Raster: Adjustment"))]
290+
#[node_macro::node(name("Black & White"), category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
289291
async fn black_and_white<T: Adjust<Color>>(
290292
_: impl Ctx,
291293
#[implementations(
@@ -357,7 +359,7 @@ async fn black_and_white<T: Adjust<Color>>(
357359
// Aims for interoperable compatibility with:
358360
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27hue%20%27%20%3D%20Old,saturation%2C%20Photoshop%205.0
359361
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=0%20%3D%20Use%20other.-,Hue/Saturation,-Hue/Saturation%20settings
360-
#[node_macro::node(name("Hue/Saturation"), category("Raster: Adjustment"))]
362+
#[node_macro::node(name("Hue/Saturation"), category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
361363
async fn hue_saturation<T: Adjust<Color>>(
362364
_: impl Ctx,
363365
#[implementations(
@@ -391,7 +393,7 @@ async fn hue_saturation<T: Adjust<Color>>(
391393

392394
// Aims for interoperable compatibility with:
393395
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27%20%3D%20Color%20Lookup-,%27nvrt%27%20%3D%20Invert,-%27post%27%20%3D%20Posterize
394-
#[node_macro::node(category("Raster: Adjustment"))]
396+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
395397
async fn invert<T: Adjust<Color>>(
396398
_: impl Ctx,
397399
#[implementations(
@@ -413,7 +415,7 @@ async fn invert<T: Adjust<Color>>(
413415

414416
// Aims for interoperable compatibility with:
415417
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=post%27%20%3D%20Posterize-,%27thrs%27%20%3D%20Threshold,-%27grdm%27%20%3D%20Gradient
416-
#[node_macro::node(category("Raster: Adjustment"))]
418+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
417419
async fn threshold<T: Adjust<Color>>(
418420
_: impl Ctx,
419421
#[implementations(
@@ -458,7 +460,7 @@ async fn threshold<T: Adjust<Color>>(
458460
// It's not the same as the saturation component of Hue/Saturation/Value. Vibrance and Saturation are both separable.
459461
// When both parameters are set, it is equivalent to running this adjustment twice, with only vibrance set and then only saturation set.
460462
// (Except for some noise probably due to rounding error.)
461-
#[node_macro::node(category("Raster: Adjustment"))]
463+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
462464
async fn vibrance<T: Adjust<Color>>(
463465
_: impl Ctx,
464466
#[implementations(
@@ -520,7 +522,8 @@ async fn vibrance<T: Adjust<Color>>(
520522
}
521523

522524
/// Color Channel
523-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
525+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
526+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
524527
#[widget(Radio)]
525528
pub enum RedGreenBlue {
526529
#[default]
@@ -530,7 +533,8 @@ pub enum RedGreenBlue {
530533
}
531534

532535
/// Color Channel
533-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
536+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
537+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
534538
#[widget(Radio)]
535539
pub enum RedGreenBlueAlpha {
536540
#[default]
@@ -541,7 +545,8 @@ pub enum RedGreenBlueAlpha {
541545
}
542546

543547
/// Style of noise pattern
544-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
548+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
549+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
545550
#[widget(Dropdown)]
546551
pub enum NoiseType {
547552
#[default]
@@ -556,7 +561,8 @@ pub enum NoiseType {
556561
WhiteNoise,
557562
}
558563

559-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
564+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
565+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
560566
/// Style of layered levels of the noise pattern
561567
pub enum FractalType {
562568
#[default]
@@ -572,7 +578,8 @@ pub enum FractalType {
572578
}
573579

574580
/// Distance function used by the cellular noise
575-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
581+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
582+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
576583
pub enum CellularDistanceFunction {
577584
#[default]
578585
Euclidean,
@@ -582,7 +589,8 @@ pub enum CellularDistanceFunction {
582589
Hybrid,
583590
}
584591

585-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
592+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
593+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
586594
pub enum CellularReturnType {
587595
CellValue,
588596
#[default]
@@ -601,7 +609,8 @@ pub enum CellularReturnType {
601609
}
602610

603611
/// Type of domain warp
604-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
612+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
613+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
605614
#[widget(Dropdown)]
606615
pub enum DomainWarpType {
607616
#[default]
@@ -616,7 +625,7 @@ pub enum DomainWarpType {
616625
// Aims for interoperable compatibility with:
617626
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27mixr%27%20%3D%20Channel%20Mixer
618627
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Lab%20color%20only-,Channel%20Mixer,-Key%20is%20%27mixr
619-
#[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"))]
628+
#[node_macro::node(category("Raster: Adjustment"), properties("channel_mixer_properties"), shader_node(PerPixelAdjust))]
620629
async fn channel_mixer<T: Adjust<Color>>(
621630
_: impl Ctx,
622631
#[implementations(
@@ -711,7 +720,8 @@ async fn channel_mixer<T: Adjust<Color>>(
711720
image
712721
}
713722

714-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
723+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
724+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
715725
#[widget(Radio)]
716726
pub enum RelativeAbsolute {
717727
#[default]
@@ -720,7 +730,8 @@ pub enum RelativeAbsolute {
720730
}
721731

722732
#[repr(C)]
723-
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, DynAny, node_macro::ChoiceType, specta::Type, serde::Serialize, serde::Deserialize)]
733+
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, node_macro::ChoiceType)]
734+
#[cfg_attr(feature = "std", derive(dyn_any::DynAny, specta::Type, serde::Serialize, serde::Deserialize))]
724735
pub enum SelectiveColorChoice {
725736
#[default]
726737
Reds,
@@ -742,7 +753,7 @@ pub enum SelectiveColorChoice {
742753
//
743754
// Algorithm based on:
744755
// https://blog.pkh.me/p/22-understanding-selective-coloring-in-adobe-photoshop.html
745-
#[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"))]
756+
#[node_macro::node(category("Raster: Adjustment"), properties("selective_color_properties"), shader_node(PerPixelAdjust))]
746757
async fn selective_color<T: Adjust<Color>>(
747758
_: impl Ctx,
748759
#[implementations(
@@ -884,7 +895,7 @@ async fn selective_color<T: Adjust<Color>>(
884895
// Algorithm based on:
885896
// https://www.axiomx.com/posterize.htm
886897
// This algorithm produces fully accurate output in relation to the industry standard.
887-
#[node_macro::node(category("Raster: Adjustment"))]
898+
#[node_macro::node(category("Raster: Adjustment"), shader_node(PerPixelAdjust))]
888899
async fn posterize<T: Adjust<Color>>(
889900
_: impl Ctx,
890901
#[implementations(
@@ -917,7 +928,7 @@ async fn posterize<T: Adjust<Color>>(
917928
//
918929
// Algorithm based on:
919930
// https://geraldbakker.nl/psnumbers/exposure.html
920-
#[node_macro::node(category("Raster: Adjustment"), properties("exposure_properties"))]
931+
#[node_macro::node(category("Raster: Adjustment"), properties("exposure_properties"), shader_node(PerPixelAdjust))]
921932
async fn exposure<T: Adjust<Color>>(
922933
_: impl Ctx,
923934
#[implementations(

0 commit comments

Comments
 (0)