Fawkes API  Fawkes Development Version
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
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, "YUY2") == 0) {
46  return YUY2;
47  } else if (strcmp(mode, "YVY2") == 0) {
48  return YVY2;
49  } else if (strcmp(mode, "BGR") == 0) {
50  return BGR;
51  } else if (strcmp(mode, "YUV422_PACKED") == 0) {
52  return YUV422_PACKED;
53  } else if (strcmp(mode, "YUV444_PACKED") == 0) {
54  return YUV444_PACKED;
55  } else if (strcmp(mode, "YVU444_PACKED") == 0) {
56  return YVU444_PACKED;
57  } else if (strcmp(mode, "YUV422_PLANAR") == 0) {
58  return YUV422_PLANAR;
59  } else if (strcmp(mode, "YUV422_PLANAR_QUARTER") == 0) {
60  return YUV422_PLANAR_QUARTER;
61  } else if (strcmp(mode, "GRAY8") == 0) {
62  return GRAY8;
63  } else if (strcmp(mode, "RGB_WITH_ALPHA") == 0) {
64  return RGB_WITH_ALPHA;
65  } else if (strcmp(mode, "BGR_WITH_ALPHA") == 0) {
66  return BGR_WITH_ALPHA;
67  } else if (strcmp(mode, "BAYER_MOSAIC_RGGB") == 0) {
68  return BAYER_MOSAIC_RGGB;
69  } else if (strcmp(mode, "BAYER_MOSAIC_GBRG") == 0) {
70  return BAYER_MOSAIC_GBRG;
71  } else if (strcmp(mode, "BAYER_MOSAIC_GRBG") == 0) {
72  return BAYER_MOSAIC_GRBG;
73  } else if (strcmp(mode, "BAYER_MOSAIC_BGGR") == 0) {
74  return BAYER_MOSAIC_BGGR;
75  } else if (strcmp(mode, "RAW16") == 0) {
76  return RAW16;
77  } else if (strcmp(mode, "CARTESIAN_3D_FLOAT") == 0) {
78  return CARTESIAN_3D_FLOAT;
79  } else if (strcmp(mode, "CARTESIAN_3D_DOUBLE") == 0) {
80  return CARTESIAN_3D_DOUBLE;
81  } else if (strcmp(mode, "CARTESIAN_3D_FLOAT_RGB") == 0) {
82  return CARTESIAN_3D_FLOAT_RGB;
83  } else {
84  return CS_UNKNOWN;
85  }
86 
87 }
88 
89 const char *
90 colorspace_to_string(colorspace_t colorspace)
91 {
92  switch (colorspace) {
93  case RGB:
94  return "RGB";
95  case YUV411_PACKED:
96  return "YUV411_PACKED";
97  case YUV411_PLANAR:
98  return "YUV411_PLANAR";
99  case YUY2:
100  return "YUY2";
101  case YVY2:
102  return "YVY2";
103  case BGR:
104  return "BGR";
105  case YUV422_PACKED:
106  return "YUV422_PACKED";
107  case YUV444_PACKED:
108  return "YUV444_PACKED";
109  case YVU444_PACKED:
110  return "YVU444_PACKED";
111  case YUV422_PLANAR:
112  return "YUV422_PLANAR";
113  case YUV422_PLANAR_QUARTER:
114  return "YUV422_PLANAR_QUARTER";
115  case GRAY8:
116  return "GRAY8";
117  case MONO8:
118  return "MONO8";
119  case RGB_WITH_ALPHA:
120  return "RGB_WITH_ALPHA";
121  case BGR_WITH_ALPHA:
122  return "BGR_WITH_ALPHA";
123  case BAYER_MOSAIC_RGGB:
124  return "BAYER_MOSAIC_RGGB";
125  case BAYER_MOSAIC_GBRG:
126  return "BAYER_MOSAIC_GBRG";
127  case BAYER_MOSAIC_GRBG:
128  return "BAYER_MOSAIC_GRBG";
129  case BAYER_MOSAIC_BGGR:
130  return "BAYER_MOSAIC_BGGR";
131  case RAW16:
132  return "RAW16";
133  case CARTESIAN_3D_FLOAT:
134  return "CARTESIAN_3D_FLOAT";
135  case CARTESIAN_3D_DOUBLE:
136  return "CARTESIAN_3D_DOUBLE";
137  case CARTESIAN_3D_FLOAT_RGB:
138  return "CARTESIAN_3D_FLOAT_RGB";
139  default:
140  return "CS_UNKNOWN";
141  }
142 }
143 
144 
145 unsigned char *
146 malloc_buffer(colorspace_t colorspace, unsigned int width, unsigned int height)
147 {
148  return (unsigned char *)malloc(colorspace_buffer_size(colorspace, width, height));
149 }
150 
151 size_t
152 colorspace_buffer_size(colorspace_t cspace, unsigned int width, unsigned int height)
153 {
154  switch (cspace) {
155  case RGB:
156  case BGR:
157  case YUV444_PACKED:
158  case YVU444_PACKED:
159  return (width * height * 3);
160 
161  case RGB_WITH_ALPHA:
162  case BGR_WITH_ALPHA:
163  return (width * height * 4);
164 
165  case YUV411_PACKED:
166  case YUV411_PLANAR:
167  return (width * height * 3 / 2);
168 
169  case YUY2:
170  case YVY2:
171  case YUV422_PACKED:
172  case YUV422_PLANAR:
173  return (width * height * 2);
174 
175  case MONO16:
176  case RAW16:
177  return (width * height * 2);
178 
179  case RAW8:
180  case GRAY8:
181  case MONO8:
182  case BAYER_MOSAIC_RGGB:
183  case BAYER_MOSAIC_GBRG:
184  case BAYER_MOSAIC_GRBG:
185  case BAYER_MOSAIC_BGGR:
186  return (width * height);
187 
188  case YUV422_PLANAR_QUARTER:
189  return (width * height) / 2;
190 
191  case CARTESIAN_3D_FLOAT:
192  return (3 * width * height * sizeof(float));
193 
194  case CARTESIAN_3D_DOUBLE:
195  return (3 * width * height * sizeof(double));
196 
197  case CARTESIAN_3D_FLOAT_RGB:
198  return (4 * width * height * sizeof(float));
199 
200  case CS_UNKNOWN:
201  case COLORSPACE_N:
202  return 0;
203  }
204 
205  return 0;
206 }
207 
208 } // end namespace firevision