Argus Camera Sample
Argus Camera Sample
 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 
87  : m_initialized(false)
88  , m_running(false)
89  , m_guiMenuBar(NULL)
90  , m_guiContainerConfig(NULL)
91  , m_guiConfig(NULL)
92 {
93 }
94 
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. ",
117  PROPAGATE_ERROR(options.addOption(
118  Options::Option("saveconfig", 0, "FILE",
119  Options::Option::TYPE_ACTION, "save configuration to XML 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("apertureposition", 0, "POSITION",
153  "sets the aperture position to POSITION, in position units.",
154  Dispatcher::getInstance().m_aperturePosition)));
155  PROPAGATE_ERROR(options.addOption(
156  createValueOption("apertureFnum", 0, "Fnum",
157  "sets the aperture F-num, in F-num units.",
158  Dispatcher::getInstance().m_apertureFnum)));
159  PROPAGATE_ERROR(options.addOption(
160  createValueOption("aperturemotorspeed", 0, "SPEED",
161  "sets the aperture motor speed to SPEED, in steps/second units.",
162  Dispatcher::getInstance().m_apertureMotorSpeed)));
163  PROPAGATE_ERROR(options.addOption(
164  createValueOption("captureyuvformat", 0, "FORMAT",
165  "YUV format for image capture.", Dispatcher::getInstance().m_captureYuvFormat)));
166 
167  // output settings
168  PROPAGATE_ERROR(options.addOption(
169  createValueOption("outputsize", 0, "WIDTHxHEIGHT",
170  "set the still and video output size to WIDTHxHEIGHT (e.g. 1920x1080). If WIDTHxHEIGHT "
171  "is '0x0' the output size is the sensor mode size.",
172  Dispatcher::getInstance().m_outputSize)));
173  PROPAGATE_ERROR(options.addOption(
174  createValueOption("outputpath", 0, "PATH",
175  "set the output file path. A file name, an incrementing index and the file extension"
176  " will be appended. E.g. setting 'folder/' will result in 'folder/image0.jpg' or "
177  "'folder/video0.mp4'. '/dev/null' can be used to discard output.",
178  Dispatcher::getInstance().m_outputPath)));
179 
180  // stream settings
181  PROPAGATE_ERROR(options.addOption(
182  createValueOption("denoise", 0, "MODE", "set the denoising mode.",
183  Dispatcher::getInstance().m_denoiseMode)));
184  PROPAGATE_ERROR(options.addOption(
185  createValueOption("denoisestrength", 0, "POSITION", "set the denoising strength.",
186  Dispatcher::getInstance().m_denoiseStrength)));
187  PROPAGATE_ERROR(options.addOption(
188  createValueOption("edgeenhance", 0, "MODE", "set the edge enhancement mode.",
189  Dispatcher::getInstance().m_edgeEnhanceMode)));
190  PROPAGATE_ERROR(options.addOption(
191  createValueOption("edgeenhancestrength", 0, "POSITION",
192  "set the edge enhancement strength.",
193  Dispatcher::getInstance().m_edgeEnhanceStrength)));
194 
195  // auto control settings
196  PROPAGATE_ERROR(options.addOption(
197  createValueOption("aeantibanding", 0, "MODE", "set the auto exposure antibanding mode.",
198  Dispatcher::getInstance().m_aeAntibandingMode)));
199  PROPAGATE_ERROR(options.addOption(
200  createValueOption("aelock", 0, "LOCK", "set the auto exposure lock.",
201  Dispatcher::getInstance().m_aeLock)));
202  PROPAGATE_ERROR(options.addOption(
203  createValueOption("awblock", 0, "LOCK", "set the auto white balance lock.",
204  Dispatcher::getInstance().m_awbLock)));
205  PROPAGATE_ERROR(options.addOption(
206  createValueOption("awb", 0, "MODE", "set the auto white balance mode.",
207  Dispatcher::getInstance().m_awbMode)));
208  PROPAGATE_ERROR(options.addOption(
209  createValueOption("exposurecompensation", 0, "COMPENSATION",
210  "set the exposure compensation to COMPENSATION.",
211  Dispatcher::getInstance().m_exposureCompensation)));
212  PROPAGATE_ERROR(options.addOption(
213  createValueOption("ispdigitalgainrange", 0, "RANGE",
214  "sets the ISP digital gain range.",
215  Dispatcher::getInstance().m_ispDigitalGainRange)));
216  PROPAGATE_ERROR(options.addOption(
217  createValueOption("acregionhorizontal", 0, "RANGE",
218  "sets the AC auto control region horizontal range.",
219  Dispatcher::getInstance().m_acRegionHorizontal)));
220  PROPAGATE_ERROR(options.addOption(
221  createValueOption("acregionvertical", 0, "RANGE",
222  "sets the AC auto control region vertical range.",
223  Dispatcher::getInstance().m_acRegionVertical)));
224 
225  if (Dispatcher::getInstance().supportsExtension(Argus::EXT_DE_FOG))
226  {
227  // DeFog extension settings
228  PROPAGATE_ERROR(options.addOption(
229  createValueOption("defog", 0, "ENABLE",
230  "set the DeFog enable flag to ENABLE.",
231  Dispatcher::getInstance().m_deFogEnable)));
232  PROPAGATE_ERROR(options.addOption(
233  createValueOption("defogamount", 0, "AMOUNT",
234  "sets the amount of fog to be removed to AMOUNT.",
235  Dispatcher::getInstance().m_deFogAmount)));
236  PROPAGATE_ERROR(options.addOption(
237  createValueOption("defogquality", 0, "QUALITY",
238  "sets the quality of the DeFog effect to QUALITY.",
239  Dispatcher::getInstance().m_deFogQuality)));
240  }
241 
242  m_initialized = true;
243 
244  return true;
245 }
246 
248 {
249  if (!m_initialized)
250  return true;
251 
252  PROPAGATE_ERROR_CONTINUE(stop());
253 
254  delete m_guiConfig;
255  m_guiConfig = NULL;
256 
257  m_initialized = false;
258 
259  return true;
260 }
261 
262 bool AppModuleGeneric::start(Window::IGuiMenuBar *iGuiMenuBar,
263  Window::IGuiContainer *iGuiContainerConfig)
264 {
265  if (m_running)
266  return true;
267 
268  if (iGuiMenuBar && !m_guiMenuBar)
269  {
270  // initialize the menu
271 
272  UniquePointer<Window::IGuiMenu> menu;
273  UniquePointer<Window::IGuiMenuItem> item;
274 
275  // create the elements
276  Window::IGuiMenu *fileMenu = iGuiMenuBar->getMenu("File");
277  if (!fileMenu)
278  {
279  PROPAGATE_ERROR(Window::IGuiMenu::create("File", &menu));
280  PROPAGATE_ERROR(iGuiMenuBar->add(menu.get()));
281  fileMenu = menu.get();
282  menu.release();
283  }
284  PROPAGATE_ERROR(Window::IGuiMenuItem::create("Load config", AppModuleGeneric::loadConfig,
285  NULL, &item));
286  PROPAGATE_ERROR(fileMenu->add(item.get()));
287  item.release();
288  PROPAGATE_ERROR(Window::IGuiMenuItem::create("Save Config", AppModuleGeneric::saveConfig,
289  NULL, &item));
290  PROPAGATE_ERROR(fileMenu->add(item.get()));
291  item.release();
292  PROPAGATE_ERROR(Window::IGuiMenuItem::create("Quit", AppModuleGeneric::quit, NULL,
293  &item));
294  PROPAGATE_ERROR(fileMenu->add(item.get()));
295  item.release();
296 
297  Window::IGuiMenu *helpMenu = iGuiMenuBar->getMenu("Help");
298  if (!helpMenu)
299  {
300  PROPAGATE_ERROR(Window::IGuiMenu::create("Help", &menu));
301  PROPAGATE_ERROR(iGuiMenuBar->add(menu.get()));
302  helpMenu = menu.get();
303  menu.release();
304  }
305  PROPAGATE_ERROR(Window::IGuiMenuItem::create("Info", AppModuleGeneric::info, NULL,
306  &item));
307  PROPAGATE_ERROR(helpMenu->add(item.get()));
308  item.release();
309 
310  m_guiMenuBar = iGuiMenuBar;
311  }
312 
313  if (iGuiContainerConfig && !m_guiContainerConfig)
314  {
315  // initialize the GUI
316 
317  // create a grid container
318  PROPAGATE_ERROR(Window::IGuiContainerGrid::create(&m_guiConfig));
319 
320  // create the elements
321  UniquePointer<Window::IGuiElement> element;
322  Dispatcher &dispatcher = Dispatcher::getInstance();
323 
324  Window::IGuiContainerGrid::BuildHelper buildHelper(m_guiConfig);
325 
326 #define CREATE_GUI_ELEMENT(_NAME, _VALUE) \
327  PROPAGATE_ERROR(Window::IGuiElement::createValue(&dispatcher._VALUE, &element));\
328  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
329  element.release();
330 
331 #define CREATE_GUI_ELEMENT_COMBO_BOX(_NAME, _VALUE, _FROMTYPE, _TOTYPE) \
332  assert(sizeof(_FROMTYPE) == sizeof(_TOTYPE)); \
333  PROPAGATE_ERROR(Window::IGuiElement::createValue(reinterpret_cast< \
334  Value<_TOTYPE>*>(&dispatcher._VALUE), &element)); \
335  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
336  element.release();
337 
338 #define CREATE_GUI_ELEMENT_PATH_CHOOSER(_NAME, _VALUE) \
339  PROPAGATE_ERROR(Window::IGuiElement::createFileChooser(&dispatcher._VALUE, \
340  true, &element)); \
341  PROPAGATE_ERROR(buildHelper.append(_NAME, element.get())); \
342  element.release();
343 
344  CREATE_GUI_ELEMENT("Verbose", m_verbose);
345  CREATE_GUI_ELEMENT("KPI", m_kpi);
346  CREATE_GUI_ELEMENT("Device", m_deviceIndex);
347 
348  CREATE_GUI_ELEMENT("Exposure time range (ns)", m_exposureTimeRange);
349  CREATE_GUI_ELEMENT("Gain range", m_gainRange);
350  CREATE_GUI_ELEMENT("ISP digital gain range", m_ispDigitalGainRange);
351  CREATE_GUI_ELEMENT("AC region horizontal", m_acRegionHorizontal);
352  CREATE_GUI_ELEMENT("AC region vertical", m_acRegionVertical);
353 
354  CREATE_GUI_ELEMENT_COMBO_BOX("Sensor mode index", m_sensorModeIndex,
355  uint32_t, Window::IGuiElement::ValueTypeEnum);
356  CREATE_GUI_ELEMENT_COMBO_BOX("YUV format", m_captureYuvFormat,
357  Argus::PixelFormat, Argus::NamedUUID);
358  CREATE_GUI_ELEMENT("Frame rate", m_frameRate);
359  CREATE_GUI_ELEMENT("Focus position", m_focusPosition);
360  CREATE_GUI_ELEMENT("Aperture position", m_aperturePosition);
361  CREATE_GUI_ELEMENT_COMBO_BOX("Aperture Fnum", m_apertureFnum,
362  float, Window::IGuiElement::ValueTypeEnum);
363  CREATE_GUI_ELEMENT("Aperture motor speed", m_apertureMotorSpeed);
364  CREATE_GUI_ELEMENT("Output size", m_outputSize);
365 
366  CREATE_GUI_ELEMENT_PATH_CHOOSER("Output path", m_outputPath);
367 
368  CREATE_GUI_ELEMENT_COMBO_BOX("De-Noise mode", m_denoiseMode,
369  Argus::DenoiseMode, Argus::NamedUUID);
370  CREATE_GUI_ELEMENT("De-Noise strength", m_denoiseStrength);
371  CREATE_GUI_ELEMENT_COMBO_BOX("Edge Enhance mode", m_edgeEnhanceMode,
372  Argus::EdgeEnhanceMode, Argus::NamedUUID);
373  CREATE_GUI_ELEMENT("Edge Enhance strength", m_edgeEnhanceStrength);
374 
375  CREATE_GUI_ELEMENT_COMBO_BOX("AE antibanding mode", m_aeAntibandingMode,
376  Argus::AeAntibandingMode, Argus::NamedUUID);
377 
378  CREATE_GUI_ELEMENT("AE Lock", m_aeLock);
379  CREATE_GUI_ELEMENT("AWB Lock", m_awbLock);
380  CREATE_GUI_ELEMENT_COMBO_BOX("AWB mode", m_awbMode,
381  Argus::AwbMode, Argus::NamedUUID);
382  CREATE_GUI_ELEMENT("Exposure compensation (ev)", m_exposureCompensation);
383 
384 #undef CREATE_GUI_ELEMENT
385 #undef CREATE_GUI_ELEMENT_COMBO_BOX
386 #undef CREATE_GUI_ELEMENT_PATH_CHOOSER
387 
388  m_guiContainerConfig = iGuiContainerConfig;
389  }
390 
392  PROPAGATE_ERROR(m_guiContainerConfig->add(m_guiConfig));
393 
394  m_running = true;
395 
396  return true;
397 }
398 
400 {
401  if (!m_running)
402  return true;
403 
405  PROPAGATE_ERROR(m_guiContainerConfig->remove(m_guiConfig));
406 
407  m_running = true;
408 
409  return true;
410 }
411 
412 }; // namespace ArgusSamples