Commit f0197006 authored by Vladimir Dudnik's avatar Vladimir Dudnik

removed original samples and replace them with new ones. modified new samples…

removed original samples and replace them with new ones. modified new samples (reduce code duplication, add cmd line params and short description)
parent 3cb4954d
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
#include <string>
#include <iostream>
#include <queue>
#include "opencv2/core.hpp"
#include "opencv2/core/directx.hpp"
#include "opencv2/core/ocl.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "winapp.hpp"
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
class D3DSample : public WinApp
{
public:
enum MODE
{
MODE_NOP,
MODE_CPU,
MODE_GPU
};
D3DSample(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
WinApp(width, height, window_name)
{
m_shutdown = false;
m_mode = MODE_NOP;
m_modeStr[0] = cv::String("No processing");
m_modeStr[1] = cv::String("Processing on CPU");
m_modeStr[2] = cv::String("Processing on GPU");
m_disableProcessing = false;
m_cap = cap;
}
~D3DSample() {}
virtual int create() { return WinApp::create(); }
virtual int render() = 0;
virtual int cleanup()
{
m_shutdown = true;
return WinApp::cleanup();
}
static float getFps()
{
static std::queue<int64> time_queue;
int64 now = cv::getTickCount();
int64 then = 0;
time_queue.push(now);
if (time_queue.size() >= 2)
then = time_queue.front();
if (time_queue.size() >= 25)
time_queue.pop();
size_t sz = time_queue.size();
float fps = sz * (float)cv::getTickFrequency() / (now - then);
return fps;
}
protected:
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CHAR:
if (wParam >= '0' && wParam <= '2')
{
m_mode = static_cast<MODE>((char)wParam - '0');
return 0;
}
else if (wParam == VK_SPACE)
{
m_disableProcessing = !m_disableProcessing;
return 0;
}
else if (wParam == VK_ESCAPE)
{
return cleanup();
}
break;
case WM_CLOSE:
return cleanup();
case WM_DESTROY:
::PostQuitMessage(0);
return 0;
}
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
// do render at idle
virtual int idle() { return render(); }
protected:
bool m_shutdown;
bool m_disableProcessing;
MODE m_mode;
cv::String m_modeStr[3];
cv::VideoCapture m_cap;
cv::Mat m_frame_bgr;
cv::Mat m_frame_rgba;
};
#define ENTRY_POINT(type, title) \
static void help() \
{ \
printf( \
"\nSample demonstrating interoperability of DirectX and OpenCL with OpenCV.\n" \
"Hot keys: \n" \
" 0 - no processing\n" \
" 1 - blur DX surface on CPU through OpenCV\n" \
" 2 - blur DX surface on GPU through OpenCV using OpenCL\n" \
" ESC - exit\n\n"); \
} \
\
static const char* keys = \
{ \
"{c camera | true | use camera or not}" \
"{f file | | movie file name }" \
"{h help | false | print help info }" \
}; \
\
\
int main(int argc, char** argv) \
{ \
cv::CommandLineParser parser(argc, argv, keys); \
bool useCamera = parser.has("camera"); \
string file = parser.get<string>("file"); \
bool showHelp = parser.get<bool>("help"); \
\
if (showHelp) \
help(); \
\
parser.printMessage(); \
\
cv::VideoCapture cap; \
\
if (useCamera) \
cap.open(0); \
else \
cap.open(file.c_str()); \
\
if (!cap.isOpened()) \
{ \
printf("can not open camera or video file\n"); \
return -1; \
} \
\
int width = (int)cap.get(CAP_PROP_FRAME_WIDTH); \
int height = (int)cap.get(CAP_PROP_FRAME_HEIGHT); \
\
std::string wndname = title; \
\
type app(width, height, wndname, cap); \
\
try \
{ \
app.create(); \
return app.run(); \
} \
\
catch (cv::Exception& e) \
{ \
std::cerr << "Exception: " << e.what() << std::endl; \
return 10; \
} \
\
catch (...) \
{ \
std::cerr << "FATAL ERROR: Unknown exception" << std::endl; \
return 11; \
} \
}
......@@ -5,8 +5,6 @@
#define WINCLASS "WinAppWnd"
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
class WinApp
{
public:
......@@ -16,14 +14,13 @@ public:
m_height = height;
m_window_name = window_name;
m_hInstance = ::GetModuleHandle(NULL);
m_hWnd = 0;
}
virtual ~WinApp()
{
::UnregisterClass(WINCLASS, m_hInstance);
}
virtual ~WinApp() {}
int Create()
virtual int create()
{
WNDCLASSEX wcex;
......@@ -41,9 +38,12 @@ public:
wcex.hIconSm = 0;
ATOM wc = ::RegisterClassEx(&wcex);
if (!wc)
return -1;
RECT rc = { 0, 0, m_width, m_height };
::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false);
if(!::AdjustWindowRect(&rc, WS_OVERLAPPEDWINDOW, false))
return -1;
m_hWnd = ::CreateWindow(
(LPCTSTR)wc, m_window_name.c_str(),
......@@ -58,10 +58,9 @@ public:
::UpdateWindow(m_hWnd);
::SetFocus(m_hWnd);
return init();
}
return 0;
} // create()
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
int run()
{
......@@ -78,23 +77,32 @@ public:
}
else
{
render();
idle();
}
}
return static_cast<int>(msg.wParam);
}
} // run()
virtual int cleanup()
{
::DestroyWindow(m_hWnd);
::UnregisterClass(WINCLASS, m_hInstance);
return 0;
} // cleanup()
protected:
// dispatch message handling to method of class
static LRESULT CALLBACK StaticWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
WinApp* pWnd;
if (message == WM_NCCREATE)
{
LPCREATESTRUCT pCreateStruct = ((LPCREATESTRUCT)lParam);
pWnd = (WinApp*)(pCreateStruct->lpCreateParams);
::SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)pWnd);
LPCREATESTRUCT pCreateStruct = reinterpret_cast<LPCREATESTRUCT>(lParam);
pWnd = static_cast<WinApp*>(pCreateStruct->lpCreateParams);
::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
}
pWnd = GetObjectFromWindow(hWnd);
......@@ -103,15 +111,14 @@ protected:
return pWnd->WndProc(hWnd, message, wParam, lParam);
else
return ::DefWindowProc(hWnd, message, wParam, lParam);
}
} // StaticWndProc()
inline static WinApp* GetObjectFromWindow(HWND hWnd)
{
return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA);
}
inline static WinApp* GetObjectFromWindow(HWND hWnd) { return (WinApp*)::GetWindowLongPtr(hWnd, GWLP_USERDATA); }
virtual int init() = 0;
virtual int render() = 0;
// actual wnd message handling
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) = 0;
// idle processing
virtual int idle() = 0;
HINSTANCE m_hInstance;
HWND m_hWnd;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment