summaryrefslogtreecommitdiffstats
path: root/zbar/qrcode/qrdec.c
blob: e4c922c5b2c6ad1c2ab1e9c141c358b482abf3a0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
/*Copyright (C) 2008-2009  Timothy B. Terriberry (tterribe@xiph.org)
  You can redistribute this library and/or modify it under the terms of the
   GNU Lesser General Public License as published by the Free Software
   Foundation; either version 2.1 of the License, or (at your option) any later
   version.*/

#include "config.h"

#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "bch15_5.h"
#include "binarize.h"
#include "error.h"
#include "image.h"
#include "isaac.h"
#include "qrcode.h"
#include "rs.h"
#include "svg.h"
#include "util.h"

#include "qrdec.h"

typedef int qr_line[3];

typedef struct qr_finder_cluster qr_finder_cluster;
typedef struct qr_finder_edge_pt qr_finder_edge_pt;
typedef struct qr_finder_center qr_finder_center;

typedef struct qr_aff qr_aff;
typedef struct qr_hom qr_hom;

typedef struct qr_finder qr_finder;

typedef struct qr_hom_cell qr_hom_cell;
typedef struct qr_sampling_grid qr_sampling_grid;
typedef struct qr_pack_buf qr_pack_buf;

/*The number of bits in an int.
  Note the cast to (int): this prevents this value from "promoting" whole
   expressions to an (unsigned) size_t.*/
#define QR_INT_BITS    ((int)sizeof(int) * CHAR_BIT)
#define QR_INT_LOGBITS (QR_ILOG(QR_INT_BITS))

/*A 14 bit resolution for a homography ensures that the ideal module size for a
   version 40 code differs from that of a version 39 code by at least 2.*/
#define QR_HOM_BITS (14)

/*The number of bits of sub-module precision to use when searching for
   alignment patterns.
  Two bits allows an alignment pattern to be found even if the modules have
   been eroded by up to 50% (due to blurring, etc.).
  This must be at least one, since it affects the dynamic range of the
   transforms, and we sample at half-module resolution to compute a bounding
   quadrilateral for the code.*/
#define QR_ALIGN_SUBPREC (2)

/* collection of finder lines */
typedef struct qr_finder_lines {
    qr_finder_line *lines;
    int nlines, clines;
} qr_finder_lines;

struct qr_reader {
    /*The GF(256) representation used in Reed-Solomon decoding.*/
    rs_gf256 gf;
    /*The random number generator used by RANSAC.*/
    isaac_ctx isaac;
    /* current finder state, horizontal and vertical lines */
    qr_finder_lines finder_lines[2];
};

/*Initializes a client reader handle.*/
static void qr_reader_init(qr_reader *reader)
{
    /*time_t now;
      now=time(NULL);
      isaac_init(&_reader->isaac,&now,sizeof(now));*/
    isaac_init(&reader->isaac, NULL, 0);
    rs_gf256_init(&reader->gf, QR_PPOLY);
}

/*Allocates a client reader handle.*/
qr_reader *_zbar_qr_create(void)
{
    qr_reader *reader = (qr_reader *)calloc(1, sizeof(*reader));
    qr_reader_init(reader);
    return (reader);
}

/*Frees a client reader handle.*/
void _zbar_qr_destroy(qr_reader *reader)
{
    zprintf(1, "max finder lines = %dx%d\n", reader->finder_lines[0].clines,
	    reader->finder_lines[1].clines);
    if (reader->finder_lines[0].lines)
	free(reader->finder_lines[0].lines);
    if (reader->finder_lines[1].lines)
	free(reader->finder_lines[1].lines);
    free(reader);
}

/* reset finder state between scans */
void _zbar_qr_reset(qr_reader *reader)
{
    reader->finder_lines[0].nlines = 0;
    reader->finder_lines[1].nlines = 0;
}

/*A cluster of lines crossing a finder pattern (all in the same direction).*/
struct qr_finder_cluster {
    /*Pointers to the lines crossing the pattern.*/
    qr_finder_line **lines;
    /*The number of lines in the cluster.*/
    int nlines;
};

/*A point on the edge of a finder pattern.
  These are obtained from the endpoints of the lines crossing this particular
   pattern.*/
struct qr_finder_edge_pt {
    /*The location of the edge point.*/
    qr_point pos;
    /*A label classifying which edge this belongs to:
    0: negative u edge (left)
    1: positive u edge (right)
    2: negative v edge (top)
    3: positive v edge (bottom)*/
    int edge;
    /*The (signed) perpendicular distance of the edge point from a line parallel
     to the edge passing through the finder center, in (u,v) coordinates.
    This is also re-used by RANSAC to store inlier flags.*/
    int extent;
};

/*The center of a finder pattern obtained from the crossing of one or more
   clusters of horizontal finder lines with one or more clusters of vertical
   finder lines.*/
struct qr_finder_center {
    /*The estimated location of the finder center.*/
    qr_point pos;
    /*The list of edge points from the crossing lines.*/
    qr_finder_edge_pt *edge_pts;
    /*The number of edge points from the crossing lines.*/
    int nedge_pts;
};

static int qr_finder_vline_cmp(const void *_a, const void *_b)
{
    const qr_finder_line *a;
    const qr_finder_line *b;
    a = (const qr_finder_line *)_a;
    b = (const qr_finder_line *)_b;
    return ((a->pos[0] > b->pos[0]) - (a->pos[0] < b->pos[0]) << 1) +
	   (a->pos[1] > b->pos[1]) - (a->pos[1] < b->pos[1]);
}

/*Clusters adjacent lines into groups that are large enough to be crossing a
   finder pattern (relative to their length).
  _clusters:  The buffer in which to store the clusters found.
  _neighbors: The buffer used to store the lists of lines in each cluster.
  _lines:     The list of lines to cluster.
              Horizontal lines must be sorted in ascending order by Y
               coordinate, with ties broken by X coordinate.
              Vertical lines must be sorted in ascending order by X coordinate,
               with ties broken by Y coordinate.
  _nlines:    The number of lines in the set of lines to cluster.
  _v:         0 for horizontal lines, or 1 for vertical lines.
  Return: The number of clusters.*/
static int qr_finder_cluster_lines(qr_finder_cluster *_clusters,
				   qr_finder_line **_neighbors,
				   qr_finder_line *_lines, int _nlines, int _v)
{
    unsigned char *mark;
    qr_finder_line **neighbors;
    int nneighbors;
    int nclusters;
    int i;
    /*TODO: Kalman filters!*/
    mark      = (unsigned char *)calloc(_nlines, sizeof(*mark));
    neighbors = _neighbors;
    nclusters = 0;
    for (i = 0; i < _nlines - 1; i++)
	if (!mark[i]) {
	    int len;
	    int j;
	    nneighbors	 = 1;
	    neighbors[0] = _lines + i;
	    len		 = _lines[i].len;
	    for (j = i + 1; j < _nlines; j++)
		if (!mark[j]) {
		    const qr_finder_line *a;
		    const qr_finder_line *b;
		    int thresh;
		    a = neighbors[nneighbors - 1];
		    b = _lines + j;
		    /*The clustering threshold is proportional to the size of the lines,
         since minor noise in large areas can interrupt patterns more easily
         at high resolutions.*/
		    thresh = a->len + 7 >> 2;
		    if (abs(a->pos[1 - _v] - b->pos[1 - _v]) > thresh)
			break;
		    if (abs(a->pos[_v] - b->pos[_v]) > thresh)
			continue;
		    if (abs(a->pos[_v] + a->len - b->pos[_v] - b->len) > thresh)
			continue;
		    if (a->boffs > 0 && b->boffs > 0 &&
			abs(a->pos[_v] - a->boffs - b->pos[_v] + b->boffs) >
			    thresh) {
			continue;
		    }
		    if (a->eoffs > 0 && b->eoffs > 0 &&
			abs(a->pos[_v] + a->len + a->eoffs - b->pos[_v] -
			    b->len - b->eoffs) > thresh) {
			continue;
		    }
		    neighbors[nneighbors++] = _lines + j;
		    len += b->len;
		}
	    /*We require at least three lines to form a cluster, which eliminates a
       large number of false positives, saving considerable decoding time.
      This should still be sufficient for 1-pixel codes with no noise.*/
	    if (nneighbors < 3)
		continue;
	    /*The expected number of lines crossing a finder pattern is equal to their
       average length.
      We accept the cluster if size is at least 1/3 their average length (this
       is a very small threshold, but was needed for some test images).*/
	    len = ((len << 1) + nneighbors) / (nneighbors << 1);
	    if (nneighbors * (5 << QR_FINDER_SUBPREC) >= len) {
		_clusters[nclusters].lines  = neighbors;
		_clusters[nclusters].nlines = nneighbors;
		for (j = 0; j < nneighbors; j++)
		    mark[neighbors[j] - _lines] = 1;
		neighbors += nneighbors;
		nclusters++;
	    }
	}
    free(mark);
    return nclusters;
}

/*Adds the coordinates of the edge points from the lines contained in the
   given list of clusters to the list of edge points for a finder center.
  Only the edge point position is initialized.
  The edge label and extent are set by qr_finder_edge_pts_aff_classify()
   or qr_finder_edge_pts_hom_classify().
  _edge_pts:   The buffer in which to store the edge points.
  _nedge_pts:  The current number of edge points in the buffer.
  _neighbors:  The list of lines in the cluster.
  _nneighbors: The number of lines in the list of lines in the cluster.
  _v:          0 for horizontal lines and 1 for vertical lines.
  Return: The new total number of edge points.*/
static int qr_finder_edge_pts_fill(qr_finder_edge_pt *_edge_pts, int _nedge_pts,
				   qr_finder_cluster **_neighbors,
				   int _nneighbors, int _v)
{
    int i;
    for (i = 0; i < _nneighbors; i++) {
	qr_finder_cluster *c;
	int j;
	c = _neighbors[i];
	for (j = 0; j < c->nlines; j++) {
	    qr_finder_line *l;
	    l = c->lines[j];
	    if (l->boffs > 0) {
		_edge_pts[_nedge_pts].pos[0] = l->pos[0];
		_edge_pts[_nedge_pts].pos[1] = l->pos[1];
		_edge_pts[_nedge_pts].pos[_v] -= l->boffs;
		_nedge_pts++;
	    }
	    if (l->eoffs > 0) {
		_edge_pts[_nedge_pts].pos[0] = l->pos[0];
		_edge_pts[_nedge_pts].pos[1] = l->pos[1];
		_edge_pts[_nedge_pts].pos[_v] += l->len + l->eoffs;
		_nedge_pts++;
	    }
	}
    }
    return _nedge_pts;
}

static int qr_finder_center_cmp(const void *_a, const void *_b)
{
    const qr_finder_center *a;
    const qr_finder_center *b;
    a = (const qr_finder_center *)_a;
    b = (const qr_finder_center *)_b;
    return ((b->nedge_pts > a->nedge_pts) - (b->nedge_pts < a->nedge_pts)
	    << 2) +
	   ((a->pos[1] > b->pos[1]) - (a->pos[1] < b->pos[1]) << 1) +
	   (a->pos[0] > b->pos[0]) - (a->pos[0] < b->pos[0]);
}

/*Determine if a horizontal line crosses a vertical line.
  _hline: The horizontal line.
  _vline: The vertical line.
  Return: A non-zero value if the lines cross, or zero if they do not.*/
static int qr_finder_lines_are_crossing(const qr_finder_line *_hline,
					const qr_finder_line *_vline)
{
    return _hline->pos[0] <= _vline->pos[0] &&
	   _vline->pos[0] < _hline->pos[0] + _hline->len &&
	   _vline->pos[1] <= _hline->pos[1] &&
	   _hline->pos[1] < _vline->pos[1] + _vline->len;
}

/*Finds horizontal clusters that cross corresponding vertical clusters,
   presumably corresponding to a finder center.
  _center:     The buffer in which to store putative finder centers.
  _edge_pts:   The buffer to use for the edge point lists for each finder
                center.
  _hclusters:  The clusters of horizontal lines crossing finder patterns.
  _nhclusters: The number of horizontal line clusters.
  _vclusters:  The clusters of vertical lines crossing finder patterns.
  _nvclusters: The number of vertical line clusters.
  Return: The number of putative finder centers.*/
static int qr_finder_find_crossings(qr_finder_center *_centers,
				    qr_finder_edge_pt *_edge_pts,
				    qr_finder_cluster *_hclusters,
				    int _nhclusters,
				    qr_finder_cluster *_vclusters,
				    int _nvclusters)
{
    qr_finder_cluster **hneighbors;
    qr_finder_cluster **vneighbors;
    unsigned char *hmark;
    unsigned char *vmark;
    int ncenters;
    int i;
    int j;
    hneighbors =
	(qr_finder_cluster **)malloc(_nhclusters * sizeof(*hneighbors));
    vneighbors =
	(qr_finder_cluster **)malloc(_nvclusters * sizeof(*vneighbors));
    hmark    = (unsigned char *)calloc(_nhclusters, sizeof(*hmark));
    vmark    = (unsigned char *)calloc(_nvclusters, sizeof(*vmark));
    ncenters = 0;
    /*TODO: This may need some re-working.
    We should be finding groups of clusters such that _all_ horizontal lines in
     _all_ horizontal clusters in the group cross _all_ vertical lines in _all_
     vertical clusters in the group.
    This is equivalent to finding the maximum bipartite clique in the
     connectivity graph, which requires linear progamming to solve efficiently.
    In principle, that is easy to do, but a realistic implementation without
     floating point is a lot of work (and computationally expensive).
    Right now we are relying on a sufficient border around the finder patterns
     to prevent false positives.*/
    for (i = 0; i < _nhclusters; i++)
	if (!hmark[i]) {
	    qr_finder_line *a;
	    qr_finder_line *b;
	    int nvneighbors;
	    int nedge_pts;
	    int y;
	    a = _hclusters[i].lines[_hclusters[i].nlines >> 1];
	    y = nvneighbors = 0;
	    for (j = 0; j < _nvclusters; j++)
		if (!vmark[j]) {
		    b = _vclusters[j].lines[_vclusters[j].nlines >> 1];
		    if (qr_finder_lines_are_crossing(a, b)) {
			vmark[j] = 1;
			y += (b->pos[1] << 1) + b->len;
			if (b->boffs > 0 && b->eoffs > 0)
			    y += b->eoffs - b->boffs;
			vneighbors[nvneighbors++] = _vclusters + j;
		    }
		}
	    if (nvneighbors > 0) {
		qr_finder_center *c;
		int nhneighbors;
		int x;
		x = (a->pos[0] << 1) + a->len;
		if (a->boffs > 0 && a->eoffs > 0)
		    x += a->eoffs - a->boffs;
		hneighbors[0] = _hclusters + i;
		nhneighbors   = 1;
		j	      = nvneighbors >> 1;
		b = vneighbors[j]->lines[vneighbors[j]->nlines >> 1];
		for (j = i + 1; j < _nhclusters; j++)
		    if (!hmark[j]) {
			a = _hclusters[j].lines[_hclusters[j].nlines >> 1];
			if (qr_finder_lines_are_crossing(a, b)) {
			    hmark[j] = 1;
			    x += (a->pos[0] << 1) + a->len;
			    if (a->boffs > 0 && a->eoffs > 0)
				x += a->eoffs - a->boffs;
			    hneighbors[nhneighbors++] = _hclusters + j;
			}
		    }
		c	     = _centers + ncenters++;
		c->pos[0]    = (x + nhneighbors) / (nhneighbors << 1);
		c->pos[1]    = (y + nvneighbors) / (nvneighbors << 1);
		c->edge_pts  = _edge_pts;
		nedge_pts    = qr_finder_edge_pts_fill(_edge_pts, 0, hneighbors,
						       nhneighbors, 0);
		nedge_pts    = qr_finder_edge_pts_fill(_edge_pts, nedge_pts,
						       vneighbors, nvneighbors, 1);
		c->nedge_pts = nedge_pts;
		_edge_pts += nedge_pts;
	    }
	}
    free(vmark);
    free(hmark);
    free(vneighbors);
    free(hneighbors);
    /*Sort the centers by decreasing numbers of edge points.*/
    qsort(_centers, ncenters, sizeof(*_centers), qr_finder_center_cmp);
    return ncenters;
}

/*Locates a set of putative finder centers in the image.
  First we search for horizontal and vertical lines that have
   (dark:light:dark:light:dark) runs with size ratios of roughly (1:1:3:1:1).
  Then we cluster them into groups such that each subsequent pair of endpoints
   is close to the line before it in the cluster.
  This will locate many line clusters that don't cross a finder pattern, but
   qr_finder_find_crossings() will filter most of them out.
  Where horizontal and vertical clusters cross, a prospective finder center is
   returned.
  _centers:  Returns a pointer to a freshly-allocated list of finder centers.
             This must be freed by the caller.
  _edge_pts: Returns a pointer to a freshly-allocated list of edge points
              around those centers.
             This must be freed by the caller.
  _img:      The binary image to search.
  _width:    The width of the image.
  _height:   The height of the image.
  Return: The number of putative finder centers located.*/
static int qr_finder_centers_locate(qr_finder_center **_centers,
				    qr_finder_edge_pt **_edge_pts,
				    qr_reader *reader, int _width, int _height)
{
    qr_finder_line *hlines = reader->finder_lines[0].lines;
    int nhlines		   = reader->finder_lines[0].nlines;
    qr_finder_line *vlines = reader->finder_lines[1].lines;
    int nvlines		   = reader->finder_lines[1].nlines;

    qr_finder_line **hneighbors;
    qr_finder_cluster *hclusters;
    int nhclusters;
    qr_finder_line **vneighbors;
    qr_finder_cluster *vclusters;
    int nvclusters;
    int ncenters;

    /*Cluster the detected lines.*/
    hneighbors = (qr_finder_line **)malloc(nhlines * sizeof(*hneighbors));
    /*We require more than one line per cluster, so there are at most nhlines/2.*/
    hclusters =
	(qr_finder_cluster *)malloc((nhlines >> 1) * sizeof(*hclusters));
    nhclusters =
	qr_finder_cluster_lines(hclusters, hneighbors, hlines, nhlines, 0);
    /*We need vertical lines to be sorted by X coordinate, with ties broken by Y
     coordinate, for clustering purposes.
    We scan the image in the opposite order for cache efficiency, so sort the
     lines we found here.*/
    qsort(vlines, nvlines, sizeof(*vlines), qr_finder_vline_cmp);
    vneighbors = (qr_finder_line **)malloc(nvlines * sizeof(*vneighbors));
    /*We require more than one line per cluster, so there are at most nvlines/2.*/
    vclusters =
	(qr_finder_cluster *)malloc((nvlines >> 1) * sizeof(*vclusters));
    nvclusters =
	qr_finder_cluster_lines(vclusters, vneighbors, vlines, nvlines, 1);
    /*Find line crossings among the clusters.*/
    if (nhclusters >= 3 && nvclusters >= 3) {
	qr_finder_edge_pt *edge_pts;
	qr_finder_center *centers;
	int nedge_pts;
	int i;
	nedge_pts = 0;
	for (i = 0; i < nhclusters; i++)
	    nedge_pts += hclusters[i].nlines;
	for (i = 0; i < nvclusters; i++)
	    nedge_pts += vclusters[i].nlines;
	nedge_pts <<= 1;
	edge_pts  = (qr_finder_edge_pt *)malloc(nedge_pts * sizeof(*edge_pts));
	centers	  = (qr_finder_center *)malloc(QR_MINI(nhclusters, nvclusters) *
					       sizeof(*centers));
	ncenters  = qr_finder_find_crossings(centers, edge_pts, hclusters,
					     nhclusters, vclusters, nvclusters);
	*_centers = centers;
	*_edge_pts = edge_pts;
    } else
	ncenters = 0;
    free(vclusters);
    free(vneighbors);
    free(hclusters);
    free(hneighbors);
    return ncenters;
}

static void qr_point_translate(qr_point _point, int _dx, int _dy)
{
    _point[0] += _dx;
    _point[1] += _dy;
}

static unsigned qr_point_distance2(const qr_point _p1, const qr_point _p2)
{
    return (_p1[0] - _p2[0]) * (_p1[0] - _p2[0]) +
	   (_p1[1] - _p2[1]) * (_p1[1] - _p2[1]);
}

/*Returns the cross product of the three points, which is positive if they are
   in CCW order (in a right-handed coordinate system), and 0 if they're
   colinear.*/
static int qr_point_ccw(const qr_point _p0, const qr_point _p1,
			const qr_point _p2)
{
    return (_p1[0] - _p0[0]) * (_p2[1] - _p0[1]) -
	   (_p1[1] - _p0[1]) * (_p2[0] - _p0[0]);
}

/*Evaluates a line equation at a point.
  _line: The line to evaluate.
  _x:    The X coordinate of the point.
  _y:    The y coordinate of the point.
  Return: The value of the line equation _line[0]*_x+_line[1]*_y+_line[2].*/
static int qr_line_eval(qr_line _line, int _x, int _y)
{
    return _line[0] * _x + _line[1] * _y + _line[2];
}

/*Computes a line passing through the given point using the specified second
   order statistics.
  Given a line defined by the equation
    A*x+B*y+C = 0 ,
   the least squares fit to n points (x_i,y_i) must satisfy the two equations
    A^2 + (Syy - Sxx)/Sxy*A*B - B^2 = 0 ,
    C = -(xbar*A+ybar*B) ,
   where
    xbar = sum(x_i)/n ,
    ybar = sum(y_i)/n ,
    Sxx = sum((x_i-xbar)**2) ,
    Sxy = sum((x_i-xbar)*(y_i-ybar)) ,
    Syy = sum((y_i-ybar)**2) .
  The quadratic can be solved for the ratio (A/B) or (B/A):
    A/B = (Syy + sqrt((Sxx-Syy)**2 + 4*Sxy**2) - Sxx)/(-2*Sxy) ,
    B/A = (Sxx + sqrt((Sxx-Syy)**2 + 4*Sxy**2) - Syy)/(-2*Sxy) .
  We pick the one that leads to the larger ratio to avoid destructive
   cancellation (and e.g., 0/0 for horizontal or vertical lines).
  The above solutions correspond to the actual minimum.
  The other solution of the quadratic corresponds to a saddle point of the
   least squares objective function.
  _l:   Returns the fitted line values A, B, and C.
  _x0:  The X coordinate of the point the line is supposed to pass through.
  _y0:  The Y coordinate of the point the line is supposed to pass through.
  _sxx: The sum Sxx.
  _sxy: The sum Sxy.
  _syy: The sum Syy.
  _res: The maximum number of bits occupied by the product of any two of
         _l[0] or _l[1].
        Smaller numbers give less angular resolution, but allow more overhead
         room for computations.*/
