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

#define RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

enum {
    PARAM_SIGMA,
    PARAM_FREQ_MIN,
    PARAM_FREQ_MAX,
    PARAM_GAUSS_ENABLE,
    PARAM_GAUSS_TAU,
    PARAM_GAUSS_GENERALIZED,
    PARAM_GAUSS_P,
    PARAM_LORENTZ_ENABLE,
    PARAM_LORENTZ_TAU,
    PARAM_POWER_ENABLE,
    PARAM_POWER_P,
    PARAM_SEED,
    PARAM_RANDOMIZE,
    PARAM_UPDATE,
    PARAM_ACTIVE_PAGE,
    BUTTON_LIKE_CURRENT_IMAGE,

    PARAM_DIMS0
};

typedef struct {
    GwyParams *params;
    GwyField *field;
    GwyField *result;
    /* Cached input image parameters. */
    gdouble zscale;  /* Negative value means there is no input image. */
} ModuleArgs;

typedef struct {
    ModuleArgs *args;
    GtkWidget *dialog;
    GwyParamTable *table_dimensions;
    GwyParamTable *table_generator;
    GwyField *template_;
} ModuleGUI;

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,
                                             GwyFile *data,
                                             gint id);
static GtkWidget*       dimensions_tab_new  (ModuleGUI *gui);
static GtkWidget*       generator_tab_new   (ModuleGUI *gui);
static void             param_changed       (ModuleGUI *gui,
                                             gint id);
static void             dialog_response     (ModuleGUI *gui,
                                             gint response);
static void             preview             (gpointer user_data);

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Generates random surfaces using spectral synthesis."),
    "Yeti <yeti@gwyddion.net>",
    "2.0",
    "David Nečas (Yeti)",
    "2009",
};

GWY_MODULE_QUERY2(module_info, fourier)

static gboolean
module_register(void)
{
    gwy_synth_func_register("fourier",
                            module_main,
                            N_("/_Noise/_Spectral..."),
                            GWY_ICON_SYNTHETIC_SPECTRAL,
                            RUN_MODES,
                            N_("Generate surface using spectral synthesis"));

    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_synth_func_current());
    gwy_param_def_add_double(paramdef, PARAM_SIGMA, "sigma", _("_RMS"), 1e-5, 1000.0, 1.0);
    gwy_param_def_add_double(paramdef, PARAM_FREQ_MIN, "freq_min", _("M_inimum frequency"),
                             0.0, G_SQRT2*G_PI, 0.0);
    gwy_param_def_add_double(paramdef, PARAM_FREQ_MAX, "freq_max", _("Ma_ximum frequency"),
                             0.0, G_SQRT2*G_PI, G_SQRT2*G_PI);
    gwy_param_def_add_boolean(paramdef, PARAM_GAUSS_ENABLE, "gauss_enable", _("Enable _Gaussian multiplier"), FALSE);
    gwy_param_def_add_double(paramdef, PARAM_GAUSS_TAU, "gauss_tau", _("Autocorrelation length"), 0.25, 1000.0, 10.0);
    gwy_param_def_add_boolean(paramdef, PARAM_GAUSS_GENERALIZED, "gauss_generalized", _("General power"), FALSE);
    gwy_param_def_add_double(paramdef, PARAM_GAUSS_P, "gauss_p", _("General power"), 0.1, 12.0, 2.0);
    gwy_param_def_add_boolean(paramdef, PARAM_LORENTZ_ENABLE, "lorentz_enable", _("Enable _Lorentz multiplier"), FALSE);
    gwy_param_def_add_double(paramdef, PARAM_LORENTZ_TAU, "lorentz_tau", _("Autocorrelation length"),
                             0.25, 1000.0, 10.0);
    gwy_param_def_add_boolean(paramdef, PARAM_POWER_ENABLE, "power_enable", _("Enable _power multiplier"), FALSE);
    gwy_param_def_add_double(paramdef, PARAM_POWER_P, "power_p", _("Po_wer"), 0.0, 5.0, 1.5);
    gwy_param_def_add_seed(paramdef, PARAM_SEED, "seed", NULL);
    gwy_param_def_add_randomize(paramdef, PARAM_RANDOMIZE, PARAM_SEED, "randomize", NULL, TRUE);
    gwy_param_def_add_instant_updates(paramdef, PARAM_UPDATE, "update", NULL, TRUE);
    gwy_param_def_add_active_page(paramdef, PARAM_ACTIVE_PAGE, "active_page", NULL);
    gwy_synth_define_dimensions_params(paramdef, PARAM_DIMS0);
    return paramdef;
}

