Split PNM ImageFormat into subformats for PBM, PGM, etc.#2841
Open
mstoeckl wants to merge 3 commits intoimage-rs:mainfrom
Open
Split PNM ImageFormat into subformats for PBM, PGM, etc.#2841mstoeckl wants to merge 3 commits intoimage-rs:mainfrom
mstoeckl wants to merge 3 commits intoimage-rs:mainfrom
Conversation
The test images are easier to compare for correctness with other decoders. Note that some test images have been modified to have unusual but valid headers and whitespace.
The new ImageFormats ensure that methods like DynamicImage::save only write image data which is compatible with the file extension. (Previously PAM image data was written for any path with a NetPBM extension, producing images that some other programs couldn't read.) The PnmDecoder continues to accept any NetPBM format. This commit also fixes a few match expression cases and parameterizes error messages.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 PR tries to fix #2810.
It splits ImageFormat::Pnm into five different formats, so that each of the NetPBM formats has its own enum entry. This makes it possible for methods like DynamicImage::save to set the exact format based on the file extension, so that saving to a ".pgm" file will only try to save a PGM image.
This behavioral change may surprise anyone who previously was writing Rgba8 images to a ".pnm" file (actually producing PAM); this will now fail because PNM does not support alpha.
Other than this change, I have tried to avoid changing the behavior of the PnmDecoder, so that anyone using it directly will continue to see the same output. The only addition is a
with_dynamic_pnm_header()method to allow autoselecting a PNM header.I've also added some test images that cover all the main netpbm format variations; some of them are also used as reference images for updated PNM encoding tests in
tests/save_pnm.rs.The test images were all made by me, and licensed Apache-2.0 OR MIT OR CC-0. I've checked their references against NetPBM's conversion (using
pamdepth 65535 <$in | pamtopng -verbose >$out.png; the test image maxvals are chosen to be exactly convertible to 16 bit without rounding.).