I have this legacy code and I don't really think this is easy to read/maintain it. Can you suggest a way to make this more readable while still saving the performance?
template<typename T>
void static ConvertBayerToRgbImageDemosaic(T* BayerChannel, T* RgbChannel, int Width, int Height, ColorSpace ColorSpace)
{
//Translates index offset in Bayer image to channel offset in RGB image
int offsets[4];
std::vector<int> colorOffsets[4];
//calculate offsets according to color space
switch (ColorSpace)
{
case ColorSpace::BGGR:
offsets[0] = 2;
offsets[1] = 1;
offsets[2] = 1;
offsets[3] = 0;
colorOffsets[0].push_back(0);
colorOffsets[0].push_back(1);
colorOffsets[0].push_back(Width);
colorOffsets[0].push_back(Width + 1);
colorOffsets[1].push_back(-1);
colorOffsets[1].push_back(0);
colorOffsets[2].push_back(0);
colorOffsets[2].push_back(1);
colorOffsets[3].push_back(-Width - 1);
colorOffsets[3].push_back(-Width);
colorOffsets[3].push_back(-1);
colorOffsets[3].push_back(0);
break;
case ColorSpace::RGGB:
offsets[0] = 0;
offsets[1] = 1;
offsets[2] = 1;
offsets[3] = 2;
colorOffsets[0].push_back(0);
colorOffsets[0].push_back(1);
colorOffsets[0].push_back(Width);
colorOffsets[0].push_back(Width + 1);
colorOffsets[1].push_back(-1);
colorOffsets[1].push_back(0);
colorOffsets[2].push_back(0);
colorOffsets[2].push_back(1);
colorOffsets[3].push_back(-Width - 1);
colorOffsets[3].push_back(-Width);
colorOffsets[3].push_back(-1);
colorOffsets[3].push_back(0);
break;
case ColorSpace::GBRG:
offsets[0] = 1;
offsets[1] = 2;
offsets[2] = 0;
offsets[3] = 1;
colorOffsets[0].push_back(0);
colorOffsets[0].push_back(1);
colorOffsets[1].push_back(-1);
colorOffsets[1].push_back(0);
colorOffsets[1].push_back(Width - 1);
colorOffsets[1].push_back(Width);
colorOffsets[2].push_back(-Width);
colorOffsets[2].push_back(-Width + 1);
colorOffsets[2].push_back(0);
colorOffsets[2].push_back(1);
colorOffsets[3].push_back(-1);
colorOffsets[3].push_back(0);
break;
case ColorSpace::GRBG:
offsets[0] = 1;
offsets[1] = 0;
offsets[2] = 2;
offsets[3] = 1;
colorOffsets[0].push_back(0);
colorOffsets[0].push_back(1);
colorOffsets[1].push_back(-1);
colorOffsets[1].push_back(0);
colorOffsets[1].push_back(Width - 1);
colorOffsets[1].push_back(Width);
colorOffsets[2].push_back(-Width);
colorOffsets[2].push_back(-Width + 1);
colorOffsets[2].push_back(0);
colorOffsets[2].push_back(1);
colorOffsets[3].push_back(-1);
colorOffsets[3].push_back(0);
break;
default:
throw "Unsupported colorSpace. Only bayer supported.";
}
#pragma omp parallel for
for (auto row = 0; row < Height; row++)
{
for (auto col = 0, bayerIndex = row * Width; col < Width; col++, bayerIndex++)
{
auto offset = (row % 2)*2 + (col % 2); //0...3
std::for_each(colorOffsets[offset].begin(), colorOffsets[offset].end(),
[&](int colorOffset)
{
auto rgbIndex = (bayerIndex + colorOffset) * 3 + offsets[offset];
RgbChannel[rgbIndex] = BayerChannel[bayerIndex];
});
}
}
}