ViSP  2.10.0
vpImageConvert.h
1 /****************************************************************************
2 *
3 * $Id: vpImageConvert.h 5204 2015-01-24 13:18:18Z 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 >= 0x030000) // Require opencv >= 3.0.0
61 # include <opencv2/core/core.hpp>
62 # include <opencv2/highgui/highgui.hpp>
63 # include <opencv2/imgproc/imgproc_c.h>
64 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020408) // Require opencv >= 2.4.8
65 # include <opencv2/core/core.hpp>
66 # include <opencv2/highgui/highgui.hpp>
67 # elif (VISP_HAVE_OPENCV_VERSION >= 0x020101) // Require opencv >= 2.1.1
68 # include <opencv2/core/core.hpp>
69 # include <opencv2/legacy/legacy.hpp>
70 # include <opencv2/highgui/highgui.hpp>
71 # include <opencv2/highgui/highgui_c.h>
72 # else
73 # include <highgui.h>
74 # endif
75 #endif
76 
77 #ifdef VISP_HAVE_YARP
78 # include <yarp/sig/Image.h>
79 #endif
80 
81 #if defined(_WIN32)
82 # include <windows.h>
83 #endif
84 
85 #if defined(VISP_HAVE_LIBJPEG)
86 # include <jpeglib.h>
87 # include <jerror.h>
88 #endif
89 
90 #if defined(VISP_HAVE_LIBPNG)
91 # include <png.h>
92 #endif
93 
107 class VISP_EXPORT vpImageConvert
108 {
109 
110 public:
111  static void convert(const vpImage<unsigned char> &src,
112  vpImage<vpRGBa> & dest) ;
113  static void convert(const vpImage<vpRGBa> &src,
114  vpImage<unsigned char> & dest) ;
115 
116  static void convert(const vpImage<float> &src,
117  vpImage<unsigned char> &dest);
118  static void convert(const vpImage<unsigned char> &src,
119  vpImage<float> &dest);
120 
121  static void convert(const vpImage<double> &src,
122  vpImage<unsigned char> &dest);
123  static void convert(const vpImage<unsigned char> &src,
124  vpImage<double> &dest);
125 
126 #ifdef VISP_HAVE_OPENCV
127  // Deprecated: will be removed with OpenCV transcient from C to C++ api
128  static void convert(const IplImage* src,
129  vpImage<vpRGBa> & dest, bool flip = false) ;
130  static void convert(const IplImage* src,
131  vpImage<unsigned char> & dest, bool flip = false) ;
132  static void convert(const vpImage<vpRGBa> & src,
133  IplImage *&dest) ;
134  static void convert(const vpImage<unsigned char> & src,
135  IplImage* &dest) ;
136 # if VISP_HAVE_OPENCV_VERSION >= 0x020100
137  static void convert(const cv::Mat& src,
138  vpImage<vpRGBa>& dest, const bool flip = false);
139  static void convert(const cv::Mat& src,
140  vpImage<unsigned char>& dest, const bool flip = false);
141  static void convert(const vpImage<vpRGBa> & src,
142  cv::Mat& dest) ;
143  static void convert(const vpImage<unsigned char> & src,
144  cv::Mat& dest, const bool copyData = true) ;
145 # endif
146 #endif
147 
148 #ifdef VISP_HAVE_YARP
149  static void convert(const vpImage<unsigned char> & src,
150  yarp::sig::ImageOf< yarp::sig::PixelMono > *dest, const bool copyData = true) ;
151  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelMono > *src,
152  vpImage<unsigned char> & dest,const bool copyData = true ) ;
153 
154 
155  static void convert(const vpImage<vpRGBa> & src,
156  yarp::sig::ImageOf< yarp::sig::PixelRgba > *dest, const bool copyData = true) ;
157  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgba > *src,
158  vpImage<vpRGBa> & dest,const bool copyData = true) ;
159 
160  static void convert(const vpImage<vpRGBa> & src,
161  yarp::sig::ImageOf< yarp::sig::PixelRgb > *dest) ;
162  static void convert(const yarp::sig::ImageOf< yarp::sig::PixelRgb > *src,
163  vpImage<vpRGBa> & dest) ;
164 #endif
165 
166 #ifdef VISP_HAVE_LIBJPEG
167 #if JPEG_LIB_VERSION > 70
168  static void convertToJPEGBuffer(const vpImage<unsigned char> &src,
169  unsigned char **dest, long unsigned int &destSize, int quality = 100);
170 
171  static void convertToJPEGBuffer(unsigned char *src, long unsigned int srcSize,
172  vpImage<unsigned char> &dest);
173 #endif
174 #endif
175 
176  static void split(const vpImage<vpRGBa> &src,
180  vpImage<unsigned char>* pa = NULL) ;
181 
199  static inline void YUVToRGB(unsigned char y,
200  unsigned char u,
201  unsigned char v,
202  unsigned char &r,
203  unsigned char &g,
204  unsigned char &b)
205  {
206  double dr, dg, db;
207  dr = floor(0.9999695*y - 0.0009508*(u-128) + 1.1359061*(v-128));
208  dg = floor(0.9999695*y - 0.3959609*(u-128) - 0.5782955*(v-128));
209  db = floor(0.9999695*y + 2.04112*(u-128) - 0.0016314*(v-128));
210 
211  dr = dr < 0. ? 0. : dr;
212  dg = dg < 0. ? 0. : dg;
213  db = db < 0. ? 0. : db;
214  dr = dr > 255. ? 255. : dr;
215  dg = dg > 255. ? 255. : dg;
216  db = db > 255. ? 255. : db;
217 
218  r = (unsigned char) dr;
219  g = (unsigned char) dg;
220  b = (unsigned char) db;
221  };
222  static void YUYVToRGBa(unsigned char* yuyv, unsigned char* rgba,
223  unsigned int width, unsigned int height);
224  static void YUYVToRGB(unsigned char* yuyv, unsigned char* rgb,
225  unsigned int width, unsigned int height);
226  static void YUYVToGrey(unsigned char* yuyv, unsigned char* grey,
227  unsigned int size);
228  static void YUV411ToRGBa(unsigned char* yuv,
229  unsigned char* rgba, unsigned int size);
230  static void YUV411ToRGB(unsigned char* yuv,
231  unsigned char* rgb, unsigned int size);
232  static void YUV411ToGrey(unsigned char* yuv,
233  unsigned char* grey, unsigned int size);
234  static void YUV422ToRGBa(unsigned char* yuv,
235  unsigned char* rgba, unsigned int size);
236  static void YUV422ToRGB(unsigned char* yuv,
237  unsigned char* rgb, unsigned int size);
238  static void YUV422ToGrey(unsigned char* yuv,
239  unsigned char* grey, unsigned int size);
240  static void YUV420ToRGBa(unsigned char* yuv,
241  unsigned char* rgba, unsigned int width, unsigned int height);
242  static void YUV420ToRGB(unsigned char* yuv,
243  unsigned char* rgb, unsigned int width, unsigned int height);
244  static void YUV420ToGrey(unsigned char* yuv,
245  unsigned char* grey, unsigned int size);
246 
247  static void YUV444ToRGBa(unsigned char* yuv,
248  unsigned char* rgba, unsigned int size);
249  static void YUV444ToRGB(unsigned char* yuv,
250  unsigned char* rgb, unsigned int size);
251  static void YUV444ToGrey(unsigned char* yuv,
252  unsigned char* grey, unsigned int size);
253 
254  static void YV12ToRGBa(unsigned char* yuv,
255  unsigned char* rgba, unsigned int width, unsigned int height);
256  static void YV12ToRGB(unsigned char* yuv,
257  unsigned char* rgb, unsigned int width, unsigned int height);
258  static void YVU9ToRGBa(unsigned char* yuv,
259  unsigned char* rgba, unsigned int width, unsigned int height);
260  static void YVU9ToRGB(unsigned char* yuv,
261  unsigned char* rgb, unsigned int width, unsigned int height);
262  static void RGBToRGBa(unsigned char* rgb,
263  unsigned char* rgba, unsigned int size);
264  static void RGBaToRGB(unsigned char* rgba,
265  unsigned char* rgb, unsigned int size);
266 
267  static void RGBToGrey(unsigned char* rgb,
268  unsigned char* grey, unsigned int size);
269  static void RGBaToGrey(unsigned char* rgba,
270  unsigned char* grey, unsigned int size);
271 
272  static void RGBToRGBa(unsigned char * bgr, unsigned char * rgba,
273  unsigned int width, unsigned int height, bool flip = false);
274  static void RGBToGrey(unsigned char * bgr, unsigned char * grey,
275  unsigned int width, unsigned int height, bool flip = false);
276 
277  static void GreyToRGBa(unsigned char* grey,
278  unsigned char* rgba, unsigned int size);
279  static void GreyToRGB(unsigned char* grey,
280  unsigned char* rgb, unsigned int size);
281 
282  static void BGRToRGBa(unsigned char * bgr, unsigned char * rgba,
283  unsigned int width, unsigned int height, bool flip=false);
284 
285  static void BGRToGrey(unsigned char * bgr, unsigned char * grey,
286  unsigned int width, unsigned int height, bool flip=false);
287 
288  static void YCbCrToRGB(unsigned char *ycbcr, unsigned char *rgb,
289  unsigned int size);
290  static void YCbCrToRGBa (unsigned char *ycbcr, unsigned char *rgb,
291  unsigned int size);
292  static void YCrCbToRGB(unsigned char *ycbcr, unsigned char *rgb,
293  unsigned int size);
294  static void YCrCbToRGBa(unsigned char *ycbcr, unsigned char *rgb,
295  unsigned int size);
296  static void YCbCrToGrey(unsigned char *ycbcr, unsigned char *grey,
297  unsigned int size);
298  static void MONO16ToGrey(unsigned char *grey16, unsigned char *grey,
299  unsigned int size);
300  static void MONO16ToRGBa(unsigned char *grey16, unsigned char *rgba,
301  unsigned int size);
302 
303 private:
304  static void computeYCbCrLUT();
305 
306 private:
307  static bool YCbCrLUTcomputed;
308  static int vpCrr[256];
309  static int vpCgb[256];
310  static int vpCgr[256];
311  static int vpCbb[256];
312 
313 } ;
314 
315 #endif
316 
317 
318 /*
319 * Local variables:
320 * c-basic-offset: 2
321 * End:
322 */
static void YUVToRGB(unsigned char y, unsigned char u, unsigned char v, unsigned char &r, unsigned char &g, unsigned char &b)