Subversion Repositories Applications.annuaire

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
296 aurelien 1
<?php
2
//============================================================+
3
// File name   : qrcode.php
4
// Version     : 1.0.008
5
// Begin       : 2010-03-22
6
// Last Update : 2010-11-15
7
// Author      : Nicola Asuni - Tecnick.com S.r.l - Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
8
// License     : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
9
// -------------------------------------------------------------------
10
// Copyright (C) 2010-2010  Nicola Asuni - Tecnick.com S.r.l.
11
//
12
// This file is part of TCPDF software library.
13
//
14
// TCPDF is free software: you can redistribute it and/or modify it
15
// under the terms of the GNU Lesser General Public License as
16
// published by the Free Software Foundation, either version 3 of the
17
// License, or (at your option) any later version.
18
//
19
// TCPDF is distributed in the hope that it will be useful, but
20
// WITHOUT ANY WARRANTY; without even the implied warranty of
21
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22
// See the GNU Lesser General Public License for more details.
23
//
24
// You should have received a copy of the GNU Lesser General Public License
25
// along with TCPDF.  If not, see <http://www.gnu.org/licenses/>.
26
//
27
// See LICENSE.TXT file for more information.
28
// -------------------------------------------------------------------
29
//
30
// DESCRIPTION :
31
//
32
// Class to create QR-code arrays for TCPDF class.
33
// QR Code symbol is a 2D barcode that can be scanned by
34
// handy terminals such as a mobile phone with CCD.
35
// The capacity of QR Code is up to 7000 digits or 4000
36
// characters, and has high robustness.
37
// This class supports QR Code model 2, described in
38
// JIS (Japanese Industrial Standards) X0510:2004
39
// or ISO/IEC 18004.
40
// Currently the following features are not supported:
41
// ECI and FNC1 mode, Micro QR Code, QR Code model 1,
42
// Structured mode.
43
//
44
// This class is derived from the following projects:
45
// ---------------------------------------------------------
46
// "PHP QR Code encoder"
47
// License: GNU-LGPLv3
48
// Copyright (C) 2010 by Dominik Dzienia <deltalab at poczta dot fm>
49
// http://phpqrcode.sourceforge.net/
50
// https://sourceforge.net/projects/phpqrcode/
51
//
52
// The "PHP QR Code encoder" is based on
53
// "C libqrencode library" (ver. 3.1.1)
54
// License: GNU-LGPL 2.1
55
// Copyright (C) 2006-2010 by Kentaro Fukuchi
56
// http://megaui.net/fukuchi/works/qrencode/index.en.html
57
//
58
// Reed-Solomon code encoder is written by Phil Karn, KA9Q.
59
// Copyright (C) 2002-2006 Phil Karn, KA9Q
60
//
61
// QR Code is registered trademark of DENSO WAVE INCORPORATED
62
// http://www.denso-wave.com/qrcode/index-e.html
63
// ---------------------------------------------------------
64
//============================================================+
65
 
66
/**
67
 * Class to create QR-code arrays for TCPDF class.
68
 * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
69
 * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
70
 * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
71
 * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
72
 *
73
 * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
74
 * Please read comments on this class source file for full copyright and license information.
75
 *
76
 * @package com.tecnick.tcpdf
77
 * @abstract Class for generating QR-code array for TCPDF.
78
 * @author Nicola Asuni
79
 * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
80
 * @link http://www.tcpdf.org
81
 * @license http://www.gnu.org/copyleft/lesser.html LGPL
82
 * @version 1.0.008
83
 */
84
 
85
// definitions
86
if (!defined('QRCODEDEFS')) {
87
 
88
	/**
89
	 * Indicate that definitions for this class are set
90
	 */
91
	define('QRCODEDEFS', true);
92
 
93
	// -----------------------------------------------------
94
 
95
	// Encoding modes (characters which can be encoded in QRcode)
96
 
97
	/**
98
	 * Encoding mode
99
	 */
100
	define('QR_MODE_NL', -1);
101
 
102
	/**
103
	 * Encoding mode numeric (0-9). 3 characters are encoded to 10bit length. In theory, 7089 characters or less can be stored in a QRcode.
104
	 */
105
	define('QR_MODE_NM', 0);
106
 
107
	/**
108
	 * Encoding mode alphanumeric (0-9A-Z $%*+-./:) 45characters. 2 characters are encoded to 11bit length. In theory, 4296 characters or less can be stored in a QRcode.
109
	 */
110
	define('QR_MODE_AN', 1);
111
 
112
	/**
113
	 * Encoding mode 8bit byte data. In theory, 2953 characters or less can be stored in a QRcode.
114
	 */
115
	define('QR_MODE_8B', 2);
116
 
117
	/**
118
	 * Encoding mode KANJI. A KANJI character (multibyte character) is encoded to 13bit length. In theory, 1817 characters or less can be stored in a QRcode.
119
	 */
120
	define('QR_MODE_KJ', 3);
121
 
122
	/**
123
	 * Encoding mode STRUCTURED (currently unsupported)
124
	 */
125
	define('QR_MODE_ST', 4);
126
 
127
	// -----------------------------------------------------
128
 
129
	// Levels of error correction.
130
	// QRcode has a function of an error correcting for miss reading that white is black.
131
	// Error correcting is defined in 4 level as below.
132
 
133
	/**
134
	 * Error correction level L : About 7% or less errors can be corrected.
135
	 */
136
	define('QR_ECLEVEL_L', 0);
137
 
138
	/**
139
	 * Error correction level M : About 15% or less errors can be corrected.
140
	 */
141
	define('QR_ECLEVEL_M', 1);
142
 
143
	/**
144
	 * Error correction level Q : About 25% or less errors can be corrected.
145
	 */
146
	define('QR_ECLEVEL_Q', 2);
147
 
148
	/**
149
	 * Error correction level H : About 30% or less errors can be corrected.
150
	 */
151
	define('QR_ECLEVEL_H', 3);
152
 
153
	// -----------------------------------------------------
154
 
155
	// Version. Size of QRcode is defined as version.
156
	// Version is from 1 to 40.
157
	// Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases.
158
	// So version 40 is 177*177 matrix.
159
 
160
	/**
161
	 * Maximum QR Code version.
162
	 */
163
	define('QRSPEC_VERSION_MAX', 40);
164
 
165
	/**
166
	 * Maximum matrix size for maximum version (version 40 is 177*177 matrix).
167
	 */
168
    define('QRSPEC_WIDTH_MAX', 177);
169
 
170
	// -----------------------------------------------------
171
 
172
	/**
173
	 * Matrix index to get width from $capacity array.
174
	 */
175
    define('QRCAP_WIDTH',    0);
176
 
177
    /**
178
	 * Matrix index to get number of words from $capacity array.
179
	 */
180
    define('QRCAP_WORDS',    1);
181
 
182
    /**
183
	 * Matrix index to get remainder from $capacity array.
184
	 */
185
    define('QRCAP_REMINDER', 2);
186
 
187
    /**
188
	 * Matrix index to get error correction level from $capacity array.
189
	 */
190
    define('QRCAP_EC',       3);
191
 
192
	// -----------------------------------------------------
193
 
194
	// Structure (currently usupported)
195
 
196
	/**
197
	 * Number of header bits for structured mode
198
	 */
199
    define('STRUCTURE_HEADER_BITS',  20);
200
 
201
    /**
202
	 * Max number of symbols for structured mode
203
	 */
204
    define('MAX_STRUCTURED_SYMBOLS', 16);
205
 
206
	// -----------------------------------------------------
207
 
208
    // Masks
209
 
210
    /**
211
	 * Down point base value for case 1 mask pattern (concatenation of same color in a line or a column)
212
	 */
213
    define('N1',  3);
214
 
215
    /**
216
	 * Down point base value for case 2 mask pattern (module block of same color)
217
	 */
218
	define('N2',  3);
219
 
220
    /**
221
	 * Down point base value for case 3 mask pattern (1:1:3:1:1(dark:bright:dark:bright:dark)pattern in a line or a column)
222
	 */
223
	define('N3', 40);
224
 
225
    /**
226
	 * Down point base value for case 4 mask pattern (ration of dark modules in whole)
227
	 */
228
	define('N4', 10);
229
 
230
	// -----------------------------------------------------
231
 
232
	// Optimization settings
233
 
234
	/**
235
	 * if true, estimates best mask (spec. default, but extremally slow; set to false to significant performance boost but (propably) worst quality code
236
	 */
237
	define('QR_FIND_BEST_MASK', true);
238
 
239
	/**
240
	 * if false, checks all masks available, otherwise value tells count of masks need to be checked, mask id are got randomly
241
	 */
242
	define('QR_FIND_FROM_RANDOM', 2);
243
 
244
	/**
245
	 * when QR_FIND_BEST_MASK === false
246
	 */
247
	define('QR_DEFAULT_MASK', 2);
248
 
249
	// -----------------------------------------------------
250
 
251
} // end of definitions
252
 
253
// #*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#*#
254
 
