This repository has been archived by the owner on Apr 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OriginalView.cpp
176 lines (137 loc) · 4.83 KB
/
OriginalView.cpp
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
176
//
// originalview.cpp
//
// The code maintaining the original view of the input images
//
#include "originalview.h"
#include "VideoUtils.hpp"
#include "gl_helper.h"
#include "impressionist.h"
#include "impressionistDoc.h"
#include <Fl/Fl.H>
#include <thread>
#ifndef WIN32
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
using namespace GLHelper;
OriginalView::OriginalView(int x, int y, int w, int h, const char *l)
: Fl_Gl_Window(x, y, w, h, l), img{nullptr, 0, 0}, original_img{nullptr, 0,
0} {
m_nWindowWidth = w;
m_nWindowHeight = h;
f = NO_FLAG;
}
void OriginalView::draw() {
#ifndef MESA
// To avoid flicker on some machines.
glDrawBuffer(GL_FRONT_AND_BACK);
#endif // !MESA
if (!valid()) {
glClearColor(0.7f, 0.7f, 0.7f, 1.0);
// We're only using 2-D, so turn off depth
glDisable(GL_DEPTH_TEST);
ortho();
// glClear(GL_COLOR_BUFFER_BIT);
}
// glClear(GL_COLOR_BUFFER_BIT);
if (original_img.bytes.size() > 0) {
// note that both OpenGL pixel storage and the Windows BMP format
// store pixels left-to-right, BOTTOM-to-TOP!! thus all the fiddling
// around with startrow.
m_nWindowWidth = w();
m_nWindowHeight = h();
GLvoid *bitstart;
// we are not using a scrollable window, so ignore it
Point scrollpos; // = GetScrollPosition();
scrollpos.x = scrollpos.y = 0;
int drawWidth = min(m_nWindowWidth, original_img.width);
int drawHeight = min(m_nWindowHeight, original_img.height);
int startrow = max(0, original_img.height - (scrollpos.y + drawHeight));
// just copy image to GLwindow conceptually
glRasterPos2i(0, m_nWindowHeight - drawHeight);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, m_pDoc->m_nWidth);
glDrawBuffer(GL_BACK);
glDrawPixels(drawWidth, drawHeight, GL_RGBA, GL_UNSIGNED_BYTE,
original_img.raw_fmt());
if ((f & DrawingFlag::DISSOLVE) == DrawingFlag::DISSOLVE) {
f = f & ~DISSOLVE;
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glRasterPos2i(0, m_nWindowHeight - dissolve_target.height);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, dissolve_target.width);
glDrawPixels(dissolve_target.width, dissolve_target.height, GL_RGBA,
GL_UNSIGNED_BYTE, dissolve_target.paint_byte());
save_to_image(original_img);
}
if (show_cursor)
render_cursor();
}
glFlush();
#ifndef MESA
// To avoid flicker on some machines.
glDrawBuffer(GL_BACK);
#endif // !MESA
this->refresh();
}
void OriginalView::save_to_image(Image &image) {
glReadBuffer(GL_FRONT_AND_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glPixelStorei(GL_PACK_ALIGNMENT, 1);
glPixelStorei(GL_PACK_ROW_LENGTH, image.width);
glReadPixels(0, m_nWindowHeight - image.height, image.width, image.height,
GL_RGBA, GL_UNSIGNED_BYTE, image.raw_fmt());
}
void OriginalView::restore_from_image(Image &image) {
glDrawBuffer(GL_BACK);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glRasterPos2i(0, m_nWindowHeight - image.height);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
glPixelStorei(GL_UNPACK_ROW_LENGTH, image.width);
glDrawPixels(image.width, image.height, GL_RGBA, GL_UNSIGNED_BYTE,
image.paint_byte());
}
void OriginalView::refresh() { redraw(); }
void OriginalView::resizeWindow(int width, int height) {
resize(x(), y(), width, height);
}
void OriginalView::set_current_img(Image &img) {
this->img = img;
original_img = img;
cursor_pos = Point{img.width / 2, img.height / 2};
// m_pDoc->m_ucBitmap = this->original_img.raw_fmt();
this->resizeWindow(original_img.width, original_img.height);
this->refresh();
}
void OriginalView::dissolve(Image &img) {
img.crop(original_img.width, original_img.height);
img.set_alpha(0.5);
dissolve_target = img;
f |= static_cast<int>(DrawingFlag::DISSOLVE);
this->refresh();
}
void OriginalView::render_cursor() {
Point top_left = cursor_pos.shift_x(-3).shift_y(-3);
Point bottom_right = cursor_pos.shift_x(3).shift_y(3);
top_left = img.clip(top_left);
bottom_right = img.clip(bottom_right);
glPointSize(1);
GLubyte c[3] = {255, 0, 0};
glColor3ubv(c);
gl_draw_shape(GL_POINTS, [&] {
img.for_range_pixel(top_left, bottom_right,
[&](int y, int x) { gl_set_point(x, y); });
});
}
void OriginalView::set_cursor(const Point &p) {
cursor_pos = p;
show_cursor = true;
this->refresh();
}
void OriginalView::hide_cusor() {
show_cursor = false;
this->refresh();
}