21 #ifndef G_COLOURSPACE__H
22 #define G_COLOURSPACE__H
30 unsigned int g_colourspace_r(
unsigned int y ,
unsigned int u ,
unsigned int v ) ;
31 unsigned int g_colourspace_g(
unsigned int y ,
unsigned int u ,
unsigned int v ) ;
32 unsigned int g_colourspace_b(
unsigned int y ,
unsigned int u ,
unsigned int v ) ;
33 unsigned int g_colourspace_y(
unsigned int r ,
unsigned int g ,
unsigned int b ) ;
34 unsigned int g_colourspace_u(
unsigned int r ,
unsigned int g ,
unsigned int b ) ;
35 unsigned int g_colourspace_v(
unsigned int r ,
unsigned int g ,
unsigned int b ) ;
85 template <
typename T>
struct triple ;
95 typedef T value_type ;
99 triple( T a , T b , T c ) : m_1(a) , m_2(b) , m_3(c) {}
100 T r() const g__noexcept {
return m_1 ; }
101 T g() const g__noexcept {
return m_2 ; }
102 T b() const g__noexcept {
return m_3 ; }
103 T y() const g__noexcept {
return m_1 ; }
104 T u() const g__noexcept {
return m_2 ; }
105 T v() const g__noexcept {
return m_3 ; }
106 T operator[](
int c )
const {
return c == 0 ? m_1 : ( c == 1 ? m_2 : m_3 ) ; }
117 namespace ColourSpace
122 template <
typename Tmatrix,
typename Tconverter,
typename Ta,
typename Ttriple>
124 typename Tconverter::fp_type
product( Ta a1 , Ta a2 , Ta a3 , Ttriple tfp ) g__noexcept
127 tfp.m_1 * Tconverter::make_fp(a1,Tmatrix::scale) +
128 tfp.m_2 * Tconverter::make_fp(a2,Tmatrix::scale) +
129 tfp.m_3 * Tconverter::make_fp(a3,Tmatrix::scale) ;
135 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
137 typename Tconverter::fp_type
y_imp( Ttriple
rgb ) g__noexcept
139 return product<Tmatrix,Tconverter>( Tmatrix::yr , Tmatrix::yg , Tmatrix::yb ,
rgb ) ;
145 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
147 typename Tconverter::fp_type
u_imp( Ttriple
rgb ) g__noexcept
149 return product<Tmatrix,Tconverter>( Tmatrix::ur , Tmatrix::ug , Tmatrix::ub ,
rgb ) ;
155 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
157 typename Tconverter::fp_type
v_imp( Ttriple
rgb ) g__noexcept
159 return product<Tmatrix,Tconverter>( Tmatrix::vr , Tmatrix::vg , Tmatrix::vb ,
rgb ) ;
165 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
167 typename Tconverter::fp_type
r_imp( Ttriple
yuv ) g__noexcept
169 return product<Tmatrix,Tconverter>( Tmatrix::ry , Tmatrix::ru , Tmatrix::rv ,
yuv ) ;
175 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
177 typename Tconverter::fp_type
g_imp( Ttriple
yuv ) g__noexcept
179 return product<Tmatrix,Tconverter>( Tmatrix::gy , Tmatrix::gu , Tmatrix::gv ,
yuv ) ;
185 template <
typename Tmatrix,
typename Tconverter,
typename Ttriple>
187 typename Tconverter::fp_type
b_imp( Ttriple
yuv ) g__noexcept
189 return product<Tmatrix,Tconverter>( Tmatrix::by , Tmatrix::bu , Tmatrix::bv ,
yuv ) ;
195 template <
typename Tmatrix,
typename Tconverter,
196 typename Trange_rgb ,
typename Trange_y,
typename Trange_uv,
201 map_to_range<Trange_y,Tconverter> ( y_imp<Tmatrix,Tconverter>( map_to_space<Tconverter,Trange_rgb,Trange_rgb,Trange_rgb>(
rgb) ) ) ,
202 map_to_range<Trange_uv,Tconverter>( u_imp<Tmatrix,Tconverter>( map_to_space<Tconverter,Trange_rgb,Trange_rgb,Trange_rgb>(
rgb) ) ) ,
203 map_to_range<Trange_uv,Tconverter>( v_imp<Tmatrix,Tconverter>( map_to_space<Tconverter,Trange_rgb,Trange_rgb,Trange_rgb>(
rgb) ) ) ) ;
209 template <
typename Tmatrix,
typename Tconverter,
210 typename Trange_rgb ,
typename Trange_y,
typename Trange_uv,
214 typedef typename Tmatrix::inverse_type matrix_type ;
216 map_to_range<Trange_rgb,Tconverter>( r_imp<matrix_type,Tconverter>( map_to_space<Tconverter,Trange_y,Trange_uv,Trange_uv>(
yuv) ) ) ,
217 map_to_range<Trange_rgb,Tconverter>( g_imp<matrix_type,Tconverter>( map_to_space<Tconverter,Trange_y,Trange_uv,Trange_uv>(
yuv) ) ) ,
218 map_to_range<Trange_rgb,Tconverter>( b_imp<matrix_type,Tconverter>( map_to_space<Tconverter,Trange_y,Trange_uv,Trange_uv>(
yuv) ) ) ) ;
239 inline unsigned char r_int(
unsigned char y ,
unsigned char ,
unsigned char v ) g__noexcept
241 const int r = table_r_from_y[y] + table_r_from_v[v] ;
242 return clamp<range_rgb,unsigned char>(r) ;
247 inline unsigned char g_int(
unsigned char y ,
unsigned char u ,
unsigned char v ) g__noexcept
249 const int g = table_g_from_y[y] + table_g_from_u[u] + table_g_from_v[v] ;
250 return clamp<range_rgb,unsigned char>(g) ;
255 inline unsigned char b_int(
unsigned char y ,
unsigned char u ,
unsigned char ) g__noexcept
257 const int b = table_b_from_y[y] + table_b_from_u[u] ;
258 return clamp<range_rgb,unsigned char>(b) ;
270 inline unsigned char y_int(
unsigned char r ,
unsigned char g ,
unsigned char b )
272 const int y = table_y_from_r[r] + table_y_from_g[g] + table_y_from_b[b] ;
273 return clamp<range_y,unsigned char>( y + range_y::min ) ;
278 inline unsigned char u_int(
unsigned char r ,
unsigned char g ,
unsigned char b )
280 static g__constexpr
int bias = (
static_cast<int>(range_uv::max) + static_cast<int>(range_uv::min) ) / 2 ;
281 const int u = table_u_from_r[r] + table_u_from_g[g] + table_u_from_b[b] ;
282 return clamp<range_uv,unsigned char>( u + bias ) ;
287 inline unsigned char v_int(
unsigned char r ,
unsigned char g ,
unsigned char b )
289 static g__constexpr
int bias = (
static_cast<int>(range_uv::max) + static_cast<int>(range_uv::min) ) / 2 ;
290 const int v = table_v_from_r[r] + table_v_from_g[g] + table_v_from_b[b] ;
291 return clamp<range_uv,unsigned char>( v + bias ) ;
311 template <
typename T>
316 typedef std::vector<triple_t> table_t ;
326 if( table.empty() ) table.resize( 256*256*256 ,
triple_t(0,0,0) ) ;
327 typename table_t::iterator p = table.begin() ;
328 for(
unsigned int a = 0 ; a <= 255 ; ++a )
329 for(
unsigned int b = 0 ; b <= 255 ; ++b )
330 for(
unsigned int c = 0 ; c <= 255 ; ++c )
335 typedef g_uint32_t index_t ;
337 (
static_cast<index_t
>(abc.m_1)<<16) |
338 (
static_cast<index_t
>(abc.m_2)<<8) |
339 (
static_cast<index_t
>(abc.m_3)) ] ;
341 template <
typename Tout>
342 Tout value( T a , T b , T c )
const
344 typedef g_uint32_t index_t ;
345 const index_t index =
346 (
static_cast<index_t
>(a)<<16) |
347 (
static_cast<index_t
>(b)<<8) |
348 (
static_cast<index_t
>(c)) ;
350 (static_cast<Tout>(table[index].m_1)<<16) |
351 (static_cast<Tout>(table[index].m_2)<<8) |
352 (static_cast<Tout>(table[index].m_3)) ;
triple< unsigned char > rgb(triple< unsigned char > yuv) g__noexcept
A top-level function that calculates rgb from yuv with default implementation options.
unsigned char v_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to v.
unsigned char b_int(unsigned char y, unsigned char u, unsigned char) g__noexcept
A fast conversion from yuv to b.
triple< unsigned char > rgb_int(triple< unsigned char > yuv) g__noexcept
A fast conversion from yuv to rgb.
A 3-tuple for holding rgb or yuv triples.
g__constexpr Tconverter::fp_type product(Ta a1, Ta a2, Ta a3, Ttriple tfp) g__noexcept
Does a cross-product using fp types.
unsigned char u_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to u.
Ttriple rgb_imp(Ttriple yuv) g__noexcept
A top-level function that calculates rgb from yuv with all the implementation options exposed as temp...
unsigned char g_int(unsigned char y, unsigned char u, unsigned char v) g__noexcept
A fast conversion from yuv to g.
g__constexpr Tconverter::fp_type y_imp(Ttriple rgb) g__noexcept
Calculates y from rgb using fp types.
g__constexpr Tconverter::fp_type u_imp(Ttriple rgb) g__noexcept
Calculates u (cb) from rgb using fp types.
Ttriple yuv_imp(Ttriple rgb) g__noexcept
A top-level function that calculates yuv from rgb with all the implementation options exposed as temp...
g__constexpr Tconverter::fp_type g_imp(Ttriple yuv) g__noexcept
Calculates g from yuv using fp types.
g__constexpr Tconverter::fp_type v_imp(Ttriple rgb) g__noexcept
Calculates v (cr) from rgb using fp types.
unsigned char r_int(unsigned char y, unsigned char, unsigned char v) g__noexcept
A fast conversion from yuv to r.
unsigned char y_int(unsigned char r, unsigned char g, unsigned char b)
A fast conversion from rgb to y.
Defines the "digital" range for y component.
g__constexpr Tconverter::fp_type b_imp(Ttriple yuv) g__noexcept
Calculates b from yuv using fp types.
Defines the "digital" range for u/v components.
triple< unsigned char > yuv(triple< unsigned char > rgb) g__noexcept
A top-level function that calculates yuv from rgb with default implementation options.
This file has been machine generated – do not edit.
Defines the "digital" range for r/g/b components.
A cache for a complete set of rgb/yuv mappings, amounting to about 50MB of data.
triple< unsigned char > yuv_int(triple< unsigned char > rgb) g__noexcept
A fast conversion from rgb to yuv.
g__constexpr Tconverter::fp_type r_imp(Ttriple yuv) g__noexcept
Calculates r from yuv using fp types.
This file has been machine generated – do not edit.