bit-matrix.js 1.5 KB

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