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

pnm: Decode PAM format variants which have alpha #2432

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mstoeckl
Copy link
Contributor

@mstoeckl mstoeckl commented Mar 1, 2025

This implements the transparent tupltypes listed in the PAM spec, at https://netpbm.sourceforge.net/doc/pam.html.

  • I am submitting this PR to a) complete PAM format support so that image viewers based on image can read all
    reasonably constructable PAM images b) ensure that the image crate itself can read all the PAM tuple types that it
    writes.

  • The format technically allows non-RGBA tuple types and depths > 4, but these should be rejected as they
    have no clear interpretation as an image.

  • I've only added simple tests in-file for PAM, extending the existing ones, but if desired I can make regression test images.

  • Other than by using image itself, PAM images with alpha channels can be produced by NetPBM and ImageMagick.
    For example:

pngtopam -alphapam rgb_alpha.png > pam_rgb_alpha.pam
pngtopam -alphapam gray_alpha.png > pam_grayscale_alpha.pam
convert rgb_alpha.png pam_rgb_alpha.pam
convert gray_alpha.png pam_grayscale_alpha.pam
convert blackandwhite_alpha.png -depth 1 pam_blackandwhite_alpha.pam

rgb_alphagray_alphablackandwhite_alpha

(Note: pngtopam does not produce BLACKANDWHITE_ALPHA; pamthreshold can, but isn't general and appears to have bugs; so ImageMagick's convert is the only standard converter which works. On the other hand, going from PAM to PNG, ImageMagick's convert incorrectly flips the alpha channel, while pamtopng works correctly.)

  • I wasn't able to find detailed documentation on the ColorType memory layout, but assume, based on how the other encoders and decoders handle alpha, that pixel types like ColorType::Rgba16 are stored unpremultiplied, with native-endian channels in R,G,B,A memory order, and a linear alpha channel.

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