embedDataBits static method

void embedDataBits(
  1. BitArray dataBits,
  2. int maskPattern,
  3. ByteMatrix matrix
)

Implementation

static void embedDataBits(
  BitArray dataBits,
  int maskPattern,
  ByteMatrix matrix,
) {
  int bitIndex = 0;
  int direction = -1;
  // Start from the right bottom cell.
  int x = matrix.width - 1;
  int y = matrix.height - 1;
  while (x > 0) {
    // Skip the vertical timing pattern.
    if (x == 6) {
      x -= 1;
    }
    while (y >= 0 && y < matrix.height) {
      for (int i = 0; i < 2; ++i) {
        final xx = x - i;
        // Skip the cell if it's not empty.
        if (!_isEmpty(matrix.get(xx, y))) {
          continue;
        }
        bool bit;
        if (bitIndex < dataBits.size) {
          bit = dataBits.get(bitIndex);
          ++bitIndex;
        } else {
          // Padding bit. If there is no bit left, we'll fill the left cells with 0, as described
          // in 8.4.9 of JISX0510:2004 (p. 24).
          bit = false;
        }

        // Skip masking if mask_pattern is -1.
        if (maskPattern != -1 &&
            MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
          bit = !bit;
        }
        matrix.set(xx, y, bit ? 1 : 0);
      }
      y += direction;
    }
    direction = -direction; // Reverse the direction.
    y += direction;
    x -= 2; // Move to the left.
  }
  // All bits should be consumed.
  if (bitIndex != dataBits.size) {
    throw WriterException(
      'Not all bits consumed: $bitIndex' '/${dataBits.size}',
    );
  }
}