问题
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?
回答1:
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.
回答2:
You'll need to set the LUT before calling the forEach()
method on the script. The LUT data is pixel format RGBA, Element.U8_4
and is in 3 dimensions. So you'll need to create an Allocation
based on a data array which has enough space for 3 dimensions * 4 bytes per pixel.
来源:https://stackoverflow.com/questions/26401503/how-to-allocate-the-lut-to-use-on-scriptintrinsic3dlut