File: filters-convdeconv.c

package info (click to toggle)
gwyddion 2.62-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 51,952 kB
  • sloc: ansic: 398,486; python: 7,877; sh: 5,492; makefile: 4,723; xml: 3,883; cpp: 1,969; pascal: 418; perl: 154; ruby: 130
file content (2227 lines) | stat: -rw-r--r-- 83,061 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
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
/*
 *  $Id: filters-convdeconv.c 24753 2022-03-29 13:41:56Z yeti-dn $
 *  Copyright (C) 2003-2022 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 <string.h>
#include <libgwyddion/gwymacros.h>
#include <libprocess/filters.h>
#include <libprocess/stats.h>
#include <libprocess/linestats.h>
#include <libprocess/grains.h>
#include <libprocess/inttrans.h>
#include <libprocess/simplefft.h>
#include <libprocess/arithmetic.h>
#include "libgwyddion/gwyomp.h"
#include "gwyprocessinternal.h"
#include "gwyfftw.h"

typedef struct {
    /* Image-sized. */
    fftw_complex *fideal;      /* Precalculated, for P = ... */
    fftw_complex *fmeas;       /* Precalculated, for P = ... */
    fftw_complex *fpsf;        /* P = ..., in each step */
    fftw_complex *cbuf;        /* FFT scratch buffer*/
    GwyDataField *psf;         /* Real-space PSF */
    fftw_plan pplan;
    gdouble sigma_scale;
    guint cstride;
    /* Area for PSF width measurement. */
    gint row;
    gint col;
    gint width;
    gint height;
} PSFSigmaOptData;

typedef struct {
    GwyDataField *matrix;
    GwyDataField *rhs;
    GwyDataField *tf;
    GwyDataField *buf;
    GwyDataField *ffield;
    GwyDataField *vfield;
    GwyDataField *wfield;
    fftw_plan fplan;
    fftw_plan bplan;
    fftw_complex *fmat;
    fftw_complex *fvec;
    gdouble matrix00;
    gdouble mnorm;
    gint want_txres;
    gint want_tyres;
    gint txres;
    gint tyres;
    gint xsize;
    gint ysize;
    gint border;
    gint cstride;
} PSFConjGradData;

typedef gdouble (*DoubleArrayFunc)(const gdouble *results);

static void
gwy_data_field_area_convolve_3x3(GwyDataField *data_field,
                                 const gdouble *kernel,
                                 gint col, gint row,
                                 gint width, gint height)
{
    gdouble *rm, *rc, *rp;
    gdouble t, v;
    gint xres, i, j;

    xres = data_field->xres;
    rp = data_field->data + row*xres + col;

    /* Special-case width == 1 to avoid complications below.  It's silly but the API guarantees it. */
    if (width == 1) {
        t = rp[0];
        for (i = 0; i < height; i++) {
            rc = rp = data_field->data + (row + i)*xres + col;
            if (i < height-1)
                rp += xres;

            v = (kernel[0] + kernel[1] + kernel[2])*t
                + (kernel[3] + kernel[4] + kernel[5])*rc[0]
                + (kernel[6] + kernel[7] + kernel[8])*rp[0];
            t = rc[0];
            rc[0] = v;
        }
        gwy_data_field_invalidate(data_field);

        return;
    }

    /* NB: This is not trivial to parallelise because of the one-pass method where we just keep a single-row buffer.
     * With multiple threads we have to ensure they do not write what we still want to read. */
    rm = g_new(gdouble, width);
    gwy_assign(rm, rp, width);

    for (i = 0; i < height; i++) {
        rc = rp;
        if (i < height-1)
            rp += xres;
        v = (kernel[0] + kernel[1])*rm[0] + kernel[2]*rm[1] + (kernel[3] + kernel[4])*rc[0] + kernel[5]*rc[1]
            + (kernel[6] + kernel[7])*rp[0] + kernel[8]*rp[1];
        t = rc[0];
        rc[0] = v;
        if (i < height-1) {
            for (j = 1; j < width-1; j++) {
                v = kernel[0]*rm[j-1] + kernel[1]*rm[j] + kernel[2]*rm[j+1] + kernel[3]*t + kernel[4]*rc[j]
                    + kernel[5]*rc[j+1] + kernel[6]*rp[j-1] + kernel[7]*rp[j] + kernel[8]*rp[j+1];
                rm[j-1] = t;
                t = rc[j];
                rc[j] = v;
            }
            v = kernel[0]*rm[j-1] + (kernel[1] + kernel[2])*rm[j] + kernel[3]*t + (kernel[4] + kernel[5])*rc[j]
                + kernel[6]*rp[j-1] + (kernel[7] + kernel[8])*rp[j];
        }
        else {
            for (j = 1; j < width-1; j++) {
                v = kernel[0]*rm[j-1] + kernel[1]*rm[j] + kernel[2]*rm[j+1] + kernel[3]*t + kernel[4]*rc[j]
                    + kernel[5]*rc[j+1] + kernel[6]*t + kernel[7]*rc[j] + kernel[8]*rc[j+1];
                rm[j-1] = t;
                t = rc[j];
                rc[j] = v;
            }
            v = kernel[0]*rm[j-1] + (kernel[1] + kernel[2])*rm[j] + kernel[3]*t + (kernel[4] + kernel[5])*rc[j]
                + kernel[6]*t + (kernel[7] + kernel[8])*rc[j];
        }
        rm[j-1] = t;
        rm[j] = rc[j];
        rc[j] = v;
    }

    g_free(rm);
    gwy_data_field_invalidate(data_field);
}

/**
 * gwy_data_field_area_convolve:
 * @data_field: A data field to convolve.  It must be at least as large as 1/3 of @kernel_field in each dimension.
 * @kernel_field: Kenrel field to convolve @data_field with.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Convolves a rectangular part of a data field with given kernel.
 *
 * Note that the convolution is done by summation and can be slow for large kernels.
 **/
void
gwy_data_field_area_convolve(GwyDataField *data_field,
                             GwyDataField *kernel_field,
                             gint col, gint row,
                             gint width, gint height)
{
    gint xres, yres, kxres, kyres, i, j;
    GwyDataField *hlp_df;
    gdouble *d, *h, *k;

    if (!_gwy_data_field_check_area(data_field, col, row, width, height))
        return;
    g_return_if_fail(GWY_IS_DATA_FIELD(kernel_field));

    xres = data_field->xres;
    yres = data_field->yres;
    kxres = kernel_field->xres;
    kyres = kernel_field->yres;

    if (kxres == 3 && kyres == 3) {
        gwy_data_field_area_convolve_3x3(data_field, kernel_field->data, col, row, width, height);
        return;
    }

    hlp_df = gwy_data_field_new(width, height, 1.0, 1.0, FALSE);
    d = data_field->data;
    k = kernel_field->data;
    h = hlp_df->data;
#ifdef _OPENMP
#pragma omp parallel for if(gwy_threads_are_enabled()) default(none) \
            private(i,j) \
            shared(d,k,h,xres,yres,kxres,kyres,col,row,width,height)
#endif
    for (i = row; i < row + height; i++) {
        for (j = col; j < col + width; j++) {
            gdouble v = 0.0;
            gint m, n, ii, jj;

            for (m = -kyres/2; m < kyres - kyres/2; m++) {
                ii = i + m;
                if (G_UNLIKELY(ii < 0))
                    ii = -ii-1;
                else if (G_UNLIKELY(ii >= yres))
                    ii = 2*yres-1 - ii;

                for (n = -kxres/2; n < kxres - kxres/2; n++) {
                    jj = j + n;
                    if (G_UNLIKELY(jj < 0))
                        jj = -jj-1;
                    else if (G_UNLIKELY(jj >= xres))
                        jj = 2*xres-1 - jj;

                    v += d[ii*xres + jj] * k[kxres*(m + kyres/2) + n + kxres/2];
                }
            }
            h[(i - row)*width + (j - col)] = v;
        }
    }
    gwy_data_field_area_copy(hlp_df, data_field, 0, 0, width, height, col, row);
    g_object_unref(hlp_df);

    gwy_data_field_invalidate(data_field);
}

/**
 * gwy_data_field_convolve:
 * @data_field: A data field to convolve.  It must be at least as large as 1/3 of @kernel_field in each dimension.
 * @kernel_field: Kenrel field to convolve @data_field with.
 *
 * Convolves a data field with given kernel.
 *
 * Note that the convolution is done by summation and can be slow for large kernels.
 **/
