Skip to content

Commit

Permalink
jxl-oxide: Fix CMYK to RGB conversion (#370)
Browse files Browse the repository at this point in the history
* jxl-oxide: Fix CMYK to RGB conversion

* Update CHANGELOG.md
  • Loading branch information
tirr-c authored Oct 29, 2024
1 parent 885c953 commit 2bc295c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 32 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- `jxl-render`: Fix requested color encoding not applied in some cases (#369).
- `jxl-oxide`: Fix CMYK to RGB conversion (#370).

## [0.9.1] - 2024-10-12

Expand Down
10 changes: 10 additions & 0 deletions crates/jxl-color/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ mod tone_map;
pub struct ColorEncodingWithProfile {
encoding: ColourEncoding,
icc_profile: Vec<u8>,
is_cmyk: bool,
}

impl std::fmt::Debug for ColorEncodingWithProfile {
Expand All @@ -22,6 +23,7 @@ impl std::fmt::Debug for ColorEncodingWithProfile {
"icc_profile",
&format_args!("({} byte(s))", self.icc_profile.len()),
)
.field("is_cmyk", &self.is_cmyk)
.finish()
}
}
Expand All @@ -32,6 +34,7 @@ impl ColorEncodingWithProfile {
Self {
encoding: ColourEncoding::Enum(encoding),
icc_profile: Vec::new(),
is_cmyk: false,
}
}

Expand All @@ -47,6 +50,7 @@ impl ColorEncodingWithProfile {
Ok(Self {
encoding: ColourEncoding::IccProfile(raw.color_space()),
icc_profile: icc_profile.to_vec(),
is_cmyk: raw.is_cmyk(),
})
}
Err(e) => Err(e),
Expand All @@ -73,6 +77,12 @@ impl ColorEncodingWithProfile {
ColourEncoding::IccProfile(color_space) => *color_space == ColourSpace::Grey,
}
}

/// Returns whether the color encoding represents CMYK color space.
#[inline]
pub fn is_cmyk(&self) -> bool {
self.is_cmyk
}
}

impl ColorEncodingWithProfile {
Expand Down
4 changes: 4 additions & 0 deletions crates/jxl-color/src/icc/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,10 @@ impl IccProfile<'_> {
ColourSpace::Unknown
}
}

pub(crate) fn is_cmyk(&self) -> bool {
&self.header.color_space == b"CMYK"
}
}

struct RawTag<'a> {
Expand Down
25 changes: 14 additions & 11 deletions crates/jxl-oxide/src/fb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,19 +211,22 @@ impl<'r> ImageStream<'r> {
}

// Find black
for (ec_idx, (ec, (region, _))) in render
.extra_channels
.iter()
.zip(&regions_and_shifts[color_channels..])
.enumerate()
{
if ec.is_black() {
grids.push(&fb[color_channels + ec_idx]);
bit_depth.push(ec.bit_depth);
start_offset_xy.push((left - region.left, top - region.top));
break;
if render.is_cmyk {
for (ec_idx, (ec, (region, _))) in render
.extra_channels
.iter()
.zip(&regions_and_shifts[color_channels..])
.enumerate()
{
if ec.is_black() {
grids.push(&fb[color_channels + ec_idx]);
bit_depth.push(ec.bit_depth);
start_offset_xy.push((left - region.left, top - region.top));
break;
}
}
}

// Find alpha
for (ec_idx, (ec, (region, _))) in render
.extra_channels
Expand Down
28 changes: 7 additions & 21 deletions crates/jxl-oxide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -599,28 +599,9 @@ impl JxlImage {

/// Returns the pixel format of the rendered image.
pub fn pixel_format(&self) -> PixelFormat {
use jxl_color::{ColourEncoding, ColourSpace};

let encoding = self.ctx.requested_color_encoding();
let (is_grayscale, has_black) = match encoding.encoding() {
ColourEncoding::Enum(EnumColourEncoding {
colour_space: ColourSpace::Grey,
..
}) => (true, false),
ColourEncoding::Enum(_) => (false, false),
ColourEncoding::IccProfile(_) => {
let profile = encoding.icc_profile();
if profile.len() < 0x14 {
(false, false)
} else {
match &profile[0x10..0x14] {
[b'G', b'R', b'A', b'Y'] => (true, false),
[b'C', b'M', b'Y', b'K'] => (false, true),
_ => (false, false),
}
}
}
};
let is_grayscale = encoding.is_grayscale();
let has_black = encoding.is_cmyk();
let mut has_alpha = false;
for ec_info in &self.image_header.metadata.ec_info {
if ec_info.is_alpha() {
Expand Down Expand Up @@ -754,6 +735,7 @@ impl JxlImage {
let frame_header = frame.header();
let target_frame_region = image_region.translate(-frame_header.x0, -frame_header.y0);

let is_cmyk = self.ctx.requested_color_encoding().is_cmyk();
let result = Render {
keyframe_index,
name: frame_header.name.clone(),
Expand All @@ -763,6 +745,7 @@ impl JxlImage {
extra_channels: self.convert_ec_info(),
target_frame_region,
color_bit_depth: self.image_header.metadata.bit_depth,
is_cmyk,
render_spot_color: self.render_spot_color,
};
Ok(result)
Expand Down Expand Up @@ -792,6 +775,7 @@ impl JxlImage {
let frame_header = frame.header();
let target_frame_region = image_region.translate(-frame_header.x0, -frame_header.y0);

let is_cmyk = self.ctx.requested_color_encoding().is_cmyk();
let result = Render {
keyframe_index: self.ctx.loaded_keyframes(),
name,
Expand All @@ -801,6 +785,7 @@ impl JxlImage {
extra_channels: self.convert_ec_info(),
target_frame_region,
color_bit_depth: self.image_header.metadata.bit_depth,
is_cmyk,
render_spot_color: self.render_spot_color,
};
Ok(result)
Expand Down Expand Up @@ -928,6 +913,7 @@ pub struct Render {
extra_channels: Vec<ExtraChannel>,
target_frame_region: Region,
color_bit_depth: BitDepth,
is_cmyk: bool,
render_spot_color: bool,
}

Expand Down

0 comments on commit 2bc295c

Please sign in to comment.