2012-11-27 37 views
2

我想借這個公式:公式轉換成Java

Formula

,並轉換成Java代碼。我不認爲下面的代碼是正確的,因爲我認爲我得到了錯誤的結果。

return (int)(2 * a * b + Math.pow(a, 2) * (1 - 2 * b)); 

這裏是我一起工作的原始圖像: http://images2.fanpop.com/images/photos/4800000/Beach-beaches-4843817-1280-800.jpg

A =圖片鏈接
B =圖片鏈接

下面的反轉是我會期待什麼我輸出看起來像(PhotoShop的):

Expected Result

這是我的輸出實際上看起來像(我的應用程序):

Actual Result

Invert inv = new Invert(); 
inv.setStage(stage); 
inv.setParent(this); 
BufferedImage img = inv.filter(); 
int[] invPixels = new int[stage.width * stage.height]; 
img.getRGB(0, 0, stage.width, stage.height, invPixels, 0, stage.width); 
for(int i = 0; i < ImageSync.originalPixels.length; i++){ 
    int fg = invPixels[i]; 
    int bg = ImageSync.originalPixels[i]; 
    int color = Blends.softLight(fg, bg); 
    invPixels[i] = color; 
} 
img = new BufferedImage(stage.width, stage.height, BufferedImage.TYPE_INT_ARGB); 
img.setRGB(0, 0, stage.width, stage.height, invPixels, 0, stage.width); 
Preview.setImage(img); 
stage.preview = Preview.getImage(); 
this.repaint(); 

柔光:

public static int softLight(int background, int foreground){ 
    return (2 * background * foreground) + ((background * background) * (1 - 2 * foreground)); 
} 
+0

看起來很對我。我認爲如果你的回答不正確,你可能會遇到精度問題。 – femtoRgon

+1

@JarrodRoberson。那麼,我沒有看到任何優先問題。 –

+0

你能發表一個你認爲你的'錯誤結果'是什麼樣的例子嗎? –

回答

5

嘗試瑣碎的計算:

return (2 * a * b) + (a * a * (1 - 2 * b)); 
2

的代碼是正確的。但是你將結果轉換爲可以改變結果的int。 id建議使用double或float。

重要的是:確保& b也浮動或雙。

0
int result = (2*a*b)+((a*a)*(1-(2*b))); 

正如@shippy說確保abint