Skip to content

Commit c3d1f9c

Browse files
committed
shaders: brightness_contrast_classic gpu node
1 parent 446bb2d commit c3d1f9c

File tree

1 file changed

+34
-11
lines changed

1 file changed

+34
-11
lines changed

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

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,38 @@ fn make_opaque<T: Adjust<Color>>(
141141
input
142142
}
143143

144+
/// See [`brightness_contrast`]
145+
#[node_macro::node(
146+
name("Brightness/Contrast classic"),
147+
category("Raster: Adjustment"),
148+
properties("brightness_contrast_properties"),
149+
shader_node(PerPixelAdjust)
150+
)]
151+
fn brightness_contrast_classic<T: Adjust<Color>>(
152+
_: impl Ctx,
153+
#[implementations(
154+
Table<Raster<CPU>>,
155+
Table<Color>,
156+
Table<GradientStops>,
157+
GradientStops,
158+
)]
159+
#[gpu_image]
160+
mut input: T,
161+
brightness: SignedPercentageF32,
162+
contrast: SignedPercentageF32,
163+
) -> T {
164+
let brightness = brightness / 255.;
165+
166+
let contrast = contrast / 100.;
167+
let contrast = if contrast > 0. { (contrast * core::f32::consts::FRAC_PI_2 - 0.01).tan() } else { contrast };
168+
169+
let offset = brightness * contrast + brightness - contrast / 2.;
170+
171+
input.adjust(|color| color.to_gamma_srgb().map_rgb(|c| (c + c * contrast + offset).clamp(0., 1.)).to_linear_srgb());
172+
173+
input
174+
}
175+
144176
// Aims for interoperable compatibility with:
145177
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=%27brit%27%20%3D%20Brightness/Contrast
146178
// https://www.adobe.com/devnet-apps/photoshop/fileformatashtml/#:~:text=Padding-,Brightness%20and%20Contrast,-Key%20is%20%27brit
@@ -149,7 +181,7 @@ fn make_opaque<T: Adjust<Color>>(
149181
// https://geraldbakker.nl/psnumbers/brightness-contrast.html
150182
#[node_macro::node(name("Brightness/Contrast"), category("Raster: Adjustment"), properties("brightness_contrast_properties"), cfg(feature = "std"))]
151183
fn brightness_contrast<T: Adjust<Color>>(
152-
_: impl Ctx,
184+
_ctx: impl Ctx,
153185
#[implementations(
154186
Table<Raster<CPU>>,
155187
Table<Color>,
@@ -163,16 +195,7 @@ fn brightness_contrast<T: Adjust<Color>>(
163195
use_classic: bool,
164196
) -> T {
165197
if use_classic {
166-
let brightness = brightness / 255.;
167-
168-
let contrast = contrast / 100.;
169-
let contrast = if contrast > 0. { (contrast * core::f32::consts::FRAC_PI_2 - 0.01).tan() } else { contrast };
170-
171-
let offset = brightness * contrast + brightness - contrast / 2.;
172-
173-
input.adjust(|color| color.to_gamma_srgb().map_rgb(|c| (c + c * contrast + offset).clamp(0., 1.)).to_linear_srgb());
174-
175-
return input;
198+
return brightness_contrast_classic(_ctx, input, brightness, contrast);
176199
}
177200

178201
const WINDOW_SIZE: usize = 1024;

0 commit comments

Comments
 (0)