File: psdf2d.c

package info (click to toggle)
gwyddion 2.52-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 46,588 kB
  • sloc: ansic: 367,740; python: 7,788; sh: 5,245; makefile: 4,317; xml: 3,631; cpp: 2,550; pascal: 418; perl: 154; ruby: 130
file content (994 lines) | stat: -rw-r--r-- 37,951 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
/*
 *  $Id: psdf2d.c 21435 2018-09-11 12:39:53Z yeti-dn $
 *  Copyright (C) 2009-2018 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 <stdlib.h>
#include <gtk/gtk.h>
#include <libgwyddion/gwymacros.h>
#include <libgwyddion/gwymath.h>
#include <libprocess/inttrans.h>
#include <libprocess/gwyprocesstypes.h>
#include <libprocess/stats.h>
#include <libprocess/linestats.h>
#include <libgwydgets/gwygraph.h>
#include <libgwydgets/gwycombobox.h>
#include <libgwydgets/gwystock.h>
#include <libgwymodule/gwymodule-process.h>
#include <app/gwymoduleutils.h>
#include <app/gwyapp.h>
#include "preview.h"

#define PSDF2D_RUN_MODES (GWY_RUN_IMMEDIATE | GWY_RUN_INTERACTIVE)

/* Keep it simple and use a predefined set of zooms, these seem suitable. */
typedef enum {
    ZOOM_1 = 1,
    ZOOM_4 = 4,
    ZOOM_16 = 16
} ZoomType;

enum {
    GRAPH_WIDTH = 320,
    NLINES = 12,
    MIN_RESOLUTION = 4,
    MAX_RESOLUTION = 16384,
    MAX_THICKNESS = 128,
};

typedef struct {
    GwyMaskingType masking;
    ZoomType zoom;
    gboolean create_image;
    gboolean zoomed_image;
    gboolean separate;
    gboolean fixres;
    gint resolution;
    gint thickness;
    GwyInterpolationType interpolation;
    GwyWindowingType windowing;
    GwyAppDataId target_graph;
} PSDFArgs;

typedef struct {
    PSDFArgs *args;
    GtkWidget *create_image;
    GtkWidget *zoomed_image;
    GtkWidget *dialogue;
    GtkObject *resolution;
    GtkWidget *fixres;
    GtkObject *thickness;
    GtkWidget *interpolation;
    GtkWidget *windowing;
    GtkWidget *view;
    GSList *masking;
    GSList *zoom;
    gdouble hx;
    gdouble hy;
    GwyDataField *dfield;
    GwyDataField *mask;
    GwyDataField *psdf;
    GwyDataField *modulus;
    GwySelection *selection;
    GtkWidget *graph;
    GwyDataLine *line;
    GwyGraphModel *gmodel;
    GtkWidget *separate;
    GtkWidget *target_graph;
    GwyContainer *mydata;
} PSDFControls;

static gboolean      module_register        (void);
static void          psdf2d                 (GwyContainer *data,
                                             GwyRunType run);
static void          psdf2d_dialogue        (PSDFArgs *args,
                                             GwyContainer *data,
                                             GwyDataField *dfield,
                                              GwyDataField *mask,
                                             gint id);
static void          masking_changed        (GtkToggleButton *toggle,
                                             PSDFControls *controls);
static void          zoom_changed           (GtkRadioButton *button,
                                             PSDFControls *controls);
static void          create_image_changed   (PSDFControls *controls);
static void          zoomed_image_changed   (PSDFControls *controls);
static void          fixres_changed         (PSDFControls *controls,
                                             GtkToggleButton *check);
static void          resolution_changed     (PSDFControls *controls,
                                             GtkAdjustment *adj);
static void          thickness_changed      (PSDFControls *controls,
                                             GtkAdjustment *adj);
static void          interpolation_changed  (GtkComboBox *combo,
                                             PSDFControls *controls);
static void          windowing_changed      (PSDFControls *controls,
                                             GtkComboBox *combo);
static void          separate_changed       (PSDFControls *controls);
static void          selection_changed      (PSDFControls *controls,
                                             gint hint);
static void          recalculate_psdf       (PSDFControls *controls);
static void          update_sensitivity     (PSDFControls *controls);
static void          update_target_graphs   (PSDFControls *controls);
static gboolean      filter_target_graphs   (GwyContainer *data,
                                             gint id,
                                             gpointer user_data);
static void          target_graph_changed   (PSDFControls *controls);
static void          update_curve           (PSDFControls *controls,
                                             gint i);
static void          init_graph_model_units (GwyGraphModel *gmodel,
                                             GwyDataField *psdf);
static void          calculate_zoomed_fields(PSDFControls *controls);
static GwyDataField* cut_field_to_zoom      (GwyDataField *dfield,
                                             gint zoom);
static void          create_output_graphs   (PSDFControls *controls,
                                             GwyContainer *data);
