File: postproc_editor.py

package info (click to toggle)
planetary-system-stacker 0.8.32~git20230901.01f3625-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 50,468 kB
  • sloc: python: 14,055; makefile: 3
file content (1897 lines) | stat: -rw-r--r-- 92,277 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
# -*- coding: utf-8; -*-
"""
Copyright (c) 2018 Rolf Hempel, rolf6419@gmx.de

This file is part of the PlanetarySystemStacker tool (PSS).
https://github.com/Rolf-Hempel/PlanetarySystemStacker

PSS 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 3 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 PSS.  If not, see <http://www.gnu.org/licenses/>.

"""

from copy import copy, deepcopy
from math import sqrt
from pathlib import Path
from sys import argv, stdout, stderr
from time import sleep

import psutil
from PyQt5 import QtWidgets, QtCore
from PyQt5.QtWidgets import QProxyStyle, QStyle
from cv2 import imread, cvtColor, COLOR_BGR2RGB, GaussianBlur, bilateralFilter, BORDER_DEFAULT, \
    COLOR_BGR2HSV, COLOR_HSV2BGR
from numpy import uint8, uint16, float32

from configuration import Configuration, PostprocLayer
from exceptions import InternalError
from frame_viewer import FrameViewer
from frames import Frames
from miscellaneous import Miscellaneous
from postproc_editor_gui import Ui_postproc_editor
from sharpening_layer_widget import Ui_sharpening_layer_widget
from version_manager_widget import Ui_version_manager_widget


class SharpeningLayerWidget(QtWidgets.QWidget, Ui_sharpening_layer_widget):
    """
    GUI widget to manipulate the parameters of a sharpening layer. An arbitrary number of sharpening
    layers can be defined (up to a maximum set as configuration parameter).
    """
    signal_toggle_luminance_other_layers = QtCore.pyqtSignal()

    def __init__(self, layer_index, remove_layer_callback, parent=None):
        """
        Initialize the widget.

        :param layer_index: Index of the sharpening layer (starting with 1).
        :param remove_layer_callback: Callback function to remove a layer from the current version.
        :param parent: Parent object.
        """

        QtWidgets.QWidget.__init__(self, parent)
        self.setupUi(self)

        self.layer_index = layer_index
        self.title = "Layer " + str(layer_index + 1)
        self.remove_layer_callback = remove_layer_callback

        self.horizontalSlider_radius.valueChanged.connect(self.horizontalSlider_radius_changed)
        self.lineEdit_radius.textChanged.connect(self.lineEdit_radius_changed)
        self.horizontalSlider_amount.valueChanged.connect(self.horizontalSlider_amount_changed)
        self.lineEdit_amount.textChanged.connect(self.lineEdit_amount_changed)
        self.horizontalSlider_bi_fraction.valueChanged.connect(self.horizontalSlider_bi_fraction_changed)
        self.lineEdit_bi_fraction.textChanged.connect(self.lineEdit_bi_fraction_changed)
        self.horizontalSlider_bi_range.valueChanged.connect(
            self.horizontalSlider_bi_range_changed)
        self.lineEdit_bi_range.textChanged.connect(self.lineEdit_bi_range_changed)
        self.horizontalSlider_denoise.valueChanged.connect(
            self.horizontalSlider_denoise_changed)
        self.lineEdit_denoise.textChanged.connect(self.lineEdit_denoise_changed)
        self.checkBox_luminance.stateChanged.connect(self.checkBox_luminance_toggled)
        self.pushButton_remove.clicked.connect(self.remove_layer)

    def set_values(self, layer):
        """
        Set the values of the GUI widget according to the current data model

        :param layer: Layer instance holding the sharpening parameters
        :return: -
        """

        self.layer = layer

        self.groupBox_layer.setTitle(self.title)
        self.horizontalSlider_radius.setValue(self.radius_to_integer(self.layer.radius))
        self.lineEdit_radius.setText(str(self.layer.radius))
        self.horizontalSlider_amount.setValue(self.amount_to_integer(self.layer.amount))
        self.lineEdit_amount.setText("{0:.2f}".format(round(self.layer.amount, 2)))
        self.horizontalSlider_bi_fraction.setValue(
            self.bi_fraction_to_integer(self.layer.bi_fraction))
        self.lineEdit_bi_fraction.setText("{0:.2f}".format(round(self.layer.bi_fraction, 2)))
        self.horizontalSlider_bi_range.setValue(self.bi_range_to_integer(self.layer.bi_range))
        self.lineEdit_bi_range.setText(str(self.layer.bi_range))
        self.horizontalSlider_denoise.setValue(
            self.denoise_to_integer(self.layer.denoise))
        self.lineEdit_denoise.setText("{0:.2f}".format(round(self.layer.denoise, 2)))

        # Temporarily block signals for the luminance checkbox. Otherwise the variable would be
        # switched immediately.
        self.checkBox_luminance.blockSignals(True)
        self.checkBox_luminance.setChecked(self.layer.luminance_only)
        self.checkBox_luminance.blockSignals(False)

    # The following four methods implement the translation between data model values and the
    # (integer) values of the horizontal GUI sliders. In the case of the sharpening amount this
    # translation is non-linear in order to add resolution at small values.
    @staticmethod
    def radius_to_integer(radius):
        return max(min(round(radius * 10.), 99), 1)

    @staticmethod
    def integer_to_radius(int):
        return int / 10.

    @staticmethod
    def amount_to_integer(amount):
        # Below the slider value 20 (amount = 1.) the behaviour is linear, above quadratic.
        if amount > 1.:
            a = 187. / 6400.
            b = 0.15 - 40. * a
            c = 400. * a - 2.
            return (round(-b / (2. * a) + sqrt(b**2 / a**2 / 4. - (c - amount) / a)))
        else:
            return round((amount + 2.) / 0.15)

    @staticmethod
    def integer_to_amount(integer):
        if integer <= 20:
            return -2. + 0.15 * integer
        else:
            a = 187. / 6400.
            b = 0.15 - 40. * a
            c = 400. * a - 2.
            return round(a * integer**2 + b * integer + c, 2)

    @staticmethod
    def bi_fraction_to_integer(bi_fraction):
        return round(bi_fraction * 100)

    @staticmethod
    def integer_to_bi_fraction(integer):
        return integer / 100.

    @staticmethod
    def bi_range_to_integer(bi_range):
        # Below the slider value 50 (bi_range = 13.) the behaviour is linear, above quadratic.
        if bi_range > 13.:
            c = 0.009022222222222221
            g = 35.59113300492611
            h = -1233.2712514256596
            return round(g + sqrt(h + bi_range / c))
        else:
            b = 0.26
            return round(bi_range / b)

    @staticmethod
    def integer_to_bi_range(integer):
        if integer <= 50:
            b = 0.26
            return round(b * integer, 1)
        else:
            c = 0.009022222222222221
            e = -0.6422222222222221
            f = 22.555555555555554
            return round(c * integer ** 2 + e * integer + f, 1)

    @staticmethod
    def denoise_to_integer(denoise):
        return round(denoise * 100)

    @staticmethod
    def integer_to_denoise(integer):
        return integer / 100.

    def horizontalSlider_radius_changed(self):
        """
        The "radius" slider has been moved. Update the corresponding lineEdit widget. Block its
        signals temporarily to avoid cross-talk.
        :return: -
        """

        self.layer.radius = self.integer_to_radius(self.horizontalSlider_radius.value())
        self.lineEdit_radius.blockSignals(True)
        self.lineEdit_radius.setText(str(self.layer.radius))
        self.lineEdit_radius.blockSignals(False)

    def lineEdit_radius_changed(self):
        """
        The text of the lineEdit widget has been changed. Update the horizontal slider. Include a
        try-except block to avoid illegal user input to crash the program.

        :return: -
        """

        try:
            self.layer.radius = max(0.1, min(float(self.lineEdit_radius.text()), 9.9))
            self.horizontalSlider_radius.blockSignals(True)
            self.horizontalSlider_radius.setValue(self.radius_to_integer(self.layer.radius))
            self.horizontalSlider_radius.blockSignals(False)
        except:
            pass

    def horizontalSlider_amount_changed(self):
        """
        The same as above for the "amount" parameter.

        :return: -
        """

        self.layer.amount = self.integer_to_amount(self.horizontalSlider_amount.value())
        self.lineEdit_amount.blockSignals(True)
        self.lineEdit_amount.setText("{0:.2f}".format(round(self.layer.amount, 2)))
        self.lineEdit_amount.blockSignals(False)

    def lineEdit_amount_changed(self):
        """
        The same as above for the "amount" parameter.

        :return: -
        """

        try:
            self.layer.amount = float(self.lineEdit_amount.text())
            self.horizontalSlider_amount.blockSignals(True)
            self.horizontalSlider_amount.setValue(self.amount_to_integer(self.layer.amount))
            self.horizontalSlider_amount.blockSignals(False)
        except:
            pass

    def horizontalSlider_bi_fraction_changed(self):
        """
        The same as above for the "bi_fraction" parameter.

        :return: -
        """

        self.layer.bi_fraction = self.integer_to_bi_fraction(self.horizontalSlider_bi_fraction.value())
        self.lineEdit_bi_fraction.blockSignals(True)
        self.lineEdit_bi_fraction.setText("{0:.2f}".format(round(self.layer.bi_fraction, 2)))
        self.lineEdit_bi_fraction.blockSignals(False)

    def lineEdit_bi_fraction_changed(self):
        """
        The same as above for the "bi_fraction" parameter.

        :return: -
        """

        try:
            self.layer.bi_fraction = float(self.lineEdit_bi_fraction.text())
            self.horizontalSlider_bi_fraction.blockSignals(True)
            self.horizontalSlider_bi_fraction.setValue(self.bi_fraction_to_integer(self.layer.bi_fraction))
            self.horizontalSlider_bi_fraction.blockSignals(False)
        except:
            pass

    def horizontalSlider_bi_range_changed(self):
        """
        The same as above for the "bi_range" parameter.

        :return: -
        """

        self.layer.bi_range = self.integer_to_bi_range(self.horizontalSlider_bi_range.value())
        self.lineEdit_bi_range.blockSignals(True)
        self.lineEdit_bi_range.setText(str(self.layer.bi_range))
        self.lineEdit_bi_range.blockSignals(False)

    def lineEdit_bi_range_changed(self):
        """
        The same as above for the "bi_range" parameter.

        :return: -
        """

        try:
            self.layer.bi_range = max(0, min(int(round(float(self.lineEdit_bi_range.text()))), 255))
            self.horizontalSlider_bi_range.blockSignals(True)
            self.horizontalSlider_bi_range.setValue(self.bi_range_to_integer(self.layer.bi_range))
            self.horizontalSlider_bi_range.blockSignals(False)
        except:
            pass

    def horizontalSlider_denoise_changed(self):
        """
        The same as above for the "denoise" parameter.

        :return: -
        """

        self.layer.denoise = self.integer_to_denoise(self.horizontalSlider_denoise.value())
        self.lineEdit_denoise.blockSignals(True)
        self.lineEdit_denoise.setText("{0:.2f}".format(round(self.layer.denoise, 2)))
        self.lineEdit_denoise.blockSignals(False)

    def lineEdit_denoise_changed(self):
        """
        The same as above for the "denoise" parameter.

        :return: -
        """

        try:
            self.layer.denoise = float(self.lineEdit_denoise.text())
            self.horizontalSlider_denoise.blockSignals(True)
            self.horizontalSlider_denoise.setValue(self.denoise_to_integer(self.layer.denoise))
            self.horizontalSlider_denoise.blockSignals(False)
        except:
            pass


    def checkBox_luminance_toggled(self):
        """
        The checkbox which tells if the sharpening should affect the luminance channel only has
        changed its state.

        :return: -
        """

        self.layer.luminance_only = not self.layer.luminance_only

        # Signal the other layers to change the flag as well.
        self.signal_toggle_luminance_other_layers.emit()

    @QtCore.pyqtSlot()
    def checkBox_luminance_toggled_no_signal(self):
        """
        The checkbox which tells if the sharpening should affect the luminance channel only has
        changed its state. This version of the method is invoked by another layer which has changed
        the toggle state. In this case, not only toggle the state variable, but also toggle the
        checkbox widget (because this was not done by the user). To avoid an avalanche, do not emit
        a signal.

        :return: -
        """

        # Block the signals of this widget to avoid a feedback loop.
        self.checkBox_luminance.blockSignals(True)
        self.checkBox_luminance.setChecked(not self.checkBox_luminance.isChecked())
        self.checkBox_luminance.blockSignals(False)
        self.layer.luminance_only = not self.layer.luminance_only

    def remove_layer(self):
        """
        This method is executed when the user presses the "remove" button. The actual action is
        performed by the higher-level "PostprocEditorWidget" object.
        :return: -
        """

        self.remove_layer_callback(self.layer_index)


