Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

perf(raster): read 16- and 32-bit rows into RGBA with the colour model resolved per row

xsvm331c7f38a553
diff --git a/crates/color/src/lib.rs b/crates/color/src/lib.rs
index a33437963..281c3e01b 100644
--- a/crates/color/src/lib.rs
+++ b/crates/color/src/lib.rs
@@ -120,6 +120,29 @@ pub fn read_sample(bytes: &[u8], sample: SampleType, index: usize) -> f32 {
}
}
+/// [`read_sample`] for the `out.len()` samples from sample index `start` on, matching the sample
+/// type once instead of per sample.
+pub fn read_samples(bytes: &[u8], sample: SampleType, start: usize, out: &mut [f32]) {
+ let end = start + out.len();
+ match sample {
+ SampleType::U8 => {
+ for (o, b) in out.iter_mut().zip(&bytes[start..end]) {
+ *o = *b as f32 / 255.0;
+ }
+ }
+ SampleType::U16 => {
+ for (o, b) in out.iter_mut().zip(bytes[start * 2..end * 2].as_chunks::<2>().0) {
+ *o = u16::from_ne_bytes(*b) as f32 / 65535.0;
+ }
+ }
+ SampleType::F32 => {
+ for (o, b) in out.iter_mut().zip(bytes[start * 4..end * 4].as_chunks::<4>().0) {
+ *o = f32::from_ne_bytes(*b);
+ }
+ }
+ }
+}
+
/// Write a normalised f32 sample; integer types are clamped and rounded.
#[inline]
pub fn write_sample(bytes: &mut [u8], sample: SampleType, index: usize, v: f32) {
diff --git a/crates/raster/src/lib.rs b/crates/raster/src/lib.rs
index 958ab38b5..8347eb51e 100644
--- a/crates/raster/src/lib.rs
+++ b/crates/raster/src/lib.rs
@@ -14,7 +14,7 @@ use std::collections::BTreeMap;
use std::fmt;
use std::sync::Arc;
-use photocraft_color::{ColorMode, PixelFormat, SampleType, read_sample, write_sample};
+use photocraft_color::{ColorMode, PixelFormat, SampleType, read_sample, read_samples, write_sample};
use photocraft_geom::{Rect, TILE_SIZE, TileCoord};
/// A checked surface allocation could not be represented or reserved.
@@ -503,9 +503,7 @@ impl Surface {
match tile {
Some(t) => {
let base = (((y - tc.ty * TILE_SIZE) as usize) * TILE_SIZE as usize + (tr.x0 - tc.tx * TILE_SIZE) as usize) * n;
- for (i, d) in dst.iter_mut().enumerate() {
- *d = read_sample(&t.data, self.format.sample, base + i);
- }
+ read_samples(&t.data, self.format.sample, base, dst);
}
None if n == 1 => dst.fill(dp[0]),
None => {
@@ -529,8 +527,8 @@ impl Surface {
let n = fmt.channels();
let mut stack = [0.0; 8];
let mut heap = Vec::new();
- let px = self.default_samples(&mut stack, &mut heap);
- let dp = to_rgba(&fmt, px);
+ let dp = to_rgba(&fmt, self.default_samples(&mut stack, &mut heap));
+ let mut row = Vec::new();
for tc in r.tiles() {
let tr = tc.rect().intersect(&r);
let tile = self.tiles.get(&tc);
@@ -574,11 +572,17 @@ impl Surface {
}
}
_ => {
- for (i, d) in dst.iter_mut().enumerate() {
- for (c, v) in px.iter_mut().enumerate() {
- *v = read_sample(&t.data, fmt.sample, base + i * n + c);
- }
- *d = to_rgba(&fmt, px);
+ // The row's samples in one pass, then its colour model resolved once:
+ // RGB and grey take their channels as `to_rgba` does, other models go
+ // through it.
+ row.resize(dst.len() * n, 0.0);
+ read_samples(&t.data, fmt.sample, base, &mut row);
+ let a = |s: &[f32], i: usize| if fmt.alpha { s[i] } else { 1.0 };
+ let pixels = dst.iter_mut().zip(row.chunks_exact(n));
+ match fmt.mode {
+ ColorMode::Rgb => pixels.for_each(|(d, s)| *d = [s[0], s[1], s[2], a(s, 3)]),
+ ColorMode::Grayscale | ColorMode::Bitmap | ColorMode::Duotone => pixels.for_each(|(d, s)| *d = [s[0], s[0], s[0], a(s, 1)]),
+ _ => pixels.for_each(|(d, s)| *d = to_rgba(&fmt, s)),
}
}
}
@@ -1603,6 +1607,31 @@ mod tests {
}
}
+ #[test]
+ fn rgba_reads_match_individual_pixels_bitwise_in_every_model_and_depth() {
+ // Across tile edges and a missing tile; float samples include -0, out-of-range and NaN.
+ let rect = Rect::new(-3, -1, 300, 2);
+ for mode in [ColorMode::Rgb, ColorMode::Grayscale, ColorMode::Lab, ColorMode::Cmyk] {
+ for depth in SampleType::ALL {
+ for alpha in [false, true] {
+ let fmt = PixelFormat::new(mode, depth, alpha);
+ let mut s = Surface::with_default(fmt, &vec![0.25; fmt.channels()]);
+ let values = [-0.0, 0.1, 0.5, 1.0, 1.5, -0.25, f32::NAN, 0.75];
+ for (i, x) in (-2..260).enumerate() {
+ let px: Vec<f32> = (0..fmt.channels()).map(|c| values[(i + c * 3) % values.len()]).collect();
+ s.write_pixel(x, 0, &px);
+ }
+ let mut out = vec![[0.0; 4]; (rect.width() * rect.height()) as usize];
+ s.read_rgba_into(rect, &mut out);
+ for (i, actual) in out.iter().enumerate() {
+ let expected = s.rgba(rect.x0 + (i % rect.width() as usize) as i32, rect.y0 + (i / rect.width() as usize) as i32);
+ assert_eq!(actual.map(f32::to_bits), expected.map(f32::to_bits), "{fmt:?}, pixel={i}");
+ }
+ }
+ }
+ }
+ }
+
#[test]
fn tile_content_bounds_scans_against_the_given_pixel() {
let mut s = Surface::new(PixelFormat::GRAY8);