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

Determine JPEG2000 mode purely from ihdr header box #5654

Merged
merged 1 commit into from Aug 24, 2021

Conversation

radarhere
Copy link
Member

Resolves #3556

Image comes from https://github.com/openpreserve/format-corpus/blob/master/jp2k-test/icc/balloon_eciRGBv2_aware.jp2 - according to https://github.com/openpreserve/format-corpus/blob/master/README.md and https://github.com/openpreserve/format-corpus/blob/master/jp2k-test/readme.md, it is either public domain or CC0. I've truncated it just to be smaller for our test suite.

We currently determine the mode

  • if unkc is true, from the "ihdr" header box, or
  • if unkc is not true, and a "colr" header box is present with meth of 1, we... also determine the mode from the "ihdr" header box, or
  • we throw an error, as is happening in the issue

What is unkc, you say?

https://www.ics.uci.edu/~dan/class/267/papers/jpeg2000.pdf, emphasis mine.

Colourspace Unknown. This field specifies if the actual colourspace of the image data is known. This field is encoded as a 1-byte unsigned integer. Legal values for this field are 0, if the colourspace of the image is known and correctly specified the colourspace boxes within the file, or 1, if the colourspace of the image is not known. A value of 1 will be used in cases such as the transcoding of legacy images where the actual colourspace of the image data is not known. In those cases, while the colourspace interpretation methods specified in the file may not accurately reproduce the image with respect to some original, the image should be treated as if the methods do accurately reproduce the image.

From there, I have made this jump in logic - if the value of unkc is not supposed to affect interpretation, then looking at this present code -

if tbox == b"ihdr":
height, width, nc, bpc, c, unkc, ipr = header.read_fields(">IIHBBBB")
size = (width, height)
if unkc:
if nc == 1 and (bpc & 0x7F) > 8:
mode = "I;16"
elif nc == 1:
mode = "L"
elif nc == 2:
mode = "LA"
elif nc == 3:
mode = "RGB"
elif nc == 4:
mode = "RGBA"

why don't we just imagine that the if unkc: condition is always true, and determine the mode purely from the "ihdr" header box?

@hugovk hugovk merged commit 57c672e into python-pillow:master Aug 24, 2021
@radarhere radarhere deleted the ihdr branch August 24, 2021 12:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image.open() fails for JPEG 2000 with embedded ICC profile
2 participants