From af806bc81628a202e36c90a26c258d10710dde9f Mon Sep 17 00:00:00 2001 From: Vladimir Dudnik Date: Tue, 19 May 2015 16:08:25 +0300 Subject: [PATCH] replaced macros with template func --- samples/directx/d3d10_interop.cpp | 6 +- samples/directx/d3d11_interop.cpp | 6 +- samples/directx/d3d9_interop.cpp | 6 +- samples/directx/d3d9ex_interop.cpp | 6 +- samples/directx/d3dsample.hpp | 131 +++++++++++++++-------------- 5 files changed, 86 insertions(+), 69 deletions(-) diff --git a/samples/directx/d3d10_interop.cpp b/samples/directx/d3d10_interop.cpp index 2e860bb575..99defa256c 100644 --- a/samples/directx/d3d10_interop.cpp +++ b/samples/directx/d3d10_interop.cpp @@ -296,4 +296,8 @@ private: // main func -ENTRY_POINT(D3D10WinApp, "D3D10 interop sample"); +int main(int argc, char** argv) +{ + std::string title = "D3D10 interop sample"; + return d3d_app(argc, argv, title); +} diff --git a/samples/directx/d3d11_interop.cpp b/samples/directx/d3d11_interop.cpp index 9a310237ef..02b576bbe7 100644 --- a/samples/directx/d3d11_interop.cpp +++ b/samples/directx/d3d11_interop.cpp @@ -302,4 +302,8 @@ private: // main func -ENTRY_POINT(D3D11WinApp, "D3D11 interop sample"); +int main(int argc, char** argv) +{ + std::string title = "D3D11 interop sample"; + return d3d_app(argc, argv, title); +} diff --git a/samples/directx/d3d9_interop.cpp b/samples/directx/d3d9_interop.cpp index f371f03247..61a916c1e5 100644 --- a/samples/directx/d3d9_interop.cpp +++ b/samples/directx/d3d9_interop.cpp @@ -294,4 +294,8 @@ private: // main func -ENTRY_POINT(D3D9WinApp, "D3D9 interop sample"); +int main(int argc, char** argv) +{ + std::string title = "D3D9 interop sample"; + return d3d_app(argc, argv, title); +} diff --git a/samples/directx/d3d9ex_interop.cpp b/samples/directx/d3d9ex_interop.cpp index 46f2c5f7ba..3d133a6b47 100644 --- a/samples/directx/d3d9ex_interop.cpp +++ b/samples/directx/d3d9ex_interop.cpp @@ -295,4 +295,8 @@ private: // main func -ENTRY_POINT(D3D9ExWinApp, "D3D9Ex interop sample"); +int main(int argc, char** argv) +{ + std::string title = "D3D9Ex interop sample"; + return d3d_app(argc, argv, title); +} diff --git a/samples/directx/d3dsample.hpp b/samples/directx/d3dsample.hpp index d829f1c9ea..e70a651055 100644 --- a/samples/directx/d3dsample.hpp +++ b/samples/directx/d3dsample.hpp @@ -113,73 +113,74 @@ protected: }; -#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) \ -{ \ +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 }" +}; + + +template +int d3d_app(int argc, char** argv, std::string& title) +{ cv::CommandLineParser parser(argc, argv, keys); \ bool useCamera = parser.has("camera"); \ string file = parser.get("file"); \ bool showHelp = parser.get("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; \ - } \ + + 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; + + TApp 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; + } }