Click here to Skip to main content
Click here to Skip to main content
Articles » Multimedia » GDI » General » Downloads
 
Add your own
alternative version

QuickFill: An efficient flood fill algorithm.

, 12 Mar 2004
Design and implimentation of efficient flood fill algorithms.
quickfilldemo_demo.zip
QuickFillDemo.exe
quickfilldemo_src.zip
CDibData
Doxygen.dat
CQuantize
QuickFill
test.bmp
QuickFillDemo
Debug
QuickFillDemo.clw
QuickFillDemo.dsp
QuickFillDemo.dsw
Release
res
bitmap1.bmp
bmp00001.bmp
bmp00002.bmp
bmp00003.bmp
bmp00004.bmp
checker.bmp
checker2.bmp
circle.bmp
clubs.bmp
cross.bmp
dchecker.bmp
diamon.bmp
dimonds.bmp
hart.bmp
it.bmp
ldiag.bmp
ne.bmp
nw.bmp
QuickFillDemo.ico
QuickFillDemoDoc.ico
rdiag.bmp
schecker.bmp
scircle.bmp
se.bmp
spades.bmp
square.bmp
sw.bmp
x.bmp
xsquare.bmp
// QuickFill.h
//
// Author : John R. Shaw (shawj2@earthlink.net)
// Date   : Jan. 26 2004
//
// Copyright (C) 2004 John R. Shaw
// All rights reserved.
//
// This code may be used in compiled form in any way you desire. This
// file may be redistributed unmodified by any means PROVIDING it is 
// not sold for profit without the authors written consent, and 
// providing that this notice and the authors name is included. If 
// the source code in this file is used in any commercial application 
// then a simple email would be nice.
//
// Warranties and Disclaimers:
// THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND
// INCLUDING, BUT NOT LIMITED TO, WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
// IN NO EVENT WILL JOHN R. SHAW BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES,
// INCLUDING DAMAGES FOR LOSS OF PROFITS, LOSS OR INACCURACY OF DATA,
// INCURRED BY ANY PERSON FROM SUCH PERSON'S USAGE OF THIS SOFTWARE
// EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
//
// Please email bug reports, bug fixes, enhancements, requests and
// comments to: shawj2@earthlink.net
//

#ifndef _QUICKFILL_H_
#define _QUICKFILL_H_

//#ifndef _GDIPLUSGRAPHICS_H
//#include <gdiplus.h>
//#endif

#ifndef __CDIBDATA_H__
#include "CDibData.h"
#endif

class CQuickFill
{
	// Public methods
public:
	int QuickFill(CBitmap* pBitmap,int x,int y,
		COLORREF fill_color,COLORREF border_color=CLR_INVALID);
	int QuickFill(CBitmap* pBitmap,LPRECT pRect,int x,int y,
		COLORREF fill_color,COLORREF border_color=CLR_INVALID);

	RECT GetInvalidRect() const { return m_rcInvalid; }

	void SetFillMask(BYTE* pMask=NULL); // Mask[8] or NULL
	BOOL SetPatternBitmap(CBitmap* pBitmap=NULL,COLORREF clrClear=CLR_INVALID);
	BOOL SetPatternBitmap(HBITMAP hBitmap,COLORREF clrClear=CLR_INVALID);
	BOOL SetPatternBitmap(LPCTSTR lpszPathName,COLORREF clrClear=CLR_INVALID);
	COLORREF SetPatternClearColor(COLORREF clrClear=CLR_INVALID);
	COLORREF GetPatternClearColor() { return m_clrPatClear; }
	BOOL HasPattern();
	BOOL HasMask();

#ifdef QUICKFILL_SLOW
	void SetSlowMode(BOOL bEnable) { m_bSlowMode = bEnable; }
	BOOL IsSlowMode() { return m_bSlowMode; }
#endif

