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

📄 bmp.cpp

📁 一个国人自己实现图像库的程序(有参考价值)
💻 CPP
字号:
#include "stdafx.h"
#include "..\..\Include\Pic\Image.h"

//===================================================================
BOOL  FCImage::LoadBmp (PCTSTR szFileName)
{
	BOOL				bRet = FALSE ;
	BITMAPFILEHEADER  * pStart = NULL ;
	HANDLE				hMap = NULL ;
	HANDLE				hFile = INVALID_HANDLE_VALUE ;

	__try
	{
		// mapping image file
		pStart = (BITMAPFILEHEADER *) this->__fooImageReadFile (szFileName, &hFile, &hMap) ;
		if (pStart == NULL)
			__leave ;

		// not support masked 16 bit color DIB
		BITMAPINFOHEADER	* pBmif = (BITMAPINFOHEADER *)(pStart + 1) ;
		if (pBmif->biCompression == BI_BITFIELDS)
			__leave ;

		// Create Dib
		if (!this->Create (pBmif->biWidth, abs(pBmif->biHeight), pBmif->biBitCount))
			__leave ;

		// read the pixel value
		DWORD		dwPitch = this->GetPitch() ;
		BYTE		* pCurr = (BYTE *)pStart + pStart->bfOffBits ;
		if (pBmif->biHeight > 0)
			::CopyMemory (pFooDib->pByte, pCurr, dwPitch * this->Height()) ;
		else // top to down DIB
		{
			for (int i = 0 ; i < Height() ; i++, pCurr += dwPitch)
				::CopyMemory (this->GetBits (i), pCurr, dwPitch) ;
		}

		// set a palette to a DIB whose colorbit <= 8
		if (this->ColorBits() <= 8)
			this->SetColorTable (0, 1 << this->ColorBits(), (RGBQUAD *)((BYTE *)pBmif + pBmif->biSize)) ;
		bRet = TRUE ;
	}
	__finally
	{
		this->__fooImageUnmapFile ((BYTE *)pStart, hMap, hFile) ;
	}
	return bRet ;
}
//===================================================================
BOOL  FCImage::SaveBmp (PCTSTR szFileName)
{
	if (this->GetHandle () == NULL)
		return FALSE ;

	// calculate the image file size
	DWORD		dwTotalSize,
				ColorSize = (this->ColorBits() <= 8) ? (1 << this->ColorBits()) : 0,
				dwPitch = this->GetPitch () ;
	dwTotalSize = dwPitch * this->Height() + sizeof(pFooDib->DibInfo) + ColorSize * 4 + sizeof (BITMAPFILEHEADER) ;

	// create file
	BITMAPFILEHEADER  * pStart = NULL ;
	HANDLE				hMap = NULL ;
	HANDLE				hFile = INVALID_HANDLE_VALUE ;
	pStart = (BITMAPFILEHEADER *) this->__fooImageSaveFile (szFileName, &hFile, &hMap, dwTotalSize) ;
	if (pStart != NULL)
	{
		// write bmp file header
		pStart->bfType      = * (WORD *) "BM" ;
		pStart->bfSize      = dwTotalSize ;
		pStart->bfReserved1 = 0 ;
		pStart->bfReserved2 = 0 ;
		pStart->bfOffBits   = dwTotalSize - dwPitch * this->Height() ;

		// write BMP file info header
		BITMAPINFOHEADER	* pBmif = (BITMAPINFOHEADER *)(pStart + 1) ;
		::CopyMemory (pBmif++, &pFooDib->DibInfo, sizeof(pFooDib->DibInfo)) ;

		// write palette into file
		if (this->ColorBits() <= 8)
			this->GetColorTable (0, ColorSize, (RGBQUAD *)pBmif) ;

		// write pixels value
		::CopyMemory ((BYTE *)pStart + pStart->bfOffBits, pFooDib->pByte, dwPitch * this->Height()) ;
	}
	this->__fooImageUnmapFile ((BYTE *)pStart, hMap, hFile) ;
	return TRUE ;
}
//===================================================================

⌨️ 快捷键说明

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