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;
    }
  • 相关阅读:
    在java中请求webservice接口并且处理xml解析实现代码
    uniapp微信小程序唤起微信支付
    微信二维码图片添加资料
    uniapp支付宝App支付编写(沙箱支付)
    Java之xlsx文件上传到服务器
    echarts中国省份坐标数据
    kafka参数配置
    docker
    mysql 异步复制,半同步复制
    centos7安装mysql
  • 原文地址:https://www.cnblogs.com/zhouLee/p/5773534.html
Copyright © 2011-2022 走看看