BayerToRGB.h
//原作者:Vojtech Pavlik //2010-10-23 23:25:16 冷却 整理、封装以及测试 #pragma once typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned long uint32_t; #define u8 uint8_t #define u32 uint32_t #define R(x,y) m_pRGB24[0 + 3 * ((x) + m_ulWidth * (y))] #define G(x,y) m_pRGB24[1 + 3 * ((x) + m_ulWidth * (y))] #define B(x,y) m_pRGB24[2 + 3 * ((x) + m_ulWidth * (y))] #define Bay(x,y) m_pBay[(x) + m_ulWidth * (y)] class BayerToRGB { public: BayerToRGB(); public: //函数名称:Bayer8ToRgb24 //描述:将8位Bayer原始数据转换为24位RGB格式数据 //注意:该函数不可重入 /* * 要求的Bayer数据排列格式如下(GBRG): * * G B G B G B G B... * R G R G R G R G... * G B G B G B G B... * R G R G R G R G... * G B G B G B G B... * R G R G R G R G... * G B G B G B G B... * R G R G R G R G... * .................. * .................. */ static HRESULT Bayer8ToRgb24(PBYTE pbBayer, DWORD dwWidth, DWORD dwHeight, PBYTE pbRgb, int iRgbBufLen); protected: HRESULT ExecBayer8ToRgb24(PBYTE pbBayer, DWORD dwWidth, DWORD dwHeight, PBYTE pbRgb, int iRgbBufLen); private: unsigned long m_ulWidth; unsigned long m_ulHeight; u8 *m_pBay; u8 *m_pRGB24; private: void bayer_copy(int x, int y); void bayer_bilinear(int x, int y); void bayer_to_rgb24(); };
BayerToRGB.cpp
#include "stdafx.h" #include "BayerToRGB.h" #pragma warning(disable: 4244) #pragma warning(disable: 4018) BayerToRGB::BayerToRGB() { m_ulWidth = 0; m_ulHeight = 0; m_pBay = NULL; m_pRGB24 = NULL; } void BayerToRGB::bayer_copy(int x, int y) { G(x + 0, y + 0) = Bay(x + 0, y + 0); G(x + 1, y + 1) = Bay(x + 1, y + 1); G(x + 0, y + 1) = G(x + 1, y + 0) = ((u32)Bay(x + 0, y + 0) + (u32)Bay(x + 1, y + 1)) / 2; R(x + 0, y + 0) = R(x + 1, y + 0) = R(x + 1, y + 1) = R(x + 0, y + 1) = Bay(x + 0, y + 1); B(x + 1, y + 1) = B(x + 0, y + 0) = B(x + 0, y + 1) = B(x + 1, y + 0) = Bay(x + 1, y + 0); } void BayerToRGB::bayer_bilinear(int x, int y) { R(x + 0, y + 0) = ((u32)Bay(x + 0, y + 1) + (u32)Bay(x + 0, y - 1)) / 2; G(x + 0, y + 0) = Bay(x + 0, y + 0); B(x + 0, y + 0) = ((u32)Bay(x - 1, y + 0) + (u32)Bay(x + 1, y + 0)) / 2; R(x + 0, y + 1) = Bay(x + 0, y + 1); G(x + 0, y + 1) = ((u32)Bay(x + 0, y + 0) + (u32)Bay(x + 0, y + 2) + (u32)Bay(x - 1, y + 1) + (u32)Bay(x + 1, y + 1)) / 4; B(x + 0, y + 1) = ((u32)Bay(x + 1, y + 0) + (u32)Bay(x - 1, y + 0) + (u32)Bay(x + 1, y + 2) + (u32)Bay(x - 1, y + 2)) / 4; R(x + 1, y + 0) = ((u32)Bay(x + 0, y + 1) + (u32)Bay(x + 2, y + 1) + (u32)Bay(x + 0, y - 1) + (u32)Bay(x + 2, y - 1)) / 4; G(x + 1, y + 0) = ((u32)Bay(x + 0, y + 0) + (u32)Bay(x + 2, y + 0) + (u32)Bay(x + 1, y - 1) + (u32)Bay(x + 1, y + 1)) / 4; B(x + 1, y + 0) = Bay(x + 1, y + 0); R(x + 1, y + 1) = ((u32)Bay(x + 0, y + 1) + (u32)Bay(x + 2, y + 1)) / 2; G(x + 1, y + 1) = Bay(x + 1, y + 1); B(x + 1, y + 1) = ((u32)Bay(x + 1, y + 0) + (u32)Bay(x + 1, y + 2)) / 2; } void BayerToRGB::bayer_to_rgb24() { int i, j; for (i = 0; i < m_ulWidth; i += 2) { for (j = 0; j < m_ulHeight; j += 2) { if (i == 0 || j == 0 || i == m_ulWidth - 2 || j == m_ulHeight - 2) { bayer_copy(i, j); } else { bayer_bilinear(i, j); } } } } HRESULT BayerToRGB::ExecBayer8ToRgb24(PBYTE pbBayer, DWORD dwWidth, DWORD dwHeight, PBYTE pbRgb, int iRgbBufLen) { if ( NULL == pbBayer || NULL == pbRgb ) { return E_POINTER; } if ( dwWidth<=0 || dwHeight<=0 || iRgbBufLen<=0 ) { return E_INVALIDARG; } if ( iRgbBufLen != dwWidth*dwHeight*3 ) { return E_INVALIDARG; } m_pBay = pbBayer; m_pRGB24 = pbRgb; m_ulWidth = dwWidth; m_ulHeight = dwHeight; bayer_to_rgb24(); return S_OK; } HRESULT BayerToRGB::Bayer8ToRgb24(PBYTE pbBayer, DWORD dwWidth, DWORD dwHeight, PBYTE pbRgb, int iRgbBufLen) { BayerToRGB cPixelFormatConvert; return cPixelFormatConvert.ExecBayer8ToRgb24(pbBayer, dwWidth, dwHeight, pbRgb, iRgbBufLen); }