2017-04-26 8 views
2

NV12 yuv를 RGB로 변환하기위한 코드를 작성했지만 색상이 올바르지 않습니다. yuv2rgb.rsandroid - NV12 yuv를 RGB로 변환하기위한 렌더러

#pragma version(1) 
#pragma rs java_package_name(com.example.myexam) 
#pragma rs_fp_relaxed 

rs_allocation gYUV; 
uint32_t gW; 
uint32_t gH; 

uchar4 __attribute__((kernel)) YUV2RGB(uint32_t x,uint32_t y) 
{ 
    uchar yps = rsGetElementAt_uchar(gYUV, x, y); 
    uchar u = rsGetElementAt_uchar(gYUV,(x & ~1),gH + (y>>1)); 
    uchar v = rsGetElementAt_uchar(gYUV,(x & ~1)+1,gH + (y>>1)); 
    uchar4 rgb = rsYuvToRGBA_uchar4(yps, u, v); 
    return rgb; 
} 

자바 코드 : I는 (X, Y)을 추측

public Bitmap NV12_toRGB(byte[] yuv,int W,int H) { 
    RenderScript rs = RenderScript.create(this); 
    Type.Builder yuvBlder = new Type.Builder(rs, Element.U8(rs)) 
      .setX(W).setY(H*3/2); 
    Allocation allocIn = Allocation.createTyped(rs,yuvBlder.create(),Allocation.USAGE_SCRIPT); 
    Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), W, H); 
    Allocation allocOut = Allocation.createTyped(rs,rgbType,Allocation.USAGE_SCRIPT); 

    ScriptC_yuv2rgb scriptC_yuv2rgb = new ScriptC_yuv2rgb(rs); 
    scriptC_yuv2rgb.set_gW(W); 
    scriptC_yuv2rgb.set_gH(H); 
    allocIn.copyFrom(yuv); 
    scriptC_yuv2rgb.set_gYUV(allocIn); 
    scriptC_yuv2rgb.forEach_YUV2RGB(allocOut); 

    Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); 
    allocOut.copyTo(bmp); 

    allocIn.destroy(); 
    scriptC_yuv2rgb.destroy(); 
    return bmp; 
} 

Y는 (x, y)에 있어야하므로 매트릭스 u는 (AT 있어야 좌표되고 (x/2) * 2, H + y/2), v는 u, ((x/2) * 2 + 1, H + y/2) 다음에 와야한다. 이 논리가 잘못된 것처럼 들립니다.

답변

2

두 가지 오류가 수정 될 필요

  1. -1

    이어야 ~ 1 개 단지와 동일한 -1 & X 있기 때문에,하지만 X & 1 ~ 그렇게 값을도 계속 마지막 비트를 마스크 것입니다.
  2. yuv 행렬 크기가 올바르지 않습니다. uv 벡터는 y 데이터의 끝에 저장되므로 전체 행렬 크기는 W * H * 3/2 여야합니다.

이 두 가지 변경 사항을 적용한 후에도 문제가 없습니다. 자바 :

public Bitmap YUV_toRGB(byte[] yuv,int W,int H) { 
     RenderScript rs = RenderScript.create(this); 
     Type.Builder yuvBlder = new Type.Builder(rs, Element.U8(rs)) 
       .setX(W).setY(H*3/2); 
     Allocation allocIn = Allocation.createTyped(rs,yuvBlder.create(),Allocation.USAGE_SCRIPT); 
     Type rgbType = Type.createXY(rs, Element.RGBA_8888(rs), W, H); 
     Allocation allocOut = Allocation.createTyped(rs,rgbType,Allocation.USAGE_SCRIPT); 

     ScriptC_yuv2rgb scriptC_yuv2rgb = new ScriptC_yuv2rgb(rs); 
     allocIn.copyFrom(yuv); 
     scriptC_yuv2rgb.set_gW(W); 
     scriptC_yuv2rgb.set_gH(H); 
     scriptC_yuv2rgb.set_gYUV(allocIn); 
     scriptC_yuv2rgb.forEach_YUV2RGB(allocOut); 

     Bitmap bmp = Bitmap.createBitmap(W, H, Bitmap.Config.ARGB_8888); 
     allocOut.copyTo(bmp); 

     allocIn.destroy(); 
     scriptC_yuv2rgb.destroy(); 
     return bmp; 
    } 

yuv2rgb.rs

#pragma version(1) 
#pragma rs java_package_name(com.example.myexam) 
#pragma rs_fp_relaxed 

rs_allocation gYUV; 
uint32_t gW; 
uint32_t gH; 

uchar4 __attribute__((kernel)) YUV2RGB(uint32_t x,uint32_t y) 
{ 
    uchar yps = rsGetElementAt_uchar(gYUV, x, y); 
    uchar u = rsGetElementAt_uchar(gYUV,(x & ~1),gH + (y>>1)); 
    uchar v = rsGetElementAt_uchar(gYUV,(x & ~1)+1,gH + (y>>1)); 
    uchar4 rgb = rsYuvToRGBA_uchar4(yps, u, v); 
    return rgb; 
} 
+0

죄송는 -1 0xFFFFFFFF를 의미하므로 및 -1 아무것도하지 동일. – lucky1928