android - 如何将颜色 LUT 应用于位图图像以在 android 中实现滤镜效果?

标签 android bitmap lookup-tables

这里我有一个关于 android 中的 LUT 的问题。

我的问题是,我有 4X4 LUT,使用这些 LUT 在 android 中为位图图像应用滤镜效果。下面是我的示例 LUT 文件链接。 Lut link sample

在android中可以吗?如果可能,请帮助我如何申请。

提前致谢。

最佳答案

我正在开发一个 LUT 应用程序库,它可以简化 Android 中 LUT 图像的使用。它使用下面的算法,但我想在未来增强它以优化内存使用。现在它也猜测 LUT 的颜色轴: https://github.com/dntks/easyLUT/wiki

您的 LUT 图像的红-绿-蓝颜色维度与我习惯的顺序不同,所以我不得不更改获取 lutIndex 时的顺序(在 getLutIndex())。 请检查我编辑的答案:

final static int X_DEPTH = 16;
final static int Y_DEPTH = 16; //One little square has 16x16 pixels in it
final static int ROW_DEPTH = 4;
final static int COLUMN_DEPTH = 4; // the image consists of 4x4 little squares
final static int COLOR_DISTORTION = 16; // 256*256*256 => 256 no distortion, 64*64*64 => 256 dividied by 4 = 64, 16x16x16 => 256 dividied by 16 = 16

private Bitmap applyLutToBitmap(Bitmap src, Bitmap lutBitmap) {
    int lutWidth = lutBitmap.getWidth();
    int lutColors[] = new int[lutWidth * lutBitmap.getHeight()];
    lutBitmap.getPixels(lutColors, 0, lutWidth, 0, 0, lutWidth, lutBitmap.getHeight());

    int mWidth = src.getWidth();
    int mHeight = src.getHeight();
    int[] pix = new int[mWidth * mHeight];
    src.getPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);

    int R, G, B;
    for (int y = 0; y < mHeight; y++)
        for (int x = 0; x < mWidth; x++) {
            int index = y * mWidth + x;
            int r = ((pix[index] >> 16) & 0xff) / COLOR_DISTORTION;
            int g = ((pix[index] >> 8) & 0xff) / COLOR_DISTORTION;
            int b = (pix[index] & 0xff) / COLOR_DISTORTION;

            int lutIndex = getLutIndex(lutWidth, r, g, b);

            R = ((lutColors[lutIndex] >> 16) & 0xff);
            G = ((lutColors[lutIndex] >> 8) & 0xff);
            B = ((lutColors[lutIndex]) & 0xff);
            pix[index] = 0xff000000 | (R << 16) | (G << 8) | B;
        }
    Bitmap filteredBitmap = Bitmap.createBitmap(mWidth, mHeight, src.getConfig());
    filteredBitmap.setPixels(pix, 0, mWidth, 0, 0, mWidth, mHeight);
    return filteredBitmap;
}

//the magic happens here
private int getLutIndex(int lutWidth, int redDepth, int greenDepth, int blueDepth) {
    int lutX = (greenDepth % ROW_DEPTH) * X_DEPTH + blueDepth;
    int lutY = (greenDepth / COLUMN_DEPTH) * Y_DEPTH + redDepth;
    return lutY * lutWidth + lutX;
}

关于android - 如何将颜色 LUT 应用于位图图像以在 android 中实现滤镜效果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32905539/

相关文章:

c - 如何优化KASUMI密码S盒?

php - 基于 php 中的键查找值的有效方法

android - FireStore REST API 调用

android - 转换为位图时未呈现 CardView 阴影

android - 在操作栏中调整应用程序名称

c - 缩放位图图像出现段错误

java - 图像未通过位图从我的画廊上传

c++ - 在 C/C++ 中计算 32 位 CRC 查找表

javascript - 如何在 WebView 中单击按钮时获取值?

android - 如何在 login.vue 中添加标题?