From ddf4b8c2fad27e7236df60e92905e038734784e5 Mon Sep 17 00:00:00 2001 From: Sophie Herold Date: Sun, 5 Mar 2023 21:18:31 +0100 Subject: [PATCH] decoder: Load HEIC/x265 as plugin Closes #85 Part-of: --- Cargo.lock | 5 ++- Cargo.toml | 2 +- build-aux/flatpak/org.gnome.Loupe.Devel.json | 47 +++++++++++++++++++- src/decoder/formats/heif.rs | 10 +++-- 4 files changed, 57 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2d83b2b..105f87b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1801,12 +1801,13 @@ dependencies = [ [[package]] name = "libheif-rs" -version = "0.18.0" +version = "0.19.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "37d09b0d2d69da084eeeda9534662bc6b6096fbce3f307149750c0e572ad0ccd" +checksum = "7fcb2c12bbd85e40a36c704003f353f193f99476985fe89251423b6d0778f5c4" dependencies = [ "enumn", "four-cc", + "libc", "libheif-sys", ] diff --git a/Cargo.toml b/Cargo.toml index 29847b8..0363acf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ url = "2.3.1" # decoders image-rs = { package = "image", version = "*", git = "https://github.com/image-rs/image.git" } -libheif-rs = "0.18.0" +libheif-rs = "0.19.0" librsvg = { git = "https://gitlab.gnome.org/GNOME/librsvg.git" } [dependencies.ashpd] diff --git a/build-aux/flatpak/org.gnome.Loupe.Devel.json b/build-aux/flatpak/org.gnome.Loupe.Devel.json index c986f2e..edaac4f 100644 --- a/build-aux/flatpak/org.gnome.Loupe.Devel.json +++ b/build-aux/flatpak/org.gnome.Loupe.Devel.json @@ -44,6 +44,19 @@ "*.la", "*.a" ], + "add-extensions": { + /* Exile HEIC (x265) codec into extension */ + "org.gnome.Loupe.Devel.HEIC" : { + /* Relative to app dir */ + "directory" : "lib/libheif-heic", + /* Relative to directory above */ + "add-ld-path" : "lib", + /* Remove extension data from main flatpak package */ + "bundle" : true, + /* Remove this extension when Loupe is deinstalled */ + "autodelete" : true + } + }, "modules" : [ { "name": "geocode-glib", @@ -91,10 +104,42 @@ ] }, { + /* HEIC (x265) decoder */ + "name" : "libde265", + "buildsystem" : "cmake", + "config-opts": [ + /* Install into folder for extension */ + "-DCMAKE_INSTALL_PREFIX=/app/lib/libheif-heic", + "-DENABLE_SDL=Off" + ], + "sources" : [ + { + "type" : "git", + "url" : "https://github.com/strukturag/libde265.git", + "tag" : "v1.0.11" + } + ] + }, + { "name" : "libheif", "buildsystem" : "cmake", "config-opts": [ - + /* Build HEIC (x265) as plugin */ + "-DWITH_LIBDE265_PLUGIN=On", + "-DPLUGIN_DIRECTORY=/app/lib/libheif-heic/lib", + "-DLIBDE265_INCLUDE_DIR=/app/lib/libheif-heic/include", + "-DLIBDE265_PKGCONF_LIBRARY_DIRS=/app/lib/libheif-heic/lib", + /* Turn all encoders off */ + "-DWITH_X265=Off", + "-DWITH_SvtEnc=Off", + "-DWITH_SvtEnc_PLUGIN=Off", + "-DWITH_AOM_ENCODER=Off", + /* Turn off plugins for AV1 */ + "-DWITH_RAV1E_PLUGIN=Off", + /* Turn off unused AV1 decoders */ + "-DWITH_RAV1E=Off", + /* No need for examples */ + "-DWITH_EXAMPLES=Off" ], "sources" : [ { diff --git a/src/decoder/formats/heif.rs b/src/decoder/formats/heif.rs index 7cb692d..70e9a12 100644 --- a/src/decoder/formats/heif.rs +++ b/src/decoder/formats/heif.rs @@ -7,7 +7,8 @@ use gtk::prelude::*; use anyhow::Context; use arc_swap::ArcSwap; -use libheif_rs::{ColorSpace, HeifContext, Plane, RgbChroma}; +use libheif_rs::{ColorSpace, HeifContext, LibHeif, Plane, RgbChroma}; +use once_cell::sync::Lazy; use std::path::PathBuf; use std::sync::Arc; @@ -15,6 +16,9 @@ use std::sync::Arc; #[derive(Debug)] pub struct Heif; +/// Loads plugins on first use +static LIBHEIF: Lazy = Lazy::new(LibHeif::new); + impl Heif { pub fn new( path: PathBuf, @@ -55,8 +59,8 @@ impl Heif { RgbChroma::Rgb }; - let mut image = handle - .decode(ColorSpace::Rgb(rgb_chroma), None) + let mut image = LIBHEIF + .decode(&handle, ColorSpace::Rgb(rgb_chroma), None) .context(i18n("Failed to decode image"))?; let plane = image