Skip to content

armgit / ente

Public Imported from GitHub
0

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

Remove libheif dependency

laurenspriem5066ce3ea384
diff --git a/mobile/apps/photos/rust/Cargo.lock b/mobile/apps/photos/rust/Cargo.lock
index 91367acb67..1ad86e5623 100644
--- a/mobile/apps/photos/rust/Cargo.lock
+++ b/mobile/apps/photos/rust/Cargo.lock
@@ -544,7 +544,6 @@ dependencies = [
"image",
"imageproc",
"kamadak-exif",
- "libheif-rs",
"nalgebra 0.33.2",
"ndarray 0.16.1",
"once_cell",
@@ -553,17 +552,6 @@ dependencies = [
"usearch",
]
-[[package]]
-name = "enumn"
-version = "0.1.14"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "2f9ed6b3789237c8a0c1c505af1c7eb2c560df6186f01b098c3a1064ea532f38"
-dependencies = [
- "proc-macro2",
- "quote",
- "syn",
-]
-
[[package]]
name = "env_filter"
version = "0.1.4"
@@ -723,12 +711,6 @@ version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "77ce24cb58228fbb8aa041425bb1050850ac19177686ea6e0f41a70416f56fdb"
-[[package]]
-name = "four-cc"
-version = "0.4.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "795cbfc56d419a7ce47ccbb7504dd9a5b7c484c083c356e797de08bd988d9629"
-
[[package]]
name = "futures"
version = "0.3.31"
@@ -1044,9 +1026,9 @@ checksum = "7a79a3332a6609480d7d0c9eab957bca6b455b91bb84e66d19f5ff66294b85b8"
[[package]]
name = "libc"
-version = "0.2.178"
+version = "0.2.182"
source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "37c93d8daa9d8a012fd8ab92f088405fb202ea0b6ab73ee2482ae66af4f42091"
+checksum = "6800badb6cb2082ffd7b6a67e6125bb39f18782f793520caee8cb8846be06112"
[[package]]
name = "libfuzzer-sys"
@@ -1058,30 +1040,6 @@ dependencies = [
"cc",
]
-[[package]]
-name = "libheif-rs"
-version = "1.1.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e4a26370abb4723a3ce73083e479b98017604206cadb0e35da5eac4813600d85"
-dependencies = [
- "enumn",
- "four-cc",
- "libc",
- "libheif-sys",
-]
-
-[[package]]
-name = "libheif-sys"
-version = "3.1.0+1.18.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "e663db80d4272b60c066c5a9d17370ffa0433a31d424152f95f1e1effb9b3860"
-dependencies = [
- "libc",
- "pkg-config",
- "vcpkg",
- "walkdir",
-]
-
[[package]]
name = "libloading"
version = "0.9.0"
@@ -1478,12 +1436,6 @@ version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b870d8c151b6f2fb93e84a13146138f05d02ed11c7e7c54f8826aaaf7c9f184"
-[[package]]
-name = "pkg-config"
-version = "0.3.32"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "7edddbd0b52d732b21ad9a5fab5c704c14cd949e5e9a1ec5929a24fded1b904c"
-
[[package]]
name = "png"
version = "0.18.1"
@@ -1807,15 +1759,6 @@ dependencies = [
"bytemuck",
]
-[[package]]
-name = "same-file"
-version = "1.0.6"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502"
-dependencies = [
- "winapi-util",
-]
-
[[package]]
name = "scopeguard"
version = "1.2.0"
@@ -2065,28 +2008,12 @@ dependencies = [
"wasm-bindgen",
]
-[[package]]
-name = "vcpkg"
-version = "0.2.15"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
-
[[package]]
name = "version_check"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
-[[package]]
-name = "walkdir"
-version = "2.5.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
-dependencies = [
- "same-file",
- "winapi-util",
-]
-
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
diff --git a/mobile/apps/photos/rust/Cargo.toml b/mobile/apps/photos/rust/Cargo.toml
index 6f5952b027..2e8c110c0e 100644
--- a/mobile/apps/photos/rust/Cargo.toml
+++ b/mobile/apps/photos/rust/Cargo.toml
@@ -14,7 +14,6 @@ fast_image_resize = { version = "5.5.0", features = ["image", "rayon"] }
image = { version = "0.25.5", default-features = true }
imageproc = "0.25.0"
kamadak-exif = "0.6.1"
-libheif-rs = { version = "1.0.2", optional = true }
nalgebra = "0.33.2"
ndarray = "0.16.1"
once_cell = "1.20.2"
@@ -26,4 +25,3 @@ unexpected_cfgs = { level = "warn", check-cfg = ['cfg(frb_expand)'] }
[features]
default = []
-heif = ["dep:libheif-rs"]
diff --git a/mobile/apps/photos/rust/src/ml/decode.rs b/mobile/apps/photos/rust/src/ml/decode.rs
index 955fae8fd3..1b31333866 100644
--- a/mobile/apps/photos/rust/src/ml/decode.rs
+++ b/mobile/apps/photos/rust/src/ml/decode.rs
@@ -1,7 +1,7 @@
use std::{io::Cursor, path::Path};
use exif::{In, Reader as ExifReader, Tag};
-use image::{DynamicImage, RgbImage};
+use image::{DynamicImage, ImageReader, RgbImage};
use crate::ml::{
error::{MlError, MlResult},
@@ -22,11 +22,13 @@ pub fn decode_image_from_path(image_path: &str) -> MlResult<DecodedImage> {
.map_err(|e| MlError::Decode(format!("failed to read image file '{image_path}': {e}")))?;
let exif_orientation = read_exif_orientation(&file_bytes);
- let decoded_rgb = if HEIF_EXTENSIONS.contains(&ext.as_str()) {
- decode_heif_from_path(image_path)?
- } else {
- image::load_from_memory(&file_bytes)?.to_rgb8()
- };
+ if HEIF_EXTENSIONS.contains(&ext.as_str()) {
+ return Err(MlError::Decode(
+ "HEIC/HEIF decoding is currently unsupported in Rust indexing".to_string(),
+ ));
+ }
+
+ let decoded_rgb = decode_with_image_crate(&file_bytes)?;
let oriented =
apply_exif_orientation(DynamicImage::ImageRgb8(decoded_rgb), exif_orientation).to_rgb8();
@@ -39,6 +41,14 @@ pub fn decode_image_from_path(image_path: &str) -> MlResult<DecodedImage> {
})
}
+fn decode_with_image_crate(file_bytes: &[u8]) -> MlResult<RgbImage> {
+ let reader = ImageReader::new(Cursor::new(file_bytes))
+ .with_guessed_format()
+ .map_err(|e| MlError::Decode(format!("failed to guess image format: {e}")))?;
+ let dynamic = reader.decode()?;
+ Ok(dynamic.to_rgb8())
+}
+
fn read_exif_orientation(image_data: &[u8]) -> u32 {
let mut reader = Cursor::new(image_data);
let exif_reader = ExifReader::new();
@@ -63,47 +73,3 @@ fn apply_exif_orientation(image: DynamicImage, orientation: u32) -> DynamicImage
_ => image,
}
}
-
-#[cfg(feature = "heif")]
-fn decode_heif_from_path(image_path: &str) -> MlResult<RgbImage> {
- use image::{ImageBuffer, Rgb};
- use libheif_rs::{ColorSpace, HeifContext, LibHeif, RgbChroma};
-
- let context = HeifContext::read_from_file(image_path)
- .map_err(|e| MlError::Decode(format!("failed to open HEIF file '{image_path}': {e}")))?;
- let handle = context
- .primary_image_handle()
- .map_err(|e| MlError::Decode(format!("failed to read HEIF primary handle: {e}")))?;
- let decoder = LibHeif::new();
- let image = decoder
- .decode(&handle, ColorSpace::Rgb(RgbChroma::Rgb), None)
- .map_err(|e| MlError::Decode(format!("failed to decode HEIF image: {e}")))?;
-
- let width = image.width();
- let height = image.height();
- let plane = image
- .planes()
- .interleaved
- .ok_or_else(|| MlError::Decode("HEIF image has no interleaved RGB plane".to_string()))?;
-
- let row_stride = plane.stride;
- let row_width = width as usize * 3;
- let mut rgb = vec![0u8; width as usize * height as usize * 3];
- for y in 0..height as usize {
- let src_start = y * row_stride;
- let src_end = src_start + row_width;
- let dst_start = y * row_width;
- rgb[dst_start..(dst_start + row_width)].copy_from_slice(&plane.data[src_start..src_end]);
- }
-
- ImageBuffer::<Rgb<u8>, _>::from_raw(width as u32, height as u32, rgb)
- .ok_or_else(|| MlError::Decode("failed to construct RGB image from HEIF plane".to_string()))
-}
-
-#[cfg(not(feature = "heif"))]
-fn decode_heif_from_path(image_path: &str) -> MlResult<RgbImage> {
- let _ = image_path;
- Err(MlError::Decode(
- "HEIF decoding is unavailable because the `heif` cargo feature is disabled".to_string(),
- ))
-}