📄 bvccolorconverter.cpp
字号:
//-----------------------------------------------------------------------------
// (c) 2002 by Basler Vision Technologies
// Section: Vision Components
// Project: BVC
// $Header: BvcColorConverter.cpp, 15, 19.07.2005 10:45:49, Happe, A.$
//-----------------------------------------------------------------------------
/**
\file BvcColorConverter.cpp
\brief Color conversion
*
* Includes conversions from YUV422 to RGB (ConvertYUV422ToRGB) and
* of Bayer8 to RGB (ConvertMono8ToRGB).
*/
//-----------------------------------------------------------------------------
#include "stdafx.h"
#include "BvcColorConverter.h"
#include "BvcDib.h"
using namespace Bvc;
/*!
Conversion of YUV422 pixel data into RGB
YUV <-> RGB
range : 0 to 255 for Y, R, G, B
-128 to 127 for U and V
R = Y + 1.4022 V
G = Y - 0.3457 U - 0.7144 V
B = Y + 1.7710 U
Y = 0.2989 R + 0.5866 G + 0.1145 B
U = - 0.1688 R - 0.3312 G + 0.5 B
V = 0.5 R - 0.5184 G - 0.0817 B
*/
void CColorConverter::ConvertYUV422ToRGB(
CDibPtr &ptrDest, //!< Smart Pointer to the destination DIB
const BYTE* pSource //!< Pointer to the pixel data to be converted
)
{
CSize destSize = ptrDest->GetSize();
ConvertYUV422ToRGB(
(BYTE*) ptrDest->GetPixels(), // destination buffer
pSource, // source buffer
destSize, // size of destination DIB (pixel),
ptrDest->GetWidthBytes() - ptrDest->GetSize().cx * ( ptrDest->GetBitsPerPixel() >> 3 ) // lineoffset
);
}
/**
* Conversion of YUV422 pixel data into RGB ( CCIR 601 )
*
* R = ( Y - 16 ) * 1.164 + (V-128) * 1.596
* G = ( Y - 16 ) * 1.164 - (U-128) * 0.391 - (V-128) * 0.813
* B = ( Y - 16 ) * 1.164 + (U-128) * 2.018
*
* The formulas are approximated by
*
* R = (298 * (Y-16) + 409 * (V-128) + 128) >> 8
* G = (298 * (Y-16) - 100 * (U-128) - 208 * (V-128) + 128) >> 8
* B = (298 * (Y-16) + 516 * (U-128) + 128) >> 8
*
*
*
*/
//------------------------------------------------------------------------------
void CColorConverter::ConvertYUV422ToRGB(
BYTE* pDest, //!< Pointer to a buffer the RGB data will be written to
const BYTE* pSource, //!< Pointer to the pixel data to be converted
const CSize& Size, //!< Size of image (in pixel)
unsigned int lineOffset //!< destination buffer's line offset
)
{
static const int LUT_G_U[256] = // ( U - 128 ) * 100
{-12800, -12700, -12600, -12500, -12400, -12300, -12200, -12100,
-12000, -11900, -11800, -11700, -11600, -11500, -11400, -11300,
-11200, -11100, -11000, -10900, -10800, -10700, -10600, -10500,
-10400, -10300, -10200, -10100, -10000, -9900, -9800, -9700,
-9600, -9500, -9400, -9300, -9200, -9100, -9000, -8900,
-8800, -8700, -8600, -8500, -8400, -8300, -8200, -8100,
-8000, -7900, -7800, -7700, -7600, -7500, -7400, -7300,
-7200, -7100, -7000, -6900, -6800, -6700, -6600, -6500,
-6400, -6300, -6200, -6100, -6000, -5900, -5800, -5700,
-5600, -5500, -5400, -5300, -5200, -5100, -5000, -4900,
-4800, -4700, -4600, -4500, -4400, -4300, -4200, -4100,
-4000, -3900, -3800, -3700, -3600, -3500, -3400, -3300,
-3200, -3100, -3000, -2900, -2800, -2700, -2600, -2500,
-2400, -2300, -2200, -2100, -2000, -1900, -1800, -1700,
-1600, -1500, -1400, -1300, -1200, -1100, -1000, -900,
-800, -700, -600, -500, -400, -300, -200, -100,
0, 100, 200, 300, 400, 500, 600, 700,
800, 900, 1000, 1100, 1200, 1300, 1400, 1500,
1600, 1700, 1800, 1900, 2000, 2100, 2200, 2300,
2400, 2500, 2600, 2700, 2800, 2900, 3000, 3100,
3200, 3300, 3400, 3500, 3600, 3700, 3800, 3900,
4000, 4100, 4200, 4300, 4400, 4500, 4600, 4700,
4800, 4900, 5000, 5100, 5200, 5300, 5400, 5500,
5600, 5700, 5800, 5900, 6000, 6100, 6200, 6300,
6400, 6500, 6600, 6700, 6800, 6900, 7000, 7100,
7200, 7300, 7400, 7500, 7600, 7700, 7800, 7900,
8000, 8100, 8200, 8300, 8400, 8500, 8600, 8700,
8800, 8900, 9000, 9100, 9200, 9300, 9400, 9500,
9600, 9700, 9800, 9900, 10000, 10100, 10200, 10300,
10400, 10500, 10600, 10700, 10800, 10900, 11000, 11100,
11200, 11300, 11400, 11500, 11600, 11700, 11800, 11900,
12000, 12100, 12200, 12300, 12400, 12500, 12600, 12700};
static const int LUT_G_V[256] = // 208 * (V-128)
{-26624, -26416, -26208, -26000, -25792, -25584, -25376, -25168,
-24960, -24752, -24544, -24336, -24128, -23920, -23712, -23504,
-23296, -23088, -22880, -22672, -22464, -22256, -22048, -21840,
-21632, -21424, -21216, -21008, -20800, -20592, -20384, -20176,
-19968, -19760, -19552, -19344, -19136, -18928, -18720, -18512,
-18304, -18096, -17888, -17680, -17472, -17264, -17056, -16848,
-16640, -16432, -16224, -16016, -15808, -15600, -15392, -15184,
-14976, -14768, -14560, -14352, -14144, -13936, -13728, -13520,
-13312, -13104, -12896, -12688, -12480, -12272, -12064, -11856,
-11648, -11440, -11232, -11024, -10816, -10608, -10400, -10192,
-9984, -9776, -9568, -9360, -9152, -8944, -8736, -8528,
-8320, -8112, -7904, -7696, -7488, -7280, -7072, -6864,
-6656, -6448, -6240, -6032, -5824, -5616, -5408, -5200,
-4992, -4784, -4576, -4368, -4160, -3952, -3744, -3536,
-3328, -3120, -2912, -2704, -2496, -2288, -2080, -1872,
-1664, -1456, -1248, -1040, -832, -624, -416, -208,
0, 208, 416, 624, 832, 1040, 1248, 1456,
1664, 1872, 2080, 2288, 2496, 2704, 2912, 3120,
3328, 3536, 3744, 3952, 4160, 4368, 4576, 4784,
4992, 5200, 5408, 5616, 5824, 6032, 6240, 6448,
6656, 6864, 7072, 7280, 7488, 7696, 7904, 8112,
8320, 8528, 8736, 8944, 9152, 9360, 9568, 9776,
9984, 10192, 10400, 10608, 10816, 11024, 11232, 11440,
11648, 11856, 12064, 12272, 12480, 12688, 12896, 13104,
13312, 13520, 13728, 13936, 14144, 14352, 14560, 14768,
14976, 15184, 15392, 15600, 15808, 16016, 16224, 16432,
16640, 16848, 17056, 17264, 17472, 17680, 17888, 18096,
18304, 18512, 18720, 18928, 19136, 19344, 19552, 19760,
19968, 20176, 20384, 20592, 20800, 21008, 21216, 21424,
21632, 21840, 22048, 22256, 22464, 22672, 22880, 23088,
23296, 23504, 23712, 23920, 24128, 24336, 24544, 24752,
24960, 25168, 25376, 25584, 25792, 26000, 26208, 26416};
static const int LUT_B_U[256] = // 516 * (U-128)
{-66048, -65532, -65016, -64500, -63984, -63468, -62952, -62436,
-61920, -61404, -60888, -60372, -59856, -59340, -58824, -58308,
-57792, -57276, -56760, -56244, -55728, -55212, -54696, -54180,
-53664, -53148, -52632, -52116, -51600, -51084, -50568, -50052,
-49536, -49020, -48504, -47988, -47472, -46956, -46440, -45924,
-45408, -44892, -44376, -43860, -43344, -42828, -42312, -41796,
-41280, -40764, -40248, -39732, -39216, -38700, -38184, -37668,
-37152, -36636, -36120, -35604, -35088, -34572, -34056, -33540,
-33024, -32508, -31992, -31476, -30960, -30444, -29928, -29412,
-28896, -28380, -27864, -27348, -26832, -26316, -25800, -25284,
-24768, -24252, -23736, -23220, -22704, -22188, -21672, -21156,
-20640, -20124, -19608, -19092, -18576, -18060, -17544, -17028,
-16512, -15996, -15480, -14964, -14448, -13932, -13416, -12900,
-12384, -11868, -11352, -10836, -10320, -9804, -9288, -8772,
-8256, -7740, -7224, -6708, -6192, -5676, -5160, -4644,
-4128, -3612, -3096, -2580, -2064, -1548, -1032, -516,
0, 516, 1032, 1548, 2064, 2580, 3096, 3612,
4128, 4644, 5160, 5676, 6192, 6708, 7224, 7740,
8256, 8772, 9288, 9804, 10320, 10836, 11352, 11868,
12384, 12900, 13416, 13932, 14448, 14964, 15480, 15996,
16512, 17028, 17544, 18060, 18576, 19092, 19608, 20124,
20640, 21156, 21672, 22188, 22704, 23220, 23736, 24252,
24768, 25284, 25800, 26316, 26832, 27348, 27864, 28380,
28896, 29412, 29928, 30444, 30960, 31476, 31992, 32508,
33024, 33540, 34056, 34572, 35088, 35604, 36120, 36636,
37152, 37668, 38184, 38700, 39216, 39732, 40248, 40764,
41280, 41796, 42312, 42828, 43344, 43860, 44376, 44892,
45408, 45924, 46440, 46956, 47472, 47988, 48504, 49020,
49536, 50052, 50568, 51084, 51600, 52116, 52632, 53148,
53664, 54180, 54696, 55212, 55728, 56244, 56760, 57276,
57792, 58308, 58824, 59340, 59856, 60372, 60888, 61404,
61920, 62436, 62952, 63468, 63984, 64500, 65016, 65532};
static const int LUT_R_V[256] = // 409 * (V-128)
{-52352, -51943, -51534, -51125, -50716, -50307, -49898, -49489,
-49080, -48671, -48262, -47853, -47444, -47035, -46626, -46217,
-45808, -45399, -44990, -44581, -44172, -43763, -43354, -42945,
-42536, -42127, -41718, -41309, -40900, -40491, -40082, -39673,
-39264, -38855, -38446, -38037, -37628, -37219, -36810, -36401,
-35992, -35583, -35174, -34765, -34356, -33947, -33538, -33129,
-32720, -32311, -31902, -31493, -31084, -30675, -30266, -29857,
-29448, -29039, -28630, -28221, -27812, -27403, -26994, -26585,
-26176, -25767, -25358, -24949, -24540, -24131, -23722, -23313,
-22904, -22495, -22086, -21677, -21268, -20859, -20450, -20041,
-19632, -19223, -18814, -18405, -17996, -17587, -17178, -16769,
-16360, -15951, -15542, -15133, -14724, -14315, -13906, -13497,
-13088, -12679, -12270, -11861, -11452, -11043, -10634, -10225,
-9816, -9407, -8998, -8589, -8180, -7771, -7362, -6953,
-6544, -6135, -5726, -5317, -4908, -4499, -4090, -3681,
-3272, -2863, -2454, -2045, -1636, -1227, -818, -409,
0, 409, 818, 1227, 1636, 2045, 2454, 2863,
3272, 3681, 4090, 4499, 4908, 5317, 5726, 6135,
6544, 6953, 7362, 7771, 8180, 8589, 8998, 9407,
9816, 10225, 10634, 11043, 11452, 11861, 12270, 12679,
13088, 13497, 13906, 14315, 14724, 15133, 15542, 15951,
16360, 16769, 17178, 17587, 17996, 18405, 18814, 19223,
19632, 20041, 20450, 20859, 21268, 21677, 22086, 22495,
22904, 23313, 23722, 24131, 24540, 24949, 25358, 25767,
26176, 26585, 26994, 27403, 27812, 28221, 28630, 29039,
29448, 29857, 30266, 30675, 31084, 31493, 31902, 32311,
32720, 33129, 33538, 33947, 34356, 34765, 35174, 35583,
35992, 36401, 36810, 37219, 37628, 38037, 38446, 38855,
39264, 39673, 40082, 40491, 40900, 41309, 41718, 42127,
42536, 42945, 43354, 43763, 44172, 44581, 44990, 45399,
45808, 46217, 46626, 47035, 47444, 47853, 48262, 48671,
49080, 49489, 49898, 50307, 50716, 51125, 51534, 51943};
static const int LUT_Y[256] = // 298 * (Y-16) + 128
{ -4640, -4342, -4044, -3746, -3448, -3150, -2852, -2554,
-2256, -1958, -1660, -1362, -1064, -766, -468, -170,
128, 426, 724, 1022, 1320, 1618, 1916, 2214,
2512, 2810, 3108, 3406, 3704, 4002, 4300, 4598,
4896, 5194, 5492, 5790, 6088, 6386, 6684, 6982,
7280, 7578, 7876, 8174, 8472, 8770, 9068, 9366,
9664, 9962, 10260, 10558, 10856, 11154, 11452, 11750,
12048, 12346, 12644, 12942, 13240, 13538, 13836, 14134,
14432, 14730, 15028, 15326, 15624, 15922, 16220, 16518,
16816, 17114, 17412, 17710, 18008, 18306, 18604, 18902,
19200, 19498, 19796, 20094, 20392, 20690, 20988, 21286,
21584, 21882, 22180, 22478, 22776, 23074, 23372, 23670,
23968, 24266, 24564, 24862, 25160, 25458, 25756, 26054,
26352, 26650, 26948, 27246, 27544, 27842, 28140, 28438,
28736, 29034, 29332, 29630, 29928, 30226, 30524, 30822,
31120, 31418, 31716, 32014, 32312, 32610, 32908, 33206,
33504, 33802, 34100, 34398, 34696, 34994, 35292, 35590,
35888, 36186, 36484, 36782, 37080, 37378, 37676, 37974,
38272, 38570, 38868, 39166, 39464, 39762, 40060, 40358,
40656, 40954, 41252, 41550, 41848, 42146, 42444, 42742,
43040, 43338, 43636, 43934, 44232, 44530, 44828, 45126,
45424, 45722, 46020, 46318, 46616, 46914, 47212, 47510,
47808, 48106, 48404, 48702, 49000, 49298, 49596, 49894,
50192, 50490, 50788, 51086, 51384, 51682, 51980, 52278,
52576, 52874, 53172, 53470, 53768, 54066, 54364, 54662,
54960, 55258, 55556, 55854, 56152, 56450, 56748, 57046,
57344, 57642, 57940, 58238, 58536, 58834, 59132, 59430,
59728, 60026, 60324, 60622, 60920, 61218, 61516, 61814,
62112, 62410, 62708, 63006, 63304, 63602, 63900, 64198,
64496, 64794, 65092, 65390, 65688, 65986, 66284, 66582,
66880, 67178, 67476, 67774, 68072, 68370, 68668, 68966,
69264, 69562, 69860, 70158, 70456, 70754, 71052, 71350};
int KY, U, V;
int deltaG, deltaB, deltaR;
int i, j, i2, m, n;
unsigned char* pData;
unsigned char* pRaw;
for (i=0; i<Size.cy; i++)
{
i2 = Size.cy - i - 1;
m = i*( 3 * Size.cx + lineOffset);
n = 2*i2*Size.cx;
pData = pDest + m;
pRaw = (unsigned char*) pSource + n;
for (j=0; j< (Size.cx & ~1); j++)
{
U = *pRaw++;
KY = LUT_Y[*pRaw++]; // (Y-16) * 298
V = *pRaw++;
deltaG = LUT_G_U[U] + LUT_G_V[V];
deltaR = LUT_R_V[V];
deltaB = LUT_B_U[U];
#define CLIP(v) (unsigned char( ( v < 0 ) ? 0 : ( (v > 255) ? 255 : v) ))
*pData++ = CLIP( ( KY + deltaB ) >> 8 ); // B
*pData++ = CLIP( ( KY - deltaG ) >> 8 ); // G
*pData++ = CLIP( ( KY + deltaR ) >> 8 ); // R
j++;
KY = LUT_Y[*pRaw++]; // (Y-16) * 298
*pData++ = CLIP( ( KY + deltaB ) >> 8 ); // B
*pData++ = CLIP( ( KY - deltaG ) >> 8 ); // G
*pData++ = CLIP( ( KY + deltaR ) >> 8 ); // R
#undef CLIP
}
if ( Size.cx & 1 )
{
// In case of an odd width erase the last column because no complete
// color information for the last column is available.
for ( int i = 0; i < 3; ++i )
*pData++=0;
}
}
}
//! Red unity LUT
static BYTE pLutR[LUT_LENGTH] = {0};
//! Green unity LUT
static BYTE pLutG[LUT_LENGTH] = {0};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -