-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGL_image_display-test-fltk.cc
361 lines (293 loc) · 11.8 KB
/
GL_image_display-test-fltk.cc
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// This is the C++ Fl_Gl_Image_Widget FLTK widget sample program. The Python
// test program is separate, and lives in GL_image_display-test-fltk.py
#include <FL/Fl.H>
#include <FL/Fl_Double_Window.H>
#include <FL/Fl_Output.H>
#include <math.h>
extern "C"
{
#include "util.h"
}
#include "Fl_Gl_Image_Widget.hh"
#define WINDOW_W 800
#define WINDOW_H 600
#define STATUS_H 20
#define DECIMATION 2
class Fl_Gl_Image_Widget_Derived;
static Fl_Double_Window* g_window;
static Fl_Gl_Image_Widget_Derived* g_gl_widgets[4];
static Fl_Output* g_status_text;
static const char*const* g_images;
static
void update_status(double image_pixel_x,
double image_pixel_y);
class Fl_Gl_Image_Widget_Derived : public Fl_Gl_Image_Widget
{
public:
Fl_Gl_Image_Widget_Derived(int x, int y, int w, int h, bool double_buffered) :
Fl_Gl_Image_Widget(x,y,w,h, double_buffered)
{}
/* These override the pan/zoom commands to pan/zoom all the widgets together
if SHIFT is depressed */
bool process_mousewheel_zoom(double dy,
double x,
double y,
double viewport_width,
double viewport_height)
{
if(!(Fl::event_state() & FL_SHIFT))
{
// We do the normal thing because shift is not pressed: the user
// didn't ask us to do anything special with this pan/zoom call
return
Fl_Gl_Image_Widget::
process_mousewheel_zoom(dy, x,y,viewport_width,viewport_height);
}
// All the widgets should pan/zoom together
int Nwidgets = (int)(sizeof(g_gl_widgets)/sizeof(g_gl_widgets[0]));
bool result = true;
for(int i=0; i<Nwidgets; i++)
{
g_gl_widgets[i]->make_current();
// I need to fake a mousewheel-zoom event in another widget, and
// I need to determine a fake center point. I select the same
// point, relatively, in the window
double viewport_width_new = (double)g_gl_widgets[i]->pixel_w();
double viewport_height_new = (double)g_gl_widgets[i]->pixel_h();
result = result &&
g_gl_widgets[i]->
Fl_Gl_Image_Widget::
process_mousewheel_zoom(dy,
(x + 0.5)/viewport_width * viewport_width_new - 0.5,
(y + 0.5)/viewport_height * viewport_height_new - 0.5,
viewport_width_new,
viewport_height_new);
}
make_current();
return result;
}
bool process_mousewheel_pan(double dx,
double dy,
double viewport_width,
double viewport_height)
{
if(!(Fl::event_state() & FL_SHIFT))
{
// We do the normal thing because shift is not pressed: the user
// didn't ask us to do anything special with this pan/zoom call
return
Fl_Gl_Image_Widget::
process_mousewheel_pan(dx,dy,viewport_width,viewport_height);
}
// All the widgets should pan/zoom together
int Nwidgets = (int)(sizeof(g_gl_widgets)/sizeof(g_gl_widgets[0]));
bool result = true;
for(int i=0; i<Nwidgets; i++)
{
g_gl_widgets[i]->make_current();
double viewport_width_new = (double)g_gl_widgets[i]->pixel_w();
double viewport_height_new = (double)g_gl_widgets[i]->pixel_h();
result = result &&
g_gl_widgets[i]->
Fl_Gl_Image_Widget::
process_mousewheel_pan(dx, dy,
viewport_width_new,
viewport_height_new);
}
make_current();
return result;
}
bool process_mousedrag_pan(double dx,
double dy,
double viewport_width,
double viewport_height)
{
if(!(Fl::event_state() & FL_SHIFT))
{
// We do the normal thing because shift is not pressed: the user
// didn't ask us to do anything special with this pan/zoom call
return
Fl_Gl_Image_Widget::
process_mousedrag_pan(dx,dy,viewport_width,viewport_height);
}
// All the widgets should pan/zoom together
int Nwidgets = (int)(sizeof(g_gl_widgets)/sizeof(g_gl_widgets[0]));
bool result = true;
for(int i=0; i<Nwidgets; i++)
{
g_gl_widgets[i]->make_current();
double viewport_width_new = (double)g_gl_widgets[i]->pixel_w();
double viewport_height_new = (double)g_gl_widgets[i]->pixel_h();
result = result &&
g_gl_widgets[i]->
Fl_Gl_Image_Widget::
process_mousedrag_pan(dx, dy,
viewport_width_new,
viewport_height_new);
}
make_current();
return result;
}
bool process_keyboard_panzoom(void)
{
if(!(Fl::event_state() & FL_SHIFT))
{
// We do the normal thing because shift is not pressed: the user
// didn't ask us to do anything special with this pan/zoom call
return
Fl_Gl_Image_Widget::
process_keyboard_panzoom();
}
// All the widgets should pan/zoom together
int Nwidgets = (int)(sizeof(g_gl_widgets)/sizeof(g_gl_widgets[0]));
bool result = true;
for(int i=0; i<Nwidgets; i++)
{
g_gl_widgets[i]->make_current();
result = result &&
g_gl_widgets[i]->
Fl_Gl_Image_Widget::
process_keyboard_panzoom();
}
make_current();
return result;
}
int handle(int event)
{
switch(event)
{
case FL_ENTER:
// I want FL_MOVE events and I want to make sure the parent widget
// does its procesing. This is required for the focus-follows-mouse
// logic for the keyboard-based navigation to work
Fl_Gl_Image_Widget::handle(event);
return 1;
case FL_MOVE:
{
double image_pixel_x, image_pixel_y;
GL_image_display_map_pixel_image_from_viewport(&m_ctx,
&image_pixel_x,
&image_pixel_y,
(double)Fl::event_x(),
(double)Fl::event_y());
update_status(image_pixel_x, image_pixel_y);
// Let the other handlers run
break;
}
case FL_PUSH:
{
double image_pixel_x, image_pixel_y;
GL_image_display_map_pixel_image_from_viewport(&m_ctx,
&image_pixel_x,
&image_pixel_y,
(double)Fl::event_x(),
(double)Fl::event_y());
const float pool[] = {(float)image_pixel_x - 50, (float)image_pixel_y,
(float)image_pixel_x + 50, (float)image_pixel_y,
(float)image_pixel_x, (float)image_pixel_y - 50,
(float)image_pixel_x, (float)image_pixel_y + 50};
const GL_image_display_line_segments_t cross =
{ .segments = {.Nsegments = 2,
.color_rgb = {1.f,0.f,0.f}},
.points = pool };
if( !set_lines(&cross, 1))
{
fprintf(stderr, "GL_image_display_set_lines() failed\n");
}
redraw();
break;
}
default: ;
}
return Fl_Gl_Image_Widget::handle(event);
}
};
static
void timer_callback(void* cookie __attribute__((unused)))
{
static int c = 0;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
{
if(!g_gl_widgets[2*i+j]->update_image2(DECIMATION,
false, false,
g_images[(2*i+j + c)%4]))
{
MSG("Couldn't update the image. Giving up.");
g_window->hide();
return;
}
}
Fl::repeat_timeout(1.0, timer_callback);
c++;
}
static
void update_status(double image_pixel_x,
double image_pixel_y)
{
char str[1024];
int str_written = 0;
#define append(fmt, ...) \
{ \
int Navailable = sizeof(str) - str_written; \
int Nwritten = snprintf(&str[str_written], Navailable, \
fmt, ##__VA_ARGS__); \
if(Navailable <= Nwritten) \
{ \
MSG("Static buffer overflow. Increase buffer size. update_status() setting empty string"); \
g_status_text->static_value(""); \
return; \
} \
str_written += Nwritten; \
}
if(image_pixel_x > 0)
{
append("Pixel (%.2f,%.2f) ",
image_pixel_x, image_pixel_y);
}
g_status_text->value(str);
#undef append
}
int main(int argc, char** argv)
{
if(argc != 5)
{
MSG("ERROR: Need 4 images on the commandline");
return 1;
}
g_images = (const char*const*)&argv[1];
Fl::lock();
g_window = new Fl_Double_Window( WINDOW_W, WINDOW_H, "OpenGL image visualizer" );
Fl_Group* images = new Fl_Group(0,0,WINDOW_W,WINDOW_H-STATUS_H);
{
int w = WINDOW_W/2;
int h = (WINDOW_H - STATUS_H)/2;
int y = 0;
bool double_buffered = true;
g_gl_widgets[0] = new Fl_Gl_Image_Widget_Derived(0, y,
w, h,
double_buffered);
g_gl_widgets[1] = new Fl_Gl_Image_Widget_Derived(w, y,
WINDOW_W-w, h,
double_buffered);
y = h;
h = WINDOW_H - STATUS_H - y;
g_gl_widgets[2] = new Fl_Gl_Image_Widget_Derived(0, y,
w, h,
double_buffered);
g_gl_widgets[3] = new Fl_Gl_Image_Widget_Derived(w, y,
WINDOW_W-w, h,
double_buffered);
}
images->end();
{
g_status_text = new Fl_Output(0, WINDOW_H-STATUS_H,
WINDOW_W, STATUS_H);
}
g_window->resizable(images);
g_window->end();
g_window->show();
Fl::add_timeout(1.0, timer_callback);
Fl::run();
return 0;
}