class CustomStyle(QProxyStyle):
    """
    This class is used to prevent the version spinbox from jumping two steps at once. The solution
    was found on Stackoverflow
    (https://stackoverflow.com/questions/40746350/why-qspinbox-jumps-twice-the-step-value).

    """

    def styleHint(self, hint, option=None, widget=None, returnData=None):
        if hint == QStyle.SH_SpinBox_KeyPressAutoRepeatRate:
            return 10**6
        elif hint == QStyle.SH_SpinBox_ClickAutoRepeatRate:
            return 10**6
        elif hint == QStyle.SH_SpinBox_ClickAutoRepeatThreshold:
            # You can use only this condition to avoid the auto-repeat,
            # but better safe than sorry ;-)
            return 10**6
        else:
            return super().styleHint(hint, option, widget, returnData)


class VersionManagerWidget(QtWidgets.QWidget, Ui_version_manager_widget):
    """
    This GUI widget manages the different postprocessing versions and controls which image is
    displayed in the image viewer. It also starts and controls the blink comparator.
    """

    # The set_photo_signal tells the image viewer which image it should show.
    set_photo_signal = QtCore.pyqtSignal(int)
    # The blink comparator emits the variant_shown_signal to highlight / de-highlight the GUI image
    # selector which corresponds to the image currently displayed.
    variant_shown_signal = QtCore.pyqtSignal(bool, bool)
    # This signal tells the PostprocEditorWidget to show the selected version in the frame viewer.
    select_version_signal = QtCore.pyqtSignal(int)

    def __init__(self, configuration, parent=None):
        """

        :param configuration: Configuration object with parameters.
        :param parent: parent object.
        """

        QtWidgets.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.configuration = configuration
        self.pss_version = configuration.global_parameters_version
        self.postproc_data_object = configuration.postproc_data_object
        self.postproc_blinking_period = configuration.postproc_blinking_period

        self.spinBox_version.valueChanged.connect(self.select_version)
        self.spinBox_compare.valueChanged.connect(self.select_version_compared)
        self.pushButton_new.clicked.connect(self.new_version)
        self.pushButton_delete.clicked.connect(self.remove_version)
        self.checkBox_blink_compare.stateChanged.connect(self.blinking_toggled)
        self.pushButton_save.clicked.connect(self.save_version)
        self.pushButton_save_as.clicked.connect(self.save_version_as)
        self.variant_shown_signal.connect(self.highlight_variant)

        # Set the ranges for version selection spinboxes. Initially, there are only two versions.
        self.spinBox_version.setMaximum(configuration.postproc_data_object.number_versions)
        self.spinBox_version.setMinimum(0)
        self.spinBox_compare.setMaximum(configuration.postproc_data_object.number_versions)
        self.spinBox_compare.setMinimum(0)
        self.spinBox_version.setStyle(CustomStyle())
        self.spinBox_compare.setStyle(CustomStyle())

        # Set the spinbox to the newly created version.
        self.spinBox_version.setValue(self.postproc_data_object.version_selected)

    def select_version(self):
        """
        This method is called when the user changes the setting of the "version selected" spinbox.

        :return: -
        """
        self.postproc_data_object.version_selected = self.spinBox_version.value()
        self.select_version_signal.emit(self.postproc_data_object.version_selected)

    def select_version_compared(self):
        """
        This method is called when the user changes the setting of the "version compared" spinbox.
        The "selected" and "compared" versions are displayed alternately by the blink comparator.
        :return: -
        """

        self.postproc_data_object.version_compared = self.spinBox_compare.value()

    def new_version(self):
        """
        Create a new postprocessing version.
        :return: -
        """

        # Create a Version object by copying the parameters from the selected version.
        new_version = self.postproc_data_object.new_postproc_version_from_existing()

        # If the new version was created from the original image (version 0), add an initial layer.
        if self.postproc_data_object.version_selected == 1:
            new_version.add_postproc_layer(PostprocLayer("Multilevel unsharp masking", 1., 1., 0.,
                                    self.configuration.postproc_bi_range_standard, 0., False))

        # Set the image viewer to the new version, and increase the range of spinboxes to include
        # the new version.
        self.set_photo_signal.emit(self.postproc_data_object.version_selected)
        self.spinBox_version.setMaximum(self.postproc_data_object.number_versions)
        self.spinBox_version.setValue(self.postproc_data_object.number_versions)
        self.spinBox_compare.setMaximum(self.postproc_data_object.number_versions)

    def remove_version(self):
        """
        Remove a postprocessing version.
        :return: -
        """

        # Remove the version from the central data object, and instruct the image viewer to
        # show the previous version.
        self.postproc_data_object.remove_postproc_version(self.postproc_data_object.version_selected)
        self.set_photo_signal.emit(self.postproc_data_object.version_selected)

        # Adjust the spinbox bounds, and set the current version parameters to the new selection.
        self.spinBox_version.setMaximum(self.postproc_data_object.number_versions)

        # Set the spinbox to the version before the deleted one.
        self.spinBox_version.setValue(self.postproc_data_object.version_selected)

        self.spinBox_compare.setMaximum(self.postproc_data_object.number_versions)
        self.spinBox_compare.setValue(
            min(self.spinBox_compare.value(), self.postproc_data_object.number_versions))
        self.select_version_signal.emit(self.postproc_data_object.version_selected)

    def blinking_toggled(self):
        """
        Switch the blink comparator on or off.

        :return: -
        """

        # Toggle the status variable.
        self.postproc_data_object.blinking = not self.postproc_data_object.blinking

        # The blink comparator is switched on.
        if self.postproc_data_object.blinking:
            # Create the blink comparator thread and start it.
            self.blink_comparator = BlinkComparator(self.postproc_data_object,
                                                    self.postproc_blinking_period,
                                                    self.set_photo_signal,
                                                    self.variant_shown_signal)
            self.blink_comparator.setTerminationEnabled(True)

        # The blink comparator is switched off. Set the image viewer to the currently selected
        # image and terminate the separate thread.
        else:
            self.set_photo_signal.emit(self.postproc_data_object.version_selected)
            self.blink_comparator.stop()

    def highlight_variant(self, selected, compare):
        """
        Whenever the blink comparator changes the display, the spinbox corressponding to the
        currently displayed image changes its font color to red. The other spinbox is blanked out
        (by setting font color to white). When the blink comparator is not active, both spinboxes
        show their numbers in bladk font.

        :param selected: True, if the image corresponding to the "selected" spinbox is displayed.
                         False, otherwise.
        :param compare: True, if the image corresponding to the "compared" spinbox is displayed.
                        False, otherwise.
        :return: -
        """

        if selected and compare:
            self.spinBox_version.setStyleSheet('color: red')
            self.spinBox_compare.setStyleSheet('color: red')
        elif not selected and not compare:
            self.spinBox_version.setStyleSheet('color: black')
            self.spinBox_compare.setStyleSheet('color: black')
        elif selected:
            self.spinBox_version.setStyleSheet('color: red')
            self.spinBox_compare.setStyleSheet('color: white')
        elif compare:
            self.spinBox_version.setStyleSheet('color: white')
            self.spinBox_compare.setStyleSheet('color: red')

    def save_version(self):
        """
        save the result as 16bit png, tiff or fits file at the standard location.

        :return: -
        """

        # In case "correction mode" is on, process the current version image in full 16bit
        # resolution.
        self.postproc_data_object.finalize_postproc_version()
        Frames.save_image(self.postproc_data_object.file_name_processed,
                          self.postproc_data_object.versions[
                              self.postproc_data_object.version_selected].image,
                          color=self.postproc_data_object.color, avoid_overwriting=False,
                          header=self.pss_version)

    def save_version_as(self):
        """
        save the result as 16bit png, tiff or fits file at a location selected by the user.

        :return: -
        """

        options = QtWidgets.QFileDialog.Options()
        filename, extension = QtWidgets.QFileDialog.getSaveFileName(self,
                            "Save result as 16bit png, tiff or fits image",
                            self.postproc_data_object.file_name_processed,
                            "Image Files (*.png *.tiff *.fits)", options=options)

        if filename and extension:
            self.postproc_data_object.finalize_postproc_version()
            Frames.save_image(filename,
                              self.postproc_data_object.versions[
                                  self.postproc_data_object.version_selected].image,
                              color=self.postproc_data_object.color, avoid_overwriting=False,
                              header=self.pss_version)