static void qr_line_fit(qr_line _l, int _x0, int _y0, int _sxx, int _sxy,
			int _syy, int _res)
{
    int dshift;
    int dround;
    int u;
    int v;
    int w;
    u = abs(_sxx - _syy);
    v = -_sxy << 1;
    w = qr_ihypot(u, v);
    /*Computations in later stages can easily overflow with moderate sizes, so we
     compute a shift factor to scale things down into a manageable range.
    We ensure that the product of any two of _l[0] and _l[1] fits within _res
     bits, which allows computation of line intersections without overflow.*/
    dshift =
	QR_MAXI(0, QR_MAXI(qr_ilog(u), qr_ilog(abs(v))) + 1 - (_res + 1 >> 1));
    dround = (1 << dshift) >> 1;
    if (_sxx > _syy) {
	_l[0] = v + dround >> dshift;
	_l[1] = u + w + dround >> dshift;
    } else {
	_l[0] = u + w + dround >> dshift;
	_l[1] = v + dround >> dshift;
    }
    _l[2] = -(_x0 * _l[0] + _y0 * _l[1]);
}

/*Perform a least-squares line fit to a list of points.
  At least two points are required.*/
static void qr_line_fit_points(qr_line _l, qr_point *_p, int _np, int _res)
{
    int sx;
    int sy;
    int xmin;
    int xmax;
    int ymin;
    int ymax;
    int xbar;
    int ybar;
    int dx;
    int dy;
    int sxx;
    int sxy;
    int syy;
    int sshift;
    int sround;
    int i;
    sx = sy = 0;
    ymax = xmax = INT_MIN;
    ymin = xmin = INT_MAX;
    for (i = 0; i < _np; i++) {
	sx += _p[i][0];
	xmin = QR_MINI(xmin, _p[i][0]);
	xmax = QR_MAXI(xmax, _p[i][0]);
	sy += _p[i][1];
	ymin = QR_MINI(ymin, _p[i][1]);
	ymax = QR_MAXI(ymax, _p[i][1]);
    }
    xbar = (sx + (_np >> 1)) / _np;
    ybar = (sy + (_np >> 1)) / _np;
    sshift =
	QR_MAXI(0, qr_ilog(_np * QR_MAXI(QR_MAXI(xmax - xbar, xbar - xmin),
					 QR_MAXI(ymax - ybar, ybar - ymin))) -
		       (QR_INT_BITS - 1 >> 1));
    sround = (1 << sshift) >> 1;
    sxx = sxy = syy = 0;
    for (i = 0; i < _np; i++) {
	dx = _p[i][0] - xbar + sround >> sshift;
	dy = _p[i][1] - ybar + sround >> sshift;
	sxx += dx * dx;
	sxy += dx * dy;
	syy += dy * dy;
    }
    qr_line_fit(_l, xbar, ybar, sxx, sxy, syy, _res);
}

static void qr_line_orient(qr_line _l, int _x, int _y)
{
    if (qr_line_eval(_l, _x, _y) < 0) {
	_l[0] = -_l[0];
	_l[1] = -_l[1];
	_l[2] = -_l[2];
    }
}

static int qr_line_isect(qr_point _p, const qr_line _l0, const qr_line _l1)
{
    int d;
    int x;
    int y;
    d = _l0[0] * _l1[1] - _l0[1] * _l1[0];
    if (d == 0)
	return -1;
    x = _l0[1] * _l1[2] - _l1[1] * _l0[2];
    y = _l1[0] * _l0[2] - _l0[0] * _l1[2];
    if (d < 0) {
	x = -x;
	y = -y;
	d = -d;
    }
    _p[0] = QR_DIVROUND(x, d);
    _p[1] = QR_DIVROUND(y, d);
    return 0;
}

/*An affine homography.
  This maps from the image (at subpel resolution) to a square domain with
   power-of-two sides (of res bits) and back.*/
struct qr_aff {
    int fwd[2][2];
    int inv[2][2];
    int x0;
    int y0;
    int res;
    int ires;
};

static void qr_aff_init(qr_aff *_aff, const qr_point _p0, const qr_point _p1,
			const qr_point _p2, int _res)
{
    int det;
    int ires;
    int dx1;
    int dy1;
    int dx2;
    int dy2;
    /*det is ensured to be positive by our caller.*/
    dx1		    = _p1[0] - _p0[0];
    dx2		    = _p2[0] - _p0[0];
    dy1		    = _p1[1] - _p0[1];
    dy2		    = _p2[1] - _p0[1];
    det		    = dx1 * dy2 - dy1 * dx2;
    ires	    = QR_MAXI((qr_ilog(abs(det)) >> 1) - 2, 0);
    _aff->fwd[0][0] = dx1;
    _aff->fwd[0][1] = dx2;
    _aff->fwd[1][0] = dy1;
    _aff->fwd[1][1] = dy2;
    _aff->inv[0][0] = QR_DIVROUND(dy2 << _res, det >> ires);
    _aff->inv[0][1] = QR_DIVROUND(-dx2 << _res, det >> ires);
    _aff->inv[1][0] = QR_DIVROUND(-dy1 << _res, det >> ires);
    _aff->inv[1][1] = QR_DIVROUND(dx1 << _res, det >> ires);
    _aff->x0	    = _p0[0];
    _aff->y0	    = _p0[1];
    _aff->res	    = _res;
    _aff->ires	    = ires;
}

/*Map from the image (at subpel resolution) into the square domain.*/
static void qr_aff_unproject(qr_point _q, const qr_aff *_aff, int _x, int _y)
{
    _q[0] = _aff->inv[0][0] * (_x - _aff->x0) +
		_aff->inv[0][1] * (_y - _aff->y0) + (1 << _aff->ires >> 1) >>
	    _aff->ires;
    _q[1] = _aff->inv[1][0] * (_x - _aff->x0) +
		_aff->inv[1][1] * (_y - _aff->y0) + (1 << _aff->ires >> 1) >>
	    _aff->ires;
}

/*Map from the square domain into the image (at subpel resolution).*/
static void qr_aff_project(qr_point _p, const qr_aff *_aff, int _u, int _v)
{
    _p[0] =
	(_aff->fwd[0][0] * _u + _aff->fwd[0][1] * _v + (1 << _aff->res - 1) >>
	 _aff->res) +
	_aff->x0;
    _p[1] =
	(_aff->fwd[1][0] * _u + _aff->fwd[1][1] * _v + (1 << _aff->res - 1) >>
	 _aff->res) +
	_aff->y0;
}

/*A full homography.
  Like the affine homography, this maps from the image (at subpel resolution)
   to a square domain with power-of-two sides (of res bits) and back.*/
struct qr_hom {
    int fwd[3][2];
    int inv[3][2];
    int fwd22;
    int inv22;
    int x0;
    int y0;
    int res;
};

static void qr_hom_init(qr_hom *_hom, int _x0, int _y0, int _x1, int _y1,
			int _x2, int _y2, int _x3, int _y3, int _res)
{
    int dx10;
    int dx20;
    int dx30;
    int dx31;
    int dx32;
    int dy10;
    int dy20;
    int dy30;
    int dy31;
    int dy32;
    int a20;
    int a21;
    int a22;
    int b0;
    int b1;
    int b2;
    int s1;
    int s2;
    int r1;
    int r2;
    dx10 = _x1 - _x0;
    dx20 = _x2 - _x0;
    dx30 = _x3 - _x0;
    dx31 = _x3 - _x1;
    dx32 = _x3 - _x2;
    dy10 = _y1 - _y0;
    dy20 = _y2 - _y0;
    dy30 = _y3 - _y0;
    dy31 = _y3 - _y1;
    dy32 = _y3 - _y2;
    a20	 = dx32 * dy10 - dx10 * dy32;
    a21	 = dx20 * dy31 - dx31 * dy20;
    a22	 = dx32 * dy31 - dx31 * dy32;
    /*Figure out if we need to downscale anything.*/
    b0 = qr_ilog(QR_MAXI(abs(dx10), abs(dy10))) + qr_ilog(abs(a20 + a22));
    b1 = qr_ilog(QR_MAXI(abs(dx20), abs(dy20))) + qr_ilog(abs(a21 + a22));
    b2 = qr_ilog(QR_MAXI(QR_MAXI(abs(a20), abs(a21)), abs(a22)));
    s1 = QR_MAXI(0, _res + QR_MAXI(QR_MAXI(b0, b1), b2) - (QR_INT_BITS - 2));
    r1 = (1 << s1) >> 1;
    /*Compute the final coefficients of the forward transform.
    The 32x32->64 bit multiplies are really needed for accuracy with large
     versions.*/
    _hom->fwd[0][0] = QR_FIXMUL(dx10, a20 + a22, r1, s1);
    _hom->fwd[0][1] = QR_FIXMUL(dx20, a21 + a22, r1, s1);
    _hom->x0	    = _x0;
    _hom->fwd[1][0] = QR_FIXMUL(dy10, a20 + a22, r1, s1);
    _hom->fwd[1][1] = QR_FIXMUL(dy20, a21 + a22, r1, s1);
    _hom->y0	    = _y0;
    _hom->fwd[2][0] = a20 + r1 >> s1;
    _hom->fwd[2][1] = a21 + r1 >> s1;
    _hom->fwd22	    = s1 > _res ? a22 + (r1 >> _res) >> s1 - _res :
					a22 << _res - s1;
    /*Now compute the inverse transform.*/
    b0 = qr_ilog(QR_MAXI(QR_MAXI(abs(dx10), abs(dx20)), abs(dx30))) +
	 qr_ilog(QR_MAXI(abs(_hom->fwd[0][0]), abs(_hom->fwd[1][0])));
    b1 = qr_ilog(QR_MAXI(QR_MAXI(abs(dy10), abs(dy20)), abs(dy30))) +
	 qr_ilog(QR_MAXI(abs(_hom->fwd[0][1]), abs(_hom->fwd[1][1])));
    b2 = qr_ilog(abs(a22)) - s1;
    s2 = QR_MAXI(0, QR_MAXI(b0, b1) + b2 - (QR_INT_BITS - 3));
    r2 = (1 << s2) >> 1;
    s1 += s2;
    r1 <<= s2;
    /*The 32x32->64 bit multiplies are really needed for accuracy with large
     versions.*/
    _hom->inv[0][0] = QR_FIXMUL(_hom->fwd[1][1], a22, r1, s1);
    _hom->inv[0][1] = QR_FIXMUL(-_hom->fwd[0][1], a22, r1, s1);
    _hom->inv[1][0] = QR_FIXMUL(-_hom->fwd[1][0], a22, r1, s1);
    _hom->inv[1][1] = QR_FIXMUL(_hom->fwd[0][0], a22, r1, s1);
    _hom->inv[2][0] =
	QR_FIXMUL(_hom->fwd[1][0], _hom->fwd[2][1],
		  -QR_EXTMUL(_hom->fwd[1][1], _hom->fwd[2][0], r2), s2);
    _hom->inv[2][1] =
	QR_FIXMUL(_hom->fwd[0][1], _hom->fwd[2][0],
		  -QR_EXTMUL(_hom->fwd[0][0], _hom->fwd[2][1], r2), s2);
    _hom->inv22 = QR_FIXMUL(_hom->fwd[0][0], _hom->fwd[1][1],
			    -QR_EXTMUL(_hom->fwd[0][1], _hom->fwd[1][0], r2),
			    s2);
    _hom->res	= _res;
}

/*Map from the image (at subpel resolution) into the square domain.
  Returns a negative value if the point went to infinity.*/
static int qr_hom_unproject(qr_point _q, const qr_hom *_hom, int _x, int _y)
{
    int x;
    int y;
    int w;
    _x -= _hom->x0;
    _y -= _hom->y0;
    x = _hom->inv[0][0] * _x + _hom->inv[0][1] * _y;
    y = _hom->inv[1][0] * _x + _hom->inv[1][1] * _y;
    w = _hom->inv[2][0] * _x + _hom->inv[2][1] * _y + _hom->inv22 +
	    (1 << _hom->res - 1) >>
	_hom->res;
    if (w == 0) {
	_q[0] = x < 0 ? INT_MIN : INT_MAX;
	_q[1] = y < 0 ? INT_MIN : INT_MAX;
	return -1;
    } else {
	if (w < 0) {
	    x = -x;
	    y = -y;
	    w = -w;
	}
	_q[0] = QR_DIVROUND(x, w);
	_q[1] = QR_DIVROUND(y, w);
    }
    return 0;
}

/*Finish a partial projection, converting from homogeneous coordinates to the
   normal 2-D representation.
  In loops, we can avoid many multiplies by computing the homogeneous _x, _y,
   and _w incrementally, but we cannot avoid the divisions, done here.*/
static void qr_hom_fproject(qr_point _p, const qr_hom *_hom, int _x, int _y,
			    int _w)
{
    if (_w == 0) {
	_p[0] = _x < 0 ? INT_MIN : INT_MAX;
	_p[1] = _y < 0 ? INT_MIN : INT_MAX;
    } else {
	if (_w < 0) {
	    _x = -_x;
	    _y = -_y;
	    _w = -_w;
	}
	_p[0] = QR_DIVROUND(_x, _w) + _hom->x0;
	_p[1] = QR_DIVROUND(_y, _w) + _hom->y0;
    }
}

#if defined(QR_DEBUG)
/*Map from the square domain into the image (at subpel resolution).
  Currently only used directly by debug code.*/
static void qr_hom_project(qr_point _p, const qr_hom *_hom, int _u, int _v)
{
    qr_hom_fproject(_p, _hom, _hom->fwd[0][0] * _u + _hom->fwd[0][1] * _v,
		    _hom->fwd[1][0] * _u + _hom->fwd[1][1] * _v,
		    _hom->fwd[2][0] * _u + _hom->fwd[2][1] * _v + _hom->fwd22);
}
#endif

/*All the information we've collected about a finder pattern in the current
   configuration.*/
struct qr_finder {
    /*The module size along each axis (in the square domain).*/
    int size[2];
    /*The version estimated from the module size along each axis.*/
    int eversion[2];
    /*The list of classified edge points for each edge.*/
    qr_finder_edge_pt *edge_pts[4];
    /*The number of edge points classified as belonging to each edge.*/
    int nedge_pts[4];
    /*The number of inliers found after running RANSAC on each edge.*/
    int ninliers[4];
    /*The center of the finder pattern (in the square domain).*/
    qr_point o;
    /*The finder center information from the original image.*/
    qr_finder_center *c;
};

static int qr_cmp_edge_pt(const void *_a, const void *_b)
{
    const qr_finder_edge_pt *a;
    const qr_finder_edge_pt *b;
    a = (const qr_finder_edge_pt *)_a;
    b = (const qr_finder_edge_pt *)_b;
    return ((a->edge > b->edge) - (a->edge < b->edge) << 1) +
	   (a->extent > b->extent) - (a->extent < b->extent);
}

/*Computes the index of the edge each edge point belongs to, and its (signed)
   distance along the corresponding axis from the center of the finder pattern
   (in the square domain).
  The resulting list of edge points is sorted by edge index, with ties broken
   by extent.*/
static void qr_finder_edge_pts_aff_classify(qr_finder *_f, const qr_aff *_aff)
{
    qr_finder_center *c;
    int i;
    int e;
    c = _f->c;
    for (e = 0; e < 4; e++)
	_f->nedge_pts[e] = 0;
    for (i = 0; i < c->nedge_pts; i++) {
	qr_point q;
	int d;
	qr_aff_unproject(q, _aff, c->edge_pts[i].pos[0], c->edge_pts[i].pos[1]);
	qr_point_translate(q, -_f->o[0], -_f->o[1]);
	d = abs(q[1]) > abs(q[0]);
	e = d << 1 | (q[d] >= 0);
	_f->nedge_pts[e]++;
	c->edge_pts[i].edge   = e;
	c->edge_pts[i].extent = q[d];
    }
    qsort(c->edge_pts, c->nedge_pts, sizeof(*c->edge_pts), qr_cmp_edge_pt);
    _f->edge_pts[0] = c->edge_pts;
    for (e = 1; e < 4; e++)
	_f->edge_pts[e] = _f->edge_pts[e - 1] + _f->nedge_pts[e - 1];
}

/*Computes the index of the edge each edge point belongs to, and its (signed)
   distance along the corresponding axis from the center of the finder pattern
   (in the square domain).
  The resulting list of edge points is sorted by edge index, with ties broken
   by extent.*/
static void qr_finder_edge_pts_hom_classify(qr_finder *_f, const qr_hom *_hom)
{
    qr_finder_center *c;
    int i;
    int e;
    c = _f->c;
    for (e = 0; e < 4; e++)
	_f->nedge_pts[e] = 0;
    for (i = 0; i < c->nedge_pts; i++) {
	qr_point q;
	int d;
	if (qr_hom_unproject(q, _hom, c->edge_pts[i].pos[0],
			     c->edge_pts[i].pos[1]) >= 0) {
	    qr_point_translate(q, -_f->o[0], -_f->o[1]);
	    d = abs(q[1]) > abs(q[0]);
	    e = d << 1 | (q[d] >= 0);
	    _f->nedge_pts[e]++;
	    c->edge_pts[i].edge	  = e;
	    c->edge_pts[i].extent = q[d];
	} else {
	    c->edge_pts[i].edge	  = 4;
	    c->edge_pts[i].extent = q[0];
	}
    }
    qsort(c->edge_pts, c->nedge_pts, sizeof(*c->edge_pts), qr_cmp_edge_pt);
    _f->edge_pts[0] = c->edge_pts;
    for (e = 1; e < 4; e++)
	_f->edge_pts[e] = _f->edge_pts[e - 1] + _f->nedge_pts[e - 1];
}

/*TODO: Perhaps these thresholds should be on the module size instead?
  Unfortunately, I'd need real-world images of codes with larger versions to
   see if these thresholds are still effective, but such versions aren't used
   often.*/

/*The amount that the estimated version numbers are allowed to differ from the
   real version number and still be considered valid.*/
#define QR_SMALL_VERSION_SLACK (1)
/*Since cell phone cameras can have severe radial distortion, the estimated
   version for larger versions can be off by larger amounts.*/
#define QR_LARGE_VERSION_SLACK (3)

/*Estimates the size of a module after classifying the edge points.
  _width:  The distance between UL and UR in the square domain.
  _height: The distance between UL and DL in the square domain.*/
static int qr_finder_estimate_module_size_and_version(qr_finder *_f, int _width,
						      int _height)
{
    qr_point offs;
    int sums[4];
    int nsums[4];
    int usize;
    int nusize;
    int vsize;
    int nvsize;
    int uversion;
    int vversion;
    int e;
    offs[0] = offs[1] = 0;
    for (e = 0; e < 4; e++)
	if (_f->nedge_pts[e] > 0) {
	    qr_finder_edge_pt *edge_pts;
	    int sum;
	    int mean;
	    int n;
	    int i;
	    /*Average the samples for this edge, dropping the top and bottom 25%.*/
	    edge_pts = _f->edge_pts[e];
	    n	     = _f->nedge_pts[e];
	    sum	     = 0;
	    for (i = (n >> 2); i < n - (n >> 2); i++)
		sum += edge_pts[i].extent;
	    n	 = n - ((n >> 2) << 1);
	    mean = QR_DIVROUND(sum, n);
	    offs[e >> 1] += mean;
	    sums[e]  = sum;
	    nsums[e] = n;
	} else
	    nsums[e] = sums[e] = 0;
    /*If we have samples on both sides of an axis, refine our idea of where the
     unprojected finder center is located.*/
    if (_f->nedge_pts[0] > 0 && _f->nedge_pts[1] > 0) {
	_f->o[0] -= offs[0] >> 1;
	sums[0] -= offs[0] * nsums[0] >> 1;
	sums[1] -= offs[0] * nsums[1] >> 1;
    }
    if (_f->nedge_pts[2] > 0 && _f->nedge_pts[3] > 0) {
	_f->o[1] -= offs[1] >> 1;
	sums[2] -= offs[1] * nsums[2] >> 1;
	sums[3] -= offs[1] * nsums[3] >> 1;
    }
    /*We must have _some_ samples along each axis... if we don't, our transform
     must be pretty severely distorting the original square (e.g., with
     coordinates so large as to cause overflow).*/
    nusize = nsums[0] + nsums[1];
    if (nusize <= 0)
	return -1;
    /*The module size is 1/3 the average edge extent.*/
    nusize *= 3;
    usize = sums[1] - sums[0];
    usize = ((usize << 1) + nusize) / (nusize << 1);
    if (usize <= 0)
	return -1;
    /*Now estimate the version directly from the module size and the distance
     between the finder patterns.
    This is done independently using the extents along each axis.
    If either falls significantly outside the valid range (1 to 40), reject the
     configuration.*/
    uversion = (_width - 8 * usize) / (usize << 2);
    if (uversion < 1 || uversion > 40 + QR_LARGE_VERSION_SLACK)
	return -1;
    /*Now do the same for the other axis.*/
    nvsize = nsums[2] + nsums[3];
    if (nvsize <= 0)
	return -1;
    nvsize *= 3;
    vsize = sums[3] - sums[2];
    vsize = ((vsize << 1) + nvsize) / (nvsize << 1);
    if (vsize <= 0)
	return -1;
    vversion = (_height - 8 * vsize) / (vsize << 2);
    if (vversion < 1 || vversion > 40 + QR_LARGE_VERSION_SLACK)
	return -1;
    /*If the estimated version using extents along one axis is significantly
     different than the estimated version along the other axis, then the axes
     have significantly different scalings (relative to the grid).
    This can happen, e.g., when we have multiple adjacent QR codes, and we've
     picked two finder patterns from one and the third finder pattern from
     another, e.g.:
      X---DL UL---X
      |....   |....
      X....  UR....
    Such a configuration might even pass any other geometric checks if we
     didn't reject it here.*/
    if (abs(uversion - vversion) > QR_LARGE_VERSION_SLACK)
	return -1;
    _f->size[0] = usize;
    _f->size[1] = vsize;
    /*We intentionally do not compute an average version from the sizes along
     both axes.
    In the presence of projective distortion, one of them will be much more
     accurate than the other.*/
    _f->eversion[0] = uversion;
    _f->eversion[1] = vversion;
    return 0;
}

/*Eliminate outliers from the classified edge points with RANSAC.*/
static void qr_finder_ransac(qr_finder *_f, const qr_aff *_hom,
			     isaac_ctx *_isaac, int _e)
{
    qr_finder_edge_pt *edge_pts;
    int best_ninliers;
    int n;
    edge_pts	  = _f->edge_pts[_e];
    n		  = _f->nedge_pts[_e];
    best_ninliers = 0;
    if (n > 1) {
	int max_iters;
	int i;
	int j;
	/*17 iterations is enough to guarantee an outlier-free sample with more
       than 99% probability given as many as 50% outliers.*/
	max_iters = 17;
	for (i = 0; i < max_iters; i++) {
	    qr_point q0;
	    qr_point q1;
	    int ninliers;
	    int thresh;
	    int p0i;
	    int p1i;
	    int *p0;
	    int *p1;
	    int j;
	    /*Pick two random points on this edge.*/
	    p0i = isaac_next_uint(_isaac, n);
	    p1i = isaac_next_uint(_isaac, n - 1);
	    if (p1i >= p0i)
		p1i++;
	    p0 = edge_pts[p0i].pos;
	    p1 = edge_pts[p1i].pos;
	    /*If the corresponding line is not within 45 degrees of the proper
         orientation in the square domain, reject it outright.
        This can happen, e.g., when highly skewed orientations cause points to
         be misclassified into the wrong edge.
        The irony is that using such points might produce a line which _does_
         pass the corresponding validity checks.*/
	    qr_aff_unproject(q0, _hom, p0[0], p0[1]);
	    qr_aff_unproject(q1, _hom, p1[0], p1[1]);
	    qr_point_translate(q0, -_f->o[0], -_f->o[1]);
	    qr_point_translate(q1, -_f->o[0], -_f->o[1]);
	    if (abs(q0[_e >> 1] - q1[_e >> 1]) >
		abs(q0[1 - (_e >> 1)] - q1[1 - (_e >> 1)]))
		continue;
	    /*Identify the other edge points which are inliers.
        The squared distance should be distributed as a \Chi^2 distribution
         with one degree of freedom, which means for a 95% confidence the
         point should lie within a factor 3.8414588 ~= 4 times the expected
         variance of the point locations.
        We grossly approximate the standard deviation as 1 pixel in one
         direction, and 0.5 pixels in the other (because we average two
         coordinates).*/
	    thresh   = qr_isqrt(qr_point_distance2(p0, p1)
				<< 2 * QR_FINDER_SUBPREC + 1);
	    ninliers = 0;
	    for (j = 0; j < n; j++) {
		if (abs(qr_point_ccw(p0, p1, edge_pts[j].pos)) <= thresh) {
		    edge_pts[j].extent |= 1;
		    ninliers++;
		} else
		    edge_pts[j].extent &= ~1;
	    }
	    if (ninliers > best_ninliers) {
		for (j = 0; j < n; j++)
		    edge_pts[j].extent <<= 1;
		best_ninliers = ninliers;
		/*The actual number of iterations required is
            log(1-\alpha)/log(1-r*r),
           where \alpha is the required probability of taking a sample with
            no outliers (e.g., 0.99) and r is the estimated ratio of inliers
            (e.g. ninliers/n).
          This is just a rough (but conservative) approximation, but it
           should be good enough to stop the iteration early when we find
           a good set of inliers.*/
		if (ninliers > n >> 1)
		    max_iters = (67 * n - 63 * ninliers - 1) / (n << 1);
	    }
	}
	/*Now collect all the inliers at the beginning of the list.*/
	for (i = j = 0; j < best_ninliers; i++)
	    if (edge_pts[i].extent & 2) {
		if (j < i) {
		    qr_finder_edge_pt tmp;
		    *&tmp	    = *(edge_pts + i);
		    *(edge_pts + j) = *(edge_pts + i);
		    *(edge_pts + i) = *&tmp;
		}
		j++;
	    }
    }
    _f->ninliers[_e] = best_ninliers;
}

/*Perform a least-squares line fit to an edge of a finder pattern using the
   inliers found by RANSAC.*/
static int qr_line_fit_finder_edge(qr_line _l, const qr_finder *_f, int _e,
				   int _res)
{
    qr_finder_edge_pt *edge_pts;
    qr_point *pts;
    int npts;
    int i;
    npts = _f->ninliers[_e];
    if (npts < 2)
	return -1;
    /*We could write a custom version of qr_line_fit_points that accesses
     edge_pts directly, but this saves on code size and doesn't measurably slow
     things down.*/
    pts	     = (qr_point *)malloc(npts * sizeof(*pts));
    edge_pts = _f->edge_pts[_e];
    for (i = 0; i < npts; i++) {
	pts[i][0] = edge_pts[i].pos[0];
	pts[i][1] = edge_pts[i].pos[1];
    }
    qr_line_fit_points(_l, pts, npts, _res);
    /*Make sure the center of the finder pattern lies in the positive halfspace
     of the line.*/
    qr_line_orient(_l, _f->c->pos[0], _f->c->pos[1]);
    free(pts);
    return 0;
}

/*Perform a least-squares line fit to a pair of common finder edges using the
   inliers found by RANSAC.
  Unlike a normal edge fit, we guarantee that this one succeeds by creating at
   least one point on each edge using the estimated module size if it has no
   inliers.*/
static void qr_line_fit_finder_pair(qr_line _l, const qr_aff *_aff,
				    const qr_finder *_f0, const qr_finder *_f1,
				    int _e)
{
    qr_point *pts;
    int npts;
    qr_finder_edge_pt *edge_pts;
    qr_point q;
    int n0;
    int n1;
    int i;
    n0 = _f0->ninliers[_e];
    n1 = _f1->ninliers[_e];
    /*We could write a custom version of qr_line_fit_points that accesses
     edge_pts directly, but this saves on code size and doesn't measurably slow
     things down.*/
    npts = QR_MAXI(n0, 1) + QR_MAXI(n1, 1);
    pts	 = (qr_point *)malloc(npts * sizeof(*pts));
    if (n0 > 0) {
	edge_pts = _f0->edge_pts[_e];
	for (i = 0; i < n0; i++) {
	    pts[i][0] = edge_pts[i].pos[0];
	    pts[i][1] = edge_pts[i].pos[1];
	}
    } else {
	q[0] = _f0->o[0];
	q[1] = _f0->o[1];
	q[_e >> 1] += _f0->size[_e >> 1] * (2 * (_e & 1) - 1);
	qr_aff_project(pts[0], _aff, q[0], q[1]);
	n0++;
    }
    if (n1 > 0) {
	edge_pts = _f1->edge_pts[_e];
	for (i = 0; i < n1; i++) {
	    pts[n0 + i][0] = edge_pts[i].pos[0];
	    pts[n0 + i][1] = edge_pts[i].pos[1];
	}
    } else {
	q[0] = _f1->o[0];
	q[1] = _f1->o[1];
	q[_e >> 1] += _f1->size[_e >> 1] * (2 * (_e & 1) - 1);
	qr_aff_project(pts[n0], _aff, q[0], q[1]);
	n1++;
    }
    qr_line_fit_points(_l, pts, npts, _aff->res);
    /*Make sure at least one finder center lies in the positive halfspace.*/
    qr_line_orient(_l, _f0->c->pos[0], _f0->c->pos[1]);
    free(pts);
}

static int qr_finder_quick_crossing_check(const unsigned char *_img, int _width,
					  int _height, int _x0, int _y0,
					  int _x1, int _y1, int _v)
{
    /*The points must be inside the image, and have a !_v:_v:!_v pattern.
    We don't scan the whole line initially, but quickly reject if the endpoints
     aren't !_v, or the midpoint isn't _v.
    If either end point is out of the image, or we don't encounter a _v pixel,
     we return a negative value, indicating the region should be considered
     empty.
    Otherwise, we return a positive value to indicate it is non-empty.*/
    if (_x0 < 0 || _x0 >= _width || _y0 < 0 || _y0 >= _height || _x1 < 0 ||
	_x1 >= _width || _y1 < 0 || _y1 >= _height) {
	return -1;
    }
    if ((!_img[_y0 * _width + _x0]) != _v || (!_img[_y1 * _width + _x1]) != _v)
	return 1;
    if ((!_img[(_y0 + _y1 >> 1) * _width + (_x0 + _x1 >> 1)]) == _v)
	return -1;
    return 0;
}

/*Locate the midpoint of a _v segment along a !_v:_v:!_v line from (_x0,_y0) to
   (_x1,_y1).
  All coordinates, which are NOT in subpel resolution, must lie inside the
   image, and the endpoints are already assumed to have the value !_v.
  The returned value is in subpel resolution.*/
static int qr_finder_locate_crossing(const unsigned char *_img, int _width,
				     int _height, int _x0, int _y0, int _x1,
				     int _y1, int _v, qr_point _p)
{
    qr_point x0;
    qr_point x1;
    qr_point dx;
    int step[2];
    int steep;
    int err;
    int derr;
    /*Use Bresenham's algorithm to trace along the line and find the exact
     transitions from !_v to _v and back.*/
    x0[0]   = _x0;
    x0[1]   = _y0;
    x1[0]   = _x1;
    x1[1]   = _y1;
    dx[0]   = abs(_x1 - _x0);
    dx[1]   = abs(_y1 - _y0);
    steep   = dx[1] > dx[0];
    err	    = 0;
    derr    = dx[1 - steep];
    step[0] = ((_x0 < _x1) << 1) - 1;
    step[1] = ((_y0 < _y1) << 1) - 1;
    /*Find the first crossing from !_v to _v.*/
    for (;;) {
	/*If we make it all the way to the other side, there's no crossing.*/
	if (x0[steep] == x1[steep])
	    return -1;
	x0[steep] += step[steep];
	err += derr;
	if (err << 1 > dx[steep]) {
	    x0[1 - steep] += step[1 - steep];
	    err -= dx[steep];
	}
	if ((!_img[x0[1] * _width + x0[0]]) != _v)
	    break;
    }
    /*Find the last crossing from _v to !_v.*/
    err = 0;
    for (;;) {
	if (x0[steep] == x1[steep])
	    break;
	x1[steep] -= step[steep];
	err += derr;
	if (err << 1 > dx[steep]) {
	    x1[1 - steep] -= step[1 - steep];
	    err -= dx[steep];
	}
	if ((!_img[x1[1] * _width + x1[0]]) != _v)
	    break;
    }
    /*Return the midpoint of the _v segment.*/
    _p[0] = (x0[0] + x1[0] + 1 << QR_FINDER_SUBPREC) >> 1;
    _p[1] = (x0[1] + x1[1] + 1 << QR_FINDER_SUBPREC) >> 1;
    return 0;
}

static int qr_aff_line_step(const qr_aff *_aff, qr_line _l, int _v, int _du,
			    int *_dv)
{
    int shift;
    int round;
    int dv;
    int n;
    int d;
    n = _aff->fwd[0][_v] * _l[0] + _aff->fwd[1][_v] * _l[1];
    d = _aff->fwd[0][1 - _v] * _l[0] + _aff->fwd[1][1 - _v] * _l[1];
    if (d < 0) {
	n = -n;
	d = -d;
    }
    shift = QR_MAXI(0, qr_ilog(_du) + qr_ilog(abs(n)) + 3 - QR_INT_BITS);
    round = (1 << shift) >> 1;
    n	  = n + round >> shift;
    d	  = d + round >> shift;
    /*The line should not be outside 45 degrees of horizontal/vertical.
    TODO: We impose this restriction to help ensure the loop below terminates,
     but it should not technically be required.
    It also, however, ensures we avoid division by zero.*/
    if (abs(n) >= d)
	return -1;
    n  = -_du * n;
    dv = QR_DIVROUND(n, d);
    if (abs(dv) >= _du)
	return -1;
    *_dv = dv;
    return 0;
}

/*Computes the Hamming distance between two bit patterns (the number of bits
   that differ).
  May stop counting after _maxdiff differences.*/
static int qr_hamming_dist(unsigned _y1, unsigned _y2, int _maxdiff)
{
    unsigned y;
    int ret;
    y = _y1 ^ _y2;
    for (ret = 0; ret < _maxdiff && y; ret++)
	y &= y - 1;
    return ret;
}

/*Retrieve a bit (guaranteed to be 0 or 1) from the image, given coordinates in
   subpel resolution which have not been bounds checked.*/
static int qr_img_get_bit(const unsigned char *_img, int _width, int _height,
			  int _x, int _y)
{
    _x >>= QR_FINDER_SUBPREC;
    _y >>= QR_FINDER_SUBPREC;
    return _img[QR_CLAMPI(0, _y, _height - 1) * _width +
		QR_CLAMPI(0, _x, _width - 1)] != 0;
}

#if defined(QR_DEBUG)
#include "image.h"

static void qr_finder_dump_aff_undistorted(qr_finder *_ul, qr_finder *_ur,
					   qr_finder *_dl, qr_aff *_aff,
					   const unsigned char *_img,
					   int _width, int _height)
{
    unsigned char *gimg;
    FILE *fout;
    int lpsz;
    int pixel_size;
    int dim;
    int min;
    int max;
    int u;
    int y;
    int i;
    int j;
    lpsz =
	qr_ilog(_ur->size[0] + _ur->size[1] + _dl->size[0] + _dl->size[1]) - 6;
    pixel_size = 1 << lpsz;
    dim	       = (1 << _aff->res - lpsz) + 128;
    gimg       = (unsigned char *)malloc(dim * dim * sizeof(*gimg));
    for (i = 0; i < dim; i++)
	for (j = 0; j < dim; j++) {
	    qr_point p;
	    qr_aff_project(p, _aff, (j - 64) << lpsz, (i - 64) << lpsz);
	    gimg[i * dim + j] =
		_img[QR_CLAMPI(0, p[1] >> QR_FINDER_SUBPREC, _height - 1) *
			 _width +
		     QR_CLAMPI(0, p[0] >> QR_FINDER_SUBPREC, _width - 1)];
	}
    {
	min = (_ur->o[0] - 7 * _ur->size[0] >> lpsz) + 64;
	if (min < 0)
	    min = 0;
	max = (_ur->o[0] + 7 * _ur->size[0] >> lpsz) + 64;
	if (max > dim)
	    max = dim;
	for (y = -7; y <= 7; y++) {
	    i = (_ur->o[1] + y * _ur->size[1] >> lpsz) + 64;
	    if (i < 0 || i >= dim)
		continue;
	    for (j = min; j < max; j++)
		gimg[i * dim + j] = 0x7F;
	}
	min = (_ur->o[1] - 7 * _ur->size[1] >> lpsz) + 64;
	if (min < 0)
	    min = 0;
	max = (_ur->o[1] + 7 * _ur->size[1] >> lpsz) + 64;
	if (max > dim)
	    max = dim;
	for (u = -7; u <= 7; u++) {
	    j = (_ur->o[0] + u * _ur->size[0] >> lpsz) + 64;
	    if (j < 0 || j >= dim)
		continue;
	    for (i = min; i < max; i++)
		gimg[i * dim + j] = 0x7F;
	}
    }
    {
	min = (_dl->o[0] - 7 * _dl->size[0] >> lpsz) + 64;
	if (min < 0)
	    min = 0;
	max = (_dl->o[0] + 7 * _dl->size[0] >> lpsz) + 64;
	if (max > dim)
	    max = dim;
	for (y = -7; y <= 7; y++) {
	    i = (_dl->o[1] + y * _dl->size[1] >> lpsz) + 64;
	    if (i < 0 || i >= dim)
		continue;
	    for (j = min; j < max; j++)
		gimg[i * dim + j] = 0x7F;
	}
	min = (_dl->o[1] - 7 * _dl->size[1] >> lpsz) + 64;
	if (min < 0)
	    min = 0;
	max = (_dl->o[1] + 7 * _dl->size[1] >> lpsz) + 64;
	if (max > dim)
	    max = dim;
	for (u = -7; u <= 7; u++) {
	    j = (_dl->o[0] + u * _dl->size[0] >> lpsz) + 64;
	    if (j < 0 || j >= dim)
		continue;
	    for (i = min; i < max; i++)
		gimg[i * dim + j] = 0x7F;
	}
    }
    fout = fopen("undistorted_aff.png", "wb");
    image_write_png(gimg, dim, dim, fout);
    fclose(fout);
    free(gimg);
}

static void qr_finder_dump_hom_undistorted(qr_finder *_ul, qr_finder *_ur,
					   qr_finder *_dl, qr_hom *_hom,
					   const unsigned char *_img,
					   int _width, int _height)
{
    unsigned char *gimg;
    FILE *fout;
    int lpsz;
    int pixel_size;
    int dim;
    int min;
    int max;
    int u;
    int v;
    int i;
    int j;
    lpsz =
	qr_ilog(_ur->size[0] + _ur->size[1] + _dl->size[0] + _dl->size[1]) - 6;
    pixel_size = 1 << lpsz;
    dim	       = (1 << _hom->res - lpsz) + 256;
    gimg       = (unsigned char *)malloc(dim * dim * sizeof(*gimg));
    for (i = 0; i < dim; i++)
	for (j = 0; j < dim; j++) {
	    qr_point p;
	    qr_hom_project(p, _hom, (j - 128) << lpsz, (i - 128) << lpsz);
	    gimg[i * dim + j] =
		_img[QR_CLAMPI(0, p[1] >> QR_FINDER_SUBPREC, _height - 1) *
			 _width +
		     QR_CLAMPI(0, p[0] >> QR_FINDER_SUBPREC, _width - 1)];
	}
    {
	min = (_ur->o[0] - 7 * _ur->size[0] >> lpsz) + 128;
	if (min < 0)
	    min = 0;
	max = (_ur->o[0] + 7 * _ur->size[0] >> lpsz) + 128;
	if (max > dim)
	    max = dim;
	for (v = -7; v <= 7; v++) {
	    i = (_ur->o[1] + v * _ur->size[1] >> lpsz) + 128;
	    if (i < 0 || i >= dim)
		continue;
	    for (j = min; j < max; j++)
		gimg[i * dim + j] = 0x7F;
	}
	min = (_ur->o[1] - 7 * _ur->size[1] >> lpsz) + 128;
	if (min < 0)
	    min = 0;
	max = (_ur->o[1] + 7 * _ur->size[1] >> lpsz) + 128;
	if (max > dim)
	    max = dim;
	for (u = -7; u <= 7; u++) {
	    j = (_ur->o[0] + u * _ur->size[0] >> lpsz) + 128;
	    if (j < 0 || j >= dim)
		continue;
	    for (i = min; i < max; i++)
		gimg[i * dim + j] = 0x7F;
	}
    }
    {
	min = (_dl->o[0] - 7 * _dl->size[0] >> lpsz) + 128;
	if (min < 0)
	    min = 0;
	max = (_dl->o[0] + 7 * _dl->size[0] >> lpsz) + 128;
	if (max > dim)
	    max = dim;
	for (v = -7; v <= 7; v++) {
	    i = (_dl->o[1] + v * _dl->size[1] >> lpsz) + 128;
	    if (i < 0 || i >= dim)
		continue;
	    for (j = min; j < max; j++)
		gimg[i * dim + j] = 0x7F;
	}
	min = (_dl->o[1] - 7 * _dl->size[1] >> lpsz) + 128;
	if (min < 0)
	    min = 0;
	max = (_dl->o[1] + 7 * _dl->size[1] >> lpsz) + 128;
	if (max > dim)
	    max = dim;
	for (u = -7; u <= 7; u++) {
	    j = (_dl->o[0] + u * _dl->size[0] >> lpsz) + 128;
	    if (j < 0 || j >= dim)
		continue;
	    for (i = min; i < max; i++)
		gimg[i * dim + j] = 0x7F;
	}
    }
    fout = fopen("undistorted_hom.png", "wb");
    image_write_png(gimg, dim, dim, fout);
    fclose(fout);
    free(gimg);
}
#endif

/*A homography from one region of the grid back to the image.
  Unlike a qr_hom, this does not include an inverse transform and maps directly
   from the grid points, not a square with power-of-two sides.*/
struct qr_hom_cell {
    int fwd[3][3];
    int x0;
    int y0;
    int u0;
    int v0;
};