void
gwy_data_field_convolve(GwyDataField *data_field,
                        GwyDataField *kernel_field)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_convolve(data_field, kernel_field, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_fft_convolve:
 * @data_field: A data field to convolve.
 * @kernel_field: Kenrel field to convolve @data_field with.  It must have the same size as @data_field.
 *
 * Convolves a data field with given kernel of the same size using FFT.
 *
 * This is a simple FFT-based convolution done by multiplication in the frequency domain.
 *
 * This is a somewhat low-level function.  There is no padding or boundary treatment; images are considered periodic.
 * The result is normalised as if the convolution was done by summation and the physical units of @data_field are
 * unchanged.
 *
 * Also note that in order to obtain unshifted result, the kernel needs to be centered around the top left corner.
 * You can use gwy_data_field_2dfft_dehumanize() to transform a centered kernel.
 *
 * Since: 2.54
 **/
void
gwy_data_field_fft_convolve(GwyDataField *data_field,
                            GwyDataField *kernel_field)
{
    fftw_plan fplan, bplan;
    fftw_complex *fieldc, *kernelc;
    gdouble *rbuf;
    guint i, xres, yres, n, cstride;

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    g_return_if_fail(GWY_IS_DATA_FIELD(kernel_field));
    xres = data_field->xres;
    yres = data_field->yres;
    g_return_if_fail(kernel_field->xres == xres);
    g_return_if_fail(kernel_field->yres == yres);

    cstride = xres/2 + 1;
    n = xres*yres;
    rbuf = gwy_fftw_new_real(n);
    fieldc = gwy_fftw_new_complex(cstride*yres);
    kernelc = gwy_fftw_new_complex(cstride*yres);

    fplan = gwy_fftw_plan_dft_r2c_2d(yres, xres, rbuf, fieldc, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    bplan = gwy_fftw_plan_dft_c2r_2d(yres, xres, fieldc, rbuf, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);

    gwy_assign(rbuf, kernel_field->data, xres*yres);
    gwy_fftw_execute(fplan);
    gwy_assign(kernelc, fieldc, cstride*yres);

    gwy_assign(rbuf, data_field->data, xres*yres);
    gwy_fftw_execute(fplan);

    for (i = 0; i < cstride*yres; i++) {
        gdouble re = (gwycreal(fieldc[i])*gwycreal(kernelc[i]) - gwycimag(fieldc[i])*gwycimag(kernelc[i]));
        gdouble im = (gwycreal(fieldc[i])*gwycimag(kernelc[i]) + gwycimag(fieldc[i])*gwycreal(kernelc[i]));
        gwycreal(fieldc[i]) = re;
        gwycimag(fieldc[i]) = im;
    }
    gwy_fftw_execute(bplan);

    gwy_assign(data_field->data, rbuf, n);
    gwy_data_field_invalidate(data_field);
    gwy_data_field_multiply(data_field, 1.0/n);

    fftw_destroy_plan(fplan);
    fftw_destroy_plan(bplan);
    fftw_free(fieldc);
    fftw_free(kernelc);
    fftw_free(rbuf);
}

static void
gwy_data_field_area_hconvolve(GwyDataField *data_field,
                              GwyDataLine *kernel_line,
                              gint col, gint row,
                              gint width, gint height)
{
    gint xres, kres, mres, k0;
    const gdouble *kernel;
    gdouble *data;

    xres = data_field->xres;
    kres = kernel_line->res;
    data = data_field->data;
    kernel = kernel_line->data;
    mres = 2*width;
    k0 = (kres/2 + 1)*mres;

#ifdef _OPENMP
#pragma omp parallel if(gwy_threads_are_enabled()) default(none) \
            shared(data,kernel,xres,kres,mres,k0,col,row,width,height)
#endif
    {
        gdouble *drow, *buf = g_new(gdouble, kres);
        gint ifrom = gwy_omp_chunk_start(height);
        gint ito = gwy_omp_chunk_end(height);
        gint i, j, k, pos;
        gdouble d;

        for (i = ifrom; i < ito; i++) {
            drow = data + (row + i)*xres + col;
            /* Initialize with a triangluar sums, mirror-extend */
            gwy_clear(buf, kres);
            for (j = 0; j < kres; j++) {
                k = (j - kres/2 + k0) % mres;
                d = drow[k < width ? k : mres-1 - k];
                for (k = 0; k <= j; k++)
                    buf[k] += kernel[j - k]*d;
            }
            pos = 0;
            /* Middle part and tail with mirror extension again, we do some O(1/2*k^2) of useless work here by not
             * separating the tail */
            for (j = 0; j < width; j++) {
                drow[j] = buf[pos];
                buf[pos] = 0.0;
                pos = (pos + 1) % kres;
                k = (j + kres - kres/2 + k0) % mres;
                d = drow[G_LIKELY(k < width) ? k : mres-1 - k];
                for (k = pos; k < kres; k++)
                    buf[k] += kernel[kres-1 - (k - pos)]*d;
                for (k = 0; k < pos; k++)
                    buf[k] += kernel[pos-1 - k]*d;
            }
        }

        g_free(buf);
    }
}

static void
gwy_data_field_area_vconvolve(GwyDataField *data_field,
                              GwyDataLine *kernel_line,
                              gint col, gint row,
                              gint width, gint height)
{
    gint kres, xres, mres, k0;
    const gdouble *kernel;
    gdouble *data;

    xres = data_field->xres;
    kres = kernel_line->res;
    data = data_field->data;
    kernel = kernel_line->data;
    mres = 2*height;
    k0 = (kres/2 + 1)*mres;

    /* This looks like a bad memory access pattern.  And for small kernels it indeed is (we should iterate row-wise
     * and directly calculate the sums). For large kernels this is mitigated by the maximum possible amount of work
     * done per a data field access. */
#ifdef _OPENMP
#pragma omp parallel if(gwy_threads_are_enabled()) default(none) \
            shared(data,kernel,xres,kres,mres,k0,col,row,width,height)
#endif
    {
        gdouble *dcol, *buf = g_new(gdouble, kres);
        gint jfrom = gwy_omp_chunk_start(width);
        gint jto = gwy_omp_chunk_end(width);
        gint i, j, k, pos;
        gdouble d;

        for (j = jfrom; j < jto; j++) {
            dcol = data + row*xres + (col + j);
            /* Initialize with a triangluar sums, mirror-extend */
            gwy_clear(buf, kres);
            for (i = 0; i < kres; i++) {
                k = (i - kres/2 + k0) % mres;
                d = dcol[k < height ? k*xres : (mres-1 - k)*xres];
                for (k = 0; k <= i; k++)
                    buf[k] += kernel[i - k]*d;
            }
            pos = 0;
            /* Middle part and tail with mirror extension again, we do some O(1/2*k^2) of useless work here by not
             * separating the tail */
            for (i = 0; i < height; i++) {
                dcol[i*xres] = buf[pos];
                buf[pos] = 0.0;
                pos = (pos + 1) % kres;
                k = (i + kres - kres/2 + k0) % mres;
                d = dcol[G_LIKELY(k < height) ? k*xres : (mres-1 - k)*xres];
                for (k = pos; k < kres; k++)
                    buf[k] += kernel[kres-1 - (k - pos)]*d;
                for (k = 0; k < pos; k++)
                    buf[k] += kernel[pos-1 - k]*d;
            }
        }

        g_free(buf);
    }
}

/**
 * gwy_data_field_area_convolve_1d:
 * @data_field: A data field to convolve.  It must be at least as large as 1/3 of @kernel_field in the corresponding
 *              dimension.
 * @kernel_line: Kernel line to convolve @data_field with.
 * @orientation: Filter orientation (%GWY_ORIENTATION_HORIZONTAL for row-wise convolution, %GWY_ORIENTATION_VERTICAL
 *               for column-wise convolution).
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Convolves a rectangular part of a data field with given linear kernel.
 *
 * For large separable kernels it can be more efficient to use a sequence of horizontal and vertical convolutions
 * instead one 2D convolution.
 *
 * Since: 2.4
 **/
void
gwy_data_field_area_convolve_1d(GwyDataField *data_field,
                                GwyDataLine *kernel_line,
                                GwyOrientation orientation,
                                gint col, gint row,
                                gint width, gint height)
{
    gint kres;

    if (!_gwy_data_field_check_area(data_field, col, row, width, height))
        return;
    g_return_if_fail(GWY_IS_DATA_LINE(kernel_line));

    kres = kernel_line->res;
    if (kres == 1) {
        gwy_data_field_area_multiply(data_field, col, row, width, height, kernel_line->data[0]);
        return;
    }

    if (orientation == GWY_ORIENTATION_HORIZONTAL)
        gwy_data_field_area_hconvolve(data_field, kernel_line, col, row, width, height);
    else
        gwy_data_field_area_vconvolve(data_field, kernel_line, col, row, width, height);

    gwy_data_field_invalidate(data_field);
}

/**
 * gwy_data_field_convolve_1d:
 * @data_field: A data field to convolve.  It must be at least as large as 1/3 of @kernel_field in the corresponding
 *              dimension.
 * @kernel_line: Kenrel line to convolve @data_field with.
 * @orientation: Filter orientation (see gwy_data_field_area_convolve_1d()).
 *
 * Convolves a data field with given linear kernel.
 *
 * Since: 2.4
 **/
void
gwy_data_field_convolve_1d(GwyDataField *data_field,
                           GwyDataLine *kernel_line,
                           GwyOrientation orientation)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_convolve_1d(data_field, kernel_line, orientation, 0, 0, data_field->xres, data_field->yres);
}

static void
ensure_defined_exterior(GwyExteriorType *exterior, gdouble *fill_value)
{
    if (*exterior == GWY_EXTERIOR_UNDEFINED) {
        g_warning("Do not use GWY_EXTERIOR_UNDEFINED for convolutions and correlations.  "
                  "Fixing to zero-filled exterior.");
        *exterior = GWY_EXTERIOR_FIXED_VALUE;
        *fill_value = 0.0;
    }
}

static void
row_convolve_direct(const GwyDataField *field,
                    guint col, guint row,
                    guint width, guint height,
                    GwyDataField *target,
                    guint targetcol, guint targetrow,
                    const GwyDataLine *kernel,
                    RowExtendFunc extend_row,
                    gdouble fill_value)
{
    guint xres = field->xres;
    guint kres = kernel->res;
    const gdouble *kdata = kernel->data;
    guint size = width + kres - 1;
    guint extend_left, extend_right, i, j, k;
    gdouble *extdata = g_new(gdouble, size);

    make_symmetrical_extension(width, size, &extend_left, &extend_right);

    /* The direct method is used only if kres ≪ res.  Don't bother optimising the boundaries, just make the inner loop
     * tight. */
    for (i = 0; i < height; i++) {
        gdouble *trow = target->data + (targetrow + i)*target->xres + targetcol;
        extend_row(field->data + (row + i)*xres, extdata, col, width, xres, extend_left, extend_right, fill_value);
        for (j = 0; j < width; j++) {
            const gdouble *d = extdata + extend_left + kres/2 + j;
            gdouble v = 0.0;
            for (k = 0; k < kres; k++, d--)
                v += kdata[k] * *d;
            trow[j] = v;
        }
    }

    g_free(extdata);
}

static inline void
complex_multiply_with(fftw_complex *a, const fftw_complex *b)
{
    gdouble re = (*a)[0]*(*b)[0] - (*a)[1]*(*b)[1];

    (*a)[1] = (*a)[1]*(*b)[0] + (*a)[0]*(*b)[1];
    (*a)[0] = re;
}

static void
row_convolve_fft(GwyDataField *field,
                 guint col, guint row,
                 guint width, guint height,
                 GwyDataField *target,
                 guint targetcol, guint targetrow,
                 GwyDataLine *kernel,
                 RowExtendFunc extend_row,
                 gdouble fill_value)
{
    guint xres = field->xres, kres = kernel->res;
    guint size = gwy_fft_find_nice_size(width + kres - 1);
    // The innermost (contiguous) dimension of R2C the complex output is slightly larger than the real input.  Note
    // @cstride is measured in fftw_complex, multiply it by 2 for doubles.
    guint cstride = size/2 + 1;
    guint extend_left, extend_right, i, j;
    gdouble *extdata = gwy_fftw_new_real(size);
    fftw_complex *datac = gwy_fftw_new_complex(cstride);
    fftw_complex *kernelc = gwy_fftw_new_complex(cstride);
    fftw_plan dplan, cplan;
    gdouble q;

    /* The R2C plan for transforming the extended data row (or kernel). */
    dplan = gwy_fftw_plan_dft_r2c_1d(size, extdata, datac, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    /* The C2R plan the backward transform of the convolution of each row. */
    cplan = gwy_fftw_plan_dft_c2r_1d(size, datac, extdata, FFTW_ESTIMATE);

    // Transform the kernel.
    extend_kernel_row(kernel->data, kres, extdata, size);
    gwy_fftw_execute(dplan);
    gwy_assign(kernelc, datac, cstride);

    // Convolve rows
    make_symmetrical_extension(width, size, &extend_left, &extend_right);
    q = 1.0/size;
    for (i = 0; i < height; i++) {
        extend_row(field->data + (row + i)*xres, extdata, col, width, xres, extend_left, extend_right, fill_value);
        gwy_fftw_execute(dplan);
        for (j = 0; j < cstride; j++) {
            complex_multiply_with(datac + j, kernelc + j);
            datac[j][0] *= q;
            datac[j][1] *= q;
        }
        gwy_fftw_execute(cplan);
        gwy_assign(target->data + (targetrow + i)*target->xres + targetcol, extdata + extend_left, width);
    }

    fftw_destroy_plan(cplan);
    fftw_destroy_plan(dplan);
    fftw_free(extdata);
    fftw_free(datac);
    fftw_free(extdata);
}

/**
 * gwy_data_field_area_ext_row_convolve:
 * @field: A two-dimensional data field.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 * @target: A two-dimensional data field where the result will be placed. It may be @field for an in-place
 *          modification.
 * @kernel: Kernel to convolve @field with.
 * @exterior: Exterior pixels handling.
 * @fill_value: The value to use with %GWY_EXTERIOR_FIXED_VALUE exterior.
 * @as_integral: %TRUE for normalisation and units as a convolution integral, %FALSE as a sum.
 *
 * Convolve a field row-wise with a one-dimensional kernel.
 *
 * Pixel dimensions of @target may match either @field or just the rectangular area.  In the former case the result is
 * written in the same rectangular area; in the latter case the result fills the entire @target.
 *
 * The convolution is performed with the kernel centred on the respective field pixels.  For an odd-sized kernel this
 * holds precisely.  For an even-sized kernel this means the kernel centre is placed 0.5 pixel to the left (towards
 * lower column indices) from the respective field pixel.
 *
 * See gwy_data_field_extend() for what constitutes the exterior and how it is handled.
 *
 * If @as_integral is %FALSE the function performs a simple discrete convolution sum and the value units of @target
 * are set to product of @field and @kernel units.
 *
 * If @as_integral is %TRUE the function approximates a convolution integral. In this case @kernel should be a sampled
 * continuous transfer function. The units of value @target are set to product of @field and @kernel value units and
 * @field lateral units.  Furthermore, the discrete sum is multiplied by the pixel size (i.e. d@x in the integral).
 *
 * In either case, the lateral units and pixel size of @kernel are assumed to be the same as for a @field's row
 * (albeit not checked), because the convolution does not make sense otherwise.
 *
 * Since: 2.49
 **/
void
gwy_data_field_area_ext_row_convolve(GwyDataField *field,
                                     guint col, guint row,
                                     guint width, guint height,
                                     GwyDataField *target,
                                     GwyDataLine *kernel,
                                     GwyExteriorType exterior,
                                     gdouble fill_value,
                                     gboolean as_integral)
{
    guint xres, yres;
    guint targetcol, targetrow;
    GwySIUnit *funit, *kunit, *tunit;
    RowExtendFunc extend_row;
    gdouble dx, dy;

    if (!_gwy_data_field_check_area(field, col, row, width, height))
        return;
    g_return_if_fail(GWY_IS_DATA_FIELD(target));
    g_return_if_fail(GWY_IS_DATA_LINE(kernel));
    xres = field->xres;
    yres = field->yres;
    g_return_if_fail((target->xres == xres && target->yres == yres)
                     || (target->xres == width && target->yres == height));
    targetcol = (target->xres == xres) ? col : 0;
    targetrow = (target->yres == yres) ? row : 0;

    ensure_defined_exterior(&exterior, &fill_value);
    if (!(extend_row = _gwy_get_row_extend_func(exterior)))
        return;

    if (width <= 12 || kernel->res <= 3.0*(log(width) - 1.0)) {
        row_convolve_direct(field, col, row, width, height,
                            target, targetcol, targetrow,
                            kernel, extend_row, fill_value);
    }
    else {
        row_convolve_fft(field, col, row, width, height,
                         target, targetcol, targetrow,
                         kernel, extend_row, fill_value);
    }

    dx = field->xreal/field->xres;
    dy = field->yreal/field->yres;
    if (target != field) {
        _gwy_copy_si_unit(field->si_unit_xy, &target->si_unit_xy);
        target->xreal = dx*target->xres;
        target->yreal = dy*target->yres;
    }

    funit = gwy_data_field_get_si_unit_z(field);
    kunit = gwy_data_line_get_si_unit_y(kernel);
    tunit = gwy_data_field_get_si_unit_z(target);
    gwy_si_unit_multiply(funit, kunit, tunit);
    if (as_integral) {
        funit = gwy_data_field_get_si_unit_xy(field);
        gwy_si_unit_multiply(tunit, funit, tunit);
        gwy_data_field_multiply(target, dx);
    }

    gwy_data_field_invalidate(target);
}

/**
 * multiconvolve_direct:
 * @field: A two-dimensional data field.
 * @col: First ROI column.
 * @row: First RIO row.
 * @width: ROI width.
 * @height: RIO height.
 * @target: A two-dimensional data field where the result will be placed. It may be @field itself.
 * @targetcol: Column to place the result into @target.
 * @targetrow: Target to place the result into @target.
 * @kernel: Array of @nkernel equally-sized kernel.
 * @nkernel: Number of items in @kernel.
 * @combine_results: Function to combine results of individual convolutions to the final result put to @target.  May
 *                   be %NULL if @nkernel is 1.
 * @extend_rect: Rectangle extending method.
 * @fill_value: The value to use with fixed-value exterior.
 *
 * Performs convolution of a field with a number of equally-sized kenrels, combining the results of individual
 * convolutions into a single value.
 */
static void
multiconvolve_direct(GwyDataField *field,
                     guint col, guint row,
                     guint width, guint height,
                     GwyDataField *target,
                     guint targetcol, guint targetrow,
                     GwyDataField **kernel,
                     guint nkernel,
                     DoubleArrayFunc combine_results,
                     RectExtendFunc extend_rect,
                     gdouble fill_value)
{
    guint xres, yres, kxres, kyres, xsize, ysize;
    guint extend_left, extend_right, extend_up, extend_down;
    gdouble *extdata;
    guint kno, i, j, ik, jk;

    g_return_if_fail(nkernel);
    g_return_if_fail(kernel);
    g_return_if_fail(nkernel == 1 || combine_results);

    xres = field->xres;
    yres = field->yres;
    kxres = kernel[0]->xres;
    kyres = kernel[0]->yres;
    for (kno = 1; kno < nkernel; kno++) {
        g_return_if_fail(kernel[kno]->xres == kxres && kernel[kno]->yres == kyres);
    }

    xsize = width + kxres - 1;
    ysize = height + kyres - 1;
    extdata = g_new(gdouble, xsize*ysize);
    make_symmetrical_extension(width, xsize, &extend_left, &extend_right);
    make_symmetrical_extension(height, ysize, &extend_up, &extend_down);

    extend_rect(field->data, xres, extdata, xsize,
                col, row, width, height, xres, yres,
                extend_left, extend_right, extend_up, extend_down, fill_value);

    /* The direct method is used only if kres ≪ res.  Don't bother optimising
     * the boundaries, just make the inner loop tight. */
    if (nkernel == 1) {
        const gdouble *kdata = kernel[0]->data;
        for (i = 0; i < height; i++) {
            gdouble *trow = target->data + ((targetrow + i)*target->xres + targetcol);
            for (j = 0; j < width; j++) {
                const gdouble *id = extdata + (extend_up + kyres/2 + i)*xsize;
                gdouble v = 0.0;
                for (ik = 0; ik < kyres; ik++, id -= xsize) {
                    const gdouble *jd = id + extend_left + kxres/2 + j;
                    const gdouble *krow = kdata + ik*kxres;
                    for (jk = 0; jk < kxres; jk++, jd--)
                        v += krow[jk] * *jd;
                }
                trow[j] = v;
            }
        }
    }
    else {
        for (i = 0; i < height; i++) {
            gdouble *trow = target->data + ((targetrow + i)*target->xres + targetcol);
            for (j = 0; j < width; j++) {
                gdouble results[nkernel];
                for (kno = 0; kno < nkernel; kno++) {
                    const gdouble *id = extdata + (extend_up + kyres/2 + i)*xsize;
                    const gdouble *kdata = kernel[kno]->data;
                    gdouble v = 0.0;
                    for (ik = 0; ik < kyres; ik++, id -= xsize) {
                        const gdouble *jd = id + extend_left + kxres/2 + j;
                        const gdouble *krow = kdata + ik*kxres;
                        for (jk = 0; jk < kxres; jk++, jd--)
                            v += krow[jk] * *jd;
                    }
                    results[kno] = v;
                }
                trow[j] = combine_results(results);
            }
        }
    }

    g_free(extdata);
}

static void
convolve_fft(GwyDataField *field,
             guint col, guint row,
             guint width, guint height,
             GwyDataField *target,
             guint targetcol, guint targetrow,
             GwyDataField *kernel,
             RectExtendFunc extend_rect,
             gdouble fill_value)
{
    guint xres = field->xres, yres = field->yres,
          kxres = kernel->xres, kyres = kernel->yres;
    guint xsize = gwy_fft_find_nice_size(width + kxres - 1);
    guint ysize = gwy_fft_find_nice_size(height + kyres - 1);
    /* The innermost (contiguous) dimension of R2C the complex output is slightly larger than the real input.  If the
     * transform is in-place the input array needs to be padded.  Note @cstride is measured in fftw_complex, multiply
     * it by 2 for doubles. */
    guint cstride = xsize/2 + 1;
    /* Use in-place transforms.  Let FFTW figure out whether allocating temporary buffers is worth it or not. */
    fftw_complex *datac = gwy_fftw_new_complex(cstride*ysize);
    gdouble *extdata = (gdouble*)datac;
    fftw_complex *kernelc = gwy_fftw_new_complex(cstride*ysize);
    guint extend_left, extend_right, extend_up, extend_down;
    guint i, k;
    fftw_plan kplan, dplan, cplan;
    gdouble q;

    make_symmetrical_extension(width, xsize, &extend_left, &extend_right);
    make_symmetrical_extension(height, ysize, &extend_up, &extend_down);

    /* The R2C plan for transforming the extended kernel.  The input is in extdata to make it an out-of-place
     * transform (this also means the input data row stride is just xsize, not 2*cstride). */
    kplan = gwy_fftw_plan_dft_r2c_2d(ysize, xsize, extdata, kernelc, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    // The R2C plan for transforming the extended data.  This one is in-place.
    dplan = gwy_fftw_plan_dft_r2c_2d(ysize, xsize, extdata, datac, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    // The C2R plan the backward transform of the convolution.  The input is in fact in kernelc to make it an
    // out-of-place transform.  So, again, the output has cstride of only xsize.
    cplan = gwy_fftw_plan_dft_c2r_2d(ysize, xsize, kernelc, extdata, FFTW_ESTIMATE);

    // Transform the kernel.
    extend_kernel_rect(kernel->data, kxres, kyres, extdata, xsize, ysize, xsize);
    gwy_fftw_execute(kplan);

    // Convolve
    extend_rect(field->data, xres, extdata, 2*cstride,
                col, row, width, height, xres, yres,
                extend_left, extend_right, extend_up, extend_down, fill_value);
    gwy_fftw_execute(dplan);

    q = 1.0/(xsize*ysize);
    for (k = 0; k < cstride*ysize; k++) {
        complex_multiply_with(kernelc + k, datac + k);
        kernelc[k][0] *= q;
        kernelc[k][1] *= q;
    }
    gwy_fftw_execute(cplan);

    for (i = 0; i < height; i++) {
        gwy_assign(target->data + (targetrow + i)*target->xres + targetcol,
                   extdata + (extend_up + i)*xsize + extend_left,
                   width);
    }

    fftw_destroy_plan(kplan);
    fftw_destroy_plan(cplan);
    fftw_destroy_plan(dplan);
    fftw_free(kernelc);
    fftw_free(datac);
}

/**
 * gwy_data_field_area_ext_convolve:
 * @field: A two-dimensional data field.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 * @target: A two-dimensional data field where the result will be placed. It may be @field for an in-place
 *          modification.
 * @kernel: Kernel to convolve @field with.
 * @exterior: Exterior pixels handling.
 * @fill_value: The value to use with %GWY_EXTERIOR_FIXED_VALUE exterior.
 * @as_integral: %TRUE for normalisation and units as a convolution integral, %FALSE as a sum.
 *
 * Convolve a field with a two-dimensional kernel.
 *
 * Pixel dimensions of @target may match either @field or just the rectangular area.  In the former case the result is
 * written in the same rectangular area; in the latter case the result fills the entire @target.
 *
 * The convolution is performed with the kernel centred on the respective field pixels.  For directions in which the
 * kernel has an odd size this holds precisely.  For an even-sized kernel this means the kernel centre is placed 0.5
 * pixel left or up (towards lower indices) from the respective field pixel.
 *
 * See gwy_data_field_extend() for what constitutes the exterior and how it is handled.
 *
 * If @as_integral is %FALSE the function performs a simple discrete convolution sum and the value units of @target
 * are set to product of @field and @kernel units.
 *
 * If @as_integral is %TRUE the function approximates a convolution integral. In this case @kernel should be a sampled
 * continuous transfer function. The units of value @target are set to product of @field and @kernel value units and
 * @field lateral units squared.  Furthermore, the discrete sum is multiplied by the pixel size (i.e. d@x d@y in the
 * integral).
 *
 * In either case, the lateral units and pixel size of @kernel are assumed to be the same as for @field (albeit not
 * checked), because the convolution does not make sense otherwise.
 *
 * Since: 2.49
 **/
void
gwy_data_field_area_ext_convolve(GwyDataField *field,
                                 guint col, guint row,
                                 guint width, guint height,
                                 GwyDataField *target,
                                 GwyDataField *kernel,
                                 GwyExteriorType exterior,
                                 gdouble fill_value,
                                 gboolean as_integral)
{
    guint xres, yres, size;
    guint targetcol, targetrow;
    GwySIUnit *funit, *kunit, *tunit;
    RectExtendFunc extend_rect;
    gdouble dx, dy;

    if (!_gwy_data_field_check_area(field, col, row, width, height))
        return;
    g_return_if_fail(GWY_IS_DATA_FIELD(target));
    g_return_if_fail(GWY_IS_DATA_FIELD(kernel));
    xres = field->xres;
    yres = field->yres;
    g_return_if_fail((target->xres == xres && target->yres == yres)
                     || (target->xres == width && target->yres == height));
    targetcol = (target->xres == xres) ? col : 0;
    targetrow = (target->yres == yres) ? row : 0;

    ensure_defined_exterior(&exterior, &fill_value);
    if (!(extend_rect =_gwy_get_rect_extend_func(exterior)))
        return;

    size = height*width;
    if (size <= 25) {
        multiconvolve_direct(field, col, row, width, height,
                             target, targetcol, targetrow,
                             &kernel, 1, NULL, extend_rect, fill_value);
    }
    else
        convolve_fft(field, col, row, width, height, target, targetcol, targetrow, kernel, extend_rect, fill_value);

    dx = field->xreal/field->xres;
    dy = field->yreal/field->yres;
    if (target != field) {
        funit = gwy_data_field_get_si_unit_xy(field);
        tunit = gwy_data_field_get_si_unit_xy(target);
        gwy_si_unit_assign(tunit, funit);
        target->xreal = dx*target->xres;
        target->yreal = dy*target->yres;
    }

    funit = gwy_data_field_get_si_unit_z(field);
    kunit = gwy_data_field_get_si_unit_z(kernel);
    tunit = gwy_data_field_get_si_unit_z(target);
    gwy_si_unit_multiply(funit, kunit, tunit);
    if (as_integral) {
        funit = gwy_data_field_get_si_unit_xy(field);
        gwy_si_unit_power_multiply(tunit, 1, funit, 2, tunit);
        gwy_data_field_multiply(target, dx*dy);
    }

    gwy_data_field_invalidate(target);
}

/**
 * gwy_data_field_area_filter_laplacian:
 * @data_field: A data field to apply the filter to.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with Laplacian filter.
 **/
void
gwy_data_field_area_filter_laplacian(GwyDataField *data_field,
                                     gint col, gint row,
                                     gint width, gint height)
{
    const gdouble laplace[] = {
        0,  1, 0,
        1, -4, 1,
        0,  1, 0,
    };

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_convolve_3x3(data_field, laplace, col, row, width, height);
}

/**
 * gwy_data_field_filter_laplacian:
 * @data_field: A data field to apply the filter to.
 *
 * Filters a data field with Laplacian filter.
 **/
void
gwy_data_field_filter_laplacian(GwyDataField *data_field)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_laplacian(data_field, 0, 0, data_field->xres, data_field->yres);
}

 /**
 * gwy_data_field_area_filter_laplacian_of_gaussians:
 * @data_field: A data field to apply the filter to.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with Laplacian of Gaussians filter.
 *
 * Since: 2.23
 **/
void
gwy_data_field_area_filter_laplacian_of_gaussians(GwyDataField *data_field,
                                                  gint col, gint row,
                                                  gint width,
                                                  gint height)
{
    /* optimized mexican hat from Scharr's works */
    enum { size = 5 };
    const gdouble laplacian_of_gaussians_data[size*size] = {
          1, -12,    3, -12,   1,
        -12,  78,  167,  78, -12,
          3, 167, -902, 167,   3,
        -12,  78,  167,  78, -12,
          1, -12,    3, -12,   1,
    };
    GwyDataField *laplacian_of_gaussians;

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    laplacian_of_gaussians = gwy_data_field_new(size, size, 1.0, 1.0, TRUE);
    gwy_assign(laplacian_of_gaussians->data, laplacian_of_gaussians_data, size*size);
    gwy_data_field_area_convolve(data_field, laplacian_of_gaussians, col, row, width, height);
    g_object_unref(laplacian_of_gaussians);
}

/**
 * gwy_data_field_filter_laplacian_of_gaussians:
 * @data_field: A data field to apply the filter to.
 *
 * Filters a data field with Laplacian of Gaussians filter.
 *
 * Since: 2.23
 **/
void
gwy_data_field_filter_laplacian_of_gaussians(GwyDataField *data_field)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_laplacian_of_gaussians(data_field, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_area_filter_sobel:
 * @data_field: A data field to apply the filter to.
 * @orientation: Filter orientation.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with a directional Sobel filter.
 **/
void
gwy_data_field_area_filter_sobel(GwyDataField *data_field,
                                 GwyOrientation orientation,
                                 gint col, gint row,
                                 gint width, gint height)
{
    static const gdouble hsobel[] = {
        0.25, 0, -0.25,
        0.5,  0, -0.5,
        0.25, 0, -0.25,
    };
    static const gdouble vsobel[] = {
         0.25,  0.5,  0.25,
         0,     0,    0,
        -0.25, -0.5, -0.25,
    };

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    if (orientation == GWY_ORIENTATION_HORIZONTAL)
        gwy_data_field_area_convolve_3x3(data_field, hsobel, col, row, width, height);
    else
        gwy_data_field_area_convolve_3x3(data_field, vsobel, col, row, width, height);
}

/**
 * gwy_data_field_filter_sobel:
 * @data_field: A data field to apply the filter to.
 * @orientation: Filter orientation.
 *
 * Filters a data field with a directional Sobel filter.
 **/
void
gwy_data_field_filter_sobel(GwyDataField *data_field,
                            GwyOrientation orientation)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_sobel(data_field, orientation, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_filter_sobel_total:
 * @data_field: A data field to apply the filter to.
 *
 * Filters a data field with total Sobel filter.
 *
 * Since: 2.31
 **/
void
gwy_data_field_filter_sobel_total(GwyDataField *data_field)
{
    GwyDataField *workspace;
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    workspace = gwy_data_field_duplicate(data_field);
    gwy_data_field_area_filter_sobel(data_field, GWY_ORIENTATION_HORIZONTAL, 0, 0, data_field->xres, data_field->yres);
    gwy_data_field_area_filter_sobel(workspace, GWY_ORIENTATION_VERTICAL, 0, 0, data_field->xres, data_field->yres);
    gwy_data_field_hypot_of_fields(data_field, data_field, workspace);
    g_object_unref(workspace);
}

/**
 * gwy_data_field_area_filter_prewitt:
 * @data_field: A data field to apply the filter to.
 * @orientation: Filter orientation.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with a directional Prewitt filter.
 **/
void
gwy_data_field_area_filter_prewitt(GwyDataField *data_field,
                                   GwyOrientation orientation,
                                   gint col, gint row,
                                   gint width, gint height)
{
    static const gdouble hprewitt[] = {
        1.0/3.0, 0, -1.0/3.0,
        1.0/3.0, 0, -1.0/3.0,
        1.0/3.0, 0, -1.0/3.0,
    };
    static const gdouble vprewitt[] = {
         1.0/3.0,  1.0/3.0,  1.0/3.0,
         0,        0,        0,
        -1.0/3.0, -1.0/3.0, -1.0/3.0,
    };

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    if (orientation == GWY_ORIENTATION_HORIZONTAL)
        gwy_data_field_area_convolve_3x3(data_field, hprewitt, col, row, width, height);
    else
        gwy_data_field_area_convolve_3x3(data_field, vprewitt, col, row, width, height);
}

/**
 * gwy_data_field_filter_prewitt:
 * @data_field: A data field to apply the filter to.
 * @orientation: Filter orientation.
 *
 * Filters a data field with Prewitt filter.
 **/
void
gwy_data_field_filter_prewitt(GwyDataField *data_field,
                              GwyOrientation orientation)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_prewitt(data_field, orientation, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_filter_prewitt_total:
 * @data_field: A data field to apply the filter to.
 *
 * Filters a data field with total Prewitt filter.
 *
 * Since: 2.31
 **/
void
gwy_data_field_filter_prewitt_total(GwyDataField *data_field)
{
    GwyDataField *workspace;
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    workspace = gwy_data_field_duplicate(data_field);
    gwy_data_field_area_filter_prewitt(data_field, GWY_ORIENTATION_HORIZONTAL,
                                       0, 0, data_field->xres, data_field->yres);
    gwy_data_field_area_filter_prewitt(workspace, GWY_ORIENTATION_VERTICAL,
                                       0, 0, data_field->xres, data_field->yres);
    gwy_data_field_hypot_of_fields(data_field, data_field, workspace);
    g_object_unref(workspace);
}

/**
 * gwy_data_field_area_filter_dechecker:
 * @data_field: A data field to apply the filter to.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with 5x5 checker pattern removal
 * filter.
 *
 * Since: 2.1
 **/
void
gwy_data_field_area_filter_dechecker(GwyDataField *data_field,
                                     gint col, gint row,
                                     gint width, gint height)
{
    enum { size = 5 };
    static const gdouble dechecker[size*size] = {
         0.0,        1.0/144.0, -1.0/72.0,  1.0/144.0,  0.0,
         1.0/144.0, -1.0/18.0,   1.0/9.0,  -1.0/18.0,   1.0/144.0,
        -1.0/72.0,   1.0/9.0,    7.0/9.0,   1.0/9.0,   -1.0/72.0,
         1.0/144.0, -1.0/18.0,   1.0/9.0,  -1.0/18.0,   1.0/144.0,
         0.0,        1.0/144.0, -1.0/72.0,  1.0/144.0,  0.0,
    };
    GwyDataField *kernel;

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    kernel = gwy_data_field_new(size, size, 1.0, 1.0, FALSE);
    gwy_assign(kernel->data, dechecker, size*size);
    gwy_data_field_area_convolve(data_field, kernel, col, row, width, height);
    g_object_unref(kernel);
}

/**
 * gwy_data_field_filter_dechecker:
 * @data_field: A data field to apply the filter to.
 *
 * Filters a data field with 5x5 checker pattern removal filter.
 *
 * Since: 2.1
 **/
void
gwy_data_field_filter_dechecker(GwyDataField *data_field)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_dechecker(data_field, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_area_filter_gaussian:
 * @data_field: A data field to apply the filter to.
 * @sigma: The sigma parameter of the Gaussian.
 * @col: Upper-left column coordinate.
 * @row: Upper-left row coordinate.
 * @width: Area width (number of columns).
 * @height: Area height (number of rows).
 *
 * Filters a rectangular part of a data field with a Gaussian filter.
 *
 * The Gausian is normalized, i.e. it is sum-preserving.
 *
 * Since: 2.4
 **/
void
gwy_data_field_area_filter_gaussian(GwyDataField *data_field,
                                    gdouble sigma,
                                    gint col, gint row,
                                    gint width, gint height)
{
    GwyDataLine *kernel;
    gdouble x;
    gint res, i;

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    g_return_if_fail(sigma >= 0.0);
    if (sigma == 0.0)
        return;

    res = (gint)ceil(5.0*sigma);
    res = 2*res + 1;
    /* FIXME */
    i = 3*MIN(data_field->xres, data_field->yres);
    if (res > i) {
        res = i;
        if (res % 2 == 0)
            res--;
    }

    kernel = gwy_data_line_new(res, 1.0, FALSE);
    for (i = 0; i < res; i++) {
        x = i - (res - 1)/2.0;
        x /= sigma;
        kernel->data[i] = exp(-x*x/2.0);
    }
    gwy_data_line_multiply(kernel, 1.0/gwy_data_line_get_sum(kernel));
    gwy_data_field_area_convolve_1d(data_field, kernel, GWY_ORIENTATION_HORIZONTAL, col, row, width, height);
    gwy_data_field_area_convolve_1d(data_field, kernel, GWY_ORIENTATION_VERTICAL, col, row, width, height);
    g_object_unref(kernel);
}

/**
 * gwy_data_field_filter_gaussian:
 * @data_field: A data field to apply the filter to.
 * @sigma: The sigma parameter of the Gaussian.
 *
 * Filters a data field with a Gaussian filter.
 *
 * Since: 2.4
 **/
void
gwy_data_field_filter_gaussian(GwyDataField *data_field,
                               gdouble sigma)
{
    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    gwy_data_field_area_filter_gaussian(data_field, sigma, 0, 0, data_field->xres, data_field->yres);
}

/**
 * gwy_data_field_row_gaussian:
 * @data_field: A data field to apply the filter to.
 * @sigma: The sigma parameter of the Gaussian.
 *
 * Filters a data field with a Gaussian filter in horizontal direction.
 *
 * The Gausian is normalized, i.e. it is sum-preserving.
 *
 * Since: 2.54
 **/
void
gwy_data_field_row_gaussian(GwyDataField *data_field,
                            gdouble sigma)
{
    GwyDataLine *kernel;
    gdouble x;
    gint res, i;

    g_return_if_fail(GWY_IS_DATA_FIELD(data_field));
    g_return_if_fail(sigma >= 0.0);
    if (sigma == 0.0)
        return;

    res = (gint)ceil(5.0*sigma);
    res = 2*res + 1;
    i = 3*data_field->xres;
    if (res > i) {
        res = i;
        if (res % 2 == 0)
            res--;
    }

    kernel = gwy_data_line_new(res, 1.0, FALSE);
    for (i = 0; i < res; i++) {
        x = i - (res - 1)/2.0;
        x /= sigma;
        kernel->data[i] = exp(-x*x/2.0);
    }
    gwy_data_line_multiply(kernel, 1.0/gwy_data_line_get_sum(kernel));

    /* FIXME: use row_convolve_fft instead */
    gwy_data_field_area_convolve_1d(data_field, kernel, GWY_ORIENTATION_HORIZONTAL,
                                    0, 0, data_field->xres, data_field->yres);
    g_object_unref(kernel);
}

/**
 * gwy_data_field_column_gaussian:
 * @data_field: A data field to apply the filter to.
 * @sigma: The sigma parameter of the Gaussian.
 *
 * Filters a data field with a Gaussian filter in vertical direction.
 *
 * The Gausian is normalized, i.e. it is sum-preserving.
 *
 * Since: 2.54
 **/
void
gwy_data_field_column_gaussian(GwyDataField *data_field, gdouble sigma)
{
    GwyDataField *flipped = gwy_data_field_new_alike(data_field, FALSE);

    gwy_data_field_flip_xy(data_field, flipped, FALSE);
    gwy_data_field_row_gaussian(flipped, sigma);
    gwy_data_field_flip_xy(flipped, data_field, FALSE);
    g_object_unref(flipped);
}

static void
set_transfer_function_units(GwyDataField *ideal, GwyDataField *measured,
                            GwyDataField *transferfunc)
{
    GwySIUnit *sunit, *iunit, *tunit, *xyunit;

    xyunit = gwy_data_field_get_si_unit_xy(measured);
    sunit = gwy_data_field_get_si_unit_z(ideal);
    iunit = gwy_data_field_get_si_unit_z(measured);
    tunit = gwy_data_field_get_si_unit_z(transferfunc);
    gwy_si_unit_divide(iunit, sunit, tunit);
    gwy_si_unit_power_multiply(tunit, 1, xyunit, -2, tunit);
}

static inline void
deconvolve_do(const fftw_complex *fproduct,
              const fftw_complex *foperand,
              fftw_complex *fresult,
              guint n,
              gdouble lambda)
{
    guint k;

    for (k = 0; k < n; k++) {
        gdouble pre = gwycreal(fproduct[k]), pim = gwycimag(fproduct[k]);
        gdouble ore = gwycreal(foperand[k]), oim = gwycimag(foperand[k]);
        gdouble opnorm = ore*ore + oim*oim;
        gdouble denom = opnorm + lambda;

        gwycreal(fresult[k]) = (pre*ore + pim*oim)/denom;
        gwycimag(fresult[k]) = (-pre*oim + pim*ore)/denom;
    }
}

/**
 * gwy_data_field_deconvolve_regularized:
 * @dfield: A data field.
 * @operand: One of the factors entering the convolution resulting in @dfield. It must have the same dimensions as
 *           @dfield and it is assumed it has also the same physical size.
 * @out: Data field where to put the result into.  It will be resized to match @dfield.  It can also be @dfield
 *       itself.
 * @sigma: Regularization parameter.
 *
 * Performs deconvolution of a data field using a simple regularization.
 *
 * The operation can be used to deblur an image or conversely recover the point spread function from ideal response
 * image.
 *
 * Convolving the result with the operand using gwy_data_field_area_ext_convolve() with @as_integral=%TRUE will
 * recover (approximately) the image.  This means the deconvolution assumes continous convolution, not discrete sums.
 * Note that for the latter case this means the point spread function will be centered in @out.
 *
 * For recovery of transfer function, @dfield and @operand should be windowed beforehand if they are not periodic.
 *
 * Since: 2.51
 **/
void
gwy_data_field_deconvolve_regularized(GwyDataField *dfield,
                                      GwyDataField *operand,
                                      GwyDataField *out,
                                      gdouble sigma)
{
    gint xres, yres, cstride;
    gdouble lambda, msq;
    fftw_complex *ffield, *foper;
    fftw_plan fplan, bplan;

    g_return_if_fail(GWY_IS_DATA_FIELD(dfield));
    g_return_if_fail(GWY_IS_DATA_FIELD(operand));
    g_return_if_fail(GWY_IS_DATA_FIELD(out));
    xres = dfield->xres;
    yres = dfield->yres;
    g_return_if_fail(operand->xres == xres);
    g_return_if_fail(operand->yres == yres);

    cstride = xres/2 + 1;
    gwy_data_field_resample(out, xres, yres, GWY_INTERPOLATION_NONE);

    msq = gwy_data_field_get_mean_square(operand);
    if (!msq) {
        g_warning("Deconvolution by zero.");
        gwy_data_field_clear(out);
        return;
    }

    ffield = gwy_fftw_new_complex(cstride*yres);
    foper = gwy_fftw_new_complex(cstride*yres);
    fplan = gwy_fftw_plan_dft_r2c_2d(yres, xres, out->data, ffield, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    bplan = gwy_fftw_plan_dft_c2r_2d(yres, xres, ffield, out->data, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);

    gwy_data_field_copy(operand, out, FALSE);
    gwy_fftw_execute(fplan);
    gwy_assign(foper, ffield, cstride*yres);

    gwy_data_field_copy(dfield, out, FALSE);
    gwy_fftw_execute(fplan);
    fftw_destroy_plan(fplan);

    /* This seems wrong, but we just compensate the FFT.  Now the RMS of
     * fridata+i*fiidata is the same as of operand. */
    lambda = sigma * msq * xres*yres;
    deconvolve_do(ffield, foper, ffield, cstride*yres, lambda);
    fftw_free(foper);
    //gwycreal(ffield[0]) = gwycimag(ffield[0]) = 0.0; //FIXME
    gwy_fftw_execute(bplan);
    fftw_destroy_plan(bplan);
    fftw_free(ffield);

    /* NB: We normalize it as an integral.  So one recovers the convolution
     * with TRUE in ext-convolve! */
    gwy_data_field_multiply(out, 1.0/(dfield->xreal * dfield->yreal));
    gwy_data_field_2dfft_humanize(out);

    out->xreal = dfield->xreal;
    out->yreal = dfield->yreal;
    out->xoff = dfield->xoff;
    out->yoff = dfield->yoff;

    gwy_data_field_invalidate(out);
    set_transfer_function_units(operand, dfield, out);
}

static void
psf_sigmaopt_estimate_size(PSFSigmaOptData *sodata)
{
    const fftw_complex *fideal = sodata->fideal;
    const fftw_complex *fmeas = sodata->fmeas;
    fftw_complex *fpsf = sodata->fpsf;
    fftw_complex *cbuf = sodata->cbuf;
    GwyDataField *psf = sodata->psf;
    gint xres = psf->xres, yres = psf->yres;
    gint i, j, imin, jmin, imax, jmax, ext;
    const gdouble *d = sodata->psf->data;
    guint n;
    gdouble lambda, m;

    n = yres*sodata->cstride;
    sodata->col = xres/3;
    sodata->row = yres/3;
    sodata->width = xres - 2*sodata->col;
    sodata->height = yres - 2*sodata->row;

    /* Use a fairly large but not yet insane sigma value 4.0 to estimate the width.  We want to err on the side of
     * size overestimation here.
     * XXX: We might want to use a proportional to 1/sqrt(xres*yres) here. */
    lambda = 4.0 * sodata->sigma_scale;
    deconvolve_do(fmeas, fideal, fpsf, n, lambda);
    gwy_assign(cbuf, fpsf, n);
    gwy_fftw_execute(sodata->pplan);
    gwy_data_field_2dfft_humanize(psf);

    imax = yres/2;
    jmax = xres/2;
    m = 0.0;
    for (i = sodata->row; i < sodata->row + sodata->height; i++) {
        for (j = sodata->col; j < sodata->col + sodata->width; j++) {
            if (fabs(d[i*xres + j]) > m) {
                m = fabs(d[i*xres + j]);
                imax = i;
                jmax = j;
            }
        }
    }
    gwy_debug("maximum at (%d,%d)", imax, jmax);
    gwy_data_field_threshold(psf, 0.05*m, 0.0, 1.0);
    g_return_if_fail(d[imax*xres + jmax] > 0.0);
    gwy_data_field_grains_extract_grain(psf, jmax, imax);

    imin = imax;
    jmin = jmax;
    for (i = 0; i < yres; i++) {
        for (j = 0; j < xres; j++) {
            if (d[i*xres + j] > 0.0) {
                if (i < imin)
                    imin = i;
                if (i > imax)
                    imax = i;
                if (j < jmin)
                    jmin = j;
                if (j > jmax)
                    jmax = j;
            }
        }
    }

    ext = GWY_ROUND(0.5*log(xres*yres)) + 1;
    sodata->col = jmin - ext;
    sodata->row = imin - ext;
    sodata->width = jmax+1 - jmin + 2*ext;
    sodata->height = imax+1 - imin + 2*ext;
    if (sodata->col < 0) {
        sodata->width += sodata->col;
        sodata->col = 0;
    }
    if (sodata->row < 0) {
        sodata->height += sodata->row;
        sodata->row = 0;
    }
    if (sodata->col + sodata->width > xres)
        sodata->width = xres - sodata->col;
    if (sodata->row + sodata->height > yres)
        sodata->height = yres - sodata->row;

    gwy_debug("estimated region: %dx%d centered at (%d,%d)",
              sodata->width, sodata->height, sodata->col + sodata->width/2, sodata->row + sodata->height/2);
}

static void
psf_sigmaopt_prepare(GwyDataField *measured, GwyDataField *ideal,
                     PSFSigmaOptData *sodata)
{
    GwyDataField *buf;
    guint n, xres = measured->xres, yres = measured->yres;
    fftw_plan fplan;

    sodata->cstride = xres/2 + 1;
    n = yres*sodata->cstride;

    sodata->psf = buf = gwy_data_field_new_alike(measured, FALSE);
    sodata->fideal = g_new(fftw_complex, n);
    sodata->fmeas = g_new(fftw_complex, n);
    sodata->fpsf = g_new(fftw_complex, n);
    sodata->cbuf = gwy_fftw_new_complex(n);

    fplan = gwy_fftw_plan_dft_r2c_2d(yres, xres, buf->data, sodata->cbuf, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);

    /* FFT(ideal) */
    gwy_data_field_copy(ideal, buf, FALSE);
    sodata->sigma_scale = gwy_data_field_get_mean_square(buf);
    sodata->sigma_scale *= xres*yres;
    gwy_fftw_execute(fplan);
    gwy_assign(sodata->fideal, sodata->cbuf, n);

    /* FFT(measured) */
    gwy_data_field_copy(measured, buf, FALSE);
    gwy_fftw_execute(fplan);
    gwy_assign(sodata->fmeas, sodata->cbuf, n);

    fftw_destroy_plan(fplan);
    sodata->pplan = gwy_fftw_plan_dft_c2r_2d(yres, xres, sodata->cbuf, buf->data, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);

    psf_sigmaopt_estimate_size(sodata);
}

static void
psf_sigmaopt_free(PSFSigmaOptData *sodata)
{
    fftw_destroy_plan(sodata->pplan);
    fftw_free(sodata->cbuf);
    g_object_unref(sodata->psf);
    g_free(sodata->fpsf);
    g_free(sodata->fideal);
    g_free(sodata->fmeas);
}

static gdouble
psf_sigmaopt_residuum(gdouble logsigma, gpointer user_data)
{
    PSFSigmaOptData *sodata = (PSFSigmaOptData*)user_data;
    GwyDataField *psf = sodata->psf;
    const fftw_complex *fideal = sodata->fideal;
    const fftw_complex *fmeas = sodata->fmeas;
    fftw_complex *fpsf = sodata->fpsf;
    fftw_complex *cbuf = sodata->cbuf;
    gdouble lambda, w, sigma = exp(logsigma);
    guint n;

    /* Calculate frequency-space PSF sodata->fpsf and real-space sodata->psf. This essentially reproduces
     * gwy_data_field_deconvolve_regularized(). */
    n = psf->yres * sodata->cstride;
    lambda = sigma * sodata->sigma_scale;
    deconvolve_do(fmeas, fideal, fpsf, n, lambda);
    gwy_assign(cbuf, fpsf, n);
    gwy_fftw_execute(sodata->pplan);
    gwy_data_field_2dfft_humanize(psf);
    gwy_data_field_area_abs(psf, sodata->row, sodata->col, sodata->width, sodata->height);
    w = gwy_data_field_area_get_dispersion(psf, NULL, GWY_MASK_IGNORE,
                                           sodata->row, sodata->col, sodata->width, sodata->height,
                                           NULL, NULL);
    w = sqrt(w);
    return w;
}

/**
 * gwy_data_field_find_regularization_sigma_for_psf:
 * @dfield: A data field with convolved noisy data.
 * @ideal: A data field with ideal sharp data.
 *
 * Finds regularization parameter for point spread function calculation using regularized deconvolution.
 *
 * The estimated value should be suitable for reconstruction of the point spread function using
 * gwy_data_field_deconvolve_regularized().  The estimate is only suitable for PSF, it does not work for image
 * sharpening using a known PSF.
 *
 * Returns: Estimated regularization parameter.
 *
 * Since: 2.51
 **/
gdouble
gwy_data_field_find_regularization_sigma_for_psf(GwyDataField *dfield,
                                                 GwyDataField *ideal)
{
    PSFSigmaOptData sodata;
    gdouble logsigma;

    g_return_val_if_fail(GWY_IS_DATA_FIELD(dfield), 0.0);
    g_return_val_if_fail(GWY_IS_DATA_FIELD(ideal), 0.0);
    g_return_val_if_fail(!gwy_data_field_check_compatibility(dfield, ideal,
                                                             GWY_DATA_COMPATIBILITY_RES
                                                             | GWY_DATA_COMPATIBILITY_REAL
                                                             | GWY_DATA_COMPATIBILITY_LATERAL),
                         0.0);

    psf_sigmaopt_prepare(dfield, ideal, &sodata);
    logsigma = gwy_math_find_minimum_1d(psf_sigmaopt_residuum, log(1e-8), log(1e3), &sodata);
    psf_sigmaopt_free(&sodata);
    /* Experimentally determined fudge factor from large-scale simulations. */
    return 0.276*exp(logsigma);
}

/* xlen and ylen are dimensions of segments from the beginning. We assume the end part is one pixel shorter, as
 * expected for usual dehumanized images of odd sizes. */
static void
copy_corners(GwyDataField *source, GwyDataField *dest,
             gint xlen, gint ylen)
{
    gint sxres = source->xres;
    gint syres = source->yres;
    gint dxres = dest->xres;
    gint dyres = dest->yres;

    g_return_if_fail(2*xlen - 1 <= sxres);
    g_return_if_fail(2*ylen - 1 <= syres);
    g_return_if_fail(2*xlen - 1 <= dxres);
    g_return_if_fail(2*ylen - 1 <= dyres);

    gwy_data_field_clear(dest);

    /* Top left. */
    gwy_data_field_area_copy(source, dest, 0, 0, xlen, ylen, 0, 0);
    /* Top right. */
    gwy_data_field_area_copy(source, dest, sxres - (xlen-1), 0, xlen-1, ylen, dxres - (xlen-1), 0);
    /* Bottom left. */
    gwy_data_field_area_copy(source, dest, 0, syres - (ylen-1), xlen, ylen-1, 0, dyres - (ylen-1));
    /* Bottom right. */
    gwy_data_field_area_copy(source, dest, sxres - (xlen-1), syres - (ylen-1), xlen-1, ylen-1,
                             dxres - (xlen-1), dyres - (ylen-1));
}

static gdouble
dotprod_fields(GwyDataField *a, GwyDataField *b)
{
    gint xres = a->xres, yres = a->yres;
    const gdouble *da = a->data;
    const gdouble *db = b->data;
    gdouble s = 0.0;
    gint k;

    for (k = 0; k < xres*yres; k++)
        s += da[k]*db[k];

    return s;
}

static void
conjgrad_update_field(GwyDataField *res, GwyDataField *a, gdouble q, GwyDataField *b)
{
    gint xres = a->xres, yres = a->yres;
    gdouble *dr = res->data;
    const gdouble *da = a->data, *db = b->data;
    gint k;

    for (k = 0; k < xres*yres; k++)
        dr[k] = da[k] - q*db[k];
}

static void
conjgrad_matrix_multiply(const fftw_complex *fmat,
                         fftw_complex *fvec,
                         GwyDataField *vec,  /* vector-sized */
                         GwyDataField *buf,  /* matrix-sized */
                         fftw_plan fplan,
                         fftw_plan bplan,
                         GwyDataField *result)
{
    gint mxres = buf->xres, myres = buf->yres;
    gint vxres = vec->xres, vyres = vec->yres;
    gint cstride = mxres/2 + 1;
    gint k;

    /* Zero-extend the vector in the middle. */
    copy_corners(vec, buf, vxres/2+1, vyres/2+1);
    /* FFT(buf) -> fvec */
    gwy_fftw_execute(fplan);

    /* Multiply in frequency domain.  The matrix is symmetrical so ignore
     * the imaginary part. */
    for (k = 0; k < cstride*myres; k++) {
        gdouble mre = gwycreal(fmat[k]);
        gdouble vre = gwycreal(fvec[k]), vim = gwycimag(fvec[k]);
        gwycreal(fvec[k]) = mre*vre;
        gwycimag(fvec[k]) = mre*vim;
    }
    /* IFFT(fvec) -> buf */
    gwy_fftw_execute(bplan);
    /* Cut. */
    copy_corners(buf, result, vxres/2+1, vyres/2+1);
}

static gdouble
conjgrad_make_equations(GwyDataField *ideal, GwyDataField *measured,
                        gint txres, gint tyres,
                        GwyDataField *matrix, GwyDataField *rhs,
                        GwyDataField *buf)
{
    GwyDataField *autocor, *product;
    gint xres = ideal->xres;
    gint yres = ideal->yres;
    gint cstride = xres/2 + 1;
    fftw_complex *f = g_new(fftw_complex, cstride*yres);
    fftw_complex *g = g_new(fftw_complex, cstride*yres);
    gdouble *b = gwy_data_field_get_data(buf);
    fftw_plan fplan, bplan;
    gdouble fnorm;
    gint k;

    fplan = gwy_fftw_plan_dft_r2c_2d(yres, xres, b, f, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    bplan = gwy_fftw_plan_dft_c2r_2d(yres, xres, f, b, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);

    product = gwy_data_field_new_alike(ideal, FALSE);
    autocor = gwy_data_field_new_alike(ideal, FALSE);

    gwy_data_field_copy(ideal, buf, FALSE);
    gwy_fftw_execute(fplan);
    gwy_assign(g, f, cstride*yres);
    for (k = 0; k < cstride*yres; k++) {
        gdouble re = gwycreal(f[k]), im = gwycimag(f[k]);
        gwycreal(f[k]) = re*re + im*im;
        gwycimag(f[k]) = 0.0;
    }
    gwy_fftw_execute(bplan);
    gwy_data_field_copy(buf, autocor, FALSE);
    gwy_data_field_multiply(autocor, 1.0/(xres*yres));

    gwy_data_field_copy(measured, buf, FALSE);
    gwy_fftw_execute(fplan);
    fftw_destroy_plan(fplan);
    for (k = 0; k < cstride*yres; k++) {
        gdouble fre = gwycreal(f[k]), fim = gwycimag(f[k]);
        gdouble gre = gwycreal(g[k]), gim = gwycimag(g[k]);
        gwycreal(f[k]) = gre*fre + gim*fim;
        gwycimag(f[k]) = gre*fim - gim*fre;
    }
    g_free(g);
    gwy_fftw_execute(bplan);
    fftw_destroy_plan(bplan);
    g_free(f);
    gwy_data_field_copy(buf, product, FALSE);
    gwy_data_field_multiply(product, 1.0/(xres*yres));

    /* Calculate the left hand side matrix.  This is the autocorrelation ideal⊛ideal.  We subsequently cut it to the
     * central part with dimensions (2txres-1)×(2tyres-1) and then possibly zero-extend slightly to implement matrix
     * multiplication by convolution.  This is done in single step by copying the four corners. */
    copy_corners(autocor, matrix, txres, tyres);
    g_object_unref(autocor);

    /* Calculate the right hand side vector.  This is the cross-correlation ideal⊛measured. Here we cut the result
     * just to txres×tyres size and zero-extend it. */
    copy_corners(product, rhs, txres/2+1, tyres/2+1);
    g_object_unref(product);

    /* The matrix and rhs data can be of wild orders of magnitude.  Scale them in sync to some reasonable range,
     * preferably a bit larger to have many orders of magnitude to go down. */
    fnorm = sqrt(gwy_data_field_get_mean_square(rhs));
    if (fnorm > 0.0) {
        fnorm = 1e24/fnorm;
        gwy_data_field_multiply(rhs, fnorm);
        gwy_data_field_multiply(matrix, fnorm);
    }
    else
        fnorm = 1.0;

    return fnorm;
}

static void
psf_least_squares_conjgrad_iterate(GwyDataField *ffield,
                                   GwyDataField *vfield,
                                   GwyDataField *wfield,
                                   GwyDataField *tf,
                                   GwyDataField *rhs,
                                   GwyDataField *buf,
                                   fftw_complex *fmat,
                                   fftw_complex *fvec,
                                   fftw_plan fplan,
                                   fftw_plan bplan,
                                   guint maxiter,
                                   gdouble eps)
{
    gdouble fnorm0, fnorm;
    guint iter;

    /* Initialise conjugated gradients.
     * f0 = v0 = matrix tf0 - rhs
     * w0 = matrix v0
     */
    conjgrad_matrix_multiply(fmat, fvec, tf, buf, fplan, bplan, vfield);
    gwy_data_field_subtract_fields(vfield, vfield, rhs);
    gwy_data_field_copy(vfield, ffield, FALSE);

    conjgrad_matrix_multiply(fmat, fvec, vfield, buf, fplan, bplan, wfield);
    fnorm0 = gwy_data_field_get_mean_square(ffield);

    /* Iterate. */
    for (iter = 0; iter < maxiter; iter++) {
        gdouble numer = dotprod_fields(ffield, vfield);
        gdouble denom = dotprod_fields(vfield, wfield);

        if (numer == 0.0 || denom == 0.0)
            break;

        /* Update solution p(j+1) = p(j) - n/d v(j) */
        conjgrad_update_field(tf, tf, numer/denom, vfield);
        /* Update residual f(j+1) = f(j) - n/d w(j) */
        conjgrad_update_field(ffield, ffield, numer/denom, wfield);
        /* Check norm of ffield and maybe stop iterations... */
        fnorm = gwy_data_field_get_mean_square(ffield);
        if (fnorm <= eps*fnorm0)
            break;
        /* Update gradient v(j+1) = f(j+1) - n/d v(j) */
        conjgrad_update_field(vfield, ffield, dotprod_fields(ffield, wfield)/denom, vfield);
        conjgrad_matrix_multiply(fmat, fvec, vfield, buf, fplan, bplan, wfield);
    }
}

/* Set up plans for FFT between buf (real) and fvec (complex). */
static gint
conjgrad_make_fft_plans(GwyDataField *buf,
                        fftw_complex **fmat, fftw_complex **fvec,
                        fftw_plan *fplan, fftw_plan *bplan,
                        gint xsize, gint ysize)
{
    gint cstride;

    /* Set up the frequency domain operations. */
    gwy_data_field_resample(buf, xsize, ysize, GWY_INTERPOLATION_NONE);
    cstride = xsize/2 + 1;
    *fmat = gwy_fftw_new_complex(cstride*ysize);
    *fvec = gwy_fftw_new_complex(cstride*ysize);
    *fplan = gwy_fftw_plan_dft_r2c_2d(ysize, xsize, buf->data, *fvec, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    *bplan = gwy_fftw_plan_dft_c2r_2d(ysize, xsize, *fvec, buf->data, FFTW_DESTROY_INPUT | FFTW_ESTIMATE);
    return cstride;
}

static gboolean
psf_least_squares_warn(gint *want_txres, gint *want_tyres, gint border,
                       gint xres, gint yres)
{
    gboolean ok = TRUE;

    if (!(*want_txres & 1)) {
        g_warning("Transfer function x-res is even.  Fixing to odd.");
        *want_txres |= 1;
        ok = FALSE;
    }
    if (!(*want_tyres & 1)) {
        g_warning("Transfer function y-res is even.  Fixing to odd.");
        *want_tyres |= 1;
        ok = FALSE;
    }

    if (*want_txres + 2*border > xres/2 || *want_tyres + 2*border > yres/2) {
        g_warning("Transfer function size is more than half image size. "
                  "I will proceed but the result will be rubbish.");
        ok = FALSE;
    }
    return ok;
}

/**
 * gwy_data_field_deconvolve_psf_leastsq:
 * @dfield: A data field.
 * @operand: Ideal sharp measurement (before convolution). It must have the same dimensions as @dfield and it is
 *           assumed it has also the same physical size.
 * @out: Output field for the transfer function.  Its dimensions are preserved and determine the transfer function
 *       support.  It must be smaller than half of @dfield.
 * @sigma: Regularization parameter.
 * @border: Number of pixel to extend and cut off the transfer function.
 *
 * Performs reconstruction of transfer function from convolved and ideal sharp images.
 *
 * The transfer function is reconstructed by solving the corresponding least squares problem.  This method is suitable
 * when the dimensions of @out are much smaller than the images.
 *
 * Since the method accumulates errors close to edges, they can be removed within the procedure by reconstructing
 * a slightly larger transfer function and then cutting the result.  The extension is given by @border, typical
 * suitable values are 2 or 3.
 *
 * Convolving the result with the operand using gwy_data_field_area_ext_convolve() with @as_integral=%TRUE will
 * recover (approximately) the image.  This means the deconvolution assumes continous convolution, not discrete sums.
 * Note that for the latter case this means the point spread function will be centered in @out.
 *
 * Fields @dfield and @operand should be windowed beforehand if they are not periodic.
 *
 * Since: 2.52
 **/
void
gwy_data_field_deconvolve_psf_leastsq(GwyDataField *measured,
                                      GwyDataField *ideal,
                                      GwyDataField *tf,
                                      gdouble sigma,
                                      gint border)
{
    gint xres, yres, want_txres, want_tyres;
    gint txres, tyres, xsize, ysize, cstride;
    gdouble dx, dy, q, mnorm, fnorm, lambda;
    GwyDataField *buf, *matrix, *rhs, *ffield, *vfield, *wfield;
    fftw_complex *fmat, *fvec;
    fftw_plan fplan, bplan;

    g_return_if_fail(GWY_IS_DATA_FIELD(measured));
    g_return_if_fail(GWY_IS_DATA_FIELD(ideal));
    g_return_if_fail(GWY_IS_DATA_FIELD(tf));
    xres = measured->xres;
    yres = measured->yres;
    g_return_if_fail(ideal->xres == xres);
    g_return_if_fail(ideal->yres == yres);
    g_return_if_fail(border >= 0);

    want_txres = tf->xres;
    want_tyres = tf->yres;
    dx = gwy_data_field_get_dx(measured);
    dy = gwy_data_field_get_dy(measured);
    q = dx*dy;

    want_txres = tf->xres;
    want_tyres = tf->yres;
    psf_least_squares_warn(&want_txres, &want_tyres, border, xres, yres);
    txres = want_txres + 2*border;
    tyres = want_tyres + 2*border;
    xsize = gwy_fft_find_nice_size(2*txres-1);
    ysize = gwy_fft_find_nice_size(2*tyres-1);

    gwy_data_field_resample(tf, txres, tyres, GWY_INTERPOLATION_NONE);
    gwy_data_field_clear(tf);

    buf = gwy_data_field_new_alike(ideal, FALSE);
    matrix = gwy_data_field_new(xsize, ysize, xsize, ysize, FALSE);
    rhs = gwy_data_field_new_alike(tf, FALSE);
    fnorm = conjgrad_make_equations(ideal, measured, txres, tyres, matrix, rhs, buf);

    /* Normalise sigma relatively to data. */
    /* Element matrix_00 is (unnormalised) ACF at zero distance, i.e. sum of squared data.  This is a good scaling
     * norm for the matrix we can use instead of actually calculating some matrix norm -- its elements are kind of
     * proportional to matrix_00 around the origin anyway, so this would add just some odd multiplicative constant. */
    mnorm = gwy_data_field_get_mean_square(ideal);
    /* We should multiply with the number of columns (not rows!) of matrix implementing the convolution operator
     * ideal*, see Golub's book. However, we then want to divide it by the square root of that to make sigma more or
     * less resolution-independent.  */
    mnorm *= xres*yres;
    /* This compensates different scaling of LSM with transfer function support size than the other two methods.  */
    mnorm *= cbrt(want_txres*want_tyres);
    lambda = sigma * mnorm * fnorm;
    gwy_data_field_set_val(matrix, 0, 0, gwy_data_field_get_val(matrix, 0, 0) + lambda);

    /* Set up the frequency domain operations. */
    gwy_data_field_resample(buf, xsize, ysize, GWY_INTERPOLATION_NONE);
    cstride = conjgrad_make_fft_plans(buf, &fmat, &fvec, &fplan, &bplan, xsize, ysize);

    gwy_data_field_copy(matrix, buf, FALSE);
    GWY_OBJECT_UNREF(matrix);
    /* Compensate unnormalised FFTs here and in conjgrad_matrix_multiply() */
    gwy_data_field_multiply(buf, 1.0/(xsize*ysize));
    gwy_fftw_execute(fplan);
    gwy_assign(fmat, fvec, cstride*ysize);

    vfield = gwy_data_field_new(txres, tyres, txres, tyres, FALSE);
    ffield = gwy_data_field_new_alike(vfield, FALSE);
    wfield = gwy_data_field_new_alike(vfield, FALSE);

    psf_least_squares_conjgrad_iterate(ffield, vfield, wfield, tf, rhs, buf, fmat, fvec, fplan, bplan, 150, 1e-40);
    fftw_free(fvec);
    fftw_free(fmat);

    gwy_data_field_2dfft_humanize(tf);
    if (border > 0)
        gwy_data_field_resize(tf, border, border, want_txres + border, want_tyres + border);
    gwy_data_field_multiply(tf, 1.0/q);
    gwy_data_field_set_xreal(tf, want_txres*dx);
    gwy_data_field_set_yreal(tf, want_tyres*dy);
    gwy_data_field_set_xoffset(tf, -0.5*want_txres*dx);
    gwy_data_field_set_yoffset(tf, -0.5*want_tyres*dy);
    set_transfer_function_units(ideal, measured, tf);

    g_object_unref(buf);
    g_object_unref(rhs);
    g_object_unref(ffield);
    g_object_unref(vfield);
    g_object_unref(wfield);
    fftw_destroy_plan(fplan);
    fftw_destroy_plan(bplan);
}

static gdouble
psf_conjgrad_residuum(gdouble logsigma, gpointer user_data)
{
    PSFConjGradData *cgdata = (PSFConjGradData*)user_data;
    gdouble lambda, sigma, width;
    gint want_txres = cgdata->want_txres, want_tyres = cgdata->want_tyres;
    gint xsize = cgdata->xsize, ysize = cgdata->ysize;
    gint border = cgdata->border;

    sigma = exp(logsigma);

    /* cgdata->mnorm already includes all the scaling factors, so just multiply
     * it with sigma. */
    lambda = sigma * cgdata->mnorm;
    gwy_data_field_set_val(cgdata->matrix, 0, 0, cgdata->matrix00 + lambda);

    gwy_data_field_copy(cgdata->matrix, cgdata->buf, FALSE);
    /* Compensate unnormalised FFTs here and in conjgrad_matrix_multiply() */
    gwy_data_field_multiply(cgdata->buf, 1.0/(xsize*ysize));
    gwy_fftw_execute(cgdata->fplan);
    gwy_assign(cgdata->fmat, cgdata->fvec, cgdata->cstride*ysize);

    gwy_data_field_clear(cgdata->tf);
    psf_least_squares_conjgrad_iterate(cgdata->ffield, cgdata->vfield, cgdata->wfield, cgdata->tf,
                                       cgdata->rhs, cgdata->buf, cgdata->fmat, cgdata->fvec,
                                       cgdata->fplan, cgdata->bplan,
                                       60, 1e-24);
    gwy_data_field_2dfft_humanize(cgdata->tf);

    gwy_data_field_abs(cgdata->tf);
    width = gwy_data_field_area_get_dispersion(cgdata->tf, NULL, GWY_MASK_IGNORE,
                                               border, border, want_txres, want_tyres, NULL, NULL);
    return sqrt(width);
}

/**
 * gwy_data_field_find_regularization_sigma_leastsq:
 * @dfield: A data field with convolved noisy data.
 * @ideal: A data field with ideal sharp data.
 * @width: Horizontal size of transfer function support.
 * @height: Vertical size of transfer function support.
 * @border: Number of pixel to extend and cut off the transfer function.
 *
 * Finds regularization parameter for point spread function calculation using least squares method.
 *
 * The estimated value should be suitable for reconstruction of the point spread function using
 * gwy_data_field_deconvolve_psf_leastsq().
 *
 * Returns: Estimated regularization parameter.
 *
 * Since: 2.52
 **/
gdouble
gwy_data_field_find_regularization_sigma_leastsq(GwyDataField *measured,
                                                 GwyDataField *ideal,
                                                 gint want_txres,
                                                 gint want_tyres,
                                                 gint border)
{
    gdouble logsigma, dx, dy, fnorm;
    gint xres, yres, txres, tyres, xsize, ysize;
    PSFConjGradData cgdata;

    g_return_val_if_fail(GWY_IS_DATA_FIELD(measured), 0.001);
    g_return_val_if_fail(GWY_IS_DATA_FIELD(ideal), 0.001);
    xres = measured->xres;
    yres = measured->yres;
    g_return_val_if_fail(ideal->xres == xres, 0.001);
    g_return_val_if_fail(ideal->yres == yres, 0.001);
    g_return_val_if_fail(want_txres > 0, 0.001);
    g_return_val_if_fail(want_tyres > 0, 0.001);
    g_return_val_if_fail(border >= 0, 0.001);
    dx = measured->xreal/xres;
    dy = measured->yreal/yres;

    psf_least_squares_warn(&want_txres, &want_tyres, border, xres, yres);
    cgdata.want_txres = want_txres;
    cgdata.want_tyres = want_tyres;
    cgdata.border = border;
    cgdata.txres = txres = want_txres + 2*border;
    cgdata.tyres = tyres = want_tyres + 2*border;
    cgdata.xsize = xsize = gwy_fft_find_nice_size(2*cgdata.txres-1);
    cgdata.ysize = ysize = gwy_fft_find_nice_size(2*cgdata.tyres-1);
    cgdata.tf = gwy_data_field_new(txres, tyres, dx*txres, dy*tyres, TRUE);
    cgdata.buf = gwy_data_field_new_alike(ideal, FALSE);
    cgdata.matrix = gwy_data_field_new(xsize, ysize, xsize, ysize, FALSE);
    cgdata.rhs = gwy_data_field_new_alike(cgdata.tf, FALSE);
    fnorm = conjgrad_make_equations(ideal, measured, txres, tyres, cgdata.matrix, cgdata.rhs, cgdata.buf);
    /* See gwy_data_field_deconvolve_psf_leastsq() for explanation. */
    cgdata.mnorm = gwy_data_field_get_mean_square(ideal);
    cgdata.mnorm *= xres*yres;
    cgdata.mnorm *= cbrt(want_txres*want_tyres);
    cgdata.mnorm *= fnorm;
    cgdata.matrix00 = gwy_data_field_get_val(cgdata.matrix, 0, 0);

    /* Set up the frequency domain operations. */
    gwy_data_field_resample(cgdata.buf, cgdata.xsize, cgdata.ysize,
                            GWY_INTERPOLATION_NONE);
    cgdata.cstride = conjgrad_make_fft_plans(cgdata.buf, &cgdata.fmat, &cgdata.fvec, &cgdata.fplan, &cgdata.bplan,
                                             xsize, ysize);

    cgdata.vfield = gwy_data_field_new(txres, tyres, txres, tyres, FALSE);
    cgdata.ffield = gwy_data_field_new_alike(cgdata.vfield, FALSE);
    cgdata.wfield = gwy_data_field_new_alike(cgdata.vfield, FALSE);

    logsigma = gwy_math_find_minimum_1d(psf_conjgrad_residuum, log(1e-8), log(1e3), &cgdata);

    fftw_free(cgdata.fvec);
    fftw_free(cgdata.fmat);
    g_object_unref(cgdata.tf);
    g_object_unref(cgdata.buf);
    g_object_unref(cgdata.rhs);
    g_object_unref(cgdata.matrix);
    g_object_unref(cgdata.ffield);
    g_object_unref(cgdata.vfield);
    g_object_unref(cgdata.wfield);
    fftw_destroy_plan(cgdata.fplan);
    fftw_destroy_plan(cgdata.bplan);

    /* Experimentally determined fudge factor from large-scale simulations. */
    return 0.298*exp(logsigma);
}

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