class BlinkComparator(QtCore.QThread):
    """
    This class implements the blink comparator. It shows alternately two image versions.
    """

    def __init__(self, postproc_data_object, postproc_blinking_period, set_photo_signal,
                 variant_shown_signal, parent=None):
        """
        :param postproc_data_object: The central postprocessing data object.
        :param postproc_blinking_period: Time between image switching.
        :param set_photo_signal: Signal which tells the image viewer which image it should show.
        :param variant_shown_signal: Signal which tells the GUI widget which spinbox should be
                                     highlighted.
        :param parent: Parent object
        """

        QtCore.QThread.__init__(self, parent)
        self.postproc_data_object = postproc_data_object
        self.postproc_blinking_period = postproc_blinking_period
        self.set_photo_signal = set_photo_signal
        self.variant_shown_signal = variant_shown_signal

        # Initially, no version is shown by the blink comparator.
        self.variant_shown_signal.emit(False, False)

        # Start the comparator thread.
        self.start()

    def run(self):
        """
        Blink comparator main loop. As long as the "blinking" status variable is True, show the
        two image versions alternately.

        :return: -
        """

        # Begin with showing the "selected" version.
        show_selected_version = True

        # Begin main loop.
        while self.postproc_data_object.blinking:
            # Show the "selected" version.
            if show_selected_version:
                # In case the blink comparator is invoked during manual RGB shift corrections,
                # produce a final image of the selected version before blinking.
                self.postproc_data_object.finalize_postproc_version()
                self.set_photo_signal.emit(self.postproc_data_object.version_selected)
                self.variant_shown_signal.emit(True, False)
            # Show the "compared" version.
            else:
                self.postproc_data_object.finalize_postproc_version(
                    version_index=self.postproc_data_object.version_compared)
                self.set_photo_signal.emit(self.postproc_data_object.version_compared)
                self.variant_shown_signal.emit(False, True)

            # Toggle back and forth between first and second image version.
            show_selected_version = not show_selected_version

            # Sleep time inserted to limit CPU consumption by idle looping.
            sleep(self.postproc_blinking_period)

    def stop(self):
        """
        Terminate the blink comparator thread.

        :return: -
        """

        self.variant_shown_signal.emit(False, False)
        self.terminate()


