/*
 *  $Id: resample.c 30047 2026-06-11 08:30:45Z yeti-dn $
 *  Copyright (C) 2019-2026 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 <gtk/gtk.h>
#include <gwy.h>
#include "../preview.h"

#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

/* Physical pixel size change with inverse factors compared to resolutions! */
#define MAX_UPSAMPLE 16.0
#define MAX_DOWNSAMPLE 16.0

enum {
    PARAM_DX,
    PARAM_DY,
    PARAM_SQUARE,
    PARAM_INTERP,
    PARAM_TEMPLATE,
    PARAM_MATCH_SIZE,
    INFO_NEWDIM,
};

typedef struct {
    GwyParams *params;
    /* Cached input image properties. */
    GwyField *field;
    gdouble orig_dx;
    gdouble orig_dy;
    gint orig_xres;
    gint orig_yres;
} ModuleArgs;

typedef struct {
    ModuleArgs *args;
    GtkWidget *dialog;
    GwyParamTable *table;
    GwyValueFormat *vf;
} ModuleGUI;

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 void             param_changed              (ModuleGUI *gui,
                                                    gint id);
static void             dialog_response            (GwyDialog *dialog,
                                                    gint response,
                                                    ModuleGUI *gui);
static gboolean         template_filter            (GwyFile *data,
                                                    gint id,
                                                    gpointer user_data);
static void             recalculate_new_resolutions(ModuleArgs *args,
                                                    gint *xres,
                                                    gint *yres);
static void             sanitise_params            (ModuleArgs *args);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Resamples data to specified pixel size."),
    "Yeti <yeti@gwyddion.net>",
    "2.2",
    "David Nečas (Yeti)",
    "2019",
};

GWY_MODULE_QUERY2(module_info, resample)

static gboolean
module_register(void)
{
    gwy_image_func_register("resample",
                            module_main,
                            N_("/_Basic Operations/_Resample..."),
                            NULL,
                            RUN_MODES,
                            GWY_MENU_FLAG_IMAGE,
                            N_("Resample to pixel size"));

    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_double(paramdef, PARAM_DX, "dx", _("_X pixel size"), G_MINDOUBLE, G_MAXDOUBLE, 1.0);
    gwy_param_def_add_double(paramdef, PARAM_DY, "dy", _("_Y pixel size"), G_MINDOUBLE, G_MAXDOUBLE, 1.0);
    gwy_param_def_add_boolean(paramdef, PARAM_SQUARE, "square", _("_Square pixels"), TRUE);
    gwy_param_def_add_enum(paramdef, PARAM_INTERP, "interp", NULL, GWY_TYPE_INTERPOLATION_TYPE,
                           GWY_INTERPOLATION_LINEAR);
    gwy_param_def_add_image_id(paramdef, PARAM_TEMPLATE, "template", _("_Match pixel size"));
    gwy_param_def_add_boolean(paramdef, PARAM_MATCH_SIZE, "match_size", _("_Match pixel size"), FALSE);
    return paramdef;
}

static void
module_main(GwyFile *data, GwyRunModeFlags mode)
{
    GwyField *field, *show;
    GwyNield *mask;
    GwyParams *params;
    gint oldid, newid;
    ModuleArgs args;

    g_return_if_fail(mode & RUN_MODES);
    gwy_data_browser_get_current(GWY_APP_FIELD, &field,
                                 GWY_APP_MASK_FIELD, &mask,
                                 GWY_APP_SHOW_FIELD, &show,
                                 GWY_APP_FIELD_ID, &oldid,
                                 0);
    g_return_if_fail(field);
    args.field = field;
    args.orig_xres = gwy_field_get_xres(field);
    args.orig_yres = gwy_field_get_yres(field);
    args.orig_dx = gwy_field_get_dx(field);
    args.orig_dy = gwy_field_get_dy(field);

    args.params = params = gwy_params_new_from_settings(define_module_params());
    sanitise_params(&args);

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

    GwyInterpolationType interp = gwy_params_get_enum(params, PARAM_INTERP);
    gint xres, yres;
    recalculate_new_resolutions(&args, &xres, &yres);
    gdouble xoff = gwy_field_get_xoffset(field);
    gdouble yoff = gwy_field_get_yoffset(field);
    gdouble dx = gwy_params_get_double(params, PARAM_DX);
    gdouble dy = gwy_params_get_double(params, PARAM_DY);
    xoff *= (xres*dx)/(args.orig_xres*args.orig_dx);
    yoff *= (yres*dy)/(args.orig_yres*args.orig_dy);

    field = gwy_field_new_resampled(field, xres, yres, interp);
    gwy_field_set_xreal(field, xres*dx);
    gwy_field_set_yreal(field, yres*dy);
    gwy_field_set_xoffset(field, xoff);
    gwy_field_set_yoffset(field, yoff);

    newid = gwy_file_add_image(data, field);
    gwy_file_set_visible(data, GWY_FILE_IMAGE, newid, TRUE);
    gwy_file_sync_items(data, GWY_FILE_IMAGE, oldid,
                        data, GWY_FILE_IMAGE, newid,
                        GWY_FILE_ITEM_PALETTE | GWY_FILE_ITEM_RANGE | GWY_FILE_ITEM_MASK_COLOR, FALSE);
    if (mask) {
        mask = gwy_nield_new_resampled(mask, xres, yres);
        gwy_file_pass_image_mask(data, newid, mask);
    }
    if (show) {
        show = gwy_field_new_resampled(show, xres, yres, interp);
        gwy_field_set_xreal(show, xres*dx);
        gwy_field_set_yreal(show, yres*dy);
        gwy_field_set_xoffset(show, xoff);
        gwy_field_set_yoffset(show, yoff);
        gwy_dict_pass_object(GWY_DICT(data), gwy_file_key_image_picture(newid), show);
    }

    gwy_file_set_title(data, GWY_FILE_IMAGE, newid, _("Resampled Data"), TRUE);
    gwy_log_add(data, GWY_FILE_IMAGE, oldid, newid);
    g_object_unref(field);

end:
    g_object_unref(params);
}