static void
module_main(GwyFile *data, GwyRunModeFlags mode)
{
    GwyDialogOutcome outcome = GWY_DIALOG_PROCEED;
    ModuleArgs args;
    GwyField *field;
    gint id;

    g_return_if_fail(mode & RUN_MODES);
    gwy_clear1(args);
    gwy_data_browser_get_current(GWY_APP_FIELD, &field,
                                 GWY_APP_FIELD_ID, &id,
                                 0);
    args.field = field;
    args.zscale = field ? gwy_field_rms(field) : -1.0;

    args.params = gwy_params_new_from_settings(define_module_params());
    gwy_synth_sanitise_params(args.params, PARAM_DIMS0, field);
    if (mode == GWY_RUN_INTERACTIVE) {
        outcome = run_gui(&args, data, id);
        gwy_params_save_to_settings(args.params);
        if (outcome == GWY_DIALOG_CANCEL)
            goto end;
    }
    args.result = gwy_synth_make_result_field((args.field = field), args.params, FALSE);
    execute(&args);
    gwy_synth_add_image_to_file(args.result, data, id, args.params);

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

static GwyDialogOutcome
run_gui(ModuleArgs *args, GwyFile *data, gint id)
{
    GwyDialogOutcome outcome;
    GwyDialog *dialog;
    GtkWidget *hbox, *dataview;
    GtkNotebook *notebook;
    ModuleGUI gui;

    gwy_clear1(gui);
    gui.args = args;
    gui.template_ = args->field;

    if (gui.template_)
        args->field = gwy_synth_make_preview_field(gui.template_, PREVIEW_SIZE);
    else
        args->field = gwy_field_new(PREVIEW_SIZE, PREVIEW_SIZE, PREVIEW_SIZE, PREVIEW_SIZE, TRUE);
    args->result = gwy_synth_make_result_field(args->field, args->params, TRUE);

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

    dataview = gwy_create_preview(args->result, NULL, PREVIEW_SIZE);
    gwy_setup_data_view(GWY_DATA_VIEW(dataview), data, GWY_FILE_IMAGE, id, GWY_FILE_ITEM_PALETTE);
    if (gui.template_)
        gwy_setup_data_view(GWY_DATA_VIEW(dataview), data, GWY_FILE_IMAGE, id, GWY_FILE_ITEM_PALETTE);
    hbox = gwy_create_dialog_preview_hbox(GTK_DIALOG(dialog), GWY_DATA_VIEW(dataview), FALSE);

    notebook = GTK_NOTEBOOK(gtk_notebook_new());
    gtk_box_pack_start(GTK_BOX(hbox), GTK_WIDGET(notebook), TRUE, TRUE, 0);

    gtk_notebook_append_page(notebook, dimensions_tab_new(&gui), gtk_label_new(_("Dimensions")));
    gtk_notebook_append_page(notebook, generator_tab_new(&gui), gtk_label_new(_("Generator")));
    gwy_param_active_page_link_to_notebook(args->params, PARAM_ACTIVE_PAGE, notebook);

    g_signal_connect_swapped(gui.table_dimensions, "param-changed", G_CALLBACK(param_changed), &gui);
    g_signal_connect_swapped(gui.table_generator, "param-changed", G_CALLBACK(param_changed), &gui);
    g_signal_connect_swapped(dialog, "response", G_CALLBACK(dialog_response), &gui);
    gwy_dialog_set_preview_func(dialog, GWY_PREVIEW_IMMEDIATE, preview, &gui, NULL);

    outcome = gwy_dialog_run(dialog);

    g_clear_object(&args->field);
    g_clear_object(&args->result);

    return outcome;
}

static GtkWidget*
dimensions_tab_new(ModuleGUI *gui)
{
    gui->table_dimensions = gwy_param_table_new(gui->args->params);
    gwy_synth_append_dimensions_to_param_table(gui->table_dimensions, 0);

    return gwy_param_table_widget(gui->table_dimensions);
}

static GtkWidget*
generator_tab_new(ModuleGUI *gui)
{
    GwyParamTable *table;

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

    gwy_param_table_append_slider(table, PARAM_SIGMA);
    gwy_param_table_slider_set_mapping(table, PARAM_SIGMA, GWY_SCALE_MAPPING_LOG);
    if (gui->template_) {
        gwy_param_table_append_button(table, BUTTON_LIKE_CURRENT_IMAGE, -1, GWY_RESPONSE_SYNTH_INIT_Z,
                                      _("_Like Current Image"));
    }

    gwy_param_table_append_separator(table);
    gwy_param_table_append_slider(table, PARAM_FREQ_MIN);
    gwy_param_table_set_unitstr(table, PARAM_FREQ_MIN, _("px<sup>-1</sup>"));
    gwy_param_table_slider_add_alt(table, PARAM_FREQ_MIN);
    gwy_param_table_append_slider(table, PARAM_FREQ_MAX);
    gwy_param_table_set_unitstr(table, PARAM_FREQ_MAX, _("px<sup>-1</sup>"));
    gwy_param_table_slider_add_alt(table, PARAM_FREQ_MAX);

    gwy_param_table_append_separator(table);
    gwy_param_table_append_checkbox(table, PARAM_GAUSS_ENABLE);
    gwy_param_table_append_slider(table, PARAM_GAUSS_TAU);
    gwy_param_table_slider_set_mapping(table, PARAM_GAUSS_TAU, GWY_SCALE_MAPPING_LOG);
    gwy_param_table_slider_add_alt(table, PARAM_GAUSS_TAU);
    gwy_param_table_append_slider(table, PARAM_GAUSS_P);
    gwy_param_table_add_enabler(table, PARAM_GAUSS_GENERALIZED, PARAM_GAUSS_P);
    gwy_param_table_slider_set_mapping(table, PARAM_GAUSS_P, GWY_SCALE_MAPPING_LINEAR);

    gwy_param_table_append_separator(table);
    gwy_param_table_append_checkbox(table, PARAM_LORENTZ_ENABLE);
    gwy_param_table_append_slider(table, PARAM_LORENTZ_TAU);
    gwy_param_table_slider_set_mapping(table, PARAM_LORENTZ_TAU, GWY_SCALE_MAPPING_LOG);
    gwy_param_table_slider_add_alt(table, PARAM_LORENTZ_TAU);

    gwy_param_table_append_separator(table);
    gwy_param_table_append_checkbox(table, PARAM_POWER_ENABLE);
    gwy_param_table_append_slider(table, PARAM_POWER_P);
    gwy_param_table_slider_set_mapping(table, PARAM_POWER_P, GWY_SCALE_MAPPING_LINEAR);

    gwy_param_table_append_header(table, -1, _("Options"));
    gwy_param_table_append_seed(table, PARAM_SEED);
    gwy_param_table_append_checkbox(table, PARAM_RANDOMIZE);
    gwy_param_table_append_separator(table);
    gwy_param_table_append_checkbox(table, PARAM_UPDATE);


    return gwy_param_table_widget(table);
}

static void
param_changed(ModuleGUI *gui, gint id)
{
    ModuleArgs *args = gui->args;
    GwyParams *params = args->params;
    GwyParamTable *table = gui->table_generator;

    if (gwy_synth_handle_param_changed(gui->table_dimensions, id))
        id = -1;

    if (id < 0 || id == PARAM_DIMS0 + GWY_DIMS_PARAM_ZUNIT) {
        static const gint zids[] = { PARAM_SIGMA };

        gwy_synth_update_value_unitstrs(table, zids, G_N_ELEMENTS(zids));
        gwy_synth_update_like_current_button_sensitivity(table, BUTTON_LIKE_CURRENT_IMAGE);
    }
    if (id < 0
        || id == PARAM_DIMS0 + GWY_DIMS_PARAM_XYUNIT
        || id == PARAM_DIMS0 + GWY_DIMS_PARAM_XRES
        || id == PARAM_DIMS0 + GWY_DIMS_PARAM_XREAL) {
        static const gint xyids[] = { PARAM_GAUSS_TAU, PARAM_LORENTZ_TAU };
        gint power10x, xres = gwy_params_get_int(params, PARAM_DIMS0 + GWY_DIMS_PARAM_XRES);
        GwyUnit *xunit = gwy_params_get_unit(params, PARAM_DIMS0 + GWY_DIMS_PARAM_XYUNIT, &power10x);
        GwyUnit *ixunit = gwy_unit_power(gwy_unit_new(NULL), xunit, -1);
        gdouble dx = gwy_params_get_double(params, PARAM_DIMS0 + GWY_DIMS_PARAM_XREAL)*gwy_exp10(power10x)/xres;
        GwyValueFormat *vf = gwy_unit_get_format_with_digits(ixunit, GWY_UNIT_FORMAT_VFMARKUP, 1.0/dx, 4, NULL);

        gwy_synth_update_lateral_alts(table, xyids, G_N_ELEMENTS(xyids));
        gwy_param_table_alt_set_linear(table, PARAM_FREQ_MIN, 1.0/(dx*vf->magnitude), 0.0, vf->units);
        gwy_param_table_alt_set_linear(table, PARAM_FREQ_MAX, 1.0/(dx*vf->magnitude), 0.0, vf->units);
        g_object_unref(ixunit);
        gwy_value_format_free(vf);
    }
    if (id < 0 || id == PARAM_GAUSS_ENABLE) {
        gboolean sens = gwy_params_get_boolean(params, PARAM_GAUSS_ENABLE);
        gwy_param_table_set_sensitive(table, PARAM_GAUSS_TAU, sens);
        gwy_param_table_set_sensitive(table, PARAM_GAUSS_P, sens);
    }
    if (id < 0 || id == PARAM_LORENTZ_ENABLE) {
        gboolean sens = gwy_params_get_boolean(params, PARAM_LORENTZ_ENABLE);
        gwy_param_table_set_sensitive(table, PARAM_LORENTZ_TAU, sens);
    }
    if (id < 0 || id == PARAM_POWER_ENABLE) {
        gboolean sens = gwy_params_get_boolean(params, PARAM_POWER_ENABLE);
        gwy_param_table_set_sensitive(table, PARAM_POWER_P, sens);
    }

    if ((id < PARAM_DIMS0 || id == PARAM_DIMS0 + GWY_DIMS_PARAM_INITIALIZE)
        && id != PARAM_UPDATE && id != PARAM_RANDOMIZE)
        gwy_dialog_invalidate(GWY_DIALOG(gui->dialog));
}

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

    if (response == GWY_RESPONSE_SYNTH_INIT_Z) {
        gdouble zscale = args->zscale;
        gint power10z;

        if (zscale > 0.0) {
            gwy_params_get_unit(args->params, PARAM_DIMS0 + GWY_DIMS_PARAM_ZUNIT, &power10z);
            gwy_param_table_set_double(gui->table_generator, PARAM_SIGMA, zscale/gwy_exp10(power10z));
        }
    }
    else if (response == GWY_RESPONSE_SYNTH_TAKE_DIMS) {
        gwy_synth_use_dimensions_template(gui->table_dimensions);
    }
}

static void
preview(gpointer user_data)
{
    ModuleGUI *gui = (ModuleGUI*)user_data;

    execute(gui->args);
    gwy_field_data_changed(gui->args->result);
}

static void
init_gauss_generalized(GwyField *finit_re, GwyField *buf_re, GwyField *buf_im,
                       gdouble tau, gdouble p)
{
    gdouble tau2 = tau*tau;

    gint xres = gwy_field_get_xres(finit_re), yres = gwy_field_get_yres(finit_re);
    gdouble *bre = gwy_field_get_data(buf_re);

#ifdef _OPENMP
#pragma omp parallel for if (gwy_threads_are_enabled()) default(none) \
            shared(bre,p,tau2,xres,yres)
#endif
    for (gint i = 0; i < yres; i++) {
        gdouble y = (i <= yres/2 ? i : yres-i);
        gdouble *row = bre + i*xres;

        for (gint j = 0; j < xres; j++) {
            gdouble x = (j <= xres/2 ? j : xres-j);
            gdouble r = (x*x + y*y)/tau2;

            row[j] = exp(-pow(r, 0.5*p));
        }
    }

    gwy_field_fft_2d_raw(buf_re, NULL, finit_re, buf_im, GWY_TRANSFORM_DIRECTION_BACKWARD);

    /* Calculate magnitude of Fourier coefficients from PSDF. */
    gdouble *fre = gwy_field_get_data(finit_re);
#ifdef _OPENMP
#pragma omp parallel for if (gwy_threads_are_enabled()) default(none) \
            shared(fre,xres,yres)
#endif
    for (gint i = 0; i < xres*yres; i++)
        fre[i] = sqrt(fabs(fre[i]));
}

