Skip to content

Commit

Permalink
Add support for GIF
Browse files Browse the repository at this point in the history
  • Loading branch information
LaurenzV committed Aug 30, 2024
1 parent 3599a86 commit 9febc8a
Show file tree
Hide file tree
Showing 21 changed files with 49 additions and 2 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ yoke = { workspace = true }
float-cmp = { workspace = true }
zune-png = "0.4.10"
zune-jpeg = "0.4.13"
gif = "0.13.1"

[dev-dependencies]
difference = { workspace = true }
Expand Down
Binary file added assets/images/rgb8.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/images/rgba8.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_ghostscript.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_mupdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_pdfbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_pdfium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_pdfjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_poppler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgb8_gif_quartz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_ghostscript.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_mupdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_pdfbox.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_pdfium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_pdfjs.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_poppler.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/refs/image_rgba8_gif_quartz.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 23 additions & 1 deletion src/object/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use crate::error::KrillaResult;
use crate::object::color_space::DEVICE_GRAY;
use crate::serialize::{FilterStream, Object, SerializerContext};
use crate::util::{NameExt, Prehashed, SizeWrapper};
use image::codecs::gif::GifDecoder;
use pdf_writer::{Chunk, Finish, Name, Ref};
use std::ops::DerefMut;
use std::sync::Arc;
Expand Down Expand Up @@ -111,6 +112,27 @@ impl Image {
}))))
}

pub fn from_gif(data: &[u8]) -> Option<Self> {
let mut decoder = gif::DecodeOptions::new();
decoder.set_color_output(gif::ColorOutput::RGBA);
let mut decoder = decoder.read_info(data).ok()?;
let first_frame = decoder.read_next_frame().ok()??;

let size = Size::from_wh(first_frame.width as f32, first_frame.height as f32)?;

let (image_data, mask_data, bits_per_component) =
handle_u8_image(first_frame.buffer.to_vec(), ColorSpace::RGBA);

Some(Self(Arc::new(Prehashed::new(Repr {
image_data,
mask_data,
is_dct_encoded: false,
bits_per_component,
image_color_space: ImageColorspace::Rgb,
size: SizeWrapper(size),
}))))
}

pub fn size(&self) -> Size {
self.0.size.0
}
Expand Down Expand Up @@ -142,7 +164,7 @@ impl Object for Image {

let image_stream = if self.0.is_dct_encoded {
FilterStream::new_from_dct_encoded(&self.0.image_data, &sc.serialize_settings)
} else {
} else {
FilterStream::new_from_binary_data(&self.0.image_data, &sc.serialize_settings)
};

Expand Down
4 changes: 4 additions & 0 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ pub fn load_jpg_image(name: &str) -> Image {
Image::from_jpeg(&std::fs::read(ASSETS_PATH.join("images").join(name)).unwrap()).unwrap()
}

pub fn load_gif_image(name: &str) -> Image {
Image::from_gif(&std::fs::read(ASSETS_PATH.join("images").join(name)).unwrap()).unwrap()
}

fn write_snapshot_to_store(name: &str, content: &[u8]) {
let mut path = STORE_PATH.clone().join("snapshots");
let _ = std::fs::create_dir_all(&path);
Expand Down
21 changes: 20 additions & 1 deletion src/tests/visreg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::rgb::Rgb;
use crate::stream::Glyph;
use crate::surface::Surface;
use crate::tests::manual::all_glyphs_to_pdf;
use crate::tests::{load_png_image, COLR_TEST_GLYPHS, NOTO_COLOR_EMOJI, NOTO_SANS, TWITTER_COLOR_EMOJI, load_jpg_image};
use crate::tests::{
load_gif_image, load_jpg_image, load_png_image, COLR_TEST_GLYPHS, NOTO_COLOR_EMOJI, NOTO_SANS,
TWITTER_COLOR_EMOJI,
};
use crate::util::SliceExt;
use crate::{rgb, Fill, LinearGradient, Paint, SpreadMethod, Stop};
use cosmic_text::{Attrs, Buffer, FontSystem, Metrics, Shaping};
Expand Down Expand Up @@ -150,6 +153,12 @@ fn jpg_image_impl(surface: &mut Surface, name: &str) {
surface.draw_image(image, size);
}

fn gif_image_impl(surface: &mut Surface, name: &str) {
let image = load_gif_image(name);
let size = image.size();
surface.draw_image(image, size);
}

#[visreg(all)]
fn image_luma8_png(surface: &mut Surface) {
png_image_impl(surface, "luma8.png");
Expand Down Expand Up @@ -189,3 +198,13 @@ fn image_luma8_jpg(surface: &mut Surface) {
fn image_rgb8_jpg(surface: &mut Surface) {
jpg_image_impl(surface, "rgb8.jpg");
}

#[visreg(all)]
fn image_rgb8_gif(surface: &mut Surface) {
gif_image_impl(surface, "rgb8.gif");
}

#[visreg(all)]
fn image_rgba8_gif(surface: &mut Surface) {
gif_image_impl(surface, "rgba8.gif");
}

0 comments on commit 9febc8a

Please sign in to comment.