	// Protected scanning and drawing methods
protected:
	DWORD GetPixel(int x, int y);
	BOOL SetPixel(int x, int y, DWORD dwValue);
	void DrawHorizontalLine(int x1, int x2, int y, DWORD dwValue);
	int ScanLeft(int x,int y,int xmin,DWORD dwValue);
	int SearchLeft(int x,int y,int xmin,DWORD dwValue);
	int ScanRight(int x,int y,int xmax,DWORD dwValue);
	int SearchRight(int x,int y,int xmax,DWORD dwValue);

	// Contructors/Destuctors
public:
	CQuickFill();
	~CQuickFill();

	// Public debug information methods
#ifdef QUICKFILL_TEST
	unsigned GetStackSize() { return m_CurStackSize; }
	unsigned GetMaxStackSize() { return m_MaxStackSize; }
	unsigned GetVisitSize() { return m_VisitSize; }
	int GetCurrentLine() { return m_CurrentLine; }
	int GetDirection() { return m_Direction; }
	int GetRevisitCount() { return m_nRevisitCount; }
	void SetShowRevisit(BOOL bFlg=TRUE) { m_bShowRevisits = bFlg; }
	BOOL GetShowRevisitFlag() { return m_bShowRevisits; }
#endif

	// Private list members
private:
	typedef struct hlineNode_st {
		int x1,x2,y,dy;
		struct hlineNode_st *pNext,*pPrev;
	} HLINE_NODE;

	HLINE_NODE*	m_pVisitList;
	HLINE_NODE*	m_pLineList;
	HLINE_NODE*	m_pFreeList;
	BOOL	m_bXSortOn;
	BOOL	m_bMemError;
	int		m_LastY;

	int MakeList(void);
	void FreeList(void);
	void PushLine(int x1,int x2,int y,int dy);
	void PopLine(int *x1,int *x2,int *y,int *dy);
	HLINE_NODE* FindNextLine(int x1,int x2,int y);
	void PopThis(HLINE_NODE *pThis);
	void PushOpposite(int OldX1,int OldX2,int x1,int x2,int y,int dy);

	void PushVisitedLine(int x1,int x2,int y);
	BOOL ExpandVisitedBlock(int x1,int x2,int y);
	BOOL IsRevisit(int x1,int x2,int y);


	// Private bitmap data members

	CDibData m_DibData;
	CDibData m_DibPattern;
	int m_xPatMod;
	int m_yPatMod;
	COLORREF m_clrPatClear;
	DWORD m_PatClearValue;
	BYTE m_FillMask[8];

	// Private information members
	RECT m_rcInvalid;

	// Private debug information
private:
#ifdef QUICKFILL_TEST
	unsigned m_CurStackSize;
	unsigned m_MaxStackSize;
	unsigned m_VisitSize;
	int m_CurrentLine;
	int m_Direction;
	int m_nRevisitCount;
	BOOL m_bShowRevisits;
#endif
#ifdef QUICKFILL_SLOW
	BOOL m_bSlowMode;
#endif
};

/* Arguments:
 *		Pointer to bitmap to fill, (x,y) coordinates of seed point,
 *		color used for solid or masked fills, border color used if area to
 *		be filled is outlined (or CLR_INVALID).
 *
 * Returns:
 *		 0 = Success.
 *		-1 = Invalid seed point.
 *		-2 = Memory allocation error.
 *		-3 = Invalid bitmap or unknown error.
 */
inline int CQuickFill::QuickFill(
	CBitmap* pBitmap,int x,int y,
	COLORREF fill_color,COLORREF border_color/*=CLR_INVALID*/)
{
	return QuickFill(pBitmap,NULL,x,y,fill_color,border_color);
}

#endif // _QUICKFILL_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

Share

About the Author

John R. Shaw
Software Developer
United States United States
I am a senior software engineer who has been designing and developing software for many years, mostly in C/C++. You might say that I think in code; which is why I am passionate about my first rule of coding: “First do no harm”. So if I get carried away in my explanations, please realize that it is just part of my personality. I enjoy learning new things and, when I have the time, passing that knowledge onto others.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.1411023.1 | Last Updated 13 Mar 2004
Article Copyright 2004 by John R. Shaw
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid