#ifndef CVD_RGB_TRAITS_H #define CVD_RGB_TRAITS_H #include #include #include #include #include #include #include #include namespace CVD { namespace Pixel { template struct Component> { typedef P type; static const size_t count = 3; //This version is much faster, with -funroll-loops static const P& get(const Rgb

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue); } static P& get(Rgb

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); // return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue); } }; template struct Component> { typedef P type; static const size_t count = 3; //This version is much faster, with -funroll-loops static const P& get(const Bgrx

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.blue : (i==1 ? pixel.green : pixel.red); } static P& get(Bgrx

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); // return i == 0 ? pixel.blue : (i==1 ? pixel.green : pixel.red); } }; template <> struct Component { typedef unsigned char type; static const size_t count = 3; static const type& get(const Rgb8& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue); } static type& get(Rgb8& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.red : (i==1 ? pixel.green : pixel.blue); } }; template struct Component> { typedef P type; static const size_t count = 4; static const P& get(const Rgba

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.red : (i==1 ? pixel.green : (i==2 ?pixel.blue: pixel.alpha)); } static P& get(Rgba

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); //return i == 0 ? pixel.red : (i==1 ? pixel.green : (i==2 ?pixel.blue: pixel.alpha)); } }; template struct Component> { typedef P type; static const size_t count = 2; static const P& get(const La

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); } static P& get(La

& pixel, size_t i) { return *(reinterpret_cast(&pixel) + i); } }; template struct Component> { typedef P type; static const size_t count = 4; static const P& get(const Argb

& pixel, size_t i) { //return *(reinterpret_cast(&pixel)+i); return i == 0 ? pixel.red : (i == 1 ? pixel.green : (i == 2 ? pixel.blue : pixel.alpha)); } static P& get(Argb

& pixel, size_t i) { //return *(reinterpret_cast(&pixel)+i); return i == 0 ? pixel.red : (i == 1 ? pixel.green : (i == 2 ? pixel.blue : pixel.alpha)); } }; template struct is_Rgb { enum { value = 0 }; }; template struct is_Rgb> { enum { value = 1 }; }; template <> struct is_Rgb { enum { value = 1 }; }; template struct is_Rgb> { enum { value = 1 }; }; template struct is_Rgb> { enum { value = 1 }; }; template struct traits, LIFT> { typedef Rgb::wider_type> wider_type; typedef Rgb::float_type> float_type; }; template struct traits, LIFT> { typedef Rgba::wider_type> wider_type; typedef Rgba::float_type> float_type; }; template struct traits, LIFT> { typedef La::wider_type> wider_type; typedef La::float_type> float_type; }; template struct traits { typedef Rgb wider_type; typedef Rgb float_type; }; } template struct Rgb_ops { template static inline T sum(const T& a, const S& b) { return T(a.red + b.red, a.green + b.green, a.blue + b.blue); } template static inline void add(T& a, const S& b) { a.red += b.red; a.green += b.green; a.blue += b.blue; } template static inline T diff(const T& a, const S& b) { return T(a.red - b.red, a.green - b.green, a.blue - b.blue); } template static inline void sub(T& a, const S& b) { a.red -= b.red; a.green -= b.green; a.blue -= b.blue; } template static inline T prod(const T& a, const S& b) { return T(a.red * b, a.green * b, a.blue * b); } template static inline void mul(T& a, const S& b) { a.red *= b; a.green *= b; a.blue *= b; } template static inline T quot(const T& a, const S& b) { return T(a.red / b, a.green / b, a.blue / b); } template static inline void div(T& a, const S& b) { a.red /= b; a.green /= b; a.blue /= b; } template static inline void assign(T& a, const S& b) { a.red = b.red; a.green = b.green; a.blue = b.blue; } }; template inline Rgb operator+(const Rgb& a, const Rgb& b) { return Rgb_ops>::sum(a, b); } template inline Rgb& operator+=(Rgb& a, const Rgb& b) { Rgb_ops>::add(a, b); return a; } template inline Rgb operator-(const Rgb& a, const Rgb& b) { return Rgb_ops>::diff(a, b); } template inline Rgb& operator-=(Rgb& a, const Rgb& b) { Rgb_ops>::sub(a, b); return a; } template inline Rgb operator*(const Rgb& a, const S& b) { return Rgb_ops>::prod(a, b); } template inline Rgb operator*(const S& b, const Rgb& a) { return Rgb_ops>::prod(a, b); } template inline Rgb& operator*=(Rgb& a, const S& b) { Rgb_ops>::mul(a, b); return a; } template inline Rgb operator/(const Rgb& a, const S& b) { return Rgb_ops>::quot(a, b); } template inline Rgb operator/(const S& b, const Rgb& a) { return Rgb_ops>::quot(a, b); } template inline Rgb& operator/=(Rgb& a, const S& b) { Rgb_ops>::div(a, b); return a; } template struct Rgba_ops { template static inline T sum(const T& a, const S& b) { return T(a.red + b.red, a.green + b.green, a.blue + b.blue, a.alpha + b.alpha); } template static inline void add(T& a, const S& b) { a.red += b.red; a.green += b.green; a.blue += b.blue; a.alpha += b.alpha; } template static inline T diff(const T& a, const S& b) { return T(a.red - b.red, a.green - b.green, a.blue - b.blue, a.alpha - b.alpha); } template static inline void sub(T& a, const S& b) { a.red -= b.red; a.green -= b.green; a.blue -= b.blue; a.alpha -= b.alpha; } template static inline T prod(const T& a, const S& b) { return T(a.red * b, a.green * b, a.blue * b, a.alpha * b); } template static inline void mul(T& a, const S& b) { a.red *= b; a.green *= b; a.blue *= b; a.alpha *= b; } template static inline T quot(const T& a, const S& b) { return T(a.red / b, a.green / b, a.blue / b, a.alpha / b); } template static inline void div(T& a, const S& b) { a.red /= b; a.green /= b; a.blue /= b; a.alpha /= b; } template static inline void assign(T& a, const S& b) { a.red = b.red; a.green = b.green; a.blue = b.blue; a.alpha = b.alpha; } }; template inline Rgba operator+(const Rgba& a, const Rgba& b) { return Rgba_ops>::sum(a, b); } template inline Rgba& operator+=(Rgba& a, const Rgba& b) { Rgba_ops>::add(a, b); return a; } template inline Rgba operator-(const Rgba& a, const Rgba& b) { return Rgba_ops>::diff(a, b); } template inline Rgba& operator-=(Rgba& a, const Rgba& b) { Rgba_ops>::sub(a, b); return a; } template inline Rgba operator*(const Rgba& a, const S& b) { return Rgba_ops>::prod(a, b); } template inline Rgba operator*(const S& b, const Rgba& a) { return Rgba_ops>::prod(a, b); } template inline Rgba& operator*=(Rgba& a, const S& b) { Rgba_ops>::mul(a, b); return a; } template inline Rgba operator/(const Rgba& a, const S& b) { return Rgba_ops>::quot(a, b); } template inline Rgba operator/(const S& b, const Rgba& a) { return Rgba_ops>::quot(a, b); } template inline Rgba& operator/=(Rgba& a, const S& b) { Rgba_ops>::div(a, b); return a; } template struct La_ops { template static inline T sum(const T& a, const S& b) { return T(a.luminance + b.luminance, a.alpha + b.alpha); } template static inline void add(T& a, const S& b) { a.luminance += b.luminance; a.alpha += b.alpha; } template static inline T diff(const T& a, const S& b) { return T(a.luminance - b.luminance, a.alpha - b.alpha); } template static inline void sub(T& a, const S& b) { a.luminance -= b.luminance; a.alpha -= b.alpha; } template static inline T prod(const T& a, const S& b) { return T(a.luminance * b, a.alpha * b); } template static inline void mul(T& a, const S& b) { a.luminance *= b; a.alpha *= b; } template static inline T quot(const T& a, const S& b) { return T(a.luminance / b, a.alpha / b); } template static inline void div(T& a, const S& b) { a.luminance /= b; a.alpha /= b; } template static inline void assign(T& a, const S& b) { a.luminance = b.luminance; a.alpha = b.alpha; } }; template inline La operator+(const La& a, const La& b) { return La_ops>::sum(a, b); } template inline La& operator+=(La& a, const La& b) { La_ops>::add(a, b); return a; } template inline La operator-(const La& a, const La& b) { return La_ops>::diff(a, b); } template inline La& operator-=(La& a, const La& b) { La_ops>::sub(a, b); return a; } template inline La operator*(const La& a, const S& b) { return La_ops>::prod(a, b); } template inline La operator*(const S& b, const La& a) { return La_ops>::prod(a, b); } template inline La& operator*=(La& a, const S& b) { La_ops>::mul(a, b); return a; } template inline La operator/(const La& a, const S& b) { return La_ops>::quot(a, b); } template inline La operator/(const S& b, const La& a) { return La_ops>::quot(a, b); } template inline La& operator/=(La& a, const S& b) { La_ops>::div(a, b); return a; } } #endif