#define OK (0)
#define FAIL (-1)
typedef struct point
{
int x;
int y;
}point_t;
//裁剪区域信息
typedef struct rect
{
point_t ul;//up-left 左上角坐标
point_t lr;//low-right 右下角坐标
}rect_t;
/************************************************************
*函数名:RGB_Crop_Image
*描述:对RGB图片按坐标进行裁剪
*输入:pSrcImage 原始RGB数据
* width 原始RGB图像的宽
* height 原始RGB图像的高
* rect 裁剪区域信息
*输出:pDstImage 裁剪后的RGB数据
*返回值:OK 成功
* FAIL 失败
*************************************************************/
int RGB_Crop_Image(char *pSrcImage, int width, int height, char *pDstImage, rect_t rect)
{
int i, j;
int crop_width, crop_height;
char *pSrc = (pSrcImage + rect.ul.y * width * 3);
char *pDst = PDstImage;
if(pSrcImage == NULL || pDstImage == NULL)
{
printf("pointer is invalid; pSrcImage(%p) pDstImage(%p)", pSrcImage, pDstImage);
return FAIL;
}
crop_width = rect.lr.x - rect.ul.x;
crop_height = rect.lr.y - rect.ul.y;
if(crop_width > width || crop_height > height || crop_width < 0 || crop_height < 0)
{
printf("coordinate error\n");
return FAIL;
}
for(j = rect.ul.y; j < rect.lr.y; j++)
{
for(i = rect.ul.x; i < rect.lr.x; i++)
{
pDst[0] = pSrc[i*3];
pDst[1] = pSrc[i*3+1];
pDst[2] = pSrc[i*3+2];
pDst += 3;
}
pSrc += width * 3;
}
return OK;
}
RGB格式图片的裁剪函数
最新推荐文章于 2021-05-23 03:02:23 发布