NV12/NV2与YUV420P相互转换

//image_src is the source image, image_dst is the converted image
void NV12_YUV420P(const unsigned char* image_src, unsigned char* image_dst,
int image_width, int image_height){
unsigned char* p = image_dst;
memcpy(p, image_src, image_width * image_height * 3 / 2);
const unsigned char* pNV = image_src + image_width * image_height;
unsigned char* pU = p + image_width * image_height;
unsigned char* pV = p + image_width * image_height + ((image_width * image_height)>>2);
for (int i=0; i<(image_width * image_height)/2; i++){
if ((i%2)==0) pU++ = (pNV + i);
else pV++ = (pNV + i);
}

//image_src is the source image, image_dst is the converted image
void NV21_YUV420P(const unsigned char* image_src, unsigned char* image_dst,
int image_width, int image_height){
unsigned char* p = image_dst;
memcpy(p, image_src, image_width * image_height * 3 / 2);
const unsigned char* pNV = image_src + image_width * image_height;
unsigned char* pU = p + image_width * image_height;
unsigned char* pV = p + image_width * image_height + ((image_width * image_height)>>2);
for (int i=0; i<(image_width * image_height)/2; i++){
if ((i%2)==0) pV++ = (pNV + i);
else pU++ = (pNV + i);
}

void YUV420PtoNV12(unsigned char Src, unsigned char Dst,int Width,int Height){
unsigned char* SrcU = Src + Width * Height;
unsigned char* SrcV = SrcU + Width * Height / 4 ;
memcpy(Dst, Src, Width * Height);
unsigned char* DstU = Dst + Width * Height;
for(int i = 0 ; i < Width * Height / 4 ; i++ ){
( *DstU++) = ( *SrcU++);
( *DstU++) = ( *SrcV++);
}
}

void YUV420PtoNV21(unsigned char *Src, unsigned char *Dst,int Width,int Height)
{
unsigned char* SrcU = Src + Width * Height;
unsigned char* SrcV = SrcU + Width * Height / 4 ;
memcpy(Dst, Src, Width * Height);
unsigned char* DstV = Dst + Width * Height;
for(int i = 0 ; i < Width * Height / 4 ; i++ )
{
(*DstV++) = (*SrcV++);
(*DstV++) = (*SrcU++);
}
}

你可能感兴趣的:(图像格式转换)