如何应用颜色LUT以位图图像在Android的滤镜效果?滤镜、位图、图像、颜色

2023-09-08 09:22:19 作者:爲妳菊中插朵花

在这里,我对Android中的LUT的问题。

here i have a question on LUTs in android.

我的问题是,我有4X4的LUT,使用这些LUT的申请在Android的位图图像滤镜效果。下面是我的示例LUT文件链接。 甩链接样品

my question is, i have 4X4 LUTs, Using these LUTs apply filter effect for bitmap image in android. Below is my sample LUT file link. Lut link sample

是否有可能在Android的?如果可能的话,请帮我如何申请。

Is it Possible in android? if possible please help me how to apply.

在此先感谢。

推荐答案

我已经编辑我的previous的答案,因为你的LUT的映像在其他命令的红,绿,蓝颜色的尺寸比我所已习惯了,所以我不得不改变得到lutIndex时的顺序(在 getLutIndex())。 请检查我的编辑答案:

I've edited my previous answer, as your LUT image has the red-green-blue color dimension in an other order than what I've been used to, so I had to change the order of when getting the lutIndex (at getLutIndex()). Please check my edited answer:

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;
}