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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
*/
#include <config_folders.h>
#include <sal/log.hxx>
#include <osl/mutex.hxx>
#include <comphelper/processfactory.hxx>
#include <comphelper/threadpool.hxx>
#include <cppuhelper/implbase.hxx>
#include <tools/fract.hxx>
#include <unotools/configmgr.hxx>
#include <tools/stream.hxx>
#include <tools/urlobj.hxx>
#include <tools/zcodec.hxx>
#include <vcl/dibtools.hxx>
#include <fltcall.hxx>
#include <vcl/salctype.hxx>
#include <vcl/pngread.hxx>
#include <vcl/pngwrite.hxx>
#include <vcl/vectorgraphicdata.hxx>
#include <vcl/virdev.hxx>
#include <impgraph.hxx>
#include <vcl/svapp.hxx>
#include <osl/file.hxx>
#include <vcl/graphicfilter.hxx>
#include <vcl/FilterConfigItem.hxx>
#include <vcl/wmf.hxx>
#include "igif/gifread.hxx"
#include <vcl/pdfread.hxx>
#include "jpeg/jpeg.hxx"
#include "ixbm/xbmread.hxx"
#include "ixpm/xpmread.hxx"
#include <osl/module.hxx>
#include <com/sun/star/uno/Reference.h>
#include <com/sun/star/awt/Size.hpp>
#include <com/sun/star/uno/XInterface.hpp>
#include <com/sun/star/io/XActiveDataSource.hpp>
#include <com/sun/star/io/XOutputStream.hpp>
#include <com/sun/star/svg/XSVGWriter.hpp>
#include <com/sun/star/xml/sax/XDocumentHandler.hpp>
#include <com/sun/star/xml/sax/Writer.hpp>
#include <unotools/ucbstreamhelper.hxx>
#include <rtl/bootstrap.hxx>
#include <rtl/instance.hxx>
#include <tools/svlibrary.h>
#include <comphelper/string.hxx>
#include <unotools/ucbhelper.hxx>
#include <vector>
#include <memory>
#include "FilterConfigCache.hxx"
#include "graphicfilter_internal.hxx"
#include <graphic/GraphicFormatDetector.hxx>
#include <graphic/GraphicReader.hxx>
#define PMGCHUNG_msOG 0x6d734f47 // Microsoft Office Animated GIF
typedef ::std::vector< GraphicFilter* > FilterList_impl;
static FilterList_impl* pFilterHdlList = nullptr;
static ::osl::Mutex& getListMutex()
{
static ::osl::Mutex s_aListProtection;
return s_aListProtection;
}
namespace {
class ImpFilterOutputStream : public ::cppu::WeakImplHelper< css::io::XOutputStream >
{
SvStream& mrStm;
virtual void SAL_CALL writeBytes( const css::uno::Sequence< sal_Int8 >& rData ) override
{ mrStm.WriteBytes(rData.getConstArray(), rData.getLength()); }
virtual void SAL_CALL flush() override
{ mrStm.Flush(); }
virtual void SAL_CALL closeOutput() override {}
public:
explicit ImpFilterOutputStream( SvStream& rStm ) : mrStm( rStm ) {}
};
}
// Helper functions
sal_uInt8* ImplSearchEntry( sal_uInt8* pSource, sal_uInt8 const * pDest, sal_uLong nComp, sal_uLong nSize )
{
while ( nComp-- >= nSize )
{
sal_uLong i;
for ( i = 0; i < nSize; i++ )
{
if ( ( pSource[i]&~0x20 ) != ( pDest[i]&~0x20 ) )
break;
}
if ( i == nSize )
return pSource;
pSource++;
}
return nullptr;
}
static OUString ImpGetExtension( const OUString &rPath )
{
OUString aExt;
INetURLObject aURL( rPath );
aExt = aURL.GetFileExtension().toAsciiUpperCase();
return aExt;
}
bool isPCT(SvStream& rStream, sal_uLong nStreamPos, sal_uLong nStreamLen)
{
sal_uInt8 sBuf[3];
// store number format
SvStreamEndian oldNumberFormat = rStream.GetEndian();
sal_uInt32 nOffset; // in MS documents the pict format is used without the first 512 bytes
for ( nOffset = 0; ( nOffset <= 512 ) && ( ( nStreamPos + nOffset + 14 ) <= nStreamLen ); nOffset += 512 )
{
short y1,x1,y2,x2;
bool bdBoxOk = true;
rStream.Seek( nStreamPos + nOffset);
// size of the pict in version 1 pict ( 2bytes) : ignored
rStream.SeekRel(2);
// bounding box (bytes 2 -> 9)
rStream.SetEndian(SvStreamEndian::BIG);
rStream.ReadInt16( y1 ).ReadInt16( x1 ).ReadInt16( y2 ).ReadInt16( x2 );
rStream.SetEndian(oldNumberFormat); // reset format
if (x1 > x2 || y1 > y2 || // bad bdbox
(x1 == x2 && y1 == y2) || // 1 pixel picture
x2-x1 > 2048 || y2-y1 > 2048 ) // picture abnormally big
bdBoxOk = false;
// read version op
rStream.ReadBytes(sBuf, 3);
// see http://developer.apple.com/legacy/mac/library/documentation/mac/pdf/Imaging_With_QuickDraw/Appendix_A.pdf
// normal version 2 - page A23 and A24
if ( sBuf[ 0 ] == 0x00 && sBuf[ 1 ] == 0x11 && sBuf[ 2 ] == 0x02)
return true;
// normal version 1 - page A25
else if (sBuf[ 0 ] == 0x11 && sBuf[ 1 ] == 0x01 && bdBoxOk)
return true;
}
return false;
}
/*************************************************************************
*
* ImpPeekGraphicFormat()
*
* Description:
* This function is two-fold:
* 1.) Start reading file, determine the file format:
* Input parameters:
* rPath - file path
* rFormatExtension - content matter
* bTest - set false
* Output parameters:
* Return value - true if success
* rFormatExtension - on success: normal file extension in capitals
* 2.) Start reading file, verify file format
* Input parameters:
* rPath - file path
* rFormatExtension - normal file extension in capitals
* bTest - set true
* Output parameters:
* Return value - false, if cannot verify the file type
* passed to the function
* true, when the format is PROBABLY verified or
* WHEN THE FORMAT IS NOT KNOWN!
*
*************************************************************************/
bool ImpPeekGraphicFormat( SvStream& rStream, OUString& rFormatExtension, bool bTest )
{
vcl::GraphicFormatDetector aDetector(rStream, rFormatExtension);
if (!aDetector.detect())
return false;
// The following variable is used when bTest == true. It remains false
// if the format (rFormatExtension) has not yet been set.
bool bSomethingTested = false;
// Now the different formats are checked. The order *does* matter. e.g. a MET file
// could also go through the BMP test, however, a BMP file can hardly go through the MET test.
// So MET should be tested prior to BMP. However, theoretically a BMP file could conceivably
// go through the MET test. These problems are of course not only in MET and BMP.
// Therefore, in the case of a format check (bTest == true) we only test *exactly* this
// format. Everything else could have fatal consequences, for example if the user says it is
// a BMP file (and it is a BMP) file, and the file would go through the MET test ...
if (!bTest || rFormatExtension.startsWith("MET"))
{
bSomethingTested = true;
if (aDetector.checkMET())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("BMP"))
{
bSomethingTested = true;
if (aDetector.checkBMP())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest ||
rFormatExtension.startsWith("WMF") ||
rFormatExtension.startsWith("EMF"))
{
bSomethingTested = true;
if (aDetector.checkWMForEMF())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PCX"))
{
bSomethingTested = true;
if (aDetector.checkPCX())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("TIF"))
{
bSomethingTested = true;
if (aDetector.checkTIF())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("GIF"))
{
bSomethingTested = true;
if (aDetector.checkGIF())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PNG"))
{
bSomethingTested = true;
if (aDetector.checkPNG())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("JPG"))
{
bSomethingTested = true;
if (aDetector.checkJPG())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("SVM"))
{
bSomethingTested = true;
if (aDetector.checkSVM())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PCD"))
{
bSomethingTested = true;
if (aDetector.checkPCD())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PSD"))
{
bSomethingTested = true;
if (aDetector.checkPSD())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("EPS"))
{
bSomethingTested = true;
if (aDetector.checkEPS())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("DXF"))
{
if (aDetector.checkDXF())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PCT"))
{
bSomethingTested = true;
if (aDetector.checkPCT())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest ||
rFormatExtension.startsWith("PBM") ||
rFormatExtension.startsWith("PGM") ||
rFormatExtension.startsWith("PPM"))
{
bSomethingTested = true;
if (aDetector.checkPBMorPGMorPPM())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("RAS"))
{
bSomethingTested = true;
if (aDetector.checkRAS())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest)
{
bSomethingTested = true;
if (aDetector.checkXPM())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
else if (rFormatExtension.startsWith("XPM"))
{
return true;
}
if (!bTest)
{
if (aDetector.checkXBM())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
else if (rFormatExtension.startsWith("XBM"))
{
return true;
}
if (!bTest)
{
if (aDetector.checkSVG())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
else if (rFormatExtension.startsWith("SVG"))
{
return true;
}
if (!bTest || rFormatExtension.startsWith("TGA"))
{
bSomethingTested = true;
if (aDetector.checkTGA())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("MOV"))
{
if (aDetector.checkMOV())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
if (!bTest || rFormatExtension.startsWith("PDF"))
{
if (aDetector.checkPDF())
{
rFormatExtension = aDetector.msDetectedFormat;
return true;
}
}
return bTest && !bSomethingTested;
}
ErrCode GraphicFilter::ImpTestOrFindFormat( const OUString& rPath, SvStream& rStream, sal_uInt16& rFormat )
{
// determine or check the filter/format by reading into it
if( rFormat == GRFILTER_FORMAT_DONTKNOW )
{
OUString aFormatExt;
if( ImpPeekGraphicFormat( rStream, aFormatExt, false ) )
{
rFormat = pConfig->GetImportFormatNumberForExtension( aFormatExt );
if( rFormat != GRFILTER_FORMAT_DONTKNOW )
return ERRCODE_NONE;
}
// determine filter by file extension
if( !rPath.isEmpty() )
{
OUString aExt( ImpGetExtension( rPath ) );
rFormat = pConfig->GetImportFormatNumberForExtension( aExt );
if( rFormat != GRFILTER_FORMAT_DONTKNOW )
return ERRCODE_NONE;
}
return ERRCODE_GRFILTER_FORMATERROR;
}
else
{
OUString aTmpStr( pConfig->GetImportFormatExtension( rFormat ) );
aTmpStr = aTmpStr.toAsciiUpperCase();
if( !ImpPeekGraphicFormat( rStream, aTmpStr, true ) )
return ERRCODE_GRFILTER_FORMATERROR;
if ( pConfig->GetImportFormatExtension( rFormat ).equalsIgnoreAsciiCase( "pcd" ) )
{
sal_Int32 nBase = 2; // default Base0
if ( pConfig->GetImportFilterType( rFormat ).equalsIgnoreAsciiCase( "pcd_Photo_CD_Base4" ) )
nBase = 1;
else if ( pConfig->GetImportFilterType( rFormat ).equalsIgnoreAsciiCase( "pcd_Photo_CD_Base16" ) )
nBase = 0;
FilterConfigItem aFilterConfigItem( "Office.Common/Filter/Graphic/Import/PCD" );
aFilterConfigItem.WriteInt32( "Resolution", nBase );
}
}
return ERRCODE_NONE;
}
static Graphic ImpGetScaledGraphic( const Graphic& rGraphic, FilterConfigItem& rConfigItem )
{
Graphic aGraphic;
sal_Int32 nLogicalWidth = rConfigItem.ReadInt32( "LogicalWidth", 0 );
sal_Int32 nLogicalHeight = rConfigItem.ReadInt32( "LogicalHeight", 0 );
if ( rGraphic.GetType() != GraphicType::NONE )
{
sal_Int32 nMode = rConfigItem.ReadInt32( "ExportMode", -1 );
if ( nMode == -1 ) // the property is not there, this is possible, if the graphic filter
{ // is called via UnoGraphicExporter and not from a graphic export Dialog
nMode = 0; // then we are defaulting this mode to 0
if ( nLogicalWidth || nLogicalHeight )
nMode = 2;
}
Size aOriginalSize;
Size aPrefSize( rGraphic.GetPrefSize() );
MapMode aPrefMapMode( rGraphic.GetPrefMapMode() );
if (aPrefMapMode.GetMapUnit() == MapUnit::MapPixel)
aOriginalSize = Application::GetDefaultDevice()->PixelToLogic(aPrefSize, MapMode(MapUnit::Map100thMM));
else
aOriginalSize = OutputDevice::LogicToLogic(aPrefSize, aPrefMapMode, MapMode(MapUnit::Map100thMM));
if ( !nLogicalWidth )
nLogicalWidth = aOriginalSize.Width();
if ( !nLogicalHeight )
nLogicalHeight = aOriginalSize.Height();
if( rGraphic.GetType() == GraphicType::Bitmap )
{
// Resolution is set
if( nMode == 1 )
{
BitmapEx aBitmap( rGraphic.GetBitmapEx() );
MapMode aMap( MapUnit::Map100thInch );
sal_Int32 nDPI = rConfigItem.ReadInt32( "Resolution", 75 );
Fraction aFrac( 1, std::min( std::max( nDPI, sal_Int32( 75 ) ), sal_Int32( 600 ) ) );
aMap.SetScaleX( aFrac );
aMap.SetScaleY( aFrac );
Size aOldSize = aBitmap.GetSizePixel();
aGraphic = rGraphic;
aGraphic.SetPrefMapMode( aMap );
aGraphic.SetPrefSize( Size( aOldSize.Width() * 100,
aOldSize.Height() * 100 ) );
}
// Size is set
else if( nMode == 2 )
{
aGraphic = rGraphic;
aGraphic.SetPrefMapMode( MapMode( MapUnit::Map100thMM ) );
aGraphic.SetPrefSize( Size( nLogicalWidth, nLogicalHeight ) );
}
else
aGraphic = rGraphic;
sal_Int32 nColors = rConfigItem.ReadInt32( "Color", 0 );
if ( nColors ) // graphic conversion necessary ?
{
BitmapEx aBmpEx( aGraphic.GetBitmapEx() );
aBmpEx.Convert( static_cast<BmpConversion>(nColors) ); // the entries in the xml section have the same meaning as
aGraphic = aBmpEx; // they have in the BmpConversion enum, so it should be
} // allowed to cast them
}
else
{
if( ( nMode == 1 ) || ( nMode == 2 ) )
{
GDIMetaFile aMtf( rGraphic.GetGDIMetaFile() );
Size aNewSize( OutputDevice::LogicToLogic(Size(nLogicalWidth, nLogicalHeight), MapMode(MapUnit::Map100thMM), aMtf.GetPrefMapMode()) );
if( aNewSize.Width() && aNewSize.Height() )
{
const Size aPreferredSize( aMtf.GetPrefSize() );
aMtf.Scale( Fraction( aNewSize.Width(), aPreferredSize.Width() ),
Fraction( aNewSize.Height(), aPreferredSize.Height() ) );
}
aGraphic = Graphic( aMtf );
}
else
aGraphic = rGraphic;
}
}
else
aGraphic = rGraphic;
return aGraphic;
}
static OUString ImpCreateFullFilterPath( const OUString& rPath, const OUString& rFilterName )
{
OUString aPathURL;
::osl::FileBase::getFileURLFromSystemPath( rPath, aPathURL );
aPathURL += "/";
OUString aSystemPath;
::osl::FileBase::getSystemPathFromFileURL( aPathURL, aSystemPath );
aSystemPath += rFilterName;
return aSystemPath;
}
namespace {
class ImpFilterLibCache;
struct ImpFilterLibCacheEntry
{
ImpFilterLibCacheEntry* mpNext;
#ifndef DISABLE_DYNLOADING
osl::Module maLibrary;
#endif
OUString maFiltername;
OUString maFormatName;
PFilterCall mpfnImport;
ImpFilterLibCacheEntry(const OUString& rPathname, const OUString& rFiltername, const OUString& rFormatName);
bool operator==( const OUString& rFiltername ) const { return maFiltername == rFiltername; }
PFilterCall GetImportFunction();
};
}
ImpFilterLibCacheEntry::ImpFilterLibCacheEntry( const OUString& rPathname, const OUString& rFiltername, const OUString& rFormatName ) :
mpNext ( nullptr ),
#ifndef DISABLE_DYNLOADING
maLibrary ( rPathname ),
#endif
maFiltername ( rFiltername ),
maFormatName ( rFormatName ),
mpfnImport ( nullptr )
{
#ifdef DISABLE_DYNLOADING
(void) rPathname;
#endif
}
#ifdef DISABLE_DYNLOADING
extern "C" bool icdGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool idxGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool imeGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool ipbGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool ipdGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool ipsGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool iptGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool ipxGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool iraGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool itgGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool itiGraphicImport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
#endif
PFilterCall ImpFilterLibCacheEntry::GetImportFunction()
{
if( !mpfnImport )
{
#ifndef DISABLE_DYNLOADING
if (maFormatName == "icd")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("icdGraphicImport"));
else if (maFormatName == "idx")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("idxGraphicImport"));
else if (maFormatName == "ime")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("imeGraphicImport"));
else if (maFormatName == "ipb")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("ipbGraphicImport"));
else if (maFormatName == "ipd")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("ipdGraphicImport"));
else if (maFormatName == "ips")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("ipsGraphicImport"));
else if (maFormatName == "ipt")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("iptGraphicImport"));
else if (maFormatName == "ipx")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("ipxGraphicImport"));
else if (maFormatName == "ira")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("iraGraphicImport"));
else if (maFormatName == "itg")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("itgGraphicImport"));
else if (maFormatName == "iti")
mpfnImport = reinterpret_cast<PFilterCall>(maLibrary.getFunctionSymbol("itiGraphicImport"));
#else
if (maFormatName == "icd")
mpfnImport = icdGraphicImport;
else if (maFormatName == "idx")
mpfnImport = idxGraphicImport;
else if (maFormatName == "ime")
mpfnImport = imeGraphicImport;
else if (maFormatName == "ipb")
mpfnImport = ipbGraphicImport;
else if (maFormatName == "ipd")
mpfnImport = ipdGraphicImport;
else if (maFormatName == "ips")
mpfnImport = ipsGraphicImport;
else if (maFormatName == "ipt")
mpfnImport = iptGraphicImport;
else if (maFormatName == "ipx")
mpfnImport = ipxGraphicImport;
else if (maFormatName == "ira")
mpfnImport = iraGraphicImport;
else if (maFormatName == "itg")
mpfnImport = itgGraphicImport;
else if (maFormatName == "iti")
mpfnImport = itiGraphicImport;
#endif
}
return mpfnImport;
}
namespace {
class ImpFilterLibCache
{
ImpFilterLibCacheEntry* mpFirst;
ImpFilterLibCacheEntry* mpLast;
public:
ImpFilterLibCache();
~ImpFilterLibCache();
ImpFilterLibCacheEntry* GetFilter( const OUString& rFilterPath, const OUString& rFiltername, const OUString& rFormatName );
};
}
ImpFilterLibCache::ImpFilterLibCache() :
mpFirst ( nullptr ),
mpLast ( nullptr )
{
}
ImpFilterLibCache::~ImpFilterLibCache()
{
ImpFilterLibCacheEntry* pEntry = mpFirst;
while( pEntry )
{
ImpFilterLibCacheEntry* pNext = pEntry->mpNext;
delete pEntry;
pEntry = pNext;
}
}
ImpFilterLibCacheEntry* ImpFilterLibCache::GetFilter(const OUString& rFilterPath, const OUString& rFilterName, const OUString& rFormatName)
{
ImpFilterLibCacheEntry* pEntry = mpFirst;
while( pEntry )
{
if( *pEntry == rFilterName && pEntry->maFormatName == rFormatName )
break;
else
pEntry = pEntry->mpNext;
}
if( !pEntry )
{
OUString aPhysicalName( ImpCreateFullFilterPath( rFilterPath, rFilterName ) );
pEntry = new ImpFilterLibCacheEntry(aPhysicalName, rFilterName, rFormatName );
#ifndef DISABLE_DYNLOADING
if ( pEntry->maLibrary.is() )
#endif
{
if( !mpFirst )
mpFirst = mpLast = pEntry;
else
mpLast = mpLast->mpNext = pEntry;
}
#ifndef DISABLE_DYNLOADING
else
{
delete pEntry;
pEntry = nullptr;
}
#endif
}
return pEntry;
};
namespace { struct Cache : public rtl::Static<ImpFilterLibCache, Cache> {}; }
GraphicFilter::GraphicFilter( bool bConfig )
: bUseConfig(bConfig)
{
ImplInit();
}
GraphicFilter::~GraphicFilter()
{
{
::osl::MutexGuard aGuard( getListMutex() );
auto it = std::find(pFilterHdlList->begin(), pFilterHdlList->end(), this);
if( it != pFilterHdlList->end() )
pFilterHdlList->erase( it );
if( pFilterHdlList->empty() )
{
delete pFilterHdlList;
pFilterHdlList = nullptr;
delete pConfig;
}
}
pErrorEx.reset();
}
void GraphicFilter::ImplInit()
{
{
::osl::MutexGuard aGuard( getListMutex() );
if ( !pFilterHdlList )
{
pFilterHdlList = new FilterList_impl;
pConfig = new FilterConfigCache( bUseConfig );
}
else
pConfig = pFilterHdlList->front()->pConfig;
pFilterHdlList->push_back( this );
}
if( bUseConfig )
{
OUString url("$BRAND_BASE_DIR/" LIBO_LIB_FOLDER);
rtl::Bootstrap::expandMacros(url); //TODO: detect failure
osl::FileBase::getSystemPathFromFileURL(url, aFilterPath);
}
pErrorEx.reset( new FilterErrorEx );
}
ErrCode GraphicFilter::ImplSetError( ErrCode nError, const SvStream* pStm )
{
pErrorEx->nStreamError = pStm ? pStm->GetError() : ERRCODE_NONE;
return nError;
}
sal_uInt16 GraphicFilter::GetImportFormatCount() const
{
return pConfig->GetImportFormatCount();
}
sal_uInt16 GraphicFilter::GetImportFormatNumber( const OUString& rFormatName )
{
return pConfig->GetImportFormatNumber( rFormatName );
}
sal_uInt16 GraphicFilter::GetImportFormatNumberForShortName( const OUString& rShortName )
{
return pConfig->GetImportFormatNumberForShortName( rShortName );
}
sal_uInt16 GraphicFilter::GetImportFormatNumberForTypeName( const OUString& rType )
{
return pConfig->GetImportFormatNumberForTypeName( rType );
}
OUString GraphicFilter::GetImportFormatName( sal_uInt16 nFormat )
{
return pConfig->GetImportFormatName( nFormat );
}
OUString GraphicFilter::GetImportFormatTypeName( sal_uInt16 nFormat )
{
return pConfig->GetImportFilterTypeName( nFormat );
}
#ifdef _WIN32
OUString GraphicFilter::GetImportFormatMediaType( sal_uInt16 nFormat )
{
return pConfig->GetImportFormatMediaType( nFormat );
}
#endif
OUString GraphicFilter::GetImportFormatShortName( sal_uInt16 nFormat )
{
return pConfig->GetImportFormatShortName( nFormat );
}
OUString GraphicFilter::GetImportWildcard( sal_uInt16 nFormat, sal_Int32 nEntry )
{
return pConfig->GetImportWildcard( nFormat, nEntry );
}
sal_uInt16 GraphicFilter::GetExportFormatCount() const
{
return pConfig->GetExportFormatCount();
}
sal_uInt16 GraphicFilter::GetExportFormatNumber( const OUString& rFormatName )
{
return pConfig->GetExportFormatNumber( rFormatName );
}
sal_uInt16 GraphicFilter::GetExportFormatNumberForMediaType( const OUString& rMediaType )
{
return pConfig->GetExportFormatNumberForMediaType( rMediaType );
}
sal_uInt16 GraphicFilter::GetExportFormatNumberForShortName( const OUString& rShortName )
{
return pConfig->GetExportFormatNumberForShortName( rShortName );
}
OUString GraphicFilter::GetExportInternalFilterName( sal_uInt16 nFormat )
{
return pConfig->GetExportInternalFilterName( nFormat );
}
sal_uInt16 GraphicFilter::GetExportFormatNumberForTypeName( const OUString& rType )
{
return pConfig->GetExportFormatNumberForTypeName( rType );
}
OUString GraphicFilter::GetExportFormatName( sal_uInt16 nFormat )
{
return pConfig->GetExportFormatName( nFormat );
}
OUString GraphicFilter::GetExportFormatMediaType( sal_uInt16 nFormat )
{
return pConfig->GetExportFormatMediaType( nFormat );
}
OUString GraphicFilter::GetExportFormatShortName( sal_uInt16 nFormat )
{
return pConfig->GetExportFormatShortName( nFormat );
}
OUString GraphicFilter::GetExportWildcard( sal_uInt16 nFormat )
{
return pConfig->GetExportWildcard( nFormat, 0 );
}
bool GraphicFilter::IsExportPixelFormat( sal_uInt16 nFormat )
{
return pConfig->IsExportPixelFormat( nFormat );
}
ErrCode GraphicFilter::CanImportGraphic( const INetURLObject& rPath,
sal_uInt16 nFormat, sal_uInt16* pDeterminedFormat )
{
ErrCode nRetValue = ERRCODE_GRFILTER_FORMATERROR;
SAL_WARN_IF( rPath.GetProtocol() == INetProtocol::NotValid, "vcl.filter", "GraphicFilter::CanImportGraphic() : ProtType == INetProtocol::NotValid" );
OUString aMainUrl( rPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
std::unique_ptr<SvStream> xStream(::utl::UcbStreamHelper::CreateStream( aMainUrl, StreamMode::READ | StreamMode::SHARE_DENYNONE ));
if (xStream)
{
nRetValue = CanImportGraphic( aMainUrl, *xStream, nFormat, pDeterminedFormat );
}
return nRetValue;
}
ErrCode GraphicFilter::CanImportGraphic( const OUString& rMainUrl, SvStream& rIStream,
sal_uInt16 nFormat, sal_uInt16* pDeterminedFormat )
{
sal_uLong nStreamPos = rIStream.Tell();
ErrCode nRes = ImpTestOrFindFormat( rMainUrl, rIStream, nFormat );
rIStream.Seek(nStreamPos);
if( nRes==ERRCODE_NONE && pDeterminedFormat!=nullptr )
*pDeterminedFormat = nFormat;
return ImplSetError( nRes, &rIStream );
}
//SJ: TODO, we need to create a GraphicImporter component
ErrCode GraphicFilter::ImportGraphic( Graphic& rGraphic, const INetURLObject& rPath,
sal_uInt16 nFormat, sal_uInt16 * pDeterminedFormat, GraphicFilterImportFlags nImportFlags )
{
ErrCode nRetValue = ERRCODE_GRFILTER_FORMATERROR;
SAL_WARN_IF( rPath.GetProtocol() == INetProtocol::NotValid, "vcl.filter", "GraphicFilter::ImportGraphic() : ProtType == INetProtocol::NotValid" );
OUString aMainUrl( rPath.GetMainURL( INetURLObject::DecodeMechanism::NONE ) );
std::unique_ptr<SvStream> xStream(::utl::UcbStreamHelper::CreateStream( aMainUrl, StreamMode::READ | StreamMode::SHARE_DENYNONE ));
if (xStream)
{
nRetValue = ImportGraphic( rGraphic, aMainUrl, *xStream, nFormat, pDeterminedFormat, nImportFlags );
}
return nRetValue;
}
ErrCode GraphicFilter::ImportGraphic(
Graphic& rGraphic,
const OUString& rPath,
SvStream& rIStream,
sal_uInt16 nFormat,
sal_uInt16* pDeterminedFormat,
GraphicFilterImportFlags nImportFlags,
WmfExternal const *pExtHeader)
{
return ImportGraphic( rGraphic, rPath, rIStream, nFormat, pDeterminedFormat, nImportFlags, nullptr, pExtHeader );
}
namespace {
/// Contains a stream and other associated data to import pixels into a
/// Graphic.
struct GraphicImportContext
{
/// Pixel data is read from this stream.
std::unique_ptr<SvStream> m_pStream;
/// The Graphic the import filter gets.
std::shared_ptr<Graphic> m_pGraphic;
/// Write pixel data using this access.
std::unique_ptr<BitmapScopedWriteAccess> m_pAccess;
/// Signals if import finished correctly.
ErrCode m_nStatus = ERRCODE_GRFILTER_FILTERERROR;
/// Original graphic format.
GfxLinkType m_eLinkType = GfxLinkType::NONE;
/// Position of the stream before reading the data.
sal_uInt64 m_nStreamBegin = 0;
/// Flags for the import filter.
GraphicFilterImportFlags m_nImportFlags = GraphicFilterImportFlags::NONE;
};
/// Graphic import worker that gets executed on a thread.
class GraphicImportTask : public comphelper::ThreadTask
{
GraphicImportContext& m_rContext;
public:
GraphicImportTask(const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, GraphicImportContext& rContext);
void doWork() override;
/// Shared code between threaded and non-threaded version.
static void doImport(GraphicImportContext& rContext);
};
}
GraphicImportTask::GraphicImportTask(const std::shared_ptr<comphelper::ThreadTaskTag>& pTag, GraphicImportContext& rContext)
: comphelper::ThreadTask(pTag),
m_rContext(rContext)
{
}
void GraphicImportTask::doWork()
{
GraphicImportTask::doImport(m_rContext);
}
void GraphicImportTask::doImport(GraphicImportContext& rContext)
{
if (!ImportJPEG(*rContext.m_pStream, *rContext.m_pGraphic, rContext.m_nImportFlags | GraphicFilterImportFlags::UseExistingBitmap, rContext.m_pAccess.get()))
rContext.m_nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
rContext.m_eLinkType = GfxLinkType::NativeJpg;
}
void GraphicFilter::ImportGraphics(std::vector< std::shared_ptr<Graphic> >& rGraphics, std::vector< std::unique_ptr<SvStream> > vStreams)
{
static bool bThreads = !getenv("VCL_NO_THREAD_IMPORT");
std::vector<GraphicImportContext> aContexts;
aContexts.reserve(vStreams.size());
comphelper::ThreadPool& rSharedPool = comphelper::ThreadPool::getSharedOptimalPool();
std::shared_ptr<comphelper::ThreadTaskTag> pTag = comphelper::ThreadPool::createThreadTaskTag();
for (auto& pStream : vStreams)
{
aContexts.emplace_back();
GraphicImportContext& rContext = aContexts.back();
if (pStream)
{
rContext.m_pStream = std::move(pStream);
rContext.m_pGraphic = std::make_shared<Graphic>();
rContext.m_nStatus = ERRCODE_NONE;
// Detect the format.
ResetLastError();
rContext.m_nStreamBegin = rContext.m_pStream->Tell();
sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW;
rContext.m_nStatus = ImpTestOrFindFormat(OUString(), *rContext.m_pStream, nFormat);
rContext.m_pStream->Seek(rContext.m_nStreamBegin);
// Import the graphic.
if (rContext.m_nStatus == ERRCODE_NONE && !rContext.m_pStream->GetError())
{
OUString aFilterName = pConfig->GetImportFilterName(nFormat);
if (aFilterName.equalsIgnoreAsciiCase(IMP_JPEG))
{
rContext.m_nImportFlags = GraphicFilterImportFlags::SetLogsizeForJpeg;
if (!ImportJPEG( *rContext.m_pStream, *rContext.m_pGraphic, rContext.m_nImportFlags | GraphicFilterImportFlags::OnlyCreateBitmap, nullptr))
rContext.m_nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
Bitmap& rBitmap = const_cast<Bitmap&>(rContext.m_pGraphic->GetBitmapExRef().GetBitmap());
rContext.m_pAccess = std::make_unique<BitmapScopedWriteAccess>(rBitmap);
rContext.m_pStream->Seek(rContext.m_nStreamBegin);
if (bThreads)
rSharedPool.pushTask(std::make_unique<GraphicImportTask>(pTag, rContext));
else
GraphicImportTask::doImport(rContext);
}
}
else
rContext.m_nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
}
rSharedPool.waitUntilDone(pTag);
// Process data after import.
for (auto& rContext : aContexts)
{
rContext.m_pAccess.reset();
if (rContext.m_nStatus == ERRCODE_NONE && (rContext.m_eLinkType != GfxLinkType::NONE) && !rContext.m_pGraphic->GetReaderContext())
{
std::unique_ptr<sal_uInt8[]> pGraphicContent;
const sal_uInt64 nStreamEnd = rContext.m_pStream->Tell();
sal_Int32 nGraphicContentSize = nStreamEnd - rContext.m_nStreamBegin;
if (nGraphicContentSize > 0)
{
try
{
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
}
catch (const std::bad_alloc&)
{
rContext.m_nStatus = ERRCODE_GRFILTER_TOOBIG;
}
if (rContext.m_nStatus == ERRCODE_NONE)
{
rContext.m_pStream->Seek(rContext.m_nStreamBegin);
rContext.m_pStream->ReadBytes(pGraphicContent.get(), nGraphicContentSize);
}
}
if (rContext.m_nStatus == ERRCODE_NONE)
rContext.m_pGraphic->SetGfxLink(std::make_shared<GfxLink>(std::move(pGraphicContent), nGraphicContentSize, rContext.m_eLinkType));
}
if (rContext.m_nStatus != ERRCODE_NONE)
rContext.m_pGraphic = nullptr;
rGraphics.push_back(rContext.m_pGraphic);
}
}
Graphic GraphicFilter::ImportUnloadedGraphic(SvStream& rIStream, sal_uInt64 sizeLimit,
const Size* pSizeHint)
{
Graphic aGraphic;
sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW;
GfxLinkType eLinkType = GfxLinkType::NONE;
ResetLastError();
const sal_uLong nStreamBegin = rIStream.Tell();
rIStream.Seek(nStreamBegin);
ErrCode nStatus = ImpTestOrFindFormat("", rIStream, nFormat);
rIStream.Seek(nStreamBegin);
sal_uInt32 nStreamLength(rIStream.remainingSize());
if (sizeLimit && sizeLimit < nStreamLength)
nStreamLength = sizeLimit;
OUString aFilterName = pConfig->GetImportFilterName(nFormat);
OUString aExternalFilterName = pConfig->GetExternalFilterName(nFormat, false);
std::unique_ptr<sal_uInt8[]> pGraphicContent;
sal_Int32 nGraphicContentSize = 0;
// read graphic
if (pConfig->IsImportInternalFilter(nFormat))
{
if (aFilterName.equalsIgnoreAsciiCase(IMP_GIF))
{
eLinkType = GfxLinkType::NativeGif;
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_PNG))
{
vcl::PNGReader aPNGReader(rIStream);
// check if this PNG contains a GIF chunk!
const std::vector<vcl::PNGReader::ChunkData>& rChunkData = aPNGReader.GetChunks();
for (auto const& chunk : rChunkData)
{
// Microsoft Office is storing Animated GIFs in following chunk
if (chunk.nType == PMGCHUNG_msOG)
{
sal_uInt32 nChunkSize = chunk.aData.size();
if (nChunkSize > 11)
{
const std::vector<sal_uInt8>& rData = chunk.aData;
nGraphicContentSize = nChunkSize - 11;
SvMemoryStream aIStrm(const_cast<sal_uInt8*>(&rData[11]), nGraphicContentSize, StreamMode::READ);
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
sal_uInt64 aCurrentPosition = aIStrm.Tell();
aIStrm.ReadBytes(pGraphicContent.get(), nGraphicContentSize);
aIStrm.Seek(aCurrentPosition);
eLinkType = GfxLinkType::NativeGif;
break;
}
}
}
if (eLinkType == GfxLinkType::NONE)
{
eLinkType = GfxLinkType::NativePng;
}
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_JPEG))
{
eLinkType = GfxLinkType::NativeJpg;
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_SVG))
{
bool bOkay(false);
if (nStreamLength > 0)
{
std::vector<sal_uInt8> aTwoBytes(2);
rIStream.ReadBytes(aTwoBytes.data(), 2);
rIStream.Seek(nStreamBegin);
if (aTwoBytes[0] == 0x1F && aTwoBytes[1] == 0x8B)
{
SvMemoryStream aMemStream;
ZCodec aCodec;
long nMemoryLength;
aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, /*gzLib*/true);
nMemoryLength = aCodec.Decompress(rIStream, aMemStream);
aCodec.EndCompression();
if (!rIStream.GetError() && nMemoryLength >= 0)
{
nGraphicContentSize = nMemoryLength;
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
aMemStream.Seek(STREAM_SEEK_TO_BEGIN);
aMemStream.ReadBytes(pGraphicContent.get(), nGraphicContentSize);
bOkay = true;
}
}
else
{
nGraphicContentSize = nStreamLength;
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
rIStream.ReadBytes(pGraphicContent.get(), nStreamLength);
bOkay = true;
}
}
if (bOkay)
{
eLinkType = GfxLinkType::NativeSvg;
}
else
{
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_BMP))
{
eLinkType = GfxLinkType::NativeBmp;
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_MOV))
{
eLinkType = GfxLinkType::NativeMov;
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_WMF) ||
aFilterName.equalsIgnoreAsciiCase(IMP_EMF))
{
nGraphicContentSize = nStreamLength;
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
rIStream.Seek(nStreamBegin);
rIStream.ReadBytes(pGraphicContent.get(), nStreamLength);
if (!rIStream.GetError())
{
eLinkType = GfxLinkType::NativeWmf;
}
else
{
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
else if (aFilterName == IMP_PDF)
{
eLinkType = GfxLinkType::NativePdf;
}
else
{
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
else
{
ImpFilterLibCacheEntry* pFilter = nullptr;
if (!aFilterPath.isEmpty())
{
// find first filter in filter paths
ImpFilterLibCache &rCache = Cache::get();
sal_Int32 nIdx{0};
do {
pFilter = rCache.GetFilter(aFilterPath.getToken(0, ';', nIdx), aFilterName, aExternalFilterName);
} while (nIdx>=0 && pFilter==nullptr);
}
if( !pFilter )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
PFilterCall pFunc = pFilter->GetImportFunction();
if (!pFunc)
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
OUString aShortName;
if (nFormat != GRFILTER_FORMAT_DONTKNOW)
aShortName = GetImportFormatShortName(nFormat).toAsciiUpperCase();
if (aShortName.startsWith(TIF_SHORTNAME))
eLinkType = GfxLinkType::NativeTif;
else if( aShortName.startsWith(MET_SHORTNAME))
eLinkType = GfxLinkType::NativeMet;
else if( aShortName.startsWith(PCT_SHORTNAME))
eLinkType = GfxLinkType::NativePct;
}
}
}
if (nStatus == ERRCODE_NONE && eLinkType != GfxLinkType::NONE)
{
if (!pGraphicContent)
{
nGraphicContentSize = nStreamLength;
if (nGraphicContentSize > 0)
{
try
{
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
}
catch (const std::bad_alloc&)
{
nStatus = ERRCODE_GRFILTER_TOOBIG;
}
if (nStatus == ERRCODE_NONE)
{
rIStream.Seek(nStreamBegin);
rIStream.ReadBytes(pGraphicContent.get(), nGraphicContentSize);
}
}
}
if( nStatus == ERRCODE_NONE )
{
bool bAnimated = false;
if (eLinkType == GfxLinkType::NativeGif)
{
SvMemoryStream aMemoryStream(pGraphicContent.get(), nGraphicContentSize, StreamMode::READ);
bAnimated = IsGIFAnimated(aMemoryStream);
}
aGraphic.SetGfxLink(std::make_shared<GfxLink>(std::move(pGraphicContent), nGraphicContentSize, eLinkType));
aGraphic.ImplGetImpGraphic()->ImplSetPrepared(bAnimated, pSizeHint);
}
}
// Set error code or try to set native buffer
if (nStatus != ERRCODE_NONE)
ImplSetError(nStatus, &rIStream);
if (nStatus != ERRCODE_NONE || eLinkType == GfxLinkType::NONE)
rIStream.Seek(nStreamBegin);
return aGraphic;
}
void GraphicFilter::preload()
{
sal_Int32 nTokenCount = comphelper::string::getTokenCount(aFilterPath, ';');
ImpFilterLibCache& rCache = Cache::get();
static const std::initializer_list<OUStringLiteral> aFilterNames = {
"icd", "idx", "ime", "ipb", "ipd", "ips", "ipt", "ipx", "ira", "itg", "iti",
};
// Load library for each filter.
for (const auto& rFilterName : aFilterNames)
{
ImpFilterLibCacheEntry* pFilter = nullptr;
// Look at the library in each element inside the filter path.
for (sal_Int32 i = 0; i < nTokenCount; ++i)
{
pFilter = rCache.GetFilter(aFilterPath.getToken(i, ';'), SVLIBRARY("gie"), rFilterName);
if (pFilter)
{
break;
}
}
}
}
ErrCode GraphicFilter::ImportGraphic( Graphic& rGraphic, const OUString& rPath, SvStream& rIStream,
sal_uInt16 nFormat, sal_uInt16* pDeterminedFormat, GraphicFilterImportFlags nImportFlags,
const css::uno::Sequence< css::beans::PropertyValue >* /*pFilterData*/,
WmfExternal const *pExtHeader )
{
OUString aFilterName;
OUString aExternalFilterName;
sal_uLong nStreamBegin;
ErrCode nStatus;
GfxLinkType eLinkType = GfxLinkType::NONE;
const bool bLinkSet = rGraphic.IsGfxLink();
std::unique_ptr<sal_uInt8[]> pGraphicContent;
sal_Int32 nGraphicContentSize = 0;
ResetLastError();
std::shared_ptr<GraphicReader> pContext = rGraphic.GetReaderContext();
bool bDummyContext = rGraphic.IsDummyContext();
if( !pContext || bDummyContext )
{
if( bDummyContext )
{
rGraphic.SetDummyContext( false );
nStreamBegin = 0;
}
else
nStreamBegin = rIStream.Tell();
nStatus = ImpTestOrFindFormat( rPath, rIStream, nFormat );
// if pending, return ERRCODE_NONE in order to request more bytes
if( rIStream.GetError() == ERRCODE_IO_PENDING )
{
rGraphic.SetDummyContext(true);
rIStream.ResetError();
rIStream.Seek( nStreamBegin );
return ImplSetError( ERRCODE_NONE );
}
rIStream.Seek( nStreamBegin );
if( ( nStatus != ERRCODE_NONE ) || rIStream.GetError() )
return ImplSetError( ( nStatus != ERRCODE_NONE ) ? nStatus : ERRCODE_GRFILTER_OPENERROR, &rIStream );
if( pDeterminedFormat )
*pDeterminedFormat = nFormat;
aFilterName = pConfig->GetImportFilterName( nFormat );
aExternalFilterName = pConfig->GetExternalFilterName(nFormat, false);
}
else
{
aFilterName = pContext->GetUpperFilterName();
nStreamBegin = 0;
nStatus = ERRCODE_NONE;
}
// read graphic
if ( pConfig->IsImportInternalFilter( nFormat ) )
{
if( aFilterName.equalsIgnoreAsciiCase( IMP_GIF ) )
{
if( !ImportGIF( rIStream, rGraphic ) )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
eLinkType = GfxLinkType::NativeGif;
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_PNG ) )
{
vcl::PNGReader aPNGReader( rIStream );
{
// check if this PNG contains a GIF chunk!
const std::vector<vcl::PNGReader::ChunkData>& rChunkData = aPNGReader.GetChunks();
for (auto const& chunk : rChunkData)
{
// Microsoft Office is storing Animated GIFs in following chunk
if (chunk.nType == PMGCHUNG_msOG)
{
sal_uInt32 nChunkSize = chunk.aData.size();
if (nChunkSize > 11)
{
const std::vector<sal_uInt8>& rData = chunk.aData;
nGraphicContentSize = nChunkSize - 11;
SvMemoryStream aIStrm(const_cast<sal_uInt8*>(&rData[11]), nGraphicContentSize, StreamMode::READ);
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
sal_uInt64 aCurrentPosition = aIStrm.Tell();
aIStrm.ReadBytes(pGraphicContent.get(), nGraphicContentSize);
aIStrm.Seek(aCurrentPosition);
ImportGIF(aIStrm, rGraphic);
eLinkType = GfxLinkType::NativeGif;
break;
}
}
}
}
if ( eLinkType == GfxLinkType::NONE )
{
BitmapEx aBmpEx( aPNGReader.Read() );
if ( aBmpEx.IsEmpty() )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
rGraphic = aBmpEx;
eLinkType = GfxLinkType::NativePng;
}
}
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_JPEG ) )
{
// set LOGSIZE flag always, if not explicitly disabled
// (see #90508 and #106763)
if( !( nImportFlags & GraphicFilterImportFlags::DontSetLogsizeForJpeg ) )
nImportFlags |= GraphicFilterImportFlags::SetLogsizeForJpeg;
sal_uInt64 nPosition = rIStream.Tell();
if( !ImportJPEG( rIStream, rGraphic, nImportFlags | GraphicFilterImportFlags::OnlyCreateBitmap, nullptr ) )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
Bitmap& rBitmap = const_cast<Bitmap&>(rGraphic.GetBitmapExRef().GetBitmap());
BitmapScopedWriteAccess pWriteAccess(rBitmap);
rIStream.Seek(nPosition);
if( !ImportJPEG( rIStream, rGraphic, nImportFlags | GraphicFilterImportFlags::UseExistingBitmap, &pWriteAccess ) )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
eLinkType = GfxLinkType::NativeJpg;
}
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_SVG ) )
{
const sal_uInt32 nStreamPosition(rIStream.Tell());
const sal_uInt32 nStreamLength(rIStream.remainingSize());
bool bOkay(false);
if(nStreamLength > 0)
{
std::vector<sal_uInt8> aTwoBytes(2);
rIStream.ReadBytes(aTwoBytes.data(), 2);
rIStream.Seek(nStreamPosition);
if(aTwoBytes[0] == 0x1F && aTwoBytes[1] == 0x8B)
{
SvMemoryStream aMemStream;
ZCodec aCodec;
long nMemoryLength;
aCodec.BeginCompression(ZCODEC_DEFAULT_COMPRESSION, /*gzLib*/true);
nMemoryLength = aCodec.Decompress(rIStream, aMemStream);
aCodec.EndCompression();
if (!rIStream.GetError() && nMemoryLength >= 0)
{
VectorGraphicDataArray aNewData(nMemoryLength);
aMemStream.Seek(STREAM_SEEK_TO_BEGIN);
aMemStream.ReadBytes(aNewData.begin(), nMemoryLength);
// Make a uncompressed copy for GfxLink
nGraphicContentSize = nMemoryLength;
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
std::copy(aNewData.begin(), aNewData.end(), pGraphicContent.get());
if(!aMemStream.GetError() )
{
auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aNewData, rPath, VectorGraphicDataType::Svg);
rGraphic = Graphic(aVectorGraphicDataPtr);
bOkay = true;
}
}
}
else
{
VectorGraphicDataArray aNewData(nStreamLength);
rIStream.ReadBytes(aNewData.begin(), nStreamLength);
if(!rIStream.GetError())
{
auto aVectorGraphicDataPtr = std::make_shared<VectorGraphicData>(aNewData, rPath, VectorGraphicDataType::Svg);
rGraphic = Graphic(aVectorGraphicDataPtr);
bOkay = true;
}
}
}
if (bOkay)
{
eLinkType = GfxLinkType::NativeSvg;
}
else
{
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_XBM ) )
{
if( !ImportXBM( rIStream, rGraphic ) )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_XPM ) )
{
if( !ImportXPM( rIStream, rGraphic ) )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_BMP ) ||
aFilterName.equalsIgnoreAsciiCase( IMP_SVMETAFILE ) )
{
// SV internal filters for import bitmaps and MetaFiles
ReadGraphic( rIStream, rGraphic );
if( rIStream.GetError() )
{
nStatus = ERRCODE_GRFILTER_FORMATERROR;
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_BMP))
{
// #i15508# added BMP type (checked, works)
eLinkType = GfxLinkType::NativeBmp;
}
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_MOV ) )
{
ReadGraphic( rIStream, rGraphic );
if( rIStream.GetError() )
nStatus = ERRCODE_GRFILTER_FORMATERROR;
else
{
rGraphic.SetDefaultType();
rIStream.Seek( STREAM_SEEK_TO_END );
eLinkType = GfxLinkType::NativeMov;
}
}
else if( aFilterName.equalsIgnoreAsciiCase( IMP_WMF ) ||
aFilterName.equalsIgnoreAsciiCase( IMP_EMF ) )
{
// use new UNO API service, do not directly import but create a
// Graphic that contains the original data and decomposes to
// primitives on demand
const sal_uInt32 nStreamLength(rIStream.remainingSize());
VectorGraphicDataArray aNewData(nStreamLength);
bool bOkay(false);
rIStream.ReadBytes(aNewData.begin(), nStreamLength);
if (!rIStream.GetError())
{
const bool bIsWmf(aFilterName.equalsIgnoreAsciiCase(IMP_WMF));
const VectorGraphicDataType aDataType(bIsWmf ? VectorGraphicDataType::Wmf : VectorGraphicDataType::Emf);
auto aVectorGraphicDataPtr =
std::make_shared<VectorGraphicData>(
aNewData,
rPath,
aDataType);
if (pExtHeader)
{
aVectorGraphicDataPtr->setWmfExternalHeader(*pExtHeader);
}
rGraphic = Graphic(aVectorGraphicDataPtr);
bOkay = true;
}
if (bOkay)
{
eLinkType = GfxLinkType::NativeWmf;
}
else
{
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
else if (aFilterName.equalsIgnoreAsciiCase(IMP_PDF))
{
if (vcl::ImportPDF(rIStream, rGraphic))
eLinkType = GfxLinkType::NativePdf;
else
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
else
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
else
{
ImpFilterLibCacheEntry* pFilter = nullptr;
if (!aFilterPath.isEmpty())
{
// find first filter in filter paths
ImpFilterLibCache &rCache = Cache::get();
sal_Int32 nIdx{0};
do {
pFilter = rCache.GetFilter(aFilterPath.getToken(0, ';', nIdx), aFilterName, aExternalFilterName);
} while (nIdx>=0 && pFilter==nullptr);
}
if( !pFilter )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
PFilterCall pFunc = pFilter->GetImportFunction();
if( !pFunc )
nStatus = ERRCODE_GRFILTER_FILTERERROR;
else
{
std::unique_ptr<FilterConfigItem> pFilterConfigItem;
OUString aShortName;
if( nFormat != GRFILTER_FORMAT_DONTKNOW )
{
aShortName = GetImportFormatShortName( nFormat ).toAsciiUpperCase();
if (aShortName == "PCD" && !utl::ConfigManager::IsFuzzing())
{
OUString aFilterConfigPath( "Office.Common/Filter/Graphic/Import/PCD" );
pFilterConfigItem = std::make_unique<FilterConfigItem>( aFilterConfigPath );
}
}
if( !(*pFunc)( rIStream, rGraphic, pFilterConfigItem.get() ) )
nStatus = ERRCODE_GRFILTER_FORMATERROR;
else
{
// try to set link type if format matches
if( nFormat != GRFILTER_FORMAT_DONTKNOW )
{
if( aShortName.startsWith( TIF_SHORTNAME ) )
eLinkType = GfxLinkType::NativeTif;
else if( aShortName.startsWith( MET_SHORTNAME ) )
eLinkType = GfxLinkType::NativeMet;
else if( aShortName.startsWith( PCT_SHORTNAME ) )
eLinkType = GfxLinkType::NativePct;
}
}
}
}
}
if( nStatus == ERRCODE_NONE && ( eLinkType != GfxLinkType::NONE ) && !rGraphic.GetReaderContext() && !bLinkSet )
{
if (!pGraphicContent)
{
const sal_uLong nStreamEnd = rIStream.Tell();
nGraphicContentSize = nStreamEnd - nStreamBegin;
if (nGraphicContentSize > 0)
{
try
{
pGraphicContent.reset(new sal_uInt8[nGraphicContentSize]);
}
catch (const std::bad_alloc&)
{
nStatus = ERRCODE_GRFILTER_TOOBIG;
}
if( nStatus == ERRCODE_NONE )
{
rIStream.Seek(nStreamBegin);
rIStream.ReadBytes(pGraphicContent.get(), nGraphicContentSize);
}
}
}
if( nStatus == ERRCODE_NONE )
{
rGraphic.SetGfxLink(std::make_shared<GfxLink>(std::move(pGraphicContent), nGraphicContentSize, eLinkType));
}
}
// Set error code or try to set native buffer
if( nStatus != ERRCODE_NONE )
{
ImplSetError( nStatus, &rIStream );
rIStream.Seek( nStreamBegin );
rGraphic.Clear();
}
return nStatus;
}
ErrCode GraphicFilter::ExportGraphic( const Graphic& rGraphic, const INetURLObject& rPath,
sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
{
SAL_INFO( "vcl.filter", "GraphicFilter::ExportGraphic() (thb)" );
ErrCode nRetValue = ERRCODE_GRFILTER_FORMATERROR;
SAL_WARN_IF( rPath.GetProtocol() == INetProtocol::NotValid, "vcl.filter", "GraphicFilter::ExportGraphic() : ProtType == INetProtocol::NotValid" );
OUString aMainUrl(rPath.GetMainURL(INetURLObject::DecodeMechanism::NONE));
bool bAlreadyExists = utl::UCBContentHelper::IsDocument(aMainUrl);
std::unique_ptr<SvStream> xStream(::utl::UcbStreamHelper::CreateStream( aMainUrl, StreamMode::WRITE | StreamMode::TRUNC ));
if (xStream)
{
nRetValue = ExportGraphic( rGraphic, aMainUrl, *xStream, nFormat, pFilterData );
xStream.reset();
if( ( ERRCODE_NONE != nRetValue ) && !bAlreadyExists )
utl::UCBContentHelper::Kill(aMainUrl);
}
return nRetValue;
}
#ifdef DISABLE_DYNLOADING
extern "C" bool egiGraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool epsGraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
extern "C" bool etiGraphicExport( SvStream& rStream, Graphic& rGraphic, FilterConfigItem* pConfigItem );
#endif
ErrCode GraphicFilter::ExportGraphic( const Graphic& rGraphic, const OUString& rPath,
SvStream& rOStm, sal_uInt16 nFormat, const css::uno::Sequence< css::beans::PropertyValue >* pFilterData )
{
SAL_INFO( "vcl.filter", "GraphicFilter::ExportGraphic() (thb)" );
sal_uInt16 nFormatCount = GetExportFormatCount();
ResetLastError();
if( nFormat == GRFILTER_FORMAT_DONTKNOW )
{
INetURLObject aURL( rPath );
OUString aExt( aURL.GetFileExtension().toAsciiUpperCase() );
for( sal_uInt16 i = 0; i < nFormatCount; i++ )
{
if ( pConfig->GetExportFormatExtension( i ).equalsIgnoreAsciiCase( aExt ) )
{
nFormat=i;
break;
}
}
}
if( nFormat >= nFormatCount )
return ImplSetError( ERRCODE_GRFILTER_FORMATERROR );
FilterConfigItem aConfigItem( pFilterData );
OUString aFilterName( pConfig->GetExportFilterName( nFormat ) );
OUString aExternalFilterName(pConfig->GetExternalFilterName(nFormat, true));
ErrCode nStatus = ERRCODE_NONE;
GraphicType eType;
Graphic aGraphic = ImpGetScaledGraphic( rGraphic, aConfigItem );
eType = aGraphic.GetType();
if( pConfig->IsExportPixelFormat( nFormat ) )
{
if( eType != GraphicType::Bitmap )
{
Size aSizePixel;
sal_uLong nBitsPerPixel,nNeededMem,nMaxMem;
ScopedVclPtrInstance< VirtualDevice > aVirDev;
nMaxMem = 1024;
nMaxMem *= 1024; // In Bytes
// Calculate how big the image would normally be:
aSizePixel=aVirDev->LogicToPixel(aGraphic.GetPrefSize(),aGraphic.GetPrefMapMode());
// Calculate how much memory the image will take up
nBitsPerPixel=aVirDev->GetBitCount();
nNeededMem=(static_cast<sal_uLong>(aSizePixel.Width())*static_cast<sal_uLong>(aSizePixel.Height())*nBitsPerPixel+7)/8;
// is the image larger than available memory?
if (nMaxMem<nNeededMem)
{
double fFak=sqrt(static_cast<double>(nMaxMem)/static_cast<double>(nNeededMem));
aSizePixel.setWidth(static_cast<sal_uLong>(static_cast<double>(aSizePixel.Width())*fFak) );
aSizePixel.setHeight(static_cast<sal_uLong>(static_cast<double>(aSizePixel.Height())*fFak) );
}
aVirDev->SetMapMode(MapMode(MapUnit::MapPixel));
aVirDev->SetOutputSizePixel(aSizePixel);
Graphic aGraphic2=aGraphic;
aGraphic2.Draw(aVirDev.get(),Point(0,0),aSizePixel); // this changes the MapMode
aVirDev->SetMapMode(MapMode(MapUnit::MapPixel));
aGraphic=Graphic(aVirDev->GetBitmapEx(Point(0,0),aSizePixel));
}
}
if( rOStm.GetError() )
nStatus = ERRCODE_GRFILTER_IOERROR;
if( ERRCODE_NONE == nStatus )
{
if ( pConfig->IsExportInternalFilter( nFormat ) )
{
if( aFilterName.equalsIgnoreAsciiCase( EXP_BMP ) )
{
BitmapEx aBmp( aGraphic.GetBitmapEx() );
BmpConversion nColorRes = static_cast<BmpConversion>(aConfigItem.ReadInt32( "Colors", 0 ));
if ( nColorRes != BmpConversion::NNONE && ( nColorRes <= BmpConversion::N24Bit) )
{
if( !aBmp.Convert( nColorRes ) )
aBmp = aGraphic.GetBitmapEx();
}
bool bRleCoding = aConfigItem.ReadBool( "RLE_Coding", true );
// save RLE encoded?
WriteDIB(aBmp, rOStm, bRleCoding);
if( rOStm.GetError() )
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else if( aFilterName.equalsIgnoreAsciiCase( EXP_SVMETAFILE ) )
{
sal_Int32 nVersion = aConfigItem.ReadInt32( "Version", 0 ) ;
if ( nVersion )
rOStm.SetVersion( nVersion );
// #i119735# just use GetGDIMetaFile, it will create a buffered version of contained bitmap now automatically
GDIMetaFile aMTF(aGraphic.GetGDIMetaFile());
aMTF.Write( rOStm );
if( rOStm.GetError() )
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else if ( aFilterName.equalsIgnoreAsciiCase( EXP_WMF ) )
{
bool bDone(false);
// do we have a native Vector Graphic Data RenderGraphic, whose data can be written directly?
auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
if (rVectorGraphicDataPtr
&& rVectorGraphicDataPtr->getVectorGraphicDataArrayLength()
&& VectorGraphicDataType::Wmf == rVectorGraphicDataPtr->getVectorGraphicDataType())
{
rOStm.WriteBytes(rVectorGraphicDataPtr->getVectorGraphicDataArray().getConstArray(), rVectorGraphicDataPtr->getVectorGraphicDataArrayLength());
if (rOStm.GetError())
{
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else
{
bDone = true;
}
}
if (!bDone)
{
// #i119735# just use GetGDIMetaFile, it will create a buffered version of contained bitmap now automatically
if (!ConvertGDIMetaFileToWMF(aGraphic.GetGDIMetaFile(), rOStm, &aConfigItem))
nStatus = ERRCODE_GRFILTER_FORMATERROR;
if (rOStm.GetError())
nStatus = ERRCODE_GRFILTER_IOERROR;
}
}
else if ( aFilterName.equalsIgnoreAsciiCase( EXP_EMF ) )
{
bool bDone(false);
// do we have a native Vector Graphic Data RenderGraphic, whose data can be written directly?
auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
if (rVectorGraphicDataPtr
&& rVectorGraphicDataPtr->getVectorGraphicDataArrayLength()
&& VectorGraphicDataType::Emf == rVectorGraphicDataPtr->getVectorGraphicDataType())
{
rOStm.WriteBytes(rVectorGraphicDataPtr->getVectorGraphicDataArray().getConstArray(), rVectorGraphicDataPtr->getVectorGraphicDataArrayLength());
if (rOStm.GetError())
{
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else
{
bDone = true;
}
}
if (!bDone)
{
// #i119735# just use GetGDIMetaFile, it will create a buffered version of contained bitmap now automatically
if (!ConvertGDIMetaFileToEMF(aGraphic.GetGDIMetaFile(), rOStm))
nStatus = ERRCODE_GRFILTER_FORMATERROR;
if (rOStm.GetError())
nStatus = ERRCODE_GRFILTER_IOERROR;
}
}
else if( aFilterName.equalsIgnoreAsciiCase( EXP_JPEG ) )
{
bool bExportedGrayJPEG = false;
if( !ExportJPEG( rOStm, aGraphic, pFilterData, &bExportedGrayJPEG ) )
nStatus = ERRCODE_GRFILTER_FORMATERROR;
if( rOStm.GetError() )
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else if ( aFilterName.equalsIgnoreAsciiCase( EXP_PNG ) )
{
vcl::PNGWriter aPNGWriter( aGraphic.GetBitmapEx(), pFilterData );
if ( pFilterData )
{
for ( const auto& rPropVal : *pFilterData )
{
if ( rPropVal.Name == "AdditionalChunks" )
{
css::uno::Sequence< css::beans::PropertyValue > aAdditionalChunkSequence;
if ( rPropVal.Value >>= aAdditionalChunkSequence )
{
for ( const auto& rAdditionalChunk : std::as_const(aAdditionalChunkSequence) )
{
if ( rAdditionalChunk.Name.getLength() == 4 )
{
sal_uInt32 nChunkType = 0;
for ( sal_Int32 k = 0; k < 4; k++ )
{
nChunkType <<= 8;
nChunkType |= static_cast<sal_uInt8>(rAdditionalChunk.Name[ k ]);
}
css::uno::Sequence< sal_Int8 > aByteSeq;
if ( rAdditionalChunk.Value >>= aByteSeq )
{
std::vector< vcl::PNGWriter::ChunkData >& rChunkData = aPNGWriter.GetChunks();
if ( !rChunkData.empty() )
{
sal_uInt32 nChunkLen = aByteSeq.getLength();
vcl::PNGWriter::ChunkData aChunkData;
aChunkData.nType = nChunkType;
if ( nChunkLen )
{
aChunkData.aData.resize( nChunkLen );
memcpy( aChunkData.aData.data(), aByteSeq.getConstArray(), nChunkLen );
}
std::vector< vcl::PNGWriter::ChunkData >::iterator aIter = rChunkData.end() - 1;
rChunkData.insert( aIter, aChunkData );
}
}
}
}
}
}
}
}
aPNGWriter.Write( rOStm );
if( rOStm.GetError() )
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else if( aFilterName.equalsIgnoreAsciiCase( EXP_SVG ) )
{
bool bDone(false);
// do we have a native Vector Graphic Data RenderGraphic, whose data can be written directly?
auto const & rVectorGraphicDataPtr(rGraphic.getVectorGraphicData());
if (rVectorGraphicDataPtr
&& rVectorGraphicDataPtr->getVectorGraphicDataArrayLength()
&& VectorGraphicDataType::Svg == rVectorGraphicDataPtr->getVectorGraphicDataType())
{
rOStm.WriteBytes(rVectorGraphicDataPtr->getVectorGraphicDataArray().getConstArray(), rVectorGraphicDataPtr->getVectorGraphicDataArrayLength());
if( rOStm.GetError() )
{
nStatus = ERRCODE_GRFILTER_IOERROR;
}
else
{
bDone = true;
}
}
if( !bDone )
{
// do the normal GDIMetaFile export instead
try
{
css::uno::Reference< css::uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
css::uno::Reference< css::xml::sax::XDocumentHandler > xSaxWriter(
css::xml::sax::Writer::create( xContext ), css::uno::UNO_QUERY_THROW);
css::uno::Sequence< css::uno::Any > aArguments( 1 );
aArguments[ 0 ] <<= aConfigItem.GetFilterData();
css::uno::Reference< css::svg::XSVGWriter > xSVGWriter(
xContext->getServiceManager()->createInstanceWithArgumentsAndContext( "com.sun.star.svg.SVGWriter", aArguments, xContext),
css::uno::UNO_QUERY );
if( xSaxWriter.is() && xSVGWriter.is() )
{
css::uno::Reference< css::io::XActiveDataSource > xActiveDataSource(
xSaxWriter, css::uno::UNO_QUERY );
if( xActiveDataSource.is() )
{
const css::uno::Reference< css::uno::XInterface > xStmIf(
static_cast< ::cppu::OWeakObject* >( new ImpFilterOutputStream( rOStm ) ) );
SvMemoryStream aMemStm( 65535, 65535 );
// #i119735# just use GetGDIMetaFile, it will create a buffered version of contained bitmap now automatically
const_cast<GDIMetaFile&>( aGraphic.GetGDIMetaFile() ).Write( aMemStm );
xActiveDataSource->setOutputStream( css::uno::Reference< css::io::XOutputStream >(
xStmIf, css::uno::UNO_QUERY ) );
css::uno::Sequence< sal_Int8 > aMtfSeq( static_cast<sal_Int8 const *>(aMemStm.GetData()), aMemStm.Tell() );
xSVGWriter->write( xSaxWriter, aMtfSeq );
}
}
}
catch(const css::uno::Exception&)
{
nStatus = ERRCODE_GRFILTER_IOERROR;
}
}
}
else
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
else
{
sal_Int32 nIdx {aFilterPath.isEmpty() ? -1 : 0};
while (nIdx>=0)
{
#ifndef DISABLE_DYNLOADING
OUString aPhysicalName( ImpCreateFullFilterPath( aFilterPath.getToken(0, ';', nIdx), aFilterName ) );
osl::Module aLibrary( aPhysicalName );
PFilterCall pFunc = nullptr;
if (aExternalFilterName == "egi")
pFunc = reinterpret_cast<PFilterCall>(aLibrary.getFunctionSymbol("egiGraphicExport"));
else if (aExternalFilterName == "eps")
pFunc = reinterpret_cast<PFilterCall>(aLibrary.getFunctionSymbol("epsGraphicExport"));
else if (aExternalFilterName == "eti")
pFunc = reinterpret_cast<PFilterCall>(aLibrary.getFunctionSymbol("etiGraphicExport"));
// Execute dialog in DLL
#else
--nIdx; // Just one iteration
PFilterCall pFunc = NULL;
if (aExternalFilterName == "egi")
pFunc = egiGraphicExport;
else if (aExternalFilterName == "eps")
pFunc = epsGraphicExport;
else if (aExternalFilterName == "eti")
pFunc = etiGraphicExport;
#endif
if( pFunc )
{
if ( !(*pFunc)( rOStm, aGraphic, &aConfigItem ) )
nStatus = ERRCODE_GRFILTER_FORMATERROR;
break;
}
else
nStatus = ERRCODE_GRFILTER_FILTERERROR;
}
}
}
if( nStatus != ERRCODE_NONE )
{
ImplSetError( nStatus, &rOStm );
}
return nStatus;
}
void GraphicFilter::ResetLastError()
{
pErrorEx->nStreamError = ERRCODE_NONE;
}
Link<ConvertData&,bool> GraphicFilter::GetFilterCallback() const
{
Link<ConvertData&,bool> aLink( LINK( const_cast<GraphicFilter*>(this), GraphicFilter, FilterCallback ) );
return aLink;
}
IMPL_LINK( GraphicFilter, FilterCallback, ConvertData&, rData, bool )
{
bool bRet = false;
sal_uInt16 nFormat = GRFILTER_FORMAT_DONTKNOW;
OString aShortName;
css::uno::Sequence< css::beans::PropertyValue > aFilterData;
switch( rData.mnFormat )
{
case ConvertDataFormat::BMP: aShortName = BMP_SHORTNAME; break;
case ConvertDataFormat::GIF: aShortName = GIF_SHORTNAME; break;
case ConvertDataFormat::JPG: aShortName = JPG_SHORTNAME; break;
case ConvertDataFormat::MET: aShortName = MET_SHORTNAME; break;
case ConvertDataFormat::PCT: aShortName = PCT_SHORTNAME; break;
case ConvertDataFormat::PNG: aShortName = PNG_SHORTNAME; break;
case ConvertDataFormat::SVM: aShortName = SVM_SHORTNAME; break;
case ConvertDataFormat::TIF: aShortName = TIF_SHORTNAME; break;
case ConvertDataFormat::WMF: aShortName = WMF_SHORTNAME; break;
case ConvertDataFormat::EMF: aShortName = EMF_SHORTNAME; break;
case ConvertDataFormat::SVG: aShortName = SVG_SHORTNAME; break;
default:
break;
}
if( GraphicType::NONE == rData.maGraphic.GetType() || rData.maGraphic.GetReaderContext() ) // Import
{
// Import
nFormat = GetImportFormatNumberForShortName( OStringToOUString( aShortName, RTL_TEXTENCODING_UTF8) );
bRet = ImportGraphic( rData.maGraphic, OUString(), rData.mrStm, nFormat ) == ERRCODE_NONE;
}
else if( !aShortName.isEmpty() )
{
// Export
#if defined(IOS) || defined(ANDROID)
if (aShortName == PNG_SHORTNAME)
{
aFilterData.realloc(aFilterData.getLength() + 1);
aFilterData[aFilterData.getLength() - 1].Name = "Compression";
// We "know" that this gets passed to zlib's deflateInit2_(). 1 means best speed.
aFilterData[aFilterData.getLength() - 1].Value <<= static_cast<sal_Int32>(1);
}
#endif
nFormat = GetExportFormatNumberForShortName( OStringToOUString(aShortName, RTL_TEXTENCODING_UTF8) );
bRet = ExportGraphic( rData.maGraphic, OUString(), rData.mrStm, nFormat, &aFilterData ) == ERRCODE_NONE;
}
return bRet;
}
namespace
{
class StandardGraphicFilter
{
public:
StandardGraphicFilter()
{
m_aFilter.GetImportFormatCount();
}
GraphicFilter m_aFilter;
};
class theGraphicFilter : public rtl::Static<StandardGraphicFilter, theGraphicFilter> {};
}
GraphicFilter& GraphicFilter::GetGraphicFilter()
{
return theGraphicFilter::get().m_aFilter;
}
ErrCode GraphicFilter::LoadGraphic( const OUString &rPath, const OUString &rFilterName,
Graphic& rGraphic, GraphicFilter* pFilter,
sal_uInt16* pDeterminedFormat )
{
if ( !pFilter )
pFilter = &GetGraphicFilter();
const sal_uInt16 nFilter = !rFilterName.isEmpty() && pFilter->GetImportFormatCount()
? pFilter->GetImportFormatNumber( rFilterName )
: GRFILTER_FORMAT_DONTKNOW;
INetURLObject aURL( rPath );
if ( aURL.HasError() )
{
aURL.SetSmartProtocol( INetProtocol::File );
aURL.SetSmartURL( rPath );
}
std::unique_ptr<SvStream> pStream;
if ( INetProtocol::File != aURL.GetProtocol() )
pStream = ::utl::UcbStreamHelper::CreateStream( rPath, StreamMode::READ );
ErrCode nRes = ERRCODE_NONE;
if ( !pStream )
nRes = pFilter->ImportGraphic( rGraphic, aURL, nFilter, pDeterminedFormat );
else
nRes = pFilter->ImportGraphic( rGraphic, rPath, *pStream, nFilter, pDeterminedFormat );
#ifdef DBG_UTIL
OUString aReturnString;
if (nRes == ERRCODE_GRFILTER_OPENERROR)
aReturnString="open error";
else if (nRes == ERRCODE_GRFILTER_IOERROR)
aReturnString="IO error";
else if (nRes == ERRCODE_GRFILTER_FORMATERROR)
aReturnString="format error";
else if (nRes == ERRCODE_GRFILTER_VERSIONERROR)
aReturnString="version error";
else if (nRes == ERRCODE_GRFILTER_FILTERERROR)
aReturnString="filter error";
else if (nRes == ERRCODE_GRFILTER_TOOBIG)
aReturnString="graphic is too big";
SAL_INFO_IF( nRes, "vcl.filter", "Problem importing graphic " << rPath << ". Reason: " << aReturnString );
#endif
return nRes;
}
ErrCode GraphicFilter::compressAsPNG(const Graphic& rGraphic, SvStream& rOutputStream)
{
css::uno::Sequence< css::beans::PropertyValue > aFilterData(1);
aFilterData[0].Name = "Compression";
aFilterData[0].Value <<= sal_uInt32(9);
sal_uInt16 nFilterFormat = GetExportFormatNumberForShortName("PNG");
return ExportGraphic(rGraphic, OUString(), rOutputStream, nFilterFormat, &aFilterData);
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|