Convert YCbCr images to sRGB when decoding with Imagick#1502
Merged
olivervogel merged 1 commit intoJun 23, 2026
Conversation
Older ImageMagick decodes AVIF/HEIF as YCbCr and does not normalize it, so the Imagick ColorspaceAnalyzer reaches the OKLAB/OKLCH match arms and references Imagick::COLORSPACE_OKLAB, which is undefined before OKLAB support and throws. Convert YCbCr to sRGB on decode (next to the existing GRAY handling), and guard the OKLAB/OKLCH constants with defined() the same way ColorspaceModifier already does.
Member
|
Thanks. |
Contributor
Author
|
Thank you @olivervogel! |
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.
Reading pixels from an AVIF (or HEIF) fails with the Imagick driver on older ImageMagick. Decoding is fine, the first
colorAt()throws.Older ImageMagick decodes AVIF/HEIF into the YCbCr colorspace and doesn't normalize it. The Imagick
ColorspaceAnalyzerhas no YCbCr branch, so thematchfalls through to the OKLAB/OKLCH arms. Those referenceImagick::COLORSPACE_OKLAB, which doesn't exist before the ImageMagick release that added OKLAB, so analysis fails with "Undefined constant Imagick::COLORSPACE_OKLAB". Newer ImageMagick returns sRGB for the same image, so it only shows up on older builds.Repro: decode an AVIF with the Imagick driver on an ImageMagick without OKLAB, then call
colorAt()orcolorspace().Measured inside the failing environment:
COLORSPACE_YCBCR)COLORSPACE_YCBCR)Every decoded AVIF reported YCbCr regardless of the encoder (aom/libavif, vips). The colorspace enum values differ between ImageMagick majors (YCbCr is 7 on IM6, 27 on IM7), so the fix uses the
Imagick::COLORSPACE_YCBCRconstant, not a literal.Fix:
NativeObjectDecoderconverts YCbCr to sRGB on decode, next to the existing GRAY handling. It usestransformImageColorspace()(a real conversion), notsetImageColorspace(), otherwise pixels come back as raw luma/chroma values. This is a no-op on newer ImageMagick where the image is already sRGB.ColorspaceAnalyzerguards the OKLAB/OKLCH constants withdefined()before using them, the same wayColorspaceModifieralready does. Unknown colorspaces now reach thedefaultarm instead of throwing on the missing constant.Tests: added a regression test in
NativeObjectDecoderTestthat forces an image to YCbCr, decodes it, and checks the result is sRGB andcolorAt()returns the original color (small tolerance, so it also catches a relabel-instead-of-convert mistake). It forces YCbCr explicitly so it runs the same on ImageMagick 6 and 7.