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
|
// Copyright (C) 2004-2024 Artifex Software, Inc.
//
// This file is part of MuPDF.
//
// MuPDF is free software: you can redistribute it and/or modify it under the
// terms of the GNU Affero General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
// details.
//
// You should have received a copy of the GNU Affero General Public License
// along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
//
// Alternative licensing terms are available from the licensor.
// For commercial licensing, see <https://www.artifex.com/> or contact
// Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
// CA 94129, USA, for further information.
#include "mupdf/fitz.h"
#include "draw-imp.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#undef DEBUG_SCAN_CONVERTER
/* Define ourselves a 'fixed' type for clarity */
typedef int fixed;
#define fixed_shift 8
#define float2fixed(x) ((int)((x)*(1<<fixed_shift)))
#define fixed2int(x) ((int)((x)>>fixed_shift))
#define fixed_half (1<<(fixed_shift-1))
#define fixed_1 (1<<fixed_shift)
#define int2fixed(x) ((x)<<fixed_shift)
enum
{
DIRN_UNSET = -1,
DIRN_UP = 0,
DIRN_DOWN = 1
};
typedef struct
{
fixed left;
fixed right;
fixed y;
signed char d; /* 0 up (or horiz), 1 down, -1 uninited */
/* unset == 1, iff the values in the above are unset */
unsigned char unset;
/* can_save == 1, iff we are eligible to 'save'. i.e. if we
* have not yet output a cursor, and have not detected
* any line segments completely out of range. */
unsigned char can_save;
unsigned char saved;
fixed save_left;
fixed save_right;
int save_iy;
int save_d;
}
cursor_t;
typedef struct fz_edgebuffer_s
{
fz_rasterizer super;
int app;
int sorted;
int n;
int index_cap;
int *index;
int table_cap;
int *table;
/* cursor section, for use with any part of pixel mode */
cursor_t cursor[3];
} fz_edgebuffer;
static fz_rasterizer_insert_fn fz_insert_edgebuffer_app;
static fz_rasterizer_insert_fn fz_insert_edgebuffer;
#ifdef DEBUG_SCAN_CONVERTER
int debugging_scan_converter = 1;
static void
fz_edgebuffer_print(fz_context *ctx, fz_output *out, fz_edgebuffer * edgebuffer)
{
int i;
int height = edgebuffer->super.clip.y1 - edgebuffer->super.clip.y0;
fz_write_printf(ctx, out, "Edgebuffer %x\n", edgebuffer);
fz_write_printf(ctx, out, "xmin=%x xmax=%x base=%x height=%x\n",
edgebuffer->super.clip.x0, edgebuffer->super.clip.x1, edgebuffer->super.clip.y0, height);
for (i=0; i < height; i++) {
int offset = edgebuffer->index[i];
int *row = &edgebuffer->table[offset];
int count = *row++;
assert ((count & 1) == 0);
fz_write_printf(ctx, out, "%x @ %x: %d =", i, offset, count);
while (count-- > 0) {
int v = *row++;
fz_write_printf(ctx, out, " %x:%d", v&~1, v&1);
}
fz_write_printf(ctx, out, "\n");
}
}
static void
fz_edgebuffer_print_app(fz_context *ctx, fz_output *out, fz_edgebuffer * edgebuffer)
{
int i;
int height = edgebuffer->super.clip.y1 - edgebuffer->super.clip.y0;
fz_write_printf(ctx, out, "Edgebuffer %x\n", edgebuffer);
fz_write_printf(ctx, out, "xmin=%x xmax=%x base=%x height=%x\n",
edgebuffer->super.clip.x0, edgebuffer->super.clip.x1, edgebuffer->super.clip.y0, height);
if (edgebuffer->table == NULL)
return;
for (i=0; i < height; i++) {
int offset = edgebuffer->index[i];
int *row = &edgebuffer->table[offset];
int count = *row++;
int count0 = count;
fz_write_printf(ctx, out, "%x @ %x: %d =", i, offset, count);
while (count-- > 0) {
int l = *row++;
int r = *row++;
fz_write_printf(ctx, out, " %x:%x", l, r);
}
assert((count0 & 1) == 0); (void)count0;
fz_write_printf(ctx, out, "\n");
}
}
#endif
static void fz_drop_edgebuffer(fz_context *ctx, fz_rasterizer *r)
{
fz_edgebuffer *eb = (fz_edgebuffer *)r;
if (eb)
{
fz_free(ctx, eb->index);
fz_free(ctx, eb->table);
}
fz_free(ctx, eb);
}
static void index_edgebuffer_insert(fz_context *ctx, fz_rasterizer *ras, float fsx, float fsy, float fex, float fey, int rev)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
int iminy, imaxy;
int height = eb->super.clip.y1 - eb->super.clip.y0;
if (fsy == fey)
return;
if (fsx < fex)
{
if (fsx < eb->super.bbox.x0) eb->super.bbox.x0 = fsx;
if (fex > eb->super.bbox.x1) eb->super.bbox.x1 = fex;
}
else
{
if (fsx > eb->super.bbox.x1) eb->super.bbox.x1 = fsx;
if (fex < eb->super.bbox.x0) eb->super.bbox.x0 = fex;
}
if (fsy < fey)
{
if (fsy < eb->super.bbox.y0) eb->super.bbox.y0 = fsy;
if (fey > eb->super.bbox.y1) eb->super.bbox.y1 = fey;
}
else
{
if (fey < eb->super.bbox.y0) eb->super.bbox.y0 = fey;
if (fsy > eb->super.bbox.y1) eb->super.bbox.y1 = fsy;
}
/* To strictly match, this should be:
* iminy = int2fixed(float2fixed(fsy))
* imaxy = int2fixed(float2fixed(fsx))
* but this is faster. It can round differently,
* (on some machines at least) hence the iminy--; below.
*/
iminy = (int)fsy;
imaxy = (int)fey;
if (iminy > imaxy)
{
int t;
t = iminy; iminy = imaxy; imaxy = t;
}
imaxy++;
iminy--;
imaxy -= eb->super.clip.y0;
if (imaxy < 0)
return;
iminy -= eb->super.clip.y0;
if (iminy < 0)
iminy = 0;
else if (iminy > height)
return;
if (imaxy > height-1)
imaxy = height-1;
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
fprintf(stderr, "%x->%x:%d\n", iminy, imaxy, eb->n);
#endif
eb->index[iminy] += eb->n;
eb->index[imaxy+1] -= eb->n;
}
static void fz_postindex_edgebuffer(fz_context *ctx, fz_rasterizer *r)
{
fz_edgebuffer *eb = (fz_edgebuffer *)r;
int height = eb->super.clip.y1 - eb->super.clip.y0 + 1;
int n = eb->n;
int total = 0;
int delta = 0;
int i;
eb->super.fns.insert = (eb->app ? fz_insert_edgebuffer_app : fz_insert_edgebuffer);
for (i = 0; i < height; i++)
{
delta += eb->index[i];
eb->index[i] = total;
total += 1 + delta*n;
}
assert(delta == 0);
if (eb->table_cap < total)
{
eb->table = fz_realloc_array(ctx, eb->table, total, int);
eb->table_cap = total;
}
for (i = 0; i < height; i++)
{
eb->table[eb->index[i]] = 0;
}
}
static int fz_reset_edgebuffer(fz_context *ctx, fz_rasterizer *r)
{
fz_edgebuffer *eb = (fz_edgebuffer *)r;
int height = eb->super.clip.y1 - eb->super.clip.y0 + 1;
int n;
eb->sorted = 0;
if (eb->index_cap < height)
{
eb->index = fz_realloc_array(ctx, eb->index, height, int);
eb->index_cap = height;
}
memset(eb->index, 0, sizeof(int) * height);
n = 1;
if (eb->app)
{
n = 2;
eb->cursor[0].saved = 0;
eb->cursor[0].unset = 1;
eb->cursor[0].can_save = 1;
eb->cursor[0].d = DIRN_UNSET;
eb->cursor[1].saved = 0;
eb->cursor[1].unset = 1;
eb->cursor[1].can_save = 1;
eb->cursor[1].d = DIRN_UNSET;
eb->cursor[2].saved = 0;
eb->cursor[2].unset = 1;
eb->cursor[2].can_save = 1;
eb->cursor[2].d = DIRN_UNSET;
}
eb->n = n;
eb->super.fns.insert = index_edgebuffer_insert;
return 1;
}
static void mark_line(fz_context *ctx, fz_edgebuffer *eb, fixed sx, fixed sy, fixed ex, fixed ey)
{
int base_y = eb->super.clip.y0;
int height = eb->super.clip.y1 - eb->super.clip.y0;
int *table = eb->table;
int *index = eb->index;
int delta;
int iy, ih;
fixed clip_sy, clip_ey;
int dirn = DIRN_UP;
int *row;
if (fixed2int(sy + fixed_half-1) == fixed2int(ey + fixed_half-1))
return;
if (sy > ey) {
int t;
t = sy; sy = ey; ey = t;
t = sx; sx = ex; ex = t;
dirn = DIRN_DOWN;
}
if (fixed2int(sx) < eb->super.bbox.x0)
eb->super.bbox.x0 = fixed2int(sx);
if (fixed2int(sx + fixed_1 - 1) > eb->super.bbox.x1)
eb->super.bbox.x1 = fixed2int(sx + fixed_1 - 1);
if (fixed2int(ex) < eb->super.bbox.x0)
eb->super.bbox.x0 = fixed2int(ex);
if (fixed2int(ex + fixed_1 - 1) > eb->super.bbox.x1)
eb->super.bbox.x1 = fixed2int(ex + fixed_1 - 1);
if (fixed2int(sy) < eb->super.bbox.y0)
eb->super.bbox.y0 = fixed2int(sy);
if (fixed2int(ey + fixed_1 - 1) > eb->super.bbox.y1)
eb->super.bbox.y1 = fixed2int(ey + fixed_1 - 1);
/* Lines go from sy to ey, closed at the start, open at the end. */
/* We clip them to a region to make them closed at both ends. */
/* Thus the unset scanline marked (>= sy) is: */
clip_sy = ((sy + fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
/* The last scanline marked (< ey) is: */
clip_ey = ((ey - fixed_half - 1) & ~(fixed_1-1)) | fixed_half;
/* Now allow for banding */
if (clip_sy < int2fixed(base_y) + fixed_half)
clip_sy = int2fixed(base_y) + fixed_half;
if (ey <= clip_sy)
return;
if (clip_ey > int2fixed(base_y + height - 1) + fixed_half)
clip_ey = int2fixed(base_y + height - 1) + fixed_half;
if (sy > clip_ey)
return;
delta = clip_sy - sy;
if (delta > 0)
{
int dx = ex - sx;
int dy = ey - sy;
int advance = (int)(((int64_t)dx * delta + (dy>>1)) / dy);
sx += advance;
sy += delta;
}
ex -= sx;
ey -= sy;
clip_ey -= clip_sy;
delta = ey - clip_ey;
if (delta > 0)
{
int advance = (int)(((int64_t)ex * delta + (ey>>1)) / ey);
ex -= advance;
ey -= delta;
}
ih = fixed2int(ey);
assert(ih >= 0);
iy = fixed2int(sy) - base_y;
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
fz_write_printf(ctx, fz_stderr(ctx), " iy=%x ih=%x\n", iy, ih);
#endif
assert(iy >= 0 && iy < height);
/* We always cross at least one scanline */
row = &table[index[iy]];
*row = (*row)+1; /* Increment the count */
row[*row] = (sx&~1) | dirn;
if (ih == 0)
return;
if (ex >= 0) {
int x_inc, n_inc, f;
/* We want to change sx by ex in ih steps. So each step, we add
* ex/ih to sx. That's x_inc + n_inc/ih.
*/
x_inc = ex/ih;
n_inc = ex-(x_inc*ih);
f = ih>>1;
delta = ih;
do {
int count;
iy++;
sx += x_inc;
f -= n_inc;
if (f < 0) {
f += ih;
sx++;
}
assert(iy >= 0 && iy < height);
row = &table[index[iy]];
count = *row = (*row)+1; /* Increment the count */
row[count] = (sx&~1) | dirn;
} while (--delta);
} else {
int x_dec, n_dec, f;
ex = -ex;
/* We want to change sx by ex in ih steps. So each step, we subtract
* ex/ih from sx. That's x_dec + n_dec/ih.
*/
x_dec = ex/ih;
n_dec = ex-(x_dec*ih);
f = ih>>1;
delta = ih;
do {
int count;
iy++;
sx -= x_dec;
f -= n_dec;
if (f < 0) {
f += ih;
sx--;
}
assert(iy >= 0 && iy < height);
row = &table[index[iy]];
count = *row = (*row)+1; /* Increment the count */
row[count] = (sx&~1) | dirn;
} while (--delta);
}
}
/* Allow for floats that are too large to safely convert to fixeds (ints),
* by just clipping them at the end. */
static fixed
safe_float2fixed(float f)
{
if (f < (float)-0x00800000)
return INT_MIN;
else if (f >= (float)0x00800000)
return INT_MAX;
else
return float2fixed(f);
}
static void fz_insert_edgebuffer(fz_context *ctx, fz_rasterizer *ras, float fsx, float fsy, float fex, float fey, int rev)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
fixed sx = safe_float2fixed(fsx);
fixed sy = safe_float2fixed(fsy);
fixed ex = safe_float2fixed(fex);
fixed ey = safe_float2fixed(fey);
mark_line(ctx, eb, sx, sy, ex, ey);
}
static inline void
cursor_output(fz_edgebuffer * FZ_RESTRICT eb, int rev, int iy)
{
int *row;
int count;
int height = eb->super.clip.y1 - eb->super.clip.y0;
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
rev &= 1; /* Edge label 0 is forwards, 1 and 2 are reverse */
if (iy >= 0 && iy < height) {
if (cr->can_save) {
/* Save it for later in case we join up */
cr->save_left = cr->left;
cr->save_right = cr->right;
cr->save_iy = iy;
cr->save_d = cr->d;
cr->saved = 1;
} else {
/* Enter it into the table */
row = &eb->table[eb->index[iy]];
if (cr->d == DIRN_UNSET)
{
/* Move 0 0; line 10 0; line 0 0; */
/* FIXME */
}
else
{
*row = count = (*row)+1; /* Increment the count */
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
fprintf(stderr, "row: %x: %x->%x %c\n", iy, cr->left, cr->right, (cr->d^rev) == DIRN_UP ? '^' : (cr->d^rev) == DIRN_DOWN ? 'v' : '-');
#endif
assert(count <= (eb->index[iy+1] - eb->index[iy] - 1)/2);
row[2 * count - 1] = (cr->left&~1) | (cr->d ^ rev);
row[2 * count] = cr->right;
}
}
}
cr->can_save = 0;
}
static inline void
cursor_output_inrange(fz_edgebuffer * FZ_RESTRICT eb, int rev, int iy)
{
int *row;
int count;
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
rev &= 1; /* Edge label 0 is forwards, 1 and 2 are reverse */
assert(iy >= 0 && iy < eb->super.clip.y1 - eb->super.clip.y0);
if (cr->can_save) {
/* Save it for later in case we join up */
cr->save_left = cr->left;
cr->save_right = cr->right;
cr->save_iy = iy;
cr->save_d = cr->d;
cr->saved = 1;
} else {
/* Enter it into the table */
assert(cr->d != DIRN_UNSET);
row = &eb->table[eb->index[iy]];
*row = count = (*row)+1; /* Increment the count */
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
printf("row= %x: %x->%x %c\n", iy, cr->left, cr->right, (cr->d^rev) == DIRN_UP ? '^' : (cr->d^rev) == DIRN_DOWN ? 'v' : '-');
#endif
row[2 * count - 1] = (cr->left&~1) | (cr->d ^ rev);
row[2 * count] = cr->right;
}
cr->can_save = 0;
}
/* Step the cursor in y, allowing for maybe crossing a scanline */
static inline void
cursor_step(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int new_iy;
int base = eb->super.clip.y0;
int iy = fixed2int(cr->y) - base;
cr->y += dy;
new_iy = fixed2int(cr->y) - base;
if (new_iy != iy) {
cursor_output(eb, rev, iy);
cr->left = x;
cr->right = x;
} else {
if (x < cr->left)
cr->left = x;
if (x > cr->right)
cr->right = x;
}
}
/* Step the cursor in y, never by enough to cross a scanline. */
static inline void
cursor_never_step_vertical(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
cr->y += dy;
}
/* Step the cursor in y, never by enough to cross a scanline,
* knowing that we are moving left, and that the right edge
* has already been accounted for. */
static inline void
cursor_never_step_left(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
if (x < cr->left)
cr->left = x;
cr->y += dy;
}
/* Step the cursor in y, never by enough to cross a scanline,
* knowing that we are moving right, and that the left edge
* has already been accounted for. */
static inline void
cursor_never_step_right(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
assert(fixed2int(cr->y+dy) == fixed2int(cr->y));
if (x > cr->right)
cr->right = x;
cr->y += dy;
}
/* Step the cursor in y, always by enough to cross a scanline. */
static inline void
cursor_always_step(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
int iy = fixed2int(cr->y) - base;
cursor_output(eb, rev, iy);
cr->y += dy;
cr->left = x;
cr->right = x;
}
/* Step the cursor in y, always by enough to cross a scanline, as
* part of a vertical line, knowing that we are moving from a
* position guaranteed to be in the valid y range. */
static inline void
cursor_always_step_inrange_vertical(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
int iy = fixed2int(cr->y) - base;
cursor_output(eb, rev, iy);
cr->y += dy;
}
/* Step the cursor in y, always by enough to cross a scanline, as
* part of a left moving line, knowing that we are moving from a
* position guaranteed to be in the valid y range. */
static inline void
cursor_always_inrange_step_left(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
int iy = fixed2int(cr->y) - base;
cr->y += dy;
cursor_output_inrange(eb, rev, iy);
cr->right = x;
}
/* Step the cursor in y, always by enough to cross a scanline, as
* part of a right moving line, knowing that we are moving from a
* position guaranteed to be in the valid y range. */
static inline void
cursor_always_inrange_step_right(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed dy, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
int iy = fixed2int(cr->y) - base;
cr->y += dy;
cursor_output_inrange(eb, rev, iy);
cr->left = x;
}
static inline void cursor_init(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed y, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
assert(y >= int2fixed(eb->super.clip.y0) && y <= int2fixed(eb->super.clip.y1));
cr->y = y;
cr->left = x;
cr->right = x;
cr->d = DIRN_UNSET;
}
static inline void cursor_left_merge(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
if (x < cr->left)
cr->left = x;
}
static inline void cursor_left(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
cr->left = x;
}
static inline void cursor_right_merge(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
if (x > cr->right)
cr->right = x;
}
static inline void cursor_right(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
cr->right = x;
}
static inline void cursor_down(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
if (cr->d == DIRN_UP)
{
cursor_output(eb, rev, fixed2int(cr->y) - base);
cr->left = x;
cr->right = x;
}
cr->d = DIRN_DOWN;
}
static inline void cursor_up(fz_edgebuffer * FZ_RESTRICT eb, int rev, fixed x)
{
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
int base = eb->super.clip.y0;
if (cr->d == DIRN_DOWN)
{
cursor_output(eb, rev, fixed2int(cr->y) - base);
cr->left = x;
cr->right = x;
}
cr->d = DIRN_UP;
}
static inline int dirns_match(int d0, int d1)
{
return d0 == d1 || d0 == DIRN_UNSET || d1 == DIRN_UNSET;
}
static inline int dirn_flip(int d)
{
return d < 0 ? d : d^1;
}
static inline int dirns_merge(int d0, int d1)
{
if (d0 == DIRN_UNSET)
return d1;
assert(dirns_match(d0, d1));
return d0;
}
static void
cursor_flush(fz_edgebuffer * FZ_RESTRICT eb)
{
int base = eb->super.clip.y0;
int iy0, iy1, iy2;
cursor_t * FZ_RESTRICT cr0 = &eb->cursor[0];
cursor_t * FZ_RESTRICT cr1 = &eb->cursor[1];
cursor_t * FZ_RESTRICT cr2 = &eb->cursor[2];
if (cr0->unset)
{
assert(cr1->unset && cr2->unset);
return;
}
iy0 = fixed2int(cr0->y) - base;
iy1 = fixed2int(cr1->y) - base;
if (!cr2->unset)
{
assert(!cr1->unset);
iy2 = fixed2int(cr2->y) - base;
/* Try to merge the end of cursor 0 with the end of cursor 1 */
if (iy0 == iy1 && dirns_match(cr0->d, dirn_flip(cr1->d)))
{
/* Succeeded! Just one to output. */
cr0->d = dirns_merge(cr0->d, dirn_flip(cr1->d));
if (cr0->left > cr1->left)
cr0->left = cr1->left;
if (cr0->right < cr1->right)
cr0->right = cr1->right;
cr1->unset = 1; /* Stop us outputting cursor 1 later */
}
/* Try to merge the end of cursor 2 with the start of cursor 0 */
if (cr0->saved)
{
if (cr0->save_iy == iy2 && dirns_match(cr0->save_d, cr2->d))
{
cr0->save_d = dirns_merge(cr0->save_d, cr2->d);
if (cr0->save_left > cr2->left)
cr0->save_left = cr2->left;
if (cr0->save_right > cr2->right)
cr0->save_right = cr2->right;
cr2->unset = 1; /* Stop us outputting cursor 2 later */
}
}
else
{
/* Maybe cursor 0 never moved from the original pixel */
if (iy0 == iy2 && dirns_match(cr0->d, cr2->d))
{
cr0->d = dirns_merge(cr0->d, cr2->d);
if (cr0->left > cr2->left)
cr0->left = cr2->left;
if (cr0->right > cr2->right)
cr0->right = cr2->right;
cr2->unset = 1; /* Stop us outputting cursor 2 later */
}
}
/* Try to merge the start of cursor 2 with the start of cursor 1 */
if (cr1->saved)
{
if (cr2->saved)
{
if (cr2->save_iy == cr1->save_iy && dirns_match(cr2->save_d, dirn_flip(cr1->save_d)))
{
cr2->save_d = dirns_merge(cr2->save_d, dirn_flip(cr1->save_d));
if (cr2->save_left > cr1->save_left)
cr2->save_left = cr1->save_left;
if (cr2->save_right > cr1->save_right)
cr2->save_right = cr1->save_right;
cr1->saved = 0; /* Don't output cr1->saved again later */
}
}
else if (!cr2->unset)
{
/* Maybe cursor 2 never moved from the original pixel */
if (iy2 == cr1->save_iy && dirns_match(cr2->d, dirn_flip(cr1->save_d)))
{
cr2->d = dirns_merge(cr2->d, dirn_flip(cr1->save_d));
if (cr2->left > cr1->save_left)
cr2->left = cr1->save_left;
if (cr2->right > cr1->save_right)
cr2->right = cr1->save_right;
cr1->saved = 0; /* Don't output cr1->saved again later */
}
}
}
else if (!cr1->unset)
{
/* Cursor 1 might not have moved from the original pixel, hence nothing saved */
if (cr2->saved)
{
if (cr2->save_iy == iy1 && dirns_match(cr2->save_d, dirn_flip(cr1->d)))
{
cr2->save_d = dirns_merge(cr2->save_d, dirn_flip(cr1->d));
if (cr2->save_left > cr1->left)
cr2->save_left = cr1->left;
if (cr2->save_right > cr1->right)
cr2->save_right = cr1->right;
cr1->unset = 1; /* Stop us outputting cursor 1 later */
}
}
else if (!cr2->unset)
{
/* Maybe cursor 2 never moved from the original pixel */
if (iy2 == iy1 && dirns_match(cr2->d, dirn_flip(cr1->d)))
{
cr2->d = dirns_merge(cr2->d, dirn_flip(cr1->d));
if (cr2->left > cr1->left)
cr2->left = cr1->left;
if (cr2->right > cr1->right)
cr2->right = cr1->right;
cr1->unset = 1; /* Stop us outputting cursor 1 later */
}
}
}
else
{
/* Cursor 1 might not have moved from the original pixel, hence nothing saved,
* AND we might have merged it with cursor 0 already! */
if (cr2->saved)
{
if (iy0 == cr2->save_iy && dirns_match(cr0->d, cr2->save_d))
{
cr0->d = dirns_merge(cr0->d, cr2->save_d);
if (cr0->left > cr2->save_left)
cr0->left = cr2->save_left;
if (cr0->right > cr2->save_right)
cr0->right = cr2->save_right;
cr2->saved = 0; /* Stop us outputting saved cursor 2 later */
}
}
else if (!cr2->unset)
{
/* Maybe cursor 2 never moved from the original pixel */
if (iy0 == iy2 && dirns_match(cr0->d, cr2->d))
{
cr0->d = dirns_merge(cr0->d, cr2->d);
if (cr0->left > cr2->left)
cr0->left = cr2->left;
if (cr0->right > cr2->right)
cr0->right = cr2->right;
cr2->unset = 1; /* Stop us outputting cursor 2 later */
}
}
}
}
else
{
/* Try to merge the end of cursor 0 with the start of cursor 0 */
if (cr0->saved)
{
if (iy0 == cr0->save_iy && dirns_match(cr0->d, cr0->save_d))
{
cr0->d = dirns_merge(cr0->d, cr0->save_d);
if (cr0->left > cr0->save_left)
cr0->left = cr0->save_left;
if (cr0->right > cr0->save_right)
cr0->right = cr0->save_right;
cr0->saved = 0; /* Stop us outputting saved cursor 0 later */
}
}
if (!cr1->unset)
{
/* Try to merge the end of cursor 1 with the start of cursor 1 */
if (cr1->saved)
{
if (iy1 == cr1->save_iy && dirns_match(cr1->d, cr1->save_d))
{
cr1->d = dirns_merge(cr1->d, cr1->save_d);
if (cr1->left > cr1->save_left)
cr1->left = cr1->save_left;
if (cr1->right > cr1->save_right)
cr1->right = cr1->save_right;
cr1->saved = 0; /* Stop us outputting saved cursor 1 later */
}
}
}
}
if (!cr0->unset)
cursor_output(eb, 0, iy0);
if (cr0->saved)
{
cr0->left = cr0->save_left;
cr0->right = cr0->save_right;
cr0->d = cr0->save_d;
cursor_output(eb, 0, cr0->save_iy);
}
if (!cr1->unset)
cursor_output(eb, 1, iy1);
if (cr1->saved)
{
cr1->left = cr1->save_left;
cr1->right = cr1->save_right;
cr1->d = cr1->save_d;
cursor_output(eb, 1, cr1->save_iy);
}
if (!cr2->unset)
cursor_output(eb, 2, iy2);
if (cr2->saved)
{
cr2->left = cr2->save_left;
cr2->right = cr2->save_right;
cr2->d = cr2->save_d;
cursor_output(eb, 2, cr2->save_iy);
}
}
static void do_mark_line_app(fz_context *ctx, fz_edgebuffer *eb, fixed sx, fixed sy, fixed ex, fixed ey, int rev)
{
int base_y = eb->super.clip.y0;
int height = eb->super.clip.y1 - eb->super.clip.y0;
int isy, iey;
fixed y_steps;
fixed save_sy = sy;
fixed save_ex = ex;
fixed save_ey = ey;
int truncated;
cursor_t * FZ_RESTRICT cr = &eb->cursor[rev];
if (cr->unset)
cr->y = sy, cr->left = sx, cr->right = sx, cr->unset = 0;
/* Floating point inaccuracies can cause these not *quite* to be true. */
assert(cr->y == sy && cr->left <= sx && cr->right >= sx && cr->d >= DIRN_UNSET && cr->d <= DIRN_DOWN);
sy = cr->y;
if (cr->left > sx)
sx = cr->left;
else if (cr->right < sx)
sx = cr->right;
if (sx == ex && sy == ey)
return;
isy = fixed2int(sy) - base_y;
iey = fixed2int(ey) - base_y;
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
fz_write_printf(ctx, fz_stderr(ctx), "Marking line (app) from %x,%x to %x,%x (%x,%x) %d\n", sx, sy, ex, ey, isy, iey, rev);
#endif
if (isy < iey) {
/* Rising line */
if (iey < 0 || isy >= height) {
/* All line is outside. */
cr->y = ey;
cr->left = ex;
cr->right = ex;
cr->can_save = 0;
return;
}
if (isy < 0) {
/* Move sy up */
int y = ey - sy;
int new_sy = int2fixed(base_y);
int dy = new_sy - sy;
sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
sy = new_sy;
cursor_init(eb, rev, sy, sx);
isy = 0;
}
truncated = iey > height;
if (truncated) {
/* Move ey down */
int y = ey - sy;
int new_ey = int2fixed(base_y + height);
int dy = ey - new_ey;
save_ex = ex;
save_ey = ey;
ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
ey = new_ey;
iey = height;
}
} else {
/* Falling line */
if (isy < 0 || iey >= height) {
/* All line is outside. */
cr->y = ey;
cr->left = ex;
cr->right = ex;
cr->can_save = 0;
return;
}
truncated = iey < 0;
if (truncated) {
/* Move ey up */
int y = ey - sy;
int new_ey = int2fixed(base_y);
int dy = ey - new_ey;
ex -= (int)((((int64_t)(ex-sx))*dy + y/2)/y);
ey = new_ey;
iey = 0;
}
if (isy >= height) {
/* Move sy down */
int y = ey - sy;
if (y) {
int new_sy = int2fixed(base_y + height);
int dy = new_sy - sy;
sx += (int)((((int64_t)(ex-sx))*dy + y/2)/y);
sy = new_sy;
cursor_init(eb, rev, sy, sx);
isy = height;
}
}
}
assert(cr->left <= sx);
assert(cr->right >= sx);
assert(cr->y == sy);
/* A note: The code below used to be of the form:
* if (isy == iey) ... deal with horizontal lines
* else if (ey > sy) {
* fixed y_steps = ey - sy;
* ... deal with rising lines ...
* } else {
* fixed y_steps = ey - sy;
* ... deal with falling lines
* }
* but that lead to problems, for instance, an example seen
* has sx=2aa8e, sy=8aee7, ex=7ffc1686, ey=8003e97a.
* Thus isy=84f, iey=ff80038a. We can see that ey < sy, but
* sy - ey < 0!
* We therefore rejig our code so that the choice between
* cases is done based on the sign of y_steps rather than
* the relative size of ey and sy.
*/
/* First, deal with lines that don't change scanline.
* This accommodates horizontal lines. */
if (isy == iey) {
if (save_sy == save_ey) {
/* Horizontal line. Don't change cr->d, don't flush. */
} else if (save_sy > save_ey) {
/* Falling line, flush if previous was rising */
cursor_down(eb, rev, sx);
} else {
/* Rising line, flush if previous was falling */
cursor_up(eb, rev, sx);
}
if (sx <= ex) {
cursor_left_merge(eb, rev, sx);
cursor_right_merge(eb, rev, ex);
} else {
cursor_left_merge(eb, rev, ex);
cursor_right_merge(eb, rev, sx);
}
cr->y = ey;
if (sy > save_ey)
goto endFalling;
} else if ((y_steps = ey - sy) > 0) {
/* We want to change from sy to ey, which are guaranteed to be on
* different scanlines. We do this in 3 phases.
* Phase 1 gets us from sy to the next scanline boundary.
* Phase 2 gets us all the way to the last scanline boundary.
* Phase 3 gets us from the last scanline boundary to ey.
*/
/* We want to change from sy to ey, which are guaranteed to be on
* different scanlines. We do this in 3 phases.
* Phase 1 gets us from sy to the next scanline boundary. (We may exit after phase 1).
* Phase 2 gets us all the way to the last scanline boundary. (This may be a null operation)
* Phase 3 gets us from the last scanline boundary to ey. (We are guaranteed to have output the cursor at least once before phase 3).
*/
int phase1_y_steps = (-sy) & (fixed_1 - 1);
int phase3_y_steps = ey & (fixed_1 - 1);
cursor_up(eb, rev, sx);
if (sx == ex) {
/* Vertical line. (Rising) */
/* Phase 1: */
cursor_left_merge(eb, rev, sx);
cursor_right_merge(eb, rev, sx);
if (phase1_y_steps) {
/* If phase 1 will move us into a new scanline, then we must
* flush it before we move. */
cursor_step(eb, rev, phase1_y_steps, sx);
sy += phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto end;
}
/* Phase 3: precalculation */
y_steps -= phase3_y_steps;
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps > 0) {
cursor_always_step(eb, rev, fixed_1, sx);
y_steps--;
while (y_steps) {
cursor_always_step_inrange_vertical(eb, rev, fixed_1, sx);
y_steps--;
}
}
/* Phase 3 */
assert(cr->left == sx && cr->right == sx);
cr->y += phase3_y_steps;
} else if (sx < ex) {
/* Lines increasing in x. (Rightwards, rising) */
int phase1_x_steps, phase3_x_steps;
fixed x_steps = ex - sx;
/* Phase 1: */
cursor_left_merge(eb, rev, sx);
if (phase1_y_steps) {
phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
sx += phase1_x_steps;
cursor_right_merge(eb, rev, sx);
x_steps -= phase1_x_steps;
cursor_step(eb, rev, phase1_y_steps, sx);
sy += phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto end;
}
/* Phase 3: precalculation */
phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
x_steps -= phase3_x_steps;
y_steps -= phase3_y_steps;
assert((y_steps & (fixed_1 - 1)) == 0);
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps) {
/* We want to change sx by x_steps in y_steps steps.
* So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/y_steps. */
int x_inc = x_steps/y_steps;
int n_inc = x_steps - (x_inc * y_steps);
int f = y_steps/2;
int d = y_steps;
/* Special casing the unset iteration, allows us to simplify
* the following loop. */
sx += x_inc;
f -= n_inc;
if (f < 0)
f += d, sx++;
cursor_right_merge(eb, rev, sx);
cursor_always_step(eb, rev, fixed_1, sx);
y_steps--;
while (y_steps) {
sx += x_inc;
f -= n_inc;
if (f < 0)
f += d, sx++;
cursor_right(eb, rev, sx);
cursor_always_inrange_step_right(eb, rev, fixed_1, sx);
y_steps--;
};
}
/* Phase 3 */
assert(cr->left <= ex && cr->right >= sx);
cursor_right(eb, rev, ex);
cr->y += phase3_y_steps;
} else {
/* Lines decreasing in x. (Leftwards, rising) */
int phase1_x_steps, phase3_x_steps;
fixed x_steps = sx - ex;
/* Phase 1: */
cursor_right_merge(eb, rev, sx);
if (phase1_y_steps) {
phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
x_steps -= phase1_x_steps;
sx -= phase1_x_steps;
cursor_left_merge(eb, rev, sx);
cursor_step(eb, rev, phase1_y_steps, sx);
sy += phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto end;
}
/* Phase 3: precalculation */
phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
x_steps -= phase3_x_steps;
y_steps -= phase3_y_steps;
assert((y_steps & (fixed_1 - 1)) == 0);
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps) {
/* We want to change sx by x_steps in y_steps steps.
* So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
int x_inc = x_steps/y_steps;
int n_inc = x_steps - (x_inc * y_steps);
int f = y_steps/2;
int d = y_steps;
/* Special casing the unset iteration, allows us to simplify
* the following loop. */
sx -= x_inc;
f -= n_inc;
if (f < 0)
f += d, sx--;
cursor_left_merge(eb, rev, sx);
cursor_always_step(eb, rev, fixed_1, sx);
y_steps--;
while (y_steps) {
sx -= x_inc;
f -= n_inc;
if (f < 0)
f += d, sx--;
cursor_left(eb, rev, sx);
cursor_always_inrange_step_left(eb, rev, fixed_1, sx);
y_steps--;
}
}
/* Phase 3 */
assert(cr->right >= ex && cr->left <= sx);
cursor_left(eb, rev, ex);
cr->y += phase3_y_steps;
}
} else {
/* So lines decreasing in y. */
/* We want to change from sy to ey, which are guaranteed to be on
* different scanlines. We do this in 3 phases.
* Phase 1 gets us from sy to the next scanline boundary. This never causes an output.
* Phase 2 gets us all the way to the last scanline boundary. This is guaranteed to cause an output.
* Phase 3 gets us from the last scanline boundary to ey. We are guaranteed to have outputted by now.
*/
int phase1_y_steps = sy & (fixed_1 - 1);
int phase3_y_steps = (-ey) & (fixed_1 - 1);
y_steps = -y_steps;
/* Cope with the awkward 0x80000000 case. */
if (y_steps < 0)
{
int mx, my;
mx = sx + ((ex-sx)>>1);
my = sy + ((ey-sy)>>1);
do_mark_line_app(ctx, eb, sx, sy, mx, my, rev);
do_mark_line_app(ctx, eb, mx, my, ex, ey, rev);
return;
}
cursor_down(eb, rev, sx);
if (sx == ex) {
/* Vertical line. (Falling) */
/* Phase 1: */
cursor_left_merge(eb, rev, sx);
cursor_right_merge(eb, rev, sx);
if (phase1_y_steps) {
/* Phase 1 in a falling line never moves us into a new scanline. */
cursor_never_step_vertical(eb, rev, -phase1_y_steps, sx);
sy -= phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto endFalling;
}
/* Phase 3: precalculation */
y_steps -= phase3_y_steps;
assert((y_steps & (fixed_1 - 1)) == 0);
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps) {
cursor_always_step(eb, rev, -fixed_1, sx);
y_steps--;
while (y_steps) {
cursor_always_step_inrange_vertical(eb, rev, -fixed_1, sx);
y_steps--;
}
}
/* Phase 3 */
if (phase3_y_steps > 0) {
cursor_step(eb, rev, -phase3_y_steps, sx);
assert(cr->left == sx && cr->right == sx);
}
} else if (sx < ex) {
/* Lines increasing in x. (Rightwards, falling) */
int phase1_x_steps, phase3_x_steps;
fixed x_steps = ex - sx;
/* Phase 1: */
cursor_left_merge(eb, rev, sx);
if (phase1_y_steps) {
phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
x_steps -= phase1_x_steps;
sx += phase1_x_steps;
/* Phase 1 in a falling line never moves us into a new scanline. */
cursor_never_step_right(eb, rev, -phase1_y_steps, sx);
sy -= phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto endFalling;
} else
cursor_right_merge(eb, rev, sx);
/* Phase 3: precalculation */
phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
x_steps -= phase3_x_steps;
y_steps -= phase3_y_steps;
assert((y_steps & (fixed_1 - 1)) == 0);
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps) {
/* We want to change sx by x_steps in y_steps steps.
* So each step, we add x_steps/y_steps to sx. That's x_inc + n_inc/ey. */
int x_inc = x_steps/y_steps;
int n_inc = x_steps - (x_inc * y_steps);
int f = y_steps/2;
int d = y_steps;
cursor_always_step(eb, rev, -fixed_1, sx);
sx += x_inc;
f -= n_inc;
if (f < 0)
f += d, sx++;
cursor_right(eb, rev, sx);
y_steps--;
while (y_steps) {
cursor_always_inrange_step_right(eb, rev, -fixed_1, sx);
sx += x_inc;
f -= n_inc;
if (f < 0)
f += d, sx++;
cursor_right(eb, rev, sx);
y_steps--;
}
}
/* Phase 3 */
if (phase3_y_steps > 0) {
cursor_step(eb, rev, -phase3_y_steps, sx);
cursor_right(eb, rev, ex);
assert(cr->left == sx && cr->right == ex);
}
} else {
/* Lines decreasing in x. (Falling) */
int phase1_x_steps, phase3_x_steps;
fixed x_steps = sx - ex;
/* Phase 1: */
cursor_right_merge(eb, rev, sx);
if (phase1_y_steps) {
phase1_x_steps = (int)(((int64_t)x_steps * phase1_y_steps + y_steps/2) / y_steps);
x_steps -= phase1_x_steps;
sx -= phase1_x_steps;
/* Phase 1 in a falling line never moves us into a new scanline. */
cursor_never_step_left(eb, rev, -phase1_y_steps, sx);
sy -= phase1_y_steps;
y_steps -= phase1_y_steps;
if (y_steps == 0)
goto endFalling;
} else
cursor_left_merge(eb, rev, sx);
/* Phase 3: precalculation */
phase3_x_steps = (int)(((int64_t)x_steps * phase3_y_steps + y_steps/2) / y_steps);
x_steps -= phase3_x_steps;
y_steps -= phase3_y_steps;
assert((y_steps & (fixed_1 - 1)) == 0);
/* Phase 2: */
y_steps = fixed2int(y_steps);
assert(y_steps >= 0);
if (y_steps) {
/* We want to change sx by x_steps in y_steps steps.
* So each step, we sub x_steps/y_steps from sx. That's x_inc + n_inc/ey. */
int x_inc = x_steps/y_steps;
int n_inc = x_steps - (x_inc * y_steps);
int f = y_steps/2;
int d = y_steps;
cursor_always_step(eb, rev, -fixed_1, sx);
sx -= x_inc;
f -= n_inc;
if (f < 0)
f += d, sx--;
cursor_left(eb, rev, sx);
y_steps--;
while (y_steps) {
cursor_always_inrange_step_left(eb, rev, -fixed_1, sx);
sx -= x_inc;
f -= n_inc;
if (f < 0)
f += d, sx--;
cursor_left(eb, rev, sx);
y_steps--;
}
}
/* Phase 3 */
if (phase3_y_steps > 0) {
cursor_step(eb, rev, -phase3_y_steps, sx);
cursor_left(eb, rev, ex);
assert(cr->left == ex && cr->right == sx);
}
}
endFalling:
if (truncated)
cursor_output(eb, rev, fixed2int(cr->y) - base_y);
}
end:
if (truncated) {
cr->left = save_ex;
cr->right = save_ex;
cr->y = save_ey;
}
}
static void mark_line_app(fz_context *ctx, fz_edgebuffer *eb, fixed sx, fixed sy, fixed ex, fixed ey, int rev)
{
if (rev == 1)
{
fixed t;
t = sx, sx = ex, ex = t;
t = sy, sy = ey, ey = t;
}
do_mark_line_app(ctx, eb, sx, sy, ex, ey, rev);
}
static void fz_insert_edgebuffer_app(fz_context *ctx, fz_rasterizer *ras, float fsx, float fsy, float fex, float fey, int rev)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
fixed sx = safe_float2fixed(fsx);
fixed sy = safe_float2fixed(fsy);
fixed ex = safe_float2fixed(fex);
fixed ey = safe_float2fixed(fey);
if (fsx < fex)
{
if (fsx < eb->super.bbox.x0) eb->super.bbox.x0 = fsx;
if (fex > eb->super.bbox.x1) eb->super.bbox.x1 = fex;
}
else
{
if (fsx > eb->super.bbox.x1) eb->super.bbox.x1 = fsx;
if (fex < eb->super.bbox.x0) eb->super.bbox.x0 = fex;
}
if (fsy < fey)
{
if (fsy < eb->super.bbox.y0) eb->super.bbox.y0 = fsy;
if (fey > eb->super.bbox.y1) eb->super.bbox.y1 = fey;
}
else
{
if (fey < eb->super.bbox.y0) eb->super.bbox.y0 = fey;
if (fsy > eb->super.bbox.y1) eb->super.bbox.y1 = fsy;
}
mark_line_app(ctx, eb, sx, sy, ex, ey, rev);
}
static int intcmp(const void *a, const void *b)
{
return *((int*)a) - *((int *)b);
}
static void fz_convert_edgebuffer(fz_context *ctx, fz_rasterizer *ras, int eofill, const fz_irect *clip, fz_pixmap *pix, unsigned char *color, fz_overprint *eop)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
int scanlines = ras->clip.y1 - ras->clip.y0;
int i, n, a, pl, pr;
int *table = eb->table;
int *index = eb->index;
uint8_t *out;
fz_solid_color_painter_t *fn;
fn = fz_get_solid_color_painter(pix->n, color, pix->alpha, eop);
assert(fn);
if (fn == NULL)
return;
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before sort:\n");
fz_edgebuffer_print(ctx, err, eb);
}
#endif
if (!eb->sorted)
{
eb->sorted = 1;
for (i = 0; i < scanlines; i++)
{
int *row = &table[index[i]];
int rowlen = *row++;
/* Bubblesort short runs, qsort longer ones. */
/* FIXME: Check "6" below */
if (rowlen <= 6) {
int j, k;
for (j = 0; j < rowlen-1; j++)
{
int t = row[j];
for (k = j+1; k < rowlen; k++)
{
int s = row[k];
if (t > s)
row[k] = t, t = row[j] = s;
}
}
} else
qsort(row, rowlen, sizeof(int), intcmp);
}
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before filter: %s\n", eofill ? "EO" : "NZ");
fz_edgebuffer_print(ctx, err, eb);
}
#endif
for (i=0; i < scanlines; i++) {
int *row = &table[index[i]];
int *rowstart = row;
int rowlen = *row++;
int *rowout = row;
while (rowlen > 0)
{
int left, right;
if (eofill) {
/* Even Odd */
left = (*row++)&~1;
right = (*row++)&~1;
rowlen -= 2;
} else {
/* Non-Zero */
int w;
left = *row++;
w = ((left&1)-1) | (left&1);
rowlen--;
do {
right = *row++;
rowlen--;
w += ((right&1)-1) | (right&1);
} while (w != 0);
left &= ~1;
right &= ~1;
}
if (right > left) {
*rowout++ = left;
*rowout++ = right;
}
}
*rowstart = (rowout-rowstart)-1;
}
}
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before render:\n");
fz_edgebuffer_print(ctx, err, eb);
}
#endif
n = pix->n;
a = pix->alpha;
pl = fz_maxi(ras->clip.x0, pix->x);
pr = fz_mini(ras->clip.x1, pix->x + pix->w);
pr -= pl;
out = pix->samples + pix->stride * fz_maxi(ras->clip.y0 - pix->y, 0) + fz_maxi(ras->clip.x0 - pix->x, 0) * n;
if (scanlines > pix->y + pix->h - ras->clip.y0)
scanlines = pix->y + pix->h - ras->clip.y0;
for (i = fz_maxi(pix->y - ras->clip.y0, 0); i < scanlines; i++) {
int *row = &table[index[i]];
int rowlen = *row++;
while (rowlen > 0) {
int left, right;
left = *row++;
right = *row++;
rowlen -= 2;
left = fixed2int(left + fixed_half) - pl;
right = fixed2int(right + fixed_half) - pl;
if (right <= 0)
continue;
if (left >= pr)
continue;
if (right > pr)
right = pr;
if (left < 0)
left = 0;
right -= left;
if (right > 0) {
(*fn)(out + left*n, n, right, color, a, eop);
}
}
out += pix->stride;
}
}
static int edgecmp(const void *a, const void *b)
{
int left = ((int*)a)[0];
int right = ((int*)b)[0];
left -= right;
if (left)
return left;
return ((int*)a)[1] - ((int*)b)[1];
}
static void fz_convert_edgebuffer_app(fz_context *ctx, fz_rasterizer *ras, int eofill, const fz_irect *clip, fz_pixmap *pix, unsigned char *color, fz_overprint *eop)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
int scanlines = ras->clip.y1 - ras->clip.y0;
int i, n, a, pl, pr;
int *table = eb->table;
int *index = eb->index;
uint8_t *out;
fz_solid_color_painter_t *fn;
fn = fz_get_solid_color_painter(pix->n, color, pix->alpha, eop);
assert(fn);
if (fn == NULL)
return;
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before sort:\n");
fz_edgebuffer_print_app(ctx, err, eb);
}
#endif
if (!eb->sorted)
{
eb->sorted = 1;
for (i = 0; i < scanlines; i++)
{
int *row = &table[index[i]];
int rowlen = *row++;
/* Bubblesort short runs, qsort longer ones. */
/* FIXME: Check "6" below */
if (rowlen <= 6) {
int j, k;
for (j = 0; j < rowlen-1; j++) {
int * FZ_RESTRICT t = &row[j<<1];
for (k = j+1; k < rowlen; k++) {
int * FZ_RESTRICT s = &row[k<<1];
int tmp;
if (t[0] < s[0])
continue;
if (t[0] > s[0])
tmp = t[0], t[0] = s[0], s[0] = tmp;
else if (t[0] <= s[1])
continue;
tmp = t[1]; t[1] = s[1]; s[1] = tmp;
}
}
} else
qsort(row, rowlen, 2*sizeof(int), edgecmp);
}
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before filter: %s\n", eofill ? "EO" : "NZ");
fz_edgebuffer_print_app(ctx, err, eb);
}
#endif
for (i=0; i < scanlines; i++) {
int *row = &table[index[i]];
int rowlen = *row++;
int *rowstart = row;
int *rowout = row;
int ll, lr, rl, rr, wind, marked_to;
/* Avoid double setting pixels, by keeping where we have marked to. */
marked_to = int2fixed(clip->x0);
while (rowlen > 0) {
if (eofill) {
/* Even Odd */
ll = (*row++)&~1;
lr = *row;
row += 2;
rowlen-=2;
/* We will fill solidly from ll to at least lr, possibly further */
assert(rowlen >= 0);
rr = (*row++);
if (rr > lr)
lr = rr;
} else {
/* Non-Zero */
int w;
ll = *row++;
lr = *row++;
wind = -(ll&1) | 1;
ll &= ~1;
rowlen--;
assert(rowlen > 0);
do {
rl = *row++;
rr = *row++;
w = -(rl&1) | 1;
/* rl &= ~1; */
rowlen--;
if (rr > lr)
lr = rr;
wind += w;
if (wind == 0)
break;
} while (rowlen > 0);
}
if (marked_to >= lr)
continue;
if (marked_to >= ll) {
if (rowout == rowstart)
ll = marked_to;
else {
rowout -= 2;
ll = *rowout;
}
}
if (lr > ll) {
*rowout++ = ll;
*rowout++ = lr;
marked_to = lr;
}
}
rowstart[-1] = rowout-rowstart;
}
}
#ifdef DEBUG_SCAN_CONVERTER
if (debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, err, "Before render:\n");
fz_edgebuffer_print_app(ctx, err, eb);
}
#endif
n = pix->n;
a = pix->alpha;
pl = clip->x0;
pr = clip->x1 - pl;
out = pix->samples + pix->stride * (clip->y0 - pix->y) + (clip->x0 - pix->x) * n;
if (scanlines > clip->y1 - ras->clip.y0)
scanlines = clip->y1 - ras->clip.y0;
i = (clip->y0 - ras->clip.y0);
if (i < 0)
return;
for (; i < scanlines; i++) {
int *row = &table[index[i]];
int rowlen = *row++;
while (rowlen > 0) {
int left, right;
left = *row++;
right = *row++;
rowlen -= 2;
left = fixed2int(left + fixed_half) - pl;
right = fixed2int(right + fixed_half) - pl;
if (right <= 0)
continue;
if (left >= pr)
break;
if (right > pr)
right = pr;
if (left < 0)
left = 0;
right -= left;
if (right > 0) {
(*fn)(out + left*n, n, right, color, a, eop);
}
}
out += pix->stride;
}
}
static void fz_gap_edgebuffer(fz_context *ctx, fz_rasterizer *ras)
{
fz_edgebuffer *eb = (fz_edgebuffer *)ras;
if (eb->app)
{
#ifdef DEBUG_SCAN_CONVERTER
if (0 && debugging_scan_converter)
{
fz_output *err = fz_stderr(ctx);
fz_write_printf(ctx, fz_stderr(ctx), "Pen up move.\n");
fz_write_printf(ctx, err, "Before flush:\n");
fz_edgebuffer_print_app(ctx, err, eb);
}
#endif
cursor_flush(eb);
eb->cursor[0].saved = 0;
eb->cursor[0].unset = 1;
eb->cursor[0].can_save = 1;
eb->cursor[0].d = DIRN_UNSET;
eb->cursor[1].saved = 0;
eb->cursor[1].unset = 1;
eb->cursor[1].can_save = 1;
eb->cursor[1].d = DIRN_UNSET;
eb->cursor[2].saved = 0;
eb->cursor[2].unset = 1;
eb->cursor[2].can_save = 1;
eb->cursor[2].d = DIRN_UNSET;
}
}
static int fz_is_rect_edgebuffer(fz_context *ctx, fz_rasterizer *r)
{
return 0;
}
static const fz_rasterizer_fns edgebuffer_app =
{
fz_drop_edgebuffer,
fz_reset_edgebuffer,
fz_postindex_edgebuffer,
fz_insert_edgebuffer_app,
NULL,
fz_gap_edgebuffer,
fz_convert_edgebuffer_app,
fz_is_rect_edgebuffer,
1 /* Reusable */
};
static const fz_rasterizer_fns edgebuffer_cop =
{
fz_drop_edgebuffer,
fz_reset_edgebuffer,
fz_postindex_edgebuffer,
fz_insert_edgebuffer,
NULL,
NULL, /* gap */
fz_convert_edgebuffer,
fz_is_rect_edgebuffer,
1 /* Reusable */
};
fz_rasterizer *
fz_new_edgebuffer(fz_context *ctx, fz_edgebuffer_rule rule)
{
fz_edgebuffer *eb;
eb = fz_new_derived_rasterizer(ctx, fz_edgebuffer, rule == FZ_EDGEBUFFER_ANY_PART_OF_PIXEL ? &edgebuffer_app : &edgebuffer_cop);
eb->app = rule == FZ_EDGEBUFFER_ANY_PART_OF_PIXEL;
return &eb->super;
}
|