0
点赞
收藏
分享

微信扫一扫

ImgFileInfo.cpp


// ImgFileInfo.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ImgFileInfo.h"
#include <stdio.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;

BOOL imgFileInfo(LPCTSTR lpcPathName)
{
	CFile file;
	if(!file.Open(lpcPathName,CFile::modeRead | CFile::shareDenyWrite))
		return FALSE;
//	LPBYTE *lpData;
	BITMAPINFOHEADER *pBMIH;
	LPVOID lpvColorTable=NULL;
	int nColorTableEntries;//颜色表颜色数目
	BITMAPFILEHEADER bmfHeader;

	//读取文件头
	if(!file.Read(&bmfHeader,sizeof(bmfHeader)))
	{
		return FALSE;
	}
	//检查开头两字节是否为BM
	if(bmfHeader.bfType != MAKEWORD('B','M'))
	{
		return FALSE;
	}
	//读取信息头
	pBMIH=new BITMAPINFOHEADER();
	if(!file.Read(pBMIH,sizeof(BITMAPINFOHEADER)))
	{
		delete pBMIH;
		return FALSE;
	}
	//定位到颜色表
	nColorTableEntries=(bmfHeader.bfOffBits-sizeof(bmfHeader)-sizeof(BITMAPINFOHEADER))/sizeof(RGBQUAD);

	if(nColorTableEntries>0)
	{
		lpvColorTable=pBMIH+1;
	}
	//pBMIH->biHeight=abs(pBMIH->biHeight);
	printf("Width:%ld,Height:%ld\n",pBMIH->biWidth,pBMIH->biHeight);
	printf("Size:%ld,sizeof(BITMAPINFOHEADER):%d,BitCount:%d\n",pBMIH->biSize,sizeof(BITMAPINFOHEADER),pBMIH->biBitCount);

	#define WIDTHBYTES(bits) (((bits) + 31) / 32 * 4)
	int nWidthBytes=WIDTHBYTES((pBMIH->biWidth)*(pBMIH->biBitCount));
	printf("biWidth*biBitCount/8=:%ld,WIDTHBYTES(biWidth*biBitCount):%d\n",(pBMIH->biWidth)*(pBMIH->biBitCount)/8,nWidthBytes);


	delete pBMIH;
	return TRUE;
}



int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	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;
	}
	else
	{
		// TODO: code your application's behavior here.
		//CString strHello;
		//strHello.LoadString(IDS_HELLO);
		//cout << (LPCTSTR)strHello << endl;

		imgFileInfo(argv[1]);

	}

	return nRetCode;
}



举报

相关推荐

0 条评论