Skip to content

Conversation

mstoeckl
Copy link

@mstoeckl mstoeckl commented Jul 28, 2025

This adds a decoder for SGI .rgb files -- of the formats that I've run into in the past which are not already decodable by image, it's perhaps the third most common, after XPM and XBM.

The implementation is a bit awkward, having been written with a goal of minimizing worst-case performance / ensuring there are no images that are unusually expensive to parse. The RLE compressed storage mode for RGB images includes a table giving the start positions of each row; the rows themselves can be placed in arbitrary order, and even overlapping, in the image data region of the file. Normally parsers would handle this by seeking to the start of each row, which can be surprisingly expensive (on a specifically constructed 2MB image, ImageMagick takes a second to read the file, with most of the time spent handling the seeks in the kernel). If I understand things correctly, things might be worse with BufReader, as it may always try to fill the entire 8KB buffer on every read after a seek, even if only a few bytes were requested. I've therefore tried to avoid seeking entirely by reading the file in a single pass, and decoding rows as they go by.

This format has a large number of possible extensions, with the most common one, .rgb, also being used by a few rarer image formats and sometimes for raw image data. The current draft register_decoding_hook and register_format_detection_hook system gets a bit repetitive to handle all the variants, but that (and the OsString + Box allocations) appear to be the cost of flexibility and generality. This is also an unusual format by the number of reported extensions.

All test images were made by me and are, like the code, offered under the image-extras project's license, MIT OR Apache-2.0.

To make development a bit simpler, the changes have placed on top of the XBM/XPM PR; I plan to drop the unrelated commits once this draft stabilizes.

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

Successfully merging this pull request may close these issues.

1 participant