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

Add support for saving 16-bit images to pnm formats #2431

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

Conversation

Enet4
Copy link

@Enet4 Enet4 commented Feb 27, 2025

Should hopefully resolve #2430, as well as #1687. It takes the assumption that when an image is 16-bit and the encoder receives a &[u8], it means that the samples were in reality 16-bit and the slice was cast to a byte slice.

Summary

  • add sample adaptation layer which recovers 16-bit samples from the original image buffer depending on the color type provided
    • Once this is done, the number of samples should match the expected number of samples, and the rest of the encoding process should simply work
  • allow for RGB and RGB Alpha tuple types in header check
  • add test suite save_pnm.rs and test sample images from pngsuite:
    • basi0g16.png: 16-bit grayscale image
    • basn2c16.png: 16-bit RGB image

Known caveat

  • I am not entirely sure whether the assumption can be made in general, or whether it would be possible for someone to request 8-bit samples to be extended and encoded as 16-bit through the encoder.
  • While this should also enable the encode to write PAM files, AFAIK PAM file decoding is not yet supported in the library.

- add sample adaptation layer which
  recovers 16-bit samples from the original image buffer
  depending on the color type provided
- allow for RGB and RGB Alpha tuple types in header check
- add test suite save_pnm and test sample images from pngsuite:
   - basi0g16.png: grayscale 16-bit
   - basn2c16.png: rgb 16-bit
@Enet4 Enet4 marked this pull request as ready for review February 27, 2025 22:19
@fintelia
Copy link
Contributor

fintelia commented Mar 1, 2025

Thanks for this PR! I am in favor of adding 16-bit support here.

One concern is that the write_image should not require that users pass aligned slices. If the color type is a 16-bit per pixel format then the user is allowed to pass any native endian pixel data without worrying about alignment. One option to handle potentially misaligned inputs is to allocate a temporary Vec<u16> and copy the provided data into it. That adds some memory overhead, but should be pretty fast to implement.

@Enet4
Copy link
Author

Enet4 commented Mar 1, 2025

One option to handle potentially misaligned inputs is to allocate a temporary Vec<u16> and copy the provided data into it. That adds some memory overhead, but should be pretty fast to implement.

I'll see what I can do. The encoder implementation cannot switch the lifetime of the samples midway, so the best place to re-adapt the byte buffer (with an allocation if necessary) would be as early as possible in the impl.

- move wide sample reinterpretation to the public function call
- reallocate image input if samples are not aligned properly
- refactor a bit of  `encode` into `encode_impl` to reduce repetition
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.

Cannot save 16-bit images directly to PNM via save
2 participants