converters.cpp
Go to the documentation of this file.
1 #include "usb_cam/converters.h"
2 #include "usb_cam/types.h"
3 #include "usb_cam/util.h"
4 #include <linux/videodev2.h>
5 
6 namespace usb_cam
7 {
8 
9 namespace util
10 {
11 
12 namespace converters
13 {
14 
15 io_method_t io_method_from_string(const std::string & str)
16 {
17  if (str == "mmap")
19  else if (str == "read")
21  else if (str == "userptr")
23  else
25 }
26 
27 pixel_format_t pixel_format_from_string(const std::string & str)
28 {
29  if ((str == "yuyv") || (str == "yuv"))
31  else if (str == "uyvy")
33  else if (str == "mjpeg")
35  else if (str == "h264")
37  else if (str == "yuvmono10")
39  else if (str == "rgb24")
41  else if (str == "grey")
43  else if (str == "yu12")
45  else if (str == "bgr24")
46  return PIXEL_FORMAT_BGR24;
47  else
49 }
50 
51 std::string pixel_format_to_string(const uint32_t & pixelformat)
52 {
53  switch (pixelformat)
54  {
56  return "yuyv";
58  return "uyvy";
60  return "mjpeg";
62  return "h264";
64  return "yuvmono10";
66  return "rgb24";
68  return "bgr24";
70  return "grey";
72  return "yu12";
74  default:
75  return "unknown";
76  }
77 }
78 
79 color_format_t color_format_from_string(const std::string & str)
80 {
81  if (str == "yuv420p")
83  else if (str == "yuv422p")
85  else
87 }
88 
89 
90 unsigned int v4l_pixel_format_from_pixel_format(const pixel_format_t &pixelformat, bool &mono)
91 {
92  mono = false;
93  switch(pixelformat)
94  {
95  case PIXEL_FORMAT_YUYV:
96  return V4L2_PIX_FMT_YUYV;
97  case PIXEL_FORMAT_UYVY:
98  return V4L2_PIX_FMT_UYVY;
99  case PIXEL_FORMAT_MJPEG:
100  return V4L2_PIX_FMT_MJPEG;
101  case PIXEL_FORMAT_H264:
102  return V4L2_PIX_FMT_H264;
104  mono = true;
105  return V4L2_PIX_FMT_YUYV;
106  case PIXEL_FORMAT_RGB24:
107  return V4L2_PIX_FMT_RGB24;
108  case PIXEL_FORMAT_BGR24:
109  return V4L2_PIX_FMT_BGR24;
110  case PIXEL_FORMAT_GREY:
111  mono = true;
112  return V4L2_PIX_FMT_GREY;
113  case PIXEL_FORMAT_YU12:
114  return V4L2_PIX_FMT_YUV420;
115  default:
116  return UINT_MAX;
117  }
118 }
119 
120 bool YUV2RGB(const unsigned char &y,
121  const unsigned char &u,
122  const unsigned char &v,
123  unsigned char *r,
124  unsigned char *g,
125  unsigned char *b)
126 {
127  const int y2 = static_cast<int>(y);
128  const int u2 = static_cast<int>(u - 128);
129  const int v2 = static_cast<int>(v - 128);
130  // std::cerr << "YUV=("<<y2<<","<<u2<<","<<v2<<")"<<std::endl;
131 
132  // This is the normal YUV conversion, but
133  // appears to be incorrect for the firewire cameras
134  // int r2 = y2 + ( (v2*91947) >> 16);
135  // int g2 = y2 - ( ((u2*22544) + (v2*46793)) >> 16 );
136  // int b2 = y2 + ( (u2*115999) >> 16);
137  // This is an adjusted version (UV spread out a bit)
138  int r2 = y2 + ((v2 * 37221) >> 15);
139  int g2 = y2 - (((u2 * 12975) + (v2 * 18949)) >> 15);
140  int b2 = y2 + ((u2 * 66883) >> 15);
141  // std::cerr << " RGB=("<<r2<<","<<g2<<","<<b2<<")"<<std::endl;
142 
143  // Cap the values.
144  *r = util::CLIPVALUE(r2);
145  *g = util::CLIPVALUE(g2);
146  *b = util::CLIPVALUE(b2);
147 
148  return true;
149 }
150 
151 bool MONO102MONO8(const char *RAW, char *&MONO, const int &NumPixels)
152 {
153  int i, j;
154  for (i = 0, j = 0; i < (NumPixels << 1); i += 2, j += 1)
155  {
156  // first byte is low byte, second byte is high byte; smash together and convert to 8-bit
157  MONO[j] = (unsigned char)(((RAW[i + 0] >> 2) & 0x3F) | ((RAW[i + 1] << 6) & 0xC0));
158  }
159  return true;
160 }
161 
162 bool YUYV2RGB(const char *YUV, char *&RGB, const int &NumPixels)
163 {
164  int i, j;
165  unsigned char y0, y1, u, v;
166  unsigned char r, g, b;
167 
168  for (i = 0, j = 0; i < (NumPixels << 1); i += 4, j += 6)
169  {
170  y0 = (unsigned char)YUV[i + 0];
171  u = (unsigned char)YUV[i + 1];
172  y1 = (unsigned char)YUV[i + 2];
173  v = (unsigned char)YUV[i + 3];
174  YUV2RGB(y0, u, v, &r, &g, &b);
175  RGB[j + 0] = r;
176  RGB[j + 1] = g;
177  RGB[j + 2] = b;
178  YUV2RGB(y1, u, v, &r, &g, &b);
179  RGB[j + 3] = r;
180  RGB[j + 4] = g;
181  RGB[j + 5] = b;
182  }
183  return true;
184 }
185 
186 bool COPY2RGB(const char *input, char *&output, const int &NumPixels)
187 {
188  memcpy(output, input, NumPixels * 3);
189  return true;
190 }
191 
192 bool YUV4202RGB(char *YUV, char *&RGB, const int &width, const int &height)
193 {
194  cv::Size size(height, width);
195  cv::Mat cv_img(height * 1.5, width, CV_8UC1, YUV);
196  cv::Mat cv_out(height, width, CV_8UC3, RGB);
197  cvtColor(cv_img, cv_out, cv::COLOR_YUV420p2BGR);
198  return true;
199 }
200 
201 std::string FCC2S(const unsigned int &val)
202 {
203  std::string s;
204 
205  s += val & 0x7f;
206  s += (val >> 8) & 0x7f;
207  s += (val >> 16) & 0x7f;
208  s += (val >> 24) & 0x7f;
209  if (val & (1 << 31)) {
210  s += "-BE";
211  }
212  return s;
213 }
214 
215 bool UYVY2RGB(const char *YUV, char *&RGB, const int &NumPixels)
216 {
217  int i, j;
218  unsigned char y0, y1, u, v;
219  unsigned char r, g, b;
220  for (i = 0, j = 0; i < (NumPixels << 1); i += 4, j += 6) {
221  u = (unsigned char)YUV[i + 0];
222  y0 = (unsigned char)YUV[i + 1];
223  v = (unsigned char)YUV[i + 2];
224  y1 = (unsigned char)YUV[i + 3];
225  YUV2RGB(y0, u, v, &r, &g, &b);
226  RGB[j + 0] = r;
227  RGB[j + 1] = g;
228  RGB[j + 2] = b;
229  YUV2RGB(y1, u, v, &r, &g, &b);
230  RGB[j + 3] = r;
231  RGB[j + 4] = g;
232  RGB[j + 5] = b;
233  }
234  return true;
235 }
236 
237 std::string v4l_control_name_to_param_name(const char *name)
238 { // https://github.com/cz172638/v4l-utils/blob/master/utils/v4l2-ctl/v4l2-ctl-common.cpp
239  std::string s;
240  int add_underscore = 0;
241 
242  while (*name) {
243  if (isalnum(*name)) {
244  if (add_underscore)
245  s += '_';
246  add_underscore = 0;
247  s += std::string(1, tolower(*name));
248  }
249  else if (s.length()) add_underscore = 1;
250  name++;
251  }
252  return s;
253 }
254 
255 }
256 
257 }
258 
259 }
XmlRpcServer s
unsigned char CLIPVALUE(const int &val)
Definition: util.cpp:35
bool YUV4202RGB(char *YUV, char *&RGB, const int &width, const int &height)
Definition: converters.cpp:192
color_format_t color_format_from_string(const std::string &str)
Definition: converters.cpp:79
std::string FCC2S(const unsigned int &val)
Definition: converters.cpp:201
bool MONO102MONO8(const char *RAW, char *&MONO, const int &NumPixels)
Definition: converters.cpp:151
CvImagePtr cvtColor(const CvImageConstPtr &source, const std::string &encoding)
std::string pixel_format_to_string(const uint32_t &pixelformat)
Definition: converters.cpp:51
io_method_t io_method_from_string(const std::string &str)
Definition: converters.cpp:15
pixel_format_t
Definition: types.h:83
io_method_t
Definition: types.h:74
std::string v4l_control_name_to_param_name(const char *name)
Definition: converters.cpp:237
bool UYVY2RGB(const char *YUV, char *&RGB, const int &NumPixels)
Definition: converters.cpp:215
color_format_t
Definition: types.h:98
pixel_format_t pixel_format_from_string(const std::string &str)
Definition: converters.cpp:27
bool COPY2RGB(const char *input, char *&output, const int &NumPixels)
Definition: converters.cpp:186
bool YUYV2RGB(const char *YUV, char *&RGB, const int &NumPixels)
Definition: converters.cpp:162
unsigned int v4l_pixel_format_from_pixel_format(const pixel_format_t &pixelformat, bool &mono)
Definition: converters.cpp:90
bool YUV2RGB(const unsigned char &y, const unsigned char &u, const unsigned char &v, unsigned char *r, unsigned char *g, unsigned char *b)
Definition: converters.cpp:120


usb_cam
Author(s): Benjamin Pitzer
autogenerated on Sat May 27 2023 02:53:04