Showing posts with label Image Processing. Show all posts
Showing posts with label Image Processing. Show all posts

Thursday, June 21, 2012

YUV - 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:

1.RGB to YUV Conversion

This function will create a YUV model from a RGB model. The algorithm behind the conversion is described by the formulas below:
RGB to YUV algorithm
The meaning of the variables:
R,G,B - the components of the RGB model (red, green, blue)
Y,U,V - the components of YIQ model (luma, chrominance components)

The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the U component should have values in the range [-0.436 ,0.436] and the V component should have values in the range [-0.615, 0.615].


The prototype of the function is:
/* Description:
 *  Creates a YuvColor structure from RGB components
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the YuvColor is the parameters are
 * correct. Otherwise returns NULL.
 */
YuvColor* Yuv_CreateFromRgbF(double r, double g, double f);
The function can be implemented as:
YuvColor* Yuv_CreateFromRgbF(double r, double g, double b)
{
    YuvColor* color = NULL;
    if (RgbF_IsValid(r, g, b) == true)
    {
        color = Yuv_Create(0.0, 0.0, 0.0);
        color->Y = 0.299 * r + 0.587 * b + 0.114 * b;
        color->U = 0.492 * (b - color->Y);
        color->V = 0.877 * (r - color->Y);
    }
    return color;
}
2.YUV to RGB Conversion
This function will create a RGB model from a YUV model. The algorithm behind the conversion is described by the formulas below:
YUV to RGB algorithm
The meaning of the variables:
R,G,B - the components of the RGB model (red, green, blue)
Y,U,V - the components of YIQ model (luma, chrominance components)

The components of the RGB model (r,g,b) and luma should have values in the range [0,1], while the U component should have values in the range [-0.436 ,0.436] and the V component should have values in the range [-0.615, 0.615].

The prototype of the function is:
/*
 * Description:
 *  Creates a RgbFColor structure from YUV components
 * Parameters:
 *  y,u,v - the components of an YUV model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the RgbFColor is the parameters are
 * correct. Otherwise returns NULL.
 */
RgbFColor* RgbF_CreateFromYuv(double y, double u, double v);
The function can be implemented as:
RgbFColor* RgbF_CreateFromYuv(double y, double u, double v)
{
    RgbFColor *color = NULL;
    if (Yiq_IsValid(y, u, v) == true)
    {
        color = RgbF_Create(0.0, 0.0, 0.0);
        color->R = y + 1.140 * v;
        color->G = y - 0.395 * u - 0.581 * v;
        color->B = y + 2.032 * u;
    }
    return color;
}
3.Exemplu
#include<stdio.h>
#include"colorspace.h"

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

    /*YUV to RGB*/
    yuv = Yuv_Create(0.4, 0.1, -0.11);
    rgbF = RgbF_CreateFromYuv(yuv->Y, yuv->U, yuv->V);
    rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B);
    printf("\nYIQ : %f %f %f", yuv->Y, yuv->U, yuv->V);
    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(yuv);
    free(rgbF);
    free(rgbI);

    /*RGB to YUV*/
    rgbI = RgbI_Create(108U, 198U, 78U);
    rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B);
    yuv = Yuv_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B);
    printf("\nYIQ : %f %f %f", yuv->Y, yuv->U, yuv->V);
    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.274600 0.424410 0.603200
 RGBi : 70 108 154
 YIQ : 0.341059 -0.017307 0.072327
 RGBf : 0.423529 0.776471 0.305882
 RGBi : 108 198 78
 */

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
 */

Sunday, June 17, 2012

HSV - 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 HSV and RGB color spaces, you can read this articles:

1. RGB to HSV Conversion 
This function will create a HSV model from a RGB model. The algorithm behind the conversion is described by the formulas below.
RGB to HSV algorithm
The meaning of the variables:
M - the RGB component with the greatest value
m - the RGB component with the smalles value
c - chroma
r,g,b - the components of the RGB model (red, green, blue)
h,s,v - the components of HSV model (hue, saturation, value)

The components of the RGB model (r,g,b), saturation (s)  and value (v) should have values in the range [0,1], while the hue (h) should have values in the rnage [0,360].

