From 002b7ed7ff155af55da44f231a7325cba6dd5d68 Mon Sep 17 00:00:00 2001 From: Christopher Serr Date: Sat, 11 Apr 2020 21:30:33 +0200 Subject: [PATCH] Stop shrinking GIFs for now Shrinking GIFs to PNGs removes all their animations, so optimally we'd like to shrink them to GIFs and retain their animation. However it looks like the image crate is brushing over too many properties of GIFs. This makes it infeasible to do this without breaking GIFs. We could eventually look into using the underlying gif crate directly. For now we'll just not shrink GIFs at all. --- Cargo.toml | 2 +- src/settings/image/shrinking.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index ca045d9f..c3dbbfcf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ tokio = { version = "0.2", features = ["macros"] } default = ["image-shrinking", "std"] doesnt-have-atomics = [] std = ["byteorder", "chrono/std", "chrono/clock", "image", "indexmap", "livesplit-hotkey/std", "palette/std", "parking_lot", "quick-xml", "serde_json", "serde/std", "snafu/std", "utf-8"] -more-image-formats = ["image/webp", "image/pnm", "image/ico", "image/jpeg", "image/gif", "image/tiff", "image/tga", "image/bmp", "image/hdr"] +more-image-formats = ["image/webp", "image/pnm", "image/ico", "image/jpeg", "image/tiff", "image/tga", "image/bmp", "image/hdr"] image-shrinking = ["std", "bytemuck", "more-image-formats"] rendering = ["std", "more-image-formats", "euclid", "lyon", "rusttype", "smallvec"] software-rendering = ["rendering", "euc", "vek", "derive_more/mul"] diff --git a/src/settings/image/shrinking.rs b/src/settings/image/shrinking.rs index ecb2ed42..ed3e5d21 100644 --- a/src/settings/image/shrinking.rs +++ b/src/settings/image/shrinking.rs @@ -1,6 +1,6 @@ use alloc::borrow::Cow; use image::{ - bmp, gif, guess_format, hdr, ico, jpeg, load_from_memory_with_format, png, pnm, tiff, webp, + bmp, guess_format, hdr, ico, jpeg, load_from_memory_with_format, png, pnm, tiff, webp, DynamicImage, GenericImageView, ImageDecoder, ImageError, ImageFormat, }; use std::io::Cursor; @@ -12,17 +12,21 @@ fn shrink_inner(data: &[u8], max_dim: u32) -> Result, ImageError> let (width, height) = match format { ImageFormat::Png => png::PngDecoder::new(cursor)?.dimensions(), ImageFormat::Jpeg => jpeg::JpegDecoder::new(cursor)?.dimensions(), - ImageFormat::Gif => gif::GifDecoder::new(cursor)?.dimensions(), ImageFormat::WebP => webp::WebPDecoder::new(cursor)?.dimensions(), ImageFormat::Tiff => tiff::TiffDecoder::new(cursor)?.dimensions(), ImageFormat::Bmp => bmp::BmpDecoder::new(cursor)?.dimensions(), ImageFormat::Ico => ico::IcoDecoder::new(cursor)?.dimensions(), ImageFormat::Hdr => hdr::HDRAdapter::new(cursor)?.dimensions(), ImageFormat::Pnm => pnm::PnmDecoder::new(cursor)?.dimensions(), + // FIXME: For GIF we would need to properly shrink the whole animation. + // The image crate can't properly handle this at this point in time. + // Some properties are not translated over properly it seems. We could + // shrink GIFs that are a single frame, but we also can't tell how many + // frames there are. // TGA doesn't have a Header. // DDS isn't a format we really care for. // And the image format is non-exhaustive. - ImageFormat::Tga | ImageFormat::Dds | _ => return Ok(data.into()), + ImageFormat::Gif | ImageFormat::Tga | ImageFormat::Dds | _ => return Ok(data.into()), }; let is_too_large = width > max_dim || height > max_dim;