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.videoio;
26 
27 import std.string;
28 
29 import opencvd.cvcore;
30 
31 struct _VideoCapture{
32     void* p;
33     
34     // Close VideoCapture object.
35     void close(){
36         VideoCapture_Close(&this);
37     }
38     
39     // fromFile opens a VideoCapture from a file and prepares
40     // to start capturing.
41     bool fromFile(string uri){
42         return VideoCapture_Open(&this, toStringz(uri));
43     }
44     
45     // fromDevice opens a VideoCapture from a device and prepares
46     // to start capturing.
47     bool fromDevice(int device){
48         return VideoCapture_OpenDevice(&this, device);
49     }
50     
51     // Set parameter with property 
52     void setProperty(int prop, double param){
53         VideoCapture_Set(&this, prop, param);
54     }
55     alias set = setProperty;
56     
57     // Get parameter with property 
58     double getProperty(int prop){
59         return VideoCapture_Get(&this, prop);
60     }
61     alias get = getProperty;
62     
63     // a file or capture device.
64     bool isOpened(){
65         return VideoCapture_IsOpened(&this) == 0 ? false: true;
66     }
67     
68     // Read reads the next frame from the VideoCapture to the Mat passed in
69     // as the param. It returns false if the VideoCapture cannot read frame.
70     bool read(Mat buf){
71         return VideoCapture_Read(&this, buf) == 0 ? false: true;
72     }
73     
74     // Grab skips a specific number of frames.
75     void grab(int skip){
76         VideoCapture_Grab(&this, skip);
77     }
78 }
79 
80 alias VideoCapture  = _VideoCapture*;
81 
82 VideoCapture newVideoCapture(){
83     return VideoCapture_New();
84 }
85 /* An example usage
86 
87 void main()
88 {
89     
90     auto cap = newVideoCapture();
91     cap.fromDevice(0);
92 
93     namedWindow("mywin", 0);
94     setWindowTitle("mywin", "süper pencere / 超级窗口");
95 
96     for(;;)
97     {
98         Mat frame = newMat();
99 
100         cap.read(frame);
101 
102         imshow("mywin", frame);
103         
104         if (waitKey(10) == 27) break;
105     }
106     
107     cap.close();
108 }
109 */
110 
111 struct _VideoWriter{
112     void* p;
113     
114     void close(){
115         VideoWriter_Close(&this);
116     }
117     
118     void toFile(string name, string codec, double fps, int width,
119             int height, bool isColor){
120         VideoWriter_Open(&this, toStringz(name), toStringz(codec), fps, width, height, isColor);
121     }
122     
123     bool isOpened(){
124         return VideoWriter_IsOpened(&this) == 0 ? false: true;
125     }
126     
127     void write(Mat img){
128         VideoWriter_Write(&this, img);
129     }
130 }
131 
132 alias VideoWriter  = _VideoWriter*;
133 
134 VideoWriter newVideoWriter(){
135     return VideoWriter_New();
136 }
137 
138 
139 //private {
140     extern (C){
141         // VideoCapture
142         VideoCapture VideoCapture_New();
143         void VideoCapture_Close(VideoCapture v);
144         bool VideoCapture_Open(VideoCapture v, const char* uri);
145         bool VideoCapture_OpenDevice(VideoCapture v, int device);
146         void VideoCapture_Set(VideoCapture v, int prop, double param);
147         double VideoCapture_Get(VideoCapture v, int prop);
148         int VideoCapture_IsOpened(VideoCapture v);
149         int VideoCapture_Read(VideoCapture v, Mat buf);
150         void VideoCapture_Grab(VideoCapture v, int skip);
151 
152         // VideoWriter
153         VideoWriter VideoWriter_New();
154         void VideoWriter_Close(VideoWriter vw);
155         void VideoWriter_Open(VideoWriter vw, const char* name, const char* codec, double fps, int width,
156             int height, bool isColor);
157         int VideoWriter_IsOpened(VideoWriter vw);
158         void VideoWriter_Write(VideoWriter vw, Mat img);
159     }
160 //}
161 
162 enum: int { 
163     CAP_PROP_POS_MSEC =0, 
164     CAP_PROP_POS_FRAMES =1, 
165     CAP_PROP_POS_AVI_RATIO =2, 
166     CAP_PROP_FRAME_WIDTH =3, 
167     CAP_PROP_FRAME_HEIGHT =4, 
168     CAP_PROP_FPS =5, 
169     CAP_PROP_FOURCC =6, 
170     CAP_PROP_FRAME_COUNT =7, 
171     CAP_PROP_FORMAT =8, 
172     CAP_PROP_MODE =9, 
173     CAP_PROP_BRIGHTNESS =10, 
174     CAP_PROP_CONTRAST =11, 
175     CAP_PROP_SATURATION =12, 
176     CAP_PROP_HUE =13, 
177     CAP_PROP_GAIN =14, 
178     CAP_PROP_EXPOSURE =15, 
179     CAP_PROP_CONVERT_RGB =16, 
180     CAP_PROP_WHITE_BALANCE_BLUE_U =17, 
181     CAP_PROP_RECTIFICATION =18, 
182     CAP_PROP_MONOCHROME =19, 
183     CAP_PROP_SHARPNESS =20, 
184     CAP_PROP_AUTO_EXPOSURE =21, 
185     CAP_PROP_GAMMA =22, 
186     CAP_PROP_TEMPERATURE =23, 
187     CAP_PROP_TRIGGER =24, 
188     CAP_PROP_TRIGGER_DELAY =25, 
189     CAP_PROP_WHITE_BALANCE_RED_V =26, 
190     CAP_PROP_ZOOM =27, 
191     CAP_PROP_FOCUS =28, 
192     CAP_PROP_GUID =29, 
193     CAP_PROP_ISO_SPEED =30, 
194     CAP_PROP_BACKLIGHT =32, 
195     CAP_PROP_PAN =33, 
196     CAP_PROP_TILT =34, 
197     CAP_PROP_ROLL =35, 
198     CAP_PROP_IRIS =36, 
199     CAP_PROP_SETTINGS =37, 
200     CAP_PROP_BUFFERSIZE =38, 
201     CAP_PROP_AUTOFOCUS =39, 
202     CAP_PROP_SAR_NUM =40, 
203     CAP_PROP_SAR_DEN =41, 
204     CAP_PROP_BACKEND =42, 
205     CAP_PROP_CHANNEL =43, 
206     CAP_PROP_AUTO_WB =44, 
207     CAP_PROP_WB_TEMPERATURE =45 
208 }