static void embedTypeInfo(ErrorCorrectionLevel ecLevel, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    BitArray typeInfoBits = new BitArray();
    makeTypeInfoBits(ecLevel, maskPattern, typeInfoBits);

    for (int i = 0; i < typeInfoBits.getSize(); ++i) {

      boolean bit = typeInfoBits.get(typeInfoBits.getSize() - 1 - i);

      int x1 = TYPE_INFO_COORDINATES[i][0];
      int y1 = TYPE_INFO_COORDINATES[i][1];
      matrix.set(x1, y1, bit);

      if (i < 8) {

        int x2 = matrix.getWidth() - i - 1;
        int y2 = 8;
        matrix.set(x2, y2, bit);
      } else {

        int x2 = 8;
        int y2 = matrix.getHeight() - 7 + (i - 8);
        matrix.set(x2, y2, bit);
      }
    }
  }
  static void makeVersionInfoBits(int version, BitArray bits) throws WriterException {
    bits.appendBits(version, 6);
    int bchCode = calculateBCHCode(version, VERSION_INFO_POLY);
    bits.appendBits(bchCode, 12);

    if (bits.getSize() != 18) {
      throw new WriterException("should not happen but we got: " + bits.getSize());
    }
  }
  static void embedDataBits(BitArray dataBits, int maskPattern, ByteMatrix matrix)
      throws WriterException {
    int bitIndex = 0;
    int direction = -1;

    int x = matrix.getWidth() - 1;
    int y = matrix.getHeight() - 1;
    while (x > 0) {

      if (x == 6) {
        x -= 1;
      }
      while (y >= 0 && y < matrix.getHeight()) {
        for (int i = 0; i < 2; ++i) {
          int xx = x - i;

          if (!isEmpty(matrix.get(xx, y))) {
            continue;
          }
          boolean bit;
          if (bitIndex < dataBits.getSize()) {
            bit = dataBits.get(bitIndex);
            ++bitIndex;
          } else {

            bit = false;
          }

          if (maskPattern != -1) {
            if (MaskUtil.getDataMaskBit(maskPattern, xx, y)) {
              bit = !bit;
            }
          }
          matrix.set(xx, y, bit);
        }
        y += direction;
      }
      direction = -direction;
      y += direction;
      x -= 2;
    }

    if (bitIndex != dataBits.getSize()) {
      throw new WriterException("Not all bits consumed: " + bitIndex + '/' + dataBits.getSize());
    }
  }
  static void maybeEmbedVersionInfo(int version, ByteMatrix matrix) throws WriterException {
    if (version < 7) {
      return;
    }
    BitArray versionInfoBits = new BitArray();
    makeVersionInfoBits(version, versionInfoBits);

    int bitIndex = 6 * 3 - 1;
    for (int i = 0; i < 6; ++i) {
      for (int j = 0; j < 3; ++j) {

        boolean bit = versionInfoBits.get(bitIndex);
        bitIndex--;

        matrix.set(i, matrix.getHeight() - 11 + j, bit);

        matrix.set(matrix.getHeight() - 11 + j, i, bit);
      }
    }
  }
  static void makeTypeInfoBits(ErrorCorrectionLevel ecLevel, int maskPattern, BitArray bits)
      throws WriterException {
    if (!QRCode.isValidMaskPattern(maskPattern)) {
      throw new WriterException("Invalid mask pattern");
    }
    int typeInfo = (ecLevel.getBits() << 3) | maskPattern;
    bits.appendBits(typeInfo, 5);

    int bchCode = calculateBCHCode(typeInfo, TYPE_INFO_POLY);
    bits.appendBits(bchCode, 10);

    BitArray maskBits = new BitArray();
    maskBits.appendBits(TYPE_INFO_MASK_PATTERN, 15);
    bits.xor(maskBits);

    if (bits.getSize() != 15) {
      throw new WriterException("should not happen but we got: " + bits.getSize());
    }
  }