[go: up one dir, main page]

Menu

[r22]: / rpgraphicex.pas  Maximize  Restore  History

Download this file

8176 lines (7085 with data), 296.9 kB

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
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
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
unit rpgraphicex;
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the
// specific language governing rights and limitations under the License.
//
// The original code is GraphicColor.pas, released November 1, 1999.
//
// The initial developer of the original code is Dipl. Ing. Mike Lischke (Plei▀a, Germany, www.delphi-gems.com),
//
// Portions created by Dipl. Ing. Mike Lischke are Copyright
// (C) 1999-2003 Dipl. Ing. Mike Lischke. All Rights Reserved.
//----------------------------------------------------------------------------------------------------------------------
//
// GraphicEx -
// This unit is an addendum to Graphics.pas, in order to enable your application
// to import many common graphic files.
//
// See help file for a description of supported image types. Additionally, there is a resample routine
// (Stretch) based on code from Anders Melander (http://www.melander.dk/delphi/resampler/index.html)
// which has been optimized quite a lot to work faster and bug fixed.
//
// version - 9.9
//
// 03-SEP-2000 ml:
// EPS with alpha channel, workaround for TIFs with wrong alpha channel indication,
// workaround for bad packbits compressed (TIF) images
// 28-AUG-2000 ml:
// small bugfixes
// 27-AUG-2000 ml:
// changed all FreeMemory(P) calls back to ... if Assigned(P) then FreeMem(P); ...
// 24-AUG-2000 ml:
// small bug in LZ77 decoder removed
// 18-AUG-2000 ml:
// TIF deflate decoding scheme
// 15-AUG-2000 ml:
// workaround for TIF images without compression, but prediction scheme set (which is not really used in this case)
// 12-AUG-2000 ml:
// small changes
//
// For older history please look into the help file.
//
// Note: The library provides usually only load support for the listed image formats but will perhaps be enhanced
// in the future to save those types too. It can be compiled with Delphi 4 or newer versions.
//
//----------------------------------------------------------------------------------------------------------------------
interface
{$I rpgraphicconfiguration.inc}
{$I rpconf.inc}
uses
Windows, Classes, ExtCtrls, Graphics, SysUtils,
{$IFDEF VCLNOTATION}
Vcl.Imaging.jpeg,
{$ENDIF}
{$IFNDEF VCLNOTATION}
jpeg,
{$ENDIF}
rpgraphiccompression, rpgraphicstrings, rpgraphiccolor;
type
TCardinalArray = array of Cardinal;
TByteArray = array of Byte;
TFloatArray = array of Single;
TImageOptions = set of (
ioTiled, // image consists of tiles not strips (TIF)
ioBigEndian, // byte order in values >= words is reversed (TIF, RLA, SGI)
ioMinIsWhite, // minimum value in grayscale palette is white not black (TIF)
ioReversed, // bit order in bytes is reveresed (TIF)
ioUseGamma // gamma correction is used
);
// describes the compression used in the image file
TCompressionType = (
ctUnknown, // compression type is unknown
ctNone, // no compression at all
ctRLE, // run length encoding
ctPackedBits, // Macintosh packed bits
ctLZW, // Lempel-Zif-Welch
ctFax3, // CCITT T.4 (1d), also known as fax group 3
ctFaxRLE, // modified Huffman (CCITT T.4 derivative)
ctFax4, // CCITT T.6, also known as fax group 4
ctFaxRLEW, // CCITT T.4 with word alignment
ctLZ77, // Hufman inflate/deflate
ctJPEG, // TIF JPEG compression (new version)
ctOJPEG, // TIF JPEG compression (old version)
ctThunderscan, // TIF thunderscan compression
ctNext,
ctIT8CTPAD,
ctIT8LW,
ctIT8MP,
ctIT8BL,
ctPixarFilm,
ctPixarLog,
ctDCS,
ctJBIG,
ctPCDHuffmann // PhotoCD Hufman compression
);
// properties of a particular image which are set while loading an image or when
// they are explicitly requested via ReadImageProperties
PImageProperties = ^TImageProperties;
TImageProperties = record
Version: Cardinal; // TIF, PSP, GIF
Options: TImageOptions; // all images
Width, // all images
Height: Cardinal; // all images
ColorScheme: TColorScheme; // all images
BitsPerSample, // all Images
SamplesPerPixel, // all images
BitsPerPixel: Byte; // all images
Compression: TCompressionType; // all images
FileGamma: Single; // RLA, PNG
XResolution,
YResolution: Single; // given in dpi (TIF, PCX, PSP)
Interlaced, // GIF, PNG
HasAlpha: Boolean; // TIF, PNG
// informational data, used internally and/or by decoders
// TIF
FirstIFD,
PlanarConfig, // most of this data is needed in the JPG decoder
CurrentRow,
TileWidth,
TileLength,
BytesPerLine: Cardinal;
RowsPerStrip: TCardinalArray;
YCbCrSubSampling,
JPEGTables: TByteArray;
JPEGColorMode,
JPEGTablesMode: Cardinal;
CurrentStrip,
StripCount,
Predictor: Integer;
// PCD
Overview: Boolean; // true if image is an overview image
Rotate: Byte; // describes how the image is rotated (aka landscape vs. portrait image)
ImageCount: Word; // number of subimages if this is an overview image
// GIF
LocalColorTable: Boolean; // image uses an own color palette instead of the global one
// RLA
BottomUp: Boolean; // images is bottom to top
// PSD
Channels: Byte; // up to 24 channels per image
// PNG
FilterMode: Byte;
end;
// This is the general base class for all image types implemented in GraphicEx.
// It contains some generally used class/data.
TGraphicExGraphic = class(TBitmap)
private
FColorManager: TColorManager;
FImageProperties: TImageProperties;
FBasePosition: Cardinal; // stream start position
FStream: TStream; // used for local references of the stream the class is currently loading from
FProgressRect: TRect;
public
constructor Create; override;
destructor Destroy; override;
procedure Assign(Source: TPersistent); override;
function CanLoad(const FileName: AnsiString): Boolean; overload; virtual;
function CanLoad(Stream: TStream): Boolean; overload; virtual;
procedure LoadFromResourceName(Instance: THandle; const ResName: AnsiString);
procedure LoadFromResourceID(Instance: THandle; ResID: Integer);
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; virtual;
property ColorManager: TColorManager read FColorManager;
property ImageProperties: TImageProperties read FImageProperties write FImageProperties;
end;
TGraphicExGraphicClass = class of TGraphicExGraphic;
{$ifdef SGIGraphic}
// *.bw, *.rgb, *.rgba, *.sgi images
TSGIGraphic = class(TGraphicExGraphic)
private
FRowStart,
FRowSize: TCardinalArray; // start and length of a line (if compressed)
FDecoder: TDecoder; // ...same applies here
procedure ReadAndDecode(Red, Green, Blue, Alpha: Pointer; Row, BPC: Cardinal);
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef AutodeskGraphic}
// *.cel, *.pic images
TAutodeskGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef TIFFGraphic}
// *.tif, *.tiff images
// one entry in a an IFD (image file directory)
TIFDEntry = packed record
Tag: Word;
DataType: Word;
DataLength: Cardinal;
Offset: Cardinal;
end;
TTIFFPalette = array[0..787] of Word;
TTIFFGraphic = class(TGraphicExGraphic)
private
FIFD: array of TIFDEntry; // the tags of one image file directory
FPalette: TTIFFPalette;
FYCbCrPositioning: Cardinal;
FYCbCrCoefficients: TFloatArray;
function FindTag(Tag: Cardinal; var Index: Cardinal): Boolean;
procedure GetValueList(Stream: TStream; Tag: Cardinal; var Values: TByteArray); overload;
procedure GetValueList(Stream: TStream; Tag: Cardinal; var Values: TCardinalArray); overload;
procedure GetValueList(Stream: TStream; Tag: Cardinal; var Values: TFloatArray); overload;
function GetValue(Stream: TStream; Tag: Cardinal; Default: Single = 0): Single; overload;
function GetValue(Tag: Cardinal; Default: Cardinal = 0): Cardinal; overload;
function GetValue(Tag: Cardinal; var Size: Cardinal; Default: Cardinal = 0): Cardinal; overload;
procedure SortIFD;
procedure SwapIFD;
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
procedure SaveToStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$ifdef EPSGraphic}
TEPSGraphic = class(TTIFFGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif} // EPSGraphic
{$endif} // TIFFGraphic
{$ifdef TargaGraphic}
// *.tga; *.vst; *.icb; *.vda; *.win images
TTargaGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
procedure SaveToStream(Stream: TStream); overload; override;
procedure SaveToStream(Stream: TStream; Compressed: Boolean); reintroduce; overload;
end;
{$endif}
{$ifdef PCXGraphic}
// *.pcx; *.pcc; *.scr images
// Note: Due to the badly designed format a PCX/SCR file cannot be part in a larger stream because the position of the
// color palette as well as the decoding size can only be determined by the size of the image.
// Hence the image must be the only one in the stream or the last one.
TPCXGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef PCDGraphic}
// *.pcd images
// Note: By default the BASE resolution of a PCD image is loaded with LoadFromStream.
TPCDGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef PortableMapGraphic}
// *.ppm, *.pgm, *.pbm images
TPPMGraphic = class(TGraphicExGraphic)
private
FBuffer: array[0..4095] of AnsiChar;
FIndex: Integer;
function CurrentChar: AnsiChar;
function GetChar: AnsiChar;
function GetNumber: Cardinal;
function ReadLine: AnsiString;
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef CUTGraphic}
// *.cut (+ *.pal) images
// Note: Also this format should not be used in a stream unless it is the only image or the last one!
TCUTGraphic = class(TGraphicExGraphic)
private
FPaletteFile: AnsiString;
protected
procedure LoadPalette;
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromFile(const FileName:String); override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
property PaletteFile: AnsiString read FPaletteFile write FPaletteFile;
end;
{$endif}
{$ifdef GIFGraphic}
// *.gif images
TGIFGraphic = class(TGraphicExGraphic)
private
function SkipExtensions: Byte;
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef RLAGraphic}
// *.rla, *.rpf images
// implementation based on code from Dipl. Ing. Ingo Neumann (ingo@upstart.de, ingo_n@dialup.nacamar.de)
TRLAGraphic = class(TGraphicExGraphic)
private
procedure SwapHeader(var Header); // start position of the image header in the stream
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef PhotoshopGraphic}
// *.psd, *.pdd images
TPSDGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef PaintshopProGraphic}
// *.psp images (file version 3 and 4)
TPSPGraphic = class(TGraphicExGraphic)
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
end;
{$endif}
{$ifdef PortableNetworkGraphic}
// *.png images
TChunkType = array[0..3] of AnsiChar;
// This header is followed by a variable number of data bytes, which are followed by the CRC for this data.
// The actual size of this data is given by field length in the chunk header.
// CRC is Cardinal (4 byte unsigned integer).
TPNGChunkHeader = packed record
Length: Cardinal; // size of data (entire chunk excluding itself, CRC and type)
ChunkType: TChunkType;
end;
TPNGGraphic = class(TGraphicExGraphic)
private
FDecoder: TLZ77Decoder;
FIDATSize: Integer; // remaining bytes in the current IDAT chunk
FRawBuffer, // buffer to load raw chunk data and to check CRC
FCurrentSource: Pointer; // points into FRawBuffer for current position of decoding
FHeader: TPNGChunkHeader; // header of the current chunk
FCurrentCRC: Cardinal; // running CRC for the current chunk
FSourceBPP: Integer; // bits per pixel used in the file
FPalette: HPALETTE; // used to hold the palette handle until we can set it finally after the pixel format
// has been set too (as this destroys the current palette)
FTransparency: TByteArray; // If the image is indexed then this array might contain alpha values (depends on file)
// each entry corresponding to the same palette index as the index in this array.
// For grayscale and RGB images FTransparentColor contains the (only) transparent
// color.
FTransparentColor: TColor; // transparent color for gray and RGB
FBackgroundColor: TColor; // index or color ref
procedure ApplyFilter(Filter: Byte; Line, PrevLine, Target: PByte; BPP, BytesPerRow: Integer);
function IsChunk(ChunkType: TChunkType): Boolean;
function LoadAndSwapHeader: Cardinal;
procedure LoadBackgroundColor(const Description);
procedure LoadIDAT(const Description);
procedure LoadTransparency(const Description);
procedure ReadDataAndCheckCRC;
procedure ReadRow(RowBuffer: Pointer; BytesPerRow: Integer);
function SetupColorDepth(ColorType, BitDepth: Integer): Integer;
public
function CanLoad(Stream: TStream): Boolean; override;
procedure LoadFromStream(Stream: TStream); override;
function ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean; override;
property BackgroundColor: TColor read FBackgroundColor;
property Transparency: TByteArray read FTransparency;
end;
{$endif} // PortableNetworkGraphic
// ---------- file format management stuff
TFormatType = (
ftAnimation, // format contains an animation (like GIF or AVI)
ftLayered, // format supports multiple layers (like PSP, PSD)
ftMultiImage, // format can contain more than one image (like TIF or GIF)
ftRaster, // format is contains raster data (this is mainly used)
ftVector // format contains vector data (like DXF or PSP file version 4)
);
TFormatTypes = set of TFormatType;
TFilterSortType = (
fstNone, // do not sort entries, list them as they are registered
fstBoth, // sort entries first by description then by extension
fstDescription, // sort entries by description only
fstExtension // sort entries by extension only
);
TFilterOption = (
foCompact, // use the compact form in filter strings instead listing each extension on a separate line
foIncludeAll, // include the 'All image files' filter AnsiString
foIncludeExtension // add the extension to the description
);
TFilterOptions = set of TFilterOption;
// The file format list is an alternative to Delphi's own poor implementation which does neither allow to filter
// graphic formats nor to build common entries in filter strings nor does it care for duplicate entries or
// alphabetic ordering. Additionally, some properties are maintained for each format to do searches, filter partiuclar
// formats for a certain case etc.
TFileFormatList = class
private
FClassList,
FExtensionList: TList;
protected
function FindExtension(const Extension: AnsiString): Integer;
function FindGraphicClass(GraphicClass: TGraphicClass): Integer;
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function GetDescription(Graphic: TGraphicClass): AnsiString;
procedure GetExtensionList(List: TStrings);
function GetGraphicFilter(Formats: TFormatTypes; SortType: TFilterSortType; Options: TFilterOptions;
GraphicClass: TGraphicClass): AnsiString;
function GraphicFromExtension(S: AnsiString): TGraphicClass;
function GraphicFromContent(const FileName: AnsiString): TGraphicExGraphicClass; overload;
function GraphicFromContent(Stream: TStream): TGraphicExGraphicClass; overload;
procedure RegisterFileFormat(const Extension, Common, Individual: AnsiString; FormatTypes: TFormatTypes;
Replace, RegisterDefault: Boolean; GraphicClass: TGraphicClass);
procedure UnregisterFileFormat(const Extension: AnsiString; GraphicClass: TGraphicClass);
end;
// resampling support types
TResamplingFilter = (sfBox, sfTriangle, sfHermite, sfBell, sfSpline, sfLanczos3, sfMitchell);
// Resampling support routines
procedure Stretch(NewWidth, NewHeight: Cardinal; Filter: TResamplingFilter; Radius: Single; Source, Target: TBitmap); overload;
procedure Stretch(NewWidth, NewHeight: Cardinal; Filter: TResamplingFilter; Radius: Single; Source: TBitmap); overload;
var
FileFormatList: TFileFormatList;
//----------------------------------------------------------------------------------------------------------------------
implementation
uses
Consts, Math, rpzlib77;
type
// resampling support types
TRGBInt = record
R, G, B: Integer;
end;
PRGBWord = ^TRGBWord;
TRGBWord = record
R, G, B: Word;
end;
PRGBAWord = ^TRGBAWord;
TRGBAWord = record
R, G, B, A: Word;
end;
PBGR = ^TBGR;
TBGR = packed record
B, G, R: Byte;
end;
PBGRA = ^TBGRA;
TBGRA = packed record
B, G, R, A: Byte;
end;
PRGB = ^TRGB;
TRGB = packed record
R, G, B: Byte;
end;
PRGBA = ^TRGBA;
TRGBA = packed record
R, G, B, A: Byte;
end;
PPixelArray = ^TPixelArray;
TPixelArray = array[0..0] of TBGR;
TFilterFunction = function(Value: Single): Single;
// contributor for a Pixel
PContributor = ^TContributor;
TContributor = record
Weight: Integer; // Pixel Weight
Pixel: Integer; // Source Pixel
end;
TContributors = array of TContributor;
// list of source pixels contributing to a destination pixel
TContributorEntry = record
N: Integer;
Contributors: TContributors;
end;
TContributorList = array of TContributorEntry;
const
DefaultFilterRadius: array[TResamplingFilter] of Single = (0.5, 1, 1, 1.5, 2, 3, 2);
threadvar // globally used cache for current image (speeds up resampling about 10%)
CurrentLineR: array of Integer;
CurrentLineG: array of Integer;
CurrentLineB: array of Integer;
//----------------------------------------------------------------------------------------------------------------------
procedure GraphicExError(ErrorString: AnsiString); overload;
begin
raise EInvalidGraphic.Create(ErrorString);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure GraphicExError(ErrorString: AnsiString; Args: array of const); overload;
begin
raise EInvalidGraphic.CreateFmt(ErrorString, Args);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure Upsample(Width, Height, ScaledWidth: Cardinal; Pixels: PAnsiChar);
// Creates a new image that is a integral size greater than an existing one.
var
X, Y: Cardinal;
P, Q, R: PAnsiChar;
begin
for Y := 0 to Height - 1 do
begin
P := Pixels + (Height - 1 - Y) * ScaledWidth + (Width - 1);
Q := Pixels + ((Height - 1 - Y) shl 1) * ScaledWidth + ((Width - 1) shl 1);
Q^ := P^;
(Q + 1)^ := P^;
for X := 1 to Width - 1 do
begin
Dec(P);
Dec(Q, 2);
Q^ := P^;
(Q + 1)^ := AnsiChar((Word(P^) + Word((P + 1)^) + 1) shr 1);
end;
end;
for Y := 0 to Height - 2 do
begin
P := Pixels + (Y shl 1) * ScaledWidth;
Q := P + ScaledWidth;
R := Q + ScaledWidth;
for X := 0 to Width - 2 do
begin
Q^ := AnsiChar((Word(P^) + Word(R^) + 1) shr 1);
(Q + 1)^ := AnsiChar((Word(P^) + Word((P + 2)^) + Word(R^) + Word((R + 2)^) + 2) shr 2);
Inc(Q, 2);
Inc(P, 2);
Inc(R, 2);
end;
Q^ := AnsiChar((Word(P^) + Word(R^) + 1) shr 1);
Inc(P);
Inc(Q);
Q^ := AnsiChar((Word(P^) + Word(R^) + 1) shr 1);
end;
P := Pixels + (2 * Height - 2) * ScaledWidth;
Q := Pixels + (2 * Height - 1) * ScaledWidth;
Move(P^, Q^, 2 * Width);
end;
//----------------- filter functions for stretching --------------------------------------------------------------------
function HermiteFilter(Value: Single): Single;
// f(t) = 2|t|^3 - 3|t|^2 + 1, -1 <= t <= 1
begin
if Value < 0 then Value := -Value;
if Value < 1 then Result := (2 * Value - 3) * Sqr(Value) + 1
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function BoxFilter(Value: Single): Single;
// This filter is also known as 'nearest neighbour' Filter.
begin
if (Value > -0.5) and (Value <= 0.5) then Result := 1
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function TriangleFilter(Value: Single): Single;
// aka 'linear' or 'bilinear' filter
begin
if Value < 0 then Value := -Value;
if Value < 1 then Result := 1 - Value
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function BellFilter(Value: Single): Single;
begin
if Value < 0 then Value := -Value;
if Value < 0.5 then Result := 0.75 - Sqr(Value)
else
if Value < 1.5 then
begin
Value := Value - 1.5;
Result := 0.5 * Sqr(Value);
end
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function SplineFilter(Value: Single): Single;
// B-spline filter
var
Temp: Single;
begin
if Value < 0 then Value := -Value;
if Value < 1 then
begin
Temp := Sqr(Value);
Result := 0.5 * Temp * Value - Temp + 2 / 3;
end
else
if Value < 2 then
begin
Value := 2 - Value;
Result := Sqr(Value) * Value / 6;
end
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function Lanczos3Filter(Value: Single): Single;
//--------------- local function --------------------------------------------
function SinC(Value: Single): Single;
begin
if Value <> 0 then
begin
Value := Value * Pi;
Result := Sin(Value) / Value;
end
else Result := 1;
end;
//---------------------------------------------------------------------------
begin
if Value < 0 then Value := -Value;
if Value < 3 then Result := SinC(Value) * SinC(Value / 3)
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function MitchellFilter(Value: Single): Single;
const
B = 1 / 3;
C = 1 / 3;
var Temp: Single;
begin
if Value < 0 then Value := -Value;
Temp := Sqr(Value);
if Value < 1 then
begin
Value := (((12 - 9 * B - 6 * C) * (Value * Temp))
+ ((-18 + 12 * B + 6 * C) * Temp)
+ (6 - 2 * B));
Result := Value / 6;
end
else
if Value < 2 then
begin
Value := (((-B - 6 * C) * (Value * Temp))
+ ((6 * B + 30 * C) * Temp)
+ ((-12 * B - 48 * C) * Value)
+ (8 * B + 24 * C));
Result := Value / 6;
end
else Result := 0;
end;
//----------------------------------------------------------------------------------------------------------------------
const
FilterList: array[TResamplingFilter] of TFilterFunction = (
BoxFilter,
TriangleFilter,
HermiteFilter,
BellFilter,
SplineFilter,
Lanczos3Filter,
MitchellFilter
);
//----------------------------------------------------------------------------------------------------------------------
procedure FillLineChache(N, Delta: Integer; Line: Pointer);
var
I: Integer;
Run: PBGR;
begin
Run := Line;
for I := 0 to N - 1 do
begin
CurrentLineR[I] := Run.R;
CurrentLineG[I] := Run.G;
CurrentLineB[I] := Run.B;
Inc(PByte(Run), Delta);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function ApplyContributors(N: Integer; Contributors: TContributors): TBGR;
var
J: Integer;
RGB: TRGBInt;
Total,
Weight: Integer;
Pixel: Cardinal;
Contr: ^TContributor;
begin
RGB.R := 0;
RGB.G := 0;
RGB.B := 0;
Total := 0;
Contr := @Contributors[0];
for J := 0 to N - 1 do
begin
Weight := Contr.Weight;
Inc(Total, Weight);
Pixel := Contr.Pixel;
Inc(RGB.r, CurrentLineR[Pixel] * Weight);
Inc(RGB.g, CurrentLineG[Pixel] * Weight);
Inc(RGB.b, CurrentLineB[Pixel] * Weight);
Inc(Contr);
end;
if Total = 0 then
begin
Result.R := ClampByte(RGB.R shr 8);
Result.G := ClampByte(RGB.G shr 8);
Result.B := ClampByte(RGB.B shr 8);
end
else
begin
Result.R := ClampByte(RGB.R div Total);
Result.G := ClampByte(RGB.G div Total);
Result.B := ClampByte(RGB.B div Total);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure DoStretch(Filter: TFilterFunction; Radius: Single; Source, Target: TBitmap);
// This is the actual scaling routine. Target must be allocated already with sufficient size. Source must
// contain valid data, Radius must not be 0 and Filter must not be nil.
var
ScaleX,
ScaleY: Single; // Zoom scale factors
I, J,
K, N: Integer; // Loop variables
Center: Single; // Filter calculation variables
Width: Single;
Weight: Integer; // Filter calculation variables
Left,
Right: Integer; // Filter calculation variables
Work: TBitmap;
ContributorList: TContributorList;
SourceLine,
DestLine: PPixelArray;
DestPixel: PBGR;
Delta,
DestDelta: Integer;
SourceHeight,
SourceWidth,
TargetHeight,
TargetWidth: Integer;
begin
// shortcut variables
SourceHeight := Source.Height;
SourceWidth := Source.Width;
TargetHeight := Target.Height;
TargetWidth := Target.Width;
if (SourceHeight = 0) or (SourceWidth = 0) or
(TargetHeight = 0) or (TargetWidth = 0) then Exit;
// create intermediate image to hold horizontal zoom
Work := TBitmap.Create;
try
Work.PixelFormat := pf24Bit;
Work.Height := SourceHeight;
Work.Width := TargetWidth;
if SourceWidth = 1 then ScaleX := TargetWidth / SourceWidth
else ScaleX := (TargetWidth - 1) / (SourceWidth - 1);
if (SourceHeight = 1) or (TargetHeight = 1) then ScaleY := TargetHeight / SourceHeight
else ScaleY := (TargetHeight - 1) / (SourceHeight - 1);
// pre-calculate filter contributions for a row
SetLength(ContributorList, TargetWidth);
// horizontal sub-sampling
if ScaleX < 1 then
begin
// scales from bigger to smaller Width
Width := Radius / ScaleX;
for I := 0 to TargetWidth - 1 do
begin
ContributorList[I].N := 0;
SetLength(ContributorList[I].Contributors, Trunc(2 * Width + 1));
Center := I / ScaleX;
Left := Floor(Center - Width);
Right := Ceil(Center + Width);
for J := Left to Right do
begin
Weight := Round(Filter((Center - J) * ScaleX) * ScaleX * 256);
if Weight <> 0 then
begin
if J < 0 then N := -J
else
if J >= SourceWidth then N := SourceWidth - J + SourceWidth - 1
else N := J;
K := ContributorList[I].N;
Inc(ContributorList[I].N);
ContributorList[I].Contributors[K].Pixel := N;
ContributorList[I].Contributors[K].Weight := Weight;
end;
end;
end;
end
else
begin
// horizontal super-sampling
// scales from smaller to bigger Width
for I := 0 to TargetWidth - 1 do
begin
ContributorList[I].N := 0;
SetLength(ContributorList[I].Contributors, Trunc(2 * Radius + 1));
Center := I / ScaleX;
Left := Floor(Center - Radius);
Right := Ceil(Center + Radius);
for J := Left to Right do
begin
Weight := Round(Filter(Center - J) * 256);
if Weight <> 0 then
begin
if J < 0 then N := -J
else
if J >= SourceWidth then N := SourceWidth - J + SourceWidth - 1
else N := J;
K := ContributorList[I].N;
Inc(ContributorList[I].N);
ContributorList[I].Contributors[K].Pixel := N;
ContributorList[I].Contributors[K].Weight := Weight;
end;
end;
end;
end;
// now apply filter to sample horizontally from Src to Work
SetLength(CurrentLineR, SourceWidth);
SetLength(CurrentLineG, SourceWidth);
SetLength(CurrentLineB, SourceWidth);
for K := 0 to SourceHeight - 1 do
begin
SourceLine := Source.ScanLine[K];
FillLineChache(SourceWidth, 3, SourceLine);
DestPixel := Work.ScanLine[K];
for I := 0 to TargetWidth - 1 do
with ContributorList[I] do
begin
DestPixel^ := ApplyContributors(N, ContributorList[I].Contributors);
// move on to next column
Inc(DestPixel);
end;
end;
// free the memory allocated for horizontal filter weights, since we need the stucture again
for I := 0 to TargetWidth - 1 do ContributorList[I].Contributors := nil;
ContributorList := nil;
// pre-calculate filter contributions for a column
SetLength(ContributorList, TargetHeight);
// vertical sub-sampling
if ScaleY < 1 then
begin
// scales from bigger to smaller height
Width := Radius / ScaleY;
for I := 0 to TargetHeight - 1 do
begin
ContributorList[I].N := 0;
SetLength(ContributorList[I].Contributors, Trunc(2 * Width + 1));
Center := I / ScaleY;
Left := Floor(Center - Width);
Right := Ceil(Center + Width);
for J := Left to Right do
begin
Weight := Round(Filter((Center - J) * ScaleY) * ScaleY * 256);
if Weight <> 0 then
begin
if J < 0 then N := -J
else
if J >= SourceHeight then N := SourceHeight - J + SourceHeight - 1
else N := J;
K := ContributorList[I].N;
Inc(ContributorList[I].N);
ContributorList[I].Contributors[K].Pixel := N;
ContributorList[I].Contributors[K].Weight := Weight;
end;
end;
end
end
else
begin
// vertical super-sampling
// scales from smaller to bigger height
for I := 0 to TargetHeight - 1 do
begin
ContributorList[I].N := 0;
SetLength(ContributorList[I].Contributors, Trunc(2 * Radius + 1));
Center := I / ScaleY;
Left := Floor(Center - Radius);
Right := Ceil(Center + Radius);
for J := Left to Right do
begin
Weight := Round(Filter(Center - J) * 256);
if Weight <> 0 then
begin
if J < 0 then N := -J
else
if J >= SourceHeight then N := SourceHeight - J + SourceHeight - 1
else N := J;
K := ContributorList[I].N;
Inc(ContributorList[I].N);
ContributorList[I].Contributors[K].Pixel := N;
ContributorList[I].Contributors[K].Weight := Weight;
end;
end;
end;
end;
// apply filter to sample vertically from Work to Target
SetLength(CurrentLineR, SourceHeight);
SetLength(CurrentLineG, SourceHeight);
SetLength(CurrentLineB, SourceHeight);
SourceLine := Work.ScanLine[0];
Delta := Integer(Work.ScanLine[1]) - Integer(SourceLine);
DestLine := Target.ScanLine[0];
DestDelta := Integer(Target.ScanLine[1]) - Integer(DestLine);
for K := 0 to TargetWidth - 1 do
begin
DestPixel := Pointer(DestLine);
FillLineChache(SourceHeight, Delta, SourceLine);
for I := 0 to TargetHeight - 1 do
with ContributorList[I] do
begin
DestPixel^ := ApplyContributors(N, ContributorList[I].Contributors);
Inc(Integer(DestPixel), DestDelta);
end;
Inc(SourceLine);
Inc(DestLine);
end;
// free the memory allocated for vertical filter weights
for I := 0 to TargetHeight - 1 do ContributorList[I].Contributors := nil;
// this one is done automatically on exit, but is here for completeness
ContributorList := nil;
finally
Work.Free;
CurrentLineR := nil;
CurrentLineG := nil;
CurrentLineB := nil;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure Stretch(NewWidth, NewHeight: Cardinal; Filter: TResamplingFilter; Radius: Single; Source, Target: TBitmap);
// Scales the source bitmap to the given size (NewWidth, NewHeight) and stores the Result in Target.
// Filter describes the filter function to be applied and Radius the size of the filter area.
// Is Radius = 0 then the recommended filter area will be used (see DefaultFilterRadius).
begin
if Radius = 0 then Radius := DefaultFilterRadius[Filter];
Target.Handle := 0;
Target.PixelFormat := pf24Bit;
Target.Width := NewWidth;
Target.Height := NewHeight;
Source.PixelFormat := pf24Bit;
DoStretch(FilterList[Filter], Radius, Source, Target);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure Stretch(NewWidth, NewHeight: Cardinal; Filter: TResamplingFilter; Radius: Single; Source: TBitmap);
var
Target: TBitmap;
begin
if Radius = 0 then Radius := DefaultFilterRadius[Filter];
Target := TBitmap.Create;
try
Target.PixelFormat := pf24Bit;
Target.Width := NewWidth;
Target.Height := NewHeight;
Source.PixelFormat := pf24Bit;
DoStretch(FilterList[Filter], Radius, Source, Target);
Source.Assign(Target);
finally
Target.Free;
end;
end;
//----------------- support functions for image loading ----------------------------------------------------------------
procedure SwapShort(P: PWord; Count: Cardinal);
// swaps high and low byte of 16 bit values
// EAX contains P, EDX contains Count
asm
@@Loop:
MOV CX, [EAX]
XCHG CH, CL
MOV [EAX], CX
ADD EAX, 2
DEC EDX
JNZ @@Loop
end;
//----------------------------------------------------------------------------------------------------------------------
procedure SwapLong(P: PInteger; Count: Cardinal); overload;
// swaps high and low bytes of 32 bit values
// EAX contains P, EDX contains Count
asm
@@Loop:
MOV ECX, [EAX]
BSWAP ECX
MOV [EAX], ECX
ADD EAX, 4
DEC EDX
JNZ @@Loop
end;
//----------------------------------------------------------------------------------------------------------------------
function SwapLong(Value: Cardinal): Cardinal; overload;
// swaps high and low bytes of the given 32 bit value
asm
BSWAP EAX
end;
//----------------- various conversion routines ------------------------------------------------------------------------
procedure Depredict1(P: Pointer; Count: Cardinal);
// EAX contains P and EDX Count
asm
@@1:
MOV CL, [EAX]
ADD [EAX + 1], CL
INC EAX
DEC EDX
JNZ @@1
end;
//----------------------------------------------------------------------------------------------------------------------
procedure Depredict3(P: Pointer; Count: Cardinal);
// EAX contains P and EDX Count
asm
MOV ECX, EDX
SHL ECX, 1
ADD ECX, EDX // 3 * Count
@@1:
MOV DL, [EAX]
ADD [EAX + 3], DL
INC EAX
DEC ECX
JNZ @@1
end;
//----------------------------------------------------------------------------------------------------------------------
procedure Depredict4(P: Pointer; Count: Cardinal);
// EAX contains P and EDX Count
asm
SHL EDX, 2 // 4 * Count
@@1:
MOV CL, [EAX]
ADD [EAX + 4], CL
INC EAX
DEC EDX
JNZ @@1
end;
//----------------- TGraphicExGraphic ----------------------------------------------------------------------------------
constructor TGraphicExGraphic.Create;
begin
inherited;
FColorManager := TColorManager.Create;
end;
//----------------------------------------------------------------------------------------------------------------------
destructor TGraphicExGraphic.Destroy;
begin
FColorManager.Free;
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TGraphicExGraphic.Assign(Source: TPersistent);
begin
if Source is TGraphicExGraphic then FImageProperties := TGraphicExGraphic(Source).FImageProperties;
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
function TGraphicExGraphic.CanLoad(const FileName: AnsiString): Boolean;
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Result := CanLoad(Stream);
finally
Stream.Free;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TGraphicExGraphic.CanLoad(Stream: TStream): Boolean;
// Descentants have to override this method and return True if they consider the data in Stream
// as loadable by the particular class.
// Note: Make sure the stream position is the same on exit as it was on enter!
begin
Result := False;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TGraphicExGraphic.LoadFromResourceID(Instance: THandle; ResID: Integer);
var
Stream: TResourceStream;
begin
Stream := TResourceStream.CreateFromID(Instance, ResID, RT_RCDATA);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TGraphicExGraphic.LoadFromResourceName(Instance: THandle; const ResName: AnsiString);
var
Stream: TResourceStream;
begin
Stream := TResourceStream.Create(Instance, ResName, RT_RCDATA);
try
LoadFromStream(Stream);
finally
Stream.Free;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TGraphicExGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
// Initializes the internal image properties structure.
// Descentants must override this method to fill in the actual values.
// Result is always False to show there is no image to load.
begin
Finalize(FImageProperties);
ZeroMemory(@FImageProperties, SizeOf(FImageProperties));
FImageProperties.FileGamma := 1;
Result := False;
end;
//----------------- TAutodeskGraphic -----------------------------------------------------------------------------------
{$ifdef AutodeskGraphic}
type
TAutodeskHeader = packed record
Width,
Height,
XCoord,
YCoord: Word;
Depth,
Compression: Byte;
DataSize: Cardinal;
Reserved: array[0..15] of Byte;
end;
//----------------------------------------------------------------------------------------------------------------------
function TAutodeskGraphic.CanLoad(Stream: TStream): Boolean;
var
FileID: Word;
Header: TAutodeskHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
Result := (Size - Position) > (SizeOf(FileID) + SizeOf(Header));
if Result then
begin
LastPosition := Position;
Read(FileID, SizeOf(FileID));
Result := FileID = $9119;
if Result then
begin
// read image dimensions
Read(Header, SizeOf(Header));
Result := (Header.Depth = 8) and (Header.Compression = 0);
end;
Position := LastPosition;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TAutodeskGraphic.LoadFromStream(Stream: TStream);
var
FileID: Word;
FileHeader: TAutodeskHeader;
LogPalette: TMaxLogPalette;
I: Integer;
begin
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with Stream do
begin
Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
Read(FileID, 2);
// read image dimensions
Read(FileHeader, SizeOf(FileHeader));
// read palette entries and create a palette
ZeroMemory(@LogPalette, SizeOf(LogPalette));
LogPalette.palVersion := $300;
LogPalette.palNumEntries := 256;
for I := 0 to 255 do
begin
Read(LogPalette.palPalEntry[I], 3);
LogPalette.palPalEntry[I].peBlue := LogPalette.palPalEntry[I].peBlue shl 2;
LogPalette.palPalEntry[I].peGreen := LogPalette.palPalEntry[I].peGreen shl 2;
LogPalette.palPalEntry[I].peRed := LogPalette.palPalEntry[I].peRed shl 2;
end;
// setup bitmap properties
PixelFormat := pf8Bit;
Palette := CreatePalette(PLogPalette(@LogPalette)^);
Width := FileHeader.Width;
Height := FileHeader.Height;
// finally read image data
for I := 0 to Height - 1 do
begin
Read(Scanline[I]^, FileHeader.Width);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end
else GraphicExError(gesInvalidImage, ['Autodesk']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TAutodeskGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
FileID: Word;
Header: TAutodeskHeader;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
Read(FileID, 2);
if FileID = $9119 then
begin
// read image dimensions
Read(Header, SizeOf(Header));
ColorScheme := csIndexed;
Width := Header.Width;
Height := Header.Height;
BitsPerSample := 8;
SamplesPerPixel := 1;
BitsPerPixel := 8;
Compression := ctNone;
Result := True;
end;
end;
end;
{$endif} // AutodeskGraphic
//----------------- TSGIGraphic ----------------------------------------------------------------------------------------
{$ifdef SGIGraphic}
const
SGIMagic = 474;
SGI_COMPRESSION_VERBATIM = 0;
SGI_COMPRESSION_RLE = 1;
type
TSGIHeader = packed record
Magic: SmallInt; // IRIS image file magic number
Storage, // Storage format
BPC: Byte; // Number of bytes per pixel channel (1 or 2)
Dimension: Word; // Number of dimensions
// 1 - one single scanline (and one channel) of length XSize
// 2 - two dimensional (one channel) of size XSize x YSize
// 3 - three dimensional (ZSize channels) of size XSize x YSize
XSize, // width of image
YSize, // height of image
ZSize: Word; // number of channels/planes in image (3 for RGB, 4 for RGBA etc.)
PixMin, // Minimum pixel value
PixMax: Cardinal; // Maximum pixel value
Dummy: Cardinal; // ignored
ImageName: array[0..79] of AnsiChar;
ColorMap: Integer; // Colormap ID
// 0 - default, almost all images are stored with this flag
// 1 - dithered, only one channel of data (pixels are packed), obsolete
// 2 - screen (palette) image, obsolete
// 3 - no image data, palette only, not displayable
Dummy2: array[0..403] of Byte; // ignored
end;
//----------------------------------------------------------------------------------------------------------------------
function TSGIGraphic.CanLoad(Stream: TStream): Boolean;
// returns True if the data in Stream represents a graphic which can be loaded by this class
var
Header: TSGIHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
Result := (Size - Position) > SizeOf(TSGIHeader);
if Result then
begin
LastPosition := Position;
ReadBuffer(Header, SizeOf(Header));
// one number as check is too unreliable, hence we take some more fields into the check
Result := (Swap(Header.Magic) = SGIMagic) and
(Header.BPC in [1, 2]) and
(Swap(Header.Dimension) in [1..3]);
Position := LastPosition;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TSGIGraphic.ReadAndDecode(Red, Green, Blue, Alpha: Pointer; Row, BPC: Cardinal);
var
Count: Cardinal;
RawBuffer: Pointer;
begin
with FStream, FImageProperties do
// compressed image?
if Assigned(FDecoder) then
begin
if Assigned(Red) then
begin
Position := FBasePosition + FRowStart[Row + 0 * Height];
Count := BPC * FRowSize[Row + 0 * Height];
GetMem(RawBuffer, Count);
try
Read(RawBuffer^, Count);
FDecoder.Decode(RawBuffer, Red, Count, Width);
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end;
if Assigned(Green) then
begin
Position := FBasePosition + FRowStart[Row + 1 * Height];
Count := BPC * FRowSize[Row + 1 * Height];
GetMem(RawBuffer, Count);
try
Read(RawBuffer^, Count);
FDecoder.Decode(RawBuffer, Green, Count, Width);
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end;
if Assigned(Blue) then
begin
Position := FBasePosition + FRowStart[Row + 2 * Height];
Count := BPC * FRowSize[Row + 2 * Height];
GetMem(RawBuffer, Count);
try
Read(RawBuffer^, Count);
FDecoder.Decode(RawBuffer, Blue, Count, Width);
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end;
if Assigned(Alpha) then
begin
Position := FBasePosition + FRowStart[Row + 3 * Height];
Count := BPC * FRowSize[Row + 3 * Height];
GetMem(RawBuffer, Count);
try
Read(RawBuffer^, Count);
FDecoder.Decode(RawBuffer, Alpha, Count, Width);
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end;
end
else
begin
if Assigned(Red) then
begin
Position := FBasePosition + 512 + (Row * Width);
Read(Red^, BPC * Width);
end;
if Assigned(Green) then
begin
Position := FBasePosition + 512 + (Row * Width) + (Width * Height);
Read(Green^, BPC * Width);
end;
if Assigned(Blue) then
begin
Position := FBasePosition + 512 + (Row * Width) + (2 * Width * Height);
Read(Blue^, BPC * Width);
end;
if Assigned(Alpha) then
begin
Position := FBasePosition + 512 + (Row * Width) + (3 * Width * Height);
Read(Alpha^, BPC * Width);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TSGIGraphic.LoadFromStream(Stream: TStream);
var
Y: Cardinal;
RedBuffer,
GreenBuffer,
BlueBuffer,
AlphaBuffer: Pointer;
Header: TSGIHeader;
Count: Cardinal;
begin
// free previous image
Handle := 0;
// keep stream reference and start position for seek operations
FStream := Stream;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with FImageProperties, Stream do
begin
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
Stream.Position := FBasePosition;
// read header again, we need some additional information
ReadBuffer(Header, SizeOf(Header));
// SGI images are always stored in big endian style
ColorManager.SourceOptions := [coNeedByteSwap];
with Header do ColorMap := SwapLong(ColorMap);
if Compression = ctRLE then
begin
Count := Height * SamplesPerPixel;
SetLength(FRowStart, Count);
SetLength(FRowSize, Count);
// read line starts and sizes from stream
Read(FRowStart[0], Count * SizeOf(Cardinal));
SwapLong(@FRowStart[0], Count);
Read(FRowSize[0], Count * SizeOf(Cardinal));
SwapLong(@FRowSize[0], Count);
FDecoder := TSGIRLEDecoder.Create(BitsPerSample);
end
else
begin
FDecoder := nil;
end;
// set pixel format before size to avoid possibly large conversion operation
with ColorManager do
begin
SourceBitsPerSample := BitsPerSample;
TargetBitsPerSample := 8;
SourceSamplesPerPixel := SamplesPerPixel;
TargetSamplesPerPixel := SamplesPerPixel;
SourceColorScheme := ColorScheme;
case ColorScheme of
csRGBA:
TargetColorScheme := csBGRA;
csRGB:
TargetColorScheme := csBGR;
else
TargetColorScheme := csIndexed;
end;
PixelFormat := TargetPixelFormat;
end;
Self.Width := Width;
Self.Height := Height;
RedBuffer := nil;
GreenBuffer := nil;
BlueBuffer := nil;
AlphaBuffer := nil;
Progress(Self, psEnding, 100, True, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
try
Count := (BitsPerPixel div 8) * Width;
// read lines and put them into the bitmap
case ColorScheme of
csRGBA:
begin
GetMem(RedBuffer, Count);
GetMem(GreenBuffer, Count);
GetMem(BlueBuffer, Count);
GetMem(AlphaBuffer, Count);
for Y := 0 to Height - 1 do
begin
ReadAndDecode(RedBuffer, GreenBuffer, BlueBuffer, AlphaBuffer, Y, Header.BPC);
ColorManager.ConvertRow([RedBuffer, GreenBuffer, BlueBuffer, AlphaBuffer],
ScanLine[Height - Y - 1], Width, $FF);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
csRGB:
begin
GetMem(RedBuffer, Count);
GetMem(GreenBuffer, Count);
GetMem(BlueBuffer, Count);
for Y := 0 to Height - 1 do
begin
ReadAndDecode(RedBuffer, GreenBuffer, BlueBuffer, nil, Y, Header.BPC);
ColorManager.ConvertRow([RedBuffer, GreenBuffer, BlueBuffer],
ScanLine[Height - Y - 1], Width, $FF);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
else
// any other format is interpreted as being 256 gray scales
Palette := ColorManager.CreateGrayscalePalette(False);
for Y := 0 to Height - 1 do
begin
ReadAndDecode(ScanLine[Height - Y - 1], nil, nil, nil, Y, Header.BPC);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
finally
Progress(Self, psEnding, 100, True, FProgressRect, '');
if Assigned(RedBuffer) then FreeMem(RedBuffer);
if Assigned(GreenBuffer) then FreeMem(GreenBuffer);
if Assigned(BlueBuffer) then FreeMem(BlueBuffer);
if Assigned(AlphaBuffer) then FreeMem(AlphaBuffer);
FDecoder.Free;
end;
end;
end
else GraphicExError(gesInvalidImage, ['sgi, bw or rgb(a)']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TSGIGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TSGIHeader;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with FImageProperties do
begin
Stream.ReadBuffer(Header, SizeOf(Header));
if Swap(Header.Magic) = SGIMagic then
begin
Options := [ioBigEndian];
BitsPerSample := Header.BPC * 8;
Width := Swap(Header.XSize);
Height := Swap(Header.YSize);
SamplesPerPixel := Swap(Header.ZSize);
case SamplesPerPixel of
4:
ColorScheme := csRGBA;
3:
ColorScheme := csRGB;
else
// all other is considered as being 8 bit gray scale
ColorScheme := csIndexed;
end;
BitsPerPixel := BitsPerSample * SamplesPerPixel;
if Header.Storage = SGI_COMPRESSION_RLE then Compression := ctRLE
else Compression := ctNone;
Result := True;
end;
end;
end;
{$endif} // SGIGraphic
//----------------- TTIFFGraphic ---------------------------------------------------------------------------------------
{$ifdef TIFFGraphic}
const // TIFF tags
TIFFTAG_SUBFILETYPE = 254; // subfile data descriptor
FILETYPE_REDUCEDIMAGE = $1; // reduced resolution version
FILETYPE_PAGE = $2; // one page of many
FILETYPE_MASK = $4; // transparency mask
TIFFTAG_OSUBFILETYPE = 255; // kind of data in subfile (obsolete by revision 5.0)
OFILETYPE_IMAGE = 1; // full resolution image data
OFILETYPE_REDUCEDIMAGE = 2; // reduced size image data
OFILETYPE_PAGE = 3; // one page of many
TIFFTAG_IMAGEWIDTH = 256; // image width in pixels
TIFFTAG_IMAGELENGTH = 257; // image height in pixels
TIFFTAG_BITSPERSAMPLE = 258; // bits per channel (sample)
TIFFTAG_COMPRESSION = 259; // data compression technique
COMPRESSION_NONE = 1; // dump mode
COMPRESSION_CCITTRLE = 2; // CCITT modified Huffman RLE
COMPRESSION_CCITTFAX3 = 3; // CCITT Group 3 fax encoding
COMPRESSION_CCITTFAX4 = 4; // CCITT Group 4 fax encoding
COMPRESSION_LZW = 5; // Lempel-Ziv & Welch
COMPRESSION_OJPEG = 6; // 6.0 JPEG (old version)
COMPRESSION_JPEG = 7; // JPEG DCT compression (new version)
COMPRESSION_ADOBE_DEFLATE = 8; // new id but same as COMPRESSION_DEFLATE
COMPRESSION_NEXT = 32766; // next 2-bit RLE
COMPRESSION_CCITTRLEW = 32771; // modified Huffman with word alignment
COMPRESSION_PACKBITS = 32773; // Macintosh RLE
COMPRESSION_THUNDERSCAN = 32809; // ThunderScan RLE
// codes 32895-32898 are reserved for ANSI IT8 TIFF/IT <dkelly@etsinc.com)
COMPRESSION_IT8CTPAD = 32895; // IT8 CT w/padding
COMPRESSION_IT8LW = 32896; // IT8 Linework RLE
COMPRESSION_IT8MP = 32897; // IT8 Monochrome picture
COMPRESSION_IT8BL = 32898; // IT8 Binary line art
// compression codes 32908-32911 are reserved for Pixar
COMPRESSION_PIXARFILM = 32908; // Pixar companded 10bit LZW
COMPRESSION_PIXARLOG = 32909; // Pixar companded 11bit ZIP
COMPRESSION_DEFLATE = 32946; // Deflate compression (LZ77)
// compression code 32947 is reserved for Oceana Matrix <dev@oceana.com>
COMPRESSION_DCS = 32947; // Kodak DCS encoding
COMPRESSION_JBIG = 34661; // ISO JBIG
TIFFTAG_PHOTOMETRIC = 262; // photometric interpretation
PHOTOMETRIC_MINISWHITE = 0; // min value is white
PHOTOMETRIC_MINISBLACK = 1; // min value is black
PHOTOMETRIC_RGB = 2; // RGB color model
PHOTOMETRIC_PALETTE = 3; // color map indexed
PHOTOMETRIC_MASK = 4; // holdout mask
PHOTOMETRIC_SEPARATED = 5; // color separations
PHOTOMETRIC_YCBCR = 6; // CCIR 601
PHOTOMETRIC_CIELAB = 8; // 1976 CIE L*a*b*
TIFFTAG_THRESHHOLDING = 263; // thresholding used on data (obsolete by revision 5.0)
THRESHHOLD_BILEVEL = 1; // b&w art scan
THRESHHOLD_HALFTONE = 2; // or dithered scan
THRESHHOLD_ERRORDIFFUSE = 3; // usually floyd-steinberg
TIFFTAG_CELLWIDTH = 264; // dithering matrix width (obsolete by revision 5.0)
TIFFTAG_CELLLENGTH = 265; // dithering matrix height (obsolete by revision 5.0)
TIFFTAG_FILLORDER = 266; // data order within a Byte
FILLORDER_MSB2LSB = 1; // most significant -> least
FILLORDER_LSB2MSB = 2; // least significant -> most
TIFFTAG_DOCUMENTNAME = 269; // name of doc. image is from
TIFFTAG_IMAGEDESCRIPTION = 270; // info about image
TIFFTAG_MAKE = 271; // scanner manufacturer name
TIFFTAG_MODEL = 272; // scanner model name/number
TIFFTAG_STRIPOFFSETS = 273; // Offsets to data strips
TIFFTAG_ORIENTATION = 274; // image FOrientation (obsolete by revision 5.0)
ORIENTATION_TOPLEFT = 1; // row 0 top, col 0 lhs
ORIENTATION_TOPRIGHT = 2; // row 0 top, col 0 rhs
ORIENTATION_BOTRIGHT = 3; // row 0 bottom, col 0 rhs
ORIENTATION_BOTLEFT = 4; // row 0 bottom, col 0 lhs
ORIENTATION_LEFTTOP = 5; // row 0 lhs, col 0 top
ORIENTATION_RIGHTTOP = 6; // row 0 rhs, col 0 top
ORIENTATION_RIGHTBOT = 7; // row 0 rhs, col 0 bottom
ORIENTATION_LEFTBOT = 8; // row 0 lhs, col 0 bottom
TIFFTAG_SAMPLESPERPIXEL = 277; // samples per pixel
TIFFTAG_ROWSPERSTRIP = 278; // rows per strip of data
TIFFTAG_STRIPBYTECOUNTS = 279; // bytes counts for strips
TIFFTAG_MINSAMPLEVALUE = 280; // minimum sample value (obsolete by revision 5.0)
TIFFTAG_MAXSAMPLEVALUE = 281; // maximum sample value (obsolete by revision 5.0)
TIFFTAG_XRESOLUTION = 282; // pixels/resolution in x
TIFFTAG_YRESOLUTION = 283; // pixels/resolution in y
TIFFTAG_PLANARCONFIG = 284; // storage organization
PLANARCONFIG_CONTIG = 1; // single image plane
PLANARCONFIG_SEPARATE = 2; // separate planes of data
TIFFTAG_PAGENAME = 285; // page name image is from
TIFFTAG_XPOSITION = 286; // x page offset of image lhs
TIFFTAG_YPOSITION = 287; // y page offset of image lhs
TIFFTAG_FREEOFFSETS = 288; // byte offset to free block (obsolete by revision 5.0)
TIFFTAG_FREEBYTECOUNTS = 289; // sizes of free blocks (obsolete by revision 5.0)
TIFFTAG_GRAYRESPONSEUNIT = 290; // gray scale curve accuracy
GRAYRESPONSEUNIT_10S = 1; // tenths of a unit
GRAYRESPONSEUNIT_100S = 2; // hundredths of a unit
GRAYRESPONSEUNIT_1000S = 3; // thousandths of a unit
GRAYRESPONSEUNIT_10000S = 4; // ten-thousandths of a unit
GRAYRESPONSEUNIT_100000S = 5; // hundred-thousandths
TIFFTAG_GRAYRESPONSECURVE = 291; // gray scale response curve
TIFFTAG_GROUP3OPTIONS = 292; // 32 flag bits
GROUP3OPT_2DENCODING = $1; // 2-dimensional coding
GROUP3OPT_UNCOMPRESSED = $2; // data not compressed
GROUP3OPT_FILLBITS = $4; // fill to byte boundary
TIFFTAG_GROUP4OPTIONS = 293; // 32 flag bits
GROUP4OPT_UNCOMPRESSED = $2; // data not compressed
TIFFTAG_RESOLUTIONUNIT = 296; // units of resolutions
RESUNIT_NONE = 1; // no meaningful units
RESUNIT_INCH = 2; // english
RESUNIT_CENTIMETER = 3; // metric
TIFFTAG_PAGENUMBER = 297; // page numbers of multi-page
TIFFTAG_COLORRESPONSEUNIT = 300; // color curve accuracy
COLORRESPONSEUNIT_10S = 1; // tenths of a unit
COLORRESPONSEUNIT_100S = 2; // hundredths of a unit
COLORRESPONSEUNIT_1000S = 3; // thousandths of a unit
COLORRESPONSEUNIT_10000S = 4; // ten-thousandths of a unit
COLORRESPONSEUNIT_100000S = 5; // hundred-thousandths
TIFFTAG_TRANSFERFUNCTION = 301; // colorimetry info
TIFFTAG_SOFTWARE = 305; // name & release
TIFFTAG_DATETIME = 306; // creation date and time
TIFFTAG_ARTIST = 315; // creator of image
TIFFTAG_HOSTCOMPUTER = 316; // machine where created
TIFFTAG_PREDICTOR = 317; // prediction scheme w/ LZW
PREDICTION_NONE = 1; // no prediction scheme used before coding
PREDICTION_HORZ_DIFFERENCING = 2; // horizontal differencing prediction scheme used
TIFFTAG_WHITEPOINT = 318; // image white point
TIFFTAG_PRIMARYCHROMATICITIES = 319; // primary chromaticities
TIFFTAG_COLORMAP = 320; // RGB map for pallette image
TIFFTAG_HALFTONEHINTS = 321; // highlight+shadow info
TIFFTAG_TILEWIDTH = 322; // rows/data tile
TIFFTAG_TILELENGTH = 323; // cols/data tile
TIFFTAG_TILEOFFSETS = 324; // offsets to data tiles
TIFFTAG_TILEBYTECOUNTS = 325; // Byte counts for tiles
TIFFTAG_BADFAXLINES = 326; // lines w/ wrong pixel count
TIFFTAG_CLEANFAXDATA = 327; // regenerated line info
CLEANFAXDATA_CLEAN = 0; // no errors detected
CLEANFAXDATA_REGENERATED = 1; // receiver regenerated lines
CLEANFAXDATA_UNCLEAN = 2; // uncorrected errors exist
TIFFTAG_CONSECUTIVEBADFAXLINES = 328; // max consecutive bad lines
TIFFTAG_SUBIFD = 330; // subimage descriptors
TIFFTAG_INKSET = 332; // inks in separated image
INKSET_CMYK = 1; // cyan-magenta-yellow-black
TIFFTAG_INKNAMES = 333; // ascii names of inks
TIFFTAG_DOTRANGE = 336; // 0% and 100% dot codes
TIFFTAG_TARGETPRINTER = 337; // separation target
TIFFTAG_EXTRASAMPLES = 338; // info about extra samples
EXTRASAMPLE_UNSPECIFIED = 0; // unspecified data
EXTRASAMPLE_ASSOCALPHA = 1; // associated alpha data
EXTRASAMPLE_UNASSALPHA = 2; // unassociated alpha data
TIFFTAG_SAMPLEFORMAT = 339; // data sample format
SAMPLEFORMAT_UINT = 1; // unsigned integer data
SAMPLEFORMAT_INT = 2; // signed integer data
SAMPLEFORMAT_IEEEFP = 3; // IEEE floating point data
SAMPLEFORMAT_VOID = 4; // untyped data
TIFFTAG_SMINSAMPLEVALUE = 340; // variable MinSampleValue
TIFFTAG_SMAXSAMPLEVALUE = 341; // variable MaxSampleValue
TIFFTAG_JPEGTABLES = 347; // JPEG table stream
// Tags 512-521 are obsoleted by Technical Note #2 which specifies a revised JPEG-in-TIFF scheme.
TIFFTAG_JPEGPROC = 512; // JPEG processing algorithm
JPEGPROC_BASELINE = 1; // baseline sequential
JPEGPROC_LOSSLESS = 14; // Huffman coded lossless
TIFFTAG_JPEGIFOFFSET = 513; // Pointer to SOI marker
TIFFTAG_JPEGIFBYTECOUNT = 514; // JFIF stream length
TIFFTAG_JPEGRESTARTINTERVAL = 515; // restart interval length
TIFFTAG_JPEGLOSSLESSPREDICTORS = 517; // lossless proc predictor
TIFFTAG_JPEGPOINTTRANSFORM = 518; // lossless point transform
TIFFTAG_JPEGQTABLES = 519; // Q matrice offsets
TIFFTAG_JPEGDCTABLES = 520; // DCT table offsets
TIFFTAG_JPEGACTABLES = 521; // AC coefficient offsets
TIFFTAG_YCBCRCOEFFICIENTS = 529; // RGB -> YCbCr transform
TIFFTAG_YCBCRSUBSAMPLING = 530; // YCbCr subsampling factors
TIFFTAG_YCBCRPOSITIONING = 531; // subsample positioning
YCBCRPOSITION_CENTERED = 1; // as in PostScript Level 2
YCBCRPOSITION_COSITED = 2; // as in CCIR 601-1
TIFFTAG_REFERENCEBLACKWHITE = 532; // colorimetry info
// tags 32952-32956 are private tags registered to Island Graphics
TIFFTAG_REFPTS = 32953; // image reference points
TIFFTAG_REGIONTACKPOINT = 32954; // region-xform tack point
TIFFTAG_REGIONWARPCORNERS = 32955; // warp quadrilateral
TIFFTAG_REGIONAFFINE = 32956; // affine transformation mat
// tags 32995-32999 are private tags registered to SGI
TIFFTAG_MATTEING = 32995; // use ExtraSamples
TIFFTAG_DATATYPE = 32996; // use SampleFormat
TIFFTAG_IMAGEDEPTH = 32997; // z depth of image
TIFFTAG_TILEDEPTH = 32998; // z depth/data tile
// tags 33300-33309 are private tags registered to Pixar
//
// 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.
TIFFTAG_PIXAR_IMAGEFULLWIDTH = 33300; // full image size in x
TIFFTAG_PIXAR_IMAGEFULLLENGTH = 33301; // full image size in y
// tag 33405 is a private tag registered to Eastman Kodak
TIFFTAG_WRITERSERIALNUMBER = 33405; // device serial number
// tag 33432 is listed in the 6.0 spec w/ unknown ownership
TIFFTAG_COPYRIGHT = 33432; // copyright AnsiString
// 34016-34029 are reserved for ANSI IT8 TIFF/IT <dkelly@etsinc.com)
TIFFTAG_IT8SITE = 34016; // site name
TIFFTAG_IT8COLORSEQUENCE = 34017; // color seq. [RGB,CMYK,etc]
TIFFTAG_IT8HEADER = 34018; // DDES Header
TIFFTAG_IT8RASTERPADDING = 34019; // raster scanline padding
TIFFTAG_IT8BITSPERRUNLENGTH = 34020; // # of bits in short run
TIFFTAG_IT8BITSPEREXTENDEDRUNLENGTH = 34021; // # of bits in long run
TIFFTAG_IT8COLORTABLE = 34022; // LW colortable
TIFFTAG_IT8IMAGECOLORINDICATOR = 34023; // BP/BL image color switch
TIFFTAG_IT8BKGCOLORINDICATOR = 34024; // BP/BL bg color switch
TIFFTAG_IT8IMAGECOLORVALUE = 34025; // BP/BL image color value
TIFFTAG_IT8BKGCOLORVALUE = 34026; // BP/BL bg color value
TIFFTAG_IT8PIXELINTENSITYRANGE = 34027; // MP pixel intensity value
TIFFTAG_IT8TRANSPARENCYINDICATOR = 34028; // HC transparency switch
TIFFTAG_IT8COLORCHARACTERIZATION = 34029; // color character. table
// tags 34232-34236 are private tags registered to Texas Instruments
TIFFTAG_FRAMECOUNT = 34232; // Sequence Frame Count
// tag 34750 is a private tag registered to Pixel Magic
TIFFTAG_JBIGOPTIONS = 34750; // JBIG options
// tags 34908-34914 are private tags registered to SGI
TIFFTAG_FAXRECVPARAMS = 34908; // encoded class 2 ses. parms
TIFFTAG_FAXSUBADDRESS = 34909; // received SubAddr AnsiString
TIFFTAG_FAXRECVTIME = 34910; // receive time (secs)
// tag 65535 is an undefined tag used by Eastman Kodak
TIFFTAG_DCSHUESHIFTVALUES = 65535; // hue shift correction data
// The following are 'pseudo tags' that can be used to control codec-specific functionality.
// These tags are not written to file. Note that these values start at $FFFF + 1 so that they'll
// never collide with Aldus-assigned tags.
TIFFTAG_FAXMODE = 65536; // Group 3/4 format control
FAXMODE_CLASSIC = $0000; // default, include RTC
FAXMODE_NORTC = $0001; // no RTC at end of data
FAXMODE_NOEOL = $0002; // no EOL code at end of row
FAXMODE_BYTEALIGN = $0004; // Byte align row
FAXMODE_WORDALIGN = $0008; // Word align row
FAXMODE_CLASSF = FAXMODE_NORTC; // TIFF class F
TIFFTAG_JPEGQUALITY = 65537; // compression quality level
// Note: quality level is on the IJG 0-100 scale. Default value is 75
TIFFTAG_JPEGCOLORMODE = 65538; // Auto RGB<=>YCbCr convert?
JPEGCOLORMODE_RAW = $0000; // no conversion (default)
JPEGCOLORMODE_RGB = $0001; // do auto conversion
TIFFTAG_JPEGTABLESMODE = 65539; // What to put in JPEGTables
JPEGTABLESMODE_QUANT = $0001; // include quantization tbls
JPEGTABLESMODE_HUFF = $0002; // include Huffman tbls
// Note: default is JPEGTABLESMODE_QUANT or JPEGTABLESMODE_HUFF
TIFFTAG_FAXFILLFUNC = 65540; // G3/G4 fill function
TIFFTAG_PIXARLOGDATAFMT = 65549; // PixarLogCodec I/O data sz
PIXARLOGDATAFMT_8BIT = 0; // regular u_char samples
PIXARLOGDATAFMT_8BITABGR = 1; // ABGR-order u_chars
PIXARLOGDATAFMT_11BITLOG = 2; // 11-bit log-encoded (raw)
PIXARLOGDATAFMT_12BITPICIO = 3; // as per PICIO (1.0==2048)
PIXARLOGDATAFMT_16BIT = 4; // signed short samples
PIXARLOGDATAFMT_FLOAT = 5; // IEEE float samples
// 65550-65556 are allocated to Oceana Matrix <dev@oceana.com>
TIFFTAG_DCSIMAGERTYPE = 65550; // imager model & filter
DCSIMAGERMODEL_M3 = 0; // M3 chip (1280 x 1024)
DCSIMAGERMODEL_M5 = 1; // M5 chip (1536 x 1024)
DCSIMAGERMODEL_M6 = 2; // M6 chip (3072 x 2048)
DCSIMAGERFILTER_IR = 0; // infrared filter
DCSIMAGERFILTER_MONO = 1; // monochrome filter
DCSIMAGERFILTER_CFA = 2; // color filter array
DCSIMAGERFILTER_OTHER = 3; // other filter
TIFFTAG_DCSINTERPMODE = 65551; // interpolation mode
DCSINTERPMODE_NORMAL = $0; // whole image, default
DCSINTERPMODE_PREVIEW = $1; // preview of image (384x256)
TIFFTAG_DCSBALANCEARRAY = 65552; // color balance values
TIFFTAG_DCSCORRECTMATRIX = 65553; // color correction values
TIFFTAG_DCSGAMMA = 65554; // gamma value
TIFFTAG_DCSTOESHOULDERPTS = 65555; // toe & shoulder points
TIFFTAG_DCSCALIBRATIONFD = 65556; // calibration file desc
// Note: quality level is on the ZLIB 1-9 scale. Default value is -1
TIFFTAG_ZIPQUALITY = 65557; // compression quality level
TIFFTAG_PIXARLOGQUALITY = 65558; // PixarLog uses same scale
// TIFF data types
TIFF_NOTYPE = 0; // placeholder
TIFF_BYTE = 1; // 8-bit unsigned integer
TIFF_ASCII = 2; // 8-bit bytes w/ last byte null
TIFF_SHORT = 3; // 16-bit unsigned integer
TIFF_LONG = 4; // 32-bit unsigned integer
TIFF_RATIONAL = 5; // 64-bit unsigned fraction
TIFF_SBYTE = 6; // 8-bit signed integer
TIFF_UNDEFINED = 7; // 8-bit untyped data
TIFF_SSHORT = 8; // 16-bit signed integer
TIFF_SLONG = 9; // 32-bit signed integer
TIFF_SRATIONAL = 10; // 64-bit signed fraction
TIFF_FLOAT = 11; // 32-bit IEEE floating point
TIFF_DOUBLE = 12; // 64-bit IEEE floating point
TIFF_BIGENDIAN = $4D4D;
TIFF_LITTLEENDIAN = $4949;
TIFF_VERSION = 42;
type
TTIFFHeader = packed record
ByteOrder: Word;
Version: Word;
FirstIFD: Cardinal;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TTIFFHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
LastPosition := Position;
Stream.ReadBuffer(Header, SizeOf(Header));
Result := (Header.ByteOrder = TIFF_BIGENDIAN) or
(Header.ByteOrder = TIFF_LITTLEENDIAN);
if Result then
begin
if Header.ByteOrder = TIFF_BIGENDIAN then
begin
Header.Version := Swap(Header.Version);
Header.FirstIFD := SwapLong(Header.FirstIFD);
end;
Result := (Header.Version = TIFF_VERSION) and (Integer(Header.FirstIFD) < (Size - Integer(LastPosition)));
end;
Position := LastPosition;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.FindTag(Tag: Cardinal; var Index: Cardinal): Boolean;
// looks through the currently loaded IFD for the entry indicated by Tag;
// returns True and the index of the entry in Index if the entry is there
// otherwise the result is False and Index undefined
// Note: The IFD is sorted so we can use a binary search here.
var
L, H, I, C: Integer;
begin
Result := False;
L := 0;
H := High(FIFD);
while L <= H do
begin
I := (L + H) shr 1;
C := Integer(FIFD[I].Tag) - Integer(Tag);
if C < 0 then L := I + 1
else
begin
H := I - 1;
if C = 0 then
begin
Result := True;
L := I;
end;
end;
end;
Index := L;
end;
//----------------------------------------------------------------------------------------------------------------------
const
DataTypeToSize: array[TIFF_NOTYPE..TIFF_SLONG] of Byte = (0, 1, 1, 2, 4, 8, 1, 1, 2, 4);
procedure TTIFFGraphic.GetValueList(Stream: TStream; Tag: Cardinal; var Values: TByteArray);
// returns the values of the IFD entry indicated by Tag
var
Index,
Value,
Shift: Cardinal;
I: Integer;
begin
Values := nil;
if FindTag(Tag, Index) and
(FIFD[Index].DataLength > 0) then
begin
// prepare value list
SetLength(Values, FIFD[Index].DataLength);
// determine whether the data fits into 4 bytes
Value := DataTypeToSize[FIFD[Index].DataType] * FIFD[Index].DataLength;
// data fits into one cardinal -> extract it
if Value <= 4 then
begin
Shift := DataTypeToSize[FIFD[Index].DataType] * 8;
Value := FIFD[Index].Offset;
for I := 0 to FIFD[Index].DataLength - 1 do
begin
case FIFD[Index].DataType of
TIFF_BYTE:
Values[I] := Byte(Value);
TIFF_SHORT,
TIFF_SSHORT:
// no byte swap needed here because values in the IFD are already swapped
// (if necessary at all)
Values[I] := Word(Value);
TIFF_LONG,
TIFF_SLONG:
Values[I] := Value;
end;
Value := Value shr Shift;
end;
end
else
begin
// data of this tag does not fit into one 32 bits value
Stream.Position := FBasePosition + FIFD[Index].Offset;
// bytes sized data can be read directly instead of looping through the array
if FIFD[Index].DataType in [TIFF_BYTE, TIFF_ASCII, TIFF_SBYTE, TIFF_UNDEFINED]
then Stream.Read(Values[0], Value)
else
begin
for I := 0 to High(Values) do
begin
Stream.Read(Value, DataTypeToSize[FIFD[Index].DataType]);
case FIFD[Index].DataType of
TIFF_BYTE:
Value := Byte(Value);
TIFF_SHORT,
TIFF_SSHORT:
begin
if ioBigEndian in FImageProperties.Options then Value := Swap(Word(Value))
else Value := Word(Value);
end;
TIFF_LONG,
TIFF_SLONG:
if ioBigEndian in FImageProperties.Options then Value := SwapLong(Value);
end;
Values[I] := Value;
end;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.GetValueList(Stream: TStream; Tag: Cardinal; var Values: TCardinalArray);
// returns the values of the IFD entry indicated by Tag
var
Index,
Value,
Shift: Cardinal;
I: Integer;
begin
Values := nil;
if FindTag(Tag, Index) and
(FIFD[Index].DataLength > 0) then
begin
// prepare value list
SetLength(Values, FIFD[Index].DataLength);
// determine whether the data fits into 4 bytes
Value := DataTypeToSize[FIFD[Index].DataType] * FIFD[Index].DataLength;
// data fits into one cardinal -> extract it
if Value <= 4 then
begin
Shift := DataTypeToSize[FIFD[Index].DataType] * 8;
Value := FIFD[Index].Offset;
for I := 0 to FIFD[Index].DataLength - 1 do
begin
case FIFD[Index].DataType of
TIFF_BYTE,
TIFF_ASCII,
TIFF_SBYTE,
TIFF_UNDEFINED:
Values[I] := Byte(Value);
TIFF_SHORT,
TIFF_SSHORT:
// no byte swap needed here because values in the IFD are already swapped
// (if necessary at all)
Values[I] := Word(Value);
TIFF_LONG,
TIFF_SLONG:
Values[I] := Value;
end;
Value := Value shr Shift;
end;
end
else
begin
// data of this tag does not fit into one 32 bits value
Stream.Position := FBasePosition + FIFD[Index].Offset;
// even bytes sized data must be read by the loop as it is expanded to cardinals
for I := 0 to High(Values) do
begin
Stream.Read(Value, DataTypeToSize[FIFD[Index].DataType]);
case FIFD[Index].DataType of
TIFF_BYTE:
Value := Byte(Value);
TIFF_SHORT,
TIFF_SSHORT:
begin
if ioBigEndian in FImageProperties.Options then Value := Swap(Word(Value))
else Value := Word(Value);
end;
TIFF_LONG,
TIFF_SLONG:
if ioBigEndian in FImageProperties.Options then Value := SwapLong(Value);
end;
Values[I] := Value;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.GetValueList(Stream: TStream; Tag: Cardinal; var Values: TFloatArray);
// returns the values of the IFD entry indicated by Tag
var
Index,
Shift,
IntValue: Cardinal;
Value: Single;
I: Integer;
IntNominator,
IntDenominator: Cardinal;
FloatNominator,
FloatDenominator: Cardinal;
begin
Values := nil;
if FindTag(Tag, Index) and
(FIFD[Index].DataLength > 0) then
begin
// prepare value list
SetLength(Values, FIFD[Index].DataLength);
// determine whether the data fits into 4 bytes
Value := DataTypeToSize[FIFD[Index].DataType] * FIFD[Index].DataLength;
// data fits into one cardinal -> extract it
if Value <= 4 then
begin
Shift := DataTypeToSize[FIFD[Index].DataType] * 8;
IntValue := FIFD[Index].Offset;
for I := 0 to FIFD[Index].DataLength - 1 do
begin
case FIFD[Index].DataType of
TIFF_BYTE,
TIFF_ASCII,
TIFF_SBYTE,
TIFF_UNDEFINED:
Values[I] := Byte(IntValue);
TIFF_SHORT,
TIFF_SSHORT:
// no byte swap needed here because values in the IFD are already swapped
// (if necessary at all)
Values[I] := Word(IntValue);
TIFF_LONG,
TIFF_SLONG:
Values[I] := IntValue;
end;
IntValue := IntValue shr Shift;
end;
end
else
begin
// data of this tag does not fit into one 32 bits value
Stream.Position := FBasePosition + FIFD[Index].Offset;
// even bytes sized data must be read by the loop as it is expanded to Single
for I := 0 to High(Values) do
begin
case FIFD[Index].DataType of
TIFF_BYTE:
begin
Stream.Read(IntValue, DataTypeToSize[FIFD[Index].DataType]);
Value := Byte(IntValue);
end;
TIFF_SHORT,
TIFF_SSHORT:
begin
Stream.Read(IntValue, DataTypeToSize[FIFD[Index].DataType]);
if ioBigEndian in FImageProperties.Options then Value := Swap(Word(IntValue))
else Value := Word(IntValue);
end;
TIFF_LONG,
TIFF_SLONG:
begin
Stream.Read(IntValue, DataTypeToSize[FIFD[Index].DataType]);
if ioBigEndian in FImageProperties.Options then Value := SwapLong(IntValue);
end;
TIFF_RATIONAL,
TIFF_SRATIONAL:
begin
Stream.ReadBuffer(FloatNominator, SizeOf(FloatNominator));
Stream.ReadBuffer(FloatDenominator, SizeOf(FloatDenominator));
if ioBigEndian in FImageProperties.Options then
begin
FloatNominator := SwapLong(Cardinal(FloatNominator));
FloatDenominator := SwapLong(Cardinal(FloatDenominator));
end;
Value := FloatNominator / FloatDenominator;
end;
TIFF_FLOAT:
begin
Stream.ReadBuffer(IntNominator, SizeOf(IntNominator));
Stream.ReadBuffer(IntDenominator, SizeOf(IntDenominator));
if ioBigEndian in FImageProperties.Options then
begin
IntNominator := SwapLong(IntNominator);
IntDenominator := SwapLong(IntDenominator);
end;
Value := IntNominator / IntDenominator;
end;
end;
Values[I] := Value;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.GetValue(Stream: TStream; Tag: Cardinal; Default: Single = 0): Single;
// returns the value of the IFD entry indicated by Tag or the default value if the entry is not there
var
Index: Cardinal;
IntNominator,
IntDenominator: Cardinal;
FloatNominator,
FloatDenominator: Cardinal;
begin
Result := Default;
if FindTag(Tag, Index) then
begin
// if the data length is > 1 then Offset is a real offset into the stream,
// otherwise it is the value itself and must be shortend depending on the data type
if FIFD[Index].DataLength = 1 then
begin
case FIFD[Index].DataType of
TIFF_BYTE:
Result := Byte(FIFD[Index].Offset);
TIFF_SHORT,
TIFF_SSHORT:
Result := Word(FIFD[Index].Offset);
TIFF_LONG,
TIFF_SLONG: // nothing to do
Result := FIFD[Index].Offset;
TIFF_RATIONAL,
TIFF_SRATIONAL:
begin
Stream.Position := FBasePosition + FIFD[Index].Offset;
Stream.ReadBuffer(FloatNominator, SizeOf(FloatNominator));
Stream.ReadBuffer(FloatDenominator, SizeOf(FloatDenominator));
if ioBigEndian in FImageProperties.Options then
begin
FloatNominator := SwapLong(Cardinal(FloatNominator));
FloatDenominator := SwapLong(Cardinal(FloatDenominator));
end;
Result := FloatNominator / FloatDenominator;
end;
TIFF_FLOAT:
begin
Stream.Position := FBasePosition + FIFD[Index].Offset;
Stream.ReadBuffer(IntNominator, SizeOf(IntNominator));
Stream.ReadBuffer(IntDenominator, SizeOf(IntDenominator));
if ioBigEndian in FImageProperties.Options then
begin
IntNominator := SwapLong(IntNominator);
IntDenominator := SwapLong(IntDenominator);
end;
Result := IntNominator / IntDenominator;
end;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.GetValue(Tag: Cardinal; Default: Cardinal = 0): Cardinal;
// returns the value of the IFD entry indicated by Tag or the default value if the entry is not there
var
Index: Cardinal;
begin
if not FindTag(Tag, Index) then Result := Default
else
begin
Result := FIFD[Index].Offset;
// if the data length is > 1 then Offset is a real offset into the stream,
// otherwise it is the value itself and must be shortend depending on the data type
if FIFD[Index].DataLength = 1 then
begin
case FIFD[Index].DataType of
TIFF_BYTE:
Result := Byte(Result);
TIFF_SHORT,
TIFF_SSHORT:
Result := Word(Result);
TIFF_LONG,
TIFF_SLONG: // nothing to do
;
else
Result := Default;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.GetValue(Tag: Cardinal; var Size: Cardinal; Default: Cardinal): Cardinal;
// Returns the value of the IFD entry indicated by Tag or the default value if the entry is not there.
// If the tag exists then also the data size is returned.
var
Index: Cardinal;
begin
if not FindTag(Tag, Index) then
begin
Result := Default;
Size := 0;
end
else
begin
Result := FIFD[Index].Offset;
Size := FIFD[Index].DataLength;
// if the data length is > 1 then Offset is a real offset into the stream,
// otherwise it is the value itself and must be shortend depending on the data type
if FIFD[Index].DataLength = 1 then
begin
case FIFD[Index].DataType of
TIFF_BYTE:
Result := Byte(Result);
TIFF_SHORT,
TIFF_SSHORT:
Result := Word(Result);
TIFF_LONG,
TIFF_SLONG: // nothing to do
;
else
Result := Default;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.SortIFD;
// Although all entries in the IFD should be sorted there are still files where this is not the case.
// Because the lookup for certain tags in the IFD uses binary search it must be made sure the IFD is
// sorted (what we do here).
//--------------- local function --------------------------------------------
procedure QuickSort(L, R: Integer);
var
I, J, M: Integer;
T: TIFDEntry;
begin
repeat
I := L;
J := R;
M := (L + R) shr 1;
repeat
while FIFD[I].Tag < FIFD[M].Tag do Inc(I);
while FIFD[J].Tag > FIFD[M].Tag do Dec(J);
if I <= J then
begin
T := FIFD[I];
FIFD[I] := FIFD[J];
FIFD[J] := T;
Inc(I);
Dec(J);
end;
until I > J;
if L < J then QuickSort(L, J);
L := I;
until I >= R;
end;
//--------------- end local functions ---------------------------------------
begin
QuickSort(0, High(FIFD));
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.SwapIFD;
// swap the member fields of all entries of the currently loaded IFD from big endian to little endian
var
I: Integer;
Size: Cardinal;
begin
for I := 0 to High(FIFD) do
with FIFD[I] do
begin
Tag := Swap(Tag);
DataType := Swap(DataType);
DataLength := SwapLong(DataLength);
// determine whether the data fits into 4 bytes
Size := DataTypeToSize[FIFD[I].DataType] * FIFD[I].DataLength;
if Size >= 4 then Offset := SwapLong(Offset)
else
case DataType of
TIFF_SHORT,
TIFF_SSHORT:
if DataLength > 1 then Offset := SwapLong(Offset)
else Offset := Swap(Word(Offset));
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.LoadFromStream(Stream: TStream);
var
IFDCount: Word;
Buffer: Pointer;
Run: PAnsiChar;
Pixels,
EncodedData,
DataPointerCopy: Pointer;
Offsets,
ByteCounts: TCardinalArray;
ColorMap: Cardinal;
StripSize: Cardinal;
Decoder: TDecoder;
// dynamically assigned handler
Deprediction: procedure(P: Pointer; Count: Cardinal);
begin
Handle := 0;
Deprediction := nil;
Decoder := nil;
// we need to keep the current stream position because all position information
// are relative to this one
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with FImageProperties do
try
// tiled images aren't supported
if ioTiled in Options then Exit;
FProgressRect := Rect(0, 0, 0, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
// read data of the first image file directory (IFD)
Stream.Position := FBasePosition + FirstIFD;
Stream.ReadBuffer(IFDCount, SizeOf(IFDCount));
if ioBigEndian in Options then IFDCount := Swap(IFDCount);
SetLength(FIFD, IFDCount);
Stream.ReadBuffer(FIFD[0], IFDCount * SizeOf(TIFDEntry));
if ioBigEndian in Options then SwapIFD;
SortIFD;
// --- read the data of the directory which are needed to actually load the image:
// data organization
GetValueList(Stream, TIFFTAG_STRIPOFFSETS, Offsets);
GetValueList(Stream, TIFFTAG_STRIPBYTECOUNTS, ByteCounts);
// retrive additional tile data if necessary
if ioTiled in Options then
begin
GetValueList(Stream, TIFFTAG_TILEOFFSETS, Offsets);
GetValueList(Stream, TIFFTAG_TILEBYTECOUNTS, ByteCounts);
end;
// determine pixelformat and setup color conversion
with ColorManager do
begin
if ioBigEndian in Options then SourceOptions := [coNeedByteSwap]
else SourceOptions := [];
SourceBitsPerSample := BitsPerSample;
if SourceBitsPerSample = 16 then TargetBitsPerSample := 8
else TargetBitsPerSample := SourceBitsPerSample;
// the JPEG lib does internally a conversion to RGB
if Compression in [ctOJPEG, ctJPEG] then
begin
Raise Exception.Create('JPeg compression disabled');
// SourceColorScheme := csBGR;
end
else SourceColorScheme := ColorScheme;
case SourceColorScheme of
csRGBA:
TargetColorScheme := csBGRA;
csRGB:
TargetColorScheme := csBGR;
csCMY,
csCMYK,
csCIELab,
csYCbCr:
TargetColorScheme := csBGR;
csIndexed:
begin
if HasAlpha then SourceColorScheme := csGA; // fake indexed images with alpha (used in EPS)
// as being grayscale with alpha
TargetColorScheme := csIndexed;
end;
else
TargetColorScheme := SourceColorScheme;
end;
SourceSamplesPerPixel := SamplesPerPixel;
if SourceColorScheme = csCMYK then TargetSamplesPerPixel := 3
else TargetSamplesPerPixel := SamplesPerPixel;
if SourceColorScheme = csCIELab then SourceOptions := SourceOptions + [coLabByteRange];
if SourceColorScheme = csGA then PixelFormat := pf8Bit
else PixelFormat := TargetPixelFormat;
end;
// now that the pixel format is set we can also set the (possibly large) image dimensions
Self.Width := Width;
Self.Height := Height;
if (Width = 0) or (Height = 0) then GraphicExError(gesInvalidImage, ['TIF/TIFF']);
FProgressRect.Right := Width;
if ColorManager.TargetColorScheme in [csIndexed, csG, csGA] then
begin
// load palette data and build palette
if ColorManager.TargetColorScheme = csIndexed then
begin
ColorMap := GetValue(TIFFTAG_COLORMAP, StripSize, 0);
if StripSize > 0 then
begin
Stream.Position := FBasePosition + ColorMap;
// number of palette entries is also given by the color map tag
// (3 components each (r,g,b) and two bytes per component)
Stream.ReadBuffer(FPalette[0] , 2 * StripSize);
Palette := ColorManager.CreateColorPalette([@FPalette[0], @FPalette[StripSize div 3],
@FPalette[2 * StripSize div 3]], pfPlane16Triple, StripSize, False);
end;
end
else Palette := ColorManager.CreateGrayScalePalette(ioMinIsWhite in Options);
end
else
if ColorManager.SourceColorScheme = csYCbCr then
ColorManager.SetYCbCrParameters(FYCbCrCoefficients, YCbCrSubSampling[0], YCbCrSubSampling[1]);
// intermediate buffer for data
BytesPerLine := (BitsPerPixel * Width + 7) div 8;
// determine prediction scheme
if Compression <> ctNone then
begin
// Prediction without compression makes no sense at all (as it is to improve
// compression ratios). Appearently there are image which are uncompressed but still
// have a prediction scheme set. Hence we must check for it.
case Predictor of
PREDICTION_HORZ_DIFFERENCING: // currently only one prediction scheme is defined
case SamplesPerPixel of
4:
Deprediction := Depredict4;
3:
Deprediction := Depredict3;
else
Deprediction := Depredict1;
end;
end;
end;
// create decompressor for the image
case Compression of
ctNone:
;
{$ifdef UseLZW}
ctLZW:
Decoder := TTIFFLZWDecoder.Create;
{$endif}
ctPackedBits:
Decoder := TPackbitsRLEDecoder.Create;
ctFaxRLE,
ctFaxRLEW:
Decoder := TCCITTMHDecoder.Create(GetValue(TIFFTAG_GROUP3OPTIONS),
ioReversed in Options,
Compression = ctFaxRLEW,
Width);
ctFax3:
Decoder := TCCITTFax3Decoder.Create(GetValue(TIFFTAG_GROUP3OPTIONS), ioReversed in Options, False, Width);
ctJPEG:
begin
// some extra work is needed for JPEG
// GetValueList(Stream, TIFFTAG_JPEGTABLES, JPEGTables);
// Decoder := TTIFFJPEGDecoder.Create(@FImageProperties);
Raise Exception.Create('JPeg compression disabled');
end;
ctThunderscan:
Decoder := TThunderDecoder.Create(Width);
ctLZ77:
Decoder := TLZ77Decoder.Create(Z_PARTIAL_FLUSH, True);
else
{
COMPRESSION_OJPEG,
COMPRESSION_CCITTFAX4
COMPRESSION_NEXT
COMPRESSION_IT8CTPAD
COMPRESSION_IT8LW
COMPRESSION_IT8MP
COMPRESSION_IT8BL
COMPRESSION_PIXARFILM
COMPRESSION_PIXARLOG
COMPRESSION_DCS
COMPRESSION_JBIG}
GraphicExError(gesUnsupportedFeature, [gesCompressionScheme, 'TIF/TIFF']);
end;
if Assigned(Decoder) then Decoder.DecodeInit;
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
// go for each strip in the image (which might contain more than one line)
CurrentRow := 0;
CurrentStrip := 0;
StripCount := Length(Offsets);
while CurrentStrip < StripCount do
begin
Stream.Position := FBasePosition + Offsets[CurrentStrip];
if CurrentStrip < Length(RowsPerStrip) then StripSize := BytesPerLine * RowsPerStrip[CurrentStrip]
else StripSize := BytesPerLine * RowsPerStrip[High(RowsPerStrip)];
GetMem(Buffer, StripSize);
Run := Buffer;
try
// decompress strip if necessary
if Assigned(Decoder) then
begin
GetMem(EncodedData, ByteCounts[CurrentStrip]);
try
DataPointerCopy := EncodedData;
Stream.Read(EncodedData^, ByteCounts[CurrentStrip]);
// need pointer copies here because they could get modified
// while decoding
Decoder.Decode(DataPointerCopy, Pointer(Run), ByteCounts[CurrentStrip], StripSize);
finally
if Assigned(EncodedData) then FreeMem(EncodedData);
end;
end
else
begin
Stream.Read(Buffer^, StripSize);
end;
Run := Buffer;
// go for each line (row) in the strip
while (CurrentRow < Height) and ((Run - Buffer) < Integer(StripSize)) do
begin
Pixels := ScanLine[CurrentRow];
// depredict strip if necessary
if Assigned(Deprediction) then Deprediction(Run, Width - 1);
// any color conversion comes last
ColorManager.ConvertRow([Run], Pixels, Width, $FF);
Inc(PAnsiChar(Run), BytesPerLine);
Inc(CurrentRow);
Progress(Self, psRunning, MulDiv(CurrentRow, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
if Assigned(Buffer) then FreeMem(Buffer);
end;
Inc(CurrentStrip);
end;
finally
Progress(Self, psEnding, 0, False, FProgressRect, '');
if Assigned(Decoder) then Decoder.DecodeEnd;
Decoder.Free;
end;
end
else GraphicExError(gesInvalidImage, ['TIF/TIFF']);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTIFFGraphic.SaveToStream(Stream: TStream);
begin
end;
//----------------------------------------------------------------------------------------------------------------------
function TTIFFGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
// Reads all relevant TIF properties of the image of index ImageIndex (zero based).
// Returns True if the image ImageIndex could be read, otherwise False.
const
PhotometricToColorScheme: array[PHOTOMETRIC_MINISWHITE..PHOTOMETRIC_CIELAB] of TColorScheme = (
csG,
csG,
csRGBA,
csIndexed,
csUnknown,
csCMYK,
csYCbCr,
csUnknown,
csCIELab
);
var
IFDCount: Word;
ExtraSamples: TCardinalArray;
PhotometricInterpretation: Byte;
TIFCompression: Word;
Index: Cardinal;
IFDOffset: Cardinal;
Header: TTIFFHeader;
LocalBitsPerSample: TCardinalArray;
begin
// clear image properties
Result := inherited ReadImageProperties(Stream, ImageIndex);
with FImageProperties do
begin
// rewind stream to header position
Stream.Position := FBasePosition;
Stream.ReadBuffer(Header, SizeOf(Header));
if Header.ByteOrder = TIFF_BIGENDIAN then
begin
Options := Options + [ioBigEndian];
Header.Version := Swap(Header.Version);
Header.FirstIFD := SwapLong(Header.FirstIFD);
end;
Version := Header.Version;
FirstIFD := Header.FirstIFD;
if Version = TIFF_VERSION then
begin
IFDOffset := Header.FirstIFD;
// advance to next IFD until we have the desired one
repeat
Stream.Position := FBasePosition + IFDOffset;
// number of entries in this directory
Stream.ReadBuffer(IFDCount, SizeOf(IFDCount));
if Header.ByteOrder = TIFF_BIGENDIAN then IFDCount := Swap(IFDCount);
// if we already have the desired image then get out of here
if ImageIndex = 0 then Break;
Dec(ImageIndex);
// advance to offset for next IFD
Stream.Seek(IFDCount * SizeOf(TIFDEntry), soFromCurrent);
Stream.ReadBuffer(IFDOffset, SizeOf(IFDOffset));
// no further image available, but the required index is still not found
if IFDOffset = 0 then Exit;
until False;
SetLength(FIFD, IFDCount);
Stream.ReadBuffer(FIFD[0], IFDCount * SizeOf(TIFDEntry));
if Header.ByteOrder = TIFF_BIGENDIAN then SwapIFD;
SortIFD;
Width := GetValue(TIFFTAG_IMAGEWIDTH);
Height := GetValue(TIFFTAG_IMAGELENGTH);
if (Width = 0) or (Height = 0) then Exit;
// data organization
GetValueList(Stream, TIFFTAG_ROWSPERSTRIP, RowsPerStrip);
// some images rely on the default size ($FFFFFFFF) if only one stripe is in the image,
// make sure there's a valid value also in this case
if (Length(RowsPerStrip) = 0) or (RowsPerStrip[0] = $FFFFFFFF) then
begin
SetLength(RowsPerStrip, 1);
RowsPerStrip[0] := Height;
end;
// number of color components per pixel (1 for b&w, 16 and 256 colors, 3 for RGB, 4 for CMYK etc.)
SamplesPerPixel := GetValue(TIFFTAG_SAMPLESPERPIXEL, 1);
// number of bits per color component
GetValueList(Stream, TIFFTAG_BITSPERSAMPLE, LocalBitsPerSample);
if Length(LocalBitsPerSample) = 0 then BitsPerSample := 1
else BitsPerSample := LocalBitsPerSample[0];
// determine whether image is tiled and retrive tile data if necessary
TileWidth := GetValue(TIFFTAG_TILEWIDTH, 0);
TileLength := GetValue(TIFFTAG_TILELENGTH, 0);
if (TileWidth > 0) and (TileLength > 0) then Include(Options, ioTiled);
// photometric interpretation determines the color space
PhotometricInterpretation := GetValue(TIFFTAG_PHOTOMETRIC);
// type of extra information for additional samples per pixel
GetValueList(Stream, TIFFTAG_EXTRASAMPLES, ExtraSamples);
// determine whether extra samples must be considered
HasAlpha := Length(ExtraSamples) > 0;
// if any of the extra sample contains an invalid value then consider
// it as being not existant to avoid wrong interpretation for badly
// written images
if HasAlpha then
begin
for Index := 0 to High(ExtraSamples) do
if ExtraSamples[Index] > EXTRASAMPLE_UNASSALPHA then
begin
HasAlpha := False;
Break;
end;
end;
// currently all bits per sample values are equal
BitsPerPixel := BitsPerSample * SamplesPerPixel;
// create decompressor for the image
TIFCompression := GetValue(TIFFTAG_COMPRESSION);
case TIFCompression of
COMPRESSION_NONE:
Compression := ctNone;
COMPRESSION_LZW:
Compression := ctLZW;
COMPRESSION_PACKBITS:
Compression := ctPackedBits;
COMPRESSION_CCITTRLE:
Compression := ctFaxRLE;
COMPRESSION_CCITTRLEW:
Compression := ctFaxRLEW;
COMPRESSION_CCITTFAX3:
Compression := ctFax3;
COMPRESSION_OJPEG:
Compression := ctOJPEG;
COMPRESSION_JPEG:
Compression := ctJPEG;
COMPRESSION_CCITTFAX4:
Compression := ctFax4;
COMPRESSION_NEXT:
Compression := ctNext;
COMPRESSION_THUNDERSCAN:
Compression := ctThunderscan;
COMPRESSION_IT8CTPAD:
Compression := ctIT8CTPAD;
COMPRESSION_IT8LW:
Compression := ctIT8LW;
COMPRESSION_IT8MP:
Compression := ctIT8MP;
COMPRESSION_IT8BL:
Compression := ctIT8BL;
COMPRESSION_PIXARFILM:
Compression := ctPixarFilm;
COMPRESSION_PIXARLOG: // also a LZ77 clone
Compression := ctPixarLog;
COMPRESSION_ADOBE_DEFLATE,
COMPRESSION_DEFLATE:
Compression := ctLZ77;
COMPRESSION_DCS:
Compression := ctDCS;
COMPRESSION_JBIG:
Compression := ctJBIG;
else
Compression := ctUnknown;
end;
if PhotometricInterpretation in [PHOTOMETRIC_MINISWHITE..PHOTOMETRIC_CIELAB] then
begin
ColorScheme := PhotometricToColorScheme[PhotometricInterpretation];
if (PhotometricInterpretation = PHOTOMETRIC_RGB) and (SamplesPerPixel < 4) then ColorScheme := csRGB;
if PhotometricInterpretation = PHOTOMETRIC_MINISWHITE then Include(Options, ioMinIsWhite);
// extra work necessary for YCbCr
if PhotometricInterpretation = PHOTOMETRIC_YCBCR then
begin
if FindTag(TIFFTAG_YCBCRSUBSAMPLING, Index)
then GetValueList(Stream, TIFFTAG_YCBCRSUBSAMPLING, YCbCrSubSampling)
else
begin
// initialize default values if nothing is given in the file
SetLength(YCbCrSubSampling, 2);
YCbCrSubSampling[0] := 2;
YCbCrSubSampling[1] := 2;
end;
if FindTag(TIFFTAG_YCBCRPOSITIONING, Index) then FYCbCrPositioning := GetValue(TIFFTAG_YCBCRPOSITIONING)
else FYCbCrPositioning := YCBCRPOSITION_CENTERED;
if FindTag(TIFFTAG_YCBCRCOEFFICIENTS, Index)
then GetValueList(Stream, TIFFTAG_YCBCRCOEFFICIENTS, FYCbCrCoefficients)
else
begin
// defaults are from CCIR recommendation 601-1
SetLength(FYCbCrCoefficients, 3);
FYCbCrCoefficients[0] := 0.299;
FYCbCrCoefficients[1] := 0.587;
FYCbCrCoefficients[2] := 0.114;
end;
end;
end
else ColorScheme := csUnknown;
JPEGColorMode := GetValue(TIFFTAG_JPEGCOLORMODE, JPEGCOLORMODE_RAW);
JPEGTablesMode := GetValue(TIFFTAG_JPEGTABLESMODE, JPEGTABLESMODE_QUANT or JPEGTABLESMODE_HUFF);
PlanarConfig := GetValue(TIFFTAG_PLANARCONFIG);
// other image properties
XResolution := GetValue(Stream, TIFFTAG_XRESOLUTION);
YResolution := GetValue(Stream, TIFFTAG_YRESOLUTION);
if GetValue(TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH) = RESUNIT_CENTIMETER then
begin
// Resolution is given in centimeters.
// Although I personally prefer the metric system over the old english one :-)
// I still convert to inches because it is an unwritten rule to give image resolutions in dpi.
XResolution := XResolution * 2.54;
YResolution := YResolution * 2.54;
end;
// determine prediction scheme
Predictor := GetValue(TIFFTAG_PREDICTOR);
// determine fill order in bytes
if GetValue(TIFFTAG_FILLORDER, FILLORDER_MSB2LSB) = FILLORDER_LSB2MSB then Include(Options, ioReversed);
// finally show that we found and read an image
Result := True;
end;
end;
end;
//----------------- TEPSGraphic ----------------------------------------------------------------------------------------
{$ifdef EPSGraphic}
// Note: This EPS implementation does only read embedded pixel graphics in TIF format (preview).
// Credits to:
// Olaf Stieleke
// Torsten Pohlmeyer
// CPS Krohn GmbH
// for providing the base information about how to read the preview image.
type
TEPSHeader = packed record
Code: Cardinal; // alway $C6D3D0C5, if not there then this is not an EPS or it is not a binary EPS
PSStart, // Offset PostScript-Code
PSLen, // length of PostScript-Code
MetaPos, // position of a WMF
MetaLen, // length of a WMF
TiffPos, // position of TIFF (preview images should be either WMF or TIF but not both)
TiffLen: Integer; // length of the TIFF
Checksum: SmallInt;
end;
//----------------------------------------------------------------------------------------------------------------------
function TEPSGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TEPSHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
LastPosition := Position;
Stream.ReadBuffer(Header, SizeOf(Header));
Result := (Header.Code = $C6D3D0C5) and
(Header.TiffPos > Integer(LastPosition) + SizeOf(Header)) and
(Header.TiffLen > 0);
Position := LastPosition;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TEPSGraphic.LoadFromStream(Stream: TStream);
var
Header: TEPSHeader;
begin
Stream.ReadBuffer(Header, SizeOf(Header));
if Header.Code <> $C6D3D0C5 then GraphicExError(gesInvalidImage, ['EPS']);
Stream.Seek(Header.TiffPos - SizeOf(Header), soFromCurrent);
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
function TEPSGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
end;
{$endif} // EPSGraphic
{$endif} // TIFFGraphic
//----------------- TTargaGraphic --------------------------------------------------------------------------------------
{$ifdef TargaGraphic}
// FILE STRUCTURE FOR THE ORIGINAL TRUEVISION TGA FILE
// FIELD 1: NUMBER OF CHARACTERS IN ID FIELD (1 BYTES)
// FIELD 2: COLOR MAP TYPE (1 BYTES)
// FIELD 3: IMAGE TYPE CODE (1 BYTES)
// = 0 NO IMAGE DATA INCLUDED
// = 1 UNCOMPRESSED, COLOR-MAPPED IMAGE
// = 2 UNCOMPRESSED, TRUE-COLOR IMAGE
// = 3 UNCOMPRESSED, BLACK AND WHITE IMAGE (black and white is actually grayscale)
// = 9 RUN-LENGTH ENCODED COLOR-MAPPED IMAGE
// = 10 RUN-LENGTH ENCODED TRUE-COLOR IMAGE
// = 11 RUN-LENGTH ENCODED BLACK AND WHITE IMAGE
// FIELD 4: COLOR MAP SPECIFICATION (5 BYTES)
// 4.1: COLOR MAP ORIGIN (2 BYTES)
// 4.2: COLOR MAP LENGTH (2 BYTES)
// 4.3: COLOR MAP ENTRY SIZE (1 BYTES)
// FIELD 5:IMAGE SPECIFICATION (10 BYTES)
// 5.1: X-ORIGIN OF IMAGE (2 BYTES)
// 5.2: Y-ORIGIN OF IMAGE (2 BYTES)
// 5.3: WIDTH OF IMAGE (2 BYTES)
// 5.4: HEIGHT OF IMAGE (2 BYTES)
// 5.5: IMAGE PIXEL SIZE (1 BYTE)
// 5.6: IMAGE DESCRIPTOR BYTE (1 BYTE)
// bit 0..3: attribute bits per pixel
// bit 4..5: image orientation:
// 0: bottom left
// 1: bottom right
// 2: top left
// 3: top right
// bit 6..7: interleaved flag
// 0: two way (even-odd) interleave (e.g. IBM Graphics Card Adapter), obsolete
// 1: four way interleave (e.g. AT&T 6300 High Resolution), obsolete
// FIELD 6: IMAGE ID FIELD (LENGTH SPECIFIED BY FIELD 1)
// FIELD 7: COLOR MAP DATA (BIT WIDTH SPECIFIED BY FIELD 4.3 AND
// NUMBER OF COLOR MAP ENTRIES SPECIFIED IN FIELD 4.2)
// FIELD 8: IMAGE DATA FIELD (WIDTH AND HEIGHT SPECIFIED IN FIELD 5.3 AND 5.4)
const
TARGA_NO_COLORMAP = 0;
TARGA_COLORMAP = 1;
TARGA_EMPTY_IMAGE = 0;
TARGA_INDEXED_IMAGE = 1;
TARGA_TRUECOLOR_IMAGE = 2;
TARGA_BW_IMAGE = 3;
TARGA_INDEXED_RLE_IMAGE = 9;
TARGA_TRUECOLOR_RLE_IMAGE = 10;
TARGA_BW_RLE_IMAGE = 11;
type
TTargaHeader = packed record
IDLength,
ColorMapType,
ImageType: Byte;
ColorMapOrigin,
ColorMapSize: Word;
ColorMapEntrySize: Byte;
XOrigin,
YOrigin,
Width,
Height: Word;
PixelSize: Byte;
ImageDescriptor: Byte;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTargaGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TTargaHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
ReadBuffer(Header, SizeOf(Header));
// Targa images are hard to determine because there is no magic id or something like that.
// Hence all we can do is to check if all values from the header are within correct limits.
Result := (Header.ImageType in [TARGA_EMPTY_IMAGE, TARGA_INDEXED_IMAGE, TARGA_TRUECOLOR_IMAGE, TARGA_BW_IMAGE,
TARGA_INDEXED_RLE_IMAGE, TARGA_TRUECOLOR_RLE_IMAGE, TARGA_BW_RLE_IMAGE]) and
(Header.ColorMapType in [TARGA_NO_COLORMAP, TARGA_COLORMAP]) and
(Header.ColorMapEntrySize in [15, 16, 24, 32]) and
(Header.PixelSize in [8, 15, 16, 24, 32]);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTargaGraphic.LoadFromStream(Stream: TStream);
var
Run,
RLEBuffer: PAnsiChar;
I: Integer;
LineSize: Integer;
LineBuffer: Pointer;
ReadLength: Integer;
LogPalette: TMaxLogPalette;
Color16: Word;
Header: TTargaHeader;
FlipV: Boolean;
Decoder: TTargaRLEDecoder;
begin
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
with Stream, FImageProperties do
begin
Stream.Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
Stream.Read(Header, SizeOf(Header));
FlipV := (Header.ImageDescriptor and $20) <> 0;
Header.ImageDescriptor := Header.ImageDescriptor and $F;
// skip image ID
Seek(Header.IDLength, soFromCurrent);
with ColorManager do
begin
SourceSamplesPerPixel := SamplesPerPixel;
TargetSamplesPerPixel := SamplesPerPixel;
SourceColorScheme := ColorScheme;
SourceOptions := [];
TargetColorScheme := csBGR;
SourceBitsPerSample := BitsPerSample;
TargetBitsPerSample := BitsPerSample;
PixelFormat := TargetPixelFormat;
end;
if (Header.ColorMapType = TARGA_COLORMAP) or
(Header.ImageType in [TARGA_BW_IMAGE, TARGA_BW_RLE_IMAGE]) then
begin
if Header.ImageType in [TARGA_BW_IMAGE, TARGA_BW_RLE_IMAGE] then
Palette := ColorManager.CreateGrayscalePalette(False)
else
begin
LineSize := (Header.ColorMapEntrySize div 8) * Header.ColorMapSize;
GetMem(LineBuffer, LineSize);
try
ReadBuffer(LineBuffer^, LineSize);
case Header.ColorMapEntrySize of
32:
Palette := ColorManager.CreateColorPalette([LineBuffer], pfInterlaced8Quad, Header.ColorMapSize, True);
24:
Palette := ColorManager.CreateColorPalette([LineBuffer], pfInterlaced8Triple, Header.ColorMapSize, True);
else
with LogPalette do
begin
// read palette entries and create a palette
ZeroMemory(@LogPalette, SizeOf(LogPalette));
palVersion := $300;
palNumEntries := Header.ColorMapSize;
// 15 and 16 bits per color map entry (handle both like 555 color format
// but make 8 bit from 5 bit per color component)
for I := 0 to Header.ColorMapSize - 1 do
begin
Stream.Read(Color16, 2);
palPalEntry[I].peBlue := (Color16 and $1F) shl 3;
palPalEntry[I].peGreen := (Color16 and $3E0) shr 2;
palPalEntry[I].peRed := (Color16 and $7C00) shr 7;
end;
Palette := CreatePalette(PLogPalette(@LogPalette)^);
end;
end;
finally
if Assigned(LineBuffer) then FreeMem(LineBuffer);
end;
end;
end;
Self.Width := Header.Width;
Self.Height := Header.Height;
LineSize := Width * (Header.PixelSize div 8);
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
case Header.ImageType of
TARGA_EMPTY_IMAGE: // nothing to do here
;
TARGA_BW_IMAGE,
TARGA_INDEXED_IMAGE,
TARGA_TRUECOLOR_IMAGE:
begin
for I := 0 to Height - 1 do
begin
if FlipV then LineBuffer := ScanLine[I]
else LineBuffer := ScanLine[Header.Height - (I + 1)];
ReadBuffer(LineBuffer^, LineSize);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
TARGA_BW_RLE_IMAGE,
TARGA_INDEXED_RLE_IMAGE,
TARGA_TRUECOLOR_RLE_IMAGE:
begin
RLEBuffer := nil;
Decoder := TTargaRLEDecoder.Create(Header.PixelSize);
try
GetMem(RLEBuffer, 2 * LineSize);
for I := 0 to Height - 1 do
begin
if FlipV then LineBuffer := ScanLine[I]
else LineBuffer := ScanLine[Header.Height - (I + 1)];
ReadLength := Stream.Read(RLEBuffer^, 2 * LineSize);
Run := RLEBuffer;
Decoder.Decode(Pointer(Run), LineBuffer, 2 * LineSize, Width);
Stream.Position := Stream.Position - ReadLength + (Run - RLEBuffer);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
if Assigned(RLEBuffer) then FreeMem(RLEBuffer);
Decoder.Free;
end;
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TTargaGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TTargaHeader;
begin
inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
ReadBuffer(Header, SizeOf(Header));
Header.ImageDescriptor := Header.ImageDescriptor and $F;
Width := Header.Width;
Height := Header.Height;
BitsPerSample := 8;
case Header.PixelSize of
8:
begin
if Header.ImageType in [TARGA_BW_IMAGE, TARGA_BW_RLE_IMAGE] then ColorScheme := csG
else ColorScheme := csIndexed;
SamplesPerPixel := 1;
end;
15,
16: // actually, 16 bit are meant being 15 bit
begin
ColorScheme := csRGB;
BitsPerSample := 5;
SamplesPerPixel := 3;
end;
24:
begin
ColorScheme := csRGB;
SamplesPerPixel := 3;
end;
32:
begin
ColorScheme := csRGBA;
SamplesPerPixel := 4;
end;
end;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
if Header.ImageType in [TARGA_BW_RLE_IMAGE, TARGA_INDEXED_RLE_IMAGE, TARGA_TRUECOLOR_RLE_IMAGE]
then Compression := ctRLE
else Compression := ctNone;
Width := Header.Width;
Height := Header.Height;
Result := True;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTargaGraphic.SaveToStream(Stream: TStream);
begin
SaveToStream(Stream, True);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TTargaGraphic.SaveToStream(Stream: TStream; Compressed: Boolean);
// The format of the image to be saved depends on the current properties of the bitmap not
// on the values which may be set in the header during a former load.
var
RLEBuffer: Pointer;
I: Integer;
LineSize: Integer;
WriteLength: Cardinal;
LogPalette: TMaxLogPalette;
BPP: Byte;
Header: TTargaHeader;
Encoder: TTargaRLEDecoder;
begin
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
// prepare color depth
case PixelFormat of
pf1Bit,
pf4Bit: // Note: 1 bit and 4 bits per pixel are not supported in the Targa format, an image
// with one of these pixel formats is implicitly converted to 256 colors.
begin
PixelFormat := pf8Bit;
BPP := 1;
end;
pf8Bit:
BPP := 1;
pf15Bit,
pf16Bit:
BPP := 2;
pf24Bit:
BPP := 3;
pf32Bit:
BPP := 4;
else
BPP := GetDeviceCaps(Canvas.Handle, BITSPIXEL) div 8;
end;
if not Empty then
begin
with Header do
begin
IDLength := 0;
if BPP = 1 then ColorMapType := 1
else ColorMapType := 0;
if not Compressed then
// can't distinct between a B&W and an color indexed image here, so I use always the latter
if BPP = 1 then ImageType := TARGA_INDEXED_IMAGE
else ImageType := TARGA_TRUECOLOR_IMAGE
else
if BPP = 1 then ImageType := TARGA_INDEXED_RLE_IMAGE
else ImageType := TARGA_TRUECOLOR_RLE_IMAGE;
ColorMapOrigin := 0;
// always save entire palette
ColorMapSize := 256;
// always save complete color information
ColorMapEntrySize := 24;
XOrigin := 0;
YOrigin := 0;
Width := Self.Width;
Height := Self.Height;
PixelSize := 8 * BPP;
// if the image is a bottom-up DIB then indicate this in the image descriptor
if Cardinal(Scanline[0]) > Cardinal(Scanline[1]) then ImageDescriptor := $20
else ImageDescriptor := 0;
end;
Stream.Write(Header, SizeOf(Header));
// store color palette if necessary
if Header.ColorMapType = 1 then
with LogPalette do
begin
// read palette entries
GetPaletteEntries(Palette, 0, 256, palPalEntry);
for I := 0 to 255 do
begin
Stream.Write(palPalEntry[I].peBlue, 1);
Stream.Write(palPalEntry[I].peGreen, 1);
Stream.Write(palPalEntry[I].peRed, 1);
end;
end;
LineSize := Width * (Header.PixelSize div 8);
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
// finally write image data
if Compressed then
begin
RLEBuffer := nil;
Encoder := TTargaRLEDecoder.Create(Header.PixelSize);
try
GetMem(RLEBuffer, 2 * LineSize);
for I := 0 to Height - 1 do
begin
Encoder.Encode(ScanLine[I], RLEBuffer, Width, WriteLength);
Stream.WriteBuffer(RLEBuffer^, WriteLength);
Progress(Self, psRunning, 0, False, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
if Assigned(RLEBuffer) then FreeMem(RLEBuffer);
Encoder.Free;
end;
end
else
begin
for I := 0 to Height - 1 do
begin
Stream.WriteBuffer(ScanLine[I]^, LineSize);
Progress(Self, psRunning, 0, False, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
{$endif} // TargaGraphic
//----------------- TPCXGraphic ----------------------------------------------------------------------------------------
{$ifdef PCXGraphic}
type
TPCXHeader = record
FileID: Byte; // $0A for PCX files, $CD for SCR files
Version: Byte; // 0: version 2.5; 2: 2.8 with palette; 3: 2.8 w/o palette; 5: version 3
Encoding: Byte; // 0: uncompressed; 1: RLE encoded
BitsPerPixel: Byte;
XMin,
YMin,
XMax,
YMax, // coordinates of the corners of the image
HRes, // horizontal resolution in dpi
VRes: Word; // vertical resolution in dpi
ColorMap: array[0..15] of TRGB; // color table
Reserved,
ColorPlanes: Byte; // color planes (at most 4)
BytesPerLine, // number of bytes of one line of one plane
PaletteType: Word; // 1: color or b&w; 2: gray scale
Fill: array[0..57] of Byte;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPCXGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TPCXHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
// Small bug in graphic ex first check and after read, wrong
// Result := (Header.FileID in [$0A, $0C]) and
// (Header.Version in [0, 2, 3, 5]) and
// (Header.Encoding in [0, 1]);
// ReadBuffer(Header, SizeOf(Header));
// Corrent, first read then check
ReadBuffer(Header, SizeOf(Header));
Result := (Header.FileID in [$0A, $0C]) and
(Header.Version in [0, 2, 3, 5]) and
(Header.Encoding in [0, 1]);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPCXGraphic.LoadFromStream(Stream: TStream);
var
Header: TPCXHeader;
//--------------- local functions -------------------------------------------
procedure MakePalette;
var
PCXPalette: array[0..255] of TRGB;
OldPos: Integer;
Marker: Byte;
begin
if (Header.Version <> 3) or (PixelFormat = pf1Bit) then
begin
case PixelFormat of
pf1Bit:
Palette := ColorManager.CreateGrayScalePalette(False);
pf4Bit:
with Header do
begin
if paletteType = 2 then Palette := ColorManager.CreateGrayScalePalette(False)
else Palette := ColorManager.CreateColorPalette([@ColorMap], pfInterlaced8Triple, 16, False);
end;
pf8Bit:
begin
OldPos := Stream.Position;
// 256 colors with 3 components plus one marker byte
Stream.Position := Stream.Size - 769;
Stream.Read(Marker, 1);
if Marker <> $0C then
begin
// palette ID is wrong, perhaps gray scale?
if Header.PaletteType = 2 then Palette := ColorManager.CreateGrayScalePalette(False)
else ; // ignore palette
end
else
begin
Stream.Read(PCXPalette[0], 768);
Palette := ColorManager.CreateColorPalette([@PCXPalette], pfInterlaced8Triple, 256, False);
end;
Stream.Position := OldPos;
end;
end;
end
else
begin
// version 2.8 without palette information, just use the system palette
// 256 colors will not be correct with this assignment...
Palette := SystemPalette16;
end;
end;
//--------------- end local functions ---------------------------------------
var
PCXSize,
Size: Cardinal;
RawBuffer,
DecodeBuffer: Pointer;
Run: PByte;
Plane1,
Plane2,
Plane3,
Plane4: PByte;
Value,
Mask: Byte;
I, J: Integer;
Line: PByte;
Increment: Cardinal;
NewPixelFormat: TPixelFormat;
begin
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
Stream.Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
Stream.Read(Header, SizeOf(Header));
PCXSize := Stream.Size - Stream.Position;
with Header, FImageProperties do
begin
if not (FileID in [$0A, $CD]) then GraphicExError(gesInvalidImage, ['PCX or SCR']);
with ColorManager do
begin
SourceColorScheme := ColorScheme;
SourceBitsPerSample := BitsPerSample;
SourceSamplesPerPixel := SamplesPerPixel;
if ColorScheme = csIndexed then TargetColorScheme := csIndexed
else TargetColorScheme := csBGR;
if BitsPerPixel = 2 then TargetBitsPerSample := 4
else TargetBitsPerSample := BitsPerSample;
// Note: pixel depths of 2 and 4 bits may not be used with more than one plane
// otherwise the image will not show up correctly
TargetSamplesPerPixel := SamplesPerPixel;
end;
NewPixelFormat := ColorManager.TargetPixelFormat;
if NewPixelFormat = pfCustom then
begin
// there can be a special case comprising 4 planes each with 1 bit
if (SamplesPerPixel = 4) and (BitsPerPixel = 4) then NewPixelFormat := pf4Bit
else GraphicExError(gesInvalidColorFormat, ['PCX']);
end;
PixelFormat := NewPixelFormat;
// 256 colors palette is appended to the actual PCX data
if PixelFormat = pf8Bit then Dec(PCXSize, 769);
if PixelFormat <> pf24Bit then MakePalette;
Self.Width := Width;
Self.Height := Height;
// adjust alignment of line
Increment := SamplesPerPixel * Header.BytesPerLine;
// allocate pixel data buffer and decode data if necessary
if Compression = ctRLE then
begin
Size := Increment * Height;
GetMem(DecodeBuffer, Size);
GetMem(RawBuffer, PCXSize);
try
Stream.ReadBuffer(RawBuffer^, PCXSize);
with TPCXRLEDecoder.Create do
try
Decode(RawBuffer, DecodeBuffer, PCXSize, Size);
finally
Free;
end;
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end
else
begin
GetMem(DecodeBuffer, PCXSize);
Stream.ReadBuffer(DecodeBuffer^, PCXSize);
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
try
Run := DecodeBuffer;
if (SamplesPerPixel = 4) and (BitsPerPixel = 4) then
begin
// 4 planes with one bit
for I := 0 to Height - 1 do
begin
Plane1 := Run;
PAnsiChar(Plane2) := PAnsiChar(Run) + Increment div 4;
PAnsiChar(Plane3) := PAnsiChar(Run) + 2 * (Increment div 4);
PAnsiChar(Plane4) := PAnsiChar(Run) + 3 * (Increment div 4);
Line := ScanLine[I];
// number of bytes to write
Size := (Width * BitsPerPixel + 7) div 8;
Mask := 0;
while Size > 0 do
begin
Value := 0;
for J := 0 to 1 do
asm
MOV AL, [Value]
MOV EDX, [Plane4] // take the 4 MSBs from the 4 runs and build a nibble
SHL BYTE PTR [EDX], 1 // read MSB and prepare next run at the same time
RCL AL, 1 // MSB from previous shift is in CF -> move it to AL
MOV EDX, [Plane3] // now do the same with the other three runs
SHL BYTE PTR [EDX], 1
RCL AL, 1
MOV EDX, [Plane2]
SHL BYTE PTR [EDX], 1
RCL AL, 1
MOV EDX, [Plane1]
SHL BYTE PTR [EDX], 1
RCL AL, 1
MOV [Value], AL
end;
Line^ := Value;
Inc(Line);
Dec(Size);
// two runs above (to construct two nibbles -> one byte), now update marker
// to know when to switch to next byte in the planes
Mask := (Mask + 2) mod 8;
if Mask = 0 then
begin
Inc(Plane1);
Inc(Plane2);
Inc(Plane3);
Inc(Plane4);
end;
end;
Inc(Run, Increment);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end
else
if PixelFormat = pf24Bit then
begin
// true color
for I := 0 to Height - 1 do
begin
Line := ScanLine[I];
Plane1 := Run;
PAnsiChar(Plane2) := PAnsiChar(Run) + Increment div 3;
PAnsiChar(Plane3) := PAnsiChar(Run) + 2 * (Increment div 3);
ColorManager.ConvertRow([Plane1, Plane2, Plane3], Line, Width, $FF);
Inc(Run, Increment);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end
end
else
begin
// other indexed formats
for I := 0 to Height - 1 do
begin
Line := ScanLine[I];
ColorManager.ConvertRow([Run], Line, Width, $FF);
Inc(Run, Increment);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
finally
if Assigned(DecodeBuffer) then FreeMem(DecodeBuffer);
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPCXGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TPCXHeader;
begin
Result := inherited ReadImageProperties(Stream, 0);
with Stream do
begin
ReadBuffer(Header, SizeOf(Header));
with FImageProperties do
begin
if Header.FileID in [$0A, $CD] then
begin
Width := Header.XMax - Header.XMin + 1;
Height := Header.YMax - Header.YMin + 1;
SamplesPerPixel := Header.ColorPlanes;
BitsPerSample := Header.BitsPerPixel;
BitsPerPixel := BitsPerSample * SamplesPerPixel;
if BitsPerPixel <= 8 then ColorScheme := csIndexed
else ColorScheme := csRGB;
if Header.Encoding = 1 then Compression := ctRLE
else Compression := ctNone;
XResolution := Header.HRes;
YResolution := Header.VRes;
Result := True;
end;
end;
end;
end;
{$endif} // PCXGraphic
//----------------- TPCDGraphic ----------------------------------------------------------------------------------------
{$ifdef PCDGraphic}
const
PCD_BEGIN_BASE16 = 8192;
PCD_BEGIN_BASE4 = 47104;
PCD_BEGIN_BASE = 196608;
PCD_BEGIN_ORIENTATION = 194635;
PCD_BEGIN = 2048;
PCD_MAGIC = 'PCD_IPI';
//----------------------------------------------------------------------------------------------------------------------
function TPCDGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: array of Byte;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > 3 * $800;
if Result then
begin
SetLength(Header, $803);
ReadBuffer(Header[0], Length(Header));
Result := (StrLComp(@Header[0], 'PCD_OPA', 7) = 0) or
(StrLComp(@Header[$800], 'PCD', 3) = 0);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPCDGraphic.LoadFromStream(Stream: TStream);
var
C1, C2, YY: PAnsiChar;
YCbCrData: array[0..2] of PAnsiChar;
SourceDummy,
DestDummy: Pointer;
Offset, I,
X, Y,
ImageIndex,
Rows,
Columns: Cardinal;
ScanLines: array of Pointer;
LineBuffer: Pointer;
Line,
Run: PBGR;
Decoder: TPCDDecoder;
begin
Handle := 0;
FBasePosition := Stream.Position;
ImageIndex := 2; // third image is Base resolution
if ReadImageProperties(Stream, ImageIndex) then
begin
with Stream, FImageProperties do
begin
Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
Columns := 192 shl Min(ImageIndex, 2);
Rows := 128 shl Min(ImageIndex, 2);
// since row and columns might be swapped because of rotated images
// we determine the final dimensions once more
Width := 192 shl ImageIndex;
Height := 128 shl ImageIndex;
ZeroMemory(@YCbCrData, SizeOf(YCbCrData));
try
GetMem(YCbCrData[0], Width * Height);
GetMem(YCbCrData[1], Width * Height);
GetMem(YCbCrData[2], Width * Height);
// advance to image data
Offset := 96;
if Overview then Offset := 5
else
if ImageIndex = 1 then Offset := 23
else
if ImageIndex = 0 then Offset := 4;
Seek(Offset * $800 , soFromCurrent);
// color conversion setup
with ColorManager do
begin
SourceColorScheme := csPhotoYCC;
SourceBitsPerSample := 8;
SourceSamplesPerPixel := 3;
TargetColorScheme := csBGR;
TargetBitsPerSample := 8;
TargetSamplesPerPixel := 3;
end;
PixelFormat := pf24Bit;
// PhotoYCC format uses CCIR Recommendation 709 coefficients and is subsampled
// by factor 2 vertically and horizontally
ColorManager.SetYCbCrParameters([0.2125, 0.7154, 0.0721], 2, 2);
Progress(Self, psEnding, 0, False, FProgressRect, '');
if False then
begin
// if Overview then ... no info yet about overview image structure
end
else
begin
YY := YCbCrData[0];
C1 := YCbCrData[1];
C2 := YCbCrData[2];
I := 0;
Progress(Self, psStarting, 0, False, FProgressRect, gesLoadingData);
while I < Rows do
begin
Progress(Self, psRunning, MulDiv(I, 100, Rows), False, FProgressRect, '');
ReadBuffer(YY^, Columns);
Inc(YY, Width);
ReadBuffer(YY^, Columns);
Inc(YY, Width);
ReadBuffer(C1^, Columns shr 1);
Inc(C1, Width);
ReadBuffer(C2^, Columns shr 1);
Inc(C2, Width);
Inc(I, 2);
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesUpsampling);
// Y stands here for maximum number of upsample calls
Y := 5;
if ImageIndex >= 3 then
begin
Inc(Y, 3 * (ImageIndex - 3));
Decoder := TPCDDecoder.Create(Stream);
SourceDummy := @YCbCrData;
DestDummy := nil;
try
// recover luminance deltas for 1536 x 1024 image
Progress(Self, psRunning, MulDiv(0, 100, Y), False, FProgressRect, '');
Upsample(768, 512, Width, YCbCrData[0]);
Progress(Self, psRunning, MulDiv(1, 100, Y), False, FProgressRect, '');
Upsample(384, 256, Width, YCbCrData[1]);
Progress(Self, psRunning, MulDiv(2, 100, Y), False, FProgressRect, '');
Upsample(384, 256, Width, YCbCrData[2]);
Seek(4 * $800, soFromCurrent);
Decoder.Decode(SourceDummy, DestDummy, Width, 1024);
if ImageIndex >= 4 then
begin
// recover luminance deltas for 3072 x 2048 image
Progress(Self, psRunning, MulDiv(3, 100, Y), False, FProgressRect, '');
Upsample(1536, 1024, Width, YCbCrData[0]);
Progress(Self, psRunning, MulDiv(4, 100, Y), False, FProgressRect, '');
Upsample(768, 512, Width, YCbCrData[1]);
Progress(Self, psRunning, MulDiv(5, 100, Y), False, FProgressRect, '');
Upsample(768, 512, Width, YCbCrData[2]);
Offset := (Position - Integer(FBasePosition)) div $800 + 12;
Seek(FBasePosition + Offset * $800, soFromBeginning);
Decoder.Decode(SourceDummy, DestDummy, Width, 2048);
if ImageIndex = 5 then
begin
// recover luminance deltas for 6144 x 4096 image (vaporware)
Progress(Self, psRunning, MulDiv(6, 100, Y), False, FProgressRect, '');
Upsample(3072, 2048, Width, YCbCrData[1]);
Progress(Self, psRunning, MulDiv(7, 100, Y), False, FProgressRect, '');
Upsample(1536, 1024, Width, YCbCrData[1]);
Progress(Self, psRunning, MulDiv(8, 100, Y), False, FProgressRect, '');
Upsample(1536, 1024, Width, YCbCrData[2]);
end;
end;
finally
Decoder.Free;
end;
end;
Progress(Self, psRunning, MulDiv(Y - 1, 100, Y), False, FProgressRect, '');
Upsample(Width shr 1, Height shr 1, Width, YCbCrData[1]);
Progress(Self, psRunning, MulDiv(Y, 100, Y), False, FProgressRect, '');
Upsample(Width shr 1, Height shr 1, Width, YCbCrData[2]);
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
// transfer luminance and chrominance channels
YY := YCbCrData[0];
C1 := YCbCrData[1];
C2 := YCbCrData[2];
// For the rotated mode where we need to turn the image by 90░. We can speed up loading
// the image by factor 2 by using a local copy of the Scanline pointers.
if Rotate in [1, 3] then
begin
Self.Width := Height;
Self.Height := Width;
FProgressRect.Right := Height;
SetLength(ScanLines, Width);
for Y := 0 to Width - 1 do ScanLines[Y] := ScanLine[Y];
GetMem(LineBuffer, 3 * Width);
end
else
begin
ScanLines := nil;
Self.Width := Width;
Self.Height := Height;
LineBuffer := nil;
end;
try
case Rotate of
1: // rotate -90░
begin
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([YY, C1, C2], LineBuffer, Width, $FF);
Inc(YY, Width);
Inc(C1, Width);
Inc(C2, Width);
Run := LineBuffer;
for X := 0 to Width - 1 do
begin
PAnsiChar(Line) := PAnsiChar(ScanLines[Width - X - 1]) + Y * 3;
Line^ := Run^;
Inc(Run);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
3: // rotate 90░
begin
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([YY, C1, C2], LineBuffer, Width, $FF);
Inc(YY, Width);
Inc(C1, Width);
Inc(C2, Width);
Run := LineBuffer;
for X := 0 to Width - 1 do
begin
PAnsiChar(Line) := PAnsiChar(ScanLines[X]) + (Height - Y - 1) * 3;
Line^ := Run^;
Inc(Run);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
else
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([YY, C1, C2], ScanLine[Y], Width, $FF);
Inc(YY, Width);
Inc(C1, Width);
Inc(C2, Width);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
finally
ScanLines := nil;
if Assigned(LineBuffer) then FreeMem(LineBuffer);
end;
end;
finally
if Assigned(YCbCrData[2]) then FreeMem(YCbCrData[2]);
if Assigned(YCbCrData[1]) then FreeMem(YCbCrData[1]);
if Assigned(YCbCrData[0]) then FreeMem(YCbCrData[0]);
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPCDGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: array of Byte;
Temp: Cardinal;
begin
if ImageIndex > 5 then ImageIndex := 5;
Result := inherited ReadImageProperties(Stream, ImageIndex) and
((Stream.Size - Integer(FBasePosition)) > 3 * $800);
with Stream, FImageProperties do
begin
SetLength(Header, 3 * $800);
ReadBuffer(Header[0], Length(Header));
try
Overview := StrLComp(@Header[0], 'PCD_OPA', 7) = 0;
// determine if image is a PhotoCD image
if Overview or (StrLComp(@Header[$800], 'PCD', 3) = 0) then
begin
Rotate := Header[$0E02] and 3;
// image sizes are fixed, depending on the given image index
if Overview then ImageIndex := 0;
Width := 192 shl ImageIndex;
Height := 128 shl ImageIndex;
if (Rotate = 1) or (Rotate = 3) then
begin
Temp := Width;
Width := Height;
Height := Temp;
end;
ColorScheme := csPhotoYCC;
BitsPerSample := 8;
SamplesPerPixel := 3;
BitsPerPixel := BitsPerSample * SamplesPerPixel;
if ImageIndex > 2 then Compression := ctPCDHuffmann
else Compression := ctNone;
ImageCount := (Header[10] shl 8) or Header[11];
Result := True;
end;
finally
Header := nil;
end;
end;
end;
{$endif} // PCDGraphic
//----------------- TPPMGraphic ----------------------------------------------------------------------------------------
{$ifdef PortableMapGraphic}
function TPPMGraphic.CanLoad(Stream: TStream): Boolean;
var
Buffer: array[0..9] of AnsiChar;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > 10;
if Result then
begin
ReadBuffer(Buffer, SizeOf(Buffer));
Result := (Buffer[0] = 'P') and (Buffer[1] in ['1'..'6']);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPPMGraphic.CurrentChar: AnsiChar;
begin
if FIndex = SizeOf(FBuffer) then Result := #0
else Result := FBuffer[FIndex];
end;
//----------------------------------------------------------------------------------------------------------------------
function TPPMGraphic.GetChar: AnsiChar;
// buffered I/O
begin
if FIndex = SizeOf(FBuffer) then
begin
if FStream.Position = FStream.Size then GraphicExError(gesStreamReadError, ['PPM']);
FIndex := 0;
FStream.Read(FBuffer, SizeOf(FBuffer));
end;
Result := FBuffer[FIndex];
Inc(FIndex);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPPMGraphic.GetNumber: Cardinal;
// reads the next number from the stream (and skips all characters which are not in 0..9)
var
Ch: AnsiChar;
begin
// skip all non-numbers
repeat
Ch := GetChar;
// skip comments
if Ch = '#' then
begin
ReadLine;
Ch := GetChar;
end;
until Ch in ['0'..'9'];
// read the number characters and convert meanwhile
Result := 0;
repeat
Result := 10 * Result + Ord(Ch) - $30;
Ch := GetChar;
until not (Ch in ['0'..'9']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPPMGraphic.ReadLine: AnsiString;
// reads one text line from stream and skips comments
var
Ch: AnsiChar;
I: Integer;
begin
Result := '';
repeat
Ch := GetChar;
if Ch in [#13, #10] then Break
else Result := Result + Ch;
until False;
// eat #13#10 combination
if (Ch = #13) and (CurrentChar = #10) then GetChar;
// delete comments
I := Pos('#', Result);
if I > 0 then Delete(Result, I, MaxInt);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPPMGraphic.LoadFromStream(Stream: TStream);
var
Buffer: AnsiString;
Line24: PBGR;
Line8: PByte;
X, Y: Integer;
Pixel: Byte;
begin
Handle := 0;
FBasePosition := Stream.Position;
// copy reference for buffered access
FStream := Stream;
if ReadImageProperties(Stream, 0) then
begin
with FImageProperties do
begin
Stream.Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
// set index pointer to end of buffer to cause reload
FIndex := SizeOf(FBuffer);
with Stream do
begin
Buffer := ReadLine;
case StrToInt(Buffer[2]) of
1: // PBM ASCII format (black & white)
begin
PixelFormat := pf1Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
ColorManager.TargetSamplesPerPixel := 1;
ColorManager.TargetBitsPerSample := 1;
Palette := ColorManager.CreateGrayScalePalette(True);
// read image data
for Y := 0 to Height - 1 do
begin
Line8 := ScanLine[Y];
Pixel := 0;
for X := 1 to Width do
begin
Pixel := (Pixel shl 1) or (GetNumber and 1);
if (X mod 8) = 0 then
begin
Line8^ := Pixel;
Inc(Line8);
Pixel := 0;
end;
end;
if (Width mod 8) <> 0 then Line8^ := Pixel shl (8 - (Width mod 8));
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
2: // PGM ASCII form (gray scale)
begin
PixelFormat := pf8Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
// skip maximum color value
GetNumber;
ColorManager.TargetSamplesPerPixel := 1;
ColorManager.TargetBitsPerSample := 8;
Palette := ColorManager.CreateGrayScalePalette(False);
// read image data
for Y := 0 to Height - 1 do
begin
Line8 := ScanLine[Y];
for X := 0 to Width - 1 do
begin
Line8^ := GetNumber;
Inc(Line8);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
3: // PPM ASCII form (true color)
begin
PixelFormat := pf24Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
// skip maximum color value
GetNumber;
for Y := 0 to Height - 1 do
begin
Line24 := ScanLine[Y];
for X := 0 to Width - 1 do
begin
Line24.R := GetNumber;
Line24.G := GetNumber;
Line24.B := GetNumber;
Inc(Line24);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
4: // PBM binary format (black & white)
begin
PixelFormat := pf1Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
ColorManager.TargetSamplesPerPixel := 1;
ColorManager.TargetBitsPerSample := 1;
Palette := ColorManager.CreateGrayScalePalette(True);
// read image data
for Y := 0 to Height - 1 do
begin
Line8 := ScanLine[Y];
for X := 0 to (Width div 8) - 1 do
begin
Line8^ := Byte(GetChar);
Inc(Line8);
end;
if (Width mod 8) <> 0 then Line8^ := Byte(GetChar);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
5: // PGM binary form (gray scale)
begin
PixelFormat := pf8Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
// skip maximum color value
GetNumber;
ColorManager.TargetSamplesPerPixel := 1;
ColorManager.TargetBitsPerSample := 8;
Palette := ColorManager.CreateGrayScalePalette(False);
// read image data
for Y := 0 to Height - 1 do
begin
Line8 := ScanLine[Y];
for X := 0 to Width - 1 do
begin
Line8^ := Byte(GetChar);
Inc(Line8);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
6: // PPM binary form (true color)
begin
PixelFormat := pf24Bit;
Self.Width := GetNumber;
Self.Height := GetNumber;
// skip maximum color value
GetNumber;
// Pixel values are store linearly (but RGB instead BGR).
// There's one allowed white space which will automatically be skipped by the first
// GetChar call below
// now read the pixels
for Y := 0 to Height - 1 do
begin
Line24 := ScanLine[Y];
for X := 0 to Width - 1 do
begin
Line24.R := Byte(GetChar);
Line24.G := Byte(GetChar);
Line24.B := Byte(GetChar);
Inc(Line24);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end
else GraphicExError(gesInvalidImage, ['PBM, PGM or PPM']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPPMGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Buffer: AnsiString;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
// set index pointer to end of buffer to cause reload
FIndex := SizeOf(FBuffer);
Buffer := ReadLine;
Compression := ctNone;
if Buffer[1] = 'P' then
begin
case StrToInt(Buffer[2]) of
1: // PBM ASCII format (black & white)
begin
Width := GetNumber;
Height := GetNumber;
SamplesPerPixel := 1;
BitsPerSample := 1;
ColorScheme := csIndexed;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
2: // PGM ASCII form (gray scale)
begin
Width := GetNumber;
Height := GetNumber;
// skip maximum color value
GetNumber;
SamplesPerPixel := 1;
BitsPerSample := 8;
ColorScheme := csIndexed;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
3: // PPM ASCII form (true color)
begin
Width := GetNumber;
Height := GetNumber;
// skip maximum color value
GetNumber;
SamplesPerPixel := 3;
BitsPerSample := 8;
ColorScheme := csRGB;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
4: // PBM binary format (black & white)
begin
Width := GetNumber;
Height := GetNumber;
SamplesPerPixel := 1;
BitsPerSample := 1;
ColorScheme := csIndexed;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
5: // PGM binary form (gray scale)
begin
Width := GetNumber;
Height := GetNumber;
// skip maximum color value
GetNumber;
SamplesPerPixel := 1;
BitsPerSample := 8;
ColorScheme := csIndexed;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
6: // PPM binary form (true color)
begin
Width := GetNumber;
Height := GetNumber;
// skip maximum color value
GetNumber;
SamplesPerPixel := 3;
BitsPerSample := 8;
ColorScheme := csRGB;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
end;
end;
Result := True;
end;
end;
end;
{$endif} // PortableMapGraphic
//----------------- TCUTGraphic ----------------------------------------------------------------------------------------
{$ifdef CUTGraphic}
function TCUTGraphic.CanLoad(Stream: TStream): Boolean;
// Note: cut files cannot be determined from stream because the only information
// is width and height of the image at stream/image start which is by no means
// enough to identify a cut (or any other) image.
begin
Result := False;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCUTGraphic.LoadFromFile(const FileName: String);
// overridden to extract an implicit palette file name
begin
FPaletteFile := ChangeFileExt(FileName, '.pal');
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCUTGraphic.LoadFromStream(Stream: TStream);
var
Buffer: PByte;
Run,
Line: Pointer;
Decoder: TCUTRLEDecoder;
CUTSize: Cardinal;
Y: Integer;
begin
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with Stream, FImageProperties do
begin
Position := FBasePosition + 6;
FProgressRect := Rect(0, 0, Width, 0);
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
PixelFormat := pf8Bit;
Self.Width := Width;
Self.Height := Height;
LoadPalette;
CutSize := Stream.Size - Stream.Position;
Decoder := TCUTRLEDecoder.Create;
Buffer := nil;
try
GetMem(Buffer, CutSize);
Stream.ReadBuffer(Buffer^, CUTSize);
Run := Buffer;
for Y := 0 to Height - 1 do
begin
Line := ScanLine[Y];
Decoder.Decode(Run, Line, 0, Width);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
Decoder.Free;
if Assigned(Buffer) then FreeMem(Buffer);
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TCUTGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Dummy: Word;
begin
inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
PixelFormat := pf8Bit;
ReadBuffer(Dummy, SizeOf(Dummy));
Width := Dummy;
ReadBuffer(Dummy, SizeOf(Dummy));
Height := Dummy;
ColorScheme := csIndexed;
BitsPerSample := 8;
SamplesPerPixel := 1;
BitsPerPixel := BitsPerSample * SamplesPerPixel;
Compression := ctRLE;
Result := True;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
type
// the palette file header is actually more complex than the
// image file's header, funny...
PHaloPaletteHeader = ^THaloPaletteHeader;
THaloPaletteHeader = packed record
ID: array[0..1] of AnsiChar; // should be 'AH'
Version,
Size: Word;
FileType,
SubType: Byte;
BrdID,
GrMode: Word;
MaxIndex,
MaxRed,
MaxGreen,
MaxBlue: Word; // colors = MaxIndex + 1
Signature: array[0..7] of AnsiChar; // 'Dr. Halo'
Filler: array[0..11] of Byte;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TCUTGraphic.LoadPalette;
var
Header: PHaloPaletteHeader;
LogPalette: TMaxLogPalette;
I: Integer;
Buffer: array[0..511] of Byte;
Run: PWord;
begin
LogPalette.palVersion := $300;
if FileExists(FPaletteFile) then
begin
with TFileStream.Create(FPaletteFile, fmOpenRead or fmShareDenyNone) do
try
// quite strange file organization here, we need always to load 512 bytes blocks
// and skip occasionally some bytes
ReadBuffer(Buffer, SizeOf(Buffer));
Header := @Buffer;
LogPalette.palNumEntries := Header.MaxIndex + 1;
Run := @Buffer;
Inc(PByte(Run), SizeOf(Header^));
for I := 0 to LogPalette.palNumEntries - 1 do
begin
// load next 512 bytes buffer if necessary
if (Integer(Run) - Integer(@Buffer)) > 506 then
begin
ReadBuffer(Buffer, SizeOf(Buffer));
Run := @Buffer;
end;
LogPalette.palPalEntry[I].peRed := Run^;
Inc(Run);
LogPalette.palPalEntry[I].peGreen := Run^;
Inc(Run);
LogPalette.palPalEntry[I].peBlue := Run^;
Inc(Run);
end;
finally
Free;
end;
end
else
begin
LogPalette.palNumEntries := 256;
// no external palette so use gray scale
for I := 0 to 255 do
begin
LogPalette.palPalEntry[I].peBlue := I;
LogPalette.palPalEntry[I].peGreen := I;
LogPalette.palPalEntry[I].peRed := I;
end;
end;
// finally create palette
Palette := CreatePalette(PLogPalette(@LogPalette)^);
end;
{$endif} // CUTGraphic
//----------------- TGIFGraphic ----------------------------------------------------------------------------------------
{$ifdef GIFGraphic}
const
// logical screen descriptor packed field masks
GIF_GLOBALCOLORTABLE = $80;
GIF_COLORRESOLUTION = $70;
GIF_GLOBALCOLORTABLESORTED = $08;
GIF_COLORTABLESIZE = $07;
// image flags
GIF_LOCALCOLORTABLE = $80;
GIF_INTERLACED = $40;
GIF_LOCALCOLORTABLESORTED= $20;
// block identifiers
GIF_PLAINTEXT = $01;
GIF_GRAPHICCONTROLEXTENSION = $F9;
GIF_COMMENTEXTENSION = $FE;
GIF_APPLICATIONEXTENSION = $FF;
GIF_IMAGEDESCRIPTOR = Ord(',');
GIF_EXTENSIONINTRODUCER = Ord('!');
GIF_TRAILER = Ord(';');
type
TGIFHeader = packed record
Signature: array[0..2] of AnsiChar; // magic ID 'GIF'
Version: array[0..2] of AnsiChar; // '87a' or '89a'
end;
TLogicalScreenDescriptor = packed record
ScreenWidth: Word;
ScreenHeight: Word;
PackedFields,
BackgroundColorIndex, // index into global color table
AspectRatio: Byte; // actual ratio = (AspectRatio + 15) / 64
end;
TImageDescriptor = packed record
//Separator: Byte; // leave that out since we always read one bye ahead
Left: Word; // X position of image with respect to logical screen
Top: Word; // Y position
Width: Word;
Height: Word;
PackedFields: Byte;
end;
//----------------------------------------------------------------------------------------------------------------------
function TGIFGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TGIFHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > (SizeOf(TGIFHeader) + SizeOf(TLogicalScreenDescriptor) + SizeOf(TImageDescriptor));
if Result then
begin
ReadBuffer(Header, SizeOf(Header));
Result := UpperCase(Header.Signature) = 'GIF';
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TGIFGraphic.SkipExtensions: Byte;
// Skips all blocks until an image block has been found in the data stream.
// Result is the image block ID if an image block could be found.
var
Increment: Byte;
begin
with FStream do
begin
// iterate through the blocks until first image is found
repeat
ReadBuffer(Result, 1);
if Result = GIF_EXTENSIONINTRODUCER then
begin
// skip any extension
ReadBuffer(Result, 1);
case Result of
GIF_PLAINTEXT:
begin
// block size of text grid data
ReadBuffer(Increment, 1);
Seek(Increment, soFromCurrent);
// skip variable lengthed text block
repeat
// block size
ReadBuffer(Increment, 1);
if Increment = 0 then Break;
Seek(Increment, soFromCurrent);
until False;
end;
GIF_GRAPHICCONTROLEXTENSION:
begin
// block size
ReadBuffer(Increment, 1);
// skip block and its terminator
Seek(Increment + 1, soFromCurrent);
end;
GIF_COMMENTEXTENSION:
repeat
// block size
ReadBuffer(Increment, 1);
if Increment = 0 then Break;
Seek(Increment, soFromCurrent);
until False;
GIF_APPLICATIONEXTENSION:
begin
// application id and authentication code plus potential application data
repeat
ReadBuffer(Increment, 1);
if Increment = 0 then Break;
Seek(Increment, soFromCurrent);
until False;
end;
end;
end;
until (Result = GIF_IMAGEDESCRIPTOR) or (Result = GIF_TRAILER);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TGIFGraphic.LoadFromStream(Stream: TStream);
var
Header: TGIFHeader;
ScreenDescriptor: TLogicalScreenDescriptor;
ImageDescriptor: TImageDescriptor;
LogPalette: TMaxLogPalette;
I: Cardinal;
BlockID: Byte;
InitCodeSize: Byte;
RawData,
Run: PByte;
TargetBuffer,
TargetRun,
Line: Pointer;
Pass,
Increment,
Marker: Integer;
Decoder: TDecoder;
begin
// release old image
Handle := 0;
FBasePosition := Stream.Position;
FStream := Stream;
if ReadImageProperties(Stream, 0) then
begin
with Stream, FImageProperties do
begin
Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
ReadBuffer(Header, SizeOf(Header));
PixelFormat := pf8Bit;
// general information
ReadBuffer(ScreenDescriptor, SizeOf(ScreenDescriptor));
ZeroMemory(@LogPalette, SizeOf(LogPalette));
LogPalette.palVersion := $300;
// read global color table if given
if (ScreenDescriptor.PackedFields and GIF_GLOBALCOLORTABLE) <> 0 then
begin
// the global color table immediately follows the screen descriptor
LogPalette.palNumEntries := 2 shl (ScreenDescriptor.PackedFields and GIF_COLORTABLESIZE);
for I := 0 to LogPalette.palNumEntries - 1 do
begin
ReadBuffer(LogPalette.palPalEntry[I].peRed, 1);
ReadBuffer(LogPalette.palPalEntry[I].peGreen, 1);
ReadBuffer(LogPalette.palPalEntry[I].peBlue, 1);
end;
// finally create palette
Palette := CreatePalette(PLogPalette(@LogPalette)^);
end;
BlockID := SkipExtensions;
Progress(Self, psEnding, 0, False, FProgressRect, '');
// image found?
if BlockID = GIF_IMAGEDESCRIPTOR then
begin
Progress(Self, psStarting, 0, False, FProgressRect, gesLoadingData);
ReadBuffer(ImageDescriptor, SizeOf(TImageDescriptor));
Self.Width := Width;
Self.Height := Height;
// if there is a local color table then override the already set one
if (ImageDescriptor.PackedFields and GIF_LOCALCOLORTABLE) <> 0 then
begin
// the global color table immediately follows the image descriptor
LogPalette.palNumEntries := 2 shl (ImageDescriptor.PackedFields and GIF_COLORTABLESIZE);
for I := 0 to LogPalette.palNumEntries - 1 do
begin
ReadBuffer(LogPalette.palPalEntry[I].peRed, 1);
ReadBuffer(LogPalette.palPalEntry[I].peGreen, 1);
ReadBuffer(LogPalette.palPalEntry[I].peBlue, 1);
end;
Palette := CreatePalette(PLogPalette(@LogPalette)^);
end;
ReadBuffer(InitCodeSize, 1);
// decompress data in one step
// 1) count data
Marker := Position;
Pass := 0;
Increment := 0;
repeat
if Read(Increment, 1) = 0 then Break;
Inc(Pass, Increment);
Seek(Increment, soFromCurrent);
until Increment = 0;
// 2) allocate enough memory
GetMem(RawData, Pass);
// add one extra line of extra memory for badly coded images
GetMem(TargetBuffer, Width * (Height + 1));
try
// 3) read and decode data
Position := Marker;
Increment := 0;
Run := RawData;
repeat
if Read(Increment, 1) = 0 then Break;
Read(Run^, Increment);
Inc(Run, Increment);
until Increment = 0;
Decoder := TGIFLZWDecoder.Create(InitCodeSize);
try
Run := RawData;
Decoder.Decode(Pointer(Run), TargetBuffer, Pass, Width * Height);
finally
Decoder.Free;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
// finally transfer image data
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
if (ImageDescriptor.PackedFields and GIF_INTERLACED) = 0 then
begin
TargetRun := TargetBuffer;
for I := 0 to Height - 1 do
begin
Line := Scanline[I];
Move(TargetRun^, Line^, Width);
Inc(PByte(TargetRun), Width);
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end
else
begin
TargetRun := TargetBuffer;
// interlaced image, need to move in four passes
for Pass := 0 to 3 do
begin
// determine start line and increment of the pass
case Pass of
0:
begin
I := 0;
Increment := 8;
end;
1:
begin
I := 4;
Increment := 8;
end;
2:
begin
I := 2;
Increment := 4;
end;
else
I := 1;
Increment := 2;
end;
while I < Height do
begin
Line := Scanline[I];
Move(TargetRun^, Line^, Width);
Inc(PByte(TargetRun), Width);
Inc(I, Increment);
if Pass = 3 then
begin
// progress events only for last (and most expensive) run
Progress(Self, psRunning, MulDiv(I, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
finally
if Assigned(TargetBuffer) then FreeMem(TargetBuffer);
if Assigned(RawData) then FreeMem(RawData);
end;
end;
end;
end
else GraphicExError(gesInvalidImage, ['GIF']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TGIFGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TGIFHeader;
ScreenDescriptor: TLogicalScreenDescriptor;
ImageDescriptor: TImageDescriptor;
BlockID: Integer;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
ReadBuffer(Header, SizeOf(Header));
if UpperCase(Header.Signature) = 'GIF' then
begin
Version := StrToInt(Copy(Header.Version, 1, 2));
ColorScheme := csIndexed;
SamplesPerPixel := 1;
// might be overwritten
BitsPerSample := 8;
Compression := ctLZW;
// general information
ReadBuffer(ScreenDescriptor, SizeOf(ScreenDescriptor));
// skip global color table if given
if (ScreenDescriptor.PackedFields and GIF_GLOBALCOLORTABLE) <> 0 then
begin
BitsPerSample := (ScreenDescriptor.PackedFields and GIF_COLORTABLESIZE) + 1;
// the global color table immediately follows the screen descriptor
Seek(3 * (1 shl BitsPerSample), soFromCurrent);
end;
BlockID := SkipExtensions;
// image found?
if BlockID = GIF_IMAGEDESCRIPTOR then
begin
ReadBuffer(ImageDescriptor, SizeOf(TImageDescriptor));
Width := ImageDescriptor.Width;
if Width = 0 then Width := ScreenDescriptor.ScreenWidth;
Height := ImageDescriptor.Height;
if Height = 0 then Height := ScreenDescriptor.ScreenHeight;
// if there is a local color table then override the already set one
LocalColorTable := (ImageDescriptor.PackedFields and GIF_LOCALCOLORTABLE) <> 0;
if LocalColorTable then
BitsPerSample := (ImageDescriptor.PackedFields and GIF_LOCALCOLORTABLE) + 1;
Interlaced := (ImageDescriptor.PackedFields and GIF_INTERLACED) <> 0;
end;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
Result := True;
end;
end;
end;
{$endif} // GIFGraphic
//----------------- TRLAGraphic ----------------------------------------------------------------------------------------
{$ifdef RLAGraphic}
// This implementation is based on code from Dipl. Ing. Ingo Neumann (ingo@upstart.de, ingo_n@dialup.nacamar.de).
type
TRLAWindow = packed record
Left,
Right,
Bottom,
Top: SmallInt;
end;
TRLAHeader = packed record
Window, // overall image size
Active_window: TRLAWindow; // size of non-zero portion of image (we use this as actual image size)
Frame, // frame number if part of a sequence
Storage_type, // type of image channels (0 - integer data, 1 - float data)
Num_chan, // samples per pixel (usually 3: r, g, b)
Num_matte, // number of matte channels (usually only 1)
Num_aux, // number of auxiliary channels, usually 0
Revision: SmallInt; // always $FFFE
Gamma: array[0..15] of AnsiChar; // gamma single value used when writing the image
Red_pri: array[0..23] of AnsiChar; // used chromaticity for red channel (typical format: "%7.4f %7.4f")
Green_pri: array[0..23] of AnsiChar; // used chromaticity for green channel
Blue_pri: array[0..23] of AnsiChar; // used chromaticity for blue channel
White_pt: array[0..23] of AnsiChar; // used chromaticity for white point
Job_num: Integer; // rendering speciifc
Name: array[0..127] of AnsiChar; // original file name
Desc: array[0..127] of AnsiChar; // a file description
ProgramName: array[0..63] of AnsiChar; // name of program which created the image
Machine: array[0..31] of AnsiChar; // name of computer on which the image was rendered
User: array[0..31] of AnsiChar; // user who ran the creation program of the image
Date: array[0..19] of AnsiChar; // creation data of image (ex: Sep 30 12:29 1993)
Aspect: array[0..23] of AnsiChar; // aspect format of the file (external resource)
Aspect_ratio: array[0..7] of AnsiChar; // float number Width /Height
Chan: array[0..31] of AnsiChar; // color space (can be: rgb, xyz, sampled or raw)
Field: SmallInt; // 0 - non-field rendered data, 1 - field rendered data
Time: array[0..11] of AnsiChar; // time needed to create the image (used when rendering)
Filter: array[0..31] of AnsiChar; // filter name to post-process image data
Chan_bits, // bits per sample
Matte_type, // type of matte channel (see aux_type)
Matte_bits, // precision of a pixel's matte channel (1..32)
Aux_type, // type of aux channel (0 - integer data; 4 - single (float) data
Aux_bits: SmallInt; // bits precision of the pixel's aux channel (1..32 bits)
Aux: array[0..31] of AnsiChar; // auxiliary channel as either range or depth
Space: array[0..35] of AnsiChar; // unused
Next: Integer; // offset for next header if multi-frame image
end;
//----------------------------------------------------------------------------------------------------------------------
function TRLAGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TRLAHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
ReadBuffer(Header, SizeOf(Header));
Result := (Swap(Word(Header.Revision)) = $FFFE) and
((LowerCase(Header.Chan) = 'rgb') or
(LowerCase(Header.Chan) = 'xyz'));
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TRLAGraphic.LoadFromStream(Stream: TStream);
var
Offsets: TCardinalArray;
RLELength: Word;
Line: Pointer;
Y: Integer;
// RLE buffers
RawBuffer,
RedBuffer,
GreenBuffer,
BlueBuffer,
AlphaBuffer: Pointer;
Decoder: TRLADecoder;
begin
// free previous image data
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with Stream, FImageProperties do
begin
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
with ColorManager do
begin
SourceSamplesPerPixel := SamplesPerPixel;
TargetSamplesPerPixel := SamplesPerPixel;
SourceBitsPerSample := BitsPerSample;
if BitsPerSample > 8 then TargetBitsPerSample := 8
else TargetBitsPerSample := BitsPerSample;
SourceColorScheme := ColorScheme;
if ColorScheme = csRGBA then TargetColorScheme := csBGRA
else TargetColorScheme := csBGR;
PixelFormat := TargetPixelFormat;
if FileGamma <> 1 then
begin
SetGamma(FileGamma);
TargetOptions := TargetOptions + [coApplyGamma];
Include(Options, ioUseGamma);
end;
end;
// dimension of image, top might be larger than bottom denoting a bottom up image
Self.Width := Width;
Self.Height := Height;
// each scanline is organized in RLE compressed strips whose location in the stream
// is determined by the offsets table
SetLength(Offsets, Height);
ReadBuffer(Offsets[0], Height * SizeOf(Cardinal));
SwapLong(@Offsets[0], Height);
// setup intermediate storage
Decoder := TRLADecoder.Create;
RawBuffer := nil;
RedBuffer := nil;
GreenBuffer := nil;
BlueBuffer := nil;
AlphaBuffer := nil;
try
GetMem(RedBuffer, Width);
GetMem(GreenBuffer, Width);
GetMem(BlueBuffer, Width);
GetMem(AlphaBuffer, Width);
// no go for each scanline
for Y := 0 to Height - 1 do
begin
Stream.Position := FBasePosition + Offsets[Y];
if BottomUp then Line := ScanLine[Integer(Height) - Y - 1]
else Line := ScanLine[Y];
// read channel data to decode
// red
ReadBuffer(RLELength, SizeOf(RLELength));
RLELength := Swap(RLELength);
ReallocMem(RawBuffer, RLELength);
ReadBuffer(RawBuffer^, RLELength);
Decoder.Decode(RawBuffer, RedBuffer, RLELength, Width);
// green
ReadBuffer(RLELength, SizeOf(RLELength));
RLELength := Swap(RLELength);
ReallocMem(RawBuffer, RLELength);
ReadBuffer(RawBuffer^, RLELength);
Decoder.Decode(RawBuffer, GreenBuffer, RLELength, Width);
// blue
ReadBuffer(RLELength, SizeOf(RLELength));
RLELength := Swap(RLELength);
ReallocMem(RawBuffer, RLELength);
ReadBuffer(RawBuffer^, RLELength);
Decoder.Decode(RawBuffer, BlueBuffer, RLELength, Width);
if ColorManager.TargetColorScheme = csBGR then
begin
ColorManager.ConvertRow([RedBuffer, GreenBuffer, BlueBuffer], Line, Width, $FF);
end
else
begin
// alpha
ReadBuffer(RLELength, SizeOf(RLELength));
RLELength := Swap(RLELength);
ReallocMem(RawBuffer, RLELength);
ReadBuffer(RawBuffer^, RLELength);
Decoder.Decode(RawBuffer, AlphaBuffer, RLELength, Width);
ColorManager.ConvertRow([RedBuffer, GreenBuffer, BlueBuffer, AlphaBuffer], Line, Width, $FF);
end;
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
if Assigned(RedBuffer) then FreeMem(RedBuffer);
if Assigned(GreenBuffer) then FreeMem(GreenBuffer);
if Assigned(BlueBuffer) then FreeMem(BlueBuffer);
if Assigned(AlphaBuffer) then FreeMem(AlphaBuffer);
Decoder.Free;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TRLAGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TRLAHeader;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
ReadBuffer(Header, SizeOf(Header));
// data is always given in big endian order, so swap data which needs this
SwapHeader(Header);
Options := [ioBigEndian];
SamplesPerPixel := Header.num_chan;
if Header.num_matte = 1 then Inc(SamplesPerPixel);
BitsPerSample := Header.Chan_bits;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
if LowerCase(Header.Chan) = 'rgb' then
begin
if Header.num_matte > 0 then ColorScheme := csRGBA
else ColorScheme := csRGB;
end
else
if LowerCase(Header.Chan) = 'xyz' then Exit;
try
FileGamma := StrToFloat(Header.Gamma);
except
end;
Compression := ctRLE;
// dimension of image, top might be larger than bottom denoting a bottom up image
Width := Header.Active_window.Right - Header.Active_window.Left + 1;
Height := Abs(Header.Active_window.Bottom - Header.Active_window.Top) + 1;
BottomUp := (Header.Active_window.Bottom - Header.Active_window.Top) < 0;
Result := True;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TRLAGraphic.SwapHeader(var Header);
// separate swap method to ease reading the main flow of the LoadFromStream method
begin
with TRLAHeader(Header) do
begin
SwapShort(@Window, 4);
SwapShort(@Active_window, 4);
Frame := Swap(Frame);
Storage_type := Swap(Storage_type);
Num_chan := Swap(Num_chan);
Num_matte := Swap(Num_matte);
Num_aux := Swap(Num_aux);
Revision := Swap(Revision);
Job_num := SwapLong(Job_num);
Field := Swap(Field);
Chan_bits := Swap(Chan_bits);
Matte_type := Swap(Matte_type);
Matte_bits := Swap(Matte_bits);
Aux_type := Swap(Aux_type);
Aux_bits := Swap(Aux_bits);
Next := SwapLong(Next);
end;
end;
{$endif} // RLAGraphic
//----------------- TPSDGraphic ----------------------------------------------------------------------------------------
{$ifdef PhotoshopGraphic}
const
// color modes
PSD_BITMAP = 0;
PSD_GRAYSCALE = 1;
PSD_INDEXED = 2;
PSD_RGB = 3;
PSD_CMYK = 4;
PSD_MULTICHANNEL = 7;
PSD_DUOTONE = 8;
PSD_LAB = 9;
PSD_COMPRESSION_NONE = 0;
PSD_COMPRESSION_RLE = 1; // RLE compression (same as TIFF packed bits)
type
TPSDHeader = packed record
Signature: array[0..3] of AnsiChar; // always '8BPS'
Version: Word; // always 1
Reserved: array[0..5] of Byte; // reserved, always 0
Channels: Word; // 1..24, number of channels in the image (including alpha)
Rows,
Columns: Cardinal; // 1..30000, size of image
Depth: Word; // 1, 8, 16 bits per channel
Mode: Word; // color mode (see constants above)
end;
//----------------------------------------------------------------------------------------------------------------------
function TPSDGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TPSDHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
ReadBuffer(Header, SizeOf(Header));
Result := (UpperCase(Header.Signature) = '8BPS') and
(Swap(Header.Version) = 1);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPSDGraphic.LoadFromStream(Stream: TStream);
var
Header: TPSDHeader;
Count: Cardinal;
Decoder: TDecoder;
RLELength: array of Word;
Y: Integer;
BPS: Cardinal; // bytes per sample either 1 or 2 for 8 bits per channel and 16 bits per channel respectively
ChannelSize: Integer; // size of one channel (taking BPS into account)
Increment: Integer; // pointer increment from one line to next
// RLE buffers
Line,
RawBuffer, // all image data compressed
Buffer: Pointer; // all iamge data uncompressed
Run1, // running pointer in Buffer 1
Run2, // etc.
Run3,
Run4: PByte;
RawPalette: array[0..767] of Byte;
begin
// free previous image data
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
with Stream, FImageProperties do
begin
Position := FBasePosition;
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
ReadBuffer(Header, SizeOf(Header));
// initialize color manager
with ColorManager do
begin
SourceOptions := [coNeedByteSwap];
SourceBitsPerSample := BitsPerSample;
if BitsPerSample = 16 then TargetBitsPerSample := 8
else TargetBitsPerSample := BitsPerSample;
SourceSamplesPerPixel := SamplesPerPixel;
TargetSamplesPerPixel := SamplesPerPixel;
// color space
SourceColorScheme := ColorScheme;
case ColorScheme of
csG,
csIndexed:
TargetColorScheme := ColorScheme;
csRGB:
TargetColorScheme := csBGR;
csRGBA:
TargetColorScheme := csBGRA;
csCMYK:
begin
TargetColorScheme := csBGR;
TargetSamplesPerPixel := 3;
end;
csCIELab:
begin
// PSD uses 0..255 for a and b so we need to convert them to -128..127
SourceOptions := SourceOptions + [coLabByteRange, coLabChromaOffset];
TargetColorScheme := csBGR;
end;
end;
end;
PixelFormat := ColorManager.TargetPixelFormat;
Self.Width := Width;
Self.Height := Height;
// size of palette
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
// setup the palette if necessary, color data immediately follows header
case ColorScheme of
csG:
Palette := ColorManager.CreateGrayscalePalette(ioMinIsWhite in Options);
csIndexed:
begin
ReadBuffer(RawPalette, Count);
Count := Count div 3;
Palette := ColorManager.CreateColorPalette([@RawPalette, @RawPalette[Count], @RawPalette[2 * Count]],
pfPlane8Triple, Count, False);
end;
end;
// skip resource and layers section
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
Seek(Count, soFromCurrent);
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
// +2 in order to skip the following compression value
Seek(Count + 2, soFromCurrent);
// now read out image data
RawBuffer := nil;
if Compression = ctPackedBits then
begin
Decoder := TPackbitsRLEDecoder.Create;
SetLength(RLELength, Height * Channels);
ReadBuffer(RLELength[0], 2 * Length(RLELength));
SwapShort(@RLELength[0], Height * Channels);
end
else Decoder := nil;
Progress(Self, psEnding, 0, False, FProgressRect, '');
try
case ColorScheme of
csG,
csIndexed:
begin
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
// very simple format here, we don't need the color conversion manager
if Assigned(Decoder) then
begin
// determine whole compressed size
Count := 0;
for Y := 0 to Height - 1 do Inc(Count, RLELength[Y]);
GetMem(RawBuffer, Count);
try
ReadBuffer(RawBuffer^, Count);
Run1 := RawBuffer;
for Y := 0 to Height - 1 do
begin
Count := RLELength[Y];
Line := ScanLine[Y];
Decoder.Decode(Pointer(Run1), Line, Count, Width);
Inc(Run1, Count);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end
else // uncompressed data
for Y := 0 to Height - 1 do
begin
ReadBuffer(ScanLine[Y]^, Width);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
csRGB,
csRGBA,
csCMYK,
csCIELab:
begin
Progress(Self, psStarting, 0, False, FProgressRect, gesLoadingData);
// Data is organized in planes. This means first all red rows, then
// all green and finally all blue rows.
BPS := BitsPerSample div 8;
ChannelSize := BPS * Width * Height;
GetMem(Buffer, Channels * ChannelSize);
try
// first run: load image data and decompress it if necessary
if Assigned(Decoder) then
begin
// determine whole compressed size
Count := 0;
for Y := 0 to High(RLELength) do Inc(Count, RLELength[Y]);
Count := Count * Cardinal(BPS);
GetMem(RawBuffer, Count);
try
ReadBuffer(RawBuffer^, Count);
Decoder.Decode(RawBuffer, Buffer, Count, Channels * ChannelSize);
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
end;
end
else
ReadBuffer(Buffer^, Channels * ChannelSize);
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
Increment := BPS * Width;
// second run: put data into image (convert color space if necessary)
case ColorScheme of
csRGB:
begin
Run1 := Buffer;
Run2 := Run1; Inc(Run2, ChannelSize);
Run3 := Run2; Inc(Run3, ChannelSize);
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([Run1, Run2, Run3], ScanLine[Y], Width, $FF);
Inc(Run1, Increment);
Inc(Run2, Increment);
Inc(Run3, Increment);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
csRGBA:
begin
Run1 := Buffer;
Run2 := Run1; Inc(Run2, ChannelSize);
Run3 := Run2; Inc(Run3, ChannelSize);
Run4 := Run3; Inc(Run4, ChannelSize);
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([Run1, Run2, Run3, Run4], ScanLine[Y], Width, $FF);
Inc(Run1, Increment);
Inc(Run2, Increment);
Inc(Run3, Increment);
Inc(Run4, Increment);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
csCMYK:
begin
// Photoshop CMYK values are given with 0 for maximum values, but the
// (general) CMYK conversion works with 255 as maxium value. Hence we must reverse
// all entries in the buffer.
Run1 := Buffer;
for Y := 1 to 4 * ChannelSize do
begin
Run1^ := 255 - Run1^;
Inc(Run1);
end;
Run1 := Buffer;
Run2 := Run1; Inc(Run2, ChannelSize);
Run3 := Run2; Inc(Run3, ChannelSize);
Run4 := Run3; Inc(Run4, ChannelSize);
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([Run1, Run2, Run3, Run4], ScanLine[Y], Width, $FF);
Inc(Run1, Increment);
Inc(Run2, Increment);
Inc(Run3, Increment);
Inc(Run4, Increment);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
csCIELab:
begin
Run1 := Buffer;
Run2 := Run1; Inc(Run2, ChannelSize);
Run3 := Run2; Inc(Run3, ChannelSize);
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([Run1, Run2, Run3], ScanLine[Y], Width, $FF);
Inc(Run1, Increment);
Inc(Run2, Increment);
Inc(Run3, Increment);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
end;
finally
if Assigned(Buffer) then FreeMem(Buffer);
end;
end;
end;
finally
Decoder.Free;
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
end
else GraphicExError(gesInvalidImage, ['PSD or PDD']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPSDGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TPSDHeader;
Dummy: Word;
Count: Cardinal;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
ReadBuffer(Header, SizeOf(Header));
if Header.Signature = '8BPS' then
begin
with Header do
begin
// PSD files are big endian only
Channels := Swap(Channels);
Rows := SwapLong(Rows);
Columns := SwapLong(Columns);
Depth := Swap(Depth);
Mode := Swap(Mode);
end;
Options := [ioBigEndian];
// initialize color manager
BitsPerSample := Header.Depth;
Channels := Header.Channels;
// 1..24 channels are supported in PSD files, we can only use 4.
// The documentation states that main image data (rgb(a), cmyk etc.) is always
// written as first channels in their component order.
if Channels > 4 then SamplesPerPixel := 4
else SamplesPerPixel := Channels;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
// color space
case Header.Mode of
PSD_DUOTONE, // duo tone should be handled as grayscale
PSD_GRAYSCALE:
ColorScheme := csG;
PSD_BITMAP: // B&W
begin
ColorScheme := csG;
Include(Options, ioMinIsWhite);
end;
PSD_INDEXED: // 8 bits only are assumed because 16 bit wouldn't make sense here
ColorScheme := csIndexed;
PSD_MULTICHANNEL,
PSD_RGB:
if Header.Channels = 3 then ColorScheme := csRGB
else ColorScheme := csRGBA;
PSD_CMYK:
ColorScheme := csCMYK;
PSD_LAB:
ColorScheme := csCIELab;
end;
Width := Header.Columns;
Height := Header.Rows;
// size of palette
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
// skip palette (count is always given, might be 0 however, e.g. for RGB)
Seek(Count, soFromCurrent);
// skip resource and layers section
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
Seek(Count, soFromCurrent);
ReadBuffer(Count, SizeOf(Count));
Count := SwapLong(Count);
Seek(Count, soFromCurrent);
ReadBuffer(Dummy, SizeOf(Dummy));
if Swap(Dummy) = 1 then Compression := ctPackedBits
else Compression := ctNone;
Result := True;
end;
end;
end;
{$endif} // PhotoshopGraphic
//----------------- TPSPGraphic ----------------------------------------------------------------------------------------
{$ifdef PaintshopProGraphic}
const
// block identifiers
PSP_IMAGE_BLOCK = 0; // General Image Attributes Block (main)
PSP_CREATOR_BLOCK = 1; // Creator Data Block (main)
PSP_COLOR_BLOCK = 2; // Color Palette Block (main and sub)
PSP_LAYER_START_BLOCK = 3; // Layer Bank Block (main)
PSP_LAYER_BLOCK = 4; // Layer Block (sub)
PSP_CHANNEL_BLOCK = 5; // Channel Block (sub)
PSP_SELECTION_BLOCK = 6; // Selection Block (main)
PSP_ALPHA_BANK_BLOCK = 7; // Alpha Bank Block (main)
PSP_ALPHA_CHANNEL_BLOCK = 8; // Alpha Channel Block (sub)
PSP_THUMBNAIL_BLOCK = 9; // Thumbnail Block (main)
PSP_EXTENDED_DATA_BLOCK = 10; // Extended Data Block (main)
PSP_TUBE_BLOCK = 11; // Picture Tube Data Block (main)
PSP_ADJUSTMENT_EXTENSION_BLOCK = 12; // Adjustment Layer Extension Block (sub)
PSP_VECTOR_EXTENSION_BLOCK = 13; // Vector Layer Extension Block (sub)
PSP_SHAPE_BLOCK = 14; // Vector Shape Block (sub)
PSP_PAINTSTYLE_BLOCK = 15; // Paint Style Block (sub)
PSP_COMPOSITE_IMAGE_BANK_BLOCK = 16; // Composite Image Bank (main)
PSP_COMPOSITE_ATTRIBUTES_BLOCK = 17; // Composite Image Attributes (sub)
PSP_JPEG_BLOCK = 18; // JPEG Image Block (sub)
// bitmap types
PSP_DIB_IMAGE = 0; // Layer color bitmap
PSP_DIB_TRANS_MASK = 1; // Layer transparency mask bitmap
PSP_DIB_USER_MASK = 2; // Layer user mask bitmap
PSP_DIB_SELECTION= 3; // Selection mask bitmap
PSP_DIB_ALPHA_MASK = 4; // Alpha channel mask bitmap
PSP_DIB_THUMBNAIL = 5; // Thumbnail bitmap
PSP_DIB_THUMBNAIL_TRANS_MASK = 6; // Thumbnail transparency mask
PSP_DIB_ADJUSTMENT_LAYER = 7; // Adjustment layer bitmap
PSP_DIB_COMPOSITE = 8; // Composite image bitmap
PSP_DIB_COMPOSITE_TRANS_MASK = 9; // Composite image transparency
// composite image type
PSP_IMAGE_COMPOSITE = 0; // Composite Image
PSP_IMAGE_THUMBNAIL = 1; // Thumbnail Image
// graphic contents flags
PSP_GC_RASTERLAYERS = 1; // At least one raster layer
PSP_GC_VectorLayers = 2; // At least one vector layer
PSP_GC_ADJUSTMENTLAYERS = 4; // At least one adjustment layer
// Additional attributes
PSP_GC_THUMBNAIL = $01000000; // Has a thumbnail
PSP_GC_THUMBNAILTRANSPARENCY = $02000000; // Thumbnail transp.
PSP_GC_COMPOSITE = $04000000; // Has a composite image
PSP_GC_COMPOSITETRANSPARENCY = $08000000; // Composite transp.
PSP_GC_FLATIMAGE = $10000000; // Just a background
PSP_GC_SELECTION = $20000000; // Has a selection
PSP_GC_FLOATINGSELECTIONLAYER = $40000000; // Has float. selection
PSP_GC_ALPHACHANNELS = $80000000; // Has alpha channel(s)
// character style flags
PSP_STYLE_ITALIC = 1; // Italic property bit
PSP_STYLE_STRUCK = 2; // Strike-out property bit
PSP_STYLE_UNDERLINED = 4; // Underlined property bit
// layer flags
PSP_LAYER_VISIBLEFLAG = 1; // Layer is visible
PSP_LAYER_MASKPRESENCEFLAG = 2; // Layer has a mask
// Shape property flags
PSP_SHAPE_ANTIALIASED = 1; // Shape is anti-aliased
PSP_SHAPE_Selected = 2; // Shape is selected
PSP_SHAPE_Visible = 4; // Shape is visible
// Polyline node type flags
PSP_NODE_UNCONSTRAINED = 0; // Default node type
PSP_NODE_SMOOTH = 1; // Node is smooth
PSP_NODE_SYMMETRIC = 2; // Node is symmetric
PSP_NODE_ALIGNED = 4; // Node is aligned
PSP_NODE_ACTIVE = 8; // Node is active
PSP_NODE_LOCKED = 16; // Node is locked (PSP doc says 0x16 here, but this seems to be a typo)
PSP_NODE_SELECTED = 32; // Node is selected (PSP doc says 0x32 here)
PSP_NODE_VISIBLE = 64; // Node is visible (PSP doc says 0x64 here)
PSP_NODE_CLOSED = 128; // Node is closed (PSP doc says 0x128 here)
// Blend modes
LAYER_BLEND_NORMAL = 0;
LAYER_BLEND_DARKEN = 1;
LAYER_BLEND_LIGHTEN = 2;
LAYER_BLEND_HUE = 3;
LAYER_BLEND_SATURATION = 4;
LAYER_BLEND_COLOR = 5;
LAYER_BLEND_LUMINOSITY = 6;
LAYER_BLEND_MULTIPLY = 7;
LAYER_BLEND_SCREEN = 8;
LAYER_BLEND_DISSOLVE = 9;
LAYER_BLEND_OVERLAY = 10;
LAYER_BLEND_HARD_LIGHT = 11;
LAYER_BLEND_SOFT_LIGHT = 12;
LAYER_BLEND_DIFFERENCE = 130;
LAYER_BLEND_DODGE = 14;
LAYER_BLEND_BURN = 15;
LAYER_BLEND_EXCLUSION = 16;
LAYER_BLEND_ADJUST = 255;
// Adjustment layer types
PSP_ADJUSTMENT_NONE = 0; // Undefined adjustment layer type
PSP_ADJUSTMENT_LEVEL = 1; // Level adjustment
PSP_ADJUSTMENT_CURVE = 2; // Curve adjustment
PSP_ADJUSTMENT_BRIGHTCONTRAST = 3; // Brightness-contrast adjustment
PSP_ADJUSTMENT_COLORBAL = 4; // Color balance adjustment
PSP_ADJUSTMENT_HSL = 5; // HSL adjustment
PSP_ADJUSTMENT_CHANNELMIXER = 6; // Channel mixer adjustment
PSP_ADJUSTMENT_INVERT = 7; // Invert adjustment
PSP_ADJUSTMENT_THRESHOLD = 8; // Threshold adjustment
PSP_ADJUSTMENT_POSTER = 9; // Posterize adjustment
// Vector shape types
PSP_VST_Unknown = 0; // Undefined vector type
PSP_VST_TEXT = 1; // Shape represents lines of text
PSP_VST_POLYLINE = 2; // Shape represents a multiple segment line
PSP_VST_ELLIPSE = 3; // Shape represents an ellipse (or circle)
PSP_VST_POLYGON = 4; // Shape represents a closed polygon
// Text element types
PSP_TET_UNKNOWN = 0; // Undefined text element type
PSP_TET_CHAR = 1; // A single character code
PSP_TET_CHARSTYLE = 2; // A character style change
PSP_TET_LINESTYLE = 3; // A line style change
// Text alignment types
PSP_TAT_LEFT = 0; // Left text alignment
PSP_TAT_CENTER = 1; // Center text alignment
PSP_TAT_RIGHT = 2; // Right text alignment
// Paint style types
PSP_STYLE_NONE = 0; // Undefined paint style
PSP_STYLE_COLOR = 1; // Paint using color (RGB or palette index)
PSP_STYLE_GRADIENT = 2; // Paint using gradient
// Channel types
PSP_CHANNEL_COMPOSITE = 0; // Channel of single channel bitmap
PSP_CHANNEL_RED = 1; // Red channel of 24 bit bitmap
PSP_CHANNEL_GREEN = 2; // Green channel of 24 bit bitmap
PSP_CHANNEL_BLUE = 3; // Blue channel of 24 bit bitmap
// Resolution metrics
PSP_METRIC_UNDEFINED = 0; // Metric unknown
PSP_METRIC_INCH = 1; // Resolution is in inches
PSP_METRIC_CM = 2; // Resolution is in centimeters
// Compression types
PSP_COMP_NONE = 0; // No compression
PSP_COMP_RLE = 1; // RLE compression
PSP_COMP_LZ77 = 2; // LZ77 compression
PSP_COMP_JPEG = 3; // JPEG compression (only used by thumbnail and composite image)
// Picture tube placement mode
PSP_TPM_Random = 0; // Place tube images in random intervals
PSPS_TPM_Constant = 1; // Place tube images in constant intervals
// Tube selection mode
PSP_TSM_RANDOM =0; // Randomly select the next image in tube to display
PSP_TSM_INCREMENTAL = 1; // Select each tube image in turn
PSP_TSM_ANGULAR = 2; // Select image based on cursor direction
PSP_TSM_PRESSURE = 3; // Select image based on pressure (from pressure-sensitive pad)
PSP_TSM_VELOCITY = 4; // Select image based on cursor speed
// Extended data field types
PSP_XDATA_TRNS_INDEX = 0; // Transparency index field
// Creator field types
PSP_CRTR_FLD_TITLE = 0; // Image document title field
PSP_CRTR_FLD_CRT_DATE = 1; // Creation date field
PSP_CRTR_FLD_MOD_DATE = 2; // Modification date field
PSP_CRTR_FLD_ARTIST = 3; // Artist name field
PSP_CRTR_FLD_CPYRGHT = 4; // Copyright holder name field
PSP_CRTR_FLD_DESC = 5; // Image document description field
PSP_CRTR_FLD_APP_ID = 6; // Creating app id field
PSP_CRTR_FLD_APP_VER = 7; // Creating app version field
// Creator application identifier
PSP_CREATOR_APP_UNKNOWN = 0; // Creator application unknown
PSP_CREATOR_APP_PAINT_SHOP_PRO = 1; // Creator is Paint Shop Pro
// Layer types (file version 3)
PSP_LAYER_NORMAL = 0; // Normal layer
PSP_LAYER_FLOATING_SELECTION = 1; // Floating selection layer
// Layer types (file version 4)
PSP_LAYER_UNDEFINED = 0; // Undefined layer type
PSP_LAYER_RASTER = 1; // Standard raster layer
PSP_LAYER_FLOATINGRASTERSELECTION = 2; // Floating selection (raster layer)
PSP_LAYER_Vector = 3; // Vector layer
PSP_LAYER_ADJUSTMENT = 4; // Adjustment layer
MagicID = 'Paint Shop Pro Image File';
type
// These block header structures are here for informational purposes only because the data of those
// headers is read member by member to generalize code for the different file versions
TPSPBlockHeader3 = packed record // block header file version 3
HeaderIdentifier: array[0..3] of AnsiChar; // i.e. "~BK" followed by a zero byte
BlockIdentifier: Word; // one of the block identifiers
InitialChunkLength, // length of the first sub chunk header or similar
TotalBlockLength: Cardinal; // length of this block excluding this header
end;
TPSPBlockHeader4 = packed record // block header file version 4
HeaderIdentifier: array[0..3] of AnsiChar; // i.e. "~BK" followed by a zero byte
BlockIdentifier: Word; // one of the block identifiers
TotalBlockLength: Cardinal; // length of this block excluding this header
end;
TPSPColorPaletteInfoChunk = packed record
EntryCount: Cardinal; // number of entries in the palette
end;
TPSPColorPaletteChunk = array[0..255] of TRGBQuad; // might actually be shorter
TPSPChannelInfoChunk = packed record
CompressedSize,
UncompressedSize: Cardinal;
BitmapType, // one of the bitmap types
ChannelType: Word; // one of the channel types
end;
// PSP defines a channel content chunk which is just a bunch of bytes (size is CompressedSize).
// There is no sense to define this record type here.
TPSPFileHeader = packed record
Signature: array[0..31] of AnsiChar; // the AnsiString "Paint Shop Pro Image File\n\x1a", padded with zeroes
MajorVersion,
MinorVersion: Word;
end;
TPSPImageAttributes = packed record
Width,
Height: Integer;
Resolution: Double; // Number of pixels per metric
ResolutionMetric: Byte; // Metric used for resolution (one of the metric constants)
Compression, // compression type of image (not thumbnail, it has its own compression)
BitDepth, // The bit depth of the color bitmap in each Layer of the image document
// (must be 1, 4, 8 or 24).
PlaneCount: Word; // Number of planes in each layer of the image document (usually 1)
ColorCount: Cardinal; // number of colors in each layer (2^bit depth)
GreyscaleFlag: Boolean; // Indicates whether the color bitmap in each layer of image document is a
// greyscale (False = not greyscale, True = greyscale).
TotalImageSize: Cardinal; // Sum of the sizes of all layer color bitmaps.
ActiveLayer: Integer; // Identifies the layer that was active when the image document was saved.
LayerCount: Word; // Number of layers in the document.
GraphicContents: Cardinal; // A series of flags that helps define the image's graphic contents.
end;
TPSPLayerInfoChunk = packed record
//LayerName: array[0..255] of AnsiChar; // Name of layer (in ASCII text). Has been replaced in version 4
// by a Delphi like short AnsiString (length word and variable length AnsiString)
LayerType: Byte; // Type of layer.
ImageRectangle, // Rectangle defining image border.
SavedImageRectangle: TRect; // Rectangle within image rectangle that contains "significant" data
// (only the contents of this rectangle are saved to the file).
LayerOpacity: Byte; // Overall layer opacity.
BlendingMode: Byte; // Mode to use when blending layer.
Visible: Boolean; // TRUE if layer was visible at time of save, FALSE otherwise.
TransparencyProtected: Boolean; // TRUE if transparency is protected.
LinkGroupIdentifier: Byte; // Identifies group to which this layer belongs.
MaskRectangle, // Rectangle defining user mask border.
SavedMaskRectangle: TRect; // Rectangle within mask rectangle that contains "significant" data
// (only the contents of this rectangle are saved to the file).
MaskLinked: Boolean; // TRUE if mask linked to layer (i.e., mask moves relative to layer)
MaskDisabled: Boolean; // TRUE if mask is disabled, FALSE otherwise.
InvertMask: Boolean; // TRUE if mask should be inverted when the layer is merged, FALSE otherwise.
BlendRangeCount: Word; // Number of valid source-destination field pairs to follow (note, there are
// currently always 5 such pairs, but they are not necessarily all valid).
SourceBlendRange1, // First source blend range value.
DestinationBlendRange1, // First destination blend range value.
SourceBlendRange2,
DestinationBlendRange2,
SourceBlendRange3,
DestinationBlendRange3,
SourceBlendRange4,
DestinationBlendRange4,
SourceBlendRange5,
DestinationBlendRange5: array[0..3] of Byte;
// these fields are obsolete since file version 4 because there's an own chunk for them
// BitmapCount: Word; // Number of bitmaps to follow.
// ChannelCount: Word; // Number of channels to follow.
end;
//----------------------------------------------------------------------------------------------------------------------
function TPSPGraphic.CanLoad(Stream: TStream): Boolean;
var
Header: TPSPFileHeader;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Header);
if Result then
begin
ReadBuffer(Header, SizeOf(Header));
Result := (StrLIComp(Header.Signature, MagicID, Length(MagicID)) = 0) and
(Header.MajorVersion >= 3);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPSPGraphic.LoadFromStream(Stream: TStream);
var
Header: TPSPFileHeader;
Image: TPSPImageAttributes;
// to use the code below for file 3 and 4 I read the parts of the block header
// separately instead as a structure
HeaderIdentifier: array[0..3] of AnsiChar; // i.e. "~BK" followed by a zero byte
BlockIdentifier: Word; // one of the block identifiers
InitialChunkLength, // length of the first sub chunk header or similar
TotalBlockLength: Cardinal; // length of this block excluding this header
LastPosition,
ChunkSize: Cardinal;
LayerInfo: TPSPLayerInfoChunk;
ChannelInfo: TPSPChannelInfoChunk;
LayerName: AnsiString;
NameLength: Word;
NextLayerPosition,
NextMainBlock: Integer;
// file version 4 specific data
BitmapCount,
ChannelCount: Word;
// load and decoding of image data
R, G, B, C: PByte;
RedBuffer,
GreenBuffer,
BlueBuffer,
CompBuffer: Pointer;
X, Y,
Index,
RowSize: Integer; // size in bytes of one scanline
// other data
RawPalette: array[0..4 * 256 - 1] of Byte;
//--------------- local functions -------------------------------------------
function ReadBlockHeader: Boolean;
// Fills in the block header variables according to the file version.
// Returns True if a block header could be read otherwise False (stream end).
begin
with Stream do
begin
Result := Position < Size;
if Result then
begin
ReadBuffer(HeaderIdentifier, SizeOf(HeaderIdentifier));
ReadBuffer(BlockIdentifier, SizeOf(BlockIdentifier));
if Header.MajorVersion = 3 then ReadBuffer(InitialChunkLength, SizeOf(InitialChunkLength));
ReadBuffer(TotalBlockLength, SizeOf(TotalBlockLength));
end;
end;
end;
//---------------------------------------------------------------------------
procedure ReadAndDecompress(Target: Pointer);
// reads a stream of data from file stream and decompresses it into Target
var
RawBuffer: Pointer;
Decoder: TDecoder;
Source: Pointer;
begin
Decoder := nil;
GetMem(RawBuffer, ChannelInfo.CompressedSize);
try
Stream.ReadBuffer(RawBuffer^, ChannelInfo.CompressedSize);
// pointer might be advanced while decoding, so use a copy
Source := RawBuffer;
case Image.Compression of
PSP_COMP_RLE:
begin
Decoder := TPSPRLEDecoder.Create;
Decoder.Decode(Source, Target, ChannelInfo.CompressedSize, ChannelInfo.UncompressedSize);
end;
PSP_COMP_LZ77:
begin
Decoder := TLZ77Decoder.Create(Z_FINISH, False);
Decoder.DecodeInit;
Decoder.Decode(Source, Target, ChannelInfo.CompressedSize, ChannelInfo.UncompressedSize);
end;
PSP_COMP_JPEG: // here just for completeness, used only in thumbnails and composite images
;
end;
Decoder.DecodeEnd;
finally
if Assigned(RawBuffer) then FreeMem(RawBuffer);
Decoder.Free;
end;
end;
//---------------------------------------------------------------------------
procedure ReadChannelData;
// Reads the actual data of one channel from the current stream position.
// Decompression is done by the way.
begin
with Stream do
begin
ReadBlockHeader;
if Header.MajorVersion > 3 then ReadBuffer(ChunkSize, SizeOf(ChunkSize));
ReadBuffer(ChannelInfo, SizeOf(ChannelInfo));
case ChannelInfo.ChannelType of
PSP_CHANNEL_COMPOSITE: // single channel bitmap (indexed or transparency mask)
begin
GetMem(CompBuffer, ChannelInfo.UncompressedSize);
if Image.Compression <> PSP_COMP_NONE then ReadAndDecompress(CompBuffer)
else ReadBuffer(CompBuffer^, ChannelInfo.CompressedSize);
end;
PSP_CHANNEL_RED: // red channel of 24 bit bitmap
begin
GetMem(RedBuffer, ChannelInfo.UncompressedSize);
if Image.Compression <> PSP_COMP_NONE then ReadAndDecompress(RedBuffer)
else ReadBuffer(RedBuffer^, ChannelInfo.CompressedSize);
end;
PSP_CHANNEL_GREEN:
begin
GetMem(GreenBuffer, ChannelInfo.UncompressedSize);
if Image.Compression <> PSP_COMP_NONE then ReadAndDecompress(GreenBuffer)
else ReadBuffer(GreenBuffer^, ChannelInfo.CompressedSize);
end;
PSP_CHANNEL_BLUE:
begin
GetMem(BlueBuffer, ChannelInfo.UncompressedSize);
if Image.Compression <> PSP_COMP_NONE then ReadAndDecompress(BlueBuffer)
else ReadBuffer(BlueBuffer^, ChannelInfo.CompressedSize);
end;
end;
end;
end;
//--------------- end local functions ---------------------------------------
begin
// free previous image data
Handle := 0;
FBasePosition := Stream.Position;
if ReadImageProperties(Stream, 0) then
begin
Stream.Position := FBasePosition;
RedBuffer := nil;
GreenBuffer := nil;
BlueBuffer := nil;
with Stream, FImageProperties do
try
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
// Note: To be robust with future PSP images any reader must be able to skip data
// which it doesn't know instead of relying on the size of known structures.
// Hence there's some extra work needed with the stream (mainly to keep the
// current position before a chunk is read and advancing the stream using the
// chunk size field).
ReadBuffer(Header, SizeOf(Header));
// read general image attribute block
ReadBlockHeader;
LastPosition := Position;
if Version > 3 then ReadBuffer(ChunkSize, SizeOf(ChunkSize));
ReadBuffer(Image, SizeOf(Image));
Position := LastPosition + TotalBlockLength;
with ColorManager, Image do
begin
SourceOptions := [];
SourceBitsPerSample := BitsPerSample;
TargetBitsPerSample := BitsPerSample;
SourceSamplesPerPixel := SamplesPerPixel;
TargetSamplesPerPixel := SamplesPerPixel;
SourceColorScheme := ColorScheme;
if ColorScheme = csRGB then TargetColorScheme := csBGR
else TargetColorScheme := ColorScheme;
PixelFormat := TargetPixelFormat;
end;
// set bitmap properties
RowSize := 0; // make compiler quiet
case BitsPerSample of
1:
RowSize := (Image.Width + 7) div 8;
4:
RowSize := Image.Width div 2 + 1;
8:
RowSize := Image.Width;
else
GraphicExError(gesInvalidColorFormat, ['PSP']);
end;
Self.Width := Width;
Self.Height := Height;
Progress(Self, psEnding, 0, False, FProgressRect, '');
// go through main blocks and read what is needed
repeat
if not ReadBlockHeader then Break;
NextMainBlock := Position + Integer(TotalBlockLength);
// no more blocks?
if HeaderIdentifier[0] <> '~' then Break;
case BlockIdentifier of
PSP_COMPOSITE_IMAGE_BANK_BLOCK:
begin
// composite image block, if present then it must appear before the layer start block
// and represents a composition of several layers
// do not need to read anything further
//Break;
end;
PSP_LAYER_START_BLOCK:
repeat
if not ReadBlockHeader then Break;
Progress(Self, psStarting, 0, False, FProgressRect, gesLoadingData);
// calculate start of next (layer) block in case we need to skip this one
NextLayerPosition := Position + Integer(TotalBlockLength);
// if all layers have been considered the break loop to continue with other blocks if necessary
if BlockIdentifier <> PSP_LAYER_BLOCK then Break;
// layer information chunk
if Version > 3 then
begin
LastPosition := Position;
ReadBuffer(ChunkSize, SizeOf(ChunkSize));
ReadBuffer(NameLength, SizeOf(NameLength));
SetLength(LayerName, NameLength);
if NameLength > 0 then ReadBuffer(LayerName[1], NameLength);
ReadBuffer(LayerInfo, SizeOf(LayerInfo));
Position := LastPosition + ChunkSize;
// continue only with undefined or raster chunks
if not (LayerInfo.LayerType in [PSP_LAYER_UNDEFINED, PSP_LAYER_RASTER]) then
begin
Position := NextLayerPosition;
Continue;
end;
// in file version 4 there's also an additional bitmap chunk which replaces
// two fields formerly located in the LayerInfo chunk
LastPosition := Position;
ReadBuffer(ChunkSize, SizeOf(ChunkSize));
end
else
begin
SetLength(LayerName, 256);
ReadBuffer(LayerName[1], 256);
ReadBuffer(LayerInfo, SizeOf(LayerInfo));
// continue only with normal (raster) chunks
if LayerInfo.LayerType <> PSP_LAYER_NORMAL then
begin
Position := NextLayerPosition;
Continue;
end;
end;
ReadBuffer(BitmapCount, SizeOf(BitmapCount));
ReadBuffer(ChannelCount, SizeOf(ChannelCount));
// But now we can reliably say whether we have an alpha channel or not.
// This kind of information can only be read very late and causes us to
// possibly reallocate the entire image (because it is copied by the VCL
// when changing the pixel format).
// I don't know another way (preferably before the size of the image is set).
if ChannelCount > 3 then
begin
ColorManager.TargetColorScheme := csBGRA;
PixelFormat := pf32Bit;
end;
if Version > 3 then Position := LastPosition + ChunkSize;
// allocate memory for all channels and read raw data
for X := 0 to ChannelCount - 1 do ReadChannelData;
Progress(Self, psEnding, 0, False, FProgressRect, '');
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
R := RedBuffer;
G := GreenBuffer;
B := BlueBuffer;
C := CompBuffer;
with ColorManager do
begin
if TargetColorScheme in [csIndexed, csG] then
begin
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([C], ScanLine[Y], Width, $FF);
Inc(C, RowSize);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end
else
begin
for Y := 0 to Height - 1 do
begin
ColorManager.ConvertRow([R, G, B, C], ScanLine[Y], Width, $FF);
Inc(R, RowSize);
Inc(G, RowSize);
Inc(B, RowSize);
Inc(C, RowSize);
Progress(Self, psRunning, MulDiv(Y, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
end;
Progress(Self, psEnding, 0, False, FProgressRect, '');
// after the raster layer has been read there's no need to loop further
Break;
until False; // layer loop
PSP_COLOR_BLOCK: // color palette block (this is also present for gray scale and b&w images)
begin
if Version > 3 then ReadBuffer(ChunkSize, SizeOf(ChunkSize));
ReadBuffer(Index, SizeOf(Index));
ReadBuffer(RawPalette, Index * SizeOf(TRGBQuad));
Palette := ColorManager.CreateColorPalette([@RawPalette], pfInterlaced8Quad, Index, True);
end;
end;
// explicitly set stream position to next main block as we might have read a block only partially
Position := NextMainBlock;
until False; // main block loop
finally
if Assigned(RedBuffer) then FreeMem(RedBuffer);
if Assigned(GreenBuffer) then FreeMem(GreenBuffer);
if Assigned(BlueBuffer) then FreeMem(BlueBuffer);
end;
end
else GraphicExError(gesInvalidImage, ['PSP']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPSPGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Header: TPSPFileHeader;
Image: TPSPImageAttributes;
// to use the code below for file 3 and 4 I read the parts of the block header
// separately instead as a structure
HeaderIdentifier: array[0..3] of AnsiChar; // i.e. "~BK" followed by a zero byte
BlockIdentifier: Word; // one of the block identifiers
InitialChunkLength, // length of the first sub chunk header or similar
TotalBlockLength: Cardinal; // length of this block excluding this header
LastPosition,
ChunkSize: Cardinal;
//--------------- local functions -------------------------------------------
function ReadBlockHeader: Boolean;
// Fills in the block header variables according to the file version.
// Returns True if a block header could be read otherwise False (stream end).
begin
with Stream do
begin
Result := Position < Size;
if Result then
begin
ReadBuffer(HeaderIdentifier, SizeOf(HeaderIdentifier));
ReadBuffer(BlockIdentifier, SizeOf(BlockIdentifier));
if Header.MajorVersion = 3 then ReadBuffer(InitialChunkLength, SizeOf(InitialChunkLength));
ReadBuffer(TotalBlockLength, SizeOf(TotalBlockLength));
end;
end;
end;
//--------------- end local functions ---------------------------------------
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
with Stream, FImageProperties do
begin
ReadBuffer(Header, SizeOf(Header));
if (StrLIComp(Header.Signature, MagicID, Length(MagicID)) = 0) and
(Header.MajorVersion >= 3) then
begin
Version := Header.MajorVersion;
// read general image attribute block
ReadBlockHeader;
LastPosition := Position;
if Header.MajorVersion > 3 then ReadBuffer(ChunkSize, SizeOf(ChunkSize));
ReadBuffer(Image, SizeOf(Image));
Position := LastPosition + TotalBlockLength;
if Image.BitDepth = 24 then
begin
BitsPerSample := 8;
SamplesPerPixel := 3;
ColorScheme := csRGB; // an alpha channel might exist, this is determined by the layer's channel count
end
else
begin
BitsPerSample := Image.BitDepth;
SamplesPerPixel := 1;
if Image.GreyscaleFlag then ColorScheme := csG
else ColorScheme := csIndexed;
end;
BitsPerPixel := BitsPerSample * SamplesPerPixel;
Width := Image.Width;
Height := Image.Height;
case Image.Compression of
PSP_COMP_NONE:
Compression := ctNone;
PSP_COMP_RLE:
Compression := ctRLE;
PSP_COMP_LZ77:
Compression := ctLZ77;
PSP_COMP_JPEG:
Compression := ctJPEG;
else
Compression := ctUnknown;
end;
XResolution := Image.Resolution;
if Image.ResolutionMetric = PSP_METRIC_CM then XResolution := XResolution * 2.54;
YResolution := XResolution;
Result := True;
end;
end;
end;
{$endif} // PaintshopProGraphic
//----------------- TPNGGraphic ----------------------------------------------------------------------------------------
{$ifdef PortableNetworkGraphic}
const
PNGMagic: array[0..7] of Byte = (137, 80, 78, 71, 13, 10, 26, 10);
// recognized and handled chunk types
IHDR = 'IHDR';
IDAT = 'IDAT';
IEND = 'IEND';
PLTE = 'PLTE';
gAMA = 'gAMA';
tRNS = 'tRNS';
bKGD = 'bKGD';
CHUNKMASK = $20; // used to check bit 5 in chunk types
type
// The following chunks structures are those which appear in the data field of the general chunk structure
// given above.
// chunk type: 'IHDR'
PIHDRChunk = ^TIHDRChunk;
TIHDRChunk = packed record
Width,
Height: Cardinal;
BitDepth, // bits per sample (allowed are 1, 2, 4, 8 and 16)
ColorType, // combination of:
// 1 - palette used
// 2 - colors used
// 4 - alpha channel used
// allowed values are:
// 0 - gray scale (allowed bit depths are: 1, 2, 4, 8, 16)
// 2 - RGB (8, 16)
// 3 - palette (1, 2, 4, 8)
// 4 - gray scale with alpha (8, 16)
// 6 - RGB with alpha (8, 16)
Compression, // 0 - LZ77, others are not yet defined
Filter, // filter mode 0 is the only one currently defined
Interlaced: Byte; // 0 - not interlaced, 1 - Adam7 interlaced
end;
//----------------------------------------------------------------------------------------------------------------------
function TPNGGraphic.CanLoad(Stream: TStream): Boolean;
var
Magic: array[0..7] of Byte;
LastPosition: Cardinal;
begin
with Stream do
begin
LastPosition := Position;
Result := (Size - Position) > SizeOf(Magic);
if Result then
begin
ReadBuffer(Magic, SizeOf(Magic));
Result := CompareMem(@Magic, @PNGMagic, 8);
end;
Position := LastPosition;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPNGGraphic.IsChunk(ChunkType: TChunkType): Boolean;
// determines, independant of the cruxial 5ths bits in each "letter", whether the
// current chunk type in the header is the same as the given chunk type
const
Mask = not $20202020;
var
chtype:Cardinal;
begin
chtype:=Cardinal(FHeader.ChunkType);
Result := (chtype and Mask) = (Cardinal(ChunkType) and Mask);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPNGGraphic.LoadAndSwapHeader: Cardinal;
// read next chunk header and swap fields to little endian,
// returns the intial CRC value for following checks
begin
FStream.ReadBuffer(FHeader, SizeOf(FHeader));
Result := CRC32(0, @FHeader.ChunkType, 4);
FHeader.Length := SwapLong(FHeader.Length);
end;
//----------------------------------------------------------------------------------------------------------------------
function PaethPredictor(a, b, c: Byte): Byte;
var
p, pa, pb, pc: Integer;
begin
// a = left, b = above, c = upper left
p := a + b - c; // initial estimate
pa := Abs(p - a); // distances to a, b, c
pb := Abs(p - b);
pc := Abs(p - c);
// return nearest of a, b, c, breaking ties in order a, b, c
if (pa <= pb) and (pa <= pc) then Result := a
else
if pb <= pc then Result := b
else Result := c;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.ApplyFilter(Filter: Byte; Line, PrevLine, Target: PByte; BPP, BytesPerRow: Integer);
// Applies the filter given in Filter to all bytes in Line (eventually using PrevLine).
// Note: The filter type is assumed to be of filter mode 0, as this is the only one currently
// defined in PNG.
// In opposition to the PNG documentation different identifiers are used here.
// Raw refers to the current, not yet decoded value. Decoded refers to the current, already
// decoded value (this one is called "raw" in the docs) and Prior is the current value in the
// previous line. For the Paeth prediction scheme a fourth pointer is used (PriorDecoded) to describe
// the value in the previous line but less the BPP value (Prior[x - BPP]).
var
I: Integer;
Raw,
Decoded,
Prior,
PriorDecoded,
TargetRun: PByte;
begin
case Filter of
0: // no filter, just copy data
Move(Line^, Target^, BytesPerRow);
1: // subtraction filter
begin
Raw := Line;
TargetRun := Target;
// Transfer BPP bytes without filtering. This mimics the effect of bytes left to the
// scanline being zero.
Move(Raw^, TargetRun^, BPP);
// now do rest of the line
Decoded := TargetRun;
Inc(Raw, BPP);
Inc(TargetRun, BPP);
Dec(BytesPerRow, BPP);
while BytesPerRow > 0 do
begin
TargetRun^ := Byte(Raw^ + Decoded^);
Inc(Raw);
Inc(Decoded);
Inc(TargetRun);
Dec(BytesPerRow);
end;
end;
2: // Up filter
begin
Raw := Line;
Prior := PrevLine;
TargetRun := Target;
while BytesPerRow > 0 do
begin
TargetRun^ := Byte(Raw^ + Prior^);
Inc(Raw);
Inc(Prior);
Inc(TargetRun);
Dec(BytesPerRow);
end;
end;
3: // average filter
begin
// first handle BPP virtual pixels to the left
Raw := Line;
Decoded := Line;
Prior := PrevLine;
TargetRun := Target;
for I := 0 to BPP - 1 do
begin
TargetRun^ := Byte(Raw^ + Floor(Prior^ / 2));
Inc(Raw);
Inc(Prior);
Inc(TargetRun);
end;
Dec(BytesPerRow, BPP);
// now do rest of line
while BytesPerRow > 0 do
begin
TargetRun^ := Byte(Raw^ + Floor((Decoded^ + Prior^) / 2));
Inc(Raw);
Inc(Decoded);
Inc(Prior);
Inc(TargetRun);
Dec(BytesPerRow);
end;
end;
4: // paeth prediction
begin
// again, start with first BPP pixel which would refer to non-existing pixels to the left
Raw := Line;
Decoded := Target;
Prior := PrevLine;
PriorDecoded := PrevLine;
TargetRun := Target;
for I := 0 to BPP - 1 do
begin
TargetRun^ := Byte(Raw^ + PaethPredictor(0, Prior^, 0));
Inc(Raw);
Inc(Prior);
Inc(TargetRun);
end;
Dec(BytesPerRow, BPP);
// finally do rest of line
while BytesPerRow > 0 do
begin
TargetRun^ := Byte(Raw^ + PaethPredictor(Decoded^, Prior^, PriorDecoded^));
Inc(Raw);
Inc(Decoded);
Inc(Prior);
Inc(PriorDecoded);
Inc(TargetRun);
Dec(BytesPerRow);
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.LoadFromStream(Stream: TStream);
var
Description: TIHDRChunk;
begin
// free previous image data
Handle := 0;
FBasePosition := Stream.Position;
FDecoder := nil;
FStream := Stream;
if ReadImageProperties(Stream, 0) then
begin
with Stream, FImageProperties do
begin
Position := FBasePosition + 8; // skip magic
FProgressRect := Rect(0, 0, Width, 1);
Progress(Self, psStarting, 0, False, FProgressRect, gesPreparing);
FPalette := 0;
FTransparency := nil;
FBackgroundColor := clWhite;
FTransparentColor := clNone;
// first chunk must be an IHDR chunk
FCurrentCRC := LoadAndSwapHeader;
FRawBuffer := nil;
ColorManager.SourceOptions := [coNeedByteSwap];
try
// read IHDR chunk
ReadDataAndCheckCRC;
Move(FRawBuffer^, Description, SizeOf(Description));
SwapLong(@Description, 2);
// currently only one compression type is supported by PNG (LZ77)
if Compression = ctLZ77 then
begin
FDecoder := TLZ77Decoder.Create(Z_PARTIAL_FLUSH, False);
FDecoder.DecodeInit;
end
else
GraphicExError(gesUnsupportedFeature, [gesCompressionScheme, 'PNG']);
// setup is done, now go for the chunks
repeat
FCurrentCRC := LoadAndSwapHeader;
if IsChunk(IDAT) then
begin
Progress(Self, psEnding, 0, False, FProgressRect, '');
LoadIDAT(Description);
// After reading the image data the next chunk header has already been loaded
// so continue with code below instead trying to load a new chunk header.
end
else
if IsChunk(PLTE) then
begin
// palette chunk
if (FHeader.Length mod 3) <> 0 then GraphicExError(gesInvalidPalette, ['PNG']);
ReadDataAndCheckCRC;
// load palette only if the image is indexed colors
if Description.ColorType = 3 then
begin
// first setup pixel format before actually creating a palette
FSourceBPP := SetupColorDepth(Description.ColorType, Description.BitDepth);
FPalette := ColorManager.CreateColorPalette([FRawBuffer], pfInterlaced8Triple, FHeader.Length div 3, False);
end;
Continue;
end
else
if IsChunk(gAMA) then
begin
ReadDataAndCheckCRC;
// the file gamme given here is a scaled cardinal (e.g. 0.45 is expressed as 45000)
ColorManager.SetGamma(SwapLong(PCardinal(FRawBuffer)^) / 100000);
ColorManager.TargetOptions := ColorManager.TargetOptions + [coApplyGamma];
Include(Options, ioUseGamma);
Continue;
end
else
if IsChunk(bKGD) then
begin
LoadBackgroundColor(Description);
Continue;
end
else
if IsChunk(tRNS) then
begin
LoadTransparency(Description);
Continue;
end;
// Skip unknown or unsupported chunks (+4 because of always present CRC).
// IEND will be skipped as well, but this chunk is empty, so the stream will correctly
// end on the first byte after the IEND chunk.
Seek(FHeader.Length + 4, soFromCurrent);
if IsChunk(IEND) then Break;
// Note: According to the specs an unknown, but as critical marked chunk is a fatal error.
if (Byte(FHeader.ChunkType[0]) and CHUNKMASK) = 0 then GraphicExError(gesUnknownCriticalChunk);
until False;
finally
if Assigned(FDecoder) then
begin
FDecoder.DecodeEnd;
FDecoder.Free;
end;
if Assigned(FRawBuffer) then FreeMem(FRawBuffer);
Progress(Self, psEnding, 0, False, FProgressRect, '');
end;
end;
end
else GraphicExError(gesInvalidImage, ['PNG']);
end;
//----------------------------------------------------------------------------------------------------------------------
function TPNGGraphic.ReadImageProperties(Stream: TStream; ImageIndex: Cardinal): Boolean;
var
Magic: array[0..7] of Byte;
Description: TIHDRChunk;
begin
Result := inherited ReadImageProperties(Stream, ImageIndex);
FStream := Stream;
with Stream, FImageProperties do
begin
ReadBuffer(Magic, 8);
if CompareMem(@Magic, @PNGMagic, 8) then
begin
// first chunk must be an IHDR chunk
FCurrentCRC := LoadAndSwapHeader;
if IsChunk(IHDR) then
begin
Include(Options, ioBigEndian);
// read IHDR chunk
ReadDataAndCheckCRC;
Move(FRawBuffer^, Description, SizeOf(Description));
SwapLong(@Description, 2);
if (Description.Width = 0) or (Description.Height = 0) then Exit;
Width := Description.Width;
Height := Description.Height;
if Description.Compression = 0 then Compression := ctLZ77
else Compression := ctUnknown;
BitsPerSample := Description.BitDepth;
SamplesPerPixel := 1;
case Description.ColorType of
0:
ColorScheme := csG;
2:
begin
ColorScheme := csRGB;
SamplesPerPixel := 3;
end;
3:
ColorScheme := csIndexed;
4:
ColorScheme := csGA;
6:
begin
ColorScheme := csRGBA;
SamplesPerPixel := 4;
end;
else
ColorScheme := csUnknown;
end;
BitsPerPixel := SamplesPerPixel * BitsPerSample;
FilterMode := Description.Filter;
Interlaced := Description.Interlaced <> 0;
HasAlpha := ColorScheme in [csGA, csRGBA, csBGRA];
// find gamma
repeat
FCurrentCRC := LoadAndSwapHeader;
if IsChunk(gAMA) then
begin
ReadDataAndCheckCRC;
// the file gamme given here is a scaled cardinal (e.g. 0.45 is expressed as 45000)
FileGamma := SwapLong(PCardinal(FRawBuffer)^) / 100000;
Break;
end;
Seek(FHeader.Length + 4, soFromCurrent);
if IsChunk(IEND) then Break;
until False;
Result := True;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.LoadBackgroundColor(const Description);
// loads the data from the current chunk (must be a bKGD chunk) and fills the bitmpap with that color
var
Run: PWord;
R, G, B: Byte;
begin
ReadDataAndCheckCRC;
with TIHDRChunk(Description) do
begin
case ColorType of
0, 4: // G(A)
begin
case BitDepth of
2:
FBackgroundColor := MulDiv16(Swap(PWord(FRawBuffer)^), 15, 3);
16:
FBackgroundColor := MulDiv16(Swap(PWord(FRawBuffer)^), 255, 65535);
else // 1, 4, 8 bits gray scale
FBackgroundColor := Byte(Swap(PWord(FRawBuffer)^));
end;
end;
2, 6: // RGB(A)
begin
Run := FRawBuffer;
if BitDepth = 16 then
begin
R := MulDiv16(Swap(Run^), 255, 65535); Inc(Run);
G := MulDiv16(Swap(Run^), 255, 65535); Inc(Run);
B := MulDiv16(Swap(Run^), 255, 65535);
end
else
begin
R := Byte(Swap(Run^)); Inc(Run);
G := Byte(Swap(Run^)); Inc(Run);
B := Byte(Swap(Run^));
end;
FBackgroundColor := RGB(R, G, B);
end;
else // indexed color scheme (3)
FBackgroundColor := PByte(FRawBuffer)^;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.LoadIDAT(const Description);
// loads image data from the current position of the stream
const
// interlace start and offsets
RowStart: array[0..6] of Integer = (0, 0, 4, 0, 2, 0, 1);
ColumnStart: array[0..6] of Integer = (0, 4, 0, 2, 0, 1, 0);
RowIncrement: array[0..6] of Integer = (8, 8, 8, 4, 4, 2, 2);
ColumnIncrement: array[0..6] of Integer = (8, 8, 4, 4, 2, 2, 1);
PassMask: array[0..6] of Byte = ($80, $08, $88, $22, $AA, $55, $FF);
var
Row: Integer;
TargetBPP: Integer;
RowBuffer: array[Boolean] of PAnsiChar; // I use PAnsiChar here instead of simple pointer to ease pointer math below
EvenRow: Boolean; // distincts between the two rows we need to hold for filtering
Pass: Integer;
BytesPerRow,
InterlaceRowBytes,
InterlaceWidth: Integer;
begin
Progress(Self, psStarting, 0, False, FProgressRect, gesTransfering);
RowBuffer[False] := nil;
RowBuffer[True] := nil;
try
// adjust pixel format etc. if not yet done
if PixelFormat = pfDevice then
FSourceBPP := SetupColorDepth(TIHDRChunk(Description).ColorType, TIHDRChunk(Description).BitDepth);
if TIHDRChunk(Description).BitDepth = 16 then TargetBPP := FSourceBPP div 2
else TargetBPP := FSourceBPP;
if FPalette <> 0 then Palette := FPalette;
// after setting the pixel format we can set the dimensions too without
// initiating color conversions
Width := TIHDRChunk(Description).Width;
Height := TIHDRChunk(Description).Height;
// set background and transparency color, these values must be set after the
// bitmap is actually valid (although, not filled)
Canvas.Lock;
try
Canvas.Brush.Color := FBackgroundColor;
Canvas.FillRect(Rect(0, 0, Width, Height));
finally
Canvas.Unlock;
end;
if FTransparentColor <> clNone then
begin
TransparentColor := FTransparentColor;
Transparent := True;
end;
// determine maximum number of bytes per row and consider there's one filter byte at the start of each row
BytesPerRow := TargetBPP * ((Width * TIHDRChunk(Description).BitDepth + 7) div 8) + 1;
RowBuffer[True] := AllocMem(BytesPerRow);
RowBuffer[False] := AllocMem(BytesPerRow);
// there can be more than one IDAT chunk in the file but then they must directly
// follow each other (handled in ReadRow)
EvenRow := True;
// prepare interlaced images
if TIHDRChunk(Description).Interlaced = 1 then
begin
for Pass := 0 to 6 do
begin
// prepare next interlace run
if Width <= ColumnStart[Pass] then Continue;
InterlaceWidth := (Width + ColumnIncrement[Pass] - 1 - ColumnStart[Pass]) div ColumnIncrement[Pass];
InterlaceRowBytes := TargetBPP * ((InterlaceWidth * TIHDRChunk(Description).BitDepth + 7) div 8) + 1;
Row := RowStart[Pass];
while Row < Height do
begin
ReadRow(RowBuffer[EvenRow], InterlaceRowBytes);
ApplyFilter(Byte(RowBuffer[EvenRow]^),
Pointer(RowBuffer[EvenRow] + 1),
Pointer(RowBuffer[not EvenRow] + 1),
Pointer(RowBuffer[EvenRow] + 1),
FSourceBPP,
InterlaceRowBytes - 1);
ColorManager.ConvertRow([Pointer(RowBuffer[EvenRow] + 1)], ScanLine[Row], Width, PassMask[Pass]);
EvenRow := not EvenRow;
// continue with next row in interlaced order
Inc(Row, RowIncrement[Pass]);
if Pass = 6 then
begin
// progress event only for last (and most expensive) pass
Progress(Self, psRunning, MulDiv(Row, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
end;
end
else
begin
for Row := 0 to Height - 1 do
begin
ReadRow(RowBuffer[EvenRow], BytesPerRow);
ApplyFilter(Byte(RowBuffer[EvenRow]^),
Pointer(RowBuffer[EvenRow] + 1),
Pointer(RowBuffer[not EvenRow] + 1),
Pointer(RowBuffer[EvenRow] + 1),
FSourceBPP,
BytesPerRow - 1);
ColorManager.ConvertRow([Pointer(RowBuffer[EvenRow] + 1)], ScanLine[Row], Width, $FF);
EvenRow := not EvenRow;
Progress(Self, psRunning, MulDiv(Row, 100, Height), True, FProgressRect, '');
OffsetRect(FProgressRect, 0, 1);
end;
end;
// in order to improve safe failness we read all remaining but not read IDAT chunks here
while IsChunk(IDAT) do
begin
ReadDataAndCheckCRC;
FCurrentCRC := LoadAndSwapHeader;
end;
finally
if Assigned(RowBuffer[True]) then FreeMem(RowBuffer[True]);
if Assigned(RowBuffer[False]) then FreeMem(RowBuffer[False]);
end;
// ending progress event is issued in main method
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.LoadTransparency(const Description);
// reads the data of the current transparency chunk
var
Run: PWord;
R, G, B: Byte;
begin
ReadDataAndCheckCRC;
with TIHDRChunk(Description) do
begin
case ColorType of
0: // gray
begin
case BitDepth of
2:
R := MulDiv16(Swap(PWord(FRawBuffer)^), 15, 3);
16:
R := MulDiv16(Swap(PWord(FRawBuffer)^), 255, 65535);
else // 1, 4, 8 bits gray scale
R := Byte(Swap(PWord(FRawBuffer)^));
end;
FTransparentColor := RGB(R, R, R);
end;
2: // RGB
begin
Run := FRawBuffer;
if BitDepth = 16 then
begin
R := MulDiv16(Swap(Run^), 255, 65535); Inc(Run);
G := MulDiv16(Swap(Run^), 255, 65535); Inc(Run);
B := MulDiv16(Swap(Run^), 255, 65535);
end
else
begin
R := Byte(Swap(Run^)); Inc(Run);
G := Byte(Swap(Run^)); Inc(Run);
B := Byte(Swap(Run^));
end;
FTransparentColor := RGB(R, G, B);
end;
4, 6:
// formats with full alpha channel, they shouldn't have a transparent color
else
// Indexed color scheme (3), with at most 256 alpha values (for each palette entry).
SetLength(FTransparency, 255);
// read the values (at most 256)...
Move(FRawBuffer^, FTransparency[0], Max(FHeader.Length, 256));
// ...and set default values (255, fully opaque) for non-supplied values
if FHeader.Length < 256 then FillChar(FTransparency[FHeader.Length], 256 - FHeader.Length, $FF);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.ReadDataAndCheckCRC;
// Allocates memory in FRawBuffer and reads the next Header.Length bytes from Stream.
// Furthermore, the CRC value following the data is read as well and compared with
// the CRC value which is calculated here.
var
FileCRC: Cardinal;
begin
ReallocMem(FRawBuffer, FHeader.Length);
FStream.ReadBuffer(FRawBuffer^, FHeader.Length);
FStream.ReadBuffer(FileCRC, SizeOf(FileCRC));
FileCRC := SwapLong(FileCRC);
// The type field of a chunk is included in the CRC, this serves as initial value
// for the calculation here and is determined in LoadAndSwapHeader.
FCurrentCRC := CRC32(FCurrentCRC, FRawBuffer, FHeader.Length);
if FCurrentCRC <> FileCRC then GraphicExError(gesInvalidCRC, ['PNG']);
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TPNGGraphic.ReadRow(RowBuffer: Pointer; BytesPerRow: Integer);
// reads and decodes one scanline
var
LocalBuffer: Pointer;
PendingOutput: Integer;
begin
LocalBuffer := RowBuffer;
PendingOutput := BytesPerRow;
repeat
// read pending chunk data if available input has dropped to zero
if FDecoder.AvailableInput = 0 then
begin
FIDATSize := 0;
// read all following chunks until enough data is available or there is no further IDAT chunk
while FIDATSize = 0 do
begin
// finish if the current chunk is not an IDAT chunk
if not IsChunk(IDAT) then Exit;
ReadDataAndCheckCRC;
FCurrentSource := FRawBuffer;
FIDATSize := FHeader.Length;
// prepare next chunk (plus CRC)
FCurrentCRC := LoadAndSwapHeader;
end;
end;
// this decode call will advance Source and Target accordingly
FDecoder.Decode(FCurrentSource,
LocalBuffer,
FIDATSize - (Integer(FCurrentSource) - Integer(FRawBuffer)),
PendingOutput);
if FDecoder.ZLibResult = Z_STREAM_END then
begin
if (FDecoder.AvailableOutput <> 0) or
(FDecoder.AvailableInput <> 0) then GraphicExError(gesExtraCompressedData, ['PNG']);
Break;
end;
if FDecoder.ZLibResult <> Z_OK then GraphicExError(gesCompression, ['PNG']);
PendingOutput := BytesPerRow - (Integer(LocalBuffer) - Integer(RowBuffer));
until PendingOutput = 0;
end;
//----------------------------------------------------------------------------------------------------------------------
function TPNGGraphic.SetupColorDepth(ColorType, BitDepth: Integer): Integer;
begin
Result := 0;
// determine color scheme and setup related stuff,
// Note: The calculated BPP value is always at least 1 even for 1 bits per pixel etc. formats
// and used in filter calculation.
case ColorType of
0: // gray scale (allowed bit depths are: 1, 2, 4, 8, 16 bits)
if BitDepth in [1, 2, 4, 8, 16] then
with ColorManager do
begin
SourceColorScheme := csG;
TargetColorScheme := csG;
SourceSamplesPerPixel := 1;
TargetSamplesPerPixel := 1;
SourceBitsPerSample := BitDepth;
// 2 bits values are converted to 4 bits values because DIBs don't know the former variant
case BitDepth of
2:
TargetBitsPerSample := 4;
16:
TargetBitsPerSample := 8;
else
TargetBitsPerSample := BitDepth;
end;
PixelFormat := TargetPixelFormat;
FPalette := CreateGrayscalePalette(False);
Result := (BitDepth + 7) div 8;
end
else GraphicExError(gesInvalidColorFormat, ['PNG']);
2: // RGB
if BitDepth in [8, 16] then
with ColorManager do
begin
SourceSamplesPerPixel := 3;
TargetSamplesPerPixel := 3;
SourceColorScheme := csRGB;
TargetColorScheme := csBGR;
SourceBitsPerSample := BitDepth;
TargetBitsPerSample := 8;
PixelFormat := pf24Bit;
Result := BitDepth * 3 div 8;
end
else GraphicExError(gesInvalidColorFormat, ['PNG']);
3: // palette
if BitDepth in [1, 2, 4, 8] then
with ColorManager do
begin
SourceColorScheme := csIndexed;
TargetColorScheme := csIndexed;
SourceSamplesPerPixel := 1;
TargetSamplesPerPixel := 1;
SourceBitsPerSample := BitDepth;
// 2 bits values are converted to 4 bits values because DIBs don't know the former variant
if BitDepth = 2 then TargetBitsPerSample := 4
else TargetBitsPerSample := BitDepth;
PixelFormat := TargetPixelFormat;
Result := 1;
end
else GraphicExError(gesInvalidColorFormat, ['PNG']);
4: // gray scale with alpha,
// For the moment this format is handled without alpha, but might later be converted
// to RGBA with gray pixels or use a totally different approach.
if BitDepth in [8, 16] then
with ColorManager do
begin
SourceSamplesPerPixel := 1;
TargetSamplesPerPixel := 1;
SourceBitsPerSample := BitDepth;
TargetBitsPerSample := 8;
SourceColorScheme := csGA;
TargetColorScheme := csIndexed;
PixelFormat := pf8Bit;
FPalette := CreateGrayScalePalette(False);
Result := 2 * BitDepth div 8;
end
else GraphicExError(gesInvalidColorFormat, ['PNG']);
6: // RGB with alpha (8, 16)
if BitDepth in [8, 16] then
with ColorManager do
begin
SourceSamplesPerPixel := 4;
TargetSamplesPerPixel := 4;
SourceColorScheme := csRGBA;
TargetColorScheme := csBGRA;
SourceBitsPerSample := BitDepth;
TargetBitsPerSample := 8;
PixelFormat := pf32Bit;
Result := BitDepth * 4 div 8;
end
else GraphicExError(gesInvalidColorFormat, ['PNG']);
else
GraphicExError(gesInvalidColorFormat, ['PNG']);
end;
end;
{$endif} // PortableNetworkGraphic
//----------------- TFileFormatList ------------------------------------------------------------------------------------
type
PClassEntry = ^TClassEntry;
TClassEntry = record
GraphicClass: TGraphicClass;
Description: AnsiString;
Count: Cardinal;
end;
PExtensionEntry = ^TExtensionEntry;
TExtensionEntry = record
Extension,
Description: AnsiString;
FormatTypes: TFormatTypes;
ClassReference: PClassEntry;
end;
constructor TFileFormatList.Create;
begin
FClassList := TList.Create;
FExtensionList := TList.Create;
end;
//----------------------------------------------------------------------------------------------------------------------
destructor TFileFormatList.Destroy;
begin
Clear;
FClassList.Free;
FExtensionList.Free;
inherited;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TFileFormatList.Clear;
var
I: Integer;
begin
for I := 0 to FClassList.Count - 1 do
begin
TPicture.UnregisterGraphicClass(PClassEntry(FClassList[I]).GraphicClass);
Dispose(PClassEntry(FClassList[I])); // need Dispose with type casting to free strings too
end;
FClassList.Clear;
for I := 0 to FExtensionList.Count - 1 do
Dispose(PExtensionEntry(FExtensionList[I]));
FExtensionList.Clear;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.FindExtension(const Extension: AnsiString): Integer;
// Returns the entry which belongs to the given extension AnsiString or -1 if there's nothing in the list for this ext.
var
I: Integer;
begin
Result := -1;
if Extension <> '' then
for I := 0 to FExtensionList.Count - 1 do
if CompareText(PExtensionEntry(FExtensionList[I]).Extension, Extension) = 0 then
begin
Result := I;
Break;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.FindGraphicClass(GraphicClass: TGraphicClass): Integer;
// returns the entry index which belongs to the given graphic class or -1
var
I: Integer;
begin
Result := -1;
for I := 0 to FClassList.Count - 1 do
if PClassEntry(FClassList[I]).GraphicClass = GraphicClass then
begin
Result := I;
Break;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.GetDescription(Graphic: TGraphicClass): AnsiString;
// returns the registered description AnsiString for the given class
var
I: Integer;
begin
Result := '';
I := FindGraphicClass(Graphic);
if I > -1 then Result := PClassEntry(FClassList[I]).Description;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TFileFormatList.GetExtensionList(List: TStrings);
// returns a list of registered extensions (letters only, no *. part)
var
I: Integer;
ExtEntry: PExtensionEntry;
begin
List.Clear;
for I := 0 to FExtensionList.Count - 1 do
begin
ExtEntry := FExtensionList[I];
List.Add(ExtEntry.Extension);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.GetGraphicFilter(Formats: TFormatTypes; SortType: TFilterSortType;
Options: TFilterOptions; GraphicClass: TGraphicClass): AnsiString;
// Creates a AnsiString which can directly be used in an open or save dialog's filter property.
// Formats may be used to limit the number of formats to return.
// SortType determines how to sort the entries.
// Compact determines whether to group extensions (= True) or to put every extension on a separate line.
// AllImages finally determines whether to include the 'All image file' entry which includes all allowed extensions
// which qualify by the other properties.
// Usually all these options determine quite nicely which formats are well suited for a particular task
// but sometimes you may find it better to specify a graphic class to limit returned formats further.
// In this case set GraphicClass to the particular class otherwise set it nil.
var
I, J: Integer;
DL, EL, All: TStringList;
ExtEntry: PExtensionEntry;
ClassEntry: PClassEntry;
S,
DescriptionFormat: AnsiString;
begin
Result := '';
if Formats = [] then Formats := [ftAnimation..ftVector];
DL := TStringList.Create;
DL.Sorted := SortType in [fstDescription, fstBoth];
EL := TStringList.Create;
EL.Sorted := SortType in [fstExtension, fstBoth];
// this AnsiString list is used to hold the (possibly sorted) list of all allowed extensions
All := TStringList.Create;
All.Sorted := SortType in [fstExtension, fstBoth];
// using an adjusted format AnsiString makes the code below easier for different options
DescriptionFormat := '%s';
if foIncludeExtension in Options then DescriptionFormat := DescriptionFormat + '%s';
if foCompact in Options then
begin
// all extension for a particular image class on one line
for I := 0 to FClassList.Count - 1 do
begin
ClassEntry := FClassList[I];
if (GraphicClass = nil) or (GraphicClass = ClassEntry.GraphicClass) then
begin
EL.Clear;
// collect allowed extensions for the current graphic class,
// this will automatically sort the entries if wanted
for J := 0 to FExtensionList.Count - 1 do
begin
ExtEntry := FExtensionList[J];
if (ExtEntry.ClassReference = ClassEntry) and ((ExtEntry.FormatTypes * Formats) <> []) then
EL.Add(ExtEntry.Extension);
end;
// build the extension list and an description entry
if foIncludeAll in Options then All.AddStrings(EL);
S := '';
for J := 0 to EL.Count - 1 do S := S + '*.' + EL[J] + '; ';
// remove last semicolon and space
SetLength(S, Length(S) - 2);
if S <> '' then DL.AddObject(ClassEntry.Description, Pointer(StrNew(PAnsiChar(S))));
end;
end;
end
else
begin
// list each extension separately
for I := 0 to FExtensionList.Count - 1 do
begin
ExtEntry := FExtensionList[I];
if ((GraphicClass = nil) or (ExtEntry.ClassReference.GraphicClass = GraphicClass)) and
((ExtEntry.FormatTypes * Formats) <> []) then
begin
S := ExtEntry.Description;
if S = '' then S := ExtEntry.ClassReference.Description;
DL.AddObject(S, Pointer(StrNew(PAnsiChar('*.' + ExtEntry.Extension))));
if foIncludeAll in Options then All.Add(ExtEntry.Extension);
end;
end;
end;
// build final filter AnsiString out of the collected sub strings
if (foIncludeAll in Options) and (All.Count > 0) then
begin
// first include the general entry if wanted (this entry is never taken into sort order
S := '';
for J := 0 to All.Count - 1 do S := S + '*.' + All[J] + '; ';
SetLength(S, Length(S) - 2);
Result := gesAllImages + '|' + S + '|';
end;
for I := 0 to DL.Count - 1 do
begin
S := PAnsiChar(DL.Objects[I]);
StrDispose(PAnsiChar(DL.Objects[I]));
Result := Result + Format(DescriptionFormat, [DL[I], ' (' + S + ')']) + '|' + S + '|';
end;
// remove last separator in AnsiString
if Length(Result) > 0 then SetLength(Result, Length(Result) - 1);
All.Free;
EL.Free;
DL.Free;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.GraphicFromExtension(S: AnsiString): TGraphicClass;
// Returns the class which belongs to the extension given in S or nil if there's non registered.
// S may contain a regular file name (also UNC is allowed), a AnsiString returned from ExtractFileExt (with period) or just
// an extension AnsiString.
var
Index: Integer;
begin
Result := nil;
Index := Pos('.', S);
if Index > 0 then Delete(S, 1, Index);
Index := FindExtension(S);
if Index > -1 then Result := PExtensionEntry(FExtensionList[Index]).ClassReference.GraphicClass;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.GraphicFromContent(const FileName: AnsiString): TGraphicExGraphicClass;
// description see other overloaded version
var
Stream: TFileStream;
begin
Stream := TFileStream.Create(FileName, fmOpenRead or fmShareDenyWrite);
try
Result := GraphicFromContent(Stream);
finally
Stream.Free;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
function TFileFormatList.GraphicFromContent(Stream: TStream): TGraphicExGraphicClass;
// Determines the type of image in the stream. This test is only available for TGraphicExGraphic
// classes (this excludes TBitmap, TIcon, TMetaFile etc.).
// Note: Not all image types can be found using this code because they are not
// uniquely identifyable (e.g. Dr. Halo *.cut images).
var
I: Integer;
T: TGraphicExGraphicClass;
TInst:TGraphicExGraphic;
begin
Result := nil;
if (Stream<>nil) then
begin
for I := 0 to FClassList.Count - 1 do
begin
if PClassEntry(FClassList[I]).GraphicClass.InheritsFrom(TGraphicExGraphic) then
begin
T := TGraphicExGraphicClass(PClassEntry(FClassList[I]).GraphicClass);
TInst:=T.Create;
try
if TInst.CanLoad(Stream) then
begin
Result := T;
end;
finally
TInst.free;
end;
if Assigned(Result) then
break;
end;
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TFileFormatList.RegisterFileFormat(const Extension, Common, Individual: AnsiString; FormatTypes: TFormatTypes;
Replace, RegisterDefault: Boolean; GraphicClass: TGraphicClass);
// Registers the given graphic class with the passed extension AnsiString. If there's already a class registered with this
// extension then either the registration of the older entry is replaced by the new one (Replace = True) or an exception
// is raised.
// This method takes also care to register the new extension with TPicture to make the default handling work too
// if RegisterDefault is True.
// Further parameters are:
// - Extension: the new extension to be registered (not necessarily with only 3 characters, but without a period).
// - Common: a description AnsiString for all extensions registered with the same class used when several extensions are
// listed on one filter line. Pass '' to avoid changing a previously set value if there's one.
// - Individual: a description AnsiString used when each extension is listed separately.
// - FormatTypes: classifies the given file type as being a raster or vector file, with single or multiple images etc.
// - GraphicClass: the TGraphic descentant to be used to load and save the particular file.
var
ExtIndex,
ClassIndex: Integer;
ExtEntry: PExtensionEntry;
ClassEntry,
OldReference: PClassEntry;
//--------------- local functions -------------------------------------------
procedure UpdateClassEntry;
// updates a class entry (creates one if necessary)
begin
if ClassIndex = -1 then
begin
New(ClassEntry);
ClassEntry.GraphicClass := GraphicClass;
ClassEntry.Count := 0;
FClassList.Add(ClassEntry);
end
else
ClassEntry := FClassList[ClassIndex];
if Common <> '' then ClassEntry.Description := Common;
Inc(ClassEntry.Count);
ExtEntry.ClassReference := ClassEntry;
end;
//--------------- end local functions ---------------------------------------
var
S: AnsiString;
begin
if Extension <> '' then
begin
ExtIndex := FindExtension(Extension);
ClassIndex := FindGraphicClass(GraphicClass);
if ExtIndex = -1 then
begin
// extension not yet registered
New(ExtEntry);
ExtEntry.Extension := Extension;
ExtEntry.Description := Individual;
ExtEntry.FormatTypes := FormatTypes;
FExtensionList.Add(ExtEntry);
UpdateClassEntry;
end
else
if Replace then
begin
// replace current extension entry with new one
ExtEntry := FExtensionList[ExtIndex];
if ExtEntry.ClassReference.GraphicClass <> GraphicClass then
begin
// assign existing extension to new graphic class
OldReference := ExtEntry.ClassReference;
UpdateClassEntry;
Dec(OldReference.Count);
// remove the graphic class entry if no longer used
if OldReference.Count = 0 then FClassList.Remove(OldReference);
end;
// otherwise do nothing
end
else
GraphicExError(gesRegistration, [Extension]);
// finally make TPicture work
S := Individual;
if S = '' then S := ClassEntry.Description;
TPicture.RegisterFileFormat(Extension, S, GraphicClass);
end;
end;
//----------------------------------------------------------------------------------------------------------------------
procedure TFileFormatList.UnregisterFileFormat(const Extension: AnsiString; GraphicClass: TGraphicClass);
// Removes the entry for the given extension from the internal list.
// If Extension is '' then all associations for the given GraphicClass are removed otherwise the class is ignored and
// only the one particular extension is removed.
// Unregistration from TPicture is done here too, if necessary.
var
ExtIndex,
ClassIndex: Integer;
ExtEntry: PExtensionEntry;
ClassEntry: PClassEntry;
begin
ExtIndex := FindExtension(Extension);
// make sure we don't try to remove a non-registered extension
if (Extension = '') or (ExtIndex > -1) then
begin
if ExtIndex > -1 then
begin
// there's an entry for the extension
ExtEntry := FExtensionList[ExtIndex];
Dec(ExtEntry.ClassReference.Count);
// unregister graphic class too if necessary
if ExtEntry.ClassReference.Count = 0 then
begin
TPicture.UnregisterGraphicClass(ExtEntry.ClassReference.GraphicClass);
Dispose(ExtEntry.ClassReference);
FClassList.Remove(ExtEntry.ClassReference);
end;
// finally delete extension entry
Dispose(ExtEntry);
FExtensionList.Delete(ExtIndex);
end
else
begin
// all entries for the given graphic class must be removed
ClassIndex := FindGraphicClass(GraphicClass);
ClassEntry := FClassList[ClassIndex];
for ExtIndex := FExtensionList.Count - 1 downto 0 do
begin
if PExtensionEntry(FExtensionList[ExtIndex]).ClassReference.GraphicClass = GraphicClass then
begin
Dec(ClassEntry.Count);
Dispose(PExtensionEntry(FExtensionList[ExtIndex]));
FExtensionList.Delete(ExtIndex);
// no need to run through further entries if all references are done
if ClassEntry.Count = 0 then Break;
end;
end;
Dispose(ClassEntry);
FClassList.Delete(ClassIndex);
TPicture.UnregisterGraphicClass(GraphicClass);
end;
end;
end;
//----------------------------------------------------------------------------------------------------------------------
initialization
FileFormatList := TFileFormatList.Create;
with FileFormatList do
begin
// internally register Delphi's "built in" formats, these will not be unregistered on exit and
// also not registered with TPicture (because they are already or will soon be)
RegisterFileFormat('bmp', gesBitmaps, '', [ftRaster], False, False, TBitmap);
RegisterFileFormat('ico', gesIcons, '', [ftRaster], False, False, TIcon);
RegisterFileFormat('wmf', gesMetaFiles, '', [ftVector], False, False, TMetafile);
RegisterFileFormat('emf', gesMetaFiles, gesEnhancedMetaFiles, [ftVector], False, False, TMetafile);
RegisterFileFormat('jfif', gesJPGImages, gesJFIFImages, [ftRaster], False, False, TJPEGImage);
RegisterFileFormat('jpg', '', gesJPGImages, [ftRaster], False, False, TJPEGImage);
RegisterFileFormat('jpe', '', gesJPEImages, [ftRaster], False, False, TJPEGImage);
RegisterFileFormat('jpeg', '', gesJPEGImages, [ftRaster], False, False, TJPEGImage);
// register our own formats
RegisterFileFormat('rle', gesBitmaps, gesRLEBitmaps, [ftRaster], False, True, TBitmap);
RegisterFileFormat('dib', '', gesDIBs, [ftRaster], False, True, TBitmap);
{$ifdef TargaGraphic}
RegisterFileFormat('win', gesTruevision, '', [ftRaster], False, True, TTargaGraphic);
RegisterFileFormat('vst', '', '', [ftRaster], False, True, TTargaGraphic);
RegisterFileFormat('vda', '', '', [ftRaster], False, True, TTargaGraphic);
RegisterFileFormat('tga', '', '', [ftRaster], False, True, TTargaGraphic);
RegisterFileFormat('icb', '', '', [ftRaster], False, True, TTargaGraphic);
{$endif}
{$ifdef TIFFGraphic}
RegisterFileFormat('tiff', gesTIFF, gesMacTIFF, [ftRaster, ftMultiImage], False,
True, TTIFFGraphic);
RegisterFileFormat('tif', '', gesPCTIF, [ftRaster, ftMultiImage], False, True, TTIFFGraphic);
RegisterFileFormat('fax', '', gesGFIFax, [ftRaster, ftMultiImage], False, True, TTIFFGraphic);
{$ifdef EPSGraphic}
RegisterFileFormat('eps', gesEPS, '', [ftRaster], False, True, TEPSGraphic);
{$endif}
{$endif}
{$ifdef PCXGraphic}
RegisterFileFormat('pcx', gesZSoft, '', [ftRaster], False, True, TPCXGraphic);
RegisterFileFormat('pcc', '', '', [ftRaster], False, True, TPCXGraphic);
RegisterFileFormat('scr', '', gesZSoftWord, [ftRaster], False, True, TPCXGraphic);
{$endif}
{$ifdef RLAGraphic}
RegisterFileFormat('rpf', gesAliasWaveFront, '', [ftRaster], False, True, TRLAGraphic);
RegisterFileFormat('rla', '', '', [ftRaster], False, True, TRLAGraphic);
{$endif}
{$ifdef SGIGraphic}
RegisterFileFormat('sgi', gesSGI, gesSGITrueColor, [ftRaster], False, True, TSGIGraphic);
RegisterFileFormat('rgba', '', gesSGITrueColorAlpha, [ftRaster], False, True, TSGIGraphic);
RegisterFileFormat('rgb', '', gesSGITrueColor, [ftRaster], False, True, TSGIGraphic);
RegisterFileFormat('bw', '', gesSGIMono, [ftRaster], False, True, TSGIGraphic);
{$endif}
{$ifdef PhotoshopGraphic}
RegisterFileFormat('psd', gesPhotoshop, '', [ftRaster, ftLayered], False, True, TPSDGraphic);
RegisterFileFormat('pdd', '', '', [ftRaster, ftLayered], False, True, TPSDGraphic);
{$endif}
{$ifdef PortableMapGraphic}
RegisterFileFormat('ppm', gesPortable, gesPortablePixel, [ftRaster], False, True, TPPMGraphic);
RegisterFileFormat('pgm', '', gesPortableGray, [ftRaster], False, True, TPPMGraphic);
RegisterFileFormat('pbm', '', gesPortableMono, [ftRaster], False, True, TPPMGraphic);
{$endif}
{$ifdef AutodeskGraphic}
RegisterFileFormat('cel', gesAutodesk, '', [ftRaster], False, True, TAutodeskGraphic);
RegisterFileFormat('pic', gesAutodesk, '', [ftRaster], False, True, TAutodeskGraphic);
{$endif}
{$ifdef PCDGraphic}
RegisterFileFormat('pcd', gesKodakPhotoCD, '', [ftRaster], False, True, TPCDGraphic);
{$endif}
{$ifdef GIFGraphic}
RegisterFileFormat('gif', gesCompuserve, '', [ftRaster, ftMultiImage, ftAnimation], False, True, TGIFGraphic);
{$endif}
{$ifdef CUTGraphic}
RegisterFileFormat('cut', gesHalo, '', [ftRaster], False, True, TCUTGraphic);
{$endif}
{$ifdef PaintshopProGraphic}
RegisterFileFormat('psp', gesPaintshopPro, '', [ftRaster, ftVector], False, True, TPSPGraphic);
{$endif}
{$ifdef PortableNetworkGraphic}
RegisterFileFormat('png', gesPortableNetworkGraphic, '', [ftRaster], False, True, TPNGGraphic);
{$endif}
end;
finalization
with FileFormatList do
begin
{$ifdef PaintshopProGraphic} UnregisterFileFormat('', TPSPGraphic); {$endif}
{$ifdef PhotoshopGraphic} UnregisterFileFormat('', TPSDGraphic); {$endif}
{$ifdef TargaGraphic} UnregisterFileFormat('', TTargaGraphic); {$endif}
{$ifdef TIFFGraphic} UnregisterFileFormat('', TTIFFGraphic); {$endif}
{$ifdef SGIGraphic} UnregisterFileFormat('', TSGIGraphic); {$endif}
{$ifdef PCXGraphic} UnregisterFileFormat('', TPCXGraphic); {$endif}
{$ifdef AutodeskGraphic} UnregisterFileFormat('', TAutodeskGraphic); {$endif}
{$ifdef PCDGraphic} UnregisterFileFormat('', TPCDGraphic); {$endif}
{$ifdef PortableMapGraphic} UnregisterFileFormat('', TPPMGraphic); {$endif}
{$ifdef CUTGraphic} UnregisterFileFormat('', TCUTGraphic); {$endif}
{$ifdef GIFGraphic} UnregisterFileFormat('', TGIFGraphic); {$endif}
{$ifdef RLAGraphic} UnregisterFileFormat('', TRLAGraphic); {$endif}
UnregisterFileFormat('rle', TBitmap);
UnregisterFileFormat('dib', TBitmap);
{$ifdef PortableNetworkGraphic} UnregisterFileFormat('', TPNGGraphic); {$endif}
Free;
end;
end.