Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Add Rust FOGRA39 CMYK JPEG encoder for photobook exports

laurenspriem6d8a82990afd
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 13157f4ef8..f39980a98d 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2866,6 +2866,23 @@ dependencies = [
"wasm-bindgen-futures",
]
+[[package]]
+name = "ente-photobook"
+version = "0.0.0"
+dependencies = [
+ "jpeg-encoder",
+ "moxcms 0.9.1",
+]
+
+[[package]]
+name = "ente-photobook-wasm"
+version = "0.0.0"
+dependencies = [
+ "ente-photobook",
+ "ente-wasm-lib",
+ "wasm-bindgen",
+]
+
[[package]]
name = "ente-photos"
version = "0.0.0"
@@ -5410,6 +5427,12 @@ dependencies = [
"libc",
]
+[[package]]
+name = "jpeg-encoder"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a0370574b86f7eca156b9f298392b5e69a23f8c86f3f865add60bbc2e79467a6"
+
[[package]]
name = "js-sys"
version = "0.3.102"
diff --git a/rust/Cargo.toml b/rust/Cargo.toml
index 39eb130f0c..dd2797f511 100644
--- a/rust/Cargo.toml
+++ b/rust/Cargo.toml
@@ -18,6 +18,7 @@ members = [
"bindings/wasm/lib",
"bindings/wasm/locker",
"bindings/wasm/paste",
+ "bindings/wasm/photobook",
"bindings/wasm/photos",
"bindings/wasm/prelogin",
"bindings/wasm/space",
@@ -37,6 +38,7 @@ members = [
"crates/locker",
"crates/ml",
"crates/paste",
+ "crates/photobook",
"crates/photos",
"crates/photos-export",
"crates/space",
@@ -74,6 +76,7 @@ ente-location = { path = "crates/location" }
ente-locker = { path = "crates/locker" }
ente-ml = { path = "crates/ml" }
ente-paste = { path = "crates/paste" }
+ente-photobook = { path = "crates/photobook" }
ente-photos = { path = "crates/photos" }
ente-photos-export = { path = "crates/photos-export" }
ente-space = { path = "crates/space" }
@@ -92,11 +95,13 @@ hkdf = "0.13.0"
hmac = "0.13.0"
icu_casemap = "2.2.0"
image = "0.25.10"
+jpeg-encoder = "0.7.1"
js-sys = "0.3.102"
keyring = { version = "3.6.3", default-features = false }
log = "0.4.34"
md-5 = "0.11.0"
mockito = "1.7.2"
+moxcms = "0.9.1"
open = "5.4.4"
ort = { version = "=2.0.0-rc.13", default-features = false }
ort-sys = { version = "=2.0.0-rc.13", default-features = false }
diff --git a/rust/bindings/wasm/photobook/Cargo.toml b/rust/bindings/wasm/photobook/Cargo.toml
new file mode 100644
index 0000000000..56c8cefb3f
--- /dev/null
+++ b/rust/bindings/wasm/photobook/Cargo.toml
@@ -0,0 +1,14 @@
+[package]
+name = "ente-photobook-wasm"
+edition.workspace = true
+
+[lib]
+crate-type = ["cdylib"]
+
+[dependencies]
+ente-photobook.workspace = true
+ente-wasm-lib.workspace = true
+wasm-bindgen.workspace = true
+
+[lints]
+workspace = true
diff --git a/rust/bindings/wasm/photobook/src/lib.rs b/rust/bindings/wasm/photobook/src/lib.rs
new file mode 100644
index 0000000000..f248044048
--- /dev/null
+++ b/rust/bindings/wasm/photobook/src/lib.rs
@@ -0,0 +1,43 @@
+use ente_photobook::Error as E;
+use wasm_bindgen::prelude::*;
+
+pub struct Error(E);
+
+impl From<E> for Error {
+ fn from(error: E) -> Self {
+ Self(error)
+ }
+}
+
+impl From<Error> for JsValue {
+ fn from(Error(error): Error) -> Self {
+ let name = match error {
+ E::InvalidProfile(_) | E::NotCmykProfile => Some("invalid_profile"),
+ E::InvalidDimensions { .. } => Some("invalid_dimensions"),
+ E::InvalidQuality(_) => Some("invalid_quality"),
+ E::Convert(_) | E::Encode(_) => None,
+ };
+ ente_wasm_lib::js_error(&error, name)
+ }
+}
+
+#[wasm_bindgen]
+pub struct CmykJpegEncoder(ente_photobook::CmykJpegEncoder);
+
+#[wasm_bindgen]
+impl CmykJpegEncoder {
+ #[wasm_bindgen(constructor)]
+ pub fn new(output_profile: &[u8]) -> Result<Self, Error> {
+ Ok(Self(ente_photobook::CmykJpegEncoder::new(output_profile)?))
+ }
+
+ pub fn encode(
+ &self,
+ srgb_rgba: Vec<u8>,
+ width: u16,
+ height: u16,
+ quality: u8,
+ ) -> Result<Vec<u8>, Error> {
+ Ok(self.0.encode(srgb_rgba, width, height, quality)?)
+ }
+}
diff --git a/rust/crates/image/Cargo.toml b/rust/crates/image/Cargo.toml
index 8fd8ca2be8..2cfcba8850 100644
--- a/rust/crates/image/Cargo.toml
+++ b/rust/crates/image/Cargo.toml
@@ -10,7 +10,7 @@ fast_image_resize.workspace = true
image.workspace = true
jxl-oxide = { version = "0.12.6", features = ["image"] }
kamadak-exif = "0.6.1"
-moxcms = { version = "0.9.1", features = ["in_place"] }
+moxcms = { workspace = true, features = ["in_place"] }
png = "0.18.1"
thiserror.workspace = true
tiff = "0.11.3"
diff --git a/rust/crates/photobook/Cargo.toml b/rust/crates/photobook/Cargo.toml
new file mode 100644
index 0000000000..b19cec73f0
--- /dev/null
+++ b/rust/crates/photobook/Cargo.toml
@@ -0,0 +1,10 @@
+[package]
+name = "ente-photobook"
+edition.workspace = true
+
+[dependencies]
+jpeg-encoder.workspace = true
+moxcms.workspace = true
+
+[lints]
+workspace = true
diff --git a/rust/crates/photobook/src/lib.rs b/rust/crates/photobook/src/lib.rs
new file mode 100644
index 0000000000..187f8b5a1e
--- /dev/null
+++ b/rust/crates/photobook/src/lib.rs
@@ -0,0 +1,254 @@
+use std::{fmt, sync::Arc};
+
+use jpeg_encoder::{ColorType, Encoder, EncodingError, SamplingFactor};
+use moxcms::{
+ CmsError, ColorProfile, DataColorSpace, Layout, RenderingIntent, Transform16BitExecutor,
+ TransformOptions,
+};
+
+#[derive(Debug)]
+pub enum Error {
+ InvalidProfile(CmsError),
+ NotCmykProfile,
+ InvalidDimensions { width: u16, height: u16, len: usize },
+ InvalidQuality(u8),
+ Convert(CmsError),
+ Encode(EncodingError),
+}
+
+impl fmt::Display for Error {
+ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
+ match self {
+ Self::InvalidProfile(_) => f.write_str("invalid output profile"),
+ Self::NotCmykProfile => f.write_str("output profile is not a CMYK profile"),
+ Self::InvalidDimensions { width, height, len } => {
+ write!(f, "{len} bytes is not a {width}x{height} RGBA image")
+ }
+ Self::InvalidQuality(quality) => {
+ write!(f, "JPEG quality {quality} is not between 1 and 100")
+ }
+ Self::Convert(_) => f.write_str("CMYK conversion failed"),
+ Self::Encode(_) => f.write_str("JPEG encoding failed"),
+ }
+ }
+}
+
+impl std::error::Error for Error {
+ fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
+ match self {
+ Self::InvalidProfile(e) | Self::Convert(e) => Some(e),
+ Self::Encode(e) => Some(e),
+ Self::NotCmykProfile | Self::InvalidDimensions { .. } | Self::InvalidQuality(_) => None,
+ }
+ }
+}
+
+pub struct CmykJpegEncoder {
+ transform: Arc<Transform16BitExecutor>,
+}
+
+impl CmykJpegEncoder {
+ pub fn new(output_profile: &[u8]) -> Result<Self, Error> {
+ let output = ColorProfile::new_from_slice(output_profile).map_err(Error::InvalidProfile)?;
+ if output.color_space != DataColorSpace::Cmyk {
+ return Err(Error::NotCmykProfile);
+ }
+ let options = TransformOptions {
+ rendering_intent: RenderingIntent::Perceptual,
+ ..TransformOptions::default()
+ };
+ let transform = ColorProfile::new_srgb()
+ .create_transform_16bit(Layout::Rgb, &output, Layout::Rgba, options)
+ .map_err(Error::InvalidProfile)?;
+ Ok(Self { transform })
+ }
+
+ pub fn encode(
+ &self,
+ mut srgb_rgba: Vec<u8>,
+ width: u16,
+ height: u16,
+ quality: u8,
+ ) -> Result<Vec<u8>, Error> {
+ if !(1..=100).contains(&quality) {
+ return Err(Error::InvalidQuality(quality));
+ }
+ let row_len = usize::from(width) * 4;
+ if row_len == 0 || height == 0 || srgb_rgba.len() != row_len * usize::from(height) {
+ return Err(Error::InvalidDimensions {
+ width,
+ height,
+ len: srgb_rgba.len(),
+ });
+ }
+ self.convert_in_place(&mut srgb_rgba, usize::from(width))?;
+ let cmyk = srgb_rgba;
+
+ let mut jpeg = Vec::new();
+ let mut encoder = Encoder::new(&mut jpeg, quality);
+ encoder.set_sampling_factor(SamplingFactor::F_1_1);
+ encoder.set_optimized_huffman_tables(true);
+ encoder
+ .encode(&cmyk, width, height, ColorType::Cmyk)
+ .map_err(Error::Encode)?;
+ Ok(jpeg)
+ }
+
+ fn convert_in_place(&self, pixels: &mut [u8], width: usize) -> Result<(), Error> {
+ let mut rgb_row = vec![[0u16; 3]; width];
+ let mut cmyk_row = vec![0u16; width * 4];
+ for row in pixels.chunks_exact_mut(width * 4) {
+ for (rgb, &[r, g, b, a]) in rgb_row.iter_mut().zip(row.as_chunks::<4>().0) {
+ *rgb = [r, g, b].map(|value| over_white(value, u32::from(a)));
+ }
+ self.transform
+ .transform(rgb_row.as_flattened(), &mut cmyk_row)
+ .map_err(Error::Convert)?;
+ for (byte, &value) in row.iter_mut().zip(&cmyk_row) {
+ *byte = to_u8(value);
+ }
+ }
+ Ok(())
+ }
+}
+
+fn over_white(value: u8, alpha: u32) -> u16 {
+ ((u32::from(value) * alpha * 257 + 65535 * (255 - alpha) + 127) / 255) as u16
+}
+
+fn to_u8(value: u16) -> u8 {
+ ((u32::from(value) * 255 + 32767) / 65535) as u8
+}
+
+#[cfg(test)]
+mod tests {
+ use moxcms::{
+ Chromaticity, LutDataType, LutStore, LutType, LutWarehouse, Matrix3d, ProfileClass,
+ };
+
+ use super::*;
+
+ fn key_only_profile() -> Vec<u8> {
+ let black_below_white = (0..8)
+ .flat_map(|index| [0, 0, 0, if index < 4 { u16::MAX } else { 0 }])
+ .collect();
+ let mut profile = ColorProfile::default();
+ profile.profile_class = ProfileClass::OutputDevice;
+ profile.color_space = DataColorSpace::Cmyk;
+ profile.pcs = DataColorSpace::Lab;
+ profile.white_point = Chromaticity::D50.to_xyzd();
+ profile.lut_b_to_a_perceptual = Some(LutWarehouse::Lut(LutDataType {
+ num_input_channels: 3,
+ num_output_channels: 4,
+ num_clut_grid_points: 2,
+ matrix: Matrix3d::IDENTITY,
+ num_input_table_entries: 2,
+ num_output_table_entries: 2,
+ input_table: LutStore::Store16([0, u16::MAX].repeat(3)),
+ clut_table: LutStore::Store16(black_below_white),
+ output_table: LutStore::Store16([0, u16::MAX].repeat(4)),
+ lut_type: LutType::Lut16,
+ }));
+ profile.encode().unwrap()
+ }
+
+ fn cmyk_of(pixels: &[[u8; 4]]) -> Vec<[u8; 4]> {
+ let encoder = CmykJpegEncoder::new(&key_only_profile()).unwrap();
+ let mut bytes = pixels.concat();
+ encoder.convert_in_place(&mut bytes, pixels.len()).unwrap();
+ bytes.as_chunks::<4>().0.to_vec()
+ }
+
+ fn segments(jpeg: &[u8]) -> Vec<(u8, &[u8])> {
+ assert_eq!(jpeg[..2], [0xFF, 0xD8]);
+ let mut segments = Vec::new();
+ let mut at = 2;
+ while jpeg[at + 1] != 0xDA {
+ assert_eq!(jpeg[at], 0xFF);
+ let len = usize::from(u16::from_be_bytes([jpeg[at + 2], jpeg[at + 3]]));
+ segments.push((jpeg[at + 1], &jpeg[at + 4..at + 2 + len]));
+ at += 2 + len;
+ }
+ assert_eq!(jpeg[jpeg.len() - 2..], [0xFF, 0xD9]);
+ segments
+ }
+
+ #[test]
+ fn converts_through_the_output_profile() {
+ let cmyk = cmyk_of(&[[255, 255, 255, 255], [119, 119, 119, 255], [0, 0, 0, 255]]);
+ let [white, gray, black] = cmyk[..] else {
+ panic!("expected three pixels")
+ };
+ assert!(white.iter().all(|&ink| ink <= 2), "{white:?}");
+ assert!(black[..3].iter().all(|&ink| ink <= 2), "{black:?}");
+ assert!(black[3] >= 253, "{black:?}");
+ assert!(
+ gray[3] > white[3] + 64 && gray[3] < black[3] - 64,
+ "{gray:?}"
+ );
+ }
+
+ #[test]
+ fn composites_transparency_over_white_paper() {
+ let cmyk = cmyk_of(&[
+ [255, 255, 255, 255],
+ [0, 0, 0, 0],
+ [0, 0, 0, 128],
+ [0, 0, 0, 255],
+ ]);
+ assert_eq!(cmyk[1], cmyk[0]);
+ assert!(cmyk[2][3] > cmyk[0][3] + 64 && cmyk[2][3] < cmyk[3][3] - 64);
+ }
+
+ #[test]
+ fn writes_unsubsampled_adobe_cmyk_jpeg() {
+ let encoder = CmykJpegEncoder::new(&key_only_profile()).unwrap();
+ let jpeg = encoder.encode(vec![128; 3 * 2 * 4], 3, 2, 98).unwrap();
+ let segments = segments(&jpeg);
+
+ let adobe = segments
+ .iter()
+ .find(|(marker, data)| *marker == 0xEE && data.starts_with(b"Adobe"))
+ .map(|(_, data)| data)
+ .unwrap();
+ assert_eq!(adobe[11], 0);
+
+ let frame = segments
+ .iter()
+ .find(|(marker, _)| *marker == 0xC0)
+ .map(|(_, data)| data)
+ .unwrap();
+ assert_eq!(frame[..6], [8, 0, 2, 0, 3, 4]);
+ assert!(frame[6..].as_chunks::<3>().0.iter().all(|c| c[1] == 0x11));
+ }
+
+ #[test]
+ fn rejects_invalid_profiles() {
+ assert!(matches!(
+ CmykJpegEncoder::new(b"not a profile"),
+ Err(Error::InvalidProfile(_))
+ ));
+ let srgb = ColorProfile::new_srgb().encode().unwrap();
+ assert!(matches!(
+ CmykJpegEncoder::new(&srgb),
+ Err(Error::NotCmykProfile)
+ ));
+ }
+
+ #[test]
+ fn rejects_invalid_images() {
+ let encoder = CmykJpegEncoder::new(&key_only_profile()).unwrap();
+ for (len, width, height) in [(16, 2, 1), (0, 0, 0), (8, 0, 2)] {
+ assert!(matches!(
+ encoder.encode(vec![0; len], width, height, 98),
+ Err(Error::InvalidDimensions { .. })
+ ));
+ }
+ for quality in [0, 101] {
+ assert!(matches!(
+ encoder.encode(vec![0; 4], 1, 1, quality),
+ Err(Error::InvalidQuality(_))
+ ));
+ }
+ }
+}