问题
I tried to implement the Renderscript from this answer
So my Renderscript looks like the following:
#pragma version(1)
#pragma rs java_package_name(bavarit.app.cinnac)
rs_allocation inImage;
int inWidth;
int inHeight;
uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = inWidth - 1 - y;
uint32_t inY = x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
uint32_t inX = y;
uint32_t inY = inHeight - 1 - x;
const uchar4 *out = rsGetElementAt(inImage, inX, inY);
return *out;
}
The Javacode like this:
private static Bitmap rotateBitmapNew(Bitmap bitmap, Context ctx) {
RenderScript rs = RenderScript.create(ctx);
ScriptC_imageRotation script = new ScriptC_imageRotation(rs);
script.set_inWidth(bitmap.getWidth());
script.set_inHeight(bitmap.getHeight());
Allocation sourceAllocation = Allocation.createFromBitmap(
rs, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT
);
bitmap.recycle();
script.set_inImage(sourceAllocation);
int targetHeight = bitmap.getWidth();
int targetWidth = bitmap.getHeight();
Bitmap.Config config = bitmap.getConfig();
Bitmap target = Bitmap.createBitmap(targetWidth, targetHeight, config);
final Allocation targetAllocation = Allocation.createFromBitmap(
rs, target, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT
);
script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
targetAllocation.copyTo(target);
rs.destroy();
return target;
}
But when I call this function, I get this error
android.renderscript.RSRuntimeException: Type mismatch with U8_4!
I tried to debug this and find the error, and I found the source was this line in the ScriptC_imageRotation.java class
public void forEach_rotate_90_clockwise(Allocation ain, Allocation aout, Script.LaunchOptions sc) {
// check ain
if (!ain.getType().getElement().isCompatible(__U8_4)) {
throw new RSRuntimeException("Type mismatch with U8_4!");
}
...
}
Since I have no experience with RenderScript, I could only google, but nothing to this error could be found. Maybe one of you guys has a clue.
What I think I can say is that the type of the Allocation is incorrect, when I logged out the .getType().getElement()
of the allocation, it was something like U_5_6_5. Maybe thats helpful for somebody who understands this
回答1:
The renderscript code is expecting to have RGBA_888
as the element type (since you are using uchar4
as your input/output type.) The provided Bitmap
is in RGB 565 format, so the element type set for the renderscript Allocation
by createFromBitmap()
is being set to RGB_565
.
You need to make sure your source Bitmap
is in ARGB 8888 format since your target Bitmap
is created using the source's Bitmap.Config
and the RS code is expecting the source Allocation
to also be in this format.
来源:https://stackoverflow.com/questions/43739853/android-renderscript-type-mismatch-with-u8-4