Skip to content

Draft: Add support for XBM and XPM #4

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

mstoeckl
Copy link

@mstoeckl mstoeckl commented Jul 13, 2025

This PR adds support for decoding the X BitMap and X PixMap formats. It would resolve image-rs/image#2410, and supersede image-rs/image#2416.

I'm marking it as draft for now since the main image crate has not yet finalized a decoding hook mechanism. Please let me know if you'd be interesting in merging either of these two formats any earlier, in which case I could split off/adjust this PR as needed.

Notes:

  • I've tried to structure the code in a way that should make it easier to adapt to use no_std in the future: the core image decoding logic uses Iterator<Item = u8>, and the ImageDecoder implementations are just wrappers under/over the core to handle IO errors and pixel output. Unfortunately this is rather verbose. The only memory allocations performed in the core logic are by the XPM decoder when creating a color name table.
  • XBM and XPM are text based image formats, so I've written the decoders to provide error messages indicating the line and column of parse errors, when possible. This does make the implementations a bit slower and more complicated -- but XBM/XPM are obsolete and very inefficient formats anyway, that were never really used for images much larger than icons, so there is little to gain in optimizing for speed over usability.
  • All test images have been created by me and are, like the code, offered under the image-extras project's license, MIT OR Apache-2.0.
  • Decoding existing images in the X PixMap format requires that one have a copy of the X11 color name table. For license isolation purposes (it has an X11 license, which is MIT + no advertising clause) I've put the color name table into a separate crate (x11r6colornames. I could also move it to a subfolder of the image-extras repository if that is more convenient.
  • To keep this easier to build / based on the main branch of the image repository, I've included a commit that drops the use of register_decoding_hook, which I plan to undo once decoding hook details are merged in some fashion in image. (Edit: register_decoding_hook now uses file extensions/signatures and is easy to use, commit removed.)

Let me know if you'd like any changes, additional tests, etc. (It may take me some time to respond.)

Copy link
Contributor

@fintelia fintelia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be fine to include the color names table as a file in this repository with a note at the top about the license. That would be simpler than requiring an external crate.

@@ -0,0 +1,10 @@
# Fuzzing with libfuzzer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having this also reference the note in the crate README.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (linked to the README and repeated part of the note).

Comment on lines 3 to 4
extern crate image_extras;
extern crate image;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These lines shouldn't be needed anymore

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed; also removed the unnecessary extern crate in fuzzer_script_xbm.rs and src/xpm.rs.

@mstoeckl
Copy link
Author

mstoeckl commented Jul 14, 2025

I think it would be fine to include the color names table as a file in this repository with a note at the top about the license. That would be simpler than requiring an external crate.

Licenses are also part of the Cargo.toml metadata, for which I am not aware of a way to express that the X11 license only applies to part of the library if the xpm feature is used. If we'd like to include it as a regular file, then I think image-extras would need to be labeled (MIT OR Apache-2.0) AND X11 even when the xpm feature is not used. To avoid this, I've instead incorporated the table as a crate image-x11r6colors in a subfolder, which unfortunately would require separate publishing to crates.io to implement. I don't think there are any good solutions here; let me know if you'd prefer incorporation as a regular file or a different approach.

Also: I've made a few other minor changes to module documentation in xpm.rs, and switched BufReader to the more natural Cursor in the fuzzer scripts. Edit 2025-07-15: made a few more documentation improvements, renamed image-x11r6colornames to image-x11r6colors, and added #![no_std] to image-x11r6colors/src/lib.rs.

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.

Add support for decoding XBM and XPM
2 participants