[75c70a]: src / hugin_base / panodata / Panorama.cpp Maximize Restore History

Download this file

Panorama.cpp    2724 lines (2468 with data), 98.1 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
// -*- c-basic-offset: 4 -*-
/** @file Panorama.cpp
*
* @brief implementation of Panorama Class
*
* @author Pablo d'Angelo <pablo.dangelo@web.de>
*
* $Id$
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
#include "Panorama.h"
#include "PTScriptParsing.h"
#include "ImageVariableTranslate.h"
#include "StandardImageVariableGroups.h"
#include <panotools/PanoToolsInterface.h>
#include <algorithms/basic/CalculateOverlap.h>
#include <fstream>
#include <typeinfo>
#include <vigra/impex.hxx>
namespace HuginBase {
using namespace hugin_utils;
Panorama::Panorama()
: //currentProcess(NO_PROCESS),
//optimizerExe("PTOptimizer"),
//stitcherExe("PTStitcher"),
//PTScriptFile("PT_script.txt"),
dirty(false),
m_forceImagesUpdate(false)
{
// init map with ptoptimizer variables.
m_ptoptimizerVarNames.insert("a");
m_ptoptimizerVarNames.insert("b");
m_ptoptimizerVarNames.insert("c");
m_ptoptimizerVarNames.insert("d");
m_ptoptimizerVarNames.insert("e");
m_ptoptimizerVarNames.insert("g");
m_ptoptimizerVarNames.insert("t");
m_ptoptimizerVarNames.insert("v");
m_ptoptimizerVarNames.insert("r");
m_ptoptimizerVarNames.insert("p");
m_ptoptimizerVarNames.insert("y");
m_ptoptimizerVarNames.insert("TrX");
m_ptoptimizerVarNames.insert("TrY");
m_ptoptimizerVarNames.insert("TrZ");
m_ptoptimizerVarNames.insert("Te0");
m_ptoptimizerVarNames.insert("Te1");
m_ptoptimizerVarNames.insert("Te2");
/*
settings.setPath("dangelo","PanoAssistant");
readSettings();
process.setCommunication(QProcess::Stdin|
QProcess::Stdout|
QProcess::Stderr|
QProcess::DupStderr);
connect(&process, SIGNAL(processExited()), this, SLOT(processExited()));
*/
}
Panorama::~Panorama()
{
DEBUG_TRACE("dtor");
reset();
DEBUG_TRACE("dtor about to finish");
}
void Panorama::reset()
{
//
// imageChanged(0);
// delete all images and control points.
state.ctrlPoints.clear();
state.deleteAllImages();
state.options.reset();
state.optvec.clear();
state.needsOptimization = false;
}
#if 0
QDomElement Panorama::toXML(QDomDocument & doc)
{
QDomElement root = doc.createElement("panorama");
// serialize global options:
root.appendChild(options.toXML(doc));
// serialize image
QDomElement images_ = doc.createElement("images");
for (ImageVector::iterator it = images.begin(); it != images.end(); ++it) {
images_.appendChild(it->toXML(doc));
}
root.appendChild(images_);
// control points
QDomElement cps = doc.createElement("control_points");
for (CPVector::iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
cps.appendChild(it->toXML(doc));
}
root.appendChild(cps);
// lenses
QDomElement lenses_ = doc.createElement("lenses");
for (std::vector<Lens>::iterator it = lenses.begin(); it != lenses.end(); ++it) {
lenses_.appendChild(it->toXML(doc));
}
root.appendChild(lenses_);
return root;
}
void Panorama::setFromXML(const QDomNode & elem)
{
DEBUG_DEBUG("Panorama::setFromXML");
clear();
Q_ASSERT(elem.nodeName() == "panorama");
// read global options
QDomNode n = elem.namedItem("output");
Q_ASSERT(!n.isNull);
options.setFromXML(n);
n = elem.namedItem("images");
Q_ASSERT(!n.isNull);
n = n.firstChild();
while( !n.isNull() ) {
QDomElement e = n.toElement(); // try to convert the node to an element.
Q_ASSERT((!e.isNull()) && e.tagName() == "image" );
images.push_back(PanoImage(*this, e));
reportAddedImage(images.size() -1);
n = n.nextSibling();
}
n = elem.namedItem("control_points");
Q_ASSERT(!n.isNull());
n = n.firstChild();
while( !n.isNull() ) {
QDomElement e = n.toElement();// try to convert the node to an element.
Q_ASSERT((!e.isNull()) && e.tagName() == "control_point" );
controlPoints.push_back(ControlPoint(*this, e));
reportAddedCtrlPoint(controlPoints.size() -1);
n = n.nextSibling();
}
}
#endif
std::vector<unsigned int> Panorama::getCtrlPointsForImage(unsigned int imgNr) const
{
std::vector<unsigned int> result;
unsigned int i = 0;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
result.push_back(i);
}
i++;
}
return result;
}
CPointVector Panorama::getCtrlPointsVectorForImage(unsigned int imgNr) const
{
CPointVector result;
for(unsigned int i=0;i<state.ctrlPoints.size();i++)
{
ControlPoint point=state.ctrlPoints[i];
if(point.image1Nr==imgNr)
{
result.push_back(std::make_pair(i,point));
}
else
{
if(point.image2Nr==imgNr)
{
point.mirror();
result.push_back(std::make_pair(i,point));
};
};
};
return result;
};
VariableMapVector Panorama::getVariables() const
{
VariableMapVector map;
for (size_t i = 0; i < state.images.size(); i++)
{
map.push_back(state.images[i]->getVariableMap());
}
return map;
}
const VariableMap Panorama::getImageVariables(unsigned int imgNr) const
{
assert(imgNr < state.images.size());
return state.images[imgNr]->getVariableMap();
}
void Panorama::updateCtrlPointErrors(const UIntSet & imgs, const CPVector & cps)
{
unsigned sc = 0;
unsigned ic = 0;
std::map<unsigned int, unsigned int> script2CPMap;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
script2CPMap[sc] = ic;
sc++;
}
ic++;
}
// need to have same number of control points!
assert(cps.size() == script2CPMap.size());
unsigned i=0;
for (CPVector::const_iterator it = cps.begin(); it != cps.end(); ++it) {
imageChanged(script2CPMap[it->image1Nr]);
imageChanged(script2CPMap[it->image2Nr]);
state.ctrlPoints[script2CPMap[i]].error = it->error;
i++;
}
}
void Panorama::updateCtrlPointErrors(const CPVector & cps)
{
assert(cps.size() == state.ctrlPoints.size());
unsigned int nrp = cps.size();
for (unsigned int i = 0; i < nrp ; i++) {
imageChanged(state.ctrlPoints[i].image1Nr);
imageChanged(state.ctrlPoints[i].image2Nr);
state.ctrlPoints[i].error = cps[i].error;
}
}
void Panorama::updateVariables(const VariableMapVector & vars)
{
assert(vars.size() == state.images.size());
unsigned int i = 0;
for (VariableMapVector::const_iterator it = vars.begin(); it != vars.end(); ++it) {
updateVariables(i, *it);
i++;
}
}
void Panorama::updateVariables(const UIntSet & imgs, const VariableMapVector & vars)
{
VariableMapVector::const_iterator v_it = vars.begin();
for (UIntSet::const_iterator it = imgs.begin(); it != imgs.end(); ++it) {
assert(*it < state.images.size());
updateVariables(*it, *v_it);
++v_it;
}
}
void Panorama::updateVariables(unsigned int imgNr, const VariableMap & var)
{
if (imgNr > state.images.size())
return;
for (VariableMap::const_iterator it = var.begin(); it != var.end() ; ++it) {
updateVariable(imgNr,it->second);
}
}
void Panorama::updateVariable(unsigned int imgNr, const Variable &var)
{
if (imgNr > state.images.size())
return;
// update a single variable, and everything linked to it.
state.images[imgNr]->setVar(var.getName(), var.getValue());
// call imageChanged for the images affected by this.
#define image_variable( name, type, default_value ) \
if (PTOVariableConverterFor##name::checkApplicability(var.getName())) \
{\
for (std::size_t i = 0; i < getNrOfImages(); i++)\
{\
if (state.images[imgNr]->name##isLinkedWith(*state.images[i]))\
{\
imageChanged(i);\
}\
}\
}\
else
#include "image_variables.h"
#undef image_variable
{// this is for the final else.
DEBUG_ERROR("Unknown variable " << var.getName());
}
state.needsOptimization = true;
}
void Panorama::UpdateFocalLength(UIntSet imgs, double newFocalLength)
{
for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();it++)
{
state.images[*it]->updateFocalLength(newFocalLength);
imageChanged(*it);
};
//search for images with linked HFOV and mark these also as changed
for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();it++)
{
SrcPanoImage *img=state.images[*it];
if(state.images[*it]->HFOVisLinked())
{
for(unsigned int j=0;j<getNrOfImages();j++)
{
if(*it!=j)
{
if(state.images[*it]->HFOVisLinkedWith(*img))
{
imageChanged(j);
};
};
};
};
};
};
void Panorama::UpdateCropFactor(UIntSet imgs, double newCropFactor)
{
//store list of focal length, so we can keep the focal length unchanged and change hfov instead
std::vector<double> focalLengthVector(getNrOfImages(),0.0);
for(unsigned i=0;i<getNrOfImages();i++)
{
focalLengthVector[i]=state.images[i]->calcFocalLength(state.images[i]->getProjection(),
state.images[i]->getHFOV(),state.images[i]->getExifCropFactor(),state.images[i]->getSize());
};
for(UIntSet::const_iterator it=imgs.begin();it!=imgs.end();it++)
{
state.images[*it]->updateCropFactor(focalLengthVector[*it],newCropFactor);
imageChanged(*it);
};
};
// What counts as changed in terms of the image variable links?
// Should I call imageChanged on every image linked to sourceImg and destImg?
// destImg's variable will change value to sourceImg's, so the images linked to
// destImg should be linked.
/// @todo call imageChanged on those images changed by the linking.
#define image_variable( name, type, default_value )\
void Panorama::linkImageVariable##name(unsigned int sourceImgNr, unsigned int destImgNr)\
{\
state.images[destImgNr]->link##name(state.images[sourceImgNr]);\
imageChanged(destImgNr);\
imageChanged(sourceImgNr);\
state.needsOptimization = true;\
}
#include "image_variables.h"
#undef image_variable
/// @todo call imageChanged on those images changed by the unlinking.
#define image_variable( name, type, default_value )\
void Panorama::unlinkImageVariable##name(unsigned int imgNr)\
{\
state.images[imgNr]->unlink##name();\
imageChanged(imgNr);\
state.needsOptimization = true;\
}
#include "image_variables.h"
#undef image_variable
void Panorama::setOptimizeVector(const OptimizeVector & optvec)
{
DEBUG_ASSERT(optvec.size() == state.images.size());
state.optvec = optvec;
}
unsigned int Panorama::addImage(const SrcPanoImage &img)
{
unsigned int nr = state.images.size();
state.images.push_back(new SrcPanoImage(img));
// create empty optimisation vector
state.optvec.push_back(std::set<std::string>());
imageChanged(nr);
return nr;
}
void Panorama::removeImage(unsigned int imgNr)
{
DEBUG_DEBUG("Panorama::removeImage(" << imgNr << ")");
assert(imgNr < state.images.size());
// remove control points
CPVector::iterator it = state.ctrlPoints.begin();
while (it != state.ctrlPoints.end()) {
if ((it->image1Nr == imgNr) || (it->image2Nr == imgNr)) {
// remove point that refernce to imgNr
it = state.ctrlPoints.erase(it);
} else {
// correct point references
if (it->image1Nr > imgNr) it->image1Nr--;
if (it->image2Nr > imgNr) it->image2Nr--;
++it;
}
}
DEBUG_TRACE("Remove variables and image from panorama state")
delete state.images[imgNr];
state.images.erase(state.images.begin() + imgNr);
state.optvec.erase(state.optvec.begin() + imgNr);
// check if reference image has been moved
if (state.options.optimizeReferenceImage >= state.images.size()) {
state.options.optimizeReferenceImage = 0;
imageChanged(state.options.optimizeReferenceImage);
}
if (state.options.colorReferenceImage >= state.images.size()) {
state.options.colorReferenceImage = 0;
imageChanged(state.options.colorReferenceImage);
}
// change all other (moved) images
DEBUG_TRACE("flag moved images as dirty");
for (unsigned int i=imgNr; i < state.images.size(); i++) {
imageChanged(i);
}
m_forceImagesUpdate = true;
}
void Panorama::setImageFilename(unsigned int i, const std::string & fname)
{
DEBUG_ASSERT(i < state.images.size());
state.images[i]->setFilename(fname);
imageChanged(i);
m_forceImagesUpdate = true;
}
unsigned int Panorama::addCtrlPoint(const ControlPoint & point )
{
unsigned int nr = state.ctrlPoints.size();
state.ctrlPoints.push_back(point);
imageChanged(point.image1Nr);
imageChanged(point.image2Nr);
state.needsOptimization = true;
return nr;
}
void Panorama::removeCtrlPoint(unsigned int pNr)
{
DEBUG_ASSERT(pNr < state.ctrlPoints.size());
ControlPoint & point = state.ctrlPoints[pNr];
unsigned int i1 = point.image1Nr;
unsigned int i2 = point.image2Nr;
state.ctrlPoints.erase(state.ctrlPoints.begin() + pNr);
// update line control points
updateLineCtrlPoints();
imageChanged(i1);
imageChanged(i2);
state.needsOptimization = true;
}
void Panorama::removeDuplicateCtrlPoints()
{
std::set<std::string> listOfCPs;
std::set<unsigned int> duplicateCPs;
for(unsigned int i=0; i<state.ctrlPoints.size();i++)
{
std::string s=state.ctrlPoints[i].getCPString();
std::pair<std::set<std::string>::iterator,bool> it=listOfCPs.insert(s);
if(it.second==false)
{
duplicateCPs.insert(i);
};
}
//now remove duplicate control points, mark affected images as changed
if(duplicateCPs.size()>0)
{
for(std::set<unsigned int>::reverse_iterator it=duplicateCPs.rbegin();it!=duplicateCPs.rend();it++)
{
ControlPoint cp=state.ctrlPoints[*it];
imageChanged(cp.image1Nr);
imageChanged(cp.image2Nr);
removeCtrlPoint(*it);
};
};
updateLineCtrlPoints();
}
void Panorama::changeControlPoint(unsigned int pNr, const ControlPoint & point)
{
assert(pNr < state.ctrlPoints.size());
// change notify for all involved images
imageChanged(state.ctrlPoints[pNr].image1Nr);
imageChanged(state.ctrlPoints[pNr].image2Nr);
imageChanged(point.image1Nr);
imageChanged(point.image2Nr);
state.needsOptimization = true;
state.ctrlPoints[pNr] = point;
updateLineCtrlPoints();
}
void Panorama::setCtrlPoints(const CPVector & points)
{
for (CPVector::const_iterator it = state.ctrlPoints.begin();
it != state.ctrlPoints.end(); it++)
{
imageChanged(it->image1Nr);
imageChanged(it->image2Nr);
}
state.ctrlPoints = points;
for (CPVector::const_iterator it = state.ctrlPoints.begin();
it != state.ctrlPoints.end(); it++)
{
imageChanged(it->image1Nr);
imageChanged(it->image2Nr);
}
state.needsOptimization = true;
updateLineCtrlPoints();
}
// close holes in line control points
void Panorama::updateLineCtrlPoints()
{
// sort all line control points
std::map<int, int> lines;
for (CPVector::const_iterator it = state.ctrlPoints.begin();
it != state.ctrlPoints.end(); it++)
{
if (it->mode > 2)
lines[it->mode] = 0;
}
int i=3;
for (std::map<int,int >::iterator it = lines.begin(); it != lines.end(); ++it)
{
(*it).second = i;
i++;
}
for (CPVector::iterator it = state.ctrlPoints.begin();
it != state.ctrlPoints.end(); it++)
{
if (it->mode > 2) {
int newmode = lines[it->mode];
if (it->mode != newmode) {
it->mode = newmode;
imageChanged(it->image1Nr);
imageChanged(it->image2Nr);
}
}
}
}
void Panorama::printPanoramaScript(std::ostream & o,
const OptimizeVector & optvars,
const PanoramaOptions & output,
const UIntSet & imgs,
bool forPTOptimizer,
const std::string & stripPrefix) const
{
using namespace std;
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
if (forPTOptimizer) {
o << "# PTOptimizer script, written by hugin" << std::endl
<< std::endl;
} else {
o << "# hugin project file" << std::endl;
o << "#hugin_ptoversion 2" << std::endl;
}
// output options..
output.printScriptLine(o, forPTOptimizer);
std::map<unsigned int, unsigned int> linkAnchors;
// map from script img nr -> pano image nr
std::map<unsigned int, unsigned int> imageNrMap;
o << std::endl
<< "# image lines" << std::endl;
// somewhere to store the v lines, which give the variables to be optimised.
std::stringstream vlines;
std::stringstream masklines;
unsigned int ic = 0;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
unsigned int imgNr = *imgNrIt;
imageNrMap[imgNr] = ic;
const SrcPanoImage & img = *state.images[imgNr];
VariableMap vars;
// print special comment line with hugin GUI data
o << "#-hugin ";
if (img.getCropMode() != BaseSrcPanoImage::NO_CROP) {
if (img.getAutoCenterCrop())
o << " autoCenterCrop=1";
}
o << " cropFactor=" << img.getExifCropFactor() ;
if (! img.getActive()) {
o << " disabled";
}
o << std::endl;
o << "i w" << img.getSize().width() << " h" << img.getSize().height()
<<" f" << img.getProjection() << " ";
// print variables with links
/* Individually do all the variables specified by each SrcPanoImg variable.
* Clear the list after each SrcPanoImg variable.
* If there is any links to previous images, write that in the script.
* If there are no links to previous images, write the value instead.
* Each variable in SrcPanoImg may produce any number of variables in the map,
* but the linking properties are shared.
* Additionally, when we are writing variables by value which are set to be
* optimised, we should remember them so we can write them later as 'v' lines.
*/
#define image_variable( name, type, default_value )\
PTOVariableConverterFor##name::addToVariableMap(state.images[imgNr]->get##name##IV(), vars);\
if (!vars.empty())\
{\
bool linking = false;\
std::size_t link_target;\
if (ic!=0)\
{\
if (state.images[imgNr]->name##isLinked())\
{\
for (link_target = 0; link_target < imgNr; link_target++)\
{\
if (set_contains(imgs,link_target) && state.images[imgNr]->name##isLinkedWith(*state.images[link_target]))\
{\
linking = true;\
break;\
}\
}\
}\
}\
for (VariableMap::const_iterator vit = vars.begin();\
vit != vars.end(); ++vit)\
{\
if (forPTOptimizer && !set_contains(m_ptoptimizerVarNames,vit->first))\
continue;\
else if (linking)\
{\
o << vit->first << "=" << imageNrMap[link_target] << " ";\
} else {\
if (set_contains(optvars[imgNr], vit->first))\
{\
vlines << "v " << vit->first << imageNrMap[imgNr] << std::endl;\
}\
if (((vit->first == "a" && set_contains(optvars[imgNr], "a") )|| \
(vit->first == "b" && set_contains(optvars[imgNr], "b") )|| \
(vit->first == "c" && set_contains(optvars[imgNr], "c") )|| \
(vit->first == "TrX" && set_contains(optvars[imgNr], "TrX") )|| \
(vit->first == "TrY" && set_contains(optvars[imgNr], "TrY") )|| \
(vit->first == "TrZ" && set_contains(optvars[imgNr], "TrZ") )|| \
(vit->first == "Te0" && set_contains(optvars[imgNr], "Te0") )|| \
(vit->first == "Te1" && set_contains(optvars[imgNr], "Te1") )|| \
(vit->first == "Te2" && set_contains(optvars[imgNr], "Te2") )\
)\
&& forPTOptimizer && vit->second.getValue() == 0.0) \
{\
o << vit->first << 1e-5 << " ";\
} else if (( (vit->first == "r" && set_contains(optvars[imgNr], "r") ) || \
(vit->first == "p" && set_contains(optvars[imgNr], "p") ) || \
(vit->first == "y" && set_contains(optvars[imgNr], "y") ) \
)\
&& forPTOptimizer && fabs(vit->second.getValue()) < 1e-13)\
{\
o << vit->first << 0 << " ";\
} else {\
vit->second.print(o) << " ";\
}\
}\
}\
}\
vars.clear();
#include "image_variables.h"
#undef image_variable
if (img.getCropMode()!=SrcPanoImage::NO_CROP) {
// print crop parameters
vigra::Rect2D c = img.getCropRect();
o << " S" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
}
if (!forPTOptimizer) {
if (img.getVigCorrMode() != SrcPanoImage::VIGCORR_NONE) {
o << " Vm" << img.getVigCorrMode();
}
if (img.getFlatfieldFilename().size() > 0) {
o << " Vf\"" << img.getFlatfieldFilename() << "\"";
}
if (img.getResponseType() > 0) {
o << " Rt" << img.getResponseType();
}
if(img.hasMasks())
img.printMaskLines(masklines,ic);
}
#if 0
//panotools paramters, currently not used
o << " u" << output.featherWidth
<< (img.getMorph() ? " o" : "");
#endif
std::string fname = img.getFilename();
if (stripPrefix.size() > 0) {
// strip prefix from image names.
// check if the prefix is acutally the same
std::string tmp = fname.substr(0,stripPrefix.size());
if (tmp.compare(stripPrefix) == 0) {
DEBUG_DEBUG("striping " << stripPrefix << " from " << fname);
fname = fname.erase(0,stripPrefix.size());
DEBUG_DEBUG("after stripping: " << fname);
} else {
DEBUG_DEBUG(stripPrefix << " does not match " << fname);
}
}
o << " n\"" << fname << "\"" << std::endl;
ic++;
}
o << std::endl << std::endl
<< "# specify variables that should be optimized" << std::endl
<< vlines.str()
<< "v" << std::endl; // empty v line to work around libpano13 bug.
o << std::endl << std::endl
<< "# control points" << std::endl;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
o << "c n" << imageNrMap[it->image1Nr]
<< " N" << imageNrMap[it->image2Nr]
<< " x" << it->x1 << " y" << it->y1
<< " X" << it->x2 << " Y" << it->y2
<< " t" << it->mode << std::endl;
}
}
o << std::endl;
if(masklines.str().length()>0)
o << "# masks" << std::endl
<< masklines.str()
<< std::endl;
// special line with hugins options.
o << "#hugin_optimizeReferenceImage " << output.optimizeReferenceImage << std::endl;
o << "#hugin_blender ";
switch (output.blendMode) {
case PanoramaOptions::NO_BLEND:
o << "none" << endl;
break;
case PanoramaOptions::PTBLENDER_BLEND:
o << "PTblender" << endl;
break;
case PanoramaOptions::SMARTBLEND_BLEND:
o << "smartblend" << endl;
break;
case PanoramaOptions::PTMASKER_BLEND:
o << "PTmasker" << endl;
break;
default:
case PanoramaOptions::ENBLEND_BLEND:
o << "enblend" << endl;
break;
}
o << "#hugin_remapper ";
switch (output.remapper) {
case PanoramaOptions::PTMENDER:
o << "PTmender" << endl;
break;
default:
case PanoramaOptions::NONA:
o << "nona" << endl;
break;
}
o << "#hugin_enblendOptions " << output.enblendOptions << endl;
o << "#hugin_enfuseOptions " << output.enfuseOptions << endl;
o << "#hugin_hdrmergeOptions " << output.hdrmergeOptions << endl;
o << "#hugin_outputLDRBlended " << (output.outputLDRBlended ? "true" : "false") << endl;
o << "#hugin_outputLDRLayers " << (output.outputLDRLayers ? "true" : "false") << endl;
o << "#hugin_outputLDRExposureRemapped " << (output.outputLDRExposureRemapped ? "true" : "false") << endl;
o << "#hugin_outputLDRExposureLayers " << (output.outputLDRExposureLayers ? "true" : "false") << endl;
o << "#hugin_outputLDRExposureBlended " << (output.outputLDRExposureBlended ? "true" : "false") << endl;
o << "#hugin_outputLDRExposureLayersFused " << (output.outputLDRExposureLayersFused ? "true" : "false") << endl;
o << "#hugin_outputHDRBlended " << (output.outputHDRBlended ? "true" : "false") << endl;
o << "#hugin_outputHDRLayers " << (output.outputHDRLayers ? "true" : "false") << endl;
o << "#hugin_outputHDRStacks " << (output.outputHDRStacks ? "true" : "false") << endl;
o << "#hugin_outputLayersCompression " << output.outputLayersCompression << endl;
o << "#hugin_outputImageType " << output.outputImageType << endl;
o << "#hugin_outputImageTypeCompression " << output.outputImageTypeCompression << endl;
o << "#hugin_outputJPEGQuality " << output.quality << endl;
o << "#hugin_outputImageTypeHDR " << output.outputImageTypeHDR << endl;
o << "#hugin_outputImageTypeHDRCompression " << output.outputImageTypeHDRCompression << endl;
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::printStitcherScript(std::ostream & o,
const PanoramaOptions & target,
const UIntSet & imgs) const
{
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
o << "# PTStitcher script, written by hugin" << std::endl
<< std::endl;
// output options..
target.printScriptLine(o, true);
o << std::endl
<< "# output image lines" << std::endl;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end(); ++imgNrIt) {
unsigned int imgNr = *imgNrIt;
const SrcPanoImage & img = *state.images[imgNr];
// DGSW FIXME - Unreferenced
// const Lens & lens = state.lenses[lensNr];
const VariableMap & vars = state.images[imgNr]->getVariableMap();
o << "o w" << img.getSize().width() << " h" << img.getSize().height()
<<" f" << img.getProjection() << " ";
// print variables, without links
VariableMap::const_iterator vit;
for(vit = vars.begin(); vit != vars.end(); ++vit)
{
if (!set_contains(m_ptoptimizerVarNames,vit->first)) {
continue;
}
vit->second.print(o) << " ";
}
#if 0
// panotools parameters, currently not used
o << " u" << img.getFeatureWidth()
<< (img.getMorph() ? " o" : "");
#endif
o << " n\"" << img.getFilename() << "\"";
if (img.getCropMode()!=SrcPanoImage::NO_CROP) {
// print crop parameters
vigra::Rect2D c = img.getCropRect();
o << " S" << c.left() << "," << c.right() << "," << c.top() << "," << c.bottom();
}
o << std::endl;
}
o << std::endl;
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::parseOptimizerScript(std::istream & i, const UIntSet & imgs,
VariableMapVector & imgVars, CPVector & CPs) const
{
using namespace std;
using namespace PTScriptParsing;
DEBUG_TRACE("");
#ifdef __unix__
// set numeric locale to C, for correct number output
char * t = setlocale(LC_NUMERIC,NULL);
char * old_locale = (char*) malloc(strlen(t)+1);
strcpy(old_locale, t);
setlocale(LC_NUMERIC,"C");
#endif
unsigned int ic=0;
std::map<unsigned int, unsigned int> script2ImgMap;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
unsigned int imgNr = *imgNrIt;
script2ImgMap[ic] = imgNr;
ic++;
}
ic = 0;
unsigned int sc = 0;
std::map<unsigned int, unsigned int> script2CPMap;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
script2CPMap[sc] = ic;
sc++;
}
ic++;
}
// 0 = read output (image lines), 1 = read control point distances
int state = 0;
string line;
unsigned int lineNr = 0;
unsigned int scriptImgCounter = 0;
unsigned int scriptCPCounter = 0;
// VariableMapVector::iterator varIt = imgVars.begin();
// CPVector::iterator pointIt = CPs.begin();
// DGSW FIXME - Unreferenced
// int pnr=0;
while (!i.eof()) {
std::getline(i, line);
lineNr++;
switch (state) {
case 0:
{
// we are reading the output lines:
// o f3 r0 p0 y0 v89.2582 a-0.027803 b0.059851 c-0.073115 d10.542470 e16.121145 u10 -buf
if ((line.compare("# Control Points: Distance between desired and fitted Position") == 0 )
|| (line.compare("# Control Points: Distance between desired and fitted Position (in Pixels)") == 0 )
|| (line.compare("# Control Points: Distance between desired and fitted Position (in \"Pixels\")") == 0 )) {
// switch to reading the control point distance
if (scriptImgCounter != imgs.size()) {
DEBUG_ERROR("Read only " << scriptImgCounter << " images from PTOptimizer file");
}
DEBUG_DEBUG("Changing state to read control point distances");
state = 1;
break;
}
if (line[0] != 'o') continue;
// select variables of the image
VariableMap & var = imgVars[script2ImgMap[scriptImgCounter]];
DEBUG_DEBUG("reading image variables for image:" << scriptImgCounter);
// read position variables
int link;
readVar(map_get(var, "r"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "p"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "y"), link, line);
DEBUG_ASSERT(link == -1);
DEBUG_DEBUG("yaw: " << map_get(var, "y").getValue()
<< " pitch " << map_get(var, "p").getValue()
<< " roll " << map_get(var, "r").getValue());
// read lens variables
readVar(map_get(var, "TrX"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "TrY"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "TrZ"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "Te0"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "Te1"), link, line);
DEBUG_ASSERT(link == -1);
readVar(map_get(var, "Te2"), link, line);
DEBUG_ASSERT(link == -1);
DEBUG_DEBUG("X: " << map_get(var, "TrX").getValue()
<< " Y " << map_get(var, "TrY").getValue()
<< " Z " << map_get(var, "TrZ").getValue()
<< " Spin " << map_get(var, "Te0").getValue()
<< " Tilt " << map_get(var, "Te1").getValue()
<< " Rotate " << map_get(var, "Te2").getValue());
// read lens variables
for (const char **c = Lens::variableNames; *c != 0; ++c) {
Variable & curVar = map_get(var, *c);
if (!readVar(curVar, link, line)) {
DEBUG_ERROR("Could not read "<< *c << " at script line " << lineNr);
}
// linking in output forbidden
DEBUG_ASSERT(link == -1);
}
scriptImgCounter++;
break;
}
case 1:
{
// read ctrl point distances:
// # Control Point No 0: 0.428994
if (line[0] == 'C') {
// DEBUG_DEBUG(CPs.size() << " points, read: " << pnr);
state = 2;
break;
}
if (line.find("# Control Point No") != 0) continue;
DEBUG_DEBUG("reading cp dist line: " << line);
string::size_type p;
if ((p=line.find(':')) == string::npos) assert(0);
p++;
DEBUG_DEBUG("parsing point " << scriptCPCounter << " (idx:" << p << "): " << line.substr(p));
double err = -1;
hugin_utils::stringToDouble(line.substr(p), err);
CPs[script2CPMap[scriptCPCounter]].error = err;
DEBUG_DEBUG("read CP distance " << err);
scriptCPCounter++;
break;
}
default:
// ignore line..
break;
}
}
#ifdef __unix__
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
#endif
}
void Panorama::changeFinished(bool keepDirty)
{
if (state.images.size() == 0) {
// force an empty update if all images have been
// removed
DEBUG_DEBUG("forcing images update, with no images");
m_forceImagesUpdate = true;
}
// remove change notification for nonexisting images from set.
UIntSet::iterator uB = changedImages.lower_bound(state.images.size());
changedImages.erase(uB,changedImages.end());
std::stringstream t;
copy(changedImages.begin(), changedImages.end(),
std::ostream_iterator<unsigned int>(t, " "));
DEBUG_TRACE("changed image(s) " << t.str() << " begin");
//force update of crops
if(changedImages.size()>0)
{
for(UIntSet::iterator it=changedImages.begin();it!=changedImages.end();it++)
{
//if the projection was changed, we need to update the crop mode
updateCropMode(*it);
//now center the crop if user requested it
if(state.images[*it]->getAutoCenterCrop())
{
centerCrop(*it);
};
};
};
//update masks
updateMasks();
std::set<PanoramaObserver *>::iterator it;
for(it = observers.begin(); it != observers.end(); ++it) {
DEBUG_TRACE("notifying listener");
if (changedImages.size() > 0 || m_forceImagesUpdate) {
(*it)->panoramaImagesChanged(*this, changedImages);
}
(*it)->panoramaChanged(*this);
}
// reset changed images
changedImages.clear();
m_forceImagesUpdate = false;
if (!keepDirty) {
dirty = true;
AppBase::DocumentData::setDirty(dirty);
}
DEBUG_TRACE("end");
}
void Panorama::updateMasksForImage(unsigned int imgNr, MaskPolygonVector newMasks)
{
DEBUG_ASSERT(imgNr < state.images.size());
state.images[imgNr]->setMasks(newMasks);
imageChanged(imgNr);
m_forceImagesUpdate = true;
};
void Panorama::transferMask(MaskPolygon mask,unsigned int imgNr, const UIntSet targetImgs)
{
if(targetImgs.size()==0)
{
return;
};
MaskPolygon transformedMask=mask;
// clip positive mask to image boundaries or clip region
vigra::Rect2D clipRect=vigra::Rect2D(0,0,state.images[imgNr]->getWidth(),state.images[imgNr]->getHeight());
if(mask.isPositive())
{
//clip to crop region only positive masks
switch(state.images[imgNr]->getCropMode())
{
case BaseSrcPanoImage::CROP_RECTANGLE:
clipRect=clipRect & state.images[imgNr]->getCropRect();
if(clipRect.isEmpty())
{
return;
};
break;
case BaseSrcPanoImage::CROP_CIRCLE:
{
vigra::Rect2D cropRect=state.images[imgNr]->getCropRect();
FDiff2D center=FDiff2D((cropRect.left()+cropRect.right())/2.0,(cropRect.top()+cropRect.bottom())/2.0);
double radius=((cropRect.width()<cropRect.height())?cropRect.width():cropRect.height())/2.0;
if(!transformedMask.clipPolygon(center,radius))
{
return;
};
};
break;
};
};
int origWindingNumber=transformedMask.getTotalWindingNumber();
if(transformedMask.clipPolygon(clipRect))
{
//increase resolution of positive mask to get better transformation
//of vertices, especially for fisheye images
transformedMask.subSample(20);
//transform polygon to panorama space
HuginBase::PTools::Transform trans;
trans.createInvTransform(getImage(imgNr),getOptions());
transformedMask.transformPolygon(trans);
for(UIntSet::const_iterator it=targetImgs.begin();it!=targetImgs.end();it++)
{
if(imgNr==(*it))
{
continue;
};
MaskPolygon targetMask;
if(state.images[imgNr]->YawisLinkedWith(*(state.images[*it])))
{
//if yaw is linked, we simply copy the mask
targetMask=mask;
}
else
{
targetMask=transformedMask;
PTools::Transform targetTrans;
targetTrans.createTransform(getImage(*it),getOptions());
targetMask.transformPolygon(targetTrans);
//check if transformation has produced invalid polygon
if(targetMask.getMaskPolygon().size()<3)
{
continue;
};
//check if mask was inverted - outside became inside and vice versa
//if so, invert mask
int newWindingNumber=targetMask.getTotalWindingNumber();
targetMask.setInverted(origWindingNumber * newWindingNumber < 0);
};
//now clip polygon to image rectangle, add mask only when polygon is inside image
if(targetMask.clipPolygon(vigra::Rect2D(-maskOffset,-maskOffset,
state.images[*it]->getWidth()+maskOffset,state.images[*it]->getHeight()+maskOffset)))
{
targetMask.setMaskType(MaskPolygon::Mask_negative);
targetMask.setImgNr(*it);
state.images[*it]->addActiveMask(targetMask);
};
};
};
};
void Panorama::updateMasks(bool convertPosMaskToNeg)
{
// update masks
UIntSet imgWithPosMasks;
for(unsigned int i=0;i<state.images.size();i++)
{
state.images[i]->clearActiveMasks();
if(state.images[i]->hasPositiveMasks())
{
imgWithPosMasks.insert(i);
};
};
CalculateImageOverlap overlap(this);
overlap.limitToImages(imgWithPosMasks);
overlap.calculate(10);
ConstStandardImageVariableGroups variable_groups(*this);
ConstImageVariableGroup & lenses = variable_groups.getLenses();
for(unsigned int i=0;i<state.images.size();i++)
{
if(state.images[i]->hasMasks())
{
MaskPolygonVector masks=state.images[i]->getMasks();
for(unsigned int j=0;j<masks.size();j++)
{
if(convertPosMaskToNeg)
{
//this is used for masking in the cp finder, we are consider
//all masks as negative masks, because at this moment
//the final position of the images is not known
switch(masks[j].getMaskType())
{
case MaskPolygon::Mask_negative:
case MaskPolygon::Mask_positive:
masks[j].setImgNr(i);
masks[j].setMaskType(MaskPolygon::Mask_negative);
state.images[i]->addActiveMask(masks[j]);
break;
case MaskPolygon::Mask_Stack_negative:
case MaskPolygon::Mask_Stack_positive:
{
//copy mask to all images of the same stack
UIntSet imgStack;
for(unsigned int k=0;k<getNrOfImages();k++)
{
if(i!=k)
{
if(state.images[i]->StackisLinkedWith(*(state.images[k])))
{
imgStack.insert(k);
};
};
};
masks[j].setImgNr(i);
masks[j].setMaskType(MaskPolygon::Mask_negative);
state.images[i]->addActiveMask(masks[j]);
transferMask(masks[j],i,imgStack);
};
break;
case MaskPolygon::Mask_negative_lens:
{
unsigned int lensNr=lenses.getPartNumber(i);
//copy masks to all image of the same lens
UIntSet imgLens;
for(unsigned int k=0;k<getNrOfImages();k++)
{
if(lenses.getPartNumber(k)==lensNr)
{
masks[j].setImgNr(k);
masks[j].setMaskType(MaskPolygon::Mask_negative_lens);
state.images[k]->addActiveMask(masks[j]);
};
};
};
break;
};
}
else
{
switch(masks[j].getMaskType())
{
case MaskPolygon::Mask_negative:
//negative mask, simply copy mask to active mask
masks[j].setImgNr(i);
state.images[i]->addActiveMask(masks[j]);
break;
case MaskPolygon::Mask_positive:
//propagate positive mask only if image is active
if(state.images[i]->getActive())
{
UIntSet overlapImgs=overlap.getOverlapForImage(i);
transferMask(masks[j],i,overlapImgs);
};
break;
case MaskPolygon::Mask_Stack_negative:
{
//search all images of the stack
UIntSet imgStack;
for(unsigned int k=0;k<getNrOfImages();k++)
{
if(i!=k)
{
if(state.images[i]->StackisLinkedWith(*(state.images[k])))
{
imgStack.insert(k);
};
};
};
//copy mask also to the image which contains the mask
masks[j].setImgNr(i);
masks[j].setMaskType(MaskPolygon::Mask_negative);
state.images[i]->addActiveMask(masks[j]);
transferMask(masks[j],i,imgStack);
};
break;
case MaskPolygon::Mask_Stack_positive:
{
//remove all images from the stack from the set
UIntSet imgStack;
fill_set(imgStack,0,getNrOfImages()-1);
imgStack.erase(i);
for(unsigned int k=0;k<getNrOfImages();k++)
{
if(i!=k)
{
if(state.images[i]->StackisLinkedWith(*(state.images[k])))
{
imgStack.erase(k);
};
};
};
//only leave overlapping images in set
UIntSet imgOverlap=overlap.getOverlapForImage(i);
UIntSet imgs;
std::set_intersection(imgStack.begin(),imgStack.end(),imgOverlap.begin(),imgOverlap.end(),inserter(imgs,imgs.begin()));
//now transfer mask
transferMask(masks[j],i,imgs);
};
break;
case MaskPolygon::Mask_negative_lens:
{
unsigned int lensNr=lenses.getPartNumber(i);
//copy masks to all image of the same lens
UIntSet imgLens;
for(unsigned int k=0;k<getNrOfImages();k++)
{
if(lenses.getPartNumber(k)==lensNr)
{
masks[j].setImgNr(k);
masks[j].setMaskType(MaskPolygon::Mask_negative_lens);
state.images[k]->addActiveMask(masks[j]);
};
};
};
break;
};
};
};
};
};
};
void Panorama::updateCropMode(unsigned int imgNr)
{
vigra::Rect2D r=state.images[imgNr]->getCropRect();
if(r.isEmpty() || r==vigra::Rect2D(state.images[imgNr]->getSize()))
{
state.images[imgNr]->setCropMode(SrcPanoImage::NO_CROP);
}
else
{
if (state.images[imgNr]->isCircularCrop())
{
state.images[imgNr]->setCropMode(SrcPanoImage::CROP_CIRCLE);
}
else
{
state.images[imgNr]->setCropMode(SrcPanoImage::CROP_RECTANGLE);
};
};
};
vigra::Rect2D Panorama::centerCropImage(unsigned int imgNr)
{
vigra::Rect2D cropRect;
if(state.images[imgNr]->getCropMode()==SrcPanoImage::NO_CROP)
{
return cropRect;
};
int dx = roundi(state.images[imgNr]->getRadialDistortionCenterShift().x);
int dy = roundi(state.images[imgNr]->getRadialDistortionCenterShift().y);
vigra::Point2D center = vigra::Point2D(state.images[imgNr]->getSize().width()/2 + dx, state.images[imgNr]->getSize().height()/2 + dy);
vigra::Diff2D d(state.images[imgNr]->getCropRect().width() / 2, state.images[imgNr]->getCropRect().height() / 2);
cropRect.setUpperLeft( center - d);
cropRect.setLowerRight( center + d);
return cropRect;
};
void Panorama::centerCrop(unsigned int imgNr)
{
vigra::Rect2D cropRect;
if( state.images[imgNr]->getCropMode()!=SrcPanoImage::NO_CROP &&
state.images[imgNr]->getAutoCenterCrop() &&
!(state.images[imgNr]->getCropRect().isEmpty())
)
{
cropRect=centerCropImage(imgNr);
if(!cropRect.isEmpty())
{
state.images[imgNr]->setCropRect(cropRect);
imageChanged(imgNr);
};
};
for (std::size_t i = 0; i < getNrOfImages(); i++)
{
if(i==imgNr)
{
continue;
};
if (state.images[imgNr]->RadialDistortionCenterShiftisLinkedWith(*state.images[i]))
{
if( state.images[i]->getCropMode()!=SrcPanoImage::NO_CROP &&
state.images[i]->getAutoCenterCrop() &&
!(state.images[i]->getCropRect().isEmpty())
)
{
cropRect=centerCropImage(i);
if(!cropRect.isEmpty())
{
state.images[i]->setCropRect(cropRect);
imageChanged(i);
};
};
};
};
};
void Panorama::swapImages(unsigned int img1, unsigned int img2)
{
DEBUG_TRACE("swapping images " << img1 << ", " << img2);
DEBUG_ASSERT(img1 < state.images.size());
DEBUG_ASSERT(img2 < state.images.size());
// first, swap image pointers in the list.
SrcPanoImage * pimg1 = state.images[img1];
state.images[img1] = state.images[img2];
state.images[img2] = pimg1;
// update control points
for (CPVector::iterator it=state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
int n1 = (*it).image1Nr;
int n2 = (*it).image2Nr;
if ((*it).image1Nr == img1) {
n1 = img2;
} else if ((*it).image1Nr == img2) {
n1 = img1;
}
if ((*it).image2Nr == img1) {
n2 = img2;
} else if ((*it).image2Nr == img2) {
n2 = img1;
}
(*it).image1Nr = n1;
(*it).image2Nr = n2;
}
// update panorama options
if (state.options.colorReferenceImage == img1) {
state.options.colorReferenceImage = img2;
} else if (state.options.colorReferenceImage == img2) {
state.options.colorReferenceImage = img1;
}
if (state.options.optimizeReferenceImage == img1) {
state.options.optimizeReferenceImage = img2;
} else if (state.options.optimizeReferenceImage == img2) {
state.options.optimizeReferenceImage = img1;
}
imageChanged(img1);
imageChanged(img2);
}
bool Panorama::setMementoToCopyOf(const PanoramaDataMemento* memento)
{
if(memento==NULL)
return false;
const PanoramaMemento* mymemento;
try {
mymemento = dynamic_cast<const PanoramaMemento*>(memento);
} catch (std::bad_cast e) {
// std::cerr << "Incompatible memento type." << std::endl;
DEBUG_DEBUG("Incompatible memento type.");
return false;
}
setMemento(PanoramaMemento(*mymemento));
return true;
}
/// set the internal state
void Panorama::setMemento(const PanoramaMemento& memento)
{
DEBUG_TRACE("");
// remove old content.
reset();
DEBUG_DEBUG("nr of images in memento:" << memento.images.size());
state = memento;
updateMasks();
unsigned int nNewImages = state.images.size();
DEBUG_DEBUG("nNewImages:" << nNewImages);
// send changes for all images
for (unsigned int i = 0; i < nNewImages; i++) {
imageChanged(i);
}
}
PanoramaDataMemento* Panorama::getNewMemento() const
{
return new PanoramaMemento(getMemento());
}
void Panorama::setOptions(const PanoramaOptions & opt)
{
if (state.options.optimizeReferenceImage != opt.optimizeReferenceImage) {
imageChanged(opt.optimizeReferenceImage);
imageChanged(state.options.optimizeReferenceImage);
}
if (state.options.colorReferenceImage != opt.colorReferenceImage) {
imageChanged(opt.colorReferenceImage);
imageChanged(state.options.colorReferenceImage);
}
state.options = opt;
}
void Panorama::addObserver(PanoramaObserver * o)
{
observers.insert(o);
}
bool Panorama::removeObserver(PanoramaObserver * o)
{
return observers.erase(o) > 0;
}
void Panorama::clearObservers()
{
observers.clear();
}
void Panorama::imageChanged(unsigned int imgNr)
{
// DEBUG_TRACE("adding image " << imgNr);
changedImages.insert(imgNr);
assert(changedImages.find(imgNr) != changedImages.end());
}
void Panorama::activateImage(unsigned int imgNr, bool active)
{
assert(imgNr < state.images.size());
if (state.images[imgNr]->getActive() != active)
{
state.images[imgNr]->setActive(active);
imageChanged(imgNr);
}
}
UIntSet Panorama::getActiveImages() const
{
UIntSet activeImgs;
for (unsigned int i = 0; i < state.images.size(); i++) {
if (state.images[i]->getActive())
{
activeImgs.insert(i);
}
}
return activeImgs;
}
//==== internal function for variable management
SrcPanoImage Panorama::getSrcImage(unsigned imgNr) const
{
DEBUG_ASSERT(imgNr < state.images.size());
return *state.images[imgNr];
}
void Panorama::setSrcImage(unsigned int imgNr, const SrcPanoImage & img)
{
DEBUG_ASSERT(imgNr < state.images.size());
/* Copy the variables. We don't assign directly so we can do the changes to
* any linked variables.
*/
SrcPanoImage *dest = state.images[imgNr];
#define image_variable( name, type, default_value ) \
dest->set##name (img.get##name());
#include "image_variables.h"
#undef image_variable
// mark the potentially changed images.
#define image_variable( name, type, default_value ) \
for (std::size_t i = 0; i < getNrOfImages(); i++)\
{\
if(state.images[imgNr]->name##isLinkedWith(*state.images[i]))\
{\
imageChanged(i);\
}\
}
#include "image_variables.h"
#undef image_variable
}
Panorama Panorama::duplicate() const
{
Panorama pano(*this);
pano.observers.clear();
return pano;
}
Panorama Panorama::getSubset(const UIntSet & imgs) const
{
Panorama subset;
// copy data except for listners
// bits that don't change in the subset.
subset.imgFilePrefix = imgFilePrefix;
subset.dirty = dirty;
subset.state.options = state.options;
subset.state.needsOptimization = state.needsOptimization;
subset.changedImages = changedImages;
subset.m_forceImagesUpdate = m_forceImagesUpdate;
subset.m_ptoptimizerVarNames = m_ptoptimizerVarNames;
// create image number map.
std::map<unsigned int, unsigned int> imageNrMap;
// copy image information
unsigned int ic = 0;
for (UIntSet::const_iterator imgNrIt = imgs.begin(); imgNrIt != imgs.end();
++imgNrIt)
{
subset.state.images.push_back(new SrcPanoImage(*state.images[*imgNrIt]));
subset.state.optvec.push_back(state.optvec[*imgNrIt]);
imageNrMap[*imgNrIt] = ic;
ic++;
}
// recreate links between image variables.
ic = 0;
for (UIntSet::const_iterator i = imgs.begin(); i != imgs.end(); ++i)
{
unsigned int jc = ic + 1;
UIntSet::const_iterator j = i;
for (++j; j != imgs.end(); ++j)
{
/** @todo It should be possible to speed this up by not linking
* things that have been already linked to something previously
* linked to the target.
*/
#define image_variable( name, type, default_value )\
if (state.images[*i]->name##isLinkedWith(*state.images[*j]))\
{\
subset.state.images[ic]->link##name(subset.state.images[jc]);\
}
#include "image_variables.h"
#undef image_variable
jc++;
}
ic++;
}
// select and translate control points.
subset.state.ctrlPoints.clear();
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it) {
if (set_contains(imgs, it->image1Nr) && set_contains(imgs, it->image2Nr)) {
ControlPoint pnt = *it;
pnt.image1Nr = imageNrMap[pnt.image1Nr];
pnt.image2Nr = imageNrMap[pnt.image2Nr];
subset.state.ctrlPoints.push_back(pnt);
}
}
//update optimizeReferenceImage and colorReferenceImage number
unsigned int newRefImg=0;
std::map<unsigned int, unsigned int>::iterator it=imageNrMap.find(state.options.optimizeReferenceImage);
if(it!=imageNrMap.end())
{
newRefImg=it->second;
};
it=imageNrMap.find(state.options.colorReferenceImage);
subset.state.options.optimizeReferenceImage=newRefImg;
newRefImg=0;
if(it!=imageNrMap.end())
{
newRefImg=it->second;
}
subset.state.options.colorReferenceImage=newRefImg;
return subset;
}
void Panorama::mergePanorama(const Panorama &newPano)
{
if(newPano.getNrOfImages()>0)
{
std::vector<unsigned int> new_image_nr(newPano.getNrOfImages());
unsigned int oldNrOfImage=getNrOfImages();
HuginBase::OptimizeVector optVec=getOptimizeVector();
HuginBase::OptimizeVector optVecNew=newPano.getOptimizeVector();
//add only new images
for(unsigned int i=0;i<newPano.getNrOfImages();i++)
{
std::string filename=newPano.getImage(i).getFilename();
bool found=false;
for(unsigned int j=0;j<getNrOfImages();j++)
{
if(getImage(j).getFilename()==filename)
{
//image is already in panorama, we remember the image nr
found=true;
new_image_nr[i]=j;
// now check if we have to update the masks
HuginBase::MaskPolygonVector masksOld=getImage(j).getMasks();
HuginBase::MaskPolygonVector masksNew=newPano.getImage(i).getMasks();
if(masksNew.size()>0)
{
for(unsigned int k=0;k<masksNew.size();k++)
{
bool usedMasks=false;
unsigned int l=0;
while((!usedMasks) && l<masksOld.size())
{
usedMasks=(masksNew[k]==masksOld[l]);
l++;
};
if(!usedMasks)
masksOld.push_back(masksNew[k]);
};
updateMasksForImage(j,masksOld);
};
break;
};
};
if(!found)
{
//new image found, add it
new_image_nr[i]=addImage(newPano.getImage(i));
//copy also optimise vector
optVec.push_back(optVecNew[i]);
};
};
setOptimizeVector(optVec);
// recreate links between image variables.
for (unsigned int i=0; i<newPano.getNrOfImages(); i++)
{
for(unsigned int j=i+1;j<newPano.getNrOfImages();j++)
{
const HuginBase::SrcPanoImage &img=newPano.getImage(i);
#define image_variable( name, type, default_value )\
if(img.name##isLinkedWith(newPano.getImage(j)))\
{\
linkImageVariable##name(new_image_nr[i],new_image_nr[j]);\
};
#include "panodata/image_variables.h"
#undef image_variable
}
}
//now translate cp
CPVector cps=newPano.getCtrlPoints();
for(unsigned int i=0;i<cps.size();i++)
{
HuginBase::ControlPoint cp(new_image_nr[cps[i].image1Nr], cps[i].x1, cps[i].y1,
new_image_nr[cps[i].image2Nr],cps[i].x2, cps[i].y2, cps[i].mode);
addCtrlPoint(cp);
};
removeDuplicateCtrlPoints();
};
};
int Panorama::getNextCPTypeLineNumber() const
{
int t=0;
for (CPVector::const_iterator it = state.ctrlPoints.begin(); it != state.ctrlPoints.end(); ++it)
{
t = std::max(t, it->mode);
}
if (t <= 2) {
t=2;
}
return t+1;
}
Panorama::ReadWriteError Panorama::readData(std::istream& dataInput, std::string documentType)
{
// [TODO] check the document type, return INCOMPATIBLE_TYPE
if(!dataInput.good() || dataInput.eof())
{
DEBUG_WARN("Failed to read from dataInput.");
return INVALID_DATA;
}
PanoramaMemento newPano;
int ptoVersion;
if (newPano.loadPTScript(dataInput, ptoVersion, getFilePrefix())) {
this->setMemento(newPano);
return SUCCESSFUL;
} else {
DEBUG_FATAL("Could not parse the data input successfully.");
return PARCER_ERROR;
}
}
///
Panorama::ReadWriteError Panorama::writeData(std::ostream& dataOutput, std::string documentType)
{
UIntSet all;
if (getNrOfImages() > 0)
fill_set(all, 0, getNrOfImages()-1);
printPanoramaScript(dataOutput, getOptimizeVector(), getOptions(), all, false, getFilePrefix());
return SUCCESSFUL;
}
void Panorama::updateWhiteBalance(double redFactor, double blueFactor)
{
UIntSet modified_images;
for(unsigned int i=0;i<getNrOfImages();i++)
{
if(!set_contains(modified_images,i))
{
state.images[i]->setWhiteBalanceRed(redFactor * state.images[i]->getWhiteBalanceRed());
state.images[i]->setWhiteBalanceBlue(blueFactor * state.images[i]->getWhiteBalanceBlue());
modified_images.insert(i);
imageChanged(i);
//check linked images and remember for later
if(state.images[i]->WhiteBalanceRedisLinked())
{
if(i+1<getNrOfImages())
{
for(unsigned int j=i+1;j<getNrOfImages();j++)
{
if(state.images[i]->WhiteBalanceRedisLinkedWith(*(state.images[j])))
{
modified_images.insert(j);
imageChanged(j);
};
};
};
};
};
};
};
PanoramaMemento::PanoramaMemento(const PanoramaMemento & data)
{
// Use the assignment operator to get the work done: see the next function.
*this = data;
}
PanoramaMemento & PanoramaMemento::operator=(const PanoramaMemento & data)
{
// Copy the PanoramaMemento.
// Don't do anything in the case of self assignment. This is important as we
// are about to delete the image information.
if (&data == this)
{
return *this;
}
// Remove any images we currently had.
deleteAllImages();
// copy image variables
for (std::vector<SrcPanoImage *>::const_iterator it = data.images.begin();
it != data.images.end(); it++)
{
images.push_back(new SrcPanoImage(*(*it)));
}
// Copies of SrcPanoImage's variables aren't linked, so we have to create
// new links in the same pattern.
/** @todo This is quite inefficent, maybe we should store the links as a
* vector of sets of image numbers for each variable to speed up this?
* Links / unlinks should all go through the Panorama object, so we
* could keep track of them easily.
*/
std::size_t num_imgs = images.size();
for (std::size_t i = 0; i < num_imgs; i++)
{
// copy this image's links.
// links to lower numbered images will have already been spotted, since
// they are bi-directional.
for (std::size_t j = i + 1; j < num_imgs; j++)
{
#define image_variable( name, type, default_value )\
if (data.images[i]->name##isLinkedWith(*data.images[j]))\
{\
images[i]->link##name(images[j]);\
}
#include "image_variables.h"
#undef image_variable
}
}
ctrlPoints = data.ctrlPoints;
options = data.options;
optvec = data.optvec;
needsOptimization = data.needsOptimization;
return *this;
}
PanoramaMemento::~PanoramaMemento()
{
deleteAllImages();
}
void PanoramaMemento::deleteAllImages()
{
// delete all the images pointed to by the images vector.
for (std::vector<SrcPanoImage *>::iterator it = images.begin();
it != images.end(); it++)
{
delete *it;
}
// now clear the pointers themselves.
images.clear();
}
bool PanoramaMemento::loadPTScript(std::istream &i, int & ptoVersion, const std::string &prefix)
{
using namespace std;
using namespace PTScriptParsing;
DEBUG_TRACE("");
// set numeric locale to C, for correct number output
char * p = setlocale(LC_NUMERIC,NULL);
char * old_locale = strdup(p);
setlocale(LC_NUMERIC,"C");
PTParseState state;
string line;
// vector with the different information lines about images
vector<ImgInfo> oImgInfo;
vector<ImgInfo> iImgInfo;
// strange comment informations.
vector<ImgInfo> cImgInfo;
// hugin additional information
vector<ImgInfo> huginImgInfo;
// vector with readed masks
MaskPolygonVector ImgMasks;
// indicate lines that should be skipped for whatever reason
bool skipNextLine = false;
bool PTGUIScriptFile = false;
int PTGUIScriptVersion = 0;
// PTGui lens line detected
int ctrlPointsImgNrOffset = 0;
bool PTGUILensLine = false;
bool PTGUILensLoaded = false;
ImgInfo PTGUILens;
// set new options to some sensible default.
options.tiff_saveROI = false;
options.blendMode = PanoramaOptions::ENBLEND_BLEND;
options.remapper = PanoramaOptions::NONA;
options.hdrMergeMode = PanoramaOptions::HDRMERGE_AVERAGE;
options.enblendOptions.clear();
options.enfuseOptions.clear();
options.hdrmergeOptions.clear();
options.outputLDRBlended = true;
options.outputLDRLayers = false;
options.outputLDRExposureLayers = false;
options.outputLDRExposureLayersFused = false;
options.outputHDRBlended = false;
options.outputHDRLayers = false;
options.outputHDRStacks = false;
ptoVersion = 1;
bool firstOptVecParse = true;
unsigned int lineNr = 0;
while (i.good()) {
std::getline(i, line);
lineNr++;
DEBUG_DEBUG(lineNr << ": " << line);
if (skipNextLine) {
skipNextLine = false;
continue;
}
//skip emtpy lines
if(line.empty())
continue;
// check for a known line
switch(line[0]) {
case 'p':
{
DEBUG_DEBUG("p line: " << line);
int i;
if (getIntParam(i,line,"f"))
options.setProjection( (PanoramaOptions::ProjectionFormat) i );
unsigned int w;
if (getIntParam(w, line, "w"))
options.setWidth(w);
double v;
if (getDoubleParam(v, line, "v"))
options.setHFOV(v, false);
int height;
if (getIntParam(height, line, "h"))
options.setHeight(height);
double newE;
if (getDoubleParam(newE, line, "E"))
options.outputExposureValue = newE;
int ar=0;
if (getIntParam(ar, line, "R"))
options.outputMode = (PanoramaOptions::OutputMode) ar;
string format;
if (getPTParam(format,line,"T"))
options.outputPixelType = format;
if ( getPTParam(format, line, "S") ) {
int left, right, top, bottom;
int n = sscanf(format.c_str(), "%d,%d,%d,%d", &left, &right, &top, &bottom);
if (n == 4) {
options.setROI(vigra::Rect2D(left, top, right, bottom));
} else {
DEBUG_WARN("Could not parse crop string: " << format);
}
}
// parse projection parameters
if (getPTParam(format,line,"P")) {
char * tstr = strdup(format.c_str());
std::vector<double> projParam;
char * b = strtok(tstr, " \"");
if (b != NULL) {
while (b != NULL) {
double tempDbl;
if (sscanf(b, "%lf", &tempDbl) == 1) {
projParam.push_back(tempDbl);
b = strtok(NULL, " \"");
}
}
}
free(tstr);
// only set projection parameters, if the have the right size.
if (projParam.size() == options.getProjectionParameters().size()) {
options.setProjectionParameters(projParam);
}
}
// this is fragile.. hope nobody adds additional whitespace
// and other arguments than q...
// n"JPEG q80"
if (getPTParam(format,line,"n")) {
int t = format.find(' ');
options.outputFormat = options.getFormatFromName(format.substr(0,t));
// parse output format options.
switch (options.outputFormat)
{
case PanoramaOptions::JPEG:
{
// "parse" jpg quality
int q;
if (getIntParam(q, format, "q") ) {
options.quality = (int) q;
}
}
break;
case PanoramaOptions::TIFF_m:
{
int coordImgs = 0;
if (getIntParam(coordImgs, format, "p"))
if (coordImgs)
options.saveCoordImgs = true;
}
case PanoramaOptions::TIFF:
case PanoramaOptions::TIFF_mask:
case PanoramaOptions::TIFF_multilayer:
case PanoramaOptions::TIFF_multilayer_mask:
{
// parse tiff compression mode
std::string comp;
if (getPTParam(comp, format, "c:")) {
if (comp == "NONE" || comp == "LZW" ||
comp == "PACKBITS" || comp == "DEFLATE")
{
options.tiffCompression = comp;
} else {
DEBUG_WARN("No valid tiff compression found");
}
}
// read tiff roi
if (getPTParam(comp, format, "r:")) {
if (comp == "CROP") {
options.tiff_saveROI = true;
} else {
options.tiff_saveROI = false;
}
}
}
break;
default:
break;
}
}
int cRefImg = 0;
if (getIntParam(cRefImg, line,"k")) {
options.colorCorrection = PanoramaOptions::BRIGHTNESS_COLOR;
} else if (getIntParam(cRefImg, line,"b")) {
options.colorCorrection = PanoramaOptions::BRIGHTNESS;
} else if (getIntParam(cRefImg, line,"d")) {
options.colorCorrection = PanoramaOptions::COLOR;
} else {
options.colorCorrection = PanoramaOptions::NONE;
}
options.colorReferenceImage=cRefImg;
break;
}
case 'm':
{
DEBUG_DEBUG("m line: " << line);
// parse misc options
int i;
if (getIntParam(i,line,"i"))
options.interpolator = (vigra_ext::Interpolator) i;
(void)getDoubleParam(options.gamma,line,"g");
if (getIntParam(i,line,"f")) {
switch(i) {
case 0:
options.remapAcceleration = PanoramaOptions::MAX_SPEEDUP;
break;
case 1:
options.remapAcceleration = PanoramaOptions::MEDIUM_SPEEDUP;
break;
default:
options.remapAcceleration = PanoramaOptions::NO_SPEEDUP;
break;
}
} else {
options.remapAcceleration = PanoramaOptions::NO_SPEEDUP;
}
break;
}
case 'v':
{
DEBUG_DEBUG("v line: " << line);
if (!PTGUIScriptFile) {
if (firstOptVecParse) {
int nImg = max(iImgInfo.size(), oImgInfo.size());
DEBUG_DEBUG("nImg: " << nImg);
optvec = OptimizeVector(nImg);
firstOptVecParse = false;
}
std::stringstream optstream;
optstream << line.substr(1);
string var;
while (!(optstream >> std::ws).eof()) {
optstream >> var;
if (var.length() == 1) {
// special case for PTGUI
var += "0";
}
// find first numerical character
std::string::size_type np = var.find_first_of("0123456789");
if (np == std::string::npos) {
// invalid, continue
continue;
}
std::string name=var.substr(0,np);
std::string number = var.substr(np);
unsigned int nr = hugin_utils::lexical_cast<unsigned int>(number);
DEBUG_ASSERT(nr < optvec.size());
optvec[nr].insert(name);
DEBUG_DEBUG("parsing opt: >" << var << "< : var:" << name << " image:" << nr);
}
}
break;
}
case 'c':
{
DEBUG_DEBUG("c line: " << line);
int t;
// read control points
ControlPoint point;
// TODO - should verify that line syntax is correct
getIntParam(point.image1Nr, line, "n");
point.image1Nr += ctrlPointsImgNrOffset;
getIntParam(point.image2Nr, line, "N");
point.image2Nr += ctrlPointsImgNrOffset;
getDoubleParam(point.x1, line, "x");
getDoubleParam(point.x2, line, "X");
getDoubleParam(point.y1, line, "y");
getDoubleParam(point.y2, line, "Y");
if (!getIntParam(t, line, "t") ){
t = 0;
}
point.mode = t;
ctrlPoints.push_back(point);
state = P_CP;
break;
}
// handle the complicated part.. the image & lens settings.
// treat i and o lines the same.. however, o lines have priority
// over i lines.(i lines often do not contain link information!)
case 'i':
{
if (PTGUILensLine) {
PTGUILensLine = false;
PTGUILensLoaded = true;
PTGUILens.parse(line);
} else {
iImgInfo.push_back(ImgInfo(line));
}
break;
}
case 'o':
{
if (PTGUILensLine) {
PTGUILensLine = false;
PTGUILensLoaded = true;
PTGUILens.parse(line);
} else {
oImgInfo.push_back(ImgInfo(line));
}
break;
}
case 'k':
{
unsigned int param;
if (getIntParam(param,line,"i"))
{
MaskPolygon newPolygon;
newPolygon.setImgNr(param);
if (getIntParam(param,line,"t"))
newPolygon.setMaskType((HuginBase::MaskPolygon::MaskType)param);
std::string format;
if (getPTParam(format,line,"p"))
{
if(newPolygon.parsePolygonString(format))
ImgMasks.push_back(newPolygon);
};
};
break;
}
case '#':
{
// parse special comments...
if (line.substr(0,20) == string("# ptGui project file")) {
PTGUIScriptFile = true;
}
if (line.substr(0,12) == "#-dummyimage") {
PTGUILensLine = true;
}
if (PTGUIScriptFile) {
// parse special PTGUI stuff.
if (sscanf(line.c_str(), "#-fileversion %d", &PTGUIScriptVersion) > 0) {
DEBUG_DEBUG("Detected PTGUI script version: " << PTGUIScriptVersion);
switch (PTGUIScriptVersion) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
ctrlPointsImgNrOffset = -1;
// latest known version is 8
break;
}
}
}
if (line.substr(0,8) == "#-hugin ") {
// read hugin image line
ImgInfo info;
info.autoCenterCrop = (line.find("autoCenterCrop=1") != std::string::npos);
size_t pos = line.find("cropFactor=");
if (pos > 0 && pos < line.length()) {
double cropFactor=1;
const char * s = line.c_str() + pos;
sscanf(s,"cropFactor=%lf", & cropFactor);
if(cropFactor<0.01 || cropFactor > 100)
cropFactor=1;
info.cropFactor = cropFactor;
}
pos = line.find("disabled");
if (pos > 0 && pos < line.length()) {
info.enabled = false;
}
huginImgInfo.push_back(info);
}
// PTGui and PTAssember project files:
// #-imgfile 960 1280 "D:\data\bruno\074-098\087.jpg"
if (line.substr(0,10) == "#-imgfile ") {
// arghhh. I like string processing without regexps.
int b = line.find_first_not_of(" ",9);
int e = line.find_first_of(" ",b);
DEBUG_DEBUG(" width:" << line.substr(b,e-b)<<":")
int nextWidth = hugin_utils::lexical_cast<int,string>(line.substr(b,e-b));
DEBUG_DEBUG("next width " << nextWidth);
b = line.find_first_not_of(" ",e);
e = line.find_first_of(" ",b);
DEBUG_DEBUG(" height:" << line.substr(b,e-b)<<":")
int nextHeight = hugin_utils::lexical_cast<int, string>(line.substr(b,e-b));
DEBUG_DEBUG("next height " << nextHeight);
string nextFilename;
try {
b = line.find_first_not_of(" \"",e);
e = line.find_first_of("\"",b);
nextFilename = line.substr(b,e-b);
} catch (std::out_of_range& e) {
DEBUG_ERROR("ERROR PARSING INPUT FILE" << e.what( ));
return false;
}
DEBUG_DEBUG("next filename " << nextFilename);
ImgInfo info;
info.width = nextWidth;
info.height = nextHeight;
info.filename = nextFilename;
cImgInfo.push_back(info);
}
// parse hugin properties
if (line.substr(0,7) == "#hugin_") {
istringstream is(line);
string var,value;
is >> var >> value;
if (!is.fail()) {
if (var == "#hugin_ptoversion") {
ptoVersion = atoi(value.c_str());
}
if (var == "#hugin_optimizeReferenceImage") {
options.optimizeReferenceImage = atoi(value.c_str());
} else if (var == "#hugin_remapper") {
if (value == "nona") {
options.remapper = PanoramaOptions::NONA;
} else if (value == "PTmender") {
options.remapper = PanoramaOptions::PTMENDER;
}
} else if (var == "#hugin_blender") {
if (value == "none") {
options.blendMode = PanoramaOptions::NO_BLEND;
} else if (value == "PTblender") {
options.blendMode = PanoramaOptions::PTBLENDER_BLEND;
} else if (value == "enblend") {
options.blendMode = PanoramaOptions::ENBLEND_BLEND;
} else if (value == "PTmasker") {
options.blendMode = PanoramaOptions::PTMASKER_BLEND;
} else if (value == "smartblend") {
options.blendMode = PanoramaOptions::SMARTBLEND_BLEND;
}
} else if (var == "#hugin_enblendOptions") {
options.enblendOptions = value;
while (!is.eof()) {
is >> value;
if (value.length() > 0) {
options.enblendOptions += " ";
options.enblendOptions += value;
}
}
} else if (var == "#hugin_enfuseOptions") {
options.enfuseOptions = value;
while (!is.eof()) {
is >> value;
if (value.length() > 0) {
options.enfuseOptions += " ";
options.enfuseOptions += value;
}
}
} else if (var == "#hugin_hdrmergeOptions") {
options.hdrmergeOptions = value;
while (!is.eof()) {
is >> value;
if (value.length() > 0) {
options.hdrmergeOptions += " ";
options.hdrmergeOptions += value;
}
}
} else if (var == "#hugin_outputLDRBlended") {
options.outputLDRBlended = (value == "true");
} else if (var == "#hugin_outputLDRLayers") {
options.outputLDRLayers = (value == "true");
} else if (var == "#hugin_outputLDRExposureRemapped") {
options.outputLDRExposureRemapped = (value == "true");
} else if (var == "#hugin_outputLDRExposureLayers") {
options.outputLDRExposureLayers = (value == "true");
} else if (var == "#hugin_outputLDRExposureBlended") {
options.outputLDRExposureBlended = (value == "true");
} else if (var == "#hugin_outputLDRExposureLayersFused") {
options.outputLDRExposureLayersFused = (value == "true");
} else if (var == "#hugin_outputHDRBlended") {
options.outputHDRBlended = (value == "true");
} else if (var == "#hugin_outputHDRLayers") {
options.outputHDRLayers = (value == "true");
} else if (var == "#hugin_outputHDRStacks") {
options.outputHDRStacks = (value == "true");
} else if (var == "#hugin_outputLayersCompression") {
options.outputLayersCompression = value;
} else if (var == "#hugin_outputImageType") {
options.outputImageType = value;
} else if (var == "#hugin_outputImageTypeCompression") {
options.outputImageTypeCompression = value;
} else if (var == "#hugin_outputJPEGQuality") {
options.quality = atoi(value.c_str());
} else if (var == "#hugin_outputImageTypeHDR") {
options.outputImageTypeHDR = value;
} else if (var == "#hugin_outputImageTypeHDRCompression") {
options.outputImageTypeHDRCompression = value;
}
}
}
break;
}
} // case
}
// assemble images from the information read before..
/** @todo What is the PTGUI special case? What images use the lens created here?
*/
#if 0
// handle PTGUI special case
if (PTGUILensLoaded) {
// create lens with dummy info
Lens l;
for (const char **v = Lens::variableNames; *v != 0; v++) {
map_get(l.variables, *v).setValue(PTGUILens.vars[*v]);
}
l.setImageSize(vigra::Size2D(PTGUILens.width, PTGUILens.height));
l.setCropFactor(1);
l.setProjection((Lens::LensProjectionFormat) PTGUILens.f);
lenses.push_back(l);
}
#endif
/*
// ugly hack to load PTGui script files
if (ptGUIDummyImage) {
DEBUG_DEBUG("loading default PTGUI line: " << line);
Lens l;
// skip ptgui's dummy image
// load parameters into default lens...
for (LensVarMap::iterator it = l.variables.begin();
it != l.variables.end();
++it)
{
DEBUG_DEBUG("reading default lens variable " << it->first);
int link;
bool ok = readVar(it->second, link, line);
DEBUG_ASSERT(ok);
DEBUG_ASSERT(link == -1);
}
lenses.push_back(l);
ptGUIDummyImage = false;
break;
}
*/
// merge image info from the 3 different lines...
// i lines are the main reference.
int nImgs = iImgInfo.size();
int nOLines = oImgInfo.size();
int nCLines = cImgInfo.size();
if (nImgs < nOLines) {
// no, or less i lines found. scrap i lines.
DEBUG_DEBUG("throwing away " << nImgs << " i lines");
iImgInfo = oImgInfo;
nImgs = nOLines;
}
if (nOLines < nImgs) {
oImgInfo = iImgInfo;
}
// merge o lines and i lines into i lines.
for (int i=0; i < nImgs; i++) {
// move parameters from o lines -> i (only if it isn't given in the
// i lines. or it is linked on the o lines)
// ordinary variables
for (const char ** v = ImgInfo::varnames; *v ; v++) {
if (iImgInfo[i].links[*v] == -2 && oImgInfo[i].links[*v] != -2 || iImgInfo[i].links[*v] == -1 && oImgInfo[i].links[*v] >=0) {
DEBUG_DEBUG(*v << ": o -> i");
iImgInfo[i].vars[*v] = oImgInfo[i].vars[*v];
iImgInfo[i].links[*v] = oImgInfo[i].links[*v];
}
}
if (iImgInfo[i].filename == "" && oImgInfo[i].filename != "") {
DEBUG_DEBUG("filename: o -> i");
iImgInfo[i].filename = oImgInfo[i].filename;
}
if (iImgInfo[i].crop.isEmpty() && !oImgInfo[i].crop.isEmpty()) {
DEBUG_DEBUG("crop: o -> i");
iImgInfo[i].crop = oImgInfo[i].crop;
}
if (iImgInfo[i].width <= 0 && oImgInfo[i].width > 0) {
DEBUG_DEBUG("width: o -> i");
iImgInfo[i].width = oImgInfo[i].width;
}
if (iImgInfo[i].height <= 0 && oImgInfo[i].height > 0) {
DEBUG_DEBUG("height: o -> i");
iImgInfo[i].height = oImgInfo[i].height;
}
if (iImgInfo[i].f < 0 && oImgInfo[i].f > 0) {
DEBUG_DEBUG("f: o -> i");
iImgInfo[i].f = oImgInfo[i].f;
}
if (nCLines == nImgs) {
// img file & size in clines
if (cImgInfo[i].filename != "" && cImgInfo[i].width > 0) {
DEBUG_DEBUG("filename, width, height: c -> i");
iImgInfo[i].filename = cImgInfo[i].filename;
iImgInfo[i].width = cImgInfo[i].width;
iImgInfo[i].height = cImgInfo[i].height;
}
}
if (huginImgInfo.size() == (size_t)nImgs) {
iImgInfo[i].cropFactor = huginImgInfo[i].cropFactor;
iImgInfo[i].autoCenterCrop = huginImgInfo[i].autoCenterCrop;
iImgInfo[i].enabled= huginImgInfo[i].enabled;
}
}
// create images.
for (int i=0; i < nImgs; i++) {
DEBUG_DEBUG("i line: " << i);
// read the variables
VariableMap vars;
int link = -2;
fillVariableMap(vars);
for (const char ** v = ImgInfo::varnames; *v != 0; v++) {
std::string name(*v);
double val = iImgInfo[i].vars[*v];
map_get(vars,name).setValue(val);
if (iImgInfo[i].links[*v] >= 0) {
link = iImgInfo[i].links[*v];
}
}
string file = iImgInfo[i].filename;
// add prefix if only a relative path.
#ifdef WIN32
bool absPath = ( (file[1]==':' && file[2]=='\\') || (file[1]==':' && file[2]=='/') || (file[0] == '\\' && file[1] == '\\'));
#else
bool absPath = file[0] == '/';
#endif
if (!absPath) {
file.insert(0, prefix);
}
DEBUG_DEBUG("filename: " << file);
// Make a new SrcPanoImage in this list for expressing this one.
SrcPanoImage * new_img_p = new SrcPanoImage();
images.push_back(new_img_p);
// and make a reference to it so we dont keep using images.back(),
SrcPanoImage & new_img = *new_img_p;
new_img.setFilename(file);
new_img.setSize(vigra::Size2D(iImgInfo[i].width, iImgInfo[i].height));
// Panotools Script variables for the current SrcPanoImage variable.
// We just need the names.
VariableMap vars_for_name;
// A dummy SrcPanoImage to use to fill vars_for_name.
SrcPanoImage name_src;
/* Set image variables in new_img using the PanoTools Script names, with
* linking where specified in the file.
*
* We create a list of PanoTools script variable names for each variable in
* SrcPanoImg (vars_for_name). It may have multiple variables or none at all.
* If a link is found between any of the variables inside it we can link them up
* in new_img.
*
* The Panorama Tools script format makes it possible to link parts of the same
* SrcPanoImage variable differently (e.g. you can link the horizontal component
* of shearing to a different target to the vertical component, not link the
* vertical component at all). SrcPanoImage cannot handle this, so we end up
* linking all components of the same variable to anything specified in the PTO
* script for one of the components: most often we specify the same link
* multiple times.
*/
/** @todo Warn the user when the script links variables in a way not expressable
* by SrcPanoImage.
*/
#define RESET_LOCALE setlocale(LC_NUMERIC,old_locale); free(old_locale);
#define image_variable( name, type, default_value )\
PTOVariableConverterFor##name::addToVariableMap(name_src.get##name##IV(), vars_for_name);\
for (VariableMap::iterator vit = vars_for_name.begin();\
vit != vars_for_name.end(); vit++)\
{\
if (link >= 0 && iImgInfo[i].links[vit->first] >= 0)\
{\
if ( !(PTGUILensLoaded && link == 0)\
&& (int) images.size() < link && (!PTGUILensLoaded))\
{\
DEBUG_ERROR("variables must be linked to an image with a lower number" << endl\
<< "number links: " << link << " images: " << images.size() << endl\
<< "error on line " << lineNr << ":" << endl\
<< line);\
RESET_LOCALE\
return false;\
}\
DEBUG_DEBUG("anchored to image " << iImgInfo[i].links[vit->first]);\
new_img.link##name(images[iImgInfo[i].links[vit->first]]);\
} else {\
double val = map_get(vars, vit->first).getValue();\
new_img.setVar(vit->first, val);\
}\
}\
vars_for_name.clear();
#include "image_variables.h"
#undef image_variable
new_img.setProjection((SrcPanoImage::Projection) iImgInfo[i].f);
#if 0
new_img.setFeatherWidth((unsigned int) iImgInfo[i].blend_radius);
#endif
// is this right?
new_img.setExifCropFactor(iImgInfo[i].cropFactor);
new_img.setVigCorrMode(iImgInfo[i].vigcorrMode);
new_img.setFlatfieldFilename(iImgInfo[i].flatfieldname);
new_img.setResponseType((SrcPanoImage::ResponseType)iImgInfo[i].responseType);
new_img.setAutoCenterCrop(iImgInfo[i].autoCenterCrop);
new_img.setActive(iImgInfo[i].enabled);
if (!iImgInfo[i].crop.isEmpty()) {
if (new_img.getProjection() == SrcPanoImage::CIRCULAR_FISHEYE || new_img.getProjection() == SrcPanoImage::FISHEYE_THOBY)
{
new_img.setCropMode(SrcPanoImage::CROP_CIRCLE);
} else {
new_img.setCropMode(SrcPanoImage::CROP_RECTANGLE);
}
new_img.setCropRect(iImgInfo[i].crop);
}
//now fill the mask
for(unsigned int j=0;j<ImgMasks.size();j++)
if(ImgMasks[j].getImgNr()==i)
//now clip mask to image size + offset
if(ImgMasks[j].clipPolygon(vigra::Rect2D(-0.9*maskOffset,-0.9*maskOffset,
new_img.getWidth()+0.9*maskOffset,new_img.getHeight()+0.9*maskOffset)))
{
new_img.addMask(ImgMasks[j]);
};
}
// if we haven't found a v line in the project file
if (optvec.size() != images.size()) {
optvec = OptimizeVector(images.size());
}
// reset locale
setlocale(LC_NUMERIC,old_locale);
free(old_locale);
return true;
}
} // namespace