Skip to content

Commit

Permalink
✨ Support AVIF image format (#385)
Browse files Browse the repository at this point in the history
* AVIF added to generics
- Avif from Image package added to generics
- Some tests added to image/generic.rs (however they are all failing)
- Docker image works fine, and AVIF files are encoded properly (despite
  test failures)

* AVIF tests passing
- Added avif-native feature to image in cargo.toml
- Added tests to all generic formats
    - Had to strip alpha information from JPEG
- Added tests to webp from avif

* dav1d added to dockerfile

* Dav1d CI edits

* Added Luma8 Support for Jpeg

* CI composite action for dav1d setup

* Change dav1d repo

* Setup-dav1d fixes
- Added shell to setup-dav1d/action
- Added checks for dav1d for setup-script and installation for some distros
- Added system-deps to build dependencies

* Add Dav1d req to contrib docs
  • Loading branch information
smldub authored Aug 19, 2024
1 parent 8102321 commit 09ca1a9
Show file tree
Hide file tree
Showing 12 changed files with 606 additions and 56 deletions.
87 changes: 87 additions & 0 deletions .github/actions/setup-dav1d/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
name: Dav1d
description: Compile and install dav1d

runs:
using: composite
steps:
# Linux Section
- name: Install nasm
if: runner.os == 'Linux'
uses: ilammy/setup-nasm@v1

- name: Install Python 3.9
if: runner.os == 'Linux'
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install pip packages
if: runner.os == 'Linux'
shell: bash
run: |
pip install -U pip
pip install -U wheel setuptools
pip install -U meson ninja
- name: Build dav1d
if: runner.os == 'Linux'
env:
DAV1D_DIR: dav1d_dir
LIB_PATH: lib/x86_64-linux-gnu
shell: bash
run: |
git clone --branch 1.3.0 --depth 1 https://code.videolan.org/videolan/dav1d.git
cd dav1d
meson build -Dprefix=$HOME/$DAV1D_DIR -Denable_tools=false -Denable_examples=false --buildtype release
ninja -C build
ninja -C build install
echo "PKG_CONFIG_PATH=$HOME/$DAV1D_DIR/$LIB_PATH/pkgconfig" >> $GITHUB_ENV
echo "LD_LIBRARY_PATH=$HOME/$DAV1D_DIR/$LIB_PATH" >> $GITHUB_ENV
# Windows setup
- name: Install nasm
if: runner.os == 'Windows'
uses: ilammy/setup-nasm@v1

- name: Install Python 3.9
if: runner.os == 'Windows'
uses: actions/setup-python@v4
with:
python-version: '3.9'

- name: Install pip packages
if: runner.os == 'Windows'
shell: powershell
run: |
pip install -U pip
pip install -U wheel setuptools
pip install -U meson ninja
- name: Setting up environment
if: runner.os == 'Windows'
shell: bash
run: |
echo "PKG_CONFIG=c:\build\bin\pkg-config.exe" >> $GITHUB_ENV
echo "PKG_CONFIG_PATH=C:\build\lib\pkgconfig" >> $GITHUB_ENV
echo "C:\build\bin" >> $GITHUB_PATH
- name: Build pkg-config
if: runner.os == 'Windows'
shell: powershell
run: |
git clone --branch meson-glib-subproject --depth 1 https://gitlab.freedesktop.org/tpm/pkg-config.git
cd pkg-config
meson build -Dprefix=C:\build --buildtype release
ninja -C build
ninja -C build install
- name: Build dav1d
if: runner.os == 'Windows'
shell: powershell
run: |
git clone --branch 1.3.0 --depth 1 https://code.videolan.org/videolan/dav1d.git
cd dav1d
meson build -Dprefix=C:\build -Denable_tools=false -Denable_examples=false --buildtype release
ninja -C build
ninja -C build install
3 changes: 3 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup dav1d
uses: ./.github/actions/setup-dav1d

- name: Setup rust
uses: ./.github/actions/setup-rust

Expand Down
3 changes: 2 additions & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ email = { path = "../crates/email" }
epub = { git = "https://github.com/stumpapp/epub-rs", rev = "38e091abe96875952556ab7dec195022d0230e14" }
futures = { workspace = true }
globset = "0.4.14"
image = "0.25.2"
image = {version = "0.25.2", features = ["avif-native"]}
infer = "0.16.0"
itertools = "0.12.1"
prisma-client-rust = { workspace = true }
Expand Down Expand Up @@ -53,6 +53,7 @@ criterion = { version = "0.5.1", features = ["html_reports", "async_tokio"] }

[build-dependencies]
chrono = "0.4.37"
system-deps = "7.0.1"

[target.'cfg(target_os = "linux")'.dependencies]
libc = "0.2.152"
Expand Down
3 changes: 2 additions & 1 deletion core/src/filesystem/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ use walkdir::WalkDir;

use super::{media::is_accepted_cover_name, ContentType, FileError};

pub const ACCEPTED_IMAGE_EXTENSIONS: [&str; 5] = ["jpg", "png", "jpeg", "webp", "gif"];
pub const ACCEPTED_IMAGE_EXTENSIONS: [&str; 6] =
["jpg", "png", "jpeg", "webp", "avif", "gif"];

pub fn read_entire_file<P: AsRef<Path>>(path: P) -> Result<Vec<u8>, FileError> {
let mut file = File::open(path)?;
Expand Down
7 changes: 7 additions & 0 deletions core/src/filesystem/content_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub enum ContentType {
PNG,
JPEG,
WEBP,
AVIF,
GIF,
TXT,
#[default]
Expand Down Expand Up @@ -79,6 +80,7 @@ impl ContentType {
"jpg" => ContentType::JPEG,
"jpeg" => ContentType::JPEG,
"webp" => ContentType::WEBP,
"avif" => ContentType::AVIF,
"gif" => ContentType::GIF,
"txt" => ContentType::TXT,
_ => temporary_content_workarounds(extension),
Expand Down Expand Up @@ -265,6 +267,7 @@ impl ContentType {
ContentType::PNG => "png",
ContentType::JPEG => "jpg",
ContentType::WEBP => "webp",
ContentType::AVIF => "avif",
ContentType::GIF => "gif",
ContentType::TXT => "txt",
ContentType::UNKNOWN => "",
Expand All @@ -291,6 +294,7 @@ impl From<&str> for ContentType {
"image/png" => ContentType::PNG,
"image/jpeg" => ContentType::JPEG,
"image/webp" => ContentType::WEBP,
"image/avif" => ContentType::AVIF,
"image/gif" => ContentType::GIF,
_ => ContentType::UNKNOWN,
}
Expand All @@ -312,6 +316,7 @@ impl std::fmt::Display for ContentType {
ContentType::PNG => write!(f, "image/png"),
ContentType::JPEG => write!(f, "image/jpeg"),
ContentType::WEBP => write!(f, "image/webp"),
ContentType::AVIF => write!(f, "image/avif"),
ContentType::GIF => write!(f, "image/gif"),
ContentType::TXT => write!(f, "text/plain"),
ContentType::UNKNOWN => write!(f, "unknown"),
Expand All @@ -327,6 +332,7 @@ impl From<ImageFormat> for ContentType {
// ImageFormat::JpegXl => ContentType::JPEG,
ImageFormat::Png => ContentType::PNG,
ImageFormat::Webp => ContentType::WEBP,
ImageFormat::Avif => ContentType::AVIF,
}
}
}
Expand All @@ -349,6 +355,7 @@ impl TryFrom<ContentType> for image::ImageFormat {
ContentType::PNG => Ok(image::ImageFormat::Png),
ContentType::JPEG => Ok(image::ImageFormat::Jpeg),
ContentType::WEBP => Ok(image::ImageFormat::WebP),
ContentType::AVIF => Ok(image::ImageFormat::Avif),
ContentType::GIF => Ok(image::ImageFormat::Gif),
ContentType::XHTML => Err(unsupported_error("ContentType::XHTML")),
ContentType::XML => Err(unsupported_error("ContentType::XML")),
Expand Down
Loading

0 comments on commit 09ca1a9

Please sign in to comment.