node-qrcode-lite/lib/core/bit-matrix.js
Vincenzo Greco 77064f5e5e Refactor/core ()
* Split core lib into multiple files

* Refactor data encoding methods

* Refactor data masking process

* Improve qr code generation process

* Increase minimum required node version to 0.10

* Add linter

* Add tests and tests coverage

* Update travis config to fix compilation issues

* Add examples folder

* Add missing license tag in package.json

* Update build script and add sourcemap support

* Publish only strictly needed files on npm

* Update readme
2016-12-16 23:45:08 +01:00

69 lines
1.5 KiB
JavaScript

var Buffer = require('../utils/buffer')
/**
* Helper class to handle QR Code symbol modules
*
* @param {Number} size Symbol size
*/
function BitMatrix (size) {
if (!size || size < 1) {
throw new Error('BitMatrix size must be defined and greater than 0')
}
this.size = size
this.data = new Buffer(size * size)
this.data.fill(0)
this.reservedBit = new Buffer(size * size)
this.reservedBit.fill(0)
}
/**
* Set bit value at specified location
* If reserved flag is set, this bit will be ignored during masking process
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
* @param {Boolean} reserved
*/
BitMatrix.prototype.set = function (row, col, value, reserved) {
var index = row * this.size + col
this.data[index] = value
if (reserved) this.reservedBit[index] = true
}
/**
* Returns bit value at specified location
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
BitMatrix.prototype.get = function (row, col) {
return this.data[row * this.size + col]
}
/**
* Applies xor operator at specified location
* (used during masking process)
*
* @param {Number} row
* @param {Number} col
* @param {Boolean} value
*/
BitMatrix.prototype.xor = function (row, col, value) {
this.data[row * this.size + col] ^= value
}
/**
* Check if bit at specified location is reserved
*
* @param {Number} row
* @param {Number} col
* @return {Boolean}
*/
BitMatrix.prototype.isReserved = function (row, col) {
return this.reservedBit[row * this.size + col]
}
module.exports = BitMatrix