Visual Servoing Platform  version 3.0.0
vpImageConvert.h
1 /****************************************************************************
2  *
3  * This file is part of the ViSP software.
4  * Copyright (C) 2005 - 2015 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 http://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  * Authors:
34  * Eric Marchand
35  * Fabien Spindler
36  * Anthony Saunier
37  *
38  *****************************************************************************/
39 
45 #ifndef vpIMAGECONVERT_H
46 #define vpIMAGECONVERT_H
47 
48 // image
49 #include <visp3/core/vpConfig.h>
50 #include <visp3/core/vpImage.h>
51 #include <visp3/core/vpDebug.h>
52 // color
53 #include <visp3/core/vpRGBa.h>
54 
55 #ifdef VISP_HAVE_OPENCV
56 # if (VISP_HAVE_OPENCV_VERSION >= 0x030000) // Require opencv >= 3.0.0
57 # include <opencv2/core/core.hpp>
58 # include <opencv2/highgui/highgui.hpp>
59 # include <opencv2/imgproc/imgproc_c.h>
60 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
61 # include <opencv2/core/core.hpp>
62 # include <opencv2/highgui/highgui.hpp>
63 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
64 # include <opencv2/core/core.hpp>
65 # include <opencv2/legacy/legacy.hpp>
66 # include <opencv2/highgui/highgui.hpp>
67 # include <opencv2/highgui/highgui_c.h>
68 # else
69 # include <highgui.h>
70 # endif
71 #endif
72 
73 #ifdef VISP_HAVE_YARP
74 # include <yarp/sig/Image.h>
75 #endif
76 
77 #if defined(_WIN32)
78 # include <windows.h>
79 #endif
80 
94 class VISP_EXPORT vpImageConvert
95 {
96 
97 public:
98  static void convert(const vpImage<unsigned char> &src,
99  vpImage<vpRGBa> & dest) ;
100  static void convert(const vpImage<vpRGBa> &src,
101  vpImage<unsigned char> & dest) ;
102 
103  static void convert(const vpImage<float> &src,
104  vpImage<unsigned char> &dest);
105  static void convert(const vpImage<unsigned char> &src,
106  vpImage<float> &dest);
107 
108  static void convert(const vpImage<double> &src,
109  vpImage<unsigned char> &dest);
110  static void convert(const vpImage<unsigned char> &src,
111  vpImage<double> &dest);
112 
113 #ifdef VISP_HAVE_OPENCV
114  // Deprecated: will be removed with OpenCV transcient from C to C++ api
115  static void convert(const IplImage* src,
116  vpImage<vpRGBa> & dest, bool flip = false) ;
117  static void convert(const IplImage* src,
118  vpImage<unsigned char> & dest, bool flip = false) ;
119  static void convert(const vpImage<vpRGBa> & src,
120  IplImage *&dest) ;
121  static void convert(const vpImage<unsigned char> & src,
122  IplImage* &dest) ;
123 # if VISP_HAVE_OPENCV_VERSION >= 0x020100
124  static void convert(const cv::Mat& src,
125  vpImage<vpRGBa>& dest, const bool flip = false);
126  static void convert(const cv::Mat& src,
127  vpImage<unsigned char>& dest, const bool flip = false);
128  static void convert(const vpImage<vpRGBa> & src,
129  cv::Mat& dest) ;
130  static void convert(const vpImage<unsigned char> & src,
131  cv::Mat& dest, const bool copyData = true) ;
132 # endif
133 #endif
134 
135 #ifdef VISP_HAVE_YARP
136  static void convert(const vpImage<unsigned char> & src,
137  yarp::sig::ImageOf< yarp::sig::PixelMono > *dest, const bool copyData = true) ;
138  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelMono > *src,
139  vpImage<unsigned char> & dest,const bool copyData = true ) ;
140 
141 
142  static void convert(const vpImage<vpRGBa> & src,
143  yarp::sig::ImageOf< yarp::sig::PixelRgba > *dest, const bool copyData = true) ;
144  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgba > *src,
145  vpImage<vpRGBa> & dest,const bool copyData = true) ;
146 
147  static void convert(const vpImage<vpRGBa> & src,
148  yarp::sig::ImageOf< yarp::sig::PixelRgb > *dest) ;
149  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgb > *src,
150  vpImage<vpRGBa> & dest) ;
151 #endif
152 
153  static void split(const vpImage<vpRGBa> &src,
157  vpImage<unsigned char>* pa = NULL);
158 
159  static void merge(const vpImage<unsigned char> *R,
160  const vpImage<unsigned char> *G,
161  const vpImage<unsigned char> *B,
162  const vpImage<unsigned char> *a,
163  vpImage<vpRGBa> &RGBa);
164 
182  static inline void YUVToRGB(unsigned char y,
183  unsigned char u,
184  unsigned char v,
185  unsigned char &r,
186  unsigned char &g,
187  unsigned char &b)
188  {
189  double dr, dg, db;
190  dr = floor(0.9999695*y - 0.0009508*(u-128) + 1.1359061*(v-128));
191  dg = floor(0.9999695*y - 0.3959609*(u-128) - 0.5782955*(v-128));
192  db = floor(0.9999695*y + 2.04112*(u-128) - 0.0016314*(v-128));
193 
194  dr = dr < 0. ? 0. : dr;
195  dg = dg < 0. ? 0. : dg;
196  db = db < 0. ? 0. : db;
197  dr = dr > 255. ? 255. : dr;
198  dg = dg > 255. ? 255. : dg;
199  db = db > 255. ? 255. : db;
200 
201  r = (unsigned char) dr;
202  g = (unsigned char) dg;
203  b = (unsigned char) db;
204  };
205  static void YUYVToRGBa(unsigned char* yuyv, unsigned char* rgba,
206  unsigned int width, unsigned int height);
207  static void YUYVToRGB(unsigned char* yuyv, unsigned char* rgb,
208  unsigned int width, unsigned int height);
209  static void YUYVToGrey(unsigned char* yuyv, unsigned char* grey,
210  unsigned int size);
211  static void YUV411ToRGBa(unsigned char* yuv,
212  unsigned char* rgba, unsigned int size);
213  static void YUV411ToRGB(unsigned char* yuv,
214  unsigned char* rgb, unsigned int size);
215  static void YUV411ToGrey(unsigned char* yuv,
216  unsigned char* grey, unsigned int size);
217  static void YUV422ToRGBa(unsigned char* yuv,
218  unsigned char* rgba, unsigned int size);
219  static void YUV422ToRGB(unsigned char* yuv,
220  unsigned char* rgb, unsigned int size);
221  static void YUV422ToGrey(unsigned char* yuv,
222  unsigned char* grey, unsigned int size);
223  static void YUV420ToRGBa(unsigned char* yuv,
224  unsigned char* rgba, unsigned int width, unsigned int height);
225  static void YUV420ToRGB(unsigned char* yuv,
226  unsigned char* rgb, unsigned int width, unsigned int height);
227  static void YUV420ToGrey(unsigned char* yuv,
228  unsigned char* grey, unsigned int size);
229 
230  static void YUV444ToRGBa(unsigned char* yuv,
231  unsigned char* rgba, unsigned int size);
232  static void YUV444ToRGB(unsigned char* yuv,
233  unsigned char* rgb, unsigned int size);
234  static void YUV444ToGrey(unsigned char* yuv,
235  unsigned char* grey, unsigned int size);
236 
237  static void YV12ToRGBa(unsigned char* yuv,
238  unsigned char* rgba, unsigned int width, unsigned int height);
239  static void YV12ToRGB(unsigned char* yuv,
240  unsigned char* rgb, unsigned int width, unsigned int height);
241  static void YVU9ToRGBa(unsigned char* yuv,
242  unsigned char* rgba, unsigned int width, unsigned int height);
243  static void YVU9ToRGB(unsigned char* yuv,
244  unsigned char* rgb, unsigned int width, unsigned int height);
245  static void RGBToRGBa(unsigned char* rgb,
246  unsigned char* rgba, unsigned int size);
247  static void RGBaToRGB(unsigned char* rgba,
248  unsigned char* rgb, unsigned int size);
249 
250  static void RGBToGrey(unsigned char* rgb,
251  unsigned char* grey, unsigned int size);
252  static void RGBaToGrey(unsigned char* rgba,
253  unsigned char* grey, unsigned int size);
254 
255  static void RGBToRGBa(unsigned char * bgr, unsigned char * rgba,
256  unsigned int width, unsigned int height, bool flip = false);
257  static void RGBToGrey(unsigned char * bgr, unsigned char * grey,
258  unsigned int width, unsigned int height, bool flip = false);
259 
260  static void GreyToRGBa(unsigned char* grey,
261  unsigned char* rgba, unsigned int size);
262  static void GreyToRGB(unsigned char* grey,
263  unsigned char* rgb, unsigned int size);
264 
265  static void BGRToRGBa(unsigned char * bgr, unsigned char * rgba,
266  unsigned int width, unsigned int height, bool flip=false);
267 
268  static void BGRToGrey(unsigned char * bgr, unsigned char * grey,
269  unsigned int width, unsigned int height, bool flip=false);
270 
271  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb,
272  unsigned int size);
273  static void YCbCrToRGBa (unsigned char *ycbcr, unsigned char *rgb,
274  unsigned int size);
275  static void YCrCbToRGB(unsigned char *ycbcr, unsigned char *rgb,
276  unsigned int size);
277  static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb,
278  unsigned int size);
279  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey,
280  unsigned int size);
281  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey,
282  unsigned int size);
283  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba,
284  unsigned int size);
285 
286  static void HSVToRGBa(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
287  const unsigned int size);
288  static void HSVToRGBa(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
289  unsigned char *rgba, const unsigned int size);
290  static void RGBaToHSV(const unsigned char *rgba, double *hue, double *saturation, double *value,
291  const unsigned int size);
292  static void RGBaToHSV(const unsigned char *rgba, unsigned char *hue, unsigned char *saturation, unsigned char *value,
293  const unsigned int size);
294 
295  static void HSVToRGB(const double *hue, const double *saturation, const double *value, unsigned char *rgb,
296  const unsigned int size);
297  static void HSVToRGB(const unsigned char *hue, const unsigned char *saturation, const unsigned char *value,
298  unsigned char *rgb, const unsigned int size);
299  static void RGBToHSV(const unsigned char *rgb, double *hue, double *saturation, double *value,
300  const unsigned int size);
301  static void RGBToHSV(const unsigned char *rgb, unsigned char *hue, unsigned char *saturation, unsigned char *value,
302  const unsigned int size);
303 
304 private:
305  static void computeYCbCrLUT();
306 
307  static void HSV2RGB(const double *hue, const double *saturation, const double *value, unsigned char *rgba,
308  const unsigned int size, const unsigned int step);
309  static void RGB2HSV(const unsigned char *rgb, double *hue, double *saturation, double *value,
310  const unsigned int size, const unsigned int step);
311 
312 private:
313  static bool YCbCrLUTcomputed;
314  static int vpCrr[256];
315  static int vpCgb[256];
316  static int vpCgr[256];
317  static int vpCbb[256];
318 
319 } ;
320 
321 #endif
322 
323 
324 /*
325 * Local variables:
326 * c-basic-offset: 2
327 * End:
328 */
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)