JeVois  1.18
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Utils.C
Go to the documentation of this file.
1 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 //
3 // JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2016 by Laurent Itti, the University of Southern
4 // California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5 //
6 // This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7 // redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8 // Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9 // without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10 // License for more details. You should have received a copy of the GNU General Public License along with this program;
11 // if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12 //
13 // Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14 // Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15 // ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16 /*! \file */
17 
18 #include <jevois/Util/Utils.H>
19 #include <jevois/Debug/Log.H>
20 
21 #include <linux/videodev2.h>
22 #include <string>
23 #include <vector>
24 #include <regex>
25 
26 #include <string.h> // for strncmp
27 #include <fstream>
28 #include <cstdarg> // for va_start, etc
29 #include <cstdio>
30 #include <iostream>
31 #include <memory>
32 #include <stdexcept>
33 #include <array>
34 #include <cctype> // for std::isspace()
35 #include <cmath> // for std::sqrt()
36 
37 #include <opencv2/core/hal/interface.h> // for CV_MAT_DEPTH_MASK
38 
39 // For ""s std::string literals:
40 using namespace std::literals;
41 using namespace std::string_literals;
42 using namespace std::literals::string_literals;
43 
44 // ####################################################################################################
45 std::string jevois::fccstr(unsigned int fcc)
46 {
47  if (fcc == 0) return "NONE"; // for no video over USB output
48 
49  std::string ret(" ");
50  ret[0] = static_cast<char>(fcc & 0xff);
51  ret[1] = static_cast<char>((fcc >> 8) & 0xff);
52  ret[2] = static_cast<char>((fcc >> 16) & 0xff);
53  ret[3] = static_cast<char>((fcc >> 24) & 0xff);
54  return ret;
55 }
56 
57 // ####################################################################################################
58 std::string jevois::cvtypestr(unsigned int cvtype)
59 {
60  // From: https://gist.github.com/rummanwaqar/cdaddd5a175f617c0b107d353fd33695
61  std::string r;
62 
63  uchar depth = cvtype & CV_MAT_DEPTH_MASK;
64  uchar chans = 1 + (cvtype >> CV_CN_SHIFT);
65 
66  switch (depth)
67  {
68  case CV_8U: r = "8U"; break;
69  case CV_8S: r = "8S"; break;
70  case CV_16U: r = "16U"; break;
71  case CV_16S: r = "16S"; break;
72  case CV_16F: r = "16F"; break;
73  case CV_32S: r = "32S"; break;
74  case CV_32F: r = "32F"; break;
75  case CV_64F: r = "64F"; break;
76  default: r = "User"; break;
77  }
78 
79  if (chans > 1)
80  {
81  r += 'C';
82  r += (chans + '0');
83  }
84 
85  return r;
86 }
87 
88 // ####################################################################################################
89 unsigned int jevois::cvBytesPerPix(unsigned int cvtype)
90 {
91  uchar depth = cvtype & CV_MAT_DEPTH_MASK;
92  uchar chans = 1 + (cvtype >> CV_CN_SHIFT);
93  int r = 0;
94  switch (depth)
95  {
96  case CV_8U: r = 1; break;
97  case CV_8S: r = 1; break;
98  case CV_16U: r = 2; break;
99  case CV_16S: r = 2; break;
100  case CV_16F: r = 2; break;
101  case CV_32S: r = 4; break;
102  case CV_32F: r = 4; break;
103  case CV_64F: r = 4; break;
104  default: LFATAL("Unsupported OpenCV type " << cvtype);
105  }
106 
107  return r * chans;
108 }
109 
110 // ####################################################################################################
111 unsigned int jevois::strfcc(std::string const & str)
112 {
113  if (str == "BAYER") return V4L2_PIX_FMT_SRGGB8;
114  else if (str == "YUYV") return V4L2_PIX_FMT_YUYV;
115  else if (str == "GREY" || str == "GRAY") return V4L2_PIX_FMT_GREY;
116  else if (str == "MJPG") return V4L2_PIX_FMT_MJPEG;
117  else if (str == "RGB565") return V4L2_PIX_FMT_RGB565;
118  else if (str == "BGR24") return V4L2_PIX_FMT_BGR24;
119  else if (str == "RGB3") return V4L2_PIX_FMT_RGB24;
120  else if (str == "BGR3") return V4L2_PIX_FMT_BGR24;
121 
122  // Additional modes supported by JeVois-Pro ISP. We do not necessarily support all but we can name them:
123  else if (str == "RGB24") return V4L2_PIX_FMT_RGB24;
124  else if (str == "RGB32") return V4L2_PIX_FMT_RGB32;
125  else if (str == "UYVY") return V4L2_PIX_FMT_UYVY;
126  else if (str == "BAYER16") return V4L2_PIX_FMT_SBGGR16;
127  else if (str == "NV12") return V4L2_PIX_FMT_NV12;
128  else if (str == "YUV444") return V4L2_PIX_FMT_YUV444;
129  else if (str == "META") return ISP_V4L2_PIX_FMT_META;
130 
131  else if (str == "JVUI") return JEVOISPRO_FMT_GUI;
132 
133  else if (str == "NONE") return 0;
134  else throw std::runtime_error("Invalid pixel format " + str);
135 }
136 
137 // ####################################################################################################
138 unsigned int jevois::v4l2BytesPerPix(unsigned int fcc)
139 {
140  switch (fcc)
141  {
142  case V4L2_PIX_FMT_YUYV: return 2U;
143  case V4L2_PIX_FMT_GREY: return 1U;
144  case V4L2_PIX_FMT_SRGGB8: return 1U;
145  case V4L2_PIX_FMT_RGB565: return 2U;
146  case V4L2_PIX_FMT_MJPEG: return 2U; // at most??
147  case V4L2_PIX_FMT_BGR24: return 3U;
148  case V4L2_PIX_FMT_RGB24: return 3U;
149  case V4L2_PIX_FMT_RGB32: return 4U;
150  case V4L2_PIX_FMT_UYVY: return 2U;
151  case V4L2_PIX_FMT_SBGGR16: return 2U;
152  case V4L2_PIX_FMT_NV12: return 2U; // actually, it has 4:2:0 sampling so 1.5 bytes/pix
153  case V4L2_PIX_FMT_YUV444: return 3U;
154  case ISP_V4L2_PIX_FMT_META: return 1U;
155  case JEVOISPRO_FMT_GUI: return 4U; // RGBA
156  case 0: return 0U; // for NONE output to USB mode
157  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
158  }
159 }
160 
161 // ####################################################################################################
162 unsigned int jevois::v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
163 { return width * height * jevois::v4l2BytesPerPix(fcc); }
164 
165 // ####################################################################################################
166 unsigned int jevois::blackColor(unsigned int fcc)
167 {
168  switch (fcc)
169  {
170  case V4L2_PIX_FMT_YUYV: return 0x8000;
171  case V4L2_PIX_FMT_GREY: return 0;
172  case V4L2_PIX_FMT_SRGGB8: return 0;
173  case V4L2_PIX_FMT_RGB565: return 0;
174  case V4L2_PIX_FMT_MJPEG: return 0;
175  case V4L2_PIX_FMT_BGR24: return 0;
176  case V4L2_PIX_FMT_RGB24: return 0;
177  case V4L2_PIX_FMT_RGB32: return 0;
178  case V4L2_PIX_FMT_UYVY: return 0x8000;
179  case V4L2_PIX_FMT_SBGGR16: return 0;
180  case V4L2_PIX_FMT_NV12: return 0;
181  case V4L2_PIX_FMT_YUV444: return 0x008080;
182  case JEVOISPRO_FMT_GUI: return 0;
183  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
184  }
185 }
186 
187 // ####################################################################################################
188 unsigned int jevois::whiteColor(unsigned int fcc)
189 {
190  switch (fcc)
191  {
192  case V4L2_PIX_FMT_YUYV: return 0x80ff;
193  case V4L2_PIX_FMT_GREY: return 0xff;
194  case V4L2_PIX_FMT_SRGGB8: return 0xff;
195  case V4L2_PIX_FMT_RGB565: return 0xffff;
196  case V4L2_PIX_FMT_MJPEG: return 0xff;
197  case V4L2_PIX_FMT_BGR24: return 0xffffff;
198  case V4L2_PIX_FMT_RGB24: return 0xffffff;
199  case V4L2_PIX_FMT_RGB32: return 0xffffffff;
200  case V4L2_PIX_FMT_UYVY: return 0xff80;
201  case V4L2_PIX_FMT_SBGGR16: return 0xffff;
202  case V4L2_PIX_FMT_NV12: return 0xffff; // probably wrong
203  case V4L2_PIX_FMT_YUV444: return 0xff8080;
204  case JEVOISPRO_FMT_GUI: return 0xffffffff;
205  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
206  }
207 }
208 
209 // ####################################################################################################
210 void jevois::applyLetterBox(unsigned int & imw, unsigned int & imh,
211  unsigned int const winw, unsigned int const winh, bool noalias)
212 {
213  if (imw == 0 || imh == 0 || winw == 0 || winh == 0) LFATAL("Cannot handle zero width or height");
214 
215  if (noalias)
216  {
217  // We want an integer scaling factor to avoid aliasing:
218  double const facw = double(winw) / imw, fach = double(winh) / imh;
219  double const minfac = std::min(facw, fach);
220  if (minfac >= 1.0)
221  {
222  // Image is smaller than window: scale image up by integer part of fac:
223  unsigned int const ifac = minfac;
224  imw *= ifac; imh *= ifac;
225  }
226  else
227  {
228  // Image is larger than window: scale image down by integer part of 1/fac:
229  double const maxfac = std::max(facw, fach);
230  unsigned int const ifac = 1.0 / maxfac;
231  imw /= ifac; imh /= ifac;
232  }
233  }
234  else
235  {
236  // Slightly different algo here to make sure we exactly hit the window width or height with no rounding errors:
237  double const ia = double(imw) / double (imh);
238  double const wa = double(winw) / double (winh);
239 
240  if (ia >= wa)
241  {
242  // Image aspect ratio is wider than window aspect ratio. Thus, resize image width to window width, then resize
243  // image height accordingly using the image aspect ratio:
244  imw = winw;
245  imh = (unsigned int)(imw / ia + 0.4999999);
246  }
247  else
248  {
249  // Image aspect ratio is taller than window aspect ratio. Thus, resize image height to window height, then resize
250  // image width accordingly using the image aspect ratio:
251  imh = winh;
252  imw = (unsigned int)(imh * ia + 0.4999999);
253  }
254  }
255 }
256 
257 // ####################################################################################################
258 std::vector<std::string> jevois::split(std::string const & input, std::string const & regex)
259 {
260  // This code is from: http://stackoverflow.com/questions/9435385/split-a-string-using-c11
261  // passing -1 as the submatch index parameter performs splitting
262  std::regex re(regex);
263  std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last;
264  return { first, last };
265 }
266 
267 // ####################################################################################################
268 std::string jevois::join(std::vector<std::string> const & strings, std::string const & delimiter)
269 {
270  if (strings.empty()) return "";
271  if (strings.size() == 1) return strings[0];
272 
273  std::string ret; size_t const szm1 = strings.size() - 1;
274 
275  for (size_t i = 0; i < szm1; ++i) ret += strings[i] + delimiter;
276  ret += strings[szm1];
277 
278  return ret;
279 }
280 
281 // ####################################################################################################
282 bool jevois::stringStartsWith(std::string const & str, std::string const & prefix)
283 {
284  return (strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0);
285 }
286 
287 // ####################################################################################################
288 std::string jevois::replaceWhitespace(std::string const & str, char rep)
289 {
290  std::string ret = str;
291  for (char & c : ret) if (std::isspace(c)) c = rep;
292  return ret;
293 }
294 
295 // ####################################################################################################
296 std::string jevois::strip(std::string const & str)
297 {
298  int idx = str.length() - 1;
299  while (idx >= 0 && std::isspace(str[idx])) --idx;
300  return str.substr(0, idx + 1);
301 }
302 
303 // ####################################################################################################
304 std::string jevois::extractString(std::string const & str, std::string const & startsep, std::string const & endsep)
305 {
306  size_t idx = str.find(startsep);
307  if (idx == std::string::npos) return std::string();
308  idx += startsep.size();
309 
310  if (endsep.empty()) return str.substr(idx);
311 
312  size_t idx2 = str.find(endsep, idx);
313  if (idx2 == std::string::npos) return std::string();
314  return str.substr(idx, idx2 - idx);
315 }
316 
317 // ####################################################################################################
318 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
319 size_t jevois::replaceStringFirst(std::string & str, std::string const & from, std::string const & to)
320 {
321  if (from.empty()) return 0;
322 
323  size_t start_pos = str.find(from);
324  if (start_pos == std::string::npos) return 0;
325 
326  str.replace(start_pos, from.length(), to);
327  return 1;
328 }
329 
330 // ####################################################################################################
331 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
332 size_t jevois::replaceStringAll(std::string & str, std::string const & from, std::string const & to)
333 {
334  if (from.empty()) return 0;
335 
336  size_t start_pos = 0, n = 0;
337  while((start_pos = str.find(from, start_pos)) != std::string::npos)
338  {
339  str.replace(start_pos, from.length(), to);
340  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
341  ++n;
342  }
343 
344  return n;
345 }
346 
347 // ####################################################################################################
348 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
349 std::string jevois::replaceAll(std::string const & str, std::string const & from, std::string const & to)
350 {
351  if (from.empty()) return str;
352  std::string ret = str;
353 
354  size_t start_pos = 0;
355  while((start_pos = ret.find(from, start_pos)) != std::string::npos)
356  {
357  ret.replace(start_pos, from.length(), to);
358  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
359  }
360 
361  return ret;
362 }
363 
364 // ####################################################################################################
365 std::filesystem::path jevois::absolutePath(std::filesystem::path const & root, std::filesystem::path const & path)
366 {
367  // If path is empty, return root (be it empty of not):
368  if (path.empty()) return root;
369 
370  // no-op if the given path is already absolute:
371  if (path.is_absolute()) return path;
372 
373  // no-op if root is empty:
374  if (root.empty()) return path;
375 
376  // We know root is not empty and path does not start with a / and is not empty; concatenate both:
377  return root / path;
378 }
379 
380 // ####################################################################################################
381 namespace
382 {
383  // This code is from NRT, and before that from the iLab C++ neuromorphic vision toolkit
384  std::string vsformat(char const * fmt, va_list ap)
385  {
386  // if we have a null pointer or an empty string, then just return an empty std::string
387  if (fmt == nullptr || fmt[0] == '\0') return std::string();
388 
389  int bufsize = 1024;
390  while (true)
391  {
392  char buf[bufsize];
393 
394  int const nchars = vsnprintf(buf, bufsize, fmt, ap);
395 
396  if (nchars < 0)
397  {
398  // Better leave this as LFATAL() rather than LERROR(), otherwise we have to return a bogus std::string (e.g. an
399  // empty string, or "none", or...), which might be dangerous if it is later used as a filename, for example.
400  LFATAL("vsnprintf failed for format '" << fmt << "' with bufsize = " << bufsize);
401  }
402  else if (nchars >= bufsize)
403  {
404  // buffer was too small, so let's double the bufsize and try again:
405  bufsize *= 2;
406  continue;
407  }
408  else
409  {
410  // OK, the vsnprintf() succeeded:
411  return std::string(&buf[0], nchars);
412  }
413  }
414  return std::string(); // can't happen, but placate the compiler
415  }
416 }
417 
418 // ####################################################################################################
419 std::string jevois::sformat(char const * fmt, ...)
420 {
421  va_list a;
422  va_start(a, fmt);
423  std::string result = vsformat(fmt, a);
424  va_end(a);
425  return result;
426 }
427 
428 // ####################################################################################################
430 {
431 #ifdef JEVOIS_PLATFORM_A33
432  std::ofstream ofs("/proc/sys/vm/drop_caches");
433  if (ofs.is_open()) ofs << "3" << std::endl;
434  else LERROR("Failed to flush cache -- ignored");
435 #endif
436 }
437 
438 // ####################################################################################################
439 // This code modified from here: https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-
440 // output-of-command-within-c-using-posix
441 std::string jevois::system(std::string const & cmd, bool errtoo)
442 {
443  std::array<char, 128> buffer; std::string result;
444  std::shared_ptr<FILE> pip;
445  if (errtoo) pip.reset(popen((cmd + " 2>&1").c_str(), "r"), pclose);
446  else pip.reset(popen(cmd.c_str(), "r"), pclose);
447  if (!pip) LFATAL("popen() failed for command [" << cmd << ']');
448  while (!feof(pip.get())) if (fgets(buffer.data(), 128, pip.get()) != NULL) result += buffer.data();
449  return result;
450 }
451 
452 // ####################################################################################################
453 std::string jevois::secs2str(double secs)
454 {
455  if (secs < 1.0e-6) return jevois::sformat("%.2fns", secs * 1.0e9);
456  else if (secs < 1.0e-3) return jevois::sformat("%.2fus", secs * 1.0e6);
457  else if (secs < 1.0) return jevois::sformat("%.2fms", secs * 1.0e3);
458  else return jevois::sformat("%.2fs", secs);
459 }
460 
461 // ####################################################################################################
462 std::string jevois::secs2str(std::vector<double> secs)
463 {
464  if (secs.empty()) return "0.0 +/- 0.0s";
465  double sum = 0.0, sumsq = 0.0;
466  for (double s : secs) { sum += s; sumsq += s*s; }
467  double const avg = sum / secs.size();
468  double const std = std::sqrt(sumsq/secs.size() - avg*avg); // E[(X-E[X])^2] = E[X^2]-E[X]^2
469 
470  if (avg < 1.0e-6) return jevois::sformat("%.1f +/- %.1f ns", avg * 1.0e9, std * 1.0e9);
471  else if (avg < 1.0e-3) return jevois::sformat("%.1f +/- %.1f us", avg * 1.0e6, std * 1.0e6);
472  else if (avg < 1.0) return jevois::sformat("%.1f +/- %.1f ms", avg * 1.0e3, std * 1.0e3);
473  else return jevois::sformat("%.1f +/- %.1f s", avg, std);
474 }
475 
476 // ####################################################################################################
477 void jevois::secs2str(std::ostringstream & ss, double secs)
478 {
479  if (secs < 1.0e-6) ss << secs * 1.0e9 << "ns";
480  else if (secs < 1.0e-3) ss << secs * 1.0e6 << "us";
481  else if (secs < 1.0) ss << secs * 1.0e3 << "ms";
482  else ss << secs << 's';
483 }
484 
485 // ####################################################################################################
486 std::string jevois::num2str(double n)
487 {
488  if (n < 1.0e3) return jevois::sformat("%.2f", n);
489  else if (n < 1.0e6) return jevois::sformat("%.2fK", n / 1.0e3);
490  else if (n < 1.0e9) return jevois::sformat("%.2fM", n / 1.0e6);
491  else if (n < 1.0e12) return jevois::sformat("%.2fG", n / 1.0e9);
492  else if (n < 1.0e15) return jevois::sformat("%.2fT", n / 1.0e12);
493  else if (n < 1.0e18) return jevois::sformat("%.2fP", n / 1.0e15);
494  else if (n < 1.0e21) return jevois::sformat("%.2fE", n / 1.0e18);
495  else if (n < 1.0e24) return jevois::sformat("%.2fZ", n / 1.0e21);
496  else if (n < 1.0e27) return jevois::sformat("%.2fY", n / 1.0e24);
497  else return jevois::sformat("%.2f", n);
498 }
499 
500 // ####################################################################################################
501 void jevois::num2str(std::ostringstream & ss, double n)
502 {
503  if (n < 1.0e3) ss << n;
504  else if (n < 1.0e6) ss << n / 1.0e3 << 'K';
505  else if (n < 1.0e9) ss << n / 1.0e6 << 'M';
506  else if (n < 1.0e12) ss << n / 1.0e9 << 'G';
507  else if (n < 1.0e15) ss << n / 1.0e12 << 'T';
508  else if (n < 1.0e18) ss << n / 1.0e15 << 'P';
509  else if (n < 1.0e21) ss << n / 1.0e18 << 'E';
510  else if (n < 1.0e24) ss << n / 1.0e21 << 'Z';
511  else if (n < 1.0e27) ss << n / 1.0e24 << 'Y';
512  else ss << n;
513 }
514 
515 // ####################################################################################################
516 std::string jevois::getFileString(char const * fname, int skip)
517 {
518  std::ifstream ifs(fname);
519  if (ifs.is_open() == false) throw std::runtime_error("Cannot read file: "s + fname);
520  std::string str;
521  while (skip-- >= 0) std::getline(ifs, str);
522  ifs.close();
523 
524  return str;
525 }
JEVOISPRO_FMT_GUI
#define JEVOISPRO_FMT_GUI
JeVois-Pro zero-copy display of camera input frame (to be used as output mode in VideoMapping)
Definition: Utils.H:31
jevois::whiteColor
unsigned int whiteColor(unsigned int fcc)
Return a value that corresponds to white for the given video format.
Definition: Utils.C:188
jevois::sformat
std::string sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition: Utils.C:419
jevois::split
std::vector< std::string > split(std::string const &input, std::string const &regex="\\s+")
Split string into vector of tokens using a regex to specify what to split on; default regex splits by...
Definition: Utils.C:258
jevois::cvBytesPerPix
unsigned int cvBytesPerPix(unsigned int cvtype)
Return the number of bytes per pixel for a given OpenCV pixel type.
Definition: Utils.C:89
jevois::flushcache
void flushcache()
Flush the caches, may sometimes be useful when running the camera in turbo mode.
Definition: Utils.C:429
jevois::cvtypestr
std::string cvtypestr(unsigned int cvtype)
Convert cv::Mat::type() code to to a string (e.g., CV_8UC1, CV_32SC3, etc)
Definition: Utils.C:58
jevois::replaceStringFirst
size_t replaceStringFirst(std::string &str, std::string const &from, std::string const &to)
Replace first instance of 'from' with 'to'.
Definition: Utils.C:319
ISP_V4L2_PIX_FMT_META
#define ISP_V4L2_PIX_FMT_META
Metadata V4L2 format used by Amlogic A311D camera ISP.
Definition: Utils.H:27
jevois::blackColor
unsigned int blackColor(unsigned int fcc)
Return a value that corresponds to black for the given video format.
Definition: Utils.C:166
LERROR
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level.
Definition: Log.H:211
jevois::getFileString
std::string getFileString(char const *fname, int skip=0)
Read one line from a file and return it as a string.
Definition: Utils.C:516
jevois::replaceWhitespace
std::string replaceWhitespace(std::string const &str, char rep='_')
Replace white space characters in a string with underscore (default) or another character.
Definition: Utils.C:288
jevois::replaceAll
std::string replaceAll(std::string const &str, std::string const &from, std::string const &to)
Replace all instances of 'from' with 'to'.
Definition: Utils.C:349
jevois::absolutePath
std::filesystem::path absolutePath(std::filesystem::path const &root, std::filesystem::path const &path)
Compute an absolute path from two paths.
Definition: Utils.C:365
Log.H
jevois::system
std::string system(std::string const &cmd, bool errtoo=true)
Execute a command and grab stdout output to a string.
Definition: Utils.C:441
jevois::v4l2BytesPerPix
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:138
jevois::replaceStringAll
size_t replaceStringAll(std::string &str, std::string const &from, std::string const &to)
Replace all instances of 'from' with 'to'.
Definition: Utils.C:332
jevois::join
std::string join(std::vector< std::string > const &strings, std::string const &delimiter)
Concatenate a vector of tokens into a string.
Definition: Utils.C:268
jevois::extractString
std::string extractString(std::string const &str, std::string const &startsep, std::string const &endsep)
Extract a portion of a string between two delimiters.
Definition: Utils.C:304
jevois::fccstr
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:45
jevois::applyLetterBox
void applyLetterBox(unsigned int &imw, unsigned int &imh, unsigned int const winw, unsigned int const winh, bool noalias)
Apply a letterbox resizing to fit an image into a window.
Definition: Utils.C:210
LFATAL
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level.
jevois::stringStartsWith
bool stringStartsWith(std::string const &str, std::string const &prefix)
Return true if str starts with prefix (including if both strings are equal)
Definition: Utils.C:282
jevois::secs2str
std::string secs2str(double secs)
Report a duration given in seconds with variable units (ns, us, ms, or s), with precision of 2 decima...
Definition: Utils.C:453
jevois::num2str
std::string num2str(double n)
Report a number with variable multipliers (K, M, G, T, P, E, Z, Y), with precision of 2 decimal point...
Definition: Utils.C:486
Utils.H
jevois::v4l2ImageSize
unsigned int v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
Return the image size in bytes for a given V4L2_PIX_FMT_..., width, height.
Definition: Utils.C:162
jevois::strfcc
unsigned int strfcc(std::string const &str)
Convert a JeVois video format string to V4L2 four-cc code (V4L2_PIX_FMT_...)
Definition: Utils.C:111
jevois::strip
std::string strip(std::string const &str)
Strip white space (including CR, LF, tabs, etc) from the end of a string.
Definition: Utils.C:296