AliceVision
Photogrammetric Computer Vision Framework
pixelTypes.hpp
1 // This file is part of the AliceVision project.
2 // Copyright (c) 2016 AliceVision contributors.
3 // Copyright (c) 2012 openMVG contributors.
4 // This Source Code Form is subject to the terms of the Mozilla Public License,
5 // v. 2.0. If a copy of the MPL was not distributed with this file,
6 // You can obtain one at https://mozilla.org/MPL/2.0/.
7 
8 #pragma once
9 
10 #include "aliceVision/numeric/numeric.hpp"
11 
12 namespace aliceVision {
13 namespace image {
14 
19 template<typename T>
20 class Rgb : public Eigen::Matrix<T, 3, 1, 0, 3, 1>
21 {
22  using Base = Eigen::Matrix<T, 3, 1, 0, 3, 1>;
23  using TBase = T;
24 
25  public:
26  //------------------------------
27  //-- construction method
34  inline Rgb(T red, T green, T blue)
35  : Base(red, green, blue)
36  {}
37 
42  explicit inline Rgb(const Base& val)
43  : Base(val)
44  {}
45 
51  explicit inline Rgb(const T val = 0)
52  : Base(val, val, val)
53  {}
54  //-- construction method
55  //------------------------------
56 
57  //------------------------------
58  //-- accessors/getters methods
63  inline const T& r() const { return (*this)(0); }
64 
69  inline T& r() { return (*this)(0); }
70 
75  inline const T& g() const { return (*this)(1); }
76 
81  inline T& g() { return (*this)(1); }
82 
87  inline const T& b() const { return (*this)(2); }
88 
93  inline T& b() { return (*this)(2); }
94 
99  inline operator T() const { return T(0.299 * r() + 0.587 * g() + 0.114 * b()); }
100 
101  //-- accessors/getters methods
102  //------------------------------
103 
110  friend std::ostream& operator<<(std::ostream& os, const Rgb& col)
111  {
112  os << " {";
113  for (int i = 0; i < 2; ++i)
114  {
115  os << col(i) << ",";
116  }
117  os << col(2) << "} ";
118  return os;
119  }
120 
127  inline Rgb operator-(const Rgb& other) const { return Rgb(((*this)(0) - other(0)), ((*this)(1) - other(1)), ((*this)(2) - other(2))); }
128 
135  inline Rgb operator+(const Rgb& other) const { return Rgb(((*this)(0) + other(0)), ((*this)(1) + other(1)), ((*this)(2) + other(2))); }
136 
143  inline Rgb operator*(const Rgb& other) const { return Rgb(((*this)(0) * other(0)), ((*this)(1) * other(1)), ((*this)(2) * other(2))); }
144 
151  template<typename Z>
152  inline Rgb operator/(const Z& val) const
153  {
154  return Rgb(T((Z)((*this)(0)) / val), T((Z)((*this)(1)) / val), T((Z)((*this)(2)) / val));
155  }
156 
163  template<typename Z>
164  inline Rgb operator*(const Z& val) const
165  {
166  return Rgb(T((Z)(*this)(0) * val), T((Z)(*this)(1) * val), T((Z)(*this)(2) * val));
167  }
168 };
169 
173 using RGBfColor = Rgb<float>;
174 
178 template<typename T>
179 class Rgba : public Eigen::Matrix<T, 4, 1, 0, 4, 1>
180 {
181  using Base = Eigen::Matrix<T, 4, 1, 0, 4, 1>;
182 
183  public:
184  //------------------------------
185  //-- construction method
193  inline Rgba(const T red, const T green, const T blue, const T alpha = T(1))
194  : Base(red, green, blue, alpha)
195  {}
196 
201  explicit inline Rgba(const Base& val)
202  : Base(val)
203  {}
204 
210  explicit inline Rgba(const T val)
211  : Base(val, val, val, T(1))
212  {}
213 
220  explicit inline Rgba()
221  : Base(T(0), T(0), T(0), T(0))
222  {}
223 
228  inline Rgba(const RGBColor& val, const T alpha)
229  : Base(val.r(), val.g(), val.b(), alpha)
230  {}
231 
232  //-- construction method
233  //------------------------------
234 
235  //------------------------------
236  //-- accessors/getters methods
241  inline const T& r() const { return (*this)(0); }
246  inline T& r() { return (*this)(0); }
251  inline const T& g() const { return (*this)(1); }
256  inline T& g() { return (*this)(1); }
261  inline const T& b() const { return (*this)(2); }
266  inline T& b() { return (*this)(2); }
271  inline const T& a() const { return (*this)(3); }
276  inline T& a() { return (*this)(3); }
277 
282  inline operator T() const { return T(0.299 * r() + 0.587 * g() + 0.114 * b()); }
283  //-- accessors/getters methods
284  //------------------------------
291  friend std::ostream& operator<<(std::ostream& os, const Rgba& col)
292  {
293  os << " {";
294  for (int i = 0; i < 3; ++i)
295  {
296  os << col(i) << ",";
297  }
298  os << col(3) << "} ";
299  return os;
300  }
301 
308  template<class Z>
309  inline Rgba operator/(const Z& val) const
310  {
311  return Rgba(T((Z)(*this)(0) / val), T((Z)(*this)(1) / val), T((Z)(*this)(2) / val), T((Z)(*this)(3) / val));
312  }
313 
320  template<class Z>
321  inline Rgba operator*(const Z& val) const
322  {
323  return Rgba(T((Z)(*this)(0) * val), T((Z)(*this)(1) * val), T((Z)(*this)(2) * val), T((Z)(*this)(3) * val));
324  }
325 
332  inline Rgba operator+(const Rgba& other) const
333  {
334  return Rgba(((*this)(0) + other(0)), ((*this)(1) + other(1)), ((*this)(2) + other(2)), ((*this)(3) + other(3)));
335  }
336 };
337 
339 typedef Rgba<float> RGBAfColor;
340 
344 using RGBAfColor = Rgba<float>;
345 
346 const RGBColor WHITE(255, 255, 255);
347 const RGBColor BLACK(0, 0, 0);
348 const RGBColor BLUE(0, 0, 255);
349 const RGBColor RED(255, 0, 0);
350 const RGBColor GREEN(0, 255, 0);
351 const RGBColor YELLOW(255, 255, 0);
352 const RGBColor CYAN(0, 255, 255);
353 const RGBColor MAGENTA(255, 0, 255);
354 
355 const RGBfColor FWHITE(1.0f, 1.0f, 1.0f);
356 const RGBfColor FBLACK(.0f, .0f, .0f);
357 
358 template<typename T>
360 {
361  // no size parameter, so no default value.
362  // An error will be raise at compile time if this type traits is not defined.
363 };
364 
365 template<>
366 struct NbChannels<unsigned char>
367 {
368  static const int size = 1;
369 };
370 template<>
371 struct NbChannels<float>
372 {
373  static const int size = 1;
374 };
375 template<>
377 {
378  static const int size = 3;
379 };
380 template<>
382 {
383  static const int size = 3;
384 };
385 template<>
387 {
388  static const int size = 4;
389 };
390 template<>
392 {
393  static const int size = 4;
394 };
395 
396 } // namespace image
397 } // namespace aliceVision
aliceVision::image::Rgba::operator*
Rgba operator*(const Z &val) const
scalar multiplication
Definition: pixelTypes.hpp:321
aliceVision::image::Rgb::b
const T & b() const
Get readonly Blue channel value.
Definition: pixelTypes.hpp:87
aliceVision::image::Rgb::g
const T & g() const
Get readonly Green channel value.
Definition: pixelTypes.hpp:75
aliceVision::image::Rgba::operator/
Rgba operator/(const Z &val) const
scalar division
Definition: pixelTypes.hpp:309
aliceVision::image::Rgb::operator*
Rgb operator*(const Z &val) const
scalar multiplication
Definition: pixelTypes.hpp:164
aliceVision::image::Rgba::a
const T & a() const
Get readonly Alpha channel value.
Definition: pixelTypes.hpp:271
aliceVision::image::Rgba
RGBA templated pixel type.
Definition: pixelTypes.hpp:179
aliceVision::image::Rgb::Rgb
Rgb(const T val=0)
Single value construction.
Definition: pixelTypes.hpp:51
aliceVision::image::Rgba::g
T & g()
Get rw Green channel value.
Definition: pixelTypes.hpp:256
aliceVision::image::Rgb::operator<<
friend std::ostream & operator<<(std::ostream &os, const Rgb &col)
stream operator
Definition: pixelTypes.hpp:110
aliceVision
Definition: checkerDetector.cpp:32
aliceVision::image::Rgba::g
const T & g() const
Get readonly Green channel value.
Definition: pixelTypes.hpp:251
aliceVision::image::Rgb::Rgb
Rgb(T red, T green, T blue)
Full constructor.
Definition: pixelTypes.hpp:34
aliceVision::image::Rgb::g
T & g()
Get rw Green channel value.
Definition: pixelTypes.hpp:81
aliceVision::image::Rgb::r
T & r()
Get rw Red channel value.
Definition: pixelTypes.hpp:69
aliceVision::image::Rgba::r
const T & r() const
Get readonly Red channel value.
Definition: pixelTypes.hpp:241
aliceVision::image::Rgb::operator*
Rgb operator*(const Rgb &other) const
Elementwise multiplication.
Definition: pixelTypes.hpp:143
aliceVision::image::Rgb::Rgb
Rgb(const Base &val)
Copy constructor.
Definition: pixelTypes.hpp:42
aliceVision::image::Rgba::Rgba
Rgba(const T val)
RGBA constructor with default alpha value to 1.
Definition: pixelTypes.hpp:210
aliceVision::image::NbChannels
Definition: pixelTypes.hpp:359
aliceVision::image::Rgb::operator+
Rgb operator+(const Rgb &other) const
Elementwise addition.
Definition: pixelTypes.hpp:135
aliceVision::image::Rgba::Rgba
Rgba(const T red, const T green, const T blue, const T alpha=T(1))
Full constructor.
Definition: pixelTypes.hpp:193
aliceVision::image::Rgb::operator/
Rgb operator/(const Z &val) const
scalar division
Definition: pixelTypes.hpp:152
aliceVision::image::Rgba::operator<<
friend std::ostream & operator<<(std::ostream &os, const Rgba &col)
stream operator
Definition: pixelTypes.hpp:291
aliceVision::image::Rgba::a
T & a()
Get rw Alpha channel value.
Definition: pixelTypes.hpp:276
aliceVision::image::Rgba::r
T & r()
Get rw Red channel value.
Definition: pixelTypes.hpp:246
aliceVision::image::Rgb::b
T & b()
Get rw Blue channel value.
Definition: pixelTypes.hpp:93
aliceVision::image::Rgba::Rgba
Rgba()
Default RGBA constructor set all channels to zero (including the alpha channel)
Definition: pixelTypes.hpp:220
aliceVision::image::Rgb::r
const T & r() const
Get readonly Red channel value.
Definition: pixelTypes.hpp:63
aliceVision::image::Rgb
Definition: pixelTypes.hpp:20
aliceVision::image::Rgba::Rgba
Rgba(const RGBColor &val, const T alpha)
Copy constructor.
Definition: pixelTypes.hpp:228
aliceVision::image::Rgba::operator+
Rgba operator+(const Rgba &other) const
Elementwise addition.
Definition: pixelTypes.hpp:332
aliceVision::image::Rgba::b
T & b()
Get rw Blue channel value.
Definition: pixelTypes.hpp:266
aliceVision::image::Rgba::b
const T & b() const
Get readonly Blue channel value.
Definition: pixelTypes.hpp:261
aliceVision::image::Rgba::Rgba
Rgba(const Base &val)
Copy constructor from internal data.
Definition: pixelTypes.hpp:201
aliceVision::image::Rgb::operator-
Rgb operator-(const Rgb &other) const
Elementwise substraction.
Definition: pixelTypes.hpp:127