flood_fill_seed: Reduce code duplicatino

This is also a preparation to eliminate the uses of goto.
This commit is contained in:
YAMAMOTO Takashi 2021-05-11 14:41:01 +09:00
parent eadbc2caca
commit a436fde8a0

View file

@ -158,6 +158,48 @@ static void flood_fill_line(struct quirc *q, int x, int y,
func(user_data, y, left, right); func(user_data, y, left, right);
} }
static struct quirc_flood_fill_vars *flood_fill_call_next(
struct quirc *q,
quirc_pixel_t *row,
int from, int to,
span_func_t func, void *user_data,
struct quirc_flood_fill_vars *vars,
int direction)
{
int *leftp;
if (direction < 0) {
leftp = &vars->left_up;
} else {
leftp = &vars->left_down;
}
while (*leftp <= vars->right) {
if (row[*leftp] == from) {
struct quirc_flood_fill_vars *next_vars;
/* Set up the next context */
next_vars = vars + 1;
next_vars->left_up = *leftp;
next_vars->y = vars->y + direction;
/* Fill the extent */
flood_fill_line(q,
next_vars->left_up,
next_vars->y,
from, to,
func, user_data,
&next_vars->left_up,
&next_vars->right);
next_vars->left_down = next_vars->left_up;
return next_vars;
}
(*leftp)++;
}
return NULL;
}
static void flood_fill_seed(struct quirc *q, static void flood_fill_seed(struct quirc *q,
int x0, int y0, int x0, int y0,
int from, int to, int from, int to,
@ -198,51 +240,25 @@ return_from_call:
if (vars->y > 0) { if (vars->y > 0) {
row = q->pixels + (vars->y - 1) * q->w; row = q->pixels + (vars->y - 1) * q->w;
while (vars->left_up <= vars->right) { next_vars = flood_fill_call_next(q, row,
if (row[vars->left_up] == from) { from, to,
/* Set up the next context */
next_vars = vars + 1;
next_vars->left_up = vars->left_up;
next_vars->y = vars->y - 1;
/* Fill the extent */
flood_fill_line(q,
next_vars->left_up,
next_vars->y, from, to,
func, user_data, func, user_data,
&next_vars->left_up, vars, -1);
&next_vars->right); if (next_vars != NULL) {
next_vars->left_down = next_vars->left_up;
goto call; goto call;
} }
vars->left_up++;
}
} }
if (vars->y < q->h - 1) { if (vars->y < q->h - 1) {
row = q->pixels + (vars->y + 1) * q->w; row = q->pixels + (vars->y + 1) * q->w;
while (vars->left_down <= vars->right) { next_vars = flood_fill_call_next(q, row,
if (row[vars->left_down] == from) { from, to,
/* Set up the next context */
next_vars = vars + 1;
next_vars->left_up = vars->left_down;
next_vars->y = vars->y + 1;
/* Fill the extent */
flood_fill_line(q,
next_vars->left_up,
next_vars->y, from, to,
func, user_data, func, user_data,
&next_vars->left_up, vars, 1);
&next_vars->right); if (next_vars != NULL) {
next_vars->left_down = next_vars->left_up;
goto call; goto call;
} }
vars->left_down++;
}
} }
if (vars > stack) { if (vars > stack) {