Skip to content

Commit

Permalink
AVIF: Wire up the decoder.
Browse files Browse the repository at this point in the history
  • Loading branch information
linkmauve committed Jan 27, 2021
1 parent 7a9df7a commit d63ad64
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ quickcheck = "0.9"
criterion = "0.3"

[features]
# TODO: Add "avif" to this list while preparing for 0.24.0
default = ["gif", "jpeg", "ico", "png", "pnm", "tga", "tiff", "webp", "bmp", "hdr", "dxt", "dds", "farbfeld", "jpeg_rayon"]

ico = ["bmp", "png"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ All image processing functions provided operate on types that implement the `Gen
| ICO | Yes | Yes |
| TIFF | Baseline(no fax support) + LZW + PackBits | RGB(8), RGBA(8), Gray(8) |
| WebP | Lossy(Luma channel only) | No |
| AVIF | No | Lossy |
| AVIF | Only 8-bit | Lossy |
| PNM | PBM, PGM, PPM, standard PAM | Yes |
| DDS | DXT1, DXT3, DXT5 | No |
| TGA | Yes | RGB(8), RGBA(8), BGR(8), BGRA(8), Gray(8), GrayA(8) |
Expand Down
8 changes: 5 additions & 3 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ impl ImageFormat {
let ext = ext.to_str()?.to_ascii_lowercase();

Some(match ext.as_str() {
"avif" => ImageFormat::Avif,
"jpg" | "jpeg" => ImageFormat::Jpeg,
"png" => ImageFormat::Png,
"gif" => ImageFormat::Gif,
Expand Down Expand Up @@ -150,7 +151,7 @@ impl ImageFormat {
ImageFormat::Hdr => true,
ImageFormat::Pnm => true,
ImageFormat::Farbfeld => true,
ImageFormat::Avif => false,
ImageFormat::Avif => true,
ImageFormat::__NonExhaustive(marker) => match marker._private {},
}
}
Expand Down Expand Up @@ -201,7 +202,7 @@ impl ImageFormat {
ImageFormat::Hdr => &["hdr"],
ImageFormat::Farbfeld => &["ff"],
// According to: https://aomediacodec.github.io/av1-avif/#mime-registration
ImageFormat::Avif => &["avif", "heif", "heifs", "hif"],
ImageFormat::Avif => &["avif"],
ImageFormat::__NonExhaustive(marker) => match marker._private {},
}
}
Expand Down Expand Up @@ -1203,6 +1204,7 @@ mod tests {
assert_eq!(from_path("./a.pAM").unwrap(), ImageFormat::Pnm);
assert_eq!(from_path("./a.Ppm").unwrap(), ImageFormat::Pnm);
assert_eq!(from_path("./a.pgm").unwrap(), ImageFormat::Pnm);
assert_eq!(from_path("./a.AViF").unwrap(), ImageFormat::Avif);
assert!(from_path("./a.txt").is_err());
assert!(from_path("./a").is_err());
}
Expand Down Expand Up @@ -1298,7 +1300,7 @@ mod tests {
#[test]
fn image_formats_are_recognized() {
use ImageFormat::*;
const ALL_FORMATS: &'static [ImageFormat] = &[Png, Jpeg, Gif, WebP, Pnm, Tiff, Tga, Dds, Bmp, Ico, Hdr, Farbfeld];
const ALL_FORMATS: &'static [ImageFormat] = &[Avif, Png, Jpeg, Gif, WebP, Pnm, Tiff, Tga, Dds, Bmp, Ico, Hdr, Farbfeld];
for &format in ALL_FORMATS {
let mut file = Path::new("file.nothing").to_owned();
for ext in format.extensions_str() {
Expand Down
9 changes: 7 additions & 2 deletions src/io/free_functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use crate::codecs::webp;
#[cfg(feature = "farbfeld")]
use crate::codecs::farbfeld;
#[cfg(feature = "avif")]
use crate::avif;
use crate::codecs::avif;

use crate::color;
use crate::image;
Expand Down Expand Up @@ -59,6 +59,8 @@ pub fn load<R: BufRead + Seek>(r: R, format: ImageFormat) -> ImageResult<Dynamic
#[allow(unreachable_patterns)]
// Default is unreachable if all features are supported.
match format {
#[cfg(feature = "avif")]
image::ImageFormat::Avif => DynamicImage::from_decoder(avif::AvifDecoder::new(r)?),
#[cfg(feature = "png")]
image::ImageFormat::Png => DynamicImage::from_decoder(png::PngDecoder::new(r)?),
#[cfg(feature = "gif")]
Expand Down Expand Up @@ -105,6 +107,8 @@ pub(crate) fn image_dimensions_with_format_impl<R: BufRead + Seek>(fin: R, forma
// Default is unreachable if all features are supported.
// Code after the match is unreachable if none are.
Ok(match format {
#[cfg(feature = "avif")]
image::ImageFormat::Avif => avif::AvifDecoder::new(fin)?.dimensions(),
#[cfg(feature = "jpeg")]
image::ImageFormat::Jpeg => jpeg::JpegDecoder::new(fin)?.dimensions(),
#[cfg(feature = "png")]
Expand Down Expand Up @@ -204,7 +208,7 @@ pub(crate) fn save_buffer_with_format_impl(
}
}

static MAGIC_BYTES: [(&[u8], ImageFormat); 19] = [
static MAGIC_BYTES: [(&[u8], ImageFormat); 20] = [
(b"\x89PNG\r\n\x1a\n", ImageFormat::Png),
(&[0xff, 0xd8, 0xff], ImageFormat::Jpeg),
(b"GIF89a", ImageFormat::Gif),
Expand All @@ -224,6 +228,7 @@ static MAGIC_BYTES: [(&[u8], ImageFormat); 19] = [
(b"P6", ImageFormat::Pnm),
(b"P7", ImageFormat::Pnm),
(b"farbfeld", ImageFormat::Farbfeld),
(b"\0\0\0 ftypavif", ImageFormat::Avif),
];

/// Guess image format from memory block
Expand Down

0 comments on commit d63ad64

Please sign in to comment.