bitcoin-atm
bitcoin atm for pyc inc.
git clone https://9o.is/git/bitcoin-atm.git
grid.js
(4722B)
1 /*
2 Ported to JavaScript by Lazar Laszlo 2011
3
4 lazarsoft@gmail.com, www.lazarsoft.info
5
6 */
7
8 /*
9 *
10 * Copyright 2007 ZXing authors
11 *
12 * Licensed under the Apache License, Version 2.0 (the "License");
13 * you may not use this file except in compliance with the License.
14 * You may obtain a copy of the License at
15 *
16 * http://www.apache.org/licenses/LICENSE-2.0
17 *
18 * Unless required by applicable law or agreed to in writing, software
19 * distributed under the License is distributed on an "AS IS" BASIS,
20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21 * See the License for the specific language governing permissions and
22 * limitations under the License.
23 */
24
25
26 GridSampler = {};
27
28 GridSampler.checkAndNudgePoints=function( image, points)
29 {
30 var width = qrcode.width;
31 var height = qrcode.height;
32 // Check and nudge points from start until we see some that are OK:
33 var nudged = true;
34 for (var offset = 0; offset < points.length && nudged; offset += 2)
35 {
36 var x = Math.floor (points[offset]);
37 var y = Math.floor( points[offset + 1]);
38 if (x < - 1 || x > width || y < - 1 || y > height)
39 {
40 throw "Error.checkAndNudgePoints ";
41 }
42 nudged = false;
43 if (x == - 1)
44 {
45 points[offset] = 0.0;
46 nudged = true;
47 }
48 else if (x == width)
49 {
50 points[offset] = width - 1;
51 nudged = true;
52 }
53 if (y == - 1)
54 {
55 points[offset + 1] = 0.0;
56 nudged = true;
57 }
58 else if (y == height)
59 {
60 points[offset + 1] = height - 1;
61 nudged = true;
62 }
63 }
64 // Check and nudge points from end:
65 nudged = true;
66 for (var offset = points.length - 2; offset >= 0 && nudged; offset -= 2)
67 {
68 var x = Math.floor( points[offset]);
69 var y = Math.floor( points[offset + 1]);
70 if (x < - 1 || x > width || y < - 1 || y > height)
71 {
72 throw "Error.checkAndNudgePoints ";
73 }
74 nudged = false;
75 if (x == - 1)
76 {
77 points[offset] = 0.0;
78 nudged = true;
79 }
80 else if (x == width)
81 {
82 points[offset] = width - 1;
83 nudged = true;
84 }
85 if (y == - 1)
86 {
87 points[offset + 1] = 0.0;
88 nudged = true;
89 }
90 else if (y == height)
91 {
92 points[offset + 1] = height - 1;
93 nudged = true;
94 }
95 }
96 }
97
98
99
100 GridSampler.sampleGrid3=function( image, dimension, transform)
101 {
102 var bits = new BitMatrix(dimension);
103 var points = new Array(dimension << 1);
104 for (var y = 0; y < dimension; y++)
105 {
106 var max = points.length;
107 var iValue = y + 0.5;
108 for (var x = 0; x < max; x += 2)
109 {
110 points[x] = (x >> 1) + 0.5;
111 points[x + 1] = iValue;
112 }
113 transform.transformPoints1(points);
114 // Quick check to see if points transformed to something inside the image;
115 // sufficient to check the endpoints
116 GridSampler.checkAndNudgePoints(image, points);
117 try
118 {
119 for (var x = 0; x < max; x += 2)
120 {
121 var xpoint = (Math.floor( points[x]) * 4) + (Math.floor( points[x + 1]) * qrcode.width * 4);
122 var bit = image[Math.floor( points[x])+ qrcode.width* Math.floor( points[x + 1])];
123 qrcode.imagedata.data[xpoint] = bit?255:0;
124 qrcode.imagedata.data[xpoint+1] = bit?255:0;
125 qrcode.imagedata.data[xpoint+2] = 0;
126 qrcode.imagedata.data[xpoint+3] = 255;
127 //bits[x >> 1][ y]=bit;
128 if(bit)
129 bits.set_Renamed(x >> 1, y);
130 }
131 }
132 catch ( aioobe)
133 {
134 // This feels wrong, but, sometimes if the finder patterns are misidentified, the resulting
135 // transform gets "twisted" such that it maps a straight line of points to a set of points
136 // whose endpoints are in bounds, but others are not. There is probably some mathematical
137 // way to detect this about the transformation that I don't know yet.
138 // This results in an ugly runtime exception despite our clever checks above -- can't have
139 // that. We could check each point's coordinates but that feels duplicative. We settle for
140 // catching and wrapping ArrayIndexOutOfBoundsException.
141 throw "Error.checkAndNudgePoints";
142 }
143 }
144 return bits;
145 }
146
147 GridSampler.sampleGridx=function( image, dimension, p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY)
148 {
149 var transform = PerspectiveTransform.quadrilateralToQuadrilateral(p1ToX, p1ToY, p2ToX, p2ToY, p3ToX, p3ToY, p4ToX, p4ToY, p1FromX, p1FromY, p2FromX, p2FromY, p3FromX, p3FromY, p4FromX, p4FromY);
150
151 return GridSampler.sampleGrid3(image, dimension, transform);
152 }