File: psd.c

package info (click to toggle)
gimp 2.2.13-1etch4
  • links: PTS
  • area: main
  • in suites: etch
  • size: 94,832 kB
  • ctags: 47,113
  • sloc: ansic: 524,858; xml: 36,798; lisp: 9,870; sh: 9,409; makefile: 7,923; python: 2,674; perl: 2,589; yacc: 520; lex: 334
file content (2855 lines) | stat: -rw-r--r-- 69,834 bytes parent folder | download | duplicates (2)
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
/*
 * PSD Plugin version 3.0.14
 * This GIMP plug-in is designed to load Adobe Photoshop(tm) files (.PSD)
 *
 * Adam D. Moss <adam@gimp.org> <adam@foxbox.org>
 *
 *     If this plug-in fails to load a file which you think it should,
 *     please file a Bugzilla bug describing what seemed to go wrong,
 *     and anything you know about the image you tried to load.  Attach a
 *     problematic PSD file to the bug report.
 *
 *          Copyright (C) 1997-2004 Adam D. Moss
 *          Copyright (C) 1996      Torsten Martinsen
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*
 * Adobe and Adobe Photoshop are trademarks of Adobe Systems
 * Incorporated that may be registered in certain jurisdictions.
 */

/*
 * Revision history:
 *
 *  2004-02-12 / v3.0.14 / Adam D. Moss
 *       Fix a twisted utf8-obsessive bug diagnosed by
 *       Piotr Krysiuk <KrysiukP@prokom.pl>
 *
 *  2004-01-06 / v3.0.13 / Adam D. Moss
 *       Disable one of the PanoTools fixes by default, since it causes
 *       regressions in some ordinary PSD file loading.
 *
 *  2004-01-06 / v3.0.12 / Adam D. Moss
 *       Try to avoid 0-sized drawables (including channels) in all
 *       circumstances under GIMP 2.
 *
 *  2004-01-01 / v3.0.11 / Daniel Rogers <dsrogers@phaseveloctiy.org>
 *       GIMP crashes on 0x0 layers, so we skip them.
 *
 *  2003-11-27 / v3.0.10 / Adam D. Moss
 *       GIMP 1.3/2.0 needs its layer/channel names to be UTF8 or it
 *       fails wackily, so convert the strings from the PSD file to
 *       UTF8 instead of using them raw.
 *
 *  2003-10-05 / v3.0.9 / Morten Eriksen
 *       Fixed memory corruption bug: too little memory was allocated
 *       for the bitmap image buffer if (imgwidth % 8 != 0) for
 *       monocolor PSD images.
 *
 *  2003-08-31 / v3.0.8 / applied (modified) patch from Andy Wallis
 *       Fix for handling of layer masks. See bug #68538.
 *
 *  2003-06-16 / v3.0.7 / Adam D. Moss
 *       Avoid memory corruption when things get shot to hell in the
 *       image unpacking phase.  Major version bumped to distinguish
 *       GIMP 1.3 development thread.
 *
 *  2000-08-23 / v2.0.6 / Adam D. Moss
 *       Eliminate more debugging output (don't people have more
 *       substantial problems to report?  I'm poised at my keyboard).
 *
 *  1999-11-14 / v2.0.5 / Adam D. Moss
 *       Applied patch by Andy Hefner to load 1-bit images.
 *
 *  1999-08-13 / v2.0.4 / Adam D. Moss
 *       Allowed NULL layer names again, whee.  (Also fixed the time machine.)
 *
 *  1999-08-20 / v2.0.3 / Adam D. Moss
 *       Ensure that NULL name does not get passed to gimp_layer_new(),
 *       or it will fail to create the layer and cause problems down
 *       the line (only since April 1999).
 *
 *  1999-01-18 / v2.0.2 / Adam D. Moss
 *       Better guess at how PSD files store Guide position precision.
 *
 *  1999-01-10 / v2.0.1 / Adam D. Moss
 *       Greatly reduced memory requirements for layered image loading -
 *       we now do just-in-time channel unpacking.  Some little
 *       cleanups too.
 *
 *  1998-09-04 / v2.0.0 / Adam D. Moss
 *       Now recognises and loads the new Guides extensions written
 *       by Photoshop 4 and 5.
 *
 *  1998-07-31 / v1.9.9.9f / Adam D. Moss
 *       Use GIMP_OVERLAY_MODE if available.
 *
 *  1998-07-31 / v1.9.9.9e / Adam D. Moss
 *       Worked around some buggy PSD savers (suspect PS4 on Mac) - ugh.
 *       Fixed a bug when loading layer masks of certain dimensions.
 *
 *  1998-05-04 / v1.9.9.9b / Adam D. Moss
 *       Changed the Pascal-style string-reading stuff.  That fixed
 *       some file-padding problems.  Made all debugging output
 *       compile-time optional (please leave it enabled for now).
 *       Reduced memory requirements; still much room for improvement.
 *
 *  1998-04-28 / v1.9.9.9 / Adam D. Moss
 *       Fixed the correct channel interlacing of 'raw' flat images.
 *       Thanks to Christian Kirsch and Jay Cox for spotting this.
 *       Changed some of the I/O routines.
 *
 *  1998-04-26 / v1.9.9.8 / Adam D. Moss
 *       Implemented Aux-channels for layered files.  Got rid
 *       of <endian.h> nonsense.  Improved Layer Mask padding.
 *       Enforced num_layers/num_channels limit checks.
 *
 *  1998-04-23 / v1.9.9.5 / Adam D. Moss
 *       Got Layer Masks working, got Aux-channels working
 *       for unlayered files, fixed 'raw' channel loading, fixed
 *       some other mini-bugs, slightly better progress meters.
 *       Thanks to everyone who is helping with the testing!
 *
 *  1998-04-21 / v1.9.9.1 / Adam D. Moss
 *       A little cleanup.  Implemented Layer Masks but disabled
 *       them again - PS masks can be a different size to their
 *       owning layer, unlike those in GIMP.
 *
 *  1998-04-19 / v1.9.9.0 / Adam D. Moss
 *       Much happier now.
 *
 *  1997-03-13 / v1.9.0 / Adam D. Moss
 *       Layers, channels and masks, oh my.
 *       + Bugfixes & rearchitecturing.
 *
 *  1997-01-30 / v1.0.12 / Torsten Martinsen
 *       Flat PSD image loading.
 */

/*
 * TODO:
 *
 *      Crush 16bpp channels *
 *	CMYK -> RGB *
 *      * I don't think these should be done lossily -- wait for
 *        GIMP to be able to support them natively.
 *
 *      Read in the paths.
 *
 *      File saving (someone has an alpha plugin for this)
 */

/*
 * BUGS:
 *
 *      Sometimes creates a superfluous aux channel?  Harmless.
 */



/* *** USER DEFINES *** */

/* set to TRUE if you want debugging, FALSE otherwise */
#define PSD_DEBUG FALSE

/* the max number of channels that this plugin should let a layer have */
#define MAX_CHANNELS 30

/* set to TRUE to allow a fix for transparency in PSD files
   generated by PanoTools that unfortunately causes regressions
   in some other ordinary files saved by Photoshop. */
#define PANOTOOLS_FIX FALSE

/* *** END OF USER DEFINES *** */



#define IFDBG if (PSD_DEBUG)

#include "config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <string.h>

#include <glib.h>

#include <libgimp/gimp.h>

#include "libgimp/stdplugins-intl.h"


/* Local types etc
 */
typedef enum
{
  PSD_UNKNOWN_IMAGE,
  PSD_RGB_IMAGE,
  PSD_RGBA_IMAGE,
  PSD_GRAY_IMAGE,
  PSD_GRAYA_IMAGE,
  PSD_INDEXED_IMAGE,
  PSD_INDEXEDA_IMAGE,
  PSD_BITMAP_IMAGE
} psd_imagetype;


typedef struct PsdChannel
{
  gchar *name;
  guchar *data;
  gint type;

  guint32 compressedsize;

  fpos_t fpos; /* Remember where the data is in the file, so we can
		  come back to it! */

 /* We can't just assume that the channel's width and height are the
  * same as those of the layer that owns the channel, since this
  * channel may be a layer mask, which Photoshop allows to have a
  * different size from the layer which it applies to.
  */
  guint32 width;
  guint32 height;

} PSDchannel;

typedef struct PsdGuide
{
  gboolean horizontal; /* else vertical */
  gint position;
} PSDguide;

typedef struct PsdLayer
{
  gint num_channels;
  PSDchannel *channel;

  gint32 x;
  gint32 y;
  guint32 width;
  guint32 height;

  gchar blendkey[4];
  guchar opacity;
  gchar clipping;

  gboolean protecttrans;
  gboolean visible;

  gchar* name;

  gint32 lm_x;
  gint32 lm_y;
  gint32 lm_width;
  gint32 lm_height;

} PSDlayer;


typedef gint32 Fixed;

typedef struct
{
  Fixed hRes;
  gint16 hRes_unit;
  gint16 widthUnit;

  Fixed vRes;
  gint16 vRes_unit;
  gint16 heightUnit;

/* Res_unit :
	1 == Pixels per inch
        2 == Pixels per cm
*/

} PSDresolution;

typedef struct PsdImage
{
  gint num_layers;
  PSDlayer *layer;

  gboolean absolute_alpha;

  gint type;

  gulong colmaplen;
  guchar *colmapdata;

  guint num_aux_channels;
  PSDchannel aux_channel[MAX_CHANNELS];

  guint num_guides;
  PSDguide *guides;

  gchar *caption;

  guint active_layer_num;

  guint resolution_is_set;
  PSDresolution resolution;
} PSDimage;

/* Declare some local functions.
 */
static void   query      (void);
static void   run        (const gchar      *name,
                          gint              nparams,
                          const GimpParam  *param,
                          gint             *nreturn_vals,
                          GimpParam       **return_vals);

static GimpImageType  psd_type_to_gimp_type      (psd_imagetype  psdtype);
static GimpImageBaseType     psd_type_to_gimp_base_type (psd_imagetype  psdtype);
static GimpLayerModeEffects     psd_lmode_to_gimp_lmode    (gchar          modekey[4]);
static GimpUnit       psd_unit_to_gimp_unit      (gint           psdunit);

static gint32         load_image                 (const gchar  *filename);



/* Various local variables...
 */
GimpPlugInInfo PLUG_IN_INFO =
{
  NULL,  /* init_proc  */
  NULL,  /* quit_proc  */
  query, /* query_proc */
  run,   /* run_proc   */
};


