Click here to Skip to main content
11,633,983 members (65,868 online)
Click here to Skip to main content
Add your own
alternative version

A C++ implementation of Douglas-Peucker Line Approximation Algorithm

, 3 Mar 2003 633.2K 11.1K 120
DP Line approximation algorithm is a well-known method to approximate 2D lines. It is quite fast, O(nlog_2(n)) for a n-points line and can drastically compress a data curve. Here, a fully OOP implementation is given.
#include "gl/gl.h"
#include "CRGBSurface.h"

#ifndef _CGLIMAGE_H_
#define _CGLIMAGE_H_

typedef GLvoid* GL_PTR_TO_VOID;
typedef BYTE*	PTR_TO_BYTE;

/*! \brief An rendering context for GL.

\ingroup OGLToolsGroup
 CGLImage class adds red and blue component swaping 
 to some CRGBSurface::Create() methods.

 A GDI DIB section object created by CGLImage,
 may be used directly as a source image 
 for OpenGL texture object, as OpenGL or GDI rendering surface,
 or for a direct bit manipulations.
  

Examples:
\code
	CGLImage glImage;
	glImage.Create(10, 10, GL_RGBA);  // create empty, black image
	glImage.Create(&CBitmapObject);	  // create from DDB bitmap GDI object
	glImage.Create("D:/TestImg/testimg.bmp", GL_RGBA); // create from file DIB 
	glImage.Render(0, 100);   // render image                               
	GLvoid* ptr = glImage.GetPixels();  // get save pointer for direct bit manipulation
	glTexImage2D(...., glImage); // use GL_PTR_TO_VOID() conversion operator to get save pointer
\endcode
\sa CRGBSurface
\sa CWGL
\author (c) W.Weyna 'Voytec', http://shiptech.tuniv.szczecin.pl/~weyna/OpenGL/
*/
class CGLImage : public CRGBSurface
{
// Attributes
public:
	/*! \name Attributes: */
	//@{
	/*! \brief Get Frame Pixels

	Flushes OpenGL calls and returns a pointer to the frame buffer.
	\la CRGB::GetPixels
	*/
	GLvoid* GetPixels() {glFlush(); return (GLvoid*)CRGBSurface::GetPixels();}
	/*! \brief Pixel format
	
	Returns pixel format :
	<ul>
	<li> GL_RGB 24 bits.
	<li> GL_RGBA 32 bits
	</ul>
	It produces an assert otherwize.
	
	\sa GetImagePixelFormatName
	\sa CWGL::SetPixelFormat
	*/
	int GetImagePixelFormat();
	/*! \brief Pixel format
	
	Returns pixel format :
	<ul>
	<li> "GL_RGB" 24 bits.
	<li> "GL_RGBA" 32 bits
	</ul>
	It produces an assert otherwize.
	\sa GetImagePixelFormatName
	\sa CWGL::SetPixelFormat
	*/
	const char* GetImagePixelFormatName();
	//@}
// Convertions
public:
	/*! \name Conversions: */
	//@{
	//! returns GetPixels()
	operator GL_PTR_TO_VOID() {return GetPixels(); }
	//! returns (BYTE*)GetPixels()
	operator PTR_TO_BYTE() {return (BYTE*)GetPixels(); }
	//@}

// Operations
public:  
	/*! \name Operations:
	*/
	//@{
	//! Create DIB using CRGBSurface::Create(CRGBSurface* const)
	bool Create(CRGBSurface* const pSourceSurface)
		{return CRGBSurface::Create(pSourceSurface); }  // must be explisitly declared because of ambiguity with Create(const HBITMAP hBitmap ...
	/*! \brief  Create cx x cy DIB

	\param cx width of DIB
	\param cy height of DIB
	\param nBitsPerPixel nb of pits per pixels (default is 24). RGBA image need 32.
	\sa CRGBSurface::Create(int, int, int)
	*/
	bool Create(int cx, int cy, int nBitsPerPixel = 24)
		{return CRGBSurface::Create(cx, cy, GLPFtoBPP(nBitsPerPixel));}

	//! Create DIB using CRGBSurface::Create(HBITMAP,int,CDC*) then swap Red and Blue.
	bool Create(const HBITMAP hBitmap, int nBitsPerPixel = 24, CDC* pDC = NULL)
		{bool bRetVal = CRGBSurface::Create(hBitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}
	//! Create DIB using CRGBSurface::Create(CBitmap*,int,CDC*) then swap Red and Blue.
	bool Create(CBitmap* pBitmap, int nBitsPerPixel = 24, CDC* pDC = NULL) 
		{bool bRetVal = CRGBSurface::Create(pBitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}	
	//! Create DIB using CRGBSurface::Create(const CBitmap&,int,CDC*) then swap Red and Blue.
	bool Create(const CBitmap& bitmap, int nBitsPerPixel = 24, CDC* pDC = NULL)
		{bool bRetVal = CRGBSurface::Create(bitmap, GLPFtoBPP(nBitsPerPixel), pDC); SwapRB(); return bRetVal;}
	//! Create DIB using CRGBSurface::Create(const char*,int) then swap Red and Blue.	
	bool Create(const CString& strPath, int nBitsPerPixel = 24)
		{bool bRetVal = CRGBSurface::Create(strPath, GLPFtoBPP(nBitsPerPixel)); SwapRB(); return bRetVal;}

	//! Wraper to glDrawPixels().
	void Render(int x, int y, int z = 0);

	//! Sets the pixel storage mode to GL_UNPACK_ALIGNMENT.
	void PixelStore() { glPixelStorei(GL_UNPACK_ALIGNMENT, 4); }

	/*! \brief Swap red and blue bytes in all pixels of DIB section.
	*/
	void SwapRB();
	/*! \brief Convert pixel mode from RGB to RGBA
	
	Convert from OpenGL pixel format name GL_RGB or GL_RGBA
	to a number of bits per pixel.
	If bits per pixel are passed instead of pixel format name,
	check if this number is valid for OpenGL image (24 bits or 32 bits)
	 and return valid value;
	*/
	int GLPFtoBPP(const int nGLPForBPP);
	//@}
public:
	/*! \name Multi-Threading
	*/
	//@{
	//! Locks the object on this thread
	BOOL Lock();
	//! Unlocks the object on this thread
	BOOL Unlock();
	//! returns TRUE if locked
	BOOL IsLocked() const;
	//@}
protected:
	CMutex* m_pThreadSynchro;
	CSingleLock* m_pLock;
};

#endif

////////////////////////////////////////////////////////////

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

Share

About the Author

Jonathan de Halleux
Engineer
United States United States
Jonathan de Halleux is Civil Engineer in Applied Mathematics. He finished his PhD in 2004 in the rainy country of Belgium. After 2 years in the Common Language Runtime (i.e. .net), he is now working at Microsoft Research on Pex (http://research.microsoft.com/pex).

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150728.1 | Last Updated 4 Mar 2003
Article Copyright 2002 by Jonathan de Halleux
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid