Wednesday, June 20, 2012

YIQ - RGB Conversion Algorithms in C

In order to understand this article you should probably read this article first:

If you are curious about the theory behind the HSI and RGB color spaces, you can read this articles:
YIQ color space
RGB color space

1.RGB to YIQ Conversion

This function will create a YIQ model from a RGB model. The algorithm behind the conversion is described by the formulas below:
RGB To YIQ Algorithm
The meaning of the variables:
R,G,B - the components of the RGB model (red, green, blue)
Y,I,Q - the components of YIQ model (luma, in-phase, quadrature)

The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the the in-phase (I) should have values in the range [-0.5957 ,0.5957] and the quadrature should have values in the range [-0.5226, 0.5226].

The prototype of the function is:
/* Description:
 *  Creates a YiqColor structure from RGB components
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the YiqColor is the parameters are
 * correct. Otherwise returns NULL.
 */
YiqColor* Yiq_CreateFromRgbF(double r, double g, double b);
The function can be implemented as:
YiqColor* Yiq_CreateFromRgbF(double r, double g, double b)
{
    YiqColor* color = NULL;
    if (RgbF_IsValid(r, g, b) == true)
    {
        color = Yiq_Create(0.0, 0.0, 0.0);
        color->Y = 0.299900 * r + 0.587000 * b + 0.114000 * b;
        color->I = 0.595716 * r - 0.274453 * b - 0.321264 * b;
        color->Q = 0.211456 * r - 0.522591 * b + 0.311350 * b;
    }
    return color;
}
2.YIQ to RGB Conversion
This function will create a RGB model from a YIQ model. The algorithm behind the conversion is described by the formulas below:
YIQ to RGB Algorithm
The meaning of the variables:
R,G,B - the components of the RGB model (red, green, blue)
Y,I,Q - the components of YIQ model (luma, in-phase, quadrature)

The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the the in-phase (I) should have values in the range [-0.5957 ,0.5957] and the quadrature should have values in the range [-0.5226, 0.5226].

The prototype of the function is:
/*
 * Description:
 *  Creates a RgbFColor structure from YIQ components
 * Parameters:
 *  y,i,q - the components of an YIQ model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the RgbFColor is the parameters are
 * correct. Otherwise returns NULL.
 */
RgbFColor* RgbF_CreateFromYiq(double y, double i, double q);
The function can be implemented as:
RgbFColor* RgbF_CreateFromYiq(double y, double i, double q)
{
    RgbFColor *color = NULL;
    if (Yiq_IsValid(y, i, q) == true)
    {
        color = RgbF_Create(0.0, 0.0, 0.0);
        color->R = y + 0.9563 * i + 0.6210 * q;
        color->G = y - 0.2721 * i - 0.6474 * q;
        color->B = y - 1.1070 * i + 1.7046 * q;
    }
    return color;
}
3.Example
#include<stdio.h>
#include"colorspace.h"

int main(int argc, char** argv)
{
    YiqColor* yiq = NULL;
    RgbFColor* rgbF = NULL;
    RgbIColor* rgbI = NULL;

    /*YIQ to RGB*/
    yiq = Yiq_Create(0.4, 0.1, -0.11);
    rgbF = RgbF_CreateFromYiq(yiq->Y, yiq->I, yiq->Q);
    rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B);
    printf("\nYIQ : %f %f %f", yiq->Y, yiq->I, yiq->Q);
    printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B);
    printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B);

    /*Frees the resources*/
    free(yiq);
    free(rgbF);
    free(rgbI);

    /*RGB to YIQ*/
    rgbI = RgbI_Create(108U, 198U, 78U);
    rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B);
    yiq = Yiq_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B);
    printf("\nYIQ : %f %f %f", yiq->Y, yiq->I, yiq->Q);
    printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B);
    printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B);
    return 0;
}
/*
 YIQ : 0.400000 0.100000 -0.110000
 RGBf : 0.427320 0.444004 0.101794
 RGBi : 109 113 26
 YIQ : 0.341440 0.070084 0.024943
 RGBf : 0.423529 0.776471 0.305882
 RGBi : 108 198 78
 */

No comments:

Post a Comment

Got a question regarding something in the article? Leave me a comment and I will get back at you as soon as I can!

Related Posts Plugin for WordPress, Blogger...
Recommended Post Slide Out For Blogger