Templates -- Meow  204.13.18
A C++ template contains kinds of interesting classes and functions
YUV_Space.h
Go to the documentation of this file.
1 #ifndef colors_YUV_Space_H__
2 #define colors_YUV_Space_H__
3 
4 #include "Color3_Space.h"
5 #include "../geo/Vectors.h"
6 
7 #include "RGB_Space.h"
8 #include "../math/utility.h"
9 
10 #include <cstdlib>
11 
12 namespace meow {
13 
21 class YUVf_Space: public Color3_Space<double> {
22 public:
24  Vector3D<double>(1.0, 1.0, 1.0),
25  Vector3D<double>(0.0, 0.0, 0.0)) {
26  }
27  YUVf_Space(double c): Color3_Space<double>(Vector3D<double>(0.0, 0.0, 0.0),
28  Vector3D<double>(1.0, 1.0, 1.0),
29  Vector3D<double>( c, c, c)) {
30  }
32  Color3_Space<double>(Vector3D<double>(0.0, 0.0, 0.0),
33  Vector3D<double>(1.0, 1.0, 1.0),
34  Vector3D<double>(v)) {
35  }
36  YUVf_Space(YUV_Space const& b): Color3_Space<double>(b) {
37  }
39  }
40  double const& yuvMin(size_t i) const { return min(i); }
41  double const& yMin( ) const { return min(0); }
42  double const& uMin( ) const { return min(1); }
43  double const& vMin( ) const { return min(2); }
44  double const& yuvMax(size_t i) const { return max(i); }
45  double const& yMax( ) const { return max(0); }
46  double const& uMax( ) const { return max(1); }
47  double const& vMax( ) const { return max(2); }
48  double const& yuv(size_t i) const { return val(i); }
49  double const& y( ) const { return yuv(0); }
50  double const& u( ) const { return yuv(1); }
51  double const& v( ) const { return yuv(2); }
52  double const& yuv(size_t i, double c) { return val(i, c); }
53  double const& y( double c) { return yuv(0, c); }
54  double const& u( double c) { return yuv(1, c); }
55  double const& v( double c) { return yuv(2, c); }
56  double& yuvGet(size_t i) { return valGet(i); }
57  double& yGet( ) { return yuvGet(0); }
58  double& uGet( ) { return yuvGet(1); }
59  double& vGet( ) { return yuvGet(2); }
61  copyFrom(b);
62  return *this;
63  }
64  YUVf_Space operator+(YUVf_Space const& b) const {
65  return YUVf_Space(val_ + b.val_);
66  }
67  YUVf_Space operator-(YUVf_Space const& b) const {
68  return YUVf_Space(val_ - b.val_);
69  }
70  YUVf_Space operator*(double const& c) const {
71  return YUVf_Space(val_ * c);
72  }
73  YUVf_Space operator/(double const& c) const {
74  return YUVf_Space(val_ / c);
75  }
76  double operator*(YUVf_Space const& b) const {
77  return val_ * b.val_;
78  }
79 };
80 
84 inline void colorTransformate(RGBf_Space const& rgb, YUVf_Space* yuv) {
85  double r = normalize(rgb.rMin(), rgb.rMax(), rgb.r());
86  double g = normalize(rgb.gMin(), rgb.gMax(), rgb.g());
87  double b = normalize(rgb.bMin(), rgb.bMax(), rgb.b());
88  double y = 0.299 * r + 0.587 * g + 0.114 * b;
89  double u = -0.169 * r - 0.331 * g + 0.500 * b + 0.5;
90  double v = 0.500 * r - 0.419 * g - 0.081 * b + 0.5;
91  yuv->y(denormalize(yuv->yMin(), yuv->yMax(), y));
92  yuv->u(denormalize(yuv->uMin(), yuv->uMax(), u));
93  yuv->v(denormalize(yuv->vMin(), yuv->vMax(), v));
94 }
95 
99 inline void colorTransformate(YUVf_Space const& yuv, RGBf_Space* rgb) {
100  double y = normalize(yuv.yMin(),yuv.yMax(),yuv.y());
101  double u = normalize(yuv.uMin(),yuv.uMax(),yuv.u());
102  double v = normalize(yuv.vMin(),yuv.vMax(),yuv.v());
103  double r = y - 0.00093 * (u - 0.5) + 1.401687 * (v - 0.5);
104  double g = y - 0.34370 * (u - 0.5) - 0.714170 * (v - 0.5);
105  double b = y + 1.77216 * (u - 0.5) - 0.000990 * (v - 0.5);
106  rgb->r(denormalize(rgb->rMin(), rgb->rMax(), r));
107  rgb->g(denormalize(rgb->gMin(), rgb->gMax(), g));
108  rgb->b(denormalize(rgb->bMin(), rgb->bMax(), b));
109 }
110 
114 inline void colorTransformate(RGBi_Space const& rgb, YUVf_Space* yuv) {
115  RGBf_Space tmp;
116  tmp.copyFrom(rgb);
117  colorTransformate(tmp, yuv);
118 }
119 
123 inline void colorTransformate(YUVf_Space const& yuv, RGBi_Space* rgb) {
124  RGBf_Space tmp;
125  colorTransformate(yuv, &tmp);
126  rgb->copyFrom(tmp);
127 }
128 
129 } // meow
130 
131 #endif // colors_YUV_H__
double const & yMax() const
Definition: YUV_Space.h:45
T normalize(T lower, T upper, T value)
(value-lower)/(upper-lower)
Definition: utility.h:27
double const & rMin() const
Definition: RGB_Space.h:106
double const & y() const
Definition: YUV_Space.h:49
double const & v() const
Definition: YUV_Space.h:51
void colorTransformate(RGBf_Space const &rgb, HSLf_Space *hsl)
RGBf_Space to HSLf_Space
Definition: HSL_Space.h:85
YUVf_Space(YUV_Space const &b)
Definition: YUV_Space.h:36
以浮點數Red, Green, Blue 三個值所組成的色彩空間
Definition: RGB_Space.h:86
double const & u() const
Definition: YUV_Space.h:50
doubleconst & min(size_t id) const
Return the minimum of the i -th channel.
Definition: Color3_Space.h:99
double const & yuv(size_t i, double c)
Definition: YUV_Space.h:52
double const & v(double c)
Definition: YUV_Space.h:55
double & yuvGet(size_t i)
Definition: YUV_Space.h:56
YUVf_Space & operator=(YUVf_Space const &b)
Definition: YUV_Space.h:60
double const & u(double c)
Definition: YUV_Space.h:54
double & valGet(size_t id)
Get the non-constant reference of value of the i -th channel.
Definition: Color3_Space.h:139
double & vGet()
Definition: YUV_Space.h:59
double const & uMin() const
Definition: YUV_Space.h:42
double operator*(YUVf_Space const &b) const
Definition: YUV_Space.h:76
T denormalize(T lower, T upper, T _ratio)
(lower+_ratio*(upper-lower))
Definition: utility.h:35
double const & r() const
Definition: RGB_Space.h:114
doubleconst & max(size_t id) const
Return the maximum of the i -th channel.
Definition: Color3_Space.h:107
double & yGet()
Definition: YUV_Space.h:57
double const & yMin() const
Definition: YUV_Space.h:41
以浮點數Y(亮度), U(色度), V(濃度) 三個值所組成的色彩空間
Definition: YUV_Space.h:21
Base class of color space with 3 channels.
Definition: Color3_Space.h:18
double const & yuvMin(size_t i) const
Definition: YUV_Space.h:40
double & uGet()
Definition: YUV_Space.h:58
3D's vector
Definition: Vectors.h:265
YUVf_Space operator/(double const &c) const
Definition: YUV_Space.h:73
double const & gMin() const
Definition: RGB_Space.h:107
以整數 Red, Green, Blue 三個值所組成的色彩空間
Definition: RGB_Space.h:19
YUVf_Space(double c)
Definition: YUV_Space.h:27
double const & bMax() const
Definition: RGB_Space.h:112
double const & rMax() const
Definition: RGB_Space.h:110
YUVf_Space(Vector3D< double > const &v)
Definition: YUV_Space.h:31
YUVf_Space operator+(YUVf_Space const &b) const
Definition: YUV_Space.h:64
double const & gMax() const
Definition: RGB_Space.h:111
double const & b() const
Definition: RGB_Space.h:116
YUVf_Space operator*(double const &c) const
Definition: YUV_Space.h:70
double const & g() const
Definition: RGB_Space.h:115
double const & bMin() const
Definition: RGB_Space.h:108
double const & yuv(size_t i) const
Definition: YUV_Space.h:48
Color3_Space< double > & copyFrom(Color3_Space< double > const &b)
Copy method.
Definition: Color3_Space.h:54
double const & yuvMax(size_t i) const
Definition: YUV_Space.h:44
double const & uMax() const
Definition: YUV_Space.h:46
YUVf_Space operator-(YUVf_Space const &b) const
Definition: YUV_Space.h:67
doubleconst & val(size_t id) const
Return the value of the i -th channel.
Definition: Color3_Space.h:117
Vector3D< double > val_
Definition: Color3_Space.h:22
double const & y(double c)
Definition: YUV_Space.h:53
double const & vMin() const
Definition: YUV_Space.h:43
double const & vMax() const
Definition: YUV_Space.h:47