873bee80925ed07bc447f216cf2b2ff4e7cc3739
[platform/upstream/harfbuzz.git] / util / ansi-print.cc
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 #define MIN(a,b) ((a) < (b) ? (a) : (b))
45
46 #define CELL_W 8
47 #define CELL_H (2 * CELL_W)
48
49 struct color_diff_t
50 {
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]; }
53
54   int v[4];
55 };
56
57 struct color_t
58 {
59   static color_t from_ansi (unsigned int x)
60   {
61     color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))};
62     return c;
63   }
64   unsigned int to_ansi (void)
65   {
66     return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
67   }
68
69   color_diff_t diff (const color_t &o)
70   {
71     color_diff_t d;
72     for (unsigned int i = 0; i < 4; i++)
73       d.v[i] = (int) ((v >> (i*8))&0xFF) - (int) ((o.v >> (i*8))&0xFF);
74     return d;
75   }
76
77   uint32_t v;
78 };
79
80 struct image_t
81 {
82   public:
83
84   image_t (unsigned int width_,
85            unsigned int height_,
86            const uint32_t *data_,
87            unsigned int stride_) :
88                 width (width_),
89                 height (height_),
90                 own_data (false),
91                 data ((color_t *) data_),
92                 stride (stride_) {}
93   image_t (unsigned int width_,
94            unsigned int height_) :
95                 width (width_),
96                 height (height_),
97                 own_data (true),
98                 data ((color_t *) malloc (sizeof (data[0]) * width * height)),
99                 stride (width) {}
100   ~image_t (void)
101   { if (own_data) free (data); }
102
103   color_t &operator () (unsigned int x, unsigned int y)
104   { return data[x + y * stride]; }
105
106   color_t operator () (unsigned int x, unsigned int y) const
107   { return data[x + y * stride]; }
108
109   void
110   copy_sub_image (const image_t &s,
111                   unsigned int x, unsigned int y,
112                   unsigned int w, unsigned int h)
113   {
114     assert (x < width);
115     assert (y < height);
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;
119       if (x + w <= width)
120         for (unsigned int col = 0; col < w; col++)
121           *q++ = *p++;
122       else {
123         unsigned int limit = width - x;
124         for (unsigned int col = 0; col < limit; col++)
125           *q++ = *p++;
126         p--;
127         for (unsigned int col = limit; col < w; col++)
128           *q++ = *p;
129       }
130     }
131   }
132
133   const unsigned int width;
134   const unsigned int height;
135
136   private:
137   bool own_data;
138   color_t * const data;
139   const unsigned int stride;
140 };
141
142 struct biimage_t
143 {
144   public:
145
146   biimage_t (unsigned int width, unsigned int height) :
147                 width (width),
148                 height (height),
149                 data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
150   ~biimage_t (void)
151   { free (data); }
152
153   void set (const image_t &image)
154   {
155     assert (image.width == width);
156     assert (image.height == height);
157     int freq[8] = {0};
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 ()]++;
162       }
163     bg = 0;
164     for (unsigned int i = 1; i < 8; i++)
165       if (freq[bg] < freq[i])
166         bg = i;
167     fg = 0;
168     for (unsigned int i = 1; i < 8; i++)
169       if (i != bg && freq[fg] < freq[i])
170         fg = i;
171     if (fg == bg || freq[fg] == 0) {
172       fg = bg;
173       unicolor = true;
174     }
175     else
176       unicolor = false;
177
178     /* Set the data... */
179
180     if (unicolor) {
181       memset (data, 0, sizeof (data[0]) * width * height);
182       return;
183     }
184
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);
193       }
194   }
195
196   uint8_t &operator () (unsigned int x, unsigned int y)
197   { return data[x + y * width]; }
198
199   uint8_t operator () (unsigned int x, unsigned int y) const
200   { return data[x + y * width]; }
201
202   const unsigned int width;
203   const unsigned int height;
204   unsigned int bg;
205   unsigned int fg;
206   bool unicolor;
207
208   private:
209   uint8_t * const data;
210 };
211
212 const char *
213 block_best (const biimage_t &bi, unsigned int *score, bool *inverse)
214 {
215   assert (bi.width  <= CELL_W);
216   assert (bi.height <= CELL_H);
217
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;
230       row_sum[y] += c;
231       row_sum_i[y] += c_i;
232       col_sum[x] += c;
233       col_sum_i[x] += c_i;
234       quad[2 * y / bi.height][2 * x / bi.width] += c;
235       quad_i[2 * y / bi.height][2 * x / bi.width] += c_i;
236       total += c;
237       total_i += c_i;
238     }
239
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];
244   }
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];
248   }
249
250   const char *best_c = " ";
251
252   /* Maybe empty is better! */
253   if (total < *score) {
254     *score = total;
255     *inverse = false;
256     best_c = " ";
257   }
258   /* Maybe full is better! */
259   if (total_i < *score) {
260     *score = total_i;
261     *inverse = true;
262     best_c = " ";
263   }
264
265   /* Find best lower line */
266   if (1) {
267     unsigned int best_s = (unsigned int) -1;
268     bool best_inv = false;
269     int best_i = 0;
270     for (unsigned int i = 0; i < bi.height - 1; i++)
271     {
272       unsigned int s;
273       s = row_sum[i] + total_i - row_sum_i[i];
274       if (s < best_s) {
275         best_s = s;
276         best_i = i;
277         best_inv = false;
278       }
279       s = row_sum_i[i] + total - row_sum[i];
280       if (s < best_s) {
281         best_s = s;
282         best_i = i;
283         best_inv = true;
284       }
285     }
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) {
290         *score = best_s;
291         *inverse = best_inv;
292         best_c = lower[7 - which];
293       }
294     }
295   }
296
297   /* Find best left line */
298   if (1) {
299     unsigned int best_s = (unsigned int) -1;
300     bool best_inv = false;
301     int best_i = 0;
302     for (unsigned int i = 0; i < bi.width - 1; i++)
303     {
304       unsigned int s;
305       s = col_sum[i] + total_i - col_sum_i[i];
306       if (s < best_s) {
307         best_s = s;
308         best_i = i;
309         best_inv = true;
310       }
311       s = col_sum_i[i] + total - col_sum[i];
312       if (s < best_s) {
313         best_s = s;
314         best_i = i;
315         best_inv = false;
316       }
317     }
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) {
322         *score = best_s;
323         *inverse = best_inv;
324         best_c = left[which - 1];
325       }
326     }
327   }
328
329   /* Find best quadrant */
330   if (1) {
331     unsigned int q = 0;
332     unsigned int qs = 0;
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);
337           qs += quad_i[i][j];
338         } else
339           qs += quad[i][j];
340     if (qs < *score) {
341       const char *c = NULL;
342       bool inv = false;
343       switch (q) {
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;
354       }
355       if (c) {
356         *score = qs;
357         *inverse = inv;
358         best_c = c;
359       }
360     }
361   }
362
363   return best_c;
364 }
365
366 void
367 ansi_print_image_rgb24 (const uint32_t *data,
368                         unsigned int width,
369                         unsigned int height,
370                         unsigned int stride)
371 {
372   image_t image (width, height, data, stride);
373
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);
382       bi.set (cell);
383       if (bi.unicolor) {
384         if (last_bg != bi.bg) {
385           printf ("\e[%dm", 40 + bi.bg);
386           last_bg = bi.bg;
387         }
388         printf (" ");
389       } else {
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);
394         if (inverse) {
395           if (last_bg != bi.fg || last_fg != bi.bg) {
396             printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
397             last_bg = bi.fg;
398             last_fg = bi.bg;
399           }
400         } else {
401           if (last_bg != bi.bg || last_fg != bi.fg) {
402             printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
403             last_bg = bi.bg;
404             last_fg = bi.fg;
405           }
406         }
407         printf ("%s", c);
408       }
409     }
410     printf ("\e[0m\n"); /* Reset */
411     last_bg = last_fg = -1;
412   }
413 }