ViSP  2.9.0
vpImageConvert.h
1 /****************************************************************************
2 *
3 * $Id: vpImageConvert.h 4604 2014-01-21 14:15:23Z fspindle $
4 *
5 * This file is part of the ViSP software.
6 * Copyright (C) 2005 - 2014 by INRIA. All rights reserved.
7 *
8 * This software is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * ("GPL") version 2 as published by the Free Software Foundation.
11 * See the file LICENSE.txt at the root directory of this source
12 * distribution for additional information about the GNU GPL.
13 *
14 * For using ViSP with software that can not be combined with the GNU
15 * GPL, please contact INRIA about acquiring a ViSP Professional
16 * Edition License.
17 *
18 * See http://www.irisa.fr/lagadic/visp/visp.html for more information.
19 *
20 * This software was developed at:
21 * INRIA Rennes - Bretagne Atlantique
22 * Campus Universitaire de Beaulieu
23 * 35042 Rennes Cedex
24 * France
25 * http://www.irisa.fr/lagadic
26 *
27 * If you have questions regarding the use of this file, please contact
28 * INRIA at visp@inria.fr
29 *
30 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
31 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
32 *
33 *
34 * Description:
35 * Convert image types.
36 *
37 * Authors:
38 * Eric Marchand
39 * Fabien Spindler
40 * Anthony Saunier
41 *
42 *****************************************************************************/
43 
49 #ifndef vpIMAGECONVERT_H
50 #define vpIMAGECONVERT_H
51 
52 // image
53 #include <visp/vpConfig.h>
54 #include <visp/vpImage.h>
55 #include <visp/vpDebug.h>
56 // color
57 #include <visp/vpRGBa.h>
58 
59 #ifdef VISP_HAVE_OPENCV
60 # if (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
61 # include <opencv2/core/core.hpp>
62 # include <opencv2/legacy/legacy.hpp>
63 # include <opencv2/highgui/highgui.hpp>
64 # include <opencv2/highgui/highgui_c.h>
65 # else
66 # include <highgui.h>
67 # endif
68 #endif
69 
70 #ifdef VISP_HAVE_YARP
71 # include <yarp/sig/Image.h>
72 #endif
73 
74 #if defined(_WIN32)
75 # include <windows.h>
76 #endif
77 
78 #if defined(VISP_HAVE_LIBJPEG)
79 # include <jpeglib.h>
80 # include <jerror.h>
81 #endif
82 
83 #if defined(VISP_HAVE_LIBPNG)
84 # include <png.h>
85 #endif
86 
100 class VISP_EXPORT vpImageConvert
101 {
102 
103 public:
104  static void convert(const vpImage<unsigned char> &src,
105  vpImage<vpRGBa> & dest) ;
106  static void convert(const vpImage<vpRGBa> &src,
107  vpImage<unsigned char> & dest) ;
108 
109  static void convert(const vpImage<float> &src,
110  vpImage<unsigned char> &dest);
111  static void convert(const vpImage<unsigned char> &src,
112  vpImage<float> &dest);
113 
114  static void convert(const vpImage<double> &src,
115  vpImage<unsigned char> &dest);
116  static void convert(const vpImage<unsigned char> &src,
117  vpImage<double> &dest);
118 
119 #ifdef VISP_HAVE_OPENCV
120  static void convert(const IplImage* src,
121  vpImage<vpRGBa> & dest, bool flip = false) ;
122  static void convert(const IplImage* src,
123  vpImage<unsigned char> & dest, bool flip = false) ;
124  static void convert(const vpImage<vpRGBa> & src,
125  IplImage *&dest) ;
126  static void convert(const vpImage<unsigned char> & src,
127  IplImage* &dest) ;
128 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
129  static void convert(const cv::Mat& src,
130  vpImage<vpRGBa>& dest, const bool flip = false);
131  static void convert(const cv::Mat& src,
132  vpImage<unsigned char>& dest, const bool flip = false);
133  static void convert(const vpImage<vpRGBa> & src,
134  cv::Mat& dest) ;
135  static void convert(const vpImage<unsigned char> & src,
136  cv::Mat& dest, const bool copyData = true) ;
137 #endif
138 #endif
139 
140 #ifdef VISP_HAVE_YARP
141  static void convert(const vpImage<unsigned char> & src,
142  yarp::sig::ImageOf< yarp::sig::PixelMono > *dest, const bool copyData = true) ;
143  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelMono > *src,
144  vpImage<unsigned char> & dest,const bool copyData = true ) ;
145 
146 
147  static void convert(const vpImage<vpRGBa> & src,
148  yarp::sig::ImageOf< yarp::sig::PixelRgba > *dest, const bool copyData = true) ;
149  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgba > *src,
150  vpImage<vpRGBa> & dest,const bool copyData = true) ;
151 
152  static void convert(const vpImage<vpRGBa> & src,
153  yarp::sig::ImageOf< yarp::sig::PixelRgb > *dest) ;
154  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgb > *src,
155  vpImage<vpRGBa> & dest) ;
156 #endif
157 
158 #ifdef VISP_HAVE_LIBJPEG
159 #if JPEG_LIB_VERSION > 70
160  static void convertToJPEGBuffer(const vpImage<unsigned char> &src,
161  unsigned char **dest, long unsigned int &destSize, int quality = 100);
162 
163  static void convertToJPEGBuffer(unsigned char *src, long unsigned int srcSize,
164  vpImage<unsigned char> &dest);
165 #endif
166 #endif
167 
168  static void split(const vpImage<vpRGBa> &src,
172  vpImage<unsigned char>* pa = NULL) ;
173 
191  static inline void YUVToRGB(unsigned char y,
192  unsigned char u,
193  unsigned char v,
194  unsigned char &r,
195  unsigned char &g,
196  unsigned char &b)
197  {
198  double dr, dg, db;
199  dr = floor(0.9999695*y - 0.0009508*(u-128) + 1.1359061*(v-128));
200  dg = floor(0.9999695*y - 0.3959609*(u-128) - 0.5782955*(v-128));
201  db = floor(0.9999695*y + 2.04112*(u-128) - 0.0016314*(v-128));
202 
203  dr = dr < 0. ? 0. : dr;
204  dg = dg < 0. ? 0. : dg;
205  db = db < 0. ? 0. : db;
206  dr = dr > 255. ? 255. : dr;
207  dg = dg > 255. ? 255. : dg;
208  db = db > 255. ? 255. : db;
209 
210  r = (unsigned char) dr;
211  g = (unsigned char) dg;
212  b = (unsigned char) db;
213  };
214  static void YUYVToRGBa(unsigned char* yuyv, unsigned char* rgba,
215  unsigned int width, unsigned int height);
216  static void YUYVToRGB(unsigned char* yuyv, unsigned char* rgb,
217  unsigned int width, unsigned int height);
218  static void YUYVToGrey(unsigned char* yuyv, unsigned char* grey,
219  unsigned int size);
220  static void YUV411ToRGBa(unsigned char* yuv,
221  unsigned char* rgba, unsigned int size);
222  static void YUV411ToRGB(unsigned char* yuv,
223  unsigned char* rgb, unsigned int size);
224  static void YUV411ToGrey(unsigned char* yuv,
225  unsigned char* grey, unsigned int size);
226  static void YUV422ToRGBa(unsigned char* yuv,
227  unsigned char* rgba, unsigned int size);
228  static void YUV422ToRGB(unsigned char* yuv,
229  unsigned char* rgb, unsigned int size);
230  static void YUV422ToGrey(unsigned char* yuv,
231  unsigned char* grey, unsigned int size);
232  static void YUV420ToRGBa(unsigned char* yuv,
233  unsigned char* rgba, unsigned int width, unsigned int height);
234  static void YUV420ToRGB(unsigned char* yuv,
235  unsigned char* rgb, unsigned int width, unsigned int height);
236  static void YUV420ToGrey(unsigned char* yuv,
237  unsigned char* grey, unsigned int size);
238 
239  static void YUV444ToRGBa(unsigned char* yuv,
240  unsigned char* rgba, unsigned int size);
241  static void YUV444ToRGB(unsigned char* yuv,
242  unsigned char* rgb, unsigned int size);
243  static void YUV444ToGrey(unsigned char* yuv,
244  unsigned char* grey, unsigned int size);
245 
246  static void YV12ToRGBa(unsigned char* yuv,
247  unsigned char* rgba, unsigned int width, unsigned int height);
248  static void YV12ToRGB(unsigned char* yuv,
249  unsigned char* rgb, unsigned int width, unsigned int height);
250  static void YVU9ToRGBa(unsigned char* yuv,
251  unsigned char* rgba, unsigned int width, unsigned int height);
252  static void YVU9ToRGB(unsigned char* yuv,
253  unsigned char* rgb, unsigned int width, unsigned int height);
254  static void RGBToRGBa(unsigned char* rgb,
255  unsigned char* rgba, unsigned int size);
256  static void RGBaToRGB(unsigned char* rgba,
257  unsigned char* rgb, unsigned int size);
258 
259  static void RGBToGrey(unsigned char* rgb,
260  unsigned char* grey, unsigned int size);
261  static void RGBaToGrey(unsigned char* rgba,
262  unsigned char* grey, unsigned int size);
263 
264  static void RGBToRGBa(unsigned char * bgr, unsigned char * rgba,
265  unsigned int width, unsigned int height, bool flip = false);
266  static void RGBToGrey(unsigned char * bgr, unsigned char * grey,
267  unsigned int width, unsigned int height, bool flip = false);
268 
269  static void GreyToRGBa(unsigned char* grey,
270  unsigned char* rgba, unsigned int size);
271  static void GreyToRGB(unsigned char* grey,
272  unsigned char* rgb, unsigned int size);
273 
274  static void BGRToRGBa(unsigned char * bgr, unsigned char * rgba,
275  unsigned int width, unsigned int height, bool flip);
276 
277  static void BGRToGrey(unsigned char * bgr, unsigned char * grey,
278  unsigned int width, unsigned int height, bool flip);
279 
280  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb,
281  unsigned int size);
282  static void YCbCrToRGBa (unsigned char *ycbcr, unsigned char *rgb,
283  unsigned int size);
284  static void YCrCbToRGB(unsigned char *ycbcr, unsigned char *rgb,
285  unsigned int size);
286  static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb,
287  unsigned int size);
288  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey,
289  unsigned int size);
290  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey,
291  unsigned int size);
292  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba,
293  unsigned int size);
294 
295 private:
296  static void computeYCbCrLUT();
297 
298 private:
299  static bool YCbCrLUTcomputed;
300  static int vpCrr[256];
301  static int vpCgb[256];
302  static int vpCgr[256];
303  static int vpCbb[256];
304 
305 } ;
306 
307 #endif
308 
309 
310 /*
311 * Local variables:
312 * c-basic-offset: 2
313 * End:
314 */
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)