I’m trying to combine 2 images with a certain algorithm. But in its current state it is too slow. It takes about 70ms to combine two 512x512 images. This is OK but as soon as the images get bigger the time it takes to combine them increases.
This is the code in c# (Fast work with Bitmaps in C#)
var t = new Vec3f(0);
var u = new Vec3f(0);
var r = new Vec3f(0);
for (int i = 0; i < bData1.Height; ++i)
{
for (int j = 0; j < bData1.Width; ++j)
{
byte* dataBase = bData1Scan0Ptr + i * bData1.Stride + j * m_BitsPerPixel / 8;
byte* dataDetail = bData2Scan0Ptr + i * bData2.Stride + j * m_BitsPerPixel / 8;
byte* dataCombined = bDataCombinedScan0Ptr + i * bDataCombined.Stride + j * m_BitsPerPixel / 8;
t.x = (dataBase[2] / 255.0f) * 2.0f - 1.0f;
t.y = (dataBase[1] / 255.0f) * 2.0f - 1.0f;
t.z = (dataBase[0] / 255.0f) * 2.0f;
u.x = (dataDetail[2] / 255.0f) * -2.0f + 1.0f;
u.y = (dataDetail[1] / 255.0f) * -2.0f + 1.0f;
u.z = (dataDetail[0] / 255.0f) * 2.0f - 1.0f;
r = t * t.Dot(u) - u * t.z;
r.Normalize();
//Write data to our new bitmap
dataCombined[2] = (byte)Math.Round((r.x * 0.5f + 0.5f) * 255.0f);
dataCombined[1] = (byte)Math.Round((r.y * 0.5f + 0.5f) * 255.0f);
dataCombined[0] = (byte)Math.Round((r.z * 0.5f + 0.5f) * 255.0f);
m_VectorImageArray[index, i, j] = t; //base
m_VectorImageArray[index + 1, i, j] = u; //detail
m_VectorImageArray[index + 2, i, j] = r; //Combined
}
}
m_CombinedBitmap.UnlockBits(bDataCombined);
Because I wanted to speed this up I also tried to make a c++ dll and load it in my C# project with DLLImport. I've implemented this vector class (http://fastcpp.blogspot.co.uk/2011/12/simple-vector3-class-with-sse-support.html) thinking it would result in a significant speed gain, but unfortunately it turned out to be only ~10ms faster.
I want to make this faster because I’d like to update the image real-time (looping over the vectors which are stored in m_VectorImageArray).
The problem isn't related to reading/writing to the bitmap but more to the algorithm itself. I don’t think I can use a parallel.for because the pixels need to be in the exact same order, or is this possible after all?