海思YUV420图片的裁剪与旋转

部分做了一些优化

#include <stdio.h>
#include "malloc.h" 
#include <stdint.h>
#include <string.h>
#include <stdbool.h>

static void Cut_YV12(char* Src,int x,int y,int srcWidth,int srcHeight,char* Dst,int desWidth,int desHeight)//图片按位置裁剪
{
	//得到B图像所在A的坐标
	int nIndex=0;
	int BPosX=x ;//列
	int BPosY=y;//行
	for(int i=0;i<desHeight;i++)//
	{	
		memcpy(Dst+desWidth*i,Src+(srcWidth*BPosY)+BPosX+nIndex,desWidth);
		nIndex+=(srcWidth);
	}
 
	nIndex=0;
	char *pUSour=Src+srcWidth*srcHeight;
	char *pUDest=Dst+desWidth*desHeight;
	for(int i=0;i<desHeight/2;i++)//
	{	
		memcpy(pUDest+desWidth/2*i,pUSour+(srcWidth/2*BPosY/2)+BPosX/2+nIndex,desWidth/2);
		nIndex+=(srcWidth/2);
	}
 
	nIndex=0;
	char *pVSour=Src+srcWidth*srcHeight*5/4;
	char *pVDest=Dst+desWidth*desHeight*5/4;
	for(int i=0;i<desHeight/2;i++)//
	{	
		memcpy(pVDest+desWidth/2*i,pVSour+(srcWidth/2*BPosY/2)+BPosX/2+nIndex,desWidth/2);
		nIndex+=(srcWidth/2);
	}
 
}

static void Cut_YV12_yh(char* Src,int x,int y,int srcWidth,int srcHeight,char* Dst,int desWidth,int desHeight)//图片按位置裁剪
{
	//得到B图像所在A的坐标
	unsigned int nIndex=0;
	unsigned int BPosX=x ;//列
	unsigned int BPosY=y;//行
	
	unsigned int npos = 0;

	npos = srcWidth*BPosY;
	for(int i=0;i<desHeight;i++)//
	{	
		memcpy(Dst+desWidth*i,Src+npos+BPosX+nIndex,desWidth);
		nIndex+=(srcWidth);
	}
 
	nIndex=0;
	char *pUSour=Src+srcWidth*srcHeight;
	char *pUDest=Dst+desWidth*desHeight;
	npos = (srcWidth>>1)*(BPosY>>1);
	for(int i=0;i<desHeight>>1;i++)//
	{	
		memcpy(pUDest+desWidth/2*i,pUSour+npos+(BPosX>>1)+nIndex,desWidth>>1);
		nIndex+=(srcWidth>>1);
	}
 
	nIndex=0;
	char *pVSour=Src+srcWidth*srcHeight*5/4;
	char *pVDest=Dst+desWidth*desHeight*5/4;
	npos = (srcWidth>>1)*(BPosY>>1);
	for(int i=0;i<desHeight>>1;i++)//
	{	
		memcpy(pVDest+(desWidth>>1)*i,pVSour+npos+(BPosX>>1)+nIndex,desWidth>>1);
		nIndex+=(srcWidth>>1);
	}
 
}

int readyuv(char *buff,char *fname)
{
	int ret = 0;
	FILE *fprb;
	fprb = fopen(fname, "rb");
	if ( fprb <= 0 )  //> fname is path of file by const char* type
	{
		printf("can't open file\n");
		return false;
	}
	ret = fread(buff, 1080*1920*1.5, 1, fprb);  //> Con is a container to contain the input data
	fclose(fprb);
	return ret;
}
int writeyuv(char *buff,unsigned int len,char *fname)
{
	int ret = 0;
	FILE *fprb;
	fprb = fopen(fname, "wb");
	if ( fprb <= 0 )  //> fname is path of file by const char* type
	{
		printf("can't open file\n");
		return false;
	}
	ret = fwrite(buff, len, 1, fprb);  //> Con is a container to contain the input data
	fclose(fprb);
	return ret;
}

void yuv_rotate_90(char *des,char *src,int width,int height)  
{  
    int n = 0;  
    int hw = width / 2;  
    int hh = height / 2;  
    //copy y  
    for(int j = 0; j < width;j++)  
    {  
        for(int i = height - 1; i >= 0; i--)  
        {  
            des[n++] = src[width * i + j];  
        }  
    }  
  
    //copy u  
    char *ptemp = src + width * height;  
    for(int j = 0;j < hw;j++)  
    {  
        for(int i = hh - 1;i >= 0;i--)  
        {  
            des[n++] = ptemp[ hw*i + j ];  
        }  
    }  
  
    //copy v  
    ptemp += width * height / 4;  
    for(int j = 0; j < hw; j++)  
    {  
        for(int i = hh - 1;i >= 0;i--)  
        {  
            des[n++] = ptemp[hw*i + j];  
        }  
    }  
}  
  