The prototype of the function is:
/* Description:
 *  Creates a HsvColor structure from RGB components
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the HsvColor is the parameters are
 * correct. Otherwise returns NULL.
 */
HsvColor* Hsv_CreateFromRgbF(double r, double g, double b);
The function can be implemented as:
HsvColor* Hsv_CreateFromRgbF(double r, double g, double b)
{
    double M = 0.0, m = 0.0, c = 0.0;
    HsvColor* color = NULL;
    if (RgbF_IsValid(r, g, b) == true)
    {
        color = Hsv_Create(0.0, 0.0, 0.0);
        M = Double_GetMaximum(r, g, b);
        m = Double_GetMinimum(r, g, b);
        c = M - m;
        color->V = M;
        if (c != 0.0f)
        {
            if (M == r)
            {
                color->H = fmod(((g - b) / c), 6.0);
            }
            else if (M == g)
            {
                color->H = (b - r) / c + 2.0;
            }
            else /*if(M==b)*/
            {
                color->H = (r - g) / c + 4.0;
            }
            color->H *= 60.0;
            color->S = c / color->V;
        }
    }
    return color;
}
2.HSV to RGB Conversion
This function will create a RGB model from a HSV model. The algorithm behind the conversion is described by the formulas below.
HSV to RGB algorithm
The meaning of the variables:
c - chroma
m - the RGB component with the smalles value
x - an intermediate value used for computing the RGB model
r,g,b - the components of the RGB model (red, green, blue)
h,s,v - the components of HSV model (hue, saturation, value)

The components of the RGB model (r,g,b), saturation (s)  and value (v) should have values in the range [0,1], while the hue (h) should have values in the rnage [0,360].


The prototype of the function is:
/*
 * Description:
 *  Creates a RgbFColor structure from HSV components
 * Parameters:
 *  h,s,v - the components of an HSV model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the RgbFColor is the parameters are
 * correct. Otherwise returns NULL.
 */
RgbFColor* RgbF_CreateFromHsv(double h, double s, double v);
The function can be implemented as:
HsvColor* Hsv_CreateFromRgbF(double r, double g, double b)
{
    double M = 0.0, m = 0.0, c = 0.0;
    HsvColor* color = NULL;
    if (RgbF_IsValid(r, g, b) == true)
    {
        color = Hsv_Create(0.0, 0.0, 0.0);
        M = Double_GetMaximum(r, g, b);
        m = Double_GetMinimum(r, g, b);
        c = M - m;
        color->V = M;
        if (c != 0.0f)
        {
            if (M == r)
            {
                color->H = fmod(((g - b) / c), 6.0);
            }
            else if (M == g)
            {
                color->H = (b - r) / c + 2.0;
            }
            else /*if(M==b)*/
            {
                color->H = (r - g) / c + 4.0;
            }
            color->H *= 60.0;
            color->S = c / color->V;
        }
    }
    return color;
}

The function can be implemented as:
RgbFColor* RgbF_CreateFromHsv(double h, double s, double v)
{
    double c = 0.0, m = 0.0, x = 0.0;
    RgbFColor *color = NULL;
    if (Hsv_IsValid(h, s, v) == true)
    {
        c = v * s;
        x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
        m = v - c;
        if (h >= 0.0 && h < 60.0)
        {
            color = RgbF_Create(c + m, x + m, m);
        }
        else if (h >= 60.0 && h < 120.0)
        {
            color = RgbF_Create(x + m, c + m, m);
        }
        else if (h >= 120.0 && h < 180.0)
        {
            color = RgbF_Create(m, c + m, x + m);
        }
        else if (h >= 180.0 && h < 240.0)
        {
            color = RgbF_Create(m, x + m, c + m);
        }
        else if (h >= 240.0 && h < 300.0)
        {
            color = RgbF_Create(x + m, m, c + m);
        }
        else if (h >= 300.0 && h < 360.0)
        {
            color = RgbF_Create(c + m, m, x + m);
        }
        else
        {
            color = RgbF_Create(m, m, m);
        }
    }
    return color;
}
3.Example
#include<stdio.h>
#include"colorspace.h"

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

    /*HSI to RGB*/
    hsv = Hsv_Create(240.5, 0.316, 0.721);
    rgbF = RgbF_CreateFromHsv(hsv->H, hsv->S, hsv->V);
    rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSV : %f %f %f", hsv->H, hsv->S, hsv->V);
    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(hsv);
    free(rgbF);
    free(rgbI);

    /*RGB to HSI*/
    rgbI = RgbI_Create(108U, 198U, 78U);
    rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B);
    hsv = Hsv_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSV : %f %f %f", hsv->H, hsv->S, hsv->V);
    printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B);
    printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B);
    return 0;
}
/*Output:
HSV : 240.500000 0.316000 0.721000
RGBf : 0.495063 0.493164 0.721000
RGBi : 126 126 184
HSV : 105.000000 0.606061 0.776471
RGBf : 0.423529 0.776471 0.305882
RGBi : 108 198 78
 */

