blob: 49a0477fcff0d174b7db5f23d2cc508940284f8c [file] [log] [blame]
Behdad Esfahbod52e7b142012-05-13 02:02:58 +02001/*
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
27#ifdef HAVE_CONFIG_H
28#include "config.h"
29#endif
30
31#include "ansi-print.hh"
32
33#include <assert.h>
34#include <stdlib.h>
35#include <stddef.h>
36#include <string.h>
37#include <stdio.h>
38#include <math.h>
39#include <fcntl.h>
40#ifdef HAVE_UNISTD_H
41#include <unistd.h> /* for isatty() */
42#endif
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020043
Chun-wei Fan998e8dd2015-11-02 16:55:29 +080044#if defined (_MSC_VER) && (_MSC_VER < 1800)
Chun-wei Fanb4c5c522013-06-03 17:55:29 +080045static inline long int
46lround (double x)
47{
48 if (x >= 0)
49 return floor (x + 0.5);
50 else
51 return ceil (x - 0.5);
52}
53#endif
54
Chun-wei Fan998e8dd2015-11-02 16:55:29 +080055#define ESC_E (char)27
56
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020057#define MIN(a,b) ((a) < (b) ? (a) : (b))
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020058
59#define CELL_W 8
60#define CELL_H (2 * CELL_W)
61
62struct color_diff_t
63{
64 int dot (const color_diff_t &o)
65 { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
66
67 int v[4];
68};
69
70struct color_t
71{
72 static color_t from_ansi (unsigned int x)
73 {
Ebrahim Byagowi6353cc12018-10-02 21:39:19 +033074 color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020075 return c;
76 }
Ebrahim Byagowie4120082018-12-17 21:31:01 +033077 unsigned int to_ansi ()
Behdad Esfahbod52e7b142012-05-13 02:02:58 +020078 {
79 return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
80 }
81
82 color_diff_t diff (const color_t &o)
83 {
84 color_diff_t d;
85 for (unsigned int i = 0; i < 4; i++)
86 d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
87 return d;
88 }
89
90 uint32_t v;
91};
92
93struct image_t
94{
95 public:
96
97 image_t (unsigned int width_,
98 unsigned int height_,
99 const uint32_t *data_,
100 unsigned int stride_) :
101 width (width_),
102 height (height_),
103 own_data (false),
104 data ((color_t *) data_),
105 stride (stride_) {}
106 image_t (unsigned int width_,
107 unsigned int height_) :
108 width (width_),
109 height (height_),
110 own_data (true),
111 data ((color_t *) malloc (sizeof (data[0]) * width * height)),
112 stride (width) {}
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330113 ~image_t ()
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200114 { if (own_data) free (data); }
115
116 color_t &operator () (unsigned int x, unsigned int y)
117 { return data[x + y * stride]; }
118
119 color_t operator () (unsigned int x, unsigned int y) const
120 { return data[x + y * stride]; }
121
122 void
123 copy_sub_image (const image_t &s,
124 unsigned int x, unsigned int y,
125 unsigned int w, unsigned int h)
126 {
127 assert (x < width);
128 assert (y < height);
129 for (unsigned int row = 0; row < h; row++) {
130 color_t *p = data + x + MIN (y + row, height - 1) * stride;
131 color_t *q = s.data + row * s.stride;
132 if (x + w <= width)
133 for (unsigned int col = 0; col < w; col++)
134 *q++ = *p++;
135 else {
136 unsigned int limit = width - x;
137 for (unsigned int col = 0; col < limit; col++)
138 *q++ = *p++;
139 p--;
140 for (unsigned int col = limit; col < w; col++)
141 *q++ = *p;
142 }
143 }
144 }
145
146 const unsigned int width;
147 const unsigned int height;
148
149 private:
150 bool own_data;
151 color_t * const data;
152 const unsigned int stride;
153};
154
155struct biimage_t
156{
157 public:
158
159 biimage_t (unsigned int width, unsigned int height) :
160 width (width),
161 height (height),
Behdad Esfahbod7235f332013-06-10 14:39:51 -0400162 bg (0), fg (0), unicolor (true),
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200163 data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
Ebrahim Byagowie4120082018-12-17 21:31:01 +0330164 ~biimage_t ()
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200165 { free (data); }
166
167 void set (const image_t &image)
168 {
169 assert (image.width == width);
170 assert (image.height == height);
171 int freq[8] = {0};
172 for (unsigned int y = 0; y < height; y++)
173 for (unsigned int x = 0; x < width; x++) {
174 color_t c = image (x, y);
175 freq[c.to_ansi ()]++;
176 }
177 bg = 0;
178 for (unsigned int i = 1; i < 8; i++)
179 if (freq[bg] < freq[i])
180 bg = i;
181 fg = 0;
182 for (unsigned int i = 1; i < 8; i++)
183 if (i != bg && freq[fg] < freq[i])
184 fg = i;
185 if (fg == bg || freq[fg] == 0) {
186 fg = bg;
187 unicolor = true;
188 }
189 else
190 unicolor = false;
191
192 /* Set the data... */
193
194 if (unicolor) {
195 memset (data, 0, sizeof (data[0]) * width * height);
196 return;
197 }
198
199 color_t bgc = color_t::from_ansi (bg);
200 color_t fgc = color_t::from_ansi (fg);
201 color_diff_t diff = fgc.diff (bgc);
202 int dd = diff.dot (diff);
203 for (unsigned int y = 0; y < height; y++)
204 for (unsigned int x = 0; x < width; x++) {
205 int d = diff.dot (image (x, y).diff (bgc));
206 (*this)(x, y) = d < 0 ? 0 : d > dd ? 255 : lround (d * 255. / dd);
207 }
208 }
209
210 uint8_t &operator () (unsigned int x, unsigned int y)
211 { return data[x + y * width]; }
212
213 uint8_t operator () (unsigned int x, unsigned int y) const
214 { return data[x + y * width]; }
215
216 const unsigned int width;
217 const unsigned int height;
218 unsigned int bg;
219 unsigned int fg;
220 bool unicolor;
221
222 private:
223 uint8_t * const data;
224};
225
Ebrahim Byagowi6353cc12018-10-02 21:39:19 +0330226static const char *
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700227block_best (const biimage_t &bi, bool *inverse)
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200228{
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400229 assert (bi.width <= CELL_W);
230 assert (bi.height <= CELL_H);
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200231
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700232 unsigned int score = (unsigned int) -1;
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400233 unsigned int row_sum[CELL_H] = {0};
234 unsigned int col_sum[CELL_W] = {0};
235 unsigned int row_sum_i[CELL_H] = {0};
236 unsigned int col_sum_i[CELL_W] = {0};
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200237 unsigned int quad[2][2] = {{0}};
238 unsigned int quad_i[2][2] = {{0}};
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400239 unsigned int total = 0;
240 unsigned int total_i = 0;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200241 for (unsigned int y = 0; y < bi.height; y++)
242 for (unsigned int x = 0; x < bi.width; x++) {
243 unsigned int c = bi (x, y);
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400244 unsigned int c_i = 255 - c;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200245 row_sum[y] += c;
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400246 row_sum_i[y] += c_i;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200247 col_sum[x] += c;
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400248 col_sum_i[x] += c_i;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200249 quad[2 * y / bi.height][2 * x / bi.width] += c;
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400250 quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200251 total += c;
Behdad Esfahbod7ec83052012-06-07 13:32:57 -0400252 total_i += c_i;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200253 }
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200254
255 /* Make the sums cummulative */
256 for (unsigned int i = 1; i < bi.height; i++) {
257 row_sum[i] += row_sum[i - 1];
258 row_sum_i[i] += row_sum_i[i - 1];
259 }
260 for (unsigned int i = 1; i < bi.width; i++) {
261 col_sum[i] += col_sum[i - 1];
262 col_sum_i[i] += col_sum_i[i - 1];
263 }
264
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200265 const char *best_c = " ";
266
267 /* Maybe empty is better! */
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700268 if (total < score) {
269 score = total;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200270 *inverse = false;
271 best_c = " ";
272 }
273 /* Maybe full is better! */
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700274 if (total_i < score) {
275 score = total_i;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200276 *inverse = true;
277 best_c = " ";
278 }
279
280 /* Find best lower line */
281 if (1) {
282 unsigned int best_s = (unsigned int) -1;
283 bool best_inv = false;
284 int best_i = 0;
285 for (unsigned int i = 0; i < bi.height - 1; i++)
286 {
287 unsigned int s;
288 s = row_sum[i] + total_i - row_sum_i[i];
289 if (s < best_s) {
290 best_s = s;
291 best_i = i;
292 best_inv = false;
293 }
294 s = row_sum_i[i] + total - row_sum[i];
295 if (s < best_s) {
296 best_s = s;
297 best_i = i;
298 best_inv = true;
299 }
300 }
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700301 if (best_s < score) {
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200302 static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800303 unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200304 if (1 <= which && which <= 7) {
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700305 score = best_s;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200306 *inverse = best_inv;
307 best_c = lower[7 - which];
308 }
309 }
310 }
311
312 /* Find best left line */
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200313 if (1) {
314 unsigned int best_s = (unsigned int) -1;
315 bool best_inv = false;
316 int best_i = 0;
317 for (unsigned int i = 0; i < bi.width - 1; i++)
318 {
319 unsigned int s;
320 s = col_sum[i] + total_i - col_sum_i[i];
321 if (s < best_s) {
322 best_s = s;
323 best_i = i;
324 best_inv = true;
325 }
326 s = col_sum_i[i] + total - col_sum[i];
327 if (s < best_s) {
328 best_s = s;
329 best_i = i;
330 best_inv = false;
331 }
332 }
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700333 if (best_s < score) {
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200334 static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800335 unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200336 if (1 <= which && which <= 7) {
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700337 score = best_s;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200338 *inverse = best_inv;
339 best_c = left[which - 1];
340 }
341 }
342 }
343
Behdad Esfahbod8caf5dc2012-05-13 17:10:18 +0200344 /* Find best quadrant */
345 if (1) {
346 unsigned int q = 0;
347 unsigned int qs = 0;
348 for (unsigned int i = 0; i < 2; i++)
349 for (unsigned int j = 0; j < 2; j++)
350 if (quad[i][j] > quad_i[i][j]) {
351 q += 1 << (2 * i + j);
352 qs += quad_i[i][j];
353 } else
354 qs += quad[i][j];
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700355 if (qs < score) {
Behdad Esfahboddbdbfe32017-10-15 12:11:08 +0200356 const char *c = nullptr;
Behdad Esfahbod8caf5dc2012-05-13 17:10:18 +0200357 bool inv = false;
358 switch (q) {
359 case 1: c = "▟"; inv = true; break;
360 case 2: c = "▙"; inv = true; break;
361 case 4: c = "▖"; inv = false; break;
362 case 8: c = "▗"; inv = false; break;
363 case 9: c = "▚"; inv = false; break;
364 case 6: c = "▞"; inv = false; break;
365 case 7: c = "▜"; inv = true; break;
366 case 11: c = "▜"; inv = true; break;
367 case 13: c = "▙"; inv = true; break;
368 case 14: c = "▟"; inv = true; break;
369 }
370 if (c) {
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700371 score = qs;
Behdad Esfahbod8caf5dc2012-05-13 17:10:18 +0200372 *inverse = inv;
373 best_c = c;
374 }
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200375 }
376 }
377
378 return best_c;
379}
380
381void
382ansi_print_image_rgb24 (const uint32_t *data,
383 unsigned int width,
384 unsigned int height,
385 unsigned int stride)
386{
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200387 image_t image (width, height, data, stride);
388
389 unsigned int rows = (height + CELL_H - 1) / CELL_H;
Behdad Esfahbod8caf5dc2012-05-13 17:10:18 +0200390 unsigned int cols = (width + CELL_W - 1) / CELL_W;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200391 image_t cell (CELL_W, CELL_H);
392 biimage_t bi (CELL_W, CELL_H);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200393 unsigned int last_bg = -1, last_fg = -1;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200394 for (unsigned int row = 0; row < rows; row++) {
395 for (unsigned int col = 0; col < cols; col++) {
396 image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
397 bi.set (cell);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200398 if (bi.unicolor) {
399 if (last_bg != bi.bg) {
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800400 printf ("%c[%dm", ESC_E, 40 + bi.bg);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200401 last_bg = bi.bg;
402 }
403 printf (" ");
404 } else {
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200405 /* Figure out the closest character to the biimage */
Behdad Esfahbod8caf5dc2012-05-13 17:10:18 +0200406 bool inverse = false;
Behdad Esfahbodff0f2102015-05-18 14:16:28 -0700407 const char *c = block_best (bi, &inverse);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200408 if (inverse) {
409 if (last_bg != bi.fg || last_fg != bi.bg) {
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800410 printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200411 last_bg = bi.fg;
412 last_fg = bi.bg;
413 }
414 } else {
415 if (last_bg != bi.bg || last_fg != bi.fg) {
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800416 printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg);
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200417 last_bg = bi.bg;
418 last_fg = bi.fg;
419 }
420 }
421 printf ("%s", c);
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200422 }
423 }
Chun-wei Fan998e8dd2015-11-02 16:55:29 +0800424 printf ("%c[0m\n", ESC_E); /* Reset */
Behdad Esfahboddb0de7c2012-05-13 13:02:38 +0200425 last_bg = last_fg = -1;
Behdad Esfahbod52e7b142012-05-13 02:02:58 +0200426 }
427}