Search code examples
androidrenderscript

How to allocate the LUT to use on ScriptIntrinsic3DLUT?


working with RenderScript, I'm trying to use the ScriptIntrinsic3DLUT (http://developer.android.com/reference/android/renderscript/ScriptIntrinsic3DLUT.html)

In general this scrip works just like any other renderscript

  • create RS context
  • create script with this context
  • create input and output allocations
  • set script parameters
  • call the kernel

may problem is on the set script parameters step, from the docs I should call script.setLUT (Allocation lut), but what is the appropriate way of generating/setting values for this allocation?

sample of the code I have is:

// create RS context
RenderScript rs = RenderScript.create(context);

// create output bitmap
Bitmap bitmapOut = Bitmap.createBitmap(bitmapIn.getWidth(), bitmapIn.getHeight(), bitmapIn.getConfig());

// create bitmap allocations
Allocation allocIn = Allocation.createFromBitmap(rs, bitmapIn);
Allocation allocOut = Allocation.createFromBitmap(rs, bitmapOut);

// create script
ScriptIntrinsic3DLUT script = ScriptIntrinsic3DLUT.create(rs, Element.U8_4(rs));

// set 3D LUT for the script
how to create the `Allocation lut` ??
script.setLUT(lut);

// process the script
script.forEach(allocIn, allocOut);

// copy result to bitmap output
allocOut.copyTo(bitmapOut);

My question is similar to How to use ScriptIntrinsic3DLUT with a .cube file?

but it is not the same. I do not care about the cube file. I can create the LUT from array of bytes, ints, matrix, whatever. I just want to know how-to/where-to place those bytes inside the allocation. What's the appropriate formatting for this allocation?


Solution

  • I found an example on the web here: https://android.googlesource.com/platform/frameworks/rs/+/master/java/tests/ImageProcessing/src/com/android/rs/image/ColorCube.java

    This creates a 3D LUT that doesn't modify the actual color values. Good example though how it could work.

    private ScriptIntrinsic3DLUT mIntrinsic;
    private void initCube() {
            final int sx = 32;
            final int sy = 32;
            final int sz = 16;
            Type.Builder tb = new Type.Builder(mRS, Element.U8_4(mRS));
            tb.setX(sx);
            tb.setY(sy);
            tb.setZ(sz);
            Type t = tb.create();
            mCube = Allocation.createTyped(mRS, t);
            int dat[] = new int[sx * sy * sz];
            for (int z = 0; z < sz; z++) {
                for (int y = 0; y < sy; y++) {
                    for (int x = 0; x < sx; x++ ) {
                        int v = 0xff000000;
                        v |= (0xff * x / (sx - 1));
                        v |= (0xff * y / (sy - 1)) << 8;
                        v |= (0xff * z / (sz - 1)) << 16;
                        dat[z*sy*sx + y*sx + x] = v;
                    }
                }
            }
            mCube.copyFromUnchecked(dat);
        }
    

    and then use it like so

    mIntrinsic.setLUT(mCube);
    

    If anyone can help with parsing a .cube file and fitting into this i would be very grateful.