static gint          create_results         (GwyContainer *data,
                                             GwyDataField *psdf,
                                             GwySelection *selection,
                                             gint oldid,
                                             gint zoom);
static void          calculate_psdf         (GwyDataField *dfield,
                                             GwyDataField *mask,
                                             const PSDFArgs *args,
                                             GwyDataField *psdf,
                                             GwyDataField *modulus);
static void          psdf2d_load_args       (GwyContainer *container,
                                             PSDFArgs *args);
static void          psdf2d_save_args       (GwyContainer *container,
                                             PSDFArgs *args);
static void          psdf2d_sanitize_args   (PSDFArgs *args);

static const PSDFArgs psdf2d_defaults = {
    GWY_MASK_IGNORE, ZOOM_1, TRUE, TRUE,
    FALSE, FALSE, 120, 1,
    GWY_INTERPOLATION_LINEAR,
    GWY_WINDOWING_HANN,
    GWY_APP_DATA_ID_NONE,
};

static GwyAppDataId target_id = GWY_APP_DATA_ID_NONE;

static GwyModuleInfo module_info = {
    GWY_MODULE_ABI_VERSION,
    &module_register,
    N_("Calculates two-dimensional power spectrum density function "
       "and extracts its linear profiles."),
    "Yeti <yeti@gwyddion.net>",
    "1.1",
    "David Nečas (Yeti)",
    "2009",
};

GWY_MODULE_QUERY2(module_info, psdf2d)

static gboolean
module_register(void)
{
    gwy_process_func_register("psdf2d",
                              (GwyProcessFunc)&psdf2d,
                              N_("/_Statistics/2D _PSDF..."),
                              /*GWY_STOCK_PSDF_SECTION*/ NULL,
                              PSDF2D_RUN_MODES,
                              GWY_MENU_FLAG_DATA,
                              N_("Calculate 2D power spectrum density"));

    return TRUE;
}

static void
psdf2d(GwyContainer *data, GwyRunType run)
{
    PSDFArgs args;
    GwyDataField *dfield, *mask, *psdf, *modulus;
    gint id;

    g_return_if_fail(run & PSDF2D_RUN_MODES);
    g_return_if_fail(g_type_from_name("GwyLayerPoint"));
    psdf2d_load_args(gwy_app_settings_get(), &args);
    gwy_app_data_browser_get_current(GWY_APP_DATA_FIELD, &dfield,
                                     GWY_APP_MASK_FIELD, &mask,
                                     GWY_APP_DATA_FIELD_ID, &id,
                                     0);
    g_return_if_fail(dfield);

    if (run == GWY_RUN_IMMEDIATE) {
        /* Is it reasonable to simply do nothing when settings say to not
         * create the PSDF image? */
        if (args.create_image) {
            psdf = gwy_data_field_new(1, 1, 1.0, 1.0, FALSE);
            modulus = gwy_data_field_new(1, 1, 1.0, 1.0, FALSE);
            calculate_psdf(dfield, mask, &args, psdf, modulus);
            create_results(data, psdf, NULL, id,
                           args.zoomed_image ? args.zoom : ZOOM_1);
            g_object_unref(psdf);
            g_object_unref(modulus);
        }
    }
    else {
        psdf2d_dialogue(&args, data, dfield, mask, id);
        psdf2d_save_args(gwy_app_settings_get(), &args);
    }
}

