File: PTcommon.c

package info (click to toggle)
libpano12 2.8.3-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 3,168 kB
  • ctags: 2,419
  • sloc: ansic: 28,076; sh: 8,724; makefile: 223
file content (3127 lines) | stat: -rw-r--r-- 94,836 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
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
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
/*
 *  PTcommon.c
 *
 *  Many of the routines are based on the program PTStitcher by Helmut
 *  Dersch.
 * 
 *  Copyright Helmut Dersch and Daniel M. German
 *  
 *  Dec 2006
 *
 *  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
 *
 *
 *  Author: Daniel M German dmgerman at uvic doooot ca
 * 
 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <unistd.h>
#include <stdint.h>
#include <math.h>

#include "filter.h"
#include "panorama.h"

#include "tiffio.h"

#include "PTcommon.h"


//declare functions
void getCropInformationFromTiff(TIFF *tif, CropInfo *c);
void getROI( TrformStr *TrPtr, aPrefs *aP, PTRect *ROIRect );
void setFullSizeImageParameters(pt_tiff_parms *imageParameters, CropInfo *crop_info);

int ptQuietFlag = 0;

void tiffErrorHandler(const char* module, const char* fmt, va_list ap)
{
  PrintError("Error in TIFF file (%s) ", module);
  PrintError(fmt, ap);
}


// This function verifies that all the TIFF files have the same width, length and other
// parameters. Returns TRUE on success.
//

TIFF *OpenTiffFromFullPath(fullPath *file, char *mode)
{
  char tempString[MAX_PATH_LENGTH];
  if (GetFullPath(file, tempString) != 0) {
    PrintError("Could not get filename");
    return NULL;
  }
  return TIFFOpen(tempString, mode);
  
}

int TiffGetImageParameters(TIFF *tiffFile, pt_tiff_parms *tiffData)
{
  assert(tiffFile != NULL);

  if (!TIFFGetField(tiffFile, TIFFTAG_IMAGEWIDTH, &tiffData->imageWidth)) {
    PrintError("File did not include image width information.");
    return 0;
  }
  if (!TIFFGetField(tiffFile, TIFFTAG_IMAGELENGTH, &tiffData->imageLength)) {
    PrintError("File did not include image length information.");
    return 0;
  }

  if (!TIFFGetField(tiffFile, TIFFTAG_BITSPERSAMPLE, &tiffData->bitsPerSample)) {
    PrintError("File did not include bits per sample information.");
    return 0;
  }

  if (!TIFFGetField(tiffFile, TIFFTAG_SAMPLESPERPIXEL, &tiffData->samplesPerPixel)) {
    PrintError("File did not include samples per pixel information.");
    return 0;
  }

  if (!TIFFGetField(tiffFile, TIFFTAG_COMPRESSION, &tiffData->compression)) {
    PrintError("File did not include compression information.");
    return 0;
  }
  if (tiffData->compression == COMPRESSION_LZW) {
    //predictor only exists in LZW compressed files
    if (!TIFFGetField(tiffFile, TIFFTAG_PREDICTOR, &tiffData->predictor)) {
      PrintError("File did not include predictor information.");
      return 0;
    }
  }


  tiffData->bytesPerLine = TIFFScanlineSize(tiffFile);
  if (tiffData->bytesPerLine <= 0) {
    PrintError("File did not include proper bytes per line information.");
    return 0;
  }
  tiffData->bitsPerPixel = tiffData->samplesPerPixel * tiffData->bitsPerSample;

  //tiffData->rowsPerStrip = tiffData->imageLength;
  if (!TIFFGetField(tiffFile, TIFFTAG_ROWSPERSTRIP, &tiffData->rowsPerStrip)) {
    PrintError("File did not include rows per strip information.");
    return 0;
  }  
    
  return 1;
}


int TiffGetImageParametersFromPathName(fullPath *filename, pt_tiff_parms *tiffData)
{
  TIFF *file;
  int returnValue;

  assert(filename != NULL);
  assert(tiffData != NULL);
  file = OpenTiffFromFullPath(filename, "r");
  if (file == NULL) {
    PrintError("Could not open TIFF file %s", filename->name);
    return 0;
  }
  returnValue = TiffGetImageParameters(file, tiffData);
  TIFFClose(file);

  return returnValue;
}



int TiffSetImageParameters(TIFF *tiffFile, pt_tiff_parms *tiffData)
{
  int returnValue = 1;

  assert(tiffFile != NULL);
  assert(tiffData != NULL);

  // Each of the invocations below returns 1 if ok, 0 if error. 

  returnValue = TIFFSetField(tiffFile, TIFFTAG_IMAGEWIDTH, tiffData->imageWidth)
    && TIFFSetField(tiffFile, TIFFTAG_IMAGELENGTH, tiffData->imageLength)
    && TIFFSetField(tiffFile, TIFFTAG_BITSPERSAMPLE, tiffData->bitsPerSample)
    && TIFFSetField(tiffFile, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
    && TIFFSetField(tiffFile,  TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)
    && TIFFSetField(tiffFile, TIFFTAG_SAMPLESPERPIXEL, tiffData->samplesPerPixel)
    && TIFFSetField(tiffFile, TIFFTAG_COMPRESSION, tiffData->compression)
    && TIFFSetField(tiffFile, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT)
    && TIFFSetField(tiffFile,  TIFFTAG_ROWSPERSTRIP, tiffData->rowsPerStrip);

  if (returnValue && tiffData->compression == COMPRESSION_LZW) {
    returnValue = TIFFSetField(tiffFile, TIFFTAG_PREDICTOR, tiffData->predictor);
  }

  return returnValue;
  
}



int VerifyTiffsAreCompatible(fullPath *tiffFiles, int numberImages)
{
  int currentImage;
  pt_tiff_parms firstFileParms;
  pt_tiff_parms otherFileParms;
  char *errorMsg;

  //MRDL: Reluctantly commented these out...the calls to TIFFSetWarningHandler and 
  //TIFFSetErrorHandler cause to GCC to abort, with a series of errors like this:
  //../../../LibTiff/tiff-v3.6.1/libtiff/libtiff.a(tif_unix.o)(.text+0x11a): In function `TIFFOpen':
  //../../../libtiff/tiff-v3.6.1/libtiff/../libtiff/tif_unix.c:144: multiple definition of `TIFFOpen'
  //../libpano12.a(dyces00121.o)(.text+0x0): first defined here
  // Make sure we have a tiff error handler
  //TIFFSetWarningHandler(tiffErrorHandler);
  //TIFFSetErrorHandler(tiffErrorHandler);
  
  // Open TIFFs

  if (!TiffGetImageParametersFromPathName(&tiffFiles[0], &firstFileParms)) {
    PrintError("Unable to read tiff file");
    return 0;
  }

  for (currentImage = 1;  currentImage < numberImages ; currentImage ++ ) {
    
    if (!TiffGetImageParametersFromPathName(&tiffFiles[currentImage], &otherFileParms)) {
      PrintError("Unable to read tiff file");
      return 0;
    }
    
    errorMsg = NULL;
    if (firstFileParms.imageWidth != otherFileParms.imageWidth) {
      errorMsg = "Image 0 and %d do have the same width\n";
    } else if (firstFileParms.imageLength != otherFileParms.imageLength) {
      errorMsg = "Image 0 and %d do have the same length\n";
    } else if (firstFileParms.bitsPerPixel != otherFileParms.bitsPerPixel) {
      errorMsg = "Image 0 and %d do have the same colour depth\n";
    } else if (firstFileParms.bytesPerLine  != otherFileParms.bytesPerLine) {
      errorMsg = "Image 0 and %d do have the same scan line size\n";
    } else {
      ;
    }
    if (errorMsg != NULL) {
      PrintError(errorMsg, currentImage);
      return 0;
    }
  } // for loop

  return 1;

}


int  CreatePSD(  fullPath *fullPathImages, int numberImages, fullPath* outputFileName)
{

  Image *ptrImage;
  int i;
  stBuf stitchInfo;
  fullPath tempFile;
  char tempString[128];
  Image image;

  assert(numberImages > 0);
  assert(fullPathImages != NULL);
  assert(outputFileName != NULL);
  
  if ( ptQuietFlag == 0 ) {
    Progress(_initProgress, "Converting TIFF to PSD");
    sprintf(tempString, "%d", 100/numberImages);
    Progress(_setProgress, tempString);
  }

  // Process background of PSD
  SetImageDefaults(&image);

  if (readTIFF(&image, &fullPathImages[0]) != 0 ) {
  
    PrintError("Could not read TIFF image No 0");
	 if ( ptQuietFlag == 0 )
      Progress(_disposeProgress, tempString);

    return -1;
  }


  if (!( image.bitsPerPixel == 64 ||  image.bitsPerPixel == 32)  ) {
    PrintError("Image type not supported (%d bits per pixel)\n", image.bitsPerPixel);
    return 0;
  }

  if (numberImages > 1 && image.bitsPerPixel != 32) {
    if ( image.bitsPerPixel == 64 ) {
      PrintError("Panotools is not able to save 16bit PSD images. Downsampling to 8 bit");
      TwoToOneByte(&image); //we need to downsample to 8 bit if we are provided 16 bit images
    }
  }

  //Write out the first image as the base layer in the PSD file
  if (writePSDwithLayer(&image, outputFileName) != 0) {
    PrintError("Could not write PSD-file");
    if ( ptQuietFlag != 0 ) 
      Progress(_disposeProgress, tempString);
    return -1;
  }

  myfree((void**)image.data);
  ptrImage = &image;
  
  //Now iterate over all other images and add them as layers to the PSD file
  for (i = 1; i < numberImages; i++) {
    
    if ( ptQuietFlag == 0 ) {
		sprintf(tempString, "%d", i * 100/numberImages);    
      if ( Progress(_setProgress,tempString) == 0 ) {
		  remove(outputFileName->name);
		  return -1;
      }
    }

    if (readTIFF(ptrImage, &fullPathImages[i]) != 0) {

		PrintError("Could not read TIFF image No &d", i);
      if ( ptQuietFlag == 0 )
		  Progress(_disposeProgress, tempString);
      return -1;
    }
    
    // We can't process 16 bit TIFFs. We have to downsample to 8 bit if necessary
    if ( image.bitsPerPixel == 64 ) 
      TwoToOneByte(ptrImage);
    
    // Create a new file with the result PSD, then delete the current one
    
    strcpy(tempFile.name, outputFileName->name);
    
    if (makeTempPath(&tempFile) != 0) {
      PrintError("Could not make Tempfile");
      return -1;
      
    }

    stitchInfo.seam = 1;
    stitchInfo.feather = 0;
        
    if (addLayerToFile(ptrImage, outputFileName, &tempFile, &stitchInfo) != 0) {
      PrintError("Could not write Panorama File");
      return -1;
    }
    
    remove(outputFileName->name);
    rename(tempFile.name, outputFileName->name);
    
    myfree((void**)image.data);
  }
  
  if (! ptQuietFlag) {
    Progress(_setProgress, "100");
    Progress(_disposeProgress, tempString);
  }

  return 0;
}


static void ComputeStitchingMask8bits(Image *image)
{

  int column;
  int row;
  unsigned char *ptr;
  unsigned char *pixel;
  uint16_t  *ptrCounter;
  uint16_t count;

  // Use the GreenBlue pixel area is used to keep a counter of the
  // minimum distance (in pixels) away we are from the edges of the
  // mask (horizontal or vertical)

  // The algorithm is fairly simple:

  // For each column
  //   Process each row from top to down
  //     Set each pixel counter to the number of pixels from edge of the mask (from the left)
  //   Process each row from bottom to top
  // Set each pixel counter to the minimum between current counter and number of pixels
  // from edge (from the right)

  // for each row
  //   repeat the same algorithm (done per column)
  
  for (column = 0; column < image->width; column ++) {
    count = 0;
    // Point to the given column in row 0
    ptr = *image->data + column * 4;
    //    fprintf(stderr, "St1.1 Column[%d]\n", column);

    for (row = 0; row < image->height; row ++) {
      pixel = row * image->bytesPerLine + ptr;
      count = (*pixel == 0) ? 0 : count+1;

      ptrCounter = (uint16_t*)(pixel +2);
      *ptrCounter = count;
    }


    count = 0;
    row = image->height;

    while (--row  >= 0) {
      pixel = ptr + row + image->bytesPerLine;

      count = (*pixel == 0) ? 0 : count+1;

      ptrCounter = (uint16_t*)(pixel +2);
      if ( *ptrCounter < count)
        count = *ptrCounter; 
      else
        *ptrCounter  = count;
    
    } //while
    
    //    fprintf(stderr, "St1.5 Column[%d]\n", column);

    
  } //

  ///////////// row by row

  //  fprintf(stderr, "St2\n");

  for (row = 0; row < image->height; row ++) {
    count = image->width;
    ptr = row * image->bytesPerLine + *(image->data);

    // process from left to right
    for (column = 0; column < image->width; column ++ ) {
      pixel = ptr + 4 * column;
      count = (*pixel == 0) ? 0 : count+1;

      ptrCounter = (uint16_t*)(pixel +2);
      
      if (*ptrCounter > count)
		  *ptrCounter = count;
    } // for column

    //-----------------------------;;


    //  fprintf(stderr, "St3\n");

	 count = 0;
	 column = image->width;

    while (--column >= 0) {
      pixel = ptr + column * 4;

      count = (*pixel == 0) ? 0 : count+1;

      ptrCounter =  (uint16_t*)(pixel +2);
      if (*ptrCounter < count)
		  count = *ptrCounter;
      else
        *ptrCounter = count;

    } //    while (--column >= 0)


  } // end of for row


}

static void ComputeStitchingMask16bits(Image *image)
{

  int column;
  int row;
  unsigned char *ptr;
  uint16_t *pixel;
  uint16_t  *ptrCounter;
  uint16_t count;

  //  fprintf(stderr, "St1\n");

  for (column = 0; column < image->width; column ++) {

    count = 0;

    // Point to the given column in row 0

    ptr = *image->data + column * 8;

    //    fprintf(stderr, "St1.1 Column[%d]\n", column);

    for (row = 0; row < image->height; row ++) {

      //      fprintf(stderr, "St1.2 Column[%d] Row[%d]\n", column, row);

      pixel = (uint16_t*) (row * image->bytesPerLine + ptr);

      if (*pixel == 0) {
   
		  count = 0;
   
      } else {

		  count ++;
   
      }
      // Use the G pixel area to keep a count of how many pixels we have seen in 
      // the mask area.

      ptrCounter = pixel +2;
      *ptrCounter = count;

    } //     for (row = 0; row < image->heght; row ++) {


    //    fprintf(stderr, "St1.3 Column[%d]\n", column);

    count = 0;
    row = image->height;

    while (--row  >= 0) {

      //      fprintf(stderr, "St1.4 Column[%d] Row[%d]\n", column, row);

      pixel = (uint16_t*) (row * image->bytesPerLine + ptr);

      if ( *pixel == 0 ) {

		  count = 0;

      } else {

		  count ++;

      }

      ptrCounter = pixel +2;

      if ( *ptrCounter < count) {
      
		  count = *ptrCounter; 
      
      } else {
      
		  *ptrCounter  = count;
      
      }
    
    
    } //while
    
    //    fprintf(stderr, "St1.5 Column[%d]\n", column);

    
  } //

  ///////////// row by row

  //  fprintf(stderr, "St2\n");
  //  return; ///AAAAAAAAAAAAAAAA


  for (row = 0; row < image->height; row ++) {

    count = image->width;
    
    ptr = row * image->bytesPerLine + *(image->data);

    for (column = 0; column < image->width; column ++ ) {
      
      
      pixel = (uint16_t*)(ptr + 4 * 2 * column);
      
      if ( *pixel == 0 ) {
   
		  count = 0;
   
      } else {
   
		  count ++;
   
      }
      
      ptrCounter = pixel +2;
      
      if (*ptrCounter < count) {
   
		  // count = *ptrCounter; AAAAAAAAAAAAA
   
      } else {
   
		  *ptrCounter = count;
   
      } //

    } // for column

    //-----------------------------;;


    for (column = 0; column < image->width; column ++ ) {


      pixel = (uint16_t*)(ptr + 4 * 2 * column);

      if ( *pixel == 0 ) {
		  count = 0;
      } else {
		  count ++;
      }

      ptrCounter = pixel +2;

      if (*ptrCounter < count) {
		  // count = *ptrCounter; AAAAAAAAAAAAAAA
      } else {
		  *ptrCounter = count;
      }
    } // for

    //---------------------------------------;

    //  fprintf(stderr, "St3\n");

    count = image->width;
    column = image->width;

    while (--column >= 0) {

      pixel = (uint16_t*)(ptr + 4 * 2 * column);

      if (0 == *pixel ) {
		  count = 0;
      } else {
		  count ++;
      }

      ptrCounter =  pixel +2;

      if (*ptrCounter < count) {
		  count = *ptrCounter;
      } else {
		  *ptrCounter = count;
      }
    } //    while (--column >= 0) {

    //--------------------------------;
    column = image->width;

    //  fprintf(stderr, "St4\n");

    while (--column >= 0) {

      pixel = (uint16_t*)(ptr + 4 * 2 * column);


      if ( *pixel == 0 ) {
		  count = 0;
      } else {
		  count ++;
      }

      ptrCounter =  pixel +2;

      if (*ptrCounter < count) {
   
		  count = *ptrCounter ;
      
      } else {
		  *ptrCounter = count;
      }
    } // end of while
    
  } // end of for row
}


static void ComputeStitchingMaskMap(Image *image)
{
  if ( image->bitsPerPixel == 32 ) {
    ComputeStitchingMask8bits(image);
    return;
  } else  if ( image->bitsPerPixel == 64 ) {
    ComputeStitchingMask16bits(image);
    return;
  }
  fprintf(stderr, "Masking not supported for this image type (%d bitsPerPixel)\n", (int)image->bitsPerPixel);
  exit(1);
}




//
// Compute the map of the stitching mask and create a file with it.
// The stitching mask will be contained in the GB channels (this is,
// the 16 bits corresponding to the G and B channel will contain a uint16_t that
// contains, for that particular point, the stitching mask.
//
int CreateMaskMapFiles(fullPath *inputFiles, fullPath *maskFiles, int numberImages)
{
  int index;
  char tempString[512];
  Image image; 

  if ( ptQuietFlag == 0 ) 
    Progress(_initProgress, "Preparing Stitching Masks");

  // for each image, create merging mask and save to temporal file
  for (index = 0; index < numberImages; index ++ ) {
    
    sprintf(tempString, "%d", index * 100/numberImages);
    
    // Do progress
    if ( ptQuietFlag == 0 ) {
      if (Progress(_setProgress, tempString) ==0 ) {
		  return 0;
      }
    }
    
    if (readTIFF(&image, &inputFiles[index]) != 0) {
      PrintError("Could not read TIFF-file");
      return 0;
    }

    // Compute the stitching mask in-situ
    ComputeStitchingMaskMap(&image);

    strcpy(maskFiles[index].name, inputFiles[0].name); 

    if (makeTempPath(&maskFiles[index]) != 0) {
      PrintError("Could not make Tempfile");
      return -1;
    }

    writeTIFF(&image, &maskFiles[index]);
    
    //    fprintf(stderr, "Written to file %s\n", maskFiles[index].name);
    
    myfree((void**)image.data);

  } // for (index...

    
    // Do progress

  if (!ptQuietFlag)

    Progress(_setProgress, "100");
  Progress(_disposeProgress, tempString);

  return 1;
}






static int  ReplaceAlphaChannels(fullPath *inputImagesNames, fullPath *masksNames, fullPath *outputNames, int numberImages,
											pt_tiff_parms *imageDefaultParms)
{
  int index;
  unsigned char *imageRowBuffer = NULL;
  unsigned char *maskRowBuffer = NULL;
  char tempString[MAX_PATH_LENGTH];
  int row;
  int j;

  TIFF *imageFile;
  TIFF *outputFile;
  TIFF *maskFile;

  if ((imageRowBuffer = calloc(imageDefaultParms->bytesPerLine,1 )) == NULL ||
      (maskRowBuffer = calloc(imageDefaultParms->bytesPerLine,1 )) == NULL ) {
    PrintError("Not enough memory");
    return -1;
  }

  // The algorith is fairly simple. 

  // For each image
  //   For each row in that image
  //     Read row of image
  //     Read row of mask
  //     Replace alpha channel in image with masks alpha channel
  //     Write row

  if (!ptQuietFlag) {
    Progress(_initProgress, "Inserting Alpha Channel");
  }

  for (index = 0; index < numberImages; index ++) { //

    // Do one file at a time

    if ( ptQuietFlag == 0 ) {
      sprintf(tempString, "%d", index * 100/ numberImages);
      if (Progress(_setProgress, tempString) == 0) {
		  return 0;
      }
    }
    
    // Open input image 
    if ( (imageFile = OpenTiffFromFullPath(&inputImagesNames[index], "r")) == NULL) {
      PrintError("Could not open TIFF-file");
      return 0;
    }

    // Open mask file
    if ( (maskFile = OpenTiffFromFullPath(&masksNames[index], "r")) == NULL) {
      PrintError("Could not open mask file");
      return 0;
    }

    // Create output file
    if ((outputFile = OpenTiffFromFullPath(&outputNames[index], "w")) == NULL) {
      PrintError("Could not create TIFF-file");
      return 0;
    }
    if (!TiffSetImageParameters(outputFile,imageDefaultParms)) {
      PrintError("Unable to initialize TIFF file\n");
      return 0;
    }

    // start processing each row

    for (row = 0; row < imageDefaultParms->imageLength; row ++) {

      unsigned char *source;
      unsigned char *destination;

      TIFFReadScanline(imageFile, imageRowBuffer, row, 0);

      TIFFReadScanline(maskFile, maskRowBuffer, row, 0);

      // Depending on the type of image, process...

      if ( imageDefaultParms->bitsPerPixel == 32 ) {

		  destination = imageRowBuffer + 3;
		  source = maskRowBuffer + 3;

		  for (j = 0; j < imageDefaultParms->imageWidth; j ++ ) {
			 *destination = *source;
			 destination +=4;
			 source +=4;
		  } 

      } else {

		  destination = imageRowBuffer + 6;
		  source = maskRowBuffer + 6;

		  for (j = 0; j < imageDefaultParms->imageWidth ; j ++ ) {
			 *destination = *source;

			 source += 8;
			 destination += 8;

		  } // for j
       
      } // end of if

		if (TIFFWriteScanline(outputFile, imageRowBuffer, row, 0) != 1) {
		  PrintError("Unable to write scan line\n");
		  TIFFClose(imageFile);
		  TIFFClose(maskFile);
		  TIFFClose(outputFile);
		  return 0;
		}
    } // for 

    TIFFClose(imageFile);
    TIFFClose(maskFile);
    TIFFClose(outputFile);

  } // for index

  if (!ptQuietFlag) 
    Progress(_disposeProgress, tempString);


  free(imageRowBuffer);
  free(maskRowBuffer);

  return 1;

}

static int  ReplaceAlphaChannel(fullPath *inputImage, fullPath *mask, fullPath *output)
{
  unsigned char *imageRowBuffer = NULL;
  unsigned char *maskRowBuffer = NULL;
  int row;
  int j;

  int returnValue = 0;
  
  unsigned char *source;
  unsigned char *destination;

  TIFF *imageFile;
  TIFF *outputFile;
  TIFF *maskFile;
  
  CropInfo crop_info;
  pt_tiff_parms croppedImageParms;
  
  //   For each row
  //     Read row of image
  //     Read row of mask
  //     Replace alpha channel in image with masks alpha channel
  //     Write row
  //
  //Note that all three images involved here (input image, mask image and 
  //resulting image) have the same dimensions, and we don't care what the 
  //dimensions are the for the final output image  

  // Open input image   
  if ( (imageFile = OpenTiffFromFullPath(inputImage, "r")) == NULL) {
    PrintError("Could not open TIFF-file");
    return 0;
  } else {
    TiffGetImageParameters(imageFile, &croppedImageParms);
    getCropInformationFromTiff(imageFile, &crop_info);
  }
  
  //Allocate line buffers for image and mask
  if ((imageRowBuffer = calloc(croppedImageParms.bytesPerLine,1 )) == NULL ||
      (maskRowBuffer = calloc(croppedImageParms.bytesPerLine,1 )) == NULL ) {
    PrintError("Not enough memory");
    return 0;
  }  
  
  // Open mask file
  if ( (maskFile = OpenTiffFromFullPath(mask, "r")) == NULL) {
    PrintError("Could not open mask file");
    return 0;
  }
  
  // Create output file
  if ((outputFile = OpenTiffFromFullPath(output, "w")) == NULL) {
    PrintError("Could not create TIFF-file");
    return 0;
  }
  
  if (!TiffSetImageParameters(outputFile, &croppedImageParms)) {
    PrintError("Unable to initialize TIFF file\n");
    return 0;
  }
  
  // Processing one row at a time
  int jumpBytes          = (croppedImageParms.bitsPerPixel == 32 ) ? 4 : 8;
  int alphaChannelOffset = (croppedImageParms.bitsPerPixel == 32 ) ? 3 : 6;
  
  for (row = 0; row < croppedImageParms.imageLength; row ++) {
    
    TIFFReadScanline(imageFile, imageRowBuffer, row, 0);
    TIFFReadScanline(maskFile, maskRowBuffer, row, 0);
    
    destination = imageRowBuffer + alphaChannelOffset;
    source      = maskRowBuffer  + alphaChannelOffset;
      
    for (j = 0; j < croppedImageParms.imageWidth; j++ ) {
      *destination = *source;
      destination += jumpBytes;
      source      += jumpBytes;
    } 
    
    if (TIFFWriteScanline(outputFile, imageRowBuffer, row, 0)!= 1) {
      PrintError("Unable to write data to output file\n");
      returnValue = 0;
      goto end;
    }
    
  }
  
  returnValue = 1;
 end:

  TIFFClose(imageFile);
  TIFFClose(maskFile);
  TIFFClose(outputFile);
  
  free(imageRowBuffer);
  free(maskRowBuffer);

  return returnValue;

}


static void SetBestAlphaChannel16bits(unsigned char *imagesBuffer, int numberImages, pt_tiff_parms *imageParms)
{
  //  fprintf(stderr, "SetBestAlphaChannel16bits not supported yet\n");
  //assert(0); // it should not be here... yet

  unsigned char *pixel;
  uint16_t *ptrCount;
  uint16_t best;
  uint16_t maskValue;
  int column;
  int j;


  for  (column=0, pixel = imagesBuffer;  column < imageParms->imageWidth; column++, pixel +=8) {

    best = 0;
    ptrCount = (uint16_t*)(pixel + 2);
    maskValue = *ptrCount;

    // find the image with the highest value

    for (j = 1; j < numberImages; j ++) {

      ptrCount = (uint16_t*)(pixel + imageParms->bytesPerLine * j  + 2);

      if (*ptrCount > maskValue) {

		  best = j;
		  maskValue = *ptrCount;
      
      }
    } // for j

    if ( maskValue != 0 ) {

      // set the mask of the ones above, but not below... interesting...

      for (j = best+1;  j < numberImages; j ++ ) {
		  uint16_t *pixel2;
  
		  pixel2 = (uint16_t*)(pixel +  imageParms->bytesPerLine * j);

		  if (0 != *pixel2) {
			 *pixel2 = 1;
		  } 
      }
    }
  } // for i




}

static void SetBestAlphaChannel8bits(unsigned char *imagesBuffer, int numberImages, pt_tiff_parms *imageParms)
{
  unsigned char *pixel;
  uint16_t *ptrCount;
  uint16_t best;
  uint16_t maskValue;
  int column;
  int j;


  for  (column=0, pixel = imagesBuffer;  column < imageParms->imageWidth; column++, pixel +=4) {

    best = 0;
    ptrCount = (uint16_t*)(pixel + 2);
    maskValue = *ptrCount;

    // find the image with the highest value

    for (j = 1; j < numberImages; j ++) {

      ptrCount = (uint16_t*)(pixel + imageParms->bytesPerLine * j  + 2);

      if (*ptrCount > maskValue) {

		  best = j;
		  maskValue = *ptrCount;
      
      }
    } // for j

    if ( maskValue != 0 ) {

      // set the mask of the ones above, but not below... interesting...

      for (j = best+1;  j < numberImages; j ++ ) {
		  unsigned char *pixel2;
  
		  pixel2 = pixel +  imageParms->bytesPerLine * j;

		  if (0 != *pixel2) {
			 *pixel2 = 1;
		  } 
      }
    }
  } // for i

}


static void CalculateAlphaChannel(unsigned char *imagesBuffer, int numberImages, pt_tiff_parms *imageParms)
{

  if (imageParms->bitsPerPixel == 32) {
    SetBestAlphaChannel8bits(imagesBuffer, numberImages, imageParms);
  } else if (imageParms->bitsPerPixel == 64) {
    SetBestAlphaChannel16bits(imagesBuffer, numberImages, imageParms);
  } else {
    fprintf(stderr, "CalculateAlphaChannel not supported for this image type (%d bitsPerPixel)\n", imageParms->bitsPerPixel);
    exit(1);
  }
}


/**
 * imageParameters contains the dimensions of the output image, which may not be 
 * the same as the dimensions fo the input images if they are "cropped"
 */
