我有两个像素流,基本上需要对它们进行自定义xor以获得最终结果。它工作得很好 - 唯一的问题是,它需要模拟器大约4秒来解析代码。我知道必须有一种方法来优化这个例程 - 但经过几天测试我的想法(无济于事)后,我正在寻求帮助!
这是我的代码 - 提前感谢任何建议!
//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...
答案 0 :(得分:1)
不要自己走像素缓冲区,这将非常缓慢。在CoreImage中使用自定义过滤器执行此类操作。
答案 1 :(得分:1)
您可以做的一件事是将每个流中的三个值组合成一个更大的变量。这将允许您同时对所有三个进行比较,因此您的代码中将有三分之一的比较。
sred = (int)(rawPic1Data[i + 0]);
sgreen = (int)(rawPic1Data[i + 1]);
sblue = (int)(rawPic1Data[i + 2]);
register unsigned sval = (sred << 16) | (sgreen << 8) | (sblue);
rred = (int)(rawPic2Data[i + 0]);
rgreen = (int)(rawPic2Data[i + 1]);
rblue = (int)(rawPic2Data[i + 2]);
register unsigned rval = (rred << 16) | (rgreen << 8) | (rblue);
fred = 0;
fgreen = 0;
fblue = 0;
falpha = 0;
if(sval == ((102<<16)|(153) || rval == ((102<<16)|(153)) {
fred = 102; fgreen = 0; fblue = 153; falpha = 255;
} else if(sval == ((153<<16)|(51<<8)|(204))) || rval = ((153<<16)|(51<<8)|(204)))) {
fred = 153; fgreen = 51; fblue = 204; falpha = 255;
} ...
像这样的优化加速了我的测试程序36.5%,只有5个测试用例。