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