Skip to content

Instantly share code, notes, and snippets.

@jeeb
Last active April 22, 2016 17:57
Show Gist options
  • Save jeeb/73760dde0dcad57d9284d65adf316a49 to your computer and use it in GitHub Desktop.
Save jeeb/73760dde0dcad57d9284d65adf316a49 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <math.h>
static double normalize(double input) {
if (input < 0.08145)
return input / 4.5;
else
return pow(((input + 0.0993) / 1.0993), (1.0/0.45));
}
static void YCBCR_TO_RGB(int y, int cb, int cr, int *r, int *g, int *b) {
double e_y = ((double)(y) / pow(2.0, 0.0) - 16.0) / 219.0;
double e_cb = ((double)(cb) / pow(2.0, 0.0) - 128.0) / 224.0;
double e_cr = ((double)(cr) / pow(2.0, 0.0) - 128.0) / 224.0;
printf("y=%f cb=%f cr=%f\n", (double)y, (double)cb, (double)cr);
printf("e_y=%f e_cb=%f, e_cr=%f\n", e_y, e_cb, e_cr);
double e_r = (e_y + 1.5747*e_cr);
double e_g = (e_y + (-0.1873*e_cb) + (-0.4682*e_cr));
double e_b = (e_y + 1.8556*e_cb);
printf("e_r=%f e_g=%f, e_b=%f\n"
"n_r=%f n_g=%f n_b=%f\n",
e_r * 255, e_g * 255, e_b * 255,
normalize(e_r) * 255, normalize(e_g) * 255, normalize(e_b) * 255);
}
int main() {
int r=1;
int g=2;
int b=3;
YCBCR_TO_RGB(180, 128, 128, &r, &g, &b);
YCBCR_TO_RGB(16, 128, 128, &r, &g, &b);
YCBCR_TO_RGB(145, 147, 44, &r, &g, &b);
printf("r=%d g=%d b=%d\n", r, g, b);
return 0;
}
@jeeb
Copy link
Author

jeeb commented Apr 22, 2016

Compiles with

gcc -Wall -Wextra -std=c99 -pedantic -lm -o hurr hurr.c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment