mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Saving an Indexed Color document as GIF went through the generic image encoder, which rebuilt the colour table from the colours present in the image. The entries no pixel used were dropped, the survivors changed index and the transparent index moved, so a pixel-art palette did not survive a save even though the picture looked the same. PNG and PSD already wrote the document's own table. Take the GIF save through export_mode_specific next to the palette PNG and BMP, which have the document and its colour table in hand, and encode with the codec's existing indexed GIF writer. Indices come from the document's table, so they pass through unchanged rather than being nearest-colour remapped into a new one. A GIF colour table holds 2^n entries: the writer pads the slots past the document's last entry with black, the way the palette BMP's table is padded, so no used index moves. An RGB-mode document still takes the quantizing path, and an indexed document with no usable table falls back to it rather than failing the save. The indexed route encodes from the document instead of through encode_image, so it now reports the metadata a GIF cannot hold in the words the generic encoder's fidelity warnings use. Tests (crates/io/tests/it/modes.rs): unused entries survive a save and reopening; a 200-entry palette lands in a 256-entry table with its indices intact; every pixel keeps its index at 8/16/32 bits; the transparent index is kept; a saved GIF round-trips unchanged; the metadata warnings; an RGB GIF still quantizes; no usable table falls back; a table with no opaque entry errors instead of writing a blank.