如何将Color LUT应用于位图图像以获取android中的滤镜效果?

时间:2015-10-02 10:48:33

标签: android bitmap lookup-tables

这里我对android中的LUT有疑问。

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

在Android中可能吗?如果可能请帮助我申请。

提前致谢。

3 个答案:

答案 0 :(得分:3)

我正在使用LUT应用程序库,这样可以简化在Android中使用LUT图像的过程。它使用下面的algorythm,但我希望将来增强它以优化内存使用。现在它还猜测了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;
}

答案 1 :(得分:1)

这是使用RenderScript的ScriptIntrinsic3DLUT处理图像的方法

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsic3DLUT;
import android.renderscript.Type;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
    ImageView imageView1;
    RenderScript mRs;
    Bitmap mBitmap;
    Bitmap mLutBitmap;
    ScriptIntrinsic3DLUT mScriptlut;
    Bitmap mOutputBitmap;
    Allocation mAllocIn;
    Allocation mAllocOut;
    Allocation mAllocCube;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageView1 = (ImageView) findViewById(R.id.imageView);
        mRs = RenderScript.create(this);
        Background background = new Background();
        background.execute();
    }

    class Background extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {
            if (mRs == null) {
                mRs = RenderScript.create(MainActivity.this);
            }
            if (mBitmap == null) {
                mBitmap = BitmapFactory.decodeResource(getResources(),
                        R.drawable.bugs);

                mOutputBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), mBitmap.getConfig());

                mAllocIn = Allocation.createFromBitmap(mRs, mBitmap);
                mAllocOut = Allocation.createFromBitmap(mRs, mOutputBitmap);
            }
            if (mLutBitmap == null) {
                mLutBitmap = BitmapFactory.decodeResource(getResources(),
                        R.drawable.dawizfe);
                int w = mLutBitmap.getWidth();
                int h = mLutBitmap.getHeight();
                int redDim = w / 4;
                int greenDim = h / 4;
                int blueDim = 16;
                android.renderscript.Type.Builder tb = new Type.Builder(mRs, Element.U8_4(mRs));
                tb.setX(redDim);
                tb.setY(greenDim);
                tb.setZ(blueDim);
                Type t = tb.create();
                mAllocCube = Allocation.createTyped(mRs, t);
                int[] pixels = new int[w * h];
                int[] lut = new int[w * h];
                mLutBitmap.getPixels(pixels, 0, w, 0, 0, w, h);
                int i = 0;
                for (int r = 0; r < redDim; r++) {
                   for (int g = 0; g < greenDim; g++) {
                        for (int b = 0; b < blueDim; b++) {
                            int gdown = g / 4;
                            int gright = g % 4;
                            lut[i] = pixels[b + r * w + gdown * w * redDim + gright * blueDim];
                            i++;
                        }
                    }
                }
//                This is an identity 3D LUT
//                i = 0;
//                for (int r = 0; r < redDim; r++) {
//                    for (int g = 0; g < greenDim; g++) {
//                        for (int b = 0; b < blueDim; b++) {
//                            int bcol = (b * 255) / blueDim;
//                            int gcol = (g * 255) / greenDim;
//                            int rcol = (r * 255) / redDim;
//                            lut[i] = bcol | (gcol << 8) | (rcol << 16);
//                            i++;
//                        }
//                    }
//                }
                mAllocCube.copyFromUnchecked(lut);
            }
            if (mScriptlut == null) {
                mScriptlut = ScriptIntrinsic3DLUT.create(mRs, Element.U8_4(mRs));

            }
            mScriptlut.setLUT(mAllocCube);
            mScriptlut.forEach(mAllocIn, mAllocOut);

            mAllocOut.copyTo(mOutputBitmap);
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            imageView1.setImageBitmap(mOutputBitmap);
        }
    }
}

答案 2 :(得分:1)

你可以通过这个,希望它能帮助你获得正确的过程。

照片是这里的主要位图。

mLut3D是存储在drawable

中的LUT图像数组
{{1}}

你增加mFilter值以获得不同LUT图像的不同滤镜效果,你有,检查出来。

你可以通过github上的这个链接获得更多帮助,我从这里得到了答案: - re.sub

希望它会有所帮助