1 /*
2 Copyright (c) 2019 Ferhat Kurtulmuş
3 Boost Software License - Version 1.0 - August 17th, 2003
4 Permission is hereby granted, free of charge, to any person or organization
5 obtaining a copy of the software and accompanying documentation covered by
6 this license (the "Software") to use, reproduce, display, distribute,
7 execute, and transmit the Software, and to prepare derivative works of the
8 Software, and to permit third-parties to whom the Software is furnished to
9 do so, all subject to the following:
10 The copyright notices in the Software and this entire statement, including
11 the above license grant, this restriction and the following disclaimer,
12 must be included in all copies of the Software, in whole or in part, and
13 all derivative works of the Software, unless such copies or derivative
14 works are solely in the form of machine-executable object code generated by
15 a source language processor.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 */
24 
25 module opencvd.highgui;
26 
27 import std..string;
28 
29 import opencvd.cvcore;
30 
31 // function pointers
32 extern (C) @nogc nothrow {
33     alias TrackbarCallback = void function(int, void*);
34     alias MouseCallback = void function(int, int, int, int, void*);
35 }
36 
37 enum: int {
38     EVENT_MOUSEMOVE      = 0, //!< indicates that the mouse pointer has moved over the window.
39     EVENT_LBUTTONDOWN    = 1, //!< indicates that the left mouse button is pressed.
40     EVENT_RBUTTONDOWN    = 2, //!< indicates that the right mouse button is pressed.
41     EVENT_MBUTTONDOWN    = 3, //!< indicates that the middle mouse button is pressed.
42     EVENT_LBUTTONUP      = 4, //!< indicates that left mouse button is released.
43     EVENT_RBUTTONUP      = 5, //!< indicates that right mouse button is released.
44     EVENT_MBUTTONUP      = 6, //!< indicates that middle mouse button is released.
45     EVENT_LBUTTONDBLCLK  = 7, //!< indicates that left mouse button is double clicked.
46     EVENT_RBUTTONDBLCLK  = 8, //!< indicates that right mouse button is double clicked.
47     EVENT_MBUTTONDBLCLK  = 9, //!< indicates that middle mouse button is double clicked.
48     EVENT_MOUSEWHEEL     = 10,//!< positive and negative values mean forward and backward scrolling, respectively.
49     EVENT_MOUSEHWHEEL    = 11 //!< positive and negative values mean right and left scrolling, respectively.
50 }
51 
52 enum: int {
53     EVENT_FLAG_LBUTTON   = 1, //!< indicates that the left mouse button is down.
54     EVENT_FLAG_RBUTTON   = 2, //!< indicates that the right mouse button is down.
55     EVENT_FLAG_MBUTTON   = 4, //!< indicates that the middle mouse button is down.
56     EVENT_FLAG_CTRLKEY   = 8, //!< indicates that CTRL Key is pressed.
57     EVENT_FLAG_SHIFTKEY  = 16,//!< indicates that SHIFT Key is pressed.
58     EVENT_FLAG_ALTKEY    = 32 //!< indicates that ALT Key is pressed.
59 }
60 private extern (C) @nogc nothrow {
61     // Window
62     void Window_New(const char* winname, int flags);
63     void Window_Close(const char* winname);
64     void Window_IMShow(const char* winname, Mat mat);
65     double Window_GetProperty(const char* winname, int flag);
66     void Window_SetProperty(const char* winname, int flag, double value);
67     void Window_SetTitle(const char* winname, const char* title);
68     int Window_WaitKey(int);
69     void Window_Move(const char* winname, int x, int y);
70     void Window_Resize(const char* winname, int width, int height);
71     Rect Window_SelectROI(const char* winname, Mat img);
72     Rects Window_SelectROIs(const char* winname, Mat img);
73 
74     // Trackbar
75     void Trackbar_CreateWithCallBack(const char* winname, const char* trackname, int* value, int count, TrackbarCallback on_trackbar, void* userdata);
76     
77     void Trackbar_Create(const char* winname, const char* trackname, int max);
78     int Trackbar_GetPos(const char* winname, const char* trackname);
79     void Trackbar_SetPos(const char* winname, const char* trackname, int pos);
80     void Trackbar_SetMin(const char* winname, const char* trackname, int pos);
81     void Trackbar_SetMax(const char* winname, const char* trackname, int pos);
82     
83     void Win_setMouseCallback(const	char* winname, MouseCallback onMouse, void *userdata);
84 }
85 
86 enum: int { // cv::WindowFlags
87        WINDOW_NORMAL     = 0x00000000, //!< the user can resize the window (no constraint) / also use to switch a fullscreen window to a normal size.
88        WINDOW_AUTOSIZE   = 0x00000001, //!< the user cannot resize the window, the size is constrainted by the image displayed.
89        WINDOW_OPENGL     = 0x00001000, //!< window with opengl support.
90        WINDOW_FULLSCREEN = 1,          //!< change the window to fullscreen.
91        WINDOW_FREERATIO  = 0x00000100, //!< the image expends as much as it can (no ratio constraint).
92        WINDOW_KEEPRATIO  = 0x00000000, //!< the ratio of the image is respected.
93        WINDOW_GUI_EXPANDED=0x00000000, //!< status bar and tool bar
94        WINDOW_GUI_NORMAL = 0x00000010, //!< old fashious way
95 }
96 
97 void namedWindow(string winname, int flags = 0) @nogc nothrow {
98     Window_New(winname.ptr, flags);
99 }
100 
101 int waitKey(int val = 0) @nogc nothrow {
102     return Window_WaitKey(val);
103 }
104 
105 void destroyWindow(string winname) @nogc nothrow {
106     Window_Close(winname.ptr);
107 }
108 
109 void imshow(string winname, Mat mat) @nogc nothrow {
110     Window_IMShow(winname.ptr, mat);
111 }
112 
113 enum: int {
114     WND_PROP_FULLSCREEN = 0,
115     WND_PROP_AUTOSIZE = 1,
116     WND_PROP_ASPECT_RATIO = 2,
117     WND_PROP_OPENGL = 3
118 }
119 
120 
121 double getWindowProperty(string winname, int flag) @nogc nothrow {
122     return Window_GetProperty(winname.ptr, flag);
123 }
124 
125 void setWindowProperty(string winname, int flag, double value) @nogc nothrow {
126     Window_SetProperty(winname.ptr, flag, value);
127 }
128 
129 void setWindowTitle(string winname, string title) @nogc nothrow {
130     Window_SetTitle(winname.ptr, title.ptr);
131 }
132 
133 void moveWindow(string winname, int x, int y) @nogc nothrow {
134     Window_Move(winname.ptr, x, y);
135 }
136 
137 void resizeWindow(string winname, int width, int height) @nogc nothrow {
138     Window_Resize(winname.ptr, width, height);
139 }
140 
141 Rect selectROI(string winname, Mat img) @nogc nothrow {
142     return Window_SelectROI(winname.ptr, img);
143 }
144 
145 Rects selectROIs(string winname, Mat img){
146     return Window_SelectROIs(winname.ptr, img);
147 }
148 
149 struct TrackBar {
150 	string name;
151 	string winname;
152     int max;
153     
154     this(string _name, string _winname, int _max) @nogc nothrow {
155        name = _name;
156        winname = _winname;
157        max = _max;
158        
159        Trackbar_Create(winname.ptr, name.ptr, _max);
160     }
161     
162     this(string _name, string _winname, int* value, int count, TrackbarCallback on_trackbar, void* userdata = null) @nogc nothrow {
163         name = _name;
164         winname = _winname;
165         max = count;
166         Trackbar_CreateWithCallBack(name.ptr, winname.ptr, value, count, on_trackbar, userdata);
167     }
168     
169     int getPos() @nogc nothrow {
170         return Trackbar_GetPos(winname.ptr, name.ptr);
171     }
172     
173     void setPos(int pos) @nogc nothrow {
174         Trackbar_SetPos(winname.ptr, name.ptr, pos);
175     }
176     
177     void setMin(int pos) @nogc nothrow {
178         Trackbar_SetMin(winname.ptr, name.ptr, pos);
179     }
180     
181     void setMax(int pos) @nogc nothrow {
182         Trackbar_SetMax(winname.ptr, name.ptr, pos);
183     }
184 }
185 
186 void setMouseCallback(string winname, MouseCallback onMouse, void *userdata = null) @nogc nothrow {
187     Win_setMouseCallback(winname.ptr, onMouse, userdata);
188 }
189 
190 void createTrackbar(string trackname, string winname, int* value, int count, TrackbarCallback on_trackbar, void* userdata = null) @nogc nothrow {
191     Trackbar_CreateWithCallBack(trackname.ptr, winname.ptr, value, count, on_trackbar, userdata);
192 }