static GwyDialogOutcome
run_gui(ModuleArgs *args)
{
    static const int sliders[] = { PARAM_DX, PARAM_DY };
    GwyDialogOutcome outcome;
    GwyDialog *dialog;
    GwyParamTable *table;
    ModuleGUI gui;
    gint xypow10, i;
    GwyUnit *xyunit;

    gwy_clear1(gui);
    gui.args = args;

    xypow10 = 3*GWY_ROUND(0.5*log10(args->orig_dx*args->orig_dy)/3.0);
    xyunit = gwy_field_get_unit_xy(args->field);
    gui.vf = gwy_unit_get_format_for_power10(xyunit, GWY_UNIT_FORMAT_VFMARKUP, xypow10, NULL);

    gui.dialog = gwy_dialog_new(_("Resample"));
    dialog = GWY_DIALOG(gui.dialog);
    gwy_dialog_add_stock_buttons(dialog, GWY_STOCK_RESET, GWY_STOCK_CANCEL, GWY_STOCK_OK, 0);

    table = gui.table = gwy_param_table_new(args->params);

    gwy_param_table_append_image_id(table, PARAM_TEMPLATE);
    gwy_param_table_data_id_set_filter(table, PARAM_TEMPLATE, template_filter, args->field, NULL);
    gwy_param_table_add_enabler(table, PARAM_MATCH_SIZE, PARAM_TEMPLATE);

    for (i = 0; i < (gint)G_N_ELEMENTS(sliders); i++) {
        gint id = sliders[i];
        gwy_param_table_append_slider(table, id);
        gwy_param_table_slider_restrict_range(table, id, args->orig_dx/MAX_UPSAMPLE, args->orig_dx*MAX_DOWNSAMPLE);
        gwy_param_table_slider_set_mapping(table, id, GWY_SCALE_MAPPING_LOG);
        gwy_param_table_set_unitstr(table, id, gui.vf->units);
        gwy_param_table_slider_set_factor(table, id, 1.0/gui.vf->magnitude);
    }

    gwy_param_table_append_checkbox(table, PARAM_SQUARE);
    gwy_param_table_append_combo(table, PARAM_INTERP);
    gwy_param_table_append_info(table, INFO_NEWDIM, _("New dimensions"));
    gwy_param_table_set_unitstr(table, INFO_NEWDIM, _("px"));

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

    g_signal_connect_swapped(table, "param-changed", G_CALLBACK(param_changed), &gui);
    g_signal_connect_after(dialog, "response", G_CALLBACK(dialog_response), &gui);

    outcome = gwy_dialog_run(dialog);

    gwy_value_format_free(gui.vf);

    return outcome;
}

