优化Android位图上的每像素混合

我正在尝试在两个图像上应用混合滤镜(在本例中为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)));

}
    
已邀请:
浮点运算通常比整数慢,但我没有具体说到Android的任何内容。我想知道为什么你将输入转换为
hardlight
到浮点,当操作看起来像整数一样完美? 您可以通过将公式内联到循环中而不是调用函数来获得加速。或许不是,但值得尝试和基准测试。     
此外,如果您可以牺牲3位/像素的最终图像质量/精度, - 我们可以通过使用逐位运算符重写函数hardlight()获得大约25%的性能增益:
int hardlight(int image, int mask) {
    return (((image < 128) ? 
           ( (((mask << 1) * image) >> 8) ):
           (255^( (((255^mask) << 1) * (255^image)) >> 8))));
}
    
有一段时间这样的问题(Link)。我不确定OP是否解决了他的问题。 在我的案例中我做了一个“photoshop混合”是通过将半透明的阴影覆盖应用于单一颜色。这只是我的平面设计师弄清楚如何进行阴影叠加的问题。它效果很好,我完全忽略了必须迭代位图中的像素的问题(我使用的是getPixels()和setPixels())。困难的部分确实在我的设计师的最后,但一旦他弄明白,他就产生了许多漂亮的图像。 我基本上使用阴影覆盖的alpha蒙版(生成动态颜色)。我很乐意通过代码了解解决方案,祝您好运! 编辑:另外,我不熟悉BlendMode。你从未在你的方法中使用它。这是什么,一个自定义类?     

要回复问题请先登录注册