aboutsummaryrefslogtreecommitdiff
path: root/colormap.cpp
blob: 08a3d61e69e14d4143faf73748a846241ee3daf4 (plain)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include "colormap.h"

#include <algorithm>
#include <glad/gl.h>

class ColormapTexture
{
public:
    ColormapTexture(const std::vector<float> &cmap,
                    Colormap::Orientation orientation = Colormap::Horizontal);
    ~ColormapTexture();

    bool hasAlphaChannel() const { return false; }
    bool hasMipmaps() const { return false; }
    void bind();
    bool updateTexture();

    int textureId() const { return m_texture; }
    size_t width() const { return m_width; }
    size_t height() const { return m_height; }

    void setOrientation(Colormap::Orientation orientation);

private:
    Colormap::Orientation m_orientation;
    // QSize m_size;
    size_t m_width, m_height;
    GLuint m_texture;
    const std::vector<float> &m_cmap;
};

ColormapTexture::ColormapTexture(const std::vector<float> &cmap,
                                 Colormap::Orientation orientation)
    : m_cmap(cmap)
    , m_width(0)
    , m_height(0)
{
    // Setup OpenGL texture
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    setOrientation(orientation);
}

ColormapTexture::~ColormapTexture()
{
    glDeleteTextures(1, &m_texture);
}

void ColormapTexture::bind()
{
    glBindTexture(GL_TEXTURE_2D, m_texture);
}

void ColormapTexture::setOrientation(Colormap::Orientation orientation)
{
    if (m_orientation == orientation) {
        return;
    }

    m_orientation = orientation;
    updateTexture();
}

bool ColormapTexture::updateTexture()
{
    switch (m_orientation) {
    case Colormap::Horizontal:
        m_width = m_cmap.size() / 3;
        m_height = 1;
        break;
    case Colormap::Vertical:
        m_width = 1;
        m_height = m_cmap.size() / 3;
        break;
    }

    glBindTexture(GL_TEXTURE_2D, m_texture);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, m_width, m_height,
            0, GL_RGB, GL_FLOAT, m_cmap.data());
    return true;
}

Colormap::Colormap()
    : m_shouldUpdateTexture(false)
    , m_orientation(Colormap::Horizontal)
{
}

Colormap::~Colormap()
{
}

void Colormap::update()
{
}

static void reverseCMap(std::vector<float> &cmap)
{
    decltype(cmap.size()) i = 0, j = cmap.size() - 3;

    while (i < j) {
        std::swap(cmap[i++], cmap[j++]);
        std::swap(cmap[i++], cmap[j++]);
        std::swap(cmap[i++], cmap[j++]);

        j -= 6;
    }
}

void Colormap::setOrientation(Colormap::Orientation orientation)
{
    if (m_orientation == orientation) {
        return;
    }

    if (!m_cmap.empty()) {
        reverseCMap(m_cmap);
    }

    m_orientation = orientation;
    m_shouldUpdateOrientation = true;
    update();
}

void Colormap::setColorScale(std::shared_ptr<const ColorScale> scale)
{
    m_cmap.resize(SAMPLES * 3);
    scale->sample(SAMPLES, m_cmap.data());
    if (m_orientation == Colormap::Vertical) {
        reverseCMap(m_cmap);
    }

    colorScaleChanged(scale);

    m_shouldUpdateTexture = true;
    update();
}

/*
QSGNode *Colormap::newSceneGraph()
{
    QSGSimpleTextureNode *node = new QSGSimpleTextureNode;
    m_texture = new ColormapTexture(m_cmap);

    node->setTexture(m_texture);
    node->setOwnsTexture(false);
    node->setSourceRect(0, 0, m_texture->width(), m_texture->height());

    return node;
}

QSGNode *Colormap::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
{
    QSGNode *root = oldNode ? oldNode : newSceneGraph();

    QSGSimpleTextureNode *node = static_cast<QSGSimpleTextureNode *>(root);
    node->setRect(x(), y(), width(), height());

    ColormapTexture *texture = static_cast<ColormapTexture *>(m_texture);
    if (m_shouldUpdateOrientation) {
        texture->setOrientation(m_orientation);
        m_shouldUpdateOrientation = false;
    }

    if (m_shouldUpdateTexture) {
        texture->updateTexture();
        m_shouldUpdateTexture = false;
    }

    return root;
}
*/