static void
psdf2d_dialogue(PSDFArgs *args,
                GwyContainer *data,
                GwyDataField *dfield,
                GwyDataField *mask,
                gint id)
{
    GtkWidget *hbox, *vbox, *notebook, *label, *hbox2;
    GwyDataChooser *chooser;
    GtkTable *table;
    GtkDialog *dialogue;
    GwyGraph *graph;
    GwyGraphArea *area;
    PSDFControls controls;
    gint response, row;
    GSList *l;

    controls.args = args;
    controls.dfield = dfield;
    controls.mask = mask;
    controls.psdf = gwy_data_field_new(1, 1, 1.0, 1.0, FALSE);
    controls.modulus = gwy_data_field_new(1, 1, 1.0, 1.0, FALSE);
    calculate_psdf(dfield, mask, args, controls.psdf, controls.modulus);
    controls.hx = gwy_data_field_get_dx(dfield)/(2*G_PI);
    controls.hy = gwy_data_field_get_dx(dfield)/(2*G_PI);

    controls.dialogue = gtk_dialog_new_with_buttons(_("Power Spectrum Density"),
                                                    NULL, 0,
                                                    GTK_STOCK_CLEAR,
                                                    RESPONSE_CLEAR,
                                                    GTK_STOCK_CANCEL,
                                                    GTK_RESPONSE_CANCEL,
                                                    GTK_STOCK_OK,
                                                    GTK_RESPONSE_OK,
                                                    NULL);
    dialogue = GTK_DIALOG(controls.dialogue);
    gtk_dialog_set_default_response(dialogue, GTK_RESPONSE_OK);
    gtk_dialog_set_response_sensitive(dialogue, GTK_RESPONSE_OK, FALSE);
    gwy_help_add_to_proc_dialog(GTK_DIALOG(dialogue), GWY_HELP_DEFAULT);

    hbox = gtk_hbox_new(FALSE, 2);

    gtk_box_pack_start(GTK_BOX(GTK_DIALOG(dialogue)->vbox), hbox,
                       FALSE, FALSE, 4);

    /***** PSDF preview *****/
    controls.mydata = gwy_container_new();
    calculate_zoomed_fields(&controls);
    gwy_container_set_string_by_name(controls.mydata, "/0/base/palette",
                                     g_strdup("DFit"));
    gwy_container_set_enum_by_name(controls.mydata, "/0/base/range-type",
                                   GWY_LAYER_BASIC_RANGE_AUTO);
    gwy_app_sync_data_items(data, controls.mydata, id, 0, FALSE,
                            GWY_DATA_ITEM_REAL_SQUARE,
                            0);
    controls.view = create_preview(controls.mydata, 0, PREVIEW_SIZE, FALSE);
    controls.selection = create_vector_layer(GWY_DATA_VIEW(controls.view), 0,
                                             "Point", TRUE);
    gwy_selection_set_max_objects(controls.selection, NLINES);
    g_object_set(gwy_data_view_get_top_layer(GWY_DATA_VIEW(controls.view)),
                 "draw-as-vector", TRUE,
                 NULL);
    g_signal_connect_swapped(controls.selection, "changed",
                             G_CALLBACK(selection_changed), &controls);

    gtk_box_pack_start(GTK_BOX(hbox), controls.view, FALSE, FALSE, 4);

    /***** Graph *****/
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 4);

    controls.gmodel = gwy_graph_model_new();
    g_object_set(controls.gmodel,
                 "title", _("PSDF Section"),
                 "axis-label-bottom", "k",
                 "axis-label-left", "W",
                 NULL);
    init_graph_model_units(controls.gmodel, controls.psdf);
    controls.line = gwy_data_line_new(1, 1.0, FALSE);

    controls.graph = gwy_graph_new(controls.gmodel);
    graph = GWY_GRAPH(controls.graph);
    gtk_widget_set_size_request(controls.graph, GRAPH_WIDTH, -1);
    gwy_graph_set_axis_visible(graph, GTK_POS_LEFT, FALSE);
    gwy_graph_set_axis_visible(graph, GTK_POS_RIGHT, FALSE);
    gwy_graph_set_axis_visible(graph, GTK_POS_TOP, FALSE);
    gwy_graph_set_axis_visible(graph, GTK_POS_BOTTOM, FALSE);
    gwy_graph_enable_user_input(graph, FALSE);
    area = GWY_GRAPH_AREA(gwy_graph_get_area(graph));
    gwy_graph_area_enable_user_input(area, FALSE);
    gtk_box_pack_start(GTK_BOX(vbox), controls.graph, TRUE, TRUE, 0);

    notebook = gtk_notebook_new();
    gtk_box_pack_start(GTK_BOX(vbox), notebook, FALSE, FALSE, 0);

    /***** PSDF controls *****/
    table = GTK_TABLE(gtk_table_new(6 + 4*(!!mask), 3, FALSE));
    gtk_table_set_row_spacings(table, 2);
    gtk_table_set_col_spacings(table, 6);
    gtk_container_set_border_width(GTK_CONTAINER(table), 4);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(table),
                             gtk_label_new("PSDF"));
    row = 0;

    hbox2 = gtk_hbox_new(FALSE, 8);
    gtk_table_attach(table, hbox2, 0, 4, row, row+1, GTK_FILL, 0, 0, 0);
    label = gtk_label_new(_("Zoom:"));
    gtk_box_pack_start(GTK_BOX(hbox2), label, FALSE, FALSE, 0);

    controls.zoom = gwy_radio_buttons_createl(G_CALLBACK(zoom_changed),
                                              &controls,
                                              args->zoom,
                                              "1×", ZOOM_1,
                                              "4×", ZOOM_4,
                                              "16×", ZOOM_16,
                                              NULL);
    for (l = controls.zoom; l; l = g_slist_next(l)) {
        GtkWidget *widget = GTK_WIDGET(l->data);
        gtk_box_pack_start(GTK_BOX(hbox2), widget, FALSE, FALSE, 0);
    }
    row++;

    controls.windowing
        = gwy_enum_combo_box_new(gwy_windowing_type_get_enum(), -1,
                                 G_CALLBACK(gwy_enum_combo_box_update_int),
                                 &args->windowing, args->windowing, TRUE);
    gwy_table_attach_adjbar(GTK_WIDGET(table), row, _("_Windowing type:"), NULL,
                            GTK_OBJECT(controls.windowing),
                            GWY_HSCALE_WIDGET_NO_EXPAND);
    g_signal_connect_swapped(controls.windowing, "changed",
                             G_CALLBACK(windowing_changed), &controls);
    row++;

    gtk_table_set_row_spacing(table, row-1, 8);
    controls.create_image
        = gtk_check_button_new_with_mnemonic(_("Create PSDF image"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.create_image),
                                 args->create_image);
    gtk_table_attach(table, controls.create_image,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    g_signal_connect_swapped(controls.create_image, "toggled",
                             G_CALLBACK(create_image_changed), &controls);
    row++;

    controls.zoomed_image
        = gtk_check_button_new_with_mnemonic(_("Only zoomed part"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.zoomed_image),
                                 args->zoomed_image);
    gtk_table_attach(table, controls.zoomed_image,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    g_signal_connect_swapped(controls.zoomed_image, "toggled",
                             G_CALLBACK(zoomed_image_changed), &controls);
    row++;

    if (mask) {
        gtk_table_set_row_spacing(table, row-1, 8);
        gtk_table_attach(table,
                         gwy_label_new_header(_("Masking Mode")),
                         0, 2, row, row+1, GTK_FILL, 0, 0, 0);
        row++;

        controls.masking
            = gwy_radio_buttons_create(gwy_masking_type_get_enum(), -1,
                                       G_CALLBACK(masking_changed),
                                       &controls, args->masking);
        row = gwy_radio_buttons_attach_to_table(controls.masking, table,
                                                2, row);
    }
    else
        controls.masking = NULL;

    /***** Graph controls *****/
    table = GTK_TABLE(gtk_table_new(4, 3, FALSE));
    gtk_table_set_row_spacings(table, 2);
    gtk_table_set_col_spacings(table, 6);
    gtk_container_set_border_width(GTK_CONTAINER(table), 4);
    gtk_notebook_append_page(GTK_NOTEBOOK(notebook), GTK_WIDGET(table),
                             gtk_label_new("Graph"));
    row = 0;

    controls.resolution = gtk_adjustment_new(controls.args->resolution,
                                             MIN_RESOLUTION, MAX_RESOLUTION,
                                             1, 10, 0);
    gwy_table_attach_adjbar(GTK_WIDGET(table), row,
                            _("_Fixed resolution:"), NULL,
                            controls.resolution,
                            GWY_HSCALE_CHECK | GWY_HSCALE_SQRT);
    g_signal_connect_swapped(controls.resolution, "value-changed",
                             G_CALLBACK(resolution_changed), &controls);
    controls.fixres = gwy_table_hscale_get_check(controls.resolution);
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.fixres),
                                 controls.args->fixres);
    g_signal_connect_swapped(controls.fixres, "toggled",
                             G_CALLBACK(fixres_changed), &controls);
    row++;

    controls.thickness = gtk_adjustment_new(controls.args->thickness,
                                            1, MAX_THICKNESS, 1, 10, 0);
    gwy_table_attach_adjbar(GTK_WIDGET(table), row, _("_Thickness:"), _("px"),
                            controls.thickness, GWY_HSCALE_DEFAULT);
    g_signal_connect_swapped(controls.thickness, "value-changed",
                             G_CALLBACK(thickness_changed), &controls);
    row++;

    controls.separate = gtk_check_button_new_with_mnemonic(_("_Separate curves"));
    gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(controls.separate),
                                 args->separate);
    gtk_table_attach(table, controls.separate,
                     0, 2, row, row+1, GTK_FILL, 0, 0, 0);
    g_signal_connect_swapped(controls.separate, "toggled",
                             G_CALLBACK(separate_changed), &controls);
    row++;

    controls.interpolation
        = gwy_enum_combo_box_new(gwy_interpolation_type_get_enum(), -1,
                                 G_CALLBACK(interpolation_changed),
                                 &controls,
                                 controls.args->interpolation, TRUE);
    gwy_table_attach_adjbar(GTK_WIDGET(table), row,
                            _("_Interpolation type:"), NULL,
                            GTK_OBJECT(controls.interpolation),
                            GWY_HSCALE_WIDGET_NO_EXPAND);
    row++;

    controls.target_graph = gwy_data_chooser_new_graphs();
    chooser = GWY_DATA_CHOOSER(controls.target_graph);
    gwy_data_chooser_set_none(chooser, _("New graph"));
    gwy_data_chooser_set_active(chooser, NULL, -1);
    gwy_data_chooser_set_filter(chooser, filter_target_graphs, &controls, NULL);
    gwy_data_chooser_set_active_id(chooser, &args->target_graph);
    gwy_data_chooser_get_active_id(chooser, &args->target_graph);
    gwy_table_attach_adjbar(GTK_WIDGET(table), row,
                            _("Target _graph:"), NULL,
                            GTK_OBJECT(controls.target_graph),
                            GWY_HSCALE_WIDGET_NO_EXPAND);
    g_signal_connect_swapped(controls.target_graph, "changed",
                             G_CALLBACK(target_graph_changed), &controls);
    row++;

    update_sensitivity(&controls);

    gtk_widget_show_all(controls.dialogue);
    do {
        response = gtk_dialog_run(dialogue);
        switch (response) {
            case GTK_RESPONSE_CANCEL:
            case GTK_RESPONSE_DELETE_EVENT:
            gtk_widget_destroy(controls.dialogue);
            case GTK_RESPONSE_NONE:
            goto finalize;
            break;

            case GTK_RESPONSE_OK:
            break;

            case RESPONSE_CLEAR:
            gwy_selection_clear(controls.selection);
            break;

            default:
            g_assert_not_reached();
            break;
        }
    } while (response != GTK_RESPONSE_OK);

    if (args->create_image) {
        gwy_data_field_invalidate(controls.psdf);
        create_results(data, controls.psdf, controls.selection, id,
                       args->zoomed_image ? args->zoom : ZOOM_1);
    }
    create_output_graphs(&controls, data);
    gtk_widget_destroy(controls.dialogue);
