Fawkes API  Fawkes Development Version
rgb.cpp
1 
2 /***************************************************************************
3  * rgb.h - RGB specific methods, macros and constants
4  *
5  * Created: Sat Aug 12 14:59:55 2006
6  * based on colorspaces.h from Tue Feb 23 13:49:38 2005
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/rgb.h>
26 
27 namespace firevision {
28 
29 /** Convert RGB to RGB with alpha values.
30  * This is plain C code without special optimizations.
31  * @param rgb RGB source buffer
32  * @param rgb_alpha RGB with alpha destination buffer
33  * @param width width in pixels
34  * @param height height in pixels
35  */
36 void
37 rgb_to_rgb_with_alpha_plainc(const unsigned char *rgb,
38  unsigned char * rgb_alpha,
39  unsigned int width,
40  unsigned int height)
41 {
42  for (unsigned int i = 0; i < width * height; ++i) {
43  *rgb_alpha++ = *rgb++;
44  *rgb_alpha++ = *rgb++;
45  *rgb_alpha++ = *rgb++;
46  *rgb_alpha++ = 255;
47  }
48 }
49 
50 /** Convert RGB to planar RGB.
51  * This is plain C code without special optimizations.
52  * @param rgb RGB source buffer
53  * @param rgb_planar planar RGB buffer
54  * @param width width in pixels
55  * @param height height in pixels
56  */
57 void
58 rgb_to_rgb_planar_plainc(const unsigned char *rgb,
59  unsigned char * rgb_planar,
60  const unsigned int width,
61  const unsigned int height)
62 {
63  unsigned char *r = rgb_planar;
64  unsigned char *g = rgb_planar + (width * height);
65  unsigned char *b = rgb_planar + (width * height * 2);
66  for (unsigned int i = 0; i < width * height; ++i) {
67  *r++ = *rgb++;
68  *g++ = *rgb++;
69  *b++ = *rgb++;
70  }
71 }
72 
73 /** Convert RGB to planar RGB.
74  * This is plain C code without special optimizations.
75  * @param rgb RGB source buffer
76  * @param rgb_planar planar RGB buffer
77  * @param width width in pixels
78  * @param height height in pixels
79  */
80 void
81 rgb_planar_to_rgb_plainc(const unsigned char *rgb_planar,
82  unsigned char * rgb,
83  const unsigned int width,
84  const unsigned int height)
85 {
86  const unsigned char *r = rgb_planar;
87  const unsigned char *g = rgb_planar + (width * height);
88  const unsigned char *b = rgb_planar + (width * height * 2);
89  for (unsigned int i = 0; i < width * height; ++i) {
90  *rgb++ = *r++;
91  *rgb++ = *g++;
92  *rgb++ = *b++;
93  }
94 }
95 
96 /** Convert RGB to BGR with alpha values.
97  * This is plain C code without special optimizations.
98  * @param rgb RGB source buffer
99  * @param bgr_alpha BGR with alpha values destination buffer
100  * @param width width in pixels
101  * @param height height in pixels
102  */
103 void
104 rgb_to_bgr_with_alpha_plainc(const unsigned char *rgb,
105  unsigned char * bgr_alpha,
106  unsigned int width,
107  unsigned int height)
108 {
109  for (unsigned int i = 0; i < width * height; ++i) {
110  *bgr_alpha++ = rgb[2];
111  *bgr_alpha++ = rgb[1];
112  *bgr_alpha++ = rgb[0];
113  *bgr_alpha++ = 255;
114  rgb += 3;
115  }
116 }
117 
118 /** Convert BGR to RGB
119  * This is plain C code without special optimizations.
120  * @param bgr BGR source buffer
121  * @param rgb RGB destination buffer
122  * @param width width in pixels
123  * @param height height in pixels
124  */
125 void
126 bgr_to_rgb_plainc(const unsigned char *BGR,
127  unsigned char * RGB,
128  unsigned int width,
129  unsigned int height)
130 {
131  RGB_t *rgb;
132  BGR_t *bgr;
133  for (unsigned int i = 0; i < (width * height); ++i) {
134  bgr = (BGR_t *)BGR;
135  rgb = (RGB_t *)RGB;
136  rgb->R = bgr->R;
137  rgb->G = bgr->G;
138  rgb->B = bgr->B;
139  BGR += 3;
140  RGB += 3;
141  }
142 }
143 
144 /* Convert a line of a BGR buffer to a line in a planar RGB buffer, see above for general
145  * notes about color space conversion from RGB to BGR
146  * @param RGB where the RGB output will be written to, will have pixel after pixel, 3 bytes per pixel
147  * (thus this is a 24bit RGB with one byte per color) line by line.
148  * @param BGR unsigned char array that contains the pixels, 4 pixels in 6 byte macro pixel, line after
149  * line
150  * @param width Width of the image contained in the YUV buffer
151  * @param height Height of the image contained in the YUV buffer
152  * @param rgb_line the index of the line to be converted
153  * @param yuv_line the index of the line to convert to in the YUV buffer
154  */
155 
156 void
157 convert_line_bgr_rgb(const unsigned char *BGR,
158  unsigned char * RGB,
159  unsigned int width,
160  unsigned int height)
161 {
162  unsigned int i = 0;
163  const unsigned char *r1, *r2, *r3;
164  unsigned char * n1, *n2, *n3;
165 
166  while (i < width) {
167  n1 = RGB++;
168  n2 = RGB++;
169  n3 = RGB++;
170 
171  r1 = BGR++;
172  r2 = BGR++;
173  r3 = BGR++;
174 
175  *n1 = *r3;
176  *n2 = *r2;
177  *n3 = *r1;
178 
179  i += 1;
180  }
181 }
182 
183 /** Convert one channel gray images to RGB.
184  * This is plain C code without special optimizations.
185  * @param mono8 mono source buffer
186  * @param rgb RGB destination buffer
187  * @param width width in pixels
188  * @param height height in pixels
189  */
190 void
191 gray8_to_rgb_plainc(const unsigned char *mono8,
192  unsigned char * rgb,
193  unsigned int width,
194  unsigned int height)
195 {
196  for (unsigned int i = 0; i < width * height; ++i) {
197  *rgb++ = *mono8;
198  *rgb++ = *mono8;
199  *rgb++ = *mono8++;
200  }
201 }
202 
203 } // end namespace firevision