image-rs / image

Encoding and decoding images in Rust
Apache License 2.0
5.13k stars 628 forks source link

Please support loading paletted images with a paletted in-memory format. #2391

Open Lokathor opened 1 month ago

Lokathor commented 1 month ago

I would like to be able to load a paletted image format (png, bmp, gif, etc) and retain the palette and palette index values as separate elements of the image.

My specific use case for this functionality is loading paletted images and re-packing them for use as data declarations in assembly code.

Imagine an image that's 4 pixels wide and 1 pixel tall, with indexes 1,1,3,2. After processing, the image would turn into assembly code like this (note: % indicates a binary literal in this assembly language)

db %01011110

This is more generally applicable to any application working with paletted data that would like to load a paletted image ans manipulate it directly, instead of getting an RGBA image and having to re-quantize all the values. For example, an application that wants to check if two images have compatible palettes.

Draft

A new variant can be added to the DynamicImage enum, ImagePaletteRgb8 is a suggestion for the variant name.

kornelski commented 1 month ago

If you make palette entries Rgba, then the encoder can handle tRNS for you.

fintelia commented 1 month ago

I think this would be a better fit for the low-level ImageDecoder interface. That trait could gain additional methods to extract the palette and to request that read_image return the raw palette indexes, rather than the pixel values themselves.

Trying to make DynamicImage support paletted data would mean we'd have to implement rotation, scaling, and such for paletted images. And would be another variant that potentially all users would have to worry about, rather than being transparently handled like it is now.

Lokathor commented 1 month ago

That would be acceptable. I don't need to apply image transformations, so more simple support for format detection and decoding to a provided buffer would be sufficient.