RAWK
Gigapixel Raw Image Processing Toolkit
 All Classes Functions Variables Pages
image_matrix.hpp
1 // RAWK Copyright (C) 2014 Robert Kooima
2 //
3 // This program is free software: you can redistribute it and/or modify it
4 // under the terms of the GNU General Public License as published by the Free
5 // Software Foundation, either version 3 of the License, or (at your option)
6 // any later version.
7 //
8 // This program is distributed in the hope that it will be useful, but WITH-
9 // OUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 // more details.
12 
13 #ifndef IMAGE_MATRIX_HPP
14 #define IMAGE_MATRIX_HPP
15 
16 //------------------------------------------------------------------------------
17 
19 
20 class matrix : public image
21 {
22 public:
28 
29  matrix(int rows, int columns, std::vector<double> values, image *L)
30  : image(L), rows(rows), columns(columns), values(values)
31  {
32  if (columns != L->get_depth())
33  throw std::runtime_error("Mismatched color matrix size");
34  }
35 
36  virtual double get(int i, int j, int k) const
37  {
38  const int d = L->get_depth();
39  double v = 0;
40 
41  if (0 <= k && k < rows)
42  for (int l = 0; l < d; l++)
43  if (double w = values[k * columns + l])
44  v += w * L->get(i, j, l);
45  return v;
46  }
47 
48  virtual int get_depth() const
49  {
50  return rows;
51  }
52 
53  virtual void doc(std::ostream& out) const
54  {
55  out << "matrix " << rows << " " << columns;
56  }
57 
58 private:
59  int rows;
60  int columns;
61  std::vector<double> values;
62 };
63 
64 //------------------------------------------------------------------------------
65 
67 
68 class rgb2yuv : public matrix
69 {
71 
72 public:
73  rgb2yuv(image *L) : matrix(3, 3, std::vector<double>(v, v + 9), L) { }
74 
75 private:
76  static const double v[9];
77 };
78 
79 const double rgb2yuv::v[9] = {
80  0.29900, 0.58700, 0.11400,
81  -0.14713, 0.28886, 0.43600,
82  0.61500, -0.51499, 0.10001
83 };
84 
85 //------------------------------------------------------------------------------
86 
88 
89 class yuv2rgb : public matrix
90 {
91 public:
93 
94  yuv2rgb(image *L) : matrix(3, 3, std::vector<double>(v, v + 9), L) { }
95 
96 private:
97  static const double v[9];
98 };
99 
100 const double yuv2rgb::v[9] = {
101  1.00000, 0.00000, 1.13983,
102  1.00000, -0.39465, -0.58060,
103  1.00000, 2.03211, 0.00000
104 };
105 
106 //------------------------------------------------------------------------------
107 
108 #endif
virtual double get(int i, int j, int k) const =0
Return the value of the sample at row i, column j, channel k.
image * L
Left child.
Definition: image.hpp:117
Color matrix transformation filter.
Definition: image_matrix.hpp:20
STL namespace.
Base class for all image sources, filters, and operators.
Definition: image.hpp:20
YUV to RGB color space transformation.
Definition: image_matrix.hpp:89
virtual void doc(std::ostream &out) const
Produce a string documenting the function of this object.
Definition: image_matrix.hpp:53
virtual int get_depth() const
Return the depth of this image.
Definition: image.hpp:66
RGB to YUV color space transformation.
Definition: image_matrix.hpp:68
matrix(int rows, int columns, std::vector< double > values, image *L)
Apply a color space transformation to image L. This transformation is given in the form of a matrix o...
Definition: image_matrix.hpp:29
yuv2rgb(image *L)
Transform image L from the YUV to the RGB color space.
Definition: image_matrix.hpp:94
rgb2yuv(image *L)
Transform image L from the RGB to the YUV color space.
Definition: image_matrix.hpp:73
virtual int get_depth() const
Return the depth of this image.
Definition: image_matrix.hpp:48