Skip to content

fix the pngsave bitdepth param #4516

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

Merged
merged 9 commits into from
May 17, 2025
Merged

fix the pngsave bitdepth param #4516

merged 9 commits into from
May 17, 2025

Conversation

jcupitt
Copy link
Member

@jcupitt jcupitt commented May 12, 2025

Saving an 8-bit image as 16-but PNG (and vice versa) didn't work correctly.

See #4512

Saving an 8-bit image as 16-but PNG (and vice versa) didn't work
correctly.

See #4512
@jcupitt
Copy link
Member Author

jcupitt commented May 12, 2025

I don't think any other savers have the same issue.

@jcupitt jcupitt marked this pull request as draft May 12, 2025 16:08
@jcupitt
Copy link
Member Author

jcupitt commented May 13, 2025

This is a bit more complicated than you'd think because colourspace("srgb") on a float image can give a float result, rather than forcing uint8.

For example:

$ vips colourspace k2.jpg x.v scrgb
$ vips colourspace x.v y.v srgb
$ vipsheader y.v
y.v: 1450x2048 uchar, 3 bands, srgb, jpegload

The float scrgb image becomes uint8 srgb, so precision is lost. But:

$ vips cast k2.jpg x.v double
$ vips colourspace x.v y.v srgb
$ vipsheader y.v
y.v: 1450x2048 double, 3 bands, srgb, jpegload

Now it stays float.

This seems unpredictable and is probably a bad idea. What do you think? Should colourspace conversion to srgb always output uint8, and conversion to rgb16 always output ushort?

There's a potential loss of precision, but if you want high precision srgb you should probably be using scrgb.

jcupitt added 4 commits May 14, 2025 16:14
it now enforces the format, so srgb -> srgb conversion (for example)
will make a uchar image
and revise coments a bit
@jcupitt jcupitt marked this pull request as ready for review May 15, 2025 03:26
@jcupitt
Copy link
Member Author

jcupitt commented May 15, 2025

  1. colourspace now always outputs the expected format for each colourspace, so sRGB is always uint8, XYZ is always float, and so on. I also sorted the colourspace table so it uses the same ordering everywhere, hopefully it's a bit easier to read now.
  2. The PNG savers let you use the bitdepth arg to select rgb16 output, though I'm not sure how useful this is since it's just an 8-bit image shifted up.

Copy link
Member

@kleisauke kleisauke left a comment

Choose a reason for hiding this comment

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

Nice!

@jcupitt jcupitt merged commit 9f590ce into master May 17, 2025
14 checks passed
kleisauke added a commit to kleisauke/libvips that referenced this pull request May 22, 2025
jcupitt pushed a commit that referenced this pull request May 22, 2025
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.

2 participants