Changed according to review

and removed some trailing spaces.
This commit is contained in:
Alexander Buhl 2021-12-20 17:48:13 +01:00 committed by GitHub
parent b741e7be33
commit 908995b7a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2074,7 +2074,8 @@ void ImageColorReplace(Image *image, Color color, Color replace)
ImageFormat(image, format); ImageFormat(image, format);
} }
// Flood-fills from the given position // Flood-fills from the given position
void ImageFloodFill(Image *dst, int startPosX, int startPosY, Color color){ void ImageFloodFill(Image *dst, int startPosX, int startPosY, Color color)
{
if ((dst->data == NULL) || (startPosX < 0) || (startPosX >= dst->width) || (startPosY < 0) || (startPosY >= dst->height)) return; if ((dst->data == NULL) || (startPosX < 0) || (startPosX >= dst->width) || (startPosY < 0) || (startPosY >= dst->height)) return;
int bpp = GetPixelDataSize(1,1,dst->format); int bpp = GetPixelDataSize(1,1,dst->format);
@ -2083,46 +2084,42 @@ void ImageFloodFill(Image *dst, int startPosX, int startPosY, Color color){
Color startColor = GetPixelColor(start_pixel_address, dst->format); Color startColor = GetPixelColor(start_pixel_address, dst->format);
if (startColor.r == color.r && startColor.g == color.g && startColor.b == color.b && startColor.a == color.a) return; if (startColor.r == color.r && startColor.g == color.g && startColor.b == color.b && startColor.a == color.a) return;
struct flood_fill_pixel_pos {
int x, y;
};
// x,y array for pixels which we just flood-filled, conservatively sized // x,y array for pixels which we just flood-filled, conservatively sized
// TODO: research and implement ring buffer, size could maybe be as low as ~5 * smallest image side // TODO: research and implement ring buffer, size could maybe be as low as ~5 * smallest image side
struct flood_fill_pixel_pos *a_star_edge_list = RL_MALLOC(dst->width*dst->height*sizeof(*a_star_edge_list)); int *a_star_edge_list = RL_MALLOC(dst->width*dst->height*2*sizeof(*a_star_edge_list));
if (a_star_edge_list == NULL) return; if (a_star_edge_list == NULL) return;
a_star_edge_list[0].x = startPosX; a_star_edge_list[0] = startPosX;
a_star_edge_list[0].y = startPosY; a_star_edge_list[1] = startPosY;
for (int flood = 0, pool = 1; flood < pool; flood++) for (int flood = 0, pool = 1; flood < pool; flood+=2)
{ {
int x = a_star_edge_list[flood].x; int x = a_star_edge_list[flood].x;
int y = a_star_edge_list[flood].y; int y = a_star_edge_list[flood].y;
void *pixel_address = 0; void *pixel_address = 0;
// clockwise check and color the 4 neighbours, then record // clockwise check and color the 4 neighbours, then record
struct flood_fill_pixel_pos neighbours[4] = { int neighbours[4*2] = {
{x, y - 1}, x, y - 1,
{x + 1, y}, x + 1, y,
{x, y + 1}, x, y + 1,
{x - 1, y}, x - 1, y,
}; };
Color sc = GetPixelColor(start_pixel_address, dst->format); Color sc = GetPixelColor(start_pixel_address, dst->format);
for(int n = 0; n < 4; n++) for (int n = 0; n < 4*2; n+=2)
{ {
if(!((neighbours[n].x < 0) || (neighbours[n].x >= dst->width) || (neighbours[n].y < 0) || (neighbours[n].y >= dst->height) || ((neighbours[n].x == startPosX) && (neighbours[n].y == startPosY)))) if (!((neighbours[n] < 0) || (neighbours[n] >= dst->width) || (neighbours[n + 1] < 0) || (neighbours[n + 1] >= dst->height) || ((neighbours[n] == startPosX) && (neighbours[n + 1] == startPosY))))
{ {
pixel_address = (dst->data + bpp*(neighbours[n].x + dst->width*neighbours[n].y)); pixel_address = (dst->data + bpp*(neighbours[n] + dst->width*neighbours[n + 1]));
Color cc = GetPixelColor(pixel_address, dst->format); Color cc = GetPixelColor(pixel_address, dst->format);
if (cc.r == sc.r && cc.g == sc.g && cc.b == sc.b && cc.a == sc.a) if (cc.r == sc.r && cc.g == sc.g && cc.b == sc.b && cc.a == sc.a)
{ {
a_star_edge_list[pool].x = neighbours[n].x; a_star_edge_list[pool] = neighbours[n];
a_star_edge_list[pool].y = neighbours[n].y; a_star_edge_list[pool + 1] = neighbours[n + 1];
SetPixelColor(pixel_address, color, dst->format); SetPixelColor(pixel_address, color, dst->format);
pool += 1; pool += 2;
} }
} }
} }