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 BGR with alpha values.
53  * This is plain C code without special optimizations.
54  * @param rgb RGB source buffer
55  * @param bgr_alpha BGR with alpha values destination buffer
56  * @param width width in pixels
57  * @param height height in pixels
58  */
59 void
60 rgb_to_bgr_with_alpha_plainc(const unsigned char *rgb, unsigned char *bgr_alpha,
61  unsigned int width, unsigned int height)
62 {
63  for ( unsigned int i = 0; i < width * height; ++i) {
64  *bgr_alpha++ = rgb[2];
65  *bgr_alpha++ = rgb[1];
66  *bgr_alpha++ = rgb[0];
67  *bgr_alpha++ = 255;
68  rgb += 3;
69  }
70 }
71 
72 
73 /** Convert BGR to RGB
74  * This is plain C code without special optimizations.
75  * @param bgr BGR source buffer
76  * @param rgb RGB destination buffer
77  * @param width width in pixels
78  * @param height height in pixels
79  */
80 void
81 bgr_to_rgb_plainc(const unsigned char *BGR, unsigned char *RGB,
82  unsigned int width, unsigned int height)
83 {
84  RGB_t *rgb;
85  BGR_t *bgr;
86  for (register unsigned int i = 0; i < (width * height); ++i) {
87  bgr = (BGR_t *)BGR;
88  rgb = (RGB_t *)RGB;
89  rgb->R = bgr->R;
90  rgb->G = bgr->G;
91  rgb->B = bgr->B;
92  BGR += 3;
93  RGB += 3;
94  }
95 }
96 
97 /* Convert a line of a BGR buffer to a line in a planar RGB buffer, see above for general
98  * notes about color space conversion from RGB to BGR
99  * @param RGB where the RGB output will be written to, will have pixel after pixel, 3 bytes per pixel
100  * (thus this is a 24bit RGB with one byte per color) line by line.
101  * @param BGR unsigned char array that contains the pixels, 4 pixels in 6 byte macro pixel, line after
102  * line
103  * @param width Width of the image contained in the YUV buffer
104  * @param height Height of the image contained in the YUV buffer
105  * @param rgb_line the index of the line to be converted
106  * @param yuv_line the index of the line to convert to in the YUV buffer
107  */
108 
109 void convert_line_bgr_rgb(const unsigned char *BGR, unsigned char *RGB,
110  unsigned int width, unsigned int height)
111  {
112  register unsigned int i = 0;
113  register const unsigned char *r1, *r2, *r3;
114  register unsigned char *n1, *n2, *n3;
115 
116  while( i < width ) {
117 
118  n1 = RGB++;
119  n2 = RGB++;
120  n3 = RGB++;
121 
122  r1 = BGR++;
123  r2 = BGR++;
124  r3 = BGR++;
125 
126  *n1 = *r3;
127  *n2 = *r2;
128  *n3 = *r1;
129 
130 
131  i += 1;
132  }
133 }
134 
135 } // end namespace firevision