mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Brandon Thomas <[email protected]>
This commit is contained in:
7 files changed
+89
-6
No files matched your search
@@ -493,6 +493,10 @@ pub fn bayer4(x: i32, y: i32) -> f32 {
|
||||
|
||||
/// Samples a 3D LUT (`n`³ RGB triplets, red fastest) at `c`, trilinear or tetrahedral.
|
||||
pub fn lut3d_sample(lut: &[f32], n: usize, c: [f32; 3], tetrahedral: bool) -> [f32; 3] {
|
||||
// A degenerate or short table can't be sampled: leave the colour alone.
|
||||
if n < 2 || n.checked_pow(3).and_then(|c| c.checked_mul(3)).is_none_or(|len| lut.len() < len) {
|
||||
return c;
|
||||
}
|
||||
let m = (n - 1) as f32;
|
||||
let pos = c.map(|v| v.clamp(0.0, 1.0) * m);
|
||||
let i0 = pos.map(|p| (p.floor() as usize).min(n - 2));
|
||||
@@ -1528,3 +1532,19 @@ mod tone_tests {
|
||||
/// Legacy Brightness/Contrast scales contrast around 128 of 255 (contrast 80 keeps 128 at 128,
|
||||
/// measured on Photoshop 27.11; a 0.5 pivot moved it to 130).
|
||||
pub const LEGACY_PIVOT: f32 = 128.0 / 255.0;
|
||||
|
||||
#[cfg(test)]
|
||||
mod lut3d_guard_tests {
|
||||
use super::lut3d_sample;
|
||||
|
||||
#[test]
|
||||
fn degenerate_or_short_tables_leave_the_colour_alone() {
|
||||
let c = [0.2, 0.4, 0.6];
|
||||
for tetra in [false, true] {
|
||||
assert_eq!(lut3d_sample(&[], 0, c, tetra), c);
|
||||
assert_eq!(lut3d_sample(&[0.0; 3], 1, c, tetra), c);
|
||||
assert_eq!(lut3d_sample(&[0.0; 3], 2, c, tetra), c);
|
||||
assert_eq!(lut3d_sample(&[0.0; 24], usize::MAX, c, tetra), c);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -95,6 +95,10 @@ impl Map {
|
||||
/// Shifted copy (bilinear), outside reads `outside`.
|
||||
fn shifted(&self, dx: f32, dy: f32, outside: f32) -> Map {
|
||||
let mut out = Map::new(self.w, self.h, 0.0);
|
||||
// A hostile distance (±1e30, inf, NaN) would overflow the i64 offsets below; no shift beyond
|
||||
// 2^30 px can land inside a map, so clamp (non-finite shifts read as none).
|
||||
let reach = |d: f32| if d.is_finite() { d.clamp(-1.0e9, 1.0e9) } else { 0.0 };
|
||||
let (dx, dy) = (reach(dx), reach(dy));
|
||||
let (fx, fy) = (dx.floor(), dy.floor());
|
||||
let (ax, ay) = (dx - fx, dy - fy);
|
||||
let s = |x: i64, y: i64| if x < 0 || y < 0 || x >= self.w as i64 || y >= self.h as i64 { outside } else { self.v[y as usize * self.w + x as usize] };
|
||||
@@ -3122,4 +3126,13 @@ mod tests {
|
||||
let diff: f32 = plain.v.iter().zip(&noisy.v).map(|(a, b)| (a - b).abs()).sum();
|
||||
assert!(diff > 5.0, "glow noise changes the coverage: {diff}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shifting_by_hostile_distances_does_not_overflow() {
|
||||
let m = square_shape(12);
|
||||
for d in [1.0e30, -1.0e30, f32::INFINITY, f32::NAN] {
|
||||
let s = m.shifted(d, d, 0.0);
|
||||
assert_eq!(s.v.len(), 144);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -88,6 +88,11 @@ pub fn render(doc: &Document, rect: Rect) -> Buffer {
|
||||
render_tiled(doc, rect, RENDER_TILE)
|
||||
}
|
||||
|
||||
/// Most pixels one [`render`] call will produce (2^30, a 16 GiB buffer; 32768² px). Larger rects
|
||||
/// (a hostile canvas size is up to 300000 px a side) return an empty buffer instead of aborting
|
||||
/// on allocation; callers wanting more render in pieces.
|
||||
pub const MAX_RENDER_PIXELS: u64 = 1 << 30;
|
||||
|
||||
/// [`render`] with an explicit tile size (tests check tile independence).
|
||||
pub fn render_tiled(doc: &Document, rect: Rect, tile: i32) -> Buffer {
|
||||
let patterns = pattern::PreparedPatterns::new(&doc.patterns, pattern::PREPARED_PATTERN_BYTES).in_mode(doc.pixel_format().mode);
|
||||
@@ -107,8 +112,12 @@ fn render_tiled_with(doc: &Document, rect: Rect, tile: i32, cx: &Ctx) -> Buffer
|
||||
if rect.is_empty() {
|
||||
return Buffer::transparent(rect);
|
||||
}
|
||||
// Refuse rects too large to hold: an empty buffer, not an allocation failure.
|
||||
if u64::from(rect.width()) * u64::from(rect.height()) > MAX_RENDER_PIXELS {
|
||||
return Buffer::transparent(Rect::EMPTY);
|
||||
}
|
||||
cx.set_fx_clip(rect);
|
||||
if rect.width() as i32 <= tile && rect.height() as i32 <= tile {
|
||||
if i64::from(rect.width()) <= i64::from(tile) && i64::from(rect.height()) <= i64::from(tile) {
|
||||
return photocraft_color::convert::with_cmyk_space(cmyk, || {
|
||||
let mut buf = multichannel::backdrop(doc, rect);
|
||||
in_blend_space(lab, hdr, || composite_stack_at(&doc.layers, &mut buf, cx, advanced::Scope::ROOT));
|
||||
|
||||
@@ -344,7 +344,15 @@ fn build_mask(layer: &Layer, canvas: Rect) -> Surface {
|
||||
// mask's painted tiles when the vector mask lets them through outside the path, each grown
|
||||
// by its feather.
|
||||
let mut area = match vm.and_then(|vm| vm.path.control_bounds()) {
|
||||
Some((x0, y0, x1, y1)) => grow(Rect::new(x0.floor() as i32 - 2, y0.floor() as i32 - 2, x1.ceil() as i32 + 2, y1.ceil() as i32 + 2), sv),
|
||||
Some((x0, y0, x1, y1)) => grow(
|
||||
Rect::new(
|
||||
(x0.floor() as i32).saturating_sub(2),
|
||||
(y0.floor() as i32).saturating_sub(2),
|
||||
(x1.ceil() as i32).saturating_add(2),
|
||||
(y1.ceil() as i32).saturating_add(2),
|
||||
),
|
||||
sv,
|
||||
),
|
||||
None => Rect::EMPTY,
|
||||
};
|
||||
if let Some(m) = pixel
|
||||
@@ -761,4 +769,14 @@ mod tests {
|
||||
l.vector_mask = None;
|
||||
assert!(combined_mask(&l, canvas).is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hostile_path_coordinates_do_not_overflow() {
|
||||
let mut l = Layer::raster("l", PixelFormat::RGBA8);
|
||||
let knots = [(-1.0e30, -1.0e30), (1.0e30, 0.0), (0.0, 1.0e30)].iter().map(|&(x, y)| Knot::corner(x, y)).collect();
|
||||
let mut path = Path::default();
|
||||
path.subpaths.push(Subpath { knots, closed: true, ..Default::default() });
|
||||
l.vector_mask = Some(VectorMask::new(path));
|
||||
let _ = combined_mask(&l, Rect::new(0, 0, 8, 8));
|
||||
}
|
||||
}
|
||||
@@ -159,7 +159,7 @@ impl Tile {
|
||||
if wx == 0.0 {
|
||||
continue;
|
||||
}
|
||||
let p = self.at(x0 + dx, y0 + dy);
|
||||
let p = self.at(x0.saturating_add(dx), y0.saturating_add(dy));
|
||||
let k = wx * wy;
|
||||
for c in 0..4 {
|
||||
acc[c] += p[c] * k;
|
||||
@@ -287,4 +287,14 @@ mod tests {
|
||||
let (u, v) = rot.map(1.0, 3.0);
|
||||
assert!((u - -3.0).abs() < 1e-9 && (v - 1.0).abs() < 1e-9, "{u},{v}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sampling_at_hostile_phases_does_not_overflow() {
|
||||
let patterns = [checker()];
|
||||
let prepared = PreparedPatterns::new(&patterns, PREPARED_PATTERN_BYTES);
|
||||
let t = prepared.get(&patterns[0].id, "").unwrap();
|
||||
for u in [1.0e30, -1.0e30, f64::INFINITY, f64::NAN] {
|
||||
let _ = t.sample(u, u);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -20,12 +20,15 @@ pub fn downsample(s: &Surface, k: u32) -> Surface {
|
||||
let bpp = s.format().bytes_per_pixel();
|
||||
let (x0, x1) = (b.x0.div_euclid(k), b.x1.div_euclid(k) + i32::from(b.x1.rem_euclid(k) != 0));
|
||||
let (y0, y1) = (b.y0.div_euclid(k), b.y1.div_euclid(k) + i32::from(b.y1.rem_euclid(k) != 0));
|
||||
let w = (x1 - x0).max(0) as usize;
|
||||
let w = x1.saturating_sub(x0).max(0) as usize;
|
||||
let mut row = vec![0u8; w * bpp];
|
||||
for oy in y0..y1 {
|
||||
let src = s.to_interleaved(Rect::new(x0 * k, oy * k, x1 * k, oy * k + 1));
|
||||
let src = s.to_interleaved(Rect::new(x0.saturating_mul(k), oy.saturating_mul(k), x1.saturating_mul(k), oy.saturating_mul(k).saturating_add(1)));
|
||||
for (i, px) in row.chunks_exact_mut(bpp).enumerate() {
|
||||
px.copy_from_slice(&src[i * k as usize * bpp..(i * k as usize + 1) * bpp]);
|
||||
// Saturated edges (a surface at the i32 limits) can make `src` shorter than expected.
|
||||
if let Some(from) = (i * k as usize * bpp).checked_add(bpp).and_then(|end| src.get(i * k as usize * bpp..end)) {
|
||||
px.copy_from_slice(from);
|
||||
}
|
||||
}
|
||||
out.write_interleaved(Rect::new(x0, oy, x1, oy + 1), &row);
|
||||
}
|
||||
|
||||
@@ -2488,6 +2488,16 @@ fn lab_documents_gradient_map_follows_lightness() {
|
||||
assert!((buf.px[0][0] - grey_buf.px[0][0]).abs() > 0.05, "{:?} {:?}", buf.px[0], grey_buf.px[0]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn render_refuses_rects_beyond_the_pixel_cap() {
|
||||
let doc = Document::new("c", Size::new(4, 4), ColorMode::Rgb, SampleType::U8);
|
||||
// 2^32 - 1 px wide: the width wraps negative as an i32, which once took the single-tile path.
|
||||
for tile in [crate::RENDER_TILE, 1 << 20] {
|
||||
let b = crate::render_tiled(&doc, Rect::new(i32::MIN, 0, i32::MAX, 1), tile);
|
||||
assert!(b.px.is_empty() && b.rect.is_empty());
|
||||
}
|
||||
}
|
||||
|
||||
// The effect-map cache key covers Transparency Shapes Layer and Blend If: both change the shape
|
||||
// the maps are built from (a layer's own, or a styled group's children composite) without
|
||||
// touching pixels, masks or effects, and stale maps kept stroking a hole the layer no longer
|
||||
|
||||
Reference in new issue
Block a user