Fawkes API  Fawkes Development Version
colorspaces.cpp
1 
2 /***************************************************************************
3  * colorspaces.cpp - This header defines utility functions to deal with
4  * color spaces
5  *
6  * Generated: Sat Aug 12 15:26:23 2006
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 #include <fvutils/color/colorspaces.h>
26 
27 #include <cstring>
28 #include <cstdlib>
29 
30 namespace firevision {
31 #if 0 /* just to make Emacs auto-indent happy */
32 }
33 #endif
34 
35 colorspace_t
36 colorspace_by_name(const char *mode)
37 {
38 
39  if (strcmp(mode, "RGB") == 0) {
40  return RGB;
41  } else if (strcmp(mode, "YUV411_PACKED") == 0) {
42  return YUV411_PACKED;
43  } else if (strcmp(mode, "YUV411_PLANAR") == 0) {
44  return YUV411_PLANAR;
45  } else if (strcmp(mode, "YUV420_PLANAR") == 0) {
46  return YUV420_PLANAR;
47  } else if (strcmp(mode, "YUY2") == 0) {
48  return YUY2;
49  } else if (strcmp(mode, "YVY2") == 0) {
50  return YVY2;
51  } else if (strcmp(mode, "RGB_PLANAR") == 0) {
52  return RGB_PLANAR;
53  } else if (strcmp(mode, "BGR") == 0) {
54  return BGR;
55  } else if (strcmp(mode, "YUV422_PACKED") == 0) {
56  return YUV422_PACKED;
57  } else if (strcmp(mode, "YUV444_PACKED") == 0) {
58  return YUV444_PACKED;
59  } else if (strcmp(mode, "YVU444_PACKED") == 0) {
60  return YVU444_PACKED;
61  } else if (strcmp(mode, "YUV422_PLANAR") == 0) {
62  return YUV422_PLANAR;
63  } else if (strcmp(mode, "YUV422_PLANAR_QUARTER") == 0) {
64  return YUV422_PLANAR_QUARTER;
65  } else if (strcmp(mode, "GRAY8") == 0) {
66  return GRAY8;
67  } else if (strcmp(mode, "RGB_WITH_ALPHA") == 0) {
68  return RGB_WITH_ALPHA;
69  } else if (strcmp(mode, "BGR_WITH_ALPHA") == 0) {
70  return BGR_WITH_ALPHA;
71  } else if (strcmp(mode, "BAYER_MOSAIC_RGGB") == 0) {
72  return BAYER_MOSAIC_RGGB;
73  } else if (strcmp(mode, "BAYER_MOSAIC_GBRG") == 0) {
74  return BAYER_MOSAIC_GBRG;
75  } else if (strcmp(mode, "BAYER_MOSAIC_GRBG") == 0) {
76  return BAYER_MOSAIC_GRBG;
77  } else if (strcmp(mode, "BAYER_MOSAIC_BGGR") == 0) {
78  return BAYER_MOSAIC_BGGR;
79  } else if (strcmp(mode, "RAW16") == 0) {
80  return RAW16;
81  } else if (strcmp(mode, "CARTESIAN_3D_FLOAT") == 0) {
82  return CARTESIAN_3D_FLOAT;
83  } else if (strcmp(mode, "CARTESIAN_3D_DOUBLE") == 0) {
84  return CARTESIAN_3D_DOUBLE;
85  } else if (strcmp(mode, "CARTESIAN_3D_FLOAT_RGB") == 0) {
86  return CARTESIAN_3D_FLOAT_RGB;
87  } else {
88  return CS_UNKNOWN;
89  }
90 
91 }
92 
93 const char *
94 colorspace_to_string(colorspace_t colorspace)
95 {
96  switch (colorspace) {
97  case RGB:
98  return "RGB";
99  case YUV411_PACKED:
100  return "YUV411_PACKED";
101  case YUV411_PLANAR:
102  return "YUV411_PLANAR";
103  case YUV420_PLANAR:
104  return "YUV420_PLANAR";
105  case YUY2:
106  return "YUY2";
107  case YVY2:
108  return "YVY2";
109  case BGR:
110  return "BGR";
111  case YUV422_PACKED:
112  return "YUV422_PACKED";
113  case YUV444_PACKED:
114  return "YUV444_PACKED";
115  case YVU444_PACKED:
116  return "YVU444_PACKED";
117  case YUV422_PLANAR:
118  return "YUV422_PLANAR";
119  case YUV422_PLANAR_QUARTER:
120  return "YUV422_PLANAR_QUARTER";
121  case GRAY8:
122  return "GRAY8";
123  case MONO8:
124  return "MONO8";
125  case RGB_WITH_ALPHA:
126  return "RGB_WITH_ALPHA";
127  case BGR_WITH_ALPHA:
128  return "BGR_WITH_ALPHA";
129  case BAYER_MOSAIC_RGGB:
130  return "BAYER_MOSAIC_RGGB";
131  case BAYER_MOSAIC_GBRG:
132  return "BAYER_MOSAIC_GBRG";
133  case BAYER_MOSAIC_GRBG:
134  return "BAYER_MOSAIC_GRBG";
135  case BAYER_MOSAIC_BGGR:
136  return "BAYER_MOSAIC_BGGR";
137  case RAW16:
138  return "RAW16";
139  case CARTESIAN_3D_FLOAT:
140  return "CARTESIAN_3D_FLOAT";
141  case CARTESIAN_3D_DOUBLE:
142  return "CARTESIAN_3D_DOUBLE";
143  case CARTESIAN_3D_FLOAT_RGB:
144  return "CARTESIAN_3D_FLOAT_RGB";
145  case RGB_PLANAR:
146  return "RGB_PLANAR";
147  default:
148  return "CS_UNKNOWN";
149  }
150 }
151 
152 
153 unsigned char *
154 malloc_buffer(colorspace_t colorspace, unsigned int width, unsigned int height)
155 {
156  return (unsigned char *)malloc(colorspace_buffer_size(colorspace, width, height));
157 }
158 
159 size_t
160 colorspace_buffer_size(colorspace_t cspace, unsigned int width, unsigned int height)
161 {
162  switch (cspace) {
163  case RGB:
164  case BGR:
165  case RGB_PLANAR:
166  case YUV444_PACKED:
167  case YVU444_PACKED:
168  return (width * height * 3);
169 
170  case RGB_WITH_ALPHA:
171  case BGR_WITH_ALPHA:
172  return (width * height * 4);
173 
174  case YUV411_PACKED:
175  case YUV411_PLANAR:
176  case YUV420_PLANAR:
177  return (width * height * 3 / 2);
178 
179  case YUY2:
180  case YVY2:
181  case YUV422_PACKED:
182  case YUV422_PLANAR:
183  return (width * height * 2);
184 
185  case MONO16:
186  case RAW16:
187  return (width * height * 2);
188 
189  case RAW8:
190  case GRAY8:
191  case MONO8:
192  case BAYER_MOSAIC_RGGB:
193  case BAYER_MOSAIC_GBRG:
194  case BAYER_MOSAIC_GRBG:
195  case BAYER_MOSAIC_BGGR:
196  return (width * height);
197 
198  case YUV422_PLANAR_QUARTER:
199  return (width * height) / 2;
200 
201  case CARTESIAN_3D_FLOAT:
202  return (3 * width * height * sizeof(float));
203 
204  case CARTESIAN_3D_DOUBLE:
205  return (3 * width * height * sizeof(double));
206 
207  case CARTESIAN_3D_FLOAT_RGB:
208  return (4 * width * height * sizeof(float));
209 
210  case CS_UNKNOWN:
211  case COLORSPACE_N:
212  return 0;
213  }
214 
215  return 0;
216 }
217 
218 } // end namespace firevision