finalize:
    g_object_unref(controls.mydata);
    g_object_unref(controls.psdf);
    g_object_unref(controls.modulus);
    g_object_unref(controls.gmodel);
    g_object_unref(controls.line);
}

static void
masking_changed(GtkToggleButton *toggle, PSDFControls *controls)
{
    if (!gtk_toggle_button_get_active(toggle))
        return;

    controls->args->masking = gwy_radio_button_get_value(GTK_WIDGET(toggle));
    recalculate_psdf(controls);
}

static void
create_image_changed(PSDFControls *controls)
{
    PSDFArgs *args = controls->args;
    args->create_image
        = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(controls->create_image));
    update_sensitivity(controls);
}

static void
zoomed_image_changed(PSDFControls *controls)
{
    PSDFArgs *args = controls->args;
    args->zoomed_image
        = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(controls->zoomed_image));
}

static void
zoom_changed(GtkRadioButton *button, PSDFControls *controls)
{
    PSDFArgs *args = controls->args;
    ZoomType zoom = gwy_radio_buttons_get_current(controls->zoom);
    GwyDataField *psdf;
    gdouble xoff, yoff;

    if (button && zoom == args->zoom)
        return;

    psdf = gwy_container_get_object(controls->mydata,
                                    gwy_app_get_data_key_for_id(0));
    xoff = gwy_data_field_get_xoffset(psdf);
    yoff = gwy_data_field_get_yoffset(psdf);
    args->zoom = zoom;
    calculate_zoomed_fields(controls);
    gwy_set_data_preview_size(GWY_DATA_VIEW(controls->view), PREVIEW_SIZE);
    psdf = gwy_container_get_object(controls->mydata,
                                    gwy_app_get_data_key_for_id(0));
    xoff -= gwy_data_field_get_xoffset(psdf);
    yoff -= gwy_data_field_get_yoffset(psdf);
    gwy_selection_move(controls->selection, xoff, yoff);
}

