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
|
/* Panoama_Tools - Generate, Edit and Convert Panoramic Images
Copyright (C) 1998,1999 - Helmut Dersch der@fh-furtwangen.de
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, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this software; see the file COPYING. If not, a copy
can be downloaded from http://www.gnu.org/licenses/gpl.html, or
obtained by writing to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
/*
Modifications by Max Lyons (maxlyons@erols.com):
March 16, 2002. Added LINE_LENGTH to increase the amount of memory
allocated when reading optimizer scripts. Needed to reduce constraint
on number of parameters than can be read by optimizer
/ *------------------------------------------------------------*/
/*
Modifications by Fulvio Senore, June.2004
Added support for a new letter "f" to enable fast pisels transform in "ReadModeDescription()"
Added code between
// FS+
and
// FS-
/ *------------------------------------------------------------*/
#include <assert.h>
#include "filter.h"
#include <locale.h>
#include "ZComb.h"
#include "adjust.h"
/* defined in adjust.c */
int AddEdgePoints( AlignInfo *gl );
void setFcnPanoHuberSigma(double sigma);
static int ReadControlPoint ( controlPoint * cptr, char *line);
static int ReadImageDescription( Image *imPtr, stBuf *sPtr, char *line );
static int ReadPanoramaDescription( Image *imPtr, stBuf *sPtr, char *line );
static int ReadModeDescription ( sPrefs *sP, char *line );
static int ReadCoordinates( CoordInfo *cp, char *line );
static int panoExternalToInternalInputProjection(int32_t input);
#define MY_SSCANF( str, format, ptr ) if( sscanf( str, format, ptr ) != 1 ) \
{ \
PrintError( \
"Syntax error in script: Line %d\nCould not assign variable [%s]",\
lineNum, str); \
goto fail; \
} \
#define READ_VAR(format, ptr ) nextWord( buf, &li ); \
MY_SSCANF( buf, format, ptr );
#define READ_OPT_VAR(var) nextWord( buf, &li ); \
MY_SSCANF( buf, "%d", &k); \
if( k<0 || k>= numIm ) \
{ \
PrintError("Syntax error in script: Line %d\n\nIllegal image number: %ld", lineNum, k);\
goto fail; \
} \
if( gl->opt[k].var ) \
{ \
PrintError("Conflict in script: Line %d. Multiple Instances of Variable %s Image number: %d (%d)", lineNum, #var, k, gl->opt[k].var); \
goto fail; \
} \
gl->opt[k].var = 1; \
n++; \
//Increased so more params can be parsed/optimized (MRDL - March 2002)
#define LINE_LENGTH 65536
#define panoLocaleSave char *oldLocale;oldLocale=strdup(setlocale(LC_ALL, NULL));setlocale(LC_ALL, "C")
#define panoLocaleRestore (oldLocale != NULL? (setlocale(LC_ALL,oldLocale),free(oldLocale)):0)
// Optimizer Script parser; fill global info structure
char *panoParseVariable(char *buf, char *li, int lineNum, int imgNr, int *indirectVar, double *var, char *varName )
{
if (*(li+1) == '=') {
li++; // point to next character
nextWord( buf, &li );
if( sscanf( buf, "%d", indirectVar ) != 1 ) {
PrintError("Syntax error in script: Line %d\nCould not link variable %s with \"%s\"", lineNum, varName, buf);
return NULL;
}
if ((*indirectVar) < 0 || (*indirectVar) >= imgNr)
{
PrintError("Syntax error in script : Line %d\nLinking variable %s forward or to itself is not allowed", lineNum, varName);
return NULL;
};
(*indirectVar)+=2; //its offset should be increased by 2... arghh
} else {
nextWord( buf, &li );
if( sscanf( buf, " %lf", var ) != 1 ) {
PrintError("Syntax error in script: Line %d\nCould not assign variable %s content \"%s\"", lineNum, varName, buf);
return NULL;
}
}
return li;
}
int ParseScript( char* script, AlignInfo *gl )
{
Image *im;
optVars *opt;
CoordInfo *ci;
// Variables used by parser
char *li, line[LINE_LENGTH], *ch ,*lineStart, buf[LINE_LENGTH];
int lineNum = 1;
int i,k;
int n=0; // Number of parameters to optimize
int numIm,numPts,nt;
panoLocaleSave;
gl->im = NULL;
gl->opt = NULL;
gl->cpt = NULL;
gl->t = NULL;
gl->cim = NULL;
// Determine number of images and control points
gl->numIm = numLines( script, 'i' );
gl->numPts = numLines( script, 'c' );
gl->nt = numLines( script, 't' );
// Allocate Space for Pointers to images, preferences and control points
gl->im = (Image*) malloc( gl->numIm * sizeof(Image) );
gl->opt = (optVars*) malloc( gl->numIm * sizeof(optVars) );
gl->cpt = (controlPoint*) malloc( gl->numPts * sizeof( controlPoint ));
gl->t = (triangle*) malloc( gl->nt * sizeof( triangle ));
gl->cim = (CoordInfo*) malloc( gl->numIm * sizeof(CoordInfo) );
if( gl->im == NULL || gl->opt == NULL || gl->cpt == NULL || gl->t == NULL || gl->cim == NULL ) {
PrintError("Not enough memory");
goto fail;
}
// Rik's mask-from-focus hacking
ZCombSetDisabled();
// end mask-from-focus Rik's hacking
SetImageDefaults(&(gl->pano));
SetStitchDefaults(&(gl->st)); strcpy( gl->st.srcName, "buf" ); // Default: Use buffer 'buf' for stitching
// printf("Number of images %d\n",gl-> numIm);
for(i=0; i<gl->numIm; i++) {
SetImageDefaults( &(gl->im[i]) );
SetOptDefaults ( &(gl->opt[i]));
SetCoordDefaults( &(gl->cim[i]), i);
}
numIm =0; numPts = 0; nt = 0; // reused as indices
// Parse script
ch = script;
while( *ch != 0 ) {
while(*ch == '\n')
{
ch++;
lineNum++;
};
lineStart = ch;
nextLine( line, &ch );
// parse line; use only if first character is i,p,v,c,m
switch( line[0] ) {
case 'i': // Image description
im = &(gl->im[numIm]); // This image is being set
opt = &(gl->opt[numIm]);
ci = &(gl->cim[numIm]);
li = &(line[1]);
while( *li != 0) {
switch(*li)
{
case 'w': READ_VAR( "%ud", &(im->width) ); break;
case 'h': READ_VAR( "%ud", &(im->height)); break;
case 'v':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->hfov), &(im->hfov), "v");
break;
case 'a':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->a), &(im->cP.radial_params[0][3]), "a");
im->cP.radial = TRUE;
break;
case 'b':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->b), &(im->cP.radial_params[0][2]), "b");
im->cP.radial = TRUE;
break;
case 'c':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->c), &(im->cP.radial_params[0][1]), "c");
im->cP.radial = TRUE;
break;
case 'f':
READ_VAR( "%d", &k );
switch (k)
{
case IMAGE_FORMAT_RECTILINEAR: im->format = _rectilinear; break;
case IMAGE_FORMAT_PANORAMA: im->format = _panorama;
im->cP.correction_mode |= correction_mode_vertical;
break;
case IMAGE_FORMAT_FISHEYE_EQUIDISTANCECIRC: im->format = _fisheye_circ; break;
case IMAGE_FORMAT_FISHEYE_EQUIDISTANCEFF: im->format = _fisheye_ff; break;
case IMAGE_FORMAT_EQUIRECTANGULAR: im->format = _equirectangular;
im->cP.correction_mode |= correction_mode_vertical;
break;
case IMAGE_FORMAT_MIRROR: im->format = _mirror; break;
case IMAGE_FORMAT_FISHEYE_ORTHOGRAPHIC: im->format = _orthographic; break;
case IMAGE_FORMAT_FISHEYE_STEREOGRAPHIC: im->format = _stereographic; break;
case IMAGE_FORMAT_FISHEYE_EQUISOLID: im->format = _equisolid; break;
case IMAGE_FORMAT_FISHEYE_THOBY: im->format = _thoby; break;
break;
default: PrintError("Syntax error in script. Projection not known: Line %d", lineNum);
goto fail;
break;
}
break;
case 'o': li++;
im->cP.correction_mode |= correction_mode_morph;
break;
case 'y':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->yaw), &(im->yaw), "y");
break;
case 'p':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->pitch), &(im->pitch), "p");
break;
case 'r':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->roll), &(im->roll), "r");
break;
case 'd':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->d), &(im->cP.horizontal_params[0]), "d");
im->cP.horizontal= TRUE;
break;
case 'e':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->e), &(im->cP.vertical_params[0]), "d");
im->cP.vertical = TRUE;
break;
case 'g':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->shear_x), &(im->cP.shear_x), "g");
im->cP.shear = TRUE;
break;
case 't':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->shear_y), &(im->cP.shear_y), "t");
im->cP.shear = TRUE;
break;
case 'T':
li++;
switch (*li) {
case 'i': // Tilt
li++;
switch (*li) {
case 'X':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->tiltXopt), &(im->cP.tilt_x), "TiX");
break;
case 'Y':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->tiltYopt), &(im->cP.tilt_y), "TiY");
break;
case 'Z':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->tiltZopt), &(im->cP.tilt_z), "TiZ");
break;
case 'S':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->tiltScaleOpt), &(im->cP.tilt_scale), "TiS");
if (opt->tiltScaleOpt > 1) {
// it is an actual value to optimize, not a reference... check it
if (im->cP.tilt_scale == 0) {
PrintError("TiS parameter can't be zero. Error in script: Line %d", lineNum);
goto fail;
}
}
break;
default:
PrintError("Unkonwn parameter Ti%c in script: Line %d", *li, lineNum);
goto fail;
}
if (li == NULL) goto fail;
im->cP.tilt = TRUE;
break;
case 'r': // Translation
li++;
switch (*li) {
case 'X':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->transXopt), &(im->cP.trans_x), "TrX");
break;
case 'Y':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->transYopt), &(im->cP.trans_y), "TrY");
break;
case 'Z':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->transZopt), &(im->cP.trans_z), "TrZ");
break;
default:
PrintError("Unknown translation parameter Tr%c in script: Line %d", *li, lineNum);
goto fail;
}
if (li == NULL) goto fail;
// Make sure that we only apply trans when these parameters are not zero
// Otherwise images are not rendered beyond 180 degrees FOV
if (im->cP.trans_x != 0.0 ||
im->cP.trans_y != 0.0 ||
im->cP.trans_z != 0.0) {
im->cP.trans = TRUE;
}
break;
case 'p': // Translation remap plane
li++;
switch (*li) {
case 'y':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->transYawOpt), &(im->cP.trans_yaw), "Tpy");
break;
case 'p':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->transPitchOpt), &(im->cP.trans_pitch), "Tpp");
break;
default:
PrintError("Unknown translation parameter Tp%c in script: Line %d", *li, lineNum);
goto fail;
}
if (li == NULL) goto fail;
break;
case 'e': // test parameters
li++;
switch (*li) {
case '0':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->testP0opt), &(im->cP.test_p0), "Te0");
break;
case '1':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->testP1opt), &(im->cP.test_p1), "Te1");
break;
case '2':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->testP2opt), &(im->cP.test_p2), "Te2");
break;
case '3':
li = panoParseVariable(buf, li, lineNum, numIm, &(opt->testP3opt), &(im->cP.test_p3), "Te3");
break;
default:
PrintError("Unknown Test parameter Te%c in script: Line %d", *li, lineNum);
goto fail;
}
if (li == NULL) goto fail;
im->cP.test = TRUE;
break;
default:
PrintError("Unkonwn parameter T%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
case 'n': // Set filename
nextWord( buf, &li );
snprintf( im->name, sizeof(im->name)-1, "%s", buf );
break;
case 'm': // Frame
li++;
switch( *li )
{
case 'x':
READ_VAR("%d", &(im->cP.fwidth) );
im->cP.cutFrame = TRUE;
break;
case 'y':
READ_VAR("%d", &(im->cP.fheight) );
im->cP.cutFrame = TRUE;
break;
default:
li--;
READ_VAR("%d", &(im->cP.frame) );
im->cP.cutFrame = TRUE;
break;
}
break;
case 'X': READ_VAR( "%lf", &ci->x[0] );
break;
case 'Y': READ_VAR( "%lf", &ci->x[1] );
break;
case 'Z': READ_VAR( "%lf", &ci->x[2] );
break;
case 'S': nextWord( buf, &li );
sscanf( buf, "%d,%d,%d,%d", &im->selection.left, &im->selection.right, &im->selection.top, &im->selection.bottom );
break;
case 'C': nextWord( buf, &li );
sscanf( buf, "%d,%d,%d,%d", &im->selection.left, &im->selection.right, &im->selection.top, &im->selection.bottom );
im->cP.cutFrame = TRUE;
break;
case 'V':
case 'K':
// Ignore V variables in i
nextWord( buf, &li );
break;
default:
li++;
break;
}
}
numIm++;
break;
case 't': // Triangle
li = &(line[1]);
i = 0;
while( *li != 0)
{
switch(*li)
{
case ' ' :
case '\t': li++; break;
case 'i' : READ_VAR( "%d", &(gl->t[nt].nIm)); break;
default : if(i<3)
{
li--;
READ_VAR( "%d", &(gl->t[nt].vert[i]) );
i++;
}
else
li++;
break;
}
}
nt++;
break;
case 'c': // Control Points
gl->cpt[numPts].type = 0; // default : optimize r
if( ReadControlPoint( &(gl->cpt[numPts]), &(line[1]) ) != 0 )
{
PrintError("Syntax error in script in control point 'c': Line %d", lineNum);
goto fail;
}
numPts++;
break;
case 'm': // Mode description
if( ReadModeDescription( &gl->sP, &(line[1]) ) != 0 )
{
PrintError( "Syntax error in script in mode description 'm': line %d" , lineNum);
goto fail;
}
break;
case 'v': // Variables to optimize
li = &(line[1]);
while( *li != 0)
{
switch(*li)
{
case 'y': READ_OPT_VAR(yaw);
break;
case 'p': READ_OPT_VAR(pitch);
break;
case 'r': READ_OPT_VAR(roll);
break;
case 'v': READ_OPT_VAR(hfov);
break;
case 'a': READ_OPT_VAR(a);
break;
case 'b': READ_OPT_VAR(b);
break;
case 'c': READ_OPT_VAR(c);
break;
case 'd': READ_OPT_VAR(d);
break;
case 'e': READ_OPT_VAR(e);
break;
case 'g': READ_OPT_VAR(shear_x);
break;
case 't': READ_OPT_VAR(shear_y);
break;
case 'T':
li++;
switch (*li) {
case 'i':
li++;
switch (*li) {
case 'X':
READ_OPT_VAR(tiltXopt);
break;
case 'Y':
READ_OPT_VAR(tiltYopt);
break;
case 'Z':
READ_OPT_VAR(tiltZopt);
break;
case 'S':
READ_OPT_VAR(tiltScaleOpt);
break;
default:
PrintError("Unknown variable name variable to optimize Ti%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
case 'r':
li++;
switch (*li) {
case 'X':
READ_OPT_VAR(transXopt);
break;
case 'Y':
READ_OPT_VAR(transYopt);
break;
case 'Z':
READ_OPT_VAR(transZopt);
break;
default:
PrintError("Unknown variable name to optimize Tr%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
case 'p':
li++;
switch (*li) {
case 'y':
READ_OPT_VAR(transYawOpt);
break;
case 'p':
READ_OPT_VAR(transPitchOpt);
break;
default:
PrintError("Unknown variable name to optimize Tp%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
case 'e':
li++;
switch (*li) {
case '0':
READ_OPT_VAR(testP0opt);
break;
case '1':
READ_OPT_VAR(testP1opt);
break;
case '2':
READ_OPT_VAR(testP2opt);
break;
case '3':
READ_OPT_VAR(testP3opt);
break;
default:
PrintError("Unknown variable name to optimize Te%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
default:
PrintError("Unkonwn parameter T%c in script: Line %d", *li, lineNum);
goto fail;
}
break;
case 'X': READ_VAR( "%d", &k );
if( k>=0 && k<gl->numIm )
gl->cim[k].set[0] = FALSE;
break;
case 'Y': READ_VAR( "%d", &k );
if( k>=0 && k<gl->numIm )
gl->cim[k].set[1] = FALSE;
break;
case 'Z': READ_VAR( "%d", &k );
if( k>=0 && k<gl->numIm )
gl->cim[k].set[2] = FALSE;
break;
default:
if (!isspace(*li))
{
--li; nextWord(buf, &li);
PrintError("Unknown variable name to optimize %s in script: Line %d", buf, lineNum);
goto fail;
}
li++;
break;
}
}
break;
case 'p': // panorama
gl->pano.format = 2; // _equirectangular by default
gl->pano.hfov = 360.0;
if( ReadPanoramaDescription( &(gl->pano), &(gl->st), &(line[1]) ) != 0 )
{
PrintError( "Syntax error in panorama description p line: %d (%s)" , lineNum,line);
goto fail;
}
switch (gl->pano.format) {
case PANO_FORMAT_RECTILINEAR:
gl->pano.format = _rectilinear;
break;
case PANO_FORMAT_PANORAMA:
gl->pano.format = _panorama;
break;
case PANO_FORMAT_EQUIRECTANGULAR:
gl->pano.format = _equirectangular;
break;
case PANO_FORMAT_FISHEYE_FF:
gl->pano.format = _fisheye_ff;
break;
case PANO_FORMAT_STEREOGRAPHIC:
gl->pano.format = _stereographic;
break;
case PANO_FORMAT_MERCATOR:
gl->pano.format = _mercator;
break;
case PANO_FORMAT_TRANS_MERCATOR:
gl->pano.format = _trans_mercator;
break;
case PANO_FORMAT_SINUSOIDAL:
gl->pano.format = _sinusoidal;
break;
case PANO_FORMAT_LAMBERT_EQUAL_AREA_CONIC:
gl->pano.format = _lambert;
break;
case PANO_FORMAT_LAMBERT_AZIMUTHAL:
gl->pano.format = _lambertazimuthal;
break;
case PANO_FORMAT_HAMMER:
gl->pano.format = _hammer;
break;
case PANO_FORMAT_ALBERS_EQUAL_AREA_CONIC:
gl->pano.format = _albersequalareaconic;
break;
case PANO_FORMAT_MILLER_CYLINDRICAL:
gl->pano.format = _millercylindrical;
break;
case PANO_FORMAT_PANINI:
gl->pano.format = _panini;
break;
case PANO_FORMAT_EQUI_PANINI:
gl->pano.format = _equipanini;
break;
case PANO_FORMAT_PANINI_GENERAL:
gl->pano.format = _panini_general;
break;
case PANO_FORMAT_ARCHITECTURAL:
gl->pano.format = _architectural;
break;
case PANO_FORMAT_ORTHOGRAPHIC:
gl->pano.format = _orthographic;
break;
case PANO_FORMAT_THOBY:
gl->pano.format = _thoby;
break;
case PANO_FORMAT_EQUISOLID:
gl->pano.format = _equisolid;
break;
case PANO_FORMAT_BIPLANE:
gl->pano.format = _biplane;
break;
case PANO_FORMAT_TRIPLANE:
gl->pano.format = _triplane;
break;
default:
PrintError( "Unknown panorama projection: %d", gl->pano.format );
goto fail;
}
if( (gl->pano.format == _rectilinear || gl->pano.format == _trans_mercator) && gl->pano.hfov >= 180.0 )
{
PrintError( "Destination image must have HFOV < 180" );
goto fail;
}
break;
// Rik's mask-from-focus hacking
case 'z':
// I was tempted to remove this code, but I am not sure how it will affect PToptimizer, until then.. it will remain
//PrintError( "z option is no longer supported by PTmender and it is ignored. Use PTmasker instead\n" );
//ZCombSetEnabled();
li = &(line[1]);
while( *li != 0)
{
switch(*li)
{
case 'm': { int mtype;
READ_VAR( "%d", &mtype);
ZCombSetMaskType(mtype);
}
break;
case 'f': { int fwHalfwidth;
READ_VAR( "%d", &fwHalfwidth);
ZCombSetFocusWindowHalfwidth(fwHalfwidth);
}
break;
case 's': { int swHalfwidth;
READ_VAR( "%d", &swHalfwidth);
ZCombSetSmoothingWindowHalfwidth(swHalfwidth);
}
break;
default:
li++;
break;
}
}
break;
// end Rik's mask-from-focus hacking
case '*': // End of script-data
*lineStart = 0; *ch = 0;
break;
default: break;
}
}
// Set up Panorama description
if( gl->pano.width == 0 && gl->im[0].hfov != 0.0) // Set default for panorama width based on first image
{
gl->pano.width = ( gl->pano.hfov / gl->im[0].hfov ) * gl->im[0].width;
gl->pano.width /= 10; gl->pano.width *= 10; // Round to multiple of 10
}
if( gl->pano.height == 0 )
gl->pano.height = gl->pano.width/2;
// Set up global information structure
gl->numParam = n;
gl->data = NULL;
// Set initial values for linked variables
for(i=0; i<gl->numIm; i++){
k = gl->opt[i].yaw - 2;
if( k >= 0 ) gl->im[i].yaw = gl->im[ k ].yaw;
k = gl->opt[i].pitch - 2;
if( k >= 0 ) gl->im[i].pitch = gl->im[ k ].pitch;
k = gl->opt[i].roll - 2;
if( k >= 0 ) gl->im[i].roll = gl->im[ k ].roll;
k = gl->opt[i].hfov - 2;
if( k >= 0 ) gl->im[i].hfov = gl->im[ k ].hfov;
k = gl->opt[i].a - 2;
if( k >= 0 ) gl->im[i].cP.radial_params[0][3] = gl->im[ k ].cP.radial_params[0][3];
k = gl->opt[i].b - 2;
if( k >= 0 ) gl->im[i].cP.radial_params[0][2] = gl->im[ k ].cP.radial_params[0][2];
k = gl->opt[i].c - 2;
if( k >= 0 ) gl->im[i].cP.radial_params[0][1] = gl->im[ k ].cP.radial_params[0][1];
k = gl->opt[i].d - 2;
if( k >= 0 ) gl->im[i].cP.horizontal_params[0] = gl->im[ k ].cP.horizontal_params[0];
k = gl->opt[i].e - 2;
if( k >= 0 ) gl->im[i].cP.vertical_params[0] = gl->im[ k ].cP.vertical_params[0];
k = gl->opt[i].shear_x - 2;
if( k >= 0 ) gl->im[i].cP.shear_x = gl->im[ k ].cP.shear_x;
k = gl->opt[i].shear_y - 2;
if( k >= 0 ) gl->im[i].cP.shear_y = gl->im[ k ].cP.shear_y;
// tilt variables----------------------------------------------------------------
k = gl->opt[i].tiltXopt - 2;
if( k >= 0 ) gl->im[i].cP.tilt_x = gl->im[ k ].cP.tilt_x;
k = gl->opt[i].tiltYopt - 2;
if( k >= 0 ) gl->im[i].cP.tilt_y = gl->im[ k ].cP.tilt_y;
k = gl->opt[i].tiltZopt - 2;
if( k >= 0 ) gl->im[i].cP.tilt_z = gl->im[ k ].cP.tilt_z;
k = gl->opt[i].tiltScaleOpt - 2;
if( k >= 0 ) gl->im[i].cP.tilt_scale = gl->im[ k ].cP.tilt_scale;
// translation variables----------------------------------------------------------------
k = gl->opt[i].transXopt - 2;
if( k >= 0 ) gl->im[i].cP.trans_x = gl->im[ k ].cP.trans_x;
k = gl->opt[i].transYopt - 2;
if( k >= 0 ) gl->im[i].cP.trans_y = gl->im[ k ].cP.trans_y;
k = gl->opt[i].transZopt - 2;
if( k >= 0 ) gl->im[i].cP.trans_z = gl->im[ k ].cP.trans_z;
k = gl->opt[i].transYawOpt - 2;
if( k >= 0 ) gl->im[i].cP.trans_yaw = gl->im[ k ].cP.trans_yaw;
k = gl->opt[i].transPitchOpt - 2;
if( k >= 0 ) gl->im[i].cP.trans_pitch = gl->im[ k ].cP.trans_pitch;
// test variables ----------------------------------------------------------------------
k = gl->opt[i].testP0opt - 2;
if( k >= 0 ) gl->im[i].cP.test_p0 = gl->im[ k ].cP.test_p0;
k = gl->opt[i].testP1opt - 2;
if( k >= 0 ) gl->im[i].cP.test_p1 = gl->im[ k ].cP.test_p1;
k = gl->opt[i].testP2opt - 2;
if( k >= 0 ) gl->im[i].cP.test_p2 = gl->im[ k ].cP.test_p2;
k = gl->opt[i].testP3opt - 2;
if( k >= 0 ) gl->im[i].cP.test_p3 = gl->im[ k ].cP.test_p3;
//----------------------------------------------------------------------
gl->im[i].cP.radial_params[0][0] = 1.0 - ( gl->im[i].cP.radial_params[0][3]
+ gl->im[i].cP.radial_params[0][2]
+ gl->im[i].cP.radial_params[0][1] ) ;
SetEquColor( &(gl->im[i].cP) );
}
panoLocaleRestore;
return 0;
fail:
panoLocaleRestore;
return -1;
}
// Report Results
void WriteResults( char* script, fullPath *sfile, AlignInfo *g, double ds( int i), int launch)
{
char *res, **hres, *line;
int optHfov; // Has hfov being optimized?
int opta,optb,optc; // same for a,b,c - the polynomial corrections
int format;
int i;
panoLocaleSave;
hres = (char**) mymalloc( strlen(script) + (size_t)(g->numIm) * 1000 + (size_t)(g->numPts) * 300 + 10000 ); // Do we ever need more?
if( hres == NULL )
{
PrintError("Not enough memory to create resultfile");
goto fail;
}
line = res = *hres;
line += sprintf( line, "%s", script );
line += sprintf( line, "\n*\n\n" );
line += sprintf( line, "# ====================================================================\n");
line += sprintf( line, "# Output generated by Panorama Tools\n\n" );
if( g->data != NULL )
line += sprintf( line, "%s", (char*) g->data );
line += sprintf( line, "\n# Panorama description\n" );
switch( g->pano.format )
{
case _rectilinear: format = PANO_FORMAT_RECTILINEAR; break;
case _panorama: format = PANO_FORMAT_PANORAMA; break;
case _equirectangular: format = PANO_FORMAT_EQUIRECTANGULAR; break;
case _fisheye_ff: format = PANO_FORMAT_FISHEYE_FF; break;
case _stereographic: format = PANO_FORMAT_STEREOGRAPHIC; break;
case _mercator: format = PANO_FORMAT_MERCATOR; break;
case _trans_mercator: format = PANO_FORMAT_TRANS_MERCATOR; break;
case _sinusoidal: format = PANO_FORMAT_SINUSOIDAL; break;
case _lambert: format = PANO_FORMAT_LAMBERT_EQUAL_AREA_CONIC; break;
case _lambertazimuthal: format = PANO_FORMAT_LAMBERT_AZIMUTHAL; break;
case _hammer: format = PANO_FORMAT_HAMMER; break;
case _albersequalareaconic: format = PANO_FORMAT_ALBERS_EQUAL_AREA_CONIC; break;
case _millercylindrical: format = PANO_FORMAT_MILLER_CYLINDRICAL; break;
case _panini: format = PANO_FORMAT_PANINI; break;
case _equipanini: format = PANO_FORMAT_EQUI_PANINI; break;
case _architectural: format = PANO_FORMAT_ARCHITECTURAL; break;
case _orthographic: format = PANO_FORMAT_ORTHOGRAPHIC; break;
case _equisolid: format = PANO_FORMAT_EQUISOLID; break;
case _biplane: format = PANO_FORMAT_BIPLANE; break;
case _triplane: format = PANO_FORMAT_TRIPLANE; break;
case _panini_general: format = PANO_FORMAT_PANINI_GENERAL; break;
case _thoby: format = PANO_FORMAT_THOBY; break;
default: format = -1; break;
}
line += sprintf( line, "# p f%d w %ud h%ud v%g n\"%s\"\n\n", format, g->pano.width, g->pano.height, g->pano.hfov, g->pano.name );
line += sprintf( line, "# Parameters for Each Input Image:\n" );
line += sprintf( line, "# (*) - optimized (p) - preset \n\n");
for( i=0; i<g->numIm; i++ )
{
switch( g->im[i].format )
{
case _rectilinear: format = IMAGE_FORMAT_RECTILINEAR; break;
case _panorama: format = IMAGE_FORMAT_PANORAMA; break;
case _fisheye_circ: format = IMAGE_FORMAT_FISHEYE_EQUIDISTANCECIRC; break;
case _fisheye_ff: format = IMAGE_FORMAT_FISHEYE_EQUIDISTANCEFF; break;
case _equirectangular: format = IMAGE_FORMAT_EQUIRECTANGULAR; break;
case _mirror: format = IMAGE_FORMAT_MIRROR; break;
case _orthographic: format = IMAGE_FORMAT_FISHEYE_ORTHOGRAPHIC; break;
case _stereographic: format = IMAGE_FORMAT_FISHEYE_STEREOGRAPHIC; break;
case _equisolid: format = IMAGE_FORMAT_FISHEYE_EQUISOLID; break;
case _thoby: format = IMAGE_FORMAT_FISHEYE_THOBY; break;
default: format = -1; break;
}
if( g->opt[i].hfov == 1 ||
( g->opt[i].hfov > 1 && g->opt[g->opt[i].hfov-2].hfov == 1 ))
optHfov = 1;
else
optHfov = 0;
if( g->opt[i].a == 1 ||
( g->opt[i].a > 1 && g->opt[g->opt[i].a-2].a == 1 ))
opta = 1;
else
opta = 0;
if( g->opt[i].b == 1 ||
( g->opt[i].b > 1 && g->opt[g->opt[i].b-2].b == 1 ))
optb = 1;
else
optb = 0;
if( g->opt[i].c == 1 ||
( g->opt[i].c > 1 && g->opt[g->opt[i].c-2].c == 1 ))
optc = 1;
else
optc = 0;
line += sprintf( line, "# Image No %d:\n", i );
line += sprintf( line, "# Yaw: %g deg (%c) Pitch: %g deg (%c) \n"
"# Roll: %g deg (%c) HFov: %g deg (%c)\n"
"# Polynomial Coefficients: a %f (%c); b %f (%c); c %f (%c)\n"
"# Horizontal Shift: %f (%c) Vertical Shift: %f (%c)\n"
"# TiltX: %f (%c) TiltY: %f (%c)\n"
"# TiltZ: %f (%c) TiltScale: %f (%c)\n"
"# TransX: %f (%c) TransY: %f (%c)\n"
"# TransZ: %f (%c) \n"
"# TransYaw: %f (%c) TransPitch: %f (%c)\n"
"# Test P0: %f (%c) Test P1: %f (%c)\n"
"# Test P2: %f (%c) Test P3: %f (%c)\n",
g->im[i].yaw, ( g->opt[i].yaw ? '*' : 'p' ),
g->im[i].pitch, ( g->opt[i].pitch ? '*' : 'p' ),
g->im[i].roll, ( g->opt[i].roll ? '*' : 'p' ),
g->im[i].hfov, ( optHfov ? '*' : 'p' ),
g->im[i].cP.radial_params[0][3], (opta ? '*':'p'),
g->im[i].cP.radial_params[0][2], (optb ? '*':'p'),
g->im[i].cP.radial_params[0][1], (optc ? '*':'p'),
g->im[i].cP.horizontal_params[0], (g->opt[i].d ? '*':'p'),
g->im[i].cP.vertical_params[0], (g->opt[i].e ? '*':'p'),
// Tilt
g->im[i].cP.tilt_x, (g->opt[i].tiltXopt ? '*':'p'),
g->im[i].cP.tilt_y, (g->opt[i].tiltYopt ? '*':'p'),
g->im[i].cP.tilt_z, (g->opt[i].tiltZopt ? '*':'p'),
g->im[i].cP.tilt_scale, (g->opt[i].tiltScaleOpt ? '*':'p'),
// Trans
g->im[i].cP.trans_x, (g->opt[i].transXopt ? '*':'p'),
g->im[i].cP.trans_y, (g->opt[i].transYopt ? '*':'p'),
g->im[i].cP.trans_z, (g->opt[i].transZopt ? '*':'p'),
g->im[i].cP.trans_yaw, (g->opt[i].transYawOpt ? '*':'p'),
g->im[i].cP.trans_pitch, (g->opt[i].transPitchOpt ? '*':'p'),
// test parameters
g->im[i].cP.test_p0, (g->opt[i].testP0opt ? '*':'p'),
g->im[i].cP.test_p1, (g->opt[i].testP1opt ? '*':'p'),
g->im[i].cP.test_p2, (g->opt[i].testP2opt ? '*':'p'),
g->im[i].cP.test_p3, (g->opt[i].testP3opt ? '*':'p')
);
if( opta || optb || optc )
{
line += sprintf(line,"# 4th polynomial coefficient: %g\n", 1.0 - ( g->im[i].cP.radial_params[0][3]
+ g->im[i].cP.radial_params[0][2]
+ g->im[i].cP.radial_params[0][1] ) );
}
line += sprintf(line, "# Command for Panorama Creation: \n" );
line += sprintf( line, "o f%d r%g p%g y%g v%g ", format, g->im[i].roll,g->im[i].pitch,g->im[i].yaw,g->im[i].hfov);
if( g->im[i].cP.radial )
{
line += sprintf( line, "a%f b%f c%f ", g->im[i].cP.radial_params[0][3], g->im[i].cP.radial_params[0][2], g->im[i].cP.radial_params[0][1]);
}
if( g->im[i].cP.shear )
{
line += sprintf( line, "g%f t%f ", g->im[i].cP.shear_x, g->im[i].cP.shear_y);
}
if( g->im[i].cP.tilt ) {
line += sprintf( line, "TiX%f TiY%f TiZ%f TiS%f ", g->im[i].cP.tilt_x, g->im[i].cP.tilt_y, g->im[i].cP.tilt_z, g->im[i].cP.tilt_scale);
}
if( g->im[i].cP.trans ) {
line += sprintf( line, "TrX%f TrY%f TrZ%f Tpy%f Tpp%f", g->im[i].cP.trans_x, g->im[i].cP.trans_y, g->im[i].cP.trans_z, g->im[i].cP.trans_yaw, g->im[i].cP.trans_pitch);
}
if( g->im[i].cP.test ) {
line += sprintf( line, "Te0%f Te1%f Te2%f Te3%f ", g->im[i].cP.test_p0, g->im[i].cP.test_p1, g->im[i].cP.test_p2, g->im[i].cP.test_p3 );
}
if( g->im[i].cP.cutFrame &&
!( g->im[i].selection.bottom != 0 || g->im[i].selection.right != 0 )) // g->im[i].format != _fisheye_circ && g->im[i].cP.cutFrame )
{
if( g->im[i].cP.frame != 0 )
{
line += sprintf( line, "m%d ",g->im[i].cP.frame );
}
else
{
line += sprintf( line, "mx%d my%d ",g->im[i].cP.fwidth, g->im[i].cP.fheight );
}
}
// line += sprintf( line, "d%f e%f ", g->prefs[i]->c_prefs.horizontal_params[0], g->prefs[i]->c_prefs.vertical_params[0]);
#if 0
if( g->prefs[i]->colCorrect )
{
line += sprintf( line, "t%f,%f,%f,%f,%f,%f ", g->prefs[i]->ColCoeff[0][0],
g->prefs[i]->ColCoeff[0][1],
g->prefs[i]->ColCoeff[1][0],
g->prefs[i]->ColCoeff[1][1],
g->prefs[i]->ColCoeff[2][0],
g->prefs[i]->ColCoeff[2][1]);
}
#endif
if( g->im[i].cP.horizontal )
{
line += sprintf( line, "d%f ",g->im[i].cP.horizontal_params[0]);
}
if( g->im[i].cP.vertical )
{
line += sprintf( line, "e%f ",g->im[i].cP.vertical_params[0]);
}
if( g->im[i].cP.correction_mode & correction_mode_morph )
{
line += sprintf( line, "o " );
}
if( g->im[i].selection.bottom != 0 || g->im[i].selection.right != 0 ){
if( g->im[i].cP.cutFrame ){
line += sprintf( line, " C%d,%d,%d,%d ",g->im[i].selection.left, g->im[i].selection.right,
g->im[i].selection.top, g->im[i].selection.bottom );
}else{
line += sprintf( line, " S%d,%d,%d,%d ",g->im[i].selection.left, g->im[i].selection.right,
g->im[i].selection.top, g->im[i].selection.bottom );
}
}
line += sprintf(line, "u%d ", g->st.feather);
#if 0
// Print filename
if( g->im[i].name != NULL && strlen( g->im[i].name != 0 ) ){
line += sprintf(line, " n\"%s\" ", g->im[i].name);
}
#endif
// Add command for stitcher, depending on g->st
// If '-' option has been set (destName != 0), do nothing
// else add stitch commands
if( !*(g->st.destName) )
{
if( i== 0 )
{
line += sprintf(line, "-%s ", g->st.srcName);
}
else if( i == g->numIm-1 )
{
line += sprintf(line, "+%s ", g->st.srcName);
}
else
{
line += sprintf(line, "+%s -%s ", g->st.srcName, g->st.srcName);
}
// Print Feather commands
}
line += sprintf(line, "\n\n");
} // numIm
if( g->numPts > 0 ) // Display Controlpoint distances
{
line += sprintf(line, "\n");
line += sprintf( line, "# ==========================================================================\n");
line += sprintf( line, "# Control Points: Distance between desired and fitted Position (in \"Pixels\")\n\n");
for( i=0; i<g->numPts; i++ )
{
line += sprintf( line, "# Control Point No %d: %g\n", i , sqrt ( ds(i)) );
}
// Print optimum positions for points in panorama
for( i=0; i<g->numPts; i++ )
{
double x[2],y[2],xd,yd,d,D[2];
EvaluateControlPointErrorAndComponents(i,&d,D);
// write only normal control points, not horizontals, verticals, or lines
if (g->cpt[i].type == 0)
{
GetControlPointCoordinates(i, x, y, g );
// Write only points inside image
if( x[0] >= 0.0 && x[0] < g->pano.width && x[1] >= 0.0 && x[1] < g->pano.width &&
y[0] >= 0.0 && y[0] < g->pano.height && y[1] >= 0.0 && y[1] < g->pano.height)
{
xd = (x[0]+x[1]) / 2.0;
yd = (y[0]+y[1]) / 2.0;
line += sprintf( line, "C i%d c%d x%g y%g X%g Y%g D%g Dx%g Dy%g\n", g->cpt[i].num[0], i, x[0], y[0], xd, yd, d, D[0], D[1] );
line += sprintf( line, "C i%d c%d x%g y%g X%g Y%g D%g Dx%g Dy%g\n", g->cpt[i].num[1], i, x[1], y[1], xd, yd, d, D[0], D[1] );
}
}
}
}
if( WriteScript( res, sfile, launch ) != 0 )
{
PrintError("Could not write results to scriptfile");
}
if( hres ) myfree( (void**)hres );
panoLocaleRestore;
return;
fail:
panoLocaleRestore;
}
// Reads 'adjust' parameters from script 'sfile'
// return 0 on success/ -1 if failed
int readAdjust( aPrefs *p, fullPath* sfile, int insert, sPrefs *sP )
{
char* script;
// Variables used by parser
char line[LINE_LENGTH], *ch;
int lineNum = 0;
int seto;
int seti;
panoLocaleSave;
// Set prefs and sBuf to defaults
SetAdjustDefaults( p );
script = LoadScript( sfile );
if( script == NULL )
goto fail;
// Parse script
ch = script;
seto = FALSE;
seti = FALSE;
while( *ch != 0 ) {
lineNum++;
while(*ch == '\n')
ch++;
// read a line of text into line[];
nextLine( line, &ch );
// parse line; use only if first character is p,o,m
switch( line[0] ) {
case 'i':
// The original i line was optional and it did not contain any information except
// for the filename.
// Hugin .pto files use the 'i' line instead of 'o' line.
// 'o' has priority over 'i'
// if 'o' has been read then skip
if (!seto && !seti) {
if( ReadImageDescription( &(p->im), &(p->sBuf), &(line[1]) ) != 0 ) {
PrintError("Syntax error in i-line %d (%s)", lineNum, line);
goto fail;
}
seti = TRUE;
}
break;
case 'o': // Image description
if( !seto ) { // Read only _one_ image
// 'o' has priority over 'i' lines
if( ReadImageDescription( &(p->im), &(p->sBuf), &(line[1]) ) != 0 ) {
PrintError( "Syntax error parsing o-line %d (%s)" , lineNum, line);
goto fail;
}
seto = TRUE;
}
break;
case 'm': // Mode description
if( ReadModeDescription( sP, &(line[1]) ) != 0 )
{
PrintError( "Syntax error in m-line %d (%s)" , lineNum, line);
goto fail;
}
break;
case 'p': // panorama
p->pano.format = 2; // _equirectangular by default
p->pano.hfov = 360.0;
if( ReadPanoramaDescription( &(p->pano), &(p->sBuf), &(line[1]) ) != 0 )
{
PrintError( "Syntax error in line %d" , lineNum);
goto fail;
}
switch (p->pano.format) {
case PANO_FORMAT_RECTILINEAR:
p->pano.format = _rectilinear;
break;
case PANO_FORMAT_PANORAMA:
p->pano.format = _panorama;
break;
case PANO_FORMAT_EQUIRECTANGULAR:
p->pano.format = _equirectangular;
break;
case PANO_FORMAT_FISHEYE_FF:
p->pano.format = _fisheye_ff;
break;
case PANO_FORMAT_STEREOGRAPHIC:
p->pano.format = _stereographic;
break;
case PANO_FORMAT_MERCATOR:
p->pano.format = _mercator;
break;
case PANO_FORMAT_TRANS_MERCATOR:
p->pano.format = _trans_mercator;
break;
case PANO_FORMAT_SINUSOIDAL:
p->pano.format = _sinusoidal;
break;
case PANO_FORMAT_LAMBERT_EQUAL_AREA_CONIC:
p->pano.format = _lambert;
break;
case PANO_FORMAT_LAMBERT_AZIMUTHAL:
p->pano.format = _lambertazimuthal;
break;
case PANO_FORMAT_HAMMER:
p->pano.format = _hammer;
break;
case PANO_FORMAT_ALBERS_EQUAL_AREA_CONIC:
p->pano.format = _albersequalareaconic;
break;
case PANO_FORMAT_MILLER_CYLINDRICAL:
p->pano.format = _millercylindrical;
break;
case PANO_FORMAT_PANINI:
p->pano.format = _panini;
break;
case PANO_FORMAT_PANINI_GENERAL:
p->pano.format = _panini_general;
break;
case PANO_FORMAT_EQUI_PANINI:
p->pano.format = _equipanini;
break;
case PANO_FORMAT_ARCHITECTURAL:
p->pano.format = _architectural;
break;
case PANO_FORMAT_ORTHOGRAPHIC:
p->pano.format = _orthographic;
break;
case PANO_FORMAT_EQUISOLID:
p->pano.format = _equisolid;
break;
case PANO_FORMAT_THOBY:
p->pano.format = _thoby;
break;
case PANO_FORMAT_BIPLANE:
p->pano.format = _biplane;
break;
case PANO_FORMAT_TRIPLANE:
p->pano.format = _triplane;
break;
default:
PrintError( "Unknown panorama projection: %d", p->pano.format );
goto fail;
}
if( (p->pano.format == _rectilinear || p->pano.format == _trans_mercator) && p->pano.hfov >= 180.0 ) {
PrintError( "Destination image must have HFOV < 180" );
goto fail;
}
break;
default:
// PrintError("Unknown line %s", line);
// silently ignore any unknown lines
break;
}
}
if( ! (seto || seti) ) {
PrintError( "Syntax error in scriptfile (readAdjust). It contains no 'o' line");
goto fail;
}
// Create and Write changed scriptfile if inserting
if( insert ) {
char charToFind;
// dmg: this is the part that I hate the most about the parser:
// it rewrites the file over and over again, eating one line at at a time
if (seti)
charToFind = 'i';
// 'o' has priority over 'i'
if (seto)
charToFind = 'o';
seto = FALSE; ch = script;
while( *ch != 0 && !seto )
{
while(*ch == '\n')
ch++;
if( *ch == charToFind )
seto = TRUE;
else
{
while(*ch != '\n' && *ch != 0)
ch++;
}
}
if( *ch == charToFind ) *ch = '!';
// If this is the last image to convert: recover script file
seto = FALSE; ch = script;
while( *ch != 0 && !seto ) {
while(*ch == '\n')
ch++;
if( *ch == charToFind )
seto = TRUE;
else
{
while(*ch != '\n' && *ch != 0)
ch++;
}
}
if( seto == FALSE ) { // No more images to convert
ch = script;
while( *ch != 0 ) {
while(*ch == '\n')
ch++;
if( *ch == '!' )
*ch = charToFind;
else
{
while(*ch != '\n' && *ch != 0)
ch++;
}
}
}
if( WriteScript( script, sfile, 0 ) != 0 ) {
PrintError("Could not write scriptfile");
goto fail;
}
}
free( script);
panoLocaleRestore;
return 0;
fail:
if (script != NULL)
free(script);
panoLocaleRestore;
return -1;
}
void readControlPoints(char* script, controlPoint *cp )
{
struct controlPoint defCn;
// Variables used by parser
char line[LINE_LENGTH], *ch ,*lineStart;
int lineNum = 0;
int i;
int numPts;
panoLocaleSave;
defCn.num[0] = defCn.num[1] = -1;
defCn.type = 0;
defCn.x[0] = defCn.x[1] = defCn.y[0] = defCn.y[1] = 0;
for(i=0; i< NUMPTS; i++)
memcpy( &(cp[i]), &defCn, sizeof( struct controlPoint ) );
numPts = 0; // reused as indices
// Parse script
ch = script;
while( *ch != 0 )
{
lineNum++;
while(*ch == '\n')
ch++;
lineStart = ch;
// read a line of text into line[];
nextLine( line, &ch );
// parse line; use only if first character is c
switch( line[0] )
{
case 'c': // Control Points
defCn.num[0] = defCn.num[1] = -1;
defCn.type = 0;
defCn.x[0] = defCn.x[1] = defCn.y[0] = defCn.y[1] = 0;
if( ReadControlPoint( &defCn, &(line[1]) ) != 0 )
{
PrintError("Error in line %d", lineNum);
panoLocaleRestore;
return;
}
if( defCn.num[1] == -1 ) // We found a partial controlpoint
{
*lineStart = 0; // script ends here
memcpy( &(cp[numPts]), &defCn, sizeof( struct controlPoint ) );
numPts++;
}
break;
case '*': // End of script-data
*lineStart = 0; *ch = 0;
break;
default:
break;
}
}
panoLocaleRestore;
}
// Fill 'word' with word starting at (*ch). Advance *ch
void nextWord( register char* word, char** ch )
{
register char *c;
c = *ch;
c++;
if( *c == '\"' )
{
c++;
while( *c != '\"' && *c != 0 )
*word++ = *c++;
if(0 != *c)
c++; // to eat last character
}
else
{
while( !isspace(*c) && *c != 0 )
{
*word++ = *c++;
}
}
*word = 0;
*ch = c;
}
// Fill 'line' with line starting at (*ch). Advance *ch
void nextLine( register char* line, char** ch )
{
register char *c;
register int i;
c = *ch;
while(*c == '\n')
c++;
// read a line of text into line[];
i=0;
//Increased by Max Lyons (January 12, 2003) to increase size of optimizer
//lines that can be read (previously hard-coded to 255 characters).
while( *c != 0 && *c != '\n' && i++<LINE_LENGTH)
*line++ = *c++;
*line = 0;
*ch = c;
}
// Number of lines in script starting with character 'first'
int numLines( char* script, char first )
{
register char *ch;
int result = 0;
ch = script;
while( *ch != 0 )
{
// Advance to linestart
while(*ch == '\n')
ch++;
if( *ch == first )
result++;
while(*ch != '\n' && *ch != 0)
ch++;
}
return result;
}
#undef MY_SSCANF
#define MY_SSCANF( str, format, ptr ) if( sscanf( str, format, ptr ) != 1 ) \
{ \
PrintError("Syntax error in script: Could not read value for variable");\
return -1; \
}
#undef READ_VAR
#define READ_VAR(format, ptr ) nextWord( buf, &ch ); \
MY_SSCANF( buf, format, ptr );
// Parse a line describing a single Controlpoint
static int ReadControlPoint( controlPoint * cptr, char *line)
{
controlPoint cp;
char *ch = line;
int setn, setN, setx, setX, sety, setY;
char buf[LINE_LENGTH];
setn = setN = setx= setX = sety = setY = FALSE;
memcpy( &cp, cptr, sizeof(controlPoint) );
while( *ch != 0)
{
switch(*ch)
{
case 't': READ_VAR("%d", &(cp.type));
break;
case 'n': READ_VAR("%d", &(cp.num[0]));
setn = TRUE;
break;
case 'N': READ_VAR("%d", &(cp.num[1]));
setN = TRUE;
break;
case 'x': READ_VAR("%lf", &(cp.x[0]));
setx = TRUE;
break;
case 'X': READ_VAR("%lf", &(cp.x[1]));
setX = TRUE;
break;
case 'y': READ_VAR("%lf", &(cp.y[0]));
sety = TRUE;
break;
case 'Y': READ_VAR("%lf", &(cp.y[1]));
setY = TRUE;
break;
case 'i': READ_VAR("%d", &(cp.num[0]));
cp.num[1] = cp.num[0];
setn = TRUE;
setN = TRUE;
default: ch++;
break;
}
}
// Check values
if( setn == FALSE || setN == FALSE || setx == FALSE || setX == FALSE ||
sety == FALSE || setY == FALSE )
{
PrintError("Missing Control Point Parameter");
return -1;
}
else if( cp.type < 0 )
{
PrintError("Control Point Type must be positive");
return -1;
}
// Joost: cp coordinates can be possible, no problem!
// else if( cp.x[0] < 0 || cp.y[0] < 0 || cp.x[1] < 0 || cp.y[1] < 0)
// {
// PrintError("Pixel Coordinates must be positive");
// return -1;
// }
else // looks ok so far
{
memcpy( cptr, &cp, sizeof(controlPoint) );
return 0;
}
}
// Parse a line describing a single image
static int ReadImageDescription( Image *imPtr, stBuf *sPtr, char *line )
{
// This function parses the i- and -o lines
Image im;
stBuf sBuf;
char *ch = line;
char buf[LINE_LENGTH];
int i;
int cropping = 0;
int tempInt;
char typeParm;
int32_t tempInt32;
memcpy( &im, imPtr, sizeof(Image) );
memcpy( &sBuf, sPtr, sizeof(stBuf ));
// panoPrintImage("Before read image", imPtr);
// printf("************************************* Before Cut Frame %d \n", im.cP.cutFrame);
while( *ch != 0) {
switch(*ch) {
case 'f': READ_VAR( "%d", &tempInt32 );
tempInt = panoExternalToInternalInputProjection(tempInt32);
if (tempInt < 0) {
PrintError("Syntax error in script. Projection not known: %ud", tempInt32);
return -1;
}
im.format = tempInt;
if( im.format == _panorama || im.format == _equirectangular )
im.cP.correction_mode |= correction_mode_vertical;
break;
case 'v': READ_VAR( "%lf", &im.hfov );
break;
case 'y':
READ_VAR( "%lf", &im.yaw);
break;
case 'p': READ_VAR( "%lf", &im.pitch);
break;
case 'r': READ_VAR( "%lf", &im.roll);
break;
case 'a': READ_VAR( "%lf", &(im.cP.radial_params[0][3]));
im.cP.radial = TRUE;
break;
case 'b': READ_VAR("%lf", &(im.cP.radial_params[0][2]));
im.cP.radial = TRUE;
break;
case 'c': READ_VAR("%lf", &(im.cP.radial_params[0][1]));
im.cP.radial = TRUE;
break;
case 'd': READ_VAR("%lf", &(im.cP.horizontal_params[0]));
im.cP.horizontal = TRUE;
break;
case 'e': READ_VAR("%lf", &(im.cP.vertical_params[0]));
im.cP.vertical = TRUE;
break;
case 'g': READ_VAR("%lf", &(im.cP.shear_x));
im.cP.shear = TRUE;
break;
case 't': READ_VAR("%lf", &(im.cP.shear_y));
im.cP.shear = TRUE;
break;
case 'T':
ch++;
switch (*ch) {
case 'i':
ch++;
switch (*ch) {
case 'X':
READ_VAR("%lf",&(im.cP.tilt_x));
break;
case 'Y':
READ_VAR("%lf",&(im.cP.tilt_y));
break;
case 'Z':
READ_VAR("%lf",&(im.cP.tilt_z));
break;
case 'S':
READ_VAR("%lf",&(im.cP.tilt_scale));
break;
default:
PrintError("Unknown variable name Ti%c in script", *ch);
return -1;
}
im.cP.tilt = TRUE;
break;
case 'r':
ch++;
switch (*ch) {
case 'X':
READ_VAR("%lf",&(im.cP.trans_x));
break;
case 'Y':
READ_VAR("%lf",&(im.cP.trans_y));
break;
case 'Z':
READ_VAR("%lf",&(im.cP.trans_z));
break;
default:
PrintError("Unknown variable name Tr%c in script", *ch);
return -1;
}
if (im.cP.trans_x != 0.0 ||
im.cP.trans_y != 0.0 ||
im.cP.trans_z != 0.0) {
im.cP.trans = TRUE;
}
break;
case 'p':
ch++;
switch (*ch) {
case 'y':
READ_VAR("%lf",&(im.cP.trans_yaw));
break;
case 'p':
READ_VAR("%lf",&(im.cP.trans_pitch));
break;
default:
PrintError("Unknown variable name Tp%c in script", *ch);
return -1;
}
break;
case 'e':
ch++;
switch (*ch) {
case '0':
READ_VAR("%lf",&(im.cP.test_p0));
break;
case '1':
READ_VAR("%lf",&(im.cP.test_p1));
break;
case '2':
READ_VAR("%lf",&(im.cP.test_p2));
break;
case '3':
READ_VAR("%lf",&(im.cP.test_p3));
break;
default:
PrintError("Unknown variable name Te%c in script", *ch);
return -1;
}
im.cP.test = TRUE;
break;
default:
PrintError("Unkonwn parameter T%c in script", *ch);
return -1;
}
break;
case '+': nextWord( buf, &ch );
PrintError("Obsolete + parameter is ignored in image description");
snprintf( sBuf.srcName, sizeof(sBuf.srcName)-1, "%s", buf);
break;
case '-': nextWord( buf, &ch );
PrintError("Obsolete - parameter is ignored in image description");
snprintf( sBuf.destName, sizeof(sBuf.destName)-1, "%s", buf );
break;
case 'S':
if (cropping) {
PrintError("Contradictory cropping specified. S cropping ignored\n");
// Eat next token
nextWord( buf, &ch );
break;
}
cropping = 1;
nextWord( buf, &ch );
sscanf( buf, "%d,%d,%d,%d", &im.selection.left, &im.selection.right, &im.selection.top, &im.selection.bottom );
break;
case 'C':
if (cropping) {
PrintError("Contradictory cropping specified. C cropping ignored\n");
// Eat next token
nextWord( buf, &ch );
break;
}
cropping = 1;
nextWord( buf, &ch );
sscanf( buf, "%d,%d,%d,%d", &im.selection.left, &im.selection.right, &im.selection.top, &im.selection.bottom );
im.cP.cutFrame = TRUE;
break;
case 'm': // Frame
//THiS NEEDS A GOOD FIX...
typeParm = *(ch+1);
// first consume the parameter
if (typeParm =='x' || typeParm == 'y') {
// Consume next character, then read parm
ch++;
READ_VAR( "%d", &tempInt);
}
else {
READ_VAR( "%d", &tempInt);
}
if (tempInt == 0) {
// value of zero, just ignore
break;
}
// Sometimes this is specified to force a zero. In this case
// issue a warning and ignore
if (cropping) {
PrintError("Contradictory cropping specified. M cropping ignored\n");
break;
}
// Eat next token to avoid error
cropping = 1;
im.cP.cutFrame = TRUE;
switch( typeParm ) {
case 'x':
im.cP.fwidth = tempInt;
//READ_VAR( "%d", &im.cP.fwidth );
break;
case 'y':
im.cP.fheight = tempInt;
//READ_VAR( "%d", &im.cP.fheight );
//im.cP.cutFrame = TRUE;
break;
default:
im.cP.frame = tempInt;
READ_VAR( "%d", &(im.cP.frame) );
// im.cP.cutFrame = TRUE;
break;
}
break;
case 's': READ_VAR( "%d", &sBuf.seam );
PrintError("Obsolete s parameter ignored in image description");
break;
case 'o':
ch++;
im.cP.correction_mode |= correction_mode_morph;
break;
case 'u':
READ_VAR( "%d", &i );
PrintError("Feathering is ignored. Use PTmasker");
break;
case 'w': READ_VAR( "%ud", &im.width );
break;
case 'h': READ_VAR( "%ud", &im.height );
break;
case 'n': // Name string (used for input image name)
nextWord( buf, &ch );
strcpy( im.name, buf );
break;
case 'K':
case 'V':
// Used by Hugin. Silently ignore until next space. This way we can accept .pto files for processing
nextWord( buf, &ch );
break;
case ' ':
case '\t':
case '\n':
case '\r':
// skip characters and tabs
ch++;
break;
default:
printf("Returning...........\n");
PrintError("Illegal token in adjust line [%c] rest of line [%s]", *ch, ch);
return -1;
}
}
// printf("************************************* A Cut Frame %d \n", im.cP.cutFrame);
// Set 4th polynomial parameter
im.cP.radial_params[0][0] = 1.0 - ( im.cP.radial_params[0][3] + im.cP.radial_params[0][2]
+ im.cP.radial_params[0][1] ) ;
SetEquColor( &im.cP );
SetCorrectionRadius( &im.cP );
// Do checks
// appears ok
memcpy( imPtr, &im, sizeof(Image) );
memcpy( sPtr, &sBuf, sizeof(stBuf ) );
// panoPrintImage("After read image", imPtr);
return 0;
}
static int ReadPanoramaDescription( Image *imPtr, stBuf *sPtr, char *line )
{
// This function parses the p- line
Image im;
stBuf sBuf;
char *ch = line;
char buf[LINE_LENGTH];
char *b;
int i;
double tempDbl;
memcpy( &im, imPtr, sizeof(Image) );
memcpy( &sBuf, sPtr, sizeof(stBuf ));
// printf("************************************* Before Cut Frame %d \n", im.cP.cutFrame);
while( *ch != 0) {
switch(*ch) {
case 'w': READ_VAR( "%ud", &im.width );
break;
case 'h': READ_VAR( "%ud", &im.height );
break;
case 'f': READ_VAR( "%d", &im.format );
if( im.format == _panorama || im.format == _equirectangular )
im.cP.correction_mode |= correction_mode_vertical;
break;
case 'P':
nextWord(buf, &ch);
b = strtok(buf, " \"");
if (b != NULL) {
while (b != NULL) {
if (sscanf(b, "%lf", &tempDbl) == 1) {
if (++im.formatParamCount > PANO_PROJECTION_MAX_PARMS) {
PrintError("Illegal number of projection parameters. Maximum is %d", PANO_PROJECTION_MAX_PARMS);
return -1;
}
im.formatParam[im.formatParamCount - 1] = tempDbl;
b = strtok(NULL, " \"");
} else {
PrintError("Illegal value in P parameter %s", b);
return -1;
}
}
}
break;
case 'v': READ_VAR( "%lf", &im.hfov );
break;
case 'n': // Name string (used for panorama format)
nextWord( buf, &ch );
strcpy( im.name, buf );
break;
case 'u': //Feather
READ_VAR( "%d", &i );
PrintError("Feathering is ignored. Use PTmasker");
break;
case 'k': // Colour correction
READ_VAR( "%d", &i );
PrintError("Colour correction ignored (k). Use PTblender");
break;
case 'd': // Colour correction
READ_VAR( "%d", &i );
PrintError("Colour correction ignored (d). Use PTblender");
break;
case 'b': // Colour correction
READ_VAR( "%d", &i );
PrintError("Colour correction ignored parameter ignored (b). Use PTblender");
break;
case ' ':
case '\t':
case '\n':
case '\r':
// skip characters and tabs
ch++;
break;
default:
PrintError("Illegal token in 'p'-line [%c] [%s]", *ch, ch);
ch++;
break;
}
}
// I am not sure this is needed, but I am not sure it is not :)
// code inherited from ReadImageDescription
im.cP.radial_params[0][0] = 1.0 - ( im.cP.radial_params[0][3] + im.cP.radial_params[0][2]
+ im.cP.radial_params[0][1] ) ;
SetEquColor( &im.cP );
SetCorrectionRadius( &im.cP );
memcpy( imPtr, &im, sizeof(Image) );
memcpy( sPtr, &sBuf, sizeof(stBuf ) );
return 0;
}
// Parse a line describing modes
static int ReadModeDescription( sPrefs *sP, char *line )
{
sPrefs theSprefs;
char *ch = line;
char buf[LINE_LENGTH];
double sigma = 0;
int n;
memcpy( &theSprefs, sP, sizeof(sPrefs) );
// set some default values
setFcnPanoHuberSigma(0);
while( *ch != 0)
{
switch(*ch)
{
case 'g': READ_VAR( "%lf", &theSprefs.gamma );
if( theSprefs.gamma <= 0.0 )
return -1;
break;
case 'i': READ_VAR( "%d", &theSprefs.interpolator );
if( theSprefs.interpolator < 0 || theSprefs.interpolator > 23)
theSprefs.interpolator = 0;
break;
case 'p': READ_VAR( "%d", &theSprefs.optCreatePano );
if(theSprefs.optCreatePano != 0)
theSprefs.optCreatePano = TRUE;
break;
case 'f': READ_VAR( "%d", &n );
if( n == 0 )
theSprefs.fastStep = FAST_TRANSFORM_STEP_NORMAL;
else if( n == 1 )
theSprefs.fastStep = FAST_TRANSFORM_STEP_MORPH;
else
theSprefs.fastStep = FAST_TRANSFORM_STEP_NONE;
break;
case 'm': READ_VAR( "%lf", &sigma);
setFcnPanoHuberSigma(sigma);
break;
default: ch++;
break;
}
}
// appears ok
memcpy( sP, &theSprefs, sizeof(sPrefs) );
return 0;
}
// Parse a string desscribing VRPanoOptions
int getVRPanoOptions( VRPanoOptions *v, char *line )
{
char *ch = line;
char buf[LINE_LENGTH];
VRPanoOptions VRopt;
panoLocaleSave;
memcpy( &VRopt, v, sizeof( VRPanoOptions ) );
while( *ch != 0)
{
switch(*ch)
{
case 'w': READ_VAR("%d", &VRopt.width);
break;
case 'h': READ_VAR("%d", &VRopt.height);
break;
case 'p': READ_VAR("%lf", &VRopt.pan);
break;
case 't': READ_VAR("%lf", &VRopt.tilt);
break;
case 'v': READ_VAR("%lf", &VRopt.fov);
break;
case 'c': READ_VAR("%d", &VRopt.codec);
break;
case 'q': READ_VAR("%d", &VRopt.cquality);
break;
case 'g': READ_VAR("%d", &VRopt.progressive);
break;
default: ch++;
break;
}
}
memcpy( v, &VRopt, sizeof( VRPanoOptions ) );
panoLocaleRestore;
return 0;
}
// Read coordinates of positions
int readPositions( char* script, transformCoord *tP )
{
// Variables used by parser
char line[LINE_LENGTH], *ch ;
int lineNum = 0;
int nr=0,np=0;
panoLocaleSave;
// Determine number of images and control points
tP->nump = numLines( script, 'P' );
tP->numr = numLines( script, 'R' );
// Allocate Space for Pointers to images, preferences and control points
tP->r = (CoordInfo*) malloc( tP->numr * sizeof(CoordInfo) );
tP->p = (CoordInfo*) malloc( tP->nump * sizeof(CoordInfo) );
if( tP->r == NULL || tP->p == NULL )
{
PrintError("Not enough memory");
goto fail;
}
// Parse script
ch = script;
while( *ch != 0 )
{
lineNum++;
while(*ch == '\n')
ch++;
// read a line of text into line[];
nextLine( line, &ch );
// parse line; use only if first character is p,o,m
switch( line[0] )
{
case 'P': // Coordinates as is
if( ReadCoordinates( &tP->p[np++], &(line[1]) ) != 0 )
{
PrintError( "Syntax error in line %d" , lineNum);
goto fail;
}
break;
case 'R': // Coordinate values requested
if( ReadCoordinates( &tP->r[nr++], &(line[1]) ) != 0 )
{
PrintError( "Syntax error in line %d" , lineNum);
goto fail;
}
break;
default: break;
}
}
panoLocaleRestore;
return 0;
fail:
panoLocaleRestore;
return -1;
}
static int ReadCoordinates( CoordInfo *cp, char *line )
{
CoordInfo ci;
char *ch = line;
char buf[LINE_LENGTH];
ci.num = ci.set[0] = ci.set[1] = ci.set[2] = 0;
ci.x[0] = ci.x[1] = ci.x[2] = 1.0;
while( *ch != 0)
{
switch(*ch)
{
case 'c': READ_VAR( "%d", &ci.num );
break;
case 'i': READ_VAR( "%d", &ci.num );
ci.num -= 2;
break;
case 'X': READ_VAR( "%lf", &ci.x[0] );
ci.set[0] = TRUE;
break;
case 'Y': READ_VAR( "%lf", &ci.x[1] );
ci.set[1] = TRUE;
break;
case 'Z': READ_VAR( "%lf", &ci.x[2] );
ci.set[2] = TRUE;
break;
default: ch++;
break;
}
}
// appears ok
memcpy( cp, &ci, sizeof(CoordInfo) );
return 0;
}
int ReadMorphPoints( char *script, AlignInfo *gl, int nIm )
{
// Variables used by parser
char line[LINE_LENGTH], *ch ;
int lineNum = 0;
int np = 0;
controlPoint cp;
void *tmp;
panoLocaleSave;
// Determine number of morph control points
gl->numPts = numLines( script, 'C' );
if( gl->numPts == 0 )
goto success;
// Allocate Space for Pointers to images, preferences and control points
gl->cpt = (controlPoint*) malloc( gl->numPts * sizeof(controlPoint) );
if( gl->cpt == NULL )
{
PrintError("Not enough memory");
goto fail;
}
// Parse script
ch = script;
while( *ch != 0 )
{
lineNum++;
while(*ch == '\n')
ch++;
// read a line of text into line[];
nextLine( line, &ch );
// parse line; use only if first character is p,o,m
switch( line[0] )
{
case 'C': // Coordinates as is
cp.type = 0;
if( ReadControlPoint( &cp, &(line[1]) ) != 0 )
{
PrintError( "Syntax error in line %d" , lineNum);
goto fail;
}
if( cp.num[0] == nIm )
{
cp.num[0] = 0;
cp.num[1] = 1;
memcpy( &gl->cpt[np], &cp, sizeof( controlPoint ));
np++;
}
default: break;
}
}
tmp = realloc( gl->cpt, np * sizeof( controlPoint ) );
if( tmp == NULL ) goto fail;
gl->numPts=np; gl->cpt = (controlPoint*)tmp;
success:
panoLocaleRestore;
return np;
fail:
panoLocaleRestore;
return -1;
}
void SetCoordDefaults( CoordInfo *c, int num )
{
c->num = num;
c->x[0] = (double) num;
c->x[1] = c->x[2] = 0.0;
c->set[0] = c->set[1] = c->set[2] = TRUE;
}
aPrefs* readAdjustLine( fullPath *theScript ){
sPrefs sP;
aPrefs *aP = (aPrefs*)malloc(sizeof(aPrefs));
if(aP==NULL) return NULL;
SetAdjustDefaults( aP );
SetSizeDefaults( &sP );
if( readAdjust( aP, theScript, 1, &sP ) != 0 ){
PrintError("Error processing script file" );
return NULL;
}
// Use modevalues read from script
aP->interpolator = sP.interpolator;
aP->gamma = sP.gamma;
aP->fastStep = sP.fastStep;
// Parse script again, now reading triangles if morphing requested
if( aP->im.cP.correction_mode & correction_mode_morph ){
char* script;
AlignInfo ainf;
int nIm, nPts; // Number of image being processed
Image im[2];
script = LoadScript( theScript) ;
if( script != NULL ){
nIm = numLines( script, '!' ) - 1;
if( nIm < 0)
nIm = numLines( script, 'o' ) - 1;
// Set ainf
ainf.nt = 0;
ainf.t = NULL;
ainf.numIm = 2;
ainf.im = im;
memcpy( &ainf.pano, &aP->pano, sizeof( Image ));
memcpy( &ainf.im[0], &aP->pano, sizeof( Image ));
memcpy( &ainf.im[1], &aP->pano, sizeof( Image ));
nPts = ReadMorphPoints( script, &ainf, nIm );
if(nPts > 0){
AddEdgePoints( &ainf );
TriangulatePoints( &ainf, 1 );
aP->nt = ainf.nt;
if(aP->nt > 0){
SortControlPoints( &ainf, 1 );
SetSourceTriangles( &ainf, 1, &aP->td );
SetDestTriangles( &ainf, 1, &aP->ts );
}
}
if(ainf.numPts > 0) free(ainf.cpt);
free( script );
}
}
//panoDumpAdjustData(aP, "readAdjustLine", 0);
return aP;
}
char *panoParserFindOLine(char *script, int index)
{
char *ptr;
int count = 0;
ptr = script;
while (ptr != NULL) {
if (*ptr == 'o') {
if (count == index) {
// we have found it
int length;
char *temp;
char *result;
// how big is it?
temp = strchr(ptr, '\n');
if (temp == NULL)
length = strlen(ptr);
else
length = temp -ptr;
//allocate it
result = calloc(length + 1, 1);
if (result == NULL) {
PrintError("Not enough memory");
return NULL;
} else {
strncpy(result, ptr, length);
}
return result;
} else {
count++;
}
}
// find next beginning of line
ptr = strchr(ptr, '\n');
if (ptr == NULL)
break;
ptr++;
}
return NULL;
}
int panoExternalToInternalInputProjection(int input)
{
// Internal and external projection do not match, unfortunately.
// So this code does the remapping
switch (input) {
case IMAGE_FORMAT_RECTILINEAR: return(_rectilinear);
case IMAGE_FORMAT_PANORAMA: return(_panorama);
case IMAGE_FORMAT_FISHEYE_EQUIDISTANCECIRC: return(_fisheye_circ);
case IMAGE_FORMAT_FISHEYE_EQUIDISTANCEFF: return(_fisheye_ff);
case IMAGE_FORMAT_EQUIRECTANGULAR: return(_equirectangular);
case IMAGE_FORMAT_MIRROR: return(_mirror);
case IMAGE_FORMAT_FISHEYE_ORTHOGRAPHIC: return(_orthographic);
case IMAGE_FORMAT_FISHEYE_STEREOGRAPHIC: return(_stereographic);
case IMAGE_FORMAT_FISHEYE_EQUISOLID: return(_equisolid);
case IMAGE_FORMAT_FISHEYE_THOBY: return(_thoby);
default: return -1;
}
}
|