255
if (!class_exists('QRcode', false)) {
256
 
257
	// for compatibility with PHP4
258
	if (!function_exists('str_split')) {
259
    	/**
260
    	 * Convert a string to an array (needed for PHP4 compatibility)
261
    	 * @param string $string The input string.
262
    	 * @param int $split_length Maximum length of the chunk.
263
    	 * @return  If the optional split_length  parameter is specified, the returned array will be broken down into chunks with each being split_length  in length, otherwise each chunk will be one character in length. FALSE is returned if split_length is less than 1. If the split_length length exceeds the length of string , the entire string is returned as the first (and only) array element.
264
    	 */
265
		function str_split($string, $split_length=1) {
266
			if ((strlen($string) > $split_length) OR (!$split_length)) {
267
				do {
268
					$c = strlen($string);
269
					$parts[] = substr($string, 0, $split_length);
270
					$string = substr($string, $split_length);
271
				} while ($string !== false);
272
			} else {
273
				$parts = array($string);
274
			}
275
			return $parts;
276
		}
277
	}
278
 
279
	// #####################################################
280
 
281
	/**
282
	 * Class to create QR-code arrays for TCPDF class.
283
	 * QR Code symbol is a 2D barcode that can be scanned by handy terminals such as a mobile phone with CCD.
284
	 * The capacity of QR Code is up to 7000 digits or 4000 characters, and has high robustness.
285
	 * This class supports QR Code model 2, described in JIS (Japanese Industrial Standards) X0510:2004 or ISO/IEC 18004.
286
	 * Currently the following features are not supported: ECI and FNC1 mode, Micro QR Code, QR Code model 1, Structured mode.
287
	 *
288
	 * This class is derived from "PHP QR Code encoder" by Dominik Dzienia (http://phpqrcode.sourceforge.net/) based on "libqrencode C library 3.1.1." by Kentaro Fukuchi (http://megaui.net/fukuchi/works/qrencode/index.en.html), contains Reed-Solomon code written by Phil Karn, KA9Q. QR Code is registered trademark of DENSO WAVE INCORPORATED (http://www.denso-wave.com/qrcode/index-e.html).
289
	 * Please read comments on this class source file for full copyright and license information.
290
	 *
291
	 * @name QRcode
292
	 * @package com.tecnick.tcpdf
293
	 * @abstract Class for generating QR-code array for TCPDF.
294
	 * @author Nicola Asuni
295
	 * @copyright 2010-2010 Nicola Asuni - Tecnick.com S.r.l (www.tecnick.com) Via Della Pace, 11 - 09044 - Quartucciu (CA) - ITALY - www.tecnick.com - info@tecnick.com
296
	 * @link http://www.tcpdf.org
297
	 * @license http://www.gnu.org/copyleft/lesser.html LGPL
298
	 * @version 1.0.006
299
	 */
300
	class QRcode {
301
 
302
		/**
303
		 * @var barcode array to be returned which is readable by TCPDF
304
		 * @access protected
305
		 */
306
		protected $barcode_array = array();
307
 
308
		/**
309
		 * @var QR code version. Size of QRcode is defined as version. Version is from 1 to 40. Version 1 is 21*21 matrix. And 4 modules increases whenever 1 version increases. So version 40 is 177*177 matrix.
310
		 * @access protected
311
		 */
312
		protected $version = 0;
313
 
314
		/**
315
		 * @var Levels of error correction. See definitions for possible values.
316
		 * @access protected
317
		 */
318
		protected $level = QR_ECLEVEL_L;
319
 
320
		/**
321
		 * @var Encoding mode
322
		 * @access protected
323
		 */
324
		protected $hint = QR_MODE_8B;
325
 
326
		/**
327
		 * @var if true the input string will be converted to uppercase
328
		 * @access protected
329
		 */
330
		protected $casesensitive = true;
331
 
332
		/**
333
		 * @var structured QR code (not supported yet)
334
		 * @access protected
335
		 */
336
		protected $structured = 0;
337
 
338
		/**
339
		 * @var mask data
340
		 * @access protected
341
		 */
342
		protected $data;
343
 
344
		// FrameFiller
345
 
346
		/**
347
		 * @var width
348
		 * @access protected
349
		 */
350
		protected $width;
351
 
352
		/**
353
		 * @var frame
354
		 * @access protected
355
		 */
356
		protected $frame;
357
 
358
		/**
359
		 * @var X position of bit
360
		 * @access protected
361
		 */
362
		protected $x;
363
 
364
		/**
365
		 * @var Y position of bit
366
		 * @access protected
367
		 */
368
		protected $y;
369
 
370
		/**
371
		 * @var direction
372
		 * @access protected
373
		 */
374
		protected $dir;
375
 
376
		/**
377
		 * @var single bit
378
		 * @access protected
379
		 */
380
		protected $bit;
381
 
382
		// ---- QRrawcode ----
383
 
384
		/**
385
		 * @var data code
386
		 * @access protected
387
		 */
388
		protected $datacode = array();
389
 
390
		/**
391
		 * @var error correction code
392
		 * @access protected
393
		 */
394
		protected $ecccode = array();
395
 
396
		/**
397
		 * @var blocks
398
		 * @access protected
399
		 */
400
		protected $blocks;
401
 
402
		/**
403
		 * @var Reed-Solomon blocks
404
		 * @access protected
405
		 */
406
		protected $rsblocks = array(); //of RSblock
407
 
408
		/**
409
		 * @var counter
410
		 * @access protected
411
		 */
412
		protected $count;
413
 
414
		/**
415
		 * @var data length
416
		 * @access protected
417
		 */
418
		protected $dataLength;
419
 
420
		/**
421
		 * @var error correction length
422
		 * @access protected
423
		 */
424
		protected $eccLength;
425
 
426
		/**
427
		 * @var b1
428
		 * @access protected
429
		 */
430
		protected $b1;
431
 
432
		// ---- QRmask ----
433
 
434
		/**
435
		 * @var run length
436
		 * @access protected
437
		 */
438
		protected $runLength = array();
439
 
440
		// ---- QRsplit ----
441
 
442
		/**
443
		 * @var input data string
444
		 * @access protected
445
		 */
446
		protected $dataStr = '';
447
 
448
		/**
449
		 * @var input items
450
		 * @access protected
451
		 */
452
		protected $items;
453
 
454
		// Reed-Solomon items
455
 
456
		/**
457
		 * @var Reed-Solomon items
458
		 * @access protected
459
		 */
460
		protected $rsitems = array();
461
 
462
		/**
463
		 * @var array of frames
464
		 * @access protected
465
		 */
466
		protected $frames = array();
467
 
468
		/**
469
		 * @var alphabet-numeric convesion table
470
		 * @access protected
471
		 */
472
		protected $anTable = array(
473
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
474
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
475
			36, -1, -1, -1, 37, 38, -1, -1, -1, -1, 39, 40, -1, 41, 42, 43, //
476
			 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 44, -1, -1, -1, -1, -1, //
477
			-1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, //
478
			25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, //
479
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, //
480
			-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1  //
481
			);
482
 
483
		/**
484
		 * @var array Table of the capacity of symbols
485
		 * See Table 1 (pp.13) and Table 12-16 (pp.30-36), JIS X0510:2004.
486
		 * @access protected
487
		 */
488
		protected $capacity = array(
489
			array(  0,    0, 0, array(   0,    0,    0,    0)), //
490
			array( 21,   26, 0, array(   7,   10,   13,   17)), //  1
491
			array( 25,   44, 7, array(  10,   16,   22,   28)), //
492
			array( 29,   70, 7, array(  15,   26,   36,   44)), //
493
			array( 33,  100, 7, array(  20,   36,   52,   64)), //
494
			array( 37,  134, 7, array(  26,   48,   72,   88)), //  5
495
			array( 41,  172, 7, array(  36,   64,   96,  112)), //
496
			array( 45,  196, 0, array(  40,   72,  108,  130)), //
497
			array( 49,  242, 0, array(  48,   88,  132,  156)), //
498
			array( 53,  292, 0, array(  60,  110,  160,  192)), //
499
			array( 57,  346, 0, array(  72,  130,  192,  224)), // 10
500
			array( 61,  404, 0, array(  80,  150,  224,  264)), //
501
			array( 65,  466, 0, array(  96,  176,  260,  308)), //
502
			array( 69,  532, 0, array( 104,  198,  288,  352)), //
503
			array( 73,  581, 3, array( 120,  216,  320,  384)), //
504
			array( 77,  655, 3, array( 132,  240,  360,  432)), // 15
505
			array( 81,  733, 3, array( 144,  280,  408,  480)), //
506
			array( 85,  815, 3, array( 168,  308,  448,  532)), //
507
			array( 89,  901, 3, array( 180,  338,  504,  588)), //
508
			array( 93,  991, 3, array( 196,  364,  546,  650)), //
509
			array( 97, 1085, 3, array( 224,  416,  600,  700)), // 20
510
			array(101, 1156, 4, array( 224,  442,  644,  750)), //
511
			array(105, 1258, 4, array( 252,  476,  690,  816)), //
512
			array(109, 1364, 4, array( 270,  504,  750,  900)), //
513
			array(113, 1474, 4, array( 300,  560,  810,  960)), //
514
			array(117, 1588, 4, array( 312,  588,  870, 1050)), // 25
515
			array(121, 1706, 4, array( 336,  644,  952, 1110)), //
516
			array(125, 1828, 4, array( 360,  700, 1020, 1200)), //
517
			array(129, 1921, 3, array( 390,  728, 1050, 1260)), //
518
			array(133, 2051, 3, array( 420,  784, 1140, 1350)), //
519
			array(137, 2185, 3, array( 450,  812, 1200, 1440)), // 30
520
			array(141, 2323, 3, array( 480,  868, 1290, 1530)), //
521
			array(145, 2465, 3, array( 510,  924, 1350, 1620)), //
522
			array(149, 2611, 3, array( 540,  980, 1440, 1710)), //
523
			array(153, 2761, 3, array( 570, 1036, 1530, 1800)), //
524
			array(157, 2876, 0, array( 570, 1064, 1590, 1890)), // 35
525
			array(161, 3034, 0, array( 600, 1120, 1680, 1980)), //
526
			array(165, 3196, 0, array( 630, 1204, 1770, 2100)), //
527
			array(169, 3362, 0, array( 660, 1260, 1860, 2220)), //
528
			array(173, 3532, 0, array( 720, 1316, 1950, 2310)), //
529
			array(177, 3706, 0, array( 750, 1372, 2040, 2430))  // 40
530
		);
531
 
532
		/**
533
		 * @var array Length indicator
534
		 * @access protected
535
		 */
536
		protected $lengthTableBits = array(
537
			array(10, 12, 14),
538
			array( 9, 11, 13),
539
			array( 8, 16, 16),
540
			array( 8, 10, 12)
541
		);
542
 
543
		/**
544
		 * @var array Table of the error correction code (Reed-Solomon block)
545
		 * See Table 12-16 (pp.30-36), JIS X0510:2004.
546
		 * @access protected
547
		 */
548
		protected $eccTable = array(
549
			array(array( 0,  0), array( 0,  0), array( 0,  0), array( 0,  0)), //
550
			array(array( 1,  0), array( 1,  0), array( 1,  0), array( 1,  0)), //  1
551
			array(array( 1,  0), array( 1,  0), array( 1,  0), array( 1,  0)), //
552
			array(array( 1,  0), array( 1,  0), array( 2,  0), array( 2,  0)), //
553
			array(array( 1,  0), array( 2,  0), array( 2,  0), array( 4,  0)), //
554
			array(array( 1,  0), array( 2,  0), array( 2,  2), array( 2,  2)), //  5
555
			array(array( 2,  0), array( 4,  0), array( 4,  0), array( 4,  0)), //
556
			array(array( 2,  0), array( 4,  0), array( 2,  4), array( 4,  1)), //
557
			array(array( 2,  0), array( 2,  2), array( 4,  2), array( 4,  2)), //
558
			array(array( 2,  0), array( 3,  2), array( 4,  4), array( 4,  4)), //
559
			array(array( 2,  2), array( 4,  1), array( 6,  2), array( 6,  2)), // 10
560
			array(array( 4,  0), array( 1,  4), array( 4,  4), array( 3,  8)), //
561
			array(array( 2,  2), array( 6,  2), array( 4,  6), array( 7,  4)), //
562
			array(array( 4,  0), array( 8,  1), array( 8,  4), array(12,  4)), //
563
			array(array( 3,  1), array( 4,  5), array(11,  5), array(11,  5)), //
564
			array(array( 5,  1), array( 5,  5), array( 5,  7), array(11,  7)), // 15
565
			array(array( 5,  1), array( 7,  3), array(15,  2), array( 3, 13)), //
566
			array(array( 1,  5), array(10,  1), array( 1, 15), array( 2, 17)), //
567
			array(array( 5,  1), array( 9,  4), array(17,  1), array( 2, 19)), //
568
			array(array( 3,  4), array( 3, 11), array(17,  4), array( 9, 16)), //
569
			array(array( 3,  5), array( 3, 13), array(15,  5), array(15, 10)), // 20
570
			array(array( 4,  4), array(17,  0), array(17,  6), array(19,  6)), //
571
			array(array( 2,  7), array(17,  0), array( 7, 16), array(34,  0)), //
572
			array(array( 4,  5), array( 4, 14), array(11, 14), array(16, 14)), //
573
			array(array( 6,  4), array( 6, 14), array(11, 16), array(30,  2)), //
574
			array(array( 8,  4), array( 8, 13), array( 7, 22), array(22, 13)), // 25
575
			array(array(10,  2), array(19,  4), array(28,  6), array(33,  4)), //
576
			array(array( 8,  4), array(22,  3), array( 8, 26), array(12, 28)), //
577
			array(array( 3, 10), array( 3, 23), array( 4, 31), array(11, 31)), //
578
			array(array( 7,  7), array(21,  7), array( 1, 37), array(19, 26)), //
579
			array(array( 5, 10), array(19, 10), array(15, 25), array(23, 25)), // 30
580
			array(array(13,  3), array( 2, 29), array(42,  1), array(23, 28)), //
581
			array(array(17,  0), array(10, 23), array(10, 35), array(19, 35)), //
582
			array(array(17,  1), array(14, 21), array(29, 19), array(11, 46)), //
583
			array(array(13,  6), array(14, 23), array(44,  7), array(59,  1)), //
584
			array(array(12,  7), array(12, 26), array(39, 14), array(22, 41)), // 35
585
			array(array( 6, 14), array( 6, 34), array(46, 10), array( 2, 64)), //
586
			array(array(17,  4), array(29, 14), array(49, 10), array(24, 46)), //
587
			array(array( 4, 18), array(13, 32), array(48, 14), array(42, 32)), //
588
			array(array(20,  4), array(40,  7), array(43, 22), array(10, 67)), //
589
			array(array(19,  6), array(18, 31), array(34, 34), array(20, 61))  // 40
590
		);
591
 
592
		/**
593
		 * @var array Positions of alignment patterns.
594
		 * This array includes only the second and the third position of the alignment patterns. Rest of them can be calculated from the distance between them.
595
		 * See Table 1 in Appendix E (pp.71) of JIS X0510:2004.
596
		 * @access protected
597
		 */
598
		protected $alignmentPattern = array(
599
			array( 0,  0),
600
			array( 0,  0), array(18,  0), array(22,  0), array(26,  0), array(30,  0), //  1- 5
601
			array(34,  0), array(22, 38), array(24, 42), array(26, 46), array(28, 50), //  6-10
602
			array(30, 54), array(32, 58), array(34, 62), array(26, 46), array(26, 48), // 11-15
603
			array(26, 50), array(30, 54), array(30, 56), array(30, 58), array(34, 62), // 16-20
604
			array(28, 50), array(26, 50), array(30, 54), array(28, 54), array(32, 58), // 21-25
605
			array(30, 58), array(34, 62), array(26, 50), array(30, 54), array(26, 52), // 26-30
606
			array(30, 56), array(34, 60), array(30, 58), array(34, 62), array(30, 54), // 31-35
607
			array(24, 50), array(28, 54), array(32, 58), array(26, 54), array(30, 58)  // 35-40
608
		);
609
 
610
		/**
611
		 * @var array Version information pattern (BCH coded).
612
		 * See Table 1 in Appendix D (pp.68) of JIS X0510:2004.
613
		 * size: [QRSPEC_VERSION_MAX - 6]
614
		 * @access protected
615
		 */
616
		protected $versionPattern = array(
617
			0x07c94, 0x085bc, 0x09a99, 0x0a4d3, 0x0bbf6, 0x0c762, 0x0d847, 0x0e60d, //
618
			0x0f928, 0x10b78, 0x1145d, 0x12a17, 0x13532, 0x149a6, 0x15683, 0x168c9, //
619
			0x177ec, 0x18ec4, 0x191e1, 0x1afab, 0x1b08e, 0x1cc1a, 0x1d33f, 0x1ed75, //
620
			0x1f250, 0x209d5, 0x216f0, 0x228ba, 0x2379f, 0x24b0b, 0x2542e, 0x26a64, //
621
			0x27541, 0x28c69
622
		);
623
 
624
		/**
625
		 * @var array Format information
626
		 * @access protected
627
		 */
628
		protected $formatInfo = array(
629
			array(0x77c4, 0x72f3, 0x7daa, 0x789d, 0x662f, 0x6318, 0x6c41, 0x6976), //
630
			array(0x5412, 0x5125, 0x5e7c, 0x5b4b, 0x45f9, 0x40ce, 0x4f97, 0x4aa0), //
631
			array(0x355f, 0x3068, 0x3f31, 0x3a06, 0x24b4, 0x2183, 0x2eda, 0x2bed), //
632
			array(0x1689, 0x13be, 0x1ce7, 0x19d0, 0x0762, 0x0255, 0x0d0c, 0x083b)  //
633
		);
634
 
635
 
636
		// -------------------------------------------------
637
		// -------------------------------------------------
638
 
639
 
640
		/**
641
		 * This is the class constructor.
642
		 * Creates a QRcode object
643
		 * @param string $code code to represent using QRcode
644
		 * @param string $eclevel error level: <ul><li>L : About 7% or less errors can be corrected.</li><li>M : About 15% or less errors can be corrected.</li><li>Q : About 25% or less errors can be corrected.</li><li>H : About 30% or less errors can be corrected.</li></ul>
645
		 * @access public
646
		 * @since 1.0.000
647
		 */
648
		public function __construct($code, $eclevel = 'L') {
649
			$barcode_array = array();
650
			if ((is_null($code)) OR ($code == '\0') OR ($code == '')) {
651
				return false;
652
			}
653
			// set error correction level
654
			$this->level = array_search($eclevel, array('L', 'M', 'Q', 'H'));
655
			if ($this->level === false) {
656
				$this->level = QR_ECLEVEL_L;
657
			}
658
			if (($this->hint != QR_MODE_8B) AND ($this->hint != QR_MODE_KJ)) {
659
				return false;
660
			}
661
			if (($this->version < 0) OR ($this->version > QRSPEC_VERSION_MAX)) {
662
				return false;
663
			}
664
			$this->items = array();
665
			$this->encodeString($code);
666
			if (is_null($this->data)) {
667
				return false;
668
			}
669
			$qrTab = $this->binarize($this->data);
670
			$size = count($qrTab);
671
			$barcode_array['num_rows'] = $size;
672
			$barcode_array['num_cols'] = $size;
673
			$barcode_array['bcode'] = array();
674
			foreach ($qrTab as $line) {
675
				$arrAdd = array();
676
				foreach (str_split($line) as $char) {
677
					$arrAdd[] = ($char=='1')?1:0;
678
				}
679
				$barcode_array['bcode'][] = $arrAdd;
680
			}
681
			$this->barcode_array = $barcode_array;
682
		}
683
 
684
		/**
685
		 * Returns a barcode array which is readable by TCPDF
686
		 * @return array barcode array readable by TCPDF;
687
		 * @access public
688
		 */
689
		public function getBarcodeArray() {
690
			return $this->barcode_array;
691
		}
692
 
693
		/**
694
		 * Convert the frame in binary form
695
		 * @param array $frame array to binarize
696
		 * @return array frame in binary form
697
		 */
698
		protected function binarize($frame) {
699
			$len = count($frame);
700
			// the frame is square (width = height)
701
			foreach ($frame as &$frameLine) {
702
				for ($i=0; $i<$len; $i++) {
703
					$frameLine[$i] = (ord($frameLine[$i])&1)?'1':'0';
704
				}
705
			}
706
			return $frame;
707
		}
708
 
709
		/**
710
		 * Encode the input string to QR code
711
		 * @param string $string input string to encode
712
		 */
713
		protected function encodeString($string) {
714
			$this->dataStr = $string;
715
			if (!$this->casesensitive) {
716
				$this->toUpper();
717
			}
718
			$ret = $this->splitString();
719
			if ($ret < 0) {
720
				return NULL;
721
			}
722
			$this->encodeMask(-1);
723
		}
724
 
725
		/**
726
		 * Encode mask
727
		 * @param int $mask masking mode
728
		 */
729
		protected function encodeMask($mask) {
730
			$spec = array(0, 0, 0, 0, 0);
731
			$this->datacode = $this->getByteStream($this->items);
732
			if (is_null($this->datacode)) {
733
				return NULL;
734
			}
735
			$spec = $this->getEccSpec($this->version, $this->level, $spec);
736
			$this->b1 = $this->rsBlockNum1($spec);
737
			$this->dataLength = $this->rsDataLength($spec);
738
			$this->eccLength = $this->rsEccLength($spec);
739
			$this->ecccode = array_fill(0, $this->eccLength, 0);
740
			$this->blocks = $this->rsBlockNum($spec);
741
			$ret = $this->init($spec);
742
			if ($ret < 0) {
743
				return NULL;
744
			}
745
			$this->count = 0;
746
			$this->width = $this->getWidth($this->version);
747
			$this->frame = $this->newFrame($this->version);
748
			$this->x = $this->width - 1;
749
			$this->y = $this->width - 1;
750
			$this->dir = -1;
751
			$this->bit = -1;
752
			// inteleaved data and ecc codes
753
			for ($i=0; $i < ($this->dataLength + $this->eccLength); $i++) {
754
				$code = $this->getCode();
755
				$bit = 0x80;
756
				for ($j=0; $j<8; $j++) {
757
					$addr = $this->getNextPosition();
758
					$this->setFrameAt($addr, 0x02 | (($bit & $code) != 0));
759
					$bit = $bit >> 1;
760
				}
761
			}
762
			// remainder bits
763
			$j = $this->getRemainder($this->version);
764
			for ($i=0; $i<$j; $i++) {
765
				$addr = $this->getNextPosition();
766
				$this->setFrameAt($addr, 0x02);
767
			}
768
			// masking
769
			$this->runLength = array_fill(0, QRSPEC_WIDTH_MAX + 1, 0);
770
			if ($mask < 0) {
771
				if (QR_FIND_BEST_MASK) {
772
					$masked = $this->mask($this->width, $this->frame, $this->level);
773
				} else {
774
					$masked = $this->makeMask($this->width, $this->frame, (intval(QR_DEFAULT_MASK) % 8), $this->level);
775
				}
776
			} else {
777
				$masked = $this->makeMask($this->width, $this->frame, $mask, $this->level);
778
			}
779
			if ($masked == NULL) {
780
				return NULL;
781
			}
782
			$this->data = $masked;
783
		}
784
 
785
		// - - - - - - - - - - - - - - - - - - - - - - - - -
786
 
787
		// FrameFiller
788
 
789
		/**
790
		 * Set frame value at specified position
791
		 * @param array $at x,y position
792
		 * @param int $val value of the character to set
793
		 */
794
		protected function setFrameAt($at, $val) {
795
			$this->frame[$at['y']][$at['x']] = chr($val);
796
		}
797
 
798
		/**
799
		 * Get frame value at specified position
800
		 * @param array $at x,y position
801
		 * @return value at specified position
802
		 */
803
		protected function getFrameAt($at) {
804
			return ord($this->frame[$at['y']][$at['x']]);
805
		}
806
 
807
		/**
808
		 * Return the next frame position
809
		 * @return array of x,y coordinates
810
		 */
811
		protected function getNextPosition() {
812
			do {
813
				if ($this->bit == -1) {
814
					$this->bit = 0;
815
					return array('x'=>$this->x, 'y'=>$this->y);
816
				}
817
				$x = $this->x;
818
				$y = $this->y;
819
				$w = $this->width;
820
				if ($this->bit == 0) {
821
					$x--;
822
					$this->bit++;
823
				} else {
824
					$x++;
825
					$y += $this->dir;
826
					$this->bit--;
827
				}
828
				if ($this->dir < 0) {
829
					if ($y < 0) {
830
						$y = 0;
831
						$x -= 2;
832
						$this->dir = 1;
833
						if ($x == 6) {
834
							$x--;
835
							$y = 9;
836
						}
837
					}
838
				} else {
839
					if ($y == $w) {
840
						$y = $w - 1;
841
						$x -= 2;
842
						$this->dir = -1;
843
						if ($x == 6) {
844
							$x--;
845
							$y -= 8;
846
						}
847
					}
848
				}
849
				if (($x < 0) OR ($y < 0)) {
850
					return NULL;
851
				}
852
				$this->x = $x;
853
				$this->y = $y;
854
			} while(ord($this->frame[$y][$x]) & 0x80);
855
			return array('x'=>$x, 'y'=>$y);
856
		}
857
 
858
		// - - - - - - - - - - - - - - - - - - - - - - - - -
859
 
860
		// QRrawcode
861
 
862
		/**
863
		 * Initialize code.
864
		 * @param array $spec array of ECC specification
865
		 * @return 0 in case of success, -1 in case of error
866
		 */
867
		protected function init($spec) {
868
			$dl = $this->rsDataCodes1($spec);
869
			$el = $this->rsEccCodes1($spec);
870
			$rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
871
			$blockNo = 0;
872
			$dataPos = 0;
873
			$eccPos = 0;
874
			$endfor = $this->rsBlockNum1($spec);
875
			for ($i=0; $i < $endfor; ++$i) {
876
				$ecc = array_slice($this->ecccode, $eccPos);
877
				$this->rsblocks[$blockNo] = array();
878
				$this->rsblocks[$blockNo]['dataLength'] = $dl;
879
				$this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
880
				$this->rsblocks[$blockNo]['eccLength'] = $el;
881
				$ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
882
				$this->rsblocks[$blockNo]['ecc'] = $ecc;
883
				$this->ecccode = array_merge(array_slice($this->ecccode,0, $eccPos), $ecc);
884
				$dataPos += $dl;
885
				$eccPos += $el;
886
				$blockNo++;
887
			}
888
			if ($this->rsBlockNum2($spec) == 0) {
889
				return 0;
890
			}
891
			$dl = $this->rsDataCodes2($spec);
892
			$el = $this->rsEccCodes2($spec);
893
			$rs = $this->init_rs(8, 0x11d, 0, 1, $el, 255 - $dl - $el);
894
			if ($rs == NULL) {
895
				return -1;
896
			}
897
			$endfor = $this->rsBlockNum2($spec);
898
			for ($i=0; $i < $endfor; ++$i) {
899
				$ecc = array_slice($this->ecccode, $eccPos);
900
				$this->rsblocks[$blockNo] = array();
901
				$this->rsblocks[$blockNo]['dataLength'] = $dl;
902
				$this->rsblocks[$blockNo]['data'] = array_slice($this->datacode, $dataPos);
903
				$this->rsblocks[$blockNo]['eccLength'] = $el;
904
				$ecc = $this->encode_rs_char($rs, $this->rsblocks[$blockNo]['data'], $ecc);
905
				$this->rsblocks[$blockNo]['ecc'] = $ecc;
906
				$this->ecccode = array_merge(array_slice($this->ecccode, 0, $eccPos), $ecc);
907
				$dataPos += $dl;
908
				$eccPos += $el;
909
				$blockNo++;
910
			}
911
			return 0;
912
		}
913
 
914
		/**
915
		 * Return Reed-Solomon block code.
916
		 * @return array rsblocks
917
		 */
918
		protected function getCode() {
919
			if ($this->count < $this->dataLength) {
920
				$row = $this->count % $this->blocks;
921
				$col = $this->count / $this->blocks;
922
				if ($col >= $this->rsblocks[0]['dataLength']) {
923
					$row += $this->b1;
924
				}
925
				$ret = $this->rsblocks[$row]['data'][$col];
926
			} elseif ($this->count < $this->dataLength + $this->eccLength) {
927
				$row = ($this->count - $this->dataLength) % $this->blocks;
928
				$col = ($this->count - $this->dataLength) / $this->blocks;
929
				$ret = $this->rsblocks[$row]['ecc'][$col];
930
			} else {
931
				return 0;
932
			}
933
			$this->count++;
934
			return $ret;
935
		}
936
 
937
		// - - - - - - - - - - - - - - - - - - - - - - - - -
938
 
939
		// QRmask
940
 
941
		/**
942
		 * Write Format Information on frame and returns the number of black bits
943
		 * @param int $width frame width
944
		 * @param array $frame frame
945
		 * @param array $mask masking mode
946
		 * @param int $level error correction level
947
		 * @return int blacks
948
		 */
949
		 protected function writeFormatInformation($width, &$frame, $mask, $level) {
950
			$blacks = 0;
951
			$format =  $this->getFormatInfo($mask, $level);
952
			for ($i=0; $i<8; ++$i) {
953
				if ($format & 1) {
954
					$blacks += 2;
955
					$v = 0x85;
956
				} else {
957
					$v = 0x84;
958
				}
959
				$frame[8][$width - 1 - $i] = chr($v);
960
				if ($i < 6) {
961
					$frame[$i][8] = chr($v);
962
				} else {
963
					$frame[$i + 1][8] = chr($v);
964
				}
965
				$format = $format >> 1;
966
			}
967
			for ($i=0; $i<7; ++$i) {
968
			if ($format & 1) {
969
				$blacks += 2;
970
				$v = 0x85;
971
			} else {
972
				$v = 0x84;
973
			}
974
			$frame[$width - 7 + $i][8] = chr($v);
975
			if ($i == 0) {
976
				$frame[8][7] = chr($v);
977
			} else {
978
				$frame[8][6 - $i] = chr($v);
979
			}
980
			$format = $format >> 1;
981
			}
982
			return $blacks;
983
		}
984
 
985
		/**
986
		 * mask0
987
		 * @param int $x X position
988
		 * @param int $y Y position
989
		 * @return int mask
990
		 */
991
		 protected function mask0($x, $y) {
992
			return ($x + $y) & 1;
993
		}
994
 
995
		/**
996
		 * mask1
997
		 * @param int $x X position
998
		 * @param int $y Y position
999
		 * @return int mask
1000
		 */
1001
		 protected function mask1($x, $y) {
1002
			return ($y & 1);
1003
		}
1004
 
1005
		/**
1006
		 * mask2
1007
		 * @param int $x X position
1008
		 * @param int $y Y position
1009
		 * @return int mask
1010
		 */
1011
		 protected function mask2($x, $y) {
1012
			return ($x % 3);
1013
		}
1014
 
1015
		/**
1016
		 * mask3
1017
		 * @param int $x X position
1018
		 * @param int $y Y position
1019
		 * @return int mask
1020
		 */
1021
		 protected function mask3($x, $y) {
1022
			return ($x + $y) % 3;
1023
		}
1024
 
1025
		/**
1026
		 * mask4
1027
		 * @param int $x X position
1028
		 * @param int $y Y position
1029
		 * @return int mask
1030
		 */
1031
		 protected function mask4($x, $y) {
1032
			return (((int)($y / 2)) + ((int)($x / 3))) & 1;
1033
		}
1034
 
1035
		/**
1036
		 * mask5
1037
		 * @param int $x X position
1038
		 * @param int $y Y position
1039
		 * @return int mask
1040
		 */
1041
		 protected function mask5($x, $y) {
1042
			return (($x * $y) & 1) + ($x * $y) % 3;
1043
		}
1044
 
1045
		/**
1046
		 * mask6
1047
		 * @param int $x X position
1048
		 * @param int $y Y position
1049
		 * @return int mask
1050
		 */
1051
		 protected function mask6($x, $y) {
1052
			return ((($x * $y) & 1) + ($x * $y) % 3) & 1;
1053
		}
1054
 
1055
		/**
1056
		 * mask7
1057
		 * @param int $x X position
1058
		 * @param int $y Y position
1059
		 * @return int mask
1060
		 */
1061
		 protected function mask7($x, $y) {
1062
			return ((($x * $y) % 3) + (($x + $y) & 1)) & 1;
1063
		}
1064
 
1065
		/**
1066
		 * Return bitmask
1067
		 * @param int $maskNo mask number
1068
		 * @param int $width width
1069
		 * @param array $frame frame
1070
		 * @return array bitmask
1071
		 */
1072
		protected function generateMaskNo($maskNo, $width, $frame) {
1073
			$bitMask = array_fill(0, $width, array_fill(0, $width, 0));
1074
			for ($y=0; $y<$width; ++$y) {
1075
				for ($x=0; $x<$width; ++$x) {
1076
					if (ord($frame[$y][$x]) & 0x80) {
1077
						$bitMask[$y][$x] = 0;
1078
					} else {
1079
						$maskFunc = call_user_func(array($this, 'mask'.$maskNo), $x, $y);
1080
						$bitMask[$y][$x] = ($maskFunc == 0)?1:0;
1081
					}
1082
				}
1083
			}
1084
			return $bitMask;
1085
		}
1086
 
1087
		/**
1088
		 * makeMaskNo
1089
		 * @param int $maskNo
1090
		 * @param int $width
1091
		 * @param int $s
1092
		 * @param int $d
1093
		 * @param boolean $maskGenOnly
1094
		 * @return int b
1095
		 */
1096
		 protected function makeMaskNo($maskNo, $width, $s, &$d, $maskGenOnly=false) {
1097
			$b = 0;
1098
			$bitMask = array();
1099
			$bitMask = $this->generateMaskNo($maskNo, $width, $s, $d);
1100
			if ($maskGenOnly) {
1101
				return;
1102
			}
1103
			$d = $s;
1104
			for ($y=0; $y<$width; ++$y) {
1105
				for ($x=0; $x<$width; ++$x) {
1106
					if ($bitMask[$y][$x] == 1) {
1107
						$d[$y][$x] = chr(ord($s[$y][$x]) ^ ((int)($bitMask[$y][$x])));
1108
					}
1109
					$b += (int)(ord($d[$y][$x]) & 1);
1110
				}
1111
			}
1112
			return $b;
1113
		}
1114
 
1115
		/**
1116
		 * makeMask
1117
		 * @param int $width
1118
		 * @param array $frame
1119
		 * @param int $maskNo
1120
		 * @param int $level
1121
		 * @return array mask
1122
		 */
1123
		 protected function makeMask($width, $frame, $maskNo, $level) {
1124
			$masked = array_fill(0, $width, str_repeat("\0", $width));
1125
			$this->makeMaskNo($maskNo, $width, $frame, $masked);
1126
			$this->writeFormatInformation($width, $masked, $maskNo, $level);
1127
			return $masked;
1128
		}
1129
 
1130
		/**
1131
		 * calcN1N3
1132
		 * @param int $length
1133
		 * @return int demerit
1134
		 */
1135
		 protected function calcN1N3($length) {
1136
			$demerit = 0;
1137
			for ($i=0; $i<$length; ++$i) {
1138
				if ($this->runLength[$i] >= 5) {
1139
					$demerit += (N1 + ($this->runLength[$i] - 5));
1140
				}
1141
				if ($i & 1) {
1142
					if (($i >= 3) AND ($i < ($length-2)) AND ($this->runLength[$i] % 3 == 0)) {
1143
						$fact = (int)($this->runLength[$i] / 3);
1144
						if (($this->runLength[$i-2] == $fact)
1145
							AND ($this->runLength[$i-1] == $fact)
1146
							AND ($this->runLength[$i+1] == $fact)
1147
							AND ($this->runLength[$i+2] == $fact)) {
1148
							if (($this->runLength[$i-3] < 0) OR ($this->runLength[$i-3] >= (4 * $fact))) {
1149
								$demerit += N3;
1150
							} elseif ((($i+3) >= $length) OR ($this->runLength[$i+3] >= (4 * $fact))) {
1151
								$demerit += N3;
1152
							}
1153
						}
1154
					}
1155
				}
1156
			}
1157
			return $demerit;
1158
		}
1159
 
1160
		/**
1161
		 * evaluateSymbol
1162
		 * @param int $width
1163
		 * @param array $frame
1164
		 * @return int demerit
1165
		 */
1166
		 protected function evaluateSymbol($width, $frame) {
1167
			$head = 0;
1168
			$demerit = 0;
1169
			for ($y=0; $y<$width; ++$y) {
1170
				$head = 0;
1171
				$this->runLength[0] = 1;
1172
				$frameY = $frame[$y];
1173
				if ($y > 0) {
1174
					$frameYM = $frame[$y-1];
1175
				}
1176
				for ($x=0; $x<$width; ++$x) {
1177
					if (($x > 0) AND ($y > 0)) {
1178
						$b22 = ord($frameY[$x]) & ord($frameY[$x-1]) & ord($frameYM[$x]) & ord($frameYM[$x-1]);
1179
						$w22 = ord($frameY[$x]) | ord($frameY[$x-1]) | ord($frameYM[$x]) | ord($frameYM[$x-1]);
1180
						if (($b22 | ($w22 ^ 1)) & 1) {
1181
							$demerit += N2;
1182
						}
1183
					}
1184
					if (($x == 0) AND (ord($frameY[$x]) & 1)) {
1185
						$this->runLength[0] = -1;
1186
						$head = 1;
1187
						$this->runLength[$head] = 1;
1188
					} elseif ($x > 0) {
1189
						if ((ord($frameY[$x]) ^ ord($frameY[$x-1])) & 1) {
1190
							$head++;
1191
							$this->runLength[$head] = 1;
1192
						} else {
1193
							$this->runLength[$head]++;
1194
						}
1195
					}
1196
				}
1197
				$demerit += $this->calcN1N3($head+1);
1198
			}
1199
			for ($x=0; $x<$width; ++$x) {
1200
				$head = 0;
1201
				$this->runLength[0] = 1;
1202
				for ($y=0; $y<$width; ++$y) {
1203
					if (($y == 0) AND (ord($frame[$y][$x]) & 1)) {
1204
						$this->runLength[0] = -1;
1205
						$head = 1;
1206
						$this->runLength[$head] = 1;
1207
					} elseif ($y > 0) {
1208
						if ((ord($frame[$y][$x]) ^ ord($frame[$y-1][$x])) & 1) {
1209
							$head++;
1210
							$this->runLength[$head] = 1;
1211
						} else {
1212
							$this->runLength[$head]++;
1213
						}
1214
					}
1215
				}
1216
				$demerit += $this->calcN1N3($head+1);
1217
			}
1218
			return $demerit;
1219
		}
1220
 
1221
		/**
1222
		 * mask
1223
		 * @param int $width
1224
		 * @param array $frame
1225
		 * @param int $level
1226
		 * @return array best mask
1227
		 */
1228
		 protected function mask($width, $frame, $level) {
1229
			$minDemerit = PHP_INT_MAX;
1230
			$bestMaskNum = 0;
1231
			$bestMask = array();
1232
			$checked_masks = array(0, 1, 2, 3, 4, 5, 6, 7);
1233
			if (QR_FIND_FROM_RANDOM !== false) {
1234
				$howManuOut = 8 - (QR_FIND_FROM_RANDOM % 9);
1235
				for ($i = 0; $i <  $howManuOut; ++$i) {
1236
					$remPos = rand (0, count($checked_masks)-1);
1237
					unset($checked_masks[$remPos]);
1238
					$checked_masks = array_values($checked_masks);
1239
				}
1240
			}
1241
			$bestMask = $frame;
1242
			foreach ($checked_masks as $i) {
1243
				$mask = array_fill(0, $width, str_repeat("\0", $width));
1244
				$demerit = 0;
1245
				$blacks = 0;
1246
				$blacks  = $this->makeMaskNo($i, $width, $frame, $mask);
1247
				$blacks += $this->writeFormatInformation($width, $mask, $i, $level);
1248
				$blacks  = (int)(100 * $blacks / ($width * $width));
1249
				$demerit = (int)((int)(abs($blacks - 50) / 5) * N4);
1250
				$demerit += $this->evaluateSymbol($width, $mask);
1251
				if ($demerit < $minDemerit) {
1252
					$minDemerit = $demerit;
1253
					$bestMask = $mask;
1254
					$bestMaskNum = $i;
1255
				}
1256
			}
1257
			return $bestMask;
1258
		}
1259
 
1260
		// - - - - - - - - - - - - - - - - - - - - - - - - -
1261
 
1262
		// QRsplit
1263
 
1264
		/**
1265
		 * Return true if the character at specified position is a number
1266
		 * @param string $str string
1267
		 * @param int $pos characted position
1268
		 * @return boolean true of false
1269
		 */
1270
		 protected function isdigitat($str, $pos) {
1271
			if ($pos >= strlen($str)) {
1272
				return false;
1273
			}
1274
			return ((ord($str[$pos]) >= ord('0'))&&(ord($str[$pos]) <= ord('9')));
1275
		}
1276
 
1277
		/**
1278
		 * Return true if the character at specified position is an alphanumeric character
1279
		 * @param string $str string
1280
		 * @param int $pos characted position
1281
		 * @return boolean true of false
1282
		 */
1283
		 protected function isalnumat($str, $pos) {
1284
			if ($pos >= strlen($str)) {
1285
				return false;
1286
			}
1287
			return ($this->lookAnTable(ord($str[$pos])) >= 0);
1288
		}
1289
 
1290
		/**
1291
		 * identifyMode
1292
		 * @param int $pos
1293
		 * @return int mode
1294
		 */
1295
		 protected function identifyMode($pos) {
1296
			if ($pos >= strlen($this->dataStr)) {
1297
				return QR_MODE_NL;
1298
			}
1299
			$c = $this->dataStr[$pos];
1300
			if ($this->isdigitat($this->dataStr, $pos)) {
1301
				return QR_MODE_NM;
1302
			} elseif ($this->isalnumat($this->dataStr, $pos)) {
1303
				return QR_MODE_AN;
1304
			} elseif ($this->hint == QR_MODE_KJ) {
1305
				if ($pos+1 < strlen($this->dataStr)) {
1306
					$d = $this->dataStr[$pos+1];
1307
					$word = (ord($c) << 8) | ord($d);
1308
					if (($word >= 0x8140 && $word <= 0x9ffc) OR ($word >= 0xe040 && $word <= 0xebbf)) {
1309
						return QR_MODE_KJ;
1310
					}
1311
				}
1312
			}
1313
			return QR_MODE_8B;
1314
		}
1315
 
1316
		/**
1317
		 * eatNum
1318
		 * @return int run
1319
		 */
1320
		 protected function eatNum() {
1321
			$ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1322
			$p = 0;
1323
			while($this->isdigitat($this->dataStr, $p)) {
1324
				$p++;
1325
			}
1326
			$run = $p;
1327
			$mode = $this->identifyMode($p);
1328
			if ($mode == QR_MODE_8B) {
1329
				$dif = $this->estimateBitsModeNum($run) + 4 + $ln
1330
				+ $this->estimateBitsMode8(1)         // + 4 + l8
1331
				- $this->estimateBitsMode8($run + 1); // - 4 - l8
1332
				if ($dif > 0) {
1333
					return $this->eat8();
1334
				}
1335
			}
1336
			if ($mode == QR_MODE_AN) {
1337
				$dif = $this->estimateBitsModeNum($run) + 4 + $ln
1338
				+ $this->estimateBitsModeAn(1)        // + 4 + la
1339
				- $this->estimateBitsModeAn($run + 1);// - 4 - la
1340
				if ($dif > 0) {
1341
					return $this->eatAn();
1342
				}
1343
			}
1344
			$this->items = $this->appendNewInputItem($this->items, QR_MODE_NM, $run, str_split($this->dataStr));
1345
			return $run;
1346
		}
1347
 
1348
		/**
1349
		 * eatAn
1350
		 * @return int run
1351
		 */
1352
		 protected function eatAn() {
1353
			$la = $this->lengthIndicator(QR_MODE_AN,  $this->version);
1354
			$ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1355
			$p =1 ;
1356
			while($this->isalnumat($this->dataStr, $p)) {
1357
				if ($this->isdigitat($this->dataStr, $p)) {
1358
					$q = $p;
1359
					while($this->isdigitat($this->dataStr, $q)) {
1360
						$q++;
1361
					}
1362
					$dif = $this->estimateBitsModeAn($p) // + 4 + la
1363
					+ $this->estimateBitsModeNum($q - $p) + 4 + $ln
1364
					- $this->estimateBitsModeAn($q); // - 4 - la
1365
					if ($dif < 0) {
1366
						break;
1367
					} else {
1368
						$p = $q;
1369
					}
1370
				} else {
1371
					$p++;
1372
				}
1373
			}
1374
			$run = $p;
1375
			if (!$this->isalnumat($this->dataStr, $p)) {
1376
				$dif = $this->estimateBitsModeAn($run) + 4 + $la
1377
				+ $this->estimateBitsMode8(1) // + 4 + l8
1378
				- $this->estimateBitsMode8($run + 1); // - 4 - l8
1379
				if ($dif > 0) {
1380
					return $this->eat8();
1381
				}
1382
			}
1383
			$this->items = $this->appendNewInputItem($this->items, QR_MODE_AN, $run, str_split($this->dataStr));
1384
			return $run;
1385
		}
1386
 
1387
		/**
1388
		 * eatKanji
1389
		 * @return int run
1390
		 */
1391
		 protected function eatKanji() {
1392
			$p = 0;
1393
			while($this->identifyMode($p) == QR_MODE_KJ) {
1394
				$p += 2;
1395
			}
1396
			$this->items = $this->appendNewInputItem($this->items, QR_MODE_KJ, $p, str_split($this->dataStr));
1397
			return $run;
1398
		}
1399
 
1400
		/**
1401
		 * eat8
1402
		 * @return int run
1403
		 */
1404
		 protected function eat8() {
1405
			$la = $this->lengthIndicator(QR_MODE_AN, $this->version);
1406
			$ln = $this->lengthIndicator(QR_MODE_NM, $this->version);
1407
			$p = 1;
1408
			$dataStrLen = strlen($this->dataStr);
1409
			while($p < $dataStrLen) {
1410
				$mode = $this->identifyMode($p);
1411
				if ($mode == QR_MODE_KJ) {
1412
					break;
1413
				}
1414
				if ($mode == QR_MODE_NM) {
1415
					$q = $p;
1416
					while($this->isdigitat($this->dataStr, $q)) {
1417
						$q++;
1418
					}
1419
					$dif = $this->estimateBitsMode8($p) // + 4 + l8
1420
					+ $this->estimateBitsModeNum($q - $p) + 4 + $ln
1421
					- $this->estimateBitsMode8($q); // - 4 - l8
1422
					if ($dif < 0) {
1423
						break;
1424
					} else {
1425
						$p = $q;
1426
					}
1427
				} elseif ($mode == QR_MODE_AN) {
1428
					$q = $p;
1429
					while($this->isalnumat($this->dataStr, $q)) {
1430
						$q++;
1431
					}
1432
					$dif = $this->estimateBitsMode8($p)  // + 4 + l8
1433
					+ $this->estimateBitsModeAn($q - $p) + 4 + $la
1434
					- $this->estimateBitsMode8($q); // - 4 - l8
1435
					if ($dif < 0) {
1436
						break;
1437
					} else {
1438
						$p = $q;
1439
					}
1440
				} else {
1441
					$p++;
1442
				}
1443
			}
1444
			$run = $p;
1445
			$this->items = $this->appendNewInputItem($this->items, QR_MODE_8B, $run, str_split($this->dataStr));
1446
			return $run;
1447
		}
1448
 
1449
		/**
1450
		 * splitString
1451
		 */
1452
		 protected function splitString() {
1453
			while (strlen($this->dataStr) > 0) {
1454
				if ($this->dataStr == '') {
1455
					return 0;
1456
				}
1457
				$mode = $this->identifyMode(0);
1458
				switch ($mode) {
1459
					case QR_MODE_NM: {
1460
						$length = $this->eatNum();
1461
						break;
1462
					}
1463
					case QR_MODE_AN: {
1464
						$length = $this->eatAn();
1465
						break;
1466
					}
1467
					case QR_MODE_KJ: {
1468
						if ($hint == QR_MODE_KJ) {
1469
							$length = $this->eatKanji();
1470
						} else {
1471
							$length = $this->eat8();
1472
						}
1473
						break;
1474
					}
1475
					default: {
1476
						$length = $this->eat8();
1477
						break;
1478
					}
1479
				}
1480
				if ($length == 0) {
1481
					return 0;
1482
				}
1483
				if ($length < 0) {
1484
					return -1;
1485
				}
1486
				$this->dataStr = substr($this->dataStr, $length);
1487
			}
1488
		}
1489
 
1490
		/**
1491
		 * toUpper
1492
		 */
1493
		 protected function toUpper() {
1494
			$stringLen = strlen($this->dataStr);
1495
			$p = 0;
1496
			while ($p < $stringLen) {
1497
				$mode = $this->identifyMode(substr($this->dataStr, $p), $this->hint);
1498
				if ($mode == QR_MODE_KJ) {
1499
					$p += 2;
1500
				} else {
1501
					if ((ord($this->dataStr[$p]) >= ord('a')) AND (ord($this->dataStr[$p]) <= ord('z'))) {
1502
						$this->dataStr[$p] = chr(ord($this->dataStr[$p]) - 32);
1503
					}
1504
					$p++;
1505
				}
1506
			}
1507
			return $this->dataStr;
1508
		}
1509
 
1510
		// - - - - - - - - - - - - - - - - - - - - - - - - -
1511
 
1512
		// QRinputItem
1513
 
1514
		/**
1515
		 * newInputItem
1516
		 * @param int $mode
1517
		 * @param int $size
1518
		 * @param array $data
1519
		 * @param array $bstream
1520
		 * @return array input item
1521
		 */
1522
		 protected function newInputItem($mode, $size, $data, $bstream=null) {
1523
			$setData = array_slice($data, 0, $size);
1524
			if (count($setData) < $size) {
1525
				$setData = array_merge($setData, array_fill(0, ($size - count($setData)), 0));
1526
			}
1527
			if (!$this->check($mode, $size, $setData)) {
1528
				return NULL;
1529
			}
1530
			$inputitem = array();
1531
			$inputitem['mode'] = $mode;
1532
			$inputitem['size'] = $size;
1533
			$inputitem['data'] = $setData;
1534
			$inputitem['bstream'] = $bstream;
1535
			return $inputitem;
1536
		}
1537
 
1538
		/**
1539
		 * encodeModeNum
1540
		 * @param array $inputitem
1541
		 * @param int $version
1542
		 * @return array input item
1543
		 */
1544
		 protected function encodeModeNum($inputitem, $version) {
1545
			$words = (int)($inputitem['size'] / 3);
1546
			$inputitem['bstream'] = array();
1547
			$val = 0x1;
1548
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
1549
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_NM, $version), $inputitem['size']);
1550
			for ($i=0; $i < $words; ++$i) {
1551
				$val  = (ord($inputitem['data'][$i*3  ]) - ord('0')) * 100;
1552
				$val += (ord($inputitem['data'][$i*3+1]) - ord('0')) * 10;
1553
				$val += (ord($inputitem['data'][$i*3+2]) - ord('0'));
1554
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 10, $val);
1555
			}
1556
			if ($inputitem['size'] - $words * 3 == 1) {
1557
				$val = ord($inputitem['data'][$words*3]) - ord('0');
1558
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, $val);
1559
			} elseif (($inputitem['size'] - ($words * 3)) == 2) {
1560
				$val  = (ord($inputitem['data'][$words*3  ]) - ord('0')) * 10;
1561
				$val += (ord($inputitem['data'][$words*3+1]) - ord('0'));
1562
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 7, $val);
1563
			}
1564
			return $inputitem;
1565
		}
1566
 
1567
		/**
1568
		 * encodeModeAn
1569
		 * @param array $inputitem
1570
		 * @param int $version
1571
		 * @return array input item
1572
		 */
1573
		 protected function encodeModeAn($inputitem, $version) {
1574
			$words = (int)($inputitem['size'] / 2);
1575
			$inputitem['bstream'] = array();
1576
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x02);
1577
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_AN, $version), $inputitem['size']);
1578
			for ($i=0; $i < $words; ++$i) {
1579
				$val  = (int)($this->lookAnTable(ord($inputitem['data'][$i*2])) * 45);
1580
				$val += (int)($this->lookAnTable(ord($inputitem['data'][($i*2)+1])));
1581
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 11, $val);
1582
			}
1583
			if ($inputitem['size'] & 1) {
1584
				$val = $this->lookAnTable(ord($inputitem['data'][($words * 2)]));
1585
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 6, $val);
1586
			}
1587
			return $inputitem;
1588
		}
1589
 
1590
		/**
1591
		 * encodeMode8
1592
		 * @param array $inputitem
1593
		 * @param int $version
1594
		 * @return array input item
1595
		 */
1596
		 protected function encodeMode8($inputitem, $version) {
1597
			$inputitem['bstream'] = array();
1598
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x4);
1599
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_8B, $version), $inputitem['size']);
1600
			for ($i=0; $i < $inputitem['size']; ++$i) {
1601
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][$i]));
1602
			}
1603
			return $inputitem;
1604
		}
1605
 
1606
		/**
1607
		 * encodeModeKanji
1608
		 * @param array $inputitem
1609
		 * @param int $version
1610
		 * @return array input item
1611
		 */
1612
		 protected function encodeModeKanji($inputitem, $version) {
1613
			$inputitem['bstream'] = array();
1614
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x8);
1615
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], $this->lengthIndicator(QR_MODE_KJ, $version), (int)($inputitem['size'] / 2));
1616
			for ($i=0; $i<$inputitem['size']; $i+=2) {
1617
				$val = (ord($inputitem['data'][$i]) << 8) | ord($inputitem['data'][$i+1]);
1618
				if ($val <= 0x9ffc) {
1619
					$val -= 0x8140;
1620
				} else {
1621
					$val -= 0xc140;
1622
				}
1623
				$h = ($val >> 8) * 0xc0;
1624
				$val = ($val & 0xff) + $h;
1625
				$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 13, $val);
