bit-matrix.js 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. var BufferUtil = require('../utils/buffer')
  2. /**
  3. * Helper class to handle QR Code symbol modules
  4. *
  5. * @param {Number} size Symbol size
  6. */
  7. function BitMatrix (size) {
  8. if (!size || size < 1) {
  9. throw new Error('BitMatrix size must be defined and greater than 0')
  10. }
  11. this.size = size
  12. this.data = BufferUtil.alloc(size * size)
  13. this.reservedBit = BufferUtil.alloc(size * size)
  14. }
  15. /**
  16. * Set bit value at specified location
  17. * If reserved flag is set, this bit will be ignored during masking process
  18. *
  19. * @param {Number} row
  20. * @param {Number} col
  21. * @param {Boolean} value
  22. * @param {Boolean} reserved
  23. */
  24. BitMatrix.prototype.set = function (row, col, value, reserved) {
  25. var index = row * this.size + col
  26. this.data[index] = value
  27. if (reserved) this.reservedBit[index] = true
  28. }
  29. /**
  30. * Returns bit value at specified location
  31. *
  32. * @param {Number} row
  33. * @param {Number} col
  34. * @return {Boolean}
  35. */
  36. BitMatrix.prototype.get = function (row, col) {
  37. return this.data[row * this.size + col]
  38. }
  39. /**
  40. * Applies xor operator at specified location
  41. * (used during masking process)
  42. *
  43. * @param {Number} row
  44. * @param {Number} col
  45. * @param {Boolean} value
  46. */
  47. BitMatrix.prototype.xor = function (row, col, value) {
  48. this.data[row * this.size + col] ^= value
  49. }
  50. /**
  51. * Check if bit at specified location is reserved
  52. *
  53. * @param {Number} row
  54. * @param {Number} col
  55. * @return {Boolean}
  56. */
  57. BitMatrix.prototype.isReserved = function (row, col) {
  58. return this.reservedBit[row * this.size + col]
  59. }
  60. module.exports = BitMatrix