From ca6276733208d60e6345f0214517fc7fce1dce45 Mon Sep 17 00:00:00 2001 From: Carlo Baldassi Date: Tue, 28 Apr 2009 01:22:33 +0200 Subject: [PATCH] Avoid computing the energy twice --- lqr/lqr_carver.c | 35 +++++++++++++++++++++++++++++++++++ lqr/lqr_carver_priv.h | 2 ++ lqr/lqr_energy.c | 42 ++++-------------------------------------- 3 files changed, 41 insertions(+), 38 deletions(-) diff --git a/lqr/lqr_carver.c b/lqr/lqr_carver.c index 43e1bb8..9b8328c 100644 --- a/lqr/lqr_carver.c +++ b/lqr/lqr_carver.c @@ -91,6 +91,7 @@ LqrCarver * lqr_carver_new_common (gint width, gint height, gint channels) lqr_carver_set_energy_function_builtin(r, LQR_EF_GRAD_XABS); r->nrg_xmin = NULL; r->nrg_xmax = NULL; + r->nrg_uptodate = FALSE; r->leftright = 0; r->lr_switch_frequency = 0; @@ -324,6 +325,10 @@ lqr_carver_set_image_type (LqrCarver * r, LqrImageType image_type) } r->image_type = image_type; + g_free(r->rcache); + r->rcache = NULL; + r->nrg_uptodate = FALSE; + return LQR_OK; } @@ -341,6 +346,11 @@ lqr_carver_set_alpha_channel (LqrCarver * r, gint channel_index) return LQR_ERROR; } r->image_type = LQR_CUSTOM_IMAGE; + + g_free(r->rcache); + r->rcache = NULL; + r->nrg_uptodate = FALSE; + return LQR_OK; } @@ -358,6 +368,11 @@ lqr_carver_set_black_channel (LqrCarver * r, gint channel_index) return LQR_ERROR; } r->image_type = LQR_CUSTOM_IMAGE; + + g_free(r->rcache); + r->rcache = NULL; + r->nrg_uptodate = FALSE; + return LQR_OK; } @@ -525,6 +540,11 @@ lqr_carver_build_emap (LqrCarver * r) CATCH_CANC(r); + if (r->nrg_uptodate) + { + return LQR_OK; + } + if (r->use_rcache && r->rcache == NULL) { CATCH_MEM (r->rcache = lqr_carver_generate_rcache (r)); @@ -541,6 +561,8 @@ lqr_carver_build_emap (LqrCarver * r) } } + r->nrg_uptodate = TRUE; + return LQR_OK; } @@ -985,6 +1007,7 @@ lqr_carver_inflate (LqrCarver * r, gint l) g_free (r->rigidity_mask); r->rcache = NULL; + r->nrg_uptodate = FALSE; r->rgb = new_rgb; r->preserve_in_buffer = FALSE; @@ -1073,6 +1096,8 @@ lqr_carver_carve (LqrCarver * r) #endif /* __LQR_DEBUG__ */ } } + + r->nrg_uptodate = FALSE; } @@ -1083,6 +1108,10 @@ lqr_carver_update_emap (LqrCarver * r) gint x, y; gint y1, y1_min, y1_max; + if (r->nrg_uptodate) + { + return LQR_OK; + } if (r->use_rcache) { CATCH_F (r->rcache != NULL); @@ -1123,6 +1152,9 @@ lqr_carver_update_emap (LqrCarver * r) CATCH (lqr_carver_compute_e (r, x, y)); } } + + r->nrg_uptodate = TRUE; + return LQR_OK; } @@ -1145,6 +1177,7 @@ lqr_carver_update_mmap (LqrCarver * r) gint x_stop; CATCH_CANC(r); + CATCH_F (r->nrg_uptodate); /* span first row */ /* x_min = MAX (r->vpath_x[0] - r->delta_x, 0); */ @@ -1482,6 +1515,7 @@ lqr_carver_flatten (LqrCarver * r) g_free (r->least); r->rcache = NULL; + r->nrg_uptodate = FALSE; /* allocate room for new map */ BUF_TRY_NEW0_RET_LQR(new_rgb, r->w * r->h * r->channels, r->col_depth); @@ -1648,6 +1682,7 @@ lqr_carver_transpose (LqrCarver * r) g_free (r->rgb_ro_buffer); r->rcache = NULL; + r->nrg_uptodate = FALSE; /* allocate room for the new maps */ BUF_TRY_NEW0_RET_LQR(new_rgb, r->w0 * r->h0 * r->channels, r->col_depth); diff --git a/lqr/lqr_carver_priv.h b/lqr/lqr_carver_priv.h index e808969..ad44dcc 100644 --- a/lqr/lqr_carver_priv.h +++ b/lqr/lqr_carver_priv.h @@ -249,6 +249,8 @@ struct _LqrCarver gint *nrg_xmin; /* auxiliary vector for energy update */ gint *nrg_xmax; /* auxiliary vector for energy update */ + gboolean nrg_uptodate; /* flag set if energy map is up to date */ + gdouble * rcache; /* array of brightness (or luma or else) levels for energy computation */ gboolean use_rcache; /* wheter to cache brightness, luma etc. */ diff --git a/lqr/lqr_energy.c b/lqr/lqr_energy.c index a97fc54..fd3729b 100644 --- a/lqr/lqr_energy.c +++ b/lqr/lqr_energy.c @@ -376,44 +376,6 @@ lqr_energy_builtin_null (gint x, gint y, gint img_width, gint img_height, LqrRea return 0; } - -#if 0 -/* compute energy at x, y */ -gfloat -lqr_energy_abs (LqrCarver * r, gint x, gint y) -{ - gdouble gx, gy; - - if (y == 0) - { - gy = (*(r->nrg_builtin->rfabs))(r, x, y + 1, x, y); - } - else if (y < r->h - 1) - { - gy = 0.5 * (*(r->nrg_builtin->rfabs))(r, x, y + 1, x, y - 1); - } - else - { - gy = (*(r->nrg_builtin->rfabs))(r, x, y, x, y - 1); - } - - if (x == 0) - { - gx = (*(r->nrg_builtin->rfabs))(r, x + 1, y, x, y); - } - else if (x < r->w - 1) - { - gx = 0.5 * (*(r->nrg_builtin->rfabs))(r, x + 1, y, x - 1, y); - } - else - { - gx = (*(r->nrg_builtin->rfabs))(r, x, y, x - 1, y); - } - return (*(r->nrg_builtin->gf))(gx, gy); -} -#endif - -/* gradient function for energy computation */ LQR_PUBLIC LqrRetVal lqr_carver_set_energy_function_builtin (LqrCarver * r, LqrEnergyFuncBuiltinType ef_ind) @@ -460,6 +422,10 @@ lqr_carver_set_energy_function (LqrCarver * r, LqrEnergyFunc en_func, gint radiu r->nrg_read_t = reader_type; r->nrg_extra_data = extra_data; + g_free(r->rcache); + r->rcache = NULL; + r->nrg_uptodate = FALSE; + lqr_rwindow_destroy (r->rwindow); if (reader_type == LQR_ER_CUSTOM) -- 2.11.4.GIT