Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
Lokathor opened this issue Dec 14, 2024 · 3 comments
Open

Comments

@Lokathor
Copy link

Lokathor commented Dec 14, 2024

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.

  • The image data itself would be a 2d buffer of u8 values, and the palette would be a Vec<Rgb<u8>> holding the color value for each index.
  • Even images with less than 8 bits per pixel use a full u8 per pixel when the data is decoded into memory. The realities of rust make working with sub-byte data excessively annoying. If an image has a sufficiently small palette the data could be packed down into fewer bits per pixel when encoding the data to a particular format.
  • Supporting a separate variant that's Rgba is probably not necessary. Similarity, supporting palettes of more than 256 colors is probably not necessary. They can be added in the future if the need arises.
@kornelski
Copy link
Contributor

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

@fintelia
Copy link
Contributor

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants