Draft: Add SGI Image (.rgb) decoder #5
+3,610
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 draftregister_decoding_hook
andregister_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.