/*
 *  $Id: rank.c 30111 2026-06-22 11:02:00Z yeti-dn $
 *  Copyright (C) 2014-2021 David Necas (Yeti).
 *  E-mail: yeti@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 <string.h>
#include <gtk/gtk.h>
#include <gwy.h>
#include "libgwyddion/omp.h"

#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

typedef enum {
    FILTER_RANK          = 0,
    FILTER_NORMALIZATION = 1,
    FILTER_RANGE         = 2,
    FILTER_NFILTERS,
} FilterType;

enum {
    PARAM_SIZE,
    PARAM_TYPE
};

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 GwyDialogOutcome run_gui             (ModuleArgs *args);
static gboolean         execute_rank        (ModuleArgs *args,
                                             GwyFile *data,
                                             gint id);
static void             execute_minmax      (ModuleArgs *args);
static gdouble          local_rank          (GwyField *field,
                                             gint size,
                                             const gint *xsize,
                                             gint col,
                                             gint row);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Enhances local contrast using a rank transform."),
    "Yeti <yeti@gwyddion.net>",
    "3.0",
    "David Nečas (Yeti) & Petr Klapetek",
    "2014",
};

GWY_MODULE_QUERY2(module_info, rank)

static gboolean
module_register(void)
{
    gwy_image_func_register("rank",
                            module_main,
                            N_("/_Presentation/_Rank..."),
                            GWY_ICON_RANK_FILTER,
                            RUN_MODES,
                            GWY_MENU_FLAG_IMAGE,
                            N_("Presentation with local contrast ehnanced using a rank transform"));

    return TRUE;
}

static GwyParamDef*
define_module_params(void)
{
    static const GwyEnum types[] = {
        { N_("Rank transform"),      FILTER_RANK,          },
        { N_("Local normalization"), FILTER_NORMALIZATION, },
        { N_("Value range"),         FILTER_RANGE,         },
    };
    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, 129, 15);
    gwy_param_def_add_gwyenum(paramdef, PARAM_TYPE, "type", _("_Filter type"),
                              types, G_N_ELEMENTS(types), FILTER_RANK);
    return paramdef;
}

static void
module_main(GwyFile *data, GwyRunModeFlags mode)
{
    ModuleArgs args;
    gwy_clear1(args);
    GQuark squark;
    gboolean ok = TRUE;
    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));

    if (gwy_params_get_enum(args.params, PARAM_TYPE) == FILTER_RANK)
        ok = execute_rank(&args, data, id);
    else
        execute_minmax(&args);

    if (ok) {
        gwy_field_normalize(args.result);
        gwy_app_undo_qcheckpointv(GWY_DICT(data), 1, &squark);
        gwy_dict_set_object(GWY_DICT(data), gwy_file_key_image_picture(id), 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(_("Rank Transform")));
    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_radio(table, PARAM_TYPE);
    gwy_param_table_append_separator(table);
    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_dialog_add_content(dialog, gwy_param_table_widget(table), FALSE, FALSE, 0);

    return gwy_dialog_run(dialog);
}

static gboolean
execute_rank(ModuleArgs *args, GwyFile *data, gint id)
{
    GwyField *showfield = args->result, *field = args->field;
    gdouble *show;
    gint xres, yres, asize, size, k;
    gint *xsize;
    gboolean cancelled = FALSE, *pcancelled = &cancelled;

    xres = gwy_field_get_xres(field);
    yres = gwy_field_get_yres(field);
    show = gwy_field_get_data(showfield);

    asize = gwy_params_get_int(args->params, PARAM_SIZE);
    size = 2*asize + 1;

    gwy_app_wait_start(gwy_data_browser_get_window_for_data(data, GWY_FILE_IMAGE, id), _("Rank transform..."));

    xsize = g_new(gint, size);
    for (k = -(gint)asize; k <= (gint)asize; k++) {
        gdouble x = sqrt(0.25*size*size - k*k);
        xsize[k + asize] = (gint)floor(x);
    }

#ifdef _OPENMP
#pragma omp parallel if (gwy_threads_are_enabled()) default(none) \
            shared(show,field,xres,yres,xsize,asize,pcancelled)
#endif
    {
        gint ifrom = gwy_omp_chunk_start(yres), ito = gwy_omp_chunk_end(yres);
        gint i, j;

        for (i = ifrom; i < ito; i++) {
            for (j = 0; j < xres; j++)
                show[i*xres + j] = local_rank(field, asize, xsize, j, i);

            if (gwy_omp_set_fraction_check_cancel(gwy_app_wait_set_fraction, i, ifrom, ito, pcancelled))
                break;
        }
    }
    gwy_app_wait_finish();
    g_free(xsize);

    return !cancelled;
}

static gdouble
local_rank(GwyField *field, gint size, const gint *xsize, gint col, gint row)
{
    gint xres, yres, i, j, yfrom, yto;
    guint r, hr, t;
    const gdouble *data;
    gdouble v;

    xres = field->xres;
    yres = field->yres;
    data = gwy_field_get_data_const(field);
    v = data[row*xres + col];

    yfrom = MAX(0, row - size);
    yto = MIN(yres-1, row + size);

    r = hr = t = 0;
    for (i = yfrom; i <= yto; i++) {
        gint xr = xsize[i - row + size];
        gint xfrom = MAX(0, col - xr);
        gint xto = MIN(xres-1, col + xr);
        guint xlen = xto - xfrom + 1;
        const gdouble *d = data + i*xres + xfrom;

        for (j = xlen; j; j--, d++) {
            if (*d <= v) {
                r++;
                if (G_UNLIKELY(*d == v))
                    hr++;
            }
            t++;
        }
    }

    return (r - 0.5*hr)/t;
}

static void
execute_minmax(ModuleArgs *args)
{
    GwyField *showfield = args->result;
    GwyMinMaxFilterType filtertype;
    gint size;

    size = 2*gwy_params_get_int(args->params, PARAM_SIZE) + 1;
    filtertype = (gwy_params_get_enum(args->params, PARAM_TYPE) == FILTER_NORMALIZATION
                  ? GWY_MIN_MAX_FILTER_NORMALIZATION
                  : GWY_MIN_MAX_FILTER_RANGE);
    GwyNield *kernel = gwy_nield_new(size, size);
    gwy_nield_elliptic_fill(kernel);
    gwy_field_copy_data(args->field, showfield);
    gwy_field_area_filter_min_max(showfield, kernel, filtertype, 0, 0, showfield->xres, showfield->yres);
    g_object_unref(kernel);
}

/* 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 : */
