From 9a027b801640e495e6363e96e55d6c07f8544034 Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sat, 20 Jun 2026 18:19:45 +1000 Subject: [PATCH] base: add WUFFS_BASE__YCC_MODEL__ETC --- internal/cgen/base/image-private.h | 2 +- internal/cgen/base/image-public.h | 58 +++++++++++++ internal/cgen/base/pixconv-submodule-ycck.c | 9 +- lang/builtin/builtin.go | 9 +- release/c/wuffs-unsupported-snapshot.c | 93 +++++++++++++++++---- std/jpeg/decode_jpeg.wuffs | 34 ++++---- std/vp8/decode_macroblocks.wuffs | 2 +- 7 files changed, 168 insertions(+), 39 deletions(-) diff --git a/internal/cgen/base/image-private.h b/internal/cgen/base/image-private.h index 4547b0f2..adef1655 100644 --- a/internal/cgen/base/image-private.h +++ b/internal/cgen/base/image-private.h @@ -67,7 +67,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( uint8_t v1, uint8_t v2, uint8_t v3, - bool is_rgb_or_cmyk, + uint8_t ycc_model, bool triangle_filter_for_2to1, wuffs_base__slice_u8 scratch_buffer_2k); diff --git a/internal/cgen/base/image-public.h b/internal/cgen/base/image-public.h index 34e0a357..f55210ae 100644 --- a/internal/cgen/base/image-public.h +++ b/internal/cgen/base/image-public.h @@ -737,6 +737,64 @@ wuffs_base__pixel_subsampling::denominator_y(uint32_t plane) const { // -------- +// YCbCr Models are how to convert to and from RGB. Here's the formulae from +// https://gist.github.com/yohhoy/dafa5a47dade85d8b40625261af3776a +// +// ---- +// +// Y = (a * R) + (b * G) + (c * B) +// Cb = (B - Y) / d +// Cr = (R - Y) / e +// +// BT.601 BT.709 BT.2020 +// a 0.299 0.2126 0.2627 +// b 0.587 0.7152 0.6780 +// c 0.114 0.0722 0.0593 +// d 1.772 1.8556 1.8814 +// e 1.402 1.5748 1.4746 +// +// R = Y + (e * Cr) +// G = Y - ((c / b) * d * Cb) - ((a / b) * e * Cr) +// B = Y + (d * Cb) +// +// https://www.itu.int/rec/R-REC-BT.601 +// https://www.itu.int/rec/R-REC-BT.709 +// https://www.itu.int/rec/R-REC-BT.2020 +// +// ---- +// +// The formulae and abcde constants above are for Full Range. They need scaling +// and biasing for Studio Range. +// +// Full Range means that the luma (Y) and chroma (Cb, Cr) samples all range in +// [0 ..= 255], for 8-bit samples. This is equivalent to [0.0 ..= 1.0] or +// sometimes [-0.5 ..= +0.5], for floating point samples. +// +// Studio Range means that luma (Y) ranges in [16 ..= 235] and chroma (Cb, Cr) +// ranges in [16 ..= 240], again for 8-bit samples. +// +// JPEG/JFIF uses BT.601 Full Range. WEBP uses BT.601 Studio Range. +// +// ---- +// +// Beyond the various ITU BT (Broadcast Television) standards linked from that +// gist, some nominally-YCbCr data streams (like 3- or 4-channel JPEG images) +// can, in practice, hold RGB or CMY/CMYK samples. +// +// For CMY, C = (100% - R), M = (100% - G) and Y = (100% - B). Black (K), if +// present, then modulates the three RGB values. + +// clang-format off + +#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00 +#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x01 +#define WUFFS_BASE__YCC_MODEL__RGB 0x40 +#define WUFFS_BASE__YCC_MODEL__CMY 0x80 + +// clang-format on + +// -------- + typedef struct wuffs_base__pixel_config__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. diff --git a/internal/cgen/base/pixconv-submodule-ycck.c b/internal/cgen/base/pixconv-submodule-ycck.c index 8c8c2eab..2570eec2 100644 --- a/internal/cgen/base/pixconv-submodule-ycck.c +++ b/internal/cgen/base/pixconv-submodule-ycck.c @@ -1224,7 +1224,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( uint8_t v1, uint8_t v2, uint8_t v3, - bool is_rgb_or_cmyk, + uint8_t ycc_model, bool triangle_filter_for_2to1, wuffs_base__slice_u8 scratch_buffer_2k) { if (!p) { @@ -1440,7 +1440,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( wuffs_private_impl__swizzle_ycc__convert_3_func conv3func = NULL; - if (is_rgb_or_cmyk) { + if (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) { conv3func = &wuffs_private_impl__swizzle_rgb__convert_3_general; } else { switch (dst->pixcfg.private_impl.pixfmt.repr) { @@ -1564,8 +1564,9 @@ wuffs_base__pixel_swizzler__swizzle_ycck( if ((h3 != 0u) || (v3 != 0u)) { wuffs_private_impl__swizzle_ycc__convert_4_func conv4func = - is_rgb_or_cmyk ? &wuffs_private_impl__swizzle_cmyk__convert_4_general - : &wuffs_private_impl__swizzle_ycck__convert_4_general; + (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) + ? &wuffs_private_impl__swizzle_cmyk__convert_4_general + : &wuffs_private_impl__swizzle_ycck__convert_4_general; (*func4)( // dst, x_min_incl, x_max_excl, y_min_incl, y_max_excl, // src0.ptr, src1.ptr, src2.ptr, src3.ptr, // diff --git a/lang/builtin/builtin.go b/lang/builtin/builtin.go index a4a99e17..21ceb59b 100644 --- a/lang/builtin/builtin.go +++ b/lang/builtin/builtin.go @@ -241,6 +241,13 @@ var Consts = [...]struct { // ---- {t.IDU32, "0xFFFD", "UNICODE__REPLACEMENT_CHARACTER"}, + + // ---- + + {t.IDU32, "0x00", "YCC_MODEL__BT_601_FULL_RANGE"}, + {t.IDU32, "0x01", "YCC_MODEL__BT_601_STUDIO_RANGE"}, + {t.IDU32, "0x40", "YCC_MODEL__RGB"}, + {t.IDU32, "0x80", "YCC_MODEL__CMY"}, } var Statuses = [...]string{ @@ -734,7 +741,7 @@ var funcsOther = [...]string{ "v1: u8[..= 4]," + "v2: u8[..= 4]," + "v3: u8[..= 4]," + - "is_rgb_or_cmyk: bool," + + "ycc_model: u8," + "triangle_filter_for_2to1: bool," + "scratch_buffer_2k: slice u8) status", diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index 4a6c7e4f..58dc5b4d 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -5081,6 +5081,64 @@ wuffs_base__pixel_subsampling::denominator_y(uint32_t plane) const { // -------- +// YCbCr Models are how to convert to and from RGB. Here's the formulae from +// https://gist.github.com/yohhoy/dafa5a47dade85d8b40625261af3776a +// +// ---- +// +// Y = (a * R) + (b * G) + (c * B) +// Cb = (B - Y) / d +// Cr = (R - Y) / e +// +// BT.601 BT.709 BT.2020 +// a 0.299 0.2126 0.2627 +// b 0.587 0.7152 0.6780 +// c 0.114 0.0722 0.0593 +// d 1.772 1.8556 1.8814 +// e 1.402 1.5748 1.4746 +// +// R = Y + (e * Cr) +// G = Y - ((c / b) * d * Cb) - ((a / b) * e * Cr) +// B = Y + (d * Cb) +// +// https://www.itu.int/rec/R-REC-BT.601 +// https://www.itu.int/rec/R-REC-BT.709 +// https://www.itu.int/rec/R-REC-BT.2020 +// +// ---- +// +// The formulae and abcde constants above are for Full Range. They need scaling +// and biasing for Studio Range. +// +// Full Range means that the luma (Y) and chroma (Cb, Cr) samples all range in +// [0 ..= 255], for 8-bit samples. This is equivalent to [0.0 ..= 1.0] or +// sometimes [-0.5 ..= +0.5], for floating point samples. +// +// Studio Range means that luma (Y) ranges in [16 ..= 235] and chroma (Cb, Cr) +// ranges in [16 ..= 240], again for 8-bit samples. +// +// JPEG/JFIF uses BT.601 Full Range. WEBP uses BT.601 Studio Range. +// +// ---- +// +// Beyond the various ITU BT (Broadcast Television) standards linked from that +// gist, some nominally-YCbCr data streams (like 3- or 4-channel JPEG images) +// can, in practice, hold RGB or CMY/CMYK samples. +// +// For CMY, C = (100% - R), M = (100% - G) and Y = (100% - B). Black (K), if +// present, then modulates the three RGB values. + +// clang-format off + +#define WUFFS_BASE__YCC_MODEL__BT_601_FULL_RANGE 0x00 +#define WUFFS_BASE__YCC_MODEL__BT_601_STUDIO_RANGE 0x01 +#define WUFFS_BASE__YCC_MODEL__RGB 0x40 +#define WUFFS_BASE__YCC_MODEL__CMY 0x80 + +// clang-format on + +// -------- + typedef struct wuffs_base__pixel_config__struct { // Do not access the private_impl's fields directly. There is no API/ABI // compatibility or safety guarantee if you do so. @@ -11053,7 +11111,7 @@ struct wuffs_jpeg__decoder__struct { bool f_test_only_interrupt_decode_mcu; bool f_is_jfif; uint8_t f_is_adobe; - bool f_is_rgb_or_cmyk; + uint8_t f_ycc_model; uint8_t f_sof_marker; uint8_t f_next_restart_marker; uint8_t f_max_incl_components_h; @@ -19034,7 +19092,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( uint8_t v1, uint8_t v2, uint8_t v3, - bool is_rgb_or_cmyk, + uint8_t ycc_model, bool triangle_filter_for_2to1, wuffs_base__slice_u8 scratch_buffer_2k); @@ -33411,7 +33469,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( uint8_t v1, uint8_t v2, uint8_t v3, - bool is_rgb_or_cmyk, + uint8_t ycc_model, bool triangle_filter_for_2to1, wuffs_base__slice_u8 scratch_buffer_2k) { if (!p) { @@ -33627,7 +33685,7 @@ wuffs_base__pixel_swizzler__swizzle_ycck( wuffs_private_impl__swizzle_ycc__convert_3_func conv3func = NULL; - if (is_rgb_or_cmyk) { + if (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) { conv3func = &wuffs_private_impl__swizzle_rgb__convert_3_general; } else { switch (dst->pixcfg.private_impl.pixfmt.repr) { @@ -33751,8 +33809,9 @@ wuffs_base__pixel_swizzler__swizzle_ycck( if ((h3 != 0u) || (v3 != 0u)) { wuffs_private_impl__swizzle_ycc__convert_4_func conv4func = - is_rgb_or_cmyk ? &wuffs_private_impl__swizzle_cmyk__convert_4_general - : &wuffs_private_impl__swizzle_ycck__convert_4_general; + (ycc_model >= WUFFS_BASE__YCC_MODEL__RGB) + ? &wuffs_private_impl__swizzle_cmyk__convert_4_general + : &wuffs_private_impl__swizzle_ycck__convert_4_general; (*func4)( // dst, x_min_incl, x_max_excl, y_min_incl, y_max_excl, // src0.ptr, src1.ptr, src2.ptr, src3.ptr, // @@ -55652,15 +55711,17 @@ wuffs_jpeg__decoder__decode_sof( status = wuffs_base__make_status(wuffs_jpeg__error__unsupported_fractional_sampling); goto exit; } + self->private_impl.f_ycc_model = ((uint8_t)(0u)); if (self->private_impl.f_num_components == 4u) { - self->private_impl.f_is_rgb_or_cmyk = (self->private_impl.f_is_adobe < 2u); - } else { - if (self->private_impl.f_is_jfif) { - self->private_impl.f_is_rgb_or_cmyk = false; - } else if (self->private_impl.f_is_adobe > 0u) { - self->private_impl.f_is_rgb_or_cmyk = (self->private_impl.f_is_adobe == 1u); - } else { - self->private_impl.f_is_rgb_or_cmyk = ((self->private_impl.f_components_c[0u] == 82u) && (self->private_impl.f_components_c[1u] == 71u) && (self->private_impl.f_components_c[2u] == 66u)); + if (self->private_impl.f_is_adobe < 2u) { + self->private_impl.f_ycc_model = ((uint8_t)(128u)); + } + } else if ( ! self->private_impl.f_is_jfif) { + if ((self->private_impl.f_is_adobe == 1u) || ((self->private_impl.f_is_adobe < 1u) && + (self->private_impl.f_components_c[0u] == 82u) && + (self->private_impl.f_components_c[1u] == 71u) && + (self->private_impl.f_components_c[2u] == 66u))) { + self->private_impl.f_ycc_model = ((uint8_t)(64u)); } } } @@ -57624,7 +57685,7 @@ wuffs_jpeg__decoder__swizzle_colorful( self->private_impl.f_components_v[1u], self->private_impl.f_components_v[2u], self->private_impl.f_components_v[3u], - self->private_impl.f_is_rgb_or_cmyk, + self->private_impl.f_ycc_model, ! self->private_impl.f_use_lower_quality, wuffs_base__make_slice_u8(self->private_data.f_swizzle_ycck_scratch_buffer_2k, 2048)); return wuffs_private_impl__status__ensure_not_a_suspension(v_status); @@ -84033,7 +84094,7 @@ wuffs_vp8__decoder__swizzle_one_row_of_macroblocks( 1u, 1u, 0u, - false, + 0u, false, wuffs_base__make_slice_u8(self->private_data.f_swizzle_ycck_scratch_buffer_2k, 2048)); return wuffs_private_impl__status__ensure_not_a_suspension(v_status); diff --git a/std/jpeg/decode_jpeg.wuffs b/std/jpeg/decode_jpeg.wuffs index 4240ffca..fd321c7e 100644 --- a/std/jpeg/decode_jpeg.wuffs +++ b/std/jpeg/decode_jpeg.wuffs @@ -50,9 +50,9 @@ pub struct decoder? implements base.image_decoder( test_only_interrupt_decode_mcu : base.bool, - is_jfif : base.bool, - is_adobe : base.u8, // 0: no, 1: yes+RGB/CMYK, 2:yes+YCC/YCCK. - is_rgb_or_cmyk : base.bool, + is_jfif : base.bool, + is_adobe : base.u8, // 0: no, 1: yes+RGB/CMYK, 2:yes+YCC/YCCK. + ycc_model : base.u8, sof_marker : base.u8, next_restart_marker : base.u8[..= 7], @@ -773,19 +773,21 @@ pri func decoder.decode_sof?(src: base.io_reader) { return "#unsupported fractional sampling" } - // Match the behavior of libjpeg-turbo's jdapimin.c. + // Match the behavior of libjpeg-turbo's jdapimin.c. By default, we use + // BT.601 Full Range (which is mandatory for JFIF). + // + // is_adobe means 0: no, 1: yes+RGB/CMYK, 2:yes+YCC/YCCK. + this.ycc_model = base.YCC_MODEL__BT_601_FULL_RANGE as base.u8 if this.num_components == 4 { - this.is_rgb_or_cmyk = this.is_adobe < 2 - } else { // this.num_components == 3 - if this.is_jfif { - this.is_rgb_or_cmyk = false - } else if this.is_adobe > 0 { - this.is_rgb_or_cmyk = this.is_adobe == 1 - } else { - this.is_rgb_or_cmyk = - (this.components_c[0] == 'R') and - (this.components_c[1] == 'G') and - (this.components_c[2] == 'B') + if this.is_adobe < 2 { + this.ycc_model = base.YCC_MODEL__CMY as base.u8 + } + } else if not this.is_jfif { // And also (this.num_components == 3). + if (this.is_adobe == 1) or ((this.is_adobe < 1) and + (this.components_c[0] == 'R') and + (this.components_c[1] == 'G') and + (this.components_c[2] == 'B')) { + this.ycc_model = base.YCC_MODEL__RGB as base.u8 } } } @@ -2166,7 +2168,7 @@ pri func decoder.swizzle_colorful!(dst: ptr base.pixel_buffer, workbuf: slice ba v1: this.components_v[1], v2: this.components_v[2], v3: this.components_v[3], - is_rgb_or_cmyk: this.is_rgb_or_cmyk, + ycc_model: this.ycc_model, triangle_filter_for_2to1: not this.use_lower_quality, scratch_buffer_2k: this.swizzle_ycck_scratch_buffer_2k[..]) return status diff --git a/std/vp8/decode_macroblocks.wuffs b/std/vp8/decode_macroblocks.wuffs index 6fe480c1..44f782d1 100644 --- a/std/vp8/decode_macroblocks.wuffs +++ b/std/vp8/decode_macroblocks.wuffs @@ -424,7 +424,7 @@ pri func decoder.swizzle_one_row_of_macroblocks!( v1: 1, v2: 1, v3: 0, - is_rgb_or_cmyk: false, + ycc_model: 0, // TODO. triangle_filter_for_2to1: false, scratch_buffer_2k: this.swizzle_ycck_scratch_buffer_2k[..]) return status