Hello There, Guest!
View New Posts  |  View Today's Posts
RGB to YCbCr Conversions

  • 0 Vote(s) - 0 Average


02-27-2016, 10:27 PM #1
AceInfinity
Developer
*******
Administrators
Posts: 9,733 Threads:1,026 Joined: Jun 2011 Reputation: 76

RGB to YCbCr Conversions
Code:
#ifndef COLOR_FORMATS
#define COLOR_FORMATS

#include <stdint.h>

typedef struct RGB_color_format_
{
   uint8_t R;
   uint8_t G;
   uint8_t B;
} RGB_color_format;

typedef struct YCbCr_color_format_
{
   uint8_t Y;
   uint8_t Cb;
   uint8_t Cr;
} YCbCr_color_format;

/* Translates RGB to YCbCr using color channel values */
void RGB_to_YCbCr_cc(uint8_t R, uint8_t G, uint8_t B, YCbCr_color_format *YCbCr)
{
   YCbCr->Y = (uint8_t)((0.299 * R) + (0.587 * G) + (0.114 * B));
   YCbCr->Cb = (uint8_t)((0.1687 * R) - (0.3313 * G) + (0.5 * B) + 128);
   YCbCr->Cr = (uint8_t)((0.5 * R) - (0.4187 * G) - (0.0813 * B) + 128);
}

/* Translates RGB to YCbCr using color format */
void RGB_to_YCbCr_cf(RGB_color_format RGB, YCbCr_color_format *YCbCr)
{
   RGB_to_YCbCr_cc(RGB.R, RGB.G, RGB.B, YCbCr);
}

/* Translates YCbCr to RGB using color channel values */
void YCbCr_to_RGB_cc(uint8_t Y, uint8_t Cb, uint8_t Cr, RGB_color_format *RGB)
{
   RGB->R = (uint8_t)(Y + (1.402 * (Cr - 128)));
   RGB->G = (uint8_t)(Y - (0.34414 * (Cb - 128)) - (0.71414 * (Cr - 128)));
   RGB->B = (uint8_t)(Y + (1.772 * (Cb - 128)));
}

/* Translates YCbCr to RGB using color format */
void YCbCr_to_RGB_cf(YCbCr_color_format YCbCr, RGB_color_format *RGB)
{
   YCbCr_to_RGB_cc(YCbCr.Y, YCbCr.Cb, YCbCr.Cr, RGB);
}


#endif /* COLOR_FORMATS */

Some helper functions to convert between RGB and YCbCr formats.


Microsoft MVP .NET Programming - (2012 - Present)
®Crestron DMC-T Certified Automation Programmer

Development Site: aceinfinity.net

 ▲
 ▲ ▲




Forum Jump:


Possibly Related Threads...
Thread Author Replies Views Last Post
   Base Conversions AceInfinity 2 1,415 04-02-2013, 02:47 AM
Last Post: AceInfinity


Users browsing this thread: 1 Guest(s)