static void
resolution_changed(PSDFControls *controls, GtkAdjustment *adj)
{
    controls->args->resolution = gwy_adjustment_get_int(adj);
    /* Resolution can be changed only when fixres == TRUE */
    selection_changed(controls, -1);
}

static void
thickness_changed(PSDFControls *controls, GtkAdjustment *adj)
{
    controls->args->thickness = gwy_adjustment_get_int(adj);
    selection_changed(controls, -1);
}

static void
fixres_changed(PSDFControls *controls, GtkToggleButton *check)
{
    controls->args->fixres = gtk_toggle_button_get_active(check);
    selection_changed(controls, -1);
}

static void
interpolation_changed(GtkComboBox *combo, PSDFControls *controls)
{
    controls->args->interpolation = gwy_enum_combo_box_get_active(combo);
    selection_changed(controls, -1);
}

static void
separate_changed(PSDFControls *controls)
{
    PSDFArgs *args = controls->args;
    args->separate
        = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(controls->separate));
    gwy_table_hscale_set_sensitive(GTK_OBJECT(controls->target_graph),
                                   !args->separate);
    update_target_graphs(controls);
}

static void
windowing_changed(PSDFControls *controls, GtkComboBox *combo)
{
    controls->args->windowing = gwy_enum_combo_box_get_active(combo);
    recalculate_psdf(controls);
}

static void
recalculate_psdf(PSDFControls *controls)
{
    calculate_psdf(controls->dfield, controls->mask, controls->args,
                   controls->psdf, controls->modulus);
    calculate_zoomed_fields(controls);
    selection_changed(controls, -1);
}

static void
selection_changed(PSDFControls *controls, gint hint)
{
    gint i, n;

    n = gwy_selection_get_data(controls->selection, NULL);
    if (hint < 0) {
        gwy_graph_model_remove_all_curves(controls->gmodel);
        for (i = 0; i < n; i++)
            update_curve(controls, i);
    }
    else
        update_curve(controls, hint);

    update_sensitivity(controls);
}

