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

标签 android image-processing bits-per-pixel color-blending

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

如果可以通过假设两个图像都不具有 alpha 来获得巨大 yield ,那很好。 注意: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 的输入转换为 float ,而操作看起来就像整数一样完美?

您还可以通过将公式内联到循环中而不是调用函数来提高速度。也许不是,但值得尝试和进行基准测试。

关于android - 优化 Android 位图上的每像素混合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4670759/

相关文章:

java - (Android)如何在我的音乐播放器上显示时间

android - 进行多个 http 请求调用时崩溃

android - 什么是 MediaRecorder.OutputFormat.DEFAULT?

java - Ajax 响应中的 Set-Cookie header 不适用于 android webView

python - OpenCV掩码错误错误:(-215:声明失败)(mtype == CV_8U || mtype == CV_8S)&& _mask.sameSize(* psrc1)在函数'cv::binary_op中

java - 使用ImageIO读取JCS_YCCK图像

python - 如何在 Python 中检测边缘并裁剪图像

opencv - 如何将 8 位 OpenCV IplImage* 转换为 32 位 IplImage*?

c++ - 奇怪的 SDL 内存使用取决于每像素位数