Fawkes API  Fawkes Development Version
conversions.h
1 
2 /***************************************************************************
3  * conversions.h - Conversions
4  *
5  * Created: Sat Aug 12 15:19:31 2006
6  * based on colorspaces.h from Tue Feb 23 13:49:38 2005
7  * Copyright 2005-2006 Tim Niemueller [www.niemueller.de]
8  *
9  ****************************************************************************/
10 
11 /* This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version. A runtime exception applies to
15  * this software (see LICENSE.GPL_WRE file mentioned below for details).
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  * GNU Library General Public License for more details.
21  *
22  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
23  */
24 
25 #ifndef __FIREVISION_UTILS_COLOR_CONVERSIONS_H
26 #define __FIREVISION_UTILS_COLOR_CONVERSIONS_H
27 
28 #include <fvutils/color/yuv.h>
29 #include <fvutils/color/rgb.h>
30 #include <fvutils/color/yuvrgb.h>
31 #include <fvutils/color/rgbyuv.h>
32 #include <fvutils/color/bayer.h>
33 #include <fvutils/color/colorspaces.h>
34 
35 #include <core/exception.h>
36 #include <cstring>
37 
38 namespace firevision {
39 #if 0 /* just to make Emacs auto-indent happy */
40 }
41 #endif
42 
43 /** Convert image from one colorspace to another.
44  * This is a convenience method for unified access to all conversion routines
45  * available in FireVision.
46  * @param from colorspace of the src buffer
47  * @param to colorspace to convert to
48  * @param src source buffer
49  * @param dst destination buffer
50  * @param width width of image in pixels
51  * @param height height of image in pixels
52  * @exception Exception thrown, if the desired conversion combination is not
53  * available.
54  */
55 inline void
56 convert(colorspace_t from, colorspace_t to,
57  const unsigned char *src, unsigned char *dst,
58  unsigned int width, unsigned int height)
59 {
60  if (from == to) {
61  if ( src != dst ) {
62  memcpy(dst, src, colorspace_buffer_size(from, width, height));
63  }
64  } else if ( (from == YUV422_PACKED) && (to == YUV422_PLANAR) ) {
65  yuv422packed_to_yuv422planar(src, dst, width, height);
66  } else if ( (from == YUY2) && (to == YUV422_PLANAR_QUARTER) ) {
67  yuy2_to_yuv422planar_quarter(src, dst, width, height);
68  } else if ( (from == YUY2) && (to == YUV422_PLANAR) ) {
69  yuy2_to_yuv422planar(src, dst, width, height);
70  } else if ( (from == YVY2) && (to == YUV422_PLANAR) ) {
71  yvy2_to_yuv422planar(src, dst, width, height);
72 
73 #if ( \
74  defined __i386__ || \
75  defined __386__ || \
76  defined __X86__ || \
77  defined _M_IX86 || \
78  defined i386 \
79  )
80  } else if ( (from == YUV411_PLANAR) && (to == RGB) ) {
81  yuv411planar_to_rgb_mmx(src, dst, width, height);
82 #endif
83  } else if ( (from == BGR) && (to == RGB) ) {
84  bgr_to_rgb_plainc(src, dst, width, height);
85  } else if ( (from == RGB) && (to == YUV411_PACKED) ) {
86  rgb_to_yuv411packed_plainc(src, dst, width, height);
87  } else if ( (from == RGB) && (to == YUV422_PLANAR) ) {
88  rgb_to_yuv422planar_plainc(src, dst, width, height);
89  } else if ( (from == YUV420_PLANAR) && (to == YUV422_PLANAR) ) {
90  yuv420planar_to_yuv422planar(src, dst, width, height);
91  } else if ( (from == RGB) && (to == YUV422_PACKED) ) {
92  rgb_to_yuv422packed_plainc(src, dst, width, height);
93  } else if ( (from == RGB_PLANAR) && (to == YUV422_PACKED) ) {
94  rgb_planar_to_yuv422packed_plainc(src, dst, width, height);
95  } else if ( (from == RGB) && (to == RGB_PLANAR) ) {
96  rgb_to_rgb_planar_plainc(src, dst, width, height);
97  } else if ( (from == RGB_PLANAR) && (to == RGB) ) {
98  rgb_planar_to_rgb_plainc(src, dst, width, height);
99  } else if ( (from == BGR) && (to == YUV422_PLANAR) ) {
100  bgr_to_yuv422planar_plainc(src, dst, width, height);
101  } else if ( (from == GRAY8) && (to == YUY2) ) {
102  gray8_to_yuy2(src, dst, width, height);
103  } else if ( (from == GRAY8) && (to == YUV422_PLANAR) ) {
104  gray8_to_yuv422planar_plainc(src, dst, width, height);
105  } else if ( (from == MONO8) && (to == YUV422_PLANAR) ) {
106  gray8_to_yuv422planar_plainc(src, dst, width, height);
107  } else if ( (from == MONO8) && (to == YUV422_PACKED) ) {
108  gray8_to_yuv422packed_plainc(src, dst, width, height);
109  } else if ( (from == MONO8) && (to == RGB) ) {
110  gray8_to_rgb_plainc(src, dst, width, height);
111  } else if ( (from == YUV422_PLANAR) && (to == YUV422_PACKED) ) {
112  yuv422planar_to_yuv422packed(src, dst, width, height);
113  } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PACKED) ) {
114  yuv422planar_quarter_to_yuv422packed(src, dst, width, height);
115  } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PLANAR) ) {
116  yuv422planar_quarter_to_yuv422planar(src, dst, width, height);
117  } else if ( (from == YUV422_PLANAR) && (to == RGB) ) {
118  yuv422planar_to_rgb_plainc(src, dst, width, height);
119  } else if ( (from == YUV422_PACKED) && (to == RGB) ) {
120  yuv422packed_to_rgb_plainc(src, dst, width, height);
121  } else if ( (from == YUV422_PLANAR) && (to == BGR) ) {
122  yuv422planar_to_bgr_plainc(src, dst, width, height);
123  } else if ( (from == YUV422_PLANAR) && (to == RGB_WITH_ALPHA) ) {
124  yuv422planar_to_rgb_with_alpha_plainc(src, dst, width, height);
125  } else if ( (from == RGB) && (to == RGB_WITH_ALPHA) ) {
126  rgb_to_rgb_with_alpha_plainc(src, dst, width, height);
127  } else if ( (from == RGB) && (to == BGR_WITH_ALPHA) ) {
128  rgb_to_bgr_with_alpha_plainc(src, dst, width, height);
129  } else if ( (from == YUV422_PLANAR) && (to == BGR_WITH_ALPHA) ) {
130  yuv422planar_to_bgr_with_alpha_plainc(src, dst, width, height);
131  } else if ( (from == YUV422_PACKED) && (to == BGR_WITH_ALPHA) ) {
132  yuv422packed_to_bgr_with_alpha_plainc(src, dst, width, height);
133  } else if ( (from == BAYER_MOSAIC_GBRG) && (to == YUV422_PLANAR) ) {
134  bayerGBRG_to_yuv422planar_bilinear(src, dst, width, height);
135  } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) {
136  bayerGRBG_to_yuv422planar_nearest_neighbour(src, dst, width, height);
137  } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) {
138  bayerGRBG_to_yuv422planar_bilinear(src, dst, width, height);
139  } else if ( (from == YUV444_PACKED) && (to == YUV422_PLANAR) ) {
140  yuv444packed_to_yuv422planar(src, dst, width, height);
141  } else if ( (from == YUV444_PACKED) && (to == YUV422_PACKED) ) {
142  yuv444packed_to_yuv422packed(src, dst, width, height);
143  } else if ( (from == YVU444_PACKED) && (to == YUV422_PLANAR) ) {
144  yvu444packed_to_yuv422planar(src, dst, width, height);
145  } else if ( (from == YVU444_PACKED) && (to == YUV422_PACKED) ) {
146  yvu444packed_to_yuv422packed(src, dst, width, height);
147  } else {
148  throw fawkes::Exception("Cannot convert image data from %s to %s",
149  colorspace_to_string(from),
150  colorspace_to_string(to));
151  }
152 }
153 
154 
155 inline void
156 grayscale(colorspace_t cspace,
157  unsigned char *src, unsigned char *dst,
158  unsigned int width, unsigned int height)
159 {
160  switch (cspace) {
161  case YUV422_PACKED:
162  grayscale_yuv422packed(src, dst, width, height);
163  break;
164  case YUV422_PLANAR:
165  grayscale_yuv422planar(src, dst, width, height);
166  break;
167  default:
168  fawkes::Exception e("FirevisionUtils: Cannot grayscale image. "
169  "Images from colorspace %s are not supported.",
170  colorspace_to_string(cspace));
171  throw e;
172  }
173 }
174 
175 } // end namespace firevision
176 
177 #endif
Base class for exceptions in Fawkes.
Definition: exception.h:36