From affff51fbf47d401918845583e126e9d622069ce Mon Sep 17 00:00:00 2001 From: Nigel Tao Date: Sun, 8 Sep 2024 10:37:43 +1000 Subject: [PATCH] base: add pixel_format.coloration --- doc/note/pixel-formats.md | 3 +- internal/cgen/base/image-public.h | 26 +++++++++++++++ internal/cgen/drop-in/stb.c | 18 +++-------- release/c/wuffs-unsupported-snapshot.c | 44 ++++++++++++++++++-------- 4 files changed, 62 insertions(+), 29 deletions(-) diff --git a/doc/note/pixel-formats.md b/doc/note/pixel-formats.md index 1a4f7ddb..db1435cb 100644 --- a/doc/note/pixel-formats.md +++ b/doc/note/pixel-formats.md @@ -26,7 +26,8 @@ no alpha) and floating point samples should mean a bit depth of 16, 32 or 64. Formats hold between 1 and 4 channels. For example: Y (1 channel: gray), YA (2 channels: gray and alpha), BGR (3 channels: blue, green, red) or CMYK (4 -channels: cyan, magenta, yellow, black). +channels: cyan, magenta, yellow, black). BGRX holds 4 channels even though the +X-padding channel's values are ignored. For direct formats with N > 1 channels, those channels can be laid out in either 1 (interleaved) or N (planar) planes. For example, RGBA data is usually diff --git a/internal/cgen/base/image-public.h b/internal/cgen/base/image-public.h index a890e5e6..cf546d70 100644 --- a/internal/cgen/base/image-public.h +++ b/internal/cgen/base/image-public.h @@ -321,6 +321,20 @@ typedef uint32_t wuffs_base__pixel_alpha_transparency; // -------- +// wuffs_base__pixel_coloration is whether a pixel format's color model has no +// color (alpha only), gray color or rich (non-gray) color. Rich color includes +// RGB, BGR, YCC, YCCK, CMY and CMYK. +// +// Coloration does not capture the alpha aspect of the color model. It does not +// distinguish RGB from RGBA. +typedef uint32_t wuffs_base__pixel_coloration; + +#define WUFFS_BASE__PIXEL_COLORATION__NONE 0 +#define WUFFS_BASE__PIXEL_COLORATION__GRAY 1 +#define WUFFS_BASE__PIXEL_COLORATION__RICH 3 + +// -------- + // Deprecated: use WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX_INCL. #define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4 @@ -349,6 +363,7 @@ typedef struct wuffs_base__pixel_format__struct { inline bool is_indexed() const; inline bool is_interleaved() const; inline bool is_planar() const; + inline uint32_t coloration() const; inline uint32_t num_planes() const; inline wuffs_base__pixel_alpha_transparency transparency() const; #endif // __cplusplus @@ -455,6 +470,12 @@ wuffs_base__pixel_format__is_planar(const wuffs_base__pixel_format* f) { return ((f->repr >> 16) & 0x03) != 0; } +static inline uint32_t // +wuffs_base__pixel_format__coloration(const wuffs_base__pixel_format* f) { + uint32_t n = (f->repr) >> 29; + return (n <= 1) ? n : 3; +} + static inline uint32_t // wuffs_base__pixel_format__num_planes(const wuffs_base__pixel_format* f) { return ((f->repr >> 16) & 0x03) + 1; @@ -497,6 +518,11 @@ wuffs_base__pixel_format::is_planar() const { return wuffs_base__pixel_format__is_planar(this); } +inline uint32_t // +wuffs_base__pixel_format::coloration() const { + return wuffs_base__pixel_format__coloration(this); +} + inline uint32_t // wuffs_base__pixel_format::num_planes() const { return wuffs_base__pixel_format__num_planes(this); diff --git a/internal/cgen/drop-in/stb.c b/internal/cgen/drop-in/stb.c index 6a807bab..9742eeb7 100644 --- a/internal/cgen/drop-in/stb.c +++ b/internal/cgen/drop-in/stb.c @@ -306,20 +306,10 @@ wuffs_drop_in__stb__load0( // if (channels_in_file) { wuffs_base__pixel_format src_pixfmt = wuffs_base__pixel_config__pixel_format(&ic.pixcfg); - switch (src_pixfmt.repr) { - case WUFFS_BASE__PIXEL_FORMAT__Y: - case WUFFS_BASE__PIXEL_FORMAT__Y_16LE: - case WUFFS_BASE__PIXEL_FORMAT__Y_16BE: - *channels_in_file = 1; - break; - default: - *channels_in_file = - (wuffs_base__pixel_format__transparency(&src_pixfmt) == - WUFFS_BASE__PIXEL_ALPHA_TRANSPARENCY__OPAQUE) - ? 3 - : 4; - break; - } + uint32_t n_color = wuffs_base__pixel_format__coloration(&src_pixfmt); + uint32_t n_alpha = wuffs_base__pixel_format__transparency(&src_pixfmt) != + WUFFS_BASE__PIXEL_ALPHA_TRANSPARENCY__OPAQUE; + *channels_in_file = (int)(n_color + n_alpha); } return ret; diff --git a/release/c/wuffs-unsupported-snapshot.c b/release/c/wuffs-unsupported-snapshot.c index f6c8889e..922c935a 100644 --- a/release/c/wuffs-unsupported-snapshot.c +++ b/release/c/wuffs-unsupported-snapshot.c @@ -4360,6 +4360,20 @@ typedef uint32_t wuffs_base__pixel_alpha_transparency; // -------- +// wuffs_base__pixel_coloration is whether a pixel format's color model has no +// color (alpha only), gray color or rich (non-gray) color. Rich color includes +// RGB, BGR, YCC, YCCK, CMY and CMYK. +// +// Coloration does not capture the alpha aspect of the color model. It does not +// distinguish RGB from RGBA. +typedef uint32_t wuffs_base__pixel_coloration; + +#define WUFFS_BASE__PIXEL_COLORATION__NONE 0 +#define WUFFS_BASE__PIXEL_COLORATION__GRAY 1 +#define WUFFS_BASE__PIXEL_COLORATION__RICH 3 + +// -------- + // Deprecated: use WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX_INCL. #define WUFFS_BASE__PIXEL_FORMAT__NUM_PLANES_MAX 4 @@ -4388,6 +4402,7 @@ typedef struct wuffs_base__pixel_format__struct { inline bool is_indexed() const; inline bool is_interleaved() const; inline bool is_planar() const; + inline uint32_t coloration() const; inline uint32_t num_planes() const; inline wuffs_base__pixel_alpha_transparency transparency() const; #endif // __cplusplus @@ -4494,6 +4509,12 @@ wuffs_base__pixel_format__is_planar(const wuffs_base__pixel_format* f) { return ((f->repr >> 16) & 0x03) != 0; } +static inline uint32_t // +wuffs_base__pixel_format__coloration(const wuffs_base__pixel_format* f) { + uint32_t n = (f->repr) >> 29; + return (n <= 1) ? n : 3; +} + static inline uint32_t // wuffs_base__pixel_format__num_planes(const wuffs_base__pixel_format* f) { return ((f->repr >> 16) & 0x03) + 1; @@ -4536,6 +4557,11 @@ wuffs_base__pixel_format::is_planar() const { return wuffs_base__pixel_format__is_planar(this); } +inline uint32_t // +wuffs_base__pixel_format::coloration() const { + return wuffs_base__pixel_format__coloration(this); +} + inline uint32_t // wuffs_base__pixel_format::num_planes() const { return wuffs_base__pixel_format__num_planes(this); @@ -85704,20 +85730,10 @@ wuffs_drop_in__stb__load0( // if (channels_in_file) { wuffs_base__pixel_format src_pixfmt = wuffs_base__pixel_config__pixel_format(&ic.pixcfg); - switch (src_pixfmt.repr) { - case WUFFS_BASE__PIXEL_FORMAT__Y: - case WUFFS_BASE__PIXEL_FORMAT__Y_16LE: - case WUFFS_BASE__PIXEL_FORMAT__Y_16BE: - *channels_in_file = 1; - break; - default: - *channels_in_file = - (wuffs_base__pixel_format__transparency(&src_pixfmt) == - WUFFS_BASE__PIXEL_ALPHA_TRANSPARENCY__OPAQUE) - ? 3 - : 4; - break; - } + uint32_t n_color = wuffs_base__pixel_format__coloration(&src_pixfmt); + uint32_t n_alpha = wuffs_base__pixel_format__transparency(&src_pixfmt) != + WUFFS_BASE__PIXEL_ALPHA_TRANSPARENCY__OPAQUE; + *channels_in_file = (int)(n_color + n_alpha); } return ret;