Created
July 8, 2010 00:48
-
-
Save djg/467502 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Another test. RGB->YCoCg conversion. Think I discovered a problem with negative | |
// values being returned on RGB->YCoCg->RGB. | |
#include <stdio.h> | |
struct IVect { int x, y, z; }; | |
/* | |
* [Y ] = [ 1/4 1/2 1/4][R] R, G, B = [0..255] => Y = [0..255], Co, Cg = [-128..127] | |
* [Co] [ 1/2 0 -1/2][G] | |
* [Cg] [-1/4 1/2 -1/4][B] | |
*/ | |
IVect | |
RGB_to_YCoCg(const IVect& rgb) | |
{ | |
IVect ycocg; | |
// int s = (rgb.x >> 2) + (rgb.z >> 2); | |
int s = (rgb.x + rgb.z) >> 2; | |
int t = rgb.y >> 1; | |
ycocg.x = t + s; | |
ycocg.z = t - s; | |
// ycocg.y = (rgb.x >> 1) - (rgb.z >> 1); | |
ycocg.y = (rgb.x - rgb.z) >> 1; | |
return ycocg; | |
} | |
IVect | |
YCoCg_to_RGB(const IVect& ycocg) | |
{ | |
IVect rgb; | |
int t = ycocg.x - ycocg.z; | |
rgb.x = t + ycocg.y; | |
rgb.y = ycocg.x + ycocg.z; | |
rgb.z = t - ycocg.y; | |
return rgb; | |
} | |
int | |
main() | |
{ | |
IVect colors[] = | |
{ | |
{ 0, 0, 0 }, | |
{ 255, 0, 0 }, | |
{ 0, 255, 0 }, | |
{ 255, 255, 0 }, | |
{ 0, 0, 255 }, | |
{ 255, 0, 255 }, | |
{ 0, 255, 255 }, | |
{ 255, 255, 255 }, | |
{ 255, 127, 0 }, | |
{ 127, 255, 0 }, | |
{ 255, 0, 127 }, | |
{ 127, 0, 255 }, | |
{ 0, 255, 127 }, | |
{ 0, 127, 255 }, | |
}; | |
for (int i = 0; i < 14; ++i) | |
{ | |
const IVect t1 = RGB_to_YCoCg(colors[i]); | |
const IVect t2 = YCoCg_to_RGB(t1); | |
printf("{R=%3d, G=%3d, B=%3d} => {Y=%3d, Co=%4d, Cg=%4d} => {R=%3d, G=%3d, B=%3d}\n", | |
colors[i].x, colors[i].y, colors[i].z, t1.x, t1.y, t1.z, t2.x, t2.y, t2.z); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment