-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiimageviewer.h
155 lines (131 loc) · 5.2 KB
/
siimageviewer.h
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
/*
Licensed under the MIT License <http://opensource.org/licenses/MIT>.
SPDX-License-Identifier: MIT
Copyright (c) 2023 Stefan Isak <http://sisak.at>.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#ifndef SIIMAGEVIEWER_H
#define SIIMAGEVIEWER_H
#include <QOpenGLFunctions>
#include <QOpenGLFunctions_3_3_Core>
#include <QOpenGLWidget>
#include <QMatrix4x4>
#include <QVector2D>
#include <QVector4D>
class SiImageViewer : public QOpenGLWidget, protected QOpenGLFunctions_3_3_Core
{
Q_OBJECT
public:
explicit SiImageViewer(QWidget *parent = nullptr);
~SiImageViewer();
/**
* @brief Sets the main image. The data is directly copied onto graphics memory.
* The provided QImage can be destroyed after the call.
* @param image Image to display.
*/
void setImage(const QImage& image);
/**
* @brief Sets the background color of the viewer.
* @param color Background color
*/
void setBackground(const QColor& color);
/**
* @brief Resets all user actions (rotations, translations, zoom).
*/
void reset();
/**
* @brief Rotates the image counter-clockwise around the image center.
* @param angleDeg Angle in degrees.
*/
void rotate(float angleDeg);
/**
* @brief Rotates the image counter-clockwise around the given point.
* @param angleDeg Angle in degrees.
* @param point Rotation center
*/
void rotateAround(float angleDeg, const QPoint& point);
/**
* @brief Translates the image x, y amount of pixels.
* @param x Amount in x direction.
* @param y Amount in y direction.
*/
void translate(float x, float y);
protected:
void initializeGL() override;
void paintGL() override;
void resizeGL(int width, int height) override;
void mousePressEvent(QMouseEvent *event) override;
void mouseReleaseEvent(QMouseEvent *event) override;
void mouseMoveEvent(QMouseEvent *event) override;
void wheelEvent(QWheelEvent *event) override;
void keyPressEvent(QKeyEvent *event) override;
void keyReleaseEvent(QKeyEvent *event) override;
void focusInEvent(QFocusEvent *event) override;
private:
GLuint m_vertexShader;
GLuint m_fragmentShader;
GLuint m_shaderProgram;
GLuint m_vao;
GLuint m_vbo;
GLuint m_ibo;
GLuint m_texture;
// Unifrom locations
GLuint m_textureLocation;
GLuint m_mvpLocation;
int32_t m_imageWidth{1};
int32_t m_imageHeight{1};
QColor m_backgroundColor;
QMatrix4x4 m_pre; // used to transform the vertex coordinates to match the image dimension
QMatrix4x4 m_model; // used for global transformations (user rotation, scaling, ...)
QMatrix4x4 m_view; // used for viewport transformation
QMatrix4x4 m_projection; // not in use
QMatrix4x4 m_mvp; // multiplication of projection, view, model, pre
QVector2D m_cursorPosImage; // cursor position in image coordinates
QVector2D m_originalMousePos; // cursor position on first mouse down
QVector2D m_mouseDownPos; // cursor position from mouse down event
float m_scale{1.0}; // current scaling amount to apply
float m_zoomStep; // scaling factor for one scroll wheel change
bool m_panning; // true when middle mouse button is held down
bool m_shiftDown; // true when shift is held down
bool m_ctrlDown; // true when control is held down
bool m_rDown; // true when R is held down
void resetStates();
void setupShaders();
void setupBuffers();
void setupTexture();
void setupMatrices();
void updateMatrices();
void centerImage();
/**
* @brief Gets the current cursor position relative to the widget.
* @return Relative cursor position
*/
QVector2D currentCursorPos() const;
/**
* @brief Unprojects an screen point back to the image.
* @param screen A point in screen coordinates.
* @return Image coordinates in pixels.
*/
QVector2D screenToImage(const QVector2D& screen);
/**
* @brief Projects an image point onto the screen.
* @param image A point in image coordinates (pixels).
* @return Screen coordinates in pixels.
*/
QVector2D imageToScreen(const QVector2D& image);
};
#endif // SIIMAGEVIEWER_H