lycheedev 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. package/bin/lycheedev.js +21 -6
  2. package/package.json +1 -1
  3. package/vendor/addon/Core/Bootstrap.lua +131 -131
  4. package/vendor/addon/Core/Clients/Mainline.lua +9 -9
  5. package/vendor/addon/Core/Clients/Mists.lua +9 -9
  6. package/vendor/addon/Core/Clients/Titan.lua +9 -9
  7. package/vendor/addon/Core/Compatibility.lua +43 -43
  8. package/vendor/addon/Core/Database.lua +859 -859
  9. package/vendor/addon/Core/Inspector.lua +647 -647
  10. package/vendor/addon/Core/Locale.lua +307 -307
  11. package/vendor/addon/Core/Locale_enUS.lua +318 -318
  12. package/vendor/addon/Core/Safety.lua +42 -42
  13. package/vendor/addon/Core/Serializer.lua +324 -324
  14. package/vendor/addon/Libs/AutomationQR.lua +1176 -1176
  15. package/vendor/addon/Lychee Dev_Mainline.toc +43 -43
  16. package/vendor/addon/Lychee Dev_Mists.toc +43 -43
  17. package/vendor/addon/Lychee Dev_Wrath.toc +43 -43
  18. package/vendor/addon/Modules/Automation/Controller.lua +757 -757
  19. package/vendor/addon/Modules/Automation/Report.lua +409 -409
  20. package/vendor/addon/Modules/Automation/auto/auto.lua +12 -12
  21. package/vendor/addon/Modules/Diagnostics.lua +238 -238
  22. package/vendor/addon/Modules/Events/Catalog.lua +184 -184
  23. package/vendor/addon/Modules/Events/CatalogData_Mainline.lua +1788 -1788
  24. package/vendor/addon/Modules/Events/CatalogData_Mists.lua +1489 -1489
  25. package/vendor/addon/Modules/Events/CatalogData_Titan.lua +1492 -1492
  26. package/vendor/addon/Modules/Events/Monitor.lua +181 -181
  27. package/vendor/addon/Modules/FunctionTrace.lua +85 -85
  28. package/vendor/addon/Modules/ObjectInspector.lua +329 -329
  29. package/vendor/addon/UI/AutomationOverlay.lua +183 -183
  30. package/vendor/addon/UI/Export.lua +140 -140
  31. package/vendor/addon/UI/Features.lua +890 -890
  32. package/vendor/addon/UI/MainWindow.lua +1145 -1145
  33. package/vendor/addon/UI/Pages/About.lua +223 -223
  34. package/vendor/addon/UI/Pages/Automation.lua +417 -417
  35. package/vendor/addon/UI/Pages/Diagnostics.lua +289 -289
  36. package/vendor/addon/UI/Pages/ExportRecords.lua +387 -387
  37. package/vendor/addon/UI/Pages/Object.lua +573 -573
  38. package/vendor/addon/UI/Pages/Trace.lua +165 -165
  39. package/vendor/skill/SKILL.md +94 -94
  40. package/vendor/skill/agents/openai.yaml +4 -4
  41. package/vendor/skill/references/automation.md +327 -327
  42. package/vendor/skill/references/runtime-investigations.md +97 -97
  43. package/vendor/skill/scripts/automation/__init__.py +1 -1
  44. package/vendor/skill/scripts/automation/registry.py +516 -516
  45. package/vendor/skill/scripts/automation/saved_variables.py +438 -438
  46. package/vendor/skill/scripts/automation/session.py +207 -207
  47. package/vendor/skill/scripts/automation/windows.py +573 -573
  48. package/vendor/skill/scripts/automation.py +865 -865
  49. package/vendor/skill/scripts/requirements.txt +13 -13
  50. package/vendor/skill/scripts/selftest_offline.py +981 -981
