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

📄 pcx.cpp

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

//===================================================================
BOOL  FCImage::LoadPcx (BYTE * pStart, int iPcxSize)
{
	PCXHEAD		* pPcx = (PCXHEAD *) pStart ;
	BYTE		* pCurr ;

	if (pStart == NULL)
		return FALSE ;
	//	Create DIB
	if ( !this->Create (pPcx->xmax - pPcx->xmin + 1,
						pPcx->ymax - pPcx->ymin + 1,
						(WORD) pPcx->bit_per_pixel * pPcx->color_planes) )
		return FALSE ;

	//	解码
	pCurr = pStart + sizeof(PCXHEAD) ;
	for (int Y = 0 ; Y < (int)this->Height() ; Y++)
		pCurr = ::RLE_PCX_DecodeLine (pCurr, this->ColorBits(),
									  this->Width(), this->GetBits(Y)) ;

	//	填充调色板
	if (this->ColorBits() <= 8)
	{
		RGBQUAD		* pPalette = new RGBQUAD [1 << this->ColorBits()] ;
		pCurr = (this->ColorBits() <= 4) ? pPcx->palette
										 : (pStart + iPcxSize - 768) ;
		for (int i = 0 ; i < (1 << this->ColorBits()) ; i++)
		{
			pPalette[i].rgbRed = *pCurr++ ; // R
			pPalette[i].rgbGreen = *pCurr++ ; // G
			pPalette[i].rgbBlue = *pCurr++ ; // B
		}
		this->SetColorTable (0, 1 << this->ColorBits(), pPalette) ;
		delete[] pPalette ;
	}
	return TRUE ;
}
//===================================================================
BOOL  FCImage::LoadPcx (PCTSTR resName, PCTSTR resType)
{
	HRSRC	res = ::FindResource (NULL, resName, resType) ;
	HGLOBAL	gol = ::LoadResource (NULL, res) ;
	BYTE	* pPcxData = (BYTE *) ::LockResource (gol) ;
	return this->LoadPcx (pPcxData, ::SizeofResource (NULL, res)) ;
}
//===================================================================
BOOL  FCImage::LoadPcx (PCTSTR szFileName)
{
	BOOL			bRet = FALSE ;
	BYTE			* pStart = NULL ;
	HANDLE			hMap = NULL ;
	HANDLE			hFile = INVALID_HANDLE_VALUE ;

	__try
	{
		if (pFooDib->hBitmap != NULL)
			this->Unload () ;

		//	映射文件
		pStart = this->__fooImageReadFile (szFileName, &hFile, &hMap) ;
		if (pStart == NULL)
			__leave ;
		if (!this->LoadPcx (pStart, ::GetFileSize(hFile, NULL))) // 读文件
			__leave ;
		bRet = TRUE ;
	}
	__finally
	{
		this->__fooImageUnmapFile ((BYTE *)pStart, hMap, hFile) ;
	}
	return bRet ;
}
//===================================================================
BOOL  FCImage::SavePcx (PCTSTR szFileName)
{
	BOOL				bRet = FALSE ;
	HANDLE				hMap = NULL ;
	HANDLE				hFile = INVALID_HANDLE_VALUE ;
	BYTE				* pStart = NULL, * pCurr = NULL ;
	PCXHEAD				PcxHead ;
	DWORD				Y ;
	RGBQUAD				rgb ;

	__try
	{
		if (pFooDib->hBitmap == NULL)
			__leave ;
		if ( (this->ColorBits() == 16) || (this->ColorBits() == 32) )
			__leave ;

		//	创建文件
		pStart = this->__fooImageSaveFile (szFileName, &hFile, &hMap, this->GetPitch() * this->Height() * 2 + 2048) ;
		if (pStart == NULL)
			__leave ;

		// Init PCX Header
		ZeroMemory (&PcxHead, sizeof(PcxHead)) ;
		PcxHead.signal			= 0x0A ;
		PcxHead.version			= 5 ;
		PcxHead.encoding		= 1 ;
		PcxHead.bit_per_pixel	= this->ColorBits() % 16 ;
		PcxHead.xmax			= (WORD) this->Width() - 1 ;
		PcxHead.ymax			= (WORD) this->Height() - 1 ;
		PcxHead.Xresolution		= (WORD) GetSystemMetrics (SM_CXSCREEN) ;
		PcxHead.Yresolution		= (WORD) GetSystemMetrics (SM_CYSCREEN) ;
		PcxHead.color_planes	= (this->ColorBits() == 24) ? 3 : 1 ;
		PcxHead.byte_per_line	= (WORD) 2 * ((this->Width() * PcxHead.bit_per_pixel + 15) / 16) ; // 每平面每行字节数
		PcxHead.palette_type	= 1 ;

		// Set 1-or-4 bit color table
		if (this->ColorBits() <= 4)
			for (Y = 0 ; Y < (DWORD)1 << this->ColorBits() ; Y ++)
			{
				this->GetColorTable (Y, &rgb) ;
				PcxHead.palette [Y * 3    ] = rgb.rgbRed ;
				PcxHead.palette [Y * 3 + 1] = rgb.rgbGreen ;
				PcxHead.palette [Y * 3 + 2] = rgb.rgbBlue ;
			}

		// 写头
		CopyMemory (pStart, &PcxHead, sizeof(PCXHEAD)) ;

		// 压缩编码
		pCurr = pStart + sizeof(PCXHEAD) ;
		for (Y = 0 ; Y < this->Height() ; Y ++)
			pCurr = ::RLE_PCX_EncodeLine (this->GetBits (Y), this->ColorBits(),
										  this->Width(), pCurr) ;
		// 8-bit color
		*pCurr++ = 0x0C ; // 调色板写在文件尾部
		if (this->ColorBits() == 8)
			for (Y = 0 ; Y < 256 ; Y++)
			{
				this->GetColorTable (Y, &rgb) ;
				*pCurr++ = rgb.rgbRed ;
				*pCurr++ = rgb.rgbGreen ;
				*pCurr++ = rgb.rgbBlue ;
			}
		else
			pCurr += 768 ;
		bRet = TRUE ;
	}
	__finally
	{
		if (pStart != NULL)	::UnmapViewOfFile (pStart) ;
		if (hMap != NULL)
		{
			::CloseHandle (hMap) ;
			::SetFilePointer (hFile, pCurr - pStart, NULL, FILE_BEGIN) ;
			::SetEndOfFile (hFile) ; // 设置文件大小
		}
		if (hFile != INVALID_HANDLE_VALUE)	::CloseHandle (hFile) ;
	}
	return bRet ;
}
//===================================================================

⌨️ 快捷键说明

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