static void qr_hom_cell_init(qr_hom_cell *_cell, int _u0, int _v0, int _u1,
			     int _v1, int _u2, int _v2, int _u3, int _v3,
			     int _x0, int _y0, int _x1, int _y1, int _x2,
			     int _y2, int _x3, int _y3)
{
    int du10;
    int du20;
    int du30;
    int du31;
    int du32;
    int dv10;
    int dv20;
    int dv30;
    int dv31;
    int dv32;
    int dx10;
    int dx20;
    int dx30;
    int dx31;
    int dx32;
    int dy10;
    int dy20;
    int dy30;
    int dy31;
    int dy32;
    int a00;
    int a01;
    int a02;
    int a10;
    int a11;
    int a12;
    int a20;
    int a21;
    int a22;
    int i00;
    int i01;
    int i10;
    int i11;
    int i20;
    int i21;
    int i22;
    int b0;
    int b1;
    int b2;
    int shift;
    int round;
    int x;
    int y;
    int w;
    /*First, correct for the arrangement of the source points.
    We take advantage of the fact that we know the source points have a very
     limited dynamic range (so there will never be overflow) and a small amount
     of projective distortion.*/
    du10 = _u1 - _u0;
    du20 = _u2 - _u0;
    du30 = _u3 - _u0;
    du31 = _u3 - _u1;
    du32 = _u3 - _u2;
    dv10 = _v1 - _v0;
    dv20 = _v2 - _v0;
    dv30 = _v3 - _v0;
    dv31 = _v3 - _v1;
    dv32 = _v3 - _v2;
    /*Compute the coefficients of the forward transform from the unit square to
     the source point configuration.*/
    a20 = du32 * dv10 - du10 * dv32;
    a21 = du20 * dv31 - du31 * dv20;
    if (a20 || a21)
	a22 = du32 * dv31 - du31 * dv32;
    /*If the source grid points aren't in a non-affine arrangement, there's no
     reason to scale everything by du32*dv31-du31*dv32.
    Not doing so allows a much larger dynamic range, and is the only way we can
     initialize a base cell that covers the whole grid.*/
    else
	a22 = 1;
    a00 = du10 * (a20 + a22);
    a01 = du20 * (a21 + a22);
    a10 = dv10 * (a20 + a22);
    a11 = dv20 * (a21 + a22);
    /*Now compute the inverse transform.*/
    i00 = a11 * a22;
    i01 = -a01 * a22;
    i10 = -a10 * a22;
    i11 = a00 * a22;
    i20 = a10 * a21 - a11 * a20;
    i21 = a01 * a20 - a00 * a21;
    i22 = a00 * a11 - a01 * a10;
    /*Invert the coefficients.
    Since i22 is the largest, we divide it by all the others.
    The quotient is often exact (e.g., when the source points contain no
     projective distortion), and is never zero.
    Hence we can use zero to signal "infinity" when the divisor is zero.*/
    if (i00)
	i00 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i00)), i00);
    if (i01)
	i01 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i01)), i01);
    if (i10)
	i10 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i10)), i10);
    if (i11)
	i11 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i11)), i11);
    if (i20)
	i20 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i20)), i20);
    if (i21)
	i21 = QR_FLIPSIGNI(QR_DIVROUND(i22, abs(i21)), i21);
    /*Now compute the map from the unit square into the image.*/
    dx10 = _x1 - _x0;
    dx20 = _x2 - _x0;
    dx30 = _x3 - _x0;
    dx31 = _x3 - _x1;
    dx32 = _x3 - _x2;
    dy10 = _y1 - _y0;
    dy20 = _y2 - _y0;
    dy30 = _y3 - _y0;
    dy31 = _y3 - _y1;
    dy32 = _y3 - _y2;
    a20	 = dx32 * dy10 - dx10 * dy32;
    a21	 = dx20 * dy31 - dx31 * dy20;
    a22	 = dx32 * dy31 - dx31 * dy32;
    /*Figure out if we need to downscale anything.*/
    b0	  = qr_ilog(QR_MAXI(abs(dx10), abs(dy10))) + qr_ilog(abs(a20 + a22));
    b1	  = qr_ilog(QR_MAXI(abs(dx20), abs(dy20))) + qr_ilog(abs(a21 + a22));
    b2	  = qr_ilog(QR_MAXI(QR_MAXI(abs(a20), abs(a21)), abs(a22)));
    shift = QR_MAXI(0, QR_MAXI(QR_MAXI(b0, b1), b2) -
			   (QR_INT_BITS - 3 - QR_ALIGN_SUBPREC));
    round = (1 << shift) >> 1;
    /*Compute the final coefficients of the forward transform.*/
    a00 = QR_FIXMUL(dx10, a20 + a22, round, shift);
    a01 = QR_FIXMUL(dx20, a21 + a22, round, shift);
    a10 = QR_FIXMUL(dy10, a20 + a22, round, shift);
    a11 = QR_FIXMUL(dy20, a21 + a22, round, shift);
    /*And compose the two transforms.
    Since we inverted the coefficients above, we divide by them here instead
     of multiplying.
    This lets us take advantage of the full dynamic range.
    Note a zero divisor is really "infinity", and thus the quotient should also
     be zero.*/
    _cell->fwd[0][0] =
	(i00 ? QR_DIVROUND(a00, i00) : 0) + (i10 ? QR_DIVROUND(a01, i10) : 0);
    _cell->fwd[0][1] =
	(i01 ? QR_DIVROUND(a00, i01) : 0) + (i11 ? QR_DIVROUND(a01, i11) : 0);
    _cell->fwd[1][0] =
	(i00 ? QR_DIVROUND(a10, i00) : 0) + (i10 ? QR_DIVROUND(a11, i10) : 0);
    _cell->fwd[1][1] =
	(i01 ? QR_DIVROUND(a10, i01) : 0) + (i11 ? QR_DIVROUND(a11, i11) : 0);
    _cell->fwd[2][0] = (i00 ? QR_DIVROUND(a20, i00) : 0) +
			   (i10 ? QR_DIVROUND(a21, i10) : 0) +
			   (i20 ? QR_DIVROUND(a22, i20) : 0) + round >>
		       shift;
    _cell->fwd[2][1] = (i01 ? QR_DIVROUND(a20, i01) : 0) +
			   (i11 ? QR_DIVROUND(a21, i11) : 0) +
			   (i21 ? QR_DIVROUND(a22, i21) : 0) + round >>
		       shift;
    _cell->fwd[2][2] = a22 + round >> shift;
    /*Mathematically, a02 and a12 are exactly zero.
    However, that concentrates all of the rounding error in the (_u3,_v3)
     corner; we compute offsets which distribute it over the whole range.*/
    x	= _cell->fwd[0][0] * du10 + _cell->fwd[0][1] * dv10;
    y	= _cell->fwd[1][0] * du10 + _cell->fwd[1][1] * dv10;
    w	= _cell->fwd[2][0] * du10 + _cell->fwd[2][1] * dv10 + _cell->fwd[2][2];
    a02 = dx10 * w - x;
    a12 = dy10 * w - y;
    x	= _cell->fwd[0][0] * du20 + _cell->fwd[0][1] * dv20;
    y	= _cell->fwd[1][0] * du20 + _cell->fwd[1][1] * dv20;
    w	= _cell->fwd[2][0] * du20 + _cell->fwd[2][1] * dv20 + _cell->fwd[2][2];
    a02 += dx20 * w - x;
    a12 += dy20 * w - y;
    x = _cell->fwd[0][0] * du30 + _cell->fwd[0][1] * dv30;
    y = _cell->fwd[1][0] * du30 + _cell->fwd[1][1] * dv30;
    w = _cell->fwd[2][0] * du30 + _cell->fwd[2][1] * dv30 + _cell->fwd[2][2];
    a02 += dx30 * w - x;
    a12 += dy30 * w - y;
    _cell->fwd[0][2] = a02 + 2 >> 2;
    _cell->fwd[1][2] = a12 + 2 >> 2;
    _cell->x0	     = _x0;
    _cell->y0	     = _y0;
    _cell->u0	     = _u0;
    _cell->v0	     = _v0;
}

/*Finish a partial projection, converting from homogeneous coordinates to the
   normal 2-D representation.
  In loops, we can avoid many multiplies by computing the homogeneous _x, _y,
   and _w incrementally, but we cannot avoid the divisions, done here.*/
static void qr_hom_cell_fproject(qr_point _p, const qr_hom_cell *_cell, int _x,
				 int _y, int _w)
{
    if (_w == 0) {
	_p[0] = _x < 0 ? INT_MIN : INT_MAX;
	_p[1] = _y < 0 ? INT_MIN : INT_MAX;
    } else {
	if (_w < 0) {
	    _x = -_x;
	    _y = -_y;
	    _w = -_w;
	}
	_p[0] = QR_DIVROUND(_x, _w) + _cell->x0;
	_p[1] = QR_DIVROUND(_y, _w) + _cell->y0;
    }
}

static void qr_hom_cell_project(qr_point _p, const qr_hom_cell *_cell, int _u,
				int _v, int _res)
{
    _u -= _cell->u0 << _res;
    _v -= _cell->v0 << _res;
    qr_hom_cell_fproject(_p, _cell,
			 _cell->fwd[0][0] * _u + _cell->fwd[0][1] * _v +
			     (_cell->fwd[0][2] << _res),
			 _cell->fwd[1][0] * _u + _cell->fwd[1][1] * _v +
			     (_cell->fwd[1][2] << _res),
			 _cell->fwd[2][0] * _u + _cell->fwd[2][1] * _v +
			     (_cell->fwd[2][2] << _res));
}

/*Retrieves the bits corresponding to the alignment pattern template centered
   at the given location in the original image (at subpel precision).*/
static unsigned qr_alignment_pattern_fetch(qr_point _p[5][5], int _x0, int _y0,
					   const unsigned char *_img,
					   int _width, int _height)
{
    unsigned v;
    int i;
    int j;
    int k;
    int dx;
    int dy;
    dx = _x0 - _p[2][2][0];
    dy = _y0 - _p[2][2][1];
    v  = 0;
    for (k = i = 0; i < 5; i++)
	for (j = 0; j < 5; j++, k++) {
	    v |= qr_img_get_bit(_img, _width, _height, _p[i][j][0] + dx,
				_p[i][j][1] + dy)
		 << k;
	}
    return v;
}

/*Searches for an alignment pattern near the given location.*/
static int qr_alignment_pattern_search(qr_point _p, const qr_hom_cell *_cell,
				       int _u, int _v, int _r,
				       const unsigned char *_img, int _width,
				       int _height)
{
    qr_point c[4];
    int nc[4];
    qr_point p[5][5];
    qr_point pc;
    unsigned best_match;
    int best_dist;
    int bestx;
    int besty;
    unsigned match;
    int dist;
    int u;
    int v;
    int x0;
    int y0;
    int w0;
    int x;
    int y;
    int w;
    int dxdu;
    int dydu;
    int dwdu;
    int dxdv;
    int dydv;
    int dwdv;
    int dx;
    int dy;
    int i;
    int j;
    /*Build up a basic template using _cell to control shape and scale.
    We project the points in the template back to the image just once, since if
     the alignment pattern has moved, we don't really know why.
    If it's because of radial distortion, or the code wasn't flat, or something
     else, there's no reason to expect that a re-projection around each
     subsequent search point would be any closer to the actual shape than our
     first projection.
    Therefore we simply slide this template around, as is.*/
    u	 = (_u - 2) - _cell->u0;
    v	 = (_v - 2) - _cell->v0;
    x0	 = _cell->fwd[0][0] * u + _cell->fwd[0][1] * v + _cell->fwd[0][2];
    y0	 = _cell->fwd[1][0] * u + _cell->fwd[1][1] * v + _cell->fwd[1][2];
    w0	 = _cell->fwd[2][0] * u + _cell->fwd[2][1] * v + _cell->fwd[2][2];
    dxdu = _cell->fwd[0][0];
    dydu = _cell->fwd[1][0];
    dwdu = _cell->fwd[2][0];
    dxdv = _cell->fwd[0][1];
    dydv = _cell->fwd[1][1];
    dwdv = _cell->fwd[2][1];
    for (i = 0; i < 5; i++) {
	x = x0;
	y = y0;
	w = w0;
	for (j = 0; j < 5; j++) {
	    qr_hom_cell_fproject(p[i][j], _cell, x, y, w);
	    x += dxdu;
	    y += dydu;
	    w += dwdu;
	}
	x0 += dxdv;
	y0 += dydv;
	w0 += dwdv;
    }
    bestx = p[2][2][0];
    besty = p[2][2][1];
    best_match =
	qr_alignment_pattern_fetch(p, bestx, besty, _img, _width, _height);
    best_dist = qr_hamming_dist(best_match, 0x1F8D63F, 25);
    if (best_dist > 0) {
	u = _u - _cell->u0;
	v = _v - _cell->v0;
	x = _cell->fwd[0][0] * u + _cell->fwd[0][1] * v + _cell->fwd[0][2]
	    << QR_ALIGN_SUBPREC;
	y = _cell->fwd[1][0] * u + _cell->fwd[1][1] * v + _cell->fwd[1][2]
	    << QR_ALIGN_SUBPREC;
	w = _cell->fwd[2][0] * u + _cell->fwd[2][1] * v + _cell->fwd[2][2]
	    << QR_ALIGN_SUBPREC;
	/*Search an area at most _r modules around the target location, in
       concentric squares..*/
	for (i = 1; i < _r << QR_ALIGN_SUBPREC; i++) {
	    int side_len;
	    side_len = (i << 1) - 1;
	    x -= dxdu + dxdv;
	    y -= dydu + dydv;
	    w -= dwdu + dwdv;
	    for (j = 0; j < 4 * side_len; j++) {
		int dir;
		qr_hom_cell_fproject(pc, _cell, x, y, w);
		match = qr_alignment_pattern_fetch(p, pc[0], pc[1], _img,
						   _width, _height);
		dist  = qr_hamming_dist(match, 0x1F8D63F, best_dist + 1);
		if (dist < best_dist) {
		    best_match = match;
		    best_dist  = dist;
		    bestx      = pc[0];
		    besty      = pc[1];
		}
		if (j < 2 * side_len) {
		    dir = j >= side_len;
		    x += _cell->fwd[0][dir];
		    y += _cell->fwd[1][dir];
		    w += _cell->fwd[2][dir];
		} else {
		    dir = j >= 3 * side_len;
		    x -= _cell->fwd[0][dir];
		    y -= _cell->fwd[1][dir];
		    w -= _cell->fwd[2][dir];
		}
		if (!best_dist)
		    break;
	    }
	    if (!best_dist)
		break;
	}
    }
    /*If the best result we got was sufficiently bad, reject the match.
    If we're wrong and we include it, we can grossly distort the nearby
     region, whereas using the initial starting point should at least be
     consistent with the geometry we already have.*/
    if (best_dist > 6) {
	_p[0] = p[2][2][0];
	_p[1] = p[2][2][1];
	return -1;
    }
    /*Now try to get a more accurate location of the pattern center.*/
    dx = bestx - p[2][2][0];
    dy = besty - p[2][2][1];
    memset(nc, 0, sizeof(nc));
    memset(c, 0, sizeof(c));
    /*We consider 8 lines across the finder pattern in turn.
    If we actually found a symmetric pattern along that line, search for its
     exact center in the image.
    There are plenty more lines we could use if these don't work, but if we've
     found anything remotely close to an alignment pattern, we should be able
     to use most of these.*/
    for (i = 0; i < 8; i++) {
	static const unsigned MASK_TESTS[8][2] = {
	    { 0x1040041, 0x1000001 }, { 0x0041040, 0x0001000 },
	    { 0x0110110, 0x0100010 }, { 0x0011100, 0x0001000 },
	    { 0x0420084, 0x0400004 }, { 0x0021080, 0x0001000 },
	    { 0x0006C00, 0x0004400 }, { 0x0003800, 0x0001000 },
	};
	static const unsigned char MASK_COORDS[8][2] = { { 0, 0 }, { 1, 1 },
							 { 4, 0 }, { 3, 1 },
							 { 2, 0 }, { 2, 1 },
							 { 0, 2 }, { 1, 2 } };
	if ((best_match & MASK_TESTS[i][0]) == MASK_TESTS[i][1]) {
	    int x0;
	    int y0;
	    int x1;
	    int y1;
	    x0 = p[MASK_COORDS[i][1]][MASK_COORDS[i][0]][0] + dx >>
		 QR_FINDER_SUBPREC;
	    if (x0 < 0 || x0 >= _width)
		continue;
	    y0 = p[MASK_COORDS[i][1]][MASK_COORDS[i][0]][1] + dy >>
		 QR_FINDER_SUBPREC;
	    if (y0 < 0 || y0 >= _height)
		continue;
	    x1 = p[4 - MASK_COORDS[i][1]][4 - MASK_COORDS[i][0]][0] + dx >>
		 QR_FINDER_SUBPREC;
	    if (x1 < 0 || x1 >= _width)
		continue;
	    y1 = p[4 - MASK_COORDS[i][1]][4 - MASK_COORDS[i][0]][1] + dy >>
		 QR_FINDER_SUBPREC;
	    if (y1 < 0 || y1 >= _height)
		continue;
	    if (!qr_finder_locate_crossing(_img, _width, _height, x0, y0, x1,
					   y1, i & 1, pc)) {
		int w;
		int cx;
		int cy;
		cx = pc[0] - bestx;
		cy = pc[1] - besty;
		if (i & 1) {
		    /*Weight crossings around the center dot more highly, as they are
             generally more reliable.*/
		    w = 3;
		    cx += cx << 1;
		    cy += cy << 1;
		} else
		    w = 1;
		nc[i >> 1] += w;
		c[i >> 1][0] += cx;
		c[i >> 1][1] += cy;
	    }
	}
    }
    /*Sum offsets from lines in orthogonal directions.*/
    for (i = 0; i < 2; i++) {
	int a;
	int b;
	a = nc[i << 1];
	b = nc[i << 1 | 1];
	if (a && b) {
	    int w;
	    w = QR_MAXI(a, b);
	    c[i << 1][0] =
		QR_DIVROUND(w * (b * c[i << 1][0] + a * c[i << 1 | 1][0]),
			    a * b);
	    c[i << 1][1] =
		QR_DIVROUND(w * (b * c[i << 1][1] + a * c[i << 1 | 1][1]),
			    a * b);
	    nc[i << 1] = w << 1;
	} else {
	    c[i << 1][0] += c[i << 1 | 1][0];
	    c[i << 1][1] += c[i << 1 | 1][1];
	    nc[i << 1] += b;
	}
    }
    /*Average offsets from pairs of orthogonal lines.*/
    c[0][0] += c[2][0];
    c[0][1] += c[2][1];
    nc[0] += nc[2];
    /*If we actually found any such lines, apply the adjustment.*/
    if (nc[0]) {
	dx = QR_DIVROUND(c[0][0], nc[0]);
	dy = QR_DIVROUND(c[0][1], nc[0]);
	/*But only if it doesn't make things too much worse.*/
	match = qr_alignment_pattern_fetch(p, bestx + dx, besty + dy, _img,
					   _width, _height);
	dist  = qr_hamming_dist(match, 0x1F8D63F, best_dist + 1);
	if (dist <= best_dist + 1) {
	    bestx += dx;
	    besty += dy;
	}
    }
    _p[0] = bestx;
    _p[1] = besty;
    return 0;
}