static void
update_sensitivity(PSDFControls *controls)
{
    PSDFArgs *args = controls->args;
    gint n;

    n = gwy_graph_model_get_n_curves(controls->gmodel);
    gtk_widget_set_sensitive(controls->zoomed_image, args->create_image);
    gtk_dialog_set_response_sensitive(GTK_DIALOG(controls->dialogue),
                                      GTK_RESPONSE_OK,
                                      args->create_image || n);
}

static void
update_target_graphs(PSDFControls *controls)
{
    GwyDataChooser *chooser = GWY_DATA_CHOOSER(controls->target_graph);
    gwy_data_chooser_refilter(chooser);
}

static gboolean
filter_target_graphs(GwyContainer *data, gint id, gpointer user_data)
{
    PSDFControls *controls = (PSDFControls*)user_data;
    GwyGraphModel *gmodel = controls->gmodel, *targetgmodel;
    GQuark quark = gwy_app_get_graph_key_for_id(id);

    if (controls->args->separate)
        return FALSE;

    g_return_val_if_fail(gmodel, FALSE);
    return (gwy_container_gis_object(data, quark, (GObject**)&targetgmodel)
            && gwy_graph_model_units_are_compatible(gmodel, targetgmodel));
}

static void
target_graph_changed(PSDFControls *controls)
{
    GwyDataChooser *chooser = GWY_DATA_CHOOSER(controls->target_graph);

    gwy_data_chooser_get_active_id(chooser, &controls->args->target_graph);
}

static void
calculate_zoomed_fields(PSDFControls *controls)
{
    ZoomType zoom = controls->args->zoom;
    GwyDataField *zoomed;

    zoomed = cut_field_to_zoom(controls->modulus, zoom);
    gwy_container_set_object(controls->mydata,
                             gwy_app_get_data_key_for_id(0), zoomed);
    gwy_data_field_data_changed(zoomed);
    g_object_unref(zoomed);
}

static GwyDataField*
cut_field_to_zoom(GwyDataField *dfield, gint zoom)
{
    GwyDataField *zoomed;
    guint xres, yres, width, height;

    xres = gwy_data_field_get_xres(dfield);
    yres = gwy_data_field_get_yres(dfield);
    width = (xres/zoom) | 1;
    height = (yres/zoom) | 1;
    if (width < 17)
        width = MAX(width, MIN(17, xres));
    if (height < 17)
        height = MAX(height, MIN(17, yres));

    if (width >= xres && height >= yres)
        return g_object_ref(dfield);

    zoomed = gwy_data_field_area_extract(dfield,
                                         xres - width - (xres - width)/2,
                                         yres - height - (yres - height)/2,
                                         width, height);
    gwy_data_field_set_xoffset(zoomed, -0.5*gwy_data_field_get_xreal(zoomed));
    gwy_data_field_set_yoffset(zoomed, -0.5*gwy_data_field_get_yreal(zoomed));

    return zoomed;
}

static void
update_curve(PSDFControls *controls, gint i)
{
    GwyGraphCurveModel *gcmodel;
    GwyDataField *modulus;
    gdouble xy[4], h, xofffull, yofffull;
    gint xl0, yl0, xl1, yl1;
    gint n, lineres;
    gchar *desc;

    if (!gwy_selection_get_object(controls->selection, i, xy)) {
        g_return_if_reached();
    }

    modulus = gwy_container_get_object(controls->mydata,
                                       gwy_app_get_data_key_for_id(0));
    xy[0] += gwy_data_field_get_xoffset(modulus);
    xy[1] += gwy_data_field_get_yoffset(modulus);

    /* The ω=0 pixel is always at res/2, for even dimensions it means it is
     * shifted half-a-pixel to the right from the precise centre. */
    xl0 = gwy_data_field_get_xres(controls->psdf)/2;
    yl0 = gwy_data_field_get_yres(controls->psdf)/2;

    xofffull = gwy_data_field_get_xoffset(controls->psdf);
    yofffull = gwy_data_field_get_yoffset(controls->psdf);
    xl1 = floor(gwy_data_field_rtoj(controls->psdf, xy[0] - xofffull));
    yl1 = floor(gwy_data_field_rtoi(controls->psdf, xy[1] - yofffull));

    /* FIXME FIXME FIXME: Is this correct? */
    h = hypot(controls->hx*xy[0], controls->hy*xy[1])/hypot(xy[0], xy[1]);

    if (!controls->args->fixres) {
        lineres = GWY_ROUND(hypot(abs(xl0 - xl1) + 1, abs(yl0 - yl1) + 1));
        lineres = MAX(lineres, MIN_RESOLUTION);
    }
    else
        lineres = controls->args->resolution;

    gwy_data_field_get_profile(controls->psdf, controls->line,
                               xl0, yl0, xl1, yl1,
                               lineres,
                               controls->args->thickness,
                               controls->args->interpolation);
    gwy_data_line_multiply(controls->line, h);

    n = gwy_graph_model_get_n_curves(controls->gmodel);
    if (i < n) {
        gcmodel = gwy_graph_model_get_curve(controls->gmodel, i);
    }
    else {
        gcmodel = gwy_graph_curve_model_new();
        g_object_set(gcmodel,
                     "mode", GWY_GRAPH_CURVE_LINE,
                     "color", gwy_graph_get_preset_color(i),
                     NULL);
        gwy_graph_model_add_curve(controls->gmodel, gcmodel);
        g_object_unref(gcmodel);
    }

    gwy_graph_curve_model_set_data_from_dataline(gcmodel, controls->line, 0, 0);
    desc = g_strdup_printf(_("PSDF %.0f°"), 180.0/G_PI*atan2(-xy[1], xy[0]));
    g_object_set(gcmodel, "description", desc, NULL);
    g_free(desc);
}

