| 1 |
float ycbcrCoeffs[3] = {.299, .587, .114}; |
| 2 |
/* default coding range is CCIR Rec 601-1 with no headroom/footroom */ |
| 3 |
unsigned long refBlackWhite[6] = {0, 255, 128, 255, 128, 255}; |
| 4 |
|
| 5 |
#define LumaRed ycbcrCoeffs[0] |
| 6 |
#define LumaGreen ycbcrCoeffs[1] |
| 7 |
#define LumaBlue ycbcrCoeffs[2] |
| 8 |
|
| 9 |
long eRtotal = 0; |
| 10 |
long eGtotal = 0; |
| 11 |
long eBtotal = 0; |
| 12 |
long preveRtotal = 0; |
| 13 |
long preveGtotal = 0; |
| 14 |
long preveBtotal = 0; |
| 15 |
unsigned long AbseRtotal = 0; |
| 16 |
unsigned long AbseGtotal = 0; |
| 17 |
unsigned long AbseBtotal = 0; |
| 18 |
unsigned long eCodes = 0; |
| 19 |
unsigned long preveCodes = 0; |
| 20 |
unsigned long eBits = 0; |
| 21 |
unsigned long preveBits = 0; |
| 22 |
|
| 23 |
static void setupLumaTables(); |
| 24 |
static int abs(int v) { return (v < 0 ? -v : v); } |
| 25 |
static double pct(int v, double range) { return (v * 100. / range); } |
| 26 |
static void check(int R, int G, int B); |
| 27 |
|
| 28 |
float D1, D2; |
| 29 |
float D3, D4; |
| 30 |
float D5, D6; |
| 31 |
|
| 32 |
int main(int argc, char **argv) |
| 33 |
{ |
| 34 |
int R, G, B; |
| 35 |
|
| 36 |
if (argc > 1) |
| 37 |
{ |
| 38 |
refBlackWhite[0] = 16; |
| 39 |
refBlackWhite[1] = 235; |
| 40 |
refBlackWhite[2] = 128; |
| 41 |
refBlackWhite[3] = 240; |
| 42 |
refBlackWhite[4] = 128; |
| 43 |
refBlackWhite[5] = 240; |
| 44 |
} |
| 45 |
D3 = 2 - 2 * LumaRed; |
| 46 |
D4 = 2 - 2 * LumaBlue; |
| 47 |
D1 = 1. / D3; |
| 48 |
D2 = 1. / D4; |
| 49 |
D5 = D3 * LumaRed / LumaGreen; |
| 50 |
D6 = D4 * LumaBlue / LumaGreen; |
| 51 |
setupLumaTables(); |
| 52 |
for (R = 0; R < 256; R++) |
| 53 |
{ |
| 54 |
for (G = 0; G < 256; G++) |
| 55 |
for (B = 0; B < 256; B++) |
| 56 |
check(R, G, B); |
| 57 |
printf("[%3u] c %u/%u b %u/%u (R %u/%d/%u G %u/%d/%u B %u/%d/%u)\n", R, |
| 58 |
eCodes - preveCodes, eCodes, eBits - preveBits, eBits, |
| 59 |
abs(AbseRtotal - preveRtotal), eRtotal, AbseRtotal, |
| 60 |
abs(AbseGtotal - preveGtotal), eGtotal, AbseGtotal, |
| 61 |
abs(AbseBtotal - preveBtotal), eBtotal, AbseBtotal); |
| 62 |
preveRtotal = AbseRtotal; |
| 63 |
preveGtotal = AbseGtotal; |
| 64 |
preveBtotal = AbseBtotal; |
| 65 |
preveCodes = eCodes; |
| 66 |
preveBits = eBits; |
| 67 |
} |
| 68 |
printf("%u total codes\n", 256 * 256 * 256); |
| 69 |
printf("total error: %u codes %u bits (R %d/%u G %d/%u B %d/%u)\n", eCodes, |
| 70 |
eBits, eRtotal, AbseRtotal, eGtotal, AbseGtotal, eBtotal, |
| 71 |
AbseBtotal); |
| 72 |
return (0); |
| 73 |
} |
| 74 |
|
| 75 |
float *lumaRed; |
| 76 |
float *lumaGreen; |
| 77 |
float *lumaBlue; |
| 78 |
|
| 79 |
static float *setupLuma(float c) |
| 80 |
{ |
| 81 |
float *v = (float *)_TIFFmalloc(256 * sizeof(float)); |
| 82 |
int i; |
| 83 |
for (i = 0; i < 256; i++) |
| 84 |
v[i] = c * i; |
| 85 |
return (v); |
| 86 |
} |
| 87 |
|
| 88 |
static void setupLumaTables(void) |
| 89 |
{ |
| 90 |
lumaRed = setupLuma(LumaRed); |
| 91 |
lumaGreen = setupLuma(LumaGreen); |
| 92 |
lumaBlue = setupLuma(LumaBlue); |
| 93 |
} |
| 94 |
|
| 95 |
static unsigned V2Code(float f, unsigned long RB, unsigned long RW, int CR) |
| 96 |
{ |
| 97 |
unsigned int c = (unsigned int)((((f) * (RW - RB) / CR) + RB) + .5); |
| 98 |
return (c > 255 ? 255 : c); |
| 99 |
} |
| 100 |
|
| 101 |
#define Code2V(c, RB, RW, CR) ((((c) - (int)RB) * (float)CR) / (float)(RW - RB)) |
| 102 |
|
| 103 |
#define CLAMP(f, min, max) \ |
| 104 |
(int)((f) + .5 < (min) ? (min) : (f) + .5 > (max) ? (max) : (f) + .5) |
| 105 |
|
| 106 |
static void check(int R, int G, int B) |
| 107 |
{ |
| 108 |
float Y, Cb, Cr; |
| 109 |
int iY, iCb, iCr; |
| 110 |
float rY, rCb, rCr; |
| 111 |
float rR, rG, rB; |
| 112 |
int eR, eG, eB; |
| 113 |
|
| 114 |
Y = lumaRed[R] + lumaGreen[G] + lumaBlue[B]; |
| 115 |
Cb = (B - Y) * D2; |
| 116 |
Cr = (R - Y) * D1; |
| 117 |
iY = V2Code(Y, refBlackWhite[0], refBlackWhite[1], 255); |
| 118 |
iCb = V2Code(Cb, refBlackWhite[2], refBlackWhite[3], 127); |
| 119 |
iCr = V2Code(Cr, refBlackWhite[4], refBlackWhite[5], 127); |
| 120 |
rCb = Code2V(iCb, refBlackWhite[2], refBlackWhite[3], 127); |
| 121 |
rCr = Code2V(iCr, refBlackWhite[4], refBlackWhite[5], 127); |
| 122 |
rY = Code2V(iY, refBlackWhite[0], refBlackWhite[1], 255); |
| 123 |
rR = rY + rCr * D3; |
| 124 |
rB = rY + rCb * D4; |
| 125 |
rG = rY - rCb * D6 - rCr * D5; |
| 126 |
eR = R - CLAMP(rR, 0, 255); |
| 127 |
eG = G - CLAMP(rG, 0, 255); |
| 128 |
eB = B - CLAMP(rB, 0, 255); |
| 129 |
if (abs(eR) > 1 || abs(eG) > 1 || abs(eB) > 1) |
| 130 |
{ |
| 131 |
printf("R %u G %u B %u", R, G, B); |
| 132 |
printf(" Y %g Cb %g Cr %g", Y, Cb, Cr); |
| 133 |
printf(" iY %u iCb %u iCr %u", iY, iCb, iCr); |
| 134 |
printf("\n -> Y %g Cb %g Cr %g", rY, rCb, rCr); |
| 135 |
printf(" R %g (%u) G %g (%u) B %g (%u) E=[%d %d %d])\n", rR, |
| 136 |
CLAMP(rR, 0, 255), rG, CLAMP(rG, 0, 255), rB, CLAMP(rB, 0, 255), |
| 137 |
eR, eG, eB); |
| 138 |
} |
| 139 |
eRtotal += eR; |
| 140 |
eGtotal += eG; |
| 141 |
eBtotal += eB; |
| 142 |
AbseRtotal += abs(eR); |
| 143 |
AbseGtotal += abs(eG); |
| 144 |
AbseBtotal += abs(eB); |
| 145 |
if (eR | eG | eB) |
| 146 |
eCodes++; |
| 147 |
eBits += abs(eR) + abs(eG) + abs(eB); |
| 148 |
} |