#pragma once
/**
 * 影像数据操作项
 * 所有数据相关的操作，都是基于ENVI的数据格式
 * 影像读写操作时基于 GDAL 库
 * **/



#ifndef IMAGEOPERATORBASE_H
#define IMAGEOPERATORBASE_H

#include "../LAMPTool/BaseToollib/BaseConstVariable.h"
#include "../LAMPTool/BaseToollib/GeoOperator.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <Eigen/Core>
#include <Eigen/Dense>
#include <complex>
#include <gdal.h>
#include <gdal_priv.h>
#include <gdalwarper.h>
#include <string>
#include <memory>
#include <QString>
#include <cpl_conv.h> // for CPLMalloc()
#include "../LAMPTool/referenceHeader.h"
using namespace std;
using namespace Eigen;

struct ImageGEOINFO {
	int width;
	int height;
	int bandnum;
};


// 判断是否需要输出为DLL
#define DLLOUT
// 文件打开
std::shared_ptr<GDALDataset> OpenDataset(const QString& in_path, GDALAccess rwmode= GA_ReadOnly); // 当指令销毁时，调用GDALClose 销毁类型
void CloseDataset(GDALDataset* ptr);

// 数据格式转换
int TIFF2ENVI(QString in_tiff_path,QString out_envi_path); 
int ENVI2TIFF(QString in_envi_path,QString out_tiff_path);

// 保存影像数据 --直接保存 ENVI 文件

int CreateDataset(QString new_file_path, int height, int width, int band_num, double* gt, QString projection, GDALDataType gdal_dtype, bool need_gt); // 创建文件

int saveDataset(QString new_file_path, int start_line, int start_cols, int band_ids, int datacols, int datarows, void* databuffer);

// 根据限制条件估算分块大小
int block_num_pre_memory(int width, int height, GDALDataType gdal_dtype,double memey_size);

// 将结果转换为复数 或者 实数
Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor> ReadComplexMatrixData(int start_line,int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype);

Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::RowMajor>  ReadMatrixDoubleData(int start_line, int width, int line_num, std::shared_ptr<GDALDataset> rasterDataset, GDALDataType gdal_datatype,int band_idx);

Eigen::MatrixXd getGeoTranslationArray(QString in_path);
ImageGEOINFO getImageINFO(QString in_path);

GDALDataType getGDALDataType(QString fileptah);


struct DemBox {
	double min_lat; //纬度
	double min_lon;//经度
	double max_lat;//纬度
	double max_lon;//经度
};


/// <summary>
/// gdalImage图像操作类
/// </summary>
class gdalImage
{

public: // 方法
	gdalImage();
	gdalImage(const QString& raster_path);
	~gdalImage();
	virtual void setHeight(int);
	virtual void setWidth(int);
	virtual void setTranslationMatrix(Eigen::MatrixXd gt);
	virtual void setData(Eigen::MatrixXd,int data_band_ids=1);
	virtual Eigen::MatrixXd getData(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
	virtual Eigen::MatrixXd getGeoTranslation();
	virtual GDALDataType getDataType();
	virtual void saveImage(Eigen::MatrixXd, int start_row, int start_col, int band_ids);
	virtual void saveImage();
	virtual void setNoDataValue(double nodatavalue, int band_ids);
	virtual int InitInv_gt();
	virtual Landpoint getRow_Col(double lon, double lat);
	virtual Landpoint getLandPoint(double i, double j, double ati);
	virtual double mean(int bandids = 1);
	virtual double max(int bandids = 1);
	virtual double min(int bandids = 1);
	virtual GDALRPCInfo getRPC();
	virtual Eigen::MatrixXd getLandPoint(Eigen::MatrixXd points);
 
	virtual Eigen::MatrixXd getHist(int bandids);
public:
	QString img_path; // 图像文件
	int height; // 高
	int width; // 宽
	int band_num;// 波段数
	int start_row;// 
	int start_col;//
	int data_band_ids;
	Eigen::MatrixXd gt; // 变换矩阵
	Eigen::MatrixXd inv_gt; // 逆变换矩阵
	Eigen::MatrixXd data;
	QString projection;
};







/// <summary>
/// gdalImage图像操作类
/// </summary>
class gdalImageComplex:public gdalImage
{

public: // 方法
	gdalImageComplex(const QString& raster_path);
	~gdalImageComplex();
	void setData(Eigen::MatrixXcd);
	 void saveImage(Eigen::MatrixXcd data, int start_row, int start_col, int band_ids);
	 Eigen::MatrixXcd getDataComplex(int start_row, int start_col, int rows_count, int cols_count, int band_ids);
 	 void saveImage() override;

public:
	Eigen::MatrixXcd data;
};














gdalImage CreategdalImage(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false);
gdalImageComplex CreategdalImageComplex(const QString& img_path, int height, int width, int band_num, Eigen::MatrixXd gt, QString projection, bool need_gt = true, bool overwrite = false);




int ResampleGDAL(const char* pszSrcFile, const char* pszOutFile, double* gt, int new_width, int new_height, GDALResampleAlg eResample);

int ResampleGDALs(const char* pszSrcFile, int band_ids, GDALRIOResampleAlg eResample = GRIORA_Bilinear);


//--------------------- 保存文博 -------------------------------

int saveMatrixXcd2TiFF(Eigen::MatrixXcd data, QString out_tiff_path);
 
//----------------------------------------------------



#ifndef DLLOUT





#else
//#define DllExport   __declspec( dllexport )
//double __declspec(dllexport) ProcessMGCMathX_MGC(int Xbetaidx, int Xbwidx, double XTao, double satH, char* sigma_path, char* output_path,
//	double p1_x, double p1_y, double p2_x, double p2_y, double p3_x, double p3_y, double p4_x, double p4_y)

#endif

#endif