static void
param_changed(ModuleGUI *gui, gint id)
{
    GwyParamTable *table = gui->table;
    ModuleArgs *args = gui->args;
    GwyParams *params = args->params;
    gboolean match_size = gwy_params_get_boolean(params, PARAM_MATCH_SIZE);
    gboolean square = gwy_params_get_boolean(params, PARAM_SQUARE);
    gdouble dx = gwy_params_get_double(params, PARAM_DX);
    gdouble dy = gwy_params_get_double(params, PARAM_DY);
    gboolean has_any = !gwy_params_data_id_is_none(params, PARAM_TEMPLATE);
    gint xres, yres;
    gchar *s;

    if (id < 0)
        gwy_param_table_set_sensitive(table, PARAM_TEMPLATE, has_any);
    if (id < 0 || id == PARAM_MATCH_SIZE) {
        gwy_param_table_set_sensitive(table, PARAM_DX, !match_size);
        gwy_param_table_set_sensitive(table, PARAM_DY, !match_size);
        gwy_param_table_set_sensitive(table, PARAM_SQUARE, !match_size);
    }
    if ((id < 0 || id == PARAM_TEMPLATE || id == PARAM_MATCH_SIZE) && has_any && match_size) {
        GwyField *otherfield = gwy_params_get_image(params, PARAM_TEMPLATE);
        gwy_param_table_set_double(table, PARAM_DX, (dx = gwy_field_get_dx(otherfield)));
        gwy_param_table_set_double(table, PARAM_DY, (dy = gwy_field_get_dy(otherfield)));
        if (dx != dy)
            gwy_param_table_set_boolean(table, PARAM_SQUARE, (square = FALSE));
    }
    if (id == PARAM_SQUARE && square && dx != dy) {
        dx = dy = sqrt(dx*dy);
        gwy_param_table_set_double(table, PARAM_DX, dx);
        gwy_param_table_set_double(table, PARAM_DY, dy);
        /* XXX: We should enforce identical slider ranges when square is enabled. */
    }
    if (id == PARAM_DX && square)
        gwy_param_table_set_double(table, PARAM_DY, (dy = dx));
    if (id == PARAM_DY && square)
        gwy_param_table_set_double(table, PARAM_DX, (dx = dy));

    recalculate_new_resolutions(args, &xres, &yres);
    s = g_strdup_printf(_("%d × %d"), xres, yres);
    gwy_param_table_info_set_valuestr(table, INFO_NEWDIM, s);
    g_free(s);
}

static void
dialog_response(G_GNUC_UNUSED GwyDialog *dialog, gint response, ModuleGUI *gui)
{
    ModuleArgs *args = gui->args;

    if (response == GWY_RESPONSE_RESET) {
        gwy_param_table_set_boolean(gui->table, PARAM_SQUARE, args->orig_dx == args->orig_dy);
        gwy_param_table_set_double(gui->table, PARAM_DX, args->orig_dx);
        gwy_param_table_set_double(gui->table, PARAM_DY, args->orig_dy);
    }
}

static gboolean
template_filter(GwyFile *data, gint id, gpointer user_data)
{
    GwyField *otherfield, *field = (GwyField*)user_data;
    gdouble d, dother;

    if (!(otherfield = gwy_file_get_image(data, id)))
        return FALSE;
    if (otherfield == field)
        return FALSE;
    if (gwy_field_is_incompatible(field, otherfield, GWY_DATA_MISMATCH_LATERAL))
        return FALSE;

    d = gwy_field_get_dx(field);
    dother = gwy_field_get_dx(otherfield);
    if (dother > d*MAX_DOWNSAMPLE || dother < d/MAX_UPSAMPLE)
        return FALSE;

    d = gwy_field_get_dy(field);
    dother = gwy_field_get_dy(otherfield);
    if (dother > d*MAX_DOWNSAMPLE || dother < d/MAX_UPSAMPLE)
        return FALSE;

    return TRUE;
}

static void
recalculate_new_resolutions(ModuleArgs *args, gint *xres, gint *yres)
{
    gdouble dx = gwy_params_get_double(args->params, PARAM_DX);
    gdouble dy = gwy_params_get_double(args->params, PARAM_DY);

    *xres = GWY_ROUND(args->orig_xres*args->orig_dx/dx);
    *xres = MAX(*xres, 1);
    *yres = GWY_ROUND(args->orig_yres*args->orig_dy/dy);
    *yres = MAX(*yres, 1);
}

static void
sanitise_params(ModuleArgs *args)
{
    GwyParams *params = args->params;
    gboolean match_size = gwy_params_get_boolean(params, PARAM_MATCH_SIZE);
    GwyAppDataId template = gwy_params_get_data_id(params, PARAM_TEMPLATE);
    gboolean is_none = gwy_params_data_id_is_none(params, PARAM_TEMPLATE);
    gboolean square = gwy_params_get_boolean(params, PARAM_SQUARE);
    gdouble dx = gwy_params_get_double(params, PARAM_DX);
    gdouble dy = gwy_params_get_double(params, PARAM_DY);

    if (match_size
        && (is_none || !template_filter(gwy_data_browser_get_file(template.datano), template.id, args->field)))
        gwy_params_set_boolean(params, PARAM_MATCH_SIZE, (match_size = FALSE));

    if (match_size) {
        GwyField *otherfield = gwy_params_get_image(params, PARAM_TEMPLATE);
        /* template_filter() only allows template fields which are not too crazy-sized. */
        gwy_params_set_double(params, PARAM_DX, gwy_field_get_dx(otherfield));
        gwy_params_set_double(params, PARAM_DY, gwy_field_get_dy(otherfield));
    }
    else {
        dx = CLAMP(dx, args->orig_dx/MAX_UPSAMPLE, args->orig_dx*MAX_DOWNSAMPLE);
        gwy_params_set_double(params, PARAM_DX, dx);
        dy = CLAMP(dy, args->orig_dy/MAX_UPSAMPLE, args->orig_dy*MAX_DOWNSAMPLE);
        gwy_params_set_double(params, PARAM_DX, dy);
    }

    if (dx != dy)
        gwy_params_set_boolean(params, PARAM_SQUARE, (square = FALSE));
}

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