How to Apply Color LUT to bitmap images for filter effects in android?

前端 未结 3 863
甜味超标
甜味超标 2021-01-14 03:01

here i have a question on LUTs in android.

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

3条回答
  •  忘掉有多难
    2021-01-14 03:37

    This is how you would process an image with RenderScript's 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 {
    
            @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);
            }
        }
    }
    

提交回复
热议问题