rgb.cpp

00001
00002 /***************************************************************************
00003  *  rgb.h - RGB specific methods, macros and constants
00004  *
00005  *  Created: Sat Aug 12 14:59:55 2006
00006  *  based on colorspaces.h from Tue Feb 23 13:49:38 2005
00007  *  Copyright  2005-2006  Tim Niemueller [www.niemueller.de]
00008  *
00009  ****************************************************************************/
00010
00011 /*  This program is free software; you can redistribute it and/or modify
00012  *  it under the terms of the GNU General Public License as published by
00013  *  the Free Software Foundation; either version 2 of the License, or
00014  *  (at your option) any later version. A runtime exception applies to
00015  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00016  *
00017  *  This program is distributed in the hope that it will be useful,
00018  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020  *  GNU Library General Public License for more details.
00021  *
00022  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00023  */
00024
00025 #include <fvutils/color/rgb.h>
00026 
00027 /** Convert RGB to RGB with alpha values.
00028  * This is plain C code without special optimizations.
00029  * @param rgb RGB source buffer
00030  * @param rgb_alpha RGB with alpha destination buffer
00031  * @param width width in pixels
00032  * @param height height in pixels
00033  */
00034 void
00035 rgb_to_rgb_with_alpha_plainc(const unsigned char *rgb, unsigned char *rgb_alpha,
00036                              unsigned int width, unsigned int height)
00037 {
00038   for ( unsigned int i = 0; i < width * height; ++i) {
00039     *rgb_alpha++ = *rgb++;
00040     *rgb_alpha++ = *rgb++;
00041     *rgb_alpha++ = *rgb++;
00042     *rgb_alpha++ = 255;
00043   }
00044 }
00045
00046 
00047 /** Convert RGB to BGR with alpha values.
00048  * This is plain C code without special optimizations.
00049  * @param rgb RGB source buffer
00050  * @param bgr_alpha BGR with alpha values destination buffer
00051  * @param width width in pixels
00052  * @param height height in pixels
00053  */
00054 void
00055 rgb_to_bgr_with_alpha_plainc(const unsigned char *rgb, unsigned char *bgr_alpha,
00056                              unsigned int width, unsigned int height)
00057 {
00058   for ( unsigned int i = 0; i < width * height; ++i) {
00059     *bgr_alpha++ = rgb[2];
00060     *bgr_alpha++ = rgb[1];
00061     *bgr_alpha++ = rgb[0];
00062     *bgr_alpha++ = 255;
00063     rgb += 3;
00064   }
00065 }
00066
00067 
00068 /** Convert BGR to RGB
00069  * This is plain C code without special optimizations.
00070  * @param bgr BGR source buffer
00071  * @param rgb RGB destination buffer
00072  * @param width width in pixels
00073  * @param height height in pixels
00074  */
00075 void
00076 bgr_to_rgb_plainc(const unsigned char *BGR, unsigned char *RGB,
00077                   unsigned int width, unsigned int height)
00078 {
00079   RGB_t *rgb;
00080   BGR_t *bgr;
00081   for (register unsigned int i = 0; i < (width * height); ++i) {
00082     bgr = (BGR_t *)BGR;
00083     rgb = (RGB_t *)RGB;
00084     rgb->R = bgr->R;
00085     rgb->G = bgr->G;
00086     rgb->B = bgr->B;
00087     BGR += 3;
00088     RGB += 3;
00089   }
00090 }
00091
00092 /* Convert a line of a BGR buffer to a line in a planar RGB buffer, see above for general
00093  * notes about color space conversion from RGB to BGR
00094  * @param RGB where the RGB output will be written to, will have pixel after pixel, 3 bytes per pixel
00095  *            (thus this is a 24bit RGB with one byte per color) line by line.
00096  * @param BGR unsigned char array that contains the pixels, 4 pixels in 6 byte macro pixel, line after
00097  *            line
00098  * @param width Width of the image contained in the YUV buffer
00099  * @param height Height of the image contained in the YUV buffer
00100  * @param rgb_line the index of the line to be converted
00101  * @param yuv_line the index of the line to convert to in the YUV buffer
00102  */
00103
00104 void convert_line_bgr_rgb(const unsigned char *BGR, unsigned char *RGB,
00105                             unsigned int width, unsigned int height)
00106  {
00107   register unsigned int i = 0;
00108   register const unsigned char *r1, *r2, *r3;
00109   register unsigned char *n1, *n2, *n3;
00110
00111   while( i < width ) {
00112
00113     n1 = RGB++;
00114     n2 = RGB++;
00115     n3 = RGB++;
00116
00117     r1 = BGR++;
00118     r2 = BGR++;
00119     r3 = BGR++;
00120
00121     *n1 = *r3;
00122     *n2 = *r2;
00123     *n3 = *r1;
00124
00125
00126     i += 1;
00127   }
00128 }
00129
00130
00131
00132