static int CreateAlphaChannels(fullPath *masksNames, fullPath *alphaChannelNames,
										 int numberImages, pt_tiff_parms *fullSizeImageParameters)
{
  TIFF **tiffMasks;
  TIFF **tiffAlphaChannels;
  unsigned char *imagesBuffer;
  unsigned char *ptrBuffer;
  unsigned char *ptrPixel;
  int index;
  char tempString[24];
  
  int returnValue = 0;  
  pt_tiff_parms inputImageParameters;
  int croppedImageRowIndex;
  int fullSizeRowIndex;
  CropInfo crop_info;

  
  // Allocate arrays of TIFF* for the input and output
  // images. process is one row at a time, with all images
  // processed at the same time
  tiffMasks         = (TIFF**)calloc(numberImages, sizeof(TIFF*));
  tiffAlphaChannels = (TIFF**)calloc(numberImages, sizeof(TIFF*));
  
  // The imagesBuffer contains as many rows as we have input images, and 
  // each row is as wide as the final output image
  imagesBuffer = calloc(numberImages, fullSizeImageParameters->bytesPerLine);
  
  if (imagesBuffer == NULL || tiffAlphaChannels == NULL || imagesBuffer == NULL) {
    PrintError("Not enough memory");
    return 0;
  }

  if ( ptQuietFlag == 0 )
    Progress(_initProgress, "Calculating Alpha Channel");

  // Alpha Channel calculation    
  // Open for read
  //       mask files
  //  and  input files
  // Open for write  alpha channel files

  // Open up an input image, then create a corresponding output image...repeat for all images
  for (index = 0; index < numberImages; index++) {
  
    if ((tiffMasks[index] = OpenTiffFromFullPath(&masksNames[index], "r")) == 0) {
      PrintError("Could not open TIFF-file");
      return 0;
    }
    
    TiffGetImageParameters(tiffMasks[index], &inputImageParameters);

    strcpy(alphaChannelNames[index].name, masksNames[0].name);

    if (makeTempPath(&alphaChannelNames[index]) != 0) {
      PrintError("Could not make Tempfile");
      return 0;
    }

    tiffAlphaChannels[index] = OpenTiffFromFullPath(&alphaChannelNames[index], "w");

    if ( tiffAlphaChannels[index] == NULL ) {
      PrintError("Could not create TIFF-file");
      return 0;
    }

    if (!TiffSetImageParameters(tiffAlphaChannels[index], &inputImageParameters)) {
      PrintError("Unable to initialize TIFF file\n");
      return 0;
    }
        

  }// finished opening up output files
  
   

  //  fprintf(stderr, "Files have been created, process each row\n");
 
  //iterate one row at a time, and for each row process all images
  for (fullSizeRowIndex = 0; fullSizeRowIndex < fullSizeImageParameters->imageLength; fullSizeRowIndex++) {

    if ( ptQuietFlag == 0 ) {
      if ( fullSizeRowIndex== (fullSizeRowIndex/ 20) * 20 ) {
        sprintf(tempString, "%lu", fullSizeRowIndex * 100/fullSizeImageParameters->imageLength);
        if (Progress(_setProgress, tempString) == 0) {
          for (index = 0 ; index < numberImages; index ++) {
				TIFFClose(tiffMasks[index]);
				TIFFClose(tiffAlphaChannels[index]);
				remove(alphaChannelNames[index].name);
			 }
			 return 0;
		  }
      } 
    }
    
    //    fprintf(stderr, "To process row [%d] bytesperline %d\n", i, bytesPerLine);
    //read data from all input images
    for (ptrBuffer = imagesBuffer, index = 0; index < numberImages; index++, ptrBuffer += fullSizeImageParameters->bytesPerLine) {

      memset(ptrBuffer, 0, fullSizeImageParameters->bytesPerLine);
      
      getCropInformationFromTiff(tiffMasks[index], &crop_info);
      croppedImageRowIndex = fullSizeRowIndex - crop_info.y_offset;
      
      if (croppedImageRowIndex >= 0 && croppedImageRowIndex < crop_info.cropped_height ) {
      
        if (TIFFReadScanline(tiffMasks[index], ptrBuffer + (crop_info.x_offset * fullSizeImageParameters->bitsPerPixel/8), croppedImageRowIndex, 0) != 1) {
          PrintError("Error reading temporary TIFF data");
          returnValue = 0;
          goto end;
        }
      }
      RGBAtoARGB(ptrBuffer, fullSizeImageParameters->imageWidth, fullSizeImageParameters->bitsPerPixel);      
      
    }

	 //calculate the alpha channel for this row in all images
    CalculateAlphaChannel(imagesBuffer, numberImages, fullSizeImageParameters);

	 //write out the alpha channel data for this row to all output images
    for (index = 0 , ptrBuffer = imagesBuffer; index < numberImages; index++, ptrBuffer += fullSizeImageParameters->bytesPerLine) {

      ARGBtoRGBA(ptrBuffer, fullSizeImageParameters->imageWidth, fullSizeImageParameters->bitsPerPixel);
      
      getCropInformationFromTiff(tiffMasks[index], &crop_info);
      croppedImageRowIndex = fullSizeRowIndex - crop_info.y_offset;
            
      if (croppedImageRowIndex >= 0 && croppedImageRowIndex < crop_info.cropped_height ) {

        if ( TIFFWriteScanline(tiffAlphaChannels[index], ptrBuffer + (crop_info.x_offset * fullSizeImageParameters->bitsPerPixel/8), croppedImageRowIndex, 0) != 1) {
          PrintError("Unable to write data to output file\n");
          returnValue = 0;
          goto end;
        }
      }
    }

  } //for fullSizeRowIndex

  returnValue = 1;
  
 end:

  if (!ptQuietFlag) {
    Progress(_setProgress, "100");
    Progress(_disposeProgress, "");
  }

  for (index = 0; index < numberImages; index ++) {
    TIFFClose(tiffMasks[index]);
    TIFFClose(tiffAlphaChannels[index]);
  } // for index.

  free(imagesBuffer);
  free(tiffAlphaChannels);
  free(tiffMasks);

  return returnValue;
}

