5

我正在尝试在两个图像上应用混合滤镜(在本例中为 HardLight)。基本 Android 库不支持 HardLight,因此,我在每个像素上手动进行。第一次运行是有效的,但速度低于恒星。从基本 500x500 图像和 500x500 过滤器生成 500x500 图像花费的时间太长。这段代码也用于生成缩略图 (72x72),它是应用程序核心的组成部分。我很想得到一些关于如何加快速度的建议和/或提示。

如果可以通过假设两个图像都没有 alpha 来获得巨大的收益,那很好。注意:BlendMode 和 alpha 是示例中未使用的值(BlendMode 将选择混合类型,在本例中我硬编码 HardLight)。

public Bitmap blendedBitmap(Bitmap source, Bitmap layer, BlendMode blendMode, float alpha) {
    Bitmap base = source.copy(Config.ARGB_8888, true);
    Bitmap blend = layer.copy(Config.ARGB_8888, false);

    IntBuffer buffBase = IntBuffer.allocate(base.getWidth() * base.getHeight());
    base.copyPixelsToBuffer(buffBase);
    buffBase.rewind();

    IntBuffer buffBlend = IntBuffer.allocate(blend.getWidth() * blend.getHeight());
    blend.copyPixelsToBuffer(buffBlend);
    buffBlend.rewind();

    IntBuffer buffOut = IntBuffer.allocate(base.getWidth() * base.getHeight());
    buffOut.rewind();

    while (buffOut.position() < buffOut.limit()) {
        int filterInt = buffBlend.get();
        int srcInt = buffBase.get();

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

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

        int redValueFinal = hardlight(redValueFilter, redValueSrc);
        int greenValueFinal = hardlight(greenValueFilter, greenValueSrc);
        int blueValueFinal = hardlight(blueValueFilter, blueValueSrc);

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

        buffOut.put(pixel);
    }

    buffOut.rewind();

    base.copyPixelsFromBuffer(buffOut);
    blend.recycle();

    return base;
}

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

}
4

3 回答 3

2

浮点运算通常比整数慢,虽然我不能具体说关于 Android 的任何内容。我想知道为什么hardlight当操作看起来像整数一样完美工作时,你为什么要将输入转换为浮点数?

您还可以通过将公式内联放入循环而不是调用函数来获得加速。或许不是,但值得尝试和进行基准测试。

于 2011-01-12T16:59:51.233 回答
1

此外,如果您可以牺牲 3 位/像素的最终图像质量/精度,我们可以在函数hardlight()中获得大约 25% 的性能提升,方法是使用按位运算符重写它:

int hardlight(int image, int mask) {
    return (((image < 128) ? 
           ( (((mask << 1) * image) >> 8) ):
           (255^( (((255^mask) << 1) * (255^image)) >> 8))));
}
于 2011-01-13T14:26:11.907 回答
0

不久前有一个这样的问题(链接)。我不确定OP是否解决了他的问题。

在我的案例中,为了实现“photoshop 混合”,我所做的是将半透明阴影覆盖应用于单一颜色。这只是我的图形设计师弄清楚如何进行阴影叠加的问题。效果很好,我完全忽略了必须遍历位图中的像素的问题(我使用的是 getPixels() 和 setPixels())。困难的部分确实在我的设计师方面,但是一旦他弄清楚了,他就生成了许多漂亮的图像。

我基本上使用了带有阴影覆盖的 alpha 蒙版(生成动态颜色)。我很想通过代码了解解决方案,祝你好运!

编辑:另外,我不熟悉 BlendMode。你从来没有在你的方法中使用过它。这是什么,自定义类?

于 2011-01-12T16:48:23.940 回答