/*
 *  $Id: local_contrast.c 30111 2026-06-22 11:02:00Z yeti-dn $
 *  Copyright (C) 2003-2026 David Necas (Yeti), Petr Klapetek.
 *  E-mail: yeti@gwyddion.net, klapetek@gwyddion.net.
 *
 *  This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public
 *  License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any
 *  later version.
 *
 *  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
 *  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
 *  details.
 *
 *  You should have received a copy of the GNU General Public License along with this program; if not, write to the
 *  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include "config.h"
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <gwy.h>

#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

enum {
    PARAM_SIZE,
    PARAM_DEPTH,
    PARAM_WEIGHT,
};

typedef struct {
    GwyParams *params;
    GwyField *field;
    GwyField *result;
} ModuleArgs;

static gboolean         module_register     (void);
static GwyParamDef*     define_module_params(void);
static void             module_main         (GwyFile *data,
                                             GwyRunModeFlags mode);
static void             execute             (ModuleArgs *args);
static GwyDialogOutcome run_gui             (ModuleArgs *args);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Maximizes local contrast."),
    "Yeti <yeti@gwyddion.net>",
    "2.1",
    "David Nečas (Yeti) & Petr Klapetek",
    "2005",
};

GWY_MODULE_QUERY2(module_info, local_contrast)

static gboolean
module_register(void)
{
    gwy_image_func_register("local_contrast",
                            module_main,
                            N_("/_Presentation/Local _Contrast..."),
                            NULL,
                            RUN_MODES,
                            GWY_MENU_FLAG_IMAGE,
                            N_("Presentation with maximized local contrast"));

    return TRUE;
}

static GwyParamDef*
define_module_params(void)
{
    static GwyParamDef *paramdef = NULL;

    if (paramdef)
        return paramdef;

    paramdef = gwy_param_def_new();
    gwy_param_def_set_function_name(paramdef, gwy_image_func_current());
    gwy_param_def_add_int(paramdef, PARAM_SIZE, "size", _("Kernel _size"), 1, 30, 7);
    gwy_param_def_add_int(paramdef, PARAM_DEPTH, "depth", _("Blending _depth"), 2, 7, 4);
    gwy_param_def_add_double(paramdef, PARAM_WEIGHT, "weight", _("_Weight"), 0.0, 1.0, 0.7);
    return paramdef;
}

static void
module_main(GwyFile *data, GwyRunModeFlags mode)
{
    ModuleArgs args;
    gwy_clear1(args);
    GQuark squark;
    gint id;

    g_return_if_fail(mode & RUN_MODES);
    gwy_data_browser_get_current(GWY_APP_FIELD, &args.field,
                                 GWY_APP_FIELD_ID, &id,
                                 GWY_APP_SHOW_FIELD_KEY, &squark,
                                 0);
    g_return_if_fail(args.field && squark);

    args.result = NULL;  /* Create it here once we have a GUI. */
    args.params = gwy_params_new_from_settings(define_module_params());

    if (mode == GWY_RUN_INTERACTIVE) {
        GwyDialogOutcome outcome = run_gui(&args);
        gwy_params_save_to_settings(args.params);
        if (outcome != GWY_DIALOG_PROCEED)
            goto end;
    }

    args.result = gwy_field_new_alike(args.field, FALSE);
    gwy_unit_clear(gwy_field_get_unit_z(args.result));
    execute(&args);

    gwy_field_normalize(args.result);
    gwy_app_undo_qcheckpointv(GWY_DICT(data), 1, &squark);
    gwy_dict_set_object(GWY_DICT(data), squark, args.result);
    gwy_log_add(data, GWY_FILE_IMAGE, id, id);

end:
    g_clear_object(&args.result);
    g_object_unref(args.params);
}

