JeVois  1.20
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  if (chans < 10) r += (chans + '0'); else r += std::to_string(chans);
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 #ifdef JEVOIS_PRO
127  else if (str == "BGGR16") return V4L2_PIX_FMT_SBGGR16;
128  else if (str == "GRBG16") return V4L2_PIX_FMT_SGRBG16;
129 #endif
130  else if (str == "NV12") return V4L2_PIX_FMT_NV12;
131  else if (str == "YUV444") return V4L2_PIX_FMT_YUV444;
132  else if (str == "META") return ISP_V4L2_PIX_FMT_META;
133 
134  else if (str == "JVUI") return JEVOISPRO_FMT_GUI;
135 
136  else if (str == "NONE") return 0;
137  else throw std::runtime_error("Invalid pixel format " + str);
138 }
139 
140 // ####################################################################################################
141 unsigned int jevois::v4l2BytesPerPix(unsigned int fcc)
142 {
143  switch (fcc)
144  {
145  case V4L2_PIX_FMT_YUYV: return 2U;
146  case V4L2_PIX_FMT_GREY: return 1U;
147  case V4L2_PIX_FMT_SRGGB8: return 1U;
148  case V4L2_PIX_FMT_RGB565: return 2U;
149  case V4L2_PIX_FMT_MJPEG: return 2U; // at most??
150  case V4L2_PIX_FMT_BGR24: return 3U;
151  case V4L2_PIX_FMT_RGB24: return 3U;
152  case V4L2_PIX_FMT_RGB32: return 4U;
153  case V4L2_PIX_FMT_UYVY: return 2U;
154 #ifdef JEVOIS_PRO
155  case V4L2_PIX_FMT_SBGGR16: return 2U;
156  case V4L2_PIX_FMT_SGRBG16: return 2U;
157 #endif
158  case V4L2_PIX_FMT_NV12: return 2U; // actually, it has 4:2:0 sampling so 1.5 bytes/pix
159  case V4L2_PIX_FMT_YUV444: return 3U;
160  case ISP_V4L2_PIX_FMT_META: return 1U;
161  case JEVOISPRO_FMT_GUI: return 4U; // RGBA
162  case 0: return 0U; // for NONE output to USB mode
163  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
164  }
165 }
166 
167 // ####################################################################################################
168 unsigned int jevois::v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
169 { return width * height * jevois::v4l2BytesPerPix(fcc); }
170 
171 // ####################################################################################################
172 unsigned int jevois::blackColor(unsigned int fcc)
173 {
174  switch (fcc)
175  {
176  case V4L2_PIX_FMT_YUYV: return 0x8000;
177  case V4L2_PIX_FMT_GREY: return 0;
178  case V4L2_PIX_FMT_SRGGB8: return 0;
179  case V4L2_PIX_FMT_RGB565: return 0;
180  case V4L2_PIX_FMT_MJPEG: return 0;
181  case V4L2_PIX_FMT_BGR24: return 0;
182  case V4L2_PIX_FMT_RGB24: return 0;
183  case V4L2_PIX_FMT_RGB32: return 0;
184  case V4L2_PIX_FMT_UYVY: return 0x8000;
185 #ifdef JEVOIS_PRO
186  case V4L2_PIX_FMT_SBGGR16: return 0;
187  case V4L2_PIX_FMT_SGRBG16: return 0;
188 #endif
189  case V4L2_PIX_FMT_NV12: return 0;
190  case V4L2_PIX_FMT_YUV444: return 0x008080;
191  case JEVOISPRO_FMT_GUI: return 0;
192  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
193  }
194 }
195 
196 // ####################################################################################################
197 unsigned int jevois::whiteColor(unsigned int fcc)
198 {
199  switch (fcc)
200  {
201  case V4L2_PIX_FMT_YUYV: return 0x80ff;
202  case V4L2_PIX_FMT_GREY: return 0xff;
203  case V4L2_PIX_FMT_SRGGB8: return 0xff;
204  case V4L2_PIX_FMT_RGB565: return 0xffff;
205  case V4L2_PIX_FMT_MJPEG: return 0xff;
206  case V4L2_PIX_FMT_BGR24: return 0xffffff;
207  case V4L2_PIX_FMT_RGB24: return 0xffffff;
208  case V4L2_PIX_FMT_RGB32: return 0xffffffff;
209  case V4L2_PIX_FMT_UYVY: return 0xff80;
210 #ifdef JEVOIS_PRO
211  case V4L2_PIX_FMT_SBGGR16: return 0xffff;
212  case V4L2_PIX_FMT_SGRBG16: return 0xffff;
213 #endif
214  case V4L2_PIX_FMT_NV12: return 0xffff; // probably wrong
215  case V4L2_PIX_FMT_YUV444: return 0xff8080;
216  case JEVOISPRO_FMT_GUI: return 0xffffffff;
217  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
218  }
219 }
220 
221 // ####################################################################################################
222 void jevois::applyLetterBox(unsigned int & imw, unsigned int & imh,
223  unsigned int const winw, unsigned int const winh, bool noalias)
224 {
225  if (imw == 0 || imh == 0 || winw == 0 || winh == 0) LFATAL("Cannot handle zero width or height");
226 
227  if (noalias)
228  {
229  // We want an integer scaling factor to avoid aliasing:
230  double const facw = double(winw) / imw, fach = double(winh) / imh;
231  double const minfac = std::min(facw, fach);
232  if (minfac >= 1.0)
233  {
234  // Image is smaller than window: scale image up by integer part of fac:
235  unsigned int const ifac = minfac;
236  imw *= ifac; imh *= ifac;
237  }
238  else
239  {
240  // Image is larger than window: scale image down by integer part of 1/fac:
241  double const maxfac = std::max(facw, fach);
242  unsigned int const ifac = 1.0 / maxfac;
243  imw /= ifac; imh /= ifac;
244  }
245  }
246  else
247  {
248  // Slightly different algo here to make sure we exactly hit the window width or height with no rounding errors:
249  double const ia = double(imw) / double (imh);
250  double const wa = double(winw) / double (winh);
251 
252  if (ia >= wa)
253  {
254  // Image aspect ratio is wider than window aspect ratio. Thus, resize image width to window width, then resize
255  // image height accordingly using the image aspect ratio:
256  imw = winw;
257  imh = (unsigned int)(imw / ia + 0.4999999);
258  }
259  else
260  {
261  // Image aspect ratio is taller than window aspect ratio. Thus, resize image height to window height, then resize
262  // image width accordingly using the image aspect ratio:
263  imh = winh;
264  imw = (unsigned int)(imh * ia + 0.4999999);
265  }
266  }
267 }
268 
269 // ####################################################################################################
270 std::vector<std::string> jevois::split(std::string const & input, std::string const & regex)
271 {
272  // This code is from: http://stackoverflow.com/questions/9435385/split-a-string-using-c11
273  // passing -1 as the submatch index parameter performs splitting
274  std::regex re(regex);
275  std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last;
276  return { first, last };
277 }
278 
279 // ####################################################################################################
280 std::string jevois::join(std::vector<std::string> const & strings, std::string const & delimiter)
281 {
282  if (strings.empty()) return "";
283  if (strings.size() == 1) return strings[0];
284 
285  std::string ret; size_t const szm1 = strings.size() - 1;
286 
287  for (size_t i = 0; i < szm1; ++i) ret += strings[i] + delimiter;
288  ret += strings[szm1];
289 
290  return ret;
291 }
292 
293 // ####################################################################################################
294 bool jevois::stringStartsWith(std::string const & str, std::string const & prefix)
295 {
296  return (strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0);
297 }
298 
299 // ####################################################################################################
300 std::string jevois::replaceWhitespace(std::string const & str, char rep)
301 {
302  std::string ret = str;
303  for (char & c : ret) if (std::isspace(c)) c = rep;
304  return ret;
305 }
306 
307 // ####################################################################################################
308 std::string jevois::strip(std::string const & str)
309 {
310  int idx = str.length() - 1;
311  while (idx >= 0 && std::isspace(str[idx])) --idx;
312  return str.substr(0, idx + 1);
313 }
314 
315 // ####################################################################################################
316 std::string jevois::extractString(std::string const & str, std::string const & startsep, std::string const & endsep)
317 {
318  size_t idx = str.find(startsep);
319  if (idx == std::string::npos) return std::string();
320  idx += startsep.size();
321 
322  if (endsep.empty()) return str.substr(idx);
323 
324  size_t idx2 = str.find(endsep, idx);
325  if (idx2 == std::string::npos) return std::string();
326  return str.substr(idx, idx2 - idx);
327 }
328 
329 // ####################################################################################################
330 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
331 size_t jevois::replaceStringFirst(std::string & str, std::string const & from, std::string const & to)
332 {
333  if (from.empty()) return 0;
334 
335  size_t start_pos = str.find(from);
336  if (start_pos == std::string::npos) return 0;
337 
338  str.replace(start_pos, from.length(), to);
339  return 1;
340 }
341 
342 // ####################################################################################################
343 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
344 size_t jevois::replaceStringAll(std::string & str, std::string const & from, std::string const & to)
345 {
346  if (from.empty()) return 0;
347 
348  size_t start_pos = 0, n = 0;
349  while((start_pos = str.find(from, start_pos)) != std::string::npos)
350  {
351  str.replace(start_pos, from.length(), to);
352  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
353  ++n;
354  }
355 
356  return n;
357 }
358 
359 // ####################################################################################################
360 // Code from https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
361 std::string jevois::replaceAll(std::string const & str, std::string const & from, std::string const & to)
362 {
363  if (from.empty()) return str;
364  std::string ret = str;
365 
366  size_t start_pos = 0;
367  while((start_pos = ret.find(from, start_pos)) != std::string::npos)
368  {
369  ret.replace(start_pos, from.length(), to);
370  start_pos += to.length(); // In case 'to' contains 'from', like replacing 'x' with 'yx'
371  }
372 
373  return ret;
374 }
375 
376 // ####################################################################################################
377 std::string jevois::tolower(std::string const & str)
378 {
379  std::string ret = str;
380  std::transform(ret.begin(), ret.end(), ret.begin(), [](unsigned char c) { return std::tolower(c); });
381  return ret;
382 }
383 
384 // ####################################################################################################
385 std::filesystem::path jevois::absolutePath(std::filesystem::path const & root, std::filesystem::path const & path)
386 {
387  // If path is empty, return root (be it empty of not):
388  if (path.empty()) return root;
389 
390  // no-op if the given path is already absolute:
391  if (path.is_absolute()) return path;
392 
393  // no-op if root is empty:
394  if (root.empty()) return path;
395 
396  // We know root is not empty and path does not start with a / and is not empty; concatenate both:
397  return root / path;
398 }
399 
400 // ####################################################################################################
401 namespace
402 {
403  // This code is from NRT, and before that from the iLab C++ neuromorphic vision toolkit
404  std::string vsformat(char const * fmt, va_list ap)
405  {
406  // if we have a null pointer or an empty string, then just return an empty std::string
407  if (fmt == nullptr || fmt[0] == '\0') return std::string();
408 
409  int bufsize = 1024;
410  while (true)
411  {
412  char buf[bufsize];
413 
414  int const nchars = vsnprintf(buf, bufsize, fmt, ap);
415 
416  if (nchars < 0)
417  {
418  // Better leave this as LFATAL() rather than LERROR(), otherwise we have to return a bogus std::string (e.g. an
419  // empty string, or "none", or...), which might be dangerous if it is later used as a filename, for example.
420  LFATAL("vsnprintf failed for format '" << fmt << "' with bufsize = " << bufsize);
421  }
422  else if (nchars >= bufsize)
423  {
424  // buffer was too small, so let's double the bufsize and try again:
425  bufsize *= 2;
426  continue;
427  }
428  else
429  {
430  // OK, the vsnprintf() succeeded:
431  return std::string(&buf[0], nchars);
432  }
433  }
434  return std::string(); // can't happen, but placate the compiler
435  }
436 }
437 
438 // ####################################################################################################
439 std::string jevois::sformat(char const * fmt, ...)
440 {
441  va_list a;
442  va_start(a, fmt);
443  std::string result = vsformat(fmt, a);
444  va_end(a);
445  return result;
446 }
447 
448 // ####################################################################################################
450 {
451 #ifdef JEVOIS_PLATFORM_A33
452  std::ofstream ofs("/proc/sys/vm/drop_caches");
453  if (ofs.is_open()) ofs << "3" << std::endl;
454  else LERROR("Failed to flush cache -- ignored");
455 #endif
456 }
457 
458 // ####################################################################################################
459 // This code modified from here: https://stackoverflow.com/questions/478898/how-to-execute-a-command-and-get-
460 // output-of-command-within-c-using-posix
461 std::string jevois::system(std::string const & cmd, bool errtoo)
462 {
463  std::array<char, 128> buffer; std::string result;
464 
465  FILE * pip;
466  if (errtoo) pip = popen((cmd + " 2>&1").c_str(), "r"); else pip = popen(cmd.c_str(), "r");
467  if (pip == nullptr) LFATAL("popen() failed for command [" << cmd << ']');
468  while (!feof(pip)) if (fgets(buffer.data(), 128, pip) != NULL) result += buffer.data();
469 
470  int status = pclose(pip);
471  if (status == -1 && errno == ECHILD) LFATAL("Could not start command: " << cmd);
472  else if (status) LFATAL("Command [" << cmd << "] exited with status " << status << ":\n\n" << result);
473 
474  return result;
475 }
476 
477 // ####################################################################################################
478 std::string jevois::secs2str(double secs)
479 {
480  if (secs < 1.0e-6) return jevois::sformat("%.2fns", secs * 1.0e9);
481  else if (secs < 1.0e-3) return jevois::sformat("%.2fus", secs * 1.0e6);
482  else if (secs < 1.0) return jevois::sformat("%.2fms", secs * 1.0e3);
483  else return jevois::sformat("%.2fs", secs);
484 }
485 
486 // ####################################################################################################
487 std::string jevois::secs2str(std::vector<double> secs)
488 {
489  if (secs.empty()) return "0.0 +/- 0.0s";
490  double sum = 0.0, sumsq = 0.0;
491  for (double s : secs) { sum += s; sumsq += s*s; }
492  double const avg = sum / secs.size();
493  double const std = std::sqrt(sumsq/secs.size() - avg*avg); // E[(X-E[X])^2] = E[X^2]-E[X]^2
494 
495  if (avg < 1.0e-6) return jevois::sformat("%.1f +/- %.1f ns", avg * 1.0e9, std * 1.0e9);
496  else if (avg < 1.0e-3) return jevois::sformat("%.1f +/- %.1f us", avg * 1.0e6, std * 1.0e6);
497  else if (avg < 1.0) return jevois::sformat("%.1f +/- %.1f ms", avg * 1.0e3, std * 1.0e3);
498  else return jevois::sformat("%.1f +/- %.1f s", avg, std);
499 }
500 
501 // ####################################################################################################
502 void jevois::secs2str(std::ostringstream & ss, double secs)
503 {
504  if (secs < 1.0e-6) ss << secs * 1.0e9 << "ns";
505  else if (secs < 1.0e-3) ss << secs * 1.0e6 << "us";
506  else if (secs < 1.0) ss << secs * 1.0e3 << "ms";
507  else ss << secs << 's';
508 }
509 
510 // ####################################################################################################
511 std::string jevois::num2str(double n)
512 {
513  if (n < 1.0e3) return jevois::sformat("%.2f", n);
514  else if (n < 1.0e6) return jevois::sformat("%.2fK", n / 1.0e3);
515  else if (n < 1.0e9) return jevois::sformat("%.2fM", n / 1.0e6);
516  else if (n < 1.0e12) return jevois::sformat("%.2fG", n / 1.0e9);
517  else if (n < 1.0e15) return jevois::sformat("%.2fT", n / 1.0e12);
518  else if (n < 1.0e18) return jevois::sformat("%.2fP", n / 1.0e15);
519  else if (n < 1.0e21) return jevois::sformat("%.2fE", n / 1.0e18);
520  else if (n < 1.0e24) return jevois::sformat("%.2fZ", n / 1.0e21);
521  else if (n < 1.0e27) return jevois::sformat("%.2fY", n / 1.0e24);
522  else return jevois::sformat("%.2f", n);
523 }
524 
525 // ####################################################################################################
526 void jevois::num2str(std::ostringstream & ss, double n)
527 {
528  if (n < 1.0e3) ss << n;
529  else if (n < 1.0e6) ss << n / 1.0e3 << 'K';
530  else if (n < 1.0e9) ss << n / 1.0e6 << 'M';
531  else if (n < 1.0e12) ss << n / 1.0e9 << 'G';
532  else if (n < 1.0e15) ss << n / 1.0e12 << 'T';
533  else if (n < 1.0e18) ss << n / 1.0e15 << 'P';
534  else if (n < 1.0e21) ss << n / 1.0e18 << 'E';
535  else if (n < 1.0e24) ss << n / 1.0e21 << 'Z';
536  else if (n < 1.0e27) ss << n / 1.0e24 << 'Y';
537  else ss << n;
538 }
539 
540 // ####################################################################################################
541 std::string jevois::getFileString(char const * fname, int skip)
542 {
543  std::ifstream ifs(fname);
544  if (ifs.is_open() == false) throw std::runtime_error("Cannot read file: "s + fname);
545  std::string str;
546  while (skip-- >= 0) std::getline(ifs, str);
547  ifs.close();
548 
549  return str;
550 }
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:197
jevois::sformat
std::string sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition: Utils.C:439
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:270
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:449
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:331
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:172
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:541
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:300
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:361
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:385
jevois::tolower
std::string tolower(std::string const &str)
Convert string to lowercase.
Definition: Utils.C:377
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:461
jevois::v4l2BytesPerPix
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:141
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:344
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:280
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:316
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:222
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:294
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:478
jevois::to_string
std::string to_string(T const &val)
Convert from type to string.
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:511
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:168
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:308