-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmainui.c
More file actions
1375 lines (1217 loc) · 36.3 KB
/
Copy pathmainui.c
File metadata and controls
1375 lines (1217 loc) · 36.3 KB
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
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2025, 2026 Jörg Bakker
*
* 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.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <limits.h>
#ifdef __APPLE__
#define GL_SILENCE_DEPRECATION
#endif
#ifndef SW_ONLY
#include <GL/glew.h>
#define NANOVG_GL3_IMPLEMENTATION
#endif
#ifdef __APPLE__
#include <SDL2/SDL.h>
#else
#include <SDL.h>
#endif
#define FONTSTASH_IMPLEMENTATION
#include "fontstash_xc.h"
#include "nanovg_xc.h"
#ifndef SW_ONLY
#include "nanovg_xc_vtex.h"
#endif
#define NANOVG_SW_IMPLEMENTATION
#include "nanovg_xc_sw.h"
#define BLENDISH_IMPLEMENTATION
#include "blendish_xc.h"
#define OUI_IMPLEMENTATION
#include "oui.h"
#ifndef __plan9__
#include "nfd.h"
#include "nfd_sdl2.h"
#endif
#include "cwalk.h"
#include "sis.h"
SDL_Window *sdl_window = NULL;
int sdl_window_flags = SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI | SDL_WINDOW_RESIZABLE;
SDL_Renderer *sdl_renderer = NULL;
SDL_Surface *sdl_surface = NULL;
SDL_Texture *sdl_texture = NULL;
SDL_Rect display_bounds;
int winPrevWidth = 0, winPrevHeight = 0;
int winWidth, winHeight;
int fbWidth, fbHeight;
float pxRatio;
unsigned char *sw_img_buf = NULL;
char *exe_path = NULL;
char *pref_path = NULL;
char asset_path[PATH_MAX] = {0};
bool running = true;
#ifndef SW_ONLY
SDL_GLContext sdlGLContext = NULL;
#endif
#define DEFAULT_WIN_WIDTH (800)
#define DEFAULT_WIN_HEIGHT (600)
const char *window_title = "SIS Stereogram Generator";
const char *image_read_extensions = "png,jpg,bmp,gif,hdr,tga,pic";
const char *image_write_extensions = "png";
static char dropped_file[PATH_MAX];
static int dropped_file_len = 0;
static bool use_gl = false;
static int num_threads = 0;
static int nvg_flags = NVG_NO_FONTSTASH;
const bool gui = true;
struct main_ctx {
int argc;
char **argv;
NVGcontext *vg;
UIcontext *ui_ctx;
double mx, my;
int depth_map_img, texture_img, sis_img;
} mctx;
typedef enum {
ST_PANEL = 1,
ST_BUTTON,
ST_TEXT,
ST_SLIDER,
ST_SLIDER_INT,
ST_CHECK,
ST_IMAGE,
ST_NUMFIELD,
} widget_sub_type;
typedef struct {
int subtype;
UIhandler handler;
} widget_head;
typedef struct {
widget_head head;
bool border;
} panel_head;
typedef struct {
widget_head head;
int iconid;
const char *label;
} button_head;
typedef struct {
widget_head head;
char *text;
int maxsize;
} text_head;
typedef struct {
widget_head head;
const char *label;
float *value;
float min, max;
bool perc;
} slider_head;
typedef struct {
widget_head head;
const char *label;
long int *value;
long int min, max;
bool perc;
} slider_int_head;
typedef struct {
widget_head head;
const char *label;
bool *option;
} check_head;
typedef struct {
widget_head head;
int img;
} image_head;
typedef struct {
widget_head head;
const char *label;
int *value;
int min, max;
} numfield_head;
void
show_statistics(void)
{
}
void
event_handler(int item, UIevent event)
{
widget_head *data = (widget_head *) uiGetHandle(item);
if (data && data->handler) {
data->handler(item, event);
}
}
void
button_handler(int item, UIevent event)
{
const button_head *data = (const button_head *)uiGetHandle(item);
printf("clicked item pointer: %p, label: %s\n", uiGetHandle(item), data->label);
}
int
panel(bool border)
{
int item = uiItem();
panel_head *data = (panel_head *) uiAllocHandle(item, sizeof(panel_head));
data->head.subtype = ST_PANEL;
data->head.handler = NULL;
data->border = border;
return item;
}
void
quit_button_handler(int item, UIevent event)
{
running = false;
}
static bool update_sis_view;
void
sis_button_handler(int item, UIevent event)
{
#ifndef __plan9__
nfdchar_t *outPath = NULL;
nfdu8filteritem_t filters[1] = {{"SIS files", image_write_extensions}};
nfdsavedialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
if (!NFD_GetNativeWindowFromSDLWindow(sdl_window, &args.parentWindow)) {
fprintf(stderr, "NFD_GetNativeWindowFromSDLWindow failed: %s\n", NFD_GetError());
}
nfdresult_t result = NFD_SaveDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
fprintf(stderr, "saving sis image to '%s'\n", outPath);
strncpy(SISFileName, outPath, PATH_MAX);
WriteSISFile();
free(outPath);
}
#endif
}
void update_depth_map_and_sis(void);
void update_texture(void);
void update_sis(void);
void
depth_button_handler(int item, UIevent event)
{
#ifndef __plan9__
nfdu8char_t *outPath = NULL;
nfdu8filteritem_t filters[1] = {{"Depth files", image_read_extensions}};
nfdopendialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
args.defaultPath = depth_map_path;
if (!NFD_GetNativeWindowFromSDLWindow(sdl_window, &args.parentWindow)) {
fprintf(stderr, "NFD_GetNativeWindowFromSDLWindow failed: %s\n", NFD_GetError());
}
nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
fprintf(stderr, "loading depth image '%s'\n", outPath);
strncpy(DFileName, outPath, PATH_MAX);
update_depth_map_and_sis();
free(outPath);
update_sis_view = true;
}
#endif
}
void
texture_button_handler(int item, UIevent event)
{
#ifndef __plan9__
nfdchar_t *outPath = NULL;
nfdu8filteritem_t filters[1] = {{"Texture files", image_read_extensions}};
nfdopendialogu8args_t args = {0};
args.filterList = filters;
args.filterCount = 1;
args.defaultPath = texture_path;
if (!NFD_GetNativeWindowFromSDLWindow(sdl_window, &args.parentWindow)) {
fprintf(stderr, "NFD_GetNativeWindowFromSDLWindow failed: %s\n", NFD_GetError());
}
nfdresult_t result = NFD_OpenDialogU8_With(&outPath, &args);
if (result == NFD_OKAY) {
fprintf(stderr, "loading texture image '%s'\n", outPath);
strncpy(TFileName, outPath, PATH_MAX);
update_texture();
update_sis();
free(outPath);
update_sis_view = true;
}
#endif
}
int
button(int iconid, const char *label, UIhandler handler)
{
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetEvents(item, UI_BUTTON0_HOT_UP);
button_head *data = (button_head *) uiAllocHandle(item, sizeof(button_head));
data->head.subtype = ST_BUTTON;
data->head.handler = handler;
data->iconid = iconid;
data->label = label;
return item;
}
bool update_sis_image(void);
#if 0
bool update_texture_image(void);
#endif
bool load_texture_image(void);
bool load_depth_image(void);
bool load_sis_image(void);
void
update_sis(void)
{
InitAlgorithm();
render_sis();
update_sis_image();
}
void
update_texture(void)
{
CloseTFile(Theight);
SIStype = SIS_TEXT_MAP;
OpenTFile(TFileName, &Twidth, &Theight);
#if 0
update_texture_image();
#endif
load_texture_image();
}
void
update_depth_map_and_sis(void)
{
CloseDFile();
CloseSISFile();
CloseTFile(Theight);
FreeBuffers();
/// The next three functions are usually called from FreeBuffers()
// free(DBuffer);
// free(IdentBuffer);
// free(SISBuffer);
free(SISred);
free(SISgreen);
free(SISblue);
/// FIXME deleting images causes segfault with the software backend but not with GL backend
// nvgDeleteImage(mctx.vg, mctx.depth_map_img);
// nvgDeleteImage(mctx.vg, mctx.sis_img);
// init_base(0, NULL);
init_all(0, NULL);
#if 0
/// The next four functions are usually called in init_sis()
OpenDFile(DFileName, &Dwidth, &Dheight);
if (SIStype == SIS_TEXT_MAP) {
OpenTFile(TFileName, &Twidth, &Theight);
}
InitAlgorithm();
AllocBuffers();
// /// The next functions are usually called in AllocBuffers()
// if ((DBuffer = (col_t *)calloc(Dwidth * oversam, sizeof(col_t))) == NULL) {
// fprintf(stderr, "Couldn't alloc memory for depth buffer.\n");
// exit(1);
// }
// if ((IdentBuffer = (ind_t *)calloc(SISwidth * oversam, sizeof(ind_t))) == NULL) {
// // if ((IdentBuffer = (col_t *) calloc(SISwidth * oversam, sizeof(col_t))) == NULL) {
// fprintf(stderr, "Couldn't alloc memory for ident buffer\n");
// free(DBuffer);
// exit(1);
// }
// if ((SISBuffer = (col_t *)calloc(SISwidth * oversam, sizeof(col_t))) == NULL) {
// fprintf(stderr, "Couldn't alloc memory for SIS buffer.\n");
// free(DBuffer);
// free(IdentBuffer);
// exit(1);
// }
#endif
CreateSISBuffer(SISwidth, SISheight, SIStype);
render_sis();
// WriteSISFile();
load_sis_image();
load_depth_image();
}
void
slider_handler(int item, UIevent event)
{
const slider_head *data = (const slider_head *)uiGetHandle(item);
// printf("clicked slider pointer: %p, label: %s\n", uiGetHandle(item), data->label);
UIrect r = uiGetRect(item);
UIvec2 c = uiGetCursor();
float v = (float)(c.x - r.x) / (float)r.w;
if (v < 0.0f) v = 0.0f;
if (v > 1.0f) v = 1.0f;
// *(data->progress) = v;
*(data->value) = v * (data->max - data->min) + data->min;
update_sis_view = true;
}
int
slider(const char *label, float *value, UIhandler handler, float min, float max, bool perc)
{
assert(min < max);
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetLayout(item, UI_HFILL);
uiSetEvents(item, UI_BUTTON0_CAPTURE);
slider_head *data = (slider_head *) uiAllocHandle(item, sizeof(slider_head));
data->head.subtype = ST_SLIDER;
data->head.handler = handler;
data->label = label;
data->value = value;
// data->progress = *progress * (max - min) + min;
data->min = min;
data->max = max;
data->perc = perc;
return item;
}
void
slider_int_handler(int item, UIevent event)
{
const slider_int_head *data = (const slider_int_head *)uiGetHandle(item);
// printf("clicked slider pointer: %p, label: %s\n", uiGetHandle(item), data->label);
UIrect r = uiGetRect(item);
UIvec2 c = uiGetCursor();
float v = (float)(c.x - r.x) / (float)r.w;
if (v < 0.0f) v = 0.0f;
if (v > 1.0f) v = 1.0f;
// *(data->progress) = v;
*(data->value) = v * (data->max - data->min) + data->min;
update_sis_view = true;
}
int
slider_int(const char *label, long int *value, UIhandler handler, long int min, long int max, bool perc)
{
assert(min < max);
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetLayout(item, UI_HFILL);
uiSetEvents(item, UI_BUTTON0_CAPTURE);
slider_int_head *data = (slider_int_head *) uiAllocHandle(item, sizeof(slider_int_head));
data->head.subtype = ST_SLIDER_INT;
data->head.handler = handler;
data->label = label;
data->value = value;
// data->progress = *progress * (max - min) + min;
data->min = min;
data->max = max;
data->perc = perc;
return item;
}
void
checkhandler(int item, UIevent event)
{
const check_head *data = (const check_head *)uiGetHandle(item);
*data->option = !(*data->option);
update_sis_view = true;
}
int
check(const char *label, bool *option)
{
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetLayout(item, UI_HFILL);
uiSetEvents(item, UI_BUTTON0_DOWN);
check_head *data = (check_head *)uiAllocHandle(item, sizeof(check_head));
data->head.subtype = ST_CHECK;
data->head.handler = checkhandler;
data->label = label;
data->option = option;
return item;
}
int
image(NVGcontext *vg, int img, int w, int h)
{
int item = uiItem();
int iw, ih;
nvgImageSize(vg, img, &iw, &ih);
float aspect_ratio = (float)w / (float)h;
float aspect_ratio_i = (float)iw / (float)ih;
if (iw < w && ih < h) {
uiSetSize(item, iw, ih);
}
else if (aspect_ratio > aspect_ratio_i) {
iw *= (float)h / (float)ih;
uiSetSize(item, iw, h);
} else {
ih *= (float)w / (float)iw;
uiSetSize(item, w, ih);
}
#if 0
uiSetEvents(item, UI_BUTTON0_HOT_UP);
data->head.handler = image_handler;
#endif
image_head *data = (image_head *) uiAllocHandle(item, sizeof(image_head));
data->head.subtype = ST_IMAGE;
data->img = img;
return item;
}
int
image_vert(NVGcontext *vg, int img, int w, int maxh)
{
int item = uiItem();
int iw, ih;
nvgImageSize(vg, img, &iw, &ih);
ih *= (float)w / (float)iw;
if (maxh < 0) ih = 5;
if (maxh > 0) ih = ih > maxh ? maxh : ih;
uiSetSize(item, w, ih);
#if 0
uiSetEvents(item, UI_BUTTON0_DOWN);
data->head.handler = image_vert_handler;
#endif
image_head *data = (image_head *) uiAllocHandle(item, sizeof(image_head));
data->head.subtype = ST_IMAGE;
data->img = img;
return item;
}
void
numfield_handler(int item, UIevent event)
{
const numfield_head *data = (const numfield_head *)uiGetHandle(item);
UIrect r = uiGetRect(item);
UIvec2 c = uiGetCursor();
int up = (float)(c.x - r.x) > 0.5f * (float)r.w;
if (up && *data->value < data->max) {
(*data->value)++;
update_sis_view = true;
} else if (!up && *data->value > data->min) {
(*data->value)--;
update_sis_view = true;
}
}
int
number_field(const char *label, int *value, int min, int max)
{
int item = uiItem();
uiSetSize(item, 0, BND_WIDGET_HEIGHT);
uiSetLayout(item, UI_HFILL);
uiSetEvents(item, UI_BUTTON0_DOWN);
numfield_head *data = (numfield_head *) uiAllocHandle(item, sizeof(numfield_head));
data->head.subtype = ST_NUMFIELD;
data->head.handler = numfield_handler;
data->label = label;
data->value = value;
data->min = min;
data->max = max;
return item;
}
void render_ui(NVGcontext *vg, int item, int corners);
void
render_ui_items(NVGcontext *vg, int item, int corners)
{
int kid = uiFirstChild(item);
while (kid > 0) {
render_ui(vg, kid, corners);
kid = uiNextSibling(kid);
}
}
void
render_ui(NVGcontext *vg, int item, int corners)
{
const widget_head *head = (const widget_head *)uiGetHandle(item);
UIrect rect = uiGetRect(item);
if (head) {
switch (head->subtype) {
default:{
render_ui_items(vg, item, corners);
} break;
case ST_PANEL: {
const panel_head *data = (panel_head *) head;
if (data->border) {
bndBackground(vg, rect.x, rect.y, rect.w, rect.h);
bndBevel(vg, rect.x, rect.y, rect.w, rect.h);
float cr = 5.0f;
// bndRoundedBox(vg, rect.x, rect.y, rect.w, rect.h, cr, cr, cr, cr);
// // nvgFillColor(vg, bnd_theme.backgroundColor);
// // nvgFill(vg);
// nvgStrokeColor(vg, nvgRGB(1.0, 1.0, 1.0));
// nvgStroke(vg);
}
render_ui_items(vg, item, corners);
} break;
case ST_BUTTON: {
const button_head *data = (button_head *) head;
bndToolButton(vg, rect.x, rect.y, rect.w, rect.h,
corners, (BNDwidgetState) uiGetState(item),
data->iconid, data->label);
} break;
case ST_TEXT: {
const text_head *data = (text_head*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
int idx = strlen(data->text);
bndTextField(vg, rect.x, rect.y, rect.w, rect.h,
corners, state, -1, data->text, idx, idx);
} break;
case ST_SLIDER: {
const slider_head *data = (slider_head *) head;
BNDwidgetState state = (BNDwidgetState) uiGetState(item);
static char value_str[32];
float progress = (*data->value - data->min) / (data->max - data->min);
float value = *data->value;
if (data->perc) {
value *= 100.0f;
sprintf(value_str, "%.0f%%", value);
} else {
sprintf(value_str, "%.0f", value);
}
bndSlider(vg, rect.x, rect.y, rect.w, rect.h,
corners,
state,
progress,
data->label, value_str);
} break;
case ST_SLIDER_INT: {
const slider_int_head *data = (slider_int_head *) head;
BNDwidgetState state = (BNDwidgetState) uiGetState(item);
static char value_str[32];
float progress = (float)(*data->value - data->min) / (float)(data->max - data->min);
long int value = *data->value;
if (data->perc) {
float value_float = value * 1e-2;
sprintf(value_str, "%.0f%%", value_float);
} else {
sprintf(value_str, "%ld", value);
}
bndSlider(vg, rect.x, rect.y, rect.w, rect.h,
corners,
state,
progress,
data->label, value_str);
} break;
case ST_CHECK: {
const check_head *data = (check_head*)head;
BNDwidgetState state = (BNDwidgetState)uiGetState(item);
if (*data->option)
state = BND_ACTIVE;
bndOptionButton(vg,rect.x,rect.y,rect.w,rect.h, state,
data->label);
} break;
case ST_IMAGE: {
const image_head *data = (image_head *) head;
NVGpaint img_paint = nvgImagePattern(vg,
rect.x, rect.y, rect.w, rect.h, 0, data->img, 1.0f);
nvgBeginPath(vg);
nvgRect(vg, rect.x, rect.y, rect.w, rect.h);
nvgFillPaint(vg, img_paint);
nvgFill(vg);
} break;
case ST_NUMFIELD: {
const numfield_head *data = (numfield_head *) head;
static char value_str[32];
sprintf(value_str, "%d", algorithm);
bndNumberField(vg, rect.x, rect.y, rect.w, rect.h,
corners, (BNDwidgetState) uiGetState(item),
data->label, value_str);
} break;
}
}
}
void
ui_frame(NVGcontext *vg, float w, float h)
{
/// Layout user interface
uiBeginLayout();
int M = 7;
int panel_margin_h = M;
int panel_margin_v = 5;
int panel_width = 200;
// int panel_height = 400;
int panel_height = h - 4 * panel_margin_v;
int img_width = panel_width - 2 * panel_margin_h;
int sis_view_width = w - panel_width - 4 * panel_margin_h;
int sis_view_height = h - 4 * panel_margin_v;
int pch = 0;
int root = panel(false);
uiSetSize(root, w, h);
uiSetBox(root, UI_LAYOUT);
uiSetLayout(root, UI_FILL);
int ctl_panel = panel(true);
uiSetSize(ctl_panel, panel_width, panel_height);
uiSetLayout(ctl_panel, UI_TOP | UI_LEFT);
uiSetMargins(ctl_panel, 10, 10, 5, 10);
uiSetBox(ctl_panel, UI_COLUMN);
uiInsert(root, ctl_panel);
pch += 10;
int sis_view_panel = panel(false);
uiSetSize(sis_view_panel, sis_view_width, sis_view_height);
uiSetLayout(sis_view_panel, UI_RIGHT | UI_TOP);
uiSetMargins(sis_view_panel, 5, 10, 5, 10);
uiSetBox(sis_view_panel, UI_COLUMN);
uiInsert(root, sis_view_panel);
if (update_sis_view) {
update_sis();
update_sis_view = false;
}
int sis_view = image(mctx.vg, mctx.sis_img, sis_view_width - 10, sis_view_height - 10);
uiSetLayout(sis_view, UI_TOP);
uiSetMargins(sis_view, panel_margin_h, panel_margin_v, panel_margin_h,
panel_margin_v);
uiInsert(sis_view_panel, sis_view);
int quit_button = button(BND_ICON_QUIT, "quit", quit_button_handler);
uiSetLayout(quit_button, UI_HFILL | UI_TOP);
uiSetMargins(quit_button, M, 8, M, 3);
uiInsert(ctl_panel, quit_button);
pch += BND_WIDGET_HEIGHT + 5 + 5;
int eye_dist_slider = slider_int("eye distance",
&eye_dist, slider_int_handler, 10, 0.5 * Dwidth, false);
uiSetMargins(eye_dist_slider, M, 5, M, 3);
uiInsert(ctl_panel, eye_dist_slider);
pch += BND_WIDGET_HEIGHT + 5 + 3;
int near_plane_slider = slider("scene depth", &u, slider_handler, 0.01f, 1.0f, true);
uiSetMargins(near_plane_slider, M, 3, M, 3);
uiInsert(ctl_panel, near_plane_slider);
pch += BND_WIDGET_HEIGHT + 3 + 3;
int far_plane_slider = slider("back distance", &t, slider_handler, 0.25f, 2.0f, true);
uiSetMargins(far_plane_slider, M, 3, M, 3);
uiInsert(ctl_panel, far_plane_slider);
pch += BND_WIDGET_HEIGHT + 3 + 3;
int origin_slider = slider_int("algo origin",
&origin, slider_int_handler, 0, Dwidth, false);
uiSetMargins(origin_slider, M, 3, M, 3);
uiInsert(ctl_panel, origin_slider);
pch += BND_WIDGET_HEIGHT + 3 + 3;
int algo_numfield;
algo_numfield = number_field("algorithm", &algorithm, 1, 4);
uiSetMargins(algo_numfield, M, 5, M, 5);
uiInsert(ctl_panel, algo_numfield);
pch += BND_WIDGET_HEIGHT + 3 + 3;
int opt_show_marker = check("show markers", &mark);
uiSetMargins(opt_show_marker, 2 * M, 5, 2 * M, 5);
uiInsert(ctl_panel, opt_show_marker);
pch += BND_WIDGET_HEIGHT + 2 * M + 5;
int opt_invert_depth_map = check("invert depth", &invert);
uiSetMargins(opt_invert_depth_map, 2 * M, 5, 2 * M, 5);
uiInsert(ctl_panel, opt_invert_depth_map);
pch += BND_WIDGET_HEIGHT + 2 * M + 5;
// int file_buttons_panel = panel(false);
// // uiSetLayout(file_buttons_panel, UI_HFILL);
// uiSetSize(file_buttons_panel, 0, BND_WIDGET_HEIGHT);
// uiSetMargins(file_buttons_panel, M, 5, M, 5);
// uiSetBox(file_buttons_panel, UI_ROW);
// uiInsert(ctl_panel, file_buttons_panel);
int sis_button = button(BND_ICON_GHOST, "save sis image ...", sis_button_handler);
uiSetLayout(sis_button, UI_HFILL | UI_TOP);
uiSetMargins(sis_button, M, 5, M, 5);
uiInsert(ctl_panel, sis_button);
pch += BND_WIDGET_HEIGHT + 5 + 5;
int depth_button = button(BND_ICON_GHOST, "load depth map ...", depth_button_handler);
uiSetLayout(depth_button, UI_HFILL | UI_TOP);
uiSetMargins(depth_button, M, 5, M, 5);
uiInsert(ctl_panel, depth_button);
pch += BND_WIDGET_HEIGHT + 5 + 5;
int depth_map_view = image_vert(mctx.vg, mctx.depth_map_img,
img_width, 0);
uiSetMargins(depth_map_view, panel_margin_h, 0, panel_margin_h,
panel_margin_v);
uiInsert(ctl_panel, depth_map_view);
/// The depth map height is limited by panel_width, as the aspect ration
/// of the depth image is kept
pch += panel_width;
int texture_button = button(BND_ICON_GHOST, "load texture image ...", texture_button_handler);
uiSetLayout(texture_button, UI_HFILL | UI_TOP);
uiSetMargins(texture_button, M, 5, M, 5);
uiInsert(ctl_panel, texture_button);
// pch += BND_WIDGET_HEIGHT + 5 + 5;
int texture_view = image_vert(mctx.vg, mctx.texture_img, img_width, panel_height - pch);
uiSetMargins(texture_view, panel_margin_h, 0, panel_margin_h,
panel_margin_v);
uiInsert(ctl_panel, texture_view);
int fill_panel = panel(false);
uiSetLayout(fill_panel, UI_VFILL);
uiInsert(ctl_panel, fill_panel);
uiEndLayout();
/// Need to handle dropping zones here, for now
UIvec2 c = uiGetCursor();
if (dropped_file_len && uiContains(depth_map_view, c.x, c.y) && (uiGetButton(0) == 0)) {
// printf("dropped file '%s' on depth map view\n", dropped_file);
strncpy(DFileName, dropped_file, PATH_MAX);
update_depth_map_and_sis();
dropped_file_len = 0;
}
if (dropped_file_len && uiContains(texture_view, c.x, c.y) && (uiGetButton(0) == 0)) {
// printf("dropped file '%s' on texture view\n", dropped_file);
strncpy(TFileName, dropped_file, PATH_MAX);
update_texture();
update_sis();
dropped_file_len = 0;
}
/// Render user interface
render_ui(vg, 0, BND_CORNER_NONE);
/// Process user events
uiProcess((int)(SDL_GetTicks()));
}
void init_frame_buffer(void);
void
frame(void)
{
// double t;
SDL_GetWindowSize(sdl_window, &winWidth, &winHeight);
SDL_GL_GetDrawableSize(sdl_window, &fbWidth, &fbHeight);
// SDL_GetRendererOutputSize(sdl_renderer, &fbWidth, &fbHeight);
/// Calculate pixel ration for hi-dpi devices.
pxRatio = (float)fbWidth / (float)winWidth;
if (use_gl) {
#ifndef SW_ONLY
glViewport(0, 0, winWidth, winHeight);
#endif
} else {
init_frame_buffer();
}
/// Update and render
if (use_gl) {
#ifndef SW_ONLY
glViewport(0.0f, 0.0f, fbWidth, fbHeight);
glClearColor(0.20f, 0.20f, 0.20f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_CULL_FACE);
glDisable(GL_DEPTH_TEST);
#endif
}
nvgBeginFrame(mctx.vg, winWidth, winHeight, pxRatio);
ui_frame(mctx.vg, winWidth, winHeight);
nvgEndFrame(mctx.vg);
if (use_gl) {
#ifndef SW_ONLY
glEnable(GL_DEPTH_TEST);
#endif
} else {
SDL_Rect win_rect = {0, 0, winWidth, winHeight};
SDL_UpdateTexture(
sdl_texture,
&win_rect,
sw_img_buf,
winWidth * 4
);
// copy and place a portion of the texture to the current rendering target
// set video size when copying sdl texture to sdl renderer. Texture will be stretched to blit_copy_rect!
SDL_RenderCopy(sdl_renderer, sdl_texture, NULL, &win_rect);
}
}
unsigned char *
rgb_to_rgba(unsigned char *img, int pixels)
{
unsigned char *ret = (unsigned char *)malloc(4 * pixels);
for (int i = 0; i < pixels; ++i) {
ret[4 * i + 0] = img[3 * i + 0];
ret[4 * i + 1] = img[3 * i + 1];
ret[4 * i + 2] = img[3 * i + 2];
ret[4 * i + 3] = 0xff;
}
return ret;
}
bool
load_depth_image(void)
{
mctx.depth_map_img = nvgCreateImage(mctx.vg, DFileName, 0);
return true;
}
#if 0
bool
update_depth_image(void)
{
int imageFlags = 0;
unsigned char *img = rgb_to_rgba(GetDFileBuffer(), Dwidth * Dheight);
nvgUpdateImage(mctx.vg, mctx.depth_map_img, img);
free(img);
return true;
}
#endif
bool
load_texture_image(void)
{
mctx.texture_img = nvgCreateImage(mctx.vg, TFileName, 0);
// mctx.texture_img = nvgCreateImageRGBA(mctx.vg, Twidth, Theight, imageFlags, GetTFileBuffer());
return true;
}
#if 0
bool
update_texture_image(void)
{
int imageFlags = 0;
unsigned char *img = rgb_to_rgba(GetTFileBuffer(), Twidth * Theight);
nvgUpdateImage(mctx.vg, mctx.texture_img, img);
free(img);
return true;
}
#endif
bool
load_sis_image(void)
{
update_sis_view = false;
int imageFlags = 0;
unsigned char *img = rgb_to_rgba(GetSISFileBuffer(), SISwidth * SISheight);
mctx.sis_img = nvgCreateImageRGBA(mctx.vg, SISwidth, SISheight, imageFlags, img);
free(img);
return true;
}
bool
update_sis_image(void)
{
int imageFlags = 0;
unsigned char *img = rgb_to_rgba(GetSISFileBuffer(), SISwidth * SISheight);
nvgUpdateImage(mctx.vg, mctx.sis_img, img);
free(img);
return true;
}
static float maxf(float a, float b) { return a < b ? b : a; }
static float minf(float a, float b) { return a < b ? a : b; }
typedef struct {
NVGcontext *vg;
float *fbuff;
int fbuffw, fbuffh;
float sdfScale, sdfOffset;
} SDFcontext;