Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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 == RGB) && (to == YUV422_PACKED) ) {
90  rgb_to_yuv422packed_plainc(src, dst, width, height);
91  } else if ( (from == BGR) && (to == YUV422_PLANAR) ) {
92  bgr_to_yuv422planar_plainc(src, dst, width, height);
93  } else if ( (from == GRAY8) && (to == YUY2) ) {
94  gray8_to_yuy2(src, dst, width, height);
95  } else if ( (from == GRAY8) && (to == YUV422_PLANAR) ) {
96  gray8_to_yuv422planar_plainc(src, dst, width, height);
97  } else if ( (from == MONO8) && (to == YUV422_PLANAR) ) {
98  gray8_to_yuv422planar_plainc(src, dst, width, height);
99  } else if ( (from == YUV422_PLANAR) && (to == YUV422_PACKED) ) {
100  yuv422planar_to_yuv422packed(src, dst, width, height);
101  } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PACKED) ) {
102  yuv422planar_quarter_to_yuv422packed(src, dst, width, height);
103  } else if ( (from == YUV422_PLANAR_QUARTER) && (to == YUV422_PLANAR) ) {
104  yuv422planar_quarter_to_yuv422planar(src, dst, width, height);
105  } else if ( (from == YUV422_PLANAR) && (to == RGB) ) {
106  yuv422planar_to_rgb_plainc(src, dst, width, height);
107  } else if ( (from == YUV422_PACKED) && (to == RGB) ) {
108  yuv422packed_to_rgb_plainc(src, dst, width, height);
109  } else if ( (from == YUV422_PLANAR) && (to == BGR) ) {
110  yuv422planar_to_bgr_plainc(src, dst, width, height);
111  } else if ( (from == YUV422_PLANAR) && (to == RGB_WITH_ALPHA) ) {
112  yuv422planar_to_rgb_with_alpha_plainc(src, dst, width, height);
113  } else if ( (from == RGB) && (to == RGB_WITH_ALPHA) ) {
114  rgb_to_rgb_with_alpha_plainc(src, dst, width, height);
115  } else if ( (from == RGB) && (to == BGR_WITH_ALPHA) ) {
116  rgb_to_bgr_with_alpha_plainc(src, dst, width, height);
117  } else if ( (from == YUV422_PLANAR) && (to == BGR_WITH_ALPHA) ) {
118  yuv422planar_to_bgr_with_alpha_plainc(src, dst, width, height);
119  } else if ( (from == YUV422_PACKED) && (to == BGR_WITH_ALPHA) ) {
120  yuv422packed_to_bgr_with_alpha_plainc(src, dst, width, height);
121  } else if ( (from == BAYER_MOSAIC_GBRG) && (to == YUV422_PLANAR) ) {
122  bayerGBRG_to_yuv422planar_bilinear(src, dst, width, height);
123  } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) {
124  bayerGRBG_to_yuv422planar_nearest_neighbour(src, dst, width, height);
125  } else if ( (from == BAYER_MOSAIC_GRBG) && (to == YUV422_PLANAR) ) {
126  bayerGRBG_to_yuv422planar_bilinear(src, dst, width, height);
127  } else if ( (from == YUV444_PACKED) && (to == YUV422_PLANAR) ) {
128  yuv444packed_to_yuv422planar(src, dst, width, height);
129  } else if ( (from == YUV444_PACKED) && (to == YUV422_PACKED) ) {
130  yuv444packed_to_yuv422packed(src, dst, width, height);
131  } else if ( (from == YVU444_PACKED) && (to == YUV422_PLANAR) ) {
132  yvu444packed_to_yuv422planar(src, dst, width, height);
133  } else if ( (from == YVU444_PACKED) && (to == YUV422_PACKED) ) {
134  yvu444packed_to_yuv422packed(src, dst, width, height);
135  } else {
136  throw fawkes::Exception("Cannot convert image data from %s to %s",
137  colorspace_to_string(from),
138  colorspace_to_string(to));
139  }
140 }
141 
142 
143 inline void
144 grayscale(colorspace_t cspace,
145  unsigned char *src, unsigned char *dst,
146  unsigned int width, unsigned int height)
147 {
148  switch (cspace) {
149  case YUV422_PACKED:
150  grayscale_yuv422packed(src, dst, width, height);
151  break;
152  case YUV422_PLANAR:
153  grayscale_yuv422planar(src, dst, width, height);
154  break;
155  default:
156  fawkes::Exception e("FirevisionUtils: Cannot grayscale image. "
157  "Images from colorspace %s are not supported.",
158  colorspace_to_string(cspace));
159  throw e;
160  }
161 }
162 
163 } // end namespace firevision
164 
165 #endif