Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

fix(io): an indexed GIF opens in Indexed Color with its colour table (#7886)

Korg1ch04a2169b447f
diff --git a/crates/codecs/src/codecs/gif_palette.rs b/crates/codecs/src/codecs/gif_palette.rs
new file mode 100644
index 000000000..aa71f0237
--- /dev/null
+++ b/crates/codecs/src/codecs/gif_palette.rs
@@ -0,0 +1,64 @@
+//! The colour table of a GIF, read from its blocks without decoding pixels (GIF89a
+//! specification: Logical Screen Descriptor, Global and Local Color Tables, Graphic Control
+//! Extension).
+//!
+//! A GIF is always palette-based, so it opens in Indexed Color with the table of the frame that
+//! [`crate::decode`] keeps, the first: its Local Color Table when it has one, else the Global
+//! one, and the transparent entry that frame's Graphic Control Extension names.
+
+/// Colours of the table the first frame uses, and its transparent entry. `None` for anything
+/// that isn't a well-formed GIF up to its first image, or a first frame without a table.
+pub(crate) fn palette(b: &[u8]) -> Option<(Vec<[u8; 3]>, Option<u8>)> {
+ if !(b.starts_with(b"GIF87a") || b.starts_with(b"GIF89a")) {
+ return None;
+ }
+ // Logical Screen Descriptor: width, height, packed fields, background, aspect.
+ let packed = *b.get(10)?;
+ let mut at = 13;
+ let table = |at: usize, packed: u8| -> Option<Vec<[u8; 3]>> {
+ let n = 2usize << (packed & 0x07);
+ let bytes = b.get(at..at.checked_add(n.checked_mul(3)?)?)?;
+ Some(bytes.as_chunks::<3>().0.to_vec())
+ };
+ let global = if packed & 0x80 != 0 {
+ let t = table(at, packed)?;
+ at += t.len() * 3;
+ Some(t)
+ } else {
+ None
+ };
+ let mut transparent = None;
+ loop {
+ match *b.get(at)? {
+ // Extension: label, then data sub-blocks up to a zero-length one.
+ 0x21 => {
+ let label = *b.get(at + 1)?;
+ at += 2;
+ // Graphic Control Extension: block size 4, packed (bit 0: transparent colour
+ // given), delay (2 bytes), transparent colour index.
+ if label == 0xF9 && *b.get(at)? == 4 {
+ let flags = *b.get(at + 1)?;
+ transparent = (flags & 0x01 != 0).then_some(*b.get(at + 4)?);
+ }
+ loop {
+ let size = usize::from(*b.get(at)?);
+ at = at.checked_add(1 + size)?;
+ if size == 0 {
+ break;
+ }
+ }
+ }
+ // Image Descriptor: position and size (8 bytes), packed fields, then the Local Color
+ // Table if bit 7 is set. This is the first frame: its table is the one used.
+ 0x2C => {
+ let packed = *b.get(at + 9)?;
+ let colors = if packed & 0x80 != 0 { table(at + 10, packed)? } else { global? };
+ let transparent = transparent.filter(|i| usize::from(*i) < colors.len());
+ return Some((colors, transparent));
+ }
+ // Trailer, or anything else: no image.
+ _ => return None,
+ }
+ }
+}
+
diff --git a/crates/codecs/src/codecs/mod.rs b/crates/codecs/src/codecs/mod.rs
index 32e68b336..41e1e89a1 100644
--- a/crates/codecs/src/codecs/mod.rs
+++ b/crates/codecs/src/codecs/mod.rs
@@ -5,6 +5,7 @@ pub(crate) mod exr;
pub(crate) mod exr_cryptomatte;
pub(crate) mod fits;
pub(crate) mod gif_frames;
+pub(crate) mod gif_palette;
pub(crate) mod heif;
pub(crate) mod jpeg;
pub(crate) mod pcx;
diff --git a/crates/codecs/src/lib.rs b/crates/codecs/src/lib.rs
index 67369d026..0966fba07 100644
--- a/crates/codecs/src/lib.rs
+++ b/crates/codecs/src/lib.rs
@@ -126,6 +126,12 @@ pub fn probe_size(bytes: &[u8]) -> Result<(u32, u32), CodecError> {
Ok(if (5..=8).contains(&o) { (h, w) } else { (w, h) })
}
+/// The colour table of a GIF's first frame (its Local Color Table, else the Global one) and the
+/// entry its Graphic Control Extension makes transparent, read without decoding pixels.
+pub fn gif_palette(bytes: &[u8]) -> Option<(Vec<[u8; 3]>, Option<u8>)> {
+ crate::codecs::gif_palette::palette(bytes)
+}
+
/// Returns the embedded colour table for an indexed BMP, if it has one.
pub fn bmp_palette(bytes: &[u8]) -> Result<Option<Vec<[u8; 3]>>, CodecError> {
via_image::bmp_palette(bytes)
diff --git a/crates/io/src/flat.rs b/crates/io/src/flat.rs
index 39fd2dc63..c9e3a410a 100644
--- a/crates/io/src/flat.rs
+++ b/crates/io/src/flat.rs
@@ -22,11 +22,12 @@ pub fn import_flat(name: &str, bytes: &[u8]) -> Result<ImportResult, IoError> {
return import_tiff_page(name, bytes, None);
}
let img = codecs::decode_as(format.ok_or(codecs::CodecError::UnknownFormat)?, bytes)?;
- // A palette BMP or PNG opens in Indexed Color mode with its colour table, as in Photoshop
- // (#3802, #4804).
+ // A palette BMP, PNG or GIF opens in Indexed Color mode with its colour table, as in
+ // Photoshop (#3802, #4804, #7886). A GIF is always palette-based: the first frame's table.
let palette = match format {
Some(Format::Bmp) => codecs::bmp_palette(bytes).ok().flatten().map(|c| (c, None)),
Some(Format::Png) => codecs::png_palette(bytes).ok().flatten().map(|p| (p.colors, p.transparent)),
+ Some(Format::Gif) => codecs::gif_palette(bytes),
_ => None,
};
let mut r = image_to_document(name, &img)?;