2011-07-27 76 views
0

我有兩個像素的流,基本上需要做一個自定義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

你平時的流程有多長? – Cyprian

+0

沒有所有if ... else ...部件有多快? – Eiko

+0

另一個問題:Pic1中可能存在顏色匹配的可能性以及Pic2可能性如何?你檢查的顏色是否有共同之處? – Eiko

回答

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個測試用例。

1

不要自己走像素緩衝區,它會很痛苦地慢。在CoreImage中使用自定義過濾器來做這種事情。

+0

你有這樣的代碼示例嗎?我很早就做了一些重要的搜索,因爲我完全同意你的看法!這將是最好的解決方案。我很難找到一種過濾方法,可以讓我提取某些顏色或掩蓋某些顏色.... – capikaw