zoukankan      html  css  js  c++  java
  • 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;
    }
  • 相关阅读:
    Oracle PL/SQL攻略
    Android数据库中查找一条数据 query方法详解
    验证视图MAC失败 Validation of ViewState MAC Failed
    长方体类
    用类的友元函数完成运算符的重载
    全国软件2. 三人年龄
    Android中实现带声音提示的Toast (自定义扩展Toast)
    js判断生效时间不得大于失效时间
    OpenGL运用辅助库创建规则几何对象
    Ubuntu13.04安装CUDA5.0
  • 原文地址:https://www.cnblogs.com/zhouLee/p/5773534.html
Copyright © 2011-2022 走看看