I’ve got two streams of pixels and basically need to do a custom xor on them for a final resulting picture. It’s working great – only problem is is that it’s taking the simulator about 4 seconds to parse through the code. I know there’s got to be a way to optimize this routine – but after a few days of testing out my ideas (to no avail) I’m resorting to getting some help!
Here’s my code – thanks in advance for any suggestions!
//rawPic1Data and rawPic2Data is a stream of unsigned chars that ultimately came from a UIImage
for (int i = 0 ; i < (bufferLength); i=i+4)
{
sred = (int)(rawPic1Data[i + 0]);
sgreen = (int)(rawPic1Data[i + 1]);
sblue = (int)(rawPic1Data[i + 2]);
rred = (int)(rawPic2Data[i + 0]);
rgreen = (int)(rawPic2Data[i + 1]);
rblue = (int)(rawPic2Data[i + 2]);
fred = 0;
fgreen = 0;
fblue = 0;
falpha = 0;
if (((sred == 102) && (sgreen == 0) && (sblue == 153)) || ((rred == 102) && (rgreen == 0) && (rblue == 153)))
{
fred = 102; fgreen = 0; fblue = 153; falpha = 255;
}
else if (((sred == 153) && (sgreen == 51) && (sblue == 204)) || ((rred == 153) && (rgreen == 51) && (rblue == 204)))
{
fred = 153; fgreen = 51; fblue = 204; falpha = 255;
}
//...repeat the elseifs for another 12 colors. (14 total)
}
//Use the f values for my final output...
One thing you can do is to combine the three values from each stream into one larger variable. This will allow you to perform a comparison on all three at once, so there will be one-third as many comparisons in your code.
An optimization like this sped up my test program by 36.5%, with only 5 test cases.