diff options
Diffstat (limited to 'src/lib/util/palette.h')
-rw-r--r-- | src/lib/util/palette.h | 375 |
1 files changed, 195 insertions, 180 deletions
diff --git a/src/lib/util/palette.h b/src/lib/util/palette.h index 46f01f962b5..06370118d05 100644 --- a/src/lib/util/palette.h +++ b/src/lib/util/palette.h @@ -14,162 +14,177 @@ #define __PALETTE_H__ #include "osdcore.h" +#include "coretmpl.h" -/*************************************************************************** - TYPE DEFINITIONS -***************************************************************************/ - -/* an rgb_t is a single combined R,G,B (and optionally alpha) value */ -typedef UINT32 rgb_t; - -/* an rgb15_t is a single combined 15-bit R,G,B value */ -typedef UINT16 rgb15_t; - -/* a palette is an opaque, reference counted object */ -struct palette_t; - -/* a palette client is someone who is tracking the dirty state of a palette */ -struct palette_client; - - - -/*************************************************************************** - MACROS -***************************************************************************/ +//************************************************************************** +// MACROS +//************************************************************************** -/* macros to assemble rgb_t values */ -#define MAKE_ARGB(a,r,g,b) ((((rgb_t)(a) & 0xff) << 24) | (((rgb_t)(r) & 0xff) << 16) | (((rgb_t)(g) & 0xff) << 8) | ((rgb_t)(b) & 0xff)) +// macros to assemble rgb_t values */ +#define MAKE_ARGB(a,r,g,b) (((rgb_t(a) & 0xff) << 24) | ((rgb_t(r) & 0xff) << 16) | ((rgb_t(g) & 0xff) << 8) | (rgb_t(b) & 0xff)) #define MAKE_RGB(r,g,b) (MAKE_ARGB(255,r,g,b)) -/* macros to extract components from rgb_t values */ +// macros to extract components from rgb_t values */ #define RGB_ALPHA(rgb) (((rgb) >> 24) & 0xff) #define RGB_RED(rgb) (((rgb) >> 16) & 0xff) #define RGB_GREEN(rgb) (((rgb) >> 8) & 0xff) #define RGB_BLUE(rgb) ((rgb) & 0xff) -/* common colors */ +// common colors */ #define RGB_BLACK (MAKE_ARGB(255,0,0,0)) #define RGB_WHITE (MAKE_ARGB(255,255,255,255)) -/*************************************************************************** - FUNCTION PROTOTYPES -***************************************************************************/ - - -/* ----- palette allocation ----- */ - -/* allocate a new palette object and take a single reference on it */ -palette_t *palette_alloc(UINT32 numcolors, UINT32 numgroups); - -/* reference a palette object, incrementing its reference count */ -void palette_ref(palette_t *palette); - -/* dereference a palette object; if the reference count goes to 0, it is freed */ -void palette_deref(palette_t *palette); - - - -/* ----- palette information ----- */ - -/* return the number of colors managed by the palette */ -int palette_get_num_colors(palette_t *palette); - -/* return the number of groups managed by the palette */ -int palette_get_num_groups(palette_t *palette); - -/* return the maximum allowed index (i.e., length of arrays returned by palette_entry_list*) */ -int palette_get_max_index(palette_t *palette); - -/* return the index of the black entry */ -UINT32 palette_get_black_entry(palette_t *palette); - -/* return the index of the white entry */ -UINT32 palette_get_white_entry(palette_t *palette); - - - -/* ----- palette clients ----- */ - -/* add a new client to a palette */ -palette_client *palette_client_alloc(palette_t *palette); - -/* remove a client from a palette */ -void palette_client_free(palette_client *client); - -/* return a pointer to the palette for this client */ -palette_t *palette_client_get_palette(palette_client *client); - -/* atomically get the current dirty list for a client */ -const UINT32 *palette_client_get_dirty_list(palette_client *client, UINT32 *mindirty, UINT32 *maxdirty); - - - -/* ----- color management ----- */ - -/* set the raw RGB color for a given palette index */ -void palette_entry_set_color(palette_t *palette, UINT32 index, rgb_t rgb); - -/* return the raw RGB color for a given palette index */ -rgb_t palette_entry_get_color(palette_t *palette, UINT32 index); - -/* return the adjusted RGB color (after all adjustments) for a given palette index */ -rgb_t palette_entry_get_adjusted_color(palette_t *palette, UINT32 index); - -/* return the entire palette as an array of raw RGB values */ -const rgb_t *palette_entry_list_raw(palette_t *palette); - -/* return the entire palette as an array of adjusted RGB values */ -const rgb_t *palette_entry_list_adjusted(palette_t *palette); - -/* return the entire palette as an array of adjusted RGB-15 values */ -const rgb_t *palette_entry_list_adjusted_rgb15(palette_t *palette); - - +//************************************************************************** +// TYPE DEFINITIONS +//************************************************************************** -/* ----- palette adjustments ----- */ - -/* set the overall brightness for the palette */ -void palette_set_brightness(palette_t *palette, float brightness); - -/* set the overall contrast for the palette */ -void palette_set_contrast(palette_t *palette, float contrast); - -/* set the overall gamma for the palette */ -void palette_set_gamma(palette_t *palette, float gamma); - -/* set the contrast adjustment for a single palette index */ -void palette_entry_set_contrast(palette_t *palette, UINT32 index, float contrast); - -/* return the contrast adjustment for a single palette index */ -float palette_entry_get_contrast(palette_t *palette, UINT32 index); - -/* configure overall brightness for a palette group */ -void palette_group_set_brightness(palette_t *palette, UINT32 group, float brightness); - -/* configure overall contrast for a palette group */ -void palette_group_set_contrast(palette_t *palette, UINT32 group, float contrast); - - - -/* ----- palette utilities ----- */ +// an rgb_t is a single combined R,G,B (and optionally alpha) value */ +typedef UINT32 rgb_t; -/* normalize a range of palette entries, mapping minimum brightness to lum_min and maximum - brightness to lum_max; if either value is < 0, that boundary value is not modified */ -void palette_normalize_range(palette_t *palette, UINT32 start, UINT32 end, int lum_min, int lum_max); +// an rgb15_t is a single combined 15-bit R,G,B value */ +typedef UINT16 rgb15_t; +// forward definitions +class palette_t; +// ======================> palette_client -/*************************************************************************** - INLINE FUNCTIONS -***************************************************************************/ +// a single palette client +class palette_client +{ +public: + // construction/destruction + palette_client(palette_t &palette); + ~palette_client(); + + // getters + palette_client *next() const { return m_next; } + palette_t &palette() const { return m_palette; } + const UINT32 *dirty_list(UINT32 &mindirty, UINT32 &maxdirty); + + // dirty marking + void mark_dirty(UINT32 index) { m_live->mark_dirty(index); } + +private: + // internal object to track dirty states + class dirty_state + { + public: + // construction + dirty_state(); + + // operations + const UINT32 *dirty_list(UINT32 &mindirty, UINT32 &maxdirty); + void resize(UINT32 colors); + void mark_dirty(UINT32 index); + void reset(); + + private: + // internal state + dynamic_array<UINT32> m_dirty; // bitmap of dirty entries + UINT32 m_mindirty; // minimum dirty entry + UINT32 m_maxdirty; // minimum dirty entry + }; + + // internal state + palette_t & m_palette; // reference to the palette + palette_client *m_next; // pointer to next client + dirty_state * m_live; // live dirty state + dirty_state * m_previous; // previous dirty state + dirty_state m_dirty[2]; // two dirty states +}; + + +// ======================> palette_t + +// a palette object +class palette_t +{ + friend class palette_client; + +public: + // construction/destruction + palette_t(UINT32 numcolors, UINT32 numgroups = 1); + + // reference counting + void ref() { m_refcount++; } + void deref(); + + // getters + int num_colors() const { return m_numcolors; } + int num_groups() const { return m_numgroups; } + int max_index() const { return m_numcolors * m_numgroups + 2; } + UINT32 black_entry() const { return m_numcolors * m_numgroups + 0; } + UINT32 white_entry() const { return m_numcolors * m_numgroups + 1; } + + // overall adjustments + void set_brightness(float brightness); + void set_contrast(float contrast); + void set_gamma(float gamma); + + // entry getters + rgb_t entry_color(UINT32 index) const { return (index < m_numcolors) ? m_entry_color[index] : RGB_BLACK; } + rgb_t entry_adjusted_color(UINT32 index) const { return (index < m_numcolors * m_numgroups) ? m_adjusted_color[index] : RGB_BLACK; } + float entry_contrast(UINT32 index) const { return (index < m_numcolors) ? m_entry_contrast[index] : 1.0f; } + + // entry setters + void entry_set_color(UINT32 index, rgb_t rgb); + void entry_set_contrast(UINT32 index, float contrast); + + // entry list getters + const rgb_t *entry_list_raw() const { return m_entry_color; } + const rgb_t *entry_list_adjusted() const { return m_adjusted_color; } + const rgb_t *entry_list_adjusted_rgb15() const { return m_adjusted_rgb15; } + + // group adjustments + void group_set_brightness(UINT32 group, float brightness); + void group_set_contrast(UINT32 group, float contrast); + + // utilities + void normalize_range(UINT32 start, UINT32 end, int lum_min = 0, int lum_max = 255); + +private: + // destructor -- can only destruct via + ~palette_t(); + + // internal helpers + rgb_t adjust_palette_entry(rgb_t entry, float brightness, float contrast, const UINT8 *gamma_map); + void update_adjusted_color(UINT32 group, UINT32 index); + + // internal state + UINT32 m_refcount; // reference count on the palette + UINT32 m_numcolors; // number of colors in the palette + UINT32 m_numgroups; // number of groups in the palette + + float m_brightness; // overall brightness value + float m_contrast; // overall contrast value + float m_gamma; // overall gamma value + UINT8 m_gamma_map[256]; // gamma map + + dynamic_array<rgb_t> m_entry_color; // array of raw colors + dynamic_array<float> m_entry_contrast; // contrast value for each entry + dynamic_array<rgb_t> m_adjusted_color; // array of adjusted colors + dynamic_array<rgb_t> m_adjusted_rgb15; // array of adjusted colors as RGB15 + + dynamic_array<float> m_group_bright; // brightness value for each group + dynamic_array<float> m_group_contrast; // contrast value for each group + + palette_client *m_client_list; // list of clients for this palette +}; + + + +//************************************************************************** +// INLINE FUNCTIONS +//************************************************************************** -/*------------------------------------------------- - rgb_to_rgb15 - convert an RGB triplet to - a 15-bit OSD-specified RGB value --------------------------------------------------*/ +//------------------------------------------------- +// rgb_to_rgb15 - convert an RGB triplet to +// a 15-bit OSD-specified RGB value +//------------------------------------------------- inline rgb15_t rgb_to_rgb15(rgb_t rgb) { @@ -177,9 +192,9 @@ inline rgb15_t rgb_to_rgb15(rgb_t rgb) } -/*------------------------------------------------- - rgb_clamp - clamp an RGB component to 0-255 --------------------------------------------------*/ +//------------------------------------------------- +// rgb_clamp - clamp an RGB component to 0-255 +//------------------------------------------------- inline UINT8 rgb_clamp(INT32 value) { @@ -191,9 +206,9 @@ inline UINT8 rgb_clamp(INT32 value) } -/*------------------------------------------------- - pal1bit - convert a 1-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal1bit - convert a 1-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal1bit(UINT8 bits) { @@ -201,9 +216,9 @@ inline UINT8 pal1bit(UINT8 bits) } -/*------------------------------------------------- - pal2bit - convert a 2-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal2bit - convert a 2-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal2bit(UINT8 bits) { @@ -212,9 +227,9 @@ inline UINT8 pal2bit(UINT8 bits) } -/*------------------------------------------------- - pal3bit - convert a 3-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal3bit - convert a 3-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal3bit(UINT8 bits) { @@ -223,9 +238,9 @@ inline UINT8 pal3bit(UINT8 bits) } -/*------------------------------------------------- - pal4bit - convert a 4-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal4bit - convert a 4-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal4bit(UINT8 bits) { @@ -234,9 +249,9 @@ inline UINT8 pal4bit(UINT8 bits) } -/*------------------------------------------------- - pal5bit - convert a 5-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal5bit - convert a 5-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal5bit(UINT8 bits) { @@ -245,9 +260,9 @@ inline UINT8 pal5bit(UINT8 bits) } -/*------------------------------------------------- - pal6bit - convert a 6-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal6bit - convert a 6-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal6bit(UINT8 bits) { @@ -256,9 +271,9 @@ inline UINT8 pal6bit(UINT8 bits) } -/*------------------------------------------------- - pal7bit - convert a 7-bit value to 8 bits --------------------------------------------------*/ +//------------------------------------------------- +// pal7bit - convert a 7-bit value to 8 bits +//------------------------------------------------- inline UINT8 pal7bit(UINT8 bits) { @@ -285,10 +300,10 @@ inline UINT8 palexpand(UINT8 data) } -/*------------------------------------------------- - pal332 - create a 3-3-2 color by extracting - bits from a UINT32 --------------------------------------------------*/ +//------------------------------------------------- +// pal332 - create a 3-3-2 color by extracting +// bits from a UINT32 +//------------------------------------------------- inline rgb_t pal332(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) { @@ -296,10 +311,10 @@ inline rgb_t pal332(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) } -/*------------------------------------------------- - pal444 - create a 4-4-4 color by extracting - bits from a UINT32 --------------------------------------------------*/ +//------------------------------------------------- +// pal444 - create a 4-4-4 color by extracting +// bits from a UINT32 +//------------------------------------------------- inline rgb_t pal444(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) { @@ -307,10 +322,10 @@ inline rgb_t pal444(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) } -/*------------------------------------------------- - pal555 - create a 5-5-5 color by extracting - bits from a UINT32 --------------------------------------------------*/ +//------------------------------------------------- +// pal555 - create a 5-5-5 color by extracting +// bits from a UINT32 +//------------------------------------------------- inline rgb_t pal555(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) { @@ -318,10 +333,10 @@ inline rgb_t pal555(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) } -/*------------------------------------------------- - pal565 - create a 5-6-5 color by extracting - bits from a UINT32 --------------------------------------------------*/ +//------------------------------------------------- +// pal565 - create a 5-6-5 color by extracting +// bits from a UINT32 +//------------------------------------------------- inline rgb_t pal565(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) { @@ -329,10 +344,10 @@ inline rgb_t pal565(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) } -/*------------------------------------------------- - pal888 - create a 8-8-8 color by extracting - bits from a UINT32 --------------------------------------------------*/ +//------------------------------------------------- +// pal888 - create a 8-8-8 color by extracting +// bits from a UINT32 +//------------------------------------------------- inline rgb_t pal888(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) { @@ -340,4 +355,4 @@ inline rgb_t pal888(UINT32 data, UINT8 rshift, UINT8 gshift, UINT8 bshift) } -#endif /* __PALETTE_H__ */ +#endif // __PALETTE_H__ */ |