Commit 1a27e46d authored by Øyvind "pippin" Kolås's avatar Øyvind "pippin" Kolås

workshop: make more use of GeglSampler in inpainting ops

parent fa702369
......@@ -64,7 +64,8 @@ prepare (GeglOperation *operation)
gegl_operation_set_format (operation, "output", format);
}
static void scaled_copy (GeglBuffer *in,
static void scaled_copy (PixelDuster *duster,
GeglBuffer *in,
GeglBuffer *out,
gfloat scale)
{
......@@ -78,9 +79,8 @@ static void scaled_copy (GeglBuffer *in,
{
float rgba[4];
GeglRectangle r = {x, y, 1, 1};
gegl_buffer_sample (in, x / scale, y / scale, NULL,
&rgba[0], format,
GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_f, x / scale, y / scale, NULL,
&rgba[0], 0);
gegl_buffer_set (out, &r, 0, format, &rgba[0], 0);
}
}
......@@ -91,7 +91,7 @@ static void improve (PixelDuster *duster,
gfloat scale)
{
GeglRectangle rect;
const Babl *format = babl_format ("R'G'B'A float");
const Babl *format = babl_format ("RGBA float");
gint x, y;
rect = *gegl_buffer_get_extent (out);
......@@ -118,7 +118,8 @@ static void improve (PixelDuster *duster,
gfloat rgba[4*MAX_K];
for (int j = 0; j < MAX_K; j++)
gegl_buffer_sample (duster->input, probe->source_x[j], probe->source_y[j], NULL, &rgba[j*4], format, GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_f, probe->source_x[j],
probe->source_y[j], NULL, &rgba[j*4], 0);
for (int j = 1; j < probe->k; j++)
......@@ -176,7 +177,6 @@ process (GeglOperation *operation,
GeglRectangle in_rect = *gegl_buffer_get_extent (input);
GeglRectangle out_rect = *gegl_buffer_get_extent (output);
PixelDuster *duster;
scaled_copy (input, output, o->scale);
duster = pixel_duster_new (input, output,
&in_rect, &out_rect,
o->seek_distance,
......@@ -185,6 +185,7 @@ process (GeglOperation *operation,
o->scale,
o->scale,
NULL);
scaled_copy (duster, input, output, o->scale);
seed_db (duster);
improve (duster, input, output, o->scale);
pixel_duster_destroy (duster);
......
......@@ -74,7 +74,8 @@ prepare (GeglOperation *operation)
gegl_operation_set_format (operation, "output", format);
}
static void scaled_copy (GeglBuffer *in,
static void scaled_copy (PixelDuster *duster,
GeglBuffer *in,
GeglBuffer *out,
gfloat scale)
{
......@@ -97,7 +98,7 @@ static void scaled_copy (GeglBuffer *in,
{
GeglRectangle r = {x * scale , y * scale, 1, 1};
gfloat rgba[4];
gegl_buffer_sample (in, x, y, NULL, &rgba[0], format, GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_f, x, y, NULL, &rgba[0], 0);
gegl_buffer_set (out, &r, 0, format, &rgba[0], 0);
}
}
......@@ -119,7 +120,8 @@ static void remove_grid (GeglBuffer *in,
}
}
static void remove_deviant (GeglBuffer *in,
static void remove_deviant (PixelDuster *duster,
GeglBuffer *in,
GeglBuffer *out,
gfloat scale)
{
......@@ -134,7 +136,7 @@ static void remove_deviant (GeglBuffer *in,
gfloat rgba_in[4] = {0,0,0,0};
gfloat rgba_empty[4] = {0,0,0,0};
gfloat rgba_out[4] = {0,0,0,0};
gegl_buffer_sample (in, x / scale, y / scale, NULL, &rgba_in[0], format, GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_f, x / scale, y / scale, NULL, &rgba_in[0], 0);
gegl_buffer_sample (out, x, y, NULL, &rgba_out[0], format, GEGL_SAMPLER_NEAREST, 0);
if (POW2(rgba_in[0]-rgba_out[0])+
......@@ -158,7 +160,6 @@ process (GeglOperation *operation,
GeglRectangle out_rect = *gegl_buffer_get_extent (output);
PixelDuster *duster;
scaled_copy (input, output, o->scale);
duster = pixel_duster_new (input, output,
&in_rect, &out_rect,
o->seek_distance,
......@@ -170,16 +171,17 @@ process (GeglOperation *operation,
o->scale,
o->scale,
NULL);
scaled_copy (duster, input, output, o->scale);
seed_db (duster);
pixel_duster_add_probes_for_transparent (duster);
pixel_duster_fill (duster);
remove_deviant (input, output, o->scale);
remove_deviant (duster, input, output, o->scale);
remove_grid (input, output, o->scale);
pixel_duster_remove_probes (duster);
pixel_duster_add_probes_for_transparent (duster);
pixel_duster_fill (duster);
remove_deviant (input, output, o->scale);
remove_deviant (duster, input, output, o->scale);
pixel_duster_remove_probes (duster);
pixel_duster_add_probes_for_transparent (duster);
pixel_duster_fill (duster);
......
......@@ -38,6 +38,9 @@ typedef struct
GeglBuffer *output;
GeglRectangle in_rect;
GeglRectangle out_rect;
GeglSampler *in_sampler_yu8;
GeglSampler *in_sampler_f;
GeglSampler *in_sampler_u8;
int max_k;
int seek_radius;
int minimum_neighbors;
......@@ -215,6 +218,15 @@ static PixelDuster * pixel_duster_new (GeglBuffer *input,
ret->out_rect = *out_rect;
ret->scale_x = scale_x;
ret->scale_y = scale_y;
ret->in_sampler_yu8 = gegl_buffer_sampler_new (input, babl_format ("Y'aA u8"),
GEGL_SAMPLER_NEAREST);
ret->in_sampler_u8 = gegl_buffer_sampler_new (input,
babl_format ("R'G'B'aA u8"),
GEGL_SAMPLER_NEAREST);
ret->in_sampler_f = gegl_buffer_sampler_new (input,
babl_format ("RGBA float"),
GEGL_SAMPLER_NEAREST);
for (int i = 0; i < 4096; i++)
ret->ht[i] = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
ret->probes_ht = g_hash_table_new_full (g_direct_hash, g_direct_equal, NULL, g_free);
......@@ -243,6 +255,11 @@ static void pixel_duster_destroy (PixelDuster *duster)
g_hash_table_destroy (duster->ht[i]);
}
fprintf (stderr, "\n");
g_object_unref (duster->in_sampler_yu8);
g_object_unref (duster->in_sampler_u8);
g_object_unref (duster->in_sampler_f);
g_free (duster);
}
......@@ -250,24 +267,23 @@ static void pixel_duster_destroy (PixelDuster *duster)
static void extract_site (PixelDuster *duster, GeglBuffer *input, int x, int y, guchar *dst)
{
static const Babl *format = NULL;
static const Babl *yformat = NULL;
guchar lum[8];
int bdir, maxlum;
uint64_t hist3dmask=0;
if (!format){
format = babl_format ("R'G'B'A u8");
yformat = babl_format ("Y'aA u8");
}
#define PIXDUST_DIR_INVARIANT 1
#if PIXDUST_DIR_INVARIANT==1
/* figure out which of the up/down/left/right pixels are brightest,
using premultiplied alpha - do punish blank spots */
gegl_buffer_sample (input, x + 1, y + 0, NULL, &lum[0], yformat, GEGL_SAMPLER_NEAREST, 0);
gegl_buffer_sample (input, x - 1, y + 0, NULL, &lum[2], yformat, GEGL_SAMPLER_NEAREST, 0);
gegl_buffer_sample (input, x + 0, y + 1, NULL, &lum[4], yformat, GEGL_SAMPLER_NEAREST, 0);
gegl_buffer_sample (input, x + 0, y - 1, NULL, &lum[6], yformat, GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_yu8, x + 1, y + 0, NULL, &lum[0], 0);
gegl_sampler_get (duster->in_sampler_yu8, x - 1, y + 0, NULL, &lum[2], 0);
gegl_sampler_get (duster->in_sampler_yu8, x + 0, y + 1, NULL, &lum[4], 0);
gegl_sampler_get (duster->in_sampler_yu8, x + 0, y - 1, NULL, &lum[6], 0);
bdir = 0;
......@@ -301,11 +317,7 @@ static void extract_site (PixelDuster *duster, GeglBuffer *input, int x, int y,
{
int dx, dy;
duster_idx_to_x_y (duster, i, bdir, &dx, &dy);
gegl_buffer_sample (input,
x + dx,
y + dy,
NULL, &dst[i*4], format,
GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_u8, x + dx, y + dy, NULL, &dst[i*4], 0);
{
int hist_r = dst[i*4+0]/80;
int hist_g = dst[i*4+1]/80;
......@@ -318,20 +330,12 @@ static void extract_site (PixelDuster *duster, GeglBuffer *input, int x, int y,
for (int i = 0; i <= NEIGHBORHOOD; i++)
{
guchar tmp[4];
gegl_buffer_sample (input,
x,
y,
NULL, &tmp[0], format,
GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_u8, x, y, NULL, &tmp[0], 0);
for (int i = 0; i <= NEIGHBORHOOD; i++)
{
int dx, dy;
duster_idx_to_x_y (duster, i, bdir, &dx, &dy);
gegl_buffer_sample (input,
x + dx,
y + dy,
NULL, &dst[i*4], format,
GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_u8, x + dx, y + dy, NULL, &dst[i*4], 0);
if (i==0)
for (int j = 0; j < 3; j++)
......@@ -345,6 +349,7 @@ static void extract_site (PixelDuster *duster, GeglBuffer *input, int x, int y,
#endif
dst[0] = bdir;
*((uint64_t*)(&dst[4*NEIGHBORHOOD])) = hist3dmask;
}
static inline int u8_rgb_diff (guchar *a, guchar *b)
......@@ -805,8 +810,8 @@ static inline void pixel_duster_fill (PixelDuster *duster)
for (gint i = 0; i < probe->k; i++)
{
gegl_buffer_sample (duster->input, probe->source_x[i], probe->source_y[i],
NULL, &rgba[0], format, GEGL_SAMPLER_NEAREST, 0);
gegl_sampler_get (duster->in_sampler_f, probe->source_x[i], probe->source_y[i],
NULL, &rgba[0], 0);
for (gint c = 0; c < 4; c++)
sum_rgba[c] += rgba[c];
}
......
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