static void
execute(ModuleArgs *args)
{
    GwyParams *params = args->params;
    gboolean do_initialise = gwy_params_get_boolean(params, PARAM_DIMS0 + GWY_DIMS_PARAM_INITIALIZE);
    gboolean gauss_enable = gwy_params_get_boolean(params, PARAM_GAUSS_ENABLE);
    gboolean gauss_generalized = gwy_params_get_boolean(params, PARAM_GAUSS_GENERALIZED);
    gboolean lorentz_enable = gwy_params_get_boolean(params, PARAM_LORENTZ_ENABLE);
    gboolean power_enable = gwy_params_get_boolean(params, PARAM_POWER_ENABLE);
    gdouble freq_min = gwy_params_get_double(params, PARAM_FREQ_MIN)/G_PI;
    gdouble freq_max = gwy_params_get_double(params, PARAM_FREQ_MAX)/G_PI;
    gdouble sigma = gwy_params_get_double(params, PARAM_SIGMA);
    gdouble gauss_tau = gwy_params_get_double(params, PARAM_GAUSS_TAU)*G_PI/2.0;
    gdouble gauss_p = gwy_params_get_double(params, PARAM_GAUSS_P);
    gdouble lorentz_tau = gwy_params_get_double(params, PARAM_LORENTZ_TAU)*G_PI/2.0;
    gdouble power_p = gwy_params_get_double(params, PARAM_POWER_P);
    GwyField *out_re = args->result;
    gint xres = gwy_field_get_xres(out_re), yres = gwy_field_get_yres(out_re);
    gint power10z;
    gdouble rms;

    GRand *rng = g_rand_new();
    g_rand_set_seed(rng, gwy_params_get_int(params, PARAM_SEED));

    GwyField *out_im = gwy_field_new_alike(out_re, FALSE);
    GwyField *in_re = gwy_field_new_alike(out_re, FALSE);
    GwyField *in_im = gwy_field_new_alike(out_re, FALSE);

    if (gauss_generalized && gauss_enable) {
        init_gauss_generalized(out_re, in_re, in_im, gauss_tau, gauss_p);
        /* Disable normal Gaussian when we use generalised. */
        gauss_enable = FALSE;
    }
    else
        gwy_field_fill(out_re, 1.0);

    gdouble *re = gwy_field_get_data(in_re);
    gdouble *im = gwy_field_get_data(in_im);
    gdouble *finit = gwy_field_get_data(out_re);

    /* Doing this always exactly the same way is necessary for reproducibility and stability.  */
    for (gint i = 0; i < xres*yres; i++) {
        re[i] = g_rand_double(rng);
        im[i] = g_rand_double(rng);
    }

#ifdef _OPENMP
#pragma omp parallel for if (gwy_threads_are_enabled()) default(none) \
            shared(re,im,finit,xres,yres,freq_min,freq_max,power_enable,gauss_enable,lorentz_enable,power_p,gauss_tau,lorentz_tau)
#endif
    for (gint i = 0; i < yres; i++) {
        gdouble y = (i <= yres/2 ? i : yres-i)/(0.5*yres);
        gdouble *rrow = re + i*xres, *irow = im + i*xres;
        const gdouble *frow = finit + i*xres;

        for (gint j = 0; j < xres; j++) {
            gdouble x = (j <= xres/2 ? j : xres-j)/(0.5*xres);
            gdouble r = sqrt(x*x + y*y);
            gdouble f, phi, s, c, t;

            if (r < freq_min || r > freq_max) {
                rrow[j] = irow[j] = 0.0;
                continue;
            }

            f = rrow[j]*frow[j];
            phi = 2.0*G_PI*irow[j];

            /* Note we construct Fourier coefficients, not PSDF.  So things may appear square-rooted. */
            if (power_enable)
                f /= pow(r, power_p);
            if (gauss_enable) {
                t = r*gauss_tau;
                f /= exp(0.5*t*t);
            }
            /* This is actually something that gives exponential ACF. */
            if (lorentz_enable) {
                t = r*lorentz_tau;
                t = 1.0 + t*t;
                f /= sqrt(sqrt(t*t*t));
            }

            gwy_sincos(phi, &s, &c);
            rrow[j] = f*s;
            irow[j] = f*c;
        }
    }
    re[0] = im[0] = 0.0;

    gwy_field_fft_2d_raw(in_re, in_im, out_re, out_im, GWY_TRANSFORM_DIRECTION_BACKWARD);

    gwy_params_get_unit(params, PARAM_DIMS0 + GWY_DIMS_PARAM_ZUNIT, &power10z);
    sigma *= gwy_exp10(power10z);
    rms = gwy_field_rms(out_re);
    if (rms)
        gwy_field_multiply(out_re, sigma/rms);

    if (args->field && do_initialise)
        gwy_field_sum_fields(out_re, out_re, args->field);

    g_rand_free(rng);
    g_object_unref(in_im);
    g_object_unref(in_re);
    g_object_unref(out_im);
}

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