0

I found this function on the web that I apply on every pixel of two bitmaps, for blending:

private static int hardlight(int in1, int in2) {
    float image = (float)in2;
    float mask = (float)in1;
    return ((int)((image < 128) ? (2 * mask * image / 255):(255 - 2 * (255 - mask) * (255 - image) / 255)));
}

But I also need to adjust the intensity of the blending mask, so I need to apply alpha to it, but I have no idea on how to do it.

I tried this method on http://www.pegtop.net/delphi/articles/blendmodes/opacity.htm, that I translated to Java like this:

private static int opacity(int a, int b, float o) {
    return (int) (o * hardlight(a,b) + (255 - o) * a);
}

But the result was garbage with all weird colors. I don't have much experience in bitmap manipulation, so can anyone help me?

Paulo Cesar
  • 2,250
  • 1
  • 25
  • 35

1 Answers1

1

Apply this function to your mask prior calling the hardlight method:

    private static int setAlphaToInt(int i, float percentage){
        if(percentage < 0 || percentage > 100.0f){
            throw new IllegalArgumentException();
        }

        int desiredAlpha = (int) (((float)0xff * percentage)/100.0f);
        desiredAlpha = desiredAlpha << 24;
        return ((i & 0x00ffffff) | desiredAlpha);       
    }

It produces a result int where the highest 8 bits (the alpha part) are set to a value proportional to the percentage passed as parameter. For instance, to create a mask with 50% alpha, call

int newMask = setAlphaToInt(oldMask, 50.0f);
Mister Smith
  • 27,417
  • 21
  • 110
  • 193
  • Hi, thanks for the answer, but it didn't work. It gave me weird colors. But don't bother with it, I found a solution that works for me. I'm just drawing the resulting image over the original image with opacity, and then saving the resulting canvas. It's not beautiful, but the final result is good. – Paulo Cesar Feb 22 '12 at 17:15