HSL - 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 HSL and RGB color spaces, you should read this articles:

1.RGB To HSL Conversion
This function will create a HSL model from a RGB model. The algorithm behind the conversion is described by the formulas below.
RGB to HSL algorithm
The meaning of the variables:
M - the RGB component with the greatest value
m - the RGB component with the smalles value
c - chroma
r,g,b - the components of the RGB model (red, green, blue)
h,s,l - the components of HSL model (hue, saturation, lightness)

The components of the RGB model (r,g,b), saturation (s)  and intensity (i) should have values in the range [0,1], while the hue (h) should have values in the rnage [0,360].

The prototype of the function is:
/* Description:
 *  Creates a HslColor structure from RGB components
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the HslColor is the parameters are
 * correct. Otherwise returns NULL.
 */
HslColor* Hsl_CreateFromRgbF(double r, double g, double b);
The function can be implemented as:
HslColor* Hsl_CreateFromRgbF(double r, double g, double b)
{
    double M = 0.0, m = 0.0, c = 0.0;
    HslColor* color = NULL;
    if (RgbF_IsValid(r, g, b) == true)
    {
        M = Double_GetMaximum(r, g, b);
        m = Double_GetMinimum(r, g, b);
        c = M - m;
        color = Hsl_Create(0.0, 0.0, 0.0);
        color->L = 0.5 * (M + m);
        if (c != 0.0)
        {
            if (M == r)
            {
                color->H = fmod(((g - b) / c), 6.0);
            }
            else if (M == g)
            {
                color->H = ((b - r) / c) + 2.0;
            }
            else/*if(M==b)*/
            {
                color->H = ((r - g) / c) + 4.0;
            }
            color->H *= 60.0;
            color->S = c / (1.0 - fabs(2.0 * color->L - 1.0));
        }
    }
    return color;
}
2.HSL to RGB Conversion
This function will create a RGB model from an HSL model. The algorithm behind the conversion is described by the formulas below.
HSL to RGB algorithm
The meaning of the variables:
c - chroma
x - intermediate value used for computing r,g,b
m - intermediate value used for creating the r,g,b baseline
r,g,b - the components of the RGB model (red, green, blue)
h,s,l - the components of HSL model (hue, saturation, lightness)

The components of the RGB model (r,g,b), saturation (s)  and intensity (i) should have values in the range [0,1], while the hue (h) should have values in the rnage [0,360].

The prototype of the function is:
/*
 * Description:
 *  Creates a RgbFColor structure from HSL components
 * Parameters:
 *  h,s,l - the components of an HSL model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the RgbFColor is the parameters are
 * correct. Otherwise returns NULL.
 */
