All files / src/mask-pattern qr-mask-pattern.util.js

95.83% Statements 46/48
90% Branches 9/10
100% Functions 7/7
95.83% Lines 46/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 491x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 72x 72x     72x 1x 1x 1x 1x 1x 1x 1x 1x  
/*
 mask patterns
 
  Mask 000    Mask 001    Mask 010    Mask 011
   ______      ______      ______      ______
  ▕▀▄▀▄▀▄⎸    ▕▀▀▀▀▀▀⎸    ▕█  █  ⎸    ▕▀ ▄▀ ▄⎸
  ▕▀▄▀▄▀▄⎸    ▕▀▀▀▀▀▀⎸    ▕█  █  ⎸    ▕▄▀ ▄▀ ⎸
  ▕▀▄▀▄▀▄⎸    ▕▀▀▀▀▀▀⎸    ▕█  █  ⎸    ▕ ▄▀ ▄▀⎸
   ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺
 
  Mask 100    Mask 101    Mask 110    Mask 111
   ______      ______      ______      ______
  ▕███   ⎸    ▕█▀▀▀▀▀⎸    ▕███▀▀▀⎸    ▕▀ ▀▄█▄⎸
  ▕   ███⎸    ▕█ ▄▀▄ ⎸    ▕█▀▄▀█ ⎸    ▕▀▄ ▄▀█⎸
  ▕███   ⎸    ▕█  ▀  ⎸    ▕█ ▀▀▄█⎸    ▕ ██▄  ⎸
   ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺      ⎺⎺⎺⎺⎺⎺
*/
 
/** @type {MaskPatternFunction[]} */
const maskPatternFunctions = [
  (i, j) => (i + j) % 2 === 0, // PATTERN000
  (i, _) => i % 2 === 0, // PATTERN001
  (_, j) => j % 3 === 0, // PATTERN010
  (i, j) => (i + j) % 3 === 0, // PATTERN011
  (i, j) => (Math.floor(i / 2) + Math.floor(j / 3)) % 2 === 0, // PATTERN100
  (i, j) => (i * j) % 2 + (i * j) % 3 === 0, // PATTERN101
  (i, j) => ((i * j) % 2 + (i * j) % 3) % 2 === 0, // PATTERN110
  (i, j) => ((i * j) % 3 + (i + j) % 2) % 2 === 0, // PATTERN110
]
 
/**
 * @param {number} maskPattern - mask pattern, an integer number between 0 and 7
 * @returns {MaskPatternFunction} mask pattern function
 */
export function getMaskPatternFunction (maskPattern) {
  const result = maskPatternFunctions[maskPattern]
  if (!result) {
    throw Error(`bad maskPattern: ${maskPattern}`)
  }
  return result
};
 
/**
 * @callback MaskPatternFunction
 * @param {number} x - horizontal position
 * @param {number} y - vertical position
 * @returns {boolean} true to paint a black pixel, false to paint a white pixel
 */