image-rs / image-gif

GIF en- and decoder
Apache License 2.0
148 stars 42 forks source link

Allow skipping image decoding #144

Closed ns476 closed 9 months ago

ns476 commented 1 year ago

When dealing with very large GIFs it may be useful to count the number of frames without decoding. This may be useful when writing a utility like exiftool that only cares about the metadata, or if we want to cheaply reject GIFs that are over some size limit without paying for the full decode. The screen size of the GIF is included in the header, but there is no way to count the frames (and therefore the overall size) without iterating through the file.

This PR adds a flag to DecodeOptions that allows this.

sejoker commented 1 year ago

@HeroicKatora please have a look when you have time.

sejoker commented 1 year ago

cc @kornelski.

fintelia commented 1 year ago

I think this is good functionality to expose, but would prefer a different public API. Perhaps a next_frame_metadata_only that gets the frame metadata but discards the actually image contents without decompressing?

PS: in general there's no need to ping reviewers right when a PR's been created since we'll get a notification from the PR itself (if you don't get a response after ~1 week then it is reasonable to ping because the notification might have been missed)

sejoker commented 1 year ago

@fintelia noted, thanks for the review appreciate your feedback.

fintelia commented 1 year ago

next_frame_metadata_only would be instead of the skip_frame_decoding.

So you could for instance call next_frame_metadata_only and get the metadata for the first frame. If you then called read_next_frame it would succeed and return the contents of the second frame. This to me feels a bit less like "action at a distance"

kornelski commented 1 year ago

Could you keep the LZW data, compatible with the existing pre-encoded frame support, so that it's possible to also rewrite GIF files without decompression?

https://docs.rs/gif/latest/gif/struct.Encoder.html#method.write_lzw_pre_encoded_frame

kornelski commented 9 months ago

This is now implemented in #155