Skip to content

armgit / ente

Public Imported from GitHub
0

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

Share plain Rust OCR crop interpolation across platforms

laurenspriem9d1a1e50c1d4
diff --git a/rust/Cargo.lock b/rust/Cargo.lock
index 3161eda8aa..275789937b 100644
--- a/rust/Cargo.lock
+++ b/rust/Cargo.lock
@@ -2702,7 +2702,6 @@ dependencies = [
"tokio",
"url",
"usearch",
- "wide",
]
[[package]]
diff --git a/rust/crates/ml/Cargo.toml b/rust/crates/ml/Cargo.toml
index d220eabf33..f4310e2093 100644
--- a/rust/crates/ml/Cargo.toml
+++ b/rust/crates/ml/Cargo.toml
@@ -37,7 +37,6 @@ ort = { workspace = true, features = [
] }
[target.'cfg(target_os = "android")'.dependencies]
-wide = "0.7.33"
ash = { version = "0.38.0", default-features = false, features = ["loaded"] }
ort = { workspace = true, features = [
"ndarray",
@@ -79,7 +78,6 @@ ort = { workspace = true, features = [
] }
[dev-dependencies]
-wide = "0.7.33"
anyhow = "1.0.0"
flate2.workspace = true
serde.workspace = true
diff --git a/rust/crates/ml/src/cv/mod.rs b/rust/crates/ml/src/cv/mod.rs
index d6a93e80cb..556f2b0d18 100644
--- a/rust/crates/ml/src/cv/mod.rs
+++ b/rust/crates/ml/src/cv/mod.rs
@@ -13,7 +13,6 @@ mod stats;
mod structuring;
mod transform;
mod warp;
-#[cfg(any(target_os = "android", test))]
mod warp_rgb;
use rayon::prelude::*;
diff --git a/rust/crates/ml/src/cv/warp.rs b/rust/crates/ml/src/cv/warp.rs
index d6f670b839..4bdb7a6585 100644
--- a/rust/crates/ml/src/cv/warp.rs
+++ b/rust/crates/ml/src/cv/warp.rs
@@ -1,9 +1,5 @@
-#[cfg(not(target_os = "android"))]
-use image::Rgb;
use image::RgbImage;
use imageproc::geometric_transformations::Projection;
-#[cfg(not(target_os = "android"))]
-use imageproc::geometric_transformations::{Interpolation, warp_into};
use crate::cv::OpResult;
use crate::cv::image::ImageU8;
@@ -46,19 +42,6 @@ pub(crate) fn warp_rgb_perspective(
let narrow = |c: [(f64, f64); 4]| c.map(|(x, y)| (x as f32, y as f32));
let projection = Projection::from_control_points(narrow(src_corners), narrow(dst_corners))
.ok_or_else(|| "warp_perspective: the corner pairs are degenerate".to_string())?;
- #[cfg(target_os = "android")]
let out = super::warp_rgb::warp(source, projection, width as u32, height as u32);
- #[cfg(not(target_os = "android"))]
- let out = {
- let mut out = RgbImage::new(width as u32, height as u32);
- warp_into(
- source,
- &projection,
- Interpolation::Bilinear,
- Rgb([0, 0, 0]),
- &mut out,
- );
- out
- };
ImageU8::new(width, height, 3, out.into_raw())
}
diff --git a/rust/crates/ml/src/cv/warp_rgb.rs b/rust/crates/ml/src/cv/warp_rgb.rs
index bcadf5d635..862d4983f3 100644
--- a/rust/crates/ml/src/cv/warp_rgb.rs
+++ b/rust/crates/ml/src/cv/warp_rgb.rs
@@ -1,55 +1,49 @@
use image::RgbImage;
use imageproc::geometric_transformations::Projection;
-use wide::f32x4;
+use rayon::prelude::*;
pub(super) fn warp(source: &RgbImage, projection: Projection, width: u32, height: u32) -> RgbImage {
let inverse = projection.invert();
let mut output = RgbImage::new(width, height);
let pixels = source.as_raw();
let stride = source.width() as usize * 3;
- let load = |offset: usize| {
- f32x4::from([
- f32::from(pixels[offset]),
- f32::from(pixels[offset + 1]),
- f32::from(pixels[offset + 2]),
- 0.0,
- ])
- };
- for (y, row) in output
+ output
.as_mut()
- .chunks_exact_mut(width as usize * 3)
+ .par_chunks_mut(width as usize * 3)
.enumerate()
- {
- for (x, pixel) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
- let (px, py) = inverse * (x as f32, y as f32);
- let left = px.floor();
- let top = py.floor();
- if !(left >= 0.0
- && left + 1.0 < source.width() as f32
- && top >= 0.0
- && top + 1.0 < source.height() as f32)
- {
- continue;
- }
- let right_weight = px - left;
- let bottom_weight = py - top;
- let offset = top as usize * stride + left as usize * 3;
- let right = f32x4::splat(right_weight);
- let left = f32x4::splat(1.0 - right_weight);
- let upper =
- f32x4::from_i32x4((left * load(offset) + right * load(offset + 3)).trunc_int());
- let lower = f32x4::from_i32x4(
- (left * load(offset + stride) + right * load(offset + stride + 3)).trunc_int(),
- );
- let blended = (f32x4::splat(1.0 - bottom_weight) * upper
- + f32x4::splat(bottom_weight) * lower)
- .trunc_int()
- .to_array();
- for channel in 0..3 {
- pixel[channel] = blended[channel] as u8;
+ .for_each(|(y, row)| {
+ for (x, pixel) in row.as_chunks_mut::<3>().0.iter_mut().enumerate() {
+ let (px, py) = inverse * (x as f32, y as f32);
+ let left = px.floor();
+ let top = py.floor();
+ if !(left >= 0.0
+ && left + 1.0 < source.width() as f32
+ && top >= 0.0
+ && top + 1.0 < source.height() as f32)
+ {
+ continue;
+ }
+ let right_weight = px - left;
+ let bottom_weight = py - top;
+ let offset = top as usize * stride + left as usize * 3;
+ let top = &pixels[offset..offset + 6];
+ let bottom = &pixels[offset + stride..offset + stride + 6];
+ let upper: [f32; 3] = std::array::from_fn(|c| {
+ ((1.0 - right_weight) * f32::from(top[c])
+ + right_weight * f32::from(top[c + 3]))
+ .trunc()
+ });
+ let lower: [f32; 3] = std::array::from_fn(|c| {
+ ((1.0 - right_weight) * f32::from(bottom[c])
+ + right_weight * f32::from(bottom[c + 3]))
+ .trunc()
+ });
+ let blended: [u8; 3] = std::array::from_fn(|c| {
+ ((1.0 - bottom_weight) * upper[c] + bottom_weight * lower[c]) as u8
+ });
+ *pixel = blended;
}
- }
- }
+ });
output
}