欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

快速了解 YUV 图像基本处理 - 如何裁剪 NV21 格式的图片

最编程 2024-01-13 07:33:16
...

图例中将 6x6 的 NV21 图按照横纵坐标偏移量为(2,2)裁剪成 4x4 的 NV21 图。

image.png image.png


代码实现:

// x_offSet ,y_offSet % 2 == 0
void CropYUVImage(YUVImage *pSrcImg, int x_offSet, int y_offSet, YUVImage *pDstImg)
{
    // 确保裁剪区域不存在内存越界
  int cropWidth = pSrcImg->width - x_offSet;
  cropWidth = cropWidth > pDstImg->width ? pDstImg->width : cropWidth;
  int cropHeight = pSrcImg->height - y_offSet;
  cropHeight = cropHeight > pDstImg->height ? pDstImg->height : cropHeight;
  
  unsigned char  *pSrcCursor = NULL;
  unsigned char  *pDstCursor = NULL;

  //crop yPlane
  for (size_t i = 0; i < cropHeight; i++)
  {
    pSrcCursor = pSrcImg->yPlane + (y_offSet + i) * pSrcImg->width + x_offSet;
    pDstCursor = pDstImg->yPlane + i * pDstImg->width;
    memcpy(pDstCursor, pSrcCursor, sizeof(unsigned char ) * cropWidth);
  }

  //crop uvPlane
  for (size_t i = 0; i < cropHeight / 2; i++)
  {
    pSrcCursor = pSrcImg->uvPlane + (y_offSet / 2 + i) * pSrcImg->width + x_offSet;
    pDstCursor = pDstImg->uvPlane + i * pDstImg->width;
    memcpy(pDstCursor, pSrcCursor, sizeof(unsigned char ) * cropWidth);
  }

}