关键词搜索

源码搜索 ×
×

漫话Redis源码之五十

发布2022-01-02浏览1399次

详情内容

这里的代码,主要是转换相关的。在看源码时,要抓住两点:

1. 之前形式的意义

2. 之后形式的意义

至于转换细节,可以不用看得那么细。

总之,明白目的和用途即可。

  1. /*
  2. * Copyright (c) 2018, Salvatore Sanfilippo <antirez at gmail dot com>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * * Neither the name of Redis nor the names of its contributors may be used
  14. * to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. * POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * ----------------------------------------------------------------------------
  30. *
  31. * This file implements the LOLWUT command. The command should do something
  32. * fun and interesting, and should be replaced by a new implementation at
  33. * each new version of Redis.
  34. */
  35. #include "server.h"
  36. #include "lolwut.h"
  37. #include <math.h>
  38. /* Translate a group of 8 pixels (2x4 vertical rectangle) to the corresponding
  39. * braille character. The byte should correspond to the pixels arranged as
  40. * follows, where 0 is the least significant bit, and 7 the most significant
  41. * bit:
  42. *
  43. * 0 3
  44. * 1 4
  45. * 2 5
  46. * 6 7
  47. *
  48. * The corresponding utf8 encoded character is set into the three bytes
  49. * pointed by 'output'.
  50. */
  51. #include <stdio.h>
  52. void lwTranslatePixelsGroup(int byte, char *output) {
  53. int code = 0x2800 + byte;
  54. /* Convert to unicode. This is in the U0800-UFFFF range, so we need to
  55. * emit it like this in three bytes:
  56. * 1110xxxx 10xxxxxx 10xxxxxx. */
  57. output[0] = 0xE0 | (code >> 12); /* 1110-xxxx */
  58. output[1] = 0x80 | ((code >> 6) & 0x3F); /* 10-xxxxxx */
  59. output[2] = 0x80 | (code & 0x3F); /* 10-xxxxxx */
  60. }
  61. /* Schotter, the output of LOLWUT of Redis 5, is a computer graphic art piece
  62. * generated by Georg Nees in the 60s. It explores the relationship between
  63. * caos and order.
  64. *
  65. * The function creates the canvas itself, depending on the columns available
  66. * in the output display and the number of squares per row and per column
  67. * requested by the caller. */
  68. lwCanvas *lwDrawSchotter(int console_cols, int squares_per_row, int squares_per_col) {
  69. /* Calculate the canvas size. */
  70. int canvas_width = console_cols*2;
  71. int padding = canvas_width > 4 ? 2 : 0;
  72. float square_side = (float)(canvas_width-padding*2) / squares_per_row;
  73. int canvas_height = square_side * squares_per_col + padding*2;
  74. lwCanvas *canvas = lwCreateCanvas(canvas_width, canvas_height, 0);
  75. for (int y = 0; y < squares_per_col; y++) {
  76. for (int x = 0; x < squares_per_row; x++) {
  77. int sx = x * square_side + square_side/2 + padding;
  78. int sy = y * square_side + square_side/2 + padding;
  79. /* Rotate and translate randomly as we go down to lower
  80. * rows. */
  81. float angle = 0;
  82. if (y > 1) {
  83. float r1 = (float)rand() / (float) RAND_MAX / squares_per_col * y;
  84. float r2 = (float)rand() / (float) RAND_MAX / squares_per_col * y;
  85. float r3 = (float)rand() / (float) RAND_MAX / squares_per_col * y;
  86. if (rand() % 2) r1 = -r1;
  87. if (rand() % 2) r2 = -r2;
  88. if (rand() % 2) r3 = -r3;
  89. angle = r1;
  90. sx += r2*square_side/3;
  91. sy += r3*square_side/3;
  92. }
  93. lwDrawSquare(canvas,sx,sy,square_side,angle,1);
  94. }
  95. }
  96. return canvas;
  97. }
  98. /* Converts the canvas to an SDS string representing the UTF8 characters to
  99. * print to the terminal in order to obtain a graphical representaiton of the
  100. * logical canvas. The actual returned string will require a terminal that is
  101. * widthhttps://files.jxasp.com/image/2 large and height/4 tall in order to hold the whole image without
  102. * overflowing or scrolling, since each Barille character is 2x4. */
  103. static sds renderCanvas(lwCanvas *canvas) {
  104. sds text = sdsempty();
  105. for (int y = 0; y < canvas->height; y += 4) {
  106. for (int x = 0; x < canvas->width; x += 2) {
  107. /* We need to emit groups of 8 bits according to a specific
  108. * arrangement. See lwTranslatePixelsGroup() for more info. */
  109. int byte = 0;
  110. if (lwGetPixel(canvas,x,y)) byte |= (1<<0);
  111. if (lwGetPixel(canvas,x,y+1)) byte |= (1<<1);
  112. if (lwGetPixel(canvas,x,y+2)) byte |= (1<<2);
  113. if (lwGetPixel(canvas,x+1,y)) byte |= (1<<3);
  114. if (lwGetPixel(canvas,x+1,y+1)) byte |= (1<<4);
  115. if (lwGetPixel(canvas,x+1,y+2)) byte |= (1<<5);
  116. if (lwGetPixel(canvas,x,y+3)) byte |= (1<<6);
  117. if (lwGetPixel(canvas,x+1,y+3)) byte |= (1<<7);
  118. char unicode[3];
  119. lwTranslatePixelsGroup(byte,unicode);
  120. text = sdscatlen(text,unicode,3);
  121. }
  122. if (y != canvas->height-1) text = sdscatlen(text,"\n",1);
  123. }
  124. return text;
  125. }
  126. /* The LOLWUT command:
  127. *
  128. * LOLWUT [terminal columns] [squares-per-row] [squares-per-col]
  129. *
  130. * By default the command uses 66 columns, 8 squares per row, 12 squares
  131. * per column.
  132. */
  133. void lolwut5Command(client *c) {
  134. long cols = 66;
  135. long squares_per_row = 8;
  136. long squares_per_col = 12;
  137. /* Parse the optional arguments if any. */
  138. if (c->argc > 1 &&
  139. getLongFromObjectOrReply(c,c->argv[1],&cols,NULL) != C_OK)
  140. return;
  141. if (c->argc > 2 &&
  142. getLongFromObjectOrReply(c,c->argv[2],&squares_per_row,NULL) != C_OK)
  143. return;
  144. if (c->argc > 3 &&
  145. getLongFromObjectOrReply(c,c->argv[3],&squares_per_col,NULL) != C_OK)
  146. return;
  147. /* Limits. We want LOLWUT to be always reasonably fast and cheap to execute
  148. * so we have maximum number of columns, rows, and output resolution. */
  149. if (cols < 1) cols = 1;
  150. if (cols > 1000) cols = 1000;
  151. if (squares_per_row < 1) squares_per_row = 1;
  152. if (squares_per_row > 200) squares_per_row = 200;
  153. if (squares_per_col < 1) squares_per_col = 1;
  154. if (squares_per_col > 200) squares_per_col = 200;
  155. /* Generate some computer art and reply. */
  156. lwCanvas *canvas = lwDrawSchotter(cols,squares_per_row,squares_per_col);
  157. sds rendered = renderCanvas(canvas);
  158. rendered = sdscat(rendered,
  159. "\nGeorg Nees - schotter, plotter on paper, 1968. Redis ver. ");
  160. rendered = sdscat(rendered,REDIS_VERSION);
  161. rendered = sdscatlen(rendered,"\n",1);
  162. addReplyVerbatim(c,rendered,sdslen(rendered),"txt");
  163. sdsfree(rendered);
  164. lwFreeCanvas(canvas);
  165. }

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载