2021-09-13 01:33:14 +00:00
|
|
|
/*
|
2009-08-02 15:49:17 +00:00
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT: ReactOS win32 subsystem
|
|
|
|
* PURPOSE: Flood filling support
|
2015-11-10 17:41:55 +00:00
|
|
|
* FILE: win32ss/gdi/dib/floodfill.c
|
2021-09-13 01:33:14 +00:00
|
|
|
* PROGRAMMER: Gregor Schneider <grschneider AT gmail DOT com>
|
2009-08-02 15:49:17 +00:00
|
|
|
*/
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2010-04-26 13:58:46 +00:00
|
|
|
#include <win32k.h>
|
2009-07-31 15:41:09 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
|
|
|
#include <debug.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This floodfill algorithm is an iterative four neighbors version. It works with an internal stack like data structure.
|
|
|
|
* The stack is kept in an array, sized for the worst case scenario of having to add all pixels of the surface.
|
|
|
|
* This avoids having to allocate and free memory blocks all the time. The stack grows from the end of the array towards the start.
|
2021-09-13 01:33:14 +00:00
|
|
|
* All pixels are checked before being added, against belonging to the fill rule (FLOODFILLBORDER or FLOODFILLSURFACE)
|
2009-07-31 15:41:09 +00:00
|
|
|
* and the position in respect to the clip region. This guarantees all pixels lying on the stack belong to the filled surface.
|
|
|
|
* Further optimisations of the algorithm are possible.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Floodfil helper structures and functions */
|
|
|
|
typedef struct _floodItem
|
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
ULONG x;
|
|
|
|
ULONG y;
|
2009-07-31 15:41:09 +00:00
|
|
|
} FLOODITEM;
|
|
|
|
|
2009-08-01 14:39:40 +00:00
|
|
|
typedef struct _floodInfo
|
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
ULONG floodLen;
|
|
|
|
FLOODITEM *floodStart;
|
|
|
|
FLOODITEM *floodData;
|
2009-08-01 14:39:40 +00:00
|
|
|
} FLOODINFO;
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-01 14:39:40 +00:00
|
|
|
static __inline BOOL initFlood(FLOODINFO *info, RECTL *DstRect)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
ULONG width = DstRect->right - DstRect->left;
|
|
|
|
ULONG height = DstRect->bottom - DstRect->top;
|
2021-09-13 01:33:14 +00:00
|
|
|
info->floodData = ExAllocatePoolWithTag(NonPagedPool, width * height * sizeof(FLOODITEM), TAG_DIB);
|
2009-08-02 15:49:17 +00:00
|
|
|
if (info->floodData == NULL)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
info->floodStart = info->floodData + (width * height);
|
|
|
|
DPRINT("Allocated flood stack from %p to %p\n", info->floodData, info->floodStart);
|
|
|
|
return TRUE;
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-01 14:39:40 +00:00
|
|
|
static __inline VOID finalizeFlood(FLOODINFO *info)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
ExFreePoolWithTag(info->floodData, TAG_DIB);
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-01 14:39:40 +00:00
|
|
|
static __inline VOID addItemFlood(FLOODINFO *info,
|
2021-09-13 01:33:14 +00:00
|
|
|
ULONG x,
|
|
|
|
ULONG y,
|
|
|
|
SURFOBJ *DstSurf,
|
|
|
|
RECTL *DstRect,
|
|
|
|
ULONG Color,
|
2009-07-31 15:41:09 +00:00
|
|
|
BOOL isSurf)
|
|
|
|
{
|
2011-10-02 23:09:03 +00:00
|
|
|
if (RECTL_bPointInRect(DstRect,x,y))
|
2009-08-02 15:49:17 +00:00
|
|
|
{
|
2014-11-13 10:31:45 +00:00
|
|
|
if (isSurf &&
|
2009-08-02 15:49:17 +00:00
|
|
|
DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_GetPixel(DstSurf, x, y) != Color)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
return;
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-02 15:49:17 +00:00
|
|
|
else if (isSurf == FALSE &&
|
|
|
|
DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_GetPixel(DstSurf, x, y) == Color)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
info->floodStart--;
|
|
|
|
info->floodStart->x = x;
|
|
|
|
info->floodStart->y = y;
|
|
|
|
info->floodLen++;
|
|
|
|
}
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-01 14:39:40 +00:00
|
|
|
static __inline VOID removeItemFlood(FLOODINFO *info)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
info->floodStart++;
|
|
|
|
info->floodLen--;
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
|
|
|
|
2021-09-13 01:33:14 +00:00
|
|
|
BOOLEAN DIB_XXBPP_FloodFillSolid(SURFOBJ *DstSurf,
|
|
|
|
BRUSHOBJ *Brush,
|
|
|
|
RECTL *DstRect,
|
2009-08-01 14:39:40 +00:00
|
|
|
POINTL *Origin,
|
2021-09-13 01:33:14 +00:00
|
|
|
ULONG ConvColor,
|
2009-08-01 14:39:40 +00:00
|
|
|
UINT FillType)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
ULONG x, y;
|
|
|
|
ULONG BrushColor;
|
|
|
|
FLOODINFO flood = {0, NULL, NULL};
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-02 15:49:17 +00:00
|
|
|
BrushColor = Brush->iSolidColor;
|
|
|
|
x = Origin->x;
|
|
|
|
y = Origin->y;
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-02 15:49:17 +00:00
|
|
|
if (FillType == FLOODFILLBORDER)
|
|
|
|
{
|
|
|
|
/* Check if the start pixel has the border color */
|
|
|
|
if (DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_GetPixel(DstSurf, x, y) == ConvColor)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-02 15:49:17 +00:00
|
|
|
if (initFlood(&flood, DstRect) == FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
addItemFlood(&flood, x, y, DstSurf, DstRect, ConvColor, FALSE);
|
2021-09-13 01:33:14 +00:00
|
|
|
while (flood.floodLen != 0)
|
2009-08-02 15:49:17 +00:00
|
|
|
{
|
|
|
|
x = flood.floodStart->x;
|
|
|
|
y = flood.floodStart->y;
|
|
|
|
removeItemFlood(&flood);
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-02 15:49:17 +00:00
|
|
|
DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_PutPixel(DstSurf, x, y, BrushColor);
|
|
|
|
if (flood.floodStart - 4 < flood.floodData)
|
2021-09-13 01:33:14 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
DPRINT1("Can't finish flooding!\n");
|
2009-08-01 14:39:40 +00:00
|
|
|
finalizeFlood(&flood);
|
2009-08-02 15:49:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
addItemFlood(&flood, x, y + 1, DstSurf, DstRect, ConvColor, FALSE);
|
|
|
|
addItemFlood(&flood, x, y - 1, DstSurf, DstRect, ConvColor, FALSE);
|
|
|
|
addItemFlood(&flood, x + 1, y, DstSurf, DstRect, ConvColor, FALSE);
|
|
|
|
addItemFlood(&flood, x - 1, y, DstSurf, DstRect, ConvColor, FALSE);
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-02 15:49:17 +00:00
|
|
|
finalizeFlood(&flood);
|
|
|
|
}
|
|
|
|
else if (FillType == FLOODFILLSURFACE)
|
|
|
|
{
|
|
|
|
/* Check if the start pixel has the surface color */
|
|
|
|
if (DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_GetPixel(DstSurf, x, y) != ConvColor)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
return FALSE;
|
|
|
|
}
|
2009-07-31 15:41:09 +00:00
|
|
|
|
2009-08-02 15:49:17 +00:00
|
|
|
if (initFlood(&flood, DstRect) == FALSE)
|
|
|
|
{
|
|
|
|
return FALSE;
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-02 15:49:17 +00:00
|
|
|
addItemFlood(&flood, x, y, DstSurf, DstRect, ConvColor, TRUE);
|
2021-09-13 01:33:14 +00:00
|
|
|
while (flood.floodLen != 0)
|
2009-07-31 15:41:09 +00:00
|
|
|
{
|
2009-08-02 15:49:17 +00:00
|
|
|
x = flood.floodStart->x;
|
|
|
|
y = flood.floodStart->y;
|
|
|
|
removeItemFlood(&flood);
|
|
|
|
|
|
|
|
DibFunctionsForBitmapFormat[DstSurf->iBitmapFormat].DIB_PutPixel(DstSurf, x, y, BrushColor);
|
|
|
|
if (flood.floodStart - 4 < flood.floodData)
|
|
|
|
{
|
|
|
|
DPRINT1("Can't finish flooding!\n");
|
|
|
|
finalizeFlood(&flood);
|
2009-07-31 15:41:09 +00:00
|
|
|
return FALSE;
|
2009-08-02 15:49:17 +00:00
|
|
|
}
|
|
|
|
addItemFlood(&flood, x, y + 1, DstSurf, DstRect, ConvColor, TRUE);
|
|
|
|
addItemFlood(&flood, x, y - 1, DstSurf, DstRect, ConvColor, TRUE);
|
|
|
|
addItemFlood(&flood, x + 1, y, DstSurf, DstRect, ConvColor, TRUE);
|
|
|
|
addItemFlood(&flood, x - 1, y, DstSurf, DstRect, ConvColor, TRUE);
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|
2009-08-02 15:49:17 +00:00
|
|
|
finalizeFlood(&flood);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
DPRINT1("Unsupported FloodFill type!\n");
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2009-07-31 15:41:09 +00:00
|
|
|
}
|