static PSDimage psd_image;


static struct
{
  gchar    signature[4];
  gushort  version;
  guchar   reserved[6];
  gushort  channels;
  gulong   rows;
  gulong   columns;
  gushort  bpp;
  gushort  mode;
  gulong   imgreslen;
  gulong   miscsizelen;
  gushort  compression;
  gushort *rowlength;
  long     imgdatalen;
} PSDheader;


static gchar * modename[] =
{
  "Bitmap",
  "Grayscale",
  "Indexed Colour",
  "RGB Colour",
  "CMYK Colour",
  "<invalid>",
  "<invalid>",
  "Multichannel",
  "Duotone",
  "Lab Colour",
  "<invalid>"
};


static const gchar *prog_name = "PSD";


static void unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen,
				  guint32 *offset);
static void decode(long clen, long uclen, guchar *src, guchar *dst, int step);
static void packbitsdecode(long *clenp, long uclen,
			   guchar *src, guchar *dst, int step);
static void cmyk2rgb(guchar *src, guchar *destp,
		     long width, long height, int alpha);
static void cmykp2rgb(guchar *src, guchar *destp,
		      long width, long height, int alpha);
static void bitmap2gray(guchar *src,guchar *dest,long w,long h);
static guchar getguchar(FILE *fd, gchar *why);
static gshort getgshort(FILE *fd, gchar *why);
static glong getglong(FILE *fd, gchar *why);
static void xfread(FILE *fd, void *buf, long len, gchar *why);
static void xfread_interlaced(FILE *fd, guchar *buf, long len, gchar *why,
			      gint step);
static void read_whole_file(FILE *fd);
static void reshuffle_cmap(guchar *map256);
static gchar* getpascalstring(FILE *fd, gchar *why);
static gchar* getstring(size_t n, FILE * fd, gchar *why);
static void throwchunk(size_t n, FILE * fd, gchar *why);
static void dumpchunk(size_t n, FILE * fd, gchar *why);
static void seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli);
static void validate_aux_channel_name(gint aux_index);


MAIN ()


