/****************************************************************************
- * Copyright (c) 2017 Free Software Foundation, Inc. *
+ * Copyright 2018-2022,2023 Thomas E. Dickey *
+ * Copyright 2017,2018 Free Software Foundation, Inc. *
* *
* Permission is hereby granted, free of charge, to any person obtaining a *
* copy of this software and associated documentation files (the *
* authorization. *
****************************************************************************/
/*
- * $Id: picsmap.c,v 1.90 2017/08/12 17:21:48 tom Exp $
+ * $Id: picsmap.c,v 1.149 2023/04/23 23:20:37 tom Exp $
*
* Author: Thomas E. Dickey
*
* measure the time taken to paint it normally vs randomly.
*
* TODO improve use of rgb-names using tsearch.
- * TODO when not using tsearch, dispense with intermediate index
- * TODO count/report uses of each color, giving percentiles.
*
+ * TODO add option to dump picture in non-optimized mode, e.g., like tput.
* TODO write cells/second to stderr (or log)
* TODO write picture left-to-right/top-to-bottom
* TODO write picture randomly
*/
#include <test.priv.h>
-#include <stdarg.h>
#include <sys/types.h>
#include <sys/stat.h>
#define L_CURLY '{'
#define R_CURLY '}'
+#define MaxSCALE 1000 /* input curses ranges 0..1000 */
+#define MaxRGB 255 /* output color ranges 0..255 */
#define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
-#define okRGB(n) ((n) >= 0 && (n) <= 1000)
-#define Scaled256(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
-#define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
+#define okSCALE(n) ((n) >= 0 && (n) <= MaxSCALE)
+#define Scaled256(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / 255)
+#define ScaledColor(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / scale)
+#ifndef RGB_PATH
#define RGB_PATH "/etc/X11/rgb.txt"
+#endif
-typedef int NUM_COLOR;
-
-typedef struct {
- char ch; /* nominal character to display */
- NUM_COLOR fg; /* foreground color */
-} PICS_CELL;
-
-typedef struct {
- NUM_COLOR fgcol;
- unsigned short which;
- unsigned short count;
-} FG_NODE;
-
-typedef struct {
- char *name;
- short high;
- short wide;
- int colors;
- FG_NODE **fgcol;
- PICS_CELL *cells;
-} PICS_HEAD;
-
-typedef struct {
- const char *name;
- int value;
-} RGB_NAME;
-
-typedef struct {
- short red;
- short green;
- short blue;
-} RGB_DATA;
+#include <picsmap.h>
typedef struct {
size_t file;
size_t cell;
} HOW_MUCH;
-#define stop_curses() if (in_curses) endwin()
+#undef MAX
+#define MAX(a,b) ((a)>(b)?(a):(b))
+
+/*
+ * tfind will return null on failure, so we map subscripts starting at one.
+ */
+#define P2I(n) (((int)(my_intptr_t)(n)) - 1)
+#define I2P(n) (void *)(my_intptr_t)((n) + 1)
+
+#define pause_curses() if (in_curses) stop_curses()
#define debugmsg if (debugging) logmsg
#define debugmsg2 if (debugging) logmsg2
-static void cleanup(int) GCC_NORETURN;
-static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
-static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
+static GCC_NORETURN void cleanup(int);
+static void giveup(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void logmsg(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void logmsg2(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static void warning(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
+static int gather_c_values(int);
static FILE *logfp = 0;
+static double aspect_ratio = 0.6;
static bool in_curses = FALSE;
static bool debugging = FALSE;
static bool quiet = FALSE;
static int reading_last;
static int reading_size;
-static FG_NODE **reading_ncols;
+static FG_NODE *reading_ncols;
#if HAVE_TSEARCH
static void *reading_ntree;
#endif
-#if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
+#if HAVE_ALLOC_PAIR && USE_EXTENDED_COLOR
#define USE_EXTENDED_COLORS 1
static bool use_extended_pairs = FALSE;
static bool use_extended_colors = FALSE;
#endif
static void
-logmsg(const char *fmt,...)
+logmsg(const char *fmt, ...)
{
if (logfp != 0) {
va_list ap;
}
static void
-logmsg2(const char *fmt,...)
+logmsg2(const char *fmt, ...)
{
if (logfp != 0) {
va_list ap;
static void
cleanup(int code)
{
- stop_curses();
+ pause_curses();
close_log();
ExitProgram(code);
/* NOTREACHED */
}
static void
-warning(const char *fmt,...)
+warning(const char *fmt, ...)
{
if (logfp != 0) {
va_list ap;
free_pics_head(PICS_HEAD * pics)
{
if (pics != 0) {
-#if !(HAVE_TSEARCH && HAVE_TDESTROY)
- int n;
- if (pics->fgcol != 0) {
- for (n = 0; n < pics->colors; ++n) {
- free(pics->fgcol[n]);
- }
- }
-#endif
free(pics->fgcol);
free(pics->cells);
free(pics->name);
{
reading_last = 0;
reading_size = size;
- reading_ncols = typeCalloc(FG_NODE *, size + 1);
- how_much.pair += (sizeof(FG_NODE *) * (size_t) size);
+ reading_ncols = typeCalloc(FG_NODE, size + 1);
+ how_much.pair += (sizeof(FG_NODE) * (size_t) size);
+ /* black is always the first slot, to work around P2I/I2P logic */
+ gather_c_values(0);
}
#if HAVE_TSEARCH
static int
compare_c_values(const void *p, const void *q)
{
- const FG_NODE *a = (const FG_NODE *) p;
- const FG_NODE *b = (const FG_NODE *) q;
- return (a->fgcol - b->fgcol);
+ const int a = P2I(p);
+ const int b = P2I(q);
+ return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
}
#ifdef DEBUG_TSEARCH
static int oops = 5;
FG_NODE **ft;
int n;
- if (oops-- <= 0)
- return;
for (n = 0; n < reading_last; ++n) {
- if (reading_ncols[n] == 0)
- continue;
- ft = tfind(reading_ncols[n], &reading_ntree, compare_c_values);
- if (ft != 0 && *ft != 0) {
- if ((*ft)->fgcol != reading_ncols[n]->fgcol) {
- logmsg("@%d, %d:%d (%d) %p %p fgcol %06X %06X", ln, n,
- reading_last - 1,
- reading_size,
- (*ft), reading_ncols[n],
- reading_ncols[n]->fgcol,
- (*ft)->fgcol);
- }
- if ((*ft)->which != reading_ncols[n]->which) {
- logmsg("@%d, %d:%d (%d) %p %p which %d %d", ln, n,
+ ft = tfind(I2P(n), &reading_ntree, compare_c_values);
+ if (ft != 0) {
+ int q = P2I(*ft);
+ if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
+ logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
reading_last - 1,
reading_size,
- (*ft), reading_ncols[n],
- reading_ncols[n]->which,
- (*ft)->which);
+ q, n,
+ reading_ncols[n].fgcol,
+ reading_ncols[q].fgcol);
}
} else {
- logmsg("@%d, %d:%d (%d) %p %p null %06X %d", ln, n,
+ logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
reading_last - 1,
reading_size,
- ft, reading_ncols[n],
- reading_ncols[n]->fgcol,
- reading_ncols[n]->which);
+ n,
+ reading_ncols[n].fgcol);
+ if (oops-- <= 0)
+ return;
}
}
}
#endif
#endif
-#undef MAX
-#define MAX(a,b) ((a)>(b)?(a):(b))
-
static int
gather_c_values(int fg)
{
int found = -1;
#if HAVE_TSEARCH
FG_NODE **ft;
- FG_NODE *find = typeMalloc(FG_NODE, 1);
+ int next = reading_last;
- how_much.pair += sizeof(FG_NODE);
- find->fgcol = fg;
- find->which = 0;
+ reading_ncols[next].fgcol = fg;
+ reading_ncols[next].count = 0;
check_c_values(__LINE__);
- if ((ft = tfind(find, &reading_ntree, compare_c_values)) != 0) {
- found = (*ft)->which;
+ if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
+ found = P2I(*ft);
} else {
- if (reading_last + 1 >= reading_size) {
+ if (reading_last + 2 >= reading_size) {
int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
- FG_NODE **p = typeRealloc(FG_NODE *, more, reading_ncols);
+ int last = reading_last + 1;
+ FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
if (p == 0)
goto done;
- /* FIXME - this won't reallocate pointers for tsearch */
- how_much.pair -= (sizeof(FG_NODE *) * (size_t) reading_size);
- how_much.pair += (sizeof(FG_NODE *) * (size_t) more);
reading_size = more;
reading_ncols = p;
- memset(reading_ncols + reading_last, 0,
- sizeof(FG_NODE *) * (size_t) (more - reading_last));
+ memset(reading_ncols + last, 0,
+ sizeof(FG_NODE) * (size_t) (more - last));
check_c_values(__LINE__);
}
- reading_ncols[reading_last] = find;
- find->which = (unsigned short) reading_last++;
- if ((ft = tsearch(find, &reading_ntree, compare_c_values)) != 0) {
- found = find->which;
+ ++reading_last;
+ how_much.pair += sizeof(FG_NODE);
+ if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
+ found = P2I(*ft);
+ if (found != next)
+ logmsg("OOPS expected slot %d, got %d", next, found);
+ debugmsg("allocated color #%d as #%06X", next, fg);
check_c_values(__LINE__);
}
}
int n;
for (n = 0; n < reading_last; ++n) {
- if (reading_ncols[n]->fgcol == fg) {
+ if (reading_ncols[n].fgcol == fg) {
found = n;
break;
}
}
if (found < 0) {
- FG_NODE *node = typeMalloc(FG_NODE, 1);
- how_much.pair += sizeof(FG_NODE);
if (reading_last + 2 >= reading_size) {
int more = ((reading_last + 2) * 3) / 2;
- FG_NODE **p = typeRealloc(FG_NODE *, more, reading_ncols);
+ FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
if (p == 0)
goto done;
- how_much.pair -= (sizeof(FG_NODE *) * reading_size);
- how_much.pair += (sizeof(FG_NODE *) * more);
+ how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
+ how_much.pair += (sizeof(FG_NODE) * (size_t) more);
reading_size = more;
reading_ncols = p;
memset(reading_ncols + reading_last, 0,
- sizeof(FG_NODE) * (more - reading_last));
+ sizeof(FG_NODE) * (size_t) (more - reading_last));
}
- node->fgcol = fg;
- node->which = reading_last;
- reading_ncols[reading_last] = node;
+ reading_ncols[reading_last].fgcol = fg;
found = reading_last++;
}
#endif
static void
dispose_c_values(void)
{
- int n;
#if HAVE_TSEARCH
if (reading_ntree != 0) {
-#if HAVE_TDESTROY
- tdestroy(reading_ntree, free);
-#else
+ int n;
for (n = 0; n < reading_last; ++n) {
- tdelete(reading_ncols[n], &reading_ntree, compare_c_values);
+ tdelete(I2P(n), &reading_ntree, compare_c_values);
}
-#endif
reading_ntree = 0;
}
#endif
if (reading_ncols != 0) {
- for (n = 0; n < reading_last; ++n) {
- free(reading_ncols[n]);
- }
free(reading_ncols);
reading_ncols = 0;
}
struct stat sb;
if (!quiet) {
- stop_curses();
+ pause_curses();
printf("** %s\n", filename);
}
if (is_file(filename, &sb)) {
size_t size = (size_t) sb.st_size;
- char *blob = typeMalloc(char, size + 1);
- bool had_line = TRUE;
+ char *blob = typeCalloc(char, size + 1);
bool binary = FALSE;
- unsigned j;
unsigned k = 0;
result = typeCalloc(char *, size + 1);
FILE *fp = fopen(filename, "r");
if (fp != 0) {
logmsg("opened %s", filename);
+
if (fread(blob, sizeof(char), size, fp) == size) {
+ bool had_line = TRUE;
+ unsigned j;
+
for (j = 0; (size_t) j < size; ++j) {
if (blob[j] == '\0' ||
(UChar(blob[j]) < 32 &&
- !isspace(blob[j])) ||
- (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160))
+ !isspace(UChar(blob[j]))) ||
+ (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
binary = TRUE;
+ }
if (blob[j] == '\n') {
blob[j] = '\0';
if (k && !binary) {
}
static void
-usage(void)
+usage(int ok)
{
static const char *msg[] =
{
"Usage: picsmap [options] [imagefile [...]]"
,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
,""
+ ,USAGE_COMMON
,"Options:"
- ," -d add debugging information to logfile"
- ," -l logfile write informational messages to logfile"
- ," -p palette color-palette file (default \"$TERM.dat\")"
- ," -q less verbose"
- ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
- ," -s SECS pause for SECS seconds after display vs getch"
+ ," -a ratio aspect-ratio correction for ImageMagick"
+#if HAVE_USE_DEFAULT_COLORS
+ ," -d invoke use_default_colors"
+#endif
+ ," -L add debugging information to logfile"
+ ," -l FILE write informational messages to FILE"
+ ," -p FILE color-palette file (default \"$TERM.dat\")"
+ ," -q less verbose"
+ ," -r FILE xpm uses X rgb color-names in FILE (default \"" RGB_PATH "\")"
+ ," -s SECS pause for SECS seconds after display vs getch"
#if USE_EXTENDED_COLORS
- ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
- ," Either/both extension may be given"
+ ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
+ ," Either/both extension may be given"
#endif
};
size_t n;
- stop_curses();
+ pause_curses();
fflush(stdout);
for (n = 0; n < SIZEOF(msg); n++)
fprintf(stderr, "%s\n", msg[n]);
- cleanup(EXIT_FAILURE);
+ cleanup(ok ? EXIT_SUCCESS : EXIT_FAILURE);
}
static void
-giveup(const char *fmt,...)
+giveup(const char *fmt, ...)
{
va_list ap;
- stop_curses();
+ pause_curses();
fflush(stdout);
va_start(ap, fmt);
fflush(logfp);
}
- usage();
+ usage(FALSE);
}
/*
{
static const char *data_dir = DATA_DIR;
char **result = 0;
- char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
+ size_t last = strlen(filename);
+ size_t need = (strlen(data_dir) + 20 + last);
+ char *full_name = malloc(need);
char *s;
struct stat sb;
*(s = full_name) = '\0';
if (tries & 1) {
if (strchr(filename, '/') == 0) {
- sprintf(full_name, "%s/", data_dir);
+ _nc_SPRINTF(full_name, _nc_SLIMIT(need) "%s/", data_dir);
} else {
continue;
}
}
s += strlen(s);
+ if (((size_t) (s - full_name) + last + 1) >= need)
+ continue;
- strcpy(s, filename);
+ _nc_STRCAT(full_name, filename, need);
if (tries & 4) {
char *t = s;
+ char *tc;
int num;
char chr;
int found = 0;
if (*t == '-') {
if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
chr == 'c' &&
- !strncmp(strchr(t, chr), "color", 5)) {
+ (tc = strchr(t, chr)) != 0 &&
+ !(strncmp) (tc, "color", 5)) {
found = 1;
}
break;
++t;
}
if (found && (t != s)
- && strncmp(s, "xterm", (size_t) (t - s))) {
- sprintf(s, "xterm%s", filename + (t - s));
+ && (strncmp) (s, "xterm", (size_t) (t - s))) {
+ _nc_SPRINTF(s, _nc_SLIMIT(need - (size_t) (s - full_name))
+ "xterm%s", filename + (t - s));
} else {
continue;
}
}
- s += strlen(s);
if (tries & 2) {
int len = (int) strlen(filename);
if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
- strcpy(s, ".dat");
+ _nc_STRCAT(full_name, ".dat", need);
} else {
continue;
}
{
if (palette_file != 0) {
char **data = read_palette(palette_file);
- int cp;
all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
- for (cp = 0; cp < COLORS; ++cp) {
- color_content((short) cp,
- &all_colors[cp].red,
- &all_colors[cp].green,
- &all_colors[cp].blue);
+#if HAVE_COLOR_CONTENT
+ {
+ int cp;
+ for (cp = 0; cp < COLORS; ++cp) {
+ color_content((short) cp,
+ &all_colors[cp].red,
+ &all_colors[cp].green,
+ &all_colors[cp].blue);
+ }
}
+#else
+ memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
+#endif
if (data != 0) {
int n;
int red, green, blue;
- int scale = 1000;
+ int scale = MaxSCALE;
int c;
for (n = 0; data[n] != 0; ++n) {
if (sscanf(data[n], "scale:%d", &c) == 1) {
&green,
&blue) == 4
&& okCOLOR(c)
- && okRGB(red)
- && okRGB(green)
- && okRGB(blue)) {
+ && okSCALE(red)
+ && okSCALE(green)
+ && okSCALE(blue)) {
/* *INDENT-EQLS* */
all_colors[c].red = ScaledColor(red);
all_colors[c].green = ScaledColor(green);
if ((power2 != COLORS) || ((shift % 3) != 0)) {
if (all_colors == 0) {
init_palette(getenv("TERM"));
- }
- if (all_colors == 0) {
- giveup("With %d colors, you need a palette-file", COLORS);
+ if (all_colors == 0) {
+ giveup("With %d colors, you need a palette-file", COLORS);
+ }
}
}
}
return value;
}
-static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
+static int match_c(const char *, const char *, ...) GCC_SCANFLIKE(2,3);
static char *
skip_s(char *s)
}
static int
-match_c(const char *source, const char *pattern,...)
+match_c(const char *source, const char *pattern, ...)
{
int limit = (int) strlen(source);
const char *last_s = source + limit;
int ch;
int *ip;
char *cp;
+ float *fp;
long lv;
va_start(ap, pattern);
continue;
}
/* %c, %d, %s are like sscanf except for special treatment of blanks */
- if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
+ if (ch == '%' && *pattern != '\0' && strchr("%cdnfsx", *pattern)) {
bool found = FALSE;
ch = *pattern++;
switch (ch) {
+ case '%':
+ source++;
+ break;
case 'c':
cp = va_arg(ap, char *);
do {
goto finish;
}
break;
+ case 'f':
+ /* floating point for pixels... */
+ fp = va_arg(ap, float *);
+ lv = strtol(source, &cp, 10);
+ if (cp == 0 || cp == source)
+ goto finish;
+ *fp = (float) lv;
+ source = cp;
+ if (*source == '.') {
+ lv = strtol(++source, &cp, 10);
+ if (cp == 0 || cp == source)
+ goto finish;
+ {
+ float scale = 1.0f;
+ int digits = (int) (cp - source);
+ while (digits-- > 0) {
+ scale *= 10.0f;
+ }
+ *fp += (float) lv / scale;
+ }
+ source = cp;
+ }
+ break;
case 'n':
/* not really sscanf... */
limit = *va_arg(ap, int *);
arg1[cpp] = '\0';
result = 1;
} else {
- char *t;
const char *s = skip_cs(source);
size_t have = strlen(source);
memcpy(arg1, s, (size_t) cpp);
s += cpp;
while (*s++ == '\t') {
+ char *t;
for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
if (*s == ' ') {
s = skip_cs(s);
return result;
}
+#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
+
+static int
+CaselessCmp(const char *a, const char *b)
+{ /* strcasecmp isn't portable */
+ while (*a && *b) {
+ int cmp = LOWERCASE(*a) - LOWERCASE(*b);
+ if (cmp != 0)
+ break;
+ a++, b++;
+ }
+ return LOWERCASE(*a) - LOWERCASE(*b);
+}
+
static RGB_NAME *
lookup_rgb(const char *name)
{
if (rgb_table != 0) {
int n;
for (n = 0; rgb_table[n].name != 0; ++n) {
- if (!strcasecmp(name, rgb_table[n].name)) {
+ if (!CaselessCmp(name, rgb_table[n].name)) {
result = &rgb_table[n];
break;
}
{
int n;
int state = 0;
- char buf[BUFSIZ];
+ char buf[2048];
int num;
char ch;
char *s;
result = typeCalloc(PICS_HEAD, 1);
how_much.head += sizeof(PICS_HEAD);
+ begin_c_values(2);
+ gather_c_values(0);
+ gather_c_values(0xffffff);
+
for (n = 0; data[n] != 0; ++n) {
if (strlen(s = data[n]) >= sizeof(buf) - 1)
continue;
case 0:
case 1:
case 2:
- if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
+ if (sscanf(s, "#define %1024s %d%c", buf, &num, &ch) >= 2) {
if ((t = strstr(buf, "_width")) != 0) {
state |= 1;
result->wide = (short) bytes_of(num);
} else if ((t = strstr(buf, "_height")) != 0) {
state |= 2;
result->high = (short) num;
+ } else {
+ break;
}
*t = '\0';
if (result->name) {
}
break;
case 3:
- if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
+ if (sscanf(s, "static char %1024[^_ ]_bits[]%c", buf, &ch) >= 1) {
if (strcmp(result->name, buf)) {
goto finish;
}
if (isdigit(UChar(*s))) {
long value = strtol(s, &t, 0);
int b;
- if (t != s || value > 255 || value < 0) {
+ if (t != s || value > MaxRGB || value < 0) {
s = t;
} else {
state = -1;
goto finish;
}
- /* TODO: which order? */
for (b = 0; b < 8; ++b) {
if (((1L << b) & value) != 0) {
result->cells[which].ch = '*';
result->cells[which].fg = 1;
+ reading_ncols[1].count++;
} else {
result->cells[which].ch = ' ';
result->cells[which].fg = 0;
+ reading_ncols[0].count++;
}
if (++which > cells) {
state = -1;
result = free_pics_head(result);
}
} else {
- begin_c_values(2);
- gather_c_values(0);
- gather_c_values(0xffffff);
finish_c_values(result);
}
return result;
result->cells = typeCalloc(PICS_CELL, cells);
how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
- list = typeCalloc(char *, result->colors);
- how_much.list += sizeof(char *) * (size_t) result->colors;
+ list = typeCalloc(char *, result->colors + 1);
+ how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
cpp = num[3];
state = 3;
break;
}
num_colors++;
+ free(list[reading_last]);
list[reading_last] = strdup(arg1);
if ((by_name = lookup_rgb(arg3)) != 0) {
found = gather_c_values(by_name->value);
found = gather_c_values(0); /* actually an error */
}
debugmsg(" [%d:%d] %06X", num_colors, result->colors,
- reading_ncols[(found >= 0) ? found : 0]->fgcol);
+ reading_ncols[(found >= 0) ? found : 0].fgcol);
if (num_colors >= result->colors) {
finish_c_values(result);
state = 4;
+ if (list[0] == 0)
+ list[0] = strdup("\033");
}
break;
case 4:
/* FIXME - factor out */
for (c = 0; c < result->colors; ++c) {
- if (!strncmp(cs, list[c], (size_t) cpp)) {
+ if (list[c] == 0) {
+ /* should not happen... */
+ continue;
+ }
+ if (!(strncmp) (cs, list[c], (size_t) cpp)) {
result->cells[which].ch = list[c][0];
result->cells[which].fg = c;
+ result->fgcol[c].count++;
break;
}
}
state = 5;
break;
}
- for (c = cpp; c > 0; --c, ++cs) ;
+ for (c = cpp; c > 0; --c, ++cs) {
+ if (*cs == '\0')
+ break;
+ }
}
}
break;
static PICS_HEAD *
parse_img(const char *filename)
{
- char *cmd = malloc(strlen(filename) + 256);
+ size_t need = strlen(filename) + 256;
+ char *cmd = malloc(need);
FILE *pp;
char buffer[BUFSIZ];
char dummy[BUFSIZ];
int pic_y = 0;
int width = in_curses ? COLS : 80;
- sprintf(cmd, "identify \"%s\"", filename);
+ _nc_SPRINTF(cmd, _nc_SLIMIT(need) "identify \"%s\"", filename);
if (quiet)
- strcat(cmd, " 2>/dev/null");
+ _nc_STRCAT(cmd, " 2>/dev/null", need);
logmsg("...opening pipe to %s", cmd);
size_t n = strlen(filename);
debugmsg2("...read %s", buffer);
if (strlen(buffer) > n &&
- !strncmp(buffer, filename, n) &&
+ !(strncmp) (buffer, filename, n) &&
isspace(UChar(buffer[n])) &&
sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
/* distort image to make it show normally on terminal */
- pic_x = (166 * pic_x) / 100;
+ pic_x = (int) ((double) pic_x / aspect_ratio);
} else {
pic_x = pic_y = 0;
}
if (pic_x <= 0 || pic_y <= 0)
goto finish;
- sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
- "-define txt:compliance=SVG txt:-",
- pic_x, pic_y, width, filename);
+ _nc_SPRINTF(cmd, _nc_SLIMIT(need)
+ "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
+ "-define txt:compliance=SVG txt:-",
+ pic_x, pic_y, width, filename);
if (quiet)
- strcat(cmd, " 2>/dev/null");
+ _nc_STRCAT(cmd, " 2>/dev/null", need);
logmsg("...opening pipe to %s", cmd);
if ((pp = popen(cmd, "r")) != 0) {
break;
}
} else {
- /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
+ /*
+ * subsequent lines begin "col,row: (r,g,b,a) #RGB".
+ * Those r/g/b could be integers (0..255) or float-percentages.
+ */
int r, g, b, nocolor;
+ float rf, gf, bf;
unsigned check;
- int which, c;
char *t;
char *s = t = strchr(buffer, '#');
+ bool matched = FALSE;
+
if (s != 0) {
/* after the "#RGB", there are differences - just ignore */
while (*s != '\0' && !isspace(UChar(*s)))
++s;
*++s = '\0';
}
+
if (match_c(buffer,
"%d,%d: (%d,%d,%d,%d) #%x ",
&col, &row,
&r, &g, &b, &nocolor,
&check)) {
- if ((s - t) > 8) /* 6 hex digits vs 8 */
- check /= 256;
- if (r > 255 ||
- g > 255 ||
- b > 255 ||
- check != (unsigned) ((r << 16) | (g << 8) | b)) {
+ matched = TRUE;
+ } else if (match_c(buffer,
+ "%d,%d: (%f%%,%f%%,%f%%,%d) #%x ",
+ &col, &row,
+ &rf, &gf, &bf, &nocolor,
+ &check) ||
+ match_c(buffer,
+ "%d,%d: (%f%%,%f%%,%f%%) #%x ",
+ &col, &row,
+ &rf, &gf, &bf,
+ &check)) {
+ matched = TRUE;
+
+#define fp_fix(n) (int) (MaxRGB * (((n) > 100.0 ? 100.0 : (n)) / 100.0))
+
+ r = fp_fix(rf);
+ g = fp_fix(gf);
+ b = fp_fix(bf);
+ }
+ if ((s - t) > 8) /* 6 hex digits vs 8 */
+ check /= 256;
+ if (matched) {
+ int which, c;
+ int want_r = (check >> 16) & 0xff;
+ int want_g = (check >> 8) & 0xff;
+ int want_b = (check >> 0) & 0xff;
+
+#define fp_err(tst,ref) ((tst > MaxRGB) || ((tst - ref)*(tst - ref)) > 4)
+
+ if (fp_err(r, want_r) ||
+ fp_err(g, want_g) ||
+ fp_err(b, want_b)) {
okay = FALSE;
break;
}
check == 0xffffff)
? ' '
: '#');
- result->cells[which].fg = ((c >= 0 && c < reading_last)
- ? c
- : -1);
+ if (c >= 0 && c < reading_last) {
+ result->cells[which].fg = c;
+ reading_ncols[c].count++;
+ } else {
+ result->cells[which].fg = -1;
+ }
} else {
okay = FALSE;
break;
if (okay) {
/* FIXME - is this trimming needed? */
for (len = result->colors; len > 3; len--) {
- if (result->fgcol[len - 1] == 0) {
+ if (result->fgcol[len - 1].fgcol == 0) {
result->colors = len - 1;
} else {
break;
return pics;
}
-#define fg_color(pics,n) (pics->fgcol[n]->fgcol)
+#define fg_color(pics,n) (pics->fgcol[n].fgcol)
static void
dump_picture(PICS_HEAD * pics)
}
}
+#ifndef USE_DISPLAY_DRIVER
+static void
+init_display(const char *palette_path, int opt_d)
+{
+ (void) opt_d;
+ if (isatty(fileno(stdout))) {
+ in_curses = TRUE;
+ setlocale(LC_ALL, "");
+ initscr();
+ cbreak();
+ noecho();
+ curs_set(0);
+ if (has_colors()) {
+ start_color();
+#if HAVE_USE_DEFAULT_COLORS
+ if (opt_d)
+ use_default_colors();
+#endif
+ init_palette(palette_path);
+ }
+ scrollok(stdscr, FALSE);
+ stop_curses();
+ }
+}
+
static void
show_picture(PICS_HEAD * pics)
{
int y, x;
int n;
- int my_pair, my_color;
debugmsg("called show_picture");
-#if USE_EXTENDED_COLORS
+ logmsg("...using %dx%d screen", LINES, COLS);
+#if HAVE_RESET_COLOR_PAIRS
reset_color_pairs();
-#else
+#elif HAVE_CURSCR
wclear(curscr);
clear();
#endif
if (has_colors()) {
logmsg("...using %d colors", pics->colors);
for (n = 0; n < pics->colors; ++n) {
- my_pair = (n + 1);
- my_color = map_color(fg_color(pics, n));
+ int my_pair = (n + 1);
+ int my_color = map_color(fg_color(pics, n));
#if USE_EXTENDED_COLORS
if (use_extended_pairs) {
init_extended_pair(my_pair, my_color, my_color);
if (y >= LINES)
break;
move(y, 0);
+
for (x = 0; x < pics->wide; ++x) {
+ int my_pair;
+
if (x >= COLS)
break;
n = (y * pics->wide + x);
#endif
}
} else {
- mvgetch(0, 0);
+ wmove(stdscr, 0, 0);
+ getch();
}
if (!quiet)
endwin();
}
+#endif
+
+static int
+compare_fg_counts(const void *a, const void *b)
+{
+ const FG_NODE *p = (const FG_NODE *) a;
+ const FG_NODE *q = (const FG_NODE *) b;
+ return (q->count - p->count);
+}
+
+static void
+report_colors(PICS_HEAD * pics)
+{
+ int accum;
+ double level;
+ int j;
+ int shift;
+ int total;
+ char buffer[256];
+
+ if (logfp == 0)
+ return;
+
+ qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
+ /*
+ * For debugging, show a (short) list of the colors used.
+ */
+ if (debugging && (pics->colors < 1000)) {
+ int digits = 0;
+ int high;
+ int wide = 4;
+ for (j = pics->colors; j != 0; j /= 10) {
+ ++digits;
+ if (j < 10)
+ ++digits;
+ }
+ if (digits > 8)
+ digits = 8;
+ logmsg("These colors were used:");
+ high = (pics->colors + wide - 1) / wide;
+ for (j = 0; j < high && j < pics->colors; ++j) {
+ int k;
+ char *s = buffer;
+ *s = '\0';
+ for (k = 0; k < wide; ++k) {
+ int n = j + (k * high);
+ size_t want = (sizeof(buffer) - (size_t) (s - buffer));
+ if (want < 100 || want >= sizeof(buffer))
+ break;
+ if (n >= pics->colors)
+ break;
+ if (k) {
+ *s++ = ' ';
+ if (digits < 8) {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%*s", 8 - digits,
+ " ");
+ s += strlen(s);
+ }
+ }
+ if (pics->fgcol[n].fgcol >= 0) {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d #%06X %*d", n,
+ pics->fgcol[n].fgcol,
+ digits, pics->fgcol[n].count);
+ } else {
+ _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d (empty) %*d", n,
+ digits, pics->fgcol[n].count);
+ }
+ s += strlen(s);
+ if ((s - buffer) > 100)
+ break;
+ }
+ logmsg("%s", buffer);
+ }
+ }
+
+ /*
+ * Given the list of colors sorted by the number of times they are used,
+ * log a short report showing the number of colors for 90%, 99%, 99.9%,
+ * etc.
+ */
+ logmsg("Number of colors versus number of cells");
+ total = pics->high * pics->wide;
+ accum = 0;
+ level = 0.1;
+ shift = 1;
+ for (j = 0; j < pics->colors; ++j) {
+ accum += pics->fgcol[j].count;
+ if (accum >= (total * (1.0 - level))) {
+ int after = (shift > 2) ? shift - 2 : 0;
+ logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
+ j + 1,
+ (100.0 * (j + 1)) / pics->colors,
+ accum,
+ after, (100.0 * accum) / total);
+ if (accum >= total)
+ break;
+ level /= 10.0;
+ shift++;
+ }
+ }
+}
+/* *INDENT-OFF* */
+VERSION_COMMON()
+/* *INDENT-ON* */
int
main(int argc, char *argv[])
{
- int n;
+ int ch;
+ int opt_d = FALSE;
+ char ignore_ch;
const char *palette_path = 0;
- const char *rgb_path = "/etc/X11/rgb.txt";
-
- while ((n = getopt(argc, argv, "dl:p:qr:s:x:")) != -1) {
- switch (n) {
+ const char *rgb_path = RGB_PATH;
+
+ while ((ch = getopt(argc, argv, OPTS_COMMON "a:dLl:p:qr:s:x:")) != -1) {
+ switch (ch) {
+ case 'a':
+ if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
+ || aspect_ratio < 0.1
+ || aspect_ratio > 10.) {
+ fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
+ usage(FALSE);
+ }
+ break;
+#if HAVE_USE_DEFAULT_COLORS
case 'd':
+ opt_d = TRUE;
+ break;
+#endif
+ case 'L':
debugging = TRUE;
break;
case 'l':
use_extended_colors = TRUE;
break;
default:
- usage();
+ usage(FALSE);
break;
}
}
}
break;
#endif
+ case OPTS_VERSION:
+ show_version(argv);
+ ExitProgram(EXIT_SUCCESS);
default:
- usage();
- break;
+ usage(ch == OPTS_USAGE);
+ /* NOTREACHED */
}
}
if (optind < argc) {
char **rgb_data = read_file(rgb_path);
+ int n;
if (rgb_data)
rgb_table = parse_rgb(rgb_data);
- if (isatty(fileno(stdout))) {
- in_curses = TRUE;
- initscr();
- cbreak();
- noecho();
- curs_set(0);
- if (has_colors()) {
- start_color();
- init_palette(palette_path);
- }
- scrollok(stdscr, FALSE);
- endwin();
- }
+ init_display(palette_path, opt_d);
if (optind >= argc)
giveup("expected at least one image filename");
} else {
dump_picture(pics);
}
+ report_colors(pics);
dispose_c_values();
free_data(data);
free_pics_head(pics);
free(rgb_table);
free(all_colors);
} else {
- usage();
+ usage(FALSE);
}
cleanup(EXIT_SUCCESS);