class ImageProcessor(QtCore.QThread):
    """
    This class implements the asynchronous computation of new image versions.
    """

    # The set_photo_signal tells the image viewer which image it should show.
    set_photo_signal = QtCore.pyqtSignal(int)
    # The set_shift_display_signal tells the PostprocEditorWidget to update the display of RGB
    # shift values.
    set_shift_display_signal = QtCore.pyqtSignal()
    # The set_status_bar_signal triggers the display of a (colored) message in the main GUI's
    # status bar.
    set_status_bar_signal = QtCore.pyqtSignal(str, str)
    # While computations are going on, GUI widgets are disabled to avoid race conditions.
    disable_widgets_signal = QtCore.pyqtSignal()
    # When computations have finished, reactivate GUI widgets.
    enable_widgets_signal = QtCore.pyqtSignal()

    def __init__(self, configuration, parent=None):
        """
        Whenever the parameters of the current version change, compute a new sharpened image.

        :param configuration: Data object with configuration parameters
        """

        QtCore.QThread.__init__(self, parent)
        self.configuration = configuration
        self.postproc_data_object = self.configuration.postproc_data_object
        self.postproc_idle_loop_time = self.configuration.postproc_idle_loop_time

        # Extract the file name from its path.
        self.file_name = Path(self.postproc_data_object.file_name_original).name

        self.start()

    def run(self):

        # Before executing the main loop, initialize images for all versions kept in the version
        # manager. In particular if versions have the "Auto RGB alignment" checkbox set, this may
        # take a while.

        # Change the main GUI's status bar to show that a computation is going on, and disable GUI
        # widgets to avoid race conditions.
        self.set_status_bar_signal.emit("Postprocessing " + self.file_name +
                                        ", busy computing initial images for all versions; "
                                        "This will take a while",
                                        "black")
        self.disable_widgets_signal.emit()

        # Do the computations in float32 to avoid clipping effects. If the input image is color,
        # also create a version for "luminance only" computations.
        self.image_original = self.postproc_data_object.image_original.astype(float32)
        self.color = len(self.image_original.shape) == 3
        if self.color:
            self.image_original_hsv = cvtColor(self.image_original, COLOR_BGR2HSV)

        # Initialize list of auto-rgb-aligned input images for all resolution levels.
        self.auto_rgb_aligned_images_original = [None, None, None]
        if self.color:
            self.auto_rgb_aligned_images_original_hsv = [None, None, None]
        self.auto_rgb_shifts_red = [None, None, None]
        self.auto_rgb_shifts_blue = [None, None, None]

        # Initialize images for all versions using the current layer data.
        self.last_version_layers = []

        for version in self.postproc_data_object.versions:
            # For every version, keep a copy of the current layer parameters for later checks for
            # changes. Also, remember if for this version RGB alignment was active.
            self.last_version_layers.append(deepcopy(version.layers))
            version.last_rgb_automatic = version.rgb_automatic

            # If automatic RGB alignment is selected for this version, compute the shifted
            # original image for this version's resolution if not yet available.
            if version.rgb_automatic:
                if self.auto_rgb_aligned_images_original[version.rgb_resolution_index] is None:
                    try:
                        self.auto_rgb_aligned_images_original[version.rgb_resolution_index], \
                        self.auto_rgb_shifts_red[version.rgb_resolution_index], \
                        self.auto_rgb_shifts_blue[
                            version.rgb_resolution_index] = Miscellaneous.auto_rgb_align(
                            self.image_original, self.configuration.postproc_max_shift,
                            interpolation_factor=[1, 2, 4][version.rgb_resolution_index],
                            blur_strength=version.rgb_gauss_width)
                        if self.color:
                            self.auto_rgb_aligned_images_original_hsv[
                                version.rgb_resolution_index] = cvtColor(
                                self.auto_rgb_aligned_images_original[version.rgb_resolution_index],
                                COLOR_BGR2HSV)
                    except:
                        self.auto_rgb_aligned_images_original[
                            version.rgb_resolution_index] = self.image_original
                        self.auto_rgb_shifts_red[version.rgb_resolution_index] = (0., 0.)
                        self.auto_rgb_shifts_blue[version.rgb_resolution_index] = (0., 0.)
                        if self.color:
                            self.auto_rgb_aligned_images_original_hsv[
                                version.rgb_resolution_index] = self.image_original_hsv

                version.image = Miscellaneous.post_process(
                    self.auto_rgb_aligned_images_original[version.rgb_resolution_index],
                    version.layers)
            # If shifts were set manually, apply them before sharpening the input image.
            elif version.shift_red != (0., 0.) or version.shift_blue != (0., 0.):
                # Shift the image with the resolution given by the selected interpolation factor.
                interpolation_factor = [1, 2, 4][version.rgb_resolution_index]
                shifted_image = Miscellaneous.shift_colors(self.image_original,
                                                          version.shift_red, version.shift_blue,
                                                          interpolate_input=interpolation_factor,
                                                          reduce_output=interpolation_factor)
                version.image = Miscellaneous.post_process(shifted_image, version.layers)
            # No RGB corrections were specified.
            else:
                version.image = Miscellaneous.post_process(self.image_original, version.layers)

        # Initialize lists for intermediate results (to speed up image updates). The efficient
        # reuse of intermediate results is only possible if there is enough RAM.
        self.reset_intermediate_images()
        self.ram_sufficient = True

        # Reset the status bar to its idle state, and enable GUI widgets.
        self.set_status_bar_signal.emit("Postprocessing " + self.file_name,
                                        "black")
        self.enable_widgets_signal.emit()

        # Remember the last version (and the corresponding layer parameters) shown in the image
        # viewer. As soon as this index changes, a new image is displayed.
        self.last_version_selected = 0

        # Initialize the data objects holding the currently active and the previous version.
        self.version_selected = None
        self.layers_selected = None

        # Initialize Exception state.
        exception_status = False

        # Enter the main loop and wait for parameter changes in the version selected.
        while True:

            # The whole computation is enclosed in a try/except block. If something goes wrong, e.g.
            # because during the execution of a loop pass the postproc_data_object was changed by
            # the GUI thread, the ImageProcessor tries again in the next pass.
            try:
                # To avoid chasing a moving target, copy the parameters of the currently active version
                self.version_selected = self.postproc_data_object.version_selected
                postproc_version = deepcopy(self.postproc_data_object.versions[self.version_selected])
                self.layers_selected = postproc_version.layers
                self.rgb_automatic = postproc_version.rgb_automatic
                self.rgb_resolution_index = postproc_version.rgb_resolution_index
                self.rgb_gauss_width = postproc_version.rgb_gauss_width
                self.shift_red = postproc_version.shift_red
                self.shift_blue = postproc_version.shift_blue
                self.correction_red = postproc_version.correction_red
                self.correction_blue = postproc_version.correction_blue
                self.correction_red_saved = postproc_version.correction_red_saved
                self.correction_blue_saved = postproc_version.correction_blue_saved

                compute_new_image = False
                shift_image = False

                # If the RGB auto-alignment checkbox was changed since the last image was computed for
                # this version, invalidate all intermediate results for this version.
                if self.rgb_automatic != self.postproc_data_object.versions[
                    self.version_selected].last_rgb_automatic:
                    self.postproc_data_object.versions[
                        self.version_selected].last_rgb_automatic = self.rgb_automatic
                    self.reset_intermediate_images()
                    compute_new_image = True

                # If automatic RGB alignment is on, check if the shifted image for the current
                # resolution has been computed already. Otherwise, compute it now.
                if self.rgb_automatic:
                    if self.auto_rgb_aligned_images_original[
                        self.rgb_resolution_index] is None:

                        # Change the main GUI's status bar to show that a computation is going on.
                        self.set_status_bar_signal.emit("Postprocessing " + self.file_name +
                                                        ", busy auto-aligning image", "black")
                        self.disable_widgets_signal.emit()
                        try:
                            self.auto_rgb_aligned_images_original[self.rgb_resolution_index], \
                            self.auto_rgb_shifts_red[self.rgb_resolution_index], \
                            self.auto_rgb_shifts_blue[
                                self.rgb_resolution_index] = Miscellaneous.auto_rgb_align(
                                self.image_original, self.configuration.postproc_max_shift,
                                interpolation_factor=[1, 2, 4][self.rgb_resolution_index],
                                blur_strength=self.rgb_gauss_width)
                            if self.color:
                                self.auto_rgb_aligned_images_original_hsv[
                                    self.rgb_resolution_index] = cvtColor(
                                    self.auto_rgb_aligned_images_original[self.rgb_resolution_index],
                                    COLOR_BGR2HSV)
                        except:
                            self.auto_rgb_aligned_images_original[
                                self.rgb_resolution_index] = self.image_original
                            self.auto_rgb_shifts_red[self.rgb_resolution_index] = (0., 0.)
                            self.auto_rgb_shifts_blue[self.rgb_resolution_index] = (0., 0.)
                            if self.color:
                                self.auto_rgb_aligned_images_original_hsv[
                                    self.rgb_resolution_index] = self.image_original_hsv
                        # Reset the status bar to its idle state.
                        self.set_status_bar_signal.emit("Postprocessing " + self.file_name, "black")
                        self.enable_widgets_signal.emit()

                        # Since the auto-shifted image is new, the postprocessing pipeline must be
                        # applied. (This does not seem to be necessary.)
                        # compute_new_image = True

                    # Set the processing pipeline input to the RGB aligned original image.
                    self.input_image = self.auto_rgb_aligned_images_original[self.rgb_resolution_index]
                    if self.color:
                        self.input_image_hsv = self.auto_rgb_aligned_images_original_hsv[
                                self.rgb_resolution_index]

                    # Set the RGB shifts for this version to the values computed automatically.
                    self.postproc_data_object.versions[self.version_selected].shift_red = \
                        self.auto_rgb_shifts_red[self.rgb_resolution_index]
                    self.postproc_data_object.versions[self.version_selected].shift_blue = \
                        self.auto_rgb_shifts_blue[self.rgb_resolution_index]

                # In correction mode the wavelets are computed only once, and then only the shift
                # corrections are applied.
                elif postproc_version.rgb_correction_mode:

                    # If the uncorrected image for this resolution index is not available, it must be
                    # computed. Try if an image for this version has been computed. If not, this version
                    # is new. In that case process the wavelets first (only applying the accumulated
                    # shifts), and do the shift corrections in the next pass.
                    if postproc_version.images_uncorrected[self.rgb_resolution_index] is None:

                        # Change the main GUI's status bar to show that correction mode is being
                        # initialized.
                        self.set_status_bar_signal.emit("Postprocessing " + self.file_name +
                                                        ", initializing manual alignment", "black")
                        self.disable_widgets_signal.emit()

                        if postproc_version.image is None or postproc_version.image.dtype == uint8:
                            self.input_image = Miscellaneous.shift_colors(self.image_original,
                                                                          postproc_version.shift_red,
                                                                          postproc_version.shift_blue)
                            if self.color:
                                if postproc_version.shift_red != (
                                0., 0.) or postproc_version.shift_blue != (0., 0.):
                                    self.input_image_hsv = cvtColor(self.input_image, COLOR_BGR2HSV)
                                else:
                                    self.input_image_hsv = self.image_original_hsv

                            self.reset_intermediate_images()
                            compute_new_image = True

                        # The postprocessing pipeline has been applied to this version before, set the
                        # input for this resolution index of the correction mode. The image is
                        # blown up by the resolution factor to show shifts in detail.
                        else:
                            # In the first pass, convert the input image to 8bit unsigned int (to speed
                            # up the image viewer). Store it as resolution version 0 (1 Pixel, i.e. no
                            # change in resolution).
                            if self.postproc_data_object.versions[
                                self.version_selected].images_uncorrected[0] is None:
                                self.postproc_data_object.versions[
                                    self.version_selected].images_uncorrected[0] = (
                                        postproc_version.image / 256.).astype(
                                    uint8)

                            # If the resolution selected is not 1 pixel, interpolate the 1 pixel image
                            # to the required resolution and store the result for later reuse.
                            if self.rgb_resolution_index > 0:
                                self.postproc_data_object.versions[
                                    self.version_selected].images_uncorrected[
                                    self.rgb_resolution_index] = \
                                    Miscellaneous.shift_colors(self.postproc_data_object.versions[
                                        self.version_selected].images_uncorrected[0], (0., 0.), (0., 0.),
                                        interpolate_input=[1, 2, 4][self.rgb_resolution_index])
                            shift_image = True

                        # Reset the status bar to its idle state.
                        self.set_status_bar_signal.emit("Postprocessing " + self.file_name, "black")
                        self.enable_widgets_signal.emit()

                    # The image without shift correction is available for the required resolution. Check
                    # if the shift correction has changed. If so, apply the correction. Otherwise, leave
                    # the image unchanged.
                    elif self.correction_red != self.correction_red_saved or self.correction_blue != \
                            self.correction_blue_saved:
                        self.postproc_data_object.versions[
                            self.version_selected].correction_red_saved = self.correction_red
                        self.postproc_data_object.versions[
                            self.version_selected].correction_blue_saved = self.correction_blue
                        shift_image = True

                # Wavelet mode, RGB auto-alignment off: Set the input image for the processing pipeline
                # to the original image, shifted by the accumulated shift vectors (not in correction
                # mode).
                else:
                    # The accumulated shift vectors have changed since the last computation. Apply the
                    # new shifts to the original image.
                    if postproc_version.shift_red != postproc_version.shift_red_saved or \
                        postproc_version.shift_blue != postproc_version.shift_blue_saved:
                        self.input_image = Miscellaneous.shift_colors(self.image_original,
                                                                      postproc_version.shift_red,
                                                                      postproc_version.shift_blue)
                        if self.color:
                            if postproc_version.shift_red != (
                                    0., 0.) or postproc_version.shift_blue != (0., 0.):
                                self.input_image_hsv = cvtColor(self.input_image, COLOR_BGR2HSV)
                            else:
                                self.input_image_hsv = self.image_original_hsv

                        # Save the new shifted original image together with the current accumulated
                        # shift vectors.
                        self.postproc_data_object.versions[self.version_selected].input_image_saved = \
                            self.input_image
                        if self.color:
                            self.postproc_data_object.versions[
                                self.version_selected].input_image_hsv_saved = self.input_image_hsv
                        self.postproc_data_object.versions[self.version_selected].shift_red_saved = \
                            postproc_version.shift_red
                        self.postproc_data_object.versions[
                            self.version_selected].shift_blue_saved = postproc_version.shift_blue

                        # Since the processing pipeline input has changed, reset intermediate results
                        # and set the computation flag.
                        self.reset_intermediate_images()
                        compute_new_image = True

                    # The accumulated shift vectors have not changed. Reuse the saved pipeline input
                    # images.
                    else:
                        self.input_image = postproc_version.input_image_saved
                        if self.color:
                            self.input_image_hsv = postproc_version.input_image_hsv_saved

                self.set_shift_display_signal.emit()

                # End of preparatory phase. Flags "compute_new_image" or "shift_image" have been set.
                # Now perform the appropriate action.

                # The image has already passed the postprocessing pipeline (as contained in
                # postproc_version.images_uncorrected). Only the correction shift must be applied.
                if shift_image:
                    interpolation_factor = [1, 2, 4][self.rgb_resolution_index]
                    self.postproc_data_object.versions[self.version_selected].image = \
                        Miscellaneous.shift_colors(
                            self.postproc_data_object.versions[
                                self.version_selected].images_uncorrected[self.rgb_resolution_index],
                            (self.correction_red[0] * interpolation_factor,
                             self.correction_red[1] * interpolation_factor),
                            (self.correction_blue[0] * interpolation_factor,
                             self.correction_blue[1] * interpolation_factor))
                    # Show the new image in the image viewer, and remember its parameters.
                    self.set_photo_signal.emit(self.version_selected)
                    self.last_version_selected = self.version_selected

                # A new image must be computed for this version. Special case self.version_selected = 0:
                # For the original image no layers are applied. But if the RGB automatic checkbox was
                # changed, the image must be set according to the new shift status. The shift was
                # applied in self.input_image above.
                elif compute_new_image and not self.version_selected:
                    self.postproc_data_object.versions[0].image = self.input_image.astype(uint16)
                    # Show the new image in the image viewer, and remember its parameters.
                    self.set_photo_signal.emit(self.version_selected)
                    self.last_version_selected = self.version_selected

                # General case for new image computation: Either it was decided above that a new
                # computation is required, or the test "new_computation_required" for changes in the
                # correction layers is performed.
                elif compute_new_image or self.new_computation_required(
                        self.version_selected != self.last_version_selected):
                    # Change the main GUI's status bar to show that a computation is going on.
                    self.set_status_bar_signal.emit("Postprocessing " + self.file_name +
                                                    ", busy computing a new image.", "black")

                    # Perform the new computation. Try to store intermediate results. If it fails
                    # because there is not enough RAM, switch to direct computation.
                    if self.ram_sufficient:
                        try:
                            self.postproc_data_object.versions[
                                self.version_selected].image = self.recompute_selected_version(
                                                                self.layers_selected)
                        except:
                            self.ram_sufficient = False
                            self.postproc_data_object.versions[
                                self.version_selected].image = Miscellaneous.post_process(
                                                            self.input_image, self.layers_selected)
                    else:
                        self.postproc_data_object.versions[
                            self.version_selected].image = Miscellaneous.post_process(
                            self.input_image, self.layers_selected)

                    # Reset the status bar to its idle state.
                    self.set_status_bar_signal.emit("Postprocessing " + self.file_name, "black")

                    # Show the new image in the image viewer, and remember its parameters.
                    self.set_photo_signal.emit(self.version_selected)
                    self.last_version_selected = self.version_selected

                # Neither the shift nor parameters have changed. If the version selection has changed,
                # display the image stored with the version selected.
                elif self.version_selected != self.last_version_selected:
                    # Show the new image in the image viewer, and remember its parameters.
                    self.set_photo_signal.emit(self.version_selected)
                    self.last_version_selected = self.version_selected

                # Idle loop before doing the next check for updates.
                else:
                    sleep(self.postproc_idle_loop_time)

                # Reset any exception condition after a successful loop pass.
                exception_status = False

            except Exception as e:
                # Print the error message only once. The error status is reset after the next
                # successful loop pass.
                if not exception_status:
                    print ("Exception in ImageProcessor: " + str(e) + ". Will try again.",
                           file=stderr)
                    exception_status = True
                sleep(self.postproc_idle_loop_time)

    def reset_intermediate_images(self):
        """
        Initialize all intermediate image versions, so that they will be re-computed next time.

        :return: -
        """

        self.layer_input = [None] * (self.configuration.postproc_max_layers + 1)
        self.layer_gauss = [None] * (self.configuration.postproc_max_layers)
        self.layer_bilateral = [None] * (self.configuration.postproc_max_layers)
        self.layer_denoised = [None] * (self.configuration.postproc_max_layers)

    def new_computation_required(self, version_has_changed):
        """
        Decide if a new computation is required. Invalidate only those intermediate image
        components which are affected by the parameter change. This results in a substantial
        performance improvement.

        :param version_has_changed: True, if the version has changed, so all intermediate results
                                    must be invalidated.
        :return: True, if the current version parameters have changed. False, otherwise.
        """

        # Initialize the flag which remembers if a change was found. Do not return on the first
        # detection in order to invalidate all affected intermediate results.
        change_detected = False

        # If a new version is selected, reset images with intermediate results.
        if version_has_changed:
            self.reset_intermediate_images()
            change_detected = True

        # Check if an additional version was created. Copy its layer info for later checks
        # for changes. The intermediate results are reset already because the version number must
        # have changed.
        if self.version_selected >= len(self.last_version_layers):
            self.last_version_layers.append(deepcopy(self.postproc_data_object.versions[
                                                         self.version_selected].layers))
            return change_detected

        # If the selected version is already known, check if the number of layers has changed.
        if len(self.last_version_layers[self.version_selected]) != len(self.layers_selected):
            self.last_version_layers[self.version_selected] = deepcopy(self.layers_selected)
            self.reset_intermediate_images()
            change_detected = True
            return change_detected

        # For color images, check if the "luminance only" parameter has changed. In this case all
        # layers are invalidated completely. Remember that the "luminance only" parameter is the
        # same on all layers for any given version, and that for version 0 (original image) no
        # layers are defined (and therefore there is no parameter "luminance_only").
        if self.color and self.version_selected and \
                self.last_version_layers[self.version_selected][0].luminance_only != \
                self.layers_selected[0].luminance_only:
            self.reset_intermediate_images()
            change_detected = True

        # The version is the same as before, it has the same number of layers, and the luminance
        # parameter has not changed. Now go through all layers and invalidate those intermediate
        # results individually which need to be recomputed.
        else:
            for layer_index, (last_layer, layer_selected) in enumerate(
                    zip(self.last_version_layers[self.version_selected], self.layers_selected)):
                # The "amount" parameter has changed. No intermediate results need to be invalidated
                # because the layer's contribution to the image is computed by multiplying the
                # denoised component with the amount parameter.
                if last_layer.amount != layer_selected.amount:
                    last_layer.amount = layer_selected.amount
                    change_detected = True

                # If the radius has changed, all filters at this level and the input for the next
                # level have to be recomputed.
                if  last_layer.radius != layer_selected.radius:
                    self.layer_gauss[layer_index] = self.layer_bilateral[layer_index] = \
                        self.layer_denoised[layer_index] = self.layer_input[layer_index + 1] = None
                    change_detected = True

                # If the bi_fraction has changed, the input for the next layer and the denoised
                # component on this layer must be recomputed.
                if last_layer.bi_fraction != layer_selected.bi_fraction:
                    self.layer_denoised[layer_index] = self.layer_input[layer_index + 1] = None
                    change_detected = True

                # If the bi_range parameter has changed, the bilateral filter is invalidated.
                if last_layer.bi_range != layer_selected.bi_range:
                    self.layer_bilateral[layer_index] = None
                    change_detected = True
                    # If the bi_fraction parameter is not zero, the change propagates to the
                    # denoised component and the input for the next level.
                    if abs(layer_selected.bi_fraction) > 1.e-5:
                        self.layer_denoised[layer_index] = self.layer_input[layer_index + 1] = None

                # If the denoise parameter has changed, the denoised component for this layer is
                # invalidated.
                if last_layer.denoise != layer_selected.denoise:
                    self.layer_denoised[layer_index] = None
                    change_detected = True

        # Remember the current parameter settings to compare with new parameters next time.
        self.last_version_layers[self.version_selected] = deepcopy(self.layers_selected)
        return change_detected

    def recompute_selected_version(self, layers):
        """
        Do the actual computation. Starting from the original image, for each sharpening layer
        apply a Gaussian filter using the layer's parameters. Store the result in the central
        data object for the selected version.

        :return: -
        """

        # Check if the original image is selected (version 0). In this case return the (potentially
        # RGB-shifted) original iamge.
        if not layers:
            return self.input_image.astype(uint16)

        for layer_index, layer in enumerate(layers):
            if self.layer_input[layer_index] is None:
                # For layers > 0, the layer_input must have been computed on the previous layer.
                if layer_index:
                    raise InternalError("Layer input image is None for layer > 0")
                # On layer 0, the original image is taken as layer input.
                else:
                    # For color input and luminance_only: extract the luminance channel.
                    if self.color and layer.luminance_only:
                        self.layer_input[layer_index] = self.input_image_hsv[:, :, 2]
                    else:
                        self.layer_input[layer_index] = self.input_image

            # Bilateral filter is needed:
            if abs(layer.bi_fraction) > 1.e-5:
                # Filter must be recomputed.
                if self.layer_bilateral[layer_index] is None:
                    self.layer_bilateral[layer_index] = bilateralFilter(self.layer_input[layer_index],
                        0, layer.bi_range * 256., layer.radius/3., borderType=BORDER_DEFAULT)

            # Gaussian filter is needed:
            if abs(layer.bi_fraction - 1.) > 1.e-5:
                # Filter must be recomputed.
                if self.layer_gauss[layer_index] is None:
                    self.layer_gauss[layer_index] = GaussianBlur(self.layer_input[layer_index], (0, 0),
                        layer.radius/3., borderType=BORDER_DEFAULT)

            # Compute the input image for the next layer.
            if self.layer_input[layer_index + 1] is None:
                # Case bilateral only.
                if abs(layer.bi_fraction - 1.) <= 1.e-5:
                    self.layer_input[layer_index + 1] = self.layer_bilateral[layer_index]
                # Case Gaussian only.
                elif abs(layer.bi_fraction) <= 1.e-5:
                    self.layer_input[layer_index + 1] = self.layer_gauss[layer_index]
                # Mixed case.
                else:
                    self.layer_input[layer_index + 1] = self.layer_bilateral[layer_index] * layer.bi_fraction + \
                                                        self.layer_gauss[layer_index] * (1. - layer.bi_fraction)

            # A new denoised layer component must be computed.
            if self.layer_denoised[layer_index] is None:
                layer_component_before_denoise = self.layer_input[layer_index] - self.layer_input[layer_index + 1]
                # Denoising must be applied.
                if layer.denoise > 1.e-5:
                    self.layer_denoised[layer_index] = GaussianBlur(layer_component_before_denoise, (0, 0),
                        layer.radius / 3., borderType=BORDER_DEFAULT) * layer.denoise + \
                        layer_component_before_denoise * (1. - layer.denoise)
                else:
                    self.layer_denoised[layer_index] = layer_component_before_denoise

        # Build the sharpened image as a weighted sum of the layer components. Start with the
        # maximally blurred layer input (start image of first layer beyond active layers).
        # A weight > 1 increases details at this level, a weight < 1 lowers their visibility.
        new_image = copy(self.layer_input[len(layers)])
        for layer, image_layer_component in zip(layers, self.layer_denoised):
            new_image += image_layer_component * layer.amount

        # In case of "luminance only", insert the new luminance channel into a copy of the original
        # image and change back to BGR.
        if self.color and layers[0].luminance_only:
            new_image_bgr = copy(self.input_image_hsv)
            new_image_bgr[:, :, 2] = new_image
            new_image = cvtColor(new_image_bgr, COLOR_HSV2BGR)

        # Clip pixels out of range and convert the processed image to 16bit unsigned int.
        return new_image.clip(min=0., max=65535.).astype(uint16)

    def stop(self):
        """
        Terminate the ImageProcessor.
        :return: -
        """

        self.terminate()


