2 * Copyright © 2012 Google, Inc.
4 * This is part of HarfBuzz, a text shaping library.
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.
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
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.
24 * Google Author(s): Behdad Esfahbod
31 #include "ansi-print.hh"
41 #include <unistd.h> /* for isatty() */
44 #if defined (_MSC_VER) && (_MSC_VER < 1800)
45 static inline long int
49 return floor (x + 0.5);
51 return ceil (x - 0.5);
55 #define ESC_E (char)27
57 #define MIN(a,b) ((a) < (b) ? (a) : (b))
60 #define CELL_H (2 * CELL_W)
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]; }
72 static color_t from_ansi (unsigned int x)
74 color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
77 unsigned int to_ansi ()
79 return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
82 color_diff_t diff (const color_t &o)
85 for (unsigned int i = 0; i < 4; i++)
86 d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
97 image_t (unsigned int width_,
99 const uint32_t *data_,
100 unsigned int stride_) :
104 data ((color_t *) data_),
106 image_t (unsigned int width_,
107 unsigned int height_) :
111 data ((color_t *) malloc (sizeof (data[0]) * width * height)),
114 { if (own_data) free (data); }
116 color_t &operator () (unsigned int x, unsigned int y)
117 { return data[x + y * stride]; }
119 color_t operator () (unsigned int x, unsigned int y) const
120 { return data[x + y * stride]; }
123 copy_sub_image (const image_t &s,
124 unsigned int x, unsigned int y,
125 unsigned int w, unsigned int h)
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;
133 for (unsigned int col = 0; col < w; col++)
136 unsigned int limit = width - x;
137 for (unsigned int col = 0; col < limit; col++)
140 for (unsigned int col = limit; col < w; col++)
146 const unsigned int width;
147 const unsigned int height;
151 color_t * const data;
152 const unsigned int stride;
159 biimage_t (unsigned int width, unsigned int height) :
162 bg (0), fg (0), unicolor (true),
163 data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
167 void set (const image_t &image)
169 assert (image.width == width);
170 assert (image.height == height);
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 ()]++;
178 for (unsigned int i = 1; i < 8; i++)
179 if (freq[bg] < freq[i])
182 for (unsigned int i = 1; i < 8; i++)
183 if (i != bg && freq[fg] < freq[i])
185 if (fg == bg || freq[fg] == 0) {
192 /* Set the data... */
195 memset (data, 0, sizeof (data[0]) * width * height);
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);
210 uint8_t &operator () (unsigned int x, unsigned int y)
211 { return data[x + y * width]; }
213 uint8_t operator () (unsigned int x, unsigned int y) const
214 { return data[x + y * width]; }
216 const unsigned int width;
217 const unsigned int height;
223 uint8_t * const data;
227 block_best (const biimage_t &bi, bool *inverse)
229 assert (bi.width <= CELL_W);
230 assert (bi.height <= CELL_H);
232 unsigned int score = UINT_MAX;
233 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};
237 unsigned int quad[2][2] = {{0}};
238 unsigned int quad_i[2][2] = {{0}};
239 unsigned int total = 0;
240 unsigned int total_i = 0;
241 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);
244 unsigned int c_i = 255 - c;
249 quad[2 * y / bi.height][2 * x / bi.width] += c;
250 quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
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];
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];
265 const char *best_c = " ";
267 /* Maybe empty is better! */
273 /* Maybe full is better! */
274 if (total_i < score) {
280 /* Find best lower line */
282 unsigned int best_s = UINT_MAX;
283 bool best_inv = false;
285 for (unsigned int i = 0; i < bi.height - 1; i++)
288 s = row_sum[i] + total_i - row_sum_i[i];
294 s = row_sum_i[i] + total - row_sum[i];
301 if (best_s < score) {
302 static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
303 unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
304 if (1 <= which && which <= 7) {
307 best_c = lower[7 - which];
312 /* Find best left line */
314 unsigned int best_s = UINT_MAX;
315 bool best_inv = false;
317 for (unsigned int i = 0; i < bi.width - 1; i++)
320 s = col_sum[i] + total_i - col_sum_i[i];
326 s = col_sum_i[i] + total - col_sum[i];
333 if (best_s < score) {
334 static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
335 unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
336 if (1 <= which && which <= 7) {
339 best_c = left[which - 1];
344 /* Find best quadrant */
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);
356 const char *c = nullptr;
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;
382 ansi_print_image_rgb24 (const uint32_t *data,
387 image_t image (width, height, data, stride);
389 unsigned int rows = (height + CELL_H - 1) / CELL_H;
390 unsigned int cols = (width + CELL_W - 1) / CELL_W;
391 image_t cell (CELL_W, CELL_H);
392 biimage_t bi (CELL_W, CELL_H);
393 unsigned int last_bg = -1, last_fg = -1;
394 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);
399 if (last_bg != bi.bg) {
400 printf ("%c[%dm", ESC_E, 40 + bi.bg);
405 /* Figure out the closest character to the biimage */
406 bool inverse = false;
407 const char *c = block_best (bi, &inverse);
409 if (last_bg != bi.fg || last_fg != bi.bg) {
410 printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg);
415 if (last_bg != bi.bg || last_fg != bi.fg) {
416 printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg);
424 printf ("%c[0m\n", ESC_E); /* Reset */
425 last_bg = last_fg = -1;