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 <cstdlib>
28 #include <cstring>
29 
30 namespace firevision {
31 
32 colorspace_t
33 colorspace_by_name(const char *mode)
34 {
35  if (strcmp(mode, "RGB") == 0) {
36  return RGB;
37  } else if (strcmp(mode, "YUV411_PACKED") == 0) {
38  return YUV411_PACKED;
39  } else if (strcmp(mode, "YUV411_PLANAR") == 0) {
40  return YUV411_PLANAR;
41  } else if (strcmp(mode, "YUV420_PLANAR") == 0) {
42  return YUV420_PLANAR;
43  } else if (strcmp(mode, "YUY2") == 0) {
44  return YUY2;
45  } else if (strcmp(mode, "YVY2") == 0) {
46  return YVY2;
47  } else if (strcmp(mode, "RGB_PLANAR") == 0) {
48  return RGB_PLANAR;
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 const char *
89 colorspace_to_string(colorspace_t colorspace)
90 {
91  switch (colorspace) {
92  case RGB: return "RGB";
93  case YUV411_PACKED: return "YUV411_PACKED";
94  case YUV411_PLANAR: return "YUV411_PLANAR";
95  case YUV420_PLANAR: return "YUV420_PLANAR";
96  case YUY2: return "YUY2";
97  case YVY2: return "YVY2";
98  case BGR: return "BGR";
99  case YUV422_PACKED: return "YUV422_PACKED";
100  case YUV444_PACKED: return "YUV444_PACKED";
101  case YVU444_PACKED: return "YVU444_PACKED";
102  case YUV422_PLANAR: return "YUV422_PLANAR";
103  case YUV422_PLANAR_QUARTER: return "YUV422_PLANAR_QUARTER";
104  case GRAY8: return "GRAY8";
105  case MONO8: return "MONO8";
106  case RGB_WITH_ALPHA: return "RGB_WITH_ALPHA";
107  case BGR_WITH_ALPHA: return "BGR_WITH_ALPHA";
108  case BAYER_MOSAIC_RGGB: return "BAYER_MOSAIC_RGGB";
109  case BAYER_MOSAIC_GBRG: return "BAYER_MOSAIC_GBRG";
110  case BAYER_MOSAIC_GRBG: return "BAYER_MOSAIC_GRBG";
111  case BAYER_MOSAIC_BGGR: return "BAYER_MOSAIC_BGGR";
112  case RAW16: return "RAW16";
113  case CARTESIAN_3D_FLOAT: return "CARTESIAN_3D_FLOAT";
114  case CARTESIAN_3D_DOUBLE: return "CARTESIAN_3D_DOUBLE";
115  case CARTESIAN_3D_FLOAT_RGB: return "CARTESIAN_3D_FLOAT_RGB";
116  case RGB_PLANAR: return "RGB_PLANAR";
117  default: return "CS_UNKNOWN";
118  }
119 }
120 
121 unsigned char *
122 malloc_buffer(colorspace_t colorspace, unsigned int width, unsigned int height)
123 {
124  return (unsigned char *)malloc(colorspace_buffer_size(colorspace, width, height));
125 }
126 
127 size_t
128 colorspace_buffer_size(colorspace_t cspace, unsigned int width, unsigned int height)
129 {
130  switch (cspace) {
131  case RGB:
132  case BGR:
133  case RGB_PLANAR:
134  case YUV444_PACKED:
135  case YVU444_PACKED: return (width * height * 3);
136 
137  case RGB_WITH_ALPHA:
138  case BGR_WITH_ALPHA: return (width * height * 4);
139 
140  case YUV411_PACKED:
141  case YUV411_PLANAR:
142  case YUV420_PLANAR: return (width * height * 3 / 2);
143 
144  case YUY2:
145  case YVY2:
146  case YUV422_PACKED:
147  case YUV422_PLANAR: return (width * height * 2);
148 
149  case MONO16:
150  case RAW16: return (width * height * 2);
151 
152  case RAW8:
153  case GRAY8:
154  case MONO8:
155  case BAYER_MOSAIC_RGGB:
156  case BAYER_MOSAIC_GBRG:
157  case BAYER_MOSAIC_GRBG:
158  case BAYER_MOSAIC_BGGR: return (width * height);
159 
160  case YUV422_PLANAR_QUARTER: return (width * height) / 2;
161 
162  case CARTESIAN_3D_FLOAT: return (3 * (size_t)width * height * sizeof(float));
163 
164  case CARTESIAN_3D_DOUBLE: return (3 * (size_t)width * height * sizeof(double));
165 
166  case CARTESIAN_3D_FLOAT_RGB: return (4 * (size_t)width * height * sizeof(float));
167 
168  case CS_UNKNOWN:
169  case COLORSPACE_N: return 0;
170  }
171 
172  return 0;
173 }
174 
175 } // end namespace firevision