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