创建影像金字塔代码

在main函数中调用如下:

int main()

{

    CConsoleProcess *pProgress = new CConsoleProcess();

    bool f = CreatePyramids(strArg[2].c_str(), pProgress);

}    

该部分为CreatePyramid.cpp                CreatePyramid.h在下面
#include "CreatePyramid.h"
#include "gdal_priv.h"

#define TOPOVR_SIZE 256
bool CreatePyramids(const char* pszFileName, CProcessBase *pProgress)
{
    if (pProgress != NULL)
    {
        pProgress->SetMessage("Create Pyramid:");

    }

    GDALAllRegister();
    CPLSetConfigOption("USE_OVR", "YES");    //Create OVE format file  

    /* -------------------------------------------------------------------- */
    /*      Open data file.                                                 */
    /* -------------------------------------------------------------------- */
    GDALDatasetH     hDataset;
    hDataset = GDALOpen(pszFileName, GA_ReadOnly);

    GDALDriverH hDriver = GDALGetDatasetDriver(hDataset);
    const char* pszDriver = GDALGetDriverShortName(hDriver);
    if (EQUAL(pszDriver, "HFA") || EQUAL(pszDriver, "PCIDSK"))
    {
        GDALClose(hDataset);    //If th file is Erdas'img or PCI's pix format,create inline ovr,else create outside ovr    
        hDataset = GDALOpen(pszFileName, GA_Update);
    }

    if (hDataset == NULL)
    {
        if (pProgress != NULL)
            pProgress->SetMessage("Open the image failed!!!Please Check the image!!!");

        return false;
    }

    /* -------------------------------------------------------------------- */
    /*      Get File basic infomation                                       */
    /* -------------------------------------------------------------------- */
    int iWidth = GDALGetRasterXSize(hDataset);
    int iHeigh = GDALGetRasterYSize(hDataset);

    int iPixelNum = iWidth * iHeigh;    //The numble of the total pixel'numble    
    int iTopNum = TOPOVR_SIZE*TOPOVR_SIZE;                 //The top Pyramid's size,256*256   
    int iCurNum = iPixelNum / 4;

    int anLevels[1024] = { 0 };
    int nLevelCount = 0;                //level of Pyramid    

    do    //Compute the level of Pyramid   
    {
        anLevels[nLevelCount] = static_cast<int>(pow(2.0, nLevelCount + 2));
        nLevelCount++;
        iCurNum /= 4;
    } while (iCurNum > iTopNum);

    const char      *pszResampling = "GAUSS"; //The method of resample    
    GDALProgressFunc pfnProgress = GDALTermProgress;//The rate of processing   

    /* -------------------------------------------------------------------- */
    /*      Generate overviews.                                             */
    /* -------------------------------------------------------------------- */
    if (nLevelCount > 0 &&
        GDALBuildOverviews(hDataset, pszResampling, nLevelCount, anLevels,
        0, NULL, pfnProgress, pProgress) != CE_None)
    {
        if (pProgress != NULL)

            return true;
    }

    /* -------------------------------------------------------------------- */
    /*      Cleanup                                                         */
    /* -------------------------------------------------------------------- */
    GDALClose(hDataset);
    GDALDestroyDriverManager();

    if (pProgress != NULL)
        pProgress->SetMessage("Create Pyramid Successed!");

    return true;
}

CreatePyramid.h文件在此

#include <string>
#include <Windows.h>
#include "algorithm"

using namespace std;


class  CProcessBase
{


public:
    /**
    * @brief 构造函数
    */
    CProcessBase()
    {
        m_dPosition = 0.0;
        m_iStepCount = 100;
        m_iCurStep = 0;
        m_bIsContinue = true;
    }

    /**
    * @brief 析构函数
    */
    virtual ~CProcessBase() {}

    /**
    * @brief 设置进度信息
    * @param pszMsg   进度信息
    */
    virtual void SetMessage(const char* pszMsg) = 0;

    /**
    * @brief 设置进度值
    * @param dPosition  进度值
    * @return 返回是否取消的状态,true为不取消,false为取消
    */
    virtual bool SetPosition(double dPosition) = 0;

    /**
    * @brief 进度条前进一步,返回true表示继续,false表示取消
    * @return 返回是否取消的状态,true为不取消,false为取消
    */
    virtual bool StepIt() = 0;

    /**
    * @brief 设置进度个数
    * @param iStepCount  进度个数
    */
    virtual void SetStepCount(int iStepCount)
    {
        ReSetProcess();
        m_iStepCount = iStepCount;
    }

    /**
    * @brief 获取进度信息
    * @return 返回当前进度信息
    */
    string GetMessage()
    {
        return m_strMessage;
    }

    /**
    * @brief 获取进度值
    * @return 返回当前进度值
    */
    double GetPosition()
    {
        return m_dPosition;
    }

    /**
    * @brief 重置进度条
    */
    void ReSetProcess()
    {
        m_dPosition = 0.0;
        m_iStepCount = 100;
        m_iCurStep = 0;
        m_bIsContinue = true;
    }

    /*! 进度信息 */
    string m_strMessage;
    /*! 进度值 */
    double m_dPosition;
    /*! 进度个数 */
    int m_iStepCount;
    /*! 进度当前个数 */
    int m_iCurStep;
    /*! 是否取消,值为false时表示计算取消 */
    bool m_bIsContinue;
};


/**
* @brief 控制台进度条类
*
* 提供控制台程序的进度条类接口,来反映当前算法的进度值
*/
class CConsoleProcess : public CProcessBase
{
public:
    /**
    * @brief 构造函数
    */
    CConsoleProcess()
    {
        m_dPosition = 0.0;
        m_iStepCount = 100;
        m_iCurStep = 0;
    };

    /**
    * @brief 析构函数
    */
    ~CConsoleProcess()
    {
        //remove(m_pszFile);  
    };

    /**
    * @brief 设置进度信息
    * @param pszMsg   进度信息
    */
    void SetMessage(const char* pszMsg)
    {
        m_strMessage = pszMsg;
        printf("%s\n", pszMsg);
    }

    /**
    * @brief 设置进度值
    * @param dPosition  进度值
    * @return 返回是否取消的状态,true为不取消,false为取消
    */
    bool SetPosition(double dPosition)
    {
        m_dPosition = dPosition;
        TermProgress(m_dPosition);
        m_bIsContinue = true;
        return true;
    }

    /**
    * @brief 进度条前进一步
    * @return 返回是否取消的状态,true为不取消,false为取消
    */
    bool StepIt()
    {
        m_iCurStep++;
        m_dPosition = m_iCurStep*1.0 / m_iStepCount;

        TermProgress(m_dPosition);
        m_bIsContinue = true;
        return true;
    }

private:
    void TermProgress(double dfComplete)
    {
        static int nLastTick = -1;
        int nThisTick = (int)(dfComplete * 40.0);

        nThisTick = min(40, max(0, nThisTick));

        // Have we started a new progress run?    
        if (nThisTick < nLastTick && nLastTick >= 39)
            nLastTick = -1;

        if (nThisTick <= nLastTick)
            return;

        while (nThisTick > nLastTick)
        {
            nLastTick++;
            if (nLastTick % 4 == 0)
                fprintf(stdout, "%d", (nLastTick / 4) * 10);
            else
                fprintf(stdout, ".");
        }

        if (nThisTick == 40)
            fprintf(stdout, " - done.\n");
        else
            fflush(stdout);
    }
};
bool CreatePyramids(const char* pszFileName, CProcessBase *pProgress);

猜你喜欢

转载自blog.csdn.net/weixin_38470512/article/details/81391336