static void
init_graph_model_units(GwyGraphModel *gmodel,
                       GwyDataField *dfield)
{
    GwySIUnit *unit;

    unit = gwy_data_field_get_si_unit_xy(dfield);
    unit = gwy_si_unit_duplicate(unit);
    g_object_set(gmodel, "si-unit-x", unit, NULL);
    g_object_unref(unit);

    unit = gwy_data_field_get_si_unit_z(dfield);
    unit = gwy_si_unit_duplicate(unit);
    g_object_set(gmodel, "si-unit-y", unit, NULL);
    g_object_unref(unit);
}

static void
create_output_graphs(PSDFControls *controls, GwyContainer *data)
{
    GwyGraphCurveModel *gcmodel;
    GwyGraphModel *gmodel;
    PSDFArgs *args = controls->args;
    gchar *s;
    gint i, n;

    n = gwy_selection_get_data(controls->selection, NULL);
    if (!n)
        return;

    if (!args->separate) {
        gwy_app_add_graph_or_curves(controls->gmodel,
                                    data, &args->target_graph, 1);
        return;
    }

    for (i = 0; i < n; i++) {
        gmodel = gwy_graph_model_new_alike(controls->gmodel);
        gcmodel = gwy_graph_model_get_curve(controls->gmodel, i);
        gcmodel = gwy_graph_curve_model_duplicate(gcmodel);
        gwy_graph_model_add_curve(gmodel, gcmodel);
        g_object_unref(gcmodel);
        g_object_get(gcmodel, "description", &s, NULL);
        g_object_set(gmodel, "title", s, NULL);
        g_free(s);
        gwy_app_data_browser_add_graph_model(gmodel, data, TRUE);
        g_object_unref(gmodel);
    }
}

/* Convert points to lines from the origin, which is assumed to be in the
 * centre. */
static void
add_line_selection_from_points(GwyContainer *data,
                               GwyDataField *dfield,
                               gint id,
                               GwySelection *pointsel)
{
    GwySelection *linesel;
    gint nsel, i;
    gdouble *seldata;
    gdouble xreal, yreal;
    gchar *key;

    if (!pointsel || !(nsel = gwy_selection_get_data(pointsel, NULL)))
        return;

    linesel = g_object_new(g_type_from_name("GwySelectionLine"), NULL);
    g_return_if_fail(linesel);
    gwy_selection_set_max_objects(linesel, 1024);
    seldata = g_new(gdouble, 4*nsel);
    xreal = gwy_data_field_get_xreal(dfield);
    yreal = gwy_data_field_get_yreal(dfield);

    for (i = 0; i < nsel; i++) {
        seldata[4*i + 0] = 0.5*xreal;
        seldata[4*i + 1] = 0.5*yreal;
        gwy_selection_get_object(pointsel, i, seldata + 4*i + 2);
    }

    gwy_selection_set_data(linesel, nsel, seldata);
    g_free(seldata);

    key = g_strdup_printf("/%d/select/line", id);
    gwy_container_set_object_by_name(data, key, linesel);
    g_object_unref(linesel);
}

static gint
create_results(GwyContainer *data, GwyDataField *psdf,
               GwySelection *selection,
               gint oldid, gint zoom)
{
    GwyDataField *zoomed;
    GQuark quark;
    gint newid;

    zoomed = cut_field_to_zoom(psdf, zoom);
    newid = gwy_app_data_browser_add_data_field(zoomed, data, TRUE);

    add_line_selection_from_points(data, zoomed, newid, selection);
    gwy_app_set_data_field_title(data, newid, _("2D PSDF"));
    quark = gwy_app_get_data_palette_key_for_id(newid);
    gwy_container_set_const_string(data, quark, "DFit");
    quark = gwy_app_get_data_range_type_key_for_id(newid);
    gwy_container_set_enum(data, quark, GWY_LAYER_BASIC_RANGE_AUTO);
    gwy_app_channel_log_add_proc(data, oldid, newid);

    g_object_unref(zoomed);

    return newid;
}