static int qr_hom_fit(qr_hom *_hom, qr_finder *_ul, qr_finder *_ur,
		      qr_finder *_dl, qr_point _p[4], const qr_aff *_aff,
		      isaac_ctx *_isaac, const unsigned char *_img, int _width,
		      int _height)
{
    qr_point *b;
    int nb;
    int cb;
    qr_point *r;
    int nr;
    int cr;
    qr_line l[4];
    qr_point q;
    qr_point p;
    int ox;
    int oy;
    int ru;
    int rv;
    int dru;
    int drv;
    int bu;
    int bv;
    int dbu;
    int dbv;
    int rx;
    int ry;
    int drxi;
    int dryi;
    int drxj;
    int dryj;
    int rdone;
    int nrempty;
    int rlastfit;
    int bx;
    int by;
    int dbxi;
    int dbyi;
    int dbxj;
    int dbyj;
    int bdone;
    int nbempty;
    int blastfit;
    int shift;
    int round;
    int version4;
    int brx;
    int bry;
    int i;
    /*We attempt to correct large-scale perspective distortion by fitting lines
     to the edge of the code area.
    We could also look for an alignment pattern now, but that wouldn't work for
     version 1 codes, which have no alignment pattern.
    Even if the code is supposed to have one, there's go guarantee we'd find it
     intact.*/
    /*Fitting lines is easy for the edges on which we have two finder patterns.
    After the fit, UL is guaranteed to be on the proper side, but if either of
     the other two finder patterns aren't, something is wrong.*/
    qr_finder_ransac(_ul, _aff, _isaac, 0);
    qr_finder_ransac(_dl, _aff, _isaac, 0);
    qr_line_fit_finder_pair(l[0], _aff, _ul, _dl, 0);
    if (qr_line_eval(l[0], _dl->c->pos[0], _dl->c->pos[1]) < 0 ||
	qr_line_eval(l[0], _ur->c->pos[0], _ur->c->pos[1]) < 0) {
	return -1;
    }
    qr_finder_ransac(_ul, _aff, _isaac, 2);
    qr_finder_ransac(_ur, _aff, _isaac, 2);
    qr_line_fit_finder_pair(l[2], _aff, _ul, _ur, 2);
    if (qr_line_eval(l[2], _dl->c->pos[0], _dl->c->pos[1]) < 0 ||
	qr_line_eval(l[2], _ur->c->pos[0], _ur->c->pos[1]) < 0) {
	return -1;
    }
    /*The edges which only have one finder pattern are more difficult.
    We start by fitting a line to the edge of the one finder pattern we do
     have.
    This can fail due to an insufficient number of sample points, and even if
     it succeeds can be fairly inaccurate, because all of the points are
     clustered in one corner of the QR code.
    If it fails, we just use an axis-aligned line in the affine coordinate
     system.
    Then we walk along the edge of the entire code, looking for
     light:dark:light patterns perpendicular to the edge.
    Wherever we find one, we take the center of the dark portion as an
     additional sample point.
    At the end, we re-fit the line using all such sample points found.*/
    drv = _ur->size[1] >> 1;
    qr_finder_ransac(_ur, _aff, _isaac, 1);
    if (qr_line_fit_finder_edge(l[1], _ur, 1, _aff->res) >= 0) {
	if (qr_line_eval(l[1], _ul->c->pos[0], _ul->c->pos[1]) < 0 ||
	    qr_line_eval(l[1], _dl->c->pos[0], _dl->c->pos[1]) < 0) {
	    return -1;
	}
	/*Figure out the change in ru for a given change in rv when stepping along
       the fitted line.*/
	if (qr_aff_line_step(_aff, l[1], 1, drv, &dru) < 0)
	    return -1;
    } else
	dru = 0;
    ru	= _ur->o[0] + 3 * _ur->size[0] - 2 * dru;
    rv	= _ur->o[1] - 2 * drv;
    dbu = _dl->size[0] >> 1;
    qr_finder_ransac(_dl, _aff, _isaac, 3);
    if (qr_line_fit_finder_edge(l[3], _dl, 3, _aff->res) >= 0) {
	if (qr_line_eval(l[3], _ul->c->pos[0], _ul->c->pos[1]) < 0 ||
	    qr_line_eval(l[3], _ur->c->pos[0], _ur->c->pos[1]) < 0) {
	    return -1;
	}
	/*Figure out the change in bv for a given change in bu when stepping along
       the fitted line.*/
	if (qr_aff_line_step(_aff, l[3], 0, dbu, &dbv) < 0)
	    return -1;
    } else
	dbv = 0;
    bu = _dl->o[0] - 2 * dbu;
    bv = _dl->o[1] + 3 * _dl->size[1] - 2 * dbv;
    /*Set up the initial point lists.*/
    nr = rlastfit = _ur->ninliers[1];
    cr		  = nr + (_dl->o[1] - rv + drv - 1) / drv;
    r		  = (qr_point *)malloc(cr * sizeof(*r));
    for (i = 0; i < _ur->ninliers[1]; i++) {
	memcpy(r[i], _ur->edge_pts[1][i].pos, sizeof(r[i]));
    }
    nb = blastfit = _dl->ninliers[3];
    cb		  = nb + (_ur->o[0] - bu + dbu - 1) / dbu;
    b		  = (qr_point *)malloc(cb * sizeof(*b));
    for (i = 0; i < _dl->ninliers[3]; i++) {
	memcpy(b[i], _dl->edge_pts[3][i].pos, sizeof(b[i]));
    }
    /*Set up the step parameters for the affine projection.*/
    ox	 = (_aff->x0 << _aff->res) + (1 << _aff->res - 1);
    oy	 = (_aff->y0 << _aff->res) + (1 << _aff->res - 1);
    rx	 = _aff->fwd[0][0] * ru + _aff->fwd[0][1] * rv + ox;
    ry	 = _aff->fwd[1][0] * ru + _aff->fwd[1][1] * rv + oy;
    drxi = _aff->fwd[0][0] * dru + _aff->fwd[0][1] * drv;
    dryi = _aff->fwd[1][0] * dru + _aff->fwd[1][1] * drv;
    drxj = _aff->fwd[0][0] * _ur->size[0];
    dryj = _aff->fwd[1][0] * _ur->size[0];
    bx	 = _aff->fwd[0][0] * bu + _aff->fwd[0][1] * bv + ox;
    by	 = _aff->fwd[1][0] * bu + _aff->fwd[1][1] * bv + oy;
    dbxi = _aff->fwd[0][0] * dbu + _aff->fwd[0][1] * dbv;
    dbyi = _aff->fwd[1][0] * dbu + _aff->fwd[1][1] * dbv;
    dbxj = _aff->fwd[0][1] * _dl->size[1];
    dbyj = _aff->fwd[1][1] * _dl->size[1];
    /*Now step along the lines, looking for new sample points.*/
    nrempty = nbempty = 0;
    for (;;) {
	int ret;
	int x0;
	int y0;
	int x1;
	int y1;
	/*If we take too many steps without encountering a non-zero pixel, assume
       we have wandered off the edge and stop looking before we hit the other
       side of the quiet region.
      Otherwise, stop when the lines cross (if they do so inside the affine
       region) or come close to crossing (outside the affine region).
      TODO: We don't have any way of detecting when we've wandered into the
       code interior; we could stop if the outside sample ever shows up dark,
       but this could happen because of noise in the quiet region, too.*/
	rdone = rv >= QR_MINI(bv, _dl->o[1] + bv >> 1) || nrempty > 14;
	bdone = bu >= QR_MINI(ru, _ur->o[0] + ru >> 1) || nbempty > 14;
	if (!rdone && (bdone || rv < bu)) {
	    x0 = rx + drxj >> _aff->res + QR_FINDER_SUBPREC;
	    y0 = ry + dryj >> _aff->res + QR_FINDER_SUBPREC;
	    x1 = rx - drxj >> _aff->res + QR_FINDER_SUBPREC;
	    y1 = ry - dryj >> _aff->res + QR_FINDER_SUBPREC;
	    if (nr >= cr) {
		cr = cr << 1 | 1;
		r  = (qr_point *)realloc(r, cr * sizeof(*r));
	    }
	    ret = qr_finder_quick_crossing_check(_img, _width, _height, x0, y0,
						 x1, y1, 1);
	    if (!ret) {
		ret = qr_finder_locate_crossing(_img, _width, _height, x0, y0,
						x1, y1, 1, r[nr]);
	    }
	    if (ret >= 0) {
		if (!ret) {
		    qr_aff_unproject(q, _aff, r[nr][0], r[nr][1]);
		    /*Move the current point halfway towards the crossing.
            We don't move the whole way to give us some robustness to noise.*/
		    ru = ru + q[0] >> 1;
		    /*But ensure that rv monotonically increases.*/
		    if (q[1] + drv > rv)
			rv = rv + q[1] >> 1;
		    rx = _aff->fwd[0][0] * ru + _aff->fwd[0][1] * rv + ox;
		    ry = _aff->fwd[1][0] * ru + _aff->fwd[1][1] * rv + oy;
		    nr++;
		    /*Re-fit the line to update the step direction periodically.*/
		    if (nr > QR_MAXI(1, rlastfit + (rlastfit >> 2))) {
			qr_line_fit_points(l[1], r, nr, _aff->res);
			if (qr_aff_line_step(_aff, l[1], 1, drv, &dru) >= 0) {
			    drxi =
				_aff->fwd[0][0] * dru + _aff->fwd[0][1] * drv;
			    dryi =
				_aff->fwd[1][0] * dru + _aff->fwd[1][1] * drv;
			}
			rlastfit = nr;
		    }
		}
		nrempty = 0;
	    } else
		nrempty++;
	    ru += dru;
	    /*Our final defense: if we overflow, stop.*/
	    if (rv + drv > rv)
		rv += drv;
	    else
		nrempty = INT_MAX;
	    rx += drxi;
	    ry += dryi;
	} else if (!bdone) {
	    x0 = bx + dbxj >> _aff->res + QR_FINDER_SUBPREC;
	    y0 = by + dbyj >> _aff->res + QR_FINDER_SUBPREC;
	    x1 = bx - dbxj >> _aff->res + QR_FINDER_SUBPREC;
	    y1 = by - dbyj >> _aff->res + QR_FINDER_SUBPREC;
	    if (nb >= cb) {
		cb = cb << 1 | 1;
		b  = (qr_point *)realloc(b, cb * sizeof(*b));
	    }
	    ret = qr_finder_quick_crossing_check(_img, _width, _height, x0, y0,
						 x1, y1, 1);
	    if (!ret) {
		ret = qr_finder_locate_crossing(_img, _width, _height, x0, y0,
						x1, y1, 1, b[nb]);
	    }
	    if (ret >= 0) {
		if (!ret) {
		    qr_aff_unproject(q, _aff, b[nb][0], b[nb][1]);
		    /*Move the current point halfway towards the crossing.
            We don't move the whole way to give us some robustness to noise.*/
		    /*But ensure that bu monotonically increases.*/
		    if (q[0] + dbu > bu)
			bu = bu + q[0] >> 1;
		    bv = bv + q[1] >> 1;
		    bx = _aff->fwd[0][0] * bu + _aff->fwd[0][1] * bv + ox;
		    by = _aff->fwd[1][0] * bu + _aff->fwd[1][1] * bv + oy;
		    nb++;
		    /*Re-fit the line to update the step direction periodically.*/
		    if (nb > QR_MAXI(1, blastfit + (blastfit >> 2))) {
			qr_line_fit_points(l[3], b, nb, _aff->res);
			if (qr_aff_line_step(_aff, l[3], 0, dbu, &dbv) >= 0) {
			    dbxi =
				_aff->fwd[0][0] * dbu + _aff->fwd[0][1] * dbv;
			    dbyi =
				_aff->fwd[1][0] * dbu + _aff->fwd[1][1] * dbv;
			}
			blastfit = nb;
		    }
		}
		nbempty = 0;
	    } else
		nbempty++;
	    /*Our final defense: if we overflow, stop.*/
	    if (bu + dbu > bu)
		bu += dbu;
	    else
		nbempty = INT_MAX;
	    bv += dbv;
	    bx += dbxi;
	    by += dbyi;
	} else
	    break;
    }
    /*Fit the new lines.
    If we _still_ don't have enough sample points, then just use an
     axis-aligned line from the affine coordinate system (e.g., one parallel
     to the opposite edge in the image).*/
    if (nr > 1)
	qr_line_fit_points(l[1], r, nr, _aff->res);
    else {
	qr_aff_project(p, _aff, _ur->o[0] + 3 * _ur->size[0], _ur->o[1]);
	shift	= QR_MAXI(0, qr_ilog(QR_MAXI(abs(_aff->fwd[0][1]),
					     abs(_aff->fwd[1][1]))) -
				 (_aff->res + 1 >> 1));
	round	= (1 << shift) >> 1;
	l[1][0] = _aff->fwd[1][1] + round >> shift;
	l[1][1] = -_aff->fwd[0][1] + round >> shift;
	l[1][2] = -(l[1][0] * p[0] + l[1][1] * p[1]);
    }
    free(r);
    if (nb > 1)
	qr_line_fit_points(l[3], b, nb, _aff->res);
    else {
	qr_aff_project(p, _aff, _dl->o[0], _dl->o[1] + 3 * _dl->size[1]);
	shift	= QR_MAXI(0, qr_ilog(QR_MAXI(abs(_aff->fwd[0][1]),
					     abs(_aff->fwd[1][1]))) -
				 (_aff->res + 1 >> 1));
	round	= (1 << shift) >> 1;
	l[3][0] = _aff->fwd[1][0] + round >> shift;
	l[3][1] = -_aff->fwd[0][0] + round >> shift;
	l[3][2] = -(l[1][0] * p[0] + l[1][1] * p[1]);
    }
    free(b);
    for (i = 0; i < 4; i++) {
	if (qr_line_isect(_p[i], l[i & 1], l[2 + (i >> 1)]) < 0)
	    return -1;
	/*It's plausible for points to be somewhat outside the image, but too far
       and too much of the pattern will be gone for it to be decodable.*/
	if (_p[i][0] < -_width << QR_FINDER_SUBPREC ||
	    _p[i][0] >= _width << QR_FINDER_SUBPREC + 1 ||
	    _p[i][1] < -_height << QR_FINDER_SUBPREC ||
	    _p[i][1] >= _height << QR_FINDER_SUBPREC + 1) {
	    return -1;
	}
    }
    /*By default, use the edge intersection point for the bottom-right corner.*/
    brx = _p[3][0];
    bry = _p[3][1];
    /*However, if our average version estimate is greater than 1, NOW we try to
     search for an alignment pattern.
    We get a much better success rate by doing this after our initial attempt
     to promote the transform to a homography than before.
    You might also think it would be more reliable to use the interior finder
     pattern edges, since the outer ones may be obscured or damaged, and it
     would save us a reprojection below, since they would form a nice square
     with the location of the alignment pattern, but this turns out to be a bad
     idea.
    Non-linear distortion is usually maximal on the outside edge, and thus
     estimating the grid position from points on the interior means we might
     get mis-aligned by the time we reach the edge.*/
    version4 = _ul->eversion[0] + _ul->eversion[1] + _ur->eversion[0] +
	       _dl->eversion[1];
    if (version4 > 4) {
	qr_hom_cell cell;
	qr_point p3;
	int dim;
	dim = 17 + version4;
	qr_hom_cell_init(&cell, 0, 0, dim - 1, 0, 0, dim - 1, dim - 1, dim - 1,
			 _p[0][0], _p[0][1], _p[1][0], _p[1][1], _p[2][0],
			 _p[2][1], _p[3][0], _p[3][1]);
	if (qr_alignment_pattern_search(p3, &cell, dim - 7, dim - 7, 4, _img,
					_width, _height) >= 0) {
	    long long w;
	    long long mask;
	    int c21;
	    int dx21;
	    int dy21;
	    /*There's no real need to update the bounding box corner, and in fact we
         actively perform worse if we do.
        Clearly it was good enough for us to find this alignment pattern, so
         it should be good enough to use for grid initialization.
        The point of doing the search was to get more accurate version
         estimates and a better chance of decoding the version and format info.
        This is particularly important for small versions that have no encoded
         version info, since any mismatch in version renders the code
         undecodable.*/
	    /*We do, however, need four points in a square to initialize our
         homography, so project the point from the alignment center to the
         corner of the code area.*/
	    c21	 = _p[2][0] * _p[1][1] - _p[2][1] * _p[1][0];
	    dx21 = _p[2][0] - _p[1][0];
	    dy21 = _p[2][1] - _p[1][1];
	    w	 = QR_EXTMUL(dim - 7, c21,
			     QR_EXTMUL(dim - 13, _p[0][0] * dy21 - _p[0][1] * dx21,
				       QR_EXTMUL(6, p3[0] * dy21 - p3[1] * dx21,
						 0)));
	    /*The projection failed: invalid geometry.*/
	    if (w == 0)
		return -1;
	    mask = QR_SIGNMASK(w);
	    w	 = w + mask ^ mask;
	    brx	 = (int)QR_DIVROUND(
		 QR_EXTMUL((dim - 7) * _p[0][0], p3[0] * dy21,
			   QR_EXTMUL((dim - 13) * p3[0], c21 - _p[0][1] * dx21,
				     QR_EXTMUL(6 * _p[0][0], c21 - p3[1] * dx21,
					       0))) +
			 mask ^
		     mask,
		 w);
	    bry = (int)QR_DIVROUND(
		QR_EXTMUL((dim - 7) * _p[0][1], -p3[1] * dx21,
			  QR_EXTMUL((dim - 13) * p3[1], c21 + _p[0][0] * dy21,
				    QR_EXTMUL(6 * _p[0][1], c21 + p3[0] * dy21,
					      0))) +
			mask ^
		    mask,
		w);
	}
    }
    /*Now we have four points that map to a square: initialize the projection.*/
    qr_hom_init(_hom, _p[0][0], _p[0][1], _p[1][0], _p[1][1], _p[2][0],
		_p[2][1], brx, bry, QR_HOM_BITS);
    return 0;
}

/*The BCH(18,6,3) codes are only used for version information, which must lie
   between 7 and 40 (inclusive).*/
static const unsigned BCH18_6_CODES[34] = {
    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
};

/*Corrects a BCH(18,6,3) code word.
  _y: Contains the code word to be checked on input, and the corrected value on
       output.
  Return: The number of errors.
          If more than 3 errors are detected, returns a negative value and
           performs no correction.*/
static int bch18_6_correct(unsigned *_y)
{
    unsigned x;
    unsigned y;
    int nerrs;
    y = *_y;
    /*Check the easy case first: see if the data bits were uncorrupted.*/
    x = y >> 12;
    if (x >= 7 && x <= 40) {
	nerrs = qr_hamming_dist(y, BCH18_6_CODES[x - 7], 4);
	if (nerrs < 4) {
	    *_y = BCH18_6_CODES[x - 7];
	    return nerrs;
	}
    }
    /*Exhaustive search is faster than field operations in GF(19).*/
    for (x = 0; x < 34; x++)
	if (x + 7 != y >> 12) {
	    nerrs = qr_hamming_dist(y, BCH18_6_CODES[x], 4);
	    if (nerrs < 4) {
		*_y = BCH18_6_CODES[x];
		return nerrs;
	    }
	}
    return -1;
}

#if 0
static unsigned bch18_6_encode(unsigned _x){
  return (-(_x&1)&0x01F25)^(-(_x>>1&1)&0x0216F)^(-(_x>>2&1)&0x042DE)^
   (-(_x>>3&1)&0x085BC)^(-(_x>>4&1)&0x10B78)^(-(_x>>5&1)&0x209D5);
}
#endif

/*Reads the version bits near a finder module and decodes the version number.*/
static int qr_finder_version_decode(qr_finder *_f, const qr_hom *_hom,
				    const unsigned char *_img, int _width,
				    int _height, int _dir)
{
    qr_point q;
    unsigned v;
    int x0;
    int y0;
    int w0;
    int dxi;
    int dyi;
    int dwi;
    int dxj;
    int dyj;
    int dwj;
    int ret;
    int i;
    int j;
    int k;
    v		= 0;
    q[_dir]	= _f->o[_dir] - 7 * _f->size[_dir];
    q[1 - _dir] = _f->o[1 - _dir] - 3 * _f->size[1 - _dir];
    x0		= _hom->fwd[0][0] * q[0] + _hom->fwd[0][1] * q[1];
    y0		= _hom->fwd[1][0] * q[0] + _hom->fwd[1][1] * q[1];
    w0		= _hom->fwd[2][0] * q[0] + _hom->fwd[2][1] * q[1] + _hom->fwd22;
    dxi		= _hom->fwd[0][1 - _dir] * _f->size[1 - _dir];
    dyi		= _hom->fwd[1][1 - _dir] * _f->size[1 - _dir];
    dwi		= _hom->fwd[2][1 - _dir] * _f->size[1 - _dir];
    dxj		= _hom->fwd[0][_dir] * _f->size[_dir];
    dyj		= _hom->fwd[1][_dir] * _f->size[_dir];
    dwj		= _hom->fwd[2][_dir] * _f->size[_dir];
    for (k = i = 0; i < 6; i++) {
	int x;
	int y;
	int w;
	x = x0;
	y = y0;
	w = w0;
	for (j = 0; j < 3; j++, k++) {
	    qr_point p;
	    qr_hom_fproject(p, _hom, x, y, w);
	    v |= qr_img_get_bit(_img, _width, _height, p[0], p[1]) << k;
	    x += dxj;
	    y += dyj;
	    w += dwj;
	}
	x0 += dxi;
	y0 += dyi;
	w0 += dwi;
    }
    ret = bch18_6_correct(&v);
    /*TODO: I seem to have an image with the version bits in a different order
     (the transpose of the standard order).
    Even if I change the order here so I can parse the version on this image,
     I can't decode the rest of the code.
    If this is really needed, we should just re-order the bits.*/
#if 0
  if(ret<0){
    /*17 16 15 14 13 12 11 10  9  8  7  6  5  4  3  2  1  0
       0  3  6  9 12 15  1  4  7 10 13 16  2  5  8 11 14 17
      17 13  9  5  1 -3 10  6  2 -2 -6-10  3 -1 -5 -9-13-17*/
    v=0;
    for(k=i=0;i<3;i++){
      p[_dir]=_f->o[_dir]+_f->size[_dir]*(-5-i);
      for(j=0;j<6;j++,k++){
        qr_point q;
        p[1-_dir]=_f->o[1-_dir]+_f->size[1-_dir]*(2-j);
        qr_hom_project(q,_hom,p[0],p[1]);
        v|=qr_img_get_bit(_img,_width,_height,q[0],q[1])<<k;
      }
    }
    ret=bch18_6_correct(&v);
  }
#endif
    return ret >= 0 ? (int)(v >> 12) : ret;
}

/*Reads the format info bits near the finder modules and decodes them.*/
static int qr_finder_fmt_info_decode(qr_finder *_ul, qr_finder *_ur,
				     qr_finder *_dl, const qr_hom *_hom,
				     const unsigned char *_img, int _width,
				     int _height)
{
    qr_point p;
    unsigned lo[2];
    unsigned hi[2];
    int u;
    int v;
    int x;
    int y;
    int w;
    int dx;
    int dy;
    int dw;
    int fmt_info[4];
    int count[4];
    int nerrs[4];
    int nfmt_info;
    int besti;
    int imax;
    int di;
    int i;
    int k;
    /*Read the bits around the UL corner.*/
    lo[0] = 0;
    u	  = _ul->o[0] + 5 * _ul->size[0];
    v	  = _ul->o[1] - 3 * _ul->size[1];
    x	  = _hom->fwd[0][0] * u + _hom->fwd[0][1] * v;
    y	  = _hom->fwd[1][0] * u + _hom->fwd[1][1] * v;
    w	  = _hom->fwd[2][0] * u + _hom->fwd[2][1] * v + _hom->fwd22;
    dx	  = _hom->fwd[0][1] * _ul->size[1];
    dy	  = _hom->fwd[1][1] * _ul->size[1];
    dw	  = _hom->fwd[2][1] * _ul->size[1];
    for (k = i = 0;; i++) {
	/*Skip the timing pattern row.*/
	if (i != 6) {
	    qr_hom_fproject(p, _hom, x, y, w);
	    lo[0] |= qr_img_get_bit(_img, _width, _height, p[0], p[1]) << k++;
	    /*Don't advance q in the last iteration... we'll start the next loop from
         the current position.*/
	    if (i >= 8)
		break;
	}
	x += dx;
	y += dy;
	w += dw;
    }
    hi[0] = 0;
    dx	  = -_hom->fwd[0][0] * _ul->size[0];
    dy	  = -_hom->fwd[1][0] * _ul->size[0];
    dw	  = -_hom->fwd[2][0] * _ul->size[0];
    while (i-- > 0) {
	x += dx;
	y += dy;
	w += dw;
	/*Skip the timing pattern column.*/
	if (i != 6) {
	    qr_hom_fproject(p, _hom, x, y, w);
	    hi[0] |= qr_img_get_bit(_img, _width, _height, p[0], p[1]) << k++;
	}
    }
    /*Read the bits next to the UR corner.*/
    lo[1] = 0;
    u	  = _ur->o[0] + 3 * _ur->size[0];
    v	  = _ur->o[1] + 5 * _ur->size[1];
    x	  = _hom->fwd[0][0] * u + _hom->fwd[0][1] * v;
    y	  = _hom->fwd[1][0] * u + _hom->fwd[1][1] * v;
    w	  = _hom->fwd[2][0] * u + _hom->fwd[2][1] * v + _hom->fwd22;
    dx	  = -_hom->fwd[0][0] * _ur->size[0];
    dy	  = -_hom->fwd[1][0] * _ur->size[0];
    dw	  = -_hom->fwd[2][0] * _ur->size[0];
    for (k = 0; k < 8; k++) {
	qr_hom_fproject(p, _hom, x, y, w);
	lo[1] |= qr_img_get_bit(_img, _width, _height, p[0], p[1]) << k;
	x += dx;
	y += dy;
	w += dw;
    }
    /*Read the bits next to the DL corner.*/
    hi[1] = 0;
    u	  = _dl->o[0] + 5 * _dl->size[0];
    v	  = _dl->o[1] - 3 * _dl->size[1];
    x	  = _hom->fwd[0][0] * u + _hom->fwd[0][1] * v;
    y	  = _hom->fwd[1][0] * u + _hom->fwd[1][1] * v;
    w	  = _hom->fwd[2][0] * u + _hom->fwd[2][1] * v + _hom->fwd22;
    dx	  = _hom->fwd[0][1] * _dl->size[1];
    dy	  = _hom->fwd[1][1] * _dl->size[1];
    dw	  = _hom->fwd[2][1] * _dl->size[1];
    for (k = 8; k < 15; k++) {
	qr_hom_fproject(p, _hom, x, y, w);
	hi[1] |= qr_img_get_bit(_img, _width, _height, p[0], p[1]) << k;
	x += dx;
	y += dy;
	w += dw;
    }
    /*For each group of bits we have two samples... try them in all combinations
     and pick the most popular valid code, breaking ties using the number of
     bit errors.*/
    imax      = 2 << (hi[0] != hi[1]);
    di	      = 1 + (lo[0] == lo[1]);
    nfmt_info = 0;
    for (i = 0; i < imax; i += di) {
	unsigned v;
	int ret;
	int j;
	v   = (lo[i & 1] | hi[i >> 1]) ^ 0x5412;
	ret = bch15_5_correct(&v);
	v >>= 10;
	if (ret < 0)
	    ret = 4;
	for (j = 0;; j++) {
	    if (j >= nfmt_info) {
		fmt_info[j] = v;
		count[j]    = 1;
		nerrs[j]    = ret;
		nfmt_info++;
		break;
	    }
	    if (fmt_info[j] == (int)v) {
		count[j]++;
		if (ret < nerrs[j])
		    nerrs[j] = ret;
		break;
	    }
	}
    }
    besti = 0;
    for (i = 1; i < nfmt_info; i++) {
	if (nerrs[besti] > 3 && nerrs[i] <= 3 || count[i] > count[besti] ||
	    count[i] == count[besti] && nerrs[i] < nerrs[besti]) {
	    besti = i;
	}
    }
    return nerrs[besti] < 4 ? fmt_info[besti] : -1;
}

/*The grid used to sample the image bits.
  The grid is divided into separate cells bounded by finder patterns and/or
   alignment patterns, and a separate map back to the original image is
   constructed for each cell.
  All of these structural elements, as well as the timing patterns, version
   info, and format info, are marked in fpmask so they can easily be skipped
   during decode.*/
struct qr_sampling_grid {
    qr_hom_cell *cells[6];
    unsigned *fpmask;
    int cell_limits[6];
    int ncells;
};