1626
			}
1627
			return $inputitem;
1628
		}
1629
 
1630
		/**
1631
		 * encodeModeStructure
1632
		 * @param array $inputitem
1633
		 * @return array input item
1634
		 */
1635
		 protected function encodeModeStructure($inputitem) {
1636
			$inputitem['bstream'] = array();
1637
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, 0x03);
1638
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][1]) - 1);
1639
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 4, ord($inputitem['data'][0]) - 1);
1640
			$inputitem['bstream'] = $this->appendNum($inputitem['bstream'], 8, ord($inputitem['data'][2]));
1641
			return $inputitem;
1642
		}
1643
 
1644
		/**
1645
		 * encodeBitStream
1646
		 * @param array $inputitem
1647
		 * @param int $version
1648
		 * @return array input item
1649
		 */
1650
		 protected function encodeBitStream($inputitem, $version) {
1651
			$inputitem['bstream'] = array();
1652
			$words = $this->maximumWords($inputitem['mode'], $version);
1653
			if ($inputitem['size'] > $words) {
1654
				$st1 = $this->newInputItem($inputitem['mode'], $words, $inputitem['data']);
1655
				$st2 = $this->newInputItem($inputitem['mode'], $inputitem['size'] - $words, array_slice($inputitem['data'], $words));
1656
				$st1 = $this->encodeBitStream($st1, $version);
1657
				$st2 = $this->encodeBitStream($st2, $version);
1658
				$inputitem['bstream'] = array();
1659
				$inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st1['bstream']);
1660
				$inputitem['bstream'] = $this->appendBitstream($inputitem['bstream'], $st2['bstream']);
1661
			} else {
1662
				switch($inputitem['mode']) {
1663
					case QR_MODE_NM: {
1664
						$inputitem = $this->encodeModeNum($inputitem, $version);
1665
						break;
1666
					}
1667
					case QR_MODE_AN: {
1668
						$inputitem = $this->encodeModeAn($inputitem, $version);
1669
						break;
1670
					}
1671
					case QR_MODE_8B: {
1672
						$inputitem = $this->encodeMode8($inputitem, $version);
1673
						break;
1674
					}
1675
					case QR_MODE_KJ: {
1676
						$inputitem = $this->encodeModeKanji($inputitem, $version);
1677
						break;
1678
					}
1679
					case QR_MODE_ST: {
1680
						$inputitem = $this->encodeModeStructure($inputitem);
1681
						break;
1682
					}
1683
					default: {
1684
						break;
1685
					}
1686
				}
1687
			}
1688
			return $inputitem;
1689
		}
1690
 
1691
		// - - - - - - - - - - - - - - - - - - - - - - - - -
1692
 
1693
		// QRinput
1694
 
1695
		/**
1696
		 * Append data to an input object.
1697
		 * The data is copied and appended to the input object.
1698
		 * @param array items input items
1699
		 * @param int $mode encoding mode.
1700
		 * @param int $size size of data (byte).
1701
		 * @param array $data array of input data.
1702
		 * @return items
1703
		 *
1704
		 */
1705
		protected function appendNewInputItem($items, $mode, $size, $data) {
1706
			$newitem = $this->newInputItem($mode, $size, $data);
1707
			if (!empty($newitem)) {
1708
				$items[] = $newitem;
1709
			}
1710
			return $items;
1711
		}
1712
 
1713
		/**
1714
		 * insertStructuredAppendHeader
1715
		 * @param array $items
1716
		 * @param int $size
1717
		 * @param int $index
1718
		 * @param int $parity
1719
		 * @return array items
1720
		 */
1721
		 protected function insertStructuredAppendHeader($items, $size, $index, $parity) {
1722
			if ($size > MAX_STRUCTURED_SYMBOLS) {
1723
				return -1;
1724
			}
1725
			if (($index <= 0) OR ($index > MAX_STRUCTURED_SYMBOLS)) {
1726
				return -1;
1727
			}
1728
			$buf = array($size, $index, $parity);
1729
			$entry = $this->newInputItem(QR_MODE_ST, 3, buf);
1730
			array_unshift($items, $entry);
1731
			return $items;
1732
		}
1733
 
1734
		/**
1735
		 * calcParity
1736
		 * @param array $items
1737
		 * @return int parity
1738
		 */
1739
		 protected function calcParity($items) {
1740
			$parity = 0;
1741
			foreach ($items as $item) {
1742
				if ($item['mode'] != QR_MODE_ST) {
1743
					for ($i=$item['size']-1; $i>=0; --$i) {
1744
						$parity ^= $item['data'][$i];
1745
					}
1746
				}
1747
			}
1748
			return $parity;
1749
		}
1750
 
1751
		/**
1752
		 * checkModeNum
1753
		 * @param int $size
1754
		 * @param array $data
1755
		 * @return boolean true or false
1756
		 */
1757
		 protected function checkModeNum($size, $data) {
1758
			for ($i=0; $i<$size; ++$i) {
1759
				if ((ord($data[$i]) < ord('0')) OR (ord($data[$i]) > ord('9'))){
1760
					return false;
1761
				}
1762
			}
1763
			return true;
1764
		}
1765
 
1766
		/**
1767
		 * Look up the alphabet-numeric convesion table (see JIS X0510:2004, pp.19).
1768
		 * @param int $c character value
1769
		 * @return value
1770
		 */
1771
		protected function lookAnTable($c) {
1772
			return (($c > 127)?-1:$this->anTable[$c]);
1773
		}
1774
 
1775
		/**
1776
		 * checkModeAn
1777
		 * @param int $size
1778
		 * @param array $data
1779
		 * @return boolean true or false
1780
		 */
