JeVois  1.0
JeVois Smart Embedded Machine Vision Toolkit
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 
29 #include <cstdarg> // for va_start, etc
30 
31 // ####################################################################################################
32 std::string jevois::fccstr(unsigned int fcc)
33 {
34  if (fcc == 0) return "NONE"; // for no video over USB output
35 
36  std::string ret(" ");
37  ret[0] = static_cast<char>(fcc & 0xff);
38  ret[1] = static_cast<char>((fcc >> 8) & 0xff);
39  ret[2] = static_cast<char>((fcc >> 16) & 0xff);
40  ret[3] = static_cast<char>((fcc >> 24) & 0xff);
41  return ret;
42 }
43 
44 // ####################################################################################################
45 unsigned int jevois::v4l2BytesPerPix(unsigned int fcc)
46 {
47  switch (fcc)
48  {
49  case V4L2_PIX_FMT_YUYV: return 2U;
50  case V4L2_PIX_FMT_GREY: return 1U;
51  case V4L2_PIX_FMT_SRGGB8: return 1U;
52  case V4L2_PIX_FMT_RGB565: return 2U;
53  case V4L2_PIX_FMT_MJPEG: return 2U; // at most??
54  case V4L2_PIX_FMT_BGR24: return 3U;
55  case 0: return 0U; // for NONE output to USB mode
56  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
57  }
58 }
59 
60 // ####################################################################################################
61 unsigned int jevois::v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
62 { return width * height * jevois::v4l2BytesPerPix(fcc); }
63 
64 // ####################################################################################################
65 std::vector<std::string> jevois::split(std::string const & input, std::string const & regex)
66 {
67  // This code is from: http://stackoverflow.com/questions/9435385/split-a-string-using-c11
68  // passing -1 as the submatch index parameter performs splitting
69  std::regex re(regex);
70  std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last;
71  return { first, last };
72 }
73 
74 // ####################################################################################################
75 std::string jevois::join(std::vector<std::string> const & strings, std::string const & delimiter)
76 {
77  if (strings.empty()) return "";
78  if (strings.size() == 1) return strings[0];
79 
80  std::string ret; size_t const szm1 = strings.size() - 1;
81 
82  for (size_t i = 0; i < szm1; ++i) ret += strings[i] + delimiter;
83  ret += strings[szm1];
84 
85  return ret;
86 }
87 
88 // ####################################################################################################
89 bool jevois::stringStartsWith(std::string const & str, std::string const & prefix)
90 {
91  return (strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0);
92 }
93 
94 // ####################################################################################################
95 namespace
96 {
97  // This code is from NRT, and before that from the iLab C++ neuromorphic vision toolkit
98  std::string vsformat(char const * fmt, va_list ap)
99  {
100  // if we have a null pointer or an empty string, then just return an empty std::string
101  if (fmt == nullptr || fmt[0] == '\0') return std::string();
102 
103  int bufsize = 1024;
104  while (true)
105  {
106  char buf[bufsize];
107 
108  int const nchars = vsnprintf(buf, bufsize, fmt, ap);
109 
110  if (nchars < 0)
111  {
112  // Better leave this as LFATAL() rather than LERROR(), otherwise we have to return a bogus std::string (e.g. an
113  // empty string, or "none", or...), which might be dangerous if it is later used as a filename, for example.
114  LFATAL("vsnprintf failed for format '" << fmt << "' with bufsize = " << bufsize);
115  }
116  else if (nchars >= bufsize)
117  {
118  // buffer was too small, so let's double the bufsize and try again:
119  bufsize *= 2;
120  continue;
121  }
122  else
123  {
124  // OK, the vsnprintf() succeeded:
125  return std::string(&buf[0], nchars);
126  }
127  }
128  return std::string(); // can't happen, but placate the compiler
129  }
130 }
131 
132 // ####################################################################################################
133 std::string jevois::sformat(char const * fmt, ...)
134 {
135  va_list a;
136  va_start(a, fmt);
137  std::string result = vsformat(fmt, a);
138  va_end(a);
139  return result;
140 }
141 
142 // ####################################################################################################
144 {
145 #ifdef JEVOIS_PLATFORM
146  std::ofstream ofs("/proc/sys/vm/drop_caches");
147  if (ofs.is_open()) ofs << "3" << std::endl;
148  else LERROR("Failed to flush cache -- ignored");
149 #endif
150 }
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:65
void flushcache()
Flush the caches, may sometimes be useful when running the cemare in turbo mode.
Definition: Utils.C:143
std::string sformat(char const *fmt,...) __attribute__((format(__printf__
Create a string using printf style arguments.
Definition: Utils.C:133
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:45
#define LERROR(msg)
Convenience macro for users to print out console or syslog messages, ERROR level. ...
Definition: Log.H:186
std::string join(std::vector< std::string > const &strings, std::string const &delimiter)
Concatenate a vector of tokens into a string.
Definition: Utils.C:75
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:89
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:32
#define LFATAL(msg)
Convenience macro for users to print out console or syslog messages, FATAL level. ...
Definition: Log.H:205
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:61