/*Mark a given region as belonging to the function pattern.*/
static void qr_sampling_grid_fp_mask_rect(qr_sampling_grid *_grid, int _dim,
					  int _u, int _v, int _w, int _h)
{
    int i;
    int j;
    int stride;
    stride = _dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS;
    /*Note that we store bits column-wise, since that's how they're read out of
     the grid.*/
    for (j = _u; j < _u + _w; j++)
	for (i = _v; i < _v + _h; i++) {
	    _grid->fpmask[j * stride + (i >> QR_INT_LOGBITS)] |=
		1 << (i & QR_INT_BITS - 1);
	}
}

/*Determine if a given grid location is inside the function pattern.*/
static int qr_sampling_grid_is_in_fp(const qr_sampling_grid *_grid, int _dim,
				     int _u, int _v)
{
    return _grid->fpmask[_u * (_dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS) +
			 (_v >> QR_INT_LOGBITS)] >>
	       (_v & QR_INT_BITS - 1) &
	   1;
}

/*The spacing between alignment patterns after the second for versions >= 7.
  We could compact this more, but the code to access it would eliminate the
   gains.*/
static const unsigned char QR_ALIGNMENT_SPACING[34] = {
    16, 18, 20, 22, 24, 26, 28, 20, 22, 24, 24, 26, 28, 28, 22, 24, 24,
    26, 26, 28, 28, 24, 24, 26, 26, 26, 28, 28, 24, 26, 26, 26, 28, 28
};

static inline void qr_svg_points(const char *cls, qr_point *p, int n)
{
    int i;
    svg_path_start(cls, 1, 0, 0);
    for (i = 0; i < n; i++, p++)
	svg_path_moveto(SVG_ABS, p[0][0], p[0][1]);
    svg_path_end();
}

/*Initialize the sampling grid for each region of the code.
  _version:  The (decoded) version number.
  _ul_pos:   The location of the UL finder pattern.
  _ur_pos:   The location of the UR finder pattern.
  _dl_pos:   The location of the DL finder pattern.
  _p:        On input, contains estimated positions of the four corner modules.
             On output, contains a bounding quadrilateral for the code.
  _img:      The binary input image.
  _width:    The width of the input image.
  _height:   The height of the input image.
  Return: 0 on success, or a negative value on error.*/
static void qr_sampling_grid_init(qr_sampling_grid *_grid, int _version,
				  const qr_point _ul_pos,
				  const qr_point _ur_pos,
				  const qr_point _dl_pos, qr_point _p[4],
				  const unsigned char *_img, int _width,
				  int _height)
{
    qr_hom_cell base_cell;
    int align_pos[7];
    int dim;
    int nalign;
    int i;
    dim	   = 17 + (_version << 2);
    nalign = (_version / 7) + 2;
    /*Create a base cell to bootstrap the alignment pattern search.*/
    qr_hom_cell_init(&base_cell, 0, 0, dim - 1, 0, 0, dim - 1, dim - 1, dim - 1,
		     _p[0][0], _p[0][1], _p[1][0], _p[1][1], _p[2][0], _p[2][1],
		     _p[3][0], _p[3][1]);
    /*Allocate the array of cells.*/
    _grid->ncells   = nalign - 1;
    _grid->cells[0] = (qr_hom_cell *)malloc((nalign - 1) * (nalign - 1) *
					    sizeof(*_grid->cells[0]));
    for (i = 1; i < _grid->ncells; i++)
	_grid->cells[i] = _grid->cells[i - 1] + _grid->ncells;
    /*Initialize the function pattern mask.*/
    _grid->fpmask =
	(unsigned *)calloc(dim, (dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS) *
				    sizeof(*_grid->fpmask));
    /*Mask out the finder patterns (and separators and format info bits).*/
    qr_sampling_grid_fp_mask_rect(_grid, dim, 0, 0, 9, 9);
    qr_sampling_grid_fp_mask_rect(_grid, dim, 0, dim - 8, 9, 8);
    qr_sampling_grid_fp_mask_rect(_grid, dim, dim - 8, 0, 8, 9);
    /*Mask out the version number bits.*/
    if (_version > 6) {
	qr_sampling_grid_fp_mask_rect(_grid, dim, 0, dim - 11, 6, 3);
	qr_sampling_grid_fp_mask_rect(_grid, dim, dim - 11, 0, 3, 6);
    }
    /*Mask out the timing patterns.*/
    qr_sampling_grid_fp_mask_rect(_grid, dim, 9, 6, dim - 17, 1);
    qr_sampling_grid_fp_mask_rect(_grid, dim, 6, 9, 1, dim - 17);
    /*If we have no alignment patterns (e.g., this is a version 1 code), just use
     the base cell and hope it's good enough.*/
    if (_version < 2)
	memcpy(_grid->cells[0], &base_cell, sizeof(base_cell));
    else {
	qr_point *q;
	qr_point *p;
	int j;
	int k;
	q = (qr_point *)malloc(nalign * nalign * sizeof(*q));
	p = (qr_point *)malloc(nalign * nalign * sizeof(*p));
	/*Initialize the alignment pattern position list.*/
	align_pos[0]	      = 6;
	align_pos[nalign - 1] = dim - 7;
	if (_version > 6) {
	    int d;
	    d = QR_ALIGNMENT_SPACING[_version - 7];
	    for (i = nalign - 1; i-- > 1;)
		align_pos[i] = align_pos[i + 1] - d;
	}
	/*Three of the corners use a finder pattern instead of a separate
       alignment pattern.*/
	q[0][0]			    = 3;
	q[0][1]			    = 3;
	p[0][0]			    = _ul_pos[0];
	p[0][1]			    = _ul_pos[1];
	q[nalign - 1][0]	    = dim - 4;
	q[nalign - 1][1]	    = 3;
	p[nalign - 1][0]	    = _ur_pos[0];
	p[nalign - 1][1]	    = _ur_pos[1];
	q[(nalign - 1) * nalign][0] = 3;
	q[(nalign - 1) * nalign][1] = dim - 4;
	p[(nalign - 1) * nalign][0] = _dl_pos[0];
	p[(nalign - 1) * nalign][1] = _dl_pos[1];
	/*Scan for alignment patterns using a diagonal sweep.*/
	for (k = 1; k < 2 * nalign - 1; k++) {
	    int jmin;
	    int jmax;
	    jmax = QR_MINI(k, nalign - 1) - (k == nalign - 1);
	    jmin = QR_MAXI(0, k - (nalign - 1)) + (k == nalign - 1);
	    for (j = jmin; j <= jmax; j++) {
		qr_hom_cell *cell;
		int u;
		int v;
		int k;
		i	= jmax - (j - jmin);
		k	= i * nalign + j;
		u	= align_pos[j];
		v	= align_pos[i];
		q[k][0] = u;
		q[k][1] = v;
		/*Mask out the alignment pattern.*/
		qr_sampling_grid_fp_mask_rect(_grid, dim, u - 2, v - 2, 5, 5);
		/*Pick a cell to use to govern the alignment pattern search.*/
		if (i > 1 && j > 1) {
		    qr_point p0;
		    qr_point p1;
		    qr_point p2;
		    /*Each predictor is basically a straight-line extrapolation from two
             neighboring alignment patterns (except possibly near the opposing
             finder patterns).*/
		    qr_hom_cell_project(p0, _grid->cells[i - 2] + j - 1, u, v,
					0);
		    qr_hom_cell_project(p1, _grid->cells[i - 2] + j - 2, u, v,
					0);
		    qr_hom_cell_project(p2, _grid->cells[i - 1] + j - 2, u, v,
					0);
		    /*Take the median of the predictions as the search center.*/
		    QR_SORT2I(p0[0], p1[0]);
		    QR_SORT2I(p0[1], p1[1]);
		    QR_SORT2I(p1[0], p2[0]);
		    QR_SORT2I(p1[1], p2[1]);
		    QR_SORT2I(p0[0], p1[0]);
		    QR_SORT2I(p0[1], p1[1]);
		    /*We need a cell that has the target point at a known (u,v) location.
            Since our cells don't have inverses, just construct one from our
             neighboring points.*/
		    cell = _grid->cells[i - 1] + j - 1;
		    qr_hom_cell_init(cell, q[k - nalign - 1][0],
				     q[k - nalign - 1][1], q[k - nalign][0],
				     q[k - nalign][1], q[k - 1][0], q[k - 1][1],
				     q[k][0], q[k][1], p[k - nalign - 1][0],
				     p[k - nalign - 1][1], p[k - nalign][0],
				     p[k - nalign][1], p[k - 1][0], p[k - 1][1],
				     p1[0], p1[1]);
		} else if (i > 1 && j > 0)
		    cell = _grid->cells[i - 2] + j - 1;
		else if (i > 0 && j > 1)
		    cell = _grid->cells[i - 1] + j - 2;
		else
		    cell = &base_cell;
		/*Use a very small search radius.
          A large displacement here usually means a false positive (e.g., when
           the real alignment pattern is damaged or missing), which can
           severely distort the projection.*/
		qr_alignment_pattern_search(p[k], cell, u, v, 2, _img, _width,
					    _height);
		if (i > 0 && j > 0) {
		    qr_hom_cell_init(_grid->cells[i - 1] + j - 1,
				     q[k - nalign - 1][0], q[k - nalign - 1][1],
				     q[k - nalign][0], q[k - nalign][1],
				     q[k - 1][0], q[k - 1][1], q[k][0], q[k][1],
				     p[k - nalign - 1][0], p[k - nalign - 1][1],
				     p[k - nalign][0], p[k - nalign][1],
				     p[k - 1][0], p[k - 1][1], p[k][0],
				     p[k][1]);
		}
	    }
	}
	qr_svg_points("align", p, nalign * nalign);
	free(q);
	free(p);
    }
    /*Set the limits over which each cell is used.*/
    memcpy(_grid->cell_limits, align_pos + 1,
	   (_grid->ncells - 1) * sizeof(*_grid->cell_limits));
    _grid->cell_limits[_grid->ncells - 1] = dim;
    /*Produce a bounding square for the code (to mark finder centers with).
    Because of non-linear distortion, this might not actually bound the code,
     but it should be good enough.
    I don't think it's worth computing a convex hull or anything silly like
     that.*/
    qr_hom_cell_project(_p[0], _grid->cells[0] + 0, -1, -1, 1);
    qr_hom_cell_project(_p[1], _grid->cells[0] + _grid->ncells - 1,
			(dim << 1) - 1, -1, 1);
    qr_hom_cell_project(_p[2], _grid->cells[_grid->ncells - 1] + 0, -1,
			(dim << 1) - 1, 1);
    qr_hom_cell_project(_p[3],
			_grid->cells[_grid->ncells - 1] + _grid->ncells - 1,
			(dim << 1) - 1, (dim << 1) - 1, 1);
    /*Clamp the points somewhere near the image (this is really just in case a
     corner is near the plane at infinity).*/
    for (i = 0; i < 4; i++) {
	_p[i][0] = QR_CLAMPI(-_width << QR_FINDER_SUBPREC, _p[i][0],
			     _width << QR_FINDER_SUBPREC + 1);
	_p[i][1] = QR_CLAMPI(-_height << QR_FINDER_SUBPREC, _p[i][1],
			     _height << QR_FINDER_SUBPREC + 1);
    }
    /*TODO: Make fine adjustments using the timing patterns.
    Possible strategy: scan the timing pattern at QR_ALIGN_SUBPREC (or finer)
     resolution, use dynamic programming to match midpoints between
     transitions to the ideal grid locations.*/
}

static void qr_sampling_grid_clear(qr_sampling_grid *_grid)
{
    free(_grid->fpmask);
    free(_grid->cells[0]);
}

#if defined(QR_DEBUG)
static void qr_sampling_grid_dump(qr_sampling_grid *_grid, int _version,
				  const unsigned char *_img, int _width,
				  int _height)
{
    unsigned char *gimg;
    FILE *fout;
    int dim;
    int u;
    int v;
    int x;
    int y;
    int w;
    int i;
    int j;
    int r;
    int s;
    dim	 = 17 + (_version << 2) + 8 << QR_ALIGN_SUBPREC;
    gimg = (unsigned char *)malloc(dim * dim * sizeof(*gimg));
    for (i = 0; i < dim; i++)
	for (j = 0; j < dim; j++) {
	    qr_hom_cell *cell;
	    if (i >= (4 << QR_ALIGN_SUBPREC) &&
		i <= dim - (5 << QR_ALIGN_SUBPREC) &&
		j >= (4 << QR_ALIGN_SUBPREC) &&
		j <= dim - (5 << QR_ALIGN_SUBPREC) &&
		((!(i & (1 << QR_ALIGN_SUBPREC) - 1)) ^
		 (!(j & (1 << QR_ALIGN_SUBPREC) - 1)))) {
		gimg[i * dim + j] = 0x7F;
	    } else {
		qr_point p;
		u = (j >> QR_ALIGN_SUBPREC) - 4;
		v = (i >> QR_ALIGN_SUBPREC) - 4;
		for (r = 0; r < _grid->ncells - 1; r++)
		    if (u < _grid->cell_limits[r])
			break;
		for (s = 0; s < _grid->ncells - 1; s++)
		    if (v < _grid->cell_limits[s])
			break;
		cell = _grid->cells[s] + r;
		u    = j - (cell->u0 + 4 << QR_ALIGN_SUBPREC);
		v    = i - (cell->v0 + 4 << QR_ALIGN_SUBPREC);
		x    = cell->fwd[0][0] * u + cell->fwd[0][1] * v +
		    (cell->fwd[0][2] << QR_ALIGN_SUBPREC);
		y = cell->fwd[1][0] * u + cell->fwd[1][1] * v +
		    (cell->fwd[1][2] << QR_ALIGN_SUBPREC);
		w = cell->fwd[2][0] * u + cell->fwd[2][1] * v +
		    (cell->fwd[2][2] << QR_ALIGN_SUBPREC);
		qr_hom_cell_fproject(p, cell, x, y, w);
		gimg[i * dim + j] =
		    _img[QR_CLAMPI(0, p[1] >> QR_FINDER_SUBPREC, _height - 1) *
			     _width +
			 QR_CLAMPI(0, p[0] >> QR_FINDER_SUBPREC, _width - 1)];
	    }
	}
    for (v = 0; v < 17 + (_version << 2); v++)
	for (u = 0; u < 17 + (_version << 2); u++) {
	    if (qr_sampling_grid_is_in_fp(_grid, 17 + (_version << 2), u, v)) {
		j			    = u + 4 << QR_ALIGN_SUBPREC;
		i			    = v + 4 << QR_ALIGN_SUBPREC;
		gimg[(i - 1) * dim + j - 1] = 0x7F;
		gimg[(i - 1) * dim + j]	    = 0x7F;
		gimg[(i - 1) * dim + j + 1] = 0x7F;
		gimg[i * dim + j - 1]	    = 0x7F;
		gimg[i * dim + j + 1]	    = 0x7F;
		gimg[(i + 1) * dim + j - 1] = 0x7F;
		gimg[(i + 1) * dim + j]	    = 0x7F;
		gimg[(i + 1) * dim + j + 1] = 0x7F;
	    }
	}
    fout = fopen("grid.png", "wb");
    image_write_png(gimg, dim, dim, fout);
    fclose(fout);
    free(gimg);
}
#endif

/*Generate the data mask corresponding to the given mask pattern.*/
static void qr_data_mask_fill(unsigned *_mask, int _dim, int _pattern)
{
    int stride;
    int i;
    int j;
    stride = _dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS;
    /*Note that we store bits column-wise, since that's how they're read out of
     the grid.*/
    switch (_pattern) {
    /*10101010 i+j+1&1
      01010101
      10101010
      01010101*/
    case 0: {
	int m;
	m = 0x55;
	for (j = 0; j < _dim; j++) {
	    memset(_mask + j * stride, m, stride * sizeof(*_mask));
	    m ^= 0xFF;
	}
    } break;
    /*11111111 i+1&1
      00000000
      11111111
      00000000*/
    case 1:
	memset(_mask, 0x55, _dim * stride * sizeof(*_mask));
	break;
    /*10010010 (j+1)%3&1
      10010010
      10010010
      10010010*/
    case 2: {
	unsigned m;
	m = 0xFF;
	for (j = 0; j < _dim; j++) {
	    memset(_mask + j * stride, m & 0xFF, stride * sizeof(*_mask));
	    m = m << 8 | m >> 16;
	}
    } break;
    /*10010010 (i+j+1)%3&1
      00100100
      01001001
      10010010*/
    case 3: {
	unsigned mi;
	unsigned mj;
	mj = 0;
	for (i = 0; i < (QR_INT_BITS + 2) / 3; i++)
	    mj |= 1 << 3 * i;
	for (j = 0; j < _dim; j++) {
	    mi = mj;
	    for (i = 0; i < stride; i++) {
		_mask[j * stride + i] = mi;
		mi = mi >> QR_INT_BITS % 3 | mi << 3 - QR_INT_BITS % 3;
	    }
	    mj = mj >> 1 | mj << 2;
	}
    } break;
    /*11100011 (i>>1)+(j/3)+1&1
      11100011
      00011100
      00011100*/
    case 4: {
	unsigned m;
	m = 7;
	for (j = 0; j < _dim; j++) {
	    memset(_mask + j * stride, (0xCC ^ -(m & 1)) & 0xFF,
		   stride * sizeof(*_mask));
	    m = m >> 1 | m << 5;
	}
    } break;
    /*11111111 !((i*j)%6)
      10000010
      10010010
      10101010*/
    case 5: {
	for (j = 0; j < _dim; j++) {
	    unsigned m;
	    m = 0;
	    for (i = 0; i < 6; i++)
		m |= !((i * j) % 6) << i;
	    for (i = 6; i < QR_INT_BITS; i <<= 1)
		m |= m << i;
	    for (i = 0; i < stride; i++) {
		_mask[j * stride + i] = m;
		m = m >> QR_INT_BITS % 6 | m << 6 - QR_INT_BITS % 6;
	    }
	}
    } break;
    /*11111111 (i*j)%3+i*j+1&1
      11100011
      11011011
      10101010*/
    case 6: {
	for (j = 0; j < _dim; j++) {
	    unsigned m;
	    m = 0;
	    for (i = 0; i < 6; i++)
		m |= ((i * j) % 3 + i * j + 1 & 1) << i;
	    for (i = 6; i < QR_INT_BITS; i <<= 1)
		m |= m << i;
	    for (i = 0; i < stride; i++) {
		_mask[j * stride + i] = m;
		m = m >> QR_INT_BITS % 6 | m << 6 - QR_INT_BITS % 6;
	    }
	}
    } break;
    /*10101010 (i*j)%3+i+j+1&1
      00011100
      10001110
      01010101*/
    default: {
	for (j = 0; j < _dim; j++) {
	    unsigned m;
	    m = 0;
	    for (i = 0; i < 6; i++)
		m |= ((i * j) % 3 + i + j + 1 & 1) << i;
	    for (i = 6; i < QR_INT_BITS; i <<= 1)
		m |= m << i;
	    for (i = 0; i < stride; i++) {
		_mask[j * stride + i] = m;
		m = m >> QR_INT_BITS % 6 | m << 6 - QR_INT_BITS % 6;
	    }
	}
    } break;
    }
}

static void qr_sampling_grid_sample(const qr_sampling_grid *_grid,
				    unsigned *_data_bits, int _dim,
				    int _fmt_info, const unsigned char *_img,
				    int _width, int _height)
{
    int stride;
    int u0;
    int u1;
    int j;
    /*We initialize the buffer with the data mask and XOR bits into it as we read
     them out of the image instead of unmasking in a separate step.*/
    qr_data_mask_fill(_data_bits, _dim, _fmt_info & 7);
    stride = _dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS;
    u0	   = 0;
    svg_path_start("sampling-grid", 1, 0, 0);
    /*We read data cell-by-cell to avoid having to constantly change which
     projection we're using as we read each bit.
    This (and the position-dependent data mask) is the reason we buffer the
     bits we read instead of converting them directly to codewords here.
    Note that bits are stored column-wise, since that's how we'll scan them.*/
    for (j = 0; j < _grid->ncells; j++) {
	int i;
	int v0;
	int v1;
	u1 = _grid->cell_limits[j];
	v0 = 0;
	for (i = 0; i < _grid->ncells; i++) {
	    qr_hom_cell *cell;
	    int x0;
	    int y0;
	    int w0;
	    int u;
	    int du;
	    int dv;
	    v1	 = _grid->cell_limits[i];
	    cell = _grid->cells[i] + j;
	    du	 = u0 - cell->u0;
	    dv	 = v0 - cell->v0;
	    x0 = cell->fwd[0][0] * du + cell->fwd[0][1] * dv + cell->fwd[0][2];
	    y0 = cell->fwd[1][0] * du + cell->fwd[1][1] * dv + cell->fwd[1][2];
	    w0 = cell->fwd[2][0] * du + cell->fwd[2][1] * dv + cell->fwd[2][2];
	    for (u = u0; u < u1; u++) {
		int x;
		int y;
		int w;
		int v;
		x = x0;
		y = y0;
		w = w0;
		for (v = v0; v < v1; v++) {
		    /*Skip doing all the divisions and bounds checks if the bit is in the
             function pattern.*/
		    if (!qr_sampling_grid_is_in_fp(_grid, _dim, u, v)) {
			qr_point p;
			qr_hom_cell_fproject(p, cell, x, y, w);
			_data_bits[u * stride + (v >> QR_INT_LOGBITS)] ^=
			    qr_img_get_bit(_img, _width, _height, p[0], p[1])
			    << (v & QR_INT_BITS - 1);
			svg_path_moveto(SVG_ABS, p[0], p[1]);
		    }
		    x += cell->fwd[0][1];
		    y += cell->fwd[1][1];
		    w += cell->fwd[2][1];
		}
		x0 += cell->fwd[0][0];
		y0 += cell->fwd[1][0];
		w0 += cell->fwd[2][0];
	    }
	    v0 = v1;
	}
	u0 = u1;
    }
    svg_path_end();
}

/*Arranges the sample bits read by qr_sampling_grid_sample() into bytes and
   groups those bytes into Reed-Solomon blocks.
  The individual block pointers are destroyed by this routine.*/
