mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Fix wasm build + clippy in the perf/feature arcs
rayon is native-only (algo's wasm build has no rayon). The perf arcs used raw rayon::prelude, breaking the wasm gate; switch them to the cfg-gated par_rows/par_map helpers (new par_rows2 for two-buffer loops: Sobel, quantize). Replace/Match Color revert to serial (no w param for par_rows). Also clears clippy -D warnings in frame_cmds/variables_cmds (let-chains, needless refs, redundant max). Workspace: tests + clippy + wasm + layers green. Co-Authored-By: Claude Opus 4.8 <[email protected]>
This commit is contained in:
1 parent
81b995ce17
commit
aeaaa82684
6 files changed
+76
-57
No files matched your search
@@ -8,9 +8,10 @@
|
||||
// Effects walk several per-pixel planes in lockstep; index loops read clearest.
|
||||
#![allow(clippy::needless_range_loop)]
|
||||
|
||||
use rayon::prelude::*;
|
||||
use std::f32::consts::PI;
|
||||
|
||||
use crate::photo_util::{par_rows, par_rows2};
|
||||
|
||||
use photocraft_geom::Rect;
|
||||
|
||||
use crate::Ctx;
|
||||
@@ -120,7 +121,7 @@ fn sobel(p: &[f32], w: usize, h: usize) -> (Vec<f32>, Vec<f32>) {
|
||||
let at = |x: isize, y: isize| p[(y.clamp(0, h as isize - 1) as usize) * w + x.clamp(0, w as isize - 1) as usize];
|
||||
let mut gx = vec![0.0; w * h];
|
||||
let mut gy = vec![0.0; w * h];
|
||||
gx.par_chunks_mut(w).zip(gy.par_chunks_mut(w)).enumerate().for_each(|(y, (gxr, gyr))| {
|
||||
par_rows2(&mut gx, &mut gy, w, |y, gxr, gyr| {
|
||||
let y = y as isize;
|
||||
for x in 0..w as isize {
|
||||
let xi = x as usize;
|
||||
@@ -203,7 +204,7 @@ fn kuwahara(win: &mut Win, r: usize) {
|
||||
let i = ((y.clamp(0, h as isize - 1) as usize) * w + x.clamp(0, w as isize - 1) as usize) * 5;
|
||||
&m[i..i + 5]
|
||||
};
|
||||
win.c.par_chunks_mut(w).enumerate().for_each(|(y, row)| {
|
||||
par_rows(&mut win.c, w, 1, |y, row| {
|
||||
let y = y as isize;
|
||||
for x in 0..w as isize {
|
||||
let mut best = f32::MAX;
|
||||
|
||||
@@ -136,6 +136,18 @@ pub(crate) fn par_rows<T: Send + Sync>(buf: &mut [T], w: usize, ch: usize, f: im
|
||||
buf.chunks_mut(stride).enumerate().for_each(|(y, row)| f(y, row));
|
||||
}
|
||||
|
||||
/// Like [`par_rows`] but over two row-aligned buffers in lockstep (e.g. index + pixels).
|
||||
pub(crate) fn par_rows2<A: Send, B: Send>(a: &mut [A], b: &mut [B], w: usize, f: impl Fn(usize, &mut [A], &mut [B]) + Sync + Send) {
|
||||
let s = w.max(1);
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
{
|
||||
use rayon::prelude::*;
|
||||
a.par_chunks_mut(s).zip(b.par_chunks_mut(s)).enumerate().for_each(|(y, (ar, br))| f(y, ar, br));
|
||||
}
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
a.chunks_mut(s).zip(b.chunks_mut(s)).enumerate().for_each(|(y, (ar, br))| f(y, ar, br));
|
||||
}
|
||||
|
||||
/// Maps `f` over `0..n` (in parallel on native targets), keeping order.
|
||||
pub(crate) fn par_map<T: Send>(n: usize, f: impl Fn(usize) -> T + Sync + Send) -> Vec<T> {
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
|
||||
+22
-22
@@ -5,7 +5,6 @@
|
||||
//! Buffers are straight RGBA in 0..=1, row-major. Median cut follows Heckbert (1982); k-means
|
||||
//! refinement is Lloyd's algorithm; error diffusion is Floyd & Steinberg (1976).
|
||||
|
||||
use rayon::prelude::*;
|
||||
use photocraft_color::convert::srgb_to_lab;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
@@ -419,30 +418,31 @@ pub fn quantize(
|
||||
// None / Pattern / Noise dither have no cross-pixel dependency: map every pixel in parallel.
|
||||
// (Diffusion propagates error between pixels, so it stays serial below.)
|
||||
if dither != Dither::Diffusion {
|
||||
idx.par_iter_mut().zip(px.par_iter_mut()).enumerate().for_each(|(i, (ip, p))| {
|
||||
let (x, y) = (i % w, i / w);
|
||||
let pv = *p;
|
||||
if pv[3] < 0.5 {
|
||||
if let Some(t) = transparent {
|
||||
*ip = t as u8;
|
||||
*p = [0.0, 0.0, 0.0, 0.0];
|
||||
return;
|
||||
crate::photo_util::par_rows2(&mut idx, px, w, |y, idxrow, pxrow| {
|
||||
for (x, (ip, p)) in idxrow.iter_mut().zip(pxrow.iter_mut()).enumerate() {
|
||||
let pv = *p;
|
||||
if pv[3] < 0.5 {
|
||||
if let Some(t) = transparent {
|
||||
*ip = t as u8;
|
||||
*p = [0.0, 0.0, 0.0, 0.0];
|
||||
continue;
|
||||
}
|
||||
if pv[3] <= 0.0 {
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if pv[3] <= 0.0 {
|
||||
return;
|
||||
let mut c = [pv[0], pv[1], pv[2]];
|
||||
match dither {
|
||||
Dither::Pattern => c = c.map(|v| v + bayer8(x, y) * spread),
|
||||
Dither::Noise => c = c.map(|v| v + (hash01(x, y, 7) - 0.5) * spread),
|
||||
_ => {}
|
||||
}
|
||||
let c = c.map(|v| v.clamp(0.0, 1.0));
|
||||
let k = nearest(pal, c);
|
||||
*ip = k as u8;
|
||||
let e = pal[k].map(|v| f32::from(v) / 255.0);
|
||||
*p = [e[0], e[1], e[2], if transparent.is_some() { 1.0 } else { pv[3] }];
|
||||
}
|
||||
let mut c = [pv[0], pv[1], pv[2]];
|
||||
match dither {
|
||||
Dither::Pattern => c = c.map(|v| v + bayer8(x, y) * spread),
|
||||
Dither::Noise => c = c.map(|v| v + (hash01(x, y, 7) - 0.5) * spread),
|
||||
_ => {}
|
||||
}
|
||||
let c = c.map(|v| v.clamp(0.0, 1.0));
|
||||
let k = nearest(pal, c);
|
||||
*ip = k as u8;
|
||||
let e = pal[k].map(|v| f32::from(v) / 255.0);
|
||||
*p = [e[0], e[1], e[2], if transparent.is_some() { 1.0 } else { pv[3] }];
|
||||
});
|
||||
return idx;
|
||||
}
|
||||
|
||||
+20
-14
@@ -7,10 +7,10 @@
|
||||
//! derived from its manual and observation, not from its implementation.
|
||||
|
||||
use photocraft_color::convert::{lab_to_srgb, srgb_to_lab};
|
||||
use rayon::prelude::*;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::fxutil::gauss_blur_n;
|
||||
use crate::photo_util::{par_map, par_rows};
|
||||
use crate::other2::{hsl_to_rgb, rgb_to_hsl};
|
||||
|
||||
fn luma(c: [f32; 3]) -> f32 {
|
||||
@@ -85,9 +85,11 @@ pub fn shadows_highlights(px: &mut [[f32; 4]], w: usize, h: usize, p: &ShadowsHi
|
||||
);
|
||||
let color = p.color / 100.0;
|
||||
let mid = p.midtone / 100.0;
|
||||
px.par_iter_mut().enumerate().for_each(|(i, q)| {
|
||||
par_rows(px, w, 1, |y, pxrow| {
|
||||
for (x, q) in pxrow.iter_mut().enumerate() {
|
||||
let i = y * w + x;
|
||||
if q[3] <= 0.0 {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
let c = [q[0], q[1], q[2]];
|
||||
// Mask weights: 1 in deep shadows (resp. bright highlights), fading out at the tonal width.
|
||||
@@ -116,6 +118,7 @@ pub fn shadows_highlights(px: &mut [[f32; 4]], w: usize, h: usize, p: &ShadowsHi
|
||||
q[0] = out[0].clamp(0.0, 1.0);
|
||||
q[1] = out[1].clamp(0.0, 1.0);
|
||||
q[2] = out[2].clamp(0.0, 1.0);
|
||||
}
|
||||
});
|
||||
clip_stretch(px, p.black_clip, p.white_clip);
|
||||
}
|
||||
@@ -170,9 +173,9 @@ pub fn replace_color(
|
||||
lightness: f32,
|
||||
) -> Vec<f32> {
|
||||
let mask = crate::selection::color_range(px, color, fuzziness);
|
||||
px.par_iter_mut().zip(mask.par_iter()).for_each(|(q, &k)| {
|
||||
for (q, &k) in px.iter_mut().zip(&mask) {
|
||||
if k <= 0.0 {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
let c = [q[0], q[1], q[2]];
|
||||
let [hh, ss, ll] = rgb_to_hsl(c);
|
||||
@@ -194,7 +197,7 @@ pub fn replace_color(
|
||||
for i in 0..3 {
|
||||
q[i] = (c[i] + (o[i] - c[i]) * k).clamp(0.0, 1.0);
|
||||
}
|
||||
});
|
||||
}
|
||||
mask
|
||||
}
|
||||
|
||||
@@ -280,7 +283,7 @@ pub fn match_color(
|
||||
dst_mean[1] = 0.0;
|
||||
dst_mean[2] = 0.0;
|
||||
}
|
||||
px.par_iter_mut().filter(|q| q[3] > 0.0).for_each(|q| {
|
||||
for q in px.iter_mut().filter(|q| q[3] > 0.0) {
|
||||
let c = [q[0], q[1], q[2]];
|
||||
let lab = srgb_to_lab(c);
|
||||
let l = ((lab[0] - target.mean[0]) * scale(0) + dst_mean[0]) * lum;
|
||||
@@ -290,7 +293,7 @@ pub fn match_color(
|
||||
for i in 0..3 {
|
||||
q[i] = (o[i] + (c[i] - o[i]) * fade).clamp(0.0, 1.0);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
/// Image › Adjustments › HDR Toning ("Local Adaptation" method) parameters.
|
||||
@@ -357,10 +360,10 @@ pub fn hdr_toning(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning) {
|
||||
return;
|
||||
}
|
||||
const EPS: f32 = 1e-4;
|
||||
let logl: Vec<f32> = px
|
||||
.par_iter()
|
||||
.map(|q| (luma([q[0], q[1], q[2]]).max(0.0) + EPS).ln())
|
||||
.collect();
|
||||
let logl: Vec<f32> = par_map(px.len(), |i| {
|
||||
let q = px[i];
|
||||
(luma([q[0], q[1], q[2]]).max(0.0) + EPS).ln()
|
||||
});
|
||||
let mut base = logl.clone();
|
||||
gauss_blur_n(&mut base, w, h, 1, p.radius.max(1.0) / 2.0);
|
||||
let n = px.iter().filter(|q| q[3] > 0.0).count().max(1) as f32;
|
||||
@@ -378,9 +381,11 @@ pub fn hdr_toning(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning) {
|
||||
let mut sorted = p.curve.clone();
|
||||
sorted.sort_by(|a, b| a.0.total_cmp(&b.0));
|
||||
// Per-pixel tone map: embarrassingly parallel (base/logl read by index).
|
||||
px.par_iter_mut().enumerate().for_each(|(i, q)| {
|
||||
par_rows(px, w, 1, |y, row| {
|
||||
for (x, q) in row.iter_mut().enumerate() {
|
||||
let i = y * w + x;
|
||||
if q[3] <= 0.0 {
|
||||
return;
|
||||
continue;
|
||||
}
|
||||
let c = [q[0], q[1], q[2]];
|
||||
let l0 = luma(c).max(0.0) + EPS;
|
||||
@@ -404,6 +409,7 @@ pub fn hdr_toning(px: &mut [[f32; 4]], w: usize, h: usize, p: &HdrToning) {
|
||||
q[0] = out[0].clamp(0.0, 1.0);
|
||||
q[1] = out[1].clamp(0.0, 1.0);
|
||||
q[2] = out[2].clamp(0.0, 1.0);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -22,7 +22,7 @@ fn has_layer(s: &Session) -> std::result::Result<(), String> {
|
||||
/// A reveal mask that is on inside `rect` and off (hidden) everywhere else.
|
||||
fn clip_mask(rect: Rect) -> LayerMask {
|
||||
let mut mask = LayerMask::hide_all();
|
||||
let (w, h) = (rect.width().max(0) as usize, rect.height().max(0) as usize);
|
||||
let (w, h) = (rect.width() as usize, rect.height() as usize);
|
||||
if w > 0 && h > 0 {
|
||||
mask.surface.write_region(rect, &vec![1.0f32; w * h]);
|
||||
mask.surface.prune();
|
||||
@@ -68,7 +68,7 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
params: r#"{"name":str?} → {layer, frame:[x,y,w,h]}: groups the selected layers and clips them to a frame rect"#,
|
||||
enabled: has_layer,
|
||||
journal: true,
|
||||
run: |s, p| frame_from_layers(s, &p),
|
||||
run: |s, p| frame_from_layers(s, p),
|
||||
}]
|
||||
}
|
||||
|
||||
|
||||
@@ -159,13 +159,13 @@ fn apply_to_doc(doc: &mut Document, vars: &Variables, set: &DataSet) -> Result<(
|
||||
}
|
||||
}
|
||||
(VarKind::TextReplacement, VarValue::Text(text)) => {
|
||||
if let Some(l) = doc.layer_mut(def.layer) {
|
||||
if let LayerContent::Text(t) = &mut l.content {
|
||||
t.text = text.clone();
|
||||
t.cache = None; // force re-render
|
||||
t.runs.clear(); // re-flow as one run from the summary style
|
||||
t.paragraphs.clear();
|
||||
}
|
||||
if let Some(l) = doc.layer_mut(def.layer)
|
||||
&& let LayerContent::Text(t) = &mut l.content
|
||||
{
|
||||
t.text = text.clone();
|
||||
t.cache = None; // force re-render
|
||||
t.runs.clear(); // re-flow as one run from the summary style
|
||||
t.paragraphs.clear();
|
||||
}
|
||||
}
|
||||
(VarKind::PixelReplacement { method, align, clip }, VarValue::Pixels(path)) => {
|
||||
@@ -222,10 +222,10 @@ fn replace_pixels(doc: &mut Document, layer: LayerId, path: &str, method: PixelM
|
||||
let dy = (target.y0 as f64 + (th - sh) * fy).round() as i32;
|
||||
let placed = translate_surface(&scaled, dx, dy);
|
||||
let placed = if clip { photocraft_algo::resample::crop_surface(&placed, target) } else { placed };
|
||||
if let Some(l) = doc.layer_mut(layer) {
|
||||
if let LayerContent::Raster(sfc) = &mut l.content {
|
||||
*sfc = placed;
|
||||
}
|
||||
if let Some(l) = doc.layer_mut(layer)
|
||||
&& let LayerContent::Raster(sfc) = &mut l.content
|
||||
{
|
||||
*sfc = placed;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@@ -384,19 +384,19 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
vec![
|
||||
spec!("image.variables.define", "Define…", &["Image", "Variables"],
|
||||
"{defs:[{name, layer:id, type:visibility|textReplacement|pixelReplacement, method?:fit|fill|asIs|conform, align?, clip?}]} → {defs,dataSets,active}",
|
||||
|s, p| define(s, &p)),
|
||||
|s, p| define(s, p)),
|
||||
spec!("image.variables.dataSets", "Data Sets…", &["Image", "Variables"],
|
||||
"{dataSets:[{name, values:[{variable, kind:visibility|text|pixels, value}]}], append?} → {defs,dataSets,active}",
|
||||
|s, p| data_sets(s, &p)),
|
||||
|s, p| data_sets(s, p)),
|
||||
spec!("image.applyDataSet", "Apply Data Set…", &["Image"],
|
||||
"{name|index} → {applied, index}: sets layer visibility/text/pixels from the data set (one history step)",
|
||||
|s, p| apply_data_set(s, &p)),
|
||||
|s, p| apply_data_set(s, p)),
|
||||
spec!("file.import.variableDataSets", "Variable Data Sets…", &["File", "Import"],
|
||||
"{path, delimiter?} → {imported}: CSV header = variable names, each row a data set (first column may be the data-set name)",
|
||||
|s, p| import_data_sets(s, &p)),
|
||||
|s, p| import_data_sets(s, p)),
|
||||
spec!("file.export.dataSetsAsFiles", "Data Sets as Files…", &["File", "Export"],
|
||||
"{dir, format?:png, dataSets?[names]} → {files,count}: apply each data set and export the flattened document",
|
||||
|s, p| export_as_files(s, &p)),
|
||||
|s, p| export_as_files(s, p)),
|
||||
CommandSpec { id: "variables.list", label: "List Variables", menu: &[], shortcut: None, journal: false,
|
||||
params: "{} → {defs,dataSets,active}", enabled: has_doc, run: |s, _| list(s) },
|
||||
]
|
||||
|
||||
Reference in new issue
Block a user