RgbFColor* RgbF_CreateFromHsl(double h, double s, double l);
The function can be implemented as:
RgbFColor* RgbF_CreateFromHsl(double h, double s, double l)
{
    RgbFColor* color = NULL;
    double c = 0.0, m = 0.0, x = 0.0;
    if (Hsl_IsValid(h, s, l) == true)
    {
        c = (1.0 - fabs(2 * l - 1.0)) * s;
        m = 1.0 * (l - 0.5 * c);
        x = c * (1.0 - fabs(fmod(h / 60.0, 2) - 1.0));
        if (h >= 0.0 && h < (HUE_UPPER_LIMIT / 6.0))
        {
            color = RgbF_Create(c + m, x + m, m);
        }
        else if (h >= (HUE_UPPER_LIMIT / 6.0) && h < (HUE_UPPER_LIMIT / 3.0))
        {
            color = RgbF_Create(x + m, c + m, m);
        }
        else if (h < (HUE_UPPER_LIMIT / 3.0) && h < (HUE_UPPER_LIMIT / 2.0))
        {
            color = RgbF_Create(m, c + m, x + m);
        }
        else if (h >= (HUE_UPPER_LIMIT / 2.0)
                && h < (2.0f * HUE_UPPER_LIMIT / 3.0))
        {
            color = RgbF_Create(m, x + m, c + m);
        }
        else if (h >= (2.0 * HUE_UPPER_LIMIT / 3.0)
                && h < (5.0 * HUE_UPPER_LIMIT / 6.0))
        {
            color = RgbF_Create(x + m, m, c + m);
        }
        else if (h >= (5.0 * HUE_UPPER_LIMIT / 6.0) && h < HUE_UPPER_LIMIT)
        {
            color = RgbF_Create(c + m, m, x + m);
        }
        else
        {
            color = RgbF_Create(m, m, m);
        }
    }
    return color;
}
3.Example
#include<stdio.h>
#include"colorspace.h"

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

    /*HSI to RGB*/
    hsl = Hsl_Create(84.0, 1.0, 0.4);
    rgbF = RgbF_CreateFromHsl(hsl->H, hsl->S, hsl->L);
    rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSL : %f %f %f", hsl->H, hsl->S, hsl->L);
    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(hsl);
    free(rgbF);
    free(rgbI);

    /*RGB to HSI*/
    rgbI = RgbI_Create(108U, 198U, 78U);
    rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B);
    hsl = Hsl_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSL : %f %f %f", hsl->H, hsl->S, hsl->L);
    printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B);
    printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B);
    return 0;
}
/*
HSL : 84.000000 1.000000 0.400000
RGBf : 0.480000 0.800000 0.000000
RGBi : 122 204 0
HSL : 105.000000 0.512821 0.541176
RGBf : 0.423529 0.776471 0.305882
RGBi : 108 198 78
 */

Sunday, February 19, 2012

HSI - 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:

1.RGB to HSI Conversion

This function will create a HSI model from a RGB model. The algorithm behind the conversion is described by the formulas below:
RGB to HSI conversion formulas
The meaning of the variables:
M - the RGB component with the greatest value
m - the RGB component with the smalles value
c - chroma
rf,gf,bf - the components of the RGB model (red, green, blue)
h,s,i - the components of HSL model (hue, saturation, intensity)

The components of the RGB model (r,g,b), saturation (S)  and intensity (I) should have values in the range [0,1], while the hue (h) should have values in the range [0,360].

The prototype of the function is:
/* Description:
 *  Creates a HsiColor structure from RGB components
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the HsiColor is the parameters are
 * correct. Otherwise returns NULL.
 */
HsiColor* Hsi_CreateFromRgbF(double r, double g, double b);
The function can be implemented as:
HsiColor* Hsi_CreateFromRgbF(double r, double g, double b)
{
    HsiColor* color = NULL;
    double m = Double_GetMinimum(r,g,b);
    double M = Double_GetMaximum(r,g,b);
    double c = M-m;
    if(RgbF_IsValid(r,g,b)==true)
    {
        color = (HsiColor*)malloc(sizeof(HsiColor));
        color->I = (1.0/3.0)*(r+g+b);
        if(c==0)
        {
            color->H = 0.0;
            color->S = 0.0;
        }
        else
        {
            if(M==r)
            {
                color->H = fmod(((g-b)/c), 6.0);
            }
            else if(M==g)
            {
                color->H = (b-r)/c + 2.0;
            }
            else if(M==b)
            {
                color->H = (r-g)/c + 4.0;
            }
            color->H *=60.0;
            color->S = 1.0 - (m/color->I);
        }
    }
    return color;
}
2.HSI to RGB Conversion 
This function will create a RGB model from a HSI model. The algorithm behind the conversion is described by the formulas below:
HSI to RGB conversion formulas
The meaning of the variables:
h, s, i - the components of the HSI model (hue, saturation, intensity)
hr - the hue component expressed in radians
k - the correcting coefficient for hr
x,y,z - intermediary values
x',y',z' - intensity-compensated intermediary values
Rf, Gf, Bf - the components of the RGB model (red, green blue)