class PostprocEditorWidget(QtWidgets.QFrame, Ui_postproc_editor):
    """
    This widget implements a frame viewer together with control elements to control the
    postprocessing. Several postprocessing versions can be created and managed, each one using
    a variable number of sharpening layers. A blink comparator allows comparing any two versions.
    """

    def __init__(self, configuration, image_original, name_original, set_status_bar_callback,
                 signal_save_postprocessed_image):
        """

        :param configuration: Configuration object with parameters.
        :param image_original: Original image (16bit) holding the input for postprocessing.
        :param name_original: Path name of the original image.
        :param set_status_bar_callback: Call-back function to update the main GUI's status bar.
        :param signal_save_postprocessed_image: Signal to be issued when the postprocessing
                                                widget closes. None if no signal is to be issued.
        """

        super(PostprocEditorWidget, self).__init__()
        self.setupUi(self)

        self.configuration = configuration
        self.postproc_data_object = self.configuration.postproc_data_object
        self.postproc_data_object.set_postproc_input_image(image_original, name_original,
                                                self.configuration.global_parameters_image_format)
        self.signal_save_postprocessed_image = signal_save_postprocessed_image

        self.tabWidget_postproc_control.currentChanged.connect(self.tab_changed)

        self.buttonBox.accepted.connect(self.accept)
        self.buttonBox.rejected.connect(self.reject)
        self.pushButton_add_layer.clicked.connect(self.add_layer)

        rgb_resolutions = ['     1 Pixel ', '   0.5 Pixels', '  0.25 Pixels']
        self.max_rgb_index = self.max_auto_rgb_index()
        for resolution_index in range(self.max_rgb_index + 1):
            self.comboBox_resolution.addItem(rgb_resolutions[resolution_index])

        self.fgw_slider_value.valueChanged['int'].connect(self.fgw_changed)
        self.comboBox_resolution.currentIndexChanged.connect(self.rgb_resolution_changed)
        self.checkBox_automatic.stateChanged.connect(self.rgb_automatic_changed)
        self.pushButton_red_reset.clicked.connect(self.prreset_clicked)
        self.pushButton_red_up.clicked.connect(self.pru_clicked)
        self.pushButton_red_down.clicked.connect(self.prd_clicked)
        self.pushButton_red_left.clicked.connect(self.prl_clicked)
        self.pushButton_red_right.clicked.connect(self.prr_clicked)
        self.pushButton_blue_reset.clicked.connect(self.pbreset_clicked)
        self.pushButton_blue_up.clicked.connect(self.pbu_clicked)
        self.pushButton_blue_down.clicked.connect(self.pbd_clicked)
        self.pushButton_blue_left.clicked.connect(self.pbl_clicked)
        self.pushButton_blue_right.clicked.connect(self.pbr_clicked)

        # Enable RGB alignment for color input only.
        self.tab_rgb.setEnabled(self.postproc_data_object.color)

        # Initialize list of sharpening layer widgets, and set the maximal number of layers.
        self.sharpening_layer_widgets = []
        self.max_layers = self.configuration.postproc_max_layers

        self.label_message.setText("Edit postproc versions with up to "
                                   + str(self.max_layers) + " correction layers.")
        self.label_message.setStyleSheet('color: red')

        # Start the frame viewer.
        self.frame_viewer = FrameViewer()
        self.frame_viewer.setObjectName("framewiever")
        self.gridLayout.addWidget(self.frame_viewer, 0, 0, 2, 2)
        self.frame_viewer.zoom_factor_signal.connect(self.display_zoom_factor)

        # Initialize a vertical spacer used to fill the lower part of the sharpening widget scroll
        # area.
        self.spacerItem = QtWidgets.QSpacerItem(20, 40, QtWidgets.QSizePolicy.Minimum,
                                            QtWidgets.QSizePolicy.Expanding)

        # Set the resolution index to an impossible value. It is used to check for changes.
        self.rgb_resolution_index = -1

        # Set the vertical image size (in pixels) to an impossible value. It is used to check for
        # changes later. When the pixel resolution of the displayed image changes, the "fit in view"
        # method of the image viewer is invoked to normalize the appearance of the image.
        self.image_size_y = -1

        # Create the version manager.
        self.selected_version = None
        self.version_manager_widget = VersionManagerWidget(self.configuration)
        self.gridLayout.addWidget(self.version_manager_widget, 1, 2, 1, 1)

        # The "set_photo_signal" from the VersionManagerWidget is not passed to the image viewer
        # directly. (The image viewer does not accept signals.) Instead, it sends a signal to this
        # object which in turn invokes the image viewer.
        self.version_manager_widget.set_photo_signal.connect(self.select_image)
        self.version_manager_widget.select_version_signal.connect(self.select_version)

        # Select the initial current version.
        self.select_version(self.postproc_data_object.version_selected)

        # Start the image processor thread, and connect its signals.
        self.image_processor = ImageProcessor(self.configuration)
        self.image_processor.setTerminationEnabled(True)
        self.image_processor.set_photo_signal.connect(self.select_image)
        self.image_processor.set_shift_display_signal.connect(self.display_shifts)
        self.image_processor.set_status_bar_signal.connect(set_status_bar_callback)
        self.image_processor.disable_widgets_signal.connect(self.disable_widgets)
        self.image_processor.enable_widgets_signal.connect(self.enable_widgets)

        if self.max_rgb_index == -1:
            self.checkBox_automatic.setChecked(False)
            self.tab_rgb.setEnabled(False)

        for version in self.postproc_data_object.versions:
            if self.max_rgb_index == -1:
                version.rgb_automatic = False
            version.rgb_resolution_index = min(version.rgb_resolution_index, max(self.max_rgb_index, 0))

    def max_auto_rgb_index(self):
        """
        Return the maximal auto RGB alignment index such that the image created is not larger than
        10% of the available RAM. This makes sure that the postprocessing does not run out of RAM.

        :return: Maximal RGB alignment index (0, 1, or 2). If the image is too large to do any
                 auto RGB alignment, -1 is returned.
        """

        level = 2
        max_index = 2
        factors = [1, 2, 4]

        # Look up the available RAM (without paging)
        virtual_memory = dict(psutil.virtual_memory()._asdict())
        available_ram = virtual_memory['available']

        # For monochrome images, nothing is to be done.
        if self.postproc_data_object.color:
            image = self.postproc_data_object.image_original

            # The intermediate image is in float32 (4 bytes) and has 3 color channels.
            size_of_image = image.shape[0] * image.shape[1] * 12.

            # Compute the maximal interpolation factor.
            max_factor = int(sqrt(
                available_ram * self.configuration.postproc_max_ram_percentage / 100. / size_of_image))

            # In this case auto RGB alignment does not even work for standard size.
            if max_factor == 0:
                return -1

            # Look for the maximal interpolation index.
            for level in range(max_index, -1, -1):
                if factors[level] <= max_factor:
                    break

        return level

    def disable_widgets(self):
        """
        To avoid race conditions, GUI widgets are disabled while computations are going on. The only
        button remaining active is "Cancel".

        :return: -
        """

        self.version_manager_widget.setEnabled(False)
        self.tabWidget_postproc_control.setEnabled(False)
        self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(False)

    def enable_widgets(self):
        """
        Enable the GUI buttons when background computations have finished.

        :return: -
        """

        self.version_manager_widget.setEnabled(True)
        self.tabWidget_postproc_control.setEnabled(True)
        self.buttonBox.button(QtWidgets.QDialogButtonBox.Ok).setEnabled(True)

    def fgw_changed(self, value):
        """
        When the widget changes its value, update the corresponding entry for the current version.
        Please note that for some parameters the representations differ.

        The methods following this one do the same for all other configuration parameters.

        :param value: New value sent by widget
        :return: -
        """

        gauss_width = 2 * value - 1
        self.postproc_data_object.versions[self.postproc_data_object.version_selected].rgb_gauss_width = gauss_width
        self.fgw_label_display.setText(str(gauss_width))

    def rgb_resolution_changed(self, value):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        version.rgb_resolution_index = value

        # Round the shift values according to the new resolution.
        factor = [1., 2., 4.][value]
        factor_incremented = [1.01, 2.01, 4.01][value]
        version.shift_red = (round(version.shift_red[0] * factor_incremented) / factor ,
                             round(version.shift_red[1] * factor_incremented) / factor)
        version.shift_blue = (round(version.shift_blue[0] * factor_incremented) / factor,
                              round(version.shift_blue[1] * factor_incremented) / factor)
        version.correction_red = (round(version.correction_red[0] * factor_incremented) / factor,
                             round(version.correction_red[1] * factor_incremented) / factor)
        version.correction_blue = (round(version.correction_blue[0] * factor_incremented) / factor,
                              round(version.correction_blue[1] * factor_incremented) / factor)
        self.display_shifts()

    def tab_changed(self, index):
        version = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected]
        if index and not version.rgb_automatic:
            self.rgb_correction_version_init(version)
        elif not index:
            # Force the computation of a new image in the ImageProcessor.
            version.shift_red_saved = None
            version.shift_blue_saved = None
            self.finish_rgb_correction_mode()

    def rgb_automatic_changed(self, state):
        rgb_on = state == QtCore.Qt.Checked
        version = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected]
        version.rgb_automatic = rgb_on
        if rgb_on:
            self.rgb_correction_version_reset(version)
        else:
            self.rgb_correction_version_init(version)

    def rgb_correction_version_init(self, version):
        version.rgb_correction_mode = True
        version.correction_red_saved = version.correction_blue_saved = version.correction_red = \
            version.correction_blue = (0., 0.)
        version.images_uncorrected = [None] * 3

    def rgb_correction_version_reset(self, version):
        version.rgb_correction_mode = False
        version.correction_red_saved = version.correction_blue_saved = version.correction_red =\
            version.correction_blue = (0., 0.)
        version.images_uncorrected = [None] * 3

    def finish_rgb_correction_mode(self):
        for version_index, version in enumerate(self.postproc_data_object.versions):
            if version.correction_red != (0., 0.) or version.correction_blue != (0., 0.):
                version.shift_red = (version.shift_red[0] + version.correction_red[0],
                                     version.shift_red[1] + version.correction_red[1])
                version.shift_blue = (version.shift_blue[0] + version.correction_blue[0],
                                      version.shift_blue[1] + version.correction_blue[1])
            self.rgb_correction_version_reset(version)
        self.select_image(self.postproc_data_object.version_selected)

    def prreset_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        version.correction_red = (-version.shift_red[0], -version.shift_red[1])
        self.display_shifts()

    def pru_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_red = (version.correction_red[0] - increment, version.correction_red[1])
        self.display_shifts()

    def prd_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_red = (version.correction_red[0] + increment, version.correction_red[1])
        self.display_shifts()

    def prl_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_red = (version.correction_red[0], version.correction_red[1] - increment)
        self.display_shifts()

    def prr_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_red = (version.correction_red[0], version.correction_red[1] + increment)
        self.display_shifts()

    def pbreset_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        version.correction_blue = (-version.shift_blue[0], -version.shift_blue[1])
        self.display_shifts()

    def pbu_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_blue = (version.correction_blue[0] - increment, version.correction_blue[1])
        self.display_shifts()

    def pbd_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_blue = (
        version.correction_blue[0] + increment, version.correction_blue[1])
        self.display_shifts()

    def pbl_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_blue = (version.correction_blue[0], version.correction_blue[1] - increment)
        self.display_shifts()

    def pbr_clicked(self):
        version = self.postproc_data_object.versions[self.postproc_data_object.version_selected]
        increment = [1., 0.5, 0.25][version.rgb_resolution_index]
        version.correction_blue = (
        version.correction_blue[0], version.correction_blue[1] + increment)
        self.display_shifts()

    def display_shifts(self):
        """
        Set the current channel shifts in the RGB alignment GUI tab.

        :return: -
        """

        format_string = ["{0:2.0f}", "{0:4.1f}", "{0:5.2f}"][self.postproc_data_object.versions[
            self.postproc_data_object.version_selected].rgb_resolution_index]

        # Red channel shifts:
        shift_red_base = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected].shift_red
        correction_red = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected].correction_red
        shift_red = (shift_red_base[0] + correction_red[0], shift_red_base[1] + correction_red[1])

        if abs(shift_red[0]) < 0.05:
            self.label_red_down.setText("")
            self.label_red_up.setText("")
        elif shift_red[0] > 0:
            self.label_red_down.setText((format_string.format(shift_red[0])))
            self.label_red_up.setText("")
        else:
            self.label_red_up.setText((format_string.format(-shift_red[0])))
            self.label_red_down.setText("")

        if abs(shift_red[1]) < 0.05:
            self.label_red_left.setText("")
            self.label_red_right.setText("")
        elif shift_red[1] > 0:
            self.label_red_right.setText((format_string.format(shift_red[1])))
            self.label_red_left.setText("")
        else:
            self.label_red_left.setText((format_string.format(-shift_red[1])))
            self.label_red_right.setText("")

        # Blue channel shifts:
        shift_blue_base = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected].shift_blue
        correction_blue = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected].correction_blue
        shift_blue = (shift_blue_base[0] + correction_blue[0], shift_blue_base[1] + correction_blue[1])

        if abs(shift_blue[0]) < 0.05:
            self.label_blue_down.setText("")
            self.label_blue_up.setText("")
        elif shift_blue[0] > 0:
            self.label_blue_down.setText((format_string.format(shift_blue[0])))
            self.label_blue_up.setText("")
        else:
            self.label_blue_up.setText((format_string.format(-shift_blue[0])))
            self.label_blue_down.setText("")

        if abs(shift_blue[1]) < 0.05:
            self.label_blue_left.setText("")
            self.label_blue_right.setText("")
        elif shift_blue[1] > 0:
            self.label_blue_right.setText((format_string.format(shift_blue[1])))
            self.label_blue_left.setText("")
        else:
            self.label_blue_left.setText((format_string.format(-shift_blue[1])))
            self.label_blue_right.setText("")

    def display_zoom_factor(self, zoom_factor):
        """
        Write the current viewer zoom factor under the viewer window.

        :param zoom_factor: Current zoom factor, sent by the FrameViewer via a signal.

        :return: -
        """

        self.label_zoom.setText("Zoom: " + str(zoom_factor) + "%")

    def select_version(self, version_index):
        """
        Select a new current version, update the scroll area with all layer widgets, and update the
        parameters of the RGB alignment tab.

        :param version_index: Index of the version selected in version list of the central data
                              object.
        :return: -
        """

        # Check if we are leaving a version in correction mode. In this case apply the corrections.
        if self.selected_version is not None:
            if self.selected_version.rgb_correction_mode:
                self.finish_rgb_correction_mode()

        self.selected_version = self.postproc_data_object.versions[version_index]

        # Remove all existing layer widgets and the lower vertical spacer.
        if self.sharpening_layer_widgets:
            for layer_widget in self.sharpening_layer_widgets:
                self.verticalLayout.removeWidget(layer_widget)
                layer_widget.deleteLater()
                del layer_widget
        self.sharpening_layer_widgets = []
        self.verticalLayout.removeItem(self.spacerItem)

        # Create new layer widgets for all active layers and put them into the scroll area.
        for layer_index, layer in enumerate(self.selected_version.layers):
            sharpening_layer_widget = SharpeningLayerWidget(layer_index, self.remove_layer)
            sharpening_layer_widget.set_values(layer)
            self.verticalLayout.addWidget(sharpening_layer_widget)
            self.sharpening_layer_widgets.append(sharpening_layer_widget)

        # Connect the signal issued by a sharpening layer when the "luminance only" checkbox changes
        # state with all other layers, so they do the same.
        for layer_index, sharpening_layer_widget in enumerate(self.sharpening_layer_widgets):
            toggle_luminance_signal = sharpening_layer_widget.signal_toggle_luminance_other_layers
            for other_layer_index, other_sharpening_layer_widget in enumerate(
                    self.sharpening_layer_widgets):
                if layer_index != other_layer_index:
                    toggle_luminance_signal.connect(
                        other_sharpening_layer_widget.checkBox_luminance_toggled_no_signal)

        # At the end of the scroll area, add a vertical spacer.
        self.verticalLayout.addItem(self.spacerItem)

        # Load the parameters of this version into the RGB alignment tab. Block signals to avoid
        # triggering change actions which are not required at this point.
        self.checkBox_automatic.blockSignals(True)
        self.comboBox_resolution.blockSignals(True)
        self.checkBox_automatic.setChecked(self.selected_version.rgb_automatic)
        self.comboBox_resolution.setCurrentIndex(min(self.selected_version.rgb_resolution_index,
                                                     self.max_rgb_index))
        self.checkBox_automatic.blockSignals(False)
        self.comboBox_resolution.blockSignals(False)
        self.fgw_slider_value.setValue(int((self.selected_version.rgb_gauss_width + 1) / 2))
        self.fgw_label_display.setText(str(self.selected_version.rgb_gauss_width))

        # Only if the RGB tab is selected: Enter / leave RGB correction mode.
        if self.tabWidget_postproc_control.currentIndex():
            if self.selected_version.rgb_automatic:
                self.rgb_correction_version_reset(self.selected_version)
            else:
                self.rgb_correction_version_init(self.selected_version)

        # Load the current image into the image viewer.
        self.select_image(version_index)

        # Display the current RGB shifts for the selected version.
        self.display_shifts()

    def select_image(self, version_index):
        """
        Load the current image into the image viewer. This method is either invoked by a direct
        call or by a "set_photo_signal" signal.

        :param version_index: Index of the version selected in version list of the central data
                              object.
        :return: -
        """

        image = self.postproc_data_object.versions[version_index].image
        self.frame_viewer.setPhoto(image)

        # Check if the image size has changed by more than 10%. If so, reset the zoom factor of the
        # FrameViewer.
        # print ("Select image")
        if abs((image.shape[0] - self.image_size_y)/ image.shape[0]) > 0.1:
            # print("Fit in view")
            self.frame_viewer.fitInView()
            self.image_size_y = image.shape[0]

    def add_layer(self):
        """
        This method is called when the "Add postprocessing layer" button is pressed. Add a layer
        to the currently selected postprocessing version.

        :return:-
        """

        version_selected = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected]
        num_layers_current = version_selected.number_layers

        # The "zero" layer is reserved for the original image, so for this version no additional
        # layer can be allocated.

        # The first layer is initialized with a fixed parameter set (see "init" method of DataObject
        # class).
        if self.postproc_data_object.version_selected and num_layers_current < self.max_layers:

            # This version has already at least one layer. Initialize the radius parameter of the
            # new layer to 1.5 times the radius of the previous one.
            if num_layers_current:
                previous_layer = version_selected.layers[num_layers_current - 1]
                new_layer = PostprocLayer(previous_layer.postproc_method,
                                          round(1.5 * previous_layer.radius, 1), 1.,
                                          previous_layer.bi_fraction,
                                          previous_layer.bi_range, 0.,
                                          previous_layer.luminance_only)

            # This is the first layer for this image version. Start with standard parameters.
            else:
                new_layer = PostprocLayer("Multilevel unsharp masking", 1., 1., 0., 20, 0., False)
            version_selected.add_postproc_layer(new_layer)

            # Update all layer widgets.
            self.select_version(self.postproc_data_object.version_selected)

    def remove_layer(self, layer_index):
        """
        Remove a layer from the selected version. The "remove_layer" method of class "Layer" makes
        sure that the first layer is not removed.

        :param layer_index: Index of the layer to be removed.
        :return: -
        """

        version_selected = self.postproc_data_object.versions[
            self.postproc_data_object.version_selected]
        version_selected.remove_postproc_layer(layer_index)

        # Update all layer widgets.
        self.select_version(self.postproc_data_object.version_selected)

    def accept(self):
        """
        When the user presses the "OK" button, save the currently selected version to the standard
        path and exit.

        :return: -
        """

        # In case "correction mode" is on, process the current version image in full 16bit
        # resolution.
        self.disable_widgets()
        self.finish_rgb_correction_mode()
        self.postproc_data_object.finalize_postproc_version()

        # Terminate the image processor thread.
        self.image_processor.stop()
        self.configuration.write_config()
        if self.signal_save_postprocessed_image:
            self.signal_save_postprocessed_image.emit(self.postproc_data_object.versions[
                self.postproc_data_object.version_selected].image)
        # The else branch is only for the test program below which does not provide a signal for
        # saving the result. In this case use the save method of the version manager.
        else:
            self.version_manager_widget.save_version()
        self.enable_widgets()
        self.close()

    def reject(self):
        """
        When the user presses the "Cancel" button, terminate the image processor thread and
        exit without saving anything.

        :return: -
        """

        self.image_processor.stop()
        if self.signal_save_postprocessed_image:
            self.signal_save_postprocessed_image.emit(None)
        self.close()


