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 #define MIN(a,b) ((a) < (b) ? (a) : (b))
47 #define CELL_H (2 * CELL_W)
51 int dot (const color_diff_t &o)
52 { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
59 static color_t from_ansi (unsigned int x)
61 color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))};
64 unsigned int to_ansi (void)
66 return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
69 color_diff_t diff (const color_t &o)
72 for (unsigned int i = 0; i < 4; i++)
73 d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
84 image_t (unsigned int width_,
86 const uint32_t *data_,
87 unsigned int stride_) :
91 data ((color_t *) data_),
93 image_t (unsigned int width_,
94 unsigned int height_) :
98 data ((color_t *) malloc (sizeof (data[0]) * width * height)),
101 { if (own_data) free (data); }
103 color_t &operator () (unsigned int x, unsigned int y)
104 { return data[x + y * stride]; }
106 color_t operator () (unsigned int x, unsigned int y) const
107 { return data[x + y * stride]; }
110 copy_sub_image (const image_t &s,
111 unsigned int x, unsigned int y,
112 unsigned int w, unsigned int h)
116 for (unsigned int row = 0; row < h; row++) {
117 color_t *p = data + x + MIN (y + row, height - 1) * stride;
118 color_t *q = s.data + row * s.stride;
120 for (unsigned int col = 0; col < w; col++)
123 unsigned int limit = width - x;
124 for (unsigned int col = 0; col < limit; col++)
127 for (unsigned int col = limit; col < w; col++)
133 const unsigned int width;
134 const unsigned int height;
138 color_t * const data;
139 const unsigned int stride;
146 biimage_t (unsigned int width, unsigned int height) :
149 data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
153 void set (const image_t &image)
155 assert (image.width == width);
156 assert (image.height == height);
158 for (unsigned int y = 0; y < height; y++)
159 for (unsigned int x = 0; x < width; x++) {
160 color_t c = image (x, y);
161 freq[c.to_ansi ()]++;
164 for (unsigned int i = 1; i < 8; i++)
165 if (freq[bg] < freq[i])
168 for (unsigned int i = 1; i < 8; i++)
169 if (i != bg && freq[fg] < freq[i])
171 if (fg == bg || freq[fg] == 0) {
178 /* Set the data... */
181 memset (data, 0, sizeof (data[0]) * width * height);
185 color_t bgc = color_t::from_ansi (bg);
186 color_t fgc = color_t::from_ansi (fg);
187 color_diff_t diff = fgc.diff (bgc);
188 int dd = diff.dot (diff);
189 for (unsigned int y = 0; y < height; y++)
190 for (unsigned int x = 0; x < width; x++) {
191 int d = diff.dot (image (x, y).diff (bgc));
192 (*this)(x, y) = d < 0 ? 0 : d > dd ? 255 : lround (d * 255. / dd);
196 uint8_t &operator () (unsigned int x, unsigned int y)
197 { return data[x + y * width]; }
199 uint8_t operator () (unsigned int x, unsigned int y) const
200 { return data[x + y * width]; }
202 const unsigned int width;
203 const unsigned int height;
209 uint8_t * const data;
213 block_best (const biimage_t &bi, unsigned int *score, bool *inverse)
215 assert (bi.width <= CELL_W);
216 assert (bi.height <= CELL_H);
218 unsigned int row_sum[CELL_H] = {0};
219 unsigned int col_sum[CELL_W] = {0};
220 unsigned int row_sum_i[CELL_H] = {0};
221 unsigned int col_sum_i[CELL_W] = {0};
222 unsigned int quad[2][2] = {{0}};
223 unsigned int quad_i[2][2] = {{0}};
224 unsigned int total = 0;
225 unsigned int total_i = 0;
226 for (unsigned int y = 0; y < bi.height; y++)
227 for (unsigned int x = 0; x < bi.width; x++) {
228 unsigned int c = bi (x, y);
229 unsigned int c_i = 255 - c;
234 quad[2 * y / bi.height][2 * x / bi.width] += c;
235 quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
240 /* Make the sums cummulative */
241 for (unsigned int i = 1; i < bi.height; i++) {
242 row_sum[i] += row_sum[i - 1];
243 row_sum_i[i] += row_sum_i[i - 1];
245 for (unsigned int i = 1; i < bi.width; i++) {
246 col_sum[i] += col_sum[i - 1];
247 col_sum_i[i] += col_sum_i[i - 1];
250 const char *best_c = " ";
252 /* Maybe empty is better! */
253 if (total < *score) {
258 /* Maybe full is better! */
259 if (total_i < *score) {
265 /* Find best lower line */
267 unsigned int best_s = (unsigned int) -1;
268 bool best_inv = false;
270 for (unsigned int i = 0; i < bi.height - 1; i++)
273 s = row_sum[i] + total_i - row_sum_i[i];
279 s = row_sum_i[i] + total - row_sum[i];
286 if (best_s < *score) {
287 static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
288 unsigned int which = lround (((best_i + 1) * 8) / bi.height);
289 if (1 <= which && which <= 7) {
292 best_c = lower[7 - which];
297 /* Find best left line */
299 unsigned int best_s = (unsigned int) -1;
300 bool best_inv = false;
302 for (unsigned int i = 0; i < bi.width - 1; i++)
305 s = col_sum[i] + total_i - col_sum_i[i];
311 s = col_sum_i[i] + total - col_sum[i];
318 if (best_s < *score) {
319 static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
320 unsigned int which = lround (((best_i + 1) * 8) / bi.width);
321 if (1 <= which && which <= 7) {
324 best_c = left[which - 1];
329 /* Find best quadrant */
333 for (unsigned int i = 0; i < 2; i++)
334 for (unsigned int j = 0; j < 2; j++)
335 if (quad[i][j] > quad_i[i][j]) {
336 q += 1 << (2 * i + j);
341 const char *c = NULL;
344 case 1: c = "▟"; inv = true; break;
345 case 2: c = "▙"; inv = true; break;
346 case 4: c = "▖"; inv = false; break;
347 case 8: c = "▗"; inv = false; break;
348 case 9: c = "▚"; inv = false; break;
349 case 6: c = "▞"; inv = false; break;
350 case 7: c = "▜"; inv = true; break;
351 case 11: c = "▜"; inv = true; break;
352 case 13: c = "▙"; inv = true; break;
353 case 14: c = "▟"; inv = true; break;
367 ansi_print_image_rgb24 (const uint32_t *data,
372 image_t image (width, height, data, stride);
374 unsigned int rows = (height + CELL_H - 1) / CELL_H;
375 unsigned int cols = (width + CELL_W - 1) / CELL_W;
376 image_t cell (CELL_W, CELL_H);
377 biimage_t bi (CELL_W, CELL_H);
378 unsigned int last_bg = -1, last_fg = -1;
379 for (unsigned int row = 0; row < rows; row++) {
380 for (unsigned int col = 0; col < cols; col++) {
381 image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
384 if (last_bg != bi.bg) {
385 printf ("\e[%dm", 40 + bi.bg);
390 /* Figure out the closest character to the biimage */
391 unsigned int score = (unsigned int) -1;
392 bool inverse = false;
393 const char *c = block_best (bi, &score, &inverse);
395 if (last_bg != bi.fg || last_fg != bi.bg) {
396 printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
401 if (last_bg != bi.bg || last_fg != bi.fg) {
402 printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
410 printf ("\e[0m\n"); /* Reset */
411 last_bg = last_fg = -1;