The components of the RGB model (r,g,b), saturation (S) and intensity (I) should have values in the range [0,1], while the hue (h) should have values in the range [0,360].

The prototype of the function is:
/*
 * Description:
 *  Creates a RgbFColor structure from HSI components
 * Parameters:
 *  h,s,i - the components of an HSI model expressed
 *          as real numbers
 * Returns:
 *  A pointer to the RgbFColor is the parameters are
 * correct. Otherwise returns NULL.
 */
RgbFColor* RgbF_CreateFromHsi(double h, double s, double i);
The function can be implemented as:
RgbFColor* RgbF_CreateFromHsi(double h, double s, double i)
{
    RgbFColor* color = NULL;
    if(Hsi_IsValid(h,s,i)==true)
    {
        color = RgbF_Create(0.0,0.0,0.0);
        if(h>=0.0 && h<=(HUE_UPPER_LIMIT/3.0))
        {
            color->B = (1.0/3.0)*(1.0-s);
            color->R = (1.0/3.0)*((s*cos(h))/cos(60.0-h));
            color->G = 1.0-(color->B + color->R);
        }
        else if(h>(HUE_UPPER_LIMIT/3.0) && h<=(2.0*HUE_UPPER_LIMIT/3.0))
        {
            h-=(HUE_UPPER_LIMIT/3.0);
            color->R = (1.0/3.0)*(1.0-s);
            color->G = (1.0/3.0)*((s*cos(h))/cos(60.0-h));
            color->B = 1.0-(color->G + color->R);

        }
        else /* h>240 h<360 */
        {
            h-=(2.0*HUE_UPPER_LIMIT/3.0);
            color->G = (1.0/3.0)*(1.0-s);
            color->B = (1.0/3.0)*((s*cos(h))/cos(60.0-h));
            color->R = 1.0-(color->G + color->B);
        }
    }
    return color;
}
3. Example
#include<stdio.h>
#include"colorspace.h"

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

    /*HSI to RGB*/
    hsi = Hsi_Create(30.0, 0.3, 0.2);
    rgbF = RgbF_CreateFromHsi(hsi->H, hsi->S, hsi->I);
    rgbI = RgbI_CreateFromRealForm(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSI : %f %f %f", hsi->H, hsi->S, hsi->I);
    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(hsi);
    free(rgbF);
    free(rgbI);

    /*RGB to HSI*/
    rgbI = RgbI_Create(34U, 50U, 98U);
    rgbF = RgbF_CreateFromIntegerForm(rgbI->R, rgbI->G, rgbI->B);
    hsi = Hsi_CreateFromRgbF(rgbF->R, rgbF->G, rgbF->B);
    printf("\nHSI : %f %f %f", hsi->H, hsi->S, hsi->I);
    printf("\nRGBf : %f %f %f", rgbF->R, rgbF->G, rgbF->B);
    printf("\nRGBi : %d %d %d", rgbI->R, rgbI->G, rgbI->B);
    return 0;
}
/*
 HSI : 30.000000 0.300000 0.200000
 RGBf : 0.100000 0.666667 0.233333
 RGBi : 26 170 60
 HSI : 225.000000 0.439560 0.237909
 RGBf : 0.133333 0.196078 0.384314
 RGBi : 34 50 98
 */

The Colorspace Conversions Library

The purpose of this library is to provide functions and data structures for color space conversion operations.

In order operate more easily with the components of the color spaces we shall define every color space as a structure.
Color space Structure Members
RGB RgbFColor double R,G,B
RGB RgbIColor uint8_t R,G,B
HSI HsiColor double H,S,I
HSL HslColor double H,S,L
HSV HsvColor double H,S,V
YIQ YiqColor double Y,I,Q
YUV YuvColor double Y,U,V

