Visual Servoing Platform  version 3.6.1 under development (2023-12-07)
vpImageConvert.h
1 /*
2  * ViSP, open source Visual Servoing Platform software.
3  * Copyright (C) 2005 - 2023 by Inria. All rights reserved.
4  *
5  * This software is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * See the file LICENSE.txt at the root directory of this source
10  * distribution for additional information about the GNU GPL.
11  *
12  * For using ViSP with software that can not be combined with the GNU
13  * GPL, please contact Inria about acquiring a ViSP Professional
14  * Edition License.
15  *
16  * See https://visp.inria.fr for more information.
17  *
18  * This software was developed at:
19  * Inria Rennes - Bretagne Atlantique
20  * Campus Universitaire de Beaulieu
21  * 35042 Rennes Cedex
22  * France
23  *
24  * If you have questions regarding the use of this file, please contact
25  * Inria at visp@inria.fr
26  *
27  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
28  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
29  *
30  * Description:
31  * Convert image types.
32  */
33 
39 #ifndef _vpImageConvert_h_
40 #define _vpImageConvert_h_
41 
42 #include <stdint.h>
43 
44 // image
45 #include <visp3/core/vpConfig.h>
46 #include <visp3/core/vpDebug.h>
47 #include <visp3/core/vpImage.h>
48 // color
49 #include <visp3/core/vpRGBa.h>
50 
51 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
52 #include <opencv2/imgproc.hpp>
53 #include <opencv2/imgproc/types_c.h>
54 #endif
55 
56 #ifdef VISP_HAVE_YARP
57 #include <yarp/sig/Image.h>
58 #endif
59 
60 #if defined(_WIN32)
61 // Include WinSock2.h before windows.h to ensure that winsock.h is not
62 // included by windows.h since winsock.h and winsock2.h are incompatible
63 #include <WinSock2.h>
64 #include <windows.h>
65 #endif
66 
80 class VISP_EXPORT vpImageConvert
81 {
82 
83 public:
84  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<vpRGBa> &dest_rgba);
85  static void createDepthHistogram(const vpImage<uint16_t> &src_depth, vpImage<unsigned char> &dest_depth);
86 
87  static void createDepthHistogram(const vpImage<float> &src_depth, vpImage<vpRGBa> &dest_depth);
88  static void createDepthHistogram(const vpImage<float> &src_depth, vpImage<unsigned char> &dest_depth);
89 
90  static void convert(const vpImage<unsigned char> &src, vpImage<vpRGBa> &dest);
91  static void convert(const vpImage<vpRGBa> &src, vpImage<unsigned char> &dest, unsigned int nThreads = 0);
92 
93  static void convert(const vpImage<float> &src, vpImage<unsigned char> &dest);
94  static void convert(const vpImage<vpRGBf> &src, vpImage<vpRGBa> &dest);
95  static void convert(const vpImage<unsigned char> &src, vpImage<float> &dest);
96 
97  static void convert(const vpImage<double> &src, vpImage<unsigned char> &dest);
98  static void convert(const vpImage<unsigned char> &src, vpImage<double> &dest);
99 
100  static void convert(const vpImage<uint16_t> &src, vpImage<unsigned char> &dest, unsigned char bitshift = 8);
101  static void convert(const vpImage<unsigned char> &src, vpImage<uint16_t> &dest, unsigned char bitshift = 8);
102 
108  template <typename Type> static void convert(const vpImage<Type> &src, vpImage<Type> &dest) { dest = src; }
109 
110 #if defined(VISP_HAVE_OPENCV) && defined(HAVE_OPENCV_IMGPROC)
111  static void convert(const cv::Mat &src, vpImage<vpRGBa> &dest, bool flip = false);
112  static void convert(const cv::Mat &src, vpImage<unsigned char> &dest, bool flip = false, unsigned int nThreads = 0);
113  static void convert(const cv::Mat &src, vpImage<float> &dest, bool flip = false);
114  static void convert(const cv::Mat &src, vpImage<double> &dest, bool flip = false);
115  static void convert(const cv::Mat &src, vpImage<vpRGBf> &dest, bool flip = false);
116  static void convert(const cv::Mat &src, vpImage<uint16_t> &dest, bool flip = false);
117  static void convert(const vpImage<vpRGBa> &src, cv::Mat &dest);
118  static void convert(const vpImage<unsigned char> &src, cv::Mat &dest, bool copyData = true);
119  static void convert(const vpImage<float> &src, cv::Mat &dest, bool copyData = true);
120  static void convert(const vpImage<double> &src, cv::Mat &dest, bool copyData = true);
121  static void convert(const vpImage<vpRGBf> &src, cv::Mat &dest);
122 #endif
123 
124 #ifdef VISP_HAVE_YARP
125  static void convert(const vpImage<unsigned char> &src, yarp::sig::ImageOf<yarp::sig::PixelMono> *dest,
126  bool copyData = true);
127  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelMono> *src, vpImage<unsigned char> &dest,
128  bool copyData = true);
129 
130  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgba> *dest, bool copyData = true);
131  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgba> *src, vpImage<vpRGBa> &dest, bool copyData = true);
132 
133  static void convert(const vpImage<vpRGBa> &src, yarp::sig::ImageOf<yarp::sig::PixelRgb> *dest);
134  static void convert(const yarp::sig::ImageOf<yarp::sig::PixelRgb> *src, vpImage<vpRGBa> &dest);
135 #endif
136 
137  static void split(const vpImage<vpRGBa> &src, vpImage<unsigned char> *pR, vpImage<unsigned char> *pG,
139 
140  static void merge(const vpImage<unsigned char> *R, const vpImage<unsigned char> *G, const vpImage<unsigned char> *B,
141  const vpImage<unsigned char> *a, vpImage<vpRGBa> &RGBa);
142 
157  static inline void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g,
158  unsigned char &b)
159  {
160  double dr, dg, db;
161  dr = floor(0.9999695 * y - 0.0009508 * (u - 128) + 1.1359061 * (v - 128));
162  dg = floor(0.9999695 * y - 0.3959609 * (u - 128) - 0.5782955 * (v - 128));
163  db = floor(0.9999695 * y + 2.04112 * (u - 128) - 0.0016314 * (v - 128));
164 
165  dr = dr < 0. ? 0. : dr;
166  dg = dg < 0. ? 0. : dg;
167  db = db < 0. ? 0. : db;
168  dr = dr > 255. ? 255. : dr;
169  dg = dg > 255. ? 255. : dg;
170  db = db > 255. ? 255. : db;
171 
172  r = (unsigned char)dr;
173  g = (unsigned char)dg;
174  b = (unsigned char)db;
175  }
176  static void YUYVToRGBa(unsigned char *yuyv, unsigned char *rgba, unsigned int width, unsigned int height);
177  static void YUYVToRGB(unsigned char *yuyv, unsigned char *rgb, unsigned int width, unsigned int height);
178  static void YUYVToGrey(unsigned char *yuyv, unsigned char *grey, unsigned int size);
179  static void YUV411ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
180  static void YUV411ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
181  static void YUV411ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
182  static void YUV422ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
183  static void YUV422ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
184  static void YUV422ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
185  static void YUV420ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
186  static void YUV420ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
187  static void YUV420ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
188 
189  static void YUV444ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int size);
190  static void YUV444ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int size);
191  static void YUV444ToGrey(unsigned char *yuv, unsigned char *grey, unsigned int size);
192 
193  static void YV12ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
194  static void YV12ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
195  static void YVU9ToRGBa(unsigned char *yuv, unsigned char *rgba, unsigned int width, unsigned int height);
196  static void YVU9ToRGB(unsigned char *yuv, unsigned char *rgb, unsigned int width, unsigned int height);
197  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int size);
198  static void RGBaToRGB(unsigned char *rgba, unsigned char *rgb, unsigned int size);
199 
200  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int width, unsigned int height,
201  bool flip = false);
202  static void RGBToGrey(unsigned char *rgb, unsigned char *grey, unsigned int size);
203  static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int width, unsigned int height,
204  unsigned int nThreads = 0);
205  static void RGBaToGrey(unsigned char *rgba, unsigned char *grey, unsigned int size);
206 
207  static void RGBToRGBa(unsigned char *rgb, unsigned char *rgba, unsigned int width, unsigned int height,
208  bool flip = false);
209 
210  static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int width, unsigned int height);
211  static void GreyToRGBa(unsigned char *grey, unsigned char *rgba, unsigned int size);
212  static void GreyToRGB(unsigned char *grey, unsigned char *rgb, unsigned int size);
213 
214  static void BGRToRGBa(unsigned char *bgr, unsigned char *rgba, unsigned int width, unsigned int height,
215  bool flip = false);
216 
217  static void BGRToGrey(unsigned char *bgr, unsigned char *grey, unsigned int width, unsigned int height,
218  bool flip = false, unsigned int nThreads = 0);
219 
220  static void BGRaToGrey(unsigned char *bgra, unsigned char *grey, unsigned int width, unsigned int height,
221  bool flip = false, unsigned int nThreads = 0);
222  static void BGRaToRGBa(unsigned char *bgra, unsigned char *rgba, unsigned int width, unsigned int height,
223  bool flip = false);
224 
225  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
226  static void YCbCrToRGBa(unsigned char *ycbcr, unsigned char *rgb, unsigned int size);
227  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey, unsigned int size);
228  static void YCrCbToRGB(unsigned char *ycrcb, unsigned char *rgb, unsigned int size);
229  static void YCrCbToRGBa(unsigned char *ycrcb, unsigned char *rgb, unsigned int size);
230  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey, unsigned int size);
231  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba, unsigned int size);
232 
233  static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
234  unsigned int size);
235  static void HSVToRGBa(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
236  unsigned char *rgba, unsigned int size);
237  static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value, unsigned int size);
238  static void RGBaToHSV(const unsigned char *rgba, unsigned char *hue, unsigned char *saturation, unsigned char *value,
239  unsigned int size);
240 
241  static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb,
242  unsigned int size);
243  static void HSVToRGB(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
244  unsigned char *rgb, unsigned int size);
245  static void RGBToHSV(const unsigned char *rgb, double *hue, double *saturation, double *value, unsigned int size);
246  static void RGBToHSV(const unsigned char *rgb, unsigned char *hue, unsigned char *saturation, unsigned char *value,
247  unsigned int size);
248 
249  static void demosaicBGGRToRGBaBilinear(const uint8_t *bggr, uint8_t *rgba, unsigned int width, unsigned int height,
250  unsigned int nThreads = 0);
251  static void demosaicBGGRToRGBaBilinear(const uint16_t *bggr, uint16_t *rgba, unsigned int width, unsigned int height,
252  unsigned int nThreads = 0);
253 
254  static void demosaicGBRGToRGBaBilinear(const uint8_t *gbrg, uint8_t *rgba, unsigned int width, unsigned int height,
255  unsigned int nThreads = 0);
256  static void demosaicGBRGToRGBaBilinear(const uint16_t *gbrg, uint16_t *rgba, unsigned int width, unsigned int height,
257  unsigned int nThreads = 0);
258 
259  static void demosaicGRBGToRGBaBilinear(const uint8_t *grbg, uint8_t *rgba, unsigned int width, unsigned int height,
260  unsigned int nThreads = 0);
261  static void demosaicGRBGToRGBaBilinear(const uint16_t *grbg, uint16_t *rgba, unsigned int width, unsigned int height,
262  unsigned int nThreads = 0);
263 
264  static void demosaicRGGBToRGBaBilinear(const uint8_t *rggb, uint8_t *rgba, unsigned int width, unsigned int height,
265  unsigned int nThreads = 0);
266  static void demosaicRGGBToRGBaBilinear(const uint16_t *rggb, uint16_t *rgba, unsigned int width, unsigned int height,
267  unsigned int nThreads = 0);
268 
269  static void demosaicBGGRToRGBaMalvar(const uint8_t *bggr, uint8_t *rgba, unsigned int width, unsigned int height,
270  unsigned int nThreads = 0);
271  static void demosaicBGGRToRGBaMalvar(const uint16_t *bggr, uint16_t *rgba, unsigned int width, unsigned int height,
272  unsigned int nThreads = 0);
273 
274  static void demosaicGBRGToRGBaMalvar(const uint8_t *gbrg, uint8_t *rgba, unsigned int width, unsigned int height,
275  unsigned int nThreads = 0);
276  static void demosaicGBRGToRGBaMalvar(const uint16_t *gbrg, uint16_t *rgba, unsigned int width, unsigned int height,
277  unsigned int nThreads = 0);
278 
279  static void demosaicGRBGToRGBaMalvar(const uint8_t *grbg, uint8_t *rgba, unsigned int width, unsigned int height,
280  unsigned int nThreads = 0);
281  static void demosaicGRBGToRGBaMalvar(const uint16_t *grbg, uint16_t *rgba, unsigned int width, unsigned int height,
282  unsigned int nThreads = 0);
283 
284  static void demosaicRGGBToRGBaMalvar(const uint8_t *rggb, uint8_t *rgba, unsigned int width, unsigned int height,
285  unsigned int nThreads = 0);
286  static void demosaicRGGBToRGBaMalvar(const uint16_t *rggb, uint16_t *rgba, unsigned int width, unsigned int height,
287  unsigned int nThreads = 0);
288 
289 private:
290  static void computeYCbCrLUT();
291 
292  static void HSV2RGB(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
293  unsigned int size, unsigned int step);
294  static void RGB2HSV(const unsigned char *rgb, double *hue, double *saturation, double *value, unsigned int size,
295  unsigned int step);
296 
297 private:
298  static bool YCbCrLUTcomputed;
299  static int vpCrr[256];
300  static int vpCgb[256];
301  static int vpCgr[256];
302  static int vpCbb[256];
303 };
304 
305 #endif
static void convert(const vpImage< Type > &src, vpImage< Type > &dest)
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)
Definition of the vpImage class member functions.
Definition: vpImage.h:69