Blending pixels from Two Bitmaps

Posted by MarkPowell on Stack Overflow See other posts from Stack Overflow or by MarkPowell
Published on 2011-01-05T14:44:39Z Indexed on 2011/01/05 16:54 UTC
Read the original article Hit count: 253

Filed under:
|
|

I'm beating my head against a wall here, and I'm fairly certain I'm doing something stupid, so time to make my stupidity public.

I'm trying to take two images, blend them together into a third image using standard blending algorithms (Hardlight, softlight, overlay, multiply, etc).

Because Android does not have such blend properties build in, I've gone down the path of taking each pixel and combine them using an algorithm. However, the results are garbage.

Any help would be appreciated. Below is the code, which I've tried to strip out all the "junk", but some may have made it through. I'll clean it up if something isn't clear.

    Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.base, options);
    Bitmap mutableBitmap = src.copy(Bitmap.Config.RGB_565, true);

    int imageId = getResources().getIdentifier("drawable/" + filter, null, getPackageName());
    Bitmap filterBitmap = BitmapFactory.decodeResource(getResources(), imageId, options);
    float scaleWidth = ((float) mutableBitmap.getWidth()) / filterBitmap.getWidth();
    float scaleHeight = ((float) mutableBitmap.getHeight()) / filterBitmap.getHeight();

        IntBuffer buffSrc = IntBuffer.allocate(src.getWidth() * src.getHeight());
    mutableBitmap.copyPixelsToBuffer(buffSrc);
    buffSrc.rewind();
    IntBuffer buffFilter = IntBuffer.allocate(resizedFilterBitmap.getWidth() * resizedFilterBitmap.getHeight());
    resizedFilterBitmap.copyPixelsToBuffer(buffFilter);
    buffFilter.rewind();
    IntBuffer buffOut = IntBuffer.allocate(src.getWidth() * src.getHeight());
    buffOut.rewind();

    while (buffOut.position() < buffOut.limit()) {
        int filterInt = buffFilter.get();
        int srcInt = buffSrc.get();

        int alphaValueFilter = Color.alpha(filterInt);
        int redValueFilter = Color.red(filterInt);
        int greenValueFilter = Color.green(filterInt);
        int blueValueFilter = Color.blue(filterInt);

        int alphaValueSrc = Color.alpha(srcInt);
        int redValueSrc = Color.red(srcInt);
        int greenValueSrc = Color.green(srcInt);
        int blueValueSrc = Color.blue(srcInt);

        int alphaValueFinal = convert(alphaValueFilter, alphaValueSrc);
        int redValueFinal = convert(redValueFilter, redValueSrc);
        int greenValueFinal = convert(greenValueFilter, greenValueSrc);
        int blueValueFinal = convert(blueValueFilter, blueValueSrc);

        int pixel = Color.argb(alphaValueFinal, redValueFinal, greenValueFinal, blueValueFinal);

        buffOut.put(pixel);
    }

    buffOut.rewind();

    mutableBitmap.copyPixelsFromBuffer(buffOut);        
    BitmapDrawable drawable = new BitmapDrawable(getResources(), mutableBitmap);
    imageView.setImageDrawable(drawable);
}


int convert (int in1, int in2) {
            //simple multiply for example
    return in1 * in2 / 255;
}

© Stack Overflow or respective owner

Related posts about android

Related posts about bitmap