The RgbIColor is associated with the RGB color space model and it will contain the R,G,B components expressed as natural numbers in the range [0,255].

The RbgFColor is associated with the RGB color space model and it will contain the R,G,B components expressed as real numbers in the range [0.0, 1.0].

The HslColor is associated with the HSL color space model and it will contain the H,S,L components expressed as real numbers where H is in the range [0.0, 360.0] and S and L are in the range [0.0, 1.0].

The HsvColor is associated with the HSV color space model and it will contain the H,S,V components expressed as real numbers where H is in the range [0.0, 360.0] and S and V are in the range [0.0, 1.0].

The HsiColor is associated with the HSI color space model and it will contain the H,S,I components expressed as real numbers where H is in the range [0.0, 360.0] and S and I are in the range [0.0, 1.0].

The YiqColor is associated with the YIQ color space model and it will contain the Y,I,Q components expressed as real numbers where Y is in the range [0.0, 1.0], I is in the range [-0.5957, 0.5957], Q is in the range [-0.5226, 0.5226].

The YuvColor is associated with the YUV color space model and it will contain the Y,U,V components expressed as real numbers where Y is in the range [0.0, 1.0], U is in the range [-0.436, 0.436], Q is in the range [-0.615, 0.615].

We shall also define the following functions which will be used for the conversion algorithms:
/*
 * Description:
 *  Checks if a value is within a specified interval
 * Parameters:
 *  value - the value who is checked
 *  lowerLimit - the lower limit of the interval
 *  upperLimit - the upper limit of the interval
 * Returns:
 *  true if the value is within the interval
 *  false if the value is not within the interval
 */
bool RealIsWithinBounds(double value, double lowerLimit, double upperLimit);
/*
 * Description:
 *  Checks if a value is within a specified interval
 * Parameters:
 *  value - the value who is checked
 *  lowerLimit - the lower limit of the interval
 *  upperLimit - the upper limit of the interval
 * Returns:
 *  true if the value is within the interval
 *  false if the value is not within the interval
 */
bool IntegerIsWithinBounds(uint8_t value, uint8_t lowerLimit, uint8_t upperLimit);
/*
 * Description:
 *  Returns the smallest of the three parameters
 * Parameters
 *  r,g,b - 3 real numbers
 * Returns
 *  The smallest real number from the set {r,g,b}
 */
double Double_GetMinimum(double r, double g, double b);
/*
 * Description:
 *  Returns the largest of the three parameters
 * Parameters
 *  r,g,b - 3 real numbers
 * Returns
 *  The largest real number from the set {r,g,b}
 */
double Double_GetMaximum(double r, double g, double b);

/*
 * Description:
 *  Checks if the RGB components are valid
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool RgbF_IsValid(double r, double g, double b);
/*
 * Description:
 *  Checks if the HSI components are valid
 * Parameters:
 *  h,s,i - the components of an HSI model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool Hsi_IsValid(double h, double s, double i);
/*
 * Description:
 *  Checks if the RGB components are valid
 * Parameters:
 *  r,g,b - the components of an RGB model expressed
 *          as natural numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool RgbI_IsValid(uint8_t r, uint8_t g, uint8_t b);
/*
 * Description:
 *  Checks if the HSL components are valid
 * Parameters:
 *  h,s,l - the components of an HSL model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool Hsl_IsValid(double h, double s, double l);
/*
 * Description:
 *  Checks if the HSV components are valid
 * Parameters:
 *  h,s,v - the components of an HSV model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool Hsv_IsValid(double h, double s, double v);
/*
 * Description:
 *  Checks if the YIQ components are valid
 * Parameters:
 *  y,i,q - the components of an YIQ model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool Yiq_IsValid(double y, double i, double q);
/*
 * Description:
 *  Checks if the YUV components are valid
 * Parameters:
 *  y,u,v - the components of an YUV model expressed
 *          as real numbers
 * Returns:
 *  true if the values are correct
 *  false if the values are incorrect
 */
bool Yuv_IsValid(double y, double u, double v);
If you want to view the implementation of these functions, see the links below:

Related Posts Plugin for WordPress, Blogger...