static void ApplyFeather8bits(Image *image, int featherSize)
{

  fprintf(stderr, "\nFeathering 8 bits not implemented yet\n");
  
}

static void ApplyFeather16bits(Image *image, int featherSize)
{

  fprintf(stderr, "\nFeathering 16 bits not implemented yet\n");
  
}


static int ApplyFeather(fullPath *inputFile, fullPath *outputFile, int featherSize)
{
  Image image;
  if (readTIFF(&image, inputFile) != 0) {
    PrintError("Could not open TIFF-file [%s]", inputFile->name);
    return 0;
  }

  //  fprintf(stderr, "To apply feather %d\n", featherSize);
  if (image.bitsPerPixel == 32) {
    ApplyFeather8bits(&image, featherSize);
  } else if (image.bitsPerPixel == 64) {
    ApplyFeather16bits(&image, featherSize);
  } else {
    fprintf(stderr, "Apply feather not supported for this image type (%d bitsPerPixel)\n", (int)image.bitsPerPixel);
    exit(1);
  }

  if (writeTIFF(&image, outputFile) != 0) {
    PrintError("Could not write TIFF-file [%s]", outputFile->name);
    return 0;
  }

  myfree((void**)image.data);

  return 1;

}

/**
 * Replaces the alpha channel in each image in inputFiles with a generated
 * mask.  The mask is calculated so as to route the seam between overlapping
 * images through the center of the overlap region...
 */
int AddStitchingMasks(fullPath *inputFiles, fullPath *outputFiles, int numberImages, int featherSize)
{
  fullPath *alphaChannelFiles;
  fullPath *maskFiles;
  int i;
  Image image ;
  char tempString[512];
  pt_tiff_parms imageParameters;
  CropInfo crop_info;

  if ( numberImages == 0 ) {
    return 0;
  }

  SetImageDefaults(&image);

  maskFiles = calloc(numberImages, sizeof(fullPath));
  alphaChannelFiles = calloc(numberImages, sizeof(fullPath));

  if ( maskFiles == NULL ||
       alphaChannelFiles == NULL ) {
    PrintError("Not enough memory");
    return -1;
  } 

  // CREATE stitching maps
  if (!CreateMaskMapFiles(inputFiles, maskFiles, numberImages)) {
    PrintError("Could not create the stitching masks");
    return -1;
  }
  //  exit(1);

  // Get TIFF information from 0th image in input list
  if (!TiffGetImageParametersFromPathName(&inputFiles[0], &imageParameters)) {
    PrintError("Could not read TIFF-file");
    return -1;
  }
  
  getCropInformation(inputFiles[0].name, &crop_info);
  
  // Because the 0th intermediate TIFF file might be a "cropped" file, we 
  // need to update the imageParameters so that the dimensions reflect the
  // the size of the full-sized output image, rather than one of the 
  // (potentially) cropped intermediate files
  setFullSizeImageParameters(&imageParameters, &crop_info);  
  
  if (!CreateAlphaChannels(maskFiles, alphaChannelFiles, numberImages, &imageParameters)) {
    PrintError("Could not create alpha channels");
    return -1;
  }

  // From this point on we do not need to process all files at once. This will save temporary disk space
  
  for (i=0;i<numberImages;i++) {
    fullPath withAlphaChannel;

    sprintf(tempString, "%d", 100 * i/ numberImages);

    if ( ptQuietFlag == 0 ) {
      if (Progress(_setProgress, tempString) == 0) {
		  // We have to delete any temp file
		  return -1;
      }
    }

    // We no longer need the mask files
    remove(maskFiles[i].name);

    // Reuse the temporary name
    memcpy(&withAlphaChannel, &maskFiles[i], sizeof(fullPath));

    // Replace the alpha channel of the input image
    if (!ReplaceAlphaChannel(&inputFiles[i], &alphaChannelFiles[i], &withAlphaChannel)) {
      PrintError("Unable to replace alpha channel in image %d", i);
      return -1;
    }
    // we no longer need the alpha channel
    remove(alphaChannelFiles[i].name);

    if (featherSize > 0) {

      fullPath feathered;
      memcpy(&feathered, &maskFiles[i], sizeof(fullPath));

      if (!ApplyFeather(&withAlphaChannel, &feathered, featherSize)) {
        PrintError("Unable to apply feather to image %d", i);
        return -1;
      }

      remove(withAlphaChannel.name);
      rename(feathered.name, outputFiles[i].name);

    } else {
      rename(withAlphaChannel.name, outputFiles[i].name);
    }
  }

  free(maskFiles);
  free(alphaChannelFiles);

  return 0;

}

