strukturag / libheif

libheif is an HEIF and AVIF file format decoder and encoder.
Other
1.62k stars 294 forks source link

Curiosity about why decode_image_planar always emits RGB image #636

Closed pmsoftware78 closed 1 year ago

pmsoftware78 commented 2 years ago

Not a real issue, but just curious to understand why in haif_context.cc (function decode_image_planar) the image is always converted to RGB

if (!alphaImage && target_colorspace == heif_colorspace_YCbCr) { target_colorspace = heif_colorspace_RGB; }

instead provide YUV + NCLX information. Thanks

bigcat88 commented 1 year ago

I probably has another 2 questions:

  1. Has anyone image that will be opened in heif_colorspace_YCbCr when heif_colorspace_undefined + heif_chroma_undefined passed to heif_decode_image function?
  2. Why I always get heif_chroma_444 even when I saved image with chroma=422 and open it after? (with heif_colorspace_undefined + heif_chroma_undefined)
farindk commented 1 year ago

The reason that it always outputs images in 4:4:4 is that the image transformations are also handled in the library and there are a lot of corner cases. Like, the encoded image could be 4:2:2 and rotated by 90 degrees. Then, there is no chroma format that could describe this. Or it could be cropped by one pixel on the left side which would shift the 4:2:0 chroma samples (recent, this has been disallowed for images conforming to MIAF, but it is allowed for general HEIF).

Finally, output is always in RGB because the library also handles the nclx color profiles for you (different color conversion coefficients).

I plan to add low-level access, but then all of these things have to be handled on client side.

bigcat88 commented 1 year ago

I am totally fine that there is only heif_chroma_444 and heif_chroma_monochrome returned when I pass heif_chroma_undefined to heif_decode_image. Can I suggest that this behaviour will not change in future and write code according to this?

farindk commented 1 year ago

If you set it to chroma_undefined, anything can happen. I do not want to restrict this in any way. If you want a specific chroma format, you have to specify this in heif_decode_image(). I understand your point that you do not know beforehand whether to use 444 or monochrome. But the solution to that will probably be that we add an API function to ask for the coded image chroma. How this will look like still needs some thoughts.

bigcat88 commented 1 year ago

Thanks for clarifying

bradh commented 1 year ago

Looks like the question(s) got answered. Is anything more required on this?

farindk commented 1 year ago

Closing. Tracking the todo in a separate issue.