实施机器人不同PorterDuff模式 [英] Implementing different PorterDuff modes in android

查看:155
本文介绍了实施机器人不同PorterDuff模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个图片编辑工具中,我需要合并两个图像。大多数像GIMP图像编辑工具使用 PorterDuff模式的合并或混合图像。我也使用在Android上的同样的方法。照片为Android提供的PorterDuff模式数量有限,我没能达到预期的效果。所以,我想实现PorterDuff模式(重叠,硬轻软,光,色,烧,颜色减淡),其中不包括在Android系统。维基,问题是我不知道从哪里开始。因此,这方面的任何参考或指导将是非常美联社preciated。

I am working on a picture editing tool in which I need to merge two images. Most of the image editing tools like gimp use PorterDuff modes for merging or blending images. I am also using the same approach in android.
As android provides limited number of PorterDuff modes, I am not able to achieve the desired result. So, I am thinking of implementing PorterDuff modes(Overlapping, Hard-Light, Soft-Light, Color-burn, Color-dodge) which are not included in android.
The problem is I don't know where to start. So, any reference or guidance in this regard will be highly appreciated.

推荐答案

这是如何实现的覆盖PorterDuff模式的android:

This is how you can implement the Overlay PorterDuff mode in android:

public class MyPorterDuffMode
{

    public Bitmap applyOverlayMode(Bitmap srcBmp, Bitmap destBmp)
    {
        int width = srcBmp.getWidth();
        int height = srcBmp.getHeight();
        int srcPixels[] = new int[width * height];;
        int destPixels[] = new int[width * height];
        int resultPixels[] = new int[width * height];
        int aS = 0, rS = 0, gS = 0, bS = 0;
        int rgbS = 0;
        int aD = 0, rD = 0, gD = 0, bD = 0;
        int rgbD = 0;

        try
        {
            srcBmp.getPixels(srcPixels, 0, width, 0, 0, width, height);
            destBmp.getPixels(destPixels, 0, width, 0, 0, width, height);
            srcBmp.recycle();
            destBmp.recycle();
        }
        catch(IllegalArgumentException e)
        {
        }
        catch(ArrayIndexOutOfBoundsException e)
        {
        }

        for(int y = 0; y < height; y++)
        {
            for(int x = 0; x < width; x++)
            {
                rgbS = srcPixels[y*width + x];
                aS = (rgbS >> 24) & 0xff;
                rS = (rgbS >> 16) & 0xff;
                gS = (rgbS >>  8) & 0xff;
                bS = (rgbS      ) & 0xff;

                rgbD = destPixels[y*width + x];
                aD = ((rgbD >> 24) & 0xff);
                rD = (rgbD >> 16) & 0xff;
                gD = (rgbD >>  8) & 0xff;
                bD = (rgbD      )  & 0xff;

                //overlay-mode
                rS = overlay_byte(rD, rS, aS, aD);
                gS = overlay_byte(gD, gS, aS, aD);
                bS = overlay_byte(bD, bS, aS, aD);
                aS = aS + aD - Math.round((aS * aD)/255f);

                resultPixels[y*width + x] = ((int)aS << 24) | ((int)rS << 16) | ((int)gS << 8) | (int)bS;
            }
        }

        return Bitmap.createBitmap(resultPixels, width, height, srcBmp.getConfig());
    }



    // kOverlay_Mode
    int overlay_byte(int sc, int dc, int sa, int da) {
        int tmp = sc * (255 - da) + dc * (255 - sa);
        int rc;
        if (2 * dc <= da) {
            rc = 2 * sc * dc;
        } else {
            rc = sa * da - 2 * (da - dc) * (sa - sc);
        }
        return clamp_div255round(rc + tmp);
    }


    int clamp_div255round(int prod) {
        if (prod <= 0) {
            return 0;
        } else if (prod >= 255*255) {
            return 255;
        } else {
            return Math.round((float)prod/255);
        }
    }

}

注:虽然提取的位图设置配置为ARGB_8888,这是很重要的
。 你想要做的图像融合或颜色操作任何时候,你需要确保你是在ARGB_8888模式,而不是在RGB_565模式。直到2.3,Android将usully默认为RGB_565模式,除非你明确告诉它不这样做,以节省内存。

Note: While extracting the bitmaps set the configuration to be ARGB_8888 and this is important.
Any time you want to do image blending or color manipulation, you need to make sure you are in ARGB_8888 mode, not in RGB_565 mode. Up until 2.3, android will usully default to RGB_565 mode unless you explicitly tell it to do otherwise in order to save memory.

BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Config.ARGB_8888;

这篇关于实施机器人不同PorterDuff模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