1781
		 protected function checkModeAn($size, $data) {
1782
			for ($i=0; $i<$size; ++$i) {
1783
				if ($this->lookAnTable(ord($data[$i])) == -1) {
1784
					return false;
1785
				}
1786
			}
1787
			return true;
1788
		}
1789
 
1790
		/**
1791
		 * estimateBitsModeNum
1792
		 * @param int $size
1793
		 * @return int number of bits
1794
		 */
1795
		 protected function estimateBitsModeNum($size) {
1796
			$w = (int)($size / 3);
1797
			$bits = ($w * 10);
1798
			switch($size - ($w * 3)) {
1799
				case 1: {
1800
					$bits += 4;
1801
					break;
1802
				}
1803
				case 2: {
1804
					$bits += 7;
1805
					break;
1806
				}
1807
			}
1808
			return $bits;
1809
		}
1810
 
1811
		/**
1812
		 * estimateBitsModeAn
1813
		 * @param int $size
1814
		 * @return int number of bits
1815
		 */
1816
		 protected function estimateBitsModeAn($size) {
1817
			$bits = (int)($size * 5.5); // (size / 2 ) * 11
1818
			if ($size & 1) {
1819
				$bits += 6;
1820
			}
1821
			return $bits;
1822
		}
1823
 
1824
		/**
1825
		 * estimateBitsMode8
1826
		 * @param int $size
1827
		 * @return int number of bits
1828
		 */
1829
		 protected function estimateBitsMode8($size) {
1830
			return (int)($size * 8);
1831
		}
1832
 
1833
		/**
1834
		 * estimateBitsModeKanji
1835
		 * @param int $size
1836
		 * @return int number of bits
1837
		 */
1838
		 protected function estimateBitsModeKanji($size) {
1839
			return (int)($size * 6.5); // (size / 2 ) * 13
1840
		}
1841
 
1842
		/**
1843
		 * checkModeKanji
1844
		 * @param int $size
1845
		 * @param array $data
1846
		 * @return boolean true or false
1847
		 */
1848
		 protected function checkModeKanji($size, $data) {
1849
			if ($size & 1) {
1850
				return false;
1851
			}
1852
			for ($i=0; $i<$size; $i+=2) {
1853
				$val = (ord($data[$i]) << 8) | ord($data[$i+1]);
1854
				if (($val < 0x8140) OR (($val > 0x9ffc) AND ($val < 0xe040)) OR ($val > 0xebbf)) {
1855
					return false;
1856
				}
1857
			}
1858
			return true;
1859
		}
1860
 
1861
		/**
1862
		 * Validate the input data.
1863
		 * @param int $mode encoding mode.
1864
		 * @param int $size size of data (byte).
1865
		 * @param array data data to validate
1866
		 * @return boolean true in case of valid data, false otherwise
1867
		 */
1868
		protected function check($mode, $size, $data) {
1869
			if ($size <= 0) {
1870
				return false;
1871
			}
1872
			switch($mode) {
1873
				case QR_MODE_NM: {
1874
					return $this->checkModeNum($size, $data);
1875
				}
1876
				case QR_MODE_AN: {
1877
					return $this->checkModeAn($size, $data);
1878
				}
1879
				case QR_MODE_KJ: {
1880
					return $this->checkModeKanji($size, $data);
1881
				}
1882
				case QR_MODE_8B: {
1883
					return true;
1884
				}
1885
				case QR_MODE_ST: {
1886
					return true;
1887
				}
1888
				default: {
1889
					break;
1890
				}
1891
			}
1892
			return false;
1893
		}
1894
 
1895
		/**
1896
		 * estimateBitStreamSize
1897
		 * @param array $items
1898
		 * @param int $version
1899
		 * @return int bits
1900
		 */
1901
		 protected function estimateBitStreamSize($items, $version) {
1902
			$bits = 0;
1903
			if ($version == 0) {
1904
				$version = 1;
1905
			}
1906
			foreach ($items as $item) {
1907
				switch($item['mode']) {
1908
					case QR_MODE_NM: {
1909
						$bits = $this->estimateBitsModeNum($item['size']);
1910
						break;
1911
					}
1912
					case QR_MODE_AN: {
1913
						$bits = $this->estimateBitsModeAn($item['size']);
1914
						break;
1915
					}
1916
					case QR_MODE_8B: {
1917
						$bits = $this->estimateBitsMode8($item['size']);
1918
						break;
1919
					}
1920
					case QR_MODE_KJ: {
1921
						$bits = $this->estimateBitsModeKanji($item['size']);
1922
						break;
1923
					}
1924
					case QR_MODE_ST: {
1925
						return STRUCTURE_HEADER_BITS;
1926
					}
1927
					default: {
1928
						return 0;
1929
					}
1930
				}
1931
				$l = $this->lengthIndicator($item['mode'], $version);
1932
				$m = 1 << $l;
1933
				$num = (int)(($item['size'] + $m - 1) / $m);
1934
				$bits += $num * (4 + $l);
1935
			}
1936
			return $bits;
1937
		}
1938
 
1939
		/**
1940
		 * estimateVersion
1941
		 * @param array $items
1942
		 * @return int version
1943
		 */
1944
		 protected function estimateVersion($items) {
1945
			$version = 0;
1946
			$prev = 0;
1947
			do {
1948
				$prev = $version;
1949
				$bits = $this->estimateBitStreamSize($items, $prev);
1950
				$version = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
1951
				if ($version < 0) {
1952
					return -1;
1953
				}
1954
			} while ($version > $prev);
1955
			return $version;
1956
		}
1957
 
1958
		/**
1959
		 * lengthOfCode
1960
		 * @param int $mode
1961
		 * @param int $version
1962
		 * @param int $bits
1963
		 * @return int size
1964
		 */
1965
		 protected function lengthOfCode($mode, $version, $bits) {
1966
			$payload = $bits - 4 - $this->lengthIndicator($mode, $version);
1967
			switch($mode) {
1968
				case QR_MODE_NM: {
1969
					$chunks = (int)($payload / 10);
1970
					$remain = $payload - $chunks * 10;
1971
					$size = $chunks * 3;
1972
					if ($remain >= 7) {
1973
						$size += 2;
1974
					} elseif ($remain >= 4) {
1975
						$size += 1;
1976
					}
1977
					break;
1978
				}
1979
				case QR_MODE_AN: {
1980
					$chunks = (int)($payload / 11);
1981
					$remain = $payload - $chunks * 11;
1982
					$size = $chunks * 2;
1983
					if ($remain >= 6) {
1984
						++$size;
1985
					}
1986
					break;
1987
				}
1988
				case QR_MODE_8B: {
1989
					$size = (int)($payload / 8);
1990
					break;
1991
				}
1992
				case QR_MODE_KJ: {
1993
					$size = (int)(($payload / 13) * 2);
1994
					break;
1995
				}
1996
				case QR_MODE_ST: {
1997
					$size = (int)($payload / 8);
1998
					break;
1999
				}
2000
				default: {
2001
					$size = 0;
2002
					break;
2003
				}
2004
			}
2005
			$maxsize = $this->maximumWords($mode, $version);
2006
			if ($size < 0) {
2007
				$size = 0;
2008
			}
2009
			if ($size > $maxsize) {
2010
				$size = $maxsize;
2011
			}
2012
			return $size;
2013
		}
2014
 
2015
		/**
2016
		 * createBitStream
2017
		 * @param array $items
2018
		 * @return array of items and total bits
2019
		 */
2020
		 protected function createBitStream($items) {
2021
			$total = 0;
2022
			foreach ($items as $key => $item) {
2023
				$items[$key] = $this->encodeBitStream($item, $this->version);
2024
				$bits = count($items[$key]['bstream']);
2025
				$total += $bits;
2026
			}
2027
			return array($items, $total);
2028
		}
2029
 
2030
		/**
2031
		 * convertData
2032
		 * @param array $items
2033
		 * @return array items
2034
		 */
2035
		 protected function convertData($items) {
2036
			$ver = $this->estimateVersion($items);
2037
			if ($ver > $this->version) {
2038
				$this->version = $ver;
2039
			}
2040
			for (;;) {
2041
				$cbs = $this->createBitStream($items);
2042
				$items = $cbs[0];
2043
				$bits = $cbs[1];
2044
				if ($bits < 0) {
2045
					return -1;
2046
				}
2047
				$ver = $this->getMinimumVersion((int)(($bits + 7) / 8), $this->level);
2048
				if ($ver < 0) {
2049
					return -1;
2050
				} elseif ($ver > $this->version) {
2051
					$this->version = $ver;
2052
				} else {
2053
					break;
2054
				}
2055
			}
2056
			return $items;
2057
		}
2058
 
2059
		/**
2060
		 * Append Padding Bit to bitstream
2061
		 * @param array $bstream
2062
		 * @return array bitstream
2063
		 */
2064
		 protected function appendPaddingBit($bstream) {
2065
		 	if (is_null($bstream)) {
2066
		 		return null;
2067
		 	}
2068
			$bits = count($bstream);
2069
			$maxwords = $this->getDataLength($this->version, $this->level);
2070
			$maxbits = $maxwords * 8;
2071
			if ($maxbits == $bits) {
2072
				return $bstream;
2073
			}
2074
			if ($maxbits - $bits < 5) {
2075
				return $this->appendNum($bstream, $maxbits - $bits, 0);
2076
			}
2077
			$bits += 4;
2078
			$words = (int)(($bits + 7) / 8);
2079
			$padding = array();
2080
			$padding = $this->appendNum($padding, $words * 8 - $bits + 4, 0);
2081
			$padlen = $maxwords - $words;
2082
			if ($padlen > 0) {
2083
				$padbuf = array();
2084
				for ($i=0; $i<$padlen; ++$i) {
2085
					$padbuf[$i] = ($i&1)?0x11:0xec;
2086
				}
2087
				$padding = $this->appendBytes($padding, $padlen, $padbuf);
2088
			}
2089
			return $this->appendBitstream($bstream, $padding);
2090
		}
2091
 
2092
		/**
2093
		 * mergeBitStream
2094
		 * @param array $bstream
2095
		 * @return array bitstream
2096
		 */
2097
		 protected function mergeBitStream($items) {
2098
			$items = $this->convertData($items);
2099
			if (!is_array($items)) {
2100
				return null;
2101
			}
2102
			$bstream = array();
2103
			foreach ($items as $item) {
2104
				$bstream = $this->appendBitstream($bstream, $item['bstream']);
2105
			}
2106
			return $bstream;
2107
		}
2108
 
2109
		/**
2110
		 * Returns a stream of bits.
2111
		 * @param int $items
2112
		 * @return array padded merged byte stream
2113
		 */
2114
		protected function getBitStream($items) {
2115
			$bstream = $this->mergeBitStream($items);
2116
			return $this->appendPaddingBit($bstream);
2117
		}
2118
 
2119
		/**
2120
		 * Pack all bit streams padding bits into a byte array.
2121
		 * @param int $items
2122
		 * @return array padded merged byte stream
2123
		 */
2124
		protected function getByteStream($items) {
2125
			$bstream = $this->getBitStream($items);
2126
			return $this->bitstreamToByte($bstream);
2127
		}
2128
 
2129
		// - - - - - - - - - - - - - - - - - - - - - - - - -
2130
 
2131
		// QRbitstream
2132
 
2133
		/**
2134
		 * Return an array with zeros
2135
		 * @param int $setLength array size
2136
		 * @return array
2137
		 */
2138
		 protected function allocate($setLength) {
2139
			return array_fill(0, $setLength, 0);
2140
		}
2141
 
2142
		/**
2143
		 * Return new bitstream from number
2144
		 * @param int $bits number of bits
2145
		 * @param int $num number
2146
		 * @return array bitstream
2147
		 */
2148
		 protected function newFromNum($bits, $num) {
2149
			$bstream = $this->allocate($bits);
2150
			$mask = 1 << ($bits - 1);
2151
			for ($i=0; $i<$bits; ++$i) {
2152
				if ($num & $mask) {
2153
					$bstream[$i] = 1;
2154
				} else {
2155
					$bstream[$i] = 0;
2156
				}
2157
				$mask = $mask >> 1;
2158
			}
2159
			return $bstream;
2160
		}
2161
 
2162
		/**
2163
		 * Return new bitstream from bytes
2164
		 * @param int $size size
2165
		 * @param array $data bytes
2166
		 * @return array bitstream
2167
		 */
2168
		 protected function newFromBytes($size, $data) {
2169
			$bstream = $this->allocate($size * 8);
2170
			$p=0;
2171
			for ($i=0; $i<$size; ++$i) {
2172
				$mask = 0x80;
2173
				for ($j=0; $j<8; ++$j) {
2174
					if ($data[$i] & $mask) {
2175
						$bstream[$p] = 1;
2176
					} else {
2177
						$bstream[$p] = 0;
2178
					}
2179
					$p++;
2180
					$mask = $mask >> 1;
2181
				}
2182
			}
2183
			return $bstream;
2184
		}
2185
 
2186
		/**
2187
		 * Append one bitstream to another
2188
		 * @param array $bitstream original bitstream
2189
		 * @param array $append bitstream to append
2190
		 * @return array bitstream
2191
		 */
2192
		 protected function appendBitstream($bitstream, $append) {
2193
			if ((!is_array($append)) OR (count($append) == 0)) {
2194
				return $bitstream;
2195
			}
2196
			if (count($bitstream) == 0) {
2197
				return $append;
2198
			}
2199
			return array_values(array_merge($bitstream, $append));
2200
		}
2201
 
2202
		/**
2203
		 * Append one bitstream created from number to another
2204
		 * @param array $bitstream original bitstream
2205
		 * @param int $bits number of bits
2206
		 * @param int $num number
2207
		 * @return array bitstream
2208
		 */
