image-rs / image-png

PNG decoding and encoding library in pure Rust
https://docs.rs/png
Apache License 2.0
361 stars 142 forks source link

Gain map support #532

Open anforowicz opened 1 day ago

anforowicz commented 1 day ago

Let me try to start a future-looking discussion about support for gainmaps. I say “future-looking” because AFAIK:

But, despite the caveats above, I think it’s still desirable to start some early discussions on how Rust png crate API could potentially accommodate gain maps:

/cc @ccameron-chromium

fintelia commented 21 hours ago

The current API design automatically decodes all metadata in bulk (regardless of whether the user is going to want it) and provides a stateful API to access the image data in the order it appears in the file. Within those constraints, the design you're describing probably makes the most sense. The other option would be to treat the entire gain map as metadata to be held inside the Info struct, which doesn't seem ideal.

If we required a Seek bound, it would open up other options. For instance, adding a dedicated gain_map method that read and decoded the gain map on-demand. Or a method to seek to a given ImageDataKind, and then use the normal decoding methods to read it.

(I started #534 which discusses some other changes a Seek bound would enable)

kornelski commented 14 hours ago

How about returning a separate reader instance for reading a frame of the gain map?

The current reader would remain used exclusively for SDR pixels.

fn read_gain_map(&mut self) -> Option<GainMapReader<'_, R>>

This way the current reader wouldn't have stateful multiple purposes, and the sub-reader could expose all the individual methods and properties just for the gain map.