Beatmup
matrix.cpp
Go to the documentation of this file.
1 /*
2  Beatmup image and signal processing library
3  Copyright (C) 2019, lnstadrum
4 
5  This program is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 
19 #include "matrix.h"
20 #include "color_spaces.h"
21 #include <cstring>
22 
23 using namespace Beatmup;
24 using namespace Color;
25 
26 #define MUL(a,b,i,j) (a[i][0] * b[0][j] + a[i][1] * b[1][j] + a[i][2] * b[2][j] + a[i][3] * b[3][j])
27 
29  r() = color4f{ 1, 0, 0, 0 };
30  g() = color4f{ 0, 1, 0, 0 };
31  b() = color4f{ 0, 0, 1, 0 };
32  a() = color4f{ 0, 0, 0, 1 };
33 }
34 
35 Matrix::Matrix(const color4f& r, const color4f& g, const color4f& b, const color4f& a) {
36  this->r() = r;
37  this->g() = g;
38  this->b() = b;
39  this->a() = a;
40 }
41 
43  return rows[row];
44 }
45 
46 
47 color4f Matrix::operator[](int row) const {
48  return rows[row];
49 }
50 
51 
52 Matrix Matrix::operator *(const Matrix& another) const {
53  Matrix _;
54  for (int i = 0; i < 4; i++) {
55  _.elem[i][0] = MUL(elem, another.elem, i, 0);
56  _.elem[i][1] = MUL(elem, another.elem, i, 1);
57  _.elem[i][2] = MUL(elem, another.elem, i, 2);
58  _.elem[i][3] = MUL(elem, another.elem, i, 3);
59  }
60  return _;
61 }
62 
63 
64 void Matrix::operator=(const Matrix& source) {
65  memcpy(elem, source.elem, sizeof(elem));
66 }
67 
68 
69 Matrix::Matrix(float hDegrees, float saturationFactor, float valueFactor) {
70  /*
71  R = [1 0 0; 0 cos(H) -sin(H); 0 sin(H) cos(H)];
72  A = [1 1 1; 1 / sqrt(2) - 1 / sqrt(2) 0; 1 / sqrt(6) 1 / sqrt(6) - 2 / sqrt(6)]';
73  M = simplify(A *[V 0 0; 0 V*S 0; 0 0 V*S] * R * inv(A))
74  */
75  const float H = hDegrees * (float)pi / 180,
76  S = saturationFactor, V = valueFactor;
77  r() = { (V*(12 * S*cos(H) + 6)) / 18, -(V*(6 * S*cos(H) + 6 * sqrtf(3)*S*sin(H) - 6)) / 18, (V*(6 * sqrtf(3)*S*sin(H) - 6 * S*cos(H) + 6)) / 18, 0 };
78  g() = { (V*(6 * sqrtf(3)*S*sin(H) - 6 * S*cos(H) + 6)) / 18, (V*(12 * S*cos(H) + 6)) / 18, -(V*(S*cos(H) + sqrtf(3)*S*sin(H) - 1)) / 3, 0 };
79  b() = {-(V*(6 * S*cos(H) + 6 * sqrtf(3)*S*sin(H) - 6)) / 18, (V*(6 * sqrtf(3)*S*sin(H) - 6 * S*cos(H) + 6)) / 18, (V*(4 * S*cos(H) + 2)) / 6, 0 };
80  a() = color4f{ 0, 0, 0, 1 };
81 }
82 
83 
84 Matrix::Matrix(const color3f& preservedColor, float saturationFactor, float valueFactor) {
85  /*
86  R = [1 0 0; 0 cos(H) -sin(H); 0 sin(H) cos(H)];
87  A = [1 1 1; 1 / sqrt(2) - 1 / sqrt(2) 0; 1 / sqrt(6) 1 / sqrt(6) - 2 / sqrt(6)]';
88  M = simplify(A * inv(R) * [V 0 0; 0 V*S 0; 0 0 -V*S] * R * inv(A))
89  */
90  const Color::hsva_t hsva(preservedColor);
91  const float _2H = 2 * (-hsva.h * 2 * pi - pi / 6),
92  S = saturationFactor, V = valueFactor;
93  r() = { (V*(S*cos(_2H) - sqrtf(3)*S*sin(_2H) + 1)) / 3, -(V*(2 * S*cos(_2H) - 1)) / 3, (V*(S*cos(_2H) + sqrtf(3)*S*sin(_2H) + 1)) / 3, 0 };
94  g() = {-(V*(2 * S*cos(_2H) - 1)) / 3, (V*(S*cos(_2H) + sqrtf(3)*S*sin(_2H) + 1)) / 3, (V*(S*cos(_2H) - sqrtf(3)*S*sin(_2H) + 1)) / 3, 0 };
95  b() = { (V*(S*cos(_2H) + sqrtf(3)*S*sin(_2H) + 1)) / 3, (V*(S*cos(_2H) - sqrtf(3)*S*sin(_2H) + 1)) / 3, -(V*(4 * S*cos(_2H) - 2)) / 6, 0 };
96  a() = color4f{ 0, 0, 0, 1 };
97 }
RGBA color mapping.
Definition: matrix.h:29
float elem[4][4]
Definition: matrix.h:36
Matrix()
Initializes the color matrix to identity.
Definition: matrix.cpp:28
color4f r() const
Definition: matrix.h:60
color4f g() const
Definition: matrix.h:61
void operator=(const Matrix &)
Definition: matrix.cpp:64
color4f rows[4]
Definition: matrix.h:38
Matrix operator*(const Matrix &) const
Computes the right-multiplication of the current Matrix and another Matrix.
Definition: matrix.cpp:52
color4f b() const
Definition: matrix.h:62
color4f & operator[](int)
Retrieves matrix rows by index in 0..3 range.
Definition: matrix.cpp:42
color4f a() const
Definition: matrix.h:63
#define MUL(a, b, i, j)
Definition: matrix.cpp:26
const float pi
Definition: basic_types.h:29
HSVA quad (for hue, saturation, value and alpha)
Definition: color_spaces.h:34
jobject jlong jint jint jint jint g
jobject jlong jint jint jint jint jint b
jobject jlong jint jint jint r
jobject jlong jint jint jint jint jint jint a
Beatmup::color3f preservedColor