void BlendLayers8Bit(unsigned char **imageDataBuffers, int counterImageFiles, char *resultBuffer, 
							int lines,
							int imageWidth, int scanLineSize)

{

  // 0x8(%ebp)    imageDataBuffers
  // 0xc(%ebp)    counterImageFiles
  // 0x10(%ebp)   resultBuffer
  // 0x14(%ebp)   lines
  // 0x18(%ebp)   imageWidth
  // 0x1c(%ebp)   scanLineSize

  // 0xffffffdc(%ebp)  imageIndex
  // 0xffffffe0(%ebp)  alphaChannel
  // 0xffffffe4(%ebp)  blue
  // 0xffffffe8(%ebp)  green
  // 0xffffffec(%ebp)  red
  // 0xfffffff0(%ebp)  rowOffset
  // 0xfffffff4(%ebp)  pixelOffset
  // 0xfffffff8(%ebp)  currentLine
  // 0xfffffffc(%ebp)  currentColumn

  int imageIndex = 0;
  unsigned int colours[3];
  unsigned int alphaChannel;
  unsigned int currentLine;
  unsigned int currentColumn;
  unsigned int rowOffset;

  currentLine = 0;

  for (currentLine = 0; currentLine < lines; currentLine++) {

    //printf("Currnet line %d\n", currentLine);

    rowOffset = scanLineSize * currentLine;

    for (currentColumn = 0; currentColumn < imageWidth; currentColumn++) {

      unsigned int pixelOffset;
      unsigned int i;

      //      printf("Currnet column %d\n", currentColumn);

      pixelOffset = rowOffset + currentColumn * 4;


      // Initialize colours for this pixel
      alphaChannel = 0;
      for (i=0;i<3;i++)
		  colours[i] =0;


      // Do alpha blending, from top to bottom. Bail out when alpha channel is equal to maximum

      for (imageIndex =counterImageFiles-1; imageIndex >= 0; imageIndex--) {

		  unsigned int alphaContribution;
		  unsigned char *ptrPixel;
		  unsigned int bottomAlpha;
		  unsigned int index;


		  // printf("Currnet image %d\n", imageIndex);


		  // The alpha blending algorithm is (for premultiplied values)

		  // C_result = C_above + C_below * (1 - alpha_above)
		  // A_result = Alpha_above + alpha_below * (1 - alpha_above)

		  // Find pixel in this layer
		  ptrPixel = imageDataBuffers[imageIndex] + pixelOffset;


		  // printf("TO read pixel\n");

		  bottomAlpha = *(ptrPixel + 3); // this should be the value of the mask for this particular pixel
   
		  // printf("After read pixel\n");

		  alphaContribution = ((0xff - alphaChannel) *  bottomAlpha)/0xff;

		  // I don't really think this step is necessary, but due to innestability of the calculation
		  // alphaContribution it might overflow the byte valuex

		  if ( alphaChannel + alphaContribution > 0xff ) {
			 alphaContribution = 0xff - alphaChannel;
		  }

		  alphaChannel += alphaContribution;

		  // Makek sure the alpha channel is within range
		  assert(alphaChannel >= 0 && alphaChannel <= 0xff);

		  // now do the colours

		  // printf("TO set pixel\n");

		  for (index = 0; index < 3; index ++) {
			 colours[index] += (*(ptrPixel+index) * alphaContribution)/0xff ; // 
			 if (!(colours[index] >= 0 && colours[index] <= 0xff)) {
				printf("PPPPPPPPPPPPPPPPPanic %d index [%d]\n", colours[index], index);
			 }
			 assert(colours[index] >= 0 && colours[index] <= 0xff);
		  }

		  // We don't need to continue if the alpha channel is at the max
		  if ( alphaChannel >= 0xff )
			 break;

      } // for (imageIndex =counterImageFiles-1; imageIndex >= 0; imageIndex--) {

      // Is it really necessary to check the values of the colours and alphachannel to make
      // sure they are not overflowing a byte?
      
      // Set the value of the pixel
      for (i=0;i<3;i++) {
		  assert(colours[i] <= 0xff && colours[i] >= 0);
		  *(resultBuffer + pixelOffset + i) = colours[i];
      }

      *(resultBuffer + pixelOffset + 3) = alphaChannel;


    } //(currentColumn < imageWidth)

  } //for currentLine < lines

}

void BlendLayers16Bit(unsigned char **imageDataBuffers, int counterImageFiles, char *resultBuffer, 
							 int lines,
							 int imageWidth, int scanLineSize)

{

  int imageIndex = 0;
  unsigned long long colours[3];
  unsigned long long alphaChannel;
  unsigned int currentLine;
  unsigned int currentColumn;
  unsigned int rowOffset;

  uint16_t *u16ResultBuffer=(uint16_t*)resultBuffer;
  uint16_t **u16ImageDataBuffers=(uint16**)imageDataBuffers;
  
  currentLine = 0;


  for (currentLine = 0; currentLine < lines; currentLine++) {

    //  printf("Lines %d\n", lines);
    //  printf("Width %d\n", imageWidth);
    //  printf("length %d\n", scanLineSize);


	 //printf("Currnet line %d\n", currentLine);

    // scanLineSize is in bytes, but we need the length in 16bit units
    rowOffset = (scanLineSize/2) * currentLine;

    for (currentColumn = 0; currentColumn < imageWidth; currentColumn++) {

      unsigned int pixelOffset;
      unsigned int i;

      //      printf("Currnet column %d\n", currentColumn);

      pixelOffset = rowOffset + currentColumn * 4;

      //      printf("Currnet offset %d\n", pixelOffset);

      // Initialize colours for this pixel
      alphaChannel = 0;
      for (i=0;i<3;i++)
		  colours[i] =0;


      // Do alpha blending, from top to bottom. Bail out when alpha channel is equal to maximum

      for (imageIndex =counterImageFiles-1; imageIndex >= 0; imageIndex--) {

		  unsigned long long alphaContribution;
		  uint16_t *ptrPixel;
		  unsigned long long bottomAlpha;
		  unsigned int index;


		  // printf("Currnet image %d\n", imageIndex);


		  // The alpha blending algorithm is (for premultiplied values)

		  // C_result = C_above + C_below * (1 - alpha_above)
		  // A_result = Alpha_above + alpha_below * (1 - alpha_above)

		  // Find pixel in this layer
		  ptrPixel = u16ImageDataBuffers[imageIndex] + pixelOffset;


		  // printf("TO read pixel\n");

		  bottomAlpha = *(ptrPixel + 3); // this should be the value of the mask for this particular pixel
   
		  //printf("After read pixel\n");

		  alphaContribution = ((0xffff - alphaChannel) *  bottomAlpha)/0xffff;

		  // I don't really think this step is necessary, but due to innestability of the calculation
		  // alphaContribution it might overflow the byte valuex

		  if ( alphaChannel + alphaContribution > 0xffff ) {
			 alphaContribution = 0xffff - alphaChannel;
		  }

		  alphaChannel += alphaContribution;

		  // Makek sure the alpha channel is within range
		  assert(alphaChannel >= 0 && alphaChannel <= 0xffff);

		  // now do the colours

		  //printf("TO set pixel\n");

		  for (index = 0; index < 3; index ++) {
			 colours[index] += (*(ptrPixel+index) * alphaContribution)/0xffff ; // 
			 if (!(colours[index] >= 0 && colours[index] <= 0xffff)) {
				printf("PPPPPPPPPPPPPPPPPanic %d index [%d]\n", colours[index], index);
			 }
			 assert(colours[index] >= 0 && colours[index] <= 0xffff);
		  }

		  // We don't need to continue if the alpha channel is at the max
		  if ( alphaChannel >= 0xffff )
			 break;

      } // for (imageIndex =counterImageFiles-1; imageIndex >= 0; imageIndex--) {

      // Is it really necessary to check the values of the colours and alphachannel to make
      // sure they are not overflowing a byte?
      //      printf("Done loop\n");      
      // Set the value of the pixel
      for (i=0;i<3;i++) {
		  assert(colours[i] <= 0xffff && colours[i] >= 0);
		  *(u16ResultBuffer + pixelOffset + i) = colours[i];
      }
      //      printf("Done loop 2\n");      
      *(u16ResultBuffer + pixelOffset + 3) = alphaChannel;


    } //(currentColumn < imageWidth)

  } //for currentLine < lines

}




static void BlendLayers(unsigned char **imageDataBuffers, unsigned int counterImageFiles, char *resultBuffer, 
								unsigned int linesToRead,
								unsigned int imageWidth, unsigned int bitsPerPixel, unsigned int scanLineSize)
{

  if (bitsPerPixel == 32) {
    BlendLayers8Bit(imageDataBuffers, counterImageFiles, resultBuffer, linesToRead, imageWidth, scanLineSize);
  } else if (bitsPerPixel == 64) {
    BlendLayers16Bit(imageDataBuffers, counterImageFiles, resultBuffer, linesToRead, imageWidth, scanLineSize);
  }
}

