1 // videoio to XAML bridge for OpenCV
2
3 // Copyright (c) Microsoft Open Technologies, Inc.
4 // All rights reserved.
5 //
6 // (3 - clause BSD License)
7 //
8 // Redistribution and use in source and binary forms, with or without modification, are permitted provided that
9 // the following conditions are met:
10 //
11 // 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
12 // following disclaimer.
13 // 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
14 // following disclaimer in the documentation and/or other materials provided with the distribution.
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or
16 // promote products derived from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
19 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 // PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
21 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
22 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING
24 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25 // POSSIBILITY OF SUCH DAMAGE.
26
27 #include "opencv2\videoio\cap_winrt.hpp"
28 #include "cap_winrt_capture.hpp"
29 #include "cap_winrt_bridge.hpp"
30 #include "cap_winrt_video.hpp"
31
32 using namespace Windows::Foundation;
33 using namespace Windows::Media::Capture;
34 using namespace Windows::Media::MediaProperties;
35 using namespace Windows::Devices::Enumeration;
36
37 using namespace Windows::UI::Xaml::Media::Imaging;
38 using namespace Microsoft::WRL;
39
40 using namespace Platform;
41 using namespace ::Concurrency;
42
43 using namespace ::std;
44
45 /***************************** VideoioBridge class ******************************/
46
47 // non-blocking
requestForUIthreadAsync(int action)48 void VideoioBridge::requestForUIthreadAsync(int action)
49 {
50 reporter.report(action);
51 }
52
getInstance()53 VideoioBridge& VideoioBridge::getInstance()
54 {
55 static VideoioBridge instance;
56 return instance;
57 }
58
swapInputBuffers()59 void VideoioBridge::swapInputBuffers()
60 {
61 // TODO: already locked, check validity
62 // lock_guard<mutex> lock(inputBufferMutex);
63 swap(backInputPtr, frontInputPtr);
64 //if (currentFrame != frameCounter)
65 //{
66 // currentFrame = frameCounter;
67 // swap(backInputPtr, frontInputPtr);
68 //}
69 }
70
swapOutputBuffers()71 void VideoioBridge::swapOutputBuffers()
72 {
73 lock_guard<mutex> lock(outputBufferMutex);
74 swap(frontOutputBuffer, backOutputBuffer);
75 }
76
allocateOutputBuffers()77 void VideoioBridge::allocateOutputBuffers()
78 {
79 frontOutputBuffer = ref new WriteableBitmap(width, height);
80 backOutputBuffer = ref new WriteableBitmap(width, height);
81 }
82
83 // performed on UI thread
allocateBuffers(int width,int height)84 void VideoioBridge::allocateBuffers(int width, int height)
85 {
86 // allocate input Mats (bgra8 = CV_8UC4, RGB24 = CV_8UC3)
87 frontInputMat.create(height, width, CV_8UC3);
88 backInputMat.create(height, width, CV_8UC3);
89
90 frontInputPtr = frontInputMat.ptr(0);
91 backInputPtr = backInputMat.ptr(0);
92
93 allocateOutputBuffers();
94 }
95
96 // performed on UI thread
openCamera()97 bool VideoioBridge::openCamera()
98 {
99 // buffers must alloc'd on UI thread
100 allocateBuffers(width, height);
101
102 // nb. video capture device init must be done on UI thread;
103 if (!Video::getInstance().isStarted())
104 {
105 Video::getInstance().initGrabber(deviceIndex, width, height);
106 return true;
107 }
108
109 return false;
110 }
111
112 // nb on UI thread
updateFrameContainer()113 void VideoioBridge::updateFrameContainer()
114 {
115 // copy output Mat to WBM
116 Video::getInstance().CopyOutput();
117
118 // set XAML image element with image WBM
119 cvImage->Source = backOutputBuffer;
120 }
121
imshow()122 void VideoioBridge::imshow()
123 {
124 swapOutputBuffers();
125 requestForUIthreadAsync(cv::UPDATE_IMAGE_ELEMENT);
126 }
127
getDeviceIndex()128 int VideoioBridge::getDeviceIndex()
129 {
130 return deviceIndex;
131 }
132
setDeviceIndex(int index)133 void VideoioBridge::setDeviceIndex(int index)
134 {
135 deviceIndex = index;
136 }
137
getWidth()138 int VideoioBridge::getWidth()
139 {
140 return width;
141 }
142
getHeight()143 int VideoioBridge::getHeight()
144 {
145 return height;
146 }
147
setWidth(int _width)148 void VideoioBridge::setWidth(int _width)
149 {
150 width = _width;
151 }
152
setHeight(int _height)153 void VideoioBridge::setHeight(int _height)
154 {
155 height = _height;
156 }
157
158 // end