1 /****************************************************************************
2 * Copyright (c) 2017 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 * $Id: picsmap.c,v 1.101 2017/08/20 16:42:13 tom Exp $
31 * Author: Thomas E. Dickey
33 * A little more interesting than "dots", read a simple image into memory and
34 * measure the time taken to paint it normally vs randomly.
36 * TODO improve use of rgb-names using tsearch.
38 * TODO add option to dump picture in non-optimized mode, e.g., like tput.
39 * TODO write cells/second to stderr (or log)
40 * TODO write picture left-to-right/top-to-bottom
41 * TODO write picture randomly
42 * TODO add one-shot option vs repeat-count before exiting
43 * TODO add option "-xc" for init_color vs init_extended_color
44 * TODO add option "-xa" for init_pair vs alloc_pair
45 * TODO use pad to allow pictures larger than screen
46 * TODO add option to just use convert (which can scale) vs builtin xbm/xpm.
47 * TODO add scr_dump and scr_restore calls
48 * TODO add option for assume_default_colors
50 #include <test.priv.h>
53 #include <sys/types.h>
58 #define my_intptr_t intptr_t
60 #define my_intptr_t long
67 #undef CUR /* use only the curses interface */
75 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
76 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
77 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
78 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
80 #define RGB_PATH "/etc/X11/rgb.txt"
82 typedef int NUM_COLOR;
83 typedef unsigned short NUM_COUNT;
86 char ch; /* nominal character to display */
87 NUM_COLOR fg; /* foreground color */
126 #define MAX(a,b) ((a)>(b)?(a):(b))
129 * tfind will return null on failure, so we map subscripts starting at one.
131 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
132 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
134 #define stop_curses() if (in_curses) endwin()
136 #define debugmsg if (debugging) logmsg
137 #define debugmsg2 if (debugging) logmsg2
139 static void cleanup(int) GCC_NORETURN;
140 static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
141 static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
142 static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
143 static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
144 static int gather_c_values(int);
146 static FILE *logfp = 0;
147 static bool in_curses = FALSE;
148 static bool debugging = FALSE;
149 static bool quiet = FALSE;
150 static int slow_time = -1;
151 static RGB_NAME *rgb_table;
152 static RGB_DATA *all_colors;
153 static HOW_MUCH how_much;
155 static int reading_last;
156 static int reading_size;
157 static FG_NODE *reading_ncols;
160 static void *reading_ntree;
163 #if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
164 #define USE_EXTENDED_COLORS 1
165 static bool use_extended_pairs = FALSE;
166 static bool use_extended_colors = FALSE;
168 #define USE_EXTENDED_COLORS 0
172 logmsg(const char *fmt,...)
177 vfprintf(logfp, fmt, ap);
185 logmsg2(const char *fmt,...)
190 vfprintf(logfp, fmt, ap);
200 logmsg("Allocations:");
201 logmsg("%8ld file", (long) how_much.file);
202 logmsg("%8ld name", (long) how_much.name);
203 logmsg("%8ld list", (long) how_much.list);
204 logmsg("%8ld data", (long) how_much.data);
205 logmsg("%8ld head", (long) how_much.head);
206 logmsg("%8ld pair", (long) how_much.pair);
207 logmsg("%8ld cell", (long) how_much.cell);
208 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
224 failed(const char *msg)
228 logmsg("failed with %s", strerror(save));
229 cleanup(EXIT_FAILURE);
233 warning(const char *fmt,...)
238 vfprintf(logfp, fmt, ap);
245 vfprintf(stderr, fmt, ap);
248 cleanup(EXIT_FAILURE);
253 free_data(char **data)
262 free_pics_head(PICS_HEAD * pics)
275 begin_c_values(int size)
279 reading_ncols = typeCalloc(FG_NODE, size + 1);
280 how_much.pair += (sizeof(FG_NODE) * (size_t) size);
281 /* black is always the first slot, to work around P2I/I2P logic */
287 compare_c_values(const void *p, const void *q)
289 const int a = P2I(p);
290 const int b = P2I(q);
291 return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
296 check_c_values(int ln)
301 for (n = 0; n < reading_last; ++n) {
302 ft = tfind(I2P(n), &reading_ntree, compare_c_values);
305 if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
306 logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
310 reading_ncols[n].fgcol,
311 reading_ncols[q].fgcol);
314 logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
318 reading_ncols[n].fgcol);
325 #define check_c_values(n) /* nothing */
330 gather_c_values(int fg)
335 int next = reading_last;
337 reading_ncols[next].fgcol = fg;
338 reading_ncols[next].count = 0;
340 check_c_values(__LINE__);
341 if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
344 if (reading_last + 2 >= reading_size) {
345 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
346 int last = reading_last + 1;
347 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
353 memset(reading_ncols + last, 0,
354 sizeof(FG_NODE) * (size_t) (more - last));
355 check_c_values(__LINE__);
358 how_much.pair += sizeof(FG_NODE);
359 if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
362 logmsg("OOPS expected slot %d, got %d", next, found);
363 debugmsg("allocated color #%d as #%06X", next, fg);
364 check_c_values(__LINE__);
370 for (n = 0; n < reading_last; ++n) {
371 if (reading_ncols[n].fgcol == fg) {
377 if (reading_last + 2 >= reading_size) {
378 int more = ((reading_last + 2) * 3) / 2;
379 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
383 how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
384 how_much.pair += (sizeof(FG_NODE) * (size_t) more);
387 memset(reading_ncols + reading_last, 0,
388 sizeof(FG_NODE) * (size_t) (more - reading_last));
390 reading_ncols[reading_last].fgcol = fg;
391 found = reading_last++;
399 finish_c_values(PICS_HEAD * head)
401 head->colors = reading_last;
402 head->fgcol = reading_ncols;
409 #if HAVE_TSEARCH && HAVE_TDESTROY
411 never_free(void *node GCC_UNUSED)
417 dispose_c_values(void)
420 if (reading_ntree != 0) {
422 tdestroy(reading_ntree, never_free);
425 for (n = 0; n < reading_last; ++n) {
426 tdelete(I2P(n), &reading_ntree, compare_c_values);
432 if (reading_ncols != 0) {
441 is_file(const char *filename, struct stat *sb)
444 if (stat(filename, sb) == 0
445 && (sb->st_mode & S_IFMT) == S_IFREG
446 && sb->st_size != 0) {
449 debugmsg("is_file(%s) %d", filename, result);
454 * Simplify reading xbm/xpm files by first making an array of lines. Blank
455 * lines are filtered out.
458 read_file(const char *filename)
465 printf("** %s\n", filename);
468 if (is_file(filename, &sb)) {
469 size_t size = (size_t) sb.st_size;
470 char *blob = typeCalloc(char, size + 1);
471 bool had_line = TRUE;
476 result = typeCalloc(char *, size + 1);
477 how_much.file += ((size + 1) * 2);
479 if (blob != 0 && result != 0) {
480 FILE *fp = fopen(filename, "r");
482 logmsg("opened %s", filename);
483 if (fread(blob, sizeof(char), size, fp) == size) {
484 for (j = 0; (size_t) j < size; ++j) {
485 if (blob[j] == '\0' ||
486 (UChar(blob[j]) < 32 &&
487 !isspace(blob[j])) ||
488 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160))
490 if (blob[j] == '\n') {
493 debugmsg2("[%5d] %s\n", k, result[k - 1]);
496 } else if (had_line) {
498 result[k++] = blob + j;
503 debugmsg2("[%5d] %s\n", k, result[k - 1]);
508 logmsg("cannot open %s", filename);
512 debugmsg("...file is empty");
517 debugmsg("...file is non-text");
526 static const char *msg[] =
528 "Usage: picsmap [options] [imagefile [...]]"
529 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
532 ," -d add debugging information to logfile"
533 ," -l logfile write informational messages to logfile"
534 ," -p palette color-palette file (default \"$TERM.dat\")"
536 ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
537 ," -s SECS pause for SECS seconds after display vs getch"
538 #if USE_EXTENDED_COLORS
539 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
540 ," Either/both extension may be given"
548 for (n = 0; n < SIZEOF(msg); n++)
549 fprintf(stderr, "%s\n", msg[n]);
550 cleanup(EXIT_FAILURE);
554 giveup(const char *fmt,...)
562 vfprintf(stderr, fmt, ap);
568 vfprintf(logfp, fmt, ap);
578 * Palette files are named for $TERM values. However, there are fewer palette
579 * files than $TERM's. Although there are known problems (some cannot even get
580 * black and white correct), for the purpose of comparison, pretending that
581 * those map into "xterm" is useful.
584 read_palette(const char *filename)
586 static const char *data_dir = DATA_DIR;
588 char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
592 if (full_name != 0) {
594 for (tries = 0; tries < 8; ++tries) {
596 *(s = full_name) = '\0';
598 if (strchr(filename, '/') == 0) {
599 sprintf(full_name, "%s/", data_dir);
614 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
616 !strncmp(strchr(t, chr), "color", 5)) {
623 if (found && (t != s)
624 && strncmp(s, "xterm", (size_t) (t - s))) {
625 sprintf(s, "xterm%s", filename + (t - s));
633 int len = (int) strlen(filename);
634 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
640 if (is_file(full_name, &sb))
645 result = read_file(full_name);
653 init_palette(const char *palette_file)
655 if (palette_file != 0) {
656 char **data = read_palette(palette_file);
659 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
660 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
662 #if HAVE_COLOR_CONTENT
663 for (cp = 0; cp < COLORS; ++cp) {
664 color_content((short) cp,
666 &all_colors[cp].green,
667 &all_colors[cp].blue);
670 memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
674 int red, green, blue;
677 for (n = 0; data[n] != 0; ++n) {
678 if (sscanf(data[n], "scale:%d", &c) == 1) {
680 } else if (sscanf(data[n], "%d:%d %d %d",
690 all_colors[c].red = ScaledColor(red);
691 all_colors[c].green = ScaledColor(green);
692 all_colors[c].blue = ScaledColor(blue);
698 } else if (COLORS > 1) {
702 while (power2 < COLORS) {
707 if ((power2 != COLORS) || ((shift % 3) != 0)) {
708 if (all_colors == 0) {
709 init_palette(getenv("TERM"));
711 if (all_colors == 0) {
712 giveup("With %d colors, you need a palette-file", COLORS);
719 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
720 * direct color value.
731 int red = (value & 0xff0000) >> 16;
732 int green = (value & 0x00ff00) >> 8;
733 int blue = (value & 0x0000ff) >> 0;
735 if (all_colors != 0) {
736 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
737 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
742 red = Scaled256(red);
743 green = Scaled256(green);
744 blue = Scaled256(blue);
746 for (result = 0, n = 1; n < COLORS; ++n) {
747 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
753 } else { /* direct color */
757 while (power2 < COLORS) {
767 result = ((red << (2 * (8 - shifts)))
768 + (green << (8 - shifts))
786 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
791 while (isspace(UChar(*s)))
797 skip_cs(const char *s)
799 while (isspace(UChar(*s)))
808 while (isgraph(UChar(*s)))
814 match_c(const char *source, const char *pattern,...)
816 int limit = (int) strlen(source);
817 const char *last_s = source + limit;
824 va_start(ap, pattern);
827 while (*pattern != '\0') {
828 ch = UChar(*pattern++);
829 /* blank in the pattern matches zero-or-more blanks in source */
831 source = skip_cs(source);
834 /* %c, %d, %s are like sscanf except for special treatment of blanks */
835 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
840 cp = va_arg(ap, char *);
843 } while (--limit > 0);
848 ip = va_arg(ap, int *);
849 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
850 if (cp != 0 && cp != source) {
858 /* not really sscanf... */
859 limit = *va_arg(ap, int *);
863 cp = va_arg(ap, char *);
864 while (*source != '\0') {
868 } else if (found && (ch == *skip_cs(pattern))) {
880 /* other characters are matched literally */
881 if (*source++ != ch) {
890 return (*source || *pattern) ? 0 : 1;
894 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
898 /* most files use a quasi-fixed format */
899 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
904 const char *s = skip_cs(source);
905 size_t have = strlen(source);
907 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
908 memcpy(arg1, s, (size_t) cpp);
910 while (*s++ == '\t') {
911 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
919 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
923 if (!strcmp(arg2, "c")) {
934 parse_rgb(char **data)
938 unsigned long r, g, b;
942 RGB_NAME *result = 0;
944 for (need = 0; data[need] != 0; ++need) ;
946 result = typeCalloc(RGB_NAME, need + 2);
947 how_much.name += (sizeof(RGB_NAME) * (need + 2));
949 for (n = 0; data[n] != 0; ++n) {
950 if (strlen(t = data[n]) >= sizeof(buf) - 1)
952 if (*(s = skip_s(t)) == '!')
955 r = strtoul(s, &t, 10);
957 g = strtoul(s, &t, 10);
959 b = strtoul(s, &t, 10);
962 result[item].name = s;
964 while (t-- != s && isspace(UChar(*t))) {
967 result[item].value = (int) ((r & 0xff) << 16 |
973 result[item].name = "none";
974 result[item].value = -1;
980 lookup_rgb(const char *name)
982 RGB_NAME *result = 0;
983 if (rgb_table != 0) {
985 for (n = 0; rgb_table[n].name != 0; ++n) {
986 if (!strcasecmp(name, rgb_table[n].name)) {
987 result = &rgb_table[n];
996 parse_xbm(char **data)
1009 debugmsg("called parse_xbm");
1011 result = typeCalloc(PICS_HEAD, 1);
1012 how_much.head += sizeof(PICS_HEAD);
1016 gather_c_values(0xffffff);
1018 for (n = 0; data[n] != 0; ++n) {
1019 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1025 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
1026 if ((t = strstr(buf, "_width")) != 0) {
1028 result->wide = (short) bytes_of(num);
1029 } else if ((t = strstr(buf, "_height")) != 0) {
1031 result->high = (short) num;
1035 if (strcmp(result->name, buf)) {
1039 result->name = strdup(buf);
1044 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
1045 if (strcmp(result->name, buf)) {
1049 cells = (size_t) (result->wide * result->high);
1051 result->cells = typeCalloc(PICS_CELL, cells);
1052 how_much.cell += (sizeof(PICS_CELL) * cells);
1054 if ((s = strchr(s, L_CURLY)) == 0)
1061 while (*s != '\0') {
1062 while (isspace(UChar(*s))) {
1065 if (isdigit(UChar(*s))) {
1066 long value = strtol(s, &t, 0);
1068 if (t != s || value > 255 || value < 0) {
1074 for (b = 0; b < 8; ++b) {
1075 if (((1L << b) & value) != 0) {
1076 result->cells[which].ch = '*';
1077 result->cells[which].fg = 1;
1078 reading_ncols[1].count++;
1080 result->cells[which].ch = ' ';
1081 result->cells[which].fg = 0;
1082 reading_ncols[0].count++;
1084 if (++which > cells) {
1090 if (*s == R_CURLY) {
1093 } else if (*s == ',') {
1104 debugmsg("...state was only %d", state);
1106 result = free_pics_head(result);
1109 finish_c_values(result);
1115 parse_xpm(char **data)
1122 int cpp = 1; /* chars per pixel */
1136 debugmsg("called parse_xpm");
1138 result = typeCalloc(PICS_HEAD, 1);
1139 how_much.head += sizeof(PICS_HEAD);
1141 for (n = 0; data[n] != 0; ++n) {
1142 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1146 if (match_c(s, " /* XPM */ ")) {
1151 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1153 result->name = strdup(arg1);
1158 if (match_c(s, " \" %d %d %d %d \" , ",
1159 num + 0, num + 1, num + 2, num + 3) ||
1160 match_c(s, " \" %d %d %d %d %d %d \" , ",
1161 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1162 result->wide = (short) num[0];
1163 result->high = (short) num[1];
1164 result->colors = num[2];
1166 begin_c_values(num[2]);
1168 cells = (result->wide * result->high);
1170 result->cells = typeCalloc(PICS_CELL, cells);
1171 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1173 list = typeCalloc(char *, result->colors + 1);
1174 how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
1181 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1185 list[reading_last] = strdup(arg1);
1186 if ((by_name = lookup_rgb(arg3)) != 0) {
1187 found = gather_c_values(by_name->value);
1188 } else if (*arg3 == '#') {
1189 char *rgb = arg3 + 1;
1190 unsigned long value = strtoul(rgb, &s, 16);
1191 switch ((int) strlen(rgb)) {
1195 value = (((value >> 24) & 0xff0000L)
1196 | ((value >> 16) & 0xff00L)
1197 | ((value >> 8) & 0xffL));
1200 warning("unexpected rgb value %s", rgb);
1203 found = gather_c_values((int) value);
1205 found = gather_c_values(0); /* actually an error */
1207 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1208 reading_ncols[(found >= 0) ? found : 0].fgcol);
1209 if (num_colors >= result->colors) {
1210 finish_c_values(result);
1212 if (list != 0 && list[0] == 0)
1213 list[0] = strdup("\033");
1217 if (*(cs = skip_cs(s)) == '"') {
1219 while (*cs != '\0' && *cs != '"') {
1222 /* FIXME - factor out */
1223 for (c = 0; c < result->colors; ++c) {
1225 /* should not happen... */
1228 if (!strncmp(cs, list[c], (size_t) cpp)) {
1229 result->cells[which].ch = list[c][0];
1230 result->cells[which].fg = c;
1231 result->fgcol[c].count++;
1236 if (result->cells[which].ch == 0) {
1237 result->cells[which].ch = '?';
1238 result->cells[which].fg = 0;
1241 if (++which >= cells) {
1245 for (c = cpp; c > 0; --c, ++cs) {
1255 if (result && list) {
1256 for (n = 0; n < result->colors; ++n)
1262 debugmsg("...state was only %d", state);
1263 result = free_pics_head(result);
1267 debugmsg("...allocated %d colors", result->colors);
1274 * The obscurely-named "convert" is provided by ImageMagick
1277 parse_img(const char *filename)
1279 char *cmd = malloc(strlen(filename) + 256);
1281 char buffer[BUFSIZ];
1287 int width = in_curses ? COLS : 80;
1289 sprintf(cmd, "identify \"%s\"", filename);
1291 strcat(cmd, " 2>/dev/null");
1293 logmsg("...opening pipe to %s", cmd);
1295 result = typeCalloc(PICS_HEAD, 1);
1296 how_much.head += sizeof(PICS_HEAD);
1298 if ((pp = popen(cmd, "r")) != 0) {
1299 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1300 size_t n = strlen(filename);
1301 debugmsg2("...read %s", buffer);
1302 if (strlen(buffer) > n &&
1303 !strncmp(buffer, filename, n) &&
1304 isspace(UChar(buffer[n])) &&
1305 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1306 /* distort image to make it show normally on terminal */
1307 pic_x = (166 * pic_x) / 100;
1314 if (pic_x <= 0 || pic_y <= 0)
1317 sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1318 "-define txt:compliance=SVG txt:-",
1319 pic_x, pic_y, width, filename);
1321 strcat(cmd, " 2>/dev/null");
1323 logmsg("...opening pipe to %s", cmd);
1324 if ((pp = popen(cmd, "r")) != 0) {
1329 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1330 debugmsg2("[%5d] %s", count + 1, buffer);
1331 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1337 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1338 &col, &row, &len, dummy)) {
1339 result->name = strdup(filename);
1340 result->wide = (short) col;
1341 result->high = (short) row;
1343 begin_c_values(256);
1345 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1346 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1352 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
1353 int r, g, b, nocolor;
1357 char *s = t = strchr(buffer, '#');
1359 /* after the "#RGB", there are differences - just ignore */
1360 while (*s != '\0' && !isspace(UChar(*s)))
1365 "%d,%d: (%d,%d,%d,%d) #%x ",
1367 &r, &g, &b, &nocolor,
1369 if ((s - t) > 8) /* 6 hex digits vs 8 */
1374 check != (unsigned) ((r << 16) | (g << 8) | b)) {
1378 c = gather_c_values((int) check);
1379 which = col + (row * result->wide);
1380 result->cells[which].ch = ((in_curses ||
1384 if (c >= 0 && c < reading_last) {
1385 result->cells[which].fg = c;
1386 reading_ncols[c].count++;
1388 result->cells[which].fg = -1;
1396 finish_c_values(result);
1399 /* FIXME - is this trimming needed? */
1400 for (len = result->colors; len > 3; len--) {
1401 if (result->fgcol[len - 1].fgcol == 0) {
1402 result->colors = len - 1;
1413 result = free_pics_head(result);
1420 read_picture(const char *filename, char **data)
1423 if ((pics = parse_xbm(data)) == 0) {
1425 if ((pics = parse_xpm(data)) == 0) {
1427 if ((pics = parse_img(filename)) == 0) {
1430 warning("unexpected file-format for \"%s\"", filename);
1431 } else if (pics->high == 0 || pics->wide == 0) {
1434 pics = free_pics_head(pics);
1435 warning("no picture found in \"%s\"", filename);
1442 #define fg_color(pics,n) (pics->fgcol[n].fgcol)
1445 dump_picture(PICS_HEAD * pics)
1449 printf("Name %s\n", pics->name);
1450 printf("Size %dx%d\n", pics->high, pics->wide);
1452 for (y = 0; y < pics->colors; ++y) {
1453 if (fg_color(pics, y) < 0) {
1454 printf(" %3d: %d\n", y, fg_color(pics, y));
1456 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1459 for (y = 0; y < pics->high; ++y) {
1460 for (x = 0; x < pics->wide; ++x) {
1461 putchar(pics->cells[y * pics->wide + x].ch);
1468 show_picture(PICS_HEAD * pics)
1472 int my_pair, my_color;
1474 debugmsg("called show_picture");
1475 #if USE_EXTENDED_COLORS
1476 reset_color_pairs();
1482 logmsg("...using %d colors", pics->colors);
1483 for (n = 0; n < pics->colors; ++n) {
1485 my_color = map_color(fg_color(pics, n));
1486 #if USE_EXTENDED_COLORS
1487 if (use_extended_pairs) {
1488 init_extended_pair(my_pair, my_color, my_color);
1494 init_pair((short) my_pair, (short) my_color, (short) my_color);
1497 attrset(COLOR_PAIR(1));
1500 for (y = 0; y < pics->high; ++y) {
1504 for (x = 0; x < pics->wide; ++x) {
1507 n = (y * pics->wide + x);
1508 my_pair = pics->cells[n].fg + 1;
1509 #if USE_EXTENDED_COLORS
1510 if (use_extended_pairs) {
1513 wch[0] = (wchar_t) pics->cells[n].ch;
1515 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1520 attrset(COLOR_PAIR(my_pair));
1521 addch((chtype) pics->cells[n].ch);
1525 if (slow_time >= 0) {
1527 if (slow_time > 0) {
1528 #ifdef NCURSES_VERSION
1529 napms(1000 * slow_time);
1531 sleep((unsigned) slow_time);
1535 wmove(stdscr, 0, 0);
1543 compare_fg_counts(const void *a, const void *b)
1545 const FG_NODE *p = (const FG_NODE *) a;
1546 const FG_NODE *q = (const FG_NODE *) b;
1547 return (q->count - p->count);
1551 report_colors(PICS_HEAD * pics)
1565 qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
1567 * For debugging, show a (short) list of the colors used.
1569 if (debugging && (pics->colors < 1000)) {
1571 for (j = pics->colors; j != 0; j /= 10) {
1576 logmsg("These colors were used:");
1577 high = (pics->colors + wide - 1) / wide;
1578 for (j = 0; j < high && j < pics->colors; ++j) {
1581 for (k = 0; k < wide; ++k) {
1582 int n = j + (k * high);
1583 if (n >= pics->colors)
1588 sprintf(s, "%*s", 8 - digits, " ");
1592 if (pics->fgcol[n].fgcol >= 0) {
1593 sprintf(s, "%3d #%06X %*d", n,
1594 pics->fgcol[n].fgcol,
1595 digits, pics->fgcol[n].count);
1597 sprintf(s, "%3d (empty) %*d", n,
1598 digits, pics->fgcol[n].count);
1601 if ((s - buffer) > 100)
1604 logmsg("%s", buffer);
1609 * Given the list of colors sorted by the number of times they are used,
1610 * log a short report showing the number of colors for 90%, 99%, 99.9%,
1613 logmsg("Number of colors versus number of cells");
1614 total = pics->high * pics->wide;
1618 for (j = 0; j < pics->colors; ++j) {
1619 accum += pics->fgcol[j].count;
1620 if (accum >= (total * (1.0 - level))) {
1621 int after = (shift > 2) ? shift - 2 : 0;
1622 logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
1624 (100.0 * (j + 1)) / pics->colors,
1626 after, (100.0 * accum) / total);
1636 main(int argc, char *argv[])
1639 const char *palette_path = 0;
1640 const char *rgb_path = "/etc/X11/rgb.txt";
1642 while ((n = getopt(argc, argv, "dl:p:qr:s:x:")) != -1) {
1648 if ((logfp = fopen(optarg, "a")) == 0)
1652 palette_path = optarg;
1661 slow_time = atoi(optarg);
1663 #if USE_EXTENDED_COLORS
1670 use_extended_pairs = TRUE;
1673 use_extended_colors = TRUE;
1689 if (optind < argc) {
1690 char **rgb_data = read_file(rgb_path);
1693 rgb_table = parse_rgb(rgb_data);
1695 if (isatty(fileno(stdout))) {
1703 init_palette(palette_path);
1705 scrollok(stdscr, FALSE);
1709 giveup("expected at least one image filename");
1711 for (n = optind; n < argc; ++n) {
1713 char **data = read_file(argv[n]);
1716 warning("cannot read \"%s\"", argv[n]);
1719 if ((pics = read_picture(argv[n], data)) != 0) {
1725 report_colors(pics);
1728 free_pics_head(pics);
1731 free_data(rgb_data);
1738 cleanup(EXIT_SUCCESS);