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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
/*M///////////////////////////////////////////////////////////////////////////////////////
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// Intel License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2000, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of Intel Corporation may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#include "precomp.hpp"
#ifndef WIN32
#ifdef HAVE_GTK
#include "gtk/gtk.h"
#include "gdk/gdkkeysyms.h"
#include <gdk-pixbuf/gdk-pixbuf.h>
#include <stdio.h>
#ifdef HAVE_OPENGL
#include <gtk/gtkgl.h>
#include <GL/gl.h>
#include <GL/glu.h>
#endif
// TODO Fix the initial window size when flags=0. Right now the initial window is by default
// 320x240 size. A better default would be actual size of the image. Problem
// is determining desired window size with trackbars while still allowing resizing.
//
// Gnome Totem source may be of use here, see bacon_video_widget_set_scale_ratio
// in totem/src/backend/bacon-video-widget-xine.c
////////////////////////////////////////////////////////////
// CvImageWidget GTK Widget Public API
////////////////////////////////////////////////////////////
typedef struct _CvImageWidget CvImageWidget;
typedef struct _CvImageWidgetClass CvImageWidgetClass;
struct _CvImageWidget {
GtkWidget widget;
CvMat * original_image;
CvMat * scaled_image;
int flags;
};
struct _CvImageWidgetClass
{
GtkWidgetClass parent_class;
};
/** Allocate new image viewer widget */
GtkWidget* cvImageWidgetNew (int flags);
/** Set the image to display in the widget */
void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr);
// standard GTK object macros
#define CV_IMAGE_WIDGET(obj) G_TYPE_CHECK_INSTANCE_CAST (obj, cvImageWidget_get_type (), CvImageWidget)
#define CV_IMAGE_WIDGET_CLASS(klass) GTK_CHECK_CLASS_CAST (klass, cvImageWidget_get_type (), CvImageWidgetClass)
#define CV_IS_IMAGE_WIDGET(obj) G_TYPE_CHECK_INSTANCE_TYPE (obj, cvImageWidget_get_type ())
/////////////////////////////////////////////////////////////////////////////
// Private API ////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
GType cvImageWidget_get_type (void);
static GtkWidgetClass * parent_class = NULL;
// flag to help size initial window
#define CV_WINDOW_NO_IMAGE 2
void cvImageWidgetSetImage(CvImageWidget * widget, const CvArr *arr){
CvMat * mat, stub;
int origin=0;
//printf("cvImageWidgetSetImage\n");
if( CV_IS_IMAGE_HDR( arr ))
origin = ((IplImage*)arr)->origin;
mat = cvGetMat(arr, &stub);
if(widget->original_image && !CV_ARE_SIZES_EQ(mat, widget->original_image)){
cvReleaseMat( &widget->original_image );
}
if(!widget->original_image){
widget->original_image = cvCreateMat( mat->rows, mat->cols, CV_8UC3 );
gtk_widget_queue_resize( GTK_WIDGET( widget ) );
}
cvConvertImage( mat, widget->original_image,
(origin != 0 ? CV_CVTIMG_FLIP : 0) + CV_CVTIMG_SWAP_RB );
if(widget->scaled_image){
cvResize( widget->original_image, widget->scaled_image, CV_INTER_AREA );
}
// window does not refresh without this
gtk_widget_queue_draw( GTK_WIDGET(widget) );
}
GtkWidget*
cvImageWidgetNew (int flags)
{
CvImageWidget *image_widget;
image_widget = CV_IMAGE_WIDGET( gtk_widget_new (cvImageWidget_get_type (), NULL) );
image_widget->original_image = 0;
image_widget->scaled_image = 0;
image_widget->flags = flags | CV_WINDOW_NO_IMAGE;
return GTK_WIDGET (image_widget);
}
static void
cvImageWidget_realize (GtkWidget *widget)
{
GdkWindowAttr attributes;
gint attributes_mask;
//printf("cvImageWidget_realize\n");
g_return_if_fail (widget != NULL);
g_return_if_fail (CV_IS_IMAGE_WIDGET (widget));
gtk_widget_set_realized(widget, TRUE);
attributes.x = widget->allocation.x;
attributes.y = widget->allocation.y;
attributes.width = widget->allocation.width;
attributes.height = widget->allocation.height;
attributes.wclass = GDK_INPUT_OUTPUT;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.event_mask = gtk_widget_get_events (widget) |
GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK |
GDK_BUTTON_RELEASE_MASK | GDK_POINTER_MOTION_MASK;
attributes.visual = gtk_widget_get_visual (widget);
attributes.colormap = gtk_widget_get_colormap (widget);
attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;
widget->window = gdk_window_new (widget->parent->window, &attributes, attributes_mask);
widget->style = gtk_style_attach (widget->style, widget->window);
gdk_window_set_user_data (widget->window, widget);
gtk_style_set_background (widget->style, widget->window, GTK_STATE_ACTIVE);
}
static CvSize cvImageWidget_calc_size( int im_width, int im_height, int max_width, int max_height ){
float aspect = (float)im_width/(float)im_height;
float max_aspect = (float)max_width/(float)max_height;
if(aspect > max_aspect){
return cvSize( max_width, cvRound(max_width/aspect) );
}
return cvSize( cvRound(max_height*aspect), max_height );
}
static void
cvImageWidget_size_request (GtkWidget *widget,
GtkRequisition *requisition)
{
CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
//printf("cvImageWidget_size_request ");
// the case the first time cvShowImage called or when AUTOSIZE
if( image_widget->original_image &&
((image_widget->flags & CV_WINDOW_AUTOSIZE) ||
(image_widget->flags & CV_WINDOW_NO_IMAGE)))
{
//printf("original ");
requisition->width = image_widget->original_image->cols;
requisition->height = image_widget->original_image->rows;
}
// default case
else if(image_widget->scaled_image){
//printf("scaled ");
requisition->width = image_widget->scaled_image->cols;
requisition->height = image_widget->scaled_image->rows;
}
// the case before cvShowImage called
else{
//printf("default ");
requisition->width = 320;
requisition->height = 240;
}
//printf("%d %d\n",requisition->width, requisition->height);
}
static void cvImageWidget_set_size(GtkWidget * widget, int max_width, int max_height){
CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
//printf("cvImageWidget_set_size %d %d\n", max_width, max_height);
// don't allow to set the size
if(image_widget->flags & CV_WINDOW_AUTOSIZE) return;
if(!image_widget->original_image) return;
CvSize scaled_image_size = cvImageWidget_calc_size( image_widget->original_image->cols,
image_widget->original_image->rows, max_width, max_height );
if( image_widget->scaled_image &&
( image_widget->scaled_image->cols != scaled_image_size.width ||
image_widget->scaled_image->rows != scaled_image_size.height ))
{
cvReleaseMat( &image_widget->scaled_image );
}
if( !image_widget->scaled_image ){
image_widget->scaled_image = cvCreateMat( scaled_image_size.height, scaled_image_size.width, CV_8UC3 );
}
assert( image_widget->scaled_image );
}
static void
cvImageWidget_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
CvImageWidget *image_widget;
//printf("cvImageWidget_size_allocate\n");
g_return_if_fail (widget != NULL);
g_return_if_fail (CV_IS_IMAGE_WIDGET (widget));
g_return_if_fail (allocation != NULL);
widget->allocation = *allocation;
image_widget = CV_IMAGE_WIDGET (widget);
if( (image_widget->flags & CV_WINDOW_AUTOSIZE)==0 && image_widget->original_image ){
// (re) allocated scaled image
if( image_widget->flags & CV_WINDOW_NO_IMAGE ){
cvImageWidget_set_size( widget, image_widget->original_image->cols,
image_widget->original_image->rows);
}
else{
cvImageWidget_set_size( widget, allocation->width, allocation->height );
}
cvResize( image_widget->original_image, image_widget->scaled_image, CV_INTER_AREA );
}
if (gtk_widget_get_realized (widget))
{
image_widget = CV_IMAGE_WIDGET (widget);
if( image_widget->original_image &&
((image_widget->flags & CV_WINDOW_AUTOSIZE) ||
(image_widget->flags & CV_WINDOW_NO_IMAGE)) )
{
widget->allocation.width = image_widget->original_image->cols;
widget->allocation.height = image_widget->original_image->rows;
gdk_window_move_resize( widget->window, allocation->x, allocation->y,
image_widget->original_image->cols, image_widget->original_image->rows );
if(image_widget->flags & CV_WINDOW_NO_IMAGE){
image_widget->flags &= ~CV_WINDOW_NO_IMAGE;
gtk_widget_queue_resize( GTK_WIDGET(widget) );
}
}
else{
gdk_window_move_resize (widget->window,
allocation->x, allocation->y,
allocation->width, allocation->height );
}
}
}
static void
cvImageWidget_destroy (GtkObject *object)
{
CvImageWidget *image_widget;
g_return_if_fail (object != NULL);
g_return_if_fail (CV_IS_IMAGE_WIDGET (object));
image_widget = CV_IMAGE_WIDGET (object);
cvReleaseMat( &image_widget->scaled_image );
cvReleaseMat( &image_widget->original_image );
if (GTK_OBJECT_CLASS (parent_class)->destroy)
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void cvImageWidget_class_init (CvImageWidgetClass * klass)
{
GtkObjectClass *object_class;
GtkWidgetClass *widget_class;
object_class = (GtkObjectClass*) klass;
widget_class = (GtkWidgetClass*) klass;
parent_class = GTK_WIDGET_CLASS( g_type_class_peek (gtk_widget_get_type ()) );
object_class->destroy = cvImageWidget_destroy;
widget_class->realize = cvImageWidget_realize;
widget_class->size_request = cvImageWidget_size_request;
widget_class->size_allocate = cvImageWidget_size_allocate;
widget_class->button_press_event = NULL;
widget_class->button_release_event = NULL;
widget_class->motion_notify_event = NULL;
}
static void
cvImageWidget_init (CvImageWidget *image_widget)
{
image_widget->original_image=0;
image_widget->scaled_image=0;
image_widget->flags=0;
}
GType cvImageWidget_get_type (void){
static GType image_type = 0;
if (!image_type)
{
image_type = g_type_register_static_simple( GTK_TYPE_WIDGET,
(gchar*) "CvImageWidget",
sizeof(CvImageWidgetClass),
(GClassInitFunc) cvImageWidget_class_init,
sizeof(CvImageWidget),
(GInstanceInitFunc) cvImageWidget_init,
(GTypeFlags)NULL);
}
return image_type;
}
/////////////////////////////////////////////////////////////////////////////
// End CvImageWidget
/////////////////////////////////////////////////////////////////////////////
struct CvWindow;
typedef struct CvTrackbar
{
int signature;
GtkWidget* widget;
char* name;
CvTrackbar* next;
CvWindow* parent;
int* data;
int pos;
int maxval;
CvTrackbarCallback notify;
CvTrackbarCallback2 notify2;
void* userdata;
}
CvTrackbar;
typedef struct CvWindow
{
int signature;
GtkWidget* widget;
GtkWidget* frame;
GtkWidget* paned;
char* name;
CvWindow* prev;
CvWindow* next;
int last_key;
int flags;
int status;//0 normal, 1 fullscreen (YV)
CvMouseCallback on_mouse;
void* on_mouse_param;
struct
{
int pos;
int rows;
CvTrackbar* first;
}
toolbar;
#ifdef HAVE_OPENGL
bool useGl;
CvOpenGlDrawCallback glDrawCallback;
void* glDrawData;
#endif
}
CvWindow;
static gboolean icvOnClose( GtkWidget* widget, GdkEvent* event, gpointer user_data );
static gboolean icvOnKeyPress( GtkWidget* widget, GdkEventKey* event, gpointer user_data );
static void icvOnTrackbar( GtkWidget* widget, gpointer user_data );
static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_data );
#ifdef HAVE_GTHREAD
int thread_started=0;
static gpointer icvWindowThreadLoop();
GMutex* last_key_mutex;
GCond* cond_have_key;
GMutex* window_mutex;
GThread* window_thread;
GtkWidget* cvTopLevelWidget = 0;
#endif
static int last_key = -1;
static CvWindow* hg_windows = 0;
CV_IMPL int cvInitSystem( int argc, char** argv )
{
static int wasInitialized = 0;
// check initialization status
if( !wasInitialized )
{
hg_windows = 0;
gtk_disable_setlocale();
gtk_init( &argc, &argv );
#ifdef HAVE_OPENGL
gtk_gl_init(&argc, &argv);
#endif
wasInitialized = 1;
}
return 0;
}
CV_IMPL int cvStartWindowThread(){
#ifdef HAVE_GTHREAD
cvInitSystem(0,NULL);
if (!thread_started) {
if (!g_thread_supported ()) {
/* the GThread system wasn't inited, so init it */
g_thread_init(NULL);
}
// this mutex protects the window resources
window_mutex = g_mutex_new();
// protects the 'last key pressed' variable
last_key_mutex = g_mutex_new();
// conditional that indicates a key has been pressed
cond_have_key = g_cond_new();
// this is the window update thread
window_thread = g_thread_create((GThreadFunc) icvWindowThreadLoop,
NULL, TRUE, NULL);
}
thread_started = window_thread!=NULL;
return thread_started;
#else
return 0;
#endif
}
#ifdef HAVE_GTHREAD
gpointer icvWindowThreadLoop(){
while(1){
g_mutex_lock(window_mutex);
gtk_main_iteration_do(FALSE);
g_mutex_unlock(window_mutex);
// little sleep
g_usleep(500);
g_thread_yield();
}
return NULL;
}
#define CV_LOCK_MUTEX() \
if(thread_started && g_thread_self()!=window_thread){ g_mutex_lock( window_mutex ); } else { }
#define CV_UNLOCK_MUTEX() \
if(thread_started && g_thread_self()!=window_thread){ g_mutex_unlock( window_mutex); } else { }
#else
#define CV_LOCK_MUTEX()
#define CV_UNLOCK_MUTEX()
#endif
static CvWindow* icvFindWindowByName( const char* name )
{
CvWindow* window = hg_windows;
while( window != 0 && strcmp(name, window->name) != 0 )
window = window->next;
return window;
}
static CvWindow* icvWindowByWidget( GtkWidget* widget )
{
CvWindow* window = hg_windows;
while( window != 0 && window->widget != widget &&
window->frame != widget && window->paned != widget )
window = window->next;
return window;
}
double cvGetModeWindow_GTK(const char* name)//YV
{
double result = -1;
CV_FUNCNAME( "cvGetModeWindow_GTK" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
CV_LOCK_MUTEX();
result = window->status;
CV_UNLOCK_MUTEX();
__END__;
return result;
}
void cvSetModeWindow_GTK( const char* name, double prop_value)//Yannick Verdie
{
CV_FUNCNAME( "cvSetModeWindow_GTK" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
CV_ERROR( CV_StsNullPtr, "NULL window" );
if(window->flags & CV_WINDOW_AUTOSIZE)//if the flag CV_WINDOW_AUTOSIZE is set
EXIT;
//so easy to do fullscreen here, Linux rocks !
if (window->status==CV_WINDOW_FULLSCREEN && prop_value==CV_WINDOW_NORMAL)
{
CV_LOCK_MUTEX();
gtk_window_unfullscreen(GTK_WINDOW(window->frame));
window->status=CV_WINDOW_NORMAL;
CV_UNLOCK_MUTEX();
EXIT;
}
if (window->status==CV_WINDOW_NORMAL && prop_value==CV_WINDOW_FULLSCREEN)
{
CV_LOCK_MUTEX();
gtk_window_fullscreen(GTK_WINDOW(window->frame));
window->status=CV_WINDOW_FULLSCREEN;
CV_UNLOCK_MUTEX();
EXIT;
}
__END__;
}
double cvGetPropWindowAutoSize_GTK(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvGetPropWindowAutoSize_GTK" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = window->flags & CV_WINDOW_AUTOSIZE;
__END__;
return result;
}
double cvGetRatioWindow_GTK(const char* name)
{
double result = -1;
CV_FUNCNAME( "cvGetRatioWindow_GTK" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = static_cast<double>(window->widget->allocation.width) / window->widget->allocation.height;
__END__;
return result;
}
double cvGetOpenGlProp_GTK(const char* name)
{
double result = -1;
#ifdef HAVE_OPENGL
CV_FUNCNAME( "cvGetOpenGlProp_GTK" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT; // keep silence here
result = window->useGl;
__END__;
#else
(void)name;
#endif
return result;
}
// OpenGL support
#ifdef HAVE_OPENGL
namespace
{
void createGlContext(CvWindow* window)
{
GdkGLConfig* glconfig;
CV_FUNCNAME( "createGlContext" );
__BEGIN__;
// Try double-buffered visual
glconfig = gdk_gl_config_new_by_mode((GdkGLConfigMode)(GDK_GL_MODE_RGB | GDK_GL_MODE_DEPTH | GDK_GL_MODE_DOUBLE));
if (!glconfig)
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
// Set OpenGL-capability to the widget
if (!gtk_widget_set_gl_capability(window->widget, glconfig, NULL, TRUE, GDK_GL_RGBA_TYPE))
CV_ERROR( CV_OpenGlApiCallError, "Can't Create A GL Device Context" );
window->useGl = true;
__END__;
}
void drawGl(CvWindow* window)
{
CV_FUNCNAME( "drawGl" );
__BEGIN__;
GdkGLContext* glcontext = gtk_widget_get_gl_context(window->widget);
GdkGLDrawable* gldrawable = gtk_widget_get_gl_drawable(window->widget);
if (!gdk_gl_drawable_gl_begin (gldrawable, glcontext))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
glViewport(0, 0, window->widget->allocation.width, window->widget->allocation.height);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
if (window->glDrawCallback)
window->glDrawCallback(window->glDrawData);
if (gdk_gl_drawable_is_double_buffered (gldrawable))
gdk_gl_drawable_swap_buffers(gldrawable);
else
glFlush();
gdk_gl_drawable_gl_end(gldrawable);
__END__;
}
}
#endif // HAVE_OPENGL
static gboolean cvImageWidget_expose(GtkWidget* widget, GdkEventExpose* event, gpointer data)
{
#ifdef HAVE_OPENGL
CvWindow* window = (CvWindow*)data;
if (window->useGl)
{
drawGl(window);
return TRUE;
}
#else
(void)data;
#endif
CvImageWidget *image_widget = NULL;
cairo_t *cr = NULL;
GdkPixbuf *pixbuf = NULL;
g_return_val_if_fail (widget != NULL, FALSE);
g_return_val_if_fail (CV_IS_IMAGE_WIDGET (widget), FALSE);
g_return_val_if_fail (event != NULL, FALSE);
if (event->count > 0)
return FALSE;
cr = gdk_cairo_create(widget->window);
image_widget = CV_IMAGE_WIDGET (widget);
if( image_widget->scaled_image ){
// center image in available region
int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2;
int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2;
pixbuf = gdk_pixbuf_new_from_data(image_widget->scaled_image->data.ptr, GDK_COLORSPACE_RGB, false,
8, MIN(image_widget->scaled_image->cols, widget->allocation.width),
MIN(image_widget->scaled_image->rows, widget->allocation.height),
image_widget->scaled_image->step, NULL, NULL);
gdk_cairo_set_source_pixbuf(cr, pixbuf, x0, y0);
}
else if( image_widget->original_image ){
pixbuf = gdk_pixbuf_new_from_data(image_widget->original_image->data.ptr, GDK_COLORSPACE_RGB, false,
8, MIN(image_widget->original_image->cols, widget->allocation.width),
MIN(image_widget->original_image->rows, widget->allocation.height),
image_widget->original_image->step, NULL, NULL);
gdk_cairo_set_source_pixbuf(cr, pixbuf, 0, 0);
}
cairo_paint(cr);
cairo_destroy(cr);
return TRUE;
}
CV_IMPL int cvNamedWindow( const char* name, int flags )
{
int result = 0;
CV_FUNCNAME( "cvNamedWindow" );
__BEGIN__;
CvWindow* window;
int len;
cvInitSystem(1,(char**)&name);
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name string" );
// Check the name in the storage
if( icvFindWindowByName( name ) != 0 )
{
result = 1;
EXIT;
}
len = strlen(name);
CV_CALL( window = (CvWindow*)cvAlloc(sizeof(CvWindow) + len + 1));
memset( window, 0, sizeof(*window));
window->name = (char*)(window + 1);
memcpy( window->name, name, len + 1 );
window->flags = flags;
window->signature = CV_WINDOW_MAGIC_VAL;
window->last_key = 0;
window->on_mouse = 0;
window->on_mouse_param = 0;
memset( &window->toolbar, 0, sizeof(window->toolbar));
window->next = hg_windows;
window->prev = 0;
window->status = CV_WINDOW_NORMAL;//YV
CV_LOCK_MUTEX();
window->frame = gtk_window_new( GTK_WINDOW_TOPLEVEL );
window->paned = gtk_vbox_new( FALSE, 0 );
window->widget = cvImageWidgetNew( flags );
gtk_box_pack_end( GTK_BOX(window->paned), window->widget, TRUE, TRUE, 0 );
gtk_widget_show( window->widget );
gtk_container_add( GTK_CONTAINER(window->frame), window->paned );
gtk_widget_show( window->paned );
#ifndef HAVE_OPENGL
if (flags & CV_WINDOW_OPENGL)
CV_ERROR( CV_OpenGlNotSupported, "Library was built without OpenGL support" );
#else
if (flags & CV_WINDOW_OPENGL)
createGlContext(window);
window->glDrawCallback = 0;
window->glDrawData = 0;
#endif
//
// configure event handlers
// TODO -- move this to CvImageWidget ?
g_signal_connect( window->frame, "key-press-event",
G_CALLBACK(icvOnKeyPress), window );
g_signal_connect( window->widget, "button-press-event",
G_CALLBACK(icvOnMouse), window );
g_signal_connect( window->widget, "button-release-event",
G_CALLBACK(icvOnMouse), window );
g_signal_connect( window->widget, "motion-notify-event",
G_CALLBACK(icvOnMouse), window );
g_signal_connect( window->frame, "delete-event",
G_CALLBACK(icvOnClose), window );
g_signal_connect( window->widget, "expose-event",
G_CALLBACK(cvImageWidget_expose), window );
gtk_widget_add_events (window->widget, GDK_BUTTON_RELEASE_MASK | GDK_BUTTON_PRESS_MASK | GDK_POINTER_MOTION_MASK) ;
gtk_widget_show( window->frame );
gtk_window_set_title( GTK_WINDOW(window->frame), name );
if( hg_windows )
hg_windows->prev = window;
hg_windows = window;
gtk_window_set_resizable( GTK_WINDOW(window->frame), (flags & CV_WINDOW_AUTOSIZE) == 0 );
// allow window to be resized
if( (flags & CV_WINDOW_AUTOSIZE)==0 ){
GdkGeometry geometry;
geometry.min_width = 50;
geometry.min_height = 50;
gtk_window_set_geometry_hints( GTK_WINDOW( window->frame ), GTK_WIDGET( window->widget ),
&geometry, (GdkWindowHints) (GDK_HINT_MIN_SIZE));
}
CV_UNLOCK_MUTEX();
#ifdef HAVE_OPENGL
if (window->useGl)
cvSetOpenGlContext(name);
#endif
result = 1;
__END__;
return result;
}
#ifdef HAVE_OPENGL
CV_IMPL void cvSetOpenGlContext(const char* name)
{
CvWindow* window;
GdkGLContext* glcontext;
GdkGLDrawable* gldrawable;
CV_FUNCNAME( "cvSetOpenGlContext" );
__BEGIN__;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
CV_ERROR( CV_StsNullPtr, "NULL window" );
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window doesn't support OpenGL" );
glcontext = gtk_widget_get_gl_context(window->widget);
gldrawable = gtk_widget_get_gl_drawable(window->widget);
if (!gdk_gl_drawable_make_current(gldrawable, glcontext))
CV_ERROR( CV_OpenGlApiCallError, "Can't Activate The GL Rendering Context" );
__END__;
}
CV_IMPL void cvUpdateWindow(const char* name)
{
CV_FUNCNAME( "cvUpdateWindow" );
__BEGIN__;
CvWindow* window;
if (!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if (!window)
EXIT;
// window does not refresh without this
gtk_widget_queue_draw( GTK_WIDGET(window->widget) );
__END__;
}
CV_IMPL void cvSetOpenGlDrawCallback(const char* name, CvOpenGlDrawCallback callback, void* userdata)
{
CvWindow* window;
CV_FUNCNAME( "cvCreateOpenGLCallback" );
__BEGIN__;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
EXIT;
if (!window->useGl)
CV_ERROR( CV_OpenGlNotSupported, "Window was created without OpenGL context" );
window->glDrawCallback = callback;
window->glDrawData = userdata;
__END__;
}
#endif // HAVE_OPENGL
static void icvDeleteWindow( CvWindow* window )
{
CvTrackbar* trackbar;
if( window->prev )
window->prev->next = window->next;
else
hg_windows = window->next;
if( window->next )
window->next->prev = window->prev;
window->prev = window->next = 0;
gtk_widget_destroy( window->frame );
for( trackbar = window->toolbar.first; trackbar != 0; )
{
CvTrackbar* next = trackbar->next;
cvFree( &trackbar );
trackbar = next;
}
cvFree( &window );
#ifdef HAVE_GTHREAD
// if last window, send key press signal
// to jump out of any waiting cvWaitKey's
if(hg_windows==0 && thread_started){
g_cond_broadcast(cond_have_key);
}
#endif
}
CV_IMPL void cvDestroyWindow( const char* name )
{
CV_FUNCNAME( "cvDestroyWindow" );
__BEGIN__;
CvWindow* window;
if(!name)
CV_ERROR( CV_StsNullPtr, "NULL name string" );
window = icvFindWindowByName( name );
if( !window )
EXIT;
// note that it is possible for the update thread to run this function
// if there is a call to cvShowImage in a mouse callback
// (this would produce a deadlock on window_mutex)
CV_LOCK_MUTEX();
icvDeleteWindow( window );
CV_UNLOCK_MUTEX();
__END__;
}
CV_IMPL void
cvDestroyAllWindows( void )
{
CV_LOCK_MUTEX();
while( hg_windows )
{
CvWindow* window = hg_windows;
icvDeleteWindow( window );
}
CV_UNLOCK_MUTEX();
}
// CvSize icvCalcOptimalWindowSize( CvWindow * window, CvSize new_image_size){
// CvSize window_size;
// GtkWidget * toplevel = gtk_widget_get_toplevel( window->frame );
// gdk_drawable_get_size( GDK_DRAWABLE(toplevel->window),
// &window_size.width, &window_size.height );
// window_size.width = window_size.width + new_image_size.width - window->widget->allocation.width;
// window_size.height = window_size.height + new_image_size.height - window->widget->allocation.height;
// return window_size;
// }
CV_IMPL void
cvShowImage( const char* name, const CvArr* arr )
{
CV_FUNCNAME( "cvShowImage" );
__BEGIN__;
CvWindow* window;
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name" );
CV_LOCK_MUTEX();
window = icvFindWindowByName(name);
if(!window)
{
cvNamedWindow(name, 1);
window = icvFindWindowByName(name);
}
if( window && arr )
{
#ifdef HAVE_OPENGL
if (window->useGl)
{
cv::imshow(name, cv::cvarrToMat(arr));
return;
}
#endif
CvImageWidget * image_widget = CV_IMAGE_WIDGET( window->widget );
cvImageWidgetSetImage( image_widget, arr );
}
CV_UNLOCK_MUTEX();
__END__;
}
CV_IMPL void cvResizeWindow(const char* name, int width, int height )
{
CV_FUNCNAME( "cvResizeWindow" );
__BEGIN__;
CvWindow* window;
CvImageWidget * image_widget;
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name" );
window = icvFindWindowByName(name);
if(!window)
EXIT;
image_widget = CV_IMAGE_WIDGET( window->widget );
//if(image_widget->flags & CV_WINDOW_AUTOSIZE)
//EXIT;
CV_LOCK_MUTEX();
gtk_window_set_resizable( GTK_WINDOW(window->frame), 1 );
gtk_window_resize( GTK_WINDOW(window->frame), width, height );
// disable initial resize since presumably user wants to keep
// this window size
image_widget->flags &= ~CV_WINDOW_NO_IMAGE;
CV_UNLOCK_MUTEX();
__END__;
}
CV_IMPL void cvMoveWindow( const char* name, int x, int y )
{
CV_FUNCNAME( "cvMoveWindow" );
__BEGIN__;
CvWindow* window;
if( !name )
CV_ERROR( CV_StsNullPtr, "NULL name" );
window = icvFindWindowByName(name);
if(!window)
EXIT;
CV_LOCK_MUTEX();
gtk_window_move( GTK_WINDOW(window->frame), x, y );
CV_UNLOCK_MUTEX();
__END__;
}
static CvTrackbar*
icvFindTrackbarByName( const CvWindow* window, const char* name )
{
CvTrackbar* trackbar = window->toolbar.first;
for( ; trackbar != 0 && strcmp( trackbar->name, name ) != 0; trackbar = trackbar->next )
;
return trackbar;
}
static int
icvCreateTrackbar( const char* trackbar_name, const char* window_name,
int* val, int count, CvTrackbarCallback on_notify,
CvTrackbarCallback2 on_notify2, void* userdata )
{
int result = 0;
CV_FUNCNAME( "icvCreateTrackbar" );
__BEGIN__;
/*char slider_name[32];*/
CvWindow* window = 0;
CvTrackbar* trackbar = 0;
if( !window_name || !trackbar_name )
CV_ERROR( CV_StsNullPtr, "NULL window or trackbar name" );
if( count <= 0 )
CV_ERROR( CV_StsOutOfRange, "Bad trackbar maximal value" );
window = icvFindWindowByName(window_name);
if( !window )
EXIT;
trackbar = icvFindTrackbarByName(window,trackbar_name);
CV_LOCK_MUTEX();
if( !trackbar )
{
int len = strlen(trackbar_name);
trackbar = (CvTrackbar*)cvAlloc(sizeof(CvTrackbar) + len + 1);
memset( trackbar, 0, sizeof(*trackbar));
trackbar->signature = CV_TRACKBAR_MAGIC_VAL;
trackbar->name = (char*)(trackbar+1);
memcpy( trackbar->name, trackbar_name, len + 1 );
trackbar->parent = window;
trackbar->next = window->toolbar.first;
window->toolbar.first = trackbar;
GtkWidget* hscale_box = gtk_hbox_new( FALSE, 10 );
GtkWidget* hscale_label = gtk_label_new( trackbar_name );
GtkWidget* hscale = gtk_hscale_new_with_range( 0, count, 1 );
gtk_scale_set_digits( GTK_SCALE(hscale), 0 );
//gtk_scale_set_value_pos( hscale, GTK_POS_TOP );
gtk_scale_set_draw_value( GTK_SCALE(hscale), TRUE );
trackbar->widget = hscale;
gtk_box_pack_start( GTK_BOX(hscale_box), hscale_label, FALSE, FALSE, 5 );
gtk_widget_show( hscale_label );
gtk_box_pack_start( GTK_BOX(hscale_box), hscale, TRUE, TRUE, 5 );
gtk_widget_show( hscale );
gtk_box_pack_start( GTK_BOX(window->paned), hscale_box, FALSE, FALSE, 5 );
gtk_widget_show( hscale_box );
}
if( val )
{
int value = *val;
if( value < 0 )
value = 0;
if( value > count )
value = count;
gtk_range_set_value( GTK_RANGE(trackbar->widget), value );
trackbar->pos = value;
trackbar->data = val;
}
trackbar->maxval = count;
trackbar->notify = on_notify;
trackbar->notify2 = on_notify2;
trackbar->userdata = userdata;
g_signal_connect( trackbar->widget, "value-changed",
G_CALLBACK(icvOnTrackbar), trackbar );
// queue a widget resize to trigger a window resize to
// compensate for the addition of trackbars
gtk_widget_queue_resize( GTK_WIDGET(window->widget) );
CV_UNLOCK_MUTEX();
result = 1;
__END__;
return result;
}
CV_IMPL int
cvCreateTrackbar( const char* trackbar_name, const char* window_name,
int* val, int count, CvTrackbarCallback on_notify )
{
return icvCreateTrackbar(trackbar_name, window_name, val, count,
on_notify, 0, 0);
}
CV_IMPL int
cvCreateTrackbar2( const char* trackbar_name, const char* window_name,
int* val, int count, CvTrackbarCallback2 on_notify2,
void* userdata )
{
return icvCreateTrackbar(trackbar_name, window_name, val, count,
0, on_notify2, userdata);
}
CV_IMPL void
cvSetMouseCallback( const char* window_name, CvMouseCallback on_mouse, void* param )
{
CV_FUNCNAME( "cvSetMouseCallback" );
__BEGIN__;
CvWindow* window = 0;
if( !window_name )
CV_ERROR( CV_StsNullPtr, "NULL window name" );
window = icvFindWindowByName(window_name);
if( !window )
EXIT;
window->on_mouse = on_mouse;
window->on_mouse_param = param;
__END__;
}
CV_IMPL int cvGetTrackbarPos( const char* trackbar_name, const char* window_name )
{
int pos = -1;
CV_FUNCNAME( "cvGetTrackbarPos" );
__BEGIN__;
CvWindow* window;
CvTrackbar* trackbar = 0;
if( trackbar_name == 0 || window_name == 0 )
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
window = icvFindWindowByName( window_name );
if( window )
trackbar = icvFindTrackbarByName( window, trackbar_name );
if( trackbar )
pos = trackbar->pos;
__END__;
return pos;
}
CV_IMPL void cvSetTrackbarPos( const char* trackbar_name, const char* window_name, int pos )
{
CV_FUNCNAME( "cvSetTrackbarPos" );
__BEGIN__;
CvWindow* window;
CvTrackbar* trackbar = 0;
if( trackbar_name == 0 || window_name == 0 )
CV_ERROR( CV_StsNullPtr, "NULL trackbar or window name" );
window = icvFindWindowByName( window_name );
if( window )
trackbar = icvFindTrackbarByName( window, trackbar_name );
if( trackbar )
{
if( pos < 0 )
pos = 0;
if( pos > trackbar->maxval )
pos = trackbar->maxval;
}
CV_LOCK_MUTEX();
gtk_range_set_value( GTK_RANGE(trackbar->widget), pos );
CV_UNLOCK_MUTEX();
__END__;
}
CV_IMPL void* cvGetWindowHandle( const char* window_name )
{
void* widget = 0;
CV_FUNCNAME( "cvGetWindowHandle" );
__BEGIN__;
CvWindow* window;
if( window_name == 0 )
CV_ERROR( CV_StsNullPtr, "NULL window name" );
window = icvFindWindowByName( window_name );
if( window )
widget = (void*)window->widget;
__END__;
return widget;
}
CV_IMPL const char* cvGetWindowName( void* window_handle )
{
const char* window_name = "";
CV_FUNCNAME( "cvGetWindowName" );
__BEGIN__;
CvWindow* window;
if( window_handle == 0 )
CV_ERROR( CV_StsNullPtr, "NULL window" );
window = icvWindowByWidget( (GtkWidget*)window_handle );
if( window )
window_name = window->name;
__END__;
return window_name;
}
static gboolean icvOnKeyPress( GtkWidget * /*widget*/,
GdkEventKey* event, gpointer /*user_data*/ )
{
int code = 0;
switch( event->keyval )
{
case GDK_Escape:
code = 27;
break;
case GDK_Return:
case GDK_Linefeed:
code = '\n';
break;
case GDK_Tab:
code = '\t';
break;
default:
code = event->keyval;
}
code |= event->state << 16;
#ifdef HAVE_GTHREAD
if(thread_started) g_mutex_lock(last_key_mutex);
#endif
last_key = code;
#ifdef HAVE_GTHREAD
if(thread_started){
// signal any waiting threads
g_cond_broadcast(cond_have_key);
g_mutex_unlock(last_key_mutex);
}
#endif
return FALSE;
}
static void icvOnTrackbar( GtkWidget* widget, gpointer user_data )
{
int pos = cvRound( gtk_range_get_value(GTK_RANGE(widget)));
CvTrackbar* trackbar = (CvTrackbar*)user_data;
if( trackbar && trackbar->signature == CV_TRACKBAR_MAGIC_VAL &&
trackbar->widget == widget )
{
trackbar->pos = pos;
if( trackbar->data )
*trackbar->data = pos;
if( trackbar->notify2 )
trackbar->notify2(pos, trackbar->userdata);
else if( trackbar->notify )
trackbar->notify(pos);
}
}
static gboolean icvOnClose( GtkWidget* widget, GdkEvent* /*event*/, gpointer user_data )
{
CvWindow* window = (CvWindow*)user_data;
if( window->signature == CV_WINDOW_MAGIC_VAL &&
window->frame == widget )
{
icvDeleteWindow(window);
}
return TRUE;
}
static gboolean icvOnMouse( GtkWidget *widget, GdkEvent *event, gpointer user_data )
{
// TODO move this logic to CvImageWidget
CvWindow* window = (CvWindow*)user_data;
CvPoint2D32f pt32f(-1., -1.);
CvPoint pt(-1,-1);
int cv_event = -1, state = 0;
CvImageWidget * image_widget = CV_IMAGE_WIDGET( widget );
if( window->signature != CV_WINDOW_MAGIC_VAL ||
window->widget != widget || !window->widget ||
!window->on_mouse /*|| !image_widget->original_image*/)
return FALSE;
if( event->type == GDK_MOTION_NOTIFY )
{
GdkEventMotion* event_motion = (GdkEventMotion*)event;
cv_event = CV_EVENT_MOUSEMOVE;
pt32f.x = cvRound(event_motion->x);
pt32f.y = cvRound(event_motion->y);
state = event_motion->state;
}
else if( event->type == GDK_BUTTON_PRESS ||
event->type == GDK_BUTTON_RELEASE ||
event->type == GDK_2BUTTON_PRESS )
{
GdkEventButton* event_button = (GdkEventButton*)event;
pt32f.x = cvRound(event_button->x);
pt32f.y = cvRound(event_button->y);
if( event_button->type == GDK_BUTTON_PRESS )
{
cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONDOWN :
event_button->button == 2 ? CV_EVENT_MBUTTONDOWN :
event_button->button == 3 ? CV_EVENT_RBUTTONDOWN : 0;
}
else if( event_button->type == GDK_BUTTON_RELEASE )
{
cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONUP :
event_button->button == 2 ? CV_EVENT_MBUTTONUP :
event_button->button == 3 ? CV_EVENT_RBUTTONUP : 0;
}
else if( event_button->type == GDK_2BUTTON_PRESS )
{
cv_event = event_button->button == 1 ? CV_EVENT_LBUTTONDBLCLK :
event_button->button == 2 ? CV_EVENT_MBUTTONDBLCLK :
event_button->button == 3 ? CV_EVENT_RBUTTONDBLCLK : 0;
}
state = event_button->state;
}
if( cv_event >= 0 ){
// scale point if image is scaled
if( (image_widget->flags & CV_WINDOW_AUTOSIZE)==0 &&
image_widget->original_image &&
image_widget->scaled_image ){
// image origin is not necessarily at (0,0)
int x0 = (widget->allocation.width - image_widget->scaled_image->cols)/2;
int y0 = (widget->allocation.height - image_widget->scaled_image->rows)/2;
pt.x = cvFloor( ((pt32f.x-x0)*image_widget->original_image->cols)/
image_widget->scaled_image->cols );
pt.y = cvFloor( ((pt32f.y-y0)*image_widget->original_image->rows)/
image_widget->scaled_image->rows );
}
else{
pt = cvPointFrom32f( pt32f );
}
// if((unsigned)pt.x < (unsigned)(image_widget->original_image->width) &&
// (unsigned)pt.y < (unsigned)(image_widget->original_image->height) )
{
int flags = (state & GDK_SHIFT_MASK ? CV_EVENT_FLAG_SHIFTKEY : 0) |
(state & GDK_CONTROL_MASK ? CV_EVENT_FLAG_CTRLKEY : 0) |
(state & (GDK_MOD1_MASK|GDK_MOD2_MASK) ? CV_EVENT_FLAG_ALTKEY : 0) |
(state & GDK_BUTTON1_MASK ? CV_EVENT_FLAG_LBUTTON : 0) |
(state & GDK_BUTTON2_MASK ? CV_EVENT_FLAG_MBUTTON : 0) |
(state & GDK_BUTTON3_MASK ? CV_EVENT_FLAG_RBUTTON : 0);
window->on_mouse( cv_event, pt.x, pt.y, flags, window->on_mouse_param );
}
}
return FALSE;
}
static gboolean icvAlarm( gpointer user_data )
{
*(int*)user_data = 1;
return FALSE;
}
CV_IMPL int cvWaitKey( int delay )
{
#ifdef HAVE_GTHREAD
if(thread_started && g_thread_self()!=window_thread){
gboolean expired;
int my_last_key;
// wait for signal or timeout if delay > 0
if(delay>0){
GTimeVal timer;
g_get_current_time(&timer);
g_time_val_add(&timer, delay*1000);
expired = !g_cond_timed_wait(cond_have_key, last_key_mutex, &timer);
}
else{
g_cond_wait(cond_have_key, last_key_mutex);
expired=false;
}
my_last_key = last_key;
g_mutex_unlock(last_key_mutex);
if(expired || hg_windows==0){
return -1;
}
return my_last_key;
}
else{
#endif
int expired = 0;
guint timer = 0;
if( delay > 0 )
timer = g_timeout_add( delay, icvAlarm, &expired );
last_key = -1;
while( gtk_main_iteration_do(TRUE) && last_key < 0 && !expired && hg_windows != 0 )
;
if( delay > 0 && !expired )
g_source_remove(timer);
#ifdef HAVE_GTHREAD
}
#endif
return last_key;
}
#endif // HAVE_GTK
#endif // WIN32
/* End of file. */