【问题标题】:How can I make this Bitmap gamma function in Android run faster?如何使 Android 中的 Bitmap gamma 函数运行得更快?
【发布时间】:2012-05-22 14:17:51
【问题描述】:

我编写了以下函数来更改位图的 gamma,但它有点慢,即使在小型(300 x 300)位图上也是如此。我怎样才能让这个功能运行得更快?例如,是否有更好的方法(即更快的方法)从位图中访问单个像素值?

public Bitmap apply(Bitmap bmp, float gamma) {
    if (bmp == null)
        return null;

    int width = bmp.getWidth();
    int height = bmp.getHeight();

    int[] pixels = new int[width * height];
    bmp.getPixels(pixels, 0, width, 0, 0, width, height);

    int[] powers = new int[256];
    for (int i = 0; i < powers.length; i++)
        powers[i] = (int)(Math.pow(i / 255.0f, 1.0f / gamma) * 255);

    for (int p = 0; p < pixels.length; p++) {
        int r = Color.red(pixels[p]);
        int g = Color.green(pixels[p]);
        int b = Color.blue(pixels[p]);
        int newR = powers[r];
        int newG = powers[g];
        int newB = powers[b];
        pixels[p] = Color.rgb(newR, newG, newB);
    }

    Bitmap newBmp = Bitmap.createBitmap(pixels, 0, width, width, height, Config.ARGB_8888);
    return newBmp;
}

作为优化,我提前计算所有可能像素值(0 到 255)的功率,这有帮助,但这还不够。此外,在第二个 for 循环之外声明所有 int 并没有多大帮助,所以我把它们留在里面。提前致谢。

【问题讨论】:

    标签: android optimization image-processing bitmap gamma


    【解决方案1】:

    如果您真的希望它更快,我会考虑使用 OpenGL 或 RenderScript。两者在 Android 上都有很好的集成。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多