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

📄 myimage.cpp

📁 简单的画笔程序 完全由自己编写的
💻 CPP
字号:
// MyImage.cpp: implementation of the CMyImage class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "LY16paint.h"
#include "MyImage.h"

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

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

CMyImage::CMyImage()
{
	m_szImage = CSize(1024,768);
	m_lpBMIH = (LPBITMAPINFOHEADER) new BYTE[sizeof(BITMAPINFOHEADER)];
}

CMyImage::~CMyImage()
{
	delete[] m_lpBMIH;
}

void CMyImage::SetBmpHead(LPBITMAP pbm)
{
	m_lpBMIH->biSize = sizeof(BITMAPINFOHEADER);
	m_lpBMIH->biWidth = pbm->bmWidth;
	m_lpBMIH->biHeight = pbm->bmHeight;
	m_lpBMIH->biPlanes = pbm->bmPlanes;
	m_lpBMIH->biBitCount = pbm->bmBitsPixel;
	m_lpBMIH->biCompression = BI_RGB;
	m_lpBMIH->biSizeImage = 0;  //  can't do compression
	m_lpBMIH->biXPelsPerMeter = 0;
	m_lpBMIH->biYPelsPerMeter = 0;
	m_lpBMIH->biClrUsed = 0;
	m_lpBMIH->biClrImportant = 0; // all colors is important.

}

inline DWORD CalcWidthBytes(WORD bits,DWORD width) //  4-byte align.
{
	return (width*(bits>>3)+3)/4*4;
}

void CMyImage::InverseImage(LPBYTE lpDstBits, const LPBYTE lpSrcBits,  int nHeight, int nWidthBytes)
{
	LPBYTE lpDstBitsPtr = lpDstBits;
	LPBYTE lpSrcBitsPtr = lpSrcBits + (nHeight-1) * nWidthBytes;
	for (int i=0;i < nHeight;i++)
	{
		::memcpy(lpDstBitsPtr,lpSrcBitsPtr,nWidthBytes);
		lpDstBitsPtr += nWidthBytes;
		lpSrcBitsPtr -= nWidthBytes;
	}
}

BOOL CMyImage::Save(CBitmap* pBitmap, LPCTSTR lpszFilePath)
{
	ASSERT(pBitmap);
	BITMAP bm;
	pBitmap->GetBitmap(&bm);

	CFile* pFile = new CFile(lpszFilePath, CFile::modeCreate | CFile::modeWrite);  

	DWORD dwDataBytes = bm.bmWidthBytes * bm.bmHeight;

	BITMAPFILEHEADER bmfh;
	bmfh.bfType = 0x4d42; // "BM"
	bmfh.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
	bmfh.bfSize = bmfh.bfOffBits + dwDataBytes;
	// write file header.
	pFile->Write(&bmfh, sizeof(BITMAPFILEHEADER));

	// write BMP header.
	SetBmpHead(&bm);
	pFile->Write(m_lpBMIH, sizeof(BITMAPINFOHEADER));

	LPBYTE lpBits = new BYTE[dwDataBytes];
	pBitmap->GetBitmapBits(dwDataBytes,lpBits);

	// This Bitmap's image is inversed vertically. so we must inverse it. 
	LPBYTE lpBitsInverse = new BYTE[dwDataBytes];
	InverseImage(lpBitsInverse, lpBits, bm.bmHeight, bm.bmWidthBytes);

	// write image's bits to the file.
	pFile->Write(lpBitsInverse, dwDataBytes);

	delete[] lpBits;
	delete[] lpBitsInverse;

	delete pFile;

	return TRUE;
}

BOOL CMyImage::Load(CBitmap* pBitmap, LPCTSTR lpszFilePath)
{
	ASSERT(pBitmap);

	CFile* pFile = new CFile(lpszFilePath, CFile::modeRead);

	BITMAPFILEHEADER bmfh;
	// read file header.
	pFile->Read(&bmfh, sizeof(BITMAPFILEHEADER));
	// to see whether if type is "BM"
	if(bmfh.bfType != 0x4d42 )  //  "BM"
	{
		ASSERT(FALSE);
		return FALSE;
	}

	int nInfoSize = bmfh.bfOffBits - sizeof(BITMAPFILEHEADER);
	LPBITMAPINFOHEADER lpBMIH = (LPBITMAPINFOHEADER) new BYTE[nInfoSize];

	// read bitmap information.
    pFile->Read(lpBMIH, nInfoSize); // info hdr & color table

	WORD nBitCount = lpBMIH->biBitCount;
	// only support 32 bits.
	if( nBitCount != 32 )
	{
		// read file error.
		ASSERT(FALSE);
		return FALSE;
	}
	DWORD dwWidthBytes = CalcWidthBytes(32,lpBMIH->biWidth);
	DWORD dwSizeImage = lpBMIH->biSizeImage;
	if ( dwSizeImage == 0 ) // This may be set to zero for BI_RGB bitmaps. 
		dwSizeImage = dwWidthBytes*lpBMIH->biHeight;

	LPBYTE lpBits = new BYTE[dwSizeImage];
	pFile->Read(lpBits, dwSizeImage); // info hdr & color table

	// This Bitmap's image is inversed vertically. so we must inverse it. 
	LPBYTE lpBitsInverse = new BYTE[dwSizeImage];
	InverseImage(lpBitsInverse, lpBits, lpBMIH->biHeight, dwWidthBytes);

	pBitmap->DeleteObject();
	pBitmap->CreateBitmap(lpBMIH->biWidth, lpBMIH->biHeight,
		lpBMIH->biPlanes, lpBMIH->biBitCount, lpBitsInverse);

	delete[] lpBits;
	delete[] lpBitsInverse;
	delete[] lpBMIH;

	delete pFile;

	return TRUE;
}

⌨️ 快捷键说明

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