Stay Hungry , Stay Foolish

君子生非异也,善假于物也

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
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  阅读(417)  评论(0编辑  收藏  举报