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() */
45 static inline long int
49 return floor (x + 0.5);
51 return ceil (x - 0.5);
55 #define MIN(a,b) ((a) < (b) ? (a) : (b))
58 #define CELL_H (2 * CELL_W)
62 int dot (const color_diff_t &o)
63 { return v[0]*o.v[0] + v[1]*o.v[1] + v[2]*o.v[2] + v[3]*o.v[3]; }
70 static color_t from_ansi (unsigned int x)
72 color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))};
75 unsigned int to_ansi (void)
77 return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
80 color_diff_t diff (const color_t &o)
83 for (unsigned int i = 0; i < 4; i++)
84 d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
95 image_t (unsigned int width_,
97 const uint32_t *data_,
98 unsigned int stride_) :
102 data ((color_t *) data_),
104 image_t (unsigned int width_,
105 unsigned int height_) :
109 data ((color_t *) malloc (sizeof (data[0]) * width * height)),
112 { if (own_data) free (data); }
114 color_t &operator () (unsigned int x, unsigned int y)
115 { return data[x + y * stride]; }
117 color_t operator () (unsigned int x, unsigned int y) const
118 { return data[x + y * stride]; }
121 copy_sub_image (const image_t &s,
122 unsigned int x, unsigned int y,
123 unsigned int w, unsigned int h)
127 for (unsigned int row = 0; row < h; row++) {
128 color_t *p = data + x + MIN (y + row, height - 1) * stride;
129 color_t *q = s.data + row * s.stride;
131 for (unsigned int col = 0; col < w; col++)
134 unsigned int limit = width - x;
135 for (unsigned int col = 0; col < limit; col++)
138 for (unsigned int col = limit; col < w; col++)
144 const unsigned int width;
145 const unsigned int height;
149 color_t * const data;
150 const unsigned int stride;
157 biimage_t (unsigned int width, unsigned int height) :
160 bg (0), fg (0), unicolor (true),
161 data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
165 void set (const image_t &image)
167 assert (image.width == width);
168 assert (image.height == height);
170 for (unsigned int y = 0; y < height; y++)
171 for (unsigned int x = 0; x < width; x++) {
172 color_t c = image (x, y);
173 freq[c.to_ansi ()]++;
176 for (unsigned int i = 1; i < 8; i++)
177 if (freq[bg] < freq[i])
180 for (unsigned int i = 1; i < 8; i++)
181 if (i != bg && freq[fg] < freq[i])
183 if (fg == bg || freq[fg] == 0) {
190 /* Set the data... */
193 memset (data, 0, sizeof (data[0]) * width * height);
197 color_t bgc = color_t::from_ansi (bg);
198 color_t fgc = color_t::from_ansi (fg);
199 color_diff_t diff = fgc.diff (bgc);
200 int dd = diff.dot (diff);
201 for (unsigned int y = 0; y < height; y++)
202 for (unsigned int x = 0; x < width; x++) {
203 int d = diff.dot (image (x, y).diff (bgc));
204 (*this)(x, y) = d < 0 ? 0 : d > dd ? 255 : lround (d * 255. / dd);
208 uint8_t &operator () (unsigned int x, unsigned int y)
209 { return data[x + y * width]; }
211 uint8_t operator () (unsigned int x, unsigned int y) const
212 { return data[x + y * width]; }
214 const unsigned int width;
215 const unsigned int height;
221 uint8_t * const data;
225 block_best (const biimage_t &bi, unsigned int *score, bool *inverse)
227 assert (bi.width <= CELL_W);
228 assert (bi.height <= CELL_H);
230 unsigned int row_sum[CELL_H] = {0};
231 unsigned int col_sum[CELL_W] = {0};
232 unsigned int row_sum_i[CELL_H] = {0};
233 unsigned int col_sum_i[CELL_W] = {0};
234 unsigned int quad[2][2] = {{0}};
235 unsigned int quad_i[2][2] = {{0}};
236 unsigned int total = 0;
237 unsigned int total_i = 0;
238 for (unsigned int y = 0; y < bi.height; y++)
239 for (unsigned int x = 0; x < bi.width; x++) {
240 unsigned int c = bi (x, y);
241 unsigned int c_i = 255 - c;
246 quad[2 * y / bi.height][2 * x / bi.width] += c;
247 quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
252 /* Make the sums cummulative */
253 for (unsigned int i = 1; i < bi.height; i++) {
254 row_sum[i] += row_sum[i - 1];
255 row_sum_i[i] += row_sum_i[i - 1];
257 for (unsigned int i = 1; i < bi.width; i++) {
258 col_sum[i] += col_sum[i - 1];
259 col_sum_i[i] += col_sum_i[i - 1];
262 const char *best_c = " ";
264 /* Maybe empty is better! */
265 if (total < *score) {
270 /* Maybe full is better! */
271 if (total_i < *score) {
277 /* Find best lower line */
279 unsigned int best_s = (unsigned int) -1;
280 bool best_inv = false;
282 for (unsigned int i = 0; i < bi.height - 1; i++)
285 s = row_sum[i] + total_i - row_sum_i[i];
291 s = row_sum_i[i] + total - row_sum[i];
298 if (best_s < *score) {
299 static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
300 unsigned int which = lround (((best_i + 1) * 8) / bi.height);
301 if (1 <= which && which <= 7) {
304 best_c = lower[7 - which];
309 /* Find best left line */
311 unsigned int best_s = (unsigned int) -1;
312 bool best_inv = false;
314 for (unsigned int i = 0; i < bi.width - 1; i++)
317 s = col_sum[i] + total_i - col_sum_i[i];
323 s = col_sum_i[i] + total - col_sum[i];
330 if (best_s < *score) {
331 static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
332 unsigned int which = lround (((best_i + 1) * 8) / bi.width);
333 if (1 <= which && which <= 7) {
336 best_c = left[which - 1];
341 /* Find best quadrant */
345 for (unsigned int i = 0; i < 2; i++)
346 for (unsigned int j = 0; j < 2; j++)
347 if (quad[i][j] > quad_i[i][j]) {
348 q += 1 << (2 * i + j);
353 const char *c = NULL;
356 case 1: c = "▟"; inv = true; break;
357 case 2: c = "▙"; inv = true; break;
358 case 4: c = "▖"; inv = false; break;
359 case 8: c = "▗"; inv = false; break;
360 case 9: c = "▚"; inv = false; break;
361 case 6: c = "▞"; inv = false; break;
362 case 7: c = "▜"; inv = true; break;
363 case 11: c = "▜"; inv = true; break;
364 case 13: c = "▙"; inv = true; break;
365 case 14: c = "▟"; inv = true; break;
379 ansi_print_image_rgb24 (const uint32_t *data,
384 image_t image (width, height, data, stride);
386 unsigned int rows = (height + CELL_H - 1) / CELL_H;
387 unsigned int cols = (width + CELL_W - 1) / CELL_W;
388 image_t cell (CELL_W, CELL_H);
389 biimage_t bi (CELL_W, CELL_H);
390 unsigned int last_bg = -1, last_fg = -1;
391 for (unsigned int row = 0; row < rows; row++) {
392 for (unsigned int col = 0; col < cols; col++) {
393 image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
396 if (last_bg != bi.bg) {
397 printf ("\e[%dm", 40 + bi.bg);
402 /* Figure out the closest character to the biimage */
403 unsigned int score = (unsigned int) -1;
404 bool inverse = false;
405 const char *c = block_best (bi, &score, &inverse);
407 if (last_bg != bi.fg || last_fg != bi.bg) {
408 printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
413 if (last_bg != bi.bg || last_fg != bi.fg) {
414 printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
422 printf ("\e[0m\n"); /* Reset */
423 last_bg = last_fg = -1;