Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional AVIF support #116

Merged
merged 5 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 26 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ on:
jobs:

rustfmt:
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: rustfmt
run: cargo fmt --all -- --check

clippy:
needs: rustfmt
runs-on: ubuntu-latest
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2
- name: clippy
Expand All @@ -28,8 +28,28 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-20.04, macos-latest, windows-latest]
steps:
- uses: actions/checkout@v2
- name: Build and run tests
run: env RUSTFLAGS="-C opt-level=0" cargo test --verbose --all-features
- name: Checkout
uses: actions/checkout@v2

- name: Install dependencies (linux)
if: matrix.os == 'ubuntu-20.04'
run: |
DEBIAN_FRONTEND=noninteractive sudo apt-get update
DEBIAN_FRONTEND=noninteractive sudo apt-get install -y ninja-build nasm meson

- name: Install dependencies (macOS)
if: matrix.os == 'macOS-latest'
run: |
brew install ninja nasm meson

- name: Build and run tests (linux/macOS)
if: matrix.os != 'windows-latest'
run: |
env RUSTFLAGS="-C opt-level=0" cargo test --verbose --all-features

- name: Build and run tests (windows)
if: matrix.os == 'windows-latest'
run: |
env RUSTFLAGS="-C opt-level=0" cargo test --verbose --features=networking
39 changes: 39 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ lto = true
[features]
default = []
networking = ["ureq"]
avif = ["libavif-image"]

[package.metadata.bundle]
name = "Emulsion"
Expand Down Expand Up @@ -53,3 +54,9 @@ rand = "0.7"
lexical-sort = "0.3"
trash = "1.0"
clap = { version = "2.33", default-features = false }

[dependencies.libavif-image]
version = "0.2"
default-features = false
features = ["codec-dav1d"]
optional = true
68 changes: 49 additions & 19 deletions src/image_cache/image_loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,33 @@ use self::errors::*;
pub static FOCUSED_REQUEST_ID: AtomicU32 = AtomicU32::new(0); // The first request usually
pub const NO_FOCUSED_REQUEST: u32 = std::u32::MAX;

pub enum ImgFormat {
Image(ImageFormat),
#[cfg(feature = "avif")]
Avif,
}

/// Detects the format of an image file. It looks at the first 512 bytes;
/// if that fails, it uses the file ending.
pub fn detect_format(path: &Path) -> Result<ImageFormat> {
pub fn detect_format(path: &Path) -> Result<ImgFormat> {
let mut file = fs::File::open(path)?;
let mut file_start_bytes = [0; 512];

// Try to detect the format from the first 512 bytes
if file.read_exact(&mut file_start_bytes).is_ok() {
#[cfg(feature = "avif")]
{
if libavif_image::is_avif(&file_start_bytes) {
return Ok(ImgFormat::Avif);
}
}
if let Ok(format) = image::guess_format(&file_start_bytes) {
return Ok(format);
return Ok(ImgFormat::Image(format));
}
}

// If that didn't work, try to detect the format from the file ending
Ok(ImageFormat::from_path(path)?)
Ok(ImgFormat::Image(ImageFormat::from_path(path)?))
}

pub fn load_image(path: &Path, image_format: ImageFormat) -> Result<image::RgbaImage> {
Expand Down Expand Up @@ -113,6 +125,8 @@ pub fn is_file_supported(filename: &Path) -> bool {
| "bmp" | "ico" | "hdr" | "pbm" | "pam" | "ppm" | "pgm" => {
return true;
}
#[cfg(feature = "avif")]
"avif" => return true,
_ => (),
}
}
Expand Down Expand Up @@ -221,29 +235,45 @@ impl ImageLoader {
let image_format = detect_format(&request.path)?;
img_sender.send(LoadResult::Start { req_id: request.req_id, metadata }).unwrap();

if image_format == ImageFormat::Gif {
let frames = load_gif(&request.path, request.req_id)?;
for frame in frames {
img_sender.send(frame?).unwrap();
}
} else if image_format == ImageFormat::Png {
let file = fs::File::open(&request.path)?;
let decoder = PngDecoder::new(file)?;
if decoder.is_apng() {
for frame in load_animation(request.req_id, decoder.apng())? {
match image_format {
ImgFormat::Image(ImageFormat::Gif) => {
let frames = load_gif(&request.path, request.req_id)?;
for frame in frames {
img_sender.send(frame?).unwrap();
}
} else {
}
ImgFormat::Image(ImageFormat::Png) => {
let file = fs::File::open(&request.path)?;
let decoder = PngDecoder::new(file)?;
if decoder.is_apng() {
for frame in load_animation(request.req_id, decoder.apng())? {
img_sender.send(frame?).unwrap();
}
} else {
let image = load_image(&request.path, ImageFormat::Png)?;
img_sender
.send(LoadResult::Frame {
req_id: request.req_id,
image,
delay_nano: 0,
})
.unwrap();
}
}
ImgFormat::Image(image_format) => {
let image = load_image(&request.path, image_format)?;
img_sender
.send(LoadResult::Frame { req_id: request.req_id, image, delay_nano: 0 })
.unwrap();
}
} else {
let image = load_image(&request.path, image_format)?;
img_sender
.send(LoadResult::Frame { req_id: request.req_id, image, delay_nano: 0 })
.unwrap();
#[cfg(feature = "avif")]
ImgFormat::Avif => {
let buf = fs::read(&request.path)?;
let image = libavif_image::read(&buf)?.to_rgba();
img_sender
.send(LoadResult::Frame { req_id: request.req_id, image, delay_nano: 0 })
.unwrap();
}
}
Ok(())
}
Expand Down