Commit f19181dc authored by Jehan's avatar Jehan

app: make visited into single-level allocated array.

parent 910d7934
...@@ -459,6 +459,7 @@ gimp_lineart_close (GeglBuffer *line_art, ...@@ -459,6 +459,7 @@ gimp_lineart_close (GeglBuffer *line_art,
point++; point++;
} }
g_hash_table_destroy (visited);
g_array_free (keypoints, TRUE); g_array_free (keypoints, TRUE);
g_object_unref (strokes); g_object_unref (strokes);
g_free (normals); g_free (normals);
...@@ -941,32 +942,32 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -941,32 +942,32 @@ gimp_lineart_denoise (GeglBuffer *buffer,
int minimum_area) int minimum_area)
{ {
/* Keep connected regions with significant area. */ /* Keep connected regions with significant area. */
GQueue *q = g_queue_new ();
GArray *region; GArray *region;
gint width = gegl_buffer_get_width (buffer); GQueue *q = g_queue_new ();
gint height = gegl_buffer_get_height (buffer); gint width = gegl_buffer_get_width (buffer);
gboolean visited[width][height]; gint height = gegl_buffer_get_height (buffer);
gboolean *visited = g_new0 (gboolean, width * height);
gint x, y;
memset (visited, 0, sizeof (gboolean) * width * height);
region = g_array_sized_new (TRUE, TRUE, sizeof (Pixel *), minimum_area); region = g_array_sized_new (TRUE, TRUE, sizeof (Pixel *), minimum_area);
for (int y = 0; y < height; ++y) for (y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) for (x = 0; x < width; ++x)
{ {
guchar has_stroke; guchar has_stroke;
gegl_buffer_sample (buffer, x, y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, x, y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[x][y]) if (has_stroke && ! visited[x + y * width])
{ {
Pixel *p = g_new (Pixel, 1); Pixel *p = g_new (Pixel, 1);
int regionSize = 0; gint regionSize = 0;
p->x = x; p->x = x;
p->y = y; p->y = y;
g_queue_push_tail (q, p); g_queue_push_tail (q, p);
visited[x][y] = TRUE; visited[x + y * width] = TRUE;
while (! g_queue_is_empty (q)) while (! g_queue_is_empty (q))
{ {
...@@ -980,14 +981,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -980,14 +981,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x +p2y * width] = TRUE;
} }
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -996,14 +997,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -996,14 +997,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x; p2x = p->x;
...@@ -1012,14 +1013,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1012,14 +1013,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x; p2x = p->x;
...@@ -1028,14 +1029,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1028,14 +1029,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x + 1; p2x = p->x + 1;
...@@ -1044,14 +1045,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1044,14 +1045,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -1060,14 +1061,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1060,14 +1061,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -1076,14 +1077,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1076,14 +1077,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
p2x = p->x + 1; p2x = p->x + 1;
...@@ -1092,14 +1093,14 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1092,14 +1093,14 @@ gimp_lineart_denoise (GeglBuffer *buffer,
{ {
gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL, gegl_buffer_sample (buffer, p2x, p2y, NULL, &has_stroke, NULL,
GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE); GEGL_SAMPLER_NEAREST, GEGL_ABYSS_NONE);
if (has_stroke && ! visited[p2x][p2y]) if (has_stroke && ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
} }
...@@ -1126,6 +1127,7 @@ gimp_lineart_denoise (GeglBuffer *buffer, ...@@ -1126,6 +1127,7 @@ gimp_lineart_denoise (GeglBuffer *buffer,
} }
g_array_free (region, TRUE); g_array_free (region, TRUE);
g_queue_free_full (q, g_free); g_queue_free_full (q, g_free);
g_free (visited);
} }
static void static void
...@@ -1168,27 +1170,25 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1168,27 +1170,25 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
gint width, gint width,
gint height) gint height)
{ {
gboolean *visited = g_new0 (gboolean, width * height);
GQueue *q = g_queue_new ();
GArray *max_positions; GArray *max_positions;
GQueue *q = g_queue_new ();
gboolean visited[width][height];
memset (visited, 0, sizeof (gboolean) * width * height);
max_positions = g_array_new (FALSE, TRUE, sizeof (Pixel)); max_positions = g_array_new (FALSE, TRUE, sizeof (Pixel));
for (int y = 0; y < height; ++y) for (int y = 0; y < height; ++y)
for (int x = 0; x < width; ++x) for (int x = 0; x < width; ++x)
{ {
if ((curvatures[x][y] > 0.0) && ! visited[x][y]) if ((curvatures[x][y] > 0.0) && ! visited[x + y * width])
{ {
Pixel *p = g_new (Pixel, 1); Pixel *p = g_new (Pixel, 1);
Pixel max_curvature_pixel = gimp_vector2_new (-1.0, -1.0); Pixel max_curvature_pixel = gimp_vector2_new (-1.0, -1.0);
gfloat max_curvature = 0.0f; gfloat max_curvature = 0.0f;
size_t count = 0;
p->x = x; p->x = x;
p->y = y; p->y = y;
g_queue_push_tail (q, p); g_queue_push_tail (q, p);
visited[x][y] = TRUE; visited[x + y * width] = TRUE;
while (! g_queue_is_empty (q)) while (! g_queue_is_empty (q))
{ {
...@@ -1199,7 +1199,6 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1199,7 +1199,6 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
p = (Pixel *) g_queue_pop_head (q); p = (Pixel *) g_queue_pop_head (q);
c = curvatures[(gint) p->x][(gint) p->y]; c = curvatures[(gint) p->x][(gint) p->y];
++count;
curvatures[(gint) p->x][(gint) p->y] = 0.0f; curvatures[(gint) p->x][(gint) p->y] = 0.0f;
p2x = (gint) p->x + 1; p2x = (gint) p->x + 1;
...@@ -1207,14 +1206,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1207,14 +1206,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -1222,14 +1221,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1222,14 +1221,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x; p2x = p->x;
...@@ -1237,14 +1236,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1237,14 +1236,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x; p2x = p->x;
...@@ -1252,14 +1251,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1252,14 +1251,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x + 1; p2x = p->x + 1;
...@@ -1267,14 +1266,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1267,14 +1266,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -1282,14 +1281,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1282,14 +1281,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x - 1; p2x = p->x - 1;
...@@ -1297,14 +1296,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1297,14 +1296,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
p2x = p->x + 1; p2x = p->x + 1;
...@@ -1312,14 +1311,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1312,14 +1311,14 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
if (p2x >= 0 && p2x < width && if (p2x >= 0 && p2x < width &&
p2y >= 0 && p2y < height && p2y >= 0 && p2y < height &&
curvatures[p2x][p2y] > 0.0 && curvatures[p2x][p2y] > 0.0 &&
! visited[p2x][p2y]) ! visited[p2x + p2y * width])
{ {
Pixel *p2 = g_new (Pixel, 1); Pixel *p2 = g_new (Pixel, 1);
p2->x = p2x; p2->x = p2x;
p2->y = p2y; p2->y = p2y;
g_queue_push_tail (q, p2); g_queue_push_tail (q, p2);
visited[p2x][p2y] = TRUE; visited[p2x + p2y * width] = TRUE;
} }
if (c > max_curvature) if (c > max_curvature)
...@@ -1334,6 +1333,7 @@ gimp_lineart_curvature_extremums (gfloat **curvatures, ...@@ -1334,6 +1333,7 @@ gimp_lineart_curvature_extremums (gfloat **curvatures,
} }
} }
g_queue_free_full (q, g_free); g_queue_free_full (q, g_free);
g_free (visited);
return max_positions; return max_positions;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment