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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* 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 mozilla.org Code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 2001
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
/*
* Implements the nsSanePluginInterface for the SANE plugin
*/
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <setjmp.h>
#include <gdk/gdk.h>
#include <gdk/gdkprivate.h>
#include <gtk/gtk.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "nsIIOService.h"
#include "nsSanePlugin.h"
#include "plstr.h"
#include "prmem.h"
#include "nsIEventQueueService.h"
#include "nsIEventQueue.h"
extern "C"
{
#include "jpeglib.h"
#include "sane/sane.h"
#include "sane/sanei.h"
#include "sane/saneopts.h"
}
#define MAX_OPT_SIZE (1024 * 2)
struct my_JPEG_error_mgr
{
struct jpeg_error_mgr pub;
sigjmp_buf setjmp_buffer;
};
typedef struct my_JPEG_error_mgr *emptr;
static void PR_CALLBACK ThreadedDestroyEvent(PLEvent* aEvent);
static void PR_CALLBACK ThreadedHandleEvent(PLEvent* aEvent);
//static void PR_CALLBACK scanimage_thread_routine( void * arg );
static void store_filename(GtkFileSelection *selector, gpointer user_data);
static nsresult optioncat( char ** dest, const char * src,
const char * ending );
static void my_jpeg_error_exit (j_common_ptr cinfo);
static unsigned char* jpeg_file_to_rgb (FILE * f, int *w, int *h);
static gboolean draw (GtkWidget *widget, GdkEventExpose *event, gpointer data);
static unsigned char * crop_image(unsigned char *rgb_data, int *rgb_width,
int *rgb_height, gint x, gint y,
gint w, gint h);
static unsigned char * scale_image(unsigned char *rgb_data, int rgb_width,
int rgb_height, int w, int h);
static PRInt32 gPluginObjectCount = 0;
static NS_DEFINE_CID(kCPluginManagerCID, NS_PLUGINMANAGER_CID );
static NS_DEFINE_CID(kEventQueueService, NS_EVENTQUEUESERVICE_CID );
///////////////////////////////////////////////////////////////////////////////
// Plugin Instance API Methods
nsSanePluginInstance::nsSanePluginInstance( void )
: fPeer( NULL ), fWindow( NULL ), fMode( nsPluginMode_Embedded )
{
#ifdef DEBUG
printf("nsSanePluginInstance::nsSanePluginInstance()\n");
#endif
PR_AtomicIncrement(&gPluginObjectCount);
// set default jpeg compression attributes
mCompQuality = 10;
mCompMethod = JDCT_FASTEST;
// Initialize sane interface
mSaneDevice = nsnull;
mSaneOpen = SANE_FALSE;
mRGBData = nsnull;
mBottomRightXChange = mBottomRightYChange =
mTopLeftXChange = mTopLeftYChange = 0;
// initialize JavaScript callback members
mOnScanCompleteScript = nsnull;
mOnInitCompleteScript = nsnull;
mScanThread = nsnull;
mUIThread = PR_GetCurrentThread();
mState = 0; // idle
mSuccess = 1; // success
mFileSelection = nsnull;
// initialize gtk widgets
fPlatform.widget = nsnull;
mEvent_box = nsnull;
mDrawing_area = nsnull;
if (NS_FAILED(CallCreateInstance(kCPluginManagerCID, &mPluginManager))) {
NS_ERROR("Error trying to create plugin manager!");
return;
}
}
nsSanePluginInstance::~nsSanePluginInstance(void)
{
#ifdef DEBUG
printf("nsSanePluginInstance::~nsSanePluginInstance(%i)\n",
gPluginObjectCount );
#endif
PR_AtomicDecrement(&gPluginObjectCount);
PlatformDestroy(); // Perform platform specific cleanup
if (mPluginManager) {
mPluginManager->Release();
mPluginManager = nsnull;
}
sane_exit();
// cleanup old widgets
if (fPlatform.widget)
gtk_widget_destroy(fPlatform.widget);
if (mEvent_box)
gtk_widget_destroy(mEvent_box);
if (mDrawing_area)
gtk_widget_destroy(mDrawing_area);
NS_IF_RELEASE(fPeer);
PR_FREEIF(mSaneDevice);
PR_FREEIF(mOnScanCompleteScript);
PR_FREEIF(mOnInitCompleteScript);
}
// This macro produces simple versions of QueryInterface, AddRef and Release
// See the nsISupports.h header file for details.
NS_IMPL_ISUPPORTS2(nsSanePluginInstance,
nsIPluginInstance,
nsISanePluginInstance)
NS_METHOD
nsSanePluginInstance::Initialize( nsIPluginInstancePeer* peer )
{
#ifdef DEBUG
printf("nsSanePluginInstance::Initialize()\n");
#endif
NS_ASSERTION(peer != NULL, "null peer");
nsIPluginTagInfo * taginfo;
const char * const * names = nsnull;
const char * const * values = nsnull;
PRUint16 count = 0;
nsresult result;
gdk_rgb_init();
fPeer = peer;
peer->AddRef();
result = peer->GetMode( &fMode );
if ( NS_FAILED( result ) )
return result;
result = peer->QueryInterface( nsIPluginTagInfo::GetIID(),
( void ** )&taginfo );
if ( NS_SUCCEEDED( result ) )
{
taginfo->GetAttributes( count, names, values );
NS_IF_RELEASE( taginfo );
}
// Initialize default line attribute info
mLineWidth = 5;
mLineStyle = GDK_LINE_ON_OFF_DASH;
mCapStyle = GDK_CAP_BUTT;
mJoinStyle = GDK_JOIN_MITER;
// Get user requested attribute info
for (int i=0; i < count; i++) {
if (PL_strcasecmp(names[i], "line_width") == 0) {
//////////////
// Line width
sscanf(values[i], "%i", &mLineWidth);
} else if (PL_strcasecmp(names[i], "line_style") == 0) {
//////////////
// Line style
if (PL_strcasecmp(values[i], "dash") == 0)
mLineStyle = GDK_LINE_ON_OFF_DASH;
else if (PL_strcasecmp(values[i], "solid") == 0)
mLineStyle = GDK_LINE_SOLID;
else if (PL_strcasecmp(values[i], "double_dash") == 0)
mLineStyle = GDK_LINE_DOUBLE_DASH;
} else if (PL_strcasecmp(names[i], "cap_style") == 0) {
/////////////
// Cap style
if (PL_strcasecmp(values[i], "round") == 0)
mCapStyle = GDK_CAP_ROUND;
else if (PL_strcasecmp(values[i], "projecting") == 0)
mCapStyle = GDK_CAP_PROJECTING;
else if (PL_strcasecmp(values[i], "butt") == 0)
mCapStyle = GDK_CAP_BUTT;
else if (PL_strcasecmp(values[i], "not_last") == 0)
mCapStyle = GDK_CAP_NOT_LAST;
} else if (PL_strcasecmp(names[i], "join_style") == 0) {
//////////////
// Join style
if (PL_strcasecmp(values[i], "miter") == 0)
mJoinStyle = GDK_JOIN_MITER;
else if (PL_strcasecmp(values[i], "round") == 0)
mJoinStyle = GDK_JOIN_ROUND;
else if (PL_strcasecmp(values[i], "bevel") == 0)
mJoinStyle = GDK_JOIN_BEVEL;
} else if (PL_strcasecmp(names[i], "device") == 0) {
//////////
// Device
PR_FREEIF(mSaneDevice);
mSaneDevice = PL_strdup(values[i]);
} else if (PL_strcasecmp(names[i], "onScanComplete") == 0) {
//////////////////////////
// onScanComplete Callback
PR_FREEIF(mOnScanCompleteScript);
mOnScanCompleteScript = PL_strdup(values[i]);
} else if(PL_strcasecmp(names[i], "onInitComplete") == 0) {
//////////////////////////
// onInitComplete Callback
PR_FREEIF(mOnInitCompleteScript);
mOnInitCompleteScript = PL_strdup(values[i]);
}
}
// the mImageFilename is used to store data directly
// from the sane device and also rendering to the screen
sprintf( mImageFilename, "/tmp/nsSanePlugin.%p.jpg", (void *)this );
PlatformNew(); /* Call Platform-specific initializations */
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::SaveImage()
{
#ifdef DEBUG
printf("nsSanePluginInstance::SaveImage()\n");
#endif
if (!mImageFilename) {
NS_ERROR("Attempt to save image before initial scan!");
return NS_ERROR_FAILURE;
}
mFileSelection = gtk_file_selection_new("Choose file to save image to.");
// Set a default filename
gtk_file_selection_set_filename(GTK_FILE_SELECTION(mFileSelection),
"saved.jpg");
// Callback for getting the filename
gtk_signal_connect(GTK_OBJECT(GTK_FILE_SELECTION(mFileSelection)->ok_button),
"clicked", GTK_SIGNAL_FUNC (store_filename),
(gpointer) this);
// Ensure the file selection dialog goes away after
// 'OK' or 'CANCEL' button is selected
gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(mFileSelection)->ok_button),
"clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
GTK_OBJECT(mFileSelection));
gtk_signal_connect_object(GTK_OBJECT(GTK_FILE_SELECTION(mFileSelection)->cancel_button),
"clicked", GTK_SIGNAL_FUNC (gtk_widget_destroy),
GTK_OBJECT(mFileSelection));
// ok, now show the file selection dialog
gtk_widget_show(mFileSelection);
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::DoScanCompleteCallback()
{
#ifdef DEBUG
printf("nsSanePluginInstance::DoScanCompleteCallback()\n");
#endif
char *url;
if (mOnScanCompleteScript && mPluginManager) {
url = (char *)PR_Malloc(PL_strlen(mOnScanCompleteScript) + 13);
if (!url)
return NS_ERROR_OUT_OF_MEMORY;
sprintf(url, "javascript:%s", mOnScanCompleteScript);
mPluginManager->GetURL((nsIPluginInstance *)this, url, "_self");
PR_FREEIF(url);
}
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::DoInitCompleteCallback()
{
#ifdef DEBUG
printf("nsSanePluginInstance::DoInitCompleteCallback()\n");
#endif
char *url;
if (mOnInitCompleteScript && mPluginManager) {
url = (char *)PR_Malloc(PL_strlen(mOnInitCompleteScript) + 13);
if (!url)
return NS_ERROR_OUT_OF_MEMORY;
sprintf(url, "javascript:%s", mOnInitCompleteScript);
mPluginManager->GetURL((nsIPluginInstance *)this, url, "_self");
PR_FREEIF(url);
}
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::GetPeer( nsIPluginInstancePeer* *result )
{
#ifdef DEBUG
printf("nsSanePluginInstance::GetPeer()\n");
#endif
fPeer->AddRef();
*result = fPeer;
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::Start( void )
{
#ifdef DEBUG
printf("nsSanePluginInstance::Start()\n");
#endif
PR_FREEIF(mRGBData);
mRGBData = nsnull;
sane_init(0,0);
mSaneOpen = SANE_FALSE;
nsresult rv = OpenSaneDeviceIF();
if (rv != NS_OK) {
NS_ERROR("Failed to open device in Start()\n");
return rv;
}
PlatformNew();
// call onInitComplete callback
DoInitCompleteCallback();
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::Stop( void )
{
#ifdef DEBUG
printf("nsSanePluginInstance::Stop()\n");
#endif
// Wait for scanner operation to complete.
// TODO: Warn user that they are still scanning
// and send abort command to scanner if
// the user wishes.
if (mScanThread)
PR_JoinThread(mScanThread);
PR_FREEIF(mRGBData);
// close sane
if (mSaneOpen) {
sane_close(mSaneHandle);
sane_exit();
mSaneOpen = SANE_FALSE;
}
if (remove ( mImageFilename ) == -1)
perror(mImageFilename);
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::Destroy( void )
{
#ifdef DEBUG
printf("nsSanePluginInstance::Destroy()\n");
#endif
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::SetWindow(nsPluginWindow* window)
{
#ifdef DEBUG
printf("nsSanePluginInstance::SetWindow()\n");
#endif
nsresult result;
// The real work is handled in PlatformSetWindow
// (as if this plug-in was multiplatform)
result = PlatformSetWindow( window );
fWindow = window;
return result;
}
NS_METHOD
nsSanePluginInstance::NewStream( nsIPluginStreamListener** listener )
{
#ifdef DEBUG
printf("nsSanePluginInstance::NewStream()\n");
#endif
if ( listener != NULL )
*listener = new nsSanePluginStreamListener( this );
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::Print( nsPluginPrint* printInfo )
{
#ifdef DEBUG
printf("nsSanePluginInstance::Print()\n");
#endif
if ( printInfo == NULL )
return NS_ERROR_FAILURE;
// needs to be implemented
return NS_ERROR_NOT_IMPLEMENTED;
}
NS_METHOD
nsSanePluginInstance::HandleEvent(nsPluginEvent* event, PRBool* handled)
{
#ifdef DEBUG
printf("nsSanePluginInstance::HandleEvent()\n");
#endif
// Not used on unix platform. Only here for potential
// future porting efforts
*handled = (PRBool)PlatformHandleEvent(event);
return NS_OK;
}
NS_METHOD
nsSanePluginInstance::GetValue(nsPluginInstanceVariable variable, void *value)
{
#ifdef DEBUG
printf("nsSanePluginInstance::GetValue()\n");
#endif
//Needs to be implemented
return NS_ERROR_NOT_IMPLEMENTED;
}
void
nsSanePluginInstance::PlatformNew( void )
{
#ifdef DEBUG
printf("nsSaneInstance::PlatformNew\n");
#endif
fPlatform.window = 0;
}
nsresult
nsSanePluginInstance::PlatformDestroy( void )
{
#ifdef DEBUG
printf("nsSaneInstance::PlatformDestroy()\n");
#endif
return NS_OK;
}
nsresult
nsSanePluginInstance::PlatformSetWindow( nsPluginWindow* window )
{
#ifdef DEBUG
printf("nsSaneInstance::PlatformSetWindow()\n");
#endif
if ( window == NULL || window->window == NULL )
return NS_ERROR_NULL_POINTER;
if ( fPlatform.superwin == (GdkSuperWin *)window->window )
return NS_OK;
// Record windowing info
fPlatform.superwin = (GdkSuperWin *)window->window;
fPlatform.height = window->height;
fPlatform.width = window->width;
// cleanup old widgets
if (fPlatform.widget)
gtk_widget_destroy(fPlatform.widget);
if (mEvent_box)
gtk_widget_destroy(mEvent_box);
if (mDrawing_area)
gtk_widget_destroy(mDrawing_area);
fPlatform.widget = gtk_mozbox_new(fPlatform.superwin->bin_window);
// Initialize the zoom box to outline the entire image
mZoom_box.x = 0;
mZoom_box.y = 0;
mZoom_box.width = fPlatform.width;
mZoom_box.height = fPlatform.height;
// Setup an event box to contain the drawable
mEvent_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(fPlatform.widget), mEvent_box);
gtk_widget_show(mEvent_box);
// Setup the drawable
mDrawing_area = ( GtkWidget * )gtk_drawing_area_new();
gtk_drawing_area_size( GTK_DRAWING_AREA( mDrawing_area ),
window->width, window->height);
gtk_container_add(GTK_CONTAINER(mEvent_box), mDrawing_area);
gtk_widget_show(mDrawing_area);
// Initialize line attributes for GC
GdkGC * da_gc = mDrawing_area->style->fg_gc[mDrawing_area->state];
gdk_gc_set_line_attributes ( da_gc, mLineWidth, mLineStyle,
mCapStyle, mJoinStyle);
// Register signals
gtk_signal_connect (GTK_OBJECT(mDrawing_area), "expose_event",
GTK_SIGNAL_FUNC(draw), this);
// Now make everything visable
gtk_widget_show( fPlatform.widget );
return NS_OK;
}
int16
nsSanePluginInstance::PlatformHandleEvent(nsPluginEvent* event)
{
#ifdef DEBUG
printf("nsSaneInstance::PlatformHandleEvent()\n");
#endif
/* UNIX Plugins do not use HandleEvent */
return 0;
}
NS_IMETHODIMP
nsSanePluginInstance::PaintImage(void)
{
#ifdef DEBUG
printf("nsSaneInstance::PaintImage(%p)\n", this);
#endif
if (mDrawing_area && fPlatform.widget) {
if (mImageFilename) {
if (!mRGBData) {
if (mState)
// we are currently scanning, so the jpeg file
// is not valid yet
return NS_OK;
FILE * f = fopen(mImageFilename, "rb");
if (!f)
// it's possible that an image hasn't been scanned yet
return NS_OK;
mRGBData = jpeg_file_to_rgb(f, &mRGBWidth, &mRGBHeight);
if (!mRGBData) {
NS_ERROR("Error converting jpeg to rgb data!");
return NS_ERROR_FAILURE;
}
fclose(f);
// scale down to window size
unsigned char * tmp = scale_image( mRGBData,
mRGBWidth, mRGBHeight,
fPlatform.width,
fPlatform.height);
if (!tmp) {
NS_ERROR("Error trying to scale image!");
return NS_ERROR_FAILURE;
}
// move to new data
PR_FREEIF(mRGBData);
mRGBData = tmp;
}
// render the image
gdk_draw_rgb_image (mDrawing_area->window,
mDrawing_area->style->fg_gc[GTK_STATE_NORMAL],
0, 0, fPlatform.width, fPlatform.height,
GDK_RGB_DITHER_MAX, mRGBData,
fPlatform.width * 3);
gdk_draw_rectangle (mDrawing_area->window,
mDrawing_area->style->fg_gc[mDrawing_area->state],
FALSE, mZoom_box.x, mZoom_box.y,
mZoom_box.width, mZoom_box.height);
gdk_flush();
}
}
return NS_OK;
}
char *
nsSanePluginInstance::GetImageFilename()
{
if (!mImageFilename)
return (char *)NULL;
// Caller is responsible for freeing string
return PL_strdup(mImageFilename);
}
GtkWidget *
nsSanePluginInstance::GetFileSelection()
{
if (!mFileSelection)
return (GtkWidget *)NULL;
return mFileSelection;
}
PRBool
nsSanePluginInstance::IsUIThread()
{
if (!mUIThread)
return PR_FALSE;
return (mUIThread == PR_GetCurrentThread());
}
// End of Plugin Instance API Methods.
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// Image Preview Interface.
NS_IMETHODIMP
nsSanePluginInstance::ZoomImage( PRUint16 x, PRUint16 y,
PRUint16 width, PRUint16 height)
{
#ifdef DEBUG
printf("nsSaneInstance::ZoomImage()\n");
#endif
int im_width, im_height, c_width, c_height, c_x, c_y;
if (!mRGBData)
return NS_ERROR_FAILURE;
// get the actual image width and height
im_width = mRGBWidth;
im_height = mRGBHeight;
if ( x >= (PRUint16)fPlatform.width ||
y >= (PRUint16)fPlatform.height )
return NS_ERROR_FAILURE;
if ( width + mZoom_box.x >= (PRUint16)fPlatform.width )
mZoom_box.width = fPlatform.width - mZoom_box.x;
if ( height + mZoom_box.y >= (PRUint16) fPlatform.height )
mZoom_box.height = fPlatform.height - mZoom_box.y;
mZoom_box.x = x;
mZoom_box.y = y;
// translate the zoom box geometry to the image geometry
c_width = (int)(((double)im_width/(double)fPlatform.width) *
(double)mZoom_box.width);
c_height =(int)(((double)im_height/(double)fPlatform.height) *
(double)mZoom_box.height);
c_x = (int)(((double)im_width/(double)fPlatform.width) *
(double)mZoom_box.x);
c_y = (int)(((double)im_height/(double)fPlatform.height) *
(double)mZoom_box.y);
unsigned char * tmp = crop_image(mRGBData, &mRGBWidth, &mRGBHeight,
c_x, c_y, c_width, c_height);
if (!tmp)
return NS_ERROR_FAILURE;
PR_FREEIF(mRGBData);
mRGBData = tmp;
mRGBWidth = c_width;
mRGBHeight = c_height;
mTopLeftXChange = (float)x/(float)fPlatform.width;
mTopLeftYChange = (float)y/(float)fPlatform.height;
mBottomRightXChange = (float)(fPlatform.width - (PRInt32)(x + mZoom_box.width))/(float)fPlatform.width;
mBottomRightYChange = (float)(fPlatform.height - (PRInt32)(y + mZoom_box.height))/(float)fPlatform.height;
mZoom_box.x = 0;
mZoom_box.y = 0;
mZoom_box.width = (PRUint16)fPlatform.width;
mZoom_box.height = (PRUint16)fPlatform.height;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::ZoomImageWithAttributes( PRUint16 x, PRUint16 y,
PRUint16 width, PRUint16 height,
PRInt32 req_line_width,
const char * req_line_style,
const char * req_cap_style,
const char * req_join_style )
{
#ifdef DEBUG
printf("nsSaneInstance::ZoomImageWithAttributes()\n");
#endif
if (!mRGBData)
return NS_ERROR_FAILURE;
ZoomImage(x, y, width, height);
/*
* Set zoom box line attributes
*/
// Line Width
mLineWidth = req_line_width;
// Line Style
if (PL_strcasecmp(req_line_style, "dash") == 0)
mLineStyle = GDK_LINE_ON_OFF_DASH;
else if (PL_strcasecmp(req_line_style, "solid") == 0)
mLineStyle = GDK_LINE_SOLID;
else if (PL_strcasecmp(req_line_style, "double_dash") == 0)
mLineStyle = GDK_LINE_DOUBLE_DASH;
// Cap Style
if (PL_strcasecmp(req_cap_style, "round") == 0)
mCapStyle = GDK_CAP_ROUND;
else if (PL_strcasecmp(req_cap_style, "projecting") == 0)
mCapStyle = GDK_CAP_PROJECTING;
else if (PL_strcasecmp(req_cap_style, "butt") == 0)
mCapStyle = GDK_CAP_BUTT;
else if (PL_strcasecmp(req_cap_style, "not_last") == 0)
mCapStyle = GDK_CAP_NOT_LAST;
// Join Style
if (PL_strcasecmp(req_join_style, "miter") == 0)
mJoinStyle = GDK_JOIN_MITER;
else if (PL_strcasecmp(req_join_style, "round") == 0)
mJoinStyle = GDK_JOIN_ROUND;
else if (PL_strcasecmp(req_join_style, "bevel") == 0)
mJoinStyle = GDK_JOIN_BEVEL;
// Refresh
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::Crop(PRUint16 x, PRUint16 y,
PRUint16 width, PRUint16 height)
{
#ifdef DEBUG
printf("nsSaneInstance::Crop()\n");
#endif
if ( (x <= (PRUint16)fPlatform.width - width ) &&
(y <= (PRUint16)fPlatform.height - height) ) {
mZoom_box.x = x; mZoom_box.y = y;
mZoom_box.width = width;
mZoom_box.height = height;
} else {
return NS_ERROR_FAILURE;
}
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::Restore(void)
{
#ifdef DEBUG
printf("nsSaneInstance::Restore()\n");
#endif
PR_FREEIF(mRGBData);
mRGBData = nsnull;
// Reset zoom box
mZoom_box.x = mZoom_box.y = 0;
mZoom_box.width = (PRUint16) fPlatform.width;
mZoom_box.height = (PRUint16) fPlatform.height;
PaintImage();
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomX(PRUint16 * x)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomX()\n");
#endif
NS_ASSERTION(x != NULL, "null x pointer");
if (!x)
return NS_ERROR_NULL_POINTER;
*x = mZoom_box.x;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomX(PRUint16 x)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomX()\n");
#endif
if (x > (PRInt32) fPlatform.width)
mZoom_box.x = (PRInt32) fPlatform.width;
else
mZoom_box.x = x;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomY(PRUint16 * y)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomY()\n");
#endif
NS_ASSERTION(y != NULL, "null y pointer");
if (!y)
return NS_ERROR_NULL_POINTER;
*y = mZoom_box.y;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomY(PRUint16 y)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomY()\n");
#endif
if (y > (PRInt32) fPlatform.height)
mZoom_box.y = (PRInt32)fPlatform.height;
else
mZoom_box.y = y;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomWidth(PRUint16 * width)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomWidth()\n");
#endif
NS_ASSERTION(width != NULL, "null width pointer");
if (!width)
return NS_ERROR_NULL_POINTER;
*width = mZoom_box.width;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomWidth(PRUint16 width)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomWidth()\n");
#endif
if (width + mZoom_box.x > (PRUint16)fPlatform.width)
mZoom_box.width = (PRInt32) fPlatform.width - mZoom_box.x;
else
mZoom_box.width = width;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomHeight(PRUint16 * height)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomHeight()\n");
#endif
NS_ASSERTION(height != NULL, "null height pointer");
if (!height)
return NS_ERROR_NULL_POINTER;
*height = mZoom_box.height;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomHeight(PRUint16 height)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomHeight()\n");
#endif
if (height + mZoom_box.y > (PRUint16) fPlatform.height)
mZoom_box.height = fPlatform.height - mZoom_box.y;
else
mZoom_box.height = height;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomLineWidth(PRInt32 * aZoomLineWidth)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomLineWidth()\n");
#endif
NS_ASSERTION(aZoomLineWidth != NULL, "null width pointer");
if (!aZoomLineWidth)
return NS_ERROR_NULL_POINTER;
*aZoomLineWidth = mLineWidth;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomLineWidth(PRInt32 aZoomLineWidth)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomLineWidth()\n");
#endif
if (aZoomLineWidth <= 0)
return NS_ERROR_INVALID_ARG;
mLineWidth = aZoomLineWidth;
return PaintImage();
}
#define MAX_LINE_ATTRIBUTE_LENGTH 20
NS_IMETHODIMP
nsSanePluginInstance::GetZoomLineStyle(char ** aZoomLineStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomLineStyle()\n");
#endif
NS_ASSERTION(*aZoomLineStyle != NULL, "null width pointer");
if (!*aZoomLineStyle)
return NS_ERROR_NULL_POINTER;
*aZoomLineStyle = (char*) nsMemory::Alloc(MAX_LINE_ATTRIBUTE_LENGTH);
if (! *aZoomLineStyle)
return NS_ERROR_OUT_OF_MEMORY;
if (mLineStyle == GDK_LINE_ON_OFF_DASH)
PL_strcpy(*aZoomLineStyle, "dash");
else if (mLineStyle == GDK_LINE_SOLID)
PL_strcpy(*aZoomLineStyle, "solid");
else if (mLineStyle == GDK_LINE_DOUBLE_DASH)
PL_strcpy(*aZoomLineStyle, "double_dash");
else
PL_strcpy(*aZoomLineStyle, "unknown");
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomLineStyle(const char * aZoomLineStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomLineStyle()\n");
#endif
NS_ASSERTION(aZoomLineStyle != NULL, "null width pointer");
if (!aZoomLineStyle)
return NS_ERROR_NULL_POINTER;
// Line Style
if (PL_strcasecmp(aZoomLineStyle, "dash") == 0)
mLineStyle = GDK_LINE_ON_OFF_DASH;
else if (PL_strcasecmp(aZoomLineStyle, "solid") == 0)
mLineStyle = GDK_LINE_SOLID;
else if (PL_strcasecmp(aZoomLineStyle, "double_dash") == 0)
mLineStyle = GDK_LINE_DOUBLE_DASH;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomCapStyle(char ** aZoomCapStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomCapStyle()\n");
#endif
NS_ASSERTION(*aZoomCapStyle != NULL, "null width pointer");
if (!*aZoomCapStyle)
return NS_ERROR_NULL_POINTER;
*aZoomCapStyle = (char*) nsMemory::Alloc(MAX_LINE_ATTRIBUTE_LENGTH);
if (! *aZoomCapStyle)
return NS_ERROR_OUT_OF_MEMORY;
if (mCapStyle == GDK_CAP_ROUND)
PL_strcpy(*aZoomCapStyle, "round");
else if (mCapStyle == GDK_CAP_PROJECTING)
PL_strcpy(*aZoomCapStyle, "projecting");
else if (mCapStyle == GDK_CAP_BUTT)
PL_strcpy(*aZoomCapStyle, "butt");
else if (mCapStyle == GDK_CAP_NOT_LAST)
PL_strcpy(*aZoomCapStyle, "not_last");
else
PL_strcpy(*aZoomCapStyle, "unknown");
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomCapStyle(const char * aZoomCapStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomCapStyle()\n");
#endif
NS_ASSERTION(aZoomCapStyle != NULL, "null width pointer");
if (!aZoomCapStyle)
return NS_ERROR_NULL_POINTER;
// Cap Style
if (PL_strcasecmp(aZoomCapStyle, "round") == 0)
mCapStyle = GDK_CAP_ROUND;
else if (PL_strcasecmp(aZoomCapStyle, "projecting") == 0)
mCapStyle = GDK_CAP_PROJECTING;
else if (PL_strcasecmp(aZoomCapStyle, "butt") == 0)
mCapStyle = GDK_CAP_BUTT;
else if (PL_strcasecmp(aZoomCapStyle, "not_last") == 0)
mCapStyle = GDK_CAP_NOT_LAST;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomJoinStyle(char ** aZoomJoinStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomJoinStyle()\n");
#endif
NS_ASSERTION(*aZoomJoinStyle != NULL, "null width pointer");
if (!*aZoomJoinStyle)
return NS_ERROR_NULL_POINTER;
*aZoomJoinStyle = (char*) nsMemory::Alloc(MAX_LINE_ATTRIBUTE_LENGTH);
if (! *aZoomJoinStyle)
return NS_ERROR_OUT_OF_MEMORY;
if (mJoinStyle == GDK_JOIN_MITER)
PL_strcpy(*aZoomJoinStyle, "miter");
else if (mJoinStyle == GDK_JOIN_ROUND)
PL_strcpy(*aZoomJoinStyle, "round");
else if (mJoinStyle == GDK_JOIN_BEVEL)
PL_strcpy(*aZoomJoinStyle, "bevel");
else
PL_strcpy(*aZoomJoinStyle, "unknown");
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomJoinStyle(const char * aZoomJoinStyle)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomJoinStyle()\n");
#endif
NS_ASSERTION(aZoomJoinStyle != NULL, "null width pointer");
if (!aZoomJoinStyle)
return NS_ERROR_NULL_POINTER;
// Join Style
if (PL_strcasecmp(aZoomJoinStyle, "miter") == 0)
mJoinStyle = GDK_JOIN_MITER;
else if (PL_strcasecmp(aZoomJoinStyle, "round") == 0)
mJoinStyle = GDK_JOIN_ROUND;
else if (PL_strcasecmp(aZoomJoinStyle, "bevel") == 0)
mJoinStyle = GDK_JOIN_BEVEL;
return PaintImage();
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomBR_XChange(float * aZoomBR_XChange)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomBR_XChange()\n");
#endif
*aZoomBR_XChange = mBottomRightXChange;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomBR_XChange(float aZoomBR_XChange)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomBR_XChange()\n");
#endif
// Read-Only
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomBR_YChange(float * aZoomBR_YChange)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomBR_YChange()\n");
#endif
*aZoomBR_YChange = mBottomRightYChange;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomBR_YChange(float aZoomBR_YChange)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomBR_YChange()\n");
#endif
// Read-Only
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomTL_XChange(float * aZoomTL_XChange)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomTL_XChange()\n");
#endif
*aZoomTL_XChange = mTopLeftXChange;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomTL_XChange(float aZoomTL_XChange)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomTL_XChange()\n");
#endif
// Read-Only
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetZoomTL_YChange(float * aZoomTL_YChange)
{
#ifdef DEBUG
printf("nsSaneInstance::GetZoomTL_YChange()\n");
#endif
*aZoomTL_YChange = mTopLeftYChange;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetZoomTL_YChange(float aZoomTL_YChange)
{
#ifdef DEBUG
printf("nsSaneInstance::SetZoomTL_YChange()\n");
#endif
// Read-Only
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetQuality(PRInt32 * aQuality)
{
#ifdef DEBUG
printf("nsSaneInstance::GetQuality()\n");
#endif
NS_ASSERTION(aQuality != NULL, "null compression quality pointer");
if (!aQuality)
return NS_ERROR_NULL_POINTER;
*aQuality = mCompQuality;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetQuality(PRInt32 aQuality)
{
#ifdef DEBUG
printf("nsSaneInstance::SetQuality()\n");
#endif
NS_ASSERTION(aQuality > -1 && aQuality < 101,
"Invalid compression quality");
if (aQuality < 0 || aQuality > 100)
return NS_ERROR_INVALID_ARG;
mCompQuality = aQuality;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::GetMethod(char ** aMethod)
{
#ifdef DEBUG
printf("nsSaneInstance::GetMethod()\n");
#endif
NS_ASSERTION(aMethod != NULL, "Null method pointer");
if (!aMethod)
return NS_ERROR_NULL_POINTER;
*aMethod = (char*) nsMemory::Alloc(8);
if (!*aMethod)
return NS_ERROR_OUT_OF_MEMORY;
switch (mCompMethod) {
case JDCT_ISLOW:
PL_strcpy(*aMethod, "SLOW");
break;
case JDCT_IFAST:
PL_strcpy(*aMethod, "FAST");
break;
case JDCT_FLOAT:
PL_strcpy(*aMethod, "FLOAT");
break;
default:
PL_strcpy(*aMethod, "DEFAULT");
}
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetMethod(const char * aMethod)
{
#ifdef DEBUG
printf("nsSaneInstance::SetMethod()\n");
#endif
NS_ASSERTION(aMethod != NULL, "Null method pointer");
if (!aMethod)
return NS_ERROR_NULL_POINTER;
if (PL_strcasecmp(aMethod, "SLOW") == 0)
mCompMethod = JDCT_ISLOW;
else if (PL_strcasecmp(aMethod, "FAST") == 0)
mCompMethod = JDCT_IFAST;
else if (PL_strcasecmp(aMethod, "FLOAT") == 0)
mCompMethod = JDCT_FLOAT;
else
mCompMethod = JDCT_DEFAULT;
return NS_OK;
}
// END of Image Preview Interface
///////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Generic SANE Interface
NS_IMETHODIMP
nsSanePluginInstance::GetDeviceOptions(char ** aDeviceOptions)
{
#ifdef DEBUG
printf("nsSaneInstance::GetDeviceOptions()\n");
#endif
const SANE_Option_Descriptor * optdesc;
SANE_Int string_size;
char *range_string, *word_string;
nsresult rv;
NS_ASSERTION(aDeviceOptions != NULL, "null options pointer");
if (!aDeviceOptions)
return NS_ERROR_NULL_POINTER;
*aDeviceOptions = nsnull;
rv = OpenSaneDeviceIF();
if (rv != NS_OK)
return rv;
// Find out how many options exist for device
optdesc = sane_get_option_descriptor(mSaneHandle, 0);
if (!optdesc || optdesc->size == 0) {
NS_ERROR("Error trying to get number of options for device!");
return NS_ERROR_FAILURE;
}
if (optdesc->size == 1) { /* size includes "size" option */
NS_ERROR("Hmm.. That's odd! No options for this device?");
return NS_OK; // I guess it's possible?
}
// allocate string to contain data to return to caller
string_size = ((optdesc->size - 1) * MAX_OPT_SIZE) + 1;
*aDeviceOptions = (char*) nsMemory::Alloc(string_size);
NS_ENSURE_TRUE(*aDeviceOptions, NS_ERROR_OUT_OF_MEMORY);
// initialize options string
for (int n=0; n<string_size; n++)
(*aDeviceOptions)[n] = '\0';
// step through each of the options and copy over relavent data
int i = 1;
while((optdesc = sane_get_option_descriptor(mSaneHandle, i)) != NULL) {
i++;
// don't return grouping options
if (optdesc->type == SANE_TYPE_GROUP)
continue;
/*
* WARNING! Need to replace ','s and ';'s with
* something else so that JavaScript parsing
* doesn't get hosed!
*/
// NAME
if (optdesc->name)
optioncat(aDeviceOptions, optdesc->name, ", ");
else
strcat(*aDeviceOptions, "N/A, ");
////////////////////////////////////
// TITLE
if (optdesc->title)
optioncat(aDeviceOptions, optdesc->title, ", ");
else
PL_strcat(*aDeviceOptions, "N/A, ");
////////////////////////////////////
// DESCRIPTION
if (optdesc->desc)
optioncat(aDeviceOptions, optdesc->desc, ", ");
else
PL_strcat(*aDeviceOptions, "N/A, ");
////////////////////////////////////
// Option Type
switch (optdesc->type) {
case SANE_TYPE_BOOL:
PL_strcat(*aDeviceOptions, "BOOL, ");
break;
case SANE_TYPE_INT:
PL_strcat(*aDeviceOptions, "INT, ");
break;
case SANE_TYPE_FIXED:
PL_strcat(*aDeviceOptions, "FIXED, ");
break;
case SANE_TYPE_STRING:
PL_strcat(*aDeviceOptions, "STRING, ");
break;
case SANE_TYPE_BUTTON:
PL_strcat(*aDeviceOptions, "BUTTON, ");
break;
case SANE_TYPE_GROUP:
PL_strcat(*aDeviceOptions, "GROUP, ");
break;
default:
PL_strcat(*aDeviceOptions, "UNKNOWN, ");
}
////////////////////////////////////////
// UNIT
switch (optdesc->unit) {
case SANE_UNIT_NONE:
PL_strcat(*aDeviceOptions, "NONE, ");
break;
case SANE_UNIT_PIXEL:
PL_strcat(*aDeviceOptions, "PIXEL, ");
break;
case SANE_UNIT_BIT:
PL_strcat(*aDeviceOptions, "BIT, ");
break;
case SANE_UNIT_MM:
PL_strcat(*aDeviceOptions, "MM, ");
break;
case SANE_UNIT_DPI:
PL_strcat(*aDeviceOptions, "PERCENT, ");
break;
case SANE_UNIT_MICROSECOND:
PL_strcat(*aDeviceOptions, "MICROSECOND, ");
break;
default:
PL_strcat(*aDeviceOptions, "UNKNOWN, ");
}
////////////////////////////////////////
// SIZE
char * size_option = (char *)PR_MALLOC(64);
if (!size_option)
return NS_ERROR_OUT_OF_MEMORY;
sprintf(size_option, "%i, ", optdesc->size);
PL_strcat(*aDeviceOptions, size_option);
PR_FREEIF(size_option);
////////////////////////////////////////
// ACTIVE?
if (SANE_OPTION_IS_ACTIVE(optdesc->cap))
PL_strcat(*aDeviceOptions, "ACTIVE, ");
else
PL_strcat(*aDeviceOptions, "UNACTIVE, ");
////////////////////////////////////////
// SW SETTABLE?
if (SANE_OPTION_IS_SETTABLE(optdesc->cap))
PL_strcat(*aDeviceOptions, "SETTABLE, ");
else
PL_strcat(*aDeviceOptions, "UNSETTABLE, ");
////////////////////////////////////////
// Contraints Union
switch (optdesc->constraint_type) {
case SANE_CONSTRAINT_RANGE:
range_string = (char *)PR_MALLOC(128);
if (!range_string)
return NS_ERROR_OUT_OF_MEMORY;
sprintf(range_string, "RANGE, %i, %i, %i; ",
optdesc->constraint.range->min,
optdesc->constraint.range->max,
optdesc->constraint.range->quant);
PL_strcat(*aDeviceOptions, range_string);
PR_FREEIF(range_string);
break;
case SANE_CONSTRAINT_WORD_LIST:
word_string = (char *) PR_MALLOC(64);
if (!word_string)
return NS_ERROR_OUT_OF_MEMORY;
PL_strcat(*aDeviceOptions, "WORD_LIST");
for (int word_index=1;
word_index <= optdesc->constraint.word_list[0];
word_index++) {
sprintf(word_string, ", %i", optdesc->constraint.word_list[i]);
PL_strcat(*aDeviceOptions, word_string);
}
PL_strcat(*aDeviceOptions, ";");
PR_FREEIF(word_string);
break;
case SANE_CONSTRAINT_STRING_LIST:
PL_strcat(*aDeviceOptions, "STRING_LIST");
for (int string_index=0;
optdesc->constraint.string_list[string_index];
string_index++) {
PL_strcat(*aDeviceOptions, ", ");
PL_strcat(*aDeviceOptions,
optdesc->constraint.string_list[string_index]);
}
PL_strcat(*aDeviceOptions, ";");
break;
default:
// no constraints
PL_strcat(*aDeviceOptions, "NONE;");
}
}
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetDeviceOptions(const char * aDeviceOptions)
{
#ifdef DEBUG
printf("nsSaneInstance::SetDeviceOptions()\n");
#endif
NS_ERROR("DeviceOptions is read-only!\n");
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetActiveDevice(char ** aActiveDevice)
{
#ifdef DEBUG
printf("nsSaneInstance::GetActiveDevice()\n");
#endif
NS_ASSERTION(aActiveDevice != NULL, "null device pointer");
if (!aActiveDevice)
return NS_ERROR_NULL_POINTER;
*aActiveDevice = nsnull;
if (!mSaneDevice)
return NS_OK;
*aActiveDevice = (char*) nsMemory::Alloc(PL_strlen(mSaneDevice) + 1);
NS_ENSURE_TRUE(*aActiveDevice, NS_ERROR_OUT_OF_MEMORY);
PL_strcpy(*aActiveDevice, mSaneDevice);
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetActiveDevice(const char * aActiveDevice)
{
#ifdef DEBUG
printf("nsSaneInstance::SetActiveDevice()\n");
#endif
NS_ASSERTION(aActiveDevice, "null active device pointer");
if (!aActiveDevice)
return NS_ERROR_NULL_POINTER;
PR_FREEIF (mSaneDevice);
mSaneDevice = PL_strdup(aActiveDevice);
if (mSaneOpen) {
sane_close(mSaneHandle);
sane_exit();
sane_init(0,0);
if (sane_open(mSaneDevice, &mSaneHandle) != SANE_STATUS_GOOD) {
mSaneOpen = SANE_FALSE;
NS_ERROR("Error trying to reopen sane device!\n");
return NS_ERROR_FAILURE;
}
} else {
if (sane_open(mSaneDevice, &mSaneHandle) != SANE_STATUS_GOOD) {
NS_ERROR("Error trying to open closed sane device!\n");
return NS_ERROR_FAILURE;
}
mSaneOpen = SANE_TRUE;
}
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::OpenSaneDeviceIF( void )
{
#ifdef DEBUG
printf("nsSaneInstance::OpenSaneDeviceIF()\n");
#endif
if (mSaneOpen) {
return NS_OK;
}
if (!mSaneDevice) {
NS_ERROR("Device name not set!");
return NS_ERROR_FAILURE;
}
SANE_Status status = sane_open(mSaneDevice, &mSaneHandle);
if (status != SANE_STATUS_GOOD) {
NS_ERROR ("sane_open returned error code");
return NS_ERROR_FAILURE;
}
mSaneOpen = SANE_TRUE;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::GetImageParameters(char ** aImageParameters)
{
#ifdef DEBUG
printf("nsSaneInstance::GetImageParameters()\n");
#endif
SANE_Status status;
SANE_Parameters params;
nsresult rv;
NS_ASSERTION(aImageParameters != NULL, "null parameters pointer");
if (!aImageParameters)
return NS_ERROR_NULL_POINTER;
*aImageParameters = nsnull;
rv = OpenSaneDeviceIF();;
if (rv != NS_OK)
return rv;
status = sane_get_parameters(mSaneHandle, ¶ms);
if (status != SANE_STATUS_GOOD) {
sane_close(mSaneHandle);
NS_ERROR("Failed to get parameters for sane device!");
return NS_ERROR_FAILURE;
}
#define MAX_PARAM_LEN 256
*aImageParameters = (char *) nsMemory::Alloc(MAX_PARAM_LEN);
if (!*aImageParameters)
return NS_ERROR_OUT_OF_MEMORY;
char * format_string;
switch (params.format) {
case SANE_FRAME_GRAY:
format_string = PL_strdup("GRAY");
break;
case SANE_FRAME_RGB:
format_string = PL_strdup("RGB");
break;
case SANE_FRAME_RED:
format_string = PL_strdup("RED");
break;
case SANE_FRAME_GREEN:
format_string = PL_strdup("GREEN");
break;
case SANE_FRAME_BLUE:
format_string = PL_strdup("BLUE");
break;
default:
format_string = PL_strdup("UNKNOWN");
}
sprintf(*aImageParameters, "%s, %s, %i, %i, %i, %i",
format_string,
params.last_frame ? "TRUE" : "FALSE",
params.lines,
params.depth,
params.pixels_per_line,
params.bytes_per_line);
// cleanup
PR_FREEIF(format_string);
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetImageParameters(const char * aImageParameters)
{
#ifdef DEBUG
printf("nsSaneInstance::SetImageParameters()\n");
#endif
NS_ERROR("ImageParameters is read-only!\n");
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetAvailableDevices(char ** aAvailableDevices)
{
#ifdef DEBUG
printf("nsSaneInstance::GetAvailableDevices()\n");
#endif
SANE_Status status;
const SANE_Device ** device_list;
int num, i;
NS_ASSERTION(aAvailableDevices != NULL, "null parameters pointer");
if (!aAvailableDevices)
return NS_ERROR_NULL_POINTER;
*aAvailableDevices = nsnull;
status = sane_get_devices(&device_list, SANE_TRUE);
if (status != SANE_STATUS_GOOD)
return NS_ERROR_FAILURE;
// find out how many devices are available
for ( num=0; device_list[num]; num++);
// allocate return string
#define MAX_DEVICE_SIZE 256
*aAvailableDevices = (char*) nsMemory::Alloc(num * MAX_DEVICE_SIZE);
if (!*aAvailableDevices)
return NS_ERROR_OUT_OF_MEMORY;
for ( i=0; i < num * MAX_DEVICE_SIZE; i++)
(*aAvailableDevices)[i] = '\0';
for ( i=0; i<num; i++) {
optioncat(aAvailableDevices, device_list[i]->name, ", ");
optioncat(aAvailableDevices, device_list[i]->vendor, ", ");
optioncat(aAvailableDevices, device_list[i]->model, ", ");
optioncat(aAvailableDevices, device_list[i]->type, "; ");
}
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetAvailableDevices(const char * aAvailableDevices)
{
#ifdef DEBUG
printf("nsSaneInstance::SetAvailableDevices()\n");
#endif
NS_ERROR("AvailableDevices is read-only!\n");
return NS_ERROR_NOT_AVAILABLE;
}
NS_IMETHODIMP
nsSanePluginInstance::ScanImage(void)
{
#ifdef DEBUG
printf("nsSaneInstance::ScanImage()\n");
#endif
mState = 1;
mSuccess = 0;
mScanThread = PR_CreateThread( PR_USER_THREAD, scanimage_thread_routine,
(void*)this, PR_PRIORITY_HIGH,
PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0);
if (mScanThread == NULL) {
NS_ERROR("Could not create thread!");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetOption(const char * name, const char * value)
{
#ifdef DEBUG
printf("nsSaneInstance::SetOption()\n");
#endif
nsresult rv;
SANE_Int optnum;
const SANE_Option_Descriptor * optdesc;
void * option_value;
SANE_Status status;
NS_ASSERTION(name != NULL && value != NULL, "null pointer");
if (!name || !value)
return NS_ERROR_NULL_POINTER;
rv = OpenSaneDeviceIF();
if (rv != NS_OK)
return rv;
// find the option number
for ( optnum=1;
((optdesc = sane_get_option_descriptor(mSaneHandle, optnum)) != NULL)
&& (PL_strcasecmp(name, optdesc->name) != 0);
optnum++ );
if (!optdesc) {
NS_ERROR("Option not found for sane device!");
return NS_ERROR_INVALID_ARG;
}
// determine the value to pass to device
if (optdesc->type == SANE_TYPE_BOOL) {
SANE_Bool sbool;
if (PL_strcasecmp(value, "TRUE") == 0) {
sbool = SANE_TRUE;
} else
sbool = SANE_FALSE;
option_value = (void *) &sbool;
} else if ( optdesc->type == SANE_TYPE_INT ||
optdesc->type == SANE_TYPE_FIXED ) {
SANE_Int sint;
sscanf(value, "%i", &sint);
option_value = (void *) &sint;
} else if (optdesc->type == SANE_TYPE_STRING) {
option_value = (void *) value;
} else {
option_value = NULL;
}
// set the option
status = sane_control_option( mSaneHandle, optnum,
SANE_ACTION_SET_VALUE, option_value,
NULL);
if (status != SANE_STATUS_GOOD) {
NS_ERROR("Error trying to set option!");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
int
nsSanePluginInstance::WritePNMHeader (int fd, SANE_Frame format,
int width, int height, int depth)
{
#ifdef DEBUG
printf("nsSaneInstance::WritePNMHeader()\n");
#endif
char b[32];
switch (format) {
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
case SANE_FRAME_RGB:
sprintf (b, "P6\n%d %d\n255\n", width, height);
break;
default:
if (depth == 1)
sprintf (b, "P4\n%d %d\n", width, height);
else
sprintf (b, "P5\n%d %d\n255\n", width, height);
break;
}
return write(fd, b, PL_strlen(b));
}
// END of Generic SANE Interface
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// Scanner Status Interface
NS_IMETHODIMP
nsSanePluginInstance::GetSuccess(PRBool * aSuccess)
{
#ifdef DEBUG
printf("nsSaneInstance::GetSuccess()\n");
#endif
NS_PRECONDITION(aSuccess != nsnull, "null ptr");
if (! aSuccess) {
NS_ERROR("Null pointer passed to GetSuccess!\n");
return NS_ERROR_NULL_POINTER;
}
*aSuccess = mSuccess;
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetSuccess(PRBool aSuccess)
{
#ifdef DEBUG
printf("nsSaneInstance::SetSuccess()\n");
#endif
// This is a read-only flag.
NS_ERROR("Success is a read-only flag!\n");
return NS_ERROR_FAILURE;
}
NS_IMETHODIMP
nsSanePluginInstance::GetState(char ** aState)
{
#ifdef DEBUG
printf("nsSaneInstance::GetState()\n");
#endif
NS_PRECONDITION(aState != nsnull, "null ptr");
if (! aState) {
NS_ERROR("Null pointer passed to GetSuccess!\n");
return NS_ERROR_NULL_POINTER;
}
*aState = (char*) nsMemory::Alloc(5);
if (!*aState) {
NS_ERROR("Unable to allocate State string!\n");
return NS_ERROR_OUT_OF_MEMORY;
}
if (mState == 0)
PL_strcpy(*aState, "IDLE");
else
PL_strcpy(*aState, "BUSY");
return NS_OK;
}
NS_IMETHODIMP
nsSanePluginInstance::SetState(const char * aState)
{
#ifdef DEBUG
printf("nsSaneInstance::SetState()\n");
#endif
// This is a read-only flag.
NS_ERROR("Success is a read-only flag!\n");
return NS_ERROR_FAILURE;
}
// END of Scanner Status Interface
//////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
// snPluginStreamListener Methods
nsSanePluginStreamListener::nsSanePluginStreamListener(nsSanePluginInstance* inst)
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::nsSanePluginStreamListener()\n");
#endif
PR_AtomicIncrement(&gPluginObjectCount);
mPlugInst = inst;
}
nsSanePluginStreamListener::~nsSanePluginStreamListener(void)
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::~nsSanePluginStreamListener()\n");
#endif
PR_AtomicDecrement(&gPluginObjectCount);
}
// This macro produces a simple version of QueryInterface, AddRef and Release.
// See the nsISupports.h header file for details.
NS_IMPL_ISUPPORTS1(nsSanePluginStreamListener, nsIPluginStreamListener)
NS_METHOD
nsSanePluginStreamListener::OnStartBinding( nsIPluginStreamInfo* pluginInfo )
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::OnStartBinding()\n");
#endif
return NS_OK;
}
NS_METHOD
nsSanePluginStreamListener::OnDataAvailable( nsIPluginStreamInfo* pluginInfo,
nsIInputStream* input,
PRUint32 length )
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::OnDataAvailable()\n");
#endif
// This plugin doesn't support
// streaming input data.
return NS_OK;
}
NS_METHOD
nsSanePluginStreamListener::OnFileAvailable( nsIPluginStreamInfo* pluginInfo,
const char* fileName )
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::OnFileAvailable()\n");
#endif
return NS_OK;
}
NS_METHOD
nsSanePluginStreamListener::OnStopBinding( nsIPluginStreamInfo* pluginInfo,
nsresult status )
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::OnStopBinding()\n");
#endif
return NS_OK;
}
NS_METHOD
nsSanePluginStreamListener::OnNotify( const char* url, nsresult status )
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::OnNotify()\n");
#endif
return NS_OK;
}
NS_METHOD
nsSanePluginStreamListener::GetStreamType(nsPluginStreamType *result)
{
#ifdef DEBUG
printf("nsSanePluginStreamListener::GetStreamType()\n");
#endif
*result = nsPluginStreamType_Normal;
return NS_OK;
}
// End snPluginStreamListener methods
//////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
// Helper Global Fuctions
gboolean draw (GtkWidget *widget, GdkEventExpose *event, gpointer data) {
nsSanePluginInstance * pthis = (nsSanePluginInstance *)data;
pthis->PaintImage();
return TRUE;
}
nsresult optioncat ( char ** dest, const char * src, const char * ending )
{
char * p;
int i;
NS_ASSERTION(dest != NULL, "null option string pointer");
if (!dest)
return NS_ERROR_NULL_POINTER;
NS_ASSERTION(src != NULL, "null option string pointer");
if (!src)
return NS_ERROR_NULL_POINTER;
NS_ASSERTION(ending != NULL, "null option string pointer");
if (!ending)
return NS_ERROR_NULL_POINTER;
// ensure that src string doesn't contain any ','s or ';'s
p = PL_strdup(src);
for ( i=0; p[i]; i++ ) {
if (p[i] == ',')
p[i] = '~'; // Replace ','s with '~'s
else if (p[i] == ';')
p[i] = '|'; // Replace ';'s with '|'s
}
PL_strcat(*dest, p);
PL_strcat(*dest, ending);
PR_FREEIF(p);
return NS_OK;
}
void
my_jpeg_error_exit (j_common_ptr cinfo)
{
emptr errmgr;
errmgr = (emptr) cinfo->err;
cinfo->err->output_message(cinfo);
siglongjmp(errmgr->setjmp_buffer, 1);
return;
}
unsigned char *
jpeg_file_to_rgb (FILE * f, int *w, int *h)
{
struct jpeg_decompress_struct cinfo;
struct my_JPEG_error_mgr jerr;
unsigned char *data, *line[16], *ptr;
int x, y, i;
cinfo.err = jpeg_std_error(&(jerr.pub));
jerr.pub.error_exit = my_jpeg_error_exit;
/* error handler to longjmp to, we want to preserve signals */
if (sigsetjmp(jerr.setjmp_buffer, 1)) {
/* Whoops there was a jpeg error */
jpeg_destroy_decompress(&cinfo);
return NULL;
}
jpeg_create_decompress(&cinfo);
jpeg_stdio_src(&cinfo, f);
jpeg_read_header(&cinfo, TRUE);
cinfo.do_fancy_upsampling = FALSE;
cinfo.do_block_smoothing = FALSE;
cinfo.scale_num = 2;
cinfo.scale_denom = 1;
jpeg_start_decompress(&cinfo);
*w = cinfo.output_width;
*h = cinfo.output_height;
data = (unsigned char *)PR_Malloc(*w ** h * 3);
if (!data) {
NS_ERROR("jpeg_file_to_rgb: Error trying to allocate buffer!\n");
jpeg_destroy_decompress(&cinfo);
return NULL;
}
ptr = data;
if (cinfo.rec_outbuf_height > 16) {
NS_ERROR("ERROR: JPEG uses line buffers > 16. Cannot load.\n");
return NULL;
}
if (cinfo.output_components == 3) {
for (y = 0; y < *h; y += cinfo.rec_outbuf_height) {
for (i = 0; i < cinfo.rec_outbuf_height; i++) {
line[i] = ptr;
ptr += *w * 3;
}
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
}
} else if (cinfo.output_components == 1) {
for (i = 0; i < cinfo.rec_outbuf_height; i++) {
if ((line[i] = (unsigned char *)PR_Malloc(*w)) == NULL) {
int t = 0;
NS_ERROR("jpeg_file_to_rgb: Error tying to allocate line!\n");
for (t = 0; t < i; t++)
PR_FREEIF(line[t]);
jpeg_destroy_decompress(&cinfo);
return NULL;
}
}
for (y = 0; y < *h; y += cinfo.rec_outbuf_height) {
jpeg_read_scanlines(&cinfo, line, cinfo.rec_outbuf_height);
for (i = 0; i < cinfo.rec_outbuf_height; i++) {
for (x = 0; x < *w; x++) {
*ptr++ = line[i][x];
*ptr++ = line[i][x];
*ptr++ = line[i][x];
}
}
}
for (i = 0; i < cinfo.rec_outbuf_height; i++)
PR_FREEIF(line[i]);
}
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
return data;
}
// Caller must free memory
unsigned char *
scale_image(unsigned char *rgb_data, int rgb_width, int rgb_height,
int w, int h)
{
int x, y, *xarray;
unsigned char **yarray, *ptr, *ptr2, *ptr22, *new_data;
int l, r, m, pos, inc, w3;
new_data = (unsigned char *) PR_Malloc( w * h * 3);
if (!new_data) {
NS_ERROR("ERROR: Cannot allocate image buffer\n");
return NULL;
}
xarray = (int *)PR_Malloc(sizeof(int) * w);
if (!xarray) {
NS_ERROR("ERROR: Cannot allocate X co-ord buffer\n");
PR_FREEIF(new_data);
return NULL;
}
yarray = (unsigned char **)PR_Malloc(sizeof(unsigned char *) * h);
if (!yarray) {
NS_ERROR("ERROR: Cannot allocate Y co-ord buffer\n");
PR_FREEIF(new_data);
PR_FREEIF(xarray);
return NULL;
}
ptr22 = rgb_data;
w3 = rgb_width * 3;
inc = 0;
l = 0;
r = 0;
m = w - l - r;
inc = (rgb_width << 16) / m;
pos = 0;
for (x = l; x < l + m; x++) {
xarray[x] = (pos >> 16) + (pos >> 16) + (pos >> 16);
pos += inc;
}
pos = (rgb_width - r) << 16;
for (x = w - r; x < w; x++) {
xarray[x] = (pos >> 16) + (pos >> 16) + (pos >> 16);
pos += 0x10000;
}
l = 0;
r = 0;
m = h - l - r;
if (m > 0)
inc = (rgb_height << 16) / m;
pos = 0;
for (x = l; x < l + m; x++) {
yarray[x] = ptr22 + ((pos >> 16) * w3);
pos += inc;
}
pos = (rgb_height - r) << 16;
for (x = h - r; x < h; x++) {
yarray[x] = ptr22 + ((pos >> 16) * w3);
pos += 0x10000;
}
ptr = new_data;
for (y = 0; y < h; y++) {
for (x = 0; x < w; x++) {
ptr2 = yarray[y] + xarray[x];
*ptr++ = (int)*ptr2++;
*ptr++ = (int)*ptr2++;
*ptr++ = (int)*ptr2;
}
}
PR_FREEIF(xarray);
PR_FREEIF(yarray);
return new_data;
}
// Caller must free memory
unsigned char *
crop_image(unsigned char *rgb_data, int *rgb_width, int *rgb_height,
gint x, gint y, gint w, gint h)
{
unsigned char *data;
int xx, yy, w3, w4;
unsigned char *ptr1, *ptr2;
if (!rgb_data)
return NULL;
if (x < 0)
{
w += x;
x = 0;
}
if (y < 0)
{
h += y;
y = 0;
}
if (x >= *rgb_width)
return NULL;
if (y >= *rgb_height)
return NULL;
if (w <= 0)
return NULL;
if (h <= 0)
return NULL;
if (x + w > *rgb_width)
w = *rgb_width - x;
if (y + h > *rgb_height)
h = *rgb_height - y;
if (w <= 0)
return NULL;
if (h <= 0)
return NULL;
w3 = *rgb_width * 3;
w4 = (*rgb_width - w) * 3;
data = (unsigned char *)PR_Malloc(w * h * 3);
if (data == NULL)
return NULL;
ptr1 = rgb_data + (y * w3) + (x * 3);
ptr2 = data;
for (yy = 0; yy < h; yy++)
{
for (xx = 0; xx < w; xx++)
{
*ptr2++ = *ptr1++;
*ptr2++ = *ptr1++;
*ptr2++ = *ptr1++;
}
ptr1 += w4;
}
*rgb_width = w;
*rgb_height = h;
return data;
}
void PR_CALLBACK scanimage_thread_routine( void * arg )
{
nsSanePluginInstance * pthis = (nsSanePluginInstance *)arg;
nsresult rv;
SANE_Status status;
SANE_Parameters param;
int len;
FILE * out;
SANE_Byte * buf;
JSAMPROW row_pointer[1];
char *fname;
// JPEG stuff
struct jpeg_compress_struct cinfo;
struct jpeg_error_mgr jerr;
// Error handling
jerr.error_exit = my_jpeg_error_exit; // fatal errors
cinfo.err = jpeg_std_error(&jerr);
jpeg_create_compress(&cinfo);
rv = pthis->OpenSaneDeviceIF();
if (rv != NS_OK)
goto error_exit;
// open image file
fname = pthis->GetImageFilename();
if (!fname)
goto error_exit;
out = fopen(fname, "wb");
PR_FREEIF(fname);
if (out == NULL) {
NS_ERROR("Unable to open mImageFilename!\n");
sane_cancel(pthis->mSaneHandle);
goto error_exit;
}
jpeg_stdio_dest(&cinfo, out);
status = sane_start(pthis->mSaneHandle);
if (status != SANE_STATUS_GOOD) {
NS_ERROR("Error trying to start sane device!");
goto error_exit;
}
status = sane_get_parameters(pthis->mSaneHandle, ¶m);
if (status != SANE_STATUS_GOOD) {
NS_ERROR("Error trying to get image parameters!\n");
sane_cancel(pthis->mSaneHandle);
goto error_exit;
}
cinfo.image_width = param.pixels_per_line;
cinfo.image_height = param.lines;
switch (param.format) {
case SANE_FRAME_RED:
case SANE_FRAME_GREEN:
case SANE_FRAME_BLUE:
// NOT Supported!
NS_ERROR("Multiframe devices not supported!\n");
sane_cancel(pthis->mSaneHandle);
goto error_exit;
break;
case SANE_FRAME_RGB:
cinfo.input_components = 3;
cinfo.in_color_space = JCS_RGB;
break;
case SANE_FRAME_GRAY:
cinfo.input_components = 1;
cinfo.in_color_space = JCS_GRAYSCALE;
break;
}
jpeg_set_defaults(&cinfo);
cinfo.dct_method = pthis->mCompMethod;
jpeg_set_quality(&cinfo, pthis->mCompQuality, FALSE);
jpeg_start_compress(&cinfo, TRUE);
buf = (SANE_Byte *)PR_MALLOC(param.bytes_per_line);
if (!buf) {
NS_ERROR("Error trying to allocate buffer!\n");
jpeg_destroy_compress(&cinfo);
fclose(out);
sane_cancel(pthis->mSaneHandle);
goto error_exit;
}
int total;
while(1) {
total = 0;
while (total < param.bytes_per_line) {
// Read in data from sane device
status = sane_read(pthis->mSaneHandle, buf + total,
param.bytes_per_line - total,
&len);
if (status != SANE_STATUS_GOOD) {
if (status == SANE_STATUS_EOF)
// done with this frame
goto finished_scan;
else {
NS_ERROR("Error trying to read from sane device!\n");
PR_FREEIF(buf);
jpeg_destroy_compress(&cinfo);
fclose(out);
sane_cancel(pthis->mSaneHandle);
goto error_exit;
}
}
total += len;
}
row_pointer[0] = (JSAMPLE *) buf;
jpeg_write_scanlines(&cinfo, row_pointer, 1);
} // end while
// cleanup
finished_scan:
jpeg_finish_compress(&cinfo);
jpeg_destroy_compress(&cinfo);
PR_FREEIF(buf);
sane_cancel(pthis->mSaneHandle);
fclose(out);
pthis->SetSuccess(PR_TRUE); // allow caller to check for success
error_exit:
////////////////////////////////////////////////
// Call onScanComplete callback in the UI thread
nsCOMPtr<nsIEventQueue> eventQ;
// Get the event queue of the current thread...
nsCOMPtr<nsIEventQueueService> eventQService =
do_GetService(kEventQueueService, &rv);
if (NS_FAILED(rv)) {
NS_ERROR("Unable to get event queue service!\n");
return;
}
rv = eventQService->GetThreadEventQueue(pthis->mUIThread,
getter_AddRefs(eventQ));
if (NS_FAILED(rv)) {
NS_ERROR("Unable to get thread queue!\n");
return;
}
PLEvent *event = new PLEvent;
if (!event) {
NS_ERROR("Unable to create new event!\n");
return;
}
// ThreadHandleEvent will now execute in the UI thread.
PL_InitEvent(event,
pthis,
(PLHandleEventProc) ThreadedHandleEvent,
(PLDestroyEventProc) ThreadedDestroyEvent);
if (NS_FAILED(eventQ->PostEvent(event))) {
NS_ERROR("Error trying to post event!\n");
PL_DestroyEvent(event);
return;
}
}
void PR_CALLBACK ThreadedHandleEvent(PLEvent * event)
{
nsSanePluginInstance *pthis = (nsSanePluginInstance *)event->owner;
// If this isn't the UI's thread then we are hosed!
if (PR_FALSE == pthis->IsUIThread()) {
NS_ERROR("Attempt to execute event from the wrong thread!\n");
return;
}
pthis->SetState(0); // no longer scanning
pthis->Restore(); // repaint image
// Notify user that routine is finished
pthis->DoScanCompleteCallback();
}
void PR_CALLBACK ThreadedDestroyEvent(PLEvent* aEvent)
{
delete aEvent;
}
void store_filename(GtkFileSelection *selector, gpointer user_data)
{
nsSanePluginInstance *pthis = (nsSanePluginInstance *) user_data;
gchar *cmd;
GtkWidget * fs;
char * orig_filename;
char * dest_filename;
if (!user_data || !selector)
return;
fs = pthis->GetFileSelection();
orig_filename = pthis->GetImageFilename();
if ( !fs || !orig_filename )
return;
dest_filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION(fs));
if ( !dest_filename )
return;
#ifdef DEBUG
printf("Saving %s to %s\n", orig_filename, dest_filename);
#endif
/*
* I'm just doing a simple 'cp' command with the shell.
* This probobly should be done in a NSPR safe kind
* of way.
*/
// cp /tmp/nsSanePlugin.###.jpg /path/to/save/to.jpg
cmd = (gchar *)PR_Malloc(PL_strlen(orig_filename) +
PL_strlen(dest_filename) + 6);
if (!cmd)
return;
sprintf(cmd, "cp %s %s", orig_filename, dest_filename);
system(cmd);
PR_FREEIF(cmd);
}
// END of Helper Global Functions
//////////////////////////////////////////////////////////////////////////////
|