Get Off My Lawn
Get Off My Lawn

Reputation: 36291

Convert formula into Java

I would like to take this formula:

Formula

and convert it into java code. I don't think that the below code is correct, because I think I am getting the wrong result.

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

Here is the original image I am working with: http://images2.fanpop.com/images/photos/4800000/Beach-beaches-4843817-1280-800.jpg

a = the invert of the image link
b = the image link

Below is what I would be expecting my output to look like (PhotoShop):

Expected Result

This is what my output actually looks like (My Application):

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();

softLight:

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

Upvotes: 2

Views: 5346

Answers (3)

Serv0
Serv0

Reputation: 181

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

As @shippy said make sure that a and b are int.

Upvotes: 0

schippi
schippi

Reputation: 1059

the code is correct. but you typecast the result to int which can alter the result. id recommend using double or float.

and important: make sure a & b are also float or double.

Upvotes: 2

Juvanis
Juvanis

Reputation: 25950

Try trivial calculation:

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

Upvotes: 6

Related Questions