#include <unistd.h> /* for isatty() */
#endif
-#ifdef _MSC_VER
+#if defined (_MSC_VER) && (_MSC_VER < 1800)
static inline long int
lround (double x)
{
}
#endif
+#define ESC_E (char)27
+
#define MIN(a,b) ((a) < (b) ? (a) : (b))
#define CELL_W 8
{
static color_t from_ansi (unsigned int x)
{
- color_t c = {(0xFF<<24) | ((0xFF*(x&1))<<16) | ((0xFF*((x >> 1)&1))<<8) | (0xFF*((x >> 2)&1))};
+ color_t c = {(0xFFu<<24) | ((0xFFu*(x&1))<<16) | ((0xFFu*((x >> 1)&1))<<8) | (0xFFu*((x >> 2)&1))};
return c;
}
- unsigned int to_ansi (void)
+ unsigned int to_ansi ()
{
return ((v >> 23) & 1) | ((v >> 14)&2) | ((v >> 5)&4);
}
own_data (true),
data ((color_t *) malloc (sizeof (data[0]) * width * height)),
stride (width) {}
- ~image_t (void)
+ ~image_t ()
{ if (own_data) free (data); }
color_t &operator () (unsigned int x, unsigned int y)
for (unsigned int col = 0; col < w; col++)
*q++ = *p++;
else {
- unsigned int limit = width - x;
+ unsigned int limit = width - x;
for (unsigned int col = 0; col < limit; col++)
*q++ = *p++;
p--;
height (height),
bg (0), fg (0), unicolor (true),
data ((uint8_t *) malloc (sizeof (data[0]) * width * height)) {}
- ~biimage_t (void)
+ ~biimage_t ()
{ free (data); }
void set (const image_t &image)
int freq[8] = {0};
for (unsigned int y = 0; y < height; y++)
for (unsigned int x = 0; x < width; x++) {
- color_t c = image (x, y);
- freq[c.to_ansi ()]++;
+ color_t c = image (x, y);
+ freq[c.to_ansi ()]++;
}
bg = 0;
for (unsigned int i = 1; i < 8; i++)
if (freq[bg] < freq[i])
- bg = i;
+ bg = i;
fg = 0;
for (unsigned int i = 1; i < 8; i++)
if (i != bg && freq[fg] < freq[i])
- fg = i;
+ fg = i;
if (fg == bg || freq[fg] == 0) {
fg = bg;
unicolor = true;
int dd = diff.dot (diff);
for (unsigned int y = 0; y < height; y++)
for (unsigned int x = 0; x < width; x++) {
- int d = diff.dot (image (x, y).diff (bgc));
+ int d = diff.dot (image (x, y).diff (bgc));
(*this)(x, y) = d < 0 ? 0 : d > dd ? 255 : lround (d * 255. / dd);
}
}
uint8_t * const data;
};
-const char *
-block_best (const biimage_t &bi, unsigned int *score, bool *inverse)
+static const char *
+block_best (const biimage_t &bi, bool *inverse)
{
assert (bi.width <= CELL_W);
assert (bi.height <= CELL_H);
+ unsigned int score = UINT_MAX;
unsigned int row_sum[CELL_H] = {0};
unsigned int col_sum[CELL_W] = {0};
unsigned int row_sum_i[CELL_H] = {0};
const char *best_c = " ";
/* Maybe empty is better! */
- if (total < *score) {
- *score = total;
+ if (total < score) {
+ score = total;
*inverse = false;
best_c = " ";
}
/* Maybe full is better! */
- if (total_i < *score) {
- *score = total_i;
+ if (total_i < score) {
+ score = total_i;
*inverse = true;
best_c = " ";
}
/* Find best lower line */
if (1) {
- unsigned int best_s = (unsigned int) -1;
+ unsigned int best_s = UINT_MAX;
bool best_inv = false;
int best_i = 0;
for (unsigned int i = 0; i < bi.height - 1; i++)
unsigned int s;
s = row_sum[i] + total_i - row_sum_i[i];
if (s < best_s) {
- best_s = s;
+ best_s = s;
best_i = i;
best_inv = false;
}
s = row_sum_i[i] + total - row_sum[i];
if (s < best_s) {
- best_s = s;
+ best_s = s;
best_i = i;
best_inv = true;
}
}
- if (best_s < *score) {
+ if (best_s < score) {
static const char *lower[7] = {"▁", "▂", "▃", "▄", "▅", "▆", "▇"};
- unsigned int which = lround (((best_i + 1) * 8) / bi.height);
+ unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.height);
if (1 <= which && which <= 7) {
- *score = best_s;
+ score = best_s;
*inverse = best_inv;
best_c = lower[7 - which];
}
/* Find best left line */
if (1) {
- unsigned int best_s = (unsigned int) -1;
+ unsigned int best_s = UINT_MAX;
bool best_inv = false;
int best_i = 0;
for (unsigned int i = 0; i < bi.width - 1; i++)
unsigned int s;
s = col_sum[i] + total_i - col_sum_i[i];
if (s < best_s) {
- best_s = s;
+ best_s = s;
best_i = i;
best_inv = true;
}
s = col_sum_i[i] + total - col_sum[i];
if (s < best_s) {
- best_s = s;
+ best_s = s;
best_i = i;
best_inv = false;
}
}
- if (best_s < *score) {
+ if (best_s < score) {
static const char *left [7] = {"▏", "▎", "▍", "▌", "▋", "▊", "▉"};
- unsigned int which = lround (((best_i + 1) * 8) / bi.width);
+ unsigned int which = lround ((double) ((best_i + 1) * 8) / bi.width);
if (1 <= which && which <= 7) {
- *score = best_s;
+ score = best_s;
*inverse = best_inv;
best_c = left[which - 1];
}
qs += quad_i[i][j];
} else
qs += quad[i][j];
- if (qs < *score) {
- const char *c = NULL;
+ if (qs < score) {
+ const char *c = nullptr;
bool inv = false;
switch (q) {
case 1: c = "▟"; inv = true; break;
case 14: c = "▟"; inv = true; break;
}
if (c) {
- *score = qs;
+ score = qs;
*inverse = inv;
best_c = c;
}
image.copy_sub_image (cell, col * CELL_W, row * CELL_H, CELL_W, CELL_H);
bi.set (cell);
if (bi.unicolor) {
- if (last_bg != bi.bg) {
- printf ("\e[%dm", 40 + bi.bg);
+ if (last_bg != bi.bg) {
+ printf ("%c[%dm", ESC_E, 40 + bi.bg);
last_bg = bi.bg;
}
printf (" ");
} else {
- /* Figure out the closest character to the biimage */
- unsigned int score = (unsigned int) -1;
+ /* Figure out the closest character to the biimage */
bool inverse = false;
- const char *c = block_best (bi, &score, &inverse);
+ const char *c = block_best (bi, &inverse);
if (inverse) {
if (last_bg != bi.fg || last_fg != bi.bg) {
- printf ("\e[%d;%dm", 30 + bi.bg, 40 + bi.fg);
+ printf ("%c[%d;%dm", ESC_E, 30 + bi.bg, 40 + bi.fg);
last_bg = bi.fg;
last_fg = bi.bg;
}
} else {
if (last_bg != bi.bg || last_fg != bi.fg) {
- printf ("\e[%d;%dm", 40 + bi.bg, 30 + bi.fg);
+ printf ("%c[%d;%dm", ESC_E, 40 + bi.bg, 30 + bi.fg);
last_bg = bi.bg;
last_fg = bi.fg;
}
printf ("%s", c);
}
}
- printf ("\e[0m\n"); /* Reset */
+ printf ("%c[0m\n", ESC_E); /* Reset */
last_bg = last_fg = -1;
}
}