/usr/lib/falcon/img/qrcode/lib.fal is in libfalcon-engine1 0.9.6.9-git20120606-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*
FALCON - The Falcon Programming Language.
FILE: lib.fal
QR Code library back end.
-----------------------------------------------------------------------------
Author: Giuseppe Greco
Begin: Wed, 27 Oct 2010 13:11:50 +0200
-----------------------------------------------------------------------------
(C) Copyright 2010: the FALCON developers (see list in AUTHORS file)
See LICENSE file for licensing details.
*/
/*# @beginignore */
import from gd2
load bufext
const MaxInt = 2147483647
const MaxVersion = 40
const MaxWidth = 177
const StructHeaderSize = 20
const FrameBaseName = "frame"
const MaskBaseName = "mask"
/*# Capacity attributes. */
enum QrCapacity
Width = 0
Words = 1
Reminder = 2
Ec = 3
end
/*# Encoding modes. */
enum QrMode
Null = -1
Num = 0
An = 1
Bin = 2
Kanji = 3
Struct = 4
end
/*# Error correction levels. */
enum QrEcl
L = 0
M = 1
Q = 2
H = 3
end
/*# Demerit coefficients. */
enum QrDemeritCoeff
N1 = 3
N2 = 3
N3 = 40
N4 = 10
end
/*# Length table. */
QrLengthTable = [
[10, 12, 14],
[ 9, 11, 13],
[ 8, 16, 16],
[ 8, 10, 12]
]
/*# Alphanumeric table. */
QrAnTable = [
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1,
-1, 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, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
]
/*# Capacity table. */
QrCapacityTable = [
[ 0, 0, 0, [ 0, 0, 0, 0]],
[ 21, 26, 0, [ 7, 10, 13, 17]], // 1
[ 25, 44, 7, [ 10, 16, 22, 28]],
[ 29, 70, 7, [ 15, 26, 36, 44]],
[ 33, 100, 7, [ 20, 36, 52, 64]],
[ 37, 134, 7, [ 26, 48, 72, 88]], // 5
[ 41, 172, 7, [ 36, 64, 96, 112]],
[ 45, 196, 0, [ 40, 72, 108, 130]],
[ 49, 242, 0, [ 48, 88, 132, 156]],
[ 53, 292, 0, [ 60, 110, 160, 192]],
[ 57, 346, 0, [ 72, 130, 192, 224]], //10
[ 61, 404, 0, [ 80, 150, 224, 264]],
[ 65, 466, 0, [ 96, 176, 260, 308]],
[ 69, 532, 0, [ 104, 198, 288, 352]],
[ 73, 581, 3, [ 120, 216, 320, 384]],
[ 77, 655, 3, [ 132, 240, 360, 432]], //15
[ 81, 733, 3, [ 144, 280, 408, 480]],
[ 85, 815, 3, [ 168, 308, 448, 532]],
[ 89, 901, 3, [ 180, 338, 504, 588]],
[ 93, 991, 3, [ 196, 364, 546, 650]],
[ 97, 1085, 3, [ 224, 416, 600, 700]], //20
[101, 1156, 4, [ 224, 442, 644, 750]],
[105, 1258, 4, [ 252, 476, 690, 816]],
[109, 1364, 4, [ 270, 504, 750, 900]],
[113, 1474, 4, [ 300, 560, 810, 960]],
[117, 1588, 4, [ 312, 588, 870, 1050]], //25
[121, 1706, 4, [ 336, 644, 952, 1110]],
[125, 1828, 4, [ 360, 700, 1020, 1200]],
[129, 1921, 3, [ 390, 728, 1050, 1260]],
[133, 2051, 3, [ 420, 784, 1140, 1350]],
[137, 2185, 3, [ 450, 812, 1200, 1440]], //30
[141, 2323, 3, [ 480, 868, 1290, 1530]],
[145, 2465, 3, [ 510, 924, 1350, 1620]],
[149, 2611, 3, [ 540, 980, 1440, 1710]],
[153, 2761, 3, [ 570, 1036, 1530, 1800]],
[157, 2876, 0, [ 570, 1064, 1590, 1890]], //35
[161, 3034, 0, [ 600, 1120, 1680, 1980]],
[165, 3196, 0, [ 630, 1204, 1770, 2100]],
[169, 3362, 0, [ 660, 1260, 1860, 2220]],
[173, 3532, 0, [ 720, 1316, 1950, 2310]],
[177, 3706, 0, [ 750, 1372, 2040, 2430]] //40
]
/*# Error correction code table. */
QrEccTable = [
[[ 0, 0], [ 0, 0], [ 0, 0], [ 0, 0]],
[[ 1, 0], [ 1, 0], [ 1, 0], [ 1, 0]], // 1
[[ 1, 0], [ 1, 0], [ 1, 0], [ 1, 0]],
[[ 1, 0], [ 1, 0], [ 2, 0], [ 2, 0]],
[[ 1, 0], [ 2, 0], [ 2, 0], [ 4, 0]],
[[ 1, 0], [ 2, 0], [ 2, 2], [ 2, 2]], // 5
[[ 2, 0], [ 4, 0], [ 4, 0], [ 4, 0]],
[[ 2, 0], [ 4, 0], [ 2, 4], [ 4, 1]],
[[ 2, 0], [ 2, 2], [ 4, 2], [ 4, 2]],
[[ 2, 0], [ 3, 2], [ 4, 4], [ 4, 4]],
[[ 2, 2], [ 4, 1], [ 6, 2], [ 6, 2]], //10
[[ 4, 0], [ 1, 4], [ 4, 4], [ 3, 8]],
[[ 2, 2], [ 6, 2], [ 4, 6], [ 7, 4]],
[[ 4, 0], [ 8, 1], [ 8, 4], [12, 4]],
[[ 3, 1], [ 4, 5], [11, 5], [11, 5]],
[[ 5, 1], [ 5, 5], [ 5, 7], [11, 7]], //15
[[ 5, 1], [ 7, 3], [15, 2], [ 3, 13]],
[[ 1, 5], [10, 1], [ 1, 15], [ 2, 17]],
[[ 5, 1], [ 9, 4], [17, 1], [ 2, 19]],
[[ 3, 4], [ 3, 11], [17, 4], [ 9, 16]],
[[ 3, 5], [ 3, 13], [15, 5], [15, 10]], //20
[[ 4, 4], [17, 0], [17, 6], [19, 6]],
[[ 2, 7], [17, 0], [ 7, 16], [34, 0]],
[[ 4, 5], [ 4, 14], [11, 14], [16, 14]],
[[ 6, 4], [ 6, 14], [11, 16], [30, 2]],
[[ 8, 4], [ 8, 13], [ 7, 22], [22, 13]], //25
[[10, 2], [19, 4], [28, 6], [33, 4]],
[[ 8, 4], [22, 3], [ 8, 26], [12, 28]],
[[ 3, 10], [ 3, 23], [ 4, 31], [11, 31]],
[[ 7, 7], [21, 7], [ 1, 37], [19, 26]],
[[ 5, 10], [19, 10], [15, 25], [23, 25]], //30
[[13, 3], [ 2, 29], [42, 1], [23, 28]],
[[17, 0], [10, 23], [10, 35], [19, 35]],
[[17, 1], [14, 21], [29, 19], [11, 46]],
[[13, 6], [14, 23], [44, 7], [59, 1]],
[[12, 7], [12, 26], [39, 14], [22, 41]], //35
[[ 6, 14], [ 6, 34], [46, 10], [ 2, 64]],
[[17, 4], [29, 14], [49, 10], [24, 46]],
[[ 4, 18], [13, 32], [48, 14], [42, 32]],
[[20, 4], [40, 7], [43, 22], [10, 67]],
[[19, 6], [18, 31], [34, 34], [20, 61]] //40
]
/*# Alignment pattern table. */
QrAlignmentPatternTable = [
[ 0, 0],
[ 0, 0], [18, 0], [22, 0], [26, 0], [30, 0], // 1- 5
[34, 0], [22, 38], [24, 42], [26, 46], [28, 50], // 6-10
[30, 54], [32, 58], [34, 62], [26, 46], [26, 48], //11-15
[26, 50], [30, 54], [30, 56], [30, 58], [34, 62], //16-20
[28, 50], [26, 50], [30, 54], [28, 54], [32, 58], //21-25
[30, 58], [34, 62], [26, 50], [30, 54], [26, 52], //26-30
[30, 56], [34, 60], [30, 58], [34, 62], [30, 54], //31-35
[24, 50], [28, 54], [32, 58], [26, 54], [30, 58] //35-40
]
/*# Version pattern table. */
QrVersionPatternTable = [
0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d,
0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9,
0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75,
0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64,
0x27541, 0x28c69
]
/*# Format information table. */
QrFormatInfoTable = [
[0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976],
[0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0],
[0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed],
[0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b]
]
/*# Class that represents a QR-Code error.
@param code The error code.
@param desc The error description.
@param extra Extra error information.
*/
class QrError(code, desc, extra) from Error(code, desc, extra)
functionality_not_supported = 10000
invalid_param = 10001
invalid_version = 10002
version_not_found = 10003
invalid_ecl = 10004
invalid_mode = 10005
invalid_mask = 10006
encoding_error = 10007
_desc = [
self.functionality_not_supported => i"Functionality not supported",
self.invalid_param => i"Invalid parameter",
self.invalid_version => i"Invalid version",
self.version_not_found => i"Version not found",
self.invalid_ecl => i"Invalid error correction level",
self.invalid_mode => i"Invalid mode",
self.invalid_mask => i"Invalid mask",
self.encoding_error => i"Encoding error"
]
init
if code in self._desc
self.description = self._desc[code]
end
end
end
/*# Class that represents an input item.
@param mode One of the @a QrMode values.
@param size The size of the item data.
@param data The item data.
@param bitBuf The bit buffer where the intput data will be encoded.
*/
class QrInputItem(mode, size, data, bitBuf)
_mode = QrMode.Null
_size = 0
_data = nil
_bitBuf = nil
init
if data == nil
raise QrError(QrError.invalid_param, i"data is null")
end
dataLength = len(data)
if size and size > dataLength
data[dataLength:] = "\x0" * (size - dataLength)
end
if not QrInput.verifyMode(mode, size, data)
raise QrError(QrError.encoding_error, @i"Mode $(mode) is not suitable for encoding the specified data")
end
self._mode = mode
self._size = size
self._data = data
self._bitBuf = bitBuf
end
function __get_mode()
return self._mode
end
function __get_bitBuf()
return self._bitBuf
end
function estimateBitBufferSize(version)
size = 0
if version == 0: version = 1
if self._mode == QrMode.Num
size = QrInput.estimateNumSize(self._size)
elif self._mode == QrMode.An
size = QrInput.estimateAnSize(self._size)
elif self._mode == QrMode.Bin
size = QrInput.estimateBinSize(self._size)
elif self._mode == QrMode.Kanji
size = QrInput.estimateKanjiSize(self._size)
elif self._mode == QrMode.Struct
return StructHeaderSize
else
return 0
end
length = QrSpec.getLengthIndicator(self._mode, version)
w = 1 << length
num = int((self._size + w - 1) / w)
size += num * (length + 4)
return size
end
function encodeBitBuffer(version)
words = QrSpec.getMaxWords(self._mode, version)
if self._size > words
inputItem1 = QrInputItem(self._mode, words, self._data)
inputItem2 = QrInputItem(self._mode, self.size - words, self._data[words:len(self._data) - words])
inputItem1.encodeBitBuffer(version)
inputItem2.encodeBitBuffer(version)
self._bitBuf = BitBuf()
inputItem1.bitBuf.readToBuf(self._bitBuf)
inputItem2.bitBuf.readToBuf(self._bitBuf)
else
if self._mode == QrMode.Num
self._encodeNum(version)
elif self._mode == QrMode.An
self._encodeAn(version)
elif self._mode == QrMode.Bin
self._encodeBin(version)
elif self._mode == QrMode.Kanji
self._encodeKanji(version)
elif self._mode == QrMode.Struct
self._encodeStruct()
else
raise QrError(QrError.invalid_mode, @i"$(self._mode) is not supported")
end
end
return self._bitBuf.sizeBits()
end
/*# @private */
function _encodeNum(version)
val = 0x1
words = int(self._size / 3)
zero = "0"[*0]
bitBuf = BitBuf()
bitBuf.bitCount(4).writeBits(val)
bitBuf.bitCount(QrSpec.getLengthIndicator(QrMode.Num, version)).writeBits(self._size)
// cache function reference to improve performance
funcWriteBits = bitBuf.bitCount(10).writeBits
for i in [0:words]
val = (self._data[*(i * 3)] - zero) * 100
val += (self._data[*(i * 3 + 1)] - zero) * 10
val += self._data[*(i * 3 + 2)] - zero
funcWriteBits(val)
end
if self._size - words * 3 == 1
val = self._data[*(words * 3)] - zero
bitBuf.bitCount(4).writeBits(val)
elif self._size - words * 3 == 2
val = (self._data[*(words * 3)] - zero) * 10
val += self._data[*(words * 3 + 1)] - zero
bitBuf.bitCount(7).writeBits(val)
end
self._bitBuf = bitBuf
end
/*# @private */
function _encodeAn(version)
words = self._size >> 1
bitBuf = BitBuf()
bitBuf.bitCount(4).writeBits(0x02)
bitBuf.bitCount(QrSpec.getLengthIndicator(QrMode.An, version)).writeBits(self._size)
// cache function references to improve performance
funcWriteBits = bitBuf.bitCount(11).writeBits
funcLookAnTable = QrInput.lookAnTable
for i in [0:words]
val = funcLookAnTable(self._data[*(i * 2)]) * 45
val += funcLookAnTable(self._data[*(i * 2 + 1)])
funcWriteBits(val)
end
if self._size && 1
val = funcLookAnTable(self._data[*(words * 2)])
bitBuf.bitCount(6).writeBits(val)
end
self._bitBuf = bitBuf
end
/*# @private */
function _encodeBin(version)
bitBuf = BitBuf()
bitBuf.bitCount(4).writeBits(0x4)
bitBuf.bitCount(QrSpec.getLengthIndicator(QrMode.Bin, version)).writeBits(self._size)
// cache function reference to improve performance
funcWriteBits = bitBuf.bitCount(8).writeBits
for i in [0:self._size]
funcWriteBits(self._data[*i])
end
self._bitBuf = bitBuf
end
/*# @private */
function _encodeKanji(version)
bitBuf = BitBuf()
/*
bitBuf.bitCount(4).writeBits(0x8)
bitBuf.bitCount(QrSpec.getLengthIndicator(QrMode.Kanji, version)).writeBits(int(self._size / 2))
// cache function reference to improve performance
funcWriteBits = bitBuf.bitCount(13).writeBits
for i in [0:self._size]
val = self._data[i][*0]
if val <= 0x9ffc
val -= 0x8140
else
val -= 0xc140
end
h = (val >> 8) * 0xc0
val = (val && 0xff) + h
funcWriteBits(val)
end
self._bitBuf = bitBuf
*/
// TODO: Kanji mode requires shift-jis do be used.
// ATM we don't have it. Just adding the Unicode.
bitBuf.bitCount(4).writeBits(0x8)
bitBuf.bitCount(QrSpec.getLengthIndicator(QrMode.Kanji, version)).writeBits(int(self._size))
// cache function reference to improve performance
funcWriteBits = bitBuf.bitCount(13).writeBits
for i in [0:self._size]
funcWriteBits(self._data[*i])
end
self._bitBuf = bitBuf
end
/*# @private */
function _encodeStruct()
bitBuf = BitBuf()
bitBuf.bitCount(4).writeBits(0x03)
bitBuf.bitCount(4).writeBits(self._data[*1] - 1)
bitBuf.bitCount(4).writeBits(self._data[*0] - 1)
bitBuf.bitCount(8).writeBits(self._data[*2])
self._bitBuf = bitBuf
end
end
/*# Class that represents input data.
@param version The QR-Code version.
@param ecl One of the @a QrEcl values.
*/
class QrInput(version, ecl)
_version = 0
_items = []
_ecl = 0
init
if version == nil: version = 0
if ecl == nil: ecl = QrEcl.L
self._version = version
self.ecl = ecl
end
function __get_version()
return self._version
end
function __set_version(version)
if version < 0 or version > MaxVersion
raise QrError(QrError.invalid_version, @i"$(version) is not supported")
end
self._version = version
end
function __get_ecl()
return self._ecl
end
function __set_ecl(ecl)
if ecl > QrEcl.H
raise QrError(QrError.invalid_ecl, @i"$(ecl) is not supported")
end
self._ecl = ecl
end
function appendEntry(entry)
arrayAdd(self._items, entry)
end
function append(mode, size, data)
entry = QrInputItem(mode, size, data)
arrayAdd(self._items, entry)
end
function lookAnTable(char)
return char > 127 ? -1 : QrAnTable[char]
end
function estimateNumSize(run)
w = int(run / 3)
size = w * 10
switch run - w * 3
case 1
size += 4
case 2
size += 7
end
return size;
end
function estimateAnSize(run)
size = (run >> 1) * 11
if run && 1: size += 6
return size
end
function estimateBinSize(run)
return run << 3
end
function estimateKanjiSize(run)
return (run >> 1) * 13
end
function verifyMode(mode, size, data)
if size <= 0
raise QrError(QrError.invalid_param, i"size is less than 1")
end
if mode == QrMode.Num
return self._verifyNum(size, data)
elif mode == QrMode.An
return self._verifyAn(size, data)
elif mode == QrMode.Kanji
return self._verifyKanji(size, data)
elif mode == QrMode.Bin
return true
elif mode == QrMode.Struct
return true
else
raise QrError(QrError.invalid_mode, @i"$(mode) is not supported")
end
end
function getByteStream()
bitBuf = self._mergeBitBuffer()
self._appendPaddingBits(bitBuf)
// return bitBuf.toMemBuf()
// The following piece of code is a temporary workaround; it is going to
// be removed as soon as BitBuf.toMemBuf() works correctly.
size = bitBuf.size()
if size == 0: return []
data = arrayBuffer(size , 0)
// cache function reference to improve performance
funcRead8 = bitBuf.r8
bitBuf.rposBits(0)
i = 0; while bitBuf.readableBits() > 0
data[i++] = funcRead8()
end
return data
end
/*# @private */
function _appendPaddingBits(bitBuf)
size = bitBuf.sizeBits()
maxWords = QrSpec.getDataLength(self.version, self.ecl)
maxSize = maxWords << 3
if maxSize == size: return
if maxSize - size < 5
bitBuf.bitCount(maxSize - size).writeBits(0)
return
end
size += 4
words = (size + 7) >> 3
padding = BitBuf()
padding.bitCount(words * 8 - size + 4).writeBits(0)
paddingLength = maxWords - words
if paddingLength > 0
paddingBuffer = arrayBuffer(paddingLength)
// cache function reference to improve performance
funcWrite8 = padding.w8
for i in [0:paddingLength]
paddingBuffer[i] = (i && 1) ? 0x11 : 0xec
funcWrite8(paddingBuffer[i]);
end
end
padding.readToBuf(bitBuf)
end
/*# @private */
function _convertData()
version = self._estimateVersion()
if version > self.version
self.version = version
end
loop
size = self._createBitBuffer()
version = QrSpec.getMinVersion((size + 7) >> 3, self.ecl)
if version > self.version
self.version = version
else; break; end
end false
end
/*# @private */
function _createBitBuffer()
size = 0
for item in self._items
size += item.encodeBitBuffer(self.version)
end
return size
end
/*# @private */
function _mergeBitBuffer()
self._convertData()
bitBuf = BitBuf()
for item in self._items
item.bitBuf.readToBuf(bitBuf)
end
return bitBuf
end
/*# @private */
function _estimateVersion()
version = previous = 0
loop
previous = version
size = self._estimateBitBufferSize(previous)
version = QrSpec.getMinVersion((size + 7) >> 3, self.ecl)
end not version > previous
return version
end
/*# @private */
function _estimateBitBufferSize(version)
size = 0
for item in self._items
size += item.estimateBitBufferSize(version)
end
return size
end
/*# @private */
function _verifyNum(size, data)
zero = "0"[*0]
nine = "9"[*0]
for i in [0:size]
if (data[*i] < zero) or (data[*i] > nine): return false
end
return true
end
/*# @private */
function _verifyAn(size, data)
// cache function reference to improve performance
funcLookAnTable = self.lookAnTable
for i in [0:size]
if funcLookAnTable(data[*i]) == -1: return false
end
return true
end
/*# @private */
function _verifyKanji(size, data)
for i in [0:size]
val = data[*i]
/*
if val < 0x8140 or (val > 0x9ffc and val < 0xe040) or val > 0xebbf
return false
end
*/
if val < 0x3000: return false
end
return true
end
end
/*# Class that provides functionality for masking encoded data.
@param maker The mask driver.
*/
class QrMask(maker)
_runLength = nil
_maker = maker
_mask = [
[innerfunc (x, y); return (x + y) && 1; end],
[innerfunc (x, y); return y && 1; end],
[innerfunc (x, y); return x % 3; end],
[innerfunc (x, y); return (x + y) % 3; end],
[innerfunc (x, y); return (int(y / 2) + int(x / 3)) && 1; end],
[innerfunc (x, y); return ((x * y) && 1) + (x * y) % 3; end],
[innerfunc (x, y); return (((x * y) && 1) + (x * y) % 3) && 1; end],
[innerfunc (x, y); return (((x * y) % 3) + ((x + y) && 1)) && 1; end]
]
init
self._runLength = arrayBuffer(MaxWidth + 1, 0)
end
function createMaskNo(maskNo, width, frame, mask, generateBitMaskOnly)
if generateBitMaskOnly == nil: generateBitMaskOnly = false
bitMask = []
if self._maker.cache_dir
filename = self._maker.cache_dir \
+ "/" + MaskBaseName + "_" + maskNo \
+ "/" + MaskBaseName + "_" + width \
+ "_" + maskNo + ".dat"
if fileType(filename) != FileStat.NOTFOUND
inputStream = InputStream(filename)
bitMask = deserialize(inputStream)
inputStream.close()
else
bitMask = self._generateBitMask(maskNo, width, frame)
if fileType(self._maker.cache_dir + "/" + MaskBaseName + "_" + maskNo) == FileStat.NOTFOUND
dirMake(self._maker.cache_dir + "/" + MaskBaseName + "_" + maskNo)
end
outputStream = OutputStream(filename)
serialize(bitMask, outputStream)
outputStream.close()
end
else
bitMask = self._generateBitMask(maskNo, width, frame)
end
if generateBitMaskOnly: return
mask = clone(frame)
maskNo = 0
for y in [0:width]
bitMaskRow = bitMask[y]
frameRow = frame[y]
for x in [0:width]
if bitMaskRow[x] == "\x1"
mask[y][x] = chr(frameRow[*x] ^^ bitMaskRow[*x])
end
maskNo += mask[y][*x] && 1
end
end
return maskNo
end
function createMask(width, frame, maskNo, ecl)
masked = arrayBuffer(width)
for i in [0:width]; masked[i] = "\x0" * width; end
self.createMaskNo(maskNo, width, frame, $masked)
self._writeFormatInformation(width, masked, maskNo, ecl)
return masked
end
function mask(width, frame, ecl)
minDemerit = MaxInt
bestMask = []
checkedMasks = [0, 1, 2, 3, 4, 5, 6, 7]
if self._maker.find_from_random != 0
count = 8 - (self._maker.find_from_random % 9)
for i in [0:count]
pos = random(0, len(checkedMasks) - 1)
arrayRemove(checkedMasks, pos)
end
end
bestMask = frame
for i in checkedMasks
mask = arrayBuffer(width)
for j in [0:width]; mask[j] = "\x0" * width; end
demerit = blacks = 0
blacks = self.createMaskNo(i, width, frame, $mask)
blacks += self._writeFormatInformation(width, mask, i, ecl)
blacks = int(100 * blacks / (width * width))
demerit = int(int(abs(blacks - 50) / 5) * QrDemeritCoeff.N4)
demerit += self._evaluateSymbol(width, mask)
if demerit < minDemerit
minDemerit = demerit
bestMask = mask
end
end
return bestMask
end
/*# @private */
function _calculateN1N3(length)
demerit = 0
for i in [0:length]
if self._runLength[i] >= 5
demerit += (QrDemeritCoeff.N1 + (self._runLength[i] - 5))
end
if i && 1
if (i >= 3) and (i < (length - 2)) and (self._runLength[i] % 3 == 0)
fact = int(self._runLength[i] / 3)
if (self._runLength[i - 2] == fact) and \
(self._runLength[i - 1] == fact) and \
(self._runLength[i + 1] == fact) and \
(self._runLength[i + 2] == fact)
if (self._runLength[i - 3] < 0) or (self._runLength[i - 3] >= (fact << 2))
demerit += QrDemeritCoeff.N3
elif ((i + 3) >= length) or (self._runLength[i + 3] >= (fact << 2))
demerit += QrDemeritCoeff.N3
end
end
end
end
end
return demerit
end
/*# @private */
function _evaluateSymbol(width, frame)
head = demerit = 0
// cache function reference to improve performance
funcCalculateN1N3 = self._calculateN1N3
for y in [0:width]
head = 0
self._runLength[0] = 1
currRow = frame[y]
prevRow = frame[y -1]
for x in [0:width]
if x > 0 and y > 0
b22 = currRow[*x] && currRow[*(x - 1)] && prevRow[*x] && prevRow[*(x - 1)]
w22 = currRow[*x] || currRow[*(x - 1)] || prevRow[*x] || prevRow[*(x - 1)]
if (b22 || (w22 ^^ 1)) && 1: demerit += QrDemeritCoeff.N2
end
if x == 0 and (currRow[*x] && 1)
self._runLength[0] = -1
head = 1
self._runLength[head] = 1
elif x > 0
if (currRow[*x] ^^ currRow[*(x - 1)]) && 1
head++
self._runLength[head] = 1
else
self._runLength[head]++
end
end
end
demerit += funcCalculateN1N3(head + 1)
end
for x in [0:width]
head = 0
self._runLength[0] = 1
for y in [0:width]
if y == 0 and (frame[y][*x] && 1)
self._runLength[0] = -1
head = 1
self._runLength[head] = 1
elif y > 0
if (frame[y][*x] ^^ frame[y - 1][*x]) && 1
head++
self._runLength[head] = 1
else
self._runLength[head]++
end
end
end
demerit += funcCalculateN1N3(head + 1)
end
return demerit
end
/*# @private */
function _generateBitMask(maskNo, width, frame)
bitMask = arrayBuffer(width)
for y in [0:width]
bitMaskRow = "\x0" * width
frameRow = frame[y]
for x in [0:width]
if not frameRow[*x] && 0x80 and self._mask[maskNo](x, y) == 0
bitMaskRow[x] = "\x1"
end
end
bitMask[y] = bitMaskRow
end
return bitMask
end
/*# @private */
function _writeFormatInformation(width, frame, maskNo, ecl)
blacks = 0
format = QrSpec.getFormatInfo(maskNo, ecl)
for i in [0:8]
if format && 1
blacks += 2
val = chr(0x85)
else
val = chr(0x84)
end
frame[8][width - 1 - i] = val
if i < 6
frame[i][8] = val
else
frame[i + 1][8] = val
end
format >>= 1
end
for i in [0:7]
if format && 1
blacks += 2
val = chr(0x85)
else
val = chr(0x84)
end
frame[width - 7 + i][8] = val
if i == 0
frame[8][7] = val
else
frame[8][6 - i] = val
end
format >>= 1
end
return blacks
end
end
/*# Class that represents a Reed-Solomon codec.
@param symSize The symbol size, in bits.
@param gfPoly The extended Galois field generator polynomial coefficients,
with the 0th coefficient in the low order bit. The polynomial must
be primitive.
@param fcr The first consecutive root of the code generator polynomial, in
index from.
@param prim The primitive element to generate polynomial roots.
@param nRoots The code generator polynomial degree (number of roots).
@param pad The number of padding bytes in shortened block.
*/
class QrRsCodec(symSize, gfPoly, fcr, prim, nRoots, pad)
_nn = 0
_alphaTo = nil
_indexOf = nil
_genPoly = nil
_iPrim = 0
mm = 0
nRoots = 0
fcr = 0
prim = 0
pad = 0
gfPoly = 0
init
// Common code for intializing a Reed-Solomon control block (char or int symbols)
// Copyright 2004 Phil Karn, KA9Q
// May be used under the terms of the GNU Lesser General Public License (LGPL)
if (symSize < 0 or symSize > 8) \
or (fcr < 0 or fcr >= (1 << symSize)) \
or (prim <= 0 or prim >= (1 << symSize)) \
or (nRoots < 0 or nRoots >= (1 << symSize)) \
or (pad < 0 or pad >= ((1 << symSize) -1 - nRoots))
raise QrError(QrError.encoding_error, i"error initializing Reed-Solomon control block")
end
self.mm = symSize
self._nn = (1 << symSize) - 1
self.pad = pad
self._alphaTo = arrayBuffer(self._nn + 1, 0)
self._indexOf = arrayBuffer(self._nn + 1, 0)
self._indexOf[0] = self._nn
self._alphaTo[self._nn] = 0
sr = 1
for i in [0:self._nn]
self._indexOf[sr] = i
self._alphaTo[i] = sr
sr <<= 1
if sr && (1 << symSize): sr ^= gfPoly
sr &= self._nn
end
if sr != 1
raise QrError(QrError.encoding_error, i"polynominal is not primitive")
end
self._genPoly = arrayBuffer(nRoots + 1, 0)
self.fcr = fcr
self.prim = prim
self.nRoots = nRoots
self.gfPoly = gfPoly
iprim = 1; while (iprim % prim) != 0; iprim += self._nn; end
self._iPrim = int(iprim / prim)
self._genPoly[0] = 1
root = fcr * prim
for i in [0:nRoots]
self._genPoly[i + 1] = 1
for j in [i:1:-1]
if self._genPoly[j] != 0
self._genPoly[j] = self._genPoly[j - 1] ^^ \
self._alphaTo[self._modnn(self._indexOf[self._genPoly[j]] + root)]
else
self._genPoly[j] = self._genPoly[j - 1]
end
end
self._genPoly[0] = self._alphaTo[self._modnn(self._indexOf[self._genPoly[0]] + root)]
root += prim
end
for i in [0:nRoots + 1]
self._genPoly[i] = self._indexOf[self._genPoly[i]]
end
end
function encodeChar(data)
parity = arrayBuffer(self.nRoots, 0)
for i in [0:self._nn - self.nRoots - self.pad]
feedback = self._indexOf[data[i] ^^ parity[0]]
if feedback != self._nn
feedback = self._modnn(self._nn - self._genPoly[self.nRoots] + feedback)
for j in [1:self.nRoots]
parity[j] ^= self._alphaTo[self._modnn(feedback + self._genPoly[self.nRoots - j])]
end
end
if len(parity) > 0: arrayRemove(parity, 0)
if feedback != self._nn
arrayAdd(parity, self._alphaTo[self._modnn(feedback + self._genPoly[0])])
else
arrayAdd(parity, 0)
end
end
return parity
end
/*# @private */
function _modnn(val)
while val >= self._nn
val -= self._nn
val = (val >> self.mm) + (val && self._nn)
end
return val
end
end
/*# Utility class that provides functionality for creating and caching
Reed-Solomon codecs.
*/
class QrRs
_codecs = []
function getCodec(symSize, gfPoly, fcr, prim, nRoots, pad)
for codec in self._codecs
if codec.pad != pad: continue
if codec.nRoots != nRoots: continue
if codec.mm != symSize: continue
if codec.gfPoly != gfPoly: continue
if codec.item.fcr != fcr: continue
if codec.prim != prim: continue
return codec;
end
codec = QrRsCodec(symSize, gfPoly, fcr, prim, nRoots, pad)
arrayIns(self._codecs, 0, codec)
return codec
end
end
/*# Utility class that provides functionality for splitting data.
@param data The data to split.
@param input The split data.
@param mode One of the @a QrMode values.
*/
class QrSplit(data, input, mode)
_data = nil
_input = nil
_mode = nil
init
self._data = data
self._input = input
self._mode = mode
end
function splitStringToInput(string, input, mode, caseSensitive)
if caseSensitive == nil: caseSensitive = true
if string == nil or string == "\x0" or string == ""
raise QrError(QrError.invalid_param, i"string is empty")
end
split = QrSplit(string, input, mode)
if not caseSensitive: split.toUpper()
split.splitString()
end
function splitString()
while len(self._data) > 0
if self._data == "": break
mode = self._identifyMode(0)
if mode == QrMode.Num
length = self._eatNum()
elif mode == QrMode.An
length = self._eatAn()
elif mode == QrMode.Kanji
if self._mode == QrMode.Kanji
length = self._eatKanji()
else
length = self._eatBin()
end
else
length = self._eatBin()
end
if length > 0: self._data = strBack(self._data, len(self._data) - length)
end
end
function toUpper()
stringLength = len(self._data)
a = "a"[*0]
z = "z"[*0]
i = 0; while i < stringLength
mode = self._identifyMode(strBack(self._data, stringLength - i), self._mode)
if mode == QrMode.Kanji
i += 2
else
if self._data[*i] >= a and self._data[*i] <= z
self._data[i] = chr(self._data[*i] - 32)
end
i++
end
end
return self._data
end
/*# @private */
function _eatNum()
length = QrSpec.getLengthIndicator(QrMode.Num, self._input.version)
run = 0; while self._isDigitAt(self._data, run); run++; end
mode = self._identifyMode(run)
if mode == QrMode.Bin
if QrInput.estimateNumSize(run) + 4 + length \
+ QrInput.estimateBinSize(1) \
- QrInput.estimateBinSize(run + 1) > 0
return self._eatBin()
end
end
if mode == QrMode.An
if QrInput.estimateNumSize(run) + 4 + length \
+ QrInput.estimateAnSize(1) \
- QrInput.estimateAnSize(run + 1) > 0
return self._eatAn()
end
end
self._input.append(QrMode.Num, run, self._data)
return run;
end
/*# @private */
function _eatAn()
lengthAn= QrSpec.getLengthIndicator(QrMode.An, self._input.version)
lengthNum = QrSpec.getLengthIndicator(QrMode.Num, self._input.version)
run = 0; while self._isAlNumAt(self._data, run)
if self._isDigitAt(self._data, run)
q = run
while self._isDigitAt(self._data, q); q++; end
if QrInput.estimateAnSize(run) \
+ QrInput.estimateNumSize(q - run) + 4 + lengthNum \
- QrInput.estimateAnSize(q) < 0
break
else
run = q
end
else
run++
end
end
if not self._isAlNumAt(self._data, run)
if QrInput.estimateAnSize(run) + 4 + lengthAn \
+ QrInput.estimateBinSize(1) \
- QrInput.estimateBinSize(run + 1) > 0
return self._eatBin()
end
end
self._input.append(QrMode.An, run, self._data)
return run
end
/*# @private */
function _eatKanji()
run = 0; while self._identifyMode(run) == QrMode.Kanji; run += 2; end
self._input.append(QrMode.Kanji, run, self._data)
return run
end
function _eatBin()
lengthAn = QrSpec.getLengthIndicator(QrMode.An, self._input.version)
lengthNum = QrSpec.getLengthIndicator(QrMode.Num, self._input.version)
// cache function references to improve performance
funcIdentifyMode = self._identifyMode
funcIsAlNumAt = self._isAlNumAt
funcIsDigitAt = self._isDigitAt
funcEstimateAnSize = QrInput.estimateAnSize
funcEstimateNumSize = QrInput.estimateNumSize
funcEstimateBinSize = QrInput.estimateBinSize
dataLength = len(self._data)
run = 1; while run < dataLength
mode = funcIdentifyMode(run)
if mode == QrMode.Kanji: break
if mode == QrMode.Num
q = run
while funcIsDigitAt(self._data, q); q++; end
if funcEstimateBinSize(run) \
+ funcEstimateNumSize(q - run) + 4 + lengthNum \
- funcEstimateBinSize(q) < 0
break
else
run = q
end
elif mode == QrMode.An
q = run
while funcIsAlNumAt(self._data, q); q++; end
if funcEstimateBinSize(run) \
+ funcEstimateAnSize(q - run) + 4 + lengthAn \
- funcEstimateBinSize(q) < 0
break
else
run = q
end
else
run++
end
end
self._input.append(QrMode.Bin, run, self._data)
return run
end
/*# @private */
function _identifyMode(pos)
if pos >= len(self._data): return QrMode.Null
if self._isDigitAt(self._data, pos)
return QrMode.Num
elif self._isAlNumAt(self._data, pos)
return QrMode.An
elif self._mode == QrMode.Kanji
if pos + 1 < len(self._data)
word = self._data[*pos] << 8 || self._data[*(pos + 1)]
// if (word >= 0x8140 and word <= 0x9ffc) or (word >= 0xe040 and word <= 0xebbf)
// Our kanjis are in the UNICODE range, not in shift-jis
if word >= 0x3040 and word <= 0x4db0
return QrMode.Kanji
end
end
end
return QrMode.Bin
end
/*# @private */
function _isDigitAt(str, pos)
if pos >= len(str): return false
return str[*pos] >= "0"[*0] and str[*pos] <= "9"[*0]
end
/*# @private */
function _isAlNumAt(str, pos)
if pos >= len(str): return false
return QrInput.lookAnTable(str[*pos]) >= 0
end
end
/*# Object that provides functionality for generating QR-Code images. */
object QrImage
function png(frame, outFile, pixelPerPoint, outerFrame)
img = self._create(frame, pixelPerPoint, outerFrame)
img.Png(outFile)
end
function jpg(frame, outFile, pixelPerPoint, outerFrame, quality)
if quality == nil: quality = 85
img = self._create(frame, pixelPerPoint, outerFrame)
img.Jpeg(outFile, quality)
end
/*# @private */
function _create(frame, pixelPerPoint, outerFrame)
if frame == nil
raise QrError(QrError.invalid_param, i"frame is null")
end
if pixelPerPoint == nil: pixelPerPoint = 4
if outerFrame == nil: outerFrame = 4
height = len(frame)
width = len(frame[0])
imgHeight = height + 2 * outerFrame
imgWidth = width + 2 * outerFrame
baseImg = gd2.GdImage(imgWidth, imgHeight)
col = .[
baseImg.ColorAllocate(255, 255, 255)
baseImg.ColorAllocate(0, 0, 0)
]
baseImg.Fill(0, 0, col[0])
// cache function reference to improve performance
funcSetPixel = baseImg.SetPixel
for y in [0:height]
row = frame[y]
for x in [0:width]
if row[x] == "1"
funcSetPixel(x + outerFrame, y + outerFrame, col[1])
end
end
end
targetImg = gd2.GdImage(imgWidth * pixelPerPoint, imgHeight * pixelPerPoint)
targetImg.CopyResized(baseImg, 0, 0, 0, 0, \
imgWidth * pixelPerPoint, \
imgHeight * pixelPerPoint, \
imgWidth, imgHeight)
return targetImg
end
end
/*# Class that represents a Reed-Salomon control block.
@param dataLength The length of the data to encode, in bytes.
@param data The data to encode.
@param ecc The error correction code.
@param codec The codec to be used to encode the data.
*/
class QrRsBlock(dataLength, data, ecc, codec)
dataLength = 0
data = nil
ecc = nil
init
self.dataLength = dataLength
self.data = data
self.ecc = ecc = codec.encodeChar(data)
end
end
/*# Class that represents raw code.
@param input The input data to encode.
*/
class QrRawCode(input)
_version = 0
_data = nil
_dataLength = 0
_ecc = nil
_eccLength = 0
_rsBlocks = []
_blockNum = 0
_blockNum1 = 0
_count = 0
_rs = QrRs()
init
if input == nil
raise QrError(QrError.invalid_param, i"input is null")
end
self._data = input.getByteStream()
self._version = input.version
spec = QrSpec.getEccSpec(input.version, input.ecl)
self._blockNum1 = QrSpec.rsBlockNum1(spec)
self._dataLength = QrSpec.rsDataLength(spec)
self._eccLength = QrSpec.rsEccLength(spec)
self._ecc = arrayBuffer(self._eccLength, 0)
self._blockNum = QrSpec.rsBlockNum(spec)
self._initialize(spec)
end
function __get_version()
return self._version
end
function __get_dataLength()
return self._dataLength
end
function __get_eccLength()
return self._eccLength
end
function getCode()
code = 0
if self._count < self._dataLength
row = self._count % self._blockNum
col = self._count / self._blockNum
if col >= self._rsBlocks[0].dataLength
row += self._blockNum1
end
code = self._rsBlocks[row].data[col]
elif self._count < self._dataLength + self._eccLength
row = (self._count - self._dataLength) % self._blockNum
col = (self._count - self._dataLength) / self._blockNum
code = self._rsBlocks[row].ecc[col]
else
return code
end
self._count++
return code
end
/*# @private */
function _initialize(spec)
dataPos = eccPos = 0
ecc = nil
self._rsBlocks = []
dataLength = QrSpec.rsDataCodes1(spec)
eccLength = QrSpec.rsEccCodes1(spec)
codec = self._rs.getCodec(8, 0x11d, 0, 1, eccLength, 255 - dataLength - eccLength)
for i in [0:QrSpec.rsBlockNum1(spec)]
arrayAdd(self._rsBlocks, QrRsBlock(dataLength, self._data[dataPos:], $ecc, codec))
self._ecc = self._ecc[0:eccPos] + ecc[0:]
dataPos += dataLength
eccPos += eccLength
end
if QrSpec.rsBlockNum2(spec) != 0
dataLength = QrSpec.rsDataCodes2(spec)
eccLength = QrSpec.rsEccCodes2(spec)
codec = self._rs.getCodec(8, 0x11d, 0, 1, eccLength, 255 - dataLength - eccLength)
for i in [0:QrSpec.rsBlockNum2(spec)]
arrayAdd(self._rsBlocks, QrRsBlock(dataLength, self._data[dataPos:], $ecc, codec))
self._ecc = self._ecc[0:eccPos] + ecc[0:]
dataPos += dataLength
eccPos += eccLength
end
end
end
end
/*# Class that represents a frame filler.
@param width The frame width, in bytes.
@param frame The frame to fill.
*/
class QrFrameFiller(width, frame)
_width = 0
_frame = nil
_x = 0
_y = 0
_dir = 0
_bit = 0
init
self._width = width
self._frame = frame
self._x = width - 1
self._y = width - 1
self._dir = -1
self._bit = -1
end
function __get_width()
return self._width
end
function __get_frame()
return self._frame
end
function setFrameAt(at, val)
self._frame[at["y"]][at["x"]] = chr(val)
end
function getFrameAt(at)
return self._frame[at["y"]][*at["x"]]
end
function next()
x = self._x
y = self._y
loop
if self._bit == -1
self._bit = 0
return ["x" => self._x, "y" => self._y]
end
if self._bit == 0
x--
self._bit++
else
x++
y += self._dir
self._bit--
end
if self._dir < 0
if y < 0
y = 0
x -= 2
self._dir = 1
if x == 6
x--
y = 9
end
end
else
if y == self.width
y = self.width - 1
x -= 2
self._dir = -1
if x == 6
x--
y -= 8
end
end
end
if x < 0 or y < 0
raise QrError(QrError.encoding_error, i"error masking frame")
end
self._x = x
self._y = y
end (self._frame[y][*x] && 0x80) == 0
return ["x" => x, "y" => y]
end
end
/*# Utility class that provides general-purpose functionality. */
class QrTools
function binarize(frame)
frameLength = len(frame)
for row in frame
for i in [0:frameLength]
row[i] = (row[*i] && 1) ? "1" : "0"
end
end
return frame
end
function buildCache(dir)
if dir == nil or dir == ""
raise QrError(QrError.invalid_param, i"dir is null or empty")
end
mask = QrMask()
spec = QrSpec()
for version in [1:MaxVersion + 1]
frame = spec.createFrame(version, dir)
filename = dir + "/" + FrameBaseName + "_" + version + ".png"
QrImage.png(self.binarize(frame), filename, 1, 0)
width = len(frame)
bitMask = arrayBuffer(width)
for i in [0:width]; bitMask[i] = "\x0" * width; end
for maskNo in [0:8]
mask.createMaskNo(maskNo, width, frame, $bitMask, true)
end
end
end
function dumpBitBuf(bitBuf)
size = bitBuf.sizeBits()
data = arrayBuffer(size , 0)
rpos = bitBuf.rposBits()
bitBuf.rposBits(0)
while (count = bitBuf.readableBits()) > 0
if count > 8: count = 8
bits = bitBuf.bitCount(count).readBits()
mask = 0x80
mask >>= (8 - count)
for i in [0:count]
if bits && mask; >> 1; else; >> 0; end
mask >>= 1
end
end
>
bitBuf.rposBits(rpos)
end
end
/*# Class that represents the QR-Code specification. */
class QrSpec
frames = nil
function getDataLength(version, ecl)
return QrCapacityTable[version][QrCapacity.Words] - QrCapacityTable[version][QrCapacity.Ec][ecl]
end
function getEccLength(version, ecl)
return QrCapacityTable[version][QrCapacity.Ec][ecl]
end
function getWidth(version)
return QrCapacityTable[version][QrCapacity.Width]
end
function getRemainder(version)
return QrCapacityTable[version][QrCapacity.Reminder]
end
function getMinVersion(size, ecl)
for i in [1:MaxVersion + 1]
if QrCapacityTable[i][QrCapacity.Words] - \
QrCapacityTable[i][QrCapacity.Ec][ecl] >= size
return i
end
end
raise QrError(QrError.version_not_found)
end
function getLengthIndicator(mode, version)
if mode == QrMode.Struct: return 0
length = 0
if version <= 9
length = 0
elif version <= 26
length = 1
else
length = 2
end
return QrLengthTable[mode][length]
end
function getMaxWords(mode, version)
if mode == QrMode.Struct: return 3
length = 0
if version <= 9
length = 0
elif version <= 26
length = 1
else
length = 2
end
words = (1 << (QrLengthTable[mode][length])) - 1
if mode == QrMode.Kanji: words <<= 1
return words
end
function getEccSpec(version, ecl)
spec = [0, 0, 0, 0, 0]
b1 = QrEccTable[version][ecl][0]
b2 = QrEccTable[version][ecl][1]
dataLength = self.getDataLength(version, ecl)
eccLength = self.getEccLength(version, ecl)
if b2 == 0
spec[0] = b1
spec[1] = int(dataLength / b1)
spec[2] = int(eccLength / b1)
spec[3] = 0
spec[4] = 0
else
spec[0] = b1
spec[1] = int(dataLength / (b1 + b2))
spec[2] = int(eccLength / (b1 + b2))
spec[3] = b2
spec[4] = spec[1] + 1
end
return spec
end
function putAlignmentMarker(frame, x, y)
finder = [
"\xa1\xa1\xa1\xa1\xa1",
"\xa1\xa0\xa0\xa0\xa1",
"\xa1\xa0\xa1\xa0\xa1",
"\xa1\xa0\xa0\xa0\xa1",
"\xa1\xa1\xa1\xa1\xa1"
]
x -= 2
y -= 2
for i in [0:5]
self._applyPattern(frame, x, y + i, finder[i])
end
end
function getVersionPattern(version)
if version < 7 or version > MaxVersion
raise QrError(QrError.invalid_version, @i"$(version) is not supported")
end
return QrVersionPatternTable[version -7]
end
function getFormatInfo(mask, ecl)
if mask < 0 or mask > 7;
raise QrError(QrError.invalid_mask, @i"$(mask) is not supported")
end
if ecl < QrEcl.L or ecl > QrEcl.H
raise QrError(QrError.invalid_ecl, @i"$(ecl) not supported")
end
return QrFormatInfoTable[ecl][mask]
end
function putFinderPattern(frame, x, y)
finder = [
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
"\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
]
for i in [0:7]
self._applyPattern(frame, x, y + i, finder[i])
end
end
function createFrame(version, cacheDir)
if cacheDir == "": cacheDir = nil
if version < 1 or version > MaxVersion
raise QrError(QrError.invalid_version, @i"$(version) is not supported")
end
arrayLength = version + 1
if self.frames == nil
self.frames = arrayBuffer(arrayLength, nil)
elif len(self.frames) < arrayLength
arrayResize(self.frames, arrayLength)
end
if self.frames[version] == nil
if cacheDir
filename = cacheDir + "/" + FrameBaseName + "_" + version + ".dat"
if fileType(cacheDir) == FileStat.NOTFOUND
dirMake(cacheDir, true)
fileChmod(cacheDir, 0755)
end
if fileType(filename) != FileStat.NOTFOUND
inputStream = InputStream(filename)
deserialized = deserialize(inputStream)
inputStream.close()
self.frames[version] = deserialized
else
frame = self._createFrame(version)
outputStream = OutputStream(filename)
serialize(frame, outputStream)
outputStream.close()
self.frames[version] = frame
end
else
self.frames[version] = self._createFrame(version)
end
end
if self.frames[version] == nil
raise QrError(QrError.encoding_error, i"error creating frame")
end
return self.frames[version]
end
function rsBlockNum(spec); return spec[0] + spec[3]; end
function rsBlockNum1(spec); return spec[0]; end
function rsDataCodes1(spec); return spec[1]; end
function rsEccCodes1(spec); return spec[2]; end
function rsBlockNum2(spec); return spec[3]; end
function rsDataCodes2(spec); return spec[4]; end
function rsEccCodes2(spec); return spec[2]; end
function rsDataLength(spec); return (spec[0] * spec[1]) + (spec[3] * spec[4]); end
function rsEccLength(spec); return (spec[0] + spec[3]) * spec[2]; end
/*# @private */
function _applyPattern(frame, x, y, pattern, length)
substr = strFront(frame[y], x)
srcLength = len(frame[y])
parLength = len(substr)
if length == nil
parLength += len(pattern)
substr += pattern
elif length > 0
parLength += length
substr += strFront(pattern, length)
end
delta = srcLength - parLength
if delta > 0: substr += strBack(frame[y], delta)
return (frame[y] = substr)
end
/*# @private */
function _createFrame(version)
width = QrCapacityTable[version][QrCapacity.Width]
frame = arrayBuffer(width)
for i in [0:width]; frame[i] = "\x0" * width; end
self.putFinderPattern(frame, 0, 0)
self.putFinderPattern(frame, width - 7, 0)
self.putFinderPattern(frame, 0, width - 7)
offset = width - 7
for y in [0:7]
frame[y][7] = "\xc0"
frame[y][width - 8] = "\xc0"
frame[offset][7] = "\xc0"
offset++
end
pattern = "\xc0" * 8
self._applyPattern(frame, 0, 7, pattern)
self._applyPattern(frame, width - 8, 7, pattern)
self._applyPattern(frame, 0, width - 8, pattern)
pattern = "\x84" * 9
self._applyPattern(frame, 0, 8, pattern)
self._applyPattern(frame, width - 8, 8, pattern, 8)
offset = width - 8
for y in [0:8]
frame[y][8] = "\x84"
frame[offset][8] = "\x84"
offset++
end
for i in [1:width - 15]
frame[6][7 + i] = frame[7 + i][6] = chr(0x90 || (i && 1))
end
self._putAlignmentPattern(version, frame, width)
if version >= 7
versionPattern = self.getVersionPattern(version)
v = versionPattern
for x in [0:6]
for y in [0:3]
frame[(width - 11) + y][x] = chr(0x88 || (v && 1))
v >>= 1
end
end
v = versionPattern
for y in [0:6]
for x in [0:3]
frame[y][x + (width - 11)] = chr(0x88 || (v && 1))
v >>= 1
end
end
end
frame[width - 8][8] = "\x81"
return frame
end
/*# @private */
function _putAlignmentPattern(version, frame, width)
if version < 2: return
d = QrAlignmentPatternTable[version][1] - QrAlignmentPatternTable[version][0]
if d < 0
w = 2
else
w = int((width - QrAlignmentPatternTable[version][0]) / d + 2)
end
if w * w - 3 == 1
x = QrAlignmentPatternTable[version][0]
y = QrAlignmentPatternTable[version][0]
self.putAlignmentMarker(frame, x, y)
return
end
x = QrAlignmentPatternTable[version][0]
for i in [1:w - 1]
self.putAlignmentMarker(frame, 6, x)
self.putAlignmentMarker(frame, x, 6)
x += d
end
y = QrAlignmentPatternTable[version][0]
for i in [0:w - 1]
x = QrAlignmentPatternTable[version][0]
for j in [0:w - 1]
self.putAlignmentMarker(frame, x, y)
x += d
end
y += d
end
end
end
|