#include <QFile>
#include "QtLinearToIntenisityDialog.h"
#include <QtWidgets>
#include "ImageOperatorBase.h"
#include "ui_QtLinearToIntenisityDialog.h"


QtLinearToIntenisityDialog::QtLinearToIntenisityDialog(QWidget* parent)
	: QDialog(parent), ui(new Ui::QtLinearToIntenisityDialogClass)
{
	ui->setupUi(this);

	connect(ui->pushButtonInRaster, SIGNAL(clicked(bool)), this, SLOT(On_pushButtonInRasterTriggled()));
	connect(ui->pushButtonSave, SIGNAL(clicked(bool)), this, SLOT(On_pushButtonSaveTriggled()));
	connect(ui->radioButtonIntenisty, SIGNAL(clicked(bool)), this, SLOT(On_radioButtonIntenistyTriggled()));
	connect(ui->radioButtonAmp, SIGNAL(clicked(bool)), this, SLOT(On_radioButtonAmpTriggled()));
	connect(ui->buttonBox, SIGNAL(accepted()), this, SLOT(onbtnaccepted()));
	connect(ui->buttonBox, SIGNAL(rejected()), this, SLOT(onbtnrejected()));
}

QtLinearToIntenisityDialog::~QtLinearToIntenisityDialog()
{
}

void QtLinearToIntenisityDialog::On_pushButtonInRasterTriggled()
{
	QString fileName = QFileDialog::getOpenFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->InputRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtLinearToIntenisityDialog::On_pushButtonSaveTriggled()
{
	QString fileName = QFileDialog::getSaveFileName(
		this,                           // 父窗口
		tr(u8"选择影像"),               // 标题
		QString(),                      // 默认路径
		tr(u8"tif Files (*.tif);;data Files (*.data);;bin Files (*.bin);;All Files (*)")  // 文件过滤器
	);

	// 如果用户选择了文件
	if (!fileName.isEmpty()) {
		this->ui->OutputRasterLineEdit->setText(fileName);

	}
	else {
		QMessageBox::information(this, tr(u8"没有选择文件"), tr(u8"没有选择任何文件。"));
	}
}

void QtLinearToIntenisityDialog::On_radioButtonIntenistyTriggled()
{

}

void QtLinearToIntenisityDialog::On_radioButtonAmpTriggled()
{

}

void QtLinearToIntenisityDialog::onbtnaccepted()
{
	this->hide();
	QString inputRaster = this->ui->InputRasterLineEdit->text();
	QString outputRaster = this->ui->OutputRasterLineEdit->text();
	// 复制 inputRaster 到 outputRaster,如果存在就覆盖
	QFile::copy(inputRaster, outputRaster);


	gdalImage img(outputRaster);
	long rowblock = Memory1GB / img.width / 8 * 2;
	ui->progressBar->setMaximum(img.height);
	ui->progressBar->setValue(0);
	for (long rid = 0; rid < img.height; rid = rid + rowblock)
	{
		for (long bid = 1; bid <= img.band_num; bid++)
		{
			Eigen::MatrixXd data = img.getData(rid, 0, rowblock, img.width, bid);

			for (long i = 0; i < data.rows(); i++)
			{
				for (long j = 0; j < data.cols(); j++)
				{
					if (ui->radioButtonAmp->isChecked())
					{
						data(i, j) = std::pow(10.0,data(i,j)/20.0);
					}
					else if (ui->radioButtonIntenisty->isChecked())
					{
						data(i, j) = std::pow(10.0, data(i, j) / 10.0);
					}
					else
					{
					}
				}
			}
			img.saveImage(data, rid, 0, bid);
		}
		ui->progressBar->setValue(rid);

	}
	ui->progressBar->setValue(img.height);

	QMessageBox::information(this, tr(u8"提示"), tr(u8"completed!!!"));
}

void QtLinearToIntenisityDialog::onbtnrejected()
{
	this->close();
}