int FlattenTIFF(fullPath *fullPathImages, int counterImageFiles, fullPath *outputFileName, int removeOriginals)
{
           
  //+12   counterImageFil  es
  //+8    fullPathImages

  //  scanLineSize

  /*
	 unsigned int linesToRead;
	 unsigned int offsetThisPass;
	 unsigned int linesPerPass;

	 unsigned int samplesPerPixel;
  */
  TIFF **tiffFileHandles;
  unsigned char **imageDataBuffers;
  unsigned char *resultBuffer;
  
  
  fullPath tmpFullPath;
  char tmpFilename[512];
  pt_tiff_parms otherFileParms;
  pt_tiff_parms imageParameters;
  CropInfo crop_info;
  TIFF *tiffFile;
  unsigned int linesPerPass;

  unsigned int i;
  unsigned int offsetBeforeThisPass = 0;
  int linesLeft = 0;
  unsigned int linesToRead;
  int rowInPass;
  int inputImageRowIndex;
  int outputImageRowIndex;
  unsigned char *pixelPtr;  

  // Get TIFF parameters from the 0th intermediate file...we can reuse most of
  // these for the final flattened output file
  if (!TiffGetImageParametersFromPathName(&fullPathImages[0], &imageParameters)) {
    PrintError("Could not read TIFF-file");
    return -1;
  }
  
  strcpy(tmpFullPath.name, fullPathImages[0].name);
  
  // Read the crop information from the 0th file...this will tell us what 
  // the dimensions of the "full-size" output image should be
  getCropInformation(tmpFullPath.name, &crop_info);
  
  // Because the 0th intermediate TIFF file might be a "cropped" file, we 
  // need to update the imageParameters so that the dimensions reflect the
  // the size of the full-sized output image, rather than one of the 
  // (potentially) cropped intermediate files
  setFullSizeImageParameters(&imageParameters, &crop_info);
  
  //modify "tmpFullPath" to contain the name of a new, empty temp file
  if (makeTempPath(&tmpFullPath) != 0) {
    PrintError("Could not make Tempfile");
    return -1;
  }

  //copy the name of this new tmpFullPath into a string (tmpFilename)
  if (GetFullPath(&tmpFullPath, tmpFilename) != 0) {
    PrintError("Could not get filename");
    return -1;
  }

  //open up temporary output file for writing as a TIFF
  if ((tiffFile = TIFFOpen(tmpFilename, "w")) == 0) {
    PrintError("Could not create TIFF file");
    return -1;
  }

  TiffSetImageParameters(tiffFile, &imageParameters);

  // Calculate number of lines to read at a time so that we are reading 
  // approximately 500 KB at a time from each input file.  This could be 
  // memory intensive if we have an awful lot of images and not much memory, 
  // but probably not a big problem for 99.9% of cases on 99.9% of machines.
  linesPerPass = 500000 / imageParameters.bytesPerLine;

  if (linesPerPass == 0)
    linesPerPass = 1;
  
  // We dont need to read more lines that the size of the file
  if (imageParameters.imageLength < linesPerPass ) {
    linesPerPass = imageParameters.imageLength;
    if (linesPerPass == 0) {
      PrintError("Invalid image length in TIFF file. It might be corrupted");
      return -1;
    }
  }
  
  //Open up all intermediate TIFF files at once
  tiffFileHandles = calloc(counterImageFiles, sizeof(TIFF*));

  for (i = 0; i < counterImageFiles; i ++ ) {
    
    if (GetFullPath(&fullPathImages[i], tmpFilename) != 0) {
      PrintError("Could not get filename");
      return -1;
    }
    
    if ((tiffFileHandles[i] = TIFFOpen(tmpFilename, "r")) == 0) {
      PrintError("Could not open TIFF-Layer %d", i);
      return -1;
    }
    
  }

  // Create as many image data buffers as we have input files.  Note that the 
  // input buffers are as wide as the final output image, which may be more
  // than we technically need if the input images are cropped...it makes the 
  // code simpler, however.
  imageDataBuffers = calloc(counterImageFiles,sizeof(unsigned char*));

  for (i = 0; i < counterImageFiles; i ++) {
    imageDataBuffers[i] = calloc(linesPerPass * imageParameters.bytesPerLine, 1);
	 if (imageDataBuffers[i] == NULL) {
      PrintError("Not enough memory to allocate input buffers");
      return -1;
	 }    
  }

  //we need one buffer to store output result
  resultBuffer = calloc(linesPerPass * imageParameters.bytesPerLine, 1);
  
  if (resultBuffer == NULL) {
    PrintError("Not enough memory to allocate output buffer");
    return -1;
  }

  offsetBeforeThisPass = 0;

  if (ptQuietFlag == 0) {
    Progress(_initProgress, "Flattening Image");
  }

  //  printf("To do %d lines\n", imageParameters.imageLength);

  linesLeft = imageParameters.imageLength;

  // Main flattening loop...iterate over input files, read some data from each, 
  // combine into output buffer, write to file
  while (linesLeft > 0) {

    linesToRead = (linesLeft > linesPerPass) ? linesPerPass : linesLeft;

	 // iterate over each input file
    for (i = 0; i < counterImageFiles; i ++) {
      getCropInformationFromTiff(tiffFileHandles[i], &crop_info);

      // Get a few lines of data from this input file one row at a time
      for (rowInPass = 0; rowInPass < linesToRead; rowInPass++) {
      
        //figure out which row to read/write from input/output images
        outputImageRowIndex = offsetBeforeThisPass + rowInPass;
        inputImageRowIndex  = outputImageRowIndex - crop_info.y_offset;
        
        //point to first byte on this row of the input buffer
        pixelPtr = imageDataBuffers[i]  + (imageParameters.bytesPerLine * rowInPass);
      
		  //clear out any old data, and fill with empty space (zeros)
		  memset(pixelPtr, 0, imageParameters.bytesPerLine);        

		  // Only try to read data if we are reading from a row that exists in the 
		  // input image        
        if (inputImageRowIndex >= 0 && inputImageRowIndex < crop_info.cropped_height) {
          if (TIFFReadScanline(tiffFileHandles[i], pixelPtr + (crop_info.x_offset * imageParameters.bitsPerPixel/8), inputImageRowIndex, 0) != 1) {
            PrintError("Error reading tiff file\n");
            return -1;
          }
		  }
      } 
    }

    //    printf("Passing offsetAfterThisPass [%d] of [%d] linesPerPass  %d \n",offsetAfterThisPass, imageParameters.imageLength, linesPerPass);

    if (ptQuietFlag == 0) {
      sprintf(tmpFilename, "%d", (offsetBeforeThisPass+linesToRead)  * 100 / imageParameters.imageLength);
      if (Progress(_setProgress, tmpFilename) == 0)
        return -1;
    }
    
    // FlattenImageSection
    BlendLayers(imageDataBuffers, counterImageFiles, resultBuffer, linesToRead,
					 imageParameters.imageWidth, imageParameters.bitsPerPixel, imageParameters.bytesPerLine);

    for (i = 0; i < linesToRead; i++) {
      if (TIFFWriteScanline(tiffFile, resultBuffer + imageParameters.bytesPerLine * i, offsetBeforeThisPass + i, 0) != 1) {
        PrintError("Unable to write TIFF to file\n");
        return -1;
      }
    }

    offsetBeforeThisPass += linesToRead;
    linesLeft -= linesToRead;

  } 

  if (!ptQuietFlag) 
    Progress(_disposeProgress, "Done flattening.");

  //  printf("Lines read %d from %d\n", offsetBeforeThisPass,imageParameters.imageLength);

  for (i = 0; i < counterImageFiles; i ++) {
    free(imageDataBuffers[i]);
    TIFFClose(tiffFileHandles[i]);
  }

  TIFFClose(tiffFile);

  if (removeOriginals) {
    for (i = 0; i < counterImageFiles; i ++) {
      remove(fullPathImages[i].name);
    }
  }

  rename(tmpFullPath.name, outputFileName->name);

  free(tiffFileHandles);

  free(imageDataBuffers);
  free(resultBuffer);

  return 0;

}


#ifdef __Win__
//void InsertFileName( fullPath *fp, char *fname ){
// char *c = strrchr((char*)(fp->name), PATH_SEP);
// if(c != NULL) c++;
// else c = fp->name;
// strcpy( c, fname );
//}   
#endif


static int Create_LP_ivr(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Create_LP_ivr this function is not implemented yet\n");
  exit(1);
}

static int Unknown01(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Unknown01 this function is not implemented yet\n");
  exit(1);
}

static int Unknown02(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Unknown02 this function is not implemented yet\n");
  exit(1);
}

static int Unknown03(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Unknown03 this function is not implemented yet\n");
  exit(1);
}

static int Unknown04(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Unknown04 this function is not implemented yet\n");
  exit(1);
}

static int Unknown05(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Unknown05 this function is not implemented yet\n");
  exit(1);
}

static int Create_QTVR(Image *image, fullPath* fullPathImage)
{
  fprintf(stderr,"Create QTVR is not implemented yet\n");
  exit(1);
}

static void ARGtoRGBAImage(Image *im)
{
  int right;    
  int left;
  int bottom;
  int top;
  int width;
  int i;


  if ( im->selection.bottom == 0  &&
       im->selection.right == 0 ) {

    top = 0;
    left = 0;
    bottom = im->height;
    right = im->width;


  }  else {

    top = im->selection.top;
    bottom = im->selection.bottom;
    left = im->selection.left;
    right = im->selection.right;
  }

  width = right - left;

  //fprintf(stderr, "\nWidth %10d Top: %10d bottom %10d Right %10d Left %10d-------", width, top, bottom, right, left);

  assert(width >= 0);
  assert(bottom >= top);

  for (i = 0;  i < bottom - top ; i ++) {

    ARGBtoRGBA(*(im->data) + i * im->bytesPerLine, width, im->bitsPerPixel);

  } // for 

}



void Clear_Area_Outside_Selected_Region(Image *image)
{
  // This function clears (i.e. sets to zero) the area outside the 
  // selection region 

  int right;    
  int left;
  int bottom;
  int top;
  //  int width;
  //  int var24;
  int bytesPerPixel;  // 32
  unsigned char *dataPtr;
  unsigned char *pixelPtr;

  int currentRow;
  int currentColumn;

  // Only works for 8/16 bit per channel (32/64 bits per pixel) images
  assert(image->bitsPerPixel == 32 || image->bitsPerPixel == 64);
  
  top = image->selection.top;
  bottom = image->selection.bottom;
  left = image->selection.left;
  right = image->selection.right;
  
  if ( bottom == 0 )
    bottom = image->height;
 
  if ( right == 0 )
    right = image->width;

  if ( image -> format == _fisheye_circ) {
    PrintError("Not implemented yet");
    exit(1);
  }

  if ( image->bitsPerPixel == 32 ) {
    bytesPerPixel  = 4;
  } else if (image->bitsPerPixel == 64) {
    bytesPerPixel  = 8;    
  } else {
    assert(0); // it should not reach here 
    exit(0);
  }
    
  // Clear the area at above the image
  dataPtr = *(image->data);

  for (currentRow =0; currentRow < top; currentRow++ ) {
    pixelPtr = dataPtr;
        
    for (currentColumn = 0;  currentColumn < image->width ;   currentColumn++) {
      assert(sizeof(int) == bytesPerPixel);
      memset(pixelPtr, 0, bytesPerPixel);
      pixelPtr+=bytesPerPixel;
    }
    
    dataPtr += image->bytesPerLine;
  }
      
  // Clear area below the picture
  dataPtr = bottom * image->bytesPerLine + *(image->data);
  
  for (currentRow=bottom    ;  currentRow < image->height ; currentRow++) {
    pixelPtr = dataPtr;
    for (currentColumn = 0; currentColumn < image->width ; currentColumn++) {
      memset(pixelPtr, 0, bytesPerPixel);
      pixelPtr += bytesPerPixel;
    }
    
    dataPtr += image->bytesPerLine;
    
  } //  for (    ;  %currentColumn < image->width ; currentColumn++,pixelPtr += bytesPerPixel) {
  
  
  /* Clear the area to the left of the picture */
  
  dataPtr = *(image->data);
  for (   currentRow = 0 ; currentRow < image->height;   currentRow++) {
    
    pixelPtr = dataPtr;      
    for (currentColumn = 0 ; currentColumn < left ; currentColumn++) {
      memset(pixelPtr, 0, bytesPerPixel);
      pixelPtr += bytesPerPixel;
    }
    
    dataPtr += image->bytesPerLine;
  }
  
  /* Clear the area to the right of the picture */
  
  dataPtr = *(image->data);
  
  for (currentRow = 0; currentRow < image->height; currentRow++ ) {
    
    pixelPtr = dataPtr + bytesPerPixel * right;
    
    for (currentColumn=right ;currentColumn < image->width; currentColumn++) {
      
      memset(pixelPtr, 0, bytesPerPixel);
      
      pixelPtr += bytesPerPixel;
      
    }
    
    dataPtr += image->bytesPerLine;
    
  }
  
  return;
      

#ifdef not_implemented_yet
  

  //  THIS IS the code for fisheye_circular 24 bits, but I don't understand it yet.

  pixelPtr = right;
  currentColumn = left;

  eax = left + right;


  var20 = (left + right)/2;
  var24 = (top + bottom) /2;
  assert(left >= right);
  temp = (left - right ) /2;
  tmpStr28 = temp * temp;
    

  dataPtr =  *(image->data);
    
  for (currentRow = 0 ; currentRow < image->height ; currentRow++) {
      
	 currentColumn = 0;
	 pixelPtr = dataPtr;
      
	 if ( currentColumn < image->width ) {
        
        
		temp = currentRow - var24;

		var36 = temp * temp;
        
		do { 
          
		  temp = currentColumn - var20;
		  temp = temp * temp + var36;
          
		  if ( %eax > tmpStr28 ) {
            
			 *pixelPtr = 0; //moves only 1 byte
		  }
          
		  currentColumn++;
		  pixelPtr += bytesPerPixel;
          
		} while ( currentColumn < image->width );
        
	 } //    if ( currentColumn < image->width ) {
      
      
	 dataPtr += image-> bytesPerLine;
      
      
  } //for ( ; currentRow < image->height ; ) {
    
  return;
} //if ( image->bitsPerPixel == $0x20 ) {
#endif
    


#ifdef not_implemented_yet

THIS IS the code for fisheye_circular 64 bits

    
var20 = (left + right) /2;
    
var24 = (top + bottom)/2;
    
currentColumn = (left - right )/2;
    
tmpStr28 = currentColumn * currentColumn;
    
dataPtr = *(image->data);
    
for (  currentRow = 0; ; currentRow < image->height ; ++currentRow) {
  //if ( currentRow >= image->height )
  //              return;
      
  currentColumn = 0;
  pixelPtr = dataPtr;
      
  if ( currentColumn < image->width ) {
        
	 var40 = (currentRow * var24) * (currentRow * var24);
        
	 do {
          
		eax = currentColumn * currentColumn + var40;
          
		if ( %eax > tmpStr28 ) {
		  *pixelPtr = 0; // Again CAREFUL, moves 8 bytes
		}
          
		currentColumn ++;
		pixelPtr = bytesPerPixel;
          
	 } while (currentColumn < image->width );
	 //if ( currentColumn < image->width )
	 //       
        
  } //if ( currentColumn < image->width ) {
      
  dataPtr +=image->bytesPerLine;
      
 } //for ( ; %currentRow < image->height ; ) 
#endif