static void qr_samples_unpack(unsigned char **_blocks, int _nblocks,
			      int _nshort_data, int _nshort_blocks,
			      const unsigned *_data_bits,
			      const unsigned *_fp_mask, int _dim)
{
    unsigned bits;
    int biti;
    int stride;
    int blocki;
    int blockj;
    int i;
    int j;
    stride = _dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS;
    /*If _all_ the blocks are short, don't skip anything (see below).*/
    if (_nshort_blocks >= _nblocks)
	_nshort_blocks = 0;
    /*Scan columns in pairs from right to left.*/
    bits = 0;
    for (blocki = blockj = biti = 0, j = _dim - 1; j > 0; j -= 2) {
	unsigned data1;
	unsigned data2;
	unsigned fp_mask1;
	unsigned fp_mask2;
	int nbits;
	int l;
	/*Scan up a pair of columns.*/
	nbits = (_dim - 1 & QR_INT_BITS - 1) + 1;
	l     = j * stride;
	for (i = stride; i-- > 0;) {
	    data1    = _data_bits[l + i];
	    fp_mask1 = _fp_mask[l + i];
	    data2    = _data_bits[l + i - stride];
	    fp_mask2 = _fp_mask[l + i - stride];
	    while (nbits-- > 0) {
		/*Pull a bit from the right column.*/
		if (!(fp_mask1 >> nbits & 1)) {
		    bits = bits << 1 | data1 >> nbits & 1;
		    biti++;
		}
		/*Pull a bit from the left column.*/
		if (!(fp_mask2 >> nbits & 1)) {
		    bits = bits << 1 | data2 >> nbits & 1;
		    biti++;
		}
		/*If we finished a byte, drop it in a block.*/
		if (biti >= 8) {
		    biti -= 8;
		    *_blocks[blocki++]++ = (unsigned char)(bits >> biti);
		    /*For whatever reason, the long blocks are at the _end_ of the list,
             instead of the beginning.
            Even worse, the extra bytes they get come at the end of the data
             bytes, before the parity bytes.
            Hence the logic here: when we've filled up the data portion of the
             short blocks, skip directly to the long blocks for the next byte.
            It's also the reason we increment _blocks[blocki] on each store,
             instead of just indexing with blockj (after this iteration the
             number of bytes in each block differs).*/
		    if (blocki >= _nblocks)
			blocki = ++blockj == _nshort_data ? _nshort_blocks : 0;
		}
	    }
	    nbits = QR_INT_BITS;
	}
	j -= 2;
	/*Skip the column with the vertical timing pattern.*/
	if (j == 6)
	    j--;
	/*Scan down a pair of columns.*/
	l = j * stride;
	for (i = 0; i < stride; i++) {
	    data1    = _data_bits[l + i];
	    fp_mask1 = _fp_mask[l + i];
	    data2    = _data_bits[l + i - stride];
	    fp_mask2 = _fp_mask[l + i - stride];
	    nbits    = QR_MINI(_dim - (i << QR_INT_LOGBITS), QR_INT_BITS);
	    while (nbits-- > 0) {
		/*Pull a bit from the right column.*/
		if (!(fp_mask1 & 1)) {
		    bits = bits << 1 | data1 & 1;
		    biti++;
		}
		data1 >>= 1;
		fp_mask1 >>= 1;
		/*Pull a bit from the left column.*/
		if (!(fp_mask2 & 1)) {
		    bits = bits << 1 | data2 & 1;
		    biti++;
		}
		data2 >>= 1;
		fp_mask2 >>= 1;
		/*If we finished a byte, drop it in a block.*/
		if (biti >= 8) {
		    biti -= 8;
		    *_blocks[blocki++]++ = (unsigned char)(bits >> biti);
		    /*See comments on the "up" loop for the reason behind this mess.*/
		    if (blocki >= _nblocks)
			blocki = ++blockj == _nshort_data ? _nshort_blocks : 0;
		}
	    }
	}
    }
}

/*Bit reading code blatantly stolen^W^Wadapted from libogg/libtheora (because
   I've already debugged it and I know it works).
  Portions (C) Xiph.Org Foundation 1994-2008, BSD-style license.*/
struct qr_pack_buf {
    const unsigned char *buf;
    int endbyte;
    int endbit;
    int storage;
};

static void qr_pack_buf_init(qr_pack_buf *_b, const unsigned char *_data,
			     int _ndata)
{
    _b->buf	= _data;
    _b->storage = _ndata;
    _b->endbyte = _b->endbit = 0;
}

/*Assumes 0<=_bits<=16.*/
static int qr_pack_buf_read(qr_pack_buf *_b, int _bits)
{
    const unsigned char *p;
    unsigned ret;
    int m;
    int d;
    m = 16 - _bits;
    _bits += _b->endbit;
    d = _b->storage - _b->endbyte;
    if (d <= 2) {
	/*Not the main path.*/
	if (d * 8 < _bits) {
	    _b->endbyte += _bits >> 3;
	    _b->endbit = _bits & 7;
	    return -1;
	}
	/*Special case to avoid reading p[0] below, which might be past the end of
       the buffer; also skips some useless accounting.*/
	else if (!_bits)
	    return 0;
    }
    p	= _b->buf + _b->endbyte;
    ret = p[0] << 8 + _b->endbit;
    if (_bits > 8) {
	ret |= p[1] << _b->endbit;
	if (_bits > 16)
	    ret |= p[2] >> 8 - _b->endbit;
    }
    _b->endbyte += _bits >> 3;
    _b->endbit = _bits & 7;
    return (ret & 0xFFFF) >> m;
}

static int qr_pack_buf_avail(const qr_pack_buf *_b)
{
    return (_b->storage - _b->endbyte << 3) - _b->endbit;
}

/*The characters available in QR_MODE_ALNUM.*/
static const unsigned char QR_ALNUM_TABLE[45] = {
    '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E',
    'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
    'U', 'V', 'W', 'X', 'Y', 'Z', ' ', '$', '%', '*', '+', '-', '.', '/', ':'
};

static int qr_code_data_parse(qr_code_data *_qrdata, int _version,
			      const unsigned char *_data, int _ndata)
{
    qr_pack_buf qpb;
    unsigned self_parity;
    int centries;
    int len_bits_idx;
    /*Entries are stored directly in the struct during parsing.
    Caller cleans up any allocated data on failure.*/
    _qrdata->entries  = NULL;
    _qrdata->nentries = 0;
    _qrdata->sa_size  = 0;
    self_parity	      = 0;
    centries	      = 0;
    /*The versions are divided into 3 ranges that each use a different number of
     bits for length fields.*/
    len_bits_idx = (_version > 9) + (_version > 26);
    qr_pack_buf_init(&qpb, _data, _ndata);
    /*While we have enough bits to read a mode...*/
    while (qr_pack_buf_avail(&qpb) >= 4) {
	qr_code_data_entry *entry;
	int mode;
	mode = qr_pack_buf_read(&qpb, 4);
	/*Mode 0 is a terminator.*/
	if (!mode)
	    break;
	if (_qrdata->nentries >= centries) {
	    centries	     = centries << 1 | 1;
	    _qrdata->entries = (qr_code_data_entry *)realloc(
		_qrdata->entries, centries * sizeof(*_qrdata->entries));
	}
	entry	    = _qrdata->entries + _qrdata->nentries++;
	entry->mode = mode;
	/*Set the buffer to NULL, because if parsing fails, we might try to free it
       on clean-up.*/
	entry->payload.data.buf = NULL;
	switch (mode) {
	    /*The number of bits used to encode the character count for each version
         range and each data mode.*/
	    static const unsigned char LEN_BITS[3][4] = { { 10, 9, 8, 8 },
							  { 12, 11, 16, 10 },
							  { 14, 13, 16, 12 } };
	case QR_MODE_NUM: {
	    unsigned char *buf;
	    unsigned bits;
	    unsigned c;
	    int len;
	    int count;
	    int rem;
	    len = qr_pack_buf_read(&qpb, LEN_BITS[len_bits_idx][0]);
	    if (len < 0)
		return -1;
	    /*Check to see if there are enough bits left now, so we don't have to
           in the decode loop.*/
	    count = len / 3;
	    rem	  = len % 3;
	    if (qr_pack_buf_avail(&qpb) <
		10 * count + 7 * (rem >> 1 & 1) + 4 * (rem & 1))
		return -1;
	    entry->payload.data.buf = buf =
		(unsigned char *)malloc(len * sizeof(*buf));
	    entry->payload.data.len = len;
	    /*Read groups of 3 digits encoded in 10 bits.*/
	    while (count-- > 0) {
		bits = qr_pack_buf_read(&qpb, 10);
		if (bits >= 1000)
		    return -1;
		c = '0' + bits / 100;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
		bits %= 100;
		c = '0' + bits / 10;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
		c      = '0' + bits % 10;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
	    }
	    /*Read the last two digits encoded in 7 bits.*/
	    if (rem > 1) {
		bits = qr_pack_buf_read(&qpb, 7);
		if (bits >= 100)
		    return -1;
		c = '0' + bits / 10;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
		c      = '0' + bits % 10;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
	    }
	    /*Or the last one digit encoded in 4 bits.*/
	    else if (rem) {
		bits = qr_pack_buf_read(&qpb, 4);
		if (bits >= 10)
		    return -1;
		c = '0' + bits;
		self_parity ^= c;
		*buf++ = (unsigned char)c;
	    }
	} break;
	case QR_MODE_ALNUM: {
	    unsigned char *buf;
	    unsigned bits;
	    unsigned c;
	    int len;
	    int count;
	    int rem;
	    len = qr_pack_buf_read(&qpb, LEN_BITS[len_bits_idx][1]);
	    if (len < 0)
		return -1;
	    /*Check to see if there are enough bits left now, so we don't have to
           in the decode loop.*/
	    count = len >> 1;
	    rem	  = len & 1;
	    if (qr_pack_buf_avail(&qpb) < 11 * count + 6 * rem)
		return -1;
	    entry->payload.data.buf = buf =
		(unsigned char *)malloc(len * sizeof(*buf));
	    entry->payload.data.len = len;
	    /*Read groups of two characters encoded in 11 bits.*/
	    while (count-- > 0) {
		bits = qr_pack_buf_read(&qpb, 11);
		if (bits >= 2025)
		    return -1;
		c = QR_ALNUM_TABLE[bits / 45];
		self_parity ^= c;
		*buf++ = (unsigned char)c;
		c      = QR_ALNUM_TABLE[bits % 45];
		self_parity ^= c;
		*buf++ = (unsigned char)c;
		len -= 2;
	    }
	    /*Read the last character encoded in 6 bits.*/
	    if (rem) {
		bits = qr_pack_buf_read(&qpb, 6);
		if (bits >= 45)
		    return -1;
		c = QR_ALNUM_TABLE[bits];
		self_parity ^= c;
		*buf++ = (unsigned char)c;
	    }
	} break;
	/*Structured-append header.*/
	case QR_MODE_STRUCT: {
	    int bits;
	    bits = qr_pack_buf_read(&qpb, 16);
	    if (bits < 0)
		return -1;
	    /*We save a copy of the data in _qrdata for easy reference when
           grouping structured-append codes.
          If for some reason the code has multiple S-A headers, first one wins,
           since it is supposed to come before everything else (TODO: should we
           return an error instead?).*/
	    if (_qrdata->sa_size == 0) {
		_qrdata->sa_index = entry->payload.sa.sa_index =
		    (unsigned char)(bits >> 12 & 0xF);
		_qrdata->sa_size = entry->payload.sa.sa_size =
		    (unsigned char)((bits >> 8 & 0xF) + 1);
		_qrdata->sa_parity = entry->payload.sa.sa_parity =
		    (unsigned char)(bits & 0xFF);
	    }
	} break;
	case QR_MODE_BYTE: {
	    unsigned char *buf;
	    unsigned c;
	    int len;
	    len = qr_pack_buf_read(&qpb, LEN_BITS[len_bits_idx][2]);
	    if (len < 0)
		return -1;
	    /*Check to see if there are enough bits left now, so we don't have to
           in the decode loop.*/
	    if (qr_pack_buf_avail(&qpb) < len << 3)
		return -1;
	    entry->payload.data.buf = buf =
		(unsigned char *)malloc(len * sizeof(*buf));
	    entry->payload.data.len = len;
	    while (len-- > 0) {
		c = qr_pack_buf_read(&qpb, 8);
		self_parity ^= c;
		*buf++ = (unsigned char)c;
	    }
	} break;
	/*FNC1 first position marker.*/
	case QR_MODE_FNC1_1ST:
	    break;
	/*Extended Channel Interpretation data.*/
	case QR_MODE_ECI: {
	    unsigned val;
	    int bits;
	    /*ECI uses a variable-width encoding similar to UTF-8*/
	    bits = qr_pack_buf_read(&qpb, 8);
	    if (bits < 0)
		return -1;
	    /*One byte:*/
	    if (!(bits & 0x80))
		val = bits;
	    /*Two bytes:*/
	    else if (!(bits & 0x40)) {
		val  = bits & 0x3F << 8;
		bits = qr_pack_buf_read(&qpb, 8);
		if (bits < 0)
		    return -1;
		val |= bits;
	    }
	    /*Three bytes:*/
	    else if (!(bits & 0x20)) {
		val  = bits & 0x1F << 16;
		bits = qr_pack_buf_read(&qpb, 16);
		if (bits < 0)
		    return -1;
		val |= bits;
		/*Valid ECI values are 0...999999.*/
		if (val >= 1000000)
		    return -1;
	    }
	    /*Invalid lead byte.*/
	    else
		return -1;
	    entry->payload.eci = val;
	} break;
	case QR_MODE_KANJI: {
	    unsigned char *buf;
	    unsigned bits;
	    int len;
	    len = qr_pack_buf_read(&qpb, LEN_BITS[len_bits_idx][3]);
	    if (len < 0)
		return -1;
	    /*Check to see if there are enough bits left now, so we don't have to
           in the decode loop.*/
	    if (qr_pack_buf_avail(&qpb) < 13 * len)
		return -1;
	    entry->payload.data.buf = buf =
		(unsigned char *)malloc(2 * len * sizeof(*buf));
	    entry->payload.data.len = 2 * len;
	    /*Decode 2-byte SJIS characters encoded in 13 bits.*/
	    while (len-- > 0) {
		bits = qr_pack_buf_read(&qpb, 13);
		bits = (bits / 0xC0 << 8 | bits % 0xC0) + 0x8140;
		if (bits >= 0xA000)
		    bits += 0x4000;
		/*TODO: Are values 0xXX7F, 0xXXFD...0xXXFF always invalid?
            Should we reject them here?*/
		self_parity ^= bits;
		*buf++ = (unsigned char)(bits >> 8);
		*buf++ = (unsigned char)(bits & 0xFF);
	    }
	} break;
	/*FNC1 second position marker.*/
	case QR_MODE_FNC1_2ND: {
	    int bits;
	    /*FNC1 in the 2nd position encodes an Application Indicator in one
           byte, which is either a letter (A...Z or a...z) or a 2-digit number.
          The letters are encoded with their ASCII value plus 100, the numbers
           are encoded directly with their numeric value.
          Values 100...164, 191...196, and 223...255 are invalid, so we reject
           them here.*/
	    bits = qr_pack_buf_read(&qpb, 8);
	    if (!(bits >= 0 && bits < 100 || bits >= 165 && bits < 191 ||
		  bits >= 197 && bits < 223)) {
		return -1;
	    }
	    entry->payload.ai = bits;
	} break;
	/*Unknown mode number:*/
	default: {
	    /*Unfortunately, because we have to understand the format of a mode to
           know how many bits it occupies, we can't skip unknown modes.
          Therefore we have to fail.*/
	    return -1;
	} break;
	}
    }
    /*Store the parity of the data from this code, for S-A.
    The final parity is the 8-bit XOR of all the decoded bytes of literal data.
    We don't combine the 2-byte kanji codes into one byte in the loops above,
     because we can just do it here instead.*/
    _qrdata->self_parity = ((self_parity >> 8) ^ self_parity) & 0xFF;
    /*Success.*/
    _qrdata->entries = (qr_code_data_entry *)realloc(
	_qrdata->entries, _qrdata->nentries * sizeof(*_qrdata->entries));
    return 0;
}

static void qr_code_data_clear(qr_code_data *_qrdata)
{
    int i;
    for (i = 0; i < _qrdata->nentries; i++) {
	if (QR_MODE_HAS_DATA(_qrdata->entries[i].mode)) {
	    free(_qrdata->entries[i].payload.data.buf);
	}
    }
    free(_qrdata->entries);
}

void qr_code_data_list_init(qr_code_data_list *_qrlist)
{
    _qrlist->qrdata  = NULL;
    _qrlist->nqrdata = _qrlist->cqrdata = 0;
}

void qr_code_data_list_clear(qr_code_data_list *_qrlist)
{
    int i;
    for (i = 0; i < _qrlist->nqrdata; i++)
	qr_code_data_clear(_qrlist->qrdata + i);
    free(_qrlist->qrdata);
    qr_code_data_list_init(_qrlist);
}

static void qr_code_data_list_add(qr_code_data_list *_qrlist,
				  qr_code_data *_qrdata)
{
    if (_qrlist->nqrdata >= _qrlist->cqrdata) {
	_qrlist->cqrdata = _qrlist->cqrdata << 1 | 1;
	_qrlist->qrdata	 = (qr_code_data *)realloc(
	     _qrlist->qrdata, _qrlist->cqrdata * sizeof(*_qrlist->qrdata));
    }
    memcpy(_qrlist->qrdata + _qrlist->nqrdata++, _qrdata, sizeof(*_qrdata));
}

#if 0
static const unsigned short QR_NCODEWORDS[40]={
    26,  44,  70, 100, 134, 172, 196, 242, 292, 346,
   404, 466, 532, 581, 655, 733, 815, 901, 991,1085,
  1156,1258,1364,1474,1588,1706,1828,1921,2051,2185,
  2323,2465,2611,2761,2876,3034,3196,3362,3532,3706
};
#endif

/*The total number of codewords in a QR code.*/
static int qr_code_ncodewords(unsigned _version)
{
    unsigned nalign;
    /*This is 24-27 instructions on ARM in thumb mode, or a 26-32 byte savings
     over just using a table (not counting the instructions that would be
     needed to do the table lookup).*/
    if (_version == 1)
	return 26;
    nalign = (_version / 7) + 2;
    return (_version << 4) * (_version + 8) - (5 * nalign) * (5 * nalign - 2) +
	       36 * (_version < 7) + 83 >>
	   3;
}

#if 0
/*The number of parity bytes per Reed-Solomon block for each version and error
   correction level.*/
static const unsigned char QR_RS_NPAR[40][4]={
  { 7,10,13,17},{10,16,22,28},{15,26,18,22},{20,18,26,16},
  {26,24,18,22},{18,16,24,28},{20,18,18,26},{24,22,22,26},
  {30,22,20,24},{18,26,24,28},{20,30,28,24},{24,22,26,28},
  {26,22,24,22},{30,24,20,24},{22,24,30,24},{24,28,24,30},
  {28,28,28,28},{30,26,28,28},{28,26,26,26},{28,26,30,28},
  {28,26,28,30},{28,28,30,24},{30,28,30,30},{30,28,30,30},
  {26,28,30,30},{28,28,28,30},{30,28,30,30},{30,28,30,30},
  {30,28,30,30},{30,28,30,30},{30,28,30,30},{30,28,30,30},
  {30,28,30,30},{30,28,30,30},{30,28,30,30},{30,28,30,30},
  {30,28,30,30},{30,28,30,30},{30,28,30,30},{30,28,30,30}
};
#endif

/*Bulk data for the number of parity bytes per Reed-Solomon block.*/
static const unsigned char QR_RS_NPAR_VALS[71] = {
    /*[ 0]*/ 7,	 10, 13, 17,
    /*[ 4]*/ 10, 16, 22, 28, 26, 26, 26, 22, 24, 22, 22, 26,
    24,		 18, 22,
    /*[19]*/ 15, 26, 18, 22, 24, 30, 24, 20, 24,
    /*[28]*/ 18, 16, 24, 28, 28, 28, 28, 30, 24,
    /*[37]*/ 20, 18, 18, 26, 24, 28, 24, 30, 26, 28, 28, 26,
    28,		 30, 30, 22, 20, 24,
    /*[55]*/ 20, 18, 26, 16,
    /*[59]*/ 20, 30, 28, 24, 22, 26, 28, 26, 30, 28, 30, 30
};

/*An offset into QR_RS_NPAR_DATA for each version that gives the number of
   parity bytes per Reed-Solomon block for each error correction level.*/
static const unsigned char QR_RS_NPAR_OFFS[40] = {
    0,	4,  19, 55, 15, 28, 37, 12, 51, 39, 59, 62, 10, 24,
    22, 41, 31, 44, 7,	65, 47, 33, 67, 67, 48, 32, 67, 67,
    67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67, 67
};

/*The number of Reed-Solomon blocks for each version and error correction
   level.*/
static const unsigned char QR_RS_NBLOCKS[40][4] = {
    { 1, 1, 1, 1 },	{ 1, 1, 1, 1 },	    { 1, 1, 2, 2 },
    { 1, 2, 2, 4 },	{ 1, 2, 4, 4 },	    { 2, 4, 4, 4 },
    { 2, 4, 6, 5 },	{ 2, 4, 6, 6 },	    { 2, 5, 8, 8 },
    { 4, 5, 8, 8 },	{ 4, 5, 8, 11 },    { 4, 8, 10, 11 },
    { 4, 9, 12, 16 },	{ 4, 9, 16, 16 },   { 6, 10, 12, 18 },
    { 6, 10, 17, 16 },	{ 6, 11, 16, 19 },  { 6, 13, 18, 21 },
    { 7, 14, 21, 25 },	{ 8, 16, 20, 25 },  { 8, 17, 23, 25 },
    { 9, 17, 23, 34 },	{ 9, 18, 25, 30 },  { 10, 20, 27, 32 },
    { 12, 21, 29, 35 }, { 12, 23, 34, 37 }, { 12, 25, 34, 40 },
    { 13, 26, 35, 42 }, { 14, 28, 38, 45 }, { 15, 29, 40, 48 },
    { 16, 31, 43, 51 }, { 17, 33, 45, 54 }, { 18, 35, 48, 57 },
    { 19, 37, 51, 60 }, { 19, 38, 53, 63 }, { 20, 40, 56, 66 },
    { 21, 43, 59, 70 }, { 22, 45, 62, 74 }, { 24, 47, 65, 77 },
    { 25, 49, 68, 81 }
};

/*Attempts to fully decode a QR code.
  _qrdata:   Returns the parsed code data.
  _gf:       Used for Reed-Solomon error correction.
  _ul_pos:   The location of the UL finder pattern.
  _ur_pos:   The location of the UR finder pattern.
  _dl_pos:   The location of the DL finder pattern.
  _version:  The (decoded) version number.
  _fmt_info: The decoded format info.
  _img:      The binary input image.
  _width:    The width of the input image.
  _height:   The height of the input image.
  Return: 0 on success, or a negative value on error.*/
