www.pudn.com > dstile-0.2.rar > Warper.cpp


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
#include "exceptions.h"
#include "giskit.h"
#include "CSTrans.h"
#include "Warper.h"

void Warper::Create(GDALDataset *src, GDALDataset *dst, const CSTrans& cst) {
    int srcBands = src->GetRasterCount();
    int dstBands = dst->GetRasterCount();

    if (m_needDestroy) Destroy();
    m_needDestroy = true;

    m_opts = GDALCreateWarpOptions();
    if (!m_opts) throw Exception("Unable to create warper object: GDALCreateWarpOptions failed.");

    m_opts->hSrcDS = src;
    m_opts->hDstDS = dst;
    m_opts->pTransformerArg = cst.GetCSTransArg();
    m_opts->pfnTransformer = cst.GetCSTransFunc();
    m_opts->nBandCount = dstBands;

    m_opts->panSrcBands = new int[m_opts->nBandCount];
    if (dstBands == 3) {
        if (srcBands == 3) {
	    for (int i = 0; i < 3; ++i) m_opts->panSrcBands[i] = i + 1;
	} else if (srcBands == 1) {
	    for (int i = 0; i < 3; ++i) m_opts->panSrcBands[i] = 1;
	} else throw InvalidInput("Unable to create warper object: unsupported band count.");
    } else if (dstBands == 1) {
	if (srcBands == 1) {
	    m_opts->panSrcBands[0] = 1;
	} else throw InvalidInput("Unable to create warper object: unsupported band count.");
    } else throw InvalidInput("Unable to create warper object: unsupported band count.");

    m_opts->panDstBands = new int[m_opts->nBandCount];
    for (int i = 0; i < m_opts->nBandCount; ++i) m_opts->panDstBands[i] = i + 1;

    m_opts->pfnProgress = GDALDummyProgress;
//    m_opts->eResampleAlg = GRA_Cubic;
    m_opts->eResampleAlg = GRA_NearestNeighbour;
    
    m_oper = new GDALWarpOperation;
    if (m_oper->Initialize(m_opts) != CE_None) throw Exception("Unable to create warper object: GDALCreateWarpOperation failed.");
}
    
void Warper::Destroy() {
    if (m_oper) {
	delete m_oper;
	m_oper = 0;
    }
    if (m_opts) {
        if (m_opts->panSrcBands) {
    	    delete m_opts->panSrcBands;
	    m_opts->panSrcBands = 0;
	}
	if (m_opts->panDstBands) {
	    delete m_opts->panDstBands;
	    m_opts->panDstBands = 0;
	}
	GDALDestroyWarpOptions(m_opts);
	m_opts = 0;
    }
    m_needDestroy = false;
}