π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
[rust] Give resize typed entry points instead of an image enum
laurenspriema1ef8cd45292
diff --git a/rust/crates/ml/src/cv/image.rs b/rust/crates/ml/src/cv/image.rsindex 690206b912..08ab5db7f6 100644--- a/rust/crates/ml/src/cv/image.rs+++ b/rust/crates/ml/src/cv/image.rs@@ -209,43 +209,6 @@ impl ImageF32 {}}-#[derive(Clone, Copy, Debug)]-pub(crate) enum ImageRef<'a> {- U8(&'a ImageU8),- F32(&'a ImageF32),-}--#[derive(Clone, Debug)]-pub(crate) enum Image {- U8(ImageU8),- F32(ImageF32),-}--impl ImageRef<'_> {- pub(crate) fn size(&self) -> (i32, i32) {- match self {- ImageRef::U8(i) => i.size(),- ImageRef::F32(i) => (i.width, i.height),- }- }-}--impl Image {- pub(crate) fn into_u8(self) -> OpResult<ImageU8> {- match self {- Image::U8(i) => Ok(i),- Image::F32(_) => Err("expected an 8-bit image, got 32-bit float".to_string()),- }- }-- pub(crate) fn into_f32(self) -> OpResult<ImageF32> {- match self {- Image::F32(i) => Ok(i),- Image::U8(_) => Err("expected a 32-bit float image, got 8-bit".to_string()),- }- }-}-#[derive(Clone, Debug, PartialEq)]pub(crate) struct Contour {pub points: Vec<(i32, i32)>,diff --git a/rust/crates/ml/src/cv/mod.rs b/rust/crates/ml/src/cv/mod.rsindex e8236998d2..f77c38cc37 100644--- a/rust/crates/ml/src/cv/mod.rs+++ b/rust/crates/ml/src/cv/mod.rs@@ -16,8 +16,6 @@ mod warp;use rayon::prelude::*;-use self::image::{Image, ImageRef};-pub(crate) type OpResult<T> = Result<T, String>;pub(crate) use bilateral::bilateral_filter_u8;@@ -31,6 +29,7 @@ pub(crate) use masking::{bitwise_and_u8, copy_to_masked, count_non_zero, in_range_u8, threshold_binary_u8,};pub(crate) use morph::{morphology_close, morphology_erode, morphology_open};+pub(crate) use resize::{Interp, resize_f32, resize_u8};pub(crate) use stats::{hist_256_f32, hist_256_u8, mean_f32, mean_u8c3_masked, min_max_loc_f32, percentile_f32,};@@ -38,20 +37,6 @@ pub(crate) use structuring::ellipse_kernel;pub(crate) use transform::rotate_u8;pub(crate) use warp::warp_perspective;-use resize::Interp;--pub(crate) fn resize_bilinear(src: ImageRef<'_>, width: i32, height: i32) -> OpResult<Image> {- resize::resize(src, width, height, Interp::Bilinear)-}--pub(crate) fn resize_area(src: ImageRef<'_>, width: i32, height: i32) -> OpResult<Image> {- resize::resize(src, width, height, Interp::Area)-}--pub(crate) fn resize_bicubic(src: ImageRef<'_>, width: i32, height: i32) -> OpResult<Image> {- resize::resize(src, width, height, Interp::Bicubic)-}-const PARALLEL_MIN_PIXELS: usize = 65_536;const PIXELS_PER_CHUNK: usize = 65_536;diff --git a/rust/crates/ml/src/cv/resize.rs b/rust/crates/ml/src/cv/resize.rsindex 7824954862..782f0062d2 100644--- a/rust/crates/ml/src/cv/resize.rs+++ b/rust/crates/ml/src/cv/resize.rs@@ -6,7 +6,7 @@ use fast_image_resize::{};use crate::cv::OpResult;-use crate::cv::image::{Image, ImageF32, ImageRef, ImageU8};+use crate::cv::image::{ImageF32, ImageU8};#[derive(Clone, Copy, PartialEq, Eq, Debug)]pub(crate) enum Interp {@@ -15,6 +15,17 @@ pub(crate) enum Interp {Bicubic,}+impl Interp {+ fn alg(self, upscaling: bool) -> ResizeAlg {+ match self {+ Interp::Bilinear => ResizeAlg::Interpolation(FilterType::Bilinear),+ Interp::Bicubic => ResizeAlg::Interpolation(FilterType::CatmullRom),+ Interp::Area if upscaling => ResizeAlg::Interpolation(FilterType::Bilinear),+ Interp::Area => ResizeAlg::Convolution(FilterType::Box),+ }+ }+}+fn pixel_type(channels: i32, is_f32: bool) -> OpResult<PixelType> {Ok(match (is_f32, channels) {(false, 1) => PixelType::U8,@@ -38,6 +49,9 @@ fn run(height: i32,alg: ResizeAlg,) -> OpResult<Vec<u8>> {+ if width <= 0 || height <= 0 {+ return Err(format!("resize: invalid destination size {width}x{height}"));+ }let src = FirImageRef::new(src_size.0 as u32, src_size.1 as u32, src_bytes, pixel_type).map_err(|e| format!("resize: bad source: {e}"))?;let mut dst = FirImage::new(width as u32, height as u32, pixel_type);@@ -47,59 +61,47 @@ fn run(Ok(dst.into_vec())}-pub(crate) fn resize(- src: ImageRef<'_>,+pub(crate) fn resize_u8(+ src: &ImageU8,width: i32,height: i32,interp: Interp,-) -> OpResult<Image> {- let (sw, sh) = src.size();- if width <= 0 || height <= 0 {- return Err(format!("resize: invalid destination size {width}x{height}"));+) -> OpResult<ImageU8> {+ if width == src.width && height == src.height {+ return Ok(src.clone());}- if width == sw && height == sh {- return Ok(match src {- ImageRef::U8(i) => Image::U8(i.clone()),- ImageRef::F32(i) => Image::F32(i.clone()),- });- }-- let alg = match interp {- Interp::Bilinear => ResizeAlg::Interpolation(FilterType::Bilinear),- Interp::Bicubic => ResizeAlg::Interpolation(FilterType::CatmullRom),- Interp::Area if width >= sw && height >= sh => {- ResizeAlg::Interpolation(FilterType::Bilinear)- }- Interp::Area => ResizeAlg::Convolution(FilterType::Box),- };+ let data = run(+ &src.data,+ (src.width, src.height),+ pixel_type(src.channels, false)?,+ width,+ height,+ interp.alg(width >= src.width && height >= src.height),+ )?;+ ImageU8::new(width, height, src.channels, data)+}- match src {- ImageRef::U8(s) => {- let data = run(- &s.data,- s.size(),- pixel_type(s.channels, false)?,- width,- height,- alg,- )?;- Ok(Image::U8(ImageU8::new(width, height, s.channels, data)?))- }- ImageRef::F32(s) => {- let bytes: Vec<u8> = s.data.iter().flat_map(|v| v.to_ne_bytes()).collect();- let out = run(- &bytes,- (s.width, s.height),- pixel_type(s.channels, true)?,- width,- height,- alg,- )?;- let data: Vec<f32> = out- .chunks_exact(4)- .map(|b| f32::from_ne_bytes([b[0], b[1], b[2], b[3]]))- .collect();- Ok(Image::F32(ImageF32::new(width, height, s.channels, data)?))- }+pub(crate) fn resize_f32(+ src: &ImageF32,+ width: i32,+ height: i32,+ interp: Interp,+) -> OpResult<ImageF32> {+ if width == src.width && height == src.height {+ return Ok(src.clone());}+ let bytes: Vec<u8> = src.data.iter().flat_map(|v| v.to_ne_bytes()).collect();+ let out = run(+ &bytes,+ (src.width, src.height),+ pixel_type(src.channels, true)?,+ width,+ height,+ interp.alg(width >= src.width && height >= src.height),+ )?;+ let data = out+ .chunks_exact(4)+ .map(|b| f32::from_ne_bytes([b[0], b[1], b[2], b[3]]))+ .collect();+ ImageF32::new(width, height, src.channels, data)}diff --git a/rust/crates/ml/src/scan/color.rs b/rust/crates/ml/src/scan/color.rsindex cbd7338beb..d73aac454b 100644--- a/rust/crates/ml/src/scan/color.rs+++ b/rust/crates/ml/src/scan/color.rs@@ -3,7 +3,7 @@ use super::detection::resize_for_max_pixels;use super::geometry::{Quad, norm};use super::mask::Mask;use crate::cv;-use crate::cv::image::{ImageF32, ImageRef, ImageU8};+use crate::cv::image::{ImageF32, ImageU8};#[derive(Clone, Copy, Debug, PartialEq, Eq)]pub enum ColorMode {@@ -96,8 +96,7 @@ fn document_mask(work_size: (i32, i32),) -> OpResult<ImageU8> {let mask_image = mask.binary_image()?;- let resized_mask =- cv::resize_area(ImageRef::U8(&mask_image), work_size.0, work_size.1)?.into_u8()?;+ let resized_mask = cv::resize_u8(&mask_image, work_size.0, work_size.1, cv::Interp::Area)?;let resized_quad = quad.scaled_to(orig_size.0 as f64,diff --git a/rust/crates/ml/src/scan/detection.rs b/rust/crates/ml/src/scan/detection.rsindex d09fd5a139..b778706950 100644--- a/rust/crates/ml/src/scan/detection.rs+++ b/rust/crates/ml/src/scan/detection.rs@@ -7,7 +7,7 @@ use super::perspective::estimate_real_dimensions;use super::postprocess::enhance_captured_image;use super::quad_score::score_quad_against_probmap;use crate::cv;-use crate::cv::image::{Contour, ImageRef, ImageU8};+use crate::cv::image::{Contour, ImageU8};const THRESHOLDS: [f64; 7] = [0.5, 0.7, 0.75, 0.8, 0.85, 0.9, 0.95];const LIVE_THRESHOLDS: [f64; 1] = [0.9];@@ -160,7 +160,7 @@ pub(crate) fn resize_for_max_pixels(img: &ImageU8, max_pixels: f64) -> OpResult<}let scale = (max_pixels / orig_pixels as f64).sqrt();let (width, height) = size_trunc(img.width as f64 * scale, img.height as f64 * scale);- cv::resize_area(ImageRef::U8(img), width, height)?.into_u8()+ cv::resize_u8(img, width, height, cv::Interp::Area)}pub(crate) fn extract_document(diff --git a/rust/crates/ml/src/scan/postprocess.rs b/rust/crates/ml/src/scan/postprocess.rsindex 9287a627f5..00487c0552 100644--- a/rust/crates/ml/src/scan/postprocess.rs+++ b/rust/crates/ml/src/scan/postprocess.rs@@ -2,7 +2,7 @@ use super::OpResult;use super::color::ColorMode;use super::detection::size_trunc;use crate::cv;-use crate::cv::image::{ImageF32, ImageRef, ImageU8};+use crate::cv::image::{ImageF32, ImageU8};pub(crate) fn enhance_captured_image(img: &ImageU8, color_mode: ColorMode) -> OpResult<ImageU8> {match color_mode {@@ -32,7 +32,7 @@ fn remove_shadows(channel: &ImageU8) -> OpResult<ImageU8> {} else {(width, height)};- let small = cv::resize_area(ImageRef::U8(channel), map_w, map_h)?.into_u8()?;+ let small = cv::resize_u8(channel, map_w, map_h, cv::Interp::Area)?;let k = ((map_w.max(map_h) as f64 * SHADOW_KERNEL_FRAC) as i32).max(3) | 1;let kernel = cv::ellipse_kernel(k)?;@@ -45,7 +45,7 @@ fn remove_shadows(channel: &ImageU8) -> OpResult<ImageU8> {if paper < 1.0 {return Ok(channel.clone());}- let map = cv::resize_bilinear(ImageRef::F32(&map_small), width, height)?.into_f32()?;+ let map = cv::resize_f32(&map_small, width, height, cv::Interp::Bilinear)?;let knee = paper / SHADOW_MAX_GAIN;let mut out = ImageU8::zeros(width, height, 1)?;@@ -84,7 +84,7 @@ fn multi_scale_retinex_on_l(bgr: &ImageU8) -> OpResult<ImageU8> {let small_h = l_float.height as f64 / scale_factor;let (small_cols, small_rows) = size_trunc(small_w, small_h);- let l_small = cv::resize_area(ImageRef::F32(&l_float), small_cols, small_rows)?.into_f32()?;+ let l_small = cv::resize_f32(&l_float, small_cols, small_rows, cv::Interp::Area)?;let log_l_small = l_small.map(f32::ln);let max_dim_small = small_w.max(small_h);@@ -111,9 +111,12 @@ fn multi_scale_retinex_on_l(bgr: &ImageU8) -> OpResult<ImageU8> {let inv_range = if range > 1e-6 { 1.0 / range } else { 1.0 };retinex_small.map_mut(|v| ((v - min_val as f32) as f64 * inv_range) as f32);- let retinex_norm =- cv::resize_bicubic(ImageRef::F32(&retinex_small), l_float.width, l_float.height)?- .into_f32()?;+ let retinex_norm = cv::resize_f32(+ &retinex_small,+ l_float.width,+ l_float.height,+ cv::Interp::Bicubic,+ )?;let mean_l = cv::mean_f32(&l_original_float)?;let amplitude = 60.0;diff --git a/rust/crates/ml/src/scan/segmentation.rs b/rust/crates/ml/src/scan/segmentation.rsindex f980a0bf2e..d869e1783f 100644--- a/rust/crates/ml/src/scan/segmentation.rs+++ b/rust/crates/ml/src/scan/segmentation.rs@@ -8,7 +8,7 @@ use ort::value::TensorRef;use super::OpResult;use super::scanner::ScanError;use crate::cv;-use crate::cv::image::{ImageRef, ImageU8};+use crate::cv::image::ImageU8;pub const MASK_SIDE: i32 = 256;@@ -49,7 +49,7 @@ impl Segmenter {// Resize first: the channel swap is per-pixel, so doing it after the// downscale costs 256x256 instead of a full-resolution pass, and it// folds into the tensor fill below.- let resized = cv::resize_bilinear(ImageRef::U8(bgr), MASK_SIDE, MASK_SIDE)?.into_u8()?;+ let resized = cv::resize_u8(bgr, MASK_SIDE, MASK_SIDE, cv::Interp::Bilinear)?;let mut input = vec![0.0f32; resized.data.len()];for (out, px) in input.chunks_exact_mut(3).zip(resized.data.chunks_exact(3)) {