Skip to content

Instantly share code, notes, and snippets.

@tamarous
Created February 8, 2017 06:51
Show Gist options
  • Save tamarous/e57e471977a893356d3ca1d4571747b9 to your computer and use it in GitHub Desktop.
Save tamarous/e57e471977a893356d3ca1d4571747b9 to your computer and use it in GitHub Desktop.
Convert yuv to bgr
bool YV12ToBGR24_Native(unsigned char* pYUV, unsigned char* pBGR24, int width, int height) {
if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL)
return false;
const long len = width * height;
unsigned char* yData = pYUV;
unsigned char* vData = &yData[len];
unsigned char* uData = &vData[len >> 2];
int bgr[3];
int yIdx, uIdx, vIdx, idx;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
yIdx = i * width + j;
vIdx = (i / 2) * (width / 2) + (j / 2);
uIdx = vIdx;
bgr[0] = (int)(yData[yIdx] + (uData[vIdx] - 128) + (((uData[vIdx] - 128) * 198) >> 8)); // b分量
bgr[1] = (int)(yData[yIdx] - (((uData[vIdx] - 128) * 88) >> 8) - (((vData[vIdx] - 128) * 183) >> 8)); // g分量
bgr[2] = (int)(yData[yIdx] + (vData[uIdx] - 128) + (((vData[uIdx] - 128) * 103) >> 8));
for (int k = 0; k < 3; k++) {
idx = (i * width + j) * 3 + k;
if (bgr[k] >= 0 && bgr[k] <= 255)
pBGR24[idx] = bgr[k];
else
pBGR24[idx] = (bgr[k] < 0) ? 0 : 255;
}
}
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment