Created
November 1, 2023 08:03
-
-
Save EvanLyu732/fcc9d1cfb34a1fab0af64e20c164a042 to your computer and use it in GitHub Desktop.
yuyv2bgr
This file contains 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
#pragam once | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
// Conversion algorithm from: https://en.wikipedia.org/wiki/YUV | |
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | |
constexpr int ConvertYUYV_2_BGR(const int nWidth, const int nHeight, | |
u_char *pPixSrc, u_char *pPixDst) { | |
if (nullptr == pPixSrc || nullptr == pPixDst) { | |
std::cerr << "FAIL: Cannot convert YUYV to BGR from/to NULL pixel buffers" | |
<< std::endl; | |
return -1; | |
} | |
int nStrideSrc = nWidth * 2; | |
int nStrideDst = nWidth * 3; | |
u_char *pSrc = pPixSrc; | |
u_char *pDst = pPixDst; | |
int nRow = 0; | |
int nCol = 0; | |
int nColDst = 0; | |
int c = 0; | |
int d = 0; | |
int e = 0; | |
for (nRow = 0; nRow < nHeight; ++nRow) { | |
for (nCol = 0, nColDst = 0; nCol < nStrideSrc; nCol += 4, nColDst += 6) { | |
d = (int)pSrc[nCol + 1] - 128; // d = u – 128; | |
e = (int)pSrc[nCol + 3] - 128; // e = v – 128; | |
// c = y’ – 16 (for first pixel) | |
c = 298 * ((int)pSrc[nCol] - 16); | |
// B – Blue | |
pDst[nColDst] = (u_char)clamp((c + 516 * d + 128) >> 8, 0, 255); | |
// G -Green | |
pDst[nColDst + 1] = | |
(u_char)clamp((c - 100 * d - 208 * e + 128) >> 8, 0, 255); | |
// R – Red | |
pDst[nColDst + 2] = (u_char)clamp((c + 409 * e + 128) >> 8, 0, 255); | |
// c = y’ – 16 (for second pixel) | |
c = 298 * ((int)pSrc[nCol + 2] - 16); | |
// B – Blue | |
pDst[nColDst + 3] = (u_char)clamp((c + 516 * d + 128) >> 8, 0, 255); | |
// G -Green | |
pDst[nColDst + 4] = | |
(u_char)clamp((c - 100 * d - 208 * e + 128) >> 8, 0, 255); | |
// R – Red | |
pDst[nColDst + 5] = (u_char)clamp((c + 409 * e + 128) >> 8, 0, 255); | |
} | |
pSrc += nStrideSrc; | |
pDst += nStrideDst; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
below is a cuda version