ansi-print.hh 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. /*
  2. * Copyright © 2012 Google, Inc.
  3. *
  4. * This is part of HarfBuzz, a text shaping library.
  5. *
  6. * Permission is hereby granted, without written agreement and without
  7. * license or royalty fees, to use, copy, modify, and distribute this
  8. * software and its documentation for any purpose, provided that the
  9. * above copyright notice and the following two paragraphs appear in
  10. * all copies of this software.
  11. *
  12. * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR
  13. * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  14. * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN
  15. * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH
  16. * DAMAGE.
  17. *
  18. * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING,
  19. * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
  20. * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
  21. * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO
  22. * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  23. *
  24. * Google Author(s): Behdad Esfahbod
  25. */
  26. #ifndef ANSI_PRINT_HH
  27. #define ANSI_PRINT_HH
  28. #include "hb.hh"
  29. #include <assert.h>
  30. #include <stdlib.h>
  31. #include <stddef.h>
  32. #include <string.h>
  33. #include <stdio.h>
  34. #include <math.h>
  35. #include <fcntl.h>
  36. #ifdef HAVE_UNISTD_H
  37. #include <unistd.h> /* for isatty() */
  38. #endif
  39. #if defined (_MSC_VER) && (_MSC_VER < 1800)
  40. static inline long int
  41. lround (double x)
  42. {
  43. if (x >= 0)
  44. return floor (x + 0.5);
  45. else
  46. return ceil (x - 0.5);
  47. }
  48. #endif
  49. #define ESC_E (char)27
  50. #define CELL_W 8
  51. #define CELL_H (2 * CELL_W)
  52. struct color_diff_t
  53. {
  54. int dot (const color_diff_t &o)
  55. { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
  56. int v[4];
  57. };
  58. struct color_t
  59. {
  60. static color_t from_ansi (unsigned int x)
  61. {
  62. color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
  63. return c;
  64. }
  65. unsigned int to_ansi ()
  66. {
  67. return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
  68. }
  69. color_diff_t diff (const color_t &o)
  70. {
  71. color_diff_t d;
  72. for (unsigned int i = 0; i < 4; i++)
  73. d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
  74. return d;
  75. }
  76. uint32_t v;
  77. };
  78. struct image_t
  79. {
  80. public:
  81. image_t (unsigned int width_,
  82. unsigned int height_,
  83. const uint32_t *data_,
  84. unsigned int stride_) :
  85. width (width_),
  86. height (height_),
  87. own_data (false),
  88. data ((color_t *) data_),
  89. stride (stride_) {}
  90. image_t (unsigned int width_,
  91. unsigned int height_) :
  92. width (width_),
  93. height (height_),
  94. own_data (true),
  95. data ((color_t *) malloc (sizeof (data[0]) * width * height)),
  96. stride (width) {}
  97. ~image_t ()
  98. { if (own_data) free (data); }
  99. color_t &operator () (unsigned int x, unsigned int y)
  100. { return data[x + y * stride]; }
  101. color_t operator () (unsigned int x, unsigned int y) const
  102. { return data[x + y * stride]; }
  103. void
  104. copy_sub_image (const image_t &s,
  105. unsigned int x, unsigned int y,
  106. unsigned int w, unsigned int h)
  107. {
  108. assert (x < width);
  109. assert (y < height);
  110. for (unsigned int row = 0; row < h; row++) {
  111. color_t *p = data + x + hb_min (y + row, height - 1) * stride;
  112. color_t *q = s.data + row * s.stride;
  113. if (x + w <= width)
  114. for (unsigned int col = 0; col < w; col++)
  115. *q++ = *p++;
  116. else {
  117. unsigned int limit = width - x;
  118. for (unsigned int col = 0; col < limit; col++)
  119. *q++ = *p++;
  120. p--;
  121. for (unsigned int col = limit; col < w; col++)
  122. *q++ = *p;
  123. }
  124. }
  125. }
  126. const unsigned int width;
  127. const unsigned int height;
  128. private:
  129. bool own_data;
  130. color_t * const data;
  131. const unsigned int stride;
  132. };
  133. struct biimage_t
  134. {
  135. public:
  136. biimage_t (unsigned int width, unsigned int height) :
  137. width (width),
  138. height (height),
  139. bg (0), fg (0), unicolor (true),
  140. data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
  141. ~biimage_t ()
  142. { free (data); }
  143. void set (const image_t &image)
  144. {
  145. assert (image.width == width);
  146. assert (image.height == height);
  147. int freq[8] = {0};
  148. for (unsigned int y = 0; y < height; y++)
  149. for (unsigned int x = 0; x < width; x++) {
  150. color_t c = image (x, y);
  151. freq[c.to_ansi ()]++;
  152. }
  153. bg = 0;
  154. for (unsigned int i = 1; i < 8; i++)
  155. if (freq[bg] < freq[i])
  156. bg = i;
  157. fg = 8;
  158. for (unsigned int i = 0; i < 8; i++)
  159. if (i != bg && (fg == 8 || freq[fg] < freq[i]))
  160. fg = i;
  161. if (freq[fg] == 0) {
  162. fg = bg;
  163. unicolor = true;
  164. }
  165. else
  166. unicolor = false;
  167. /* Set the data... */
  168. if (unicolor) {
  169. memset (data, 0, sizeof (data[0]) * width * height);
  170. return;
  171. }
  172. color_t bgc = color_t::from_ansi (bg);
  173. color_t fgc = color_t::from_ansi (fg);
  174. color_diff_t diff = fgc.diff (bgc);
  175. double dd = sqrt (diff.dot (diff));
  176. for (unsigned int y = 0; y < height; y++)
  177. for (unsigned int x = 0; x < width; x++) {
  178. double d = sqrt (diff.dot (image (x, y).diff (bgc)));
  179. (*this)(x, y) = d <= 0 ? 0 : d >= dd ? 255 : lround (d / dd * 255.);
  180. }
  181. }
  182. uint8_t &operator () (unsigned int x, unsigned int y)
  183. { return data[x + y * width]; }
  184. uint8_t operator () (unsigned int x, unsigned int y) const
  185. { return data[x + y * width]; }
  186. const unsigned int width;
  187. const unsigned int height;
  188. unsigned int bg;
  189. unsigned int fg;
  190. bool unicolor;
  191. private:
  192. uint8_t * const data;
  193. };
  194. static const char *
  195. block_best (const biimage_t &bi, bool *inverse)
  196. {
  197. assert (bi.width <= CELL_W);
  198. assert (bi.height <= CELL_H);
  199. unsigned int score = UINT_MAX;
  200. unsigned int row_sum[CELL_H] = {0};
  201. unsigned int col_sum[CELL_W] = {0};
  202. unsigned int row_sum_i[CELL_H] = {0};
  203. unsigned int col_sum_i[CELL_W] = {0};
  204. unsigned int quad[2][2] = {{0}};
  205. unsigned int quad_i[2][2] = {{0}};
  206. unsigned int total = 0;
  207. unsigned int total_i = 0;
  208. for (unsigned int y = 0; y < bi.height; y++)
  209. for (unsigned int x = 0; x < bi.width; x++) {
  210. unsigned int c = bi (x, y);
  211. unsigned int c_i = 255 - c;
  212. row_sum[y] += c;
  213. row_sum_i[y] += c_i;
  214. col_sum[x] += c;
  215. col_sum_i[x] += c_i;
  216. quad[2 * y / bi.height][2 * x / bi.width] += c;
  217. quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
  218. total += c;
  219. total_i += c_i;
  220. }
  221. /* Make the sums cumulative */
  222. for (unsigned int i = 1; i < bi.height; i++) {
  223. row_sum[i] += row_sum[i - 1];
  224. row_sum_i[i] += row_sum_i[i - 1];
  225. }
  226. for (unsigned int i = 1; i < bi.width; i++) {
  227. col_sum[i] += col_sum[i - 1];
  228. col_sum_i[i] += col_sum_i[i - 1];
  229. }
  230. const char *best_c = " ";
  231. /* Maybe empty is better! */
  232. if (total < score) {
  233. score = total;
  234. *inverse = false;
  235. best_c = " ";
  236. }
  237. /* Maybe full is better! */
  238. if (total_i < score) {
  239. score = total_i;
  240. *inverse = true;
  241. best_c = " ";
  242. }
  243. /* Find best lower line */
  244. if (1) {
  245. unsigned int best_s = UINT_MAX;
  246. bool best_inv = false;
  247. int best_i = 0;
  248. for (unsigned int i = 0; i < bi.height - 1; i++)
  249. {
  250. unsigned int s;
  251. s = row_sum[i] + total_i - row_sum_i[i];
  252. if (s < best_s) {
  253. best_s = s;
  254. best_i = i;
  255. best_inv = false;
  256. }
  257. s = row_sum_i[i] + total - row_sum[i];
  258. if (s < best_s) {
  259. best_s = s;
  260. best_i = i;
  261. best_inv = true;
  262. }
  263. }
  264. if (best_s < score) {
  265. static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
  266. unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
  267. if (1 <= which && which <= 7) {
  268. score = best_s;
  269. *inverse = best_inv;
  270. best_c = lower[7 - which];
  271. }
  272. }
  273. }
  274. /* Find best left line */
  275. if (1) {
  276. unsigned int best_s = UINT_MAX;
  277. bool best_inv = false;
  278. int best_i = 0;
  279. for (unsigned int i = 0; i < bi.width - 1; i++)
  280. {
  281. unsigned int s;
  282. s = col_sum[i] + total_i - col_sum_i[i];
  283. if (s < best_s) {
  284. best_s = s;
  285. best_i = i;
  286. best_inv = true;
  287. }
  288. s = col_sum_i[i] + total - col_sum[i];
  289. if (s < best_s) {
  290. best_s = s;
  291. best_i = i;
  292. best_inv = false;
  293. }
  294. }
  295. if (best_s < score) {
  296. static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
  297. unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
  298. if (1 <= which && which <= 7) {
  299. score = best_s;
  300. *inverse = best_inv;
  301. best_c = left[which - 1];
  302. }
  303. }
  304. }
  305. /* Find best quadrant */
  306. if (1) {
  307. unsigned int q = 0;
  308. unsigned int qs = 0;
  309. for (unsigned int i = 0; i < 2; i++)
  310. for (unsigned int j = 0; j < 2; j++)
  311. if (quad[i][j] > quad_i[i][j]) {
  312. q += 1 << (2 * i + j);
  313. qs += quad_i[i][j];
  314. } else
  315. qs += quad[i][j];
  316. if (qs < score) {
  317. const char *c = nullptr;
  318. bool inv = false;
  319. switch (q) {
  320. case 1: c = "▟"; inv = true; break;
  321. case 2: c = "▙"; inv = true; break;
  322. case 4: c = "▖"; inv = false; break;
  323. case 6: c = "▞"; inv = false; break;
  324. case 7: c = "▛"; inv = false; break;
  325. case 8: c = "▗"; inv = false; break;
  326. case 9: c = "▚"; inv = false; break;
  327. case 11: c = "▜"; inv = false; break;
  328. case 13: c = "▙"; inv = false; break;
  329. case 14: c = "▟"; inv = false; break;
  330. }
  331. if (c) {
  332. score = qs;
  333. *inverse = inv;
  334. best_c = c;
  335. }
  336. }
  337. }
  338. return best_c;
  339. }
  340. static inline void
  341. ansi_print_image_rgb24 (const uint32_t *data,
  342. unsigned int width,
  343. unsigned int height,
  344. unsigned int stride)
  345. {
  346. image_t image (width, height, data, stride);
  347. unsigned int rows = (height + CELL_H - 1) / CELL_H;
  348. unsigned int cols = (width + CELL_W - 1) / CELL_W;
  349. image_t cell (CELL_W, CELL_H);
  350. biimage_t bi (CELL_W, CELL_H);
  351. unsigned int last_bg = -1, last_fg = -1;
  352. for (unsigned int row = 0; row < rows; row++) {
  353. for (unsigned int col = 0; col < cols; col++) {
  354. image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
  355. bi.set (cell);
  356. if (bi.unicolor) {
  357. if (last_bg != bi.bg) {
  358. printf ("%c[%dm", ESC_E, 40 + bi.bg);
  359. last_bg = bi.bg;
  360. }
  361. printf (" ");
  362. } else {
  363. /* Figure out the closest character to the biimage */
  364. bool inverse = false;
  365. const char *c = block_best (bi, &inverse);
  366. if (inverse) {
  367. if (last_bg != bi.fg || last_fg != bi.bg) {
  368. printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg);
  369. last_bg = bi.fg;
  370. last_fg = bi.bg;
  371. }
  372. } else {
  373. if (last_bg != bi.bg || last_fg != bi.fg) {
  374. printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg);
  375. last_bg = bi.bg;
  376. last_fg = bi.fg;
  377. }
  378. }
  379. printf ("%s", c);
  380. }
  381. }
  382. printf ("%c[0m\n", ESC_E); /* Reset */
  383. last_bg = last_fg = -1;
  384. }
  385. }
  386. #endif