return;

  
}


void Unknown09(Image *currentImagePtr)
{
  // NEEDED
  fprintf(stderr,"Unknown09 this function is not implemented yet\n");
  exit(1);
}


/**
 * This function computes the minimal rectangle needed to encompass
 * the region of the output image (TrPtr->dest) that will be populated with 
 * data from the input image (TrPtr->src) using the options specified
 * in aP.  The ROIRect is populated with the left/right/top/bottom values
 * that define this ROI within the output image
 */
void getROI( TrformStr *TrPtr, aPrefs *aP, PTRect *ROIRect )
{
  struct   MakeParams  mpinv;
  fDesc    invstack[15], finvD;
  int   color               = 0;

  int             x, y, x_jump;
  double             x_d, y_d;  // Cartesian Coordinates of point in source (i.e. input) image
  double           Dx, Dy;      // Coordinates of corresponding point in destination (i.e. output) image

  double             w2   = (double) TrPtr->dest->width  / 2.0 - 0.5;   //half destination image width
  double             h2   = (double) TrPtr->dest->height / 2.0 - 0.5;   //half destination image height
  double             sw2 = (double) TrPtr->src->width   / 2.0 - 0.5;   //half source image width
  double             sh2 = (double) TrPtr->src->height  / 2.0 - 0.5;   //half source image height

  //Set initial values for ROI to be adjusted during this function
  ROIRect->left         = TrPtr->dest->width;
  ROIRect->right        = 0;
  ROIRect->top          = TrPtr->dest->height; 
  ROIRect->bottom       = 0;

  //The "forward" transform (although not used here) allows us to map pixel
  //coordinates in the output image to their location in the source image.
  //SetMakeParams( stack, &mp, &(aP->im) , &(aP->pano), color );
  //fD.func = execute_stack; fD.param = stack;

  //The "inverse" transform allows us to map pixel coordinates in each source image
  //to their location in the output image.
  SetInvMakeParams( invstack, &mpinv, &(aP->im) , &(aP->pano), color );   
  finvD.func = execute_stack_new; 
  finvD.param = invstack;
  
  //iterate over edges of input image and compute left/right/top/bottom-most coordinate
  //in output image
  for (y = 0; y <= TrPtr->src->height; y += 1) {
      
	 x_jump = (y==0 || y==TrPtr->src->height) ? 1 : TrPtr->src->width;
        
	 for (x = 0; x <= TrPtr->src->width; x += x_jump) {
		//convert source coordinates to cartesian coordinates (i.e. origin at center of image)
		x_d = (double) x - sw2 ;
		y_d = (double) y - sh2 ;
        
		//Map the source image cartesian coordinate to the destination image cartesian coordinate
		finvD.func( x_d, y_d, &Dx, &Dy, finvD.param);
        
		//Convert destination cartesian coordinate back to destination "screen" coordinates (i.e. origin at top left of image)
		Dx += w2;
		Dy =  h2 + Dy ;
         
		if( (Dx < TrPtr->dest->width) && (Dy < TrPtr->dest->height) && (Dx >= 0) && (Dy >= 0) ) {
		  //Update ROI if pixel is valid (i.e. inside the final panorama region)
		  if ((int)Dx < ROIRect->left) ROIRect->left = (int)Dx;
		  if ((int)Dx > ROIRect->right) ROIRect->right = (int)Dx;
		  if ((int)Dy < ROIRect->top) ROIRect->top = (int)Dy;
		  if ((int)Dy > ROIRect->bottom) ROIRect->bottom = (int)Dy;
		}
	 }
  }

  //PrintError("ROI: %d,%d - %d, %d", ROIRect->left, ROIRect->top, ROIRect->right, ROIRect->bottom);
}

/**
 * Populates the CropInfo struct with data about cropping of 
 * the TIFF file specified by filename
 */
void getCropInformationFromTiff(TIFF *tif, CropInfo *c)
{
  float x_position, x_resolution, y_position, y_resolution;
   
  //these are the actual, physical dimensions of the TIFF file
  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &(c->cropped_width));
  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &(c->cropped_height));

  //If nothing is stored in these tags, then this must be an "uncropped" TIFF 
  //file, in which case, the "full size" width/height is the same as the 
  //"cropped" width and height
  if (TIFFGetField(tif, TIFFTAG_PIXAR_IMAGEFULLWIDTH, &(c->full_width))==0)   (c->full_width = c->cropped_width);
  if (TIFFGetField(tif, TIFFTAG_PIXAR_IMAGEFULLLENGTH, &(c->full_height))==0) (c->full_height = c->cropped_height);
   
  if (TIFFGetField(tif, TIFFTAG_XPOSITION, &x_position)==0) x_position = 0;
  if (TIFFGetField(tif, TIFFTAG_XRESOLUTION, &x_resolution) == 0) x_resolution = 0;
  if (TIFFGetField(tif, TIFFTAG_YPOSITION, &y_position)==0) y_position = 0;
  if (TIFFGetField(tif, TIFFTAG_YRESOLUTION, &y_resolution) == 0) y_resolution = 0;

  //offset in pixels of "cropped" image from top left corner of 
  //full image (rounded to nearest integer)
  c->x_offset = (uint32)((x_position * x_resolution) + 0.49);
  c->y_offset = (uint32)((y_position * y_resolution) + 0.49);
    
  //printf("%s: %dx%d  @ %d,%d", filename, c->cropped_width, c->cropped_height, c->x_offset, c->y_offset);
}

/**
 * Populates the CropInfo struct with data about cropping of 
 * the TIFF file specified by filename
 */
void getCropInformation(char *filename, CropInfo *c)
{
   
  TIFF *tif = TIFFOpen(filename, "r");
  if (tif == NULL) {
	 PrintError("getCropInformation: Could not open TIFF file");
  } else {
	 getCropInformationFromTiff(tif, c);
	 TIFFClose(tif);
  }
          
}
 
void setFullSizeImageParameters(pt_tiff_parms *imageParameters, CropInfo *crop_info)
{
  // Update the imageParameters so that the dimensions reflect the
  // the size of the full-sized output image, (recorded in the crop_info struct)
  imageParameters->imageLength    = crop_info->full_height;
  imageParameters->imageWidth     = crop_info->full_width;
  imageParameters->bytesPerLine   = imageParameters->imageWidth * (imageParameters->bitsPerPixel/8);
}
  

