gen2brain / jpegxl

JPEG XL image encoder/decoder
MIT License
20 stars 1 forks source link

Extra channel info #1

Closed kpfaulkner closed 8 months ago

kpfaulkner commented 8 months ago

Do you know if it's possible to extract the extra-channel information? For example, I'm dealing with RGBA-X (where X is an additional channel). By default Go's Image struct really only handles RGBA... but extracting this extra channel info is eluding me.

Ever looked into that?

btw, this library + wasm approach is really good!

gen2brain commented 8 months ago

I believe JxlBasicInfo has all the information you need, see num_extra_channels:

  /** Number of color channels encoded in the image, this is either 1 for
   * grayscale data, or 3 for colored data. This count does not include
   * the alpha channel or other extra channels. To check presence of an alpha
   * channel, such as in the case of RGBA color, check alpha_bits != 0.
   * If and only if this is 1, the @ref JxlColorSpace in the @ref
   * JxlColorEncoding is
   * ::JXL_COLOR_SPACE_GRAY.
   */
  uint32_t num_color_channels;

  /** Number of additional image channels. This includes the main alpha channel,
   * but can also include additional channels such as depth, additional alpha
   * channels, spot colors, and so on. Information about the extra channels
   * can be queried with @ref JxlDecoderGetExtraChannelInfo. The main alpha
   * channel, if it exists, also has its information available in the
   * alpha_bits, alpha_exponent_bits and alpha_premultiplied fields in this @ref
   * JxlBasicInfo.
   */
  uint32_t num_extra_channels;

  /** Bit depth of the encoded alpha channel, or 0 if there is no alpha channel.
   * If present, matches the alpha_bits value of the JxlExtraChannelInfo
   * associated with this alpha channel.
   */
  uint32_t alpha_bits;

I force everywhere JXL_TYPE_UINT8 but if bits_per_sample is 16 I think one could use JXL_TYPE_UINT16 and store buffer in image.NRGBA64. All I care about is decoding images, I will see later to add animation support.

gen2brain commented 8 months ago

I added support for 16bit NRGBA64 and animations. For your issue, check JxlDecoderGetExtraChannelInfo when there are extra channels in num_extra_channels. Not sure what to do later with them though.