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