static GwyDialogOutcome
run_gui(ModuleArgs *args)
{
    GwyDialog *dialog;
    GwyParamTable *table;

    dialog = GWY_DIALOG(gwy_dialog_new(_("Increase Local Contrast")));
    gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_RESET, GWY_STOCK_CANCEL, GWY_STOCK_OK, 0);

    table = gwy_param_table_new(args->params);
    gwy_param_table_append_slider(table, PARAM_SIZE);
    gwy_param_table_slider_add_alt(table, PARAM_SIZE);
    gwy_param_table_alt_set_field_pixel_x(table, PARAM_SIZE, args->field);
    gwy_param_table_append_slider(table, PARAM_DEPTH);
    gwy_param_table_append_slider(table, PARAM_WEIGHT);

    gwy_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, FALSE, 0);

    return gwy_dialog_run(dialog);
}

static void
execute(ModuleArgs *args)
{
    GwyField *field = args->field, *result = args->result;
    gint size = gwy_params_get_int(args->params, PARAM_SIZE);
    gint depth = gwy_params_get_int(args->params, PARAM_DEPTH);
    gdouble weight = gwy_params_get_double(args->params, PARAM_WEIGHT);
    gint xres = gwy_field_get_xres(field), yres = gwy_field_get_yres(field);

    gdouble gmin = gwy_field_min(field), gmax = gwy_field_max(field);
    if (gmax == gmin) {
        gwy_field_clear(result);
        return;
    }

    GwyField *minfield = gwy_field_copy(field);
    gwy_field_filter_minimum(minfield, size);

    GwyField *maxfield = gwy_field_copy(field);
    gwy_field_filter_maximum(maxfield, size);

    const gdouble *dat = gwy_field_get_data_const(field);
    const gdouble *min = gwy_field_get_data_const(minfield);
    const gdouble *max = gwy_field_get_data_const(maxfield);
    gdouble *show = gwy_field_get_data(result);

    gdouble *weights = g_new(gdouble, depth);
    gdouble sum = 0.0;
    for (gint i = 0; i < depth; i++) {
        weights[i] = exp(-log(depth - 1.0)*i/(depth - 1.0));
        sum += weights[i];
    }

#ifdef _OPENMP
#pragma omp parallel for if(gwy_threads_are_enabled()) default(none) \
            shared(dat,show,min,max,weights,xres,yres,depth,size,weight,gmin,gmax,sum)
#endif
    for (gint i = 0; i < yres; i++) {
        for (gint j = 0; j < xres; j++) {
            gdouble minv = dat[i*xres + j], maxv = dat[i*xres + j];
            gdouble mins = minv*weights[0];
            gdouble maxs = maxv*weights[0];
            gdouble v, vc;
            gint k, l;

            for (k = 1; k < depth; k++) {
                gint itop = MAX(0, i - k*size)*xres, ibot = MIN(yres-1, i + k*size)*xres;
                gint jleft = MAX(0, j - k*size), jright = MIN(xres-1, j + k*size);
                for (l = 0; l < 2*k+1; l++) {
                    gint imid = CLAMP(i + (l - k)*size, 0, yres-1)*xres;
                    gint jmid = CLAMP(j + (l - k)*size, 0, xres-1);

                    /* top line */
                    maxv = fmax(maxv, max[itop + jmid]);
                    minv = fmin(minv, min[itop + jmid]);

                    /* bottom line */
                    maxv = fmax(maxv, max[ibot + jmid]);
                    minv = fmin(minv, min[ibot + jmid]);

                    /* left line */
                    maxv = fmax(maxv, max[imid + jleft]);
                    minv = fmin(minv, min[imid + jleft]);

                    /* right line */
                    maxv = fmax(maxv, max[imid + jright]);
                    minv = fmin(minv, min[imid + jright]);
                }
                mins += minv*weights[k];
                maxs += maxv*weights[k];
            }
            mins /= sum;
            maxs /= sum;
            v = dat[i*xres + j];
            if (mins < maxs) {
                vc = (gmax - gmin)/(maxs - mins)*(v - mins) + gmin;
                v = weight*vc + (1.0 - weight)*v;
                v = CLAMP(v, gmin, gmax);
            }
            show[i*xres +j] = v;
        }
    }

    g_free(weights);
    g_object_unref(minfield);
    g_object_unref(maxfield);
}

/* vim: set cin columns=120 tw=118 et ts=4 sw=4 cino=>1s,e0,n0,f0,{0,}0,^0,\:1s,=0,g1s,h0,t0,+1s,c3,(0,u0 : */
