Skip to content

Jasper does not write cdef box for output with opacity #392

Closed

Description

@nico

The attached file is a jp2 with rgb and a channels. (I made it in Photoshop and saved it using its "Save a Copy…" feature, which under the hood seems to use kakadu.) Its cdef says that component 0 is color 1 (red), 1 is 2 (blue), 2 is 3 (green), and 3 is alpha associated with the entire image.

kakadu.zip

('jp2h')
  ('ihdr')
  - height = 101
  - width = 119
  - num_components = 4
  - are_components_signed = false
  - bits_per_component = 8
  - compression_type = 7
  - is_colorspace_unknown = 1
  - contains_intellectual_property_rights = 0
  ('colr')
  - method = 1
  - precedence = 0
  - approximation = 1
  - enumerated_color_space = 16
  ('cdef')
  - channel_index = 0
  - channel_type = 0 (color)
  - channel_association = 1
  - channel_index = 1
  - channel_type = 0 (color)
  - channel_association = 2
  - channel_index = 2
  - channel_type = 0 (color)
  - channel_association = 3
  - channel_index = 3
  - channel_type = 1 (opacity)
  - channel_association = 0

If I re-encode this image using jasper, e.g. with build/src/app/jasper --input Tests/LibGfx/test-inputs/jpeg2000/kakadu-lossless-rgba-u8-prog1-layers1-res6-mct.jp2 --output jasper-tile3x2-res5.jp2 -O tilewidth=39 -O tileheight=53 -O numrlvls=5, it doesn't write a cdef box.

I believe this is incorrect.

T.800, I.5.3.6 Channel Definition box: "If the JP2 Header box does not contain a Component Mapping box, then a reader shall map component i to channel i, for all components in the codestream."

T.800 Table 1.18 maps 1 to R, 2 to G, 3 to B, and 4 to nothing. (The spec text probably means "map component i to channel i + 1".) That is, per spec, the last component, alpha, should be ignored if there's no cdef box.

And indeed, Preview.app at least displays jasper's output as if it didn't contain an alpha channel.

jasper should write a cdef box for rgba output.

Activity

  1. nico commented on Feb 3, 2025

    @nico
    Author

    Here's a hack; a real fix probably would want to check if the fourth component is opacity.

    diff --git a/src/libjasper/jp2/jp2_enc.c b/src/libjasper/jp2/jp2_enc.c
    index 30f8a82..fb2586e 100644
    --- a/src/libjasper/jp2/jp2_enc.c
    +++ b/src/libjasper/jp2/jp2_enc.c
    @@ -297,7 +297,7 @@ int jp2_encode(jas_image_t *image, jas_stream_t *out, const char *optstr)
            needcdef = 1;
            switch (jas_clrspc_fam(jas_image_clrspc(image))) {
            case JAS_CLRSPC_FAM_RGB:
    -               if (jas_image_numcmpts(image) >= 3 &&
    +               if (jas_image_numcmpts(image) == 3 &&
                      jas_image_cmpttype(image, 0) ==
                      JAS_IMAGE_CT_COLOR(JAS_CLRSPC_CHANIND_RGB_R) &&
                      jas_image_cmpttype(image, 1) ==

    With this, jasper produces a jp2 with a cdef box that looks right, and its output does show up with transparency in Preview.app.

  2. added a commit that references this issue on Feb 3, 2025
  3. nico commented on Feb 3, 2025

    @nico
    Author

    (OpenJPEG's opj_encode does write this cdef box with a similar invocation, fwiw.)

  4. changed the title [-]Jasper does not write cdef box for output with opactiy[/-] [+]Jasper does not write cdef box for output with opacity[/+] on Mar 2, 2025
  5. mdadams commented on Mar 5, 2026

    @mdadams
    Collaborator

    @nico: I am sorry that it took so long to address this issue. I looked at the code more carefully, and as far as I can tell your "hack" as you call it appears to be a very reasonable (i.e., non-hacky) fix. It seems that JasPer was being too strict about when a CDEF box is generated. I think that a CDEF box should only be omitted in a very specific circumstance but using ">= 3" instead of "== 3" was causing the CDEF box to be omitted in many other cases as well. Anyways, I have adopted your fix and made a similar correction for the other color spaces as well, since it would appear that the same problem also exists for YCBCR and GRAY color spaces. I have made a new release of JasPer (4.2.9) that includes this fix. So, you might want to switch to it, if this issue is a significant one for you.

  6. added a commit that references this issue on Mar 5, 2026
    afcabfe
  7. nico commented on Apr 24, 2026

    @nico
    Author

    Thanks for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions