Click here to Skip to main content
15,886,693 members
Articles / Desktop Programming / MFC

Memory bitmap class CMemBm

Rate me:
Please Sign up or sign in to vote.
4.82/5 (9 votes)
23 Feb 2006 276.5K   9.3K   96  
A simple class to make generic Windows bitmap operations easier.
///////////////////////////////////////////////////////////////////////////////
// ExacT Soft (tm) 2001																											//
// Author: Vsevolod Gromov email: gromov@eti.co.ru;													//
// MChMemBm class. implements memory bitmap, is able to copy part of source	//
// bitmap to MemBm, or copy source DC contents to MemBm (screen capture)		//
// use _WIN32_WINDOWS for conditional compiles (necessary for proper Brush	//
// origin adjustment operations	and proper transparent draw calls						//
///////////////////////////////////////////////////////////////////////////////
#ifndef _MChMemBm_H_
#define _MChMemBm_H_

//{ memory bitmap class MChMemBm
class MChMemBm
{
public:
	// create memory bitmap from source bitmap.
	// !REMEMBER! hbmSrc MUST NOT be selected into any DC before calling this function
	// or following will just create copy of hDC contents, not the hbmSrc.
	bool Create(HDC hDC, HBITMAP hbmSrc, int iLeft, int iTop,
		int iWidth, int iHeight, DWORD dwRop = SRCCOPY );
	// create memory bitmap from explicit data
	bool Create( HDC hDC, int iWidth, int iHeight, UINT cPlanes, UINT cBitsPerPel, 
		const LPVOID lpvBits = NULL, const LPRGBQUAD pColorTable = NULL);

	//copy creation
	bool Create( const MChMemBm& bmSrc, DWORD dwRop = SRCCOPY );
	//create from bitmap resource by name
	bool Create( HINSTANCE hInst, LPCTSTR pszName );
	//create from bitmap resource by ID
	bool Create( HINSTANCE hInst, UINT nID ){ return Create( hInst, MAKEINTRESOURCE(nID) ); };

	// default constructor
	MChMemBm() { Initialize(); };
	// inplace constructor
	MChMemBm(HDC hDC, HBITMAP hbmSrc, int iLeft, int iTop,
		int iWidth, int iHeight, DWORD dwRop = SRCCOPY )
	{
		Initialize();
		Create(hDC, hbmSrc, iLeft, iTop, iWidth, iHeight, dwRop);
	};
	//copy constructor
	MChMemBm( const MChMemBm& bmSrc, DWORD dwRop = SRCCOPY )
	{
		Initialize();
		Create(bmSrc, dwRop);
	};

	// create memory bitmap from explicit data (default is monochrome bitmap)
	MChMemBm( HDC hDC, int iWidth, int iHeight,
			UINT cPlanes, UINT cBitsPerPel, const LPVOID lpvBits, const LPRGBQUAD pColorTable )
	{
		Initialize();

#ifndef MCH_USE_DDB
		if( cBitsPerPel != MCH_DIB_BPP )
			MException::Throw( M_L("Explicit DIB creation with bit depth other than ")
				M_L( MCH_TO_STRING( MCH_DIB_BPP ) )
				M_L(" is not supported") );
		if( cPlanes != 1 )
			MException::Throw( M_L("Explicit DIB creation with planes count other than 1 is not supported") );
#endif

		Create( hDC, iWidth, iHeight, cPlanes, cBitsPerPel, lpvBits, pColorTable );
	};

	//construct from resource by name
	MChMemBm( HINSTANCE hInst, LPCTSTR pszName )
	{
		Initialize();
		Create(hInst, pszName);
	};
	//construct from resource by ID
	MChMemBm( HINSTANCE hInst, UINT nID )
	{
		Initialize();
		Create(hInst, nID);
	};
	virtual ~MChMemBm(){	Cleanup();	};

public:
	//cleanup object data
	void Cleanup();
	//get bitmap width
	int GetWidth() const;
	//get bitmap height
	int GetHeight() const;
	// get bits per pixel for our bitmap
	int GetBpp() const;
	// get color planes for our bitmap
	int GetPlanes() const;
	//return our memory dc with bitmap selected in it
	//(Blt operations - ready)
	operator HDC() const { return m_hdcImg; };
	//boolean validation operator !
	bool operator !() const { return !m_bCreated; };
	//perform pattern fill, adjusting brush origin if neccessary
	void Fill( HDC hDC, int iLeft, int iTop, int iWidth, int iHeight,
		bool bAdjustBrushOrg = false, DWORD dwRop = PATCOPY );
	//do 1-to-1 draw
	void Draw( HDC hDC, int iLeft, int iTop, DWORD dwRop = SRCCOPY ) const;

	//perform draw (simple or stretched)
	void Draw( HDC hDC, int iLeft, int iTop, int iWidth, int iHeight,
		DWORD dwRop = SRCCOPY, int iStretchMode =	
#ifndef UNDER_CE
			COLORONCOLOR
#else	
			0
#endif //UNDER_CE
			) const;

	//do transparent drawing
	void DrawTrans( HDC hDC, int iLeft, int iTop, int iWidth, int iHeight,
				COLORREF crTransparent ) const;

#ifndef UNDER_CE
	//do alpha blending draw (works only when _WIN32_WINDOWS > 0x0400 else do nothing)
	void DrawAlpha( HDC hDC, int iLeft, int iTop, int iWidth, int iHeight,
		const BLENDFUNCTION& blendFunc ) const;
#endif
	//get as DIB, stuffed into byte array
	void GetAsDIB(MByteString& refDIB) const;
	//pixel manipulation
	COLORREF GetPixel(int iX, int iY) const;
	void SetPixel( int iX, int iY, COLORREF clPixel );
	//special effects support
#ifndef MCH_USE_DDB
	typedef signed short MChFilter[9];
	void ApplyFilter( const MChFilter& crefFilter );
#endif

//intrinsic typedefs
protected:
	//rgbquad wrapper helper
	struct MCH_RGBQUAD : public RGBQUAD
	{
		MCH_RGBQUAD() { Initialize(); }

		inline void Initialize() { ZeroMemory( this, sizeof(RGBQUAD) ); }

		inline operator RGBQUAD () const { return *this; }
		inline operator COLORREF () const { return RGB(rgbRed, rgbGreen, rgbBlue); }

		void operator = (COLORREF clColor) 
		{ rgbRed = ((LPBYTE)&clColor)[0]; rgbGreen = ((LPBYTE)&clColor)[1]; rgbBlue = ((LPBYTE)&clColor)[2]; }

		inline BYTE GetR() const { return rgbRed; }
		inline BYTE GetG() const { return rgbGreen; }
		inline BYTE GetB() const { return rgbBlue; }
		inline void SetR(BYTE bRed) { rgbRed = bRed; }
		inline void SetG(BYTE bGreen) { rgbGreen = bGreen; }
		inline void SetB(BYTE bBlue) { rgbBlue = bBlue; }
	};
	//bitmapinfoheader wrapper helper
	struct MCH_BITMAPINFOHEADER : public BITMAPINFOHEADER
	{
		MCH_BITMAPINFOHEADER() { Initialize(); }
		inline void Initialize() { ZeroMemory( this, sizeof(MCH_BITMAPINFOHEADER) ); biSize = sizeof(BITMAPINFOHEADER); }
		//if pTable != NULL, method uses color table data from external buffer for initialization
		//the buffer must contain at least iNumColors elements. otherwise, the default initialization
		//is used. (b\w monochrome, system 16 colors palette, etc.)
		void InitColorTable(int iNumColors, const LPRGBQUAD pTable = NULL );
		int GetNumColors() const;
		int GetPaletteByteSize() const;
		int GetImageByteSize() const;

		MCH_RGBQUAD m_Colors[256];
	};


//data members
protected:
	HANDLE	m_hbmOld;
	HBITMAP m_hbmImg;
	MChDC		m_hdcImg;
#ifdef MCH_USE_DDB
	//bitmap dimensions
	int		m_iWidth;
	int		m_iHeight;
	// number of color planes in our bitmap
	int		m_iPlanes;
	// bits per pixel in our bitmap
	int		m_iBpp;
#else
	//internal bit depth by default
	enum { MCH_DIB_BPP = 32 };

	MCH_BITMAPINFOHEADER m_bmiHeader;
	LPVOID m_pImgBuff;
#endif //MCH_USE_DDB
	
	//"object created" flag
	bool	m_bCreated;

	//reset data members to 0
	void Initialize();
	//stretching required
	bool RequireStretch( int iWidth, int iHeight ) const;
};
//} memory bitmap class MChMemBm

#endif //_MChMemBm_H_

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Software Developer (Senior) Eko-Sfera
Russian Federation Russian Federation
Born in 05.20.1971, in Moscow.
Graduated from Moscow Physical Engineering Institute in 1993.
Gained PhD. in Phys. Math. sciences in 1998.
Programmer experience over 8 years.
Assembler(s), Pascal, VBasic, JScript, ANSI C, C++.
Microcontrollers, Serial communication, MSJet DB, MFC, ATL, COM.
MSDev Studio, Borland CBuilder.
Russian, English.

Married, with one child.

Comments and Discussions