int CreatePanorama(fullPath ptrImageFileNames[], int counterImageFiles, fullPath *panoFileName, fullPath *scriptFileName)
{

  Image *currentImagePtr;
  aPrefs *prefs;
  int var01;
  int var00;
  int colourCorrection;
  int panoProjection;

  int lines;
  fullPath *fullPathImages;
  int  loopCounter;
  char tmpStr48[8];
  char var40[8];
  char *tempString;        // It looks like a char *temp;          
  char tmpStr28[512];
  char var16[512];
  VRPanoOptions defaultVRPanoOptions;

  char           tmpStr[64];  // string
  fullPath       currentFullPath;
  fullPath       panoName;          // according to documention: QTVR, PNG, PICT, TIFF, etc plus options...*/
  fullPath       tempScriptFile ;
  char           output_file_format[256];
  Image          resultPanorama;    //Output Image
  Image          image1;            //Input Image

  FILE            *regFile;
  char            *regScript;
  unsigned int    regLen;
  unsigned int    regWritten;

  unsigned int    bpp;              // Bits Per Pixel

  TIFF            *tiffFile;       //Output file...will be written during this function
  TrformStr       transform;       //structure holds pointers to input and output images and misc other info

  int ebx;
  
  int croppedOutput = 1, croppedWidth = 0, croppedHeight = 0;
  PTRect ROIRect;
  unsigned int outputScanlineNumber = 0;
  
  /* Variables */
  colourCorrection = 0; // can have values of 1 2 or 3
  var00 = 0;
  var01 = 0;

  //Copy script line for line into a new temporary file
  memcpy(&tempScriptFile , scriptFileName, sizeof(fullPath));
  makeTempPath(&tempScriptFile);
    
  //TIFFSetWarningHandler(tiffErrorHandler);
  //TIFFSetErrorHandler(tiffErrorHandler);
  
  
  if ((regFile = fopen(tempScriptFile.name, "w")) == NULL) {
    PrintError("Could not open temporary Scriptfile");
    goto mainError;
  }
  
  if ((regScript = LoadScript(scriptFileName)) == 0) {
    PrintError("Could not load ScriptFile");
    goto mainError;
  }

  regLen = strlen(regScript);
  
  // Write script to temp file
  regWritten = fwrite(regScript, 1, regLen, regFile);
  
  // Make sure script was written completely
  if (regWritten != strlen(regScript)) {
    PrintError("Could not write temporary script");
    goto mainError;
  }

  fclose(regFile);
  free(regScript);

  //Initialize members to zero
  SetImageDefaults(&image1);
  SetImageDefaults(&resultPanorama);

  //transform structure holds input and output images, and some miscellaneous other information
  transform.src = &image1;            // Input image
  transform.dest = &resultPanorama;   // Output image
  transform.mode = 8;                 // How to run transformation
  transform.success = 1;              // 1 success 0 failure

  //Allocate space to hold fully qualified names of input images
  if ((fullPathImages = malloc(counterImageFiles * 512)) ==  NULL) {
    PrintError("Not enough memory");
    goto mainError;
  }
  
  // This is the main processing loop...it iterates over each input image
  // and maps the pixels in these input images into the output image(s)
  for (loopCounter = 0; loopCounter < counterImageFiles; loopCounter++) {
    
    currentImagePtr = &image1;

    // Read the next adjust line (contains yaw, pitch, roll and other information)
    // for one input image from the script file
    if ((prefs = readAdjustLine(&tempScriptFile)) == 0) {
      PrintError("Could not read Scriptfile");
      goto mainError;
    }
    
    colourCorrection = prefs->sBuf.colcorrect; 
    // This is a strange value:
    // colourCorrection == (i & 3) + (i+1)*4;
    // where i is the number of the reference image
    
    assert(colourCorrection >=0 && colourCorrection < (counterImageFiles+1) *4 );
    if (prefs->pano.cP.radial != 0) {
      assert(0); // I really don't want to execute this code yet
      
      // correct_Prefs
      //...
      //  3 colors x (4 coeffic. for 3rd order polys + correction radius)
      //radial_params[3][5] double (OFFSET 6c4 1732
      //
      //  [0][0..4]  40 bytes      6c4  6cc   6d4   6dc 6e4
      //  [1][0..4]  40 (28)       6ec  6f4   6fc   704 70c
      //  [2][0..4]  40 (28)       714  71c   724   72c 734
      //                            3c
      // radial_params 
      //  3 * 5 * 8 = 120
      
      var00 = prefs->pano.cP.radial_params[0][2]; // what is this for, I have NO idea.
      var00++;
#ifdef asdfasdf
      /*
	I AM NOT TOTALLY SURE ABOUT THIS
	804a01d:   dd 82 d4 06 00 00       fldl   0x6d4(%edx)            // loads address into FL
	804a023:   d9 bd b2 eb ff ff       fnstcw 0xffffebb2(%ebp)           ;;;;;;;;;;;>>> -5198
	804a029:   66 8b 8d b2 eb ff ff    mov    0xffffebb2(%ebp),%cx           ;;;;;;;;;;;>>> -5198
	804a030:   66 81 c9 00 0c          or     $0xc00,%cx
	804a035:   66 89 8d b0 eb ff ff    mov    %cx,0xffffebb0(%ebp)           ;;;;;;;;;;;>>> -5200
	804a03c:   d9 ad b0 eb ff ff       fldcw  0xffffebb0(%ebp)           ;;;;;;;;;;;>>> -5200
	
	804a042:   db 9d 7c e7 ff ff       fistpl 0xffffe77c(%ebp)           ;;;;;;;;;;;>>> -6276 var00
	804a048:   d9 ad b2 eb ff ff       fldcw  0xffffebb2(%ebp)           ;;;;;;;;;;;>>> -5198
	804a04e:   ff 85 7c e7 ff ff       incl   0xffffe77c(%ebp)           ;;;;;;;;;;;>>> -6276 var00
      */
#endif
      
    } // begins 804a00e
	 
	 
    if (prefs->pano.cP.horizontal != 0) {
      assert(0); // I really don't want to see this code executed yet
      
      var01 = prefs->pano.cP.horizontal_params[0] ;// 0x75c //[3] 3 colours x horizontal shift value
      var01++;
      
#ifdef adsfasdf 
      /*
	804a063:   dd 80 5c 07 00 00       fldl   0x75c(%eax)               // loads address into FL
	804a069:   d9 bd b2 eb ff ff       fnstcw 0xffffebb2(%ebp)           ;;;;;;;;;;;>>> -5198
	804a06f:   66 8b 95 b2 eb ff ff    mov    0xffffebb2(%ebp),%dx           ;;;;;;;;;;;>>> -5198
	804a076:   66 81 ca 00 0c          or     $0xc00,%dx
	804a07b:   66 89 95 b0 eb ff ff    mov    %dx,0xffffebb0(%ebp)           ;;;;;;;;;;;>>> -5200
	804a082:   d9 ad b0 eb ff ff       fldcw  0xffffebb0(%ebp)           ;;;;;;;;;;;>>> -5200
	804a088:   db 9d 78 e7 ff ff       fistpl 0xffffe778(%ebp)           ;;;;;;;;;;;>>> -6280  var01
	804a08e:   d9 ad b2 eb ff ff       fldcw  0xffffebb2(%ebp)           ;;;;;;;;;;;>>> -5198
	804a094:   ff 85 78 e7 ff ff       incl   0xffffe778(%ebp)           ;;;;;;;;;;;>>> -6280  var01
      */
#endif
    }
    
    // Copy the current output file name to he fullPathImages[loopCounter]
    memcpy( &fullPathImages[loopCounter], &panoFileName, sizeof(fullPath));
    
    // Create temporary file where output data wil be written
    if (makeTempPath(&fullPathImages[loopCounter]) != 0) {
      PrintError("Could not make Tempfile");
      goto mainError;
    }
    
    // Populate currentFullPath.name with output file name
    GetFullPath(&fullPathImages[loopCounter], currentFullPath.name);
    
    // Open up output file for writing...data will be written in TIFF format
    if ((tiffFile = TIFFOpen(currentFullPath.name, "w")) == 0) {
      PrintError("Could not open %s for writing", currentFullPath.name);
      goto mainError;
    }
    
    // Projection format for final panorama
    panoProjection = prefs->pano.format;
    
    // Copy output pano name to panoName
    memcpy(&panoName,  &prefs->pano.name, sizeof(fullPath));
    //memcpy(&global5640, &prefs->sBuf, sizeof(stBuf));
    
    //panoName.name contains the n"XXX" value from the script "p" lines (e.g. n"TIFF_m" or n"QTVR w400 h300 c1")
    tempString = panoName.name;
    --tempString; /* nextWord does ++ before testing anything, this guarantess proper execution */
    nextWord(output_file_format, &tempString);
    
    //New for PTMender...Use "cropped" TIFFs as intermediate file format when creating
    //most flattened output images (e.g. PNG, JPG), as well as for layered
    //PSD formats.  By default, PTMender works similarly to PTStitcher for 
    //TIFF_m and TIFF_mask formats, using "full size" TIFFs as intermediate 
    //(and output) format, unless the user explicitly requests cropped
    //output by inlcluding "r:CROP" as part of the "p" line (e.g. n"TIFF_m r:CROP")
    //This behavior is retained to maintain compatibility with PTStitcher, although
    //it is clearly less efficient than producting cropped TIFFs as output.
    //
    //Using cropped TIFF as the intermediate format significantly speeds up 
    //processing, with larger panos showing more dramatic increases in speed.  
    //It should also mean that the creation of the "flattened" formats will 
    //be significantly more memory-friendly, as the masking steps and PSD 
    //assembly steps won't need to load images the size of the output file 
    //into memory at once.  Unless the PTMender is fed extremely large input 
    //images, all memory constraints should now be a thing of the past (MRDL - May 2006).
    
    if ( (strstr(output_file_format, "TIFF_") && strstr(panoName.name, "r:CROP")) || 
         strcmp(output_file_format, "PSD_nomask") == 0 ||
         strcmp(output_file_format, "PSD_mask")   == 0 ||
         strcmp(output_file_format, "PSD")        == 0 ||
         strcmp(output_file_format, "JPEG")       == 0 ||
         strcmp(output_file_format, "JPG")        == 0 ||
         strcmp(output_file_format, "TIFF")       == 0 ||
         strcmp(output_file_format, "TIF")        == 0 ||
         strcmp(output_file_format, "PNG")        == 0)
      croppedOutput = 1;
    else
      croppedOutput = 0;
      
    transform.interpolator = prefs->interpolator;
    transform.gamma = prefs->gamma;

    if(ptQuietFlag == 0) {
      sprintf(tmpStr, "Converting Image %d / %d", (loopCounter+1), counterImageFiles);
      Progress(_initProgress, tmpStr );
    }
 
    //Read input image into transform.src
    if (readImage(currentImagePtr, &ptrImageFileNames[loopCounter]) != 0) {
      PrintError("could not read image");
      goto mainError;
    }

    //This "masks" the input image so that some pixels are excluded from 
    //transformation routine during pixel remapping/interpolation 
    if (prefs->im.cP.cutFrame != 0) { // remove frame? 0 - no; 1 - yes
      if (CropImage(currentImagePtr, &(prefs->im.selection)) == 0) {
        prefs->im.selection.left   = 0;  
        prefs->im.selection.right  = 0; 
        prefs->im.selection.bottom = 0; 
        prefs->im.selection.top    = 0; 
      }
    }
    
    //setup width/height of input image
    prefs->im.width = image1.width;
    prefs->im.height = image1.height;

    if (ptQuietFlag == 0) {
      if (Progress(_setProgress, "5") == 0) {
        TIFFClose(tiffFile);
        remove(fullPathImages[loopCounter].name);
        return(-1);
      }
    }
    
    //Try to set reasonable values for output pano width and/or height if not 
    //specified as part of input (Do this only when processing first image in script)
    if (loopCounter == 0) {

      if (prefs->pano.width == 0) {
        // if the pano did not set the width, then try to set it
        if (prefs->im.hfov != 0.0) {
          prefs->pano.width = prefs->im.width * prefs->pano.hfov /prefs->im.hfov;
          prefs->pano.width /=10; // Round to multiple of 10
          prefs->pano.width *=10;
        }
      }

      if (prefs->pano.height == 0)
        prefs->pano.height = prefs->pano.width/2;

      resultPanorama.height = prefs->pano.height;
      resultPanorama.width = prefs->pano.width;

      if (resultPanorama.height == 0 || resultPanorama.width == 0) {
        PrintError("Please set Panorama width/height");
        goto mainError;
      }
    } //End attempt at setting reasonable values for pano width/height


    // Set output width/height for output file 
    if (croppedOutput) {
      getROI( &transform, prefs, &ROIRect);
      //Dimensions determine size of TIFF file
      croppedWidth = (ROIRect.right - ROIRect.left) + 1;
      croppedHeight = (ROIRect.bottom - ROIRect.top)+ 1;

      TIFFSetField(tiffFile, TIFFTAG_IMAGEWIDTH, croppedWidth);
      TIFFSetField(tiffFile, TIFFTAG_IMAGELENGTH, croppedHeight);
    } else {
      TIFFSetField(tiffFile, TIFFTAG_IMAGEWIDTH, resultPanorama.width);
      TIFFSetField(tiffFile, TIFFTAG_IMAGELENGTH, resultPanorama.height);
    }
    
    
    if (image1.bitsPerPixel == 64) {
      bpp = 16;
    } else if (image1.bitsPerPixel == 32) {
      bpp = 8;
    } else {
      PrintError("Image type not supported\n");
      goto mainError;
    }


    // Number of bits per pixel...generally 8 bits per channel (but can also do 16 bits)
    TIFFSetField(tiffFile, TIFFTAG_BITSPERSAMPLE, bpp);
    
    // We always use Photometric RGB (Indicates a RGB TIFF file with no ColorMap.)
    TIFFSetField(tiffFile, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB); // 0x106

    //Indicates how the components of each pixel are stored.  
    //1 (PLANARCONFIG_CONTIG) is the default and 
    //indicates that the data are stored in "Chunky format".
    //The component values for each pixel are stored contiguously.
    //The order of the components within the pixel is specified by
    //PhotometricInterpretation. For RGB data, the data is stored as
    //RGBRGBRGB...
    TIFFSetField(tiffFile, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);

    //Always use 4 samples per pixel (RGB + Alpha channel)
    TIFFSetField(tiffFile, TIFFTAG_SAMPLESPERPIXEL, 4);

    //Packbits compression was used by original PTStitcher and is retained
    //as the default...the option to use the more efficient LZW compression
    //is also provided
    if (strstr(prefs->pano.name, "c:LZW") != NULL) {
      TIFFSetField(tiffFile, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_LZW);
      TIFFSetField(tiffFile, TIFFTAG_PREDICTOR, 2);   //using predictor usually increases LZW compression ratio for RGB data
    } else if (strstr(prefs->pano.name, "c:NONE") != NULL) {
      TIFFSetField(tiffFile, TIFFTAG_COMPRESSION, (uint16_t)COMPRESSION_NONE);
    } else if (strstr(prefs->pano.name, "c:DEFLATE") != NULL) {
      TIFFSetField(tiffFile, TIFFTAG_COMPRESSION, COMPRESSION_DEFLATE);
    }
    else { // Default is PACKBITS
      TIFFSetField(tiffFile, TIFFTAG_COMPRESSION, COMPRESSION_PACKBITS);
    }


    //"1" indicates that The 0th row represents the visual top of the image, 
    //and the 0th column represents the visual left-hand side.
    TIFFSetField(tiffFile, TIFFTAG_ORIENTATION, 1);

    //TIFFTAG_ROWSPERSTRIP indicates the number of rows per "strip" of TIFF data.  The original PTStitcher
    //set this value to the panorama height whch meant that the entire image
    //was contained in one strip.  This is not only explicitly discouraged by the 
    //TIFF specification ("Use of a single strip is not recommended. Choose RowsPerStrip 
    //such that each strip is about 8K bytes, even if the data is not compressed, 
    //since it makes buffering simpler for readers. The 8K value is fairly 
    //arbitrary, but seems to work well."), but is also makes it impossible
    //for programs to read the output from Pano Tools to perform random 
    //access on the data which leads to unnecessarily inefficient approaches to 
    //manipulating these images).
    //
    //In practice, most panoramas generated these days (Feb 2006) contain more than 
    //2000 pixels per row (equal to 8KB mentioned above), so it is easiest to
    //hard-code this value to one, which also enables complete random access to 
    //the output files by subsequent blending/processing applications
    
    //PTStitcher code:
    //TIFFSetField(tiffFile, TIFFTAG_ROWSPERSTRIP, (croppedOutput ? croppedHeight : resultPanorama.height) );
    
    //New-and-improved PTMender code:
    TIFFSetField(tiffFile, TIFFTAG_ROWSPERSTRIP, 1);

    if (croppedOutput) {
      //If writing cropped output, these tags write the postion offset (from top left)
      //into TIFF metadata. 
      
      
      //The X offset in ResolutionUnits of the left side of the image, with 
      //respect to the left side of the page.
      TIFFSetField(tiffFile, TIFFTAG_XPOSITION, (float)(ROIRect.left) / 150.0 );
      
      //The Y offset in ResolutionUnits of the top of the image, with 
      //respect to the top of the page.
      TIFFSetField(tiffFile, TIFFTAG_YPOSITION, (float)(ROIRect.top) / 150.0 );

      //The number of pixels per ResolutionUnit in the ImageWidth
      TIFFSetField(tiffFile, TIFFTAG_XRESOLUTION, (float)150.0);
      
      //The number of pixels per ResolutionUnit in the ImageLength (height)
      TIFFSetField(tiffFile, TIFFTAG_YRESOLUTION, (float)150.0);
      
      //The size of the picture represented by an image.  Note: 2 = Inches.  This
      //is required so that the computation of pixel offset using XPOSITION/YPOSITION and
      //XRESOLUTION/YRESOLUTION is valid (See tag description for XPOSITION/YPOSITION).
      TIFFSetField(tiffFile, TIFFTAG_RESOLUTIONUNIT, (uint16_t)2);      
      
      // TIFFTAG_PIXAR_IMAGEFULLWIDTH and TIFFTAG_PIXAR_IMAGEFULLLENGTH
      // are set when an image has been cropped out of a larger image.  
      // They reflect the size of the original uncropped image.
      // The TIFFTAG_XPOSITION and TIFFTAG_YPOSITION can be used
      // to determine the position of the smaller image in the larger one.
      TIFFSetField(tiffFile, TIFFTAG_PIXAR_IMAGEFULLWIDTH, resultPanorama.width);
      TIFFSetField(tiffFile, TIFFTAG_PIXAR_IMAGEFULLLENGTH, resultPanorama.height);      
    }

    //The resultPanorama.selection determines which region of the output image
    //is iterated over during the main pixel-remapping processing logic.  Much
    //of the image will be empty (black space) for any given input image.  However,
    //if cropped output is selected, then only the region of interest (ROI) into
    //which this input image will be mapped is processed...this significantly
    //speeds up processing
    if (croppedOutput) {
      resultPanorama.selection.left     = ROIRect.left;
      resultPanorama.selection.right    = ROIRect.right +1;  // the right edge is actually the pixel NOT in the pano
      resultPanorama.selection.top      = ROIRect.top;      
    } else {
      resultPanorama.selection.left     = 0;
      resultPanorama.selection.right    = resultPanorama.width;    
      resultPanorama.selection.top      = 0;      
    }

    resultPanorama.bitsPerPixel = image1.bitsPerPixel ;
    resultPanorama.bytesPerLine = TIFFScanlineSize(tiffFile);
    
    //The output image is generated a few lines at a time to make efficient use
    //of limited memory...compute a reasonable number of lines to process (must
    //be at least 1, but no more than output height)
    lines = 500000 / resultPanorama.bytesPerLine;
    
    if (lines == 0)
      lines = 1;
    
    //Don't process more lines than are available
    if (lines > (croppedOutput ? croppedHeight : resultPanorama.height) )
      lines = (croppedOutput ? croppedHeight : resultPanorama.height);
    
    if ((resultPanorama.data  = (unsigned char**)mymalloc(lines * resultPanorama.bytesPerLine ) ) == NULL) {
      PrintError("Not enough memory for output panorama buffer");
      exit(0);
    }
	 //NB resultPanorama.selection.bottom is actually one pixel beyond last row with data.
    resultPanorama.selection.bottom   = resultPanorama.selection.top + lines;
    
    //    printf("bits per pixel %d\n", resultPanorama.bitsPerPixel);
    //    printf("cropped %d\n", croppedOutput);

    if (resultPanorama.bitsPerPixel != image1.bitsPerPixel) {
      PrintError("All source images must have the same number of bits per pixel.");
      exit(0);
    }

    //Copy all position related data (yaw, pitch, roll, etc) for input image to currentImagePtr
    CopyPosition(currentImagePtr, &(prefs->im));

    //image1.selection determines how much of the input image to be 
    //included during main pixel remapping logic
    image1.selection.top =  prefs->im.selection.top ;
    image1.selection.bottom = prefs->im.selection.bottom;
    image1.selection.left = prefs->im.selection.left;
    image1.selection.right = prefs->im.selection.right;

    CopyPosition(&resultPanorama, &(prefs->pano));

    //Set image data outside selection region to zeros

    Clear_Area_Outside_Selected_Region(currentImagePtr);

    //pano.width and height must be equal to the full canvas size (not the 
    //size of the cropped output image...if selected) in order for the pixel 
    //remapping logic to work correctly.
    prefs->pano.width = resultPanorama.width;
    prefs->pano.height = resultPanorama.height;

    //Iterate over the output image multiple lines at a time, remapping pixels
    //from the input image into the output image, and writing data to an
    //output TIFF file.  Finish iterating when we reach the bottom of the 
    //output image (or, in the case of a cropped file, the bottom of the 
    //output ROI).
    outputScanlineNumber = 0;
    while (resultPanorama.selection.top < (croppedOutput ? ROIRect.bottom +1 : resultPanorama.height) ) {
  
      // Call the main pixel remapping routine...all the interpolation happens here
      MakePano(&transform, prefs);
        
      if (transform.success == 0) { // Error 
        PrintError("Error converting image");
        goto mainError;
      }
      
      //Reverse byte order before writing out to TIFF file
      ARGtoRGBAImage(&resultPanorama);

      //Write calculated data rows to TIFF file one row (aka "scanline") at a time
      for (ebx = 0; ebx< resultPanorama.selection.bottom - resultPanorama.selection.top ; ebx++) {
        if (TIFFWriteScanline(tiffFile, *resultPanorama.data + (resultPanorama.bytesPerLine * ebx), outputScanlineNumber, 1) != 1) {
          PrintError("Unable to write to TIFF file\n");
          return -1;
        }
           
        outputScanlineNumber++;
      }
  
      if (ptQuietFlag == 0) {

	//Update progress bar
	if (croppedOutput)
	  sprintf(tmpStr, "%d", (int)( (resultPanorama.selection.bottom-ROIRect.top)*100  / croppedHeight));
	else
	  sprintf(tmpStr, "%d", (int)(resultPanorama.selection.bottom*100  / resultPanorama.height));
	
        if (Progress(_setProgress, tmpStr) == 0) {
          // Cancelled by the user
          TIFFClose(tiffFile);
          remove(tempScriptFile.name);
          remove(fullPathImages[loopCounter].name);
          return(-1);
        }
      }
      
      //specify the next batch of rows to be processed 
      resultPanorama.selection.top = resultPanorama.selection.bottom;
      resultPanorama.selection.bottom = resultPanorama.selection.top + lines;
   
      //Be careful at boundary...end of image
      if ( resultPanorama.selection.bottom > (croppedOutput ? ROIRect.bottom +1: resultPanorama.height) )
        resultPanorama.selection.bottom = (croppedOutput ? ROIRect.bottom +1: resultPanorama.height);
    }
    
    TIFFClose(tiffFile);
    
    if (image1.data != NULL) {
      myfree((void**)image1.data);
      image1.data = NULL;   
    } 
    
    if (prefs->td != NULL) {
      myfree((void**)prefs->td);
    }
    
    if (prefs->ts != NULL)  {
      myfree((void**)prefs->ts);
    }
    free(prefs);
    
    if (resultPanorama.data != NULL) {
      myfree((void**)resultPanorama.data);
      resultPanorama.data = NULL;
    }
    
  }

  if (!ptQuietFlag) 
    Progress(_disposeProgress, "");

  // This is the end of the pixel remapping for all input images.
  // At this point we should have a collection of TIFF files containing
  // the warped input images.  For TIFF_m format this is all we need.  For
  // other formats, we may need to do extra work (feathering, flattening, etc.)
  
  //----------------------------------------------------------------------
  
  remove(tempScriptFile.name);
  
  if (resultPanorama.data != NULL)
    myfree((void**)resultPanorama.data);

  if (image1.data != NULL)
    myfree((void**)image1.data);
  
  // These functions are to correct and/or brightness.  They are not required for 
  // panoramas that do not need any brightness adjustments.  Moreover, Dersch
  // was not fully satisfied with the quality of results obtained from
  // using these functions, and knew that they could be significantly
  // improved.  In general, I think it best to avoid using these features, 
  // and doing any color/brightness adjustments manually either before
  // or after stitching.  While these functions work OK for some images, some 
  // of the time, they can produce some obviously wrong results in some
  // circumstances...perhaps an area for future improvement, but probably not 
  // as important a feature (now that we have multi-resolution splining 
  // software like Enblend) as when Desrch first added these (MRDL).

  if (var00 != 0) {
    ColourBrightness(fullPathImages,fullPathImages, counterImageFiles, var00 -1, 1);
  }
  
  if (var01 != 0) { //
    ColourBrightness(fullPathImages, fullPathImages, counterImageFiles, var01 - 1, 2);
  } // 

  if (colourCorrection != 0) {
    ColourBrightness(fullPathImages, fullPathImages, counterImageFiles, (colourCorrection / 4) - 1, 0);
  }

  SetVRPanoOptionsDefaults(&defaultVRPanoOptions);

  /* Soo, at this point we have skipped the first word of the panorama:
	  # n"QTVR w400 h300 c1"           additional viewer options in a quoted string together with format
	  #              the following options are recognized:
	  #                  w(width) and h(height) of viewer window (only QTVR on Macs)
	  #                  c(codec: 0-JPEG, 1-Cinepak, 2-Sorenson) (only QTVR on Macs)
	  #                  q(codec quality):
	  #                     0-high,1-normal,2-low    QTVR on Macs
	  #                     0-100(highest)           on other jpeg-formats (PAN, IVR, IVR_java, VRML)
	  #                  g  progressive jpeg (0-no, 1-yes) (PAN, IVR, IVR_java, VRML)
	  #                     Optimized JPEG (0-on(default), 2-disabled), (3-progressive with optimized disabled)
	  #                  p  initial pan angle ( QTVR on Macs, VRML, IVR)
	  #                  v  field of view (QTVR, VRML, IVR)
	  #                  Many more options can be set by editing the viewer scripts
  */
  //int getVRPanoOptions( VRPanoOptions *v, char *line )

  getVRPanoOptions(&defaultVRPanoOptions, tempString);
  
  
  //If we are dealing with an output format that is not TIFF_m or PSD_nomask,
  //then we have to add "masks" to the images before finishing...
  if ( strcmp(output_file_format, "TIFF_m") != 0 && strcmp(output_file_format, "PSD_nomask") != 0 ) {
    // There is no point in adding stitching masks for just one image 
    if (counterImageFiles > 1) {
      if (AddStitchingMasks(fullPathImages, fullPathImages, counterImageFiles, prefs->sBuf.feather)!=0) {
        PrintError("Could not create stitching masks");
        goto mainError;
      }
    }
  }
  
  /************ OUTPUT FORMATS: Multiple TIFF ***************/
  // TIFF_m and TIFF_mask...just rename the intermediate files 
  // that we've already computed with numbers (e.g. img0000.tif, img0001.tif, etc.) 
  // and we are finished processing.
  if ( strcmp(output_file_format, "TIFF_m")==0 || strcmp(output_file_format, "TIFF_mask")==0 ) {

    for (loopCounter = 0; loopCounter < counterImageFiles; loopCounter ++ ) {

      strcpy(var16, panoFileName->name);
      sprintf(var40, "%04d", loopCounter);
      strcat(var16, var40);
      ReplaceExt(var16, ".tif");

      // This renames the currently morphed image to the new filename!
      rename(fullPathImages[loopCounter].name, var16);

    } // end of for loop
    free(fullPathImages);
    return(0);
  }

  /************ OUTPUT FORMATS: Layered PSD ***************/
  // Layered PSD is less simple...we need to assemble the existing
  // intermediate files into a layered photoshop document
  if ( strcmp(output_file_format, "PSD_nomask") == 0 || strcmp(output_file_format, "PSD_mask")==0 ) {
    ReplaceExt(panoFileName->name, ".psd");
    
    if (CreatePSD(fullPathImages,counterImageFiles, panoFileName) != 0) {
      PrintError("Error creating PSD file");
      return(-1);
    }
    
    for (loopCounter = 0; loopCounter < counterImageFiles; loopCounter++) {
      remove(fullPathImages[loopCounter].name);
    }
    
    free(fullPathImages);
    return(0);
  } 
  
  
  /************ OUTPUT FORMATS: Flattened files ***************/
  // All other formats require us to "flatten" the intermediate layers into
  // one final document...general approach is to flatten to a single TIFF file, 
  // and then convert this to the desired output file format (e.g. JPEG, PNG, etc.)
  if (FlattenTIFF(fullPathImages,counterImageFiles,&fullPathImages[0], TRUE) != 0) { 
    PrintError("Error while flattening TIFF-image");
	 goto mainError;
  }

  ReplaceExt(panoFileName->name, ".tif");
  rename(fullPathImages[0].name, panoFileName->name);

  free(fullPathImages);

  //Desired output format is TIFF...no further conversion needed
  if (strcmp(output_file_format, "TIFF") == 0 || strcmp(output_file_format, "TIF") == 0)
    return(0);


  //Read back in again so we can convert to final desired format
  if (readImage(&resultPanorama, panoFileName) != 0) {
    PrintError("Could not read result image %s", panoFileName->name);
    goto mainError;
  }

  remove(panoFileName->name);

  if (strcmp(output_file_format, "QTVR") == 0)
    return Create_QTVR(&resultPanorama, panoFileName);

  if (strcmp(output_file_format, "IVR_java") == 0) { 
    if (panoProjection == 1)
      return Unknown03(&resultPanorama, panoFileName);
    else
      return Unknown02(&resultPanorama, panoFileName);
  }

  if (strcmp(output_file_format, "VRML") == 0)
    return Unknown05(&resultPanorama, panoFileName);

  if (strncmp(output_file_format, "IVR", 3) == 0) {  // compare first 3 characters of it // end at 804ae10
    if (panoProjection == 1)
      return Unknown01(&resultPanorama, panoFileName);
    else
      return Create_LP_ivr(&resultPanorama, panoFileName);
  }

  if (strcmp(output_file_format, "PAN") == 0) {  // 
    return Unknown04(&resultPanorama, panoFileName);
  }  // 804ae10

  if (strcmp(output_file_format, "JPEG") == 0 || strcmp(output_file_format, "JPG") == 0) {
    if (!ptQuietFlag) {
      printf("Creating JPEG (quality %d jpegProgressive %d)\n", defaultVRPanoOptions.cquality, defaultVRPanoOptions.progressive);
    }
    ReplaceExt(panoFileName->name, ".jpg");
    return writeJPEG(&resultPanorama, panoFileName, defaultVRPanoOptions.cquality, defaultVRPanoOptions.progressive);
  }


  if (strcmp(output_file_format, "PSD") == 0) {
    ReplaceExt(panoFileName->name, ".PSD");
    return (writePSD(&resultPanorama, panoFileName));

  } 

  if (strcmp(output_file_format, "PNG") == 0) {
    ReplaceExt(panoFileName->name, ".PNG");
    return (writePNG(&resultPanorama, panoFileName));
  }
  
  PrintError("Panorama output format not supported: %s", output_file_format);


 mainError:
  return(-1);

}