• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
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
43 
44 #if defined (_MSC_VER) && (_MSC_VER < 1800)
45 static inline long int
lround(double x)46 lround (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 
55 #define ESC_E (char)27
56 
57 #define MIN(a,b) ((a) < (b) ? (a) : (b))
58 
59 #define CELL_W 8
60 #define CELL_H (2 * CELL_W)
61 
62 struct color_diff_t
63 {
dotcolor_diff_t64   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 
70 struct color_t
71 {
from_ansicolor_t72   static color_t from_ansi (unsigned int x)
73   {
74     color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))};
75     return c;
76   }
to_ansicolor_t77   unsigned int to_ansi (void)
78   {
79     return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
80   }
81 
diffcolor_t82   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 
93 struct image_t
94 {
95   public:
96 
image_timage_t97   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_) {}
image_timage_t106   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) {}
~image_timage_t113   ~image_t (void)
114   { if (own_data) free (data); }
115 
operator ()image_t116   color_t &operator () (unsigned int x, unsigned int y)
117   { return data[x + y * stride]; }
118 
operator ()image_t119   color_t operator () (unsigned int x, unsigned int y) const
120   { return data[x + y * stride]; }
121 
122   void
copy_sub_imageimage_t123   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 
155 struct biimage_t
156 {
157   public:
158 
biimage_tbiimage_t159   biimage_t (unsigned int width, unsigned int height) :
160 		width (width),
161 		height (height),
162 		bg (0), fg (0), unicolor (true),
163 		data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
~biimage_tbiimage_t164   ~biimage_t (void)
165   { free (data); }
166 
setbiimage_t167   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 
operator ()biimage_t210   uint8_t &operator () (unsigned int x, unsigned int y)
211   { return data[x + y * width]; }
212 
operator ()biimage_t213   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 
226 const char *
block_best(const biimage_t & bi,bool * inverse)227 block_best (const biimage_t &bi, bool *inverse)
228 {
229   assert (bi.width  <= CELL_W);
230   assert (bi.height <= CELL_H);
231 
232   unsigned int score = (unsigned int) -1;
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;
245       row_sum[y] += c;
246       row_sum_i[y] += c_i;
247       col_sum[x] += c;
248       col_sum_i[x] += c_i;
249       quad[2 * y / bi.height][2 * x / bi.width] += c;
250       quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
251       total += c;
252       total_i += c_i;
253     }
254 
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 
265   const char *best_c = " ";
266 
267   /* Maybe empty is better! */
268   if (total < score) {
269     score = total;
270     *inverse = false;
271     best_c = " ";
272   }
273   /* Maybe full is better! */
274   if (total_i < score) {
275     score = total_i;
276     *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     }
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) {
305 	score = best_s;
306 	*inverse = best_inv;
307 	best_c = lower[7 - which];
308       }
309     }
310   }
311 
312   /* Find best left line */
313   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     }
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) {
337 	score = best_s;
338 	*inverse = best_inv;
339 	best_c = left[which - 1];
340       }
341     }
342   }
343 
344   /* 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];
355     if (qs < score) {
356       const char *c = NULL;
357       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) {
371 	score = qs;
372 	*inverse = inv;
373 	best_c = c;
374       }
375     }
376   }
377 
378   return best_c;
379 }
380 
381 void
ansi_print_image_rgb24(const uint32_t * data,unsigned int width,unsigned int height,unsigned int stride)382 ansi_print_image_rgb24 (const uint32_t *data,
383 			unsigned int width,
384 			unsigned int height,
385 			unsigned int stride)
386 {
387   image_t image (width, height, data, stride);
388 
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);
397       bi.set (cell);
398       if (bi.unicolor) {
399         if (last_bg != bi.bg) {
400 	  printf ("%c[%dm", ESC_E, 40 + bi.bg);
401 	  last_bg = bi.bg;
402 	}
403 	printf (" ");
404       } else {
405         /* Figure out the closest character to the biimage */
406 	bool inverse = false;
407         const char *c = block_best (bi, &inverse);
408 	if (inverse) {
409 	  if (last_bg != bi.fg || last_fg != bi.bg) {
410 	    printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg);
411 	    last_bg = bi.fg;
412 	    last_fg = bi.bg;
413 	  }
414 	} else {
415 	  if (last_bg != bi.bg || last_fg != bi.fg) {
416 	    printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg);
417 	    last_bg = bi.bg;
418 	    last_fg = bi.fg;
419 	  }
420 	}
421 	printf ("%s", c);
422       }
423     }
424     printf ("%c[0m\n", ESC_E); /* Reset */
425     last_bg = last_fg = -1;
426   }
427 }
428