ansi-print.hh 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 <cairo.h>
  30. #include <assert.h>
  31. #include <stdlib.h>
  32. #include <stddef.h>
  33. #include <string.h>
  34. #include <math.h>
  35. #if defined (_MSC_VER) && (_MSC_VER < 1800)
  36. static inline long int
  37. lround (double x)
  38. {
  39. if (x >= 0)
  40. return floor (x + 0.5);
  41. else
  42. return ceil (x - 0.5);
  43. }
  44. #endif
  45. #define CELL_W 8
  46. #define CELL_H (2 * CELL_W)
  47. struct color_diff_t
  48. {
  49. int dot (const color_diff_t &o)
  50. { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
  51. int v[4];
  52. };
  53. struct color_t
  54. {
  55. static color_t from_ansi (unsigned int x)
  56. {
  57. color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
  58. return c;
  59. }
  60. unsigned int to_ansi ()
  61. {
  62. return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
  63. }
  64. color_diff_t diff (const color_t &o)
  65. {
  66. color_diff_t d;
  67. for (unsigned int i = 0; i < 4; i++)
  68. d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
  69. return d;
  70. }
  71. uint32_t v;
  72. };
  73. struct image_t
  74. {
  75. public:
  76. image_t (unsigned int width_,
  77. unsigned int height_,
  78. const uint32_t *data_,
  79. unsigned int stride_) :
  80. width (width_),
  81. height (height_),
  82. own_data (false),
  83. data ((color_t *) data_),
  84. stride (stride_) {}
  85. image_t (unsigned int width_,
  86. unsigned int height_) :
  87. width (width_),
  88. height (height_),
  89. own_data (true),
  90. data ((color_t *) malloc (sizeof (data[0]) * width * height)),
  91. stride (width) {}
  92. ~image_t ()
  93. { if (own_data) free (data); }
  94. color_t &operator () (unsigned int x, unsigned int y)
  95. { return data[x + y * stride]; }
  96. color_t operator () (unsigned int x, unsigned int y) const
  97. { return data[x + y * stride]; }
  98. void
  99. copy_sub_image (const image_t &s,
  100. unsigned int x, unsigned int y,
  101. unsigned int w, unsigned int h)
  102. {
  103. assert (x < width);
  104. assert (y < height);
  105. for (unsigned int row = 0; row < h; row++) {
  106. color_t *p = data + x + hb_min (y + row, height - 1) * stride;
  107. color_t *q = s.data + row * s.stride;
  108. if (x + w <= width)
  109. for (unsigned int col = 0; col < w; col++)
  110. *q++ = *p++;
  111. else {
  112. unsigned int limit = width - x;
  113. for (unsigned int col = 0; col < limit; col++)
  114. *q++ = *p++;
  115. p--;
  116. for (unsigned int col = limit; col < w; col++)
  117. *q++ = *p;
  118. }
  119. }
  120. }
  121. const unsigned int width;
  122. const unsigned int height;
  123. private:
  124. bool own_data;
  125. color_t * const data;
  126. const unsigned int stride;
  127. };
  128. struct biimage_t
  129. {
  130. public:
  131. biimage_t (unsigned int width, unsigned int height) :
  132. width (width),
  133. height (height),
  134. bg (0), fg (0), unicolor (true),
  135. data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
  136. ~biimage_t ()
  137. { free (data); }
  138. void set (const image_t &image)
  139. {
  140. assert (image.width == width);
  141. assert (image.height == height);
  142. int freq[8] = {0};
  143. for (unsigned int y = 0; y < height; y++)
  144. for (unsigned int x = 0; x < width; x++) {
  145. color_t c = image (x, y);
  146. freq[c.to_ansi ()]++;
  147. }
  148. bg = 0;
  149. for (unsigned int i = 1; i < 8; i++)
  150. if (freq[bg] < freq[i])
  151. bg = i;
  152. fg = 8;
  153. for (unsigned int i = 0; i < 8; i++)
  154. if (i != bg && (fg == 8 || freq[fg] < freq[i]))
  155. fg = i;
  156. if (freq[fg] == 0) {
  157. fg = bg;
  158. unicolor = true;
  159. }
  160. else
  161. unicolor = false;
  162. /* Set the data... */
  163. if (unicolor) {
  164. memset (data, 0, sizeof (data[0]) * width * height);
  165. return;
  166. }
  167. color_t bgc = color_t::from_ansi (bg);
  168. color_t fgc = color_t::from_ansi (fg);
  169. color_diff_t diff = fgc.diff (bgc);
  170. double dd = sqrt (diff.dot (diff));
  171. for (unsigned int y = 0; y < height; y++)
  172. for (unsigned int x = 0; x < width; x++) {
  173. double d = sqrt (diff.dot (image (x, y).diff (bgc)));
  174. (*this)(x, y) = d <= 0 ? 0 : d >= dd ? 255 : lround (d / dd * 255.);
  175. }
  176. }
  177. uint8_t &operator () (unsigned int x, unsigned int y)
  178. { return data[x + y * width]; }
  179. uint8_t operator () (unsigned int x, unsigned int y) const
  180. { return data[x + y * width]; }
  181. const unsigned int width;
  182. const unsigned int height;
  183. unsigned int bg;
  184. unsigned int fg;
  185. bool unicolor;
  186. private:
  187. uint8_t * const data;
  188. };
  189. static const char *
  190. block_best (const biimage_t &bi, bool *inverse)
  191. {
  192. assert (bi.width <= CELL_W);
  193. assert (bi.height <= CELL_H);
  194. unsigned int score = UINT_MAX;
  195. unsigned int row_sum[CELL_H] = {0};
  196. unsigned int col_sum[CELL_W] = {0};
  197. unsigned int row_sum_i[CELL_H] = {0};
  198. unsigned int col_sum_i[CELL_W] = {0};
  199. unsigned int quad[2][2] = {{0}};
  200. unsigned int quad_i[2][2] = {{0}};
  201. unsigned int total = 0;
  202. unsigned int total_i = 0;
  203. for (unsigned int y = 0; y < bi.height; y++)
  204. for (unsigned int x = 0; x < bi.width; x++) {
  205. unsigned int c = bi (x, y);
  206. unsigned int c_i = 255 - c;
  207. row_sum[y] += c;
  208. row_sum_i[y] += c_i;
  209. col_sum[x] += c;
  210. col_sum_i[x] += c_i;
  211. quad[2 * y / bi.height][2 * x / bi.width] += c;
  212. quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
  213. total += c;
  214. total_i += c_i;
  215. }
  216. /* Make the sums cumulative */
  217. for (unsigned int i = 1; i < bi.height; i++) {
  218. row_sum[i] += row_sum[i - 1];
  219. row_sum_i[i] += row_sum_i[i - 1];
  220. }
  221. for (unsigned int i = 1; i < bi.width; i++) {
  222. col_sum[i] += col_sum[i - 1];
  223. col_sum_i[i] += col_sum_i[i - 1];
  224. }
  225. const char *best_c = " ";
  226. /* Maybe empty is better! */
  227. if (total < score) {
  228. score = total;
  229. *inverse = false;
  230. best_c = " ";
  231. }
  232. /* Maybe full is better! */
  233. if (total_i < score) {
  234. score = total_i;
  235. *inverse = true;
  236. best_c = " ";
  237. }
  238. /* Find best lower line */
  239. if (1) {
  240. unsigned int best_s = UINT_MAX;
  241. bool best_inv = false;
  242. int best_i = 0;
  243. for (unsigned int i = 0; i < bi.height - 1; i++)
  244. {
  245. unsigned int s;
  246. s = row_sum[i] + total_i - row_sum_i[i];
  247. if (s < best_s) {
  248. best_s = s;
  249. best_i = i;
  250. best_inv = false;
  251. }
  252. s = row_sum_i[i] + total - row_sum[i];
  253. if (s < best_s) {
  254. best_s = s;
  255. best_i = i;
  256. best_inv = true;
  257. }
  258. }
  259. if (best_s < score) {
  260. static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
  261. unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
  262. if (1 <= which && which <= 7) {
  263. score = best_s;
  264. *inverse = best_inv;
  265. best_c = lower[7 - which];
  266. }
  267. }
  268. }
  269. /* Find best left line */
  270. if (1) {
  271. unsigned int best_s = UINT_MAX;
  272. bool best_inv = false;
  273. int best_i = 0;
  274. for (unsigned int i = 0; i < bi.width - 1; i++)
  275. {
  276. unsigned int s;
  277. s = col_sum[i] + total_i - col_sum_i[i];
  278. if (s < best_s) {
  279. best_s = s;
  280. best_i = i;
  281. best_inv = true;
  282. }
  283. s = col_sum_i[i] + total - col_sum[i];
  284. if (s < best_s) {
  285. best_s = s;
  286. best_i = i;
  287. best_inv = false;
  288. }
  289. }
  290. if (best_s < score) {
  291. static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
  292. unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
  293. if (1 <= which && which <= 7) {
  294. score = best_s;
  295. *inverse = best_inv;
  296. best_c = left[which - 1];
  297. }
  298. }
  299. }
  300. /* Find best quadrant */
  301. if (1) {
  302. unsigned int q = 0;
  303. unsigned int qs = 0;
  304. for (unsigned int i = 0; i < 2; i++)
  305. for (unsigned int j = 0; j < 2; j++)
  306. if (quad[i][j] > quad_i[i][j]) {
  307. q += 1 << (2 * i + j);
  308. qs += quad_i[i][j];
  309. } else
  310. qs += quad[i][j];
  311. if (qs < score) {
  312. const char *c = nullptr;
  313. bool inv = false;
  314. switch (q) {
  315. case 1: c = "▟"; inv = true; break;
  316. case 2: c = "▙"; inv = true; break;
  317. case 4: c = "▖"; inv = false; break;
  318. case 6: c = "▞"; inv = false; break;
  319. case 7: c = "▛"; inv = false; break;
  320. case 8: c = "▗"; inv = false; break;
  321. case 9: c = "▚"; inv = false; break;
  322. case 11: c = "▜"; inv = false; break;
  323. case 13: c = "▙"; inv = false; break;
  324. case 14: c = "▟"; inv = false; break;
  325. }
  326. if (c) {
  327. score = qs;
  328. *inverse = inv;
  329. best_c = c;
  330. }
  331. }
  332. }
  333. return best_c;
  334. }
  335. static inline void
  336. ansi_print_image_rgb24 (const uint32_t *data,
  337. unsigned int width,
  338. unsigned int height,
  339. unsigned int stride,
  340. cairo_write_func_t write_func,
  341. void *closure)
  342. {
  343. image_t image (width, height, data, stride);
  344. unsigned int rows = (height + CELL_H - 1) / CELL_H;
  345. unsigned int cols = (width + CELL_W - 1) / CELL_W;
  346. image_t cell (CELL_W, CELL_H);
  347. biimage_t bi (CELL_W, CELL_H);
  348. unsigned int last_bg = -1, last_fg = -1;
  349. for (unsigned int row = 0; row < rows; row++)
  350. {
  351. for (unsigned int col = 0; col < cols; col++)
  352. {
  353. image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
  354. bi.set (cell);
  355. if (bi.unicolor)
  356. {
  357. if (last_bg != bi.bg)
  358. {
  359. char buf[] = "\033[40m";
  360. buf[3] += bi.bg;
  361. write_func (closure, (unsigned char *) buf, 5);
  362. last_bg = bi.bg;
  363. }
  364. write_func (closure, (unsigned char *) " ", 1);
  365. }
  366. else
  367. {
  368. /* Figure out the closest character to the biimage */
  369. bool inverse = false;
  370. const char *c = block_best (bi, &inverse);
  371. if (inverse)
  372. {
  373. if (last_bg != bi.fg || last_fg != bi.bg)
  374. {
  375. char buf[] = "\033[30;40m";
  376. buf[3] += bi.bg;
  377. buf[6] += bi.fg;
  378. write_func (closure, (unsigned char *) buf, 8);
  379. last_bg = bi.fg;
  380. last_fg = bi.bg;
  381. }
  382. }
  383. else
  384. {
  385. if (last_bg != bi.bg || last_fg != bi.fg)
  386. {
  387. char buf[] = "\033[40;30m";
  388. buf[3] += bi.bg;
  389. buf[6] += bi.fg;
  390. write_func (closure, (unsigned char *) buf, 8);
  391. last_bg = bi.bg;
  392. last_fg = bi.fg;
  393. }
  394. }
  395. write_func (closure, (unsigned char *) c, strlen (c));
  396. }
  397. }
  398. write_func (closure, (unsigned char *) "\033[0m\n", 5); /* Reset */
  399. last_bg = last_fg = -1;
  400. }
  401. }
  402. #endif