Argus Camera Sample Argus Camera Sample

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AppModuleGeneric.cpp
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2016-2019, NVIDIA CORPORATION. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of NVIDIA CORPORATION nor the names of its
13 * contributors may be used to endorse or promote products derived
14 * from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
20 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
23 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
24 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "AppModuleGeneric.h"
33 #include "XMLConfig.h"
34 #include "Dispatcher.h"
35 #include "Error.h"
36 #include "Options.h"
37 #include "Window.h"
38
39 #include <Argus/Ext/DeFog.h>
40
41 namespace ArgusSamples
42 {
43
44 /**
45 * Default configuration file name
46 */
47 #define DEFAULT_CONFIG_FILE "argusAppConfig.xml"
48
49 /* static */ bool AppModuleGeneric::info(void *userPtr, const char *optArg)
50 {
51 std::string info;
52 PROPAGATE_ERROR(Dispatcher::getInstance().getInfo(info));
53 printf("%s\n", info.c_str());
54
55 return true;
56 }
57
58 /* static */ bool AppModuleGeneric::loadConfig(void *userPtr, const char *optArg)
59 {
60 /// @todo ask for file if called from GUI
61
62 const char *configFile = DEFAULT_CONFIG_FILE;
63 if (optArg)
64 configFile = optArg;
65 PROPAGATE_ERROR(ArgusSamples::loadConfig(configFile));
66 return true;
67 }
68
69 /* static */ bool AppModuleGeneric::saveConfig(void *userPtr, const char *optArg)
70 {
71 /// @todo ask for file if called from GUI
72
73 const char *configFile = DEFAULT_CONFIG_FILE;
74 if (optArg)
75 configFile = optArg;
76 PROPAGATE_ERROR(ArgusSamples::saveConfig(configFile));
77 return true;
78 }
79
80 /* static */ bool AppModuleGeneric::quit(void *userPtr, const char *optArg)
81 {
82 PROPAGATE_ERROR(Window::getInstance().requestExit());
83 return true;
84 }
85
86 AppModuleGeneric::AppModuleGeneric()
87 : m_initialized(false)
88 , m_running(false)
89 , m_guiMenuBar(NULL)
90 , m_guiContainerConfig(NULL)
91 , m_guiConfig(NULL)
92 {
93 }
94
95 AppModuleGeneric::~AppModuleGeneric()
96 {
97 shutdown();
98 }
99
100 bool AppModuleGeneric::initialize(Options &options)
101 {
102 if (m_initialized)
103 return true;
104
105 PROPAGATE_ERROR(options.addDescription(
106 "The supported value range of some settings is device or sensor mode dependent.\n"
107 "Use the '--info' option to get a list of the supported values.\n"));
108
109 PROPAGATE_ERROR(options.addOption(
110 Options::Option("info", 'i', "",
111 Options::Option::TYPE_ACTION,"print information on devices.", info)));
112
113 PROPAGATE_ERROR(options.addOption(
114 Options::Option("loadconfig", 0, "FILE",
115 Options::Option::TYPE_ACTION, "load configuration from XML FILE. ",
116 loadConfig, this, DEFAULT_CONFIG_FILE)));
117 PROPAGATE_ERROR(options.addOption(
118 Options::Option("saveconfig", 0, "FILE",
119 Options::Option::TYPE_ACTION, "save configuration to XML FILE. ",
120 saveConfig, this, DEFAULT_CONFIG_FILE)));
121
122 PROPAGATE_ERROR(options.addOption(
123 createValueOption("verbose", 0, "0 or 1", "enable verbose mode.",
124 Dispatcher::getInstance().m_verbose, "1")));
125 PROPAGATE_ERROR(options.addOption(
126 createValueOption("kpi", 0, "0 or 1", "enable kpi mode.",
127 Dispatcher::getInstance().m_kpi, "1")));
128 PROPAGATE_ERROR(options.addOption(
129 createValueOption("device", 'd', "INDEX", "select camera device with INDEX.",
130 Dispatcher::getInstance().m_deviceIndex)));
131
132 // source settings
133 PROPAGATE_ERROR(options.addOption(
134 createValueOption("exposuretimerange", 0, "RANGE",
135 "sets the exposure time range to RANGE, in nanoseconds.",
136 Dispatcher::getInstance().m_exposureTimeRange)));
137 PROPAGATE_ERROR(options.addOption(
138 createValueOption("gainrange", 0, "RANGE", "sets the gain range to RANGE.",
139 Dispatcher::getInstance().m_gainRange)));
140 PROPAGATE_ERROR(options.addOption(
141 createValueOption("sensormode", 0, "INDEX", "set sensor mode to INDEX.",
142 Dispatcher::getInstance().m_sensorModeIndex)));
143 PROPAGATE_ERROR(options.addOption(
144 createValueOption("framerate", 0, "RATE",
145 "set the sensor frame rate to RATE. If RATE is 0 then VFR (variable frame rate) is "
146 "enabled.", Dispatcher::getInstance().m_frameRate)));
147 PROPAGATE_ERROR(options.addOption(
148 createValueOption("focusposition", 0, "POSITION",
149 "sets the focus position to POSITION, in focuser units.",
150 Dispatcher::getInstance().m_focusPosition)));
151 PROPAGATE_ERROR(options.addOption(
152 createValueOption("aperturemotorstep", 0, "STEP",
153 "sets the aperture motor step to STEP, in step units.",
154 Dispatcher::getInstance().m_apertureMotorStep)));
155 PROPAGATE_ERROR(options.addOption(
156 createValueOption("aperturemotorspeed", 0, "SPEED",
157 "sets the aperture motor speed to SPEED, in steps/second units.",
158 Dispatcher::getInstance().m_apertureMotorSpeed)));
159 PROPAGATE_ERROR(options.addOption(
160 createValueOption("captureyuvformat", 0, "FORMAT",
161 "YUV format for image capture.", Dispatcher::getInstance().m_captureYuvFormat)));
162
163 // output settings
164 PROPAGATE_ERROR(options.addOption(
165 createValueOption("outputsize", 0, "WIDTHxHEIGHT",
166 "set the still and video output size to WIDTHxHEIGHT (e.g. 1920x1080). If WIDTHxHEIGHT "
167 "is '0x0' the output size is the sensor mode size.",
168 Dispatcher::getInstance().m_outputSize)));
169 PROPAGATE_ERROR(options.addOption(
170 createValueOption("outputpath", 0, "PATH",
171 "set the output file path. A file name, an incrementing index and the file extension"
172 " will be appended. E.g. setting 'folder/' will result in 'folder/image0.jpg' or "
173 "'folder/video0.mp4'. '/dev/null' can be used to discard output.",
174 Dispatcher::getInstance().m_outputPath)));
175
176 // stream settings
177 PROPAGATE_ERROR(options.addOption(
178 createValueOption("denoise", 0, "MODE", "set the denoising mode.",
179 Dispatcher::getInstance().m_denoiseMode)));
180 PROPAGATE_ERROR(options.addOption(
181 createValueOption("denoisestrength", 0, "POSITION", "set the denoising strength.",
182 Dispatcher::getInstance().m_denoiseStrength)));
183 PROPAGATE_ERROR(options.addOption(
184 createValueOption("edgeenhance", 0, "MODE", "set the edge enhancement mode.",
185 Dispatcher::getInstance().m_edgeEnhanceMode)));
186 PROPAGATE_ERROR(options.addOption(
187 createValueOption("edgeenhancestrength", 0, "POSITION",
188 "set the edge enhancement strength.",
189 Dispatcher::getInstance().m_edgeEnhanceStrength)));
190
191 // auto control settings
192 PROPAGATE_ERROR(options.addOption(
193 createValueOption("aeantibanding", 0, "MODE", "set the auto exposure antibanding mode.",
194 Dispatcher::getInstance().m_aeAntibandingMode)));
195 PROPAGATE_ERROR(options.addOption(
196 createValueOption("aelock", 0, "LOCK", "set the auto exposure lock.",
197 Dispatcher::getInstance().m_aeLock)));
198 PROPAGATE_ERROR(options.addOption(
199 createValueOption("awblock", 0, "LOCK", "set the auto white balance lock.",
200 Dispatcher::getInstance().m_awbLock)));
201 PROPAGATE_ERROR(options.addOption(
202 createValueOption("awb", 0, "MODE", "set the auto white balance mode.",
203 Dispatcher::getInstance().m_awbMode)));
204 PROPAGATE_ERROR(options.addOption(
205 createValueOption("exposurecompensation", 0, "COMPENSATION",
206 "set the exposure compensation to COMPENSATION.",
207 Dispatcher::getInstance().m_exposureCompensation)));
208 PROPAGATE_ERROR(options.addOption(
209 createValueOption("ispdigitalgainrange", 0, "RANGE",
210 "sets the ISP digital gain range.",
211 Dispatcher::getInstance().m_ispDigitalGainRange)));
212
213 if (Dispatcher::getInstance().supportsExtension(Argus::EXT_DE_FOG))
214 {
215 // DeFog extension settings
216 PROPAGATE_ERROR(options.addOption(
217 createValueOption("defog", 0, "ENABLE",
218 "set the DeFog enable flag to ENABLE.",
219 Dispatcher::getInstance().m_deFogEnable)));
220 PROPAGATE_ERROR(options.addOption(
221 createValueOption("defogamount", 0, "AMOUNT",
222 "sets the amount of fog to be removed to AMOUNT.",
223 Dispatcher::getInstance().m_deFogAmount)));
224 PROPAGATE_ERROR(options.addOption(
225 createValueOption("defogquality", 0, "QUALITY",
226 "sets the quality of the DeFog effect to QUALITY.",
227 Dispatcher::getInstance().m_deFogQuality)));
228 }
229
230 m_initialized = true;
231
232 return true;
233 }
234
235 bool AppModuleGeneric::shutdown()
236 {
237 if (!m_initialized)
238 return true;
239
240 PROPAGATE_ERROR_CONTINUE(stop());
241
242 delete m_guiConfig;
243 m_guiConfig = NULL;
244
245 m_initialized = false;
246
247 return true;
248 }
249
250 bool AppModuleGeneric::start(Window::IGuiMenuBar *iGuiMenuBar,
251 Window::IGuiContainer *iGuiContainerConfig)
252 {
253 if (m_running)
254 return true;
255
256 if (iGuiMenuBar && !m_guiMenuBar)
257 {
258 // initialize the menu
259
260 UniquePointer<Window::IGuiMenu> menu;
261 UniquePointer<Window::IGuiMenuItem> item;
262
263 // create the elements
264 Window::IGuiMenu *fileMenu = iGuiMenuBar->getMenu("File");
265 if (!fileMenu)
266 {
267 PROPAGATE_ERROR(Window::IGuiMenu::create("File", &menu));
268 PROPAGATE_ERROR(iGuiMenuBar->add(menu.get()));
269 fileMenu = menu.get();
270 menu.release();
271 }
272 PROPAGATE_ERROR(Window::IGuiMenuItem::create("Load config", AppModuleGeneric::loadConfig,
273 NULL, &item));
274 PROPAGATE_ERROR(fileMenu->add(item.get()));
275 item.release();
276 PROPAGATE_ERROR(Window::IGuiMenuItem::create("Save Config", AppModuleGeneric::saveConfig,
277 NULL, &item));
278 PROPAGATE_ERROR(fileMenu->add(item.get()));
279 item.release();
280 PROPAGATE_ERROR(Window::IGuiMenuItem::create("Quit", AppModuleGeneric::quit, NULL,
281 &item));
282 PROPAGATE_ERROR(fileMenu->add(item.get()));
283 item.release();
284
285 Window::IGuiMenu *helpMenu = iGuiMenuBar->getMenu("Help");
286 if (!helpMenu)
287 {
288 PROPAGATE_ERROR(Window::IGuiMenu::create("Help", &menu));
289 PROPAGATE_ERROR(iGuiMenuBar->add(menu.get()));
290 helpMenu = menu.get();
291 menu.release();
292 }
293 PROPAGATE_ERROR(Window::IGuiMenuItem::create("Info", AppModuleGeneric::info, NULL,
294 &item));
295 PROPAGATE_ERROR(helpMenu->add(item.get()));
296 item.release();
297
298 m_guiMenuBar = iGuiMenuBar;
299 }
300
301 if (iGuiContainerConfig && !m_guiContainerConfig)
302 {
303 // initialize the GUI
304
305 // create a grid container
306 PROPAGATE_ERROR(Window::IGuiContainerGrid::create(&m_guiConfig));
307
308 // create the elements
309 UniquePointer<Window::IGuiElement> element;
310 Dispatcher &dispatcher = Dispatcher::getInstance();
311
312 Window::IGuiContainerGrid::BuildHelper buildHelper(m_guiConfig);
313
314 #define CREATE_GUI_ELEMENT(_NAME, _VALUE) \
315 PROPAGATE_ERROR(Window::IGuiElement::createValue(&dispatcher._VALUE, &element));\
316 PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
317 element.release();
318
319 #define CREATE_GUI_ELEMENT_COMBO_BOX(_NAME, _VALUE, _FROMTYPE, _TOTYPE) \
320 assert(sizeof(_FROMTYPE) == sizeof(_TOTYPE)); \
321 PROPAGATE_ERROR(Window::IGuiElement::createValue(reinterpret_cast< \
322 Value<_TOTYPE>*>(&dispatcher._VALUE), &element)); \
323 PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
324 element.release();
325
326 #define CREATE_GUI_ELEMENT_PATH_CHOOSER(_NAME, _VALUE) \
327 PROPAGATE_ERROR(Window::IGuiElement::createFileChooser(&dispatcher._VALUE, \
328 true, &element)); \
329 PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
330 element.release();
331
332 CREATE_GUI_ELEMENT("Verbose", m_verbose);
333 CREATE_GUI_ELEMENT("KPI", m_kpi);
334 CREATE_GUI_ELEMENT("Device", m_deviceIndex);
335
336 CREATE_GUI_ELEMENT("Exposure time range (ns)", m_exposureTimeRange);
337 CREATE_GUI_ELEMENT("Gain range", m_gainRange);
338 CREATE_GUI_ELEMENT("ISP digital gain range", m_ispDigitalGainRange);
339
340 CREATE_GUI_ELEMENT_COMBO_BOX("Sensor mode index", m_sensorModeIndex,
341 uint32_t, Window::IGuiElement::ValueTypeEnum);
342 CREATE_GUI_ELEMENT_COMBO_BOX("YUV format", m_captureYuvFormat,
343 Argus::PixelFormat, Argus::NamedUUID);
344 CREATE_GUI_ELEMENT("Frame rate", m_frameRate);
345 CREATE_GUI_ELEMENT("Focus position", m_focusPosition);
346 CREATE_GUI_ELEMENT("Aperture motor step", m_apertureMotorStep);
347 CREATE_GUI_ELEMENT("Aperture motor speed", m_apertureMotorSpeed);
348 CREATE_GUI_ELEMENT("Output size", m_outputSize);
349
350 CREATE_GUI_ELEMENT_PATH_CHOOSER("Output path", m_outputPath);
351
352 CREATE_GUI_ELEMENT_COMBO_BOX("De-Noise mode", m_denoiseMode,
353 Argus::DenoiseMode, Argus::NamedUUID);
354 CREATE_GUI_ELEMENT("De-Noise strength", m_denoiseStrength);
355 CREATE_GUI_ELEMENT_COMBO_BOX("Edge Enhance mode", m_edgeEnhanceMode,
356 Argus::EdgeEnhanceMode, Argus::NamedUUID);
357 CREATE_GUI_ELEMENT("Edge Enhance strength", m_edgeEnhanceStrength);
358
359 CREATE_GUI_ELEMENT_COMBO_BOX("AE antibanding mode", m_aeAntibandingMode,
360 Argus::AeAntibandingMode, Argus::NamedUUID);
361
362 CREATE_GUI_ELEMENT("AE Lock", m_aeLock);
363 CREATE_GUI_ELEMENT("AWB Lock", m_awbLock);
364 CREATE_GUI_ELEMENT_COMBO_BOX("AWB mode", m_awbMode,
365 Argus::AwbMode, Argus::NamedUUID);
366 CREATE_GUI_ELEMENT("Exposure compensation (ev)", m_exposureCompensation);
367
368 #undef CREATE_GUI_ELEMENT
369 #undef CREATE_GUI_ELEMENT_COMBO_BOX
370 #undef CREATE_GUI_ELEMENT_PATH_CHOOSER
371
372 m_guiContainerConfig = iGuiContainerConfig;
373 }
374
375 if (m_guiContainerConfig)
376 PROPAGATE_ERROR(m_guiContainerConfig->add(m_guiConfig));
377
378 m_running = true;
379
380 return true;
381 }
382
383 bool AppModuleGeneric::stop()
384 {
385 if (!m_running)
386 return true;
387
388 if (m_guiContainerConfig)
389 PROPAGATE_ERROR(m_guiContainerConfig->remove(m_guiConfig));
390
391 m_running = true;
392
393 return true;
394 }
395
396 }; // namespace ArgusSamples