class EmulateStatusBar(object):
    """
    This class is only used for unit testing with the main program below. It emulates the status
    bar of the main GUI by printing the message to standard output.
    """

    def __init__(self):
        super(EmulateStatusBar, self).__init__()
        pass

    @staticmethod
    def print_status_bar_info(message, color):
        """
        Emulate the "write_status_bar" method of the main GUI.

        :param message: Message to be displayed (str).
        :param color: String with the color of the message (e.g. "red").
        :return:
        """

        colors = {}
        colors['red'] = "\033[1;31m"
        colors['blue'] = "\033[1;34m"
        colors['cyan'] = "\033[1;36m"
        colors['green'] = "\033[0;32m"
        colors['reset'] = "\033[0;0m"
        colors['bold'] = "\033[;1m"

        # Add a try-except clause to avoid a program crash if an un-supported color is specified.
        # In this case, just use a standard print.
        try:
            stdout.write(colors[color])
            print(message)
            stdout.write(colors['reset'])
        except:
            print(message)


if __name__ == '__main__':
    # input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Moon_2018-03-24\Moon_Tile-024_043939_pss.tiff"
    # input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter_Richard\\" \
    #                   "2020-07-29-2145_3-L-Jupiter_ALTAIRGP224C_pss_p70_b48.png"
    # input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Jupiter\\2019-05-26-0115_4-L-Jupiter_ZWO ASI290MM Mini_pipp_pss.png"
    # input_file_name = "D:\SW-Development\Python\PlanetarySystemStacker\Examples\Moon_2011-04-10\South.stacked.tiff"
    input_file_name = "dummy-not-used"

    input_image = imread(input_file_name, -1)
    # Change colors to standard RGB
    if len(input_image.shape) == 3:
        input_image = cvtColor(input_image, COLOR_BGR2RGB)

    configuration = Configuration()
    configuration.initialize_configuration()
    dummy_status_bar = EmulateStatusBar()
    app = QtWidgets.QApplication(argv)
    window = PostprocEditorWidget(configuration, input_image, input_file_name,
                                  dummy_status_bar.print_status_bar_info, None)
    window.show()
    app.exec_()