static void
query (void)
{
  static GimpParamDef load_args[] =
  {
    { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
    { GIMP_PDB_STRING, "filename", "The name of the file to load" },
    { GIMP_PDB_STRING, "raw_filename", "The name of the file to load" }
  };
  static GimpParamDef load_return_vals[] =
  {
    { GIMP_PDB_IMAGE, "image", "Output image" }
  };

  gimp_install_procedure ("file_psd_load",
                          "loads files of the Photoshop(tm) PSD file format",
                          "This filter loads files of Adobe Photoshop(tm) native PSD format.  These files may be of any image type supported by GIMP, with or without layers, layer masks, aux channels and guides.",
                          "Adam D. Moss & Torsten Martinsen",
                          "Adam D. Moss & Torsten Martinsen",
                          "1996-1998",
                          "Photoshop image",
			  NULL,
                          GIMP_PLUGIN,
                          G_N_ELEMENTS (load_args),
                          G_N_ELEMENTS (load_return_vals),
                          load_args, load_return_vals);

  gimp_register_file_handler_mime ("file_psd_load", "image/x-psd");
  gimp_register_magic_load_handler ("file_psd_load",
				    "psd",
				    "",
				    "0,string,8BPS");
}


static void
run (const gchar      *name,
     gint              nparams,
     const GimpParam  *param,
     gint             *nreturn_vals,
     GimpParam       **return_vals)
{
  static GimpParam values[2];
  GimpRunMode run_mode;
  /*  GimpPDBStatusType status = GIMP_PDB_SUCCESS;*/
  gint32 image_ID;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals = values;
  values[0].type = GIMP_PDB_STATUS;
  values[0].data.d_status = GIMP_PDB_CALLING_ERROR;

  if (strcmp (name, "file_psd_load") == 0)
    {
      image_ID = load_image (param[1].data.d_string);

      if (image_ID != -1)
	{
	  *nreturn_vals = 2;
	  values[0].data.d_status = GIMP_PDB_SUCCESS;
	  values[1].type = GIMP_PDB_IMAGE;
	  values[1].data.d_image = image_ID;
	}
      else
	{
	  values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
	}
    }
}


static char *
sanitise_string(char *old_name)
{
  if (old_name) {
    char *rtn = gimp_any_to_utf8(old_name, -1,
				 _("Invalid UTF-8 string in PSD file"));
    g_free(old_name);
    return rtn;
  } else {
    return NULL;
  }
}

static GimpImageType
psd_type_to_gimp_type (psd_imagetype psdtype)
{
  switch(psdtype)
    {
    case PSD_RGBA_IMAGE: return(GIMP_RGBA_IMAGE);
    case PSD_RGB_IMAGE: return(GIMP_RGB_IMAGE);
    case PSD_GRAYA_IMAGE: return(GIMP_GRAYA_IMAGE);
    case PSD_GRAY_IMAGE: return(GIMP_GRAY_IMAGE);
    case PSD_INDEXEDA_IMAGE: return(GIMP_INDEXEDA_IMAGE);
    case PSD_INDEXED_IMAGE: return(GIMP_INDEXED_IMAGE);
    case PSD_BITMAP_IMAGE: return(GIMP_GRAY_IMAGE);
    default: return(GIMP_RGB_IMAGE);
    }
}

static GimpLayerModeEffects
psd_lmode_to_gimp_lmode (gchar modekey[4])
{
  if (strncmp(modekey, "norm", 4)==0) return(GIMP_NORMAL_MODE);
  if (strncmp(modekey, "dark", 4)==0) return(GIMP_DARKEN_ONLY_MODE);
  if (strncmp(modekey, "lite", 4)==0) return(GIMP_LIGHTEN_ONLY_MODE);
  if (strncmp(modekey, "hue ", 4)==0) return(GIMP_HUE_MODE);
  if (strncmp(modekey, "sat ", 4)==0) return(GIMP_SATURATION_MODE);
  if (strncmp(modekey, "colr", 4)==0) return(GIMP_COLOR_MODE);
  if (strncmp(modekey, "mul ", 4)==0) return(GIMP_MULTIPLY_MODE);
  if (strncmp(modekey, "scrn", 4)==0) return(GIMP_SCREEN_MODE);
  if (strncmp(modekey, "diss", 4)==0) return(GIMP_DISSOLVE_MODE);
  if (strncmp(modekey, "diff", 4)==0) return(GIMP_DIFFERENCE_MODE);
  if (strncmp(modekey, "lum ", 4)==0) return(GIMP_VALUE_MODE);
  if (strncmp(modekey, "hLit", 4)==0) return(GIMP_HARDLIGHT_MODE);
  if (strncmp(modekey, "sLit", 4)==0) return(GIMP_SOFTLIGHT_MODE);
  if (strncmp(modekey, "over", 4)==0) return(GIMP_OVERLAY_MODE);

  printf("PSD: Warning - UNKNOWN layer-blend mode, reverting to 'normal'\n");

  return(GIMP_NORMAL_MODE);
}

static GimpImageBaseType
psd_type_to_gimp_base_type (psd_imagetype psdtype)
{
  switch(psdtype)
    {
    case PSD_RGBA_IMAGE:
    case PSD_RGB_IMAGE: return(GIMP_RGB);
    case PSD_BITMAP_IMAGE:
    case PSD_GRAYA_IMAGE:
    case PSD_GRAY_IMAGE: return(GIMP_GRAY);
    case PSD_INDEXEDA_IMAGE:
    case PSD_INDEXED_IMAGE: return(GIMP_INDEXED);
    default:
      g_message ("Error: Can't convert PSD imagetype to GIMP imagetype");
      gimp_quit();
      return(GIMP_RGB);
    }
}

static GimpUnit
psd_unit_to_gimp_unit (int psdunit)
{
  switch (psdunit)
    {
    case 1:
      return GIMP_UNIT_INCH;
    case 2: /* this means cm to PS, but MM is as close as we have */
      return GIMP_UNIT_MM;
    case 3:
      return GIMP_UNIT_POINT;
    case 4:
      return GIMP_UNIT_PICA;
    case 5: /* 5 == Columns, but what the heck is a column? */
    default:
      IFDBG printf("Warning: unable to convert psd unit %d to gimp unit\n", psdunit);
      return GIMP_UNIT_PIXEL;
    }
}

static GimpImageBaseType
psd_mode_to_gimp_base_type (gushort psdtype)
{
  switch(psdtype)
    {
    case 1: return GIMP_GRAY;
    case 2: return GIMP_INDEXED;
    case 3: return GIMP_RGB;
    default:
      g_message ("Error: Can't convert PSD mode to GIMP base imagetype");
      gimp_quit();
      return GIMP_RGB;
    }
}

static void
reshuffle_cmap (guchar *map256)
{
  guchar *tmpmap;
  int i;

  tmpmap = g_malloc(3 * 256);

  for (i = 0; i < 256; i++)
    {
      tmpmap[i*3  ] = map256[i];
      tmpmap[i*3+1] = map256[i+256];
      tmpmap[i*3+2] = map256[i+512];
    }

  memcpy(map256, tmpmap, 3 * 256);
  g_free(tmpmap);
}

static void
dispatch_resID(guint ID, FILE *fd, guint32 *offset, guint32 Size)
{
  if ( (ID < 0x0bb6) && (ID >0x07d0) )
    {
      IFDBG printf ("\t\tPath data is irrelevant to GIMP at this time.\n");
      throwchunk(Size, fd, "dispatch_res path throw");
      (*offset) += Size;
    }
  else
    switch (ID)
      {
      case 0x03ee:
	{
	  gint32 remaining = Size;

	  IFDBG printf ("\t\tALPHA CHANNEL NAMES:\n");
	  if (Size > 0)
	    {
	      do
		{
		  guchar slen;

		  slen = getguchar (fd, "alpha channel name length");
		  (*offset)++;
		  remaining--;

		  /* Check for (Mac?) Photoshop (4?) file-writing bug */
		  if (slen > remaining)
		    {
		      IFDBG {printf("\nYay, a file bug.  "
				    "Yuck.  Photoshop 4/Mac?  "
				    "I'll work around you.\n");fflush(stdout);}
		      break;
		    }

		  if (slen)
		    {
		      guint32 alpha_name_len;
		      gchar* sname = getstring(slen, fd, "alpha channel name");

		      alpha_name_len = strlen(sname);

		      (*offset) += alpha_name_len;
		      remaining -= alpha_name_len;

		      sname = sanitise_string(sname);

		      psd_image.aux_channel[psd_image.num_aux_channels].name =
			sname;

		      IFDBG printf("\t\t\tname: \"%s\"\n",
				   psd_image.aux_channel[psd_image.num_aux_channels].name);
		    }
		  else
		    {
		      psd_image.aux_channel[psd_image.num_aux_channels].name =
			NULL;
		      IFDBG
			{printf("\t\t\tNull channel name %d.\n",
				psd_image.num_aux_channels);fflush(stdout);}
		    }

		  psd_image.num_aux_channels++;

		  if (psd_image.num_aux_channels > MAX_CHANNELS)
		    {
		      printf("\nPSD: Sorry - this image has too many "
			     "aux channels.  Tell Adam!\n");
		      gimp_quit();
		    }
		}
	      while (remaining > 0);
	    }

	  if (remaining)
	    {
	      IFDBG
		dumpchunk(remaining, fd, "alphaname padding 0 throw");
              else
		throwchunk(remaining, fd, "alphaname padding 0 throw");
	      (*offset) += remaining;
	      remaining = 0;
	    }
	}
	break;
      case 0x03ef:
	IFDBG printf("\t\tDISPLAYINFO STRUCTURE: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f0: /* FIXME: untested */
	{
	  psd_image.caption = getpascalstring(fd, "caption string");
	  (*offset)++;

	  if (psd_image.caption)
	    {
	      IFDBG printf("\t\t\tcontent: \"%s\"\n",psd_image.caption);
	      (*offset) += strlen(psd_image.caption);
	    }
	}
	break;
      case 0x03f2:
	IFDBG printf("\t\tBACKGROUND COLOR: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f4:
	IFDBG printf("\t\tGREY/MULTICHANNEL HALFTONING INFO: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f5:
	IFDBG printf("\t\tCOLOUR HALFTONING INFO: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f6:
	IFDBG printf("\t\tDUOTONE HALFTONING INFO: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f7:
	IFDBG printf("\t\tGREYSCALE/MULTICHANNEL TRANSFER FUNCTION: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f8:
	IFDBG printf("\t\tCOLOUR TRANSFER FUNCTION: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03f9:
	IFDBG printf("\t\tDUOTONE TRANSFER FUNCTION: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03fa:
	IFDBG printf("\t\tDUOTONE IMAGE INFO: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03fb:
	IFDBG printf("\t\tEFFECTIVE BLACK/WHITE VALUES: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x03fe:
	IFDBG printf("\t\tQUICK MASK INFO: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x0400:
	{
	  IFDBG printf("\t\tLAYER STATE INFO:\n");
	  psd_image.active_layer_num = getgshort(fd, "ID target_layer_num");

	  IFDBG printf("\t\t\ttarget: %d\n",(gint)psd_image.active_layer_num);
	  (*offset) += 2;
	}
	break;
      case 0x0402:
	IFDBG printf("\t\tLAYER GROUP INFO: unhandled\n");
	IFDBG printf("\t\t\t(Inferred number of layers: %d)\n",(gint)(Size/2));
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x0405:
	IFDBG printf ("\t\tIMAGE MODE FOR RAW FORMAT: unhandled\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;
      case 0x0408:
	{
	  gint32 remaining = Size;
	  int i;
	  IFDBG printf ("\t\tGUIDE INFORMATION:\n");

	  if (Size > 0)
	    {
	      gshort magic1, magic2, magic3, magic4, magic5, magic6;
	      glong num_guides;
	      PSDguide *guide;

	      magic1 = getgshort(fd, "guide"); (*offset) += 2;
	      magic2 = getgshort(fd, "guide"); (*offset) += 2;
	      magic3 = getgshort(fd, "guide"); (*offset) += 2;
	      magic4 = getgshort(fd, "guide"); (*offset) += 2;
	      magic5 = getgshort(fd, "guide"); (*offset) += 2;
	      magic6 = getgshort(fd, "guide"); (*offset) += 2;
	      remaining -= 12;

	      IFDBG printf("\t\t\tMagic: %d %d %d %d %d %d\n",
			   magic1, magic2, magic3, magic4, magic5, magic6);
	      IFDBG printf("\t\t\tMagic: 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x 0x%04x\n",
			   magic1, magic2, magic3, magic4, magic5, magic6);

	      num_guides = getglong(fd, "guide");
	      (*offset) += 4; remaining -= 4;

	      if (remaining != num_guides*5)
		{
		  IFDBG printf ("** FUNNY AMOUNT OF GUIDE DATA (%d)\n",
				remaining);
		  goto funny_amount_of_guide_data;
		}

	      IFDBG printf("\t\t\tNumber of guides is %ld\n", num_guides);
	      psd_image.num_guides = num_guides;
	      psd_image.guides = g_new(PSDguide, num_guides);
	      guide = psd_image.guides;

	      for (i = 0; i < num_guides; i++, guide++)
		{
		  guide->position = getglong(fd, "guide");

		  guide->horizontal = (1 == getguchar(fd, "guide"));
		  (*offset) += 5; remaining -= 5;

		  if (guide->horizontal)
		    {
		      guide->position =
			RINT((double)(guide->position * (magic4>>8))
			     /(double)(magic4&255));
		    }
		  else
		    {
		      guide->position =
			RINT((double)(guide->position * (magic6>>8))
			     /(double)(magic6&255));
		    }

		  IFDBG printf("\t\t\tGuide %d at %d, %s\n", i+1,
			       guide->position,
			       guide->horizontal ? "horizontal" : "vertical");
		}
	    }

	funny_amount_of_guide_data:

	  if (remaining)
	    {
	      IFDBG
		{
		  printf ("** GUIDE INFORMATION DROSS: ");
		  dumpchunk(remaining, fd, "dispatch_res");
		}
	      else
		{
		  throwchunk(remaining, fd, "dispatch_res");
		}

	      (*offset) += remaining;
	    }
	}
	break;
      case 0x03ed:
	{
	  IFDBG printf ("\t\tResolution Info:\n");
	  psd_image.resolution_is_set = 1;

	  psd_image.resolution.hRes = getglong(fd, "hRes");
	  psd_image.resolution.hRes_unit = getgshort(fd, "hRes_unit");
	  psd_image.resolution.widthUnit = getgshort(fd, "WidthUnit");
	  psd_image.resolution.vRes = getglong(fd, "vRes");
	  psd_image.resolution.vRes_unit = getgshort(fd, "vRes_unit");
	  psd_image.resolution.heightUnit = getgshort(fd, "HeightUnit");
	  (*offset) += Size;
	  IFDBG  printf("\t\t\tres = %f, %f\n",
			psd_image.resolution.hRes / 65536.0,
			psd_image.resolution.vRes / 65536.0);
	} break;

      case 0x03e9:
      case 0x03f1:
      case 0x03f3:
      case 0x03fd:
      case 0x0401:
      case 0x0404:
      case 0x0406:
      case 0x0bb7:
      case 0x2710:
	IFDBG printf ("\t\t<Field is irrelevant to GIMP at this time.>\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;

      case 0x03e8:
      case 0x03eb:
	IFDBG printf ("\t\t<Obsolete Photoshop 2.0 field.>\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;

      case 0x03fc:
      case 0x03ff:
      case 0x0403:
	IFDBG printf ("\t\t<Obsolete field.>\n");
	throwchunk(Size, fd, "dispatch_res");
	(*offset) += Size;
	break;

      default:
	IFDBG
	  {
	    printf ("\t\t<Undocumented field.>\n");
	    dumpchunk(Size, fd, "dispatch_res");
	  }
	else
	  throwchunk(Size, fd, "dispatch_res");

	(*offset) += Size;
	break;
      }
}

static void
do_layer_record(FILE *fd, guint32 *offset, gint layernum)
{
  PSDlayer *layer;
  gint32 top, left, bottom, right;
  guint32 extradatasize, layermaskdatasize, layerrangesdatasize;
  guint32 totaloff;
  gchar sig[4];
  guchar flags;
  gint i;

  IFDBG printf("\t\t\tLAYER RECORD (layer %d)\n", (int)layernum);

  layer = psd_image.layer + layernum;

  /* table 11-12 */
  top = getglong (fd, "layer top");
  (*offset)+=4;
  left = getglong (fd, "layer left");
  (*offset)+=4;
  bottom = getglong (fd, "layer bottom");
  (*offset)+=4;
  right = getglong (fd, "layer right");
  (*offset)+=4;

  layer->x = left;
  layer->y = top;
  layer->width = right - left;
  layer->height = bottom - top;

  IFDBG printf("\t\t\t\tLayer extents: (%d,%d) -> (%d,%d)\n",
	       left,top,right,bottom);

  layer->num_channels = getgshort (fd, "layer num_channels");
  (*offset)+=2;

  IFDBG printf("\t\t\t\tNumber of channels: %d\n",
	       (int)layer->num_channels);

  if (layer->num_channels)
    {
      layer->channel = g_new(PSDchannel, layer->num_channels);

      for (i = 0; i < layer->num_channels; i++)
	{
	  PSDchannel *channel = layer->channel + i;

	  /* table 11-13 */
	  IFDBG printf("\t\t\t\tCHANNEL LENGTH INFO (%d)\n", i);

	  channel->type = getgshort(fd, "channel id");
	  (*offset)+=2;
	  IFDBG printf("\t\t\t\t\tChannel TYPE: %d\n", channel->type);

	  channel->compressedsize = getglong(fd, "channeldatalength");
	  (*offset)+=4;
	  IFDBG printf("\t\t\t\t\tChannel Data Length: %d\n",
		       channel->compressedsize);
	}
    } else {
      IFDBG printf("\t\t\t\tOo-er, layer has no channels.  Hmm.\n");
    }

  xfread(fd, sig, 4, "layer blend sig");
  (*offset)+=4;

  if (strncmp(sig, "8BIM", 4)!=0)
    {
      IFDBG printf("\t\t\t(layer blend signature '%c%c%c%c' is incorrect: quitting)\n", sig[0], sig[1], sig[2], sig[3]);
      g_message ("Error: layer blend signature is incorrect. :-(");
      gimp_quit();
    }

  xfread(fd, layer->blendkey, 4, "layer blend key");
  (*offset)+=4;

  IFDBG printf("\t\t\t\tBlend type: PSD(\"%c%c%c%c\") = GIMP(%d)\n",
	       layer->blendkey[0],
	       layer->blendkey[1],
	       layer->blendkey[2],
	       layer->blendkey[3],
	       psd_lmode_to_gimp_lmode(layer->blendkey));

  layer->opacity = getguchar(fd, "layer opacity");
  (*offset)++;

  IFDBG printf("\t\t\t\tLayer Opacity: %d\n", layer->opacity);

  layer->clipping = getguchar(fd, "layer clipping");
  (*offset)++;

  IFDBG printf("\t\t\t\tLayer Clipping: %d (%s)\n",
	       layer->clipping,
	       layer->clipping == 0 ? "base" : "non-base");

  flags = getguchar(fd, "layer flags");
  (*offset)++;

  IFDBG printf("\t\t\t\tLayer Flags: %d (%s, %s)\n", flags,
	 flags&1?"preserve transparency":"don't preserve transparency",
	 flags&2?"visible":"not visible");

  layer->protecttrans = (flags & 1) ? TRUE : FALSE;
  layer->visible = (flags & 2) ? FALSE : TRUE;

  getguchar(fd, "layer record filler");
  (*offset)++;

  extradatasize = getglong(fd, "layer extra data size");
  (*offset)+=4;
  IFDBG printf("\t\t\t\tEXTRA DATA SIZE: %d\n",extradatasize);

  /* FIXME: should do something with this data */
  /*throwchunk(extradatasize, fd, "layer extradata throw");
  (*offset) += extradatasize;*/

  totaloff = (*offset) + extradatasize;

  /* table 11-14 */
  layermaskdatasize = getglong(fd, "layer mask data size");
  (*offset)+=4;
  IFDBG printf("\t\t\t\t\tLAYER MASK DATA SIZE: %d\n", layermaskdatasize);

  if (layermaskdatasize)
    {
      top    = getglong(fd, "lmask top");
      (*offset) += 4;
      left   = getglong(fd, "lmask left");
      (*offset) += 4;
      bottom = getglong(fd, "lmask bottom");
      (*offset) += 4;
      right  = getglong(fd, "lmask right");
      (*offset) += 4;

      layer->lm_x = left;
      layer->lm_y = top;
      layer->lm_width = right - left;
      layer->lm_height = bottom - top;

      getglong(fd, "lmask data throw");
      (*offset) += 4;

      /*      throwchunk(layermaskdatasize, fd, "layer mask data throw");
      (*offset) += layermaskdatasize;*/
    }

  layerrangesdatasize = getglong(fd, "layer ranges data size");
  (*offset)+=4;
  IFDBG printf("\t\t\t\t\t\tLAYER RANGES DATA SIZE: %d\n",layermaskdatasize);

  if (layerrangesdatasize)
    {
      throwchunk(layerrangesdatasize, fd, "layer ranges data throw");
      (*offset) += layerrangesdatasize;
    }

  layer->name = getpascalstring(fd, "layer name");
  (*offset)++;

  if (layer->name)
    {
      (*offset) += strlen(layer->name);
      IFDBG printf("\t\t\t\t\t\tLAYER NAME: '%s'\n", layer->name);
      layer->name = sanitise_string(layer->name);
    }
  else
    {
      IFDBG printf("\t\t\t\t\t\tNULL LAYER NAME\n");
    }
  /* If no layermask data - set offset and size from layer data */
  if (! layermaskdatasize)
    {
      IFDBG
        fprintf(stderr, "Setting layer mask data layer\n");

      psd_image.layer[layernum].lm_x = psd_image.layer[layernum].x;
      psd_image.layer[layernum].lm_y = psd_image.layer[layernum].y;
      psd_image.layer[layernum].lm_width =  psd_image.layer[layernum].width;
      psd_image.layer[layernum].lm_height = psd_image.layer[layernum].height;
    }

  if (totaloff-(*offset) > 0)
    {
      IFDBG
	{
	  printf("Warning: layer record dross: ");
	  dumpchunk(totaloff-(*offset), fd, "layer record dross throw");
	}
      else
	{
	  throwchunk(totaloff-(*offset), fd, "layer record dross throw");
	}
      (*offset) = totaloff;
    }
}

static void
do_layer_struct(FILE *fd, guint32 *offset)
{
  gint i;

  IFDBG printf("\t\tLAYER STRUCTURE SECTION\n");

  psd_image.num_layers = getgshort(fd, "layer struct numlayers");
  (*offset)+=2;

  IFDBG printf("\t\t\tCanonical number of layers: %d%s\n",
	 psd_image.num_layers>0?
	 (int)psd_image.num_layers:abs(psd_image.num_layers),
	 psd_image.num_layers>0?"":" (absolute/alpha)");

  if (psd_image.num_layers < 0)
    {
      psd_image.num_layers = -psd_image.num_layers;
      psd_image.absolute_alpha = TRUE;
    }
  else
    {
      psd_image.absolute_alpha = FALSE;
    }

  psd_image.layer = g_new(PSDlayer, psd_image.num_layers);

  for (i = 0; i < psd_image.num_layers; i++)
    {
      do_layer_record(fd, offset, i);
    }
}

static void
do_layer_pixeldata(FILE *fd, guint32 *offset)
{
  gint layeri, channeli;

  for (layeri = 0; layeri < psd_image.num_layers; layeri++)
    {
      PSDlayer *layer = psd_image.layer + layeri;

      for (channeli = 0; channeli < layer->num_channels; channeli++)
	{
	  PSDchannel *channel = layer->channel + channeli;

	  if (channel->type == -2)
	    {
	      channel->width = layer->lm_width;
	      channel->height = layer->lm_height;
	    }
	  else
	    {
	      channel->width = layer->width;
	      channel->height = layer->height;
	    }

	  fgetpos(fd, &channel->fpos);

	  throwchunk(channel->compressedsize, fd, "channel data skip");
	  (*offset) += channel->compressedsize;
	}
    }
}

static void
seek_to_and_unpack_pixeldata(FILE* fd, gint layeri, gint channeli)
{
  int width, height;
  guchar *tmpline;
  gint compression;
  guint32 offset = 0;
  PSDchannel *channel = &psd_image.layer[layeri].channel[channeli];

  fsetpos(fd, &channel->fpos);

  compression = getgshort(fd, "layer channel compression type");
  offset+=2;

  width = channel->width;
  height = channel->height;

  IFDBG
    {
      printf("\t\t\tLayer (%d) Channel (%d:%d) Compression: %d (%s)\n",
	     layeri,
	     channeli,
	     channel->type,
	     compression,
	     compression==0?"raw":(compression==1?"RLE":"*UNKNOWN!*"));

      fflush(stdout);
    }

  channel->data = g_malloc (width * height);

  tmpline = g_malloc(width + 1);

  switch (compression)
    {
    case 0: /* raw data */
      {
	gint linei;

	for (linei = 0; linei < height; linei++)
	  {
	    xfread(fd, channel->data + linei * width, width,
		   "raw channel line");
	    offset += width;
	  }

#if 0
	/* Pad raw data to multiple of 2? */
	if ((height * width) & 1)
	  {
	    getguchar(fd, "raw channel padding");
	    offset++;
	  }
#endif
      }
      break;
    case 1: /* RLE, one row at a time, padded to an even width */
      {
	gint linei;
	gint blockread;

	/* we throw this away because in theory we can trust the
	   data to unpack to the right length... hmm... */
	throwchunk(height * 2, fd, "widthlist");
	offset += height * 2;

	blockread = offset;

	/*IFDBG {printf("\nHere comes the guitar solo...\n");
	  fflush(stdout);}*/

	for (linei=0; linei<height; linei++)
	  {
	    /*printf(" %d ", *offset);*/
	    unpack_pb_channel(fd, tmpline,
			      width
			      /*+ (width&1)*/,
			      &offset);
	    memcpy(channel->data + linei * width, tmpline, width);
	  }

	IFDBG {printf("\t\t\t\t\tActual compressed size was %d bytes\n"
		      , offset-blockread);fflush(stdout);}
      }
      break;
    default: /* *unknown* */
      IFDBG {printf("\nEEP!\n");fflush(stdout);}
      g_message ("*** Unknown compression type in channel.");
      gimp_quit();
      break;
    }
  g_free(tmpline);
}

static void
do_layers(FILE *fd, guint32 *offset)
{
  guint32 section_length;

  section_length = getglong(fd, "layerinfo sectionlength");
  (*offset)+=4;

  IFDBG printf("\tLAYER INFO SECTION\n");
  IFDBG printf("\t\tSECTION LENGTH: %u\n",section_length);

  do_layer_struct(fd, offset);

  do_layer_pixeldata(fd, offset);
}

static void
do_layer_and_mask(FILE *fd)
{
  guint32 offset = 0;
  guint32 Size = PSDheader.miscsizelen;

  guint32 offset_now = ftell(fd);

  IFDBG printf("LAYER AND MASK INFO\n");
  IFDBG printf("\tSECTION LENGTH: %u\n",Size);

  if (Size == 0) return;

  do_layers(fd, &offset);

  IFDBG {printf("And...?\n");fflush(stdout);}

  if (offset < Size)
    {
      IFDBG
	{
	  printf("PSD: Supposedly there are %d bytes of mask info left.\n",
		 Size-offset);
	  if ((Size-offset == 4) || (Size-offset == 24))
	    printf("     That sounds good to me.\n");
	  else
	    printf("     That sounds strange to me.\n");
	}


      /*      if ((getguchar(fd, "mask info throw")!=0) ||
	  (getguchar(fd, "mask info throw")!=0) ||
	  (getguchar(fd, "mask info throw")!=0) ||
	  (getguchar(fd, "mask info throw")!=0))
	{
	  printf("*** This mask info block looks pretty bogus.\n");
	}*/
    }
  else
    printf("PSD: Stern warning - no mask info.\n");


  /* If 'offset' wasn't being buggily updated, we wouldn't need this. (!?) */
  fseek(fd, Size+offset_now, SEEK_SET);
}

static void
do_image_resources(FILE *fd)
{
  guint16 ID;
  gchar *Name;
  guint32 Size;

  guint32 offset = 0;

  IFDBG printf("IMAGE RESOURCE BLOCK:\n");

  psd_image.resolution_is_set = 0;

  /* FIXME: too trusting that the file isn't corrupt */
  while (offset < PSDheader.imgreslen-1)
    {
      gchar sig[4];

      xfread(fd, sig, 4, "imageresources signature");
      offset += 4;


      /* generic information about a block ID */


      ID = getgshort(fd, "ID num");
      offset += 2;
      IFDBG printf("\tID: 0x%04x / ",ID);

      Name = getpascalstring(fd, "ID name");
      offset++;

      if (Name)
	{
	  IFDBG printf("\"%s\" ", Name);
	  offset += strlen(Name);

	  if (!(strlen(Name)&1))
	    {
	      throwchunk(1, fd, "ID name throw");
	      offset ++;
	    }
	  g_free(Name);
	}
      else
	{
	  throwchunk(1, fd, "ID name throw2");
	  offset++;
	}

      Size = getglong(fd, "ID Size");
      offset += 4;
      IFDBG printf("Size: %d\n", Size);

      if (strncmp(sig, "8BIM", 4) == 0)
	dispatch_resID(ID, fd, &offset, Size);
      else
	{
	  printf("PSD: Warning, unknown resource signature \"%.4s\" at or before offset %d ::: skipping\n", sig, offset - 8);
	  throwchunk(Size, fd, "Skipping Unknown Resource");
	  offset += Size;
	}

      if (Size&1)
	{
	  IFDBG printf("+1");
	  throwchunk(1, fd, "ID content throw");
	  offset ++;
	}
    }

  /*  if (offset != PSDheader.imgreslen)
    {
      printf("\tSucking imageres byte...\n");
      throwchunk(1, fd, "imageres suck");
      offset ++;
    }*/

}


#if PANOTOOLS_FIX
/* Convert RGB data to RGBA data */
static guchar*
RGB_to_RGBA (const guchar* rgb_data, gint numpix)
{
  guchar* rtn;
  int i,j;

  if (!rgb_data)
    {
      printf("NULL rgb data - eep!");
      return NULL;
    }

  rtn = g_malloc(numpix * 4);

  j = 0;
  for (i=0; i<numpix; i++)
    {
      rtn[j++] = rgb_data[i*3];
      rtn[j++] = rgb_data[i*3+1];
      rtn[j++] = rgb_data[i*3+2];
      rtn[j++] = 255;
    }

  return rtn;
}
#endif /* PANOTOOLS_FIX */


static guchar*
chans_to_GRAYA (const guchar* grey,
                const guchar* alpha,
                gint numpix)
{
  guchar* rtn;
  int i;

  if (!grey || !alpha)
    {
      printf("NULL channel - eep!");
      return NULL;
    }

  rtn = g_malloc(numpix * 2);

  for (i = 0; i < numpix; i++)
    {
      rtn[i*2  ] = grey[i];
      rtn[i*2+1] = alpha[i];
    }

  return rtn;
}

static guchar*
chans_to_RGB (const guchar* red,
              const guchar* green,
              const guchar* blue,
              gint numpix)
{
  guchar* rtn;
  int i;

  if (!red || !green || !blue)
    {
      printf("NULL channel - eep!");
      return NULL;
    }

  rtn = g_malloc(numpix * 3);

  for (i=0; i<numpix; i++)
    {
      rtn[i*3  ] = red[i];
      rtn[i*3+1] = green[i];
      rtn[i*3+2] = blue[i];
    }

  return rtn;
}

static guchar*
chans_to_RGBA (const guchar* red,
               const guchar* green,
               const guchar* blue,
               const guchar* alpha,
               gint numpix)
{
  guchar* rtn;
  int i;
  gboolean careful = FALSE;

  if (!red || !green || !blue || !alpha)
    {
      printf("chans_to_RGBA : NULL channel - eep!");
      careful = TRUE;
    }

  rtn = g_malloc(numpix * 4);

  if (!careful)
    {
      for (i=0; i<numpix; i++)
	{
	  rtn[i*4  ] = red[i];
	  rtn[i*4+1] = green[i];
	  rtn[i*4+2] = blue[i];
	  rtn[i*4+3] = alpha[i];
	}
    }
  else
    {
      for (i=0; i<numpix; i++)
	{
	  rtn[i*4  ] = (red==NULL) ? 0 : red[i];
	  rtn[i*4+1] = (green==NULL) ? 0 : green[i];
	  rtn[i*4+2] = (blue==NULL) ? 0 : blue[i];
	  rtn[i*4+3] = (alpha==NULL) ? 0 : alpha[i];
	}
    }

  return rtn;
}

static
gboolean psd_layer_has_alpha(PSDlayer* layer)
{
  int i;

  for (i = 0; i < layer->num_channels; i++)
    {
      if (layer->channel[i].type == -1)
	{
	  return TRUE;
	}
    }
  return FALSE;
}

static
void validate_aux_channel_name(gint aux_index)
{
  if (psd_image.aux_channel[aux_index].name == NULL)
    {
      if (aux_index == 0)
	psd_image.aux_channel[aux_index].name = g_strdup ("Aux channel");
      else
	psd_image.aux_channel[aux_index].name =
	  g_strdup_printf ("Aux channel #%d", aux_index);
    }
}

static
void extract_data_and_channels(guchar* src, gint gimpstep, gint psstep,
			       gint32 image_ID, GimpDrawable* drawable,
			       gint width, gint height)
{
  guchar* primary_data;
  guchar* aux_data;
  GimpPixelRgn pixel_rgn;

  IFDBG printf("Extracting primary channel data (%d channels)\n"
	 "\tand %d auxiliary channels.\n", gimpstep, psstep-gimpstep);

  /* gimp doesn't like 0 width/height drawables. */
  if ((width == 0) || (height == 0))
    {
      IFDBG printf("(bad channel dimensions -- skipping)");
      return;
    }

  primary_data = g_malloc(width * height * gimpstep);
  {
    int pix,chan;

    for (pix = 0; pix < width * height; pix++)
      {
	for (chan = 0; chan < gimpstep; chan++)
	  {
	    primary_data[pix * gimpstep + chan] = src[pix * psstep + chan];
	  }
      }
    gimp_pixel_rgn_init (&pixel_rgn, drawable,
			 0, 0, drawable->width, drawable->height,
			 TRUE, FALSE);
    gimp_pixel_rgn_set_rect (&pixel_rgn, primary_data,
			     0, 0, drawable->width, drawable->height);

    gimp_drawable_flush (drawable);
    gimp_drawable_detach (drawable);
  }
  g_free (primary_data);

  aux_data = g_malloc (width * height);
  {
    int pix, chan, aux_index;
    gint32 channel_ID;
    GimpDrawable* chdrawable;
    GimpRGB colour;

    gimp_rgb_set (&colour, 0.0, 0.0, 0.0);

    for (chan=gimpstep; chan<psstep; chan++)
      {
	for (pix = 0; pix < width * height; pix++)
	  {
	    aux_data [pix] = src [pix * psstep + chan];
	  }

	aux_index = chan - gimpstep;
	validate_aux_channel_name (aux_index);

	channel_ID = gimp_channel_new (image_ID,
                                       psd_image.aux_channel[aux_index].name,
                                       width, height,
                                       100.0, &colour);
	gimp_image_add_channel (image_ID, channel_ID, 0);
	gimp_drawable_set_visible (channel_ID, FALSE);

	chdrawable = gimp_drawable_get (channel_ID);

	gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
			     0, 0, chdrawable->width, chdrawable->height,
			     TRUE, FALSE);
	gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
				 0, 0, chdrawable->width, chdrawable->height);

	gimp_drawable_flush (chdrawable);
	gimp_drawable_detach (chdrawable);
      }
  }
  g_free(aux_data);

  IFDBG printf("Done with that.\n\n");
}

static void
extract_channels(guchar* src, gint num_wanted, gint psstep,
		 gint32 image_ID,
		 gint width, gint height)
{
  guchar* aux_data;
  GimpPixelRgn pixel_rgn;

  IFDBG printf("Extracting %d/%d auxiliary channels.\n", num_wanted, psstep);

  /* gimp doesn't like 0 width/height drawables. */
  if ((width == 0) || (height == 0))
    {
      IFDBG printf("(bad channel dimensions -- skipping)");
      return;
    }

  aux_data = g_malloc(width * height);
  {
    int pix, chan, aux_index;
    gint32 channel_ID;
    GimpDrawable* chdrawable;
    GimpRGB colour;

    gimp_rgb_set (&colour, 0.0, 0.0, 0.0);

    for (chan=psstep-num_wanted; chan<psstep; chan++)
      {
	for (pix = 0; pix < width * height; pix++)
	  {
	    aux_data [pix] = src [pix * psstep + chan];
	  }

	aux_index = chan - (psstep - num_wanted);
	validate_aux_channel_name (aux_index);

	channel_ID = gimp_channel_new (image_ID,
                                       psd_image.aux_channel[aux_index].name,
                                       width, height,
                                       100.0, &colour);
	gimp_image_add_channel (image_ID, channel_ID, 0);
	gimp_drawable_set_visible (channel_ID, FALSE);

	chdrawable = gimp_drawable_get (channel_ID);

	gimp_pixel_rgn_init (&pixel_rgn, chdrawable,
			     0, 0, chdrawable->width, chdrawable->height,
			     TRUE, FALSE);
	gimp_pixel_rgn_set_rect (&pixel_rgn, aux_data,
				 0, 0, chdrawable->width, chdrawable->height);

	gimp_drawable_flush (chdrawable);
	gimp_drawable_detach (chdrawable);
      }
  }
  g_free(aux_data);

  IFDBG printf("Done with that.\n\n");
}

static void
resize_mask(guchar* src, guchar* dest,
	    gint32 src_x, gint32 src_y,
	    gint32 src_w, gint32 src_h,
	    gint32 dest_w, gint32 dest_h)
{
  int x,y;

  IFDBG printf("--> %p %p : %d %d . %d %d . %d %d\n",
	 src, dest,
	 src_x, src_y,
	 src_w, src_h,
	 dest_w, dest_h);

  for (y=0; y<dest_h; y++)
    {
      for (x=0; x<dest_w; x++)
	{
	  /* Avoid a 1-pixel border top-left */
	  if ((x>=src_x) && (x<src_x+src_w) &&
	      (y>=src_y) && (y<src_y+src_h))
	    {
	      dest[dest_w * y + x] =
		src[src_w * (y-src_y) + (x-src_x)];
	    }
	  else
	    {
	      dest[dest_w * y + x] = 255;
	    }
	}
    }
}

static gint32
load_image (const gchar *name)
{
  FILE *fd;
  gboolean want_aux;
  char *name_buf;
  guchar *cmykbuf;
  guchar *dest = NULL, *temp;
  long channels, nguchars;
  psd_imagetype imagetype;
  gboolean cmyk = FALSE;
  gint step = 1;
  gint32 image_ID = -1;
  gint32 layer_ID = -1;
  GimpDrawable *drawable = NULL;
  GimpPixelRgn pixel_rgn;
  gint32 iter;
  fpos_t tmpfpos;
  int red_chan, grn_chan, blu_chan, alpha_chan, ichan;

  IFDBG printf("------- %s ---------------------------------\n",name);

  fd = fopen (name, "rb");
  if (! fd)
    {
      g_message (_("Could not open '%s' for reading: %s"),
                 gimp_filename_to_utf8 (name), g_strerror (errno));
      return -1;
    }

  name_buf = g_strdup_printf (_("Opening '%s'..."),
                              gimp_filename_to_utf8 (name));
  gimp_progress_init (name_buf);
  g_free (name_buf);

  read_whole_file (fd);

  if (psd_image.num_layers > 0) /* PS3-style */
    {
      int lnum;
      GimpImageBaseType gimagetype;

      gimagetype = psd_mode_to_gimp_base_type(PSDheader.mode);
      image_ID =
	gimp_image_new (PSDheader.columns, PSDheader.rows, gimagetype);
      gimp_image_set_filename (image_ID, name);

      if (psd_image.resolution_is_set)
	{
	  if (psd_image.resolution.widthUnit == 2)  /* px/cm */
            {
              gdouble factor = gimp_unit_get_factor (GIMP_UNIT_MM) / 10.0;

              psd_image.resolution.hRes *= factor;
              psd_image.resolution.vRes *= factor;
            }

	  gimp_image_set_resolution (image_ID,
                                     psd_image.resolution.hRes / 65536.0,
                                     psd_image.resolution.vRes / 65536.0);

	  /* currently can only set one unit for the image so we use the
	     horizontal unit from the psd image */
	  gimp_image_set_unit (image_ID,
                               psd_unit_to_gimp_unit (psd_image.resolution.widthUnit));
	}

      fgetpos (fd, &tmpfpos);

      for (lnum = 0; lnum < psd_image.num_layers; lnum++)
	{
	  gint numc;
	  guchar* merged_data = NULL;
	  PSDlayer *layer = psd_image.layer + lnum;

	  /*
	   * since ps supports sloppy bounding boxes it is possible to
	   * have a 0x0 or Xx0 or 0xY layer.  Gimp doesn't support a
	   * 0x0 layer so we will just skip these.  We might be able
	   * to do something better here.
	   */
	  if ((layer->width == 0) || (layer->height == 0))
	    {
	      IFDBG printf("(bad layer dimensions -- skipping)");
	      continue;
	    }
	  numc = layer->num_channels;

	  IFDBG printf("Hey, it's a LAYER with %d channels!\n", numc);

	  switch (gimagetype)
	    {
	    case GIMP_GRAY:
	      {
		IFDBG printf("It's GRAY.\n");
		if (!psd_layer_has_alpha(layer))
		  {
		    merged_data = g_malloc(layer->width * layer->height);
		    seek_to_and_unpack_pixeldata(fd, lnum, 0);
		    memcpy(merged_data, layer->channel[0].data,
			   layer->width * layer->height);

		    g_free(layer->channel[0].data);
		  }
		else
		  {
		    seek_to_and_unpack_pixeldata(fd, lnum, 0);
		    seek_to_and_unpack_pixeldata(fd, lnum, 1);
		    merged_data =
		      chans_to_GRAYA (layer->channel[1].data,
				      layer->channel[0].data,
				      layer->width * layer->height);

		    g_free(layer->channel[0].data);
		    g_free(layer->channel[1].data);
		  }

		layer_ID = gimp_layer_new (image_ID,
					   layer->name,
					   layer->width,
					   layer->height,
					   (numc==1) ? GIMP_GRAY_IMAGE : GIMP_GRAYA_IMAGE,
					   (100.0 * layer->opacity) / 255.0,
					   psd_lmode_to_gimp_lmode(layer->blendkey));

	      }; break; /* case GIMP_GRAY */

	    case GIMP_RGB:
	      {
		IFDBG printf("It's RGB, %dx%d.\n", layer->width,
			     layer->height);
		if (!psd_layer_has_alpha(layer))
		  {
		    seek_to_and_unpack_pixeldata(fd, lnum, 0);
		    seek_to_and_unpack_pixeldata(fd, lnum, 1);
		    seek_to_and_unpack_pixeldata(fd, lnum, 2);
		    merged_data =
		      chans_to_RGB (layer->channel[0].data,
				    layer->channel[1].data,
				    layer->channel[2].data,
				    layer->width *
				    layer->height);

		    g_free(layer->channel[0].data);
		    g_free(layer->channel[1].data);
		    g_free(layer->channel[2].data);

		    IFDBG
		      fprintf(stderr, "YAH0a\n");
		  }
		else
		  {
		    seek_to_and_unpack_pixeldata(fd, lnum, 0);
		    seek_to_and_unpack_pixeldata(fd, lnum, 1);
		    seek_to_and_unpack_pixeldata(fd, lnum, 2);
		    seek_to_and_unpack_pixeldata(fd, lnum, 3);

		    /* Fix for unexpected layer data order for files
                     * from PS files created by PanoTools. Rather
		     * than assuming an order, we find the actual order.
		     */

		    red_chan = grn_chan = blu_chan = alpha_chan = -1;

		    for (ichan=0; ichan<numc; ichan++)
                      {
                        switch(psd_image.layer[lnum].channel[ichan].type)
                          {
                          case 0:    red_chan = ichan; break;
                          case 1:    grn_chan = ichan; break;
                          case 2:    blu_chan = ichan; break;
                          case -1: alpha_chan = ichan; break;
		      }
		    }

		    if ((red_chan < 0) ||
                        (grn_chan < 0) ||
                        (blu_chan < 0) ||
                        (alpha_chan < 0))
                      {
                        g_message ("Error: Cannot identify required RGBA channels");
                        gimp_quit();
                        break;
                      }

		    merged_data =
		      chans_to_RGBA (psd_image.layer[lnum].channel[red_chan].data,
				     psd_image.layer[lnum].channel[grn_chan].data,
				     psd_image.layer[lnum].channel[blu_chan].data,
				     psd_image.layer[lnum].channel[alpha_chan].data,
				     psd_image.layer[lnum].width *
				     psd_image.layer[lnum].height);

		    g_free(layer->channel[0].data);
		    g_free(layer->channel[1].data);
		    g_free(layer->channel[2].data);
		    g_free(layer->channel[3].data);

		    IFDBG
		      fprintf(stderr, "YAH0b\n");
		  }

		IFDBG
		  fprintf(stderr, "YAH1\n");

		layer_ID = gimp_layer_new (image_ID,
					   psd_image.layer[lnum].name,
					   psd_image.layer[lnum].width,
					   psd_image.layer[lnum].height,
					   psd_layer_has_alpha(&psd_image.layer[lnum]) ?
                                           GIMP_RGBA_IMAGE : GIMP_RGB_IMAGE,
					   (100.0 * (double)psd_image.layer[lnum].opacity) / 255.0,
					   psd_lmode_to_gimp_lmode(psd_image.layer[lnum].blendkey));

		IFDBG
		  fprintf(stderr, "YAH2\n");

	      }; break; /* case GIMP_RGB */

	    default:
	      {
		g_message ("Error: Sorry, can't deal with a layered image of this type.\n");
		gimp_quit();
	      }; break; /* default */
	    }

	  gimp_image_add_layer (image_ID, layer_ID, 0);

	  IFDBG
	    fprintf(stderr, "YAH3\n");

	  /* Do a layer mask if it exists */
	  for (iter = 0; iter < layer->num_channels; iter++)
	    {
	      if (layer->channel[iter].type == -2) /* is mask */
		{
		  gint32 mask_id;
		  guchar* lm_data;

		  IFDBG
		    fprintf(stderr, "YAH3m\n");

		  lm_data = g_malloc(layer->width * layer->height);
		  {
#if PANOTOOLS_FIX
                    guchar *tmp;
#endif

		    seek_to_and_unpack_pixeldata(fd, lnum, iter);
		    /* PS layer masks can be a different size to
		       their owning layer, so we have to resize them. */
		    resize_mask(layer->channel[iter].data,
				lm_data,
				layer->lm_x - layer->x,
				layer->lm_y - layer->y,
				layer->lm_width, layer->lm_height,
				layer->width, layer->height);

		    /* won't be needing the original data any more */
		    g_free(layer->channel[iter].data);

		    /* give it to GIMP */
		    mask_id = gimp_layer_create_mask(layer_ID, 0);

#if PANOTOOLS_FIX
 		    /* Convert the layer RGB data (not the mask) to RGBA */
                    tmp = merged_data;
 		    merged_data = RGB_to_RGBA (tmp,
                                               psd_image.layer[lnum].width *
                                               psd_image.layer[lnum].height);
                    g_free (tmp);

 		    /* Add alpha - otherwise cannot add layer mask */
 		    gimp_layer_add_alpha (layer_ID);

#endif /* PANOTOOLS_FIX */
 		    /* Add layer mask */
		    gimp_layer_add_mask (layer_ID, mask_id);

		    drawable = gimp_drawable_get (mask_id);

		    gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
					 layer->width, layer->height,
					 TRUE, FALSE);
		    gimp_pixel_rgn_set_rect (&pixel_rgn, lm_data, 0, 0,
					     layer->width, layer->height);
		  }
		  g_free(lm_data);

		  gimp_drawable_flush (drawable);
		  gimp_drawable_detach (drawable);
		}
	    }

	  IFDBG
	    fprintf(stderr, "YAH4\n");

	  gimp_layer_translate (layer_ID, layer->x, layer->y);

	  gimp_layer_set_preserve_trans (layer_ID, layer->protecttrans);
	  gimp_drawable_set_visible (layer_ID, layer->visible);

	  drawable = gimp_drawable_get (layer_ID);

	  IFDBG
	    fprintf(stderr, "YAH5 - merged_data=%p, drawable=%p, drawdim=%dx%dx%d\n",
		    merged_data,
		    drawable,
		    drawable->width,
		    drawable->height,
		    drawable->bpp);

	  gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
			       layer->width, layer->height,
			       TRUE, FALSE);
	  gimp_pixel_rgn_set_rect (&pixel_rgn, merged_data, 0, 0,
				   layer->width, layer->height);

	  IFDBG
	    fprintf(stderr, "YAH6\n");

	  gimp_drawable_flush (drawable);
	  gimp_drawable_detach (drawable);
	  drawable = NULL;

	  g_free (merged_data);

	  gimp_progress_update ((double)(lnum+1.0) /
				(double)psd_image.num_layers);
	}
      fsetpos (fd, &tmpfpos);
    }

  if ((psd_image.num_aux_channels > 0) &&
      (psd_image.num_layers > 0))
    {
      want_aux = TRUE;
      IFDBG printf("::::::::::: WANT AUX :::::::::::::::::::::::::::::::::::::::\n");
    }
  else
    {
      want_aux = FALSE;
    }


  if (want_aux || (psd_image.num_layers==0)) /* Photoshop2-style: NO LAYERS. */
    {

      IFDBG printf("Image data %ld chars\n", PSDheader.imgdatalen);

      step = PSDheader.channels;

      imagetype = PSD_UNKNOWN_IMAGE;
     switch (PSDheader.mode)
	{
	case 0:		/* Bitmap */
	  imagetype = PSD_BITMAP_IMAGE;
	  break;
	case 1:		/* Grayscale */
	  imagetype = PSD_GRAY_IMAGE;
	  break;
	case 2:		/* Indexed Colour */
	  imagetype = PSD_INDEXED_IMAGE;
	  break;
	case 3:		/* RGB Colour */
	  imagetype = PSD_RGB_IMAGE;
	  break;
	case 4:		/* CMYK Colour */
	  cmyk = TRUE;
	  switch (PSDheader.channels)
	    {
	    case 4:
	      imagetype = PSD_RGB_IMAGE;
	      break;
	    case 5:
	      imagetype = PSD_RGBA_IMAGE;
	      break;
	    default:
	      printf("%s: cannot handle CMYK with more than 5 channels\n",
		     prog_name);
	      return(-1);
	      break;
	    }
	  break;
	case 7:		/* Multichannel (?) */
	case 8:		/* Duotone */
	case 9:		/* Lab Colour */
	default:
	  break;
	}


      if (imagetype == PSD_UNKNOWN_IMAGE)
	{
	  printf("%s: Image type %d (%s) is not supported in this data format\n",
		 prog_name, PSDheader.mode,
		 (PSDheader.mode > 10) ?
		 "<out of range>" : modename[PSDheader.mode]);

	  return(-1);
	}

      if ((PSDheader.bpp != 8) && (PSDheader.bpp != 1))
	{
	  printf("%s: The GIMP only supports 8-bit or 1-bit deep PSD images "
		 "at this time.\n",
		 prog_name);
	  return(-1);
	}

      IFDBG printf(
	     "psd:%d gimp:%d gimpbase:%d\n",
	     imagetype,
	     psd_type_to_gimp_type(imagetype),
	     psd_type_to_gimp_base_type(imagetype)
	     );


      if (!want_aux)
	{
	  /* gimp doesn't like 0 width/height drawables. */
	  if ((PSDheader.columns == 0) || (PSDheader.rows == 0))
	    {
	      IFDBG printf("(bad psd2-style image dimensions -- skipping)");
	      image_ID = -1;
	      goto finish_up;
	    }

	  image_ID = gimp_image_new (PSDheader.columns, PSDheader.rows,
				     psd_type_to_gimp_base_type(imagetype));
	  gimp_image_set_filename (image_ID, name);
	  if (psd_type_to_gimp_base_type(imagetype) == GIMP_INDEXED)
	    {
	      if ((psd_image.colmaplen%3)!=0)
		printf("PSD: Colourmap looks screwed! Aiee!\n");
	      if (psd_image.colmaplen==0)
		printf("PSD: Indexed image has no colourmap!\n");
	      if (psd_image.colmaplen!=768)
		printf("PSD: Warning: Indexed image is %ld!=256 colours.\n",
		       psd_image.colmaplen/3);
	      if (psd_image.colmaplen==768)
		{
		  reshuffle_cmap(psd_image.colmapdata);
		  gimp_image_set_colormap (image_ID,
                                           psd_image.colmapdata,
                                           256);
		}
	    }

	  layer_ID = gimp_layer_new (image_ID, _("Background"),
				     PSDheader.columns, PSDheader.rows,
				     psd_type_to_gimp_type(imagetype),
				     100, GIMP_NORMAL_MODE);

	  gimp_image_add_layer (image_ID, layer_ID, 0);
	  drawable = gimp_drawable_get (layer_ID);

	}


      if (want_aux)
	{
	  switch (PSDheader.mode)
	    {
	    case 1:		/* Grayscale */
	      channels = 1;
	      break;
	    case 2:		/* Indexed Colour */
	      channels = 1;
	      break;
	    case 3:		/* RGB Colour */
	      channels = 3;
	      break;
	    default:
	      printf("aux? Aieeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee!!!!!!!!!\n");
	      channels = 1;
	      break;
	    }

	  channels = PSDheader.channels - channels;

	  if (psd_image.absolute_alpha)
	    {
	      channels--;
	    }
	}
      else
	{
	  channels = gimp_drawable_bpp(drawable->drawable_id);
	}


      dest = g_malloc( step * PSDheader.columns * PSDheader.rows );

      if (PSDheader.compression == 1)
	{
	  nguchars = PSDheader.columns * PSDheader.rows;
	  temp = g_malloc (PSDheader.imgdatalen);
	  xfread (fd, temp, PSDheader.imgdatalen, "image data");
	  if (!cmyk)
	    {
	      gimp_progress_update ((double)1.00);

	      if(imagetype==PSD_BITMAP_IMAGE) /* convert bitmap to grayscale */
		{
		  guchar *monobuf;

		  monobuf =
                    g_malloc (((PSDheader.columns + 7) >> 3) * PSDheader.rows);

		  decode (PSDheader.imgdatalen,
                          nguchars >> 3, temp,monobuf, step);
		  bitmap2gray (monobuf, dest,
                               PSDheader.columns, PSDheader.rows);

		  g_free (monobuf);
		}
	      else
		{
		  decode(PSDheader.imgdatalen, nguchars, temp, dest, step);
		}
	    }
	  else
	    {
	      gimp_progress_update (1.0);

	      cmykbuf = g_malloc (step * nguchars);
	      decode (PSDheader.imgdatalen, nguchars, temp, cmykbuf, step);

	      cmyk2rgb (cmykbuf, dest, PSDheader.columns, PSDheader.rows,
                        step > 4);
	      g_free (cmykbuf);
	    }

	  g_free (temp);
	}
      else
	{
	  if (!cmyk)
	    {
	      gimp_progress_update ((double)1.00);

	      xfread_interlaced(fd, dest, PSDheader.imgdatalen,
				"raw image data", step);
	    }
	  else
	    {
	      gimp_progress_update ((double)1.00);

	      cmykbuf = g_malloc(PSDheader.imgdatalen);
	      xfread_interlaced(fd, cmykbuf, PSDheader.imgdatalen,
				"raw cmyk image data", step);

	      cmykp2rgb(cmykbuf, dest,
			PSDheader.columns, PSDheader.rows, step > 4);
	      g_free(cmykbuf);
	    }
	}


      if (want_aux) /* want_aux */
	{
	  extract_channels(dest, channels, step,
			   image_ID,
			   PSDheader.columns, PSDheader.rows);

	  goto finish_up; /* Haha!  Look!  A goto! */
	}
      else
	{
	  gimp_progress_update ((double)1.00);

	  if (channels == step) /* gimp bpp == psd bpp */
	    {

	      if (psd_type_to_gimp_type(imagetype)==GIMP_INDEXEDA_IMAGE)
		{
		  printf("@@@@ Didn't know that this could happen...\n");
		  for (iter=0; iter<drawable->width*drawable->height; iter++)
		    {
		      dest[iter*2+1] = 255;
		    }
		}

	      gimp_pixel_rgn_init (&pixel_rgn, drawable,
				   0, 0, drawable->width, drawable->height,
				   TRUE, FALSE);
	      gimp_pixel_rgn_set_rect (&pixel_rgn, dest,
				       0, 0, drawable->width, drawable->height);

	      gimp_drawable_flush (drawable);
	      gimp_drawable_detach (drawable);
	    }
	  else
	    {
	      IFDBG printf("Uhhh... uhm... extra channels... heavy...\n");

	      extract_data_and_channels(dest, channels, step,
					image_ID, drawable,
					drawable->width, drawable->height);
	    }
	}



    finish_up:

      g_free (dest);

      if (psd_image.colmaplen > 0)
	g_free(psd_image.colmapdata);
    }

  if (psd_image.num_guides > 0)
    {
      PSDguide *guide = psd_image.guides;
      int i;

      IFDBG printf("--- Adding %d Guides\n", psd_image.num_guides);

      for (i = 0; i < psd_image.num_guides; i++, guide++)
	{
	  if (guide->horizontal)
	    gimp_image_add_hguide (image_ID, guide->position);
	  else
	    gimp_image_add_vguide (image_ID, guide->position);
	}
    }

  gimp_displays_flush();

  IFDBG printf("--- %d layers : pos %ld : a-alph %d ---\n",
	 psd_image.num_layers, (long int)ftell(fd),
	 psd_image.absolute_alpha);

  return image_ID;
}

static void
decode(long clen, long uclen, guchar *src, guchar* dst, int step)
{
    gint i, j;
    gint32 l;
    gushort * w;

    l = clen;
    for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
      {
	l -= PSDheader.rowlength[i];
      }
    if (l)
      {
	g_warning("decode: %ld should be zero\n", (long)l);
      }

    w = PSDheader.rowlength;

    packbitsdecode(&clen, uclen, src, dst++, step);

    for (j = 0; j < step-1; ++j)
      {
	for (i = 0; i < PSDheader.rows; ++i)
	  {
	    src += *w++;
	  }
	packbitsdecode(&clen, uclen, src, dst++, step);
      }

    IFDBG printf("clen %ld\n", clen);
}

/*
 * Decode a PackBits data stream.
 */
static void
packbitsdecode(long * clenp, long uclen, guchar *src, guchar *dst, int step)
{
    gint n, b;
    gint32 clen = *clenp;

    while ((clen > 0) && (uclen > 0)) {
	n = (int) *src++;
	if (n >= 128)
	    n -= 256;
	if (n < 0) {		/* replicate next guchar -n+1 times */
	    clen -= 2;
	    if (n == -128)	/* nop */
		continue;
	    n = -n + 1;
	    uclen -= n;
	    for (b = *src++; n > 0; --n) {
		*dst = b;
		dst += step;
	    }
	} else {		/* copy next n+1 guchars literally */
	    for (b = ++n; b > 0; --b) {
		*dst = *src++;
		dst += step;
	    }
	    uclen -= n;
	    clen -= n+1;
	}
    }
    if (uclen > 0) {
	printf("%s: unexpected EOF while reading image data\n", prog_name);
	gimp_quit();
    }
    *clenp = clen;
}

/*
 * Decode a PackBits channel from file.
 */
static void
unpack_pb_channel(FILE *fd, guchar *dst, gint32 unpackedlen, guint32 *offset)
{
    int n, b;
    gint32 upremain = unpackedlen;

    while (upremain > 0)
      {
	n = (int) getguchar(fd, "packbits1");
	(*offset)++;

	if (n >= 128)
	  n -= 256;
	if (n < 0)
	  {		/* replicate next guchar -n+1 times */
	    if (n == -128)	/* nop */
	      continue;
	    n = -n + 1;
	    /*	    upremain -= n;*/

	    b = getguchar(fd, "packbits2");
	    (*offset)++;
	    for (; n > 0; --n)
	      {
		if (upremain >= 0) {
		  *dst = b;
		  dst ++;
		}
		upremain--;
	      }
	  }
	else
	  {		/* copy next n+1 guchars literally */
	    for (b = ++n; b > 0; --b)
	      {
		const guchar c = getguchar(fd, "packbits3");
		if (upremain >= 0) {
		  *dst = c;
		  dst ++;
		}
		(*offset)++;
		upremain--;
	      }
	    /*	    upremain -= n;*/
	  }
      }

    if (upremain < 0)
      {
	printf("*** Unpacking overshot destination (%d) buffer by %d bytes!\n",
	       unpackedlen,
	       -upremain);
      }
}

static void
cmyk2rgb(unsigned char * src, unsigned char * dst,
	 long width, long height, int alpha)
{
    int r, g, b, k;
    int i, j;


    for (i = 0; i < height; i++) {
	for (j = 0; j < width; j++) {
	    r = *src++;
	    g = *src++;
	    b = *src++;
	    k = *src++;

	    gimp_cmyk_to_rgb_int (&r, &g, &b, &k);

	    *dst++ = r;
	    *dst++ = g;
	    *dst++ = b;

	    if (alpha)
		*dst++ = *src++;
	}

	if ((i % 5) == 0)
          gimp_progress_update ((double)(2.0*(double)height)/
                                ((double)height+(double)i));
    }
}

/*
 * Decode planar CMYK(A) to RGB(A).
 */
static void
cmykp2rgb(unsigned char * src, unsigned char * dst,
	  long width, long height, int alpha)
{
    int r, g, b, k;
    int i, j;
    long n;
    guchar *rp, *gp, *bp, *kp, *ap;

    n = width * height;
    rp = src;
    gp = rp + n;
    bp = gp + n;
    kp = bp + n;
    ap = kp + n;

    for (i = 0; i < height; i++) {
	for (j = 0; j < width; j++) {
	    r = *rp++;
	    g = *gp++;
	    b = *bp++;
	    k = *kp++;

	    gimp_cmyk_to_rgb_int (&r, &g, &b, &k);

	    *dst++ = r;
	    *dst++ = g;
	    *dst++ = b;

	    if (alpha)
		*dst++ = *ap++;
	}
	if ((i % 5) == 0)
	  gimp_progress_update ((double)(2.0*(double)height)/
                                ((double)height+(double)i));
    }
}

static void
bitmap2gray(guchar *src,guchar *dest,long w,long h)
{
  int i,j;
  for(i=0;i<h;i++)
    {
      int mask=0x80;
      for(j=0;j<w;j++)
	{
	  *dest++=(*src&mask)?0:255;
	  mask>>=1;
	  if(!mask)
	    {
	      src++;
	      mask=0x80;
	    }
	}
      if(mask!=0x80) src++;
    }
}

static void
dumpchunk(size_t n, FILE * fd, gchar *why)
{
  guint32 i;

  printf("\n");

  for (i=0;i<n;i++)
    {
      printf("%02x ",(int)getguchar(fd, why));
    }

  printf("\n");
}

static void
throwchunk(size_t n, FILE * fd, gchar *why)
{
#if 0
  guchar *tmpchunk;

  if (n==0)
    {
      return;
    }

  tmpchunk = g_malloc(n);
  xfread(fd, tmpchunk, n, why);
  g_free(tmpchunk);
#else
  if (n==0)
    {
      return;
    }

  if (fseek(fd, n, SEEK_CUR) != 0)
    {
      printf("%s: unable to seek forward while reading '%s' chunk\n",
	     prog_name, why);
      gimp_quit();
    }
#endif
}

static gchar *
getstring(size_t n, FILE * fd, gchar *why)
{
  gchar *tmpchunk;

  tmpchunk = g_malloc(n + 1);
  xfread(fd, tmpchunk, n, why);
  tmpchunk[n] = 0;

  return tmpchunk;  /* caller should free memory */
}

static gchar *
getpascalstring(FILE *fd, gchar *why)
{
  guchar *tmpchunk;
  guchar len;

  xfread(fd, &len, 1, why);

  if (len==0)
    {
      return NULL;
    }

  tmpchunk = g_malloc(len+1);

  xfread(fd, tmpchunk, len, why);
  tmpchunk[len]=0;

  return (gchar *) tmpchunk; /* caller should free memory */
}

static guchar
getguchar(FILE *fd, gchar *why)
{
  gint tmp;

  tmp = fgetc(fd);

  if (tmp == EOF)
    {
      printf("%s: unexpected EOF while reading '%s' chunk\n",
	     prog_name, why);
      gimp_quit();
    }
  return tmp;
}

static gshort
getgshort(FILE *fd, gchar *why)
{
  guchar b1, b2;

  b1 = getguchar(fd, why);
  b2 = getguchar(fd, why);

  return (gshort) ((b1 * 256) + b2);
}

static glong
getglong(FILE *fd, gchar *why)
{
  guchar s1, s2, s3, s4;

  s1 = getguchar(fd, why);
  s2 = getguchar(fd, why);
  s3 = getguchar(fd, why);
  s4 = getguchar(fd, why);

  return (glong) ((s1*256*256*256) + (s2*256*256) + (s3*256) + s4);
}

static void
xfread(FILE * fd, void * buf, long len, gchar *why)
{
  if (fread(buf, len, 1, fd) == 0)
    {
      printf("%s: unexpected EOF while reading '%s' chunk\n",
	     prog_name, why);
      gimp_quit();
    }
}

static void
xfread_interlaced(FILE* fd, guchar* buf, long len, gchar *why, gint step)
{
  guchar* dest;
  gint pix, pos, bpplane;

  bpplane = len/step;

  if (len%step != 0)
    {
      printf("PSD: Stern warning: data size is not a factor of step size!\n");
    }

  for (pix=0; pix<step; pix++)
    {
      dest = buf + pix;

      for (pos=0; pos<bpplane; pos++)
	{
	  *dest = getguchar(fd, why);
	  dest += step;
	}
    }
}

static void
read_whole_file(FILE * fd)
{
    guint16 w;
    gint32 pos;
    gchar dummy[6];
    gint i;

    xfread(fd, &PSDheader.signature, 4, "signature");
    PSDheader.version = getgshort(fd, "version");
    xfread(fd, &dummy, 6, "reserved");
    PSDheader.channels = getgshort(fd, "channels");
    PSDheader.rows = getglong(fd, "rows");
    PSDheader.columns = getglong(fd, "columns");
    PSDheader.bpp = getgshort(fd, "depth");
    PSDheader.mode = getgshort(fd, "mode");


    psd_image.num_layers = 0;
    psd_image.type = PSDheader.mode;
    psd_image.colmaplen = 0;
    psd_image.num_aux_channels = 0;
    psd_image.num_guides = 0;


    psd_image.colmaplen = getglong(fd, "color data length");

    if (psd_image.colmaplen > 0)
      {
	psd_image.colmapdata = g_malloc(psd_image.colmaplen);
	xfread(fd, psd_image.colmapdata, psd_image.colmaplen, "colormap");
      }


    PSDheader.imgreslen = getglong(fd, "image resource length");
    if (PSDheader.imgreslen > 0)
      {
	do_image_resources(fd);
      }


    PSDheader.miscsizelen = getglong(fd, "misc size data length");
    if (PSDheader.miscsizelen > 0)
      {
	do_layer_and_mask(fd);
      }


    PSDheader.compression = getgshort(fd, "compression");
    IFDBG printf("<<compr:%d>>", (int)PSDheader.compression);
    if (PSDheader.compression == 1) /* RLE */
      {
	PSDheader.rowlength = g_malloc(PSDheader.rows *
				       PSDheader.channels * sizeof(gushort));
	for (i = 0; i < PSDheader.rows*PSDheader.channels; ++i)
	  PSDheader.rowlength[i] = getgshort(fd, "x");
      }
    pos = ftell(fd);
    fseek(fd, 0, SEEK_END);
    PSDheader.imgdatalen = ftell(fd)-pos;
    fseek(fd, pos, SEEK_SET);


    if (strncmp(PSDheader.signature, "8BPS", 4) != 0)
      {
	printf("%s: not an Adobe Photoshop PSD file\n", prog_name);
	gimp_quit();
      }
    if (PSDheader.version != 1)
      {
	printf("%s: bad version number '%d', not 1\n",
	       prog_name, PSDheader.version);
	gimp_quit();
      }
    w = PSDheader.mode;
    IFDBG printf("HEAD:\n"
	   "\tChannels %d\n\tRows %ld\n\tColumns %ld\n\tDepth %d\n\tMode %d (%s)\n"
	   "\tColour data %ld guchars\n",
	   PSDheader.channels, PSDheader.rows,
	   PSDheader.columns, PSDheader.bpp,
	   w, modename[w < 10 ? w : 10],
	   psd_image.colmaplen);
    /*    printf("\tImage resource length: %lu\n", PSDheader.imgreslen);*/
    IFDBG printf("\tLayer/Mask Data length: %lu\n", PSDheader.miscsizelen);
    w = PSDheader.compression;
    IFDBG printf("\tCompression %d (%s)\n", w, w ? "RLE" : "raw");
}