@@ -1,1176 +1,1176 @@
1
- --- The qrcode library is licensed under the 3-clause BSD license (aka "new BSD")
2
- --- To get in contact with the author, mail to <gundlach@speedata.de>.
3
- ---
4
- --- Please report bugs on the [github project page](http://speedata.github.io/luaqrcode/).
5
- -- Copyright (c) 2012-2020, Patrick Gundlach and contributors, see https://github.com/speedata/luaqrcode
6
- -- All rights reserved.
7
- --
8
- -- Redistribution and use in source and binary forms, with or without
9
- -- modification, are permitted provided that the following conditions are met:
10
- -- * Redistributions of source code must retain the above copyright
11
- -- notice, this list of conditions and the following disclaimer.
12
- -- * Redistributions in binary form must reproduce the above copyright
13
- -- notice, this list of conditions and the following disclaimer in the
14
- -- documentation and/or other materials provided with the distribution.
15
- -- * Neither the name of SPEEDATA nor the
16
- -- names of its contributors may be used to endorse or promote products
17
- -- derived from this software without specific prior written permission.
18
- --
19
- -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
- -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
- -- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
- -- DISCLAIMED. IN NO EVENT SHALL SPEEDATA GMBH BE LIABLE FOR ANY
23
- -- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
- -- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
- -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
- -- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
- -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
- -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
-
30
- -- Lychee Dev adaptation of luaqrcode (upstream: https://github.com/speedata/luaqrcode).
31
- -- Changes from the reference copy: the encoder is built lazily inside
32
- -- BuildEncoder() so loading this file stays allocation-free (the upstream load
33
- -- constructs a 256x256 xor lookup table immediately), the test-mode and global
34
- -- assignment blocks were removed, and the API is exposed as ns.AutomationQR
35
- -- instead of a global. Encoding behavior is unchanged.
36
-
37
- local ADDON_NAME, ns = ...
38
-
39
- local encode
40
-
41
- local function BuildEncoder()
42
-
43
-
44
- --- Overall workflow
45
- --- ================
46
- --- The steps to generate the qrcode, assuming we already have the codeword:
47
- ---
48
- --- 1. Determine version, ec level and mode (=encoding) for codeword
49
- --- 1. Encode data
50
- --- 1. Arrange data and calculate error correction code
51
- --- 1. Generate 8 matrices with different masks and calculate the penalty
52
- --- 1. Return qrcode with least penalty
53
- ---
54
- --- Each step is of course more or less complex and needs further description
55
-
56
-
57
-
58
- --- Helper functions
59
- --- ================
60
- ---
61
- --- We start with some helper functions
62
-
63
- local max,min=math.max,math.min
64
- local floor,abs=math.floor,math.abs
65
- local byte,sub,rep=string.byte,string.sub,string.rep
66
- local gsub,match,format=string.gsub,string.match,string.format
67
- local concat = table.concat
68
-
69
-
70
- local xor_lookup = {}
71
- do
72
- -- Build a fast xor helper that stays compatible across Lua versions.
73
- -- We precompute a 256x256 lookup table once at load time using a portable
74
- -- arithmetic xor; the hot path is then a constant-time table lookup without
75
- -- requiring native bitwise operators or external bit libraries.
76
- -- Slow but portable xor used only while populating the lookup table if no native op exists.
77
- local function slow_xor(a,b)
78
- local result = 0
79
- local bitval = 1
80
- while a > 0 or b > 0 do
81
- if (a % 2) ~= (b % 2) then
82
- result = result + bitval
83
- end
84
- a = floor(a / 2)
85
- b = floor(b / 2)
86
- bitval = bitval * 2
87
- end
88
- return result
89
- end
90
-
91
- -- Always build a 256x256 table once at load time; avoids any reliance on bitwise operators.
92
- for i=0,255 do
93
- local row = {}
94
- for j=0,255 do
95
- row[j] = slow_xor(i,j)
96
- end
97
- xor_lookup[i] = row
98
- end
99
- end
100
-
101
- local decToHexTable={
102
- ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",
103
- ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",
104
- ["8"]="1000",["9"]="1001",["a"]="1010",["b"]="1011",
105
- ["c"]="1100",["d"]="1101",["e"]="1110",["f"]="1111",
106
- }
107
- local function decToHex(d) return decToHexTable[d] end
108
- -- Return the binary representation of the number x with the width of `digits`.
109
- local function binary(x,digits)
110
- local s = format("%x",x) -- dec to hex
111
- s = gsub(s,"(.)",decToHex) -- hex to bin
112
- s = gsub(s,"^0+","") -- remove leading 0s
113
- return rep("0",digits - #s) .. s
114
- end
115
-
116
- -- A small helper function for add_typeinfo_to_matrix() and add_version_information()
117
- -- Add a 2 (black by default) / -2 (blank by default) to the matrix at position x,y
118
- -- depending on the bitstring (size 1!) where "0"=blank and "1"=black.
119
- local function fill_matrix_position(matrix,bitstr,x,y)
120
- matrix[x][y] = bitstr == "1" and 2 or -2
121
- end
122
-
123
-
124
-
125
- --- Step 1: Determine version, ec level and mode for codeword
126
- --- =========================================================
127
- ---
128
- --- First we need to find out the version (= size) of the QR code. This depends on
129
- --- the input data (the mode to be used), the requested error correction level
130
- --- (normally we use the maximum level that fits into the minimal size).
131
-
132
- -- Return the mode for the given string `str`.
133
- -- See table 2 of the spec. We only support mode 1, 2 and 4.
134
- -- That is: numeric, alaphnumeric and binary.
135
- local function get_mode(str)
136
- if match(str,"^[0-9]+$") then
137
- return 1
138
- elseif match(str,"^[0-9A-Z $%%*./:+-]+$") then
139
- return 2
140
- else
141
- return 4
142
- end
143
- assert(false,"never reached") -- luacheck: ignore
144
- return nil
145
- end
146
-
147
- --- Capacity of QR codes
148
- --- --------------------
149
- --- The capacity is calculated as follow: \\(\text{Number of data bits} = \text{number of codewords} * 8\\).
150
- --- The number of data bits is now reduced by 4 (the mode indicator) and the length string,
151
- --- that varies between 8 and 16, depending on the version and the mode (see method `get_length()`). The
152
- --- remaining capacity is multiplied by the amount of data per bit string (numeric: 3, alphanumeric: 2, other: 1)
153
- --- and divided by the length of the bit string (numeric: 10, alphanumeric: 11, binary: 8, kanji: 13).
154
- --- Then the floor function is applied to the result:
155
- --- $$\Big\lfloor \frac{( \text{#data bits} - 4 - \text{length string}) * \text{data per bit string}}{\text{length of the bit string}} \Big\rfloor$$
156
- ---
157
- --- There is one problem remaining. The length string depends on the version,
158
- --- and the version depends on the length string. But we take this into account when calculating the
159
- --- the capacity, so this is not really a problem here.
160
-
161
- -- The capacity (number of codewords) of each version (1-40) for error correction levels 1-4 (LMQH).
162
- -- The higher the ec level, the lower the capacity of the version. Taken from spec, tables 7-11.
163
- local capacity = {
164
- { 19, 16, 13, 9},{ 34, 28, 22, 16},{ 55, 44, 34, 26},{ 80, 64, 48, 36},
165
- { 108, 86, 62, 46},{ 136, 108, 76, 60},{ 156, 124, 88, 66},{ 194, 154, 110, 86},
166
- { 232, 182, 132, 100},{ 274, 216, 154, 122},{ 324, 254, 180, 140},{ 370, 290, 206, 158},
167
- { 428, 334, 244, 180},{ 461, 365, 261, 197},{ 523, 415, 295, 223},{ 589, 453, 325, 253},
168
- { 647, 507, 367, 283},{ 721, 563, 397, 313},{ 795, 627, 445, 341},{ 861, 669, 485, 385},
169
- { 932, 714, 512, 406},{1006, 782, 568, 442},{1094, 860, 614, 464},{1174, 914, 664, 514},
170
- {1276, 1000, 718, 538},{1370, 1062, 754, 596},{1468, 1128, 808, 628},{1531, 1193, 871, 661},
171
- {1631, 1267, 911, 701},{1735, 1373, 985, 745},{1843, 1455, 1033, 793},{1955, 1541, 1115, 845},
172
- {2071, 1631, 1171, 901},{2191, 1725, 1231, 961},{2306, 1812, 1286, 986},{2434, 1914, 1354, 1054},
173
- {2566, 1992, 1426, 1096},{2702, 2102, 1502, 1142},{2812, 2216, 1582, 1222},{2956, 2334, 1666, 1276},
174
- }
175
-
176
- --- Return the smallest version for this codeword. If `requested_ec_level` is supplied,
177
- --- then the ec level (LMQH - 1,2,3,4) must be at least the requested level.
178
- -- mode = 1,2,4,8
179
- local function get_version_eclevel(len,mode,requested_ec_level)
180
- local local_mode = mode
181
- if mode == 4 then
182
- local_mode = 3
183
- elseif mode == 8 then
184
- local_mode = 4
185
- end
186
- assert( local_mode <= 4 )
187
-
188
- local bits, digits, modebits, c
189
- local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
190
- local minversion = 99 -- placeholder, must be replaced by a lower value
191
- local maxec_level = requested_ec_level or 1
192
- local minlv,maxlv = 1, 4
193
- if requested_ec_level and requested_ec_level >= 1 and requested_ec_level <= 4 then
194
- minlv = requested_ec_level
195
- maxlv = requested_ec_level
196
- end
197
- for ec_level=minlv,maxlv do
198
- for version=1,#capacity do
199
- bits = capacity[version][ec_level] * 8
200
- bits = bits - 4 -- the mode indicator
201
- if version < 10 then
202
- digits = tab[1][local_mode]
203
- elseif version < 27 then
204
- digits = tab[2][local_mode]
205
- elseif version <= 40 then
206
- digits = tab[3][local_mode]
207
- end
208
- modebits = bits - digits
209
- if local_mode == 1 then -- numeric
210
- c = floor(modebits * 3 / 10)
211
- elseif local_mode == 2 then -- alphanumeric
212
- c = floor(modebits * 2 / 11)
213
- elseif local_mode == 3 then -- binary
214
- c = floor(modebits * 1 / 8)
215
- else
216
- c = floor(modebits * 1 / 13)
217
- end
218
- if c >= len then
219
- if version <= minversion then
220
- minversion = version
221
- maxec_level = ec_level
222
- end
223
- break
224
- end
225
- end
226
- end
227
- assert(minversion<=40,"Data too long to encode in QR code")
228
- return minversion, maxec_level
229
- end
230
-
231
- -- Return a bit string of 0s and 1s that includes the length of the code string.
232
- -- The modes are numeric = 1, alphanumeric = 2, binary = 4, and japanese = 8
233
- local function get_length(str,version,mode)
234
- local i = mode
235
- if mode == 4 then
236
- i = 3
237
- elseif mode == 8 then
238
- i = 4
239
- end
240
- assert( i <= 4 )
241
- local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
242
- local digits
243
- if version < 10 then
244
- digits = tab[1][i]
245
- elseif version < 27 then
246
- digits = tab[2][i]
247
- elseif version <= 40 then
248
- digits = tab[3][i]
249
- else
250
- assert(false, "get_length, version > 40 not supported")
251
- end
252
- local len = binary(#str,digits)
253
- return len
254
- end
255
-
256
- --- If the `requested_ec_level` or the `mode` are provided, this will be used if possible.
257
- --- The mode depends on the characters used in the string `str`. It seems to be
258
- --- possible to split the QR code to handle multiple modes, but we don't do that.
259
- local function get_version_eclevel_mode_bistringlength(str,requested_ec_level,mode)
260
- local local_mode
261
- if mode then
262
- assert(false,"not implemented")
263
- -- check if the mode is OK for the string
264
- local_mode = mode
265
- else
266
- local_mode = get_mode(str)
267
- end
268
- local version, ec_level
269
- version, ec_level = get_version_eclevel(#str,local_mode,requested_ec_level)
270
- local length_string = get_length(str,version,local_mode)
271
- return version,ec_level,binary(local_mode,4),local_mode,length_string
272
- end
273
-
274
-
275
-
276
- --- Step 2: Encode data
277
- --- ===================
278
- ---
279
- --- There are several ways to encode the data. We currently support only numeric, alphanumeric and binary.
280
- --- We already chose the encoding (a.k.a. mode) in the first step, so we need to apply the mode to the
281
- --- codeword.
282
- ---
283
- --- **Numeric**: take three digits and encode them in 10 bits
284
- --- **Alphanumeric**: take two characters and encode them in 11 bits
285
- --- **Binary**: take one octet and encode it in 8 bits
286
-
287
- local asciitbl = {
288
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -- 0x01-0x0f
289
- -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -- 0x10-0x1f
290
- 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, -- 0x20-0x2f
291
- 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -- 0x30-0x3f
292
- -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, -- 0x40-0x4f
293
- 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -- 0x50-0x5f
294
- }
295
-
296
- -- Return a binary representation of the numeric string `str`. This must contain only digits 0-9.
297
- local function encode_string_numeric(str)
298
- local encodebuffer = {}
299
- for i = 1, #str, 3 do
300
- local a = sub(str,i,i+2)
301
- -- #a is 1, 2, or 3, so bits are 4, 7, or 10
302
- encodebuffer[#encodebuffer+1]=binary(tonumber(a), #a * 3 + 1)
303
- end
304
- return table.concat(encodebuffer)
305
- end
306
-
307
- -- Return a binary representation of the alphanumeric string `str`. This must contain only
308
- -- digits 0-9, uppercase letters A-Z, space and the following chars: $%*./:+-.
309
- local function encode_string_ascii(str)
310
- local encodebuffer = {}
311
- local int
312
- local b1, b2
313
- for i = 1, #str, 2 do
314
- local a = sub(str,i,i+1)
315
- if #a == 2 then
316
- b1 = asciitbl[byte(sub(a,1,1))]
317
- b2 = asciitbl[byte(sub(a,2,2))]
318
- int = b1 * 45 + b2
319
- encodebuffer[#encodebuffer+1] = binary(int,11)
320
- else
321
- int = asciitbl[byte(a)]
322
- encodebuffer[#encodebuffer+1] = binary(int,6)
323
- end
324
- end
325
- return table.concat(encodebuffer)
326
- end
327
-
328
- -- Return a bitstring representing string str in binary mode.
329
- -- We don't handle UTF-8 in any special way because we assume the
330
- -- scanner recognizes UTF-8 and displays it correctly.
331
- local function encode_string_binary(str)
332
- local encodebuffer = {}
333
- for i = 1, #str do
334
- encodebuffer[i] = binary(byte(str,i),8)
335
- end
336
- return table.concat(encodebuffer)
337
- end
338
-
339
- -- Return a bitstring representing string str in the given mode.
340
- local function encode_data(str,mode)
341
- if mode == 1 then
342
- return encode_string_numeric(str)
343
- elseif mode == 2 then
344
- return encode_string_ascii(str)
345
- elseif mode == 4 then
346
- return encode_string_binary(str)
347
- else
348
- assert(false,"not implemented yet")
349
- end
350
- end
351
-
352
- -- Encoding the codeword is not enough. We need to make sure that
353
- -- the length of the binary string is equal to the number of codewords of the version.
354
- local function add_pad_data(version,ec_level,data)
355
- local cpty = capacity[version][ec_level] * 8
356
- local buffer = {data}
357
- local buffer_len = #data
358
- local count_to_pad = min(4,cpty - buffer_len)
359
- if count_to_pad > 0 then
360
- buffer[#buffer + 1] = rep("0",count_to_pad)
361
- buffer_len = buffer_len + count_to_pad
362
- end
363
- if buffer_len % 8 ~= 0 then
364
- local missing = 8 - buffer_len % 8
365
- buffer[#buffer + 1] = rep("0",missing)
366
- buffer_len = buffer_len + missing
367
- end
368
- -- add "11101100" and "00010001" until enough data
369
- local remaining_bytes = (cpty - buffer_len) / 8 -- decimal doesn't matter
370
- for i=1,remaining_bytes do
371
- buffer[#buffer + 1] = i % 2 == 1 and "11101100" or "00010001"
372
- end
373
- return concat(buffer)
374
- end
375
-
376
-
377
-
378
- --- Step 3: Organize data and calculate error correction code
379
- --- =========================================================
380
- --- The data in the qrcode is not encoded linearly. For example code 5-H has four blocks, the first two blocks
381
- --- contain 11 codewords and 22 error correction codes each, the second block contain 12 codewords and 22 ec codes each.
382
- --- We just take the table from the spec and don't calculate the blocks ourself. The table `ecblocks` contains this info.
383
- ---
384
- --- During the phase of splitting the data into codewords, we do the calculation for error correction codes. This step involves
385
- --- polynomial division. Find a math book from school and follow the code here :)
386
-
387
- --- ### Reed Solomon error correction
388
- --- Now this is the slightly ugly part of the error correction. We start with log/antilog tables
389
- -- https://codyplanteen.com/assets/rs/gf256_log_antilog.pdf
390
- local alpha_int = {
391
- [0] = 1,
392
- 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76,
393
- 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157,
394
- 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70,
395
- 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95,
396
- 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253,
397
- 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217,
398
- 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129,
399
- 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133,
400
- 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168,
401
- 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230,
402
- 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227,
403
- 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130,
404
- 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81,
405
- 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18,
406
- 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44,
407
- 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 0, 0
408
- }
409
-
410
- local int_alpha = {
411
- [0] = 256, -- special value
412
- 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4,
413
- 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5,
414
- 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, 29,
415
- 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166, 6,
416
- 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136, 54,
417
- 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64, 30,
418
- 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61, 202,
419
- 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87, 7,
420
- 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24, 227,
421
- 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46, 55,
422
- 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97, 242,
423
- 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162, 31,
424
- 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246, 108,
425
- 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90, 203,
426
- 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79,
427
- 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175
428
- }
429
-
430
- -- We only need the polynomial generators for block sizes 7, 10, 13, 15, 16, 17, 18, 20, 22, 24, 26, 28, and 30. Version
431
- -- 2 of the qr codes don't need larger ones (as opposed to version 1). The table has the format x^1*a^21 + x^2*a^102 ...
432
- local generator_polynomial = {
433
- [7] = { 21, 102, 238, 149, 146, 229, 87, 0},
434
- [10] = { 45, 32, 94, 64, 70, 118, 61, 46, 67, 251, 0 },
435
- [13] = { 78, 140, 206, 218, 130, 104, 106, 100, 86, 100, 176, 152, 74, 0 },
436
- [15] = {105, 99, 5, 124, 140, 237, 58, 58, 51, 37, 202, 91, 61, 183, 8, 0},
437
- [16] = {120, 225, 194, 182, 169, 147, 191, 91, 3, 76, 161, 102, 109, 107, 104, 120, 0},
438
- [17] = {136, 163, 243, 39, 150, 99, 24, 147, 214, 206, 123, 239, 43, 78, 206, 139, 43, 0},
439
- [18] = {153, 96, 98, 5, 179, 252, 148, 152, 187, 79, 170, 118, 97, 184, 94, 158, 234, 215, 0},
440
- [20] = {190, 188, 212, 212, 164, 156, 239, 83, 225, 221, 180, 202, 187, 26, 163, 61, 50, 79, 60, 17, 0},
441
- [22] = {231, 165, 105, 160, 134, 219, 80, 98, 172, 8, 74, 200, 53, 221, 109, 14, 230, 93, 242, 247, 171, 210, 0},
442
- [24] = { 21, 227, 96, 87, 232, 117, 0, 111, 218, 228, 226, 192, 152, 169, 180, 159, 126, 251, 117, 211, 48, 135, 121, 229, 0},
443
- [26] = { 70, 218, 145, 153, 227, 48, 102, 13, 142, 245, 21, 161, 53, 165, 28, 111, 201, 145, 17, 118, 182, 103, 2, 158, 125, 173, 0},
444
- [28] = {123, 9, 37, 242, 119, 212, 195, 42, 87, 245, 43, 21, 201, 232, 27, 205, 147, 195, 190, 110, 180, 108, 234, 224, 104, 200, 223, 168, 0},
445
- [30] = {180, 192, 40, 238, 216, 251, 37, 156, 130, 224, 193, 226, 173, 42, 125, 222, 96, 239, 86, 110, 48, 50, 182, 179, 31, 216, 152, 145, 173, 41, 0}}
446
-
447
-
448
- -- Turn a binary string of length 8*x into a table size x of numbers.
449
- local function convert_bitstring_to_bytes(data)
450
- local msg = {}
451
- for i=1, #data / 8 do
452
- msg[i] = tonumber(sub(data,(i - 1) * 8 + 1,i * 8),2)
453
- end
454
- return msg
455
- end
456
-
457
- -- Return a table that has 0's in the first entries and then the alpha
458
- -- representation of the generator polynomial
459
- local function get_generator_polynomial_adjusted(num_ec_codewords,highest_exponent)
460
- local gp_alpha = {[0]=0}
461
- for i=0,highest_exponent - num_ec_codewords - 1 do
462
- gp_alpha[i] = 0
463
- end
464
- local gp = generator_polynomial[num_ec_codewords]
465
- for i=1,num_ec_codewords + 1 do
466
- gp_alpha[highest_exponent - num_ec_codewords + i - 1] = gp[i]
467
- end
468
- return gp_alpha
469
- end
470
-
471
- -- That's the heart of the error correction calculation.
472
- local function calculate_error_correction(data,num_ec_codewords)
473
- local mp
474
- if type(data)=="string" then
475
- mp = convert_bitstring_to_bytes(data)
476
- elseif type(data)=="table" then
477
- mp = data
478
- else
479
- assert(false,format("Unknown type for data: %s",type(data)))
480
- end
481
- local len_message = #mp
482
-
483
- local highest_exponent = len_message + num_ec_codewords - 1
484
- local gp_alpha
485
- local mp_int = {}
486
- -- create message shifted to left (highest exponent)
487
- for i=1,len_message do
488
- mp_int[highest_exponent - i + 1] = mp[i]
489
- end
490
- for i=1,highest_exponent - len_message do
491
- mp_int[i] = 0
492
- end
493
- mp_int[0] = 0
494
-
495
- while highest_exponent >= num_ec_codewords do
496
- gp_alpha = get_generator_polynomial_adjusted(num_ec_codewords,highest_exponent)
497
-
498
- -- Multiply generator polynomial by first coefficient of the above polynomial
499
-
500
- -- take the highest exponent from the message polynom (alpha) and add
501
- -- it to the generator polynom
502
- local exp = int_alpha[mp_int[highest_exponent]]
503
- for i=highest_exponent,highest_exponent - num_ec_codewords,-1 do
504
- if exp ~= 256 then
505
- gp_alpha[i] = (gp_alpha[i] + exp) % 255
506
- else
507
- gp_alpha[i] = 256
508
- end
509
- end
510
- for i=highest_exponent - num_ec_codewords - 1,0,-1 do
511
- gp_alpha[i] = 256
512
- end
513
-
514
- for i=highest_exponent,0,-1 do
515
- mp_int[i] = xor_lookup[alpha_int[gp_alpha[i]]][mp_int[i]]
516
- end
517
- -- remove leading 0's
518
- for i=highest_exponent,num_ec_codewords,-1 do
519
- if mp_int[i]==0 then
520
- highest_exponent=i-1
521
- else
522
- break
523
- end
524
- end
525
-
526
- if highest_exponent<num_ec_codewords then break end
527
- end
528
- local ret = {}
529
-
530
- -- reverse data
531
- for i=highest_exponent,0,-1 do
532
- ret[#ret + 1] = mp_int[i]
533
- end
534
- return ret
535
- end
536
-
537
- --- #### Arranging the data
538
- --- Now we arrange the data into smaller chunks. This table is taken from the spec.
539
- -- ecblocks has 40 entries, one for each version. Each version entry has 4 entries, for each LMQH
540
- -- ec level. Each entry has two or four fields, the odd files are the number of repetitions for the
541
- -- following block info. The first entry of the block is the total number of codewords in the block,
542
- -- the second entry is the number of data codewords. The third is not important.
543
- local ecblocks = {
544
- {{ 1,{ 26, 19, 2} }, { 1,{26,16, 4}}, { 1,{26,13, 6}}, { 1, {26, 9, 8} }},
545
- {{ 1,{ 44, 34, 4} }, { 1,{44,28, 8}}, { 1,{44,22,11}}, { 1, {44,16,14} }},
546
- {{ 1,{ 70, 55, 7} }, { 1,{70,44,13}}, { 2,{35,17, 9}}, { 2, {35,13,11} }},
547
- {{ 1,{100, 80,10} }, { 2,{50,32, 9}}, { 2,{50,24,13}}, { 4, {25, 9, 8} }},
548
- {{ 1,{134,108,13} }, { 2,{67,43,12}}, { 2,{33,15, 9}, 2,{34,16, 9}}, { 2, {33,11,11}, 2,{34,12,11}}},
549
- {{ 2,{ 86, 68, 9} }, { 4,{43,27, 8}}, { 4,{43,19,12}}, { 4, {43,15,14} }},
550
- {{ 2,{ 98, 78,10} }, { 4,{49,31, 9}}, { 2,{32,14, 9}, 4,{33,15, 9}}, { 4, {39,13,13}, 1,{40,14,13}}},
551
- {{ 2,{121, 97,12} }, { 2,{60,38,11}, 2,{61,39,11}}, { 4,{40,18,11}, 2,{41,19,11}}, { 4, {40,14,13}, 2,{41,15,13}}},
552
- {{ 2,{146,116,15} }, { 3,{58,36,11}, 2,{59,37,11}}, { 4,{36,16,10}, 4,{37,17,10}}, { 4, {36,12,12}, 4,{37,13,12}}},
553
- {{ 2,{ 86, 68, 9}, 2,{ 87, 69, 9}}, { 4,{69,43,13}, 1,{70,44,13}}, { 6,{43,19,12}, 2,{44,20,12}}, { 6, {43,15,14}, 2,{44,16,14}}},
554
- {{ 4,{101, 81,10} }, { 1,{80,50,15}, 4,{81,51,15}}, { 4,{50,22,14}, 4,{51,23,14}}, { 3, {36,12,12}, 8,{37,13,12}}},
555
- {{ 2,{116, 92,12}, 2,{117, 93,12}}, { 6,{58,36,11}, 2,{59,37,11}}, { 4,{46,20,13}, 6,{47,21,13}}, { 7, {42,14,14}, 4,{43,15,14}}},
556
- {{ 4,{133,107,13} }, { 8,{59,37,11}, 1,{60,38,11}}, { 8,{44,20,12}, 4,{45,21,12}}, { 12, {33,11,11}, 4,{34,12,11}}},
557
- {{ 3,{145,115,15}, 1,{146,116,15}}, { 4,{64,40,12}, 5,{65,41,12}}, { 11,{36,16,10}, 5,{37,17,10}}, { 11, {36,12,12}, 5,{37,13,12}}},
558
- {{ 5,{109, 87,11}, 1,{110, 88,11}}, { 5,{65,41,12}, 5,{66,42,12}}, { 5,{54,24,15}, 7,{55,25,15}}, { 11, {36,12,12}, 7,{37,13,12}}},
559
- {{ 5,{122, 98,12}, 1,{123, 99,12}}, { 7,{73,45,14}, 3,{74,46,14}}, { 15,{43,19,12}, 2,{44,20,12}}, { 3, {45,15,15}, 13,{46,16,15}}},
560
- {{ 1,{135,107,14}, 5,{136,108,14}}, { 10,{74,46,14}, 1,{75,47,14}}, { 1,{50,22,14}, 15,{51,23,14}}, { 2, {42,14,14}, 17,{43,15,14}}},
561
- {{ 5,{150,120,15}, 1,{151,121,15}}, { 9,{69,43,13}, 4,{70,44,13}}, { 17,{50,22,14}, 1,{51,23,14}}, { 2, {42,14,14}, 19,{43,15,14}}},
562
- {{ 3,{141,113,14}, 4,{142,114,14}}, { 3,{70,44,13}, 11,{71,45,13}}, { 17,{47,21,13}, 4,{48,22,13}}, { 9, {39,13,13}, 16,{40,14,13}}},
563
- {{ 3,{135,107,14}, 5,{136,108,14}}, { 3,{67,41,13}, 13,{68,42,13}}, { 15,{54,24,15}, 5,{55,25,15}}, { 15, {43,15,14}, 10,{44,16,14}}},
564
- {{ 4,{144,116,14}, 4,{145,117,14}}, { 17,{68,42,13}}, { 17,{50,22,14}, 6,{51,23,14}}, { 19, {46,16,15}, 6,{47,17,15}}},
565
- {{ 2,{139,111,14}, 7,{140,112,14}}, { 17,{74,46,14}}, { 7,{54,24,15}, 16,{55,25,15}}, { 34, {37,13,12} }},
566
- {{ 4,{151,121,15}, 5,{152,122,15}}, { 4,{75,47,14}, 14,{76,48,14}}, { 11,{54,24,15}, 14,{55,25,15}}, { 16, {45,15,15}, 14,{46,16,15}}},
567
- {{ 6,{147,117,15}, 4,{148,118,15}}, { 6,{73,45,14}, 14,{74,46,14}}, { 11,{54,24,15}, 16,{55,25,15}}, { 30, {46,16,15}, 2,{47,17,15}}},
568
- {{ 8,{132,106,13}, 4,{133,107,13}}, { 8,{75,47,14}, 13,{76,48,14}}, { 7,{54,24,15}, 22,{55,25,15}}, { 22, {45,15,15}, 13,{46,16,15}}},
569
- {{ 10,{142,114,14}, 2,{143,115,14}}, { 19,{74,46,14}, 4,{75,47,14}}, { 28,{50,22,14}, 6,{51,23,14}}, { 33, {46,16,15}, 4,{47,17,15}}},
570
- {{ 8,{152,122,15}, 4,{153,123,15}}, { 22,{73,45,14}, 3,{74,46,14}}, { 8,{53,23,15}, 26,{54,24,15}}, { 12, {45,15,15}, 28,{46,16,15}}},
571
- {{ 3,{147,117,15}, 10,{148,118,15}}, { 3,{73,45,14}, 23,{74,46,14}}, { 4,{54,24,15}, 31,{55,25,15}}, { 11, {45,15,15}, 31,{46,16,15}}},
572
- {{ 7,{146,116,15}, 7,{147,117,15}}, { 21,{73,45,14}, 7,{74,46,14}}, { 1,{53,23,15}, 37,{54,24,15}}, { 19, {45,15,15}, 26,{46,16,15}}},
573
- {{ 5,{145,115,15}, 10,{146,116,15}}, { 19,{75,47,14}, 10,{76,48,14}}, { 15,{54,24,15}, 25,{55,25,15}}, { 23, {45,15,15}, 25,{46,16,15}}},
574
- {{ 13,{145,115,15}, 3,{146,116,15}}, { 2,{74,46,14}, 29,{75,47,14}}, { 42,{54,24,15}, 1,{55,25,15}}, { 23, {45,15,15}, 28,{46,16,15}}},
575
- {{ 17,{145,115,15} }, { 10,{74,46,14}, 23,{75,47,14}}, { 10,{54,24,15}, 35,{55,25,15}}, { 19, {45,15,15}, 35,{46,16,15}}},
576
- {{ 17,{145,115,15}, 1,{146,116,15}}, { 14,{74,46,14}, 21,{75,47,14}}, { 29,{54,24,15}, 19,{55,25,15}}, { 11, {45,15,15}, 46,{46,16,15}}},
577
- {{ 13,{145,115,15}, 6,{146,116,15}}, { 14,{74,46,14}, 23,{75,47,14}}, { 44,{54,24,15}, 7,{55,25,15}}, { 59, {46,16,15}, 1,{47,17,15}}},
578
- {{ 12,{151,121,15}, 7,{152,122,15}}, { 12,{75,47,14}, 26,{76,48,14}}, { 39,{54,24,15}, 14,{55,25,15}}, { 22, {45,15,15}, 41,{46,16,15}}},
579
- {{ 6,{151,121,15}, 14,{152,122,15}}, { 6,{75,47,14}, 34,{76,48,14}}, { 46,{54,24,15}, 10,{55,25,15}}, { 2, {45,15,15}, 64,{46,16,15}}},
580
- {{ 17,{152,122,15}, 4,{153,123,15}}, { 29,{74,46,14}, 14,{75,47,14}}, { 49,{54,24,15}, 10,{55,25,15}}, { 24, {45,15,15}, 46,{46,16,15}}},
581
- {{ 4,{152,122,15}, 18,{153,123,15}}, { 13,{74,46,14}, 32,{75,47,14}}, { 48,{54,24,15}, 14,{55,25,15}}, { 42, {45,15,15}, 32,{46,16,15}}},
582
- {{ 20,{147,117,15}, 4,{148,118,15}}, { 40,{75,47,14}, 7,{76,48,14}}, { 43,{54,24,15}, 22,{55,25,15}}, { 10, {45,15,15}, 67,{46,16,15}}},
583
- {{ 19,{148,118,15}, 6,{149,119,15}}, { 18,{75,47,14}, 31,{76,48,14}}, { 34,{54,24,15}, 34,{55,25,15}}, { 20, {45,15,15}, 61,{46,16,15}}}
584
- }
585
-
586
- -- The bits that must be 0 if the version does fill the complete matrix.
587
- -- Example: for version 1, no bits need to be added after arranging the data, for version 2 we need to add 7 bits at the end.
588
- local remainder = {0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0}
589
-
590
- -- This is the formula for table 1 in the spec:
591
- -- function get_capacity_remainder( version )
592
- -- local len = version * 4 + 17
593
- -- local size = len^2
594
- -- local function_pattern_modules = 192 + 2 * len - 32 -- Position Adjustment pattern + timing pattern
595
- -- local count_alignment_pattern = #alignment_pattern[version]
596
- -- if count_alignment_pattern > 0 then
597
- -- -- add 25 for each alignment pattern
598
- -- function_pattern_modules = function_pattern_modules + 25 * ( count_alignment_pattern^2 - 3 )
599
- -- -- but subtract the timing pattern occupied by the alignment pattern on the top and left
600
- -- function_pattern_modules = function_pattern_modules - ( count_alignment_pattern - 2) * 10
601
- -- end
602
- -- size = size - function_pattern_modules
603
- -- if version > 6 then
604
- -- size = size - 67
605
- -- else
606
- -- size = size - 31
607
- -- end
608
- -- return math.floor(size/8),math.fmod(size,8)
609
- -- end
610
-
611
- --- Example: Version 5-H has four data and four error correction blocks. The table above lists
612
- --- `2, {33,11,11}, 2,{34,12,11}` for entry [5][4]. This means we take two blocks with 11 codewords
613
- --- and two blocks with 12 codewords, and two blocks with 33 - 11 = 22 ec codes and another
614
- --- two blocks with 34 - 12 = 22 ec codes.
615
- --- Block 1: D1 D2 D3 ... D11
616
- --- Block 2: D12 D13 D14 ... D22
617
- --- Block 3: D23 D24 D25 ... D33 D34
618
- --- Block 4: D35 D36 D37 ... D45 D46
619
- --- Then we place the data like this in the matrix: D1, D12, D23, D35, D2, D13, D24, D36 ... D45, D34, D46. The same goes
620
- --- with error correction codes.
621
-
622
- -- The given data can be a string of 0's and 1' (with #string mod 8 == 0).
623
- -- Alternatively the data can be a table of codewords. The number of codewords
624
- -- must match the capacity of the qr code.
625
- local function arrange_codewords_and_calculate_ec(version,ec_level,data)
626
- if type(data)=="table" then
627
- local tmp = {}
628
- for i=1,#data do
629
- tmp[i] = binary(data[i],8)
630
- end
631
- data = concat(tmp)
632
- end
633
- -- If the size of the data is not enough for the codeword, we add 0's and two special bytes until finished.
634
- local blocks = ecblocks[version][ec_level]
635
- local size_datablock_bytes, size_ecblock_bytes
636
- local datablocks = {}
637
- local final_ecblocks = {}
638
- local pos = 0
639
- for i=1,#blocks/2 do
640
- size_datablock_bytes = blocks[2*i][2]
641
- size_ecblock_bytes = blocks[2*i][1] - size_datablock_bytes
642
- for _=1,blocks[2*i - 1] do
643
- datablocks[#datablocks + 1] = sub(data, pos * 8 + 1,( pos + size_datablock_bytes)*8)
644
- local tmp_tab = calculate_error_correction(datablocks[#datablocks],size_ecblock_bytes)
645
- local tmp_str = {}
646
- for x=1,#tmp_tab do
647
- tmp_str[#tmp_str + 1] = binary(tmp_tab[x],8)
648
- end
649
- final_ecblocks[#final_ecblocks + 1] = concat(tmp_str)
650
- pos = pos + size_datablock_bytes
651
- end
652
- end
653
-
654
- -- Weave the data blocks. When there are multiple block sizes, the final data stream looks like:
655
- -- b1's 1st byte, b2's 1st byte, (b3's 1st byte, ...)
656
- -- b1's 2nd byte, b2's 2nd byte, (b3's 2nd byte, ...)
657
- -- b1's 3rd byte, ...
658
- local arranged_data = {}
659
- local maxBlockLen = 0
660
- for i = 1, #datablocks do maxBlockLen = max(maxBlockLen, #datablocks[i]) end
661
- for p = 1, maxBlockLen, 8 do
662
- for i = 1, #datablocks do
663
- arranged_data[#arranged_data + 1] = sub(datablocks[i], p, p + 7)
664
- end
665
- end
666
-
667
- -- Same for EC blocks
668
- maxBlockLen = 0
669
- for i = 1, #final_ecblocks do maxBlockLen = max(maxBlockLen, #final_ecblocks[i]) end
670
- for p = 1, maxBlockLen, 8 do
671
- for i = 1, #final_ecblocks do
672
- arranged_data[#arranged_data + 1] = sub(final_ecblocks[i], p, p + 7)
673
- end
674
- end
675
- return concat(arranged_data)
676
- end
677
-
678
-
679
-
680
- --- Step 4: Generate 8 matrices with different masks and calculate the penalty
681
- --- ==========================================================================
682
- ---
683
- --- Prepare matrix
684
- --- --------------
685
- --- The first step is to prepare an _empty_ matrix for a given size/mask. The matrix has a
686
- --- few predefined areas that must be black or blank. We encode the matrix with a two
687
- --- dimensional field where the numbers determine which pixel is blank or not.
688
- ---
689
- --- The following code is used for our matrix:
690
- --- 0 = not in use yet,
691
- --- -2 = blank by mandatory pattern,
692
- --- 2 = black by mandatory pattern,
693
- --- -1 = blank by data,
694
- --- 1 = black by data
695
- ---
696
- --- To prepare the _empty_, we add positioning, alingment and timing patters.
697
-
698
- --- ### Positioning patterns ###
699
- local function add_position_detection_patterns(tab_x)
700
- local size = #tab_x
701
- -- allocate quite zone in the matrix area
702
- for i=1,8 do
703
- for j=1,8 do
704
- tab_x[i][j] = -2
705
- tab_x[size - 8 + i][j] = -2
706
- tab_x[i][size - 8 + j] = -2
707
- end
708
- end
709
- -- draw the detection pattern (outer)
710
- for i=1,7 do
711
- -- top left
712
- tab_x[1][i]=2
713
- tab_x[7][i]=2
714
- tab_x[i][1]=2
715
- tab_x[i][7]=2
716
-
717
- -- top right
718
- tab_x[size][i]=2
719
- tab_x[size - 6][i]=2
720
- tab_x[size - i + 1][1]=2
721
- tab_x[size - i + 1][7]=2
722
-
723
- -- bottom left
724
- tab_x[1][size - i + 1]=2
725
- tab_x[7][size - i + 1]=2
726
- tab_x[i][size - 6]=2
727
- tab_x[i][size]=2
728
- end
729
- -- draw the detection pattern (inner)
730
- for i=1,3 do
731
- for j=1,3 do
732
- -- top left
733
- tab_x[2+j][i+2]=2
734
- -- top right
735
- tab_x[size - j - 1][i+2]=2
736
- -- bottom left
737
- tab_x[2 + j][size - i - 1]=2
738
- end
739
- end
740
- end
741
-
742
- --- ### Timing patterns ###
743
- -- The timing patterns (two) are the dashed lines between two adjacent positioning patterns on row/column 7.
744
- local function add_timing_pattern(tab_x)
745
- local line,col=7,9
746
- for i=col,#tab_x-8 do
747
- tab_x[i][line] = i%2==0 and -2 or 2
748
- tab_x[line][i] = i%2==0 and -2 or 2
749
- end
750
- end
751
-
752
- --- ### Alignment patterns ###
753
- --- The alignment patterns must be added to the matrix for versions > 1. The amount and positions depend on the versions and are
754
- --- given by the spec. Beware: the patterns must not be placed where we have the positioning patterns
755
- --- (that is: top left, top right and bottom left.)
756
-
757
- -- For each version, where should we place the alignment patterns? See table E.1 of the spec
758
- local alignment_pattern = {
759
- {},{6,18},{6,22},{6,26},{6,30},{6,34}, -- 1-6
760
- {6,22,38},{6,24,42},{6,26,46},{6,28,50},{6,30,54},{6,32,58},{6,34,62}, -- 7-13
761
- {6,26,46,66},{6,26,48,70},{6,26,50,74},{6,30,54,78},{6,30,56,82},{6,30,58,86},{6,34,62,90}, -- 14-20
762
- {6,28,50,72,94},{6,26,50,74,98},{6,30,54,78,102},{6,28,54,80,106},{6,32,58,84,110},{6,30,58,86,114},{6,34,62,90,118}, -- 21-27
763
- {6,26,50,74,98 ,122},{6,30,54,78,102,126},{6,26,52,78,104,130},{6,30,56,82,108,134},{6,34,60,86,112,138},{6,30,58,86,114,142},{6,34,62,90,118,146}, -- 28-34
764
- {6,30,54,78,102,126,150}, {6,24,50,76,102,128,154},{6,28,54,80,106,132,158},{6,32,58,84,110,136,162},{6,26,54,82,110,138,166},{6,30,58,86,114,142,170} -- 35 - 40
765
- }
766
-
767
- --- The alignment pattern has size 5x5 and looks like this:
768
- --- XXXXX
769
- --- X X
770
- --- X X X
771
- --- X X
772
- --- XXXXX
773
- local function add_alignment_pattern(tab_x)
774
- local version = (#tab_x - 17) / 4
775
- local ap = alignment_pattern[version]
776
- local pos_x, pos_y
777
- for x=1,#ap do
778
- for y=1,#ap do
779
- -- we must not put an alignment pattern on top of the positioning pattern
780
- if not (x == 1 and y == 1 or x == #ap and y == 1 or x == 1 and y == #ap ) then
781
- pos_x,pos_y=ap[x]+1,ap[y]+1
782
- for dy=-2,2 do
783
- for dx=-2,2 do
784
- -- form the pattern with checking chebyshev distance instead of hardcoding
785
- tab_x[pos_x+dx][pos_y+dy]=max(abs(dx),abs(dy))%2==0 and 2 or -2
786
- end
787
- end
788
- end
789
- end
790
- end
791
- end
792
-
793
- --- ### Type information ###
794
- --- Let's not forget the type information that is in column 9 next to the left positioning patterns and on row 9 below
795
- --- the top positioning patterns. This type information is not fixed, it depends on the mask and the error correction.
796
-
797
- -- The first index is ec level (LMQH,1-4), the second is the mask (0-7). This bitstring of length 15 is to be used
798
- -- as mandatory pattern in the qrcode. Mask -1 is for debugging purpose only and is the 'noop' mask.
799
- local typeinfo = {
800
- { [-1]= "111111111111111", [0] = "111011111000100", "111001011110011", "111110110101010", "111100010011101", "110011000101111", "110001100011000", "110110001000001", "110100101110110" },
801
- { [-1]= "111111111111111", [0] = "101010000010010", "101000100100101", "101111001111100", "101101101001011", "100010111111001", "100000011001110", "100111110010111", "100101010100000" },
802
- { [-1]= "111111111111111", [0] = "011010101011111", "011000001101000", "011111100110001", "011101000000110", "010010010110100", "010000110000011", "010111011011010", "010101111101101" },
803
- { [-1]= "111111111111111", [0] = "001011010001001", "001001110111110", "001110011100111", "001100111010000", "000011101100010", "000001001010101", "000110100001100", "000100000111011" }
804
- }
805
-
806
- -- The typeinfo is a mixture of mask and ec level information and is
807
- -- added twice to the qr code, one horizontal, one vertical.
808
- local function add_typeinfo_to_matrix(matrix,ec_level,mask)
809
- local ec_mask_type = typeinfo[ec_level][mask]
810
-
811
- local bit
812
- -- vertical from bottom to top
813
- for i=1,7 do
814
- bit = sub(ec_mask_type,i,i)
815
- fill_matrix_position(matrix,bit,9,#matrix - i + 1)
816
- end
817
- for i=8,9 do
818
- bit = sub(ec_mask_type,i,i)
819
- fill_matrix_position(matrix,bit,9,17-i)
820
- end
821
- for i=10,15 do
822
- bit = sub(ec_mask_type,i,i)
823
- fill_matrix_position(matrix,bit,9,16 - i)
824
- end
825
- -- horizontal, left to right
826
- for i=1,6 do
827
- bit = sub(ec_mask_type,i,i)
828
- fill_matrix_position(matrix,bit,i,9)
829
- end
830
- bit = sub(ec_mask_type,7,7)
831
- fill_matrix_position(matrix,bit,8,9)
832
- for i=8,15 do
833
- bit = sub(ec_mask_type,i,i)
834
- fill_matrix_position(matrix,bit,#matrix - 15 + i,9)
835
- end
836
- end
837
-
838
- -- Bits for version information 7-40
839
- -- The reversed strings from https://www.thonky.com/qr-code-tutorial/format-version-tables
840
- local version_information = {
841
- "001010010011111000", "001111011010000100", "100110010101100100", "110010110010010100",
842
- "011011111101110100", "010001101110001100", "111000100001101100", "101100000110011100", "000101001001111100",
843
- "000111101101000010", "101110100010100010", "111010000101010010", "010011001010110010", "011001011001001010",
844
- "110000010110101010", "100100110001011010", "001101111110111010", "001000110111000110", "100001111000100110",
845
- "110101011111010110", "011100010000110110", "010110000011001110", "111111001100101110", "101011101011011110",
846
- "000010100100111110", "101010111001000001", "000011110110100001", "010111010001010001", "111110011110110001",
847
- "110100001101001001", "011101000010101001", "001001100101011001", "100000101010111001", "100101100011000101",
848
- }
849
-
850
- -- Versions 7 and above need two bitfields with version information added to the code
851
- local function add_version_information(matrix,version)
852
- if version < 7 then return end
853
- local size = #matrix
854
- local bitstring = version_information[version - 6]
855
- local x,y, bit
856
- local start_x, start_y
857
- -- first top right
858
- start_x = size - 10
859
- start_y = 1
860
- for i=1,#bitstring do
861
- bit = sub(bitstring,i,i)
862
- x = start_x + (i - 1) % 3
863
- y = start_y + floor((i - 1) / 3)
864
- fill_matrix_position(matrix,bit,x,y)
865
- end
866
-
867
- -- now bottom left
868
- start_x = 1
869
- start_y = size - 10
870
- for i=1,#bitstring do
871
- bit = sub(bitstring,i,i)
872
- x = start_x + floor((i - 1) / 3)
873
- y = start_y + (i - 1) % 3
874
- fill_matrix_position(matrix,bit,x,y)
875
- end
876
- end
877
-
878
- --- Now it's time to use the methods above to create a prefilled matrix for the given mask
879
- local function prepare_matrix_with_mask(version,ec_level,mask)
880
- local size = version * 4 + 17
881
- local tab_x = {}
882
-
883
- for i=1,size do
884
- tab_x[i]={}
885
- for j=1,size do
886
- tab_x[i][j] = 0
887
- end
888
- end
889
- add_position_detection_patterns(tab_x)
890
- add_timing_pattern(tab_x)
891
- add_version_information(tab_x,version)
892
-
893
- -- black pixel above lower left position detection pattern
894
- tab_x[9][size - 7] = 2
895
- add_alignment_pattern(tab_x)
896
- add_typeinfo_to_matrix(tab_x,ec_level, mask)
897
- return tab_x
898
- end
899
-
900
- --- Finally we come to the place where we need to put the calculated data (remember step 3?) into the qr code.
901
- --- We do this for each mask. BTW speaking of mask, this is what we find in the spec:
902
- --- Mask Pattern Reference Condition
903
- --- 000 (y + x) mod 2 = 0
904
- --- 001 y mod 2 = 0
905
- --- 010 x mod 3 = 0
906
- --- 011 (y + x) mod 3 = 0
907
- --- 100 ((y div 2) + (x div 3)) mod 2 = 0
908
- --- 101 (y x) mod 2 + (y x) mod 3 = 0
909
- --- 110 ((y x) mod 2 + (y x) mod 3) mod 2 = 0
910
- --- 111 ((y x) mod 3 + (y+x) mod 2) mod 2 = 0
911
-
912
- -- Mask functions, i & j are 0-based, so input should be (x-1,y-1)
913
- -- true means 'invert this bit'
914
- local maskFunc={
915
- [-1]=function(_,_) return false end, -- test purpose only, no mask applied
916
- [0]=function(x,y) return (y+x)%2==0 end,
917
- function(_,y) return y%2==0 end,
918
- function(x,_) return x%3==0 end,
919
- function(x,y) return (y+x)%3==0 end,
920
- function(x,y) return (y%4-1.5)*(x%6-2.5)>0 end, -- optimized for not using math.floor (too slow) or // operation (new Lua only)
921
- function(x,y) return (y*x)%2+(y*x)%3==0 end,
922
- function(x,y) return ((y*x)%3+y*x)%2==0 end,
923
- function(x,y) return ((y*x)%3+y+x)%2==0 end,
924
- }
925
-
926
- -- Receive 0 (blank) or 1 (black) from data,
927
- -- Return -1 (blank) or 1 (black) depending on the value, mask, and position.
928
- -- Parameter mask is 0-7 (-1 for 'no mask'). x and y are 1-based coordinates,
929
- -- 1,1 = upper left. value must be 0 or 1.
930
- local function get_pixel_with_mask(mask,x,y,dataBit)
931
- local invert = maskFunc[mask](x-1,y-1)
932
- return (dataBit==0)==invert and 1 or -1
933
- -- This^ == is used as boolean XNOR:
934
- -- data F T <- invert?
935
- -- 0 F -1 1
936
- -- 1 T 1 -1
937
- end
938
-
939
- -- Add the data string (0's and 1's) to the matrix for the given mask.
940
- local function add_data_to_matrix(matrix,data,mask)
941
- local size = #matrix
942
- -- Fill data into matrix
943
- local ptr=1 -- data pointer
944
- local x,y=size,size -- writing position, starts from bottom right
945
- local x_dir,y_dir=-1,-1 -- state of movement, notice that Y step once each two X steps
946
- while true do
947
- -- 0 means available data cell to write data
948
- if matrix[x][y]==0 then
949
- matrix[x][y] = get_pixel_with_mask(mask,x,y,byte(data,ptr)-48) -- '0' = 48, '1' = 49
950
- ptr = ptr + 1
951
- if ptr > #data or x < 0 then return matrix end -- all data written, finish
952
- end
953
-
954
- -- Move to next cell (won't write into unavailable cell so it's fine to move 1 step each time)
955
- -- switch left/right
956
- x = x + x_dir
957
- -- if just stepped right, it means current 2 bits were finished
958
- if x_dir == 1 then
959
- -- so we step up/down for next 2 bits
960
- y = y + y_dir
961
-
962
- -- when we went outside the matrix, move 2 cells left and turn back
963
- if not matrix[y] then -- square, so matrix[y] will be nil if y is out of range, no matter [x][y] or [y][x]
964
- x = x - 2
965
- if x == 7 then x = 6 end -- jump over timing pattern
966
- y = y_dir == -1 and 1 or size
967
- y_dir = -y_dir
968
- end
969
- end
970
- -- prepare next left/right
971
- x_dir = -x_dir
972
- end
973
- end
974
-
975
- --- The total penalty of the matrix is the sum of four steps. The following steps are taken into account:
976
- ---
977
- --- 1. Adjacent modules in row/column in same color
978
- --- 1. Block of modules in same color
979
- --- 1. 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column
980
- --- 1. Proportion of dark modules in entire symbol
981
- ---
982
- --- This all is done to avoid bad patterns in the code that prevent the scanner from
983
- --- reading the code.
984
- -- Return the penalty for the given matrix
985
- local function calculate_penalty(matrix)
986
- local penalty1, penalty2, penalty3 = 0,0,0
987
- local size = #matrix
988
- -- this is for penalty 4
989
- local number_of_dark_cells = 0
990
-
991
- -- 1: Adjacent modules in row/column in same color
992
- -- --------------------------------------------
993
- -- No. of modules = (5+i) -> 3 + i
994
- local last_bit_blank -- < 0: blank, > 0: black
995
- local is_blank
996
- local number_of_consecutive_bits
997
- -- first: vertical
998
- for x=1,size do
999
- number_of_consecutive_bits = 0
1000
- last_bit_blank = nil
1001
- for y = 1,size do
1002
- if matrix[x][y] > 0 then
1003
- -- small optimization: this is for penalty 4
1004
- number_of_dark_cells = number_of_dark_cells + 1
1005
- is_blank = false
1006
- else
1007
- is_blank = true
1008
- end
1009
- if last_bit_blank == is_blank then
1010
- number_of_consecutive_bits = number_of_consecutive_bits + 1
1011
- else
1012
- if number_of_consecutive_bits >= 5 then
1013
- penalty1 = penalty1 + number_of_consecutive_bits - 2
1014
- end
1015
- number_of_consecutive_bits = 1
1016
- end
1017
- last_bit_blank = is_blank
1018
- end
1019
- if number_of_consecutive_bits >= 5 then
1020
- penalty1 = penalty1 + number_of_consecutive_bits - 2
1021
- end
1022
- end
1023
- -- now horizontal
1024
- for y=1,size do
1025
- number_of_consecutive_bits = 0
1026
- last_bit_blank = nil
1027
- for x = 1,size do
1028
- is_blank = matrix[x][y] < 0
1029
- if last_bit_blank == is_blank then
1030
- number_of_consecutive_bits = number_of_consecutive_bits + 1
1031
- else
1032
- if number_of_consecutive_bits >= 5 then
1033
- penalty1 = penalty1 + number_of_consecutive_bits - 2
1034
- end
1035
- number_of_consecutive_bits = 1
1036
- end
1037
- last_bit_blank = is_blank
1038
- end
1039
- if number_of_consecutive_bits >= 5 then
1040
- penalty1 = penalty1 + number_of_consecutive_bits - 2
1041
- end
1042
- end
1043
- for x=1,size do
1044
- for y=1,size do
1045
- -- 2: Block of modules in same color
1046
- -- -----------------------------------
1047
- -- Blocksize = m x n -> 3 x (m-1) x (n-1)
1048
- if (y < size - 1) and (x < size - 1) and (
1049
- (matrix[x][y] < 0 and matrix[x+1][y] < 0 and matrix[x][y+1] < 0 and matrix[x+1][y+1] < 0) or
1050
- (matrix[x][y] > 0 and matrix[x+1][y] > 0 and matrix[x][y+1] > 0 and matrix[x+1][y+1] > 0)
1051
- ) then penalty2 = penalty2 + 3 end
1052
-
1053
- -- 3: 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column
1054
- -- ------------------------------------------------------------------
1055
- -- Gives 40 points each
1056
- --
1057
- -- I have no idea why we need the extra 0000 on left or right side. The spec doesn't mention it,
1058
- -- other sources do mention it. This is heavily inspired by zxing.
1059
- if (y + 6 < size and
1060
- matrix[x][y] > 0 and
1061
- matrix[x][y + 1] < 0 and
1062
- matrix[x][y + 2] > 0 and
1063
- matrix[x][y + 3] > 0 and
1064
- matrix[x][y + 4] > 0 and
1065
- matrix[x][y + 5] < 0 and
1066
- matrix[x][y + 6] > 0 and
1067
- ((y + 10 < size and
1068
- matrix[x][y + 7] < 0 and
1069
- matrix[x][y + 8] < 0 and
1070
- matrix[x][y + 9] < 0 and
1071
- matrix[x][y + 10] < 0) or
1072
- (y - 4 >= 1 and
1073
- matrix[x][y - 1] < 0 and
1074
- matrix[x][y - 2] < 0 and
1075
- matrix[x][y - 3] < 0 and
1076
- matrix[x][y - 4] < 0))) then penalty3 = penalty3 + 40 end
1077
- if (x + 6 <= size and
1078
- matrix[x][y] > 0 and
1079
- matrix[x + 1][y] < 0 and
1080
- matrix[x + 2][y] > 0 and
1081
- matrix[x + 3][y] > 0 and
1082
- matrix[x + 4][y] > 0 and
1083
- matrix[x + 5][y] < 0 and
1084
- matrix[x + 6][y] > 0 and
1085
- ((x + 10 <= size and
1086
- matrix[x + 7][y] < 0 and
1087
- matrix[x + 8][y] < 0 and
1088
- matrix[x + 9][y] < 0 and
1089
- matrix[x + 10][y] < 0) or
1090
- (x - 4 >= 1 and
1091
- matrix[x - 1][y] < 0 and
1092
- matrix[x - 2][y] < 0 and
1093
- matrix[x - 3][y] < 0 and
1094
- matrix[x - 4][y] < 0))) then penalty3 = penalty3 + 40 end
1095
- end
1096
- end
1097
- -- 4: Proportion of dark modules in entire symbol
1098
- -- ----------------------------------------------
1099
- -- 50 +/- (5 x k)% to 50 +/- (5 x (k + 1))% -> 10 x k
1100
- local dark_ratio = number_of_dark_cells / (size * size)
1101
- local penalty4 = floor(abs(dark_ratio * 100 - 50)) * 2
1102
- return penalty1 + penalty2 + penalty3 + penalty4
1103
- end
1104
-
1105
- -- Create a matrix for the given parameters and calculate the penalty score.
1106
- -- Return both (matrix and penalty)
1107
- local function get_matrix_and_penalty(version,ec_level,data,mask)
1108
- local tab = prepare_matrix_with_mask(version,ec_level,mask)
1109
- add_data_to_matrix(tab,data,mask)
1110
- local penalty = calculate_penalty(tab)
1111
- return tab, penalty
1112
- end
1113
-
1114
- -- Return the matrix with the smallest penalty. To to this
1115
- -- we try out the matrix for all 8 masks and determine the
1116
- -- penalty (score) each.
1117
- local function get_matrix_with_lowest_penalty(version,ec_level,data)
1118
- local tab, penalty
1119
- local tab_min_penalty, min_penalty
1120
-
1121
- -- try masks 0-7
1122
- tab_min_penalty, min_penalty = get_matrix_and_penalty(version,ec_level,data,0)
1123
- for i=1,7 do
1124
- tab, penalty = get_matrix_and_penalty(version,ec_level,data,i)
1125
- if penalty < min_penalty then
1126
- tab_min_penalty = tab
1127
- min_penalty = penalty
1128
- end
1129
- end
1130
- return tab_min_penalty
1131
- end
1132
-
1133
- --- The main function. We connect everything together. Remember from above:
1134
- ---
1135
- --- 1. Determine version, ec level and mode (=encoding) for codeword
1136
- --- 1. Encode data
1137
- --- 1. Arrange data and calculate error correction code
1138
- --- 1. Generate 8 matrices with different masks and calculate the penalty
1139
- --- 1. Return qrcode with least penalty
1140
-
1141
- -- Return
1142
- -- on success: true, number matrix (only has +/-1&+/-2. positive means black, +/-2 means mandatory, in case if you didn't read comments above)
1143
- -- on failed: false, error message string
1144
- -- If ec_level or mode is given, use the ones for generating the qrcode. (mode option is not implemented yet, but it will be determined automatically)
1145
- local function qrcode(str,ec_level,mode_enc)
1146
- local arranged_data, version, data_raw, mode, len_bitstring
1147
- version, ec_level, data_raw, mode, len_bitstring = get_version_eclevel_mode_bistringlength(str,ec_level,mode_enc)
1148
- data_raw = data_raw .. len_bitstring
1149
- data_raw = data_raw .. encode_data(str,mode)
1150
- data_raw = add_pad_data(version,ec_level,data_raw)
1151
- arranged_data = arrange_codewords_and_calculate_ec(version,ec_level,data_raw)
1152
- if #arranged_data % 8 ~= 0 then
1153
- return false, format("Arranged data %% 8 != 0: data length = %d, mod 8 = %d",#arranged_data, #arranged_data % 8)
1154
- end
1155
- arranged_data = arranged_data .. rep("0",remainder[version])
1156
- local tab = get_matrix_with_lowest_penalty(version,ec_level,arranged_data)
1157
- return true, tab
1158
- end
1159
- return qrcode
1160
- end
1161
-
1162
- ns.AutomationQR = ns.AutomationQR or {}
1163
-
1164
- function ns.AutomationQR.Encode(text, ecLevel)
1165
- if type(text) ~= "string" or text == "" then
1166
- return nil, "empty payload"
1167
- end
1168
- if not encode then
1169
- encode = BuildEncoder()
1170
- end
1171
- local ok, matrix = encode(text, tonumber(ecLevel) or 2)
1172
- if not ok then
1173
- return nil, tostring(matrix)
1174
- end
1175
- return matrix
1176
- end
1
+ --- The qrcode library is licensed under the 3-clause BSD license (aka "new BSD")
2
+ --- To get in contact with the author, mail to <gundlach@speedata.de>.
3
+ ---
4
+ --- Please report bugs on the [github project page](http://speedata.github.io/luaqrcode/).
5
+ -- Copyright (c) 2012-2020, Patrick Gundlach and contributors, see https://github.com/speedata/luaqrcode
6
+ -- All rights reserved.
7
+ --
8
+ -- Redistribution and use in source and binary forms, with or without
9
+ -- modification, are permitted provided that the following conditions are met:
10
+ -- * Redistributions of source code must retain the above copyright
11
+ -- notice, this list of conditions and the following disclaimer.
12
+ -- * Redistributions in binary form must reproduce the above copyright
13
+ -- notice, this list of conditions and the following disclaimer in the
14
+ -- documentation and/or other materials provided with the distribution.
15
+ -- * Neither the name of SPEEDATA nor the
16
+ -- names of its contributors may be used to endorse or promote products
17
+ -- derived from this software without specific prior written permission.
18
+ --
19
+ -- THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20
+ -- ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21
+ -- WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ -- DISCLAIMED. IN NO EVENT SHALL SPEEDATA GMBH BE LIABLE FOR ANY
23
+ -- DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24
+ -- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25
+ -- LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26
+ -- ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27
+ -- (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28
+ -- SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
+
30
+ -- Lychee Dev adaptation of luaqrcode (upstream: https://github.com/speedata/luaqrcode).
31
+ -- Changes from the reference copy: the encoder is built lazily inside
32
+ -- BuildEncoder() so loading this file stays allocation-free (the upstream load
33
+ -- constructs a 256x256 xor lookup table immediately), the test-mode and global
34
+ -- assignment blocks were removed, and the API is exposed as ns.AutomationQR
35
+ -- instead of a global. Encoding behavior is unchanged.
36
+
37
+ local ADDON_NAME, ns = ...
38
+
39
+ local encode
40
+
41
+ local function BuildEncoder()
42
+
43
+
44
+ --- Overall workflow
45
+ --- ================
46
+ --- The steps to generate the qrcode, assuming we already have the codeword:
47
+ ---
48
+ --- 1. Determine version, ec level and mode (=encoding) for codeword
49
+ --- 1. Encode data
50
+ --- 1. Arrange data and calculate error correction code
51
+ --- 1. Generate 8 matrices with different masks and calculate the penalty
52
+ --- 1. Return qrcode with least penalty
53
+ ---
54
+ --- Each step is of course more or less complex and needs further description
55
+
56
+
57
+
58
+ --- Helper functions
59
+ --- ================
60
+ ---
61
+ --- We start with some helper functions
62
+
63
+ local max,min=math.max,math.min
64
+ local floor,abs=math.floor,math.abs
65
+ local byte,sub,rep=string.byte,string.sub,string.rep
66
+ local gsub,match,format=string.gsub,string.match,string.format
67
+ local concat = table.concat
68
+
69
+
70
+ local xor_lookup = {}
71
+ do
72
+ -- Build a fast xor helper that stays compatible across Lua versions.
73
+ -- We precompute a 256x256 lookup table once at load time using a portable
74
+ -- arithmetic xor; the hot path is then a constant-time table lookup without
75
+ -- requiring native bitwise operators or external bit libraries.
76
+ -- Slow but portable xor used only while populating the lookup table if no native op exists.
77
+ local function slow_xor(a,b)
78
+ local result = 0
79
+ local bitval = 1
80
+ while a > 0 or b > 0 do
81
+ if (a % 2) ~= (b % 2) then
82
+ result = result + bitval
83
+ end
84
+ a = floor(a / 2)
85
+ b = floor(b / 2)
86
+ bitval = bitval * 2
87
+ end
88
+ return result
89
+ end
90
+
91
+ -- Always build a 256x256 table once at load time; avoids any reliance on bitwise operators.
92
+ for i=0,255 do
93
+ local row = {}
94
+ for j=0,255 do
95
+ row[j] = slow_xor(i,j)
96
+ end
97
+ xor_lookup[i] = row
98
+ end
99
+ end
100
+
101
+ local decToHexTable={
102
+ ["0"]="0000",["1"]="0001",["2"]="0010",["3"]="0011",
103
+ ["4"]="0100",["5"]="0101",["6"]="0110",["7"]="0111",
104
+ ["8"]="1000",["9"]="1001",["a"]="1010",["b"]="1011",
105
+ ["c"]="1100",["d"]="1101",["e"]="1110",["f"]="1111",
106
+ }
107
+ local function decToHex(d) return decToHexTable[d] end
108
+ -- Return the binary representation of the number x with the width of `digits`.
109
+ local function binary(x,digits)
110
+ local s = format("%x",x) -- dec to hex
111
+ s = gsub(s,"(.)",decToHex) -- hex to bin
112
+ s = gsub(s,"^0+","") -- remove leading 0s
113
+ return rep("0",digits - #s) .. s
114
+ end
115
+
116
+ -- A small helper function for add_typeinfo_to_matrix() and add_version_information()
117
+ -- Add a 2 (black by default) / -2 (blank by default) to the matrix at position x,y
118
+ -- depending on the bitstring (size 1!) where "0"=blank and "1"=black.
119
+ local function fill_matrix_position(matrix,bitstr,x,y)
120
+ matrix[x][y] = bitstr == "1" and 2 or -2
121
+ end
122
+
123
+
124
+
125
+ --- Step 1: Determine version, ec level and mode for codeword
126
+ --- =========================================================
127
+ ---
128
+ --- First we need to find out the version (= size) of the QR code. This depends on
129
+ --- the input data (the mode to be used), the requested error correction level
130
+ --- (normally we use the maximum level that fits into the minimal size).
131
+
132
+ -- Return the mode for the given string `str`.
133
+ -- See table 2 of the spec. We only support mode 1, 2 and 4.
134
+ -- That is: numeric, alaphnumeric and binary.
135
+ local function get_mode(str)
136
+ if match(str,"^[0-9]+$") then
137
+ return 1
138
+ elseif match(str,"^[0-9A-Z $%%*./:+-]+$") then
139
+ return 2
140
+ else
141
+ return 4
142
+ end
143
+ assert(false,"never reached") -- luacheck: ignore
144
+ return nil
145
+ end
146
+
147
+ --- Capacity of QR codes
148
+ --- --------------------
149
+ --- The capacity is calculated as follow: \\(\text{Number of data bits} = \text{number of codewords} * 8\\).
150
+ --- The number of data bits is now reduced by 4 (the mode indicator) and the length string,
151
+ --- that varies between 8 and 16, depending on the version and the mode (see method `get_length()`). The
152
+ --- remaining capacity is multiplied by the amount of data per bit string (numeric: 3, alphanumeric: 2, other: 1)
153
+ --- and divided by the length of the bit string (numeric: 10, alphanumeric: 11, binary: 8, kanji: 13).
154
+ --- Then the floor function is applied to the result:
155
+ --- $$\Big\lfloor \frac{( \text{#data bits} - 4 - \text{length string}) * \text{data per bit string}}{\text{length of the bit string}} \Big\rfloor$$
156
+ ---
157
+ --- There is one problem remaining. The length string depends on the version,
158
+ --- and the version depends on the length string. But we take this into account when calculating the
159
+ --- the capacity, so this is not really a problem here.
160
+
161
+ -- The capacity (number of codewords) of each version (1-40) for error correction levels 1-4 (LMQH).
162
+ -- The higher the ec level, the lower the capacity of the version. Taken from spec, tables 7-11.
163
+ local capacity = {
164
+ { 19, 16, 13, 9},{ 34, 28, 22, 16},{ 55, 44, 34, 26},{ 80, 64, 48, 36},
165
+ { 108, 86, 62, 46},{ 136, 108, 76, 60},{ 156, 124, 88, 66},{ 194, 154, 110, 86},
166
+ { 232, 182, 132, 100},{ 274, 216, 154, 122},{ 324, 254, 180, 140},{ 370, 290, 206, 158},
167
+ { 428, 334, 244, 180},{ 461, 365, 261, 197},{ 523, 415, 295, 223},{ 589, 453, 325, 253},
168
+ { 647, 507, 367, 283},{ 721, 563, 397, 313},{ 795, 627, 445, 341},{ 861, 669, 485, 385},
169
+ { 932, 714, 512, 406},{1006, 782, 568, 442},{1094, 860, 614, 464},{1174, 914, 664, 514},
170
+ {1276, 1000, 718, 538},{1370, 1062, 754, 596},{1468, 1128, 808, 628},{1531, 1193, 871, 661},
171
+ {1631, 1267, 911, 701},{1735, 1373, 985, 745},{1843, 1455, 1033, 793},{1955, 1541, 1115, 845},
172
+ {2071, 1631, 1171, 901},{2191, 1725, 1231, 961},{2306, 1812, 1286, 986},{2434, 1914, 1354, 1054},
173
+ {2566, 1992, 1426, 1096},{2702, 2102, 1502, 1142},{2812, 2216, 1582, 1222},{2956, 2334, 1666, 1276},
174
+ }
175
+
176
+ --- Return the smallest version for this codeword. If `requested_ec_level` is supplied,
177
+ --- then the ec level (LMQH - 1,2,3,4) must be at least the requested level.
178
+ -- mode = 1,2,4,8
179
+ local function get_version_eclevel(len,mode,requested_ec_level)
180
+ local local_mode = mode
181
+ if mode == 4 then
182
+ local_mode = 3
183
+ elseif mode == 8 then
184
+ local_mode = 4
185
+ end
186
+ assert( local_mode <= 4 )
187
+
188
+ local bits, digits, modebits, c
189
+ local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
190
+ local minversion = 99 -- placeholder, must be replaced by a lower value
191
+ local maxec_level = requested_ec_level or 1
192
+ local minlv,maxlv = 1, 4
193
+ if requested_ec_level and requested_ec_level >= 1 and requested_ec_level <= 4 then
194
+ minlv = requested_ec_level
195
+ maxlv = requested_ec_level
196
+ end
197
+ for ec_level=minlv,maxlv do
198
+ for version=1,#capacity do
199
+ bits = capacity[version][ec_level] * 8
200
+ bits = bits - 4 -- the mode indicator
201
+ if version < 10 then
202
+ digits = tab[1][local_mode]
203
+ elseif version < 27 then
204
+ digits = tab[2][local_mode]
205
+ elseif version <= 40 then
206
+ digits = tab[3][local_mode]
207
+ end
208
+ modebits = bits - digits
209
+ if local_mode == 1 then -- numeric
210
+ c = floor(modebits * 3 / 10)
211
+ elseif local_mode == 2 then -- alphanumeric
212
+ c = floor(modebits * 2 / 11)
213
+ elseif local_mode == 3 then -- binary
214
+ c = floor(modebits * 1 / 8)
215
+ else
216
+ c = floor(modebits * 1 / 13)
217
+ end
218
+ if c >= len then
219
+ if version <= minversion then
220
+ minversion = version
221
+ maxec_level = ec_level
222
+ end
223
+ break
224
+ end
225
+ end
226
+ end
227
+ assert(minversion<=40,"Data too long to encode in QR code")
228
+ return minversion, maxec_level
229
+ end
230
+
231
+ -- Return a bit string of 0s and 1s that includes the length of the code string.
232
+ -- The modes are numeric = 1, alphanumeric = 2, binary = 4, and japanese = 8
233
+ local function get_length(str,version,mode)
234
+ local i = mode
235
+ if mode == 4 then
236
+ i = 3
237
+ elseif mode == 8 then
238
+ i = 4
239
+ end
240
+ assert( i <= 4 )
241
+ local tab = { {10,9,8,8},{12,11,16,10},{14,13,16,12} }
242
+ local digits
243
+ if version < 10 then
244
+ digits = tab[1][i]
245
+ elseif version < 27 then
246
+ digits = tab[2][i]
247
+ elseif version <= 40 then
248
+ digits = tab[3][i]
249
+ else
250
+ assert(false, "get_length, version > 40 not supported")
251
+ end
252
+ local len = binary(#str,digits)
253
+ return len
254
+ end
255
+
256
+ --- If the `requested_ec_level` or the `mode` are provided, this will be used if possible.
257
+ --- The mode depends on the characters used in the string `str`. It seems to be
258
+ --- possible to split the QR code to handle multiple modes, but we don't do that.
259
+ local function get_version_eclevel_mode_bistringlength(str,requested_ec_level,mode)
260
+ local local_mode
261
+ if mode then
262
+ assert(false,"not implemented")
263
+ -- check if the mode is OK for the string
264
+ local_mode = mode
265
+ else
266
+ local_mode = get_mode(str)
267
+ end
268
+ local version, ec_level
269
+ version, ec_level = get_version_eclevel(#str,local_mode,requested_ec_level)
270
+ local length_string = get_length(str,version,local_mode)
271
+ return version,ec_level,binary(local_mode,4),local_mode,length_string
272
+ end
273
+
274
+
275
+
276
+ --- Step 2: Encode data
277
+ --- ===================
278
+ ---
279
+ --- There are several ways to encode the data. We currently support only numeric, alphanumeric and binary.
280
+ --- We already chose the encoding (a.k.a. mode) in the first step, so we need to apply the mode to the
281
+ --- codeword.
282
+ ---
283
+ --- **Numeric**: take three digits and encode them in 10 bits
284
+ --- **Alphanumeric**: take two characters and encode them in 11 bits
285
+ --- **Binary**: take one octet and encode it in 8 bits
286
+
287
+ local asciitbl = {
288
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -- 0x01-0x0f
289
+ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -- 0x10-0x1f
290
+ 36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, -- 0x20-0x2f
291
+ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 44, -1, -1, -1, -1, -1, -- 0x30-0x3f
292
+ -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, -- 0x40-0x4f
293
+ 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -- 0x50-0x5f
294
+ }
295
+
296
+ -- Return a binary representation of the numeric string `str`. This must contain only digits 0-9.
297
+ local function encode_string_numeric(str)
298
+ local encodebuffer = {}
299
+ for i = 1, #str, 3 do
300
+ local a = sub(str,i,i+2)
301
+ -- #a is 1, 2, or 3, so bits are 4, 7, or 10
302
+ encodebuffer[#encodebuffer+1]=binary(tonumber(a), #a * 3 + 1)
303
+ end
304
+ return table.concat(encodebuffer)
305
+ end
306
+
307
+ -- Return a binary representation of the alphanumeric string `str`. This must contain only
308
+ -- digits 0-9, uppercase letters A-Z, space and the following chars: $%*./:+-.
309
+ local function encode_string_ascii(str)
310
+ local encodebuffer = {}
311
+ local int
312
+ local b1, b2
313
+ for i = 1, #str, 2 do
314
+ local a = sub(str,i,i+1)
315
+ if #a == 2 then
316
+ b1 = asciitbl[byte(sub(a,1,1))]
317
+ b2 = asciitbl[byte(sub(a,2,2))]
318
+ int = b1 * 45 + b2
319
+ encodebuffer[#encodebuffer+1] = binary(int,11)
320
+ else
321
+ int = asciitbl[byte(a)]
322
+ encodebuffer[#encodebuffer+1] = binary(int,6)
323
+ end
324
+ end
325
+ return table.concat(encodebuffer)
326
+ end
327
+
328
+ -- Return a bitstring representing string str in binary mode.
329
+ -- We don't handle UTF-8 in any special way because we assume the
330
+ -- scanner recognizes UTF-8 and displays it correctly.
331
+ local function encode_string_binary(str)
332
+ local encodebuffer = {}
333
+ for i = 1, #str do
334
+ encodebuffer[i] = binary(byte(str,i),8)
335
+ end
336
+ return table.concat(encodebuffer)
337
+ end
338
+
339
+ -- Return a bitstring representing string str in the given mode.
340
+ local function encode_data(str,mode)
341
+ if mode == 1 then
342
+ return encode_string_numeric(str)
343
+ elseif mode == 2 then
344
+ return encode_string_ascii(str)
345
+ elseif mode == 4 then
346
+ return encode_string_binary(str)
347
+ else
348
+ assert(false,"not implemented yet")
349
+ end
350
+ end
351
+
352
+ -- Encoding the codeword is not enough. We need to make sure that
353
+ -- the length of the binary string is equal to the number of codewords of the version.
354
+ local function add_pad_data(version,ec_level,data)
355
+ local cpty = capacity[version][ec_level] * 8
356
+ local buffer = {data}
357
+ local buffer_len = #data
358
+ local count_to_pad = min(4,cpty - buffer_len)
359
+ if count_to_pad > 0 then
360
+ buffer[#buffer + 1] = rep("0",count_to_pad)
361
+ buffer_len = buffer_len + count_to_pad
362
+ end
363
+ if buffer_len % 8 ~= 0 then
364
+ local missing = 8 - buffer_len % 8
365
+ buffer[#buffer + 1] = rep("0",missing)
366
+ buffer_len = buffer_len + missing
367
+ end
368
+ -- add "11101100" and "00010001" until enough data
369
+ local remaining_bytes = (cpty - buffer_len) / 8 -- decimal doesn't matter
370
+ for i=1,remaining_bytes do
371
+ buffer[#buffer + 1] = i % 2 == 1 and "11101100" or "00010001"
372
+ end
373
+ return concat(buffer)
374
+ end
375
+
376
+
377
+
378
+ --- Step 3: Organize data and calculate error correction code
379
+ --- =========================================================
380
+ --- The data in the qrcode is not encoded linearly. For example code 5-H has four blocks, the first two blocks
381
+ --- contain 11 codewords and 22 error correction codes each, the second block contain 12 codewords and 22 ec codes each.
382
+ --- We just take the table from the spec and don't calculate the blocks ourself. The table `ecblocks` contains this info.
383
+ ---
384
+ --- During the phase of splitting the data into codewords, we do the calculation for error correction codes. This step involves
385
+ --- polynomial division. Find a math book from school and follow the code here :)
386
+
387
+ --- ### Reed Solomon error correction
388
+ --- Now this is the slightly ugly part of the error correction. We start with log/antilog tables
389
+ -- https://codyplanteen.com/assets/rs/gf256_log_antilog.pdf
390
+ local alpha_int = {
391
+ [0] = 1,
392
+ 2, 4, 8, 16, 32, 64, 128, 29, 58, 116, 232, 205, 135, 19, 38, 76,
393
+ 152, 45, 90, 180, 117, 234, 201, 143, 3, 6, 12, 24, 48, 96, 192, 157,
394
+ 39, 78, 156, 37, 74, 148, 53, 106, 212, 181, 119, 238, 193, 159, 35, 70,
395
+ 140, 5, 10, 20, 40, 80, 160, 93, 186, 105, 210, 185, 111, 222, 161, 95,
396
+ 190, 97, 194, 153, 47, 94, 188, 101, 202, 137, 15, 30, 60, 120, 240, 253,
397
+ 231, 211, 187, 107, 214, 177, 127, 254, 225, 223, 163, 91, 182, 113, 226, 217,
398
+ 175, 67, 134, 17, 34, 68, 136, 13, 26, 52, 104, 208, 189, 103, 206, 129,
399
+ 31, 62, 124, 248, 237, 199, 147, 59, 118, 236, 197, 151, 51, 102, 204, 133,
400
+ 23, 46, 92, 184, 109, 218, 169, 79, 158, 33, 66, 132, 21, 42, 84, 168,
401
+ 77, 154, 41, 82, 164, 85, 170, 73, 146, 57, 114, 228, 213, 183, 115, 230,
402
+ 209, 191, 99, 198, 145, 63, 126, 252, 229, 215, 179, 123, 246, 241, 255, 227,
403
+ 219, 171, 75, 150, 49, 98, 196, 149, 55, 110, 220, 165, 87, 174, 65, 130,
404
+ 25, 50, 100, 200, 141, 7, 14, 28, 56, 112, 224, 221, 167, 83, 166, 81,
405
+ 162, 89, 178, 121, 242, 249, 239, 195, 155, 43, 86, 172, 69, 138, 9, 18,
406
+ 36, 72, 144, 61, 122, 244, 245, 247, 243, 251, 235, 203, 139, 11, 22, 44,
407
+ 88, 176, 125, 250, 233, 207, 131, 27, 54, 108, 216, 173, 71, 142, 0, 0
408
+ }
409
+
410
+ local int_alpha = {
411
+ [0] = 256, -- special value
412
+ 0, 1, 25, 2, 50, 26, 198, 3, 223, 51, 238, 27, 104, 199, 75, 4,
413
+ 100, 224, 14, 52, 141, 239, 129, 28, 193, 105, 248, 200, 8, 76, 113, 5,
414
+ 138, 101, 47, 225, 36, 15, 33, 53, 147, 142, 218, 240, 18, 130, 69, 29,
415
+ 181, 194, 125, 106, 39, 249, 185, 201, 154, 9, 120, 77, 228, 114, 166, 6,
416
+ 191, 139, 98, 102, 221, 48, 253, 226, 152, 37, 179, 16, 145, 34, 136, 54,
417
+ 208, 148, 206, 143, 150, 219, 189, 241, 210, 19, 92, 131, 56, 70, 64, 30,
418
+ 66, 182, 163, 195, 72, 126, 110, 107, 58, 40, 84, 250, 133, 186, 61, 202,
419
+ 94, 155, 159, 10, 21, 121, 43, 78, 212, 229, 172, 115, 243, 167, 87, 7,
420
+ 112, 192, 247, 140, 128, 99, 13, 103, 74, 222, 237, 49, 197, 254, 24, 227,
421
+ 165, 153, 119, 38, 184, 180, 124, 17, 68, 146, 217, 35, 32, 137, 46, 55,
422
+ 63, 209, 91, 149, 188, 207, 205, 144, 135, 151, 178, 220, 252, 190, 97, 242,
423
+ 86, 211, 171, 20, 42, 93, 158, 132, 60, 57, 83, 71, 109, 65, 162, 31,
424
+ 45, 67, 216, 183, 123, 164, 118, 196, 23, 73, 236, 127, 12, 111, 246, 108,
425
+ 161, 59, 82, 41, 157, 85, 170, 251, 96, 134, 177, 187, 204, 62, 90, 203,
426
+ 89, 95, 176, 156, 169, 160, 81, 11, 245, 22, 235, 122, 117, 44, 215, 79,
427
+ 174, 213, 233, 230, 231, 173, 232, 116, 214, 244, 234, 168, 80, 88, 175
428
+ }
429
+
430
+ -- We only need the polynomial generators for block sizes 7, 10, 13, 15, 16, 17, 18, 20, 22, 24, 26, 28, and 30. Version
431
+ -- 2 of the qr codes don't need larger ones (as opposed to version 1). The table has the format x^1*a^21 + x^2*a^102 ...
432
+ local generator_polynomial = {
433
+ [7] = { 21, 102, 238, 149, 146, 229, 87, 0},
434
+ [10] = { 45, 32, 94, 64, 70, 118, 61, 46, 67, 251, 0 },
435
+ [13] = { 78, 140, 206, 218, 130, 104, 106, 100, 86, 100, 176, 152, 74, 0 },
436
+ [15] = {105, 99, 5, 124, 140, 237, 58, 58, 51, 37, 202, 91, 61, 183, 8, 0},
437
+ [16] = {120, 225, 194, 182, 169, 147, 191, 91, 3, 76, 161, 102, 109, 107, 104, 120, 0},
438
+ [17] = {136, 163, 243, 39, 150, 99, 24, 147, 214, 206, 123, 239, 43, 78, 206, 139, 43, 0},
439
+ [18] = {153, 96, 98, 5, 179, 252, 148, 152, 187, 79, 170, 118, 97, 184, 94, 158, 234, 215, 0},
440
+ [20] = {190, 188, 212, 212, 164, 156, 239, 83, 225, 221, 180, 202, 187, 26, 163, 61, 50, 79, 60, 17, 0},
441
+ [22] = {231, 165, 105, 160, 134, 219, 80, 98, 172, 8, 74, 200, 53, 221, 109, 14, 230, 93, 242, 247, 171, 210, 0},
442
+ [24] = { 21, 227, 96, 87, 232, 117, 0, 111, 218, 228, 226, 192, 152, 169, 180, 159, 126, 251, 117, 211, 48, 135, 121, 229, 0},
443
+ [26] = { 70, 218, 145, 153, 227, 48, 102, 13, 142, 245, 21, 161, 53, 165, 28, 111, 201, 145, 17, 118, 182, 103, 2, 158, 125, 173, 0},
444
+ [28] = {123, 9, 37, 242, 119, 212, 195, 42, 87, 245, 43, 21, 201, 232, 27, 205, 147, 195, 190, 110, 180, 108, 234, 224, 104, 200, 223, 168, 0},
445
+ [30] = {180, 192, 40, 238, 216, 251, 37, 156, 130, 224, 193, 226, 173, 42, 125, 222, 96, 239, 86, 110, 48, 50, 182, 179, 31, 216, 152, 145, 173, 41, 0}}
446
+
447
+
448
+ -- Turn a binary string of length 8*x into a table size x of numbers.
449
+ local function convert_bitstring_to_bytes(data)
450
+ local msg = {}
451
+ for i=1, #data / 8 do
452
+ msg[i] = tonumber(sub(data,(i - 1) * 8 + 1,i * 8),2)
453
+ end
454
+ return msg
455
+ end
456
+
457
+ -- Return a table that has 0's in the first entries and then the alpha
458
+ -- representation of the generator polynomial
459
+ local function get_generator_polynomial_adjusted(num_ec_codewords,highest_exponent)
460
+ local gp_alpha = {[0]=0}
461
+ for i=0,highest_exponent - num_ec_codewords - 1 do
462
+ gp_alpha[i] = 0
463
+ end
464
+ local gp = generator_polynomial[num_ec_codewords]
465
+ for i=1,num_ec_codewords + 1 do
466
+ gp_alpha[highest_exponent - num_ec_codewords + i - 1] = gp[i]
467
+ end
468
+ return gp_alpha
469
+ end
470
+
471
+ -- That's the heart of the error correction calculation.
472
+ local function calculate_error_correction(data,num_ec_codewords)
473
+ local mp
474
+ if type(data)=="string" then
475
+ mp = convert_bitstring_to_bytes(data)
476
+ elseif type(data)=="table" then
477
+ mp = data
478
+ else
479
+ assert(false,format("Unknown type for data: %s",type(data)))
480
+ end
481
+ local len_message = #mp
482
+
483
+ local highest_exponent = len_message + num_ec_codewords - 1
484
+ local gp_alpha
485
+ local mp_int = {}
486
+ -- create message shifted to left (highest exponent)
487
+ for i=1,len_message do
488
+ mp_int[highest_exponent - i + 1] = mp[i]
489
+ end
490
+ for i=1,highest_exponent - len_message do
491
+ mp_int[i] = 0
492
+ end
493
+ mp_int[0] = 0
494
+
495
+ while highest_exponent >= num_ec_codewords do
496
+ gp_alpha = get_generator_polynomial_adjusted(num_ec_codewords,highest_exponent)
497
+
498
+ -- Multiply generator polynomial by first coefficient of the above polynomial
499
+
500
+ -- take the highest exponent from the message polynom (alpha) and add
501
+ -- it to the generator polynom
502
+ local exp = int_alpha[mp_int[highest_exponent]]
503
+ for i=highest_exponent,highest_exponent - num_ec_codewords,-1 do
504
+ if exp ~= 256 then
505
+ gp_alpha[i] = (gp_alpha[i] + exp) % 255
506
+ else
507
+ gp_alpha[i] = 256
508
+ end
509
+ end
510
+ for i=highest_exponent - num_ec_codewords - 1,0,-1 do
511
+ gp_alpha[i] = 256
512
+ end
513
+
514
+ for i=highest_exponent,0,-1 do
515
+ mp_int[i] = xor_lookup[alpha_int[gp_alpha[i]]][mp_int[i]]
516
+ end
517
+ -- remove leading 0's
518
+ for i=highest_exponent,num_ec_codewords,-1 do
519
+ if mp_int[i]==0 then
520
+ highest_exponent=i-1
521
+ else
522
+ break
523
+ end
524
+ end
525
+
526
+ if highest_exponent<num_ec_codewords then break end
527
+ end
528
+ local ret = {}
529
+
530
+ -- reverse data
531
+ for i=highest_exponent,0,-1 do
532
+ ret[#ret + 1] = mp_int[i]
533
+ end
534
+ return ret
535
+ end
536
+
537
+ --- #### Arranging the data
538
+ --- Now we arrange the data into smaller chunks. This table is taken from the spec.
539
+ -- ecblocks has 40 entries, one for each version. Each version entry has 4 entries, for each LMQH
540
+ -- ec level. Each entry has two or four fields, the odd files are the number of repetitions for the
541
+ -- following block info. The first entry of the block is the total number of codewords in the block,
542
+ -- the second entry is the number of data codewords. The third is not important.
543
+ local ecblocks = {
544
+ {{ 1,{ 26, 19, 2} }, { 1,{26,16, 4}}, { 1,{26,13, 6}}, { 1, {26, 9, 8} }},
545
+ {{ 1,{ 44, 34, 4} }, { 1,{44,28, 8}}, { 1,{44,22,11}}, { 1, {44,16,14} }},
546
+ {{ 1,{ 70, 55, 7} }, { 1,{70,44,13}}, { 2,{35,17, 9}}, { 2, {35,13,11} }},
547
+ {{ 1,{100, 80,10} }, { 2,{50,32, 9}}, { 2,{50,24,13}}, { 4, {25, 9, 8} }},
548
+ {{ 1,{134,108,13} }, { 2,{67,43,12}}, { 2,{33,15, 9}, 2,{34,16, 9}}, { 2, {33,11,11}, 2,{34,12,11}}},
549
+ {{ 2,{ 86, 68, 9} }, { 4,{43,27, 8}}, { 4,{43,19,12}}, { 4, {43,15,14} }},
550
+ {{ 2,{ 98, 78,10} }, { 4,{49,31, 9}}, { 2,{32,14, 9}, 4,{33,15, 9}}, { 4, {39,13,13}, 1,{40,14,13}}},
551
+ {{ 2,{121, 97,12} }, { 2,{60,38,11}, 2,{61,39,11}}, { 4,{40,18,11}, 2,{41,19,11}}, { 4, {40,14,13}, 2,{41,15,13}}},
552
+ {{ 2,{146,116,15} }, { 3,{58,36,11}, 2,{59,37,11}}, { 4,{36,16,10}, 4,{37,17,10}}, { 4, {36,12,12}, 4,{37,13,12}}},
553
+ {{ 2,{ 86, 68, 9}, 2,{ 87, 69, 9}}, { 4,{69,43,13}, 1,{70,44,13}}, { 6,{43,19,12}, 2,{44,20,12}}, { 6, {43,15,14}, 2,{44,16,14}}},
554
+ {{ 4,{101, 81,10} }, { 1,{80,50,15}, 4,{81,51,15}}, { 4,{50,22,14}, 4,{51,23,14}}, { 3, {36,12,12}, 8,{37,13,12}}},
555
+ {{ 2,{116, 92,12}, 2,{117, 93,12}}, { 6,{58,36,11}, 2,{59,37,11}}, { 4,{46,20,13}, 6,{47,21,13}}, { 7, {42,14,14}, 4,{43,15,14}}},
556
+ {{ 4,{133,107,13} }, { 8,{59,37,11}, 1,{60,38,11}}, { 8,{44,20,12}, 4,{45,21,12}}, { 12, {33,11,11}, 4,{34,12,11}}},
557
+ {{ 3,{145,115,15}, 1,{146,116,15}}, { 4,{64,40,12}, 5,{65,41,12}}, { 11,{36,16,10}, 5,{37,17,10}}, { 11, {36,12,12}, 5,{37,13,12}}},
558
+ {{ 5,{109, 87,11}, 1,{110, 88,11}}, { 5,{65,41,12}, 5,{66,42,12}}, { 5,{54,24,15}, 7,{55,25,15}}, { 11, {36,12,12}, 7,{37,13,12}}},
559
+ {{ 5,{122, 98,12}, 1,{123, 99,12}}, { 7,{73,45,14}, 3,{74,46,14}}, { 15,{43,19,12}, 2,{44,20,12}}, { 3, {45,15,15}, 13,{46,16,15}}},
560
+ {{ 1,{135,107,14}, 5,{136,108,14}}, { 10,{74,46,14}, 1,{75,47,14}}, { 1,{50,22,14}, 15,{51,23,14}}, { 2, {42,14,14}, 17,{43,15,14}}},
561
+ {{ 5,{150,120,15}, 1,{151,121,15}}, { 9,{69,43,13}, 4,{70,44,13}}, { 17,{50,22,14}, 1,{51,23,14}}, { 2, {42,14,14}, 19,{43,15,14}}},
562
+ {{ 3,{141,113,14}, 4,{142,114,14}}, { 3,{70,44,13}, 11,{71,45,13}}, { 17,{47,21,13}, 4,{48,22,13}}, { 9, {39,13,13}, 16,{40,14,13}}},
563
+ {{ 3,{135,107,14}, 5,{136,108,14}}, { 3,{67,41,13}, 13,{68,42,13}}, { 15,{54,24,15}, 5,{55,25,15}}, { 15, {43,15,14}, 10,{44,16,14}}},
564
+ {{ 4,{144,116,14}, 4,{145,117,14}}, { 17,{68,42,13}}, { 17,{50,22,14}, 6,{51,23,14}}, { 19, {46,16,15}, 6,{47,17,15}}},
565
+ {{ 2,{139,111,14}, 7,{140,112,14}}, { 17,{74,46,14}}, { 7,{54,24,15}, 16,{55,25,15}}, { 34, {37,13,12} }},
566
+ {{ 4,{151,121,15}, 5,{152,122,15}}, { 4,{75,47,14}, 14,{76,48,14}}, { 11,{54,24,15}, 14,{55,25,15}}, { 16, {45,15,15}, 14,{46,16,15}}},
567
+ {{ 6,{147,117,15}, 4,{148,118,15}}, { 6,{73,45,14}, 14,{74,46,14}}, { 11,{54,24,15}, 16,{55,25,15}}, { 30, {46,16,15}, 2,{47,17,15}}},
568
+ {{ 8,{132,106,13}, 4,{133,107,13}}, { 8,{75,47,14}, 13,{76,48,14}}, { 7,{54,24,15}, 22,{55,25,15}}, { 22, {45,15,15}, 13,{46,16,15}}},
569
+ {{ 10,{142,114,14}, 2,{143,115,14}}, { 19,{74,46,14}, 4,{75,47,14}}, { 28,{50,22,14}, 6,{51,23,14}}, { 33, {46,16,15}, 4,{47,17,15}}},
570
+ {{ 8,{152,122,15}, 4,{153,123,15}}, { 22,{73,45,14}, 3,{74,46,14}}, { 8,{53,23,15}, 26,{54,24,15}}, { 12, {45,15,15}, 28,{46,16,15}}},
571
+ {{ 3,{147,117,15}, 10,{148,118,15}}, { 3,{73,45,14}, 23,{74,46,14}}, { 4,{54,24,15}, 31,{55,25,15}}, { 11, {45,15,15}, 31,{46,16,15}}},
572
+ {{ 7,{146,116,15}, 7,{147,117,15}}, { 21,{73,45,14}, 7,{74,46,14}}, { 1,{53,23,15}, 37,{54,24,15}}, { 19, {45,15,15}, 26,{46,16,15}}},
573
+ {{ 5,{145,115,15}, 10,{146,116,15}}, { 19,{75,47,14}, 10,{76,48,14}}, { 15,{54,24,15}, 25,{55,25,15}}, { 23, {45,15,15}, 25,{46,16,15}}},
574
+ {{ 13,{145,115,15}, 3,{146,116,15}}, { 2,{74,46,14}, 29,{75,47,14}}, { 42,{54,24,15}, 1,{55,25,15}}, { 23, {45,15,15}, 28,{46,16,15}}},
575
+ {{ 17,{145,115,15} }, { 10,{74,46,14}, 23,{75,47,14}}, { 10,{54,24,15}, 35,{55,25,15}}, { 19, {45,15,15}, 35,{46,16,15}}},
576
+ {{ 17,{145,115,15}, 1,{146,116,15}}, { 14,{74,46,14}, 21,{75,47,14}}, { 29,{54,24,15}, 19,{55,25,15}}, { 11, {45,15,15}, 46,{46,16,15}}},
577
+ {{ 13,{145,115,15}, 6,{146,116,15}}, { 14,{74,46,14}, 23,{75,47,14}}, { 44,{54,24,15}, 7,{55,25,15}}, { 59, {46,16,15}, 1,{47,17,15}}},
578
+ {{ 12,{151,121,15}, 7,{152,122,15}}, { 12,{75,47,14}, 26,{76,48,14}}, { 39,{54,24,15}, 14,{55,25,15}}, { 22, {45,15,15}, 41,{46,16,15}}},
579
+ {{ 6,{151,121,15}, 14,{152,122,15}}, { 6,{75,47,14}, 34,{76,48,14}}, { 46,{54,24,15}, 10,{55,25,15}}, { 2, {45,15,15}, 64,{46,16,15}}},
580
+ {{ 17,{152,122,15}, 4,{153,123,15}}, { 29,{74,46,14}, 14,{75,47,14}}, { 49,{54,24,15}, 10,{55,25,15}}, { 24, {45,15,15}, 46,{46,16,15}}},
581
+ {{ 4,{152,122,15}, 18,{153,123,15}}, { 13,{74,46,14}, 32,{75,47,14}}, { 48,{54,24,15}, 14,{55,25,15}}, { 42, {45,15,15}, 32,{46,16,15}}},
582
+ {{ 20,{147,117,15}, 4,{148,118,15}}, { 40,{75,47,14}, 7,{76,48,14}}, { 43,{54,24,15}, 22,{55,25,15}}, { 10, {45,15,15}, 67,{46,16,15}}},
583
+ {{ 19,{148,118,15}, 6,{149,119,15}}, { 18,{75,47,14}, 31,{76,48,14}}, { 34,{54,24,15}, 34,{55,25,15}}, { 20, {45,15,15}, 61,{46,16,15}}}
584
+ }
585
+
586
+ -- The bits that must be 0 if the version does fill the complete matrix.
587
+ -- Example: for version 1, no bits need to be added after arranging the data, for version 2 we need to add 7 bits at the end.
588
+ local remainder = {0, 7, 7, 7, 7, 7, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 0}
589
+
590
+ -- This is the formula for table 1 in the spec:
591
+ -- function get_capacity_remainder( version )
592
+ -- local len = version * 4 + 17
593
+ -- local size = len^2
594
+ -- local function_pattern_modules = 192 + 2 * len - 32 -- Position Adjustment pattern + timing pattern
595
+ -- local count_alignment_pattern = #alignment_pattern[version]
596
+ -- if count_alignment_pattern > 0 then
597
+ -- -- add 25 for each alignment pattern
598
+ -- function_pattern_modules = function_pattern_modules + 25 * ( count_alignment_pattern^2 - 3 )
599
+ -- -- but subtract the timing pattern occupied by the alignment pattern on the top and left
600
+ -- function_pattern_modules = function_pattern_modules - ( count_alignment_pattern - 2) * 10
601
+ -- end
602
+ -- size = size - function_pattern_modules
603
+ -- if version > 6 then
604
+ -- size = size - 67
605
+ -- else
606
+ -- size = size - 31
607
+ -- end
608
+ -- return math.floor(size/8),math.fmod(size,8)
609
+ -- end
610
+
611
+ --- Example: Version 5-H has four data and four error correction blocks. The table above lists
612
+ --- `2, {33,11,11}, 2,{34,12,11}` for entry [5][4]. This means we take two blocks with 11 codewords
613
+ --- and two blocks with 12 codewords, and two blocks with 33 - 11 = 22 ec codes and another
614
+ --- two blocks with 34 - 12 = 22 ec codes.
615
+ --- Block 1: D1 D2 D3 ... D11
616
+ --- Block 2: D12 D13 D14 ... D22
617
+ --- Block 3: D23 D24 D25 ... D33 D34
618
+ --- Block 4: D35 D36 D37 ... D45 D46
619
+ --- Then we place the data like this in the matrix: D1, D12, D23, D35, D2, D13, D24, D36 ... D45, D34, D46. The same goes
620
+ --- with error correction codes.
621
+
622
+ -- The given data can be a string of 0's and 1' (with #string mod 8 == 0).
623
+ -- Alternatively the data can be a table of codewords. The number of codewords
624
+ -- must match the capacity of the qr code.
625
+ local function arrange_codewords_and_calculate_ec(version,ec_level,data)
626
+ if type(data)=="table" then
627
+ local tmp = {}
628
+ for i=1,#data do
629
+ tmp[i] = binary(data[i],8)
630
+ end
631
+ data = concat(tmp)
632
+ end
633
+ -- If the size of the data is not enough for the codeword, we add 0's and two special bytes until finished.
634
+ local blocks = ecblocks[version][ec_level]
635
+ local size_datablock_bytes, size_ecblock_bytes
636
+ local datablocks = {}
637
+ local final_ecblocks = {}
638
+ local pos = 0
639
+ for i=1,#blocks/2 do
640
+ size_datablock_bytes = blocks[2*i][2]
641
+ size_ecblock_bytes = blocks[2*i][1] - size_datablock_bytes
642
+ for _=1,blocks[2*i - 1] do
643
+ datablocks[#datablocks + 1] = sub(data, pos * 8 + 1,( pos + size_datablock_bytes)*8)
644
+ local tmp_tab = calculate_error_correction(datablocks[#datablocks],size_ecblock_bytes)
645
+ local tmp_str = {}
646
+ for x=1,#tmp_tab do
647
+ tmp_str[#tmp_str + 1] = binary(tmp_tab[x],8)
648
+ end
649
+ final_ecblocks[#final_ecblocks + 1] = concat(tmp_str)
650
+ pos = pos + size_datablock_bytes
651
+ end
652
+ end
653
+
654
+ -- Weave the data blocks. When there are multiple block sizes, the final data stream looks like:
655
+ -- b1's 1st byte, b2's 1st byte, (b3's 1st byte, ...)
656
+ -- b1's 2nd byte, b2's 2nd byte, (b3's 2nd byte, ...)
657
+ -- b1's 3rd byte, ...
658
+ local arranged_data = {}
659
+ local maxBlockLen = 0
660
+ for i = 1, #datablocks do maxBlockLen = max(maxBlockLen, #datablocks[i]) end
661
+ for p = 1, maxBlockLen, 8 do
662
+ for i = 1, #datablocks do
663
+ arranged_data[#arranged_data + 1] = sub(datablocks[i], p, p + 7)
664
+ end
665
+ end
666
+
667
+ -- Same for EC blocks
668
+ maxBlockLen = 0
669
+ for i = 1, #final_ecblocks do maxBlockLen = max(maxBlockLen, #final_ecblocks[i]) end
670
+ for p = 1, maxBlockLen, 8 do
671
+ for i = 1, #final_ecblocks do
672
+ arranged_data[#arranged_data + 1] = sub(final_ecblocks[i], p, p + 7)
673
+ end
674
+ end
675
+ return concat(arranged_data)
676
+ end
677
+
678
+
679
+
680
+ --- Step 4: Generate 8 matrices with different masks and calculate the penalty
681
+ --- ==========================================================================
682
+ ---
683
+ --- Prepare matrix
684
+ --- --------------
685
+ --- The first step is to prepare an _empty_ matrix for a given size/mask. The matrix has a
686
+ --- few predefined areas that must be black or blank. We encode the matrix with a two
687
+ --- dimensional field where the numbers determine which pixel is blank or not.
688
+ ---
689
+ --- The following code is used for our matrix:
690
+ --- 0 = not in use yet,
691
+ --- -2 = blank by mandatory pattern,
692
+ --- 2 = black by mandatory pattern,
693
+ --- -1 = blank by data,
694
+ --- 1 = black by data
695
+ ---
696
+ --- To prepare the _empty_, we add positioning, alingment and timing patters.
697
+
698
+ --- ### Positioning patterns ###
699
+ local function add_position_detection_patterns(tab_x)
700
+ local size = #tab_x
701
+ -- allocate quite zone in the matrix area
702
+ for i=1,8 do
703
+ for j=1,8 do
704
+ tab_x[i][j] = -2
705
+ tab_x[size - 8 + i][j] = -2
706
+ tab_x[i][size - 8 + j] = -2
707
+ end
708
+ end
709
+ -- draw the detection pattern (outer)
710
+ for i=1,7 do
711
+ -- top left
712
+ tab_x[1][i]=2
713
+ tab_x[7][i]=2
714
+ tab_x[i][1]=2
715
+ tab_x[i][7]=2
716
+
717
+ -- top right
718
+ tab_x[size][i]=2
719
+ tab_x[size - 6][i]=2
720
+ tab_x[size - i + 1][1]=2
721
+ tab_x[size - i + 1][7]=2
722
+
723
+ -- bottom left
724
+ tab_x[1][size - i + 1]=2
725
+ tab_x[7][size - i + 1]=2
726
+ tab_x[i][size - 6]=2
727
+ tab_x[i][size]=2
728
+ end
729
+ -- draw the detection pattern (inner)
730
+ for i=1,3 do
731
+ for j=1,3 do
732
+ -- top left
733
+ tab_x[2+j][i+2]=2
734
+ -- top right
735
+ tab_x[size - j - 1][i+2]=2
736
+ -- bottom left
737
+ tab_x[2 + j][size - i - 1]=2
738
+ end
739
+ end
740
+ end
741
+
742
+ --- ### Timing patterns ###
743
+ -- The timing patterns (two) are the dashed lines between two adjacent positioning patterns on row/column 7.
744
+ local function add_timing_pattern(tab_x)
745
+ local line,col=7,9
746
+ for i=col,#tab_x-8 do
747
+ tab_x[i][line] = i%2==0 and -2 or 2
748
+ tab_x[line][i] = i%2==0 and -2 or 2
749
+ end
750
+ end
751
+
752
+ --- ### Alignment patterns ###
753
+ --- The alignment patterns must be added to the matrix for versions > 1. The amount and positions depend on the versions and are
754
+ --- given by the spec. Beware: the patterns must not be placed where we have the positioning patterns
755
+ --- (that is: top left, top right and bottom left.)
756
+
757
+ -- For each version, where should we place the alignment patterns? See table E.1 of the spec
758
+ local alignment_pattern = {
759
+ {},{6,18},{6,22},{6,26},{6,30},{6,34}, -- 1-6
760
+ {6,22,38},{6,24,42},{6,26,46},{6,28,50},{6,30,54},{6,32,58},{6,34,62}, -- 7-13
761
+ {6,26,46,66},{6,26,48,70},{6,26,50,74},{6,30,54,78},{6,30,56,82},{6,30,58,86},{6,34,62,90}, -- 14-20
762
+ {6,28,50,72,94},{6,26,50,74,98},{6,30,54,78,102},{6,28,54,80,106},{6,32,58,84,110},{6,30,58,86,114},{6,34,62,90,118}, -- 21-27
763
+ {6,26,50,74,98 ,122},{6,30,54,78,102,126},{6,26,52,78,104,130},{6,30,56,82,108,134},{6,34,60,86,112,138},{6,30,58,86,114,142},{6,34,62,90,118,146}, -- 28-34
764
+ {6,30,54,78,102,126,150}, {6,24,50,76,102,128,154},{6,28,54,80,106,132,158},{6,32,58,84,110,136,162},{6,26,54,82,110,138,166},{6,30,58,86,114,142,170} -- 35 - 40
765
+ }
766
+
767
+ --- The alignment pattern has size 5x5 and looks like this:
768
+ --- XXXXX
769
+ --- X X
770
+ --- X X X
771
+ --- X X
772
+ --- XXXXX
773
+ local function add_alignment_pattern(tab_x)
774
+ local version = (#tab_x - 17) / 4
775
+ local ap = alignment_pattern[version]
776
+ local pos_x, pos_y
777
+ for x=1,#ap do
778
+ for y=1,#ap do
779
+ -- we must not put an alignment pattern on top of the positioning pattern
780
+ if not (x == 1 and y == 1 or x == #ap and y == 1 or x == 1 and y == #ap ) then
781
+ pos_x,pos_y=ap[x]+1,ap[y]+1
782
+ for dy=-2,2 do
783
+ for dx=-2,2 do
784
+ -- form the pattern with checking chebyshev distance instead of hardcoding
785
+ tab_x[pos_x+dx][pos_y+dy]=max(abs(dx),abs(dy))%2==0 and 2 or -2
786
+ end
787
+ end
788
+ end
789
+ end
790
+ end
791
+ end
792
+
793
+ --- ### Type information ###
794
+ --- Let's not forget the type information that is in column 9 next to the left positioning patterns and on row 9 below
795
+ --- the top positioning patterns. This type information is not fixed, it depends on the mask and the error correction.
796
+
797
+ -- The first index is ec level (LMQH,1-4), the second is the mask (0-7). This bitstring of length 15 is to be used
798
+ -- as mandatory pattern in the qrcode. Mask -1 is for debugging purpose only and is the 'noop' mask.
799
+ local typeinfo = {
800
+ { [-1]= "111111111111111", [0] = "111011111000100", "111001011110011", "111110110101010", "111100010011101", "110011000101111", "110001100011000", "110110001000001", "110100101110110" },
801
+ { [-1]= "111111111111111", [0] = "101010000010010", "101000100100101", "101111001111100", "101101101001011", "100010111111001", "100000011001110", "100111110010111", "100101010100000" },
802
+ { [-1]= "111111111111111", [0] = "011010101011111", "011000001101000", "011111100110001", "011101000000110", "010010010110100", "010000110000011", "010111011011010", "010101111101101" },
803
+ { [-1]= "111111111111111", [0] = "001011010001001", "001001110111110", "001110011100111", "001100111010000", "000011101100010", "000001001010101", "000110100001100", "000100000111011" }
804
+ }
805
+
806
+ -- The typeinfo is a mixture of mask and ec level information and is
807
+ -- added twice to the qr code, one horizontal, one vertical.
808
+ local function add_typeinfo_to_matrix(matrix,ec_level,mask)
809
+ local ec_mask_type = typeinfo[ec_level][mask]
810
+
811
+ local bit
812
+ -- vertical from bottom to top
813
+ for i=1,7 do
814
+ bit = sub(ec_mask_type,i,i)
815
+ fill_matrix_position(matrix,bit,9,#matrix - i + 1)
816
+ end
817
+ for i=8,9 do
818
+ bit = sub(ec_mask_type,i,i)
819
+ fill_matrix_position(matrix,bit,9,17-i)
820
+ end
821
+ for i=10,15 do
822
+ bit = sub(ec_mask_type,i,i)
823
+ fill_matrix_position(matrix,bit,9,16 - i)
824
+ end
825
+ -- horizontal, left to right
826
+ for i=1,6 do
827
+ bit = sub(ec_mask_type,i,i)
828
+ fill_matrix_position(matrix,bit,i,9)
829
+ end
830
+ bit = sub(ec_mask_type,7,7)
831
+ fill_matrix_position(matrix,bit,8,9)
832
+ for i=8,15 do
833
+ bit = sub(ec_mask_type,i,i)
834
+ fill_matrix_position(matrix,bit,#matrix - 15 + i,9)
835
+ end
836
+ end
837
+
838
+ -- Bits for version information 7-40
839
+ -- The reversed strings from https://www.thonky.com/qr-code-tutorial/format-version-tables
840
+ local version_information = {
841
+ "001010010011111000", "001111011010000100", "100110010101100100", "110010110010010100",
842
+ "011011111101110100", "010001101110001100", "111000100001101100", "101100000110011100", "000101001001111100",
843
+ "000111101101000010", "101110100010100010", "111010000101010010", "010011001010110010", "011001011001001010",
844
+ "110000010110101010", "100100110001011010", "001101111110111010", "001000110111000110", "100001111000100110",
845
+ "110101011111010110", "011100010000110110", "010110000011001110", "111111001100101110", "101011101011011110",
846
+ "000010100100111110", "101010111001000001", "000011110110100001", "010111010001010001", "111110011110110001",
847
+ "110100001101001001", "011101000010101001", "001001100101011001", "100000101010111001", "100101100011000101",
848
+ }
849
+
850
+ -- Versions 7 and above need two bitfields with version information added to the code
851
+ local function add_version_information(matrix,version)
852
+ if version < 7 then return end
853
+ local size = #matrix
854
+ local bitstring = version_information[version - 6]
855
+ local x,y, bit
856
+ local start_x, start_y
857
+ -- first top right
858
+ start_x = size - 10
859
+ start_y = 1
860
+ for i=1,#bitstring do
861
+ bit = sub(bitstring,i,i)
862
+ x = start_x + (i - 1) % 3
863
+ y = start_y + floor((i - 1) / 3)
864
+ fill_matrix_position(matrix,bit,x,y)
865
+ end
866
+
867
+ -- now bottom left
868
+ start_x = 1
869
+ start_y = size - 10
870
+ for i=1,#bitstring do
871
+ bit = sub(bitstring,i,i)
872
+ x = start_x + floor((i - 1) / 3)
873
+ y = start_y + (i - 1) % 3
874
+ fill_matrix_position(matrix,bit,x,y)
875
+ end
876
+ end
877
+
878
+ --- Now it's time to use the methods above to create a prefilled matrix for the given mask
879
+ local function prepare_matrix_with_mask(version,ec_level,mask)
880
+ local size = version * 4 + 17
881
+ local tab_x = {}
882
+
883
+ for i=1,size do
884
+ tab_x[i]={}
885
+ for j=1,size do
886
+ tab_x[i][j] = 0
887
+ end
888
+ end
889
+ add_position_detection_patterns(tab_x)
890
+ add_timing_pattern(tab_x)
891
+ add_version_information(tab_x,version)
892
+
893
+ -- black pixel above lower left position detection pattern
894
+ tab_x[9][size - 7] = 2
895
+ add_alignment_pattern(tab_x)
896
+ add_typeinfo_to_matrix(tab_x,ec_level, mask)
897
+ return tab_x
898
+ end
899
+
900
+ --- Finally we come to the place where we need to put the calculated data (remember step 3?) into the qr code.
901
+ --- We do this for each mask. BTW speaking of mask, this is what we find in the spec:
902
+ --- Mask Pattern Reference Condition
903
+ --- 000 (y + x) mod 2 = 0
904
+ --- 001 y mod 2 = 0
905
+ --- 010 x mod 3 = 0
906
+ --- 011 (y + x) mod 3 = 0
907
+ --- 100 ((y div 2) + (x div 3)) mod 2 = 0
908
+ --- 101 (y x) mod 2 + (y x) mod 3 = 0
909
+ --- 110 ((y x) mod 2 + (y x) mod 3) mod 2 = 0
910
+ --- 111 ((y x) mod 3 + (y+x) mod 2) mod 2 = 0
911
+
912
+ -- Mask functions, i & j are 0-based, so input should be (x-1,y-1)
913
+ -- true means 'invert this bit'
914
+ local maskFunc={
915
+ [-1]=function(_,_) return false end, -- test purpose only, no mask applied
916
+ [0]=function(x,y) return (y+x)%2==0 end,
917
+ function(_,y) return y%2==0 end,
918
+ function(x,_) return x%3==0 end,
919
+ function(x,y) return (y+x)%3==0 end,
920
+ function(x,y) return (y%4-1.5)*(x%6-2.5)>0 end, -- optimized for not using math.floor (too slow) or // operation (new Lua only)
921
+ function(x,y) return (y*x)%2+(y*x)%3==0 end,
922
+ function(x,y) return ((y*x)%3+y*x)%2==0 end,
923
+ function(x,y) return ((y*x)%3+y+x)%2==0 end,
924
+ }
925
+
926
+ -- Receive 0 (blank) or 1 (black) from data,
927
+ -- Return -1 (blank) or 1 (black) depending on the value, mask, and position.
928
+ -- Parameter mask is 0-7 (-1 for 'no mask'). x and y are 1-based coordinates,
929
+ -- 1,1 = upper left. value must be 0 or 1.
930
+ local function get_pixel_with_mask(mask,x,y,dataBit)
931
+ local invert = maskFunc[mask](x-1,y-1)
932
+ return (dataBit==0)==invert and 1 or -1
933
+ -- This^ == is used as boolean XNOR:
934
+ -- data F T <- invert?
935
+ -- 0 F -1 1
936
+ -- 1 T 1 -1
937
+ end
938
+
939
+ -- Add the data string (0's and 1's) to the matrix for the given mask.
940
+ local function add_data_to_matrix(matrix,data,mask)
941
+ local size = #matrix
942
+ -- Fill data into matrix
943
+ local ptr=1 -- data pointer
944
+ local x,y=size,size -- writing position, starts from bottom right
945
+ local x_dir,y_dir=-1,-1 -- state of movement, notice that Y step once each two X steps
946
+ while true do
947
+ -- 0 means available data cell to write data
948
+ if matrix[x][y]==0 then
949
+ matrix[x][y] = get_pixel_with_mask(mask,x,y,byte(data,ptr)-48) -- '0' = 48, '1' = 49
950
+ ptr = ptr + 1
951
+ if ptr > #data or x < 0 then return matrix end -- all data written, finish
952
+ end
953
+
954
+ -- Move to next cell (won't write into unavailable cell so it's fine to move 1 step each time)
955
+ -- switch left/right
956
+ x = x + x_dir
957
+ -- if just stepped right, it means current 2 bits were finished
958
+ if x_dir == 1 then
959
+ -- so we step up/down for next 2 bits
960
+ y = y + y_dir
961
+
962
+ -- when we went outside the matrix, move 2 cells left and turn back
963
+ if not matrix[y] then -- square, so matrix[y] will be nil if y is out of range, no matter [x][y] or [y][x]
964
+ x = x - 2
965
+ if x == 7 then x = 6 end -- jump over timing pattern
966
+ y = y_dir == -1 and 1 or size
967
+ y_dir = -y_dir
968
+ end
969
+ end
970
+ -- prepare next left/right
971
+ x_dir = -x_dir
972
+ end
973
+ end
974
+
975
+ --- The total penalty of the matrix is the sum of four steps. The following steps are taken into account:
976
+ ---
977
+ --- 1. Adjacent modules in row/column in same color
978
+ --- 1. Block of modules in same color
979
+ --- 1. 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column
980
+ --- 1. Proportion of dark modules in entire symbol
981
+ ---
982
+ --- This all is done to avoid bad patterns in the code that prevent the scanner from
983
+ --- reading the code.
984
+ -- Return the penalty for the given matrix
985
+ local function calculate_penalty(matrix)
986
+ local penalty1, penalty2, penalty3 = 0,0,0
987
+ local size = #matrix
988
+ -- this is for penalty 4
989
+ local number_of_dark_cells = 0
990
+
991
+ -- 1: Adjacent modules in row/column in same color
992
+ -- --------------------------------------------
993
+ -- No. of modules = (5+i) -> 3 + i
994
+ local last_bit_blank -- < 0: blank, > 0: black
995
+ local is_blank
996
+ local number_of_consecutive_bits
997
+ -- first: vertical
998
+ for x=1,size do
999
+ number_of_consecutive_bits = 0
1000
+ last_bit_blank = nil
1001
+ for y = 1,size do
1002
+ if matrix[x][y] > 0 then
1003
+ -- small optimization: this is for penalty 4
1004
+ number_of_dark_cells = number_of_dark_cells + 1
1005
+ is_blank = false
1006
+ else
1007
+ is_blank = true
1008
+ end
1009
+ if last_bit_blank == is_blank then
1010
+ number_of_consecutive_bits = number_of_consecutive_bits + 1
1011
+ else
1012
+ if number_of_consecutive_bits >= 5 then
1013
+ penalty1 = penalty1 + number_of_consecutive_bits - 2
1014
+ end
1015
+ number_of_consecutive_bits = 1
1016
+ end
1017
+ last_bit_blank = is_blank
1018
+ end
1019
+ if number_of_consecutive_bits >= 5 then
1020
+ penalty1 = penalty1 + number_of_consecutive_bits - 2
1021
+ end
1022
+ end
1023
+ -- now horizontal
1024
+ for y=1,size do
1025
+ number_of_consecutive_bits = 0
1026
+ last_bit_blank = nil
1027
+ for x = 1,size do
1028
+ is_blank = matrix[x][y] < 0
1029
+ if last_bit_blank == is_blank then
1030
+ number_of_consecutive_bits = number_of_consecutive_bits + 1
1031
+ else
1032
+ if number_of_consecutive_bits >= 5 then
1033
+ penalty1 = penalty1 + number_of_consecutive_bits - 2
1034
+ end
1035
+ number_of_consecutive_bits = 1
1036
+ end
1037
+ last_bit_blank = is_blank
1038
+ end
1039
+ if number_of_consecutive_bits >= 5 then
1040
+ penalty1 = penalty1 + number_of_consecutive_bits - 2
1041
+ end
1042
+ end
1043
+ for x=1,size do
1044
+ for y=1,size do
1045
+ -- 2: Block of modules in same color
1046
+ -- -----------------------------------
1047
+ -- Blocksize = m x n -> 3 x (m-1) x (n-1)
1048
+ if (y < size - 1) and (x < size - 1) and (
1049
+ (matrix[x][y] < 0 and matrix[x+1][y] < 0 and matrix[x][y+1] < 0 and matrix[x+1][y+1] < 0) or
1050
+ (matrix[x][y] > 0 and matrix[x+1][y] > 0 and matrix[x][y+1] > 0 and matrix[x+1][y+1] > 0)
1051
+ ) then penalty2 = penalty2 + 3 end
1052
+
1053
+ -- 3: 1:1:3:1:1 ratio (dark:light:dark:light:dark) pattern in row/column
1054
+ -- ------------------------------------------------------------------
1055
+ -- Gives 40 points each
1056
+ --
1057
+ -- I have no idea why we need the extra 0000 on left or right side. The spec doesn't mention it,
1058
+ -- other sources do mention it. This is heavily inspired by zxing.
1059
+ if (y + 6 < size and
1060
+ matrix[x][y] > 0 and
1061
+ matrix[x][y + 1] < 0 and
1062
+ matrix[x][y + 2] > 0 and
1063
+ matrix[x][y + 3] > 0 and
1064
+ matrix[x][y + 4] > 0 and
1065
+ matrix[x][y + 5] < 0 and
1066
+ matrix[x][y + 6] > 0 and
1067
+ ((y + 10 < size and
1068
+ matrix[x][y + 7] < 0 and
1069
+ matrix[x][y + 8] < 0 and
1070
+ matrix[x][y + 9] < 0 and
1071
+ matrix[x][y + 10] < 0) or
1072
+ (y - 4 >= 1 and
1073
+ matrix[x][y - 1] < 0 and
1074
+ matrix[x][y - 2] < 0 and
1075
+ matrix[x][y - 3] < 0 and
1076
+ matrix[x][y - 4] < 0))) then penalty3 = penalty3 + 40 end
1077
+ if (x + 6 <= size and
1078
+ matrix[x][y] > 0 and
1079
+ matrix[x + 1][y] < 0 and
1080
+ matrix[x + 2][y] > 0 and
1081
+ matrix[x + 3][y] > 0 and
1082
+ matrix[x + 4][y] > 0 and
1083
+ matrix[x + 5][y] < 0 and
1084
+ matrix[x + 6][y] > 0 and
1085
+ ((x + 10 <= size and
1086
+ matrix[x + 7][y] < 0 and
1087
+ matrix[x + 8][y] < 0 and
1088
+ matrix[x + 9][y] < 0 and
1089
+ matrix[x + 10][y] < 0) or
1090
+ (x - 4 >= 1 and
1091
+ matrix[x - 1][y] < 0 and
1092
+ matrix[x - 2][y] < 0 and
1093
+ matrix[x - 3][y] < 0 and
1094
+ matrix[x - 4][y] < 0))) then penalty3 = penalty3 + 40 end
1095
+ end
1096
+ end
1097
+ -- 4: Proportion of dark modules in entire symbol
1098
+ -- ----------------------------------------------
1099
+ -- 50 +/- (5 x k)% to 50 +/- (5 x (k + 1))% -> 10 x k
1100
+ local dark_ratio = number_of_dark_cells / (size * size)
1101
+ local penalty4 = floor(abs(dark_ratio * 100 - 50)) * 2
1102
+ return penalty1 + penalty2 + penalty3 + penalty4
1103
+ end
1104
+
1105
+ -- Create a matrix for the given parameters and calculate the penalty score.
1106
+ -- Return both (matrix and penalty)
1107
+ local function get_matrix_and_penalty(version,ec_level,data,mask)
1108
+ local tab = prepare_matrix_with_mask(version,ec_level,mask)
1109
+ add_data_to_matrix(tab,data,mask)
1110
+ local penalty = calculate_penalty(tab)
1111
+ return tab, penalty
1112
+ end
1113
+
1114
+ -- Return the matrix with the smallest penalty. To to this
1115
+ -- we try out the matrix for all 8 masks and determine the
1116
+ -- penalty (score) each.
1117
+ local function get_matrix_with_lowest_penalty(version,ec_level,data)
1118
+ local tab, penalty
1119
+ local tab_min_penalty, min_penalty
1120
+
1121
+ -- try masks 0-7
1122
+ tab_min_penalty, min_penalty = get_matrix_and_penalty(version,ec_level,data,0)
1123
+ for i=1,7 do
1124
+ tab, penalty = get_matrix_and_penalty(version,ec_level,data,i)
1125
+ if penalty < min_penalty then
1126
+ tab_min_penalty = tab
1127
+ min_penalty = penalty
1128
+ end
1129
+ end
1130
+ return tab_min_penalty
1131
+ end
1132
+
1133
+ --- The main function. We connect everything together. Remember from above:
1134
+ ---
1135
+ --- 1. Determine version, ec level and mode (=encoding) for codeword
1136
+ --- 1. Encode data
1137
+ --- 1. Arrange data and calculate error correction code
1138
+ --- 1. Generate 8 matrices with different masks and calculate the penalty
1139
+ --- 1. Return qrcode with least penalty
1140
+
1141
+ -- Return
1142
+ -- on success: true, number matrix (only has +/-1&+/-2. positive means black, +/-2 means mandatory, in case if you didn't read comments above)
1143
+ -- on failed: false, error message string
1144
+ -- If ec_level or mode is given, use the ones for generating the qrcode. (mode option is not implemented yet, but it will be determined automatically)
1145
+ local function qrcode(str,ec_level,mode_enc)
1146
+ local arranged_data, version, data_raw, mode, len_bitstring
1147
+ version, ec_level, data_raw, mode, len_bitstring = get_version_eclevel_mode_bistringlength(str,ec_level,mode_enc)
1148
+ data_raw = data_raw .. len_bitstring
1149
+ data_raw = data_raw .. encode_data(str,mode)
1150
+ data_raw = add_pad_data(version,ec_level,data_raw)
1151
+ arranged_data = arrange_codewords_and_calculate_ec(version,ec_level,data_raw)
1152
+ if #arranged_data % 8 ~= 0 then
1153
+ return false, format("Arranged data %% 8 != 0: data length = %d, mod 8 = %d",#arranged_data, #arranged_data % 8)
1154
+ end
1155
+ arranged_data = arranged_data .. rep("0",remainder[version])
1156
+ local tab = get_matrix_with_lowest_penalty(version,ec_level,arranged_data)
1157
+ return true, tab
1158
+ end
1159
+ return qrcode
1160
+ end
1161
+
1162
+ ns.AutomationQR = ns.AutomationQR or {}
1163
+
1164
+ function ns.AutomationQR.Encode(text, ecLevel)
1165
+ if type(text) ~= "string" or text == "" then
1166
+ return nil, "empty payload"
1167
+ end
1168
+ if not encode then
1169
+ encode = BuildEncoder()
1170
+ end
1171
+ local ok, matrix = encode(text, tonumber(ecLevel) or 2)
1172
+ if not ok then
1173
+ return nil, tostring(matrix)
1174
+ end
1175
+ return matrix
1176
+ end