2209
		 protected function appendNum($bitstream, $bits, $num) {
2210
			if ($bits == 0) {
2211
				return 0;
2212
			}
2213
			$b = $this->newFromNum($bits, $num);
2214
			return $this->appendBitstream($bitstream, $b);
2215
		}
2216
 
2217
		/**
2218
		 * Append one bitstream created from bytes to another
2219
		 * @param array $bitstream original bitstream
2220
		 * @param int $size size
2221
		 * @param array $data bytes
2222
		 * @return array bitstream
2223
		 */
2224
		 protected function appendBytes($bitstream, $size, $data) {
2225
			if ($size == 0) {
2226
				return 0;
2227
			}
2228
			$b = $this->newFromBytes($size, $data);
2229
			return $this->appendBitstream($bitstream, $b);
2230
		}
2231
 
2232
		/**
2233
		 * Convert bitstream to bytes
2234
		 * @param array $bitstream original bitstream
2235
		 * @return array of bytes
2236
		 */
2237
		 protected function bitstreamToByte($bstream) {
2238
			if (is_null($bstream)) {
2239
		 		return null;
2240
		 	}
2241
			$size = count($bstream);
2242
			if ($size == 0) {
2243
				return array();
2244
			}
2245
			$data = array_fill(0, (int)(($size + 7) / 8), 0);
2246
			$bytes = (int)($size / 8);
2247
			$p = 0;
2248
			for ($i=0; $i<$bytes; $i++) {
2249
				$v = 0;
2250
				for ($j=0; $j<8; $j++) {
2251
					$v = $v << 1;
2252
					$v |= $bstream[$p];
2253
					$p++;
2254
				}
2255
				$data[$i] = $v;
2256
			}
2257
			if ($size & 7) {
2258
				$v = 0;
2259
				for ($j=0; $j<($size & 7); $j++) {
2260
					$v = $v << 1;
2261
					$v |= $bstream[$p];
2262
					$p++;
2263
				}
2264
				$data[$bytes] = $v;
2265
			}
2266
			return $data;
2267
		}
2268
 
2269
		// - - - - - - - - - - - - - - - - - - - - - - - - -
2270
 
2271
		// QRspec
2272
 
2273
		/**
2274
		 * Replace a value on the array at the specified position
2275
		 * @param array $srctab
2276
		 * @param int $x X position
2277
		 * @param int $y Y position
2278
		 * @param string $repl value to replace
2279
		 * @param int $replLen length of the repl string
2280
		 * @return array srctab
2281
		 */
2282
		 protected function qrstrset($srctab, $x, $y, $repl, $replLen=false) {
2283
			$srctab[$y] = substr_replace($srctab[$y], ($replLen !== false)?substr($repl,0,$replLen):$repl, $x, ($replLen !== false)?$replLen:strlen($repl));
2284
			return $srctab;
2285
		}
2286
 
2287
		/**
2288
		 * Return maximum data code length (bytes) for the version.
2289
		 * @param int $version version
2290
		 * @param int $level error correction level
2291
		 * @return int maximum size (bytes)
2292
		 */
2293
		protected function getDataLength($version, $level) {
2294
			return $this->capacity[$version][QRCAP_WORDS] - $this->capacity[$version][QRCAP_EC][$level];
2295
		}
2296
 
2297
		/**
2298
		 * Return maximum error correction code length (bytes) for the version.
2299
		 * @param int $version version
2300
		 * @param int $level error correction level
2301
		 * @return int ECC size (bytes)
2302
		 */
2303
		protected function getECCLength($version, $level){
2304
			return $this->capacity[$version][QRCAP_EC][$level];
2305
		}
2306
 
2307
		/**
2308
		 * Return the width of the symbol for the version.
2309
		 * @param int $version version
2310
		 * @return int width
2311
		 */
2312
		protected function getWidth($version) {
2313
			return $this->capacity[$version][QRCAP_WIDTH];
2314
		}
2315
 
2316
		/**
2317
		 * Return the numer of remainder bits.
2318
		 * @param int $version version
2319
		 * @return int number of remainder bits
2320
		 */
2321
		protected function getRemainder($version) {
2322
			return $this->capacity[$version][QRCAP_REMINDER];
2323
		}
2324
 
2325
		/**
2326
		 * Return a version number that satisfies the input code length.
2327
		 * @param int $size input code length (byte)
2328
		 * @param int $level error correction level
2329
		 * @return int version number
2330
		 */
2331
		protected function getMinimumVersion($size, $level) {
2332
			for ($i=1; $i <= QRSPEC_VERSION_MAX; ++$i) {
2333
				$words = $this->capacity[$i][QRCAP_WORDS] - $this->capacity[$i][QRCAP_EC][$level];
2334
				if ($words >= $size) {
2335
					return $i;
2336
				}
2337
			}
2338
			return -1;
2339
		}
2340
 
2341
		/**
2342
		 * Return the size of length indicator for the mode and version.
2343
		 * @param int $mode encoding mode
2344
		 * @param int $version version
2345
		 * @return int the size of the appropriate length indicator (bits).
2346
		 */
2347
		protected function lengthIndicator($mode, $version) {
2348
			if ($mode == QR_MODE_ST) {
2349
				return 0;
2350
			}
2351
			if ($version <= 9) {
2352
				$l = 0;
2353
			} elseif ($version <= 26) {
2354
				$l = 1;
2355
			} else {
2356
				$l = 2;
2357
			}
2358
			return $this->lengthTableBits[$mode][$l];
2359
		}
2360
 
2361
		/**
2362
		 * Return the maximum length for the mode and version.
2363
		 * @param int $mode encoding mode
2364
		 * @param int $version version
2365
		 * @return int the maximum length (bytes)
2366
		 */
2367
		protected function maximumWords($mode, $version) {
2368
			if ($mode == QR_MODE_ST) {
2369
				return 3;
2370
			}
2371
			if ($version <= 9) {
2372
				$l = 0;
2373
			} else if ($version <= 26) {
2374
				$l = 1;
2375
			} else {
2376
				$l = 2;
2377
			}
2378
			$bits = $this->lengthTableBits[$mode][$l];
2379
			$words = (1 << $bits) - 1;
2380
			if ($mode == QR_MODE_KJ) {
2381
				$words *= 2; // the number of bytes is required
2382
			}
2383
			return $words;
2384
		}
2385
 
2386
		/**
2387
		 * Return an array of ECC specification.
2388
		 * @param int $version version
2389
		 * @param int $level error correction level
2390
		 * @param array $spec an array of ECC specification contains as following: {# of type1 blocks, # of data code, # of ecc code, # of type2 blocks, # of data code}
2391
		 * @return array spec
2392
		 */
2393
		protected function getEccSpec($version, $level, $spec) {
2394
			if (count($spec) < 5) {
2395
				$spec = array(0, 0, 0, 0, 0);
2396
			}
2397
			$b1 = $this->eccTable[$version][$level][0];
2398
			$b2 = $this->eccTable[$version][$level][1];
2399
			$data = $this->getDataLength($version, $level);
2400
			$ecc = $this->getECCLength($version, $level);
2401
			if ($b2 == 0) {
2402
				$spec[0] = $b1;
2403
				$spec[1] = (int)($data / $b1);
2404
				$spec[2] = (int)($ecc / $b1);
2405
				$spec[3] = 0;
2406
				$spec[4] = 0;
2407
			} else {
2408
				$spec[0] = $b1;
2409
				$spec[1] = (int)($data / ($b1 + $b2));
2410
				$spec[2] = (int)($ecc  / ($b1 + $b2));
2411
				$spec[3] = $b2;
2412
				$spec[4] = $spec[1] + 1;
2413
			}
2414
			return $spec;
2415
		}
2416
 
2417
		/**
2418
		 * Put an alignment marker.
2419
		 * @param array $frame frame
2420
		 * @param int $width width
2421
		 * @param int $ox X center coordinate of the pattern
2422
		 * @param int $oy Y center coordinate of the pattern
2423
		 * @return array frame
2424
		 */
2425
		protected function putAlignmentMarker($frame, $ox, $oy) {
2426
			$finder = array(
2427
				"\xa1\xa1\xa1\xa1\xa1",
2428
				"\xa1\xa0\xa0\xa0\xa1",
2429
				"\xa1\xa0\xa1\xa0\xa1",
2430
				"\xa1\xa0\xa0\xa0\xa1",
2431
				"\xa1\xa1\xa1\xa1\xa1"
2432
				);
2433
			$yStart = $oy - 2;
2434
			$xStart = $ox - 2;
2435
			for ($y=0; $y < 5; $y++) {
2436
				$frame = $this->qrstrset($frame, $xStart, $yStart+$y, $finder[$y]);
2437
			}
2438
			return $frame;
2439
		}
2440
 
2441
		/**
2442
		 * Put an alignment pattern.
2443
		 * @param int $version version
2444
		 * @param array $fram frame
2445
		 * @param int $width width
2446
		 * @return array frame
2447
		 */
2448
		 protected function putAlignmentPattern($version, $frame, $width) {
2449
			if ($version < 2) {
2450
				return $frame;
2451
			}
2452
			$d = $this->alignmentPattern[$version][1] - $this->alignmentPattern[$version][0];
2453
			if ($d < 0) {
2454
				$w = 2;
2455
			} else {
2456
				$w = (int)(($width - $this->alignmentPattern[$version][0]) / $d + 2);
2457
			}
2458
			if ($w * $w - 3 == 1) {
2459
				$x = $this->alignmentPattern[$version][0];
2460
				$y = $this->alignmentPattern[$version][0];
2461
				$frame = $this->putAlignmentMarker($frame, $x, $y);
2462
				return $frame;
2463
			}
2464
			$cx = $this->alignmentPattern[$version][0];
2465
			$wo = $w - 1;
2466
			for ($x=1; $x < $wo; ++$x) {
2467
				$frame = $this->putAlignmentMarker($frame, 6, $cx);
2468
				$frame = $this->putAlignmentMarker($frame, $cx,  6);
2469
				$cx += $d;
2470
			}
2471
			$cy = $this->alignmentPattern[$version][0];
2472
			for ($y=0; $y < $wo; ++$y) {
2473
				$cx = $this->alignmentPattern[$version][0];
2474
				for ($x=0; $x < $wo; ++$x) {
2475
					$frame = $this->putAlignmentMarker($frame, $cx, $cy);
2476
					$cx += $d;
2477
				}
2478
				$cy += $d;
2479
			}
2480
			return $frame;
2481
		}
2482
 
2483
		/**
2484
		 * Return BCH encoded version information pattern that is used for the symbol of version 7 or greater. Use lower 18 bits.
2485
		 * @param int $version version
2486
		 * @return BCH encoded version information pattern
2487
		 */
2488
		protected function getVersionPattern($version) {
2489
			if (($version < 7) OR ($version > QRSPEC_VERSION_MAX)) {
2490
				return 0;
2491
			}
2492
			return $this->versionPattern[($version - 7)];
2493
		}
2494
 
2495
		/**
2496
		 * Return BCH encoded format information pattern.
2497
		 * @param array $mask
2498
		 * @param int $level error correction level
2499
		 * @return BCH encoded format information pattern
2500
		 */
2501
		protected function getFormatInfo($mask, $level) {
2502
			if (($mask < 0) OR ($mask > 7)) {
2503
				return 0;
2504
			}
2505
			if (($level < 0) OR ($level > 3)) {
2506
				return 0;
2507
			}
2508
			return $this->formatInfo[$level][$mask];
2509
		}
2510
 
2511
		/**
2512
		 * Put a finder pattern.
2513
		 * @param array $frame frame
2514
		 * @param int $width width
2515
		 * @param int $ox X center coordinate of the pattern
2516
		 * @param int $oy Y center coordinate of the pattern
2517
		 * @return array frame
2518
		 */
2519
		protected function putFinderPattern($frame, $ox, $oy) {
2520
			$finder = array(
2521
			"\xc1\xc1\xc1\xc1\xc1\xc1\xc1",
2522
			"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
2523
			"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2524
			"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2525
			"\xc1\xc0\xc1\xc1\xc1\xc0\xc1",
2526
			"\xc1\xc0\xc0\xc0\xc0\xc0\xc1",
2527
			"\xc1\xc1\xc1\xc1\xc1\xc1\xc1"
2528
			);
2529
			for ($y=0; $y < 7; $y++) {
2530
				$frame = $this->qrstrset($frame, $ox, ($oy + $y), $finder[$y]);
2531
			}
2532
			return $frame;
2533
		}
2534
 
2535
		/**
2536
		 * Return a copy of initialized frame.
2537
		 * @param int $version version
2538
		 * @return Array of unsigned char.
2539
		 */