static int qr_code_decode(qr_code_data *_qrdata, const rs_gf256 *_gf,
			  const qr_point _ul_pos, const qr_point _ur_pos,
			  const qr_point _dl_pos, int _version, int _fmt_info,
			  const unsigned char *_img, int _width, int _height)
{
    qr_sampling_grid grid;
    unsigned *data_bits;
    unsigned char **blocks;
    unsigned char *block_data;
    int nblocks;
    int nshort_blocks;
    int ncodewords;
    int block_sz;
    int ecc_level;
    int ndata;
    int npar;
    int dim;
    int ret;
    int i;
    /*Read the bits out of the image.*/
    qr_sampling_grid_init(&grid, _version, _ul_pos, _ur_pos, _dl_pos,
			  _qrdata->bbox, _img, _width, _height);
#if defined(QR_DEBUG)
    qr_sampling_grid_dump(&grid, _version, _img, _width, _height);
#endif
    dim	      = 17 + (_version << 2);
    data_bits = (unsigned *)malloc(
	dim * (dim + QR_INT_BITS - 1 >> QR_INT_LOGBITS) * sizeof(*data_bits));
    qr_sampling_grid_sample(&grid, data_bits, dim, _fmt_info, _img, _width,
			    _height);
    /*Group those bits into Reed-Solomon codewords.*/
    ecc_level  = (_fmt_info >> 3) ^ 1;
    nblocks    = QR_RS_NBLOCKS[_version - 1][ecc_level];
    npar       = *(QR_RS_NPAR_VALS + QR_RS_NPAR_OFFS[_version - 1] + ecc_level);
    ncodewords = qr_code_ncodewords(_version);
    block_sz   = ncodewords / nblocks;
    nshort_blocks = nblocks - (ncodewords % nblocks);
    blocks	  = (unsigned char **)malloc(nblocks * sizeof(*blocks));
    block_data	  = (unsigned char *)malloc(ncodewords * sizeof(*block_data));
    blocks[0]	  = block_data;
    for (i = 1; i < nblocks; i++)
	blocks[i] = blocks[i - 1] + block_sz + (i > nshort_blocks);
    qr_samples_unpack(blocks, nblocks, block_sz - npar, nshort_blocks,
		      data_bits, grid.fpmask, dim);
    qr_sampling_grid_clear(&grid);
    free(blocks);
    free(data_bits);
    /*Perform the error correction.*/
    ndata      = 0;
    ncodewords = 0;
    ret	       = 0;
    for (i = 0; i < nblocks; i++) {
	int block_szi;
	int ndatai;
	block_szi = block_sz + (i >= nshort_blocks);
	ret = rs_correct(_gf, QR_M0, block_data + ncodewords, block_szi, npar,
			 NULL, 0);
	zprintf(1, "Number of errors corrected: %i%s\n", ret,
		ret < 0 ? " (data irrecoverable)" : "");
	/*For version 1 symbols and version 2-L and 3-L symbols, we aren't allowed
       to use all the parity bytes for correction.
      They are instead used to improve detection.
      Version 1-L reserves 3 parity bytes for detection.
      Versions 1-M and 2-L reserve 2 parity bytes for detection.
      Versions 1-Q, 1-H, and 3-L reserve 1 parity byte for detection.
      We can ignore the version 3-L restriction because it has an odd number of
       parity bytes, and we don't support erasure detection.*/
	if (ret < 0 || _version == 1 && ret > ecc_level + 1 << 1 ||
	    _version == 2 && ecc_level == 0 && ret > 4) {
	    ret = -1;
	    break;
	}
	ndatai = block_szi - npar;
	memmove(block_data + ndata, block_data + ncodewords,
		ndatai * sizeof(*block_data));
	ncodewords += block_szi;
	ndata += ndatai;
    }
    /*Parse the corrected bitstream.*/
    if (ret >= 0) {
	ret = qr_code_data_parse(_qrdata, _version, block_data, ndata);
	/*We could return any partially decoded data, but then we'd have to have
       API support for that; a mode ignoring ECC errors might also be useful.*/
	if (ret < 0)
	    qr_code_data_clear(_qrdata);
	_qrdata->version   = _version;
	_qrdata->ecc_level = ecc_level;
    }
    free(block_data);
    return ret;
}

/*Searches for an arrangement of these three finder centers that yields a valid
   configuration.
  _c: On input, the three finder centers to consider in any order.
  Return: The detected version number, or a negative value on error.*/
static int qr_reader_try_configuration(qr_reader *_reader,
				       qr_code_data *_qrdata,
				       const unsigned char *_img, int _width,
				       int _height, qr_finder_center *_c[3])
{
    int ci[7];
    unsigned maxd;
    int ccw;
    int i0;
    int i;
    /*Sort the points in counter-clockwise order.*/
    ccw = qr_point_ccw(_c[0]->pos, _c[1]->pos, _c[2]->pos);
    /*Colinear points can't be the corners of a quadrilateral.*/
    if (!ccw)
	return -1;
    /*Include a few extra copies of the cyclical list to avoid mods.*/
    ci[6] = ci[3] = ci[0] = 0;
    ci[4] = ci[1] = 1 + (ccw < 0);
    ci[5] = ci[2] = 2 - (ccw < 0);
    /*Assume the points farthest from each other are the opposite corners, and
     find the top-left point.*/
    maxd = qr_point_distance2(_c[1]->pos, _c[2]->pos);
    i0	 = 0;
    for (i = 1; i < 3; i++) {
	unsigned d;
	d = qr_point_distance2(_c[ci[i + 1]]->pos, _c[ci[i + 2]]->pos);
	if (d > maxd) {
	    i0	 = i;
	    maxd = d;
	}
    }
    /*However, try all three possible orderings, just to be sure (a severely
     skewed projection could move opposite corners closer than adjacent).*/
    for (i = i0; i < i0 + 3; i++) {
	qr_aff aff;
	qr_hom hom;
	qr_finder ul;
	qr_finder ur;
	qr_finder dl;
	qr_point bbox[4];
	int res;
	int ur_version;
	int dl_version;
	int fmt_info;
	ul.c = _c[ci[i]];
	ur.c = _c[ci[i + 1]];
	dl.c = _c[ci[i + 2]];
	/*Estimate the module size and version number from the two opposite corners.
      The module size is not constant in the image, so we compute an affine
       projection from the three points we have to a square domain, and
       estimate it there.
      Although it should be the same along both axes, we keep separate
       estimates to account for any remaining projective distortion.*/
	res = QR_INT_BITS - 2 - QR_FINDER_SUBPREC -
	      qr_ilog(QR_MAXI(_width, _height) - 1);
	qr_aff_init(&aff, ul.c->pos, ur.c->pos, dl.c->pos, res);
	qr_aff_unproject(ur.o, &aff, ur.c->pos[0], ur.c->pos[1]);
	qr_finder_edge_pts_aff_classify(&ur, &aff);
	if (qr_finder_estimate_module_size_and_version(&ur, 1 << res,
						       1 << res) < 0)
	    continue;
	qr_aff_unproject(dl.o, &aff, dl.c->pos[0], dl.c->pos[1]);
	qr_finder_edge_pts_aff_classify(&dl, &aff);
	if (qr_finder_estimate_module_size_and_version(&dl, 1 << res,
						       1 << res) < 0)
	    continue;
	/*If the estimated versions are significantly different, reject the
       configuration.*/
	if (abs(ur.eversion[1] - dl.eversion[0]) > QR_LARGE_VERSION_SLACK)
	    continue;
	qr_aff_unproject(ul.o, &aff, ul.c->pos[0], ul.c->pos[1]);
	qr_finder_edge_pts_aff_classify(&ul, &aff);
	if (qr_finder_estimate_module_size_and_version(&ul, 1 << res,
						       1 << res) < 0 ||
	    abs(ul.eversion[1] - ur.eversion[1]) > QR_LARGE_VERSION_SLACK ||
	    abs(ul.eversion[0] - dl.eversion[0]) > QR_LARGE_VERSION_SLACK) {
	    continue;
	}
#if defined(QR_DEBUG)
	qr_finder_dump_aff_undistorted(&ul, &ur, &dl, &aff, _img, _width,
				       _height);
#endif
	/*If we made it this far, upgrade the affine homography to a full
       homography.*/
	if (qr_hom_fit(&hom, &ul, &ur, &dl, bbox, &aff, &_reader->isaac, _img,
		       _width, _height) < 0) {
	    continue;
	}
	memcpy(_qrdata->bbox, bbox, sizeof(bbox));
	qr_hom_unproject(ul.o, &hom, ul.c->pos[0], ul.c->pos[1]);
	qr_hom_unproject(ur.o, &hom, ur.c->pos[0], ur.c->pos[1]);
	qr_hom_unproject(dl.o, &hom, dl.c->pos[0], dl.c->pos[1]);
	qr_finder_edge_pts_hom_classify(&ur, &hom);
	if (qr_finder_estimate_module_size_and_version(&ur, ur.o[0] - ul.o[0],
						       ur.o[0] - ul.o[0]) < 0) {
	    continue;
	}
	qr_finder_edge_pts_hom_classify(&dl, &hom);
	if (qr_finder_estimate_module_size_and_version(&dl, dl.o[1] - ul.o[1],
						       dl.o[1] - ul.o[1]) < 0) {
	    continue;
	}
#if defined(QR_DEBUG)
	qr_finder_dump_hom_undistorted(&ul, &ur, &dl, &hom, _img, _width,
				       _height);
#endif
	/*If we have a small version (less than 7), there's no encoded version
       information.
      If the estimated version on the two corners matches and is sufficiently
       small, we assume this is the case.*/
	if (ur.eversion[1] == dl.eversion[0] && ur.eversion[1] < 7) {
	    /*We used to do a whole bunch of extra geometric checks for small
         versions, because with just an affine correction, it was fairly easy
         to estimate two consistent module sizes given a random configuration.
        However, now that we're estimating a full homography, these appear to
         be unnecessary.*/
#if 0
      static const signed char LINE_TESTS[12][6]={
        /*DL left, UL > 0, UR > 0*/
        {2,0,0, 1,1, 1},
        /*DL right, UL > 0, UR < 0*/
        {2,1,0, 1,1,-1},
        /*UR top, UL > 0, DL > 0*/
        {1,2,0, 1,2, 1},
        /*UR bottom, UL > 0, DL < 0*/
        {1,3,0, 1,2,-1},
        /*UR left, DL < 0, UL < 0*/
        {1,0,2,-1,0,-1},
        /*UR right, DL > 0, UL > 0*/
        {1,1,2, 1,0, 1},
        /*DL top, UR < 0, UL < 0*/
        {2,2,1,-1,0,-1},
        /*DL bottom, UR > 0, UL > 0*/
        {2,3,1, 1,0, 1},
        /*UL left, DL > 0, UR > 0*/
        {0,0,2, 1,1, 1},
        /*UL right, DL > 0, UR < 0*/
        {0,1,2, 1,1,-1},
        /*UL top, UR > 0, DL > 0*/
        {0,2,1, 1,2, 1},
        /*UL bottom, UR > 0, DL < 0*/
        {0,3,1, 1,2,-1}
      };
      qr_finder *f[3];
      int        j;
      /*Start by decoding the format information.
        This is cheap, but unlikely to reject invalid configurations.
        56.25% of all bitstrings are valid, and we mix and match several pieces
         until we find a valid combination, so our real chances of finding a
         valid codeword in random bits are even higher.*/
      fmt_info=qr_finder_fmt_info_decode(&ul,&ur,&dl,&aff,_img,_width,_height);
      if(fmt_info<0)continue;
      /*Now we fit lines to the edges of each finder pattern and check to make
         sure the centers of the other finder patterns lie on the proper side.*/
      f[0]=&ul;
      f[1]=&ur;
      f[2]=&dl;
      for(j=0;j<12;j++){
        const signed char *t;
        qr_line            l0;
        int               *p;
        t=LINE_TESTS[j];
        qr_finder_ransac(f[t[0]],&aff,&_reader->isaac,t[1]);
        /*We may not have enough points to fit a line accurately here.
          If not, we just skip the test.*/
        if(qr_line_fit_finder_edge(l0,f[t[0]],t[1],res)<0)continue;
        p=f[t[2]]->c->pos;
        if(qr_line_eval(l0,p[0],p[1])*t[3]<0)break;
        p=f[t[4]]->c->pos;
        if(qr_line_eval(l0,p[0],p[1])*t[5]<0)break;
      }
      if(j<12)continue;
      /*All tests passed.*/
#endif
	    ur_version = ur.eversion[1];
	} else {
	    /*If the estimated versions are significantly different, reject the
         configuration.*/
	    if (abs(ur.eversion[1] - dl.eversion[0]) > QR_LARGE_VERSION_SLACK)
		continue;
	    /*Otherwise we try to read the actual version data from the image.
        If the real version is not sufficiently close to our estimated version,
         then we assume there was an unrecoverable decoding error (so many bit
         errors we were within 3 errors of another valid code), and throw that
         value away.
        If no decoded version could be sufficiently close, we don't even try.*/
	    if (ur.eversion[1] >= 7 - QR_LARGE_VERSION_SLACK) {
		ur_version = qr_finder_version_decode(&ur, &hom, _img, _width,
						      _height, 0);
		if (abs(ur_version - ur.eversion[1]) > QR_LARGE_VERSION_SLACK)
		    ur_version = -1;
	    } else
		ur_version = -1;
	    if (dl.eversion[0] >= 7 - QR_LARGE_VERSION_SLACK) {
		dl_version = qr_finder_version_decode(&dl, &hom, _img, _width,
						      _height, 1);
		if (abs(dl_version - dl.eversion[0]) > QR_LARGE_VERSION_SLACK)
		    dl_version = -1;
	    } else
		dl_version = -1;
	    /*If we got at least one valid version, or we got two and they match,
         then we found a valid configuration.*/
	    if (ur_version >= 0) {
		if (dl_version >= 0 && dl_version != ur_version)
		    continue;
	    } else if (dl_version < 0)
		continue;
	    else
		ur_version = dl_version;
	}
	qr_finder_edge_pts_hom_classify(&ul, &hom);
	if (qr_finder_estimate_module_size_and_version(&ul, ur.o[0] - dl.o[0],
						       dl.o[1] - ul.o[1]) < 0 ||
	    abs(ul.eversion[1] - ur.eversion[1]) > QR_SMALL_VERSION_SLACK ||
	    abs(ul.eversion[0] - dl.eversion[0]) > QR_SMALL_VERSION_SLACK) {
	    continue;
	}
	fmt_info = qr_finder_fmt_info_decode(&ul, &ur, &dl, &hom, _img, _width,
					     _height);
	if (fmt_info < 0 ||
	    qr_code_decode(_qrdata, &_reader->gf, ul.c->pos, ur.c->pos,
			   dl.c->pos, ur_version, fmt_info, _img, _width,
			   _height) < 0) {
	    /*The code may be flipped.
        Try again, swapping the UR and DL centers.
        We should get a valid version either way, so it's relatively cheap to
         check this, as we've already filtered out a lot of invalid
         configurations.*/
	    QR_SWAP2I(hom.inv[0][0], hom.inv[1][0]);
	    QR_SWAP2I(hom.inv[0][1], hom.inv[1][1]);
	    QR_SWAP2I(hom.fwd[0][0], hom.fwd[0][1]);
	    QR_SWAP2I(hom.fwd[1][0], hom.fwd[1][1]);
	    QR_SWAP2I(hom.fwd[2][0], hom.fwd[2][1]);
	    QR_SWAP2I(ul.o[0], ul.o[1]);
	    QR_SWAP2I(ul.size[0], ul.size[1]);
	    QR_SWAP2I(ur.o[0], ur.o[1]);
	    QR_SWAP2I(ur.size[0], ur.size[1]);
	    QR_SWAP2I(dl.o[0], dl.o[1]);
	    QR_SWAP2I(dl.size[0], dl.size[1]);
#if defined(QR_DEBUG)
	    qr_finder_dump_hom_undistorted(&ul, &dl, &ur, &hom, _img, _width,
					   _height);
#endif
	    fmt_info = qr_finder_fmt_info_decode(&ul, &dl, &ur, &hom, _img,
						 _width, _height);
	    if (fmt_info < 0)
		continue;
	    QR_SWAP2I(bbox[1][0], bbox[2][0]);
	    QR_SWAP2I(bbox[1][1], bbox[2][1]);
	    memcpy(_qrdata->bbox, bbox, sizeof(bbox));
	    if (qr_code_decode(_qrdata, &_reader->gf, ul.c->pos, dl.c->pos,
			       ur.c->pos, ur_version, fmt_info, _img, _width,
			       _height) < 0) {
		continue;
	    }
	}
	return ur_version;
    }
    return -1;
}

void qr_reader_match_centers(qr_reader *_reader, qr_code_data_list *_qrlist,
			     qr_finder_center *_centers, int _ncenters,
			     const unsigned char *_img, int _width, int _height)
{
    /*The number of centers should be small, so an O(n^3) exhaustive search of
     which ones go together should be reasonable.*/
    unsigned char *mark;
    int nfailures_max;
    int nfailures;
    int i;
    int j;
    int k;
    mark	  = (unsigned char *)calloc(_ncenters, sizeof(*mark));
    nfailures_max = QR_MAXI(8192, _width * _height >> 9);
    nfailures	  = 0;
    for (i = 0; i < _ncenters; i++) {
	/*TODO: We might be able to accelerate this step significantly by
       considering the remaining finder centers in a more intelligent order,
       based on the first finder center we just chose.*/
    for (j = i + 1; i <_ncenters && !mark[i] && j < _ncenters; j++) {
        for (k = j + 1; j<_ncenters && !mark[j] && k < _ncenters; k++)
		if (!mark[k]) {
		    qr_finder_center *c[3];
		    qr_code_data qrdata;
		    int version;
		    c[0]    = _centers + i;
		    c[1]    = _centers + j;
		    c[2]    = _centers + k;
		    version = qr_reader_try_configuration(_reader, &qrdata,
							  _img, _width, _height,
							  c);
		    if (version >= 0) {
			int ninside;
			int l;
			/*Add the data to the list.*/
			qr_code_data_list_add(_qrlist, &qrdata);
			/*Convert the bounding box we're returning to the user to normal
             image coordinates.*/
			for (l = 0; l < 4; l++) {
			    _qrlist->qrdata[_qrlist->nqrdata - 1].bbox[l][0] >>=
				QR_FINDER_SUBPREC;
			    _qrlist->qrdata[_qrlist->nqrdata - 1].bbox[l][1] >>=
				QR_FINDER_SUBPREC;
			}
			/*Mark these centers as used.*/
			mark[i] = mark[j] = mark[k] = 1;
			/*Find any other finder centers located inside this code.*/
			for (l = ninside = 0; l < _ncenters; l++)
			    if (!mark[l]) {
				if (qr_point_ccw(qrdata.bbox[0], qrdata.bbox[1],
						 _centers[l].pos) >= 0 &&
				    qr_point_ccw(qrdata.bbox[1], qrdata.bbox[3],
						 _centers[l].pos) >= 0 &&
				    qr_point_ccw(qrdata.bbox[3], qrdata.bbox[2],
						 _centers[l].pos) >= 0 &&
				    qr_point_ccw(qrdata.bbox[2], qrdata.bbox[0],
						 _centers[l].pos) >= 0) {
				    mark[l] = 2;
				    ninside++;
				}
			    }
			if (ninside >= 3) {
			    /*We might have a "Double QR": a code inside a code.
              Copy the relevant centers to a new array and do a search confined
               to that subset.*/
			    qr_finder_center *inside;
			    inside = (qr_finder_center *)malloc(
				ninside * sizeof(*inside));
			    for (l = ninside = 0; l < _ncenters; l++) {
				if (mark[l] == 2)
				    *&inside[ninside++] = *&_centers[l];
			    }
			    qr_reader_match_centers(_reader, _qrlist, inside,
						    ninside, _img, _width,
						    _height);
			    free(inside);
			}
			/*Mark _all_ such centers used: codes cannot partially overlap.*/
			for (l = 0; l < _ncenters; l++)
			    if (mark[l] == 2)
				mark[l] = 1;
			nfailures = 0;
		    } else if (++nfailures > nfailures_max) {
			/*Give up.
            We're unlikely to find a valid code in all this clutter, and we
             could spent quite a lot of time trying.*/
			i = j = k = _ncenters;
		    }
		}
	}
    }
    free(mark);
}

int _zbar_qr_found_line(qr_reader *reader, int dir, const qr_finder_line *line)
{
    /* minimally intrusive brute force version */
    qr_finder_lines *lines = &reader->finder_lines[dir];

    if (lines->nlines >= lines->clines) {
	lines->clines *= 2;
	lines->lines =
	    realloc(lines->lines, ++lines->clines * sizeof(*lines->lines));
    }

    memcpy(lines->lines + lines->nlines++, line, sizeof(*line));

    return (0);
}

static inline void qr_svg_centers(const qr_finder_center *centers, int ncenters)
{
    int i, j;
    svg_path_start("centers", 1, 0, 0);
    for (i = 0; i < ncenters; i++)
	svg_path_moveto(SVG_ABS, centers[i].pos[0], centers[i].pos[1]);
    svg_path_end();

    svg_path_start("edge-pts", 1, 0, 0);
    for (i = 0; i < ncenters; i++) {
	const qr_finder_center *cen = centers + i;
	for (j = 0; j < cen->nedge_pts; j++)
	    svg_path_moveto(SVG_ABS, cen->edge_pts[j].pos[0],
			    cen->edge_pts[j].pos[1]);
    }
    svg_path_end();
}

int _zbar_qr_decode(qr_reader *reader, zbar_image_scanner_t *iscn,
		    zbar_image_t *img)
{
    int nqrdata			= 0, ncenters;
    qr_finder_edge_pt *edge_pts = NULL;
    qr_finder_center *centers	= NULL;

    if (reader->finder_lines[0].nlines < 9 ||
	reader->finder_lines[1].nlines < 9)
	return (0);

    svg_group_start("finder", 0, 1. / (1 << QR_FINDER_SUBPREC), 0, 0, 0);

    ncenters = qr_finder_centers_locate(&centers, &edge_pts, reader, 0, 0);

    zprintf(14, "%dx%d finders, %d centers:\n", reader->finder_lines[0].nlines,
	    reader->finder_lines[1].nlines, ncenters);
    qr_svg_centers(centers, ncenters);

    if (ncenters >= 3) {
	void *bin = qr_binarize(img->data, img->width, img->height);

	qr_code_data_list qrlist;
	qr_code_data_list_init(&qrlist);

	qr_reader_match_centers(reader, &qrlist, centers, ncenters, bin,
				img->width, img->height);

	if (qrlist.nqrdata > 0)
	    nqrdata = qr_code_data_list_extract_text(&qrlist, iscn, img);

	qr_code_data_list_clear(&qrlist);
	free(bin);
    }
    svg_group_end();

    if (centers)
	free(centers);
    if (edge_pts)
	free(edge_pts);
    return (nqrdata);
}