static void
calculate_psdf(GwyDataField *dfield, GwyDataField *mask,
               const PSDFArgs *args,
               GwyDataField *psdf, GwyDataField *modulus)
{
    gint xres, yres, i;
    gdouble *p;

    xres = gwy_data_field_get_xres(dfield);
    yres = gwy_data_field_get_yres(dfield);
    gwy_data_field_area_2dpsdf_mask(dfield, psdf, mask, args->masking,
                                    0, 0, xres, yres, args->windowing, 1);

    /* We do not really care about modulus units nor its absolute scale.
     * We just have it to display the square root... */
    gwy_data_field_assign(modulus, psdf);
    p = gwy_data_field_get_data(modulus);
    for (i = 0; i < xres*yres; i++)
        p[i] = p[i] >= 0.0 ? sqrt(p[i]) : -sqrt(-p[i]);
}

static const gchar create_image_key[]  = "/module/psdf2d/create_image";
static const gchar fixres_key[]        = "/module/psdf2d/fixres";
static const gchar interpolation_key[] = "/module/psdf2d/interpolation";
static const gchar masking_key[]       = "/module/psdf2d/masking";
static const gchar resolution_key[]    = "/module/psdf2d/resolution";
static const gchar separate_key[]      = "/module/psdf2d/separate";
static const gchar thickness_key[]     = "/module/psdf2d/thickness";
static const gchar windowing_key[]     = "/module/psdf2d/windowing";
static const gchar zoom_key[]          = "/module/psdf2d/zoom";
static const gchar zoomed_image_key[]  = "/module/psdf2d/zoomed_image";

static void
psdf2d_sanitize_args(PSDFArgs *args)
{
    args->masking = gwy_enum_sanitize_value(args->masking,
                                            GWY_TYPE_MASKING_TYPE);
    args->create_image = !!args->create_image;
    args->zoomed_image = !!args->zoomed_image;
    args->separate = !!args->separate;
    args->fixres = !!args->fixres;
    if (args->zoom != ZOOM_1 && args->zoom != ZOOM_4 && args->zoom != ZOOM_16)
        args->zoom = psdf2d_defaults.zoom;
    args->resolution = CLAMP(args->resolution, MIN_RESOLUTION, MAX_RESOLUTION);
    args->thickness = CLAMP(args->thickness, 1, MAX_THICKNESS);
    args->interpolation = gwy_enum_sanitize_value(args->interpolation,
                                                  GWY_TYPE_INTERPOLATION_TYPE);
    args->windowing = gwy_enum_sanitize_value(args->windowing,
                                              GWY_TYPE_WINDOWING_TYPE);
    gwy_app_data_id_verify_graph(&args->target_graph);
}

static void
psdf2d_load_args(GwyContainer *container,
                 PSDFArgs *args)
{
    *args = psdf2d_defaults;

    gwy_container_gis_enum_by_name(container, masking_key, &args->masking);
    gwy_container_gis_boolean_by_name(container, create_image_key,
                                      &args->create_image);
    gwy_container_gis_boolean_by_name(container, zoomed_image_key,
                                      &args->zoomed_image);
    gwy_container_gis_boolean_by_name(container, separate_key, &args->separate);
    gwy_container_gis_boolean_by_name(container, fixres_key, &args->fixres);
    gwy_container_gis_int32_by_name(container, resolution_key,
                                    &args->resolution);
    gwy_container_gis_int32_by_name(container, thickness_key, &args->thickness);
    gwy_container_gis_enum_by_name(container, interpolation_key,
                                   &args->interpolation);
    gwy_container_gis_enum_by_name(container, windowing_key, &args->windowing);
    gwy_container_gis_enum_by_name(container, zoom_key, &args->zoom);
    args->target_graph = target_id;
    psdf2d_sanitize_args(args);
}

static void
psdf2d_save_args(GwyContainer *container,
                 PSDFArgs *args)
{
    gwy_container_set_enum_by_name(container, masking_key, args->masking);
    gwy_container_set_boolean_by_name(container, create_image_key,
                                      args->create_image);
    gwy_container_set_boolean_by_name(container, zoomed_image_key,
                                      args->zoomed_image);
    gwy_container_set_boolean_by_name(container, separate_key, args->separate);
    gwy_container_set_boolean_by_name(container, fixres_key, args->fixres);
    gwy_container_set_int32_by_name(container, resolution_key,
                                    args->resolution);
    gwy_container_set_int32_by_name(container, thickness_key, args->thickness);
    gwy_container_set_enum_by_name(container, interpolation_key,
                                   args->interpolation);
    gwy_container_set_enum_by_name(container, windowing_key, args->windowing);
    gwy_container_set_enum_by_name(container, zoom_key, args->zoom);
    target_id = args->target_graph;
}

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