mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Merge pull request #17391 from yoriswind/codex/brush-17314-20261128
Grade soft brush texture edges in Hard Mix and Color Dodge
This commit is contained in:
3 files changed
+274
-3
No files matched your search
@@ -0,0 +1,233 @@
|
||||
//! #17314: own 16x16 two-grey fixture; published measured profiles are acceptance samples.
|
||||
|
||||
use photocraft_color::PixelFormat;
|
||||
use photocraft_raster::Surface;
|
||||
|
||||
use crate::{BrushContext, BrushSettings, GrayTile, MaskMode, Pattern, StrokePoint, Texture, TipShape, render_stroke};
|
||||
|
||||
fn paint(mode: MaskMode, hardness: f32, flow: f32, depth: f32) -> Surface {
|
||||
let pattern = GrayTile::from_fn(16, 16, |x, _| if x < 8 { 64.0 / 255.0 } else { 192.0 / 255.0 });
|
||||
let brush = BrushSettings {
|
||||
size: 60.0,
|
||||
hardness,
|
||||
flow,
|
||||
seed: 17314,
|
||||
texture: Texture { enabled: true, pattern: Pattern::Tile(pattern), mode, depth, each_tip: true, ..Texture::default() },
|
||||
..BrushSettings::default()
|
||||
};
|
||||
let mut surface = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut surface, &brush, &[StrokePoint::new(100.0, 100.0, 1.0)], None, false, 1.0);
|
||||
surface
|
||||
}
|
||||
|
||||
fn grey(surface: &Surface, x: i32) -> f32 {
|
||||
255.0 * (1.0 - surface.rgba(x, 100)[3])
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_mix_grades_the_measured_soft_dab_rim_at_both_pattern_levels() {
|
||||
let surface = paint(MaskMode::HardMix, 0.0, 1.0, 1.0);
|
||||
let measured = [255, 255, 248, 196, 132, 255, 255, 255, 188, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 144, 232, 255, 255, 92, 164, 224, 255, 255, 255];
|
||||
for (x, expected) in (70..=130).step_by(2).zip(measured) {
|
||||
let actual = grey(&surface, x);
|
||||
assert!((actual - expected as f32).abs() <= 8.0, "x={x}: {actual}, measured {expected}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn color_dodge_keeps_the_measured_soft_shoulder() {
|
||||
let surface = paint(MaskMode::ColorDodge, 0.0, 1.0, 1.0);
|
||||
// The issue elides the middle; assert only its explicitly supplied coordinates.
|
||||
let samples = [
|
||||
(70, 218),
|
||||
(72, 116),
|
||||
(74, 71),
|
||||
(76, 22),
|
||||
(78, 0),
|
||||
(80, 127),
|
||||
(82, 100),
|
||||
(84, 73),
|
||||
(86, 44),
|
||||
(88, 0),
|
||||
(110, 0),
|
||||
(112, 29),
|
||||
(114, 58),
|
||||
(116, 87),
|
||||
(118, 115),
|
||||
(120, 0),
|
||||
(122, 0),
|
||||
(124, 49),
|
||||
(126, 94),
|
||||
(128, 213),
|
||||
(130, 223),
|
||||
];
|
||||
for (x, expected) in samples {
|
||||
let actual = grey(&surface, x);
|
||||
assert!((actual - expected as f32).abs() <= 8.0, "x={x}: {actual}, measured {expected}");
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_dab_flow_and_zero_depth_keep_their_existing_values() {
|
||||
for mode in [MaskMode::HardMix, MaskMode::ColorDodge] {
|
||||
for (flow, expected) in [(1.0, 0.0), (0.5, 127.5)] {
|
||||
let surface = paint(mode, 1.0, flow, 1.0);
|
||||
for x in [100, 108] {
|
||||
assert!((grey(&surface, x) - expected).abs() <= 2.0);
|
||||
}
|
||||
}
|
||||
let zero = paint(mode, 0.0, 1.0, 0.0);
|
||||
let plain = BrushSettings { size: 60.0, hardness: 0.0, seed: 17314, ..BrushSettings::default() };
|
||||
let mut reference = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut reference, &plain, &[StrokePoint::new(100.0, 100.0, 1.0)], None, false, 1.0);
|
||||
for x in 60..=140 {
|
||||
assert!((zero.rgba(x, 100)[3] - reference.rgba(x, 100)[3]).abs() < 1e-6, "{mode:?}, x={x}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_mix_zero_texture_keeps_its_existing_endpoint() {
|
||||
for v in [0.0, 0.25, 0.75, 0.999, 1.0] {
|
||||
for depth in [0.0, 0.25, 1.0] {
|
||||
let actual = crate::render::texture_combine(MaskMode::HardMix, v, 0.0, depth);
|
||||
let expected = crate::render::mask_combine(MaskMode::HardMix, v, 0.0, depth);
|
||||
assert_eq!(actual, expected, "coverage {v}, depth {depth}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_mix_keeps_legacy_soft_tip_coverage_for_black_and_near_black_texture() {
|
||||
let near_black = 1.0 / 255.0;
|
||||
for t in [0.0, near_black / 2.0, near_black] {
|
||||
for v in [0.9, 0.999, 1.0] {
|
||||
for depth in [0.0, 0.25, 1.0] {
|
||||
let actual = crate::render::texture_combine(MaskMode::HardMix, v, t, depth);
|
||||
let expected = crate::render::mask_combine(MaskMode::HardMix, v, t, depth);
|
||||
assert_eq!(actual, expected, "coverage {v}, texture {t}, depth {depth}");
|
||||
}
|
||||
}
|
||||
}
|
||||
// Stepping above the first grey level must not reintroduce the jump at the reported rim coverage.
|
||||
assert_eq!(crate::render::texture_combine(MaskMode::HardMix, 0.9, 2.0 / 255.0, 1.0), 0.0);
|
||||
|
||||
let mut brush = BrushSettings { size: 60.0, hardness: 0.0, seed: 17314, ..BrushSettings::default() };
|
||||
let stroke = [StrokePoint::new(100.0, 100.0, 1.0)];
|
||||
let mut plain = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut plain, &brush, &stroke, None, false, 1.0);
|
||||
|
||||
let pattern = GrayTile::from_fn(16, 16, |x, _| if x < 8 { 0.0 } else { near_black });
|
||||
for scale in [1.0, 1.5] {
|
||||
brush.texture = Texture {
|
||||
enabled: true,
|
||||
pattern: Pattern::Tile(pattern.clone()),
|
||||
mode: MaskMode::HardMix,
|
||||
depth: 1.0,
|
||||
scale,
|
||||
each_tip: true,
|
||||
..Texture::default()
|
||||
};
|
||||
let context = BrushContext::new(&brush);
|
||||
if scale > 1.0 {
|
||||
assert!((70..=130).any(|x| {
|
||||
let sample = context.texture_at(x, 100);
|
||||
sample > 0.0 && sample < near_black * 0.9
|
||||
}));
|
||||
}
|
||||
let mut actual = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut actual, &brush, &stroke, None, false, 1.0);
|
||||
for x in 70..=130 {
|
||||
let v = plain.rgba(x, 100)[3];
|
||||
let t = context.texture_at(x, 100);
|
||||
let expected = crate::render::mask_combine(MaskMode::HardMix, v, t, 1.0);
|
||||
assert_eq!(actual.rgba(x, 100)[3], expected, "x={x}, scale {scale}, coverage {v}, texture {t}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hard_tip_rims_preserve_legacy_texture_modes_for_both_each_tip_paths() {
|
||||
// The same bitmap must retain legacy math at either ignored hardness value, not just
|
||||
// agree with itself. Aliased round and Block tips likewise ignore hardness. Compare
|
||||
// each result with the explicit old laws, including fractional Flow after binary coverage.
|
||||
for (sampled, hardness, aliased, square) in [
|
||||
(false, 1.0, false, false),
|
||||
(true, 0.0, false, false),
|
||||
(true, 1.0, false, false),
|
||||
(false, 0.0, true, false),
|
||||
(false, 1.0, true, false),
|
||||
(false, 0.0, true, true),
|
||||
(false, 1.0, true, true),
|
||||
] {
|
||||
for mode in [MaskMode::HardMix, MaskMode::ColorDodge] {
|
||||
for each_tip in [false, true] {
|
||||
for flow in [0.5, 1.0] {
|
||||
for depth in [0.25, 1.0] {
|
||||
let mut b = BrushSettings {
|
||||
size: 16.0,
|
||||
hardness,
|
||||
aliased,
|
||||
square,
|
||||
flow,
|
||||
texture: Texture {
|
||||
enabled: true,
|
||||
pattern: Pattern::Tile(GrayTile::from_fn(16, 16, |x, _| if x < 8 { 64.0 / 255.0 } else { 192.0 / 255.0 })),
|
||||
mode,
|
||||
depth,
|
||||
each_tip,
|
||||
..Default::default()
|
||||
},
|
||||
..Default::default()
|
||||
};
|
||||
if sampled {
|
||||
b.tip = TipShape::Sampled(GrayTile::from_fn(16, 16, |_, _| 0.5)).into();
|
||||
}
|
||||
let points = [StrokePoint::new(100.0, 100.0, 1.0)];
|
||||
let mut actual = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut actual, &b, &points, None, false, 1.0);
|
||||
let mut plain_brush = b.clone();
|
||||
plain_brush.texture.enabled = false;
|
||||
plain_brush.flow = 1.0;
|
||||
let mut plain = Surface::new(PixelFormat::RGBA32F);
|
||||
render_stroke(&mut plain, &plain_brush, &points, None, false, 1.0);
|
||||
let context = crate::BrushContext::new(&b);
|
||||
let mut rim_pixels = 0;
|
||||
let mut solid_pixels = 0;
|
||||
for y in 84..=116 {
|
||||
for x in 84..=116 {
|
||||
let coverage = plain.rgba(x, y)[3];
|
||||
if coverage > 0.0 && coverage < 1.0 {
|
||||
rim_pixels += 1;
|
||||
}
|
||||
if coverage == 1.0 {
|
||||
solid_pixels += 1;
|
||||
}
|
||||
// Per-tip texture precedes Flow; stroke-level texture follows it.
|
||||
let v = if each_tip { coverage } else { coverage * flow };
|
||||
let t = context.texture_at(x, y);
|
||||
let legacy = match mode {
|
||||
MaskMode::HardMix => crate::render::mask_combine(mode, v, t, depth),
|
||||
// Texture Color Dodge has its own depth law, unlike mask_combine.
|
||||
_ => (v / (1.0 - depth * t).max(1e-3)).clamp(0.0, 1.0),
|
||||
};
|
||||
let expected = if each_tip { legacy * flow } else { legacy };
|
||||
let got = actual.rgba(x, y)[3];
|
||||
assert!(
|
||||
(got - expected).abs() < 1e-5,
|
||||
"{mode:?}, each_tip={each_tip}, flow={flow}, depth={depth}, ({x},{y}): {got} != {expected}"
|
||||
);
|
||||
}
|
||||
}
|
||||
if aliased {
|
||||
assert_eq!(rim_pixels, 0, "aliased round and Block coverage stays binary");
|
||||
assert!(solid_pixels > 0, "exercise the aliased tip interior");
|
||||
} else {
|
||||
assert!(rim_pixels > 0, "the hard-tip comparison must exercise antialiased coverage");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -489,6 +489,9 @@ mod mixer_load_supply_tests;
|
||||
#[cfg(test)]
|
||||
mod smoothing_catch_up_default_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_texture_hard_mix_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod brush_texture_depth_fade_tests;
|
||||
|
||||
|
||||
@@ -189,12 +189,22 @@ const HEIGHT_REACH: f32 = 12.0;
|
||||
/// - Linear Height is Height, but where the reach stays below the top of the pattern the paint
|
||||
/// fades with the pattern instead of being cut off.
|
||||
pub fn texture_combine(mode: MaskMode, v: f32, t: f32, d: f32) -> f32 {
|
||||
texture_combine_for_tip(mode, v, t, d, false)
|
||||
}
|
||||
|
||||
// Hard, sampled and aliased tips keep their existing arithmetic; the latter two ignore hardness.
|
||||
// Coverage alone cannot identify their provenance. The public four-argument API is unchanged.
|
||||
fn texture_combine_for_tip(mode: MaskMode, v: f32, t: f32, d: f32, legacy_tip: bool) -> f32 {
|
||||
if v <= 0.0 {
|
||||
return 0.0;
|
||||
}
|
||||
let r = match mode {
|
||||
MaskMode::Subtract => v - d * t,
|
||||
MaskMode::ColorDodge => v / (1.0 - d * t).max(1e-3),
|
||||
MaskMode::ColorDodge => {
|
||||
// A small soft-coverage headroom matches the measured shoulder without changing solid tips.
|
||||
let texture = if legacy_tip { t } else { t * (1.0 - (1.0 - v) / 32.0) };
|
||||
v / (1.0 - d * texture).max(1e-3)
|
||||
}
|
||||
MaskMode::ColorBurn => 1.0 - (1.0 - v) / (1.0 - d + d * t).max(1e-3),
|
||||
// Photoshop steps the depth in 1/255ths here, which shows at the lowest depths.
|
||||
MaskMode::Height => HEIGHT_REACH * (d * 255.0).round() / 255.0 * v - t,
|
||||
@@ -202,6 +212,25 @@ pub fn texture_combine(mode: MaskMode, v: f32, t: f32, d: f32) -> f32 {
|
||||
let reach = HEIGHT_REACH * (d * 255.0).round() / 255.0 * v;
|
||||
if reach < 1.0 { reach * (1.0 - t) } else { reach - t }
|
||||
}
|
||||
MaskMode::HardMix => {
|
||||
const NEAR_BLACK: f32 = 1.0 / 255.0;
|
||||
const DARKEST_MEASURED: f32 = 64.0 / 255.0;
|
||||
if legacy_tip || v >= 1.0 || t <= NEAR_BLACK {
|
||||
// Preserve the hard-tip endpoints and the legacy threshold for near-black textures.
|
||||
mask_combine(mode, v, t, d)
|
||||
} else {
|
||||
let mixed = if t < DARKEST_MEASURED {
|
||||
// Narrow the measured ramp around the legacy threshold as the pattern approaches black.
|
||||
let width = (t - NEAR_BLACK) / (DARKEST_MEASURED - NEAR_BLACK);
|
||||
let threshold = 1.0 - t;
|
||||
threshold + 4.0 * (v - threshold) / width
|
||||
} else {
|
||||
4.0 * v - 3.0 * (1.0 - t)
|
||||
};
|
||||
let mixed = mixed.clamp(0.0, 1.0);
|
||||
v + (mixed - v) * d
|
||||
}
|
||||
}
|
||||
_ => mask_combine(mode, v, t, d),
|
||||
};
|
||||
if r.is_finite() { r.clamp(0.0, 1.0) } else { 0.0 }
|
||||
@@ -477,7 +506,13 @@ impl BrushContext {
|
||||
m = mask_combine(b.dual_brush.mode, m, dv, 1.0);
|
||||
}
|
||||
if self.texture.is_some() && !b.texture.each_tip {
|
||||
m = texture_combine(b.texture.mode, m, self.texture_at(x, y), b.texture.depth.clamp(0.0, 1.0));
|
||||
m = texture_combine_for_tip(
|
||||
b.texture.mode,
|
||||
m,
|
||||
self.texture_at(x, y),
|
||||
b.texture.depth.clamp(0.0, 1.0),
|
||||
self.tip.is_some() || b.aliased || b.hardness >= 1.0,
|
||||
);
|
||||
}
|
||||
m
|
||||
}
|
||||
@@ -1678,7 +1713,7 @@ impl GeneralTip<'_> {
|
||||
val += (hard - val) * 0.15;
|
||||
}
|
||||
if tex_tip {
|
||||
val = texture_combine(b.texture.mode, val, ctx.texture_at(x, y), d.depth);
|
||||
val = texture_combine_for_tip(b.texture.mode, val, ctx.texture_at(x, y), d.depth, mips.is_some() || aliased || b.hardness >= 1.0);
|
||||
}
|
||||
if wet {
|
||||
val *= wet_factor(rn);
|
||||
|
||||
Reference in new issue
Block a user