2540
		protected function createFrame($version) {
2541
			$width = $this->capacity[$version][QRCAP_WIDTH];
2542
			$frameLine = str_repeat ("\0", $width);
2543
			$frame = array_fill(0, $width, $frameLine);
2544
			// Finder pattern
2545
			$frame = $this->putFinderPattern($frame, 0, 0);
2546
			$frame = $this->putFinderPattern($frame, $width - 7, 0);
2547
			$frame = $this->putFinderPattern($frame, 0, $width - 7);
2548
			// Separator
2549
			$yOffset = $width - 7;
2550
			for ($y=0; $y < 7; ++$y) {
2551
				$frame[$y][7] = "\xc0";
2552
				$frame[$y][$width - 8] = "\xc0";
2553
				$frame[$yOffset][7] = "\xc0";
2554
				++$yOffset;
2555
			}
2556
			$setPattern = str_repeat("\xc0", 8);
2557
			$frame = $this->qrstrset($frame, 0, 7, $setPattern);
2558
			$frame = $this->qrstrset($frame, $width-8, 7, $setPattern);
2559
			$frame = $this->qrstrset($frame, 0, $width - 8, $setPattern);
2560
			// Format info
2561
			$setPattern = str_repeat("\x84", 9);
2562
			$frame = $this->qrstrset($frame, 0, 8, $setPattern);
2563
			$frame = $this->qrstrset($frame, $width - 8, 8, $setPattern, 8);
2564
			$yOffset = $width - 8;
2565
			for ($y=0; $y < 8; ++$y,++$yOffset) {
2566
				$frame[$y][8] = "\x84";
2567
				$frame[$yOffset][8] = "\x84";
2568
			}
2569
			// Timing pattern
2570
			$wo = $width - 15;
2571
			for ($i=1; $i < $wo; ++$i) {
2572
				$frame[6][7+$i] = chr(0x90 | ($i & 1));
2573
				$frame[7+$i][6] = chr(0x90 | ($i & 1));
2574
			}
2575
			// Alignment pattern
2576
			$frame = $this->putAlignmentPattern($version, $frame, $width);
2577
			// Version information
2578
			if ($version >= 7) {
2579
				$vinf = $this->getVersionPattern($version);
2580
				$v = $vinf;
2581
				for ($x=0; $x<6; ++$x) {
2582
					for ($y=0; $y<3; ++$y) {
2583
						$frame[($width - 11)+$y][$x] = chr(0x88 | ($v & 1));
2584
						$v = $v >> 1;
2585
					}
2586
				}
2587
				$v = $vinf;
2588
				for ($y=0; $y<6; ++$y) {
2589
					for ($x=0; $x<3; ++$x) {
2590
						$frame[$y][$x+($width - 11)] = chr(0x88 | ($v & 1));
2591
						$v = $v >> 1;
2592
					}
2593
				}
2594
			}
2595
			// and a little bit...
2596
			$frame[$width - 8][8] = "\x81";
2597
			return $frame;
2598
		}
2599
 
2600
		/**
2601
		 * Set new frame for the specified version.
2602
		 * @param int $version version
2603
		 * @return Array of unsigned char.
2604
		 */
2605
		protected function newFrame($version) {
2606
			if (($version < 1) OR ($version > QRSPEC_VERSION_MAX)) {
2607
				return NULL;
2608
			}
2609
			if (!isset($this->frames[$version])) {
2610
				$this->frames[$version] = $this->createFrame($version);
2611
			}
2612
			if (is_null($this->frames[$version])) {
2613
				return NULL;
2614
			}
2615
			return $this->frames[$version];
2616
		}
2617
 
2618
		/**
2619
		 * Return block number 0
2620
		 * @param array $spec
2621
		 * @return int value
2622
		 */
2623
		 protected function rsBlockNum($spec) {
2624
			return ($spec[0] + $spec[3]);
2625
		}
2626
 
2627
		/**
2628
		* Return block number 1
2629
		 * @param array $spec
2630
		 * @return int value
2631
		 */
2632
		 protected function rsBlockNum1($spec) {
2633
			return $spec[0];
2634
		}
2635
 
2636
		/**
2637
		 * Return data codes 1
2638
		 * @param array $spec
2639
		 * @return int value
2640
		 */
2641
		 protected function rsDataCodes1($spec) {
2642
			return $spec[1];
2643
		}
2644
 
2645
		/**
2646
		 * Return ecc codes 1
2647
		 * @param array $spec
2648
		 * @return int value
2649
		 */
2650
		 protected function rsEccCodes1($spec) {
2651
			return $spec[2];
2652
		}
2653
 
2654
		/**
2655
		 * Return block number 2
2656
		 * @param array $spec
2657
		 * @return int value
2658
		 */
2659
		 protected function rsBlockNum2($spec) {
2660
			return $spec[3];
2661
		}
2662
 
2663
		/**
2664
		 * Return data codes 2
2665
		 * @param array $spec
2666
		 * @return int value
2667
		 */
2668
		 protected function rsDataCodes2($spec) {
2669
			return $spec[4];
2670
		}
2671
 
2672
		/**
2673
		 * Return ecc codes 2
2674
		 * @param array $spec
2675
		 * @return int value
2676
		 */
2677
		 protected function rsEccCodes2($spec) {
2678
			return $spec[2];
2679
		}
2680
 
2681
		/**
2682
		 * Return data length
2683
		 * @param array $spec
2684
		 * @return int value
2685
		 */
2686
		 protected function rsDataLength($spec) {
2687
			return ($spec[0] * $spec[1]) + ($spec[3] * $spec[4]);
2688
		}
2689
 
2690
		/**
2691
		 * Return ecc length
2692
		 * @param array $spec
2693
		 * @return int value
2694
		 */
2695
		 protected function rsEccLength($spec) {
2696
			return ($spec[0] + $spec[3]) * $spec[2];
2697
		}
2698
 
2699
		// - - - - - - - - - - - - - - - - - - - - - - - - -
2700
 
2701
		// QRrs
2702
 
2703
		/**
2704
		 * Initialize a Reed-Solomon codec and add it to existing rsitems
2705
		 * @param int $symsize symbol size, bits
2706
		 * @param int $gfpoly  Field generator polynomial coefficients
2707
		 * @param int $fcr  first root of RS code generator polynomial, index form
2708
		 * @param int $prim  primitive element to generate polynomial roots
2709
		 * @param int $nroots RS code generator polynomial degree (number of roots)
2710
		 * @param int $pad  padding bytes at front of shortened block
2711
		 * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
2712
		 */
2713
		 protected function init_rs($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
2714
			foreach ($this->rsitems as $rs) {
2715
				if (($rs['pad'] != $pad) OR ($rs['nroots'] != $nroots) OR ($rs['mm'] != $symsize)
2716
					OR ($rs['gfpoly'] != $gfpoly) OR ($rs['fcr'] != $fcr) OR ($rs['prim'] != $prim)) {
2717
					continue;
2718
				}
2719
				return $rs;
2720
			}
2721
			$rs = $this->init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad);
2722
			array_unshift($this->rsitems, $rs);
2723
			return $rs;
2724
		}
2725
 
2726
		// - - - - - - - - - - - - - - - - - - - - - - - - -
2727
 
2728
		// QRrsItem
2729
 
2730
		/**
2731
		 * modnn
2732
		 * @param array RS values
2733
		 * @param int $x X position
2734
		 * @return int X osition
2735
		 */
2736
		 protected function modnn($rs, $x) {
2737
			while ($x >= $rs['nn']) {
2738
				$x -= $rs['nn'];
2739
				$x = ($x >> $rs['mm']) + ($x & $rs['nn']);
2740
			}
2741
			return $x;
2742
		}
2743
 
2744
		/**
2745
		 * Initialize a Reed-Solomon codec and returns an array of values.
2746
		 * @param int $symsize symbol size, bits
2747
		 * @param int $gfpoly  Field generator polynomial coefficients
2748
		 * @param int $fcr  first root of RS code generator polynomial, index form
2749
		 * @param int $prim  primitive element to generate polynomial roots
2750
		 * @param int $nroots RS code generator polynomial degree (number of roots)
2751
		 * @param int $pad  padding bytes at front of shortened block
2752
		 * @return array Array of RS values:<ul><li>mm = Bits per symbol;</li><li>nn = Symbols per block;</li><li>alpha_to = log lookup table array;</li><li>index_of = Antilog lookup table array;</li><li>genpoly = Generator polynomial array;</li><li>nroots = Number of generator;</li><li>roots = number of parity symbols;</li><li>fcr = First consecutive root, index form;</li><li>prim = Primitive element, index form;</li><li>iprim = prim-th root of 1, index form;</li><li>pad = Padding bytes in shortened block;</li><li>gfpoly</ul>.
2753
		 */
2754
		protected function init_rs_char($symsize, $gfpoly, $fcr, $prim, $nroots, $pad) {
2755
			// Based on Reed solomon encoder by Phil Karn, KA9Q (GNU-LGPLv2)
2756
			$rs = null;
2757
			// Check parameter ranges
2758
			if (($symsize < 0) OR ($symsize > 8)) {
2759
				return $rs;
2760
			}
2761
			if (($fcr < 0) OR ($fcr >= (1<<$symsize))) {
2762
				return $rs;
2763
			}
2764
			if (($prim <= 0) OR ($prim >= (1<<$symsize))) {
2765
				return $rs;
2766
			}
2767
			if (($nroots < 0) OR ($nroots >= (1<<$symsize))) {
2768
				return $rs;
2769
			}
2770
			if (($pad < 0) OR ($pad >= ((1<<$symsize) -1 - $nroots))) {
2771
				return $rs;
2772
			}
2773
			$rs = array();
2774
			$rs['mm'] = $symsize;
2775
			$rs['nn'] = (1 << $symsize) - 1;
2776
			$rs['pad'] = $pad;
2777
			$rs['alpha_to'] = array_fill(0, ($rs['nn'] + 1), 0);
2778
			$rs['index_of'] = array_fill(0, ($rs['nn'] + 1), 0);
2779
			// PHP style macro replacement ;)
2780
			$NN =& $rs['nn'];
2781
			$A0 =& $NN;
2782
			// Generate Galois field lookup tables
2783
			$rs['index_of'][0] = $A0; // log(zero) = -inf
2784
			$rs['alpha_to'][$A0] = 0; // alpha**-inf = 0
2785
			$sr = 1;
2786
			for ($i=0; $i<$rs['nn']; ++$i) {
2787
				$rs['index_of'][$sr] = $i;
2788
				$rs['alpha_to'][$i] = $sr;
2789
				$sr <<= 1;
2790
				if ($sr & (1 << $symsize)) {
2791
					$sr ^= $gfpoly;
2792
				}
2793
				$sr &= $rs['nn'];
2794
			}
2795
			if ($sr != 1) {
2796
				// field generator polynomial is not primitive!
2797
				return NULL;
2798
			}
2799
			// Form RS code generator polynomial from its roots
2800
			$rs['genpoly'] = array_fill(0, ($nroots + 1), 0);
2801
			$rs['fcr'] = $fcr;
2802
			$rs['prim'] = $prim;
2803
			$rs['nroots'] = $nroots;
2804
			$rs['gfpoly'] = $gfpoly;
2805
			// Find prim-th root of 1, used in decoding
2806
			for ($iprim=1; ($iprim % $prim) != 0; $iprim += $rs['nn']) {
2807
				; // intentional empty-body loop!
2808
			}
2809
			$rs['iprim'] = (int)($iprim / $prim);
2810
			$rs['genpoly'][0] = 1;
2811
			for ($i = 0,$root=$fcr*$prim; $i < $nroots; $i++, $root += $prim) {
2812
				$rs['genpoly'][$i+1] = 1;
2813
				// Multiply rs->genpoly[] by  @**(root + x)
2814
				for ($j = $i; $j > 0; --$j) {
2815
					if ($rs['genpoly'][$j] != 0) {
2816
						$rs['genpoly'][$j] = $rs['genpoly'][$j-1] ^ $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][$j]] + $root)];
2817
					} else {
2818
						$rs['genpoly'][$j] = $rs['genpoly'][$j-1];
2819
					}
2820
				}
2821
				// rs->genpoly[0] can never be zero
2822
				$rs['genpoly'][0] = $rs['alpha_to'][$this->modnn($rs, $rs['index_of'][$rs['genpoly'][0]] + $root)];
2823
			}
2824
			// convert rs->genpoly[] to index form for quicker encoding
2825
			for ($i = 0; $i <= $nroots; ++$i) {
2826
				$rs['genpoly'][$i] = $rs['index_of'][$rs['genpoly'][$i]];
2827
			}
2828
			return $rs;
2829
		}
2830
 
2831
		/**
2832
		 * Encode a Reed-Solomon codec and returns the parity array
2833
		 * @param array $rs RS values
2834
		 * @param array $data data
2835
		 * @param array $parity parity
2836
		 * @return parity array
2837
		 */
2838
		 protected function encode_rs_char($rs, $data, $parity) {
2839
			$MM       =& $rs['mm']; // bits per symbol
2840
			$NN       =& $rs['nn']; // the total number of symbols in a RS block
2841
			$ALPHA_TO =& $rs['alpha_to']; // the address of an array of NN elements to convert Galois field elements in index (log) form to polynomial form
2842
			$INDEX_OF =& $rs['index_of']; // the address of an array of NN elements to convert Galois field elements in polynomial form to index (log) form
2843
			$GENPOLY  =& $rs['genpoly']; // an array of NROOTS+1 elements containing the generator polynomial in index form
2844
			$NROOTS   =& $rs['nroots']; // the number of roots in the RS code generator polynomial, which is the same as the number of parity symbols in a block
2845
			$FCR      =& $rs['fcr']; // first consecutive root, index form
2846
			$PRIM     =& $rs['prim']; // primitive element, index form
2847
			$IPRIM    =& $rs['iprim']; // prim-th root of 1, index form
2848
			$PAD      =& $rs['pad']; // the number of pad symbols in a block
2849
			$A0       =& $NN;
2850
			$parity = array_fill(0, $NROOTS, 0);
2851
			for ($i=0; $i < ($NN - $NROOTS - $PAD); $i++) {
2852
				$feedback = $INDEX_OF[$data[$i] ^ $parity[0]];
2853
				if ($feedback != $A0) {
2854
					// feedback term is non-zero
2855
					// This line is unnecessary when GENPOLY[NROOTS] is unity, as it must
2856
					// always be for the polynomials constructed by init_rs()
2857
					$feedback = $this->modnn($rs, $NN - $GENPOLY[$NROOTS] + $feedback);
2858
					for ($j=1; $j < $NROOTS; ++$j) {
2859
					$parity[$j] ^= $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[($NROOTS - $j)])];
2860
					}
2861
				}
2862
				// Shift
2863
				array_shift($parity);
2864
				if ($feedback != $A0) {
2865
					array_push($parity, $ALPHA_TO[$this->modnn($rs, $feedback + $GENPOLY[0])]);
2866
				} else {
2867
					array_push($parity, 0);
2868
				}
2869
			}
2870
			return $parity;
2871
		}
2872
 
2873
	} // end QRcode class
2874
 
2875
} // END OF "class_exists QRcode"
2876
//============================================================+
2877
// END OF FILE
2878
//============================================================+
2879
?>