Original Post
Hi. I've written the code below that takes as input a RGBA image, multiplied on a black background, and the same RGBA image, multiplied on a white background. From these two images, I recreate the original RGBA image. It works like a charm, but it is slow. I'm hoping to get some optimisation tips for the code below:
unsigned char * image1Ptr = NULL, * image2Ptr = NULL, * image3Ptr = NULL;
// The calls below are into a specific API I'm working with. Nothing magic.
// They just fill the imageXPtr's with pointers to 720x576x4bytesperpixel images
// in memory.
// image1 = RGBA on white
// image2 = RGBA on black
// image3 = output image where the result should be written in.
ThrowErr(pObj->pMmImage->LockPixels(&pCall->pArgs[1],(PMoaVoid*) &image1Ptr));
ThrowErr(pObj->pMmImage->LockPixels(&pCall->pArgs[2],(PMoaVoid*) &image2Ptr));
ThrowErr(pObj->pMmImage->LockPixels(&pCall->pArgs[3],(PMoaVoid*) &image3Ptr));
for (int y=0;y<myImageInfo1.iHeight;y++)
{
for (int x=0;x<myImageInfo1.iWidth;x++)
{
unsigned char& tRedOnBlack = image2Ptr[2];
unsigned char& tRedOnWhite = image1Ptr[2];
unsigned char tBlend = 255 - (tRedOnWhite - tRedOnBlack);
if (tBlend != 0)
{
const unsigned char& tBlueOnBlack = image2Ptr[0];
const unsigned char& tGreenOnBlack = image2Ptr[1];
const unsigned int& tInverseBlend = pObj->m_lut[tBlend];
image3Ptr[0] = (tBlueOnBlack * tInverseBlend) >> 8;
image3Ptr[1] = (tGreenOnBlack * tInverseBlend) >> 8;
image3Ptr[2] = (tRedOnBlack * tInverseBlend) >> 8;
}
image3Ptr[3] = tBlend;
image1Ptr+=4;
image2Ptr+=4;
image3Ptr+=4;
}
Where, pObj->m_lut is a pregenerated lookuptable, made like this:
unigned int lut[512]
for (int i=1;i<512;i++) m_lut = 65535.f / i;
Running this code on 720x576 images takes between 15 and 22 milliseconds in releasemode, on my p4 2.8ghz. I'd love to hear suggestion from folks that think it could be written in a faster way. Bye, Lucas