Repository navigation
fix(j2k-native): keep the declared color space over unspecified extra components - #323
Merged
jcwal1516 merged 2 commits intoNov 8, 2026
Merged
Conversation
… components A JP2 declaring sRGB over four codestream components with no cdef box failed every Rgb8/Rgba8 decode under the default strict settings with "backend color space cannot be mapped to requested 8-bit pixel format". The component-count fallback in resolve_alpha_and_color_space replaced the declared sRGB with CMYK, which has no 8-bit RGB mapping. Kakadu 7 wrote such files from RGBA TIFFs unless -jp2_alpha was given. Without a channel definition, ISO/IEC 15444-1 I.5.3.6 leaves the components after those of the declared color space unspecified. When a JP2 declares an enumerated or ICC color space, has no cdef or palette, and has more components than that space (plus alpha), keep the color space and record the extras as Image::ignored_components. Decoded output omits them: finish_decoded_image, the native RawBitmap decodes, streamed region rows and the native planes sink keep only the color and alpha components. Device plane reuse is disabled for such images. Lenient decoding still infers alpha for exactly one extra component, and raw codestreams keep their component-count interpretation. Recode keeps every component, since it reuses the source JP2 metadata. The HTJ2K batch direct-plan builder now treats DirectPlanUnsupported as "not direct" and falls back instead of failing the job.
Choose bounded u8/u16 row layouts from resolved color and alpha metadata. Compare all codestream components during recode validation and cover row output and preservation with regression tests. Update the API inventories and review records for the original JP2 color-space change.
jcoyne
added a commit
to jcoyne/persimmon
that referenced
this pull request
Nov 9, 2026
Picks up the fixes from frames-sg/j2k#323: keep the declared color space over unspecified extra components, and complete extra-component row decoding and recode validation.
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.
Problem
Under the default strict settings, every
Rgb8/Rgba8decode of a JP2 that declares sRGB over four codestream components with nocdefbox failed:resolve_alpha_and_color_spacesaw the component count disagree with the declared colour space and fell back to guessing from the count: four components without alpha became CMYK. CMYK has no 8-bit RGB mapping. Kakadu 7.10.2 wrote files like this from RGBA TIFFs when-jp2_alphawasn't given. Kakadu 8.4.1 writes thecdefbox. We hit it in production on an IIIF image server.Change
ISO/IEC 15444-1 I.5.3.6: without a channel definition, the components after those of the declared colour space are unspecified. They don't change what the colour space is.
cdefor palette, and has more components than that space (plus alpha), the colour space is kept. The extra components are recorded asImage::ignored_components.finish_decoded_image(bitmaps, borrowed and native planes,decode_into), the nativeRawBitmapdecodes (16-bit output), streamed region rows (through a sink adapter), and the native planes sink.supports_direct_device_plane_reusereturns false for these images.Rgb8, and toRgba8with opaque alpha, at full resolution, in regions and in scaled regions.Unchanged:
Recode calls a hidden
Image::keep_ignored_componentsand re-encodes every component, because it reuses the source JP2 metadata.build_direct_color_region_plan(HTJ2K batch) now treatsDirectPlanUnsupportedas "not direct" and falls back to the generic decode. Before, it failed the job, as it already did for these images when they were labelled CMYK.Tests
New file
crates/j2k/tests/unspecified_extra_components.rs. The JP2s are built by hand, because the wrapper adds an RGBAcdeffor four-component sRGB.Rgb8match the first three input channels.Rgba8has opaque alpha. Scaled regions at 1 and 2 levels work.Rgb16matches the widened samples.Gray8.ignored_components, no alpha, and the expected plane count fromdecode_native_components,decode_nativeand the streamed native planes.Checks run:
cargo test -p j2k-native -p j2kpasses.cargo xtask clippy,cargo xtask clippy-strictandcargo fmt --checkare clean.decode_region_scaled_pow2_intoat levels 0–2.Notes
J2kDecoder::info().componentsstill reports the codestream count (4). Bounded row decoding therefore picksRgba8for these files and gets opaque alpha.