File: color_matrix.cpp

package info (click to toggle)
bino 1.6.6-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 4,636 kB
  • sloc: cpp: 21,288; sh: 4,849; makefile: 314; sed: 16
file content (91 lines) | stat: -rw-r--r-- 3,221 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * This file is part of bino, a 3D video player.
 *
 * Copyright (C) 2012
 * Martin Lambers <marlam@marlam.de>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <cmath>

#include "color_matrix.h"

static void matmult(const float A[16], const float B[16], float result[16])
{
    for (int r = 0; r < 4; r++) {
        for (int c = 0; c < 4; c++) {
            result[r * 4 + c] =
                  A[r * 4 + 0] * B[0 * 4 + c]
                + A[r * 4 + 1] * B[1 * 4 + c]
                + A[r * 4 + 2] * B[2 * 4 + c]
                + A[r * 4 + 3] * B[3 * 4 + c];
        }
    }
}

void get_color_matrix(float brightness, float contrast, float hue, float saturation, float matrix[16])
{
    /*
     * See http://www.graficaobscura.com/matrix/index.html for the basic ideas.
     * Note that the hue matrix is computed in a simpler way.
     */

    const float wr = 0.3086f;
    const float wg = 0.6094f;
    const float wb = 0.0820f;

    // Brightness and Contrast
    float b = (brightness < 0.0f ? brightness : 4.0f * brightness) + 1.0f;
    float c = -contrast;
    float BC[16] = {
           b, 0.0f, 0.0f, 0.0f,
        0.0f,    b, 0.0f, 0.0f,
        0.0f, 0.0f,    b, 0.0f,
           c,    c,    c, 1.0f
    };
    // Saturation
    float s = saturation + 1.0f;
    float S[16] = {
        (1.0f - s) * wr + s, (1.0f - s) * wg    , (1.0f - s) * wb    , 0.0f,
        (1.0f - s) * wr    , (1.0f - s) * wg + s, (1.0f - s) * wb    , 0.0f,
        (1.0f - s) * wr    , (1.0f - s) * wg    , (1.0f - s) * wb + s, 0.0f,
                       0.0f,                0.0f,                0.0f, 1.0f
    };
    // Hue
    float n = 1.0f / sqrtf(3.0f);       // normalized hue rotation axis: sqrt(3) * (1 1 1)
    float h = hue * M_PI;               // hue rotation angle
    float hc = cosf(h);
    float hs = sinf(h);
    float H[16] = {     // conversion of angle/axis representation to matrix representation
        n * n * (1.0f - hc) + hc    , n * n * (1.0f - hc) - n * hs, n * n * (1.0f - hc) + n * hs, 0.0f,
        n * n * (1.0f - hc) + n * hs, n * n * (1.0f - hc) + hc    , n * n * (1.0f - hc) - n * hs, 0.0f,
        n * n * (1.0f - hc) - n * hs, n * n * (1.0f - hc) + n * hs, n * n * (1.0f - hc) + hc    , 0.0f,
                                0.0f,                         0.0f,                         0.0f, 1.0f
    };

    /* I
    float I[16] = {
        1.0f, 0.0f, 0.0f, 0.0f,
        0.0f, 1.0f, 0.0f, 0.0f,
        0.0f, 0.0f, 1.0f, 0.0f,
        0.0f, 0.0f, 0.0f, 1.0f
    };
    */

    // matrix = B * C * S * H
    float T[16];
    matmult(BC, S, T);
    matmult(T, H, matrix);
}