• 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;
    }
  • 相关阅读:
    任意钝角三角形都可以分割为7个锐角三角形
    科场的胜者
    VBA按区导出电脑字库区位码
    A macro to get all interior colorindex has been used in thisworkbook
    和菜鸟一起学linux总线驱动之初识spi总线协议
    Android应用程序与SurfaceFlinger服务之间的共享UI元数据(SharedClient)的创建过程分析
    和菜鸟一起学android4.0.3源码之vibrator振动器移植心得
    opengl_NeNe 第九课,移动图像代码.vs 2010_express OPENGL 2.0
    和菜鸟一起学linux总线驱动之初识USB设备描述符
    和菜鸟一起学OK6410之熟悉内核源码
  • 原文地址:https://www.cnblogs.com/zhouLee/p/5773534.html
Copyright © 2020-2023  润新知