⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dib.cpp

📁 电脑编程技巧和源码。很不错的。
💻 CPP
字号:
// Dib.cpp: implementation of the CDib class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Test.h"
#include "Dib.h"
#include "HistogramDialog.h"
#include "ImageSegmentation.h"

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

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CDib::CDib(LPCSTR lpcszFileName)
{
	LoadBitmapFile( lpcszFileName );

}

CDib::~CDib()
{
	delete []m_lpBmpInfo;
}

VOID CDib::LoadBitmapFile(LPCSTR lpcszFileName)
{
	BITMAPFILEHEADER	bmpFileHeader;
	CFile				file( lpcszFileName, CFile::modeRead );

	file.Read( (void*)&bmpFileHeader, sizeof( bmpFileHeader ) );

	if( bmpFileHeader.bfType != 0x4d42 )
	{
		AfxMessageBox( "Not a bitmap file." );

		m_lpBmpInfo			= NULL;
		m_lpBmpInfoHeader	= NULL;
		m_lpColorTable		= NULL;
		m_lpImageData		= NULL;
		m_numColors			= 0;
	}
	else
	{
		DWORD fileLength	= file.GetLength();
		DWORD dibSize		= fileLength - sizeof( bmpFileHeader );
		BYTE*  pDib			= new BYTE[ dibSize ];
		file.Read( (void*)pDib, dibSize );
		file.Close();

		m_lpBmpInfo			= (LPBITMAPINFO) pDib;
		m_lpBmpInfoHeader	= (LPBITMAPINFOHEADER) pDib;
		m_lpColorTable		= (RGBQUAD*)( pDib + m_lpBmpInfoHeader->biSize );

		//	计算图象颜色数
		if ( (m_lpBmpInfoHeader->biClrUsed == 0) && (m_lpBmpInfoHeader->biBitCount < 9) )
			m_numColors = (1 << m_lpBmpInfoHeader->biBitCount);
		else
			m_numColors = (UINT) m_lpBmpInfoHeader->biClrUsed;

		m_lpImageData	= (LPBYTE)m_lpColorTable + m_numColors*sizeof(RGBQUAD); 
		m_nBytesPerRow	= ( GetBmpWidth()+3 )/4*4;

		StatHistogramInfo();

	}

}

void CDib::StatHistogramInfo()
{
	if( m_numColors == 256 )
	{
		//	统计灰度直方图信息
		int i;
		LONG w, h;

		memset( (void*)m_naHistogram, 0, 256*sizeof(LONG) );

		for( h =0; h<GetBmpHeight(); h++ )
			for( w = 0; w<GetBmpWidth(); w++ )
				m_naHistogram[ *(m_lpImageData+h*m_nBytesPerRow+w) ]++;

		m_nMaxCount = 0;
		for( i=0; i<256; i++ )
		{
			if( m_nMaxCount < m_naHistogram[i] )
				m_nMaxCount = m_naHistogram[i];
		}
	}
}

void CDib::Show(CDC *pDC)
{
	if( !m_lpBmpInfo )
		return;
	
	HPALETTE hPal, hOldPal;

	if( m_lpBmpInfoHeader->biBitCount <= 8 )
	{
		LPLOGPALETTE pLogPal = (LPLOGPALETTE) new char[2 * sizeof(WORD) + m_numColors * sizeof(PALETTEENTRY)];
		pLogPal->palVersion = 0x300;
		pLogPal->palNumEntries = m_numColors;
		LPRGBQUAD pDibQuad = (LPRGBQUAD) m_lpColorTable;
		for(UINT i = 0; i < m_numColors; i++) 
		{
			pLogPal->palPalEntry[i].peRed = pDibQuad->rgbRed;
			pLogPal->palPalEntry[i].peGreen = pDibQuad->rgbGreen;
			pLogPal->palPalEntry[i].peBlue = pDibQuad->rgbBlue;
			pLogPal->palPalEntry[i].peFlags = 0;
			pDibQuad++;
		}

		hPal	= ::CreatePalette( (LPLOGPALETTE)pLogPal );
		hOldPal	= ::SelectPalette( pDC->m_hDC, hPal, FALSE );
		::RealizePalette( pDC->m_hDC );
	}
	
	LONG	bmpWidth	= GetBmpWidth();
	LONG	bmpHeight	= GetBmpHeight();

	pDC->SetStretchBltMode(COLORONCOLOR);
	StretchDIBits( pDC->m_hDC,
		0, 0, bmpWidth, bmpHeight,
		0, 0, bmpWidth, bmpHeight,
		(void*)m_lpImageData, m_lpBmpInfo,
		DIB_RGB_COLORS, SRCCOPY
		);

	if( m_lpBmpInfoHeader->biBitCount <= 8 )
	{
		::SelectPalette( pDC->m_hDC, hOldPal, FALSE );
		::DeleteObject( hPal );
	}

}

void CDib::HistogramDialog()
{
	CHistogramDialog	dlg(this);
	dlg.DoModal();
}

void CDib::HighpassFiltering()
{
	CImageSegmentation	imageSeg( this );
	imageSeg.HighpassFiltering();

}

void CDib::MaskFiltering()
{
	CImageSegmentation	imageSeg( this );
	imageSeg.MaskFiltering();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -