int Bayer_GRBG_To_RGB888(const uint8_t *src, uint8_t *dst, int width, int height)
{
assert(src != NULL);
assert(dst != NULL);
const uint8_t *pSrcRow = src;
uint8_t *pDstRow = dst;
const uint8_t *pSrcRowNext = 0;
const uint8_t *pSrcRowPrev = 0;
long nSrcBytesPerRow = 1 * width;
long nDstBytesPerRow = 3 * width;
long nSrcWidth = width;
long nSrcHeight = height;
for (long nRow=0; nRow < nSrcHeight; nRow++) {
pSrcRowNext = pSrcRow + nSrcBytesPerRow;
if (nRow == 0) pSrcRowPrev = pSrcRowNext;
else pSrcRowPrev = pSrcRow - nSrcBytesPerRow;
if (nRow == nSrcHeight-1) pSrcRowNext = pSrcRowPrev;
for (long nCol = 0, k = 0; nCol < nSrcWidth; nCol++,k += 3) {
long nColNext = nCol + 1;
long nColPrev = nCol - 1;
if (nCol == 0) nColPrev = nColNext;
if (nCol == nSrcWidth-1) nColNext = nColPrev;
if ((nRow % 2) == 0) { // even row
if ((nCol % 2) == 0) { // even column, Raw Green
pDstRow[k] = ((long)pSrcRow[nColNext] + pSrcRow[nColPrev]) / 2; // Red
pDstRow[k + 1] = pSrcRow[nCol]; // green
pDstRow[k + 2] = ((long)pSrcRowNext[nCol] + pSrcRowPrev[nCol]) / 2; // blue
} else { // odd column, Raw Red
pDstRow[k] = pSrcRow[nCol]; // Red
pDstRow[k + 1] = ((long)pSrcRow[nColPrev] + pSrcRow[nColNext] + pSrcRowNext[nCol] + pSrcRowPrev[nCol]) / 4; // Green
pDstRow[k + 2] = ((long)pSrcRowNext[nColNext] + pSrcRowNext[nColPrev] + pSrcRowPrev[nColNext] + pSrcRowPrev[nColPrev]) / 4; // blue
}
} else { // odd row
if ((nCol % 2) == 0) { // even column, Raw Blue
pDstRow[k] = ((long)pSrcRowNext[nColNext] + pSrcRowNext[nColPrev] + pSrcRowPrev[nColNext] + pSrcRowPrev[nColPrev]) / 4; // Red
pDstRow[k + 1] = ((long)pSrcRow[nColPrev] + pSrcRow[nColNext] + pSrcRowNext[nCol] + pSrcRowPrev[nCol]) / 4; // Green
pDstRow[k + 2] = pSrcRow[nCol]; // blue
} else { // odd column, Raw Green
pDstRow[k] = ((long)pSrcRowNext[nCol] + pSrcRowPrev[nCol]) / 2; // Red
pDstRow[k + 1] = pSrcRow[nCol]; // Green
pDstRow[k + 2] = ((long)pSrcRow[nColNext] + pSrcRow[nColPrev]) / 2; // blue
}
}
}
pSrcRow += nSrcBytesPerRow;
pDstRow += nDstBytesPerRow;
}
return 0;
}
Bookmarks