void yuv_rotate_180(char *des,char *src,int width,int height)  
{  
    int n = 0;  
    int hw = width / 2;  
    int hh = height / 2;  
    //copy y  
    for(int j = height - 1; j >= 0; j--)  
    {  
        for(int i = width; i > 0; i--)  
        {  
            des[n++] = src[width*j + i];  
        }  
    }  
  
    //copy u  
    char *ptemp = src + width * height;  
    for(int j = hh - 1;j >= 0; j--)  
    {  
        for(int i = hw; i > 0; i--)  
        {  
            des[n++] = ptemp[hw * j + i];  
        }  
    }  
  
    //copy v  
    ptemp += width * height / 4;  
    for(int j = hh - 1;j >= 0; j--)  
    {  
        for(int i = hw; i > 0; i--)  
        {  
            des[n++] = ptemp[hw * j + i];  
        }  
    }  
}  
  
void yuv_rotate_270(char *des,char *src,int width,int height)  
{  
    int n = 0;  
    int hw = width / 2;  
    int hh = height / 2;  
    //copy y  
    for(int j = width; j > 0; j--)  
    {  
        for(int i = 0; i < height;i++)  
        {  
            des[n++] = src[width*i + j];  
        }  
    }  
  
    //copy u  
    char *ptemp = src + width * height;  
    for(int j = hw; j > 0;j--)  
    {  
        for(int i = 0; i < hh;i++)  
        {  
            des[n++] = ptemp[hw * i + j];
        }  
    }  
  
    //copy v  
    ptemp += width * height / 4;  
    for(int j = hw; j > 0;j--)  
    {  
        for(int i = 0; i < hh;i++)  
        {  
            des[n++] = ptemp[hw * i + j];  
        }  
    }  
}  

void yuv_rotate_270_yh(unsigned char *des,unsigned char *src,int width,int height)  
{  
    int n = 0;  
    int hw = width >> 1;  
    int hh = height >> 1; 
    unsigned int nPos = 0;
    printf("copy start! \n");
    //copy y  
    for(int j = width; j > 0; j--)  
    {  
	nPos = 0;
        for(int i = 0; i < height;i++)  
        {  
		//printf("width*i + j = %d ,nPos + j = %d \n",width*i + j,nPos + j);
            //des[n++] = src[width*i + j];
		des[n++] = src[nPos + j];
		nPos += width;
        }

    }
    printf("copy y  ok \n");
    //copy u  
    char *ptemp = src + width * height;  
    for(int j = hw; j > 0;j--)  
    {  
	nPos = 0;
        for(int i = 0; i < hh;i++)  
        {  
            //des[n++] = ptemp[hw * i + j];
		des[n++] = ptemp[nPos+ j];
		nPos += hw;
        }  
    }  
    printf("copy u  ok \n");
    //copy v  
    ptemp += width * height >> 2;  
    for(int j = hw; j > 0;j--)  
    {  
	nPos = 0;
        for(int i = 0; i < hh;i++)  
        {  
            //des[n++] = ptemp[hw * i + j]; 
		des[n++] = ptemp[nPos + j]; 
		nPos += hw;
        }  
    }  
   printf("copy v  ok \n");
}  

int main(void)
{
	unsigned char *fname = "./a.yuv";
	unsigned char *out_fname = "./outb.yuv";
	char *yuvbuff_in = malloc(1920*1080*1.5);
	char *yuvbuff_out = malloc(720*576*1.5);
	char *yuvbuff_fz_out = malloc(720*576*1.5);
	int ret = 0;
	ret = readyuv(yuvbuff_in,fname);
	//Cut_I420(yuvbuff_in,0,0,1920,1080,yuvbuff_out,720,576);
	Cut_YV12_yh(yuvbuff_in,0,100,1920,1080,yuvbuff_out,720,576);
	yuv_rotate_270((unsigned char*)yuvbuff_fz_out,(unsigned char*)yuvbuff_out,720,576);
	ret = writeyuv(yuvbuff_fz_out,720*576*1.5,out_fname);
	free(yuvbuff_in);
	free(yuvbuff_out);
	return 0;
}

参考:
1.https://blog.csdn.net/xjb2006/article/details/78977776

你可能感兴趣的:(海思)