Skip to content

Instantly share code, notes, and snippets.

@GaProgMan
Created February 3, 2013 09:53

Revisions

  1. GaProgMan created this gist Feb 3, 2013.
    134 changes: 134 additions & 0 deletions yuv.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,134 @@
    /*
    * Project Name: RGB to Y'UV
    * Solution Name: RGB to Y'UV
    * Original creation date: 19/11/2011
    * Edit date: 18/01/2013
    * Programmer name: Jamie Taylor (aka "GaProgMan")
    * File name: YUV.c
    *
    * Purpose of the project:
    * I was reading through a wikipedia article on
    * the differences between YUV and RGB and found it
    * very interesting.
    * For those who don't know, YUV is a colour
    * space - a way of storing colour for digital images,
    * film and TV. It focuses on storing Luma
    * (brightness - as Y') and two chrominance (colour
    * - as U and V) values.
    * Most video and still images are stored in
    * this format, as it takes into account the Human
    * eye's ability to perceive differences in brightness
    * at higher definition than differences in colour. This
    * greatly reduces the data bandwidth required to send
    * and receive images and film in this format.
    * This short program explores the mathematical
    * formulae used to convert colour data from the RGB
    * colour space to the Y'UV colour space.
    * For an in-depth description Y'UV, RGB and
    * the formulae used, please see the following
    * website:
    * http://en.wikipedia.org/wiki/YUV
    *
    * Formulae used:
    * To convert from RGB to Y'UV, the following
    * will be used:
    * Y' = W[r]R + W[g]G + W[b]B
    * U = U[max] * (B - Y') / (1 - W[b])
    * V = V[max] * (R - Y') / (1 - W[r])
    * Where:
    * W[r] = 0.299
    * W[b] = 0.114
    * W[g] = 1 - W[r] - W[b] = 0.587
    * U[max] = 0.436
    * V[max] = 0.615
    * Note:- I have placed square brackes around
    * the letters in the formulae that are written in
    * subscript. This is to make it easier for the
    * reader.
    *
    * GNU Copyright information
    * Copyright 2011 Jamie Taylor <jamie@taylorj.org.uk>
    *
    * This program is free software; you can redistribute
    * it and/or modify it under the terms of the GNU General
    * Public License as published by the Free Software
    * Foundation; either version 2 of the License, or (at
    * your option) any later version.
    *
    * This program is distributed in the hope that it will
    * be useful, but WITHOUT ANY WARRANTY; without even the
    * implied warranty of MERCHANTABILITY or FITNESS FOR A
    * PARTICULAR PURPOSE. See the GNU General Public
    * License for more details.
    *
    * You should have received a copy of the GNU General
    * Public License along with this program; if not, write
    * to the Free Software Foundation, Inc., 51 Franklin
    * Street, Fifth Floor, Boston, MA 02110-1301, USA.
    */

    /* included libraries */
    #include <stdio.h>

    /* Constants that will be used for RGB to Y'UV */
    #define WEIGHTEDRED 0.299
    #define WEIGHTEDGREEN 0.587
    #define WEIGHTEDBLUE 0.114
    #define UMAX 0.436
    #define VMAX 0.615

    /* function declarations */
    void convertFromRGB ();

    /* global variables */
    float redValue;
    float greenValue;
    float blueValue;
    float yPrime;
    float uValue;
    float vValue;

    int main () {

    /* read values for RGB into memory */
    printf("Please input a value for RED:");
    scanf("%f", &redValue);
    printf("Please input a value for GREEN:");
    scanf("%f", &greenValue);
    printf("Please input a value for BLUE:");
    scanf("%f", &blueValue);

    /* convert RGB to Y'UV */
    convertFromRGB();

    /*display results */
    printf("\nFor the RGB input (%.2f, %.2f, %.2f),\n",
    redValue, greenValue, blueValue);
    printf("The following Y'UV is outputted (%.2f, %.2f, %.2f)",
    yPrime, uValue, vValue);
    return 0;
    }

    void convertFromRGB (){
    /*
    * implement formula:
    * Y' = W[r]R + W[g]G + W[b]B
    */
    yPrime = (WEIGHTEDRED * redValue) +
    (WEIGHTEDGREEN * greenValue) +
    (WEIGHTEDBLUE * blueValue);

    /*
    * implement formula:
    * U = U[max] * (B - Y') / (1 - W[b])
    */
    uValue = UMAX * ((blueValue - yPrime)
    / (1 - WEIGHTEDBLUE));

    /*
    * implement formula:
    * V = V[max] * (R - Y') / (1 - W[r])
    */
    vValue = VMAX * ((redValue - yPrime)
    / (1 - WEIGHTEDRED));
    }