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 // ####################################################################################################
30 std::string jevois::fccstr(unsigned int fcc)
31 {
32  if (fcc == 0) return "NONE"; // for no video over USB output
33 
34  std::string ret(" ");
35  ret[0] = static_cast<char>(fcc & 0xff);
36  ret[1] = static_cast<char>((fcc >> 8) & 0xff);
37  ret[2] = static_cast<char>((fcc >> 16) & 0xff);
38  ret[3] = static_cast<char>((fcc >> 24) & 0xff);
39  return ret;
40 }
41 
42 // ####################################################################################################
43 unsigned int jevois::v4l2BytesPerPix(unsigned int fcc)
44 {
45  switch (fcc)
46  {
47  case V4L2_PIX_FMT_YUYV: return 2U;
48  case V4L2_PIX_FMT_GREY: return 1U;
49  case V4L2_PIX_FMT_SRGGB8: return 1U;
50  case V4L2_PIX_FMT_RGB565: return 2U;
51  case V4L2_PIX_FMT_MJPEG: return 2U; // at most??
52  case V4L2_PIX_FMT_BGR24: return 3U;
53  case 0: return 0U; // for NONE output to USB mode
54  default: LFATAL("Unsupported pixel format " << jevois::fccstr(fcc));
55  }
56 }
57 
58 // ####################################################################################################
59 unsigned int jevois::v4l2ImageSize(unsigned int fcc, unsigned int width, unsigned int height)
60 { return width * height * jevois::v4l2BytesPerPix(fcc); }
61 
62 // ####################################################################################################
63 std::vector<std::string> jevois::split(std::string const & input, std::string const & regex)
64 {
65  // This code is from: http://stackoverflow.com/questions/9435385/split-a-string-using-c11
66  // passing -1 as the submatch index parameter performs splitting
67  std::regex re(regex);
68  std::sregex_token_iterator first{input.begin(), input.end(), re, -1}, last;
69  return { first, last };
70 }
71 
72 // ####################################################################################################
73 std::string jevois::join(std::vector<std::string> const & strings, std::string const & delimiter)
74 {
75  if (strings.empty()) return "";
76  if (strings.size() == 1) return strings[0];
77 
78  std::string ret; size_t const szm1 = strings.size() - 1;
79 
80  for (size_t i = 0; i < szm1; ++i) ret += strings[i] + delimiter;
81  ret += strings[szm1];
82 
83  return ret;
84 }
85 
86 // ####################################################################################################
87 bool jevois::stringStartsWith(std::string const & str, std::string const & prefix)
88 {
89  return (strncmp(str.c_str(), prefix.c_str(), prefix.length()) == 0);
90 }
91 
92 // ####################################################################################################
94 {
95 #ifdef JEVOIS_PLATFORM
96  std::ofstream ofs("/proc/sys/vm/drop_caches");
97  if (ofs.is_open()) ofs << "3" << std::endl;
98  else LERROR("Failed to flush cache -- ignored");
99 #endif
100 }
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:63
void flushcache()
Flush the caches, may sometimes be useful when running the cemare in turbo mode.
Definition: Utils.C:93
unsigned int v4l2BytesPerPix(unsigned int fcc)
Return the number of bytes per pixel for a given V4L2_PIX_FMT_...
Definition: Utils.C:43
#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:73
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:87
std::string fccstr(unsigned int fcc)
Convert a V4L2 four-cc code (V4L2_PIX_FMT_...) to a 4-char string.
Definition: Utils.C:30
#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:59