2016-08-15:从YUV420P中提取指定大小区域

typedef struct
{
    int width;
    int height;
}SizeInfo;

typedef struct 
{
    int x;
    int y;
    int width;
    int height;
}ImageRect;

/*************************************************
// Method     : ExtraceSpecifiedSizeYuv
// Author     : zhoulee
// Date       : 2016/08/15 16:14
// Description: 从YUV420P中获取指定大小的YUV420P图像
// Returns    : bool: true 获取成功; false 获取失败
// Parameter  : 
//              image: 原始YUV420P数据指针
//              imgSize: 原始图像尺寸, 图像宽高必须为偶数
//              rect: 指定的区域信息, 区域左上角坐标以及宽高必须为偶数
//              partionImg: 指定区域的YUV420P数据 
*************************************************/
bool ExtraceSpecifiedSizeYuv(const unsigned char* image, const SizeInfo& imgSize,
                             const ImageRect& rect, unsigned char* partionImg);

bool ExtraceSpecifiedSizeYuv(const unsigned char* image, const SizeInfo& imgSize,
                             const ImageRect& rect, unsigned char* partionImg)
{
    if(imgSize.width%2 != 0 || imgSize.height%2 != 0
        || rect.x%2 != 0 || rect.y%2 != 0
        || rect.width%2 != 0 || rect.height%2 != 0
        || rect.x + rect.width > imgSize.width
        || rect.y + rect.height > imgSize.height)
    {
        return false;
    }

    int yBegPos = 0;
    int uBegPos = imgSize.width * imgSize.height;
    int vBegPos = uBegPos + (imgSize.width * imgSize.height) / 4;

    int offset = 0;
    //y component
    for(int row = rect.y; row < rect.y + rect.height; ++row)
    {
        int yOffset = yBegPos + row * imgSize.width + rect.x;
        memcpy(partionImg + offset, image + yOffset, rect.width);
        offset += rect.width;
    }

    //u component
    for (int row = rect.y; row < rect.y + rect.height; row+=2)
    {
        //for (int col = rect.x; col < rect.x + rect.width; col+=2)
        //{
        //    int uOffset = row * imgSize.width / 4 + col / 2;
        //    partionImg[offset] = image[uBegPos + uOffset];
        //    ++offset;
        //}
        int uOffset = uBegPos + row * imgSize.width / 4 + rect.x / 2;
        memcpy(partionImg + offset, image + uOffset, rect.width / 2);
        offset += rect.width / 2;
    }

    //v component
    for (int row = rect.y; row < rect.y + rect.height; row+=2)
    {
        //for (int col = rect.x; col < rect.x + rect.width; col+=2)
        //{
        //    int vOffset = row * imgSize.width / 4 + col / 2;
        //    partionImg[offset] = image[vBegPos + vOffset];
        //    ++offset;
        //}
        int vOffset = vBegPos + row * imgSize.width / 4 + rect.x / 2;
        memcpy(partionImg + offset, image + vOffset, rect.width / 2);
        offset += rect.width / 2;
    }

    return true;
}
posted on 2016-08-15 16:48  octocat 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/zhouLee/p/5773534.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值