ScriptIntrinsicYuvToRGB或YUV420分配是否被破坏?
问题描述:
我尝试使用renderscript中的ScriptIntrinsicYuvToRGB类将YUV转换为RGB,其中源为YUV420格式。 我有3个原始平面,我从文件中读取并尝试将它们馈送到YUV类型的Allocation中,并通过ScriptIntrinsicYuvToRGB.forEach传递它。ScriptIntrinsicYuvToRGB或YUV420分配是否被破坏?
它正确地转换亮度(Y平面),但未能上的颜色,因为色度通道似乎读取的所有值从BUF [W * H]位置 - 见代码示例中注释部分。它看起来像分配不正确地处理UV平面时的错误。我假设是因为我在一个使用rsGetElementAtYuv_uchar_U
函数的脚本中进行了测试,并且它对任何坐标给出了相同的值(来自buf [w * h])。
我搜索了所有的地方,如果我可以进一步指定YUV格式,如步幅/偏移等,但没有发现更多的设置Element.DataKind.PIXEL_YUV
和Type.Builder.setYuvFormat(ImageFormat.YUV_420_888)
。
有人可以帮忙吗?
{
int w = 320, h = 172;
ScriptIntrinsicYuvToRGB yc = ScriptIntrinsicYuvToRGB.create(rs, Element.U8_4(rs));
{
Element elemYUV = Element.createPixel(rs, Element.DataType.UNSIGNED_8, Element.DataKind.PIXEL_YUV);
Type typeYUV = new Type.Builder(rs, elemYUV).setX(w).setY(h).setYuvFormat(ImageFormat.YUV_420_888).create();
Allocation yuv = Allocation.createTyped(rs, typeYUV);
byte[] buf = new byte[yuv.getBytesSize()];
int offs = 0;
for(int i=0; i<3; i++){
int sz = w*h;
if(i>0)
sz /= 4;
InputStream is = new FileInputStream("/sdcard/yuv/"+(i==0 ? 'y' : i==1 ? 'u' : 'v'));
int n = is.read(buf, offs, sz);
if(n!=sz)
throw new AssertionError("!");
offs += sz;
is.close();
}
// buf[w*h] = 0x40;
yuv.copyFrom(buf);
yc.setInput(yuv);
}
Type outType = new Type.Builder(rs, Element.U8_4(rs)).setX(w).setY(h).create();
Allocation out = Allocation.createTyped(rs, outType);
yc.forEach(out);
int[] buf = new int[out.getBytesSize()/4];
out.copy1DRangeToUnchecked(0, w*h, buf);
bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
bm.setPixels(buf, 0, bm.getWidth(), 0, 0, bm.getWidth(), bm.getHeight());
iv.setImageBitmap(bm);
yc.destroy();
}
答
一种解决方法是只需填写一个U8分配和做自己的索引中的自定义脚本:
#pragma rs_fp_relaxed
rs_allocation yuv_in;
uint32_t width;
uint32_t offset_to_u;
uint32_t offset_to_v;
uchar4 RS_KERNEL yuv_to_rgba(uint32_t x, uint32_t y) {
uint32_t index = y * width + x;
uint32_t uv_index = (y >> 1) * width + (x >> 1);
float Y = (float)rsGetElementAt_uchar(yuv_in, index);
float U = (float)rsGetElementAt_uchar(yuv_in, uv_index + offset_to_u);
float V = (float)rsGetElementAt_uchar(yuv_in, uv_index + offset_to_v);
float3 f_out;
f_out.r = Y + 1.403f * V;
f_out.g = Y - 0.344f * U - 0.714f * V;
f_out.b = Y + 1.770f * U;
f_out = clamp(f_out, 0.f, 255.f);
uchar4 out;
out.rgb = convert_uchar3(f_out);
out.a = 255;
return out;
}
的java:
sc.set_yuv_in(yuv_allocation);
sc.set_width(width);
sc.set_offset_to_u(width * height);
sc.set_offset_to_v(width * height + (width/2 * height/2));
sc.forEach_yuv_to_rba(out);
YUV_420_888更多的是一种通用的YUV键入从android中的其他YUV资源导入。我认为没有办法设置u/v平面的步距/偏移值,以使其对自定义转换有用。