www.gusucode.com > VC++遥感原理与数字摄影测量实习源码程序 > VC++遥感原理与数字摄影测量实习源码程序\code\RVI\RVI.cpp

    // RVI.cpp : Defines the entry point for the console application.
// Download by http://www.NewXing.com

#include "stdafx.h"
#include "RVI.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	if (argc != 5)
	{
		::MessageBox(NULL,"启动参数不正确,请从GUI.exe启动","",MB_ICONASTERISK);
		return -1;
	}
	system("Mode con: COLS=50 LINES=15");
	system("title RVI");
	int nRetCode = 0;
	
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
		return nRetCode;
	}
	
	CString file7,file5,file,flag;	//启动参数
	file7 = argv[1];
	file5 = argv[2];
	file = argv[3];
	flag = argv[4];		//判断是否在创建结果文件后显示
	int m_flag = atoi(flag);

	if (GetFileAttributes(file7) == -1)
	{
		//the module does not exist
		CString err;
		err.Format("文件:%s不存在", file7);		
		::MessageBox(NULL,err, "Error", MB_ICONERROR);
		return -1;
	}
	if (GetFileAttributes(file5) == -1)
	{
		//the module does not exist
		CString err;
		err.Format("文件:%s不存在", file5);		
		::MessageBox(NULL,err, "Error", MB_ICONERROR);
		return -1;
	}

	//////////////////////////////////////////////////////////////////////////
	FILE* pFile7 = NULL;
	FILE* pFile5 = NULL;
	pFile7 = fopen(file7,"rb");
	if(pFile7 == NULL)
	{
		printf("IR波段图像打开失败\n");
		nRetCode = 1;
		return nRetCode;
	}
	pFile5 = fopen(file5,"rb");
	if(pFile5 == NULL)
	{
		printf("RED波段图像打开失败\n");
		nRetCode = 1;
		return nRetCode;
	}

	BITMAPFILEHEADER bmfh7, bmfh5;
	BITMAPINFOHEADER bmih7, bmih5;

	fread(&bmfh7, sizeof(BITMAPFILEHEADER), 1, pFile7);
	fread(&bmfh5, sizeof(BITMAPFILEHEADER), 1, pFile5);

	fread(&bmih7, sizeof(BITMAPINFOHEADER), 1, pFile7);
	fread(&bmih5, sizeof(BITMAPINFOHEADER), 1, pFile5);

	if(bmih7.biBitCount != 8 || bmih5.biBitCount != 8)
	{
		printf("只支持8位位图\n");
		nRetCode = 1;
		return nRetCode;
	}

	DWORD width = bmih7.biWidth;
	DWORD height = bmih7.biHeight;
	DWORD _width = (width*8 + 31)/32*4;	//saved width in files
	DWORD pixelCount = _width*height;

	if(width != bmih5.biWidth || height != bmih5.biHeight)
	{
		printf("两幅影像尺寸不一致\n");
		nRetCode = 1;
		return nRetCode;
	}

	RGBQUAD rgbquad_7[256];
	RGBQUAD rgbquad_5[256];

	fread(rgbquad_7,sizeof(RGBQUAD),256,pFile7);
	fread(rgbquad_5,sizeof(RGBQUAD),256,pFile5);

	BYTE* pBits7 = new BYTE[pixelCount];
	BYTE* pBits5 = new BYTE[pixelCount];

	fread(pBits7,sizeof(BYTE),pixelCount,pFile7);
	fread(pBits5,sizeof(BYTE),pixelCount,pFile5);

	fclose(pFile7);
	fclose(pFile5);

	double* pExponent = new double[pixelCount];

	DWORD i(0);
	double temp(0);
	double max = -100;
	double min = 100;
	BYTE* p7 = pBits7;
	BYTE* p5 = pBits5;
	double* pEx = pExponent;
	for(; i < pixelCount; ++ i)
	{
		temp = *p7 + *p5;
		
		if(*p5)	//如果 B5 != 0,则计算该点指数
		{
			*pEx = *p7 / *p5;

			if(max < *pEx)
				max = *pEx;
			else if(min > *pEx)
				min = *pEx;
		}
		else
			*pEx = 0;
		
		pEx++;
		p7++;
		p5++;
	}	

	//将指数归化到0-255的范围内保存为位图
	pEx = pExponent;
	for(i = 0; i < pixelCount; ++i)
	{
		*pEx = *pEx * 127.5 + 127.5;		//灰度线性变换
		pEx++;
	}

	//保存位图
	BITMAPINFOHEADER bmih = bmih7;
	bmih.biWidth = _width;

	FILE* pFile = fopen(file,"wb");
	if(pFile == NULL)
	{
		printf("结果文件创建失败\n");
		nRetCode = 1;
		return nRetCode;
	}
	
	fwrite(&bmfh7,sizeof(BITMAPFILEHEADER),1,pFile);
	fwrite(&bmih,sizeof(BITMAPINFOHEADER),1,pFile);
	fwrite(rgbquad_7,sizeof(RGBQUAD),256,pFile);

	pEx = pExponent;
	BYTE grayscale(0);
	for(i = 0; i < pixelCount; ++i)
	{
		grayscale = (BYTE)*pEx++;
		fwrite(&grayscale,1,1,pFile);
	}
	fclose(pFile);

	//clean up
	delete[] pBits7;
	delete[] pBits5;
	delete[] pExponent;
	pBits7 = p7 = NULL;
	pBits5 = p5 = NULL;
	pExponent = pEx = NULL;

	if (m_flag == 1)
	{
		ShellExecute(NULL, "open", file, NULL, NULL, SW_SHOWNORMAL);
	}

	return nRetCode;
}