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.112 2017/11/26 00:10:57 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>
52 #include <sys/types.h>
57 #define my_intptr_t intptr_t
59 #define my_intptr_t long
66 #undef CUR /* use only the curses interface */
74 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
75 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
76 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
77 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
80 #define RGB_PATH "/etc/X11/rgb.txt"
96 #define MAX(a,b) ((a)>(b)?(a):(b))
99 * tfind will return null on failure, so we map subscripts starting at one.
101 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
102 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
104 #define stop_curses() if (in_curses) endwin()
106 #define debugmsg if (debugging) logmsg
107 #define debugmsg2 if (debugging) logmsg2
109 static void cleanup(int) GCC_NORETURN;
110 static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
111 static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
112 static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
113 static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
114 static int gather_c_values(int);
116 static FILE *logfp = 0;
117 static double aspect_ratio = 0.6;
118 static bool in_curses = FALSE;
119 static bool debugging = FALSE;
120 static bool quiet = FALSE;
121 static int slow_time = -1;
122 static RGB_NAME *rgb_table;
123 static RGB_DATA *all_colors;
124 static HOW_MUCH how_much;
126 static int reading_last;
127 static int reading_size;
128 static FG_NODE *reading_ncols;
131 static void *reading_ntree;
134 #if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
135 #define USE_EXTENDED_COLORS 1
136 static bool use_extended_pairs = FALSE;
137 static bool use_extended_colors = FALSE;
139 #define USE_EXTENDED_COLORS 0
143 logmsg(const char *fmt,...)
148 vfprintf(logfp, fmt, ap);
156 logmsg2(const char *fmt,...)
161 vfprintf(logfp, fmt, ap);
171 logmsg("Allocations:");
172 logmsg("%8ld file", (long) how_much.file);
173 logmsg("%8ld name", (long) how_much.name);
174 logmsg("%8ld list", (long) how_much.list);
175 logmsg("%8ld data", (long) how_much.data);
176 logmsg("%8ld head", (long) how_much.head);
177 logmsg("%8ld pair", (long) how_much.pair);
178 logmsg("%8ld cell", (long) how_much.cell);
179 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
195 failed(const char *msg)
199 logmsg("failed with %s", strerror(save));
200 cleanup(EXIT_FAILURE);
204 warning(const char *fmt,...)
209 vfprintf(logfp, fmt, ap);
216 vfprintf(stderr, fmt, ap);
219 cleanup(EXIT_FAILURE);
224 free_data(char **data)
233 free_pics_head(PICS_HEAD * pics)
246 begin_c_values(int size)
250 reading_ncols = typeCalloc(FG_NODE, size + 1);
251 how_much.pair += (sizeof(FG_NODE) * (size_t) size);
252 /* black is always the first slot, to work around P2I/I2P logic */
258 compare_c_values(const void *p, const void *q)
260 const int a = P2I(p);
261 const int b = P2I(q);
262 return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
267 check_c_values(int ln)
272 for (n = 0; n < reading_last; ++n) {
273 ft = tfind(I2P(n), &reading_ntree, compare_c_values);
276 if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
277 logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
281 reading_ncols[n].fgcol,
282 reading_ncols[q].fgcol);
285 logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
289 reading_ncols[n].fgcol);
296 #define check_c_values(n) /* nothing */
301 gather_c_values(int fg)
306 int next = reading_last;
308 reading_ncols[next].fgcol = fg;
309 reading_ncols[next].count = 0;
311 check_c_values(__LINE__);
312 if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
315 if (reading_last + 2 >= reading_size) {
316 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
317 int last = reading_last + 1;
318 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
324 memset(reading_ncols + last, 0,
325 sizeof(FG_NODE) * (size_t) (more - last));
326 check_c_values(__LINE__);
329 how_much.pair += sizeof(FG_NODE);
330 if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
333 logmsg("OOPS expected slot %d, got %d", next, found);
334 debugmsg("allocated color #%d as #%06X", next, fg);
335 check_c_values(__LINE__);
341 for (n = 0; n < reading_last; ++n) {
342 if (reading_ncols[n].fgcol == fg) {
348 if (reading_last + 2 >= reading_size) {
349 int more = ((reading_last + 2) * 3) / 2;
350 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
354 how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
355 how_much.pair += (sizeof(FG_NODE) * (size_t) more);
358 memset(reading_ncols + reading_last, 0,
359 sizeof(FG_NODE) * (size_t) (more - reading_last));
361 reading_ncols[reading_last].fgcol = fg;
362 found = reading_last++;
370 finish_c_values(PICS_HEAD * head)
372 head->colors = reading_last;
373 head->fgcol = reading_ncols;
380 #if HAVE_TSEARCH && HAVE_TDESTROY
382 never_free(void *node GCC_UNUSED)
388 dispose_c_values(void)
391 if (reading_ntree != 0) {
393 tdestroy(reading_ntree, never_free);
396 for (n = 0; n < reading_last; ++n) {
397 tdelete(I2P(n), &reading_ntree, compare_c_values);
403 if (reading_ncols != 0) {
412 is_file(const char *filename, struct stat *sb)
415 if (stat(filename, sb) == 0
416 && (sb->st_mode & S_IFMT) == S_IFREG
417 && sb->st_size != 0) {
420 debugmsg("is_file(%s) %d", filename, result);
425 * Simplify reading xbm/xpm files by first making an array of lines. Blank
426 * lines are filtered out.
429 read_file(const char *filename)
436 printf("** %s\n", filename);
439 if (is_file(filename, &sb)) {
440 size_t size = (size_t) sb.st_size;
441 char *blob = typeCalloc(char, size + 1);
442 bool had_line = TRUE;
447 result = typeCalloc(char *, size + 1);
448 how_much.file += ((size + 1) * 2);
450 if (blob != 0 && result != 0) {
451 FILE *fp = fopen(filename, "r");
453 logmsg("opened %s", filename);
454 if (fread(blob, sizeof(char), size, fp) == size) {
455 for (j = 0; (size_t) j < size; ++j) {
456 if (blob[j] == '\0' ||
457 (UChar(blob[j]) < 32 &&
458 !isspace(UChar(blob[j]))) ||
459 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
462 if (blob[j] == '\n') {
465 debugmsg2("[%5d] %s\n", k, result[k - 1]);
468 } else if (had_line) {
470 result[k++] = blob + j;
475 debugmsg2("[%5d] %s\n", k, result[k - 1]);
480 logmsg("cannot open %s", filename);
484 debugmsg("...file is empty");
489 debugmsg("...file is non-text");
498 static const char *msg[] =
500 "Usage: picsmap [options] [imagefile [...]]"
501 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
504 ," -a ratio aspect-ratio correction for ImageMagick"
505 #if HAVE_USE_DEFAULT_COLORS
506 ," -d invoke use_default_colors"
508 ," -L add debugging information to logfile"
509 ," -l logfile write informational messages to logfile"
510 ," -p palette color-palette file (default \"$TERM.dat\")"
512 ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
513 ," -s SECS pause for SECS seconds after display vs getch"
514 #if USE_EXTENDED_COLORS
515 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
516 ," Either/both extension may be given"
524 for (n = 0; n < SIZEOF(msg); n++)
525 fprintf(stderr, "%s\n", msg[n]);
526 cleanup(EXIT_FAILURE);
530 giveup(const char *fmt,...)
538 vfprintf(stderr, fmt, ap);
544 vfprintf(logfp, fmt, ap);
554 * Palette files are named for $TERM values. However, there are fewer palette
555 * files than $TERM's. Although there are known problems (some cannot even get
556 * black and white correct), for the purpose of comparison, pretending that
557 * those map into "xterm" is useful.
560 read_palette(const char *filename)
562 static const char *data_dir = DATA_DIR;
564 char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
568 if (full_name != 0) {
570 for (tries = 0; tries < 8; ++tries) {
572 *(s = full_name) = '\0';
574 if (strchr(filename, '/') == 0) {
575 sprintf(full_name, "%s/", data_dir);
590 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
592 !strncmp(strchr(t, chr), "color", 5)) {
599 if (found && (t != s)
600 && strncmp(s, "xterm", (size_t) (t - s))) {
601 sprintf(s, "xterm%s", filename + (t - s));
609 int len = (int) strlen(filename);
610 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
616 if (is_file(full_name, &sb))
621 result = read_file(full_name);
629 init_palette(const char *palette_file)
631 if (palette_file != 0) {
632 char **data = read_palette(palette_file);
634 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
635 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
637 #if HAVE_COLOR_CONTENT
640 for (cp = 0; cp < COLORS; ++cp) {
641 color_content((short) cp,
643 &all_colors[cp].green,
644 &all_colors[cp].blue);
648 memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
652 int red, green, blue;
655 for (n = 0; data[n] != 0; ++n) {
656 if (sscanf(data[n], "scale:%d", &c) == 1) {
658 } else if (sscanf(data[n], "%d:%d %d %d",
668 all_colors[c].red = ScaledColor(red);
669 all_colors[c].green = ScaledColor(green);
670 all_colors[c].blue = ScaledColor(blue);
676 } else if (COLORS > 1) {
680 while (power2 < COLORS) {
685 if ((power2 != COLORS) || ((shift % 3) != 0)) {
686 if (all_colors == 0) {
687 init_palette(getenv("TERM"));
689 if (all_colors == 0) {
690 giveup("With %d colors, you need a palette-file", COLORS);
697 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
698 * direct color value.
709 int red = (value & 0xff0000) >> 16;
710 int green = (value & 0x00ff00) >> 8;
711 int blue = (value & 0x0000ff) >> 0;
713 if (all_colors != 0) {
714 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
715 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
720 red = Scaled256(red);
721 green = Scaled256(green);
722 blue = Scaled256(blue);
724 for (result = 0, n = 1; n < COLORS; ++n) {
725 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
731 } else { /* direct color */
735 while (power2 < COLORS) {
745 result = ((red << (2 * (8 - shifts)))
746 + (green << (8 - shifts))
764 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
769 while (isspace(UChar(*s)))
775 skip_cs(const char *s)
777 while (isspace(UChar(*s)))
786 while (isgraph(UChar(*s)))
792 match_c(const char *source, const char *pattern,...)
794 int limit = (int) strlen(source);
795 const char *last_s = source + limit;
802 va_start(ap, pattern);
805 while (*pattern != '\0') {
806 ch = UChar(*pattern++);
807 /* blank in the pattern matches zero-or-more blanks in source */
809 source = skip_cs(source);
812 /* %c, %d, %s are like sscanf except for special treatment of blanks */
813 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
818 cp = va_arg(ap, char *);
821 } while (--limit > 0);
826 ip = va_arg(ap, int *);
827 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
828 if (cp != 0 && cp != source) {
836 /* not really sscanf... */
837 limit = *va_arg(ap, int *);
841 cp = va_arg(ap, char *);
842 while (*source != '\0') {
846 } else if (found && (ch == *skip_cs(pattern))) {
858 /* other characters are matched literally */
859 if (*source++ != ch) {
868 return (*source || *pattern) ? 0 : 1;
872 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
876 /* most files use a quasi-fixed format */
877 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
882 const char *s = skip_cs(source);
883 size_t have = strlen(source);
885 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
886 memcpy(arg1, s, (size_t) cpp);
888 while (*s++ == '\t') {
889 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
897 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
901 if (!strcmp(arg2, "c")) {
912 parse_rgb(char **data)
916 unsigned long r, g, b;
920 RGB_NAME *result = 0;
922 for (need = 0; data[need] != 0; ++need) ;
924 result = typeCalloc(RGB_NAME, need + 2);
925 how_much.name += (sizeof(RGB_NAME) * (need + 2));
927 for (n = 0; data[n] != 0; ++n) {
928 if (strlen(t = data[n]) >= sizeof(buf) - 1)
930 if (*(s = skip_s(t)) == '!')
933 r = strtoul(s, &t, 10);
935 g = strtoul(s, &t, 10);
937 b = strtoul(s, &t, 10);
940 result[item].name = s;
942 while (t-- != s && isspace(UChar(*t))) {
945 result[item].value = (int) ((r & 0xff) << 16 |
951 result[item].name = "none";
952 result[item].value = -1;
958 lookup_rgb(const char *name)
960 RGB_NAME *result = 0;
961 if (rgb_table != 0) {
963 for (n = 0; rgb_table[n].name != 0; ++n) {
964 if (!strcasecmp(name, rgb_table[n].name)) {
965 result = &rgb_table[n];
974 parse_xbm(char **data)
987 debugmsg("called parse_xbm");
989 result = typeCalloc(PICS_HEAD, 1);
990 how_much.head += sizeof(PICS_HEAD);
994 gather_c_values(0xffffff);
996 for (n = 0; data[n] != 0; ++n) {
997 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1003 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
1004 if ((t = strstr(buf, "_width")) != 0) {
1006 result->wide = (short) bytes_of(num);
1007 } else if ((t = strstr(buf, "_height")) != 0) {
1009 result->high = (short) num;
1013 if (strcmp(result->name, buf)) {
1017 result->name = strdup(buf);
1022 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
1023 if (strcmp(result->name, buf)) {
1027 cells = (size_t) (result->wide * result->high);
1029 result->cells = typeCalloc(PICS_CELL, cells);
1030 how_much.cell += (sizeof(PICS_CELL) * cells);
1032 if ((s = strchr(s, L_CURLY)) == 0)
1039 while (*s != '\0') {
1040 while (isspace(UChar(*s))) {
1043 if (isdigit(UChar(*s))) {
1044 long value = strtol(s, &t, 0);
1046 if (t != s || value > 255 || value < 0) {
1052 for (b = 0; b < 8; ++b) {
1053 if (((1L << b) & value) != 0) {
1054 result->cells[which].ch = '*';
1055 result->cells[which].fg = 1;
1056 reading_ncols[1].count++;
1058 result->cells[which].ch = ' ';
1059 result->cells[which].fg = 0;
1060 reading_ncols[0].count++;
1062 if (++which > cells) {
1068 if (*s == R_CURLY) {
1071 } else if (*s == ',') {
1082 debugmsg("...state was only %d", state);
1084 result = free_pics_head(result);
1087 finish_c_values(result);
1093 parse_xpm(char **data)
1100 int cpp = 1; /* chars per pixel */
1114 debugmsg("called parse_xpm");
1116 result = typeCalloc(PICS_HEAD, 1);
1117 how_much.head += sizeof(PICS_HEAD);
1119 for (n = 0; data[n] != 0; ++n) {
1120 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1124 if (match_c(s, " /* XPM */ ")) {
1129 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1131 result->name = strdup(arg1);
1136 if (match_c(s, " \" %d %d %d %d \" , ",
1137 num + 0, num + 1, num + 2, num + 3) ||
1138 match_c(s, " \" %d %d %d %d %d %d \" , ",
1139 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1140 result->wide = (short) num[0];
1141 result->high = (short) num[1];
1142 result->colors = num[2];
1144 begin_c_values(num[2]);
1146 cells = (result->wide * result->high);
1148 result->cells = typeCalloc(PICS_CELL, cells);
1149 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1151 list = typeCalloc(char *, result->colors + 1);
1152 how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
1159 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1163 list[reading_last] = strdup(arg1);
1164 if ((by_name = lookup_rgb(arg3)) != 0) {
1165 found = gather_c_values(by_name->value);
1166 } else if (*arg3 == '#') {
1167 char *rgb = arg3 + 1;
1168 unsigned long value = strtoul(rgb, &s, 16);
1169 switch ((int) strlen(rgb)) {
1173 value = (((value >> 24) & 0xff0000L)
1174 | ((value >> 16) & 0xff00L)
1175 | ((value >> 8) & 0xffL));
1178 warning("unexpected rgb value %s", rgb);
1181 found = gather_c_values((int) value);
1183 found = gather_c_values(0); /* actually an error */
1185 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1186 reading_ncols[(found >= 0) ? found : 0].fgcol);
1187 if (num_colors >= result->colors) {
1188 finish_c_values(result);
1190 if (list != 0 && list[0] == 0)
1191 list[0] = strdup("\033");
1195 if (*(cs = skip_cs(s)) == '"') {
1197 while (*cs != '\0' && *cs != '"') {
1200 /* FIXME - factor out */
1201 for (c = 0; c < result->colors; ++c) {
1203 /* should not happen... */
1206 if (!strncmp(cs, list[c], (size_t) cpp)) {
1207 result->cells[which].ch = list[c][0];
1208 result->cells[which].fg = c;
1209 result->fgcol[c].count++;
1214 if (result->cells[which].ch == 0) {
1215 result->cells[which].ch = '?';
1216 result->cells[which].fg = 0;
1219 if (++which >= cells) {
1223 for (c = cpp; c > 0; --c, ++cs) {
1233 if (result && list) {
1234 for (n = 0; n < result->colors; ++n)
1240 debugmsg("...state was only %d", state);
1241 result = free_pics_head(result);
1245 debugmsg("...allocated %d colors", result->colors);
1252 * The obscurely-named "convert" is provided by ImageMagick
1255 parse_img(const char *filename)
1257 char *cmd = malloc(strlen(filename) + 256);
1259 char buffer[BUFSIZ];
1265 int width = in_curses ? COLS : 80;
1267 sprintf(cmd, "identify \"%s\"", filename);
1269 strcat(cmd, " 2>/dev/null");
1271 logmsg("...opening pipe to %s", cmd);
1273 result = typeCalloc(PICS_HEAD, 1);
1274 how_much.head += sizeof(PICS_HEAD);
1276 if ((pp = popen(cmd, "r")) != 0) {
1277 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1278 size_t n = strlen(filename);
1279 debugmsg2("...read %s", buffer);
1280 if (strlen(buffer) > n &&
1281 !strncmp(buffer, filename, n) &&
1282 isspace(UChar(buffer[n])) &&
1283 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1284 /* distort image to make it show normally on terminal */
1285 pic_x = (int) ((double) pic_x / aspect_ratio);
1292 if (pic_x <= 0 || pic_y <= 0)
1295 sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1296 "-define txt:compliance=SVG txt:-",
1297 pic_x, pic_y, width, filename);
1299 strcat(cmd, " 2>/dev/null");
1301 logmsg("...opening pipe to %s", cmd);
1302 if ((pp = popen(cmd, "r")) != 0) {
1307 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1308 debugmsg2("[%5d] %s", count + 1, buffer);
1309 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1315 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1316 &col, &row, &len, dummy)) {
1317 result->name = strdup(filename);
1318 result->wide = (short) col;
1319 result->high = (short) row;
1321 begin_c_values(256);
1323 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1324 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1330 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
1331 int r, g, b, nocolor;
1335 char *s = t = strchr(buffer, '#');
1337 /* after the "#RGB", there are differences - just ignore */
1338 while (*s != '\0' && !isspace(UChar(*s)))
1343 "%d,%d: (%d,%d,%d,%d) #%x ",
1345 &r, &g, &b, &nocolor,
1347 if ((s - t) > 8) /* 6 hex digits vs 8 */
1352 check != (unsigned) ((r << 16) | (g << 8) | b)) {
1356 c = gather_c_values((int) check);
1357 which = col + (row * result->wide);
1358 result->cells[which].ch = ((in_curses ||
1362 if (c >= 0 && c < reading_last) {
1363 result->cells[which].fg = c;
1364 reading_ncols[c].count++;
1366 result->cells[which].fg = -1;
1374 finish_c_values(result);
1377 /* FIXME - is this trimming needed? */
1378 for (len = result->colors; len > 3; len--) {
1379 if (result->fgcol[len - 1].fgcol == 0) {
1380 result->colors = len - 1;
1391 result = free_pics_head(result);
1398 read_picture(const char *filename, char **data)
1401 if ((pics = parse_xbm(data)) == 0) {
1403 if ((pics = parse_xpm(data)) == 0) {
1405 if ((pics = parse_img(filename)) == 0) {
1408 warning("unexpected file-format for \"%s\"", filename);
1409 } else if (pics->high == 0 || pics->wide == 0) {
1412 pics = free_pics_head(pics);
1413 warning("no picture found in \"%s\"", filename);
1420 #define fg_color(pics,n) (pics->fgcol[n].fgcol)
1423 dump_picture(PICS_HEAD * pics)
1427 printf("Name %s\n", pics->name);
1428 printf("Size %dx%d\n", pics->high, pics->wide);
1430 for (y = 0; y < pics->colors; ++y) {
1431 if (fg_color(pics, y) < 0) {
1432 printf(" %3d: %d\n", y, fg_color(pics, y));
1434 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1437 for (y = 0; y < pics->high; ++y) {
1438 for (x = 0; x < pics->wide; ++x) {
1439 putchar(pics->cells[y * pics->wide + x].ch);
1445 #ifndef USE_DISPLAY_DRIVER
1447 init_display(const char *palette_path, int opt_d)
1449 if (isatty(fileno(stdout))) {
1457 #if HAVE_USE_DEFAULT_COLORS
1459 use_default_colors();
1461 init_palette(palette_path);
1463 scrollok(stdscr, FALSE);
1469 show_picture(PICS_HEAD * pics)
1473 int my_pair, my_color;
1475 debugmsg("called show_picture");
1476 #if USE_EXTENDED_COLORS
1477 reset_color_pairs();
1483 logmsg("...using %d colors", pics->colors);
1484 for (n = 0; n < pics->colors; ++n) {
1486 my_color = map_color(fg_color(pics, n));
1487 #if USE_EXTENDED_COLORS
1488 if (use_extended_pairs) {
1489 init_extended_pair(my_pair, my_color, my_color);
1495 init_pair((short) my_pair, (short) my_color, (short) my_color);
1498 attrset(COLOR_PAIR(1));
1501 for (y = 0; y < pics->high; ++y) {
1505 for (x = 0; x < pics->wide; ++x) {
1508 n = (y * pics->wide + x);
1509 my_pair = pics->cells[n].fg + 1;
1510 #if USE_EXTENDED_COLORS
1511 if (use_extended_pairs) {
1514 wch[0] = (wchar_t) pics->cells[n].ch;
1516 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1521 attrset(COLOR_PAIR(my_pair));
1522 addch((chtype) pics->cells[n].ch);
1526 if (slow_time >= 0) {
1528 if (slow_time > 0) {
1529 #ifdef NCURSES_VERSION
1530 napms(1000 * slow_time);
1532 sleep((unsigned) slow_time);
1536 wmove(stdscr, 0, 0);
1545 compare_fg_counts(const void *a, const void *b)
1547 const FG_NODE *p = (const FG_NODE *) a;
1548 const FG_NODE *q = (const FG_NODE *) b;
1549 return (q->count - p->count);
1553 report_colors(PICS_HEAD * pics)
1567 qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
1569 * For debugging, show a (short) list of the colors used.
1571 if (debugging && (pics->colors < 1000)) {
1573 for (j = pics->colors; j != 0; j /= 10) {
1578 logmsg("These colors were used:");
1579 high = (pics->colors + wide - 1) / wide;
1580 for (j = 0; j < high && j < pics->colors; ++j) {
1583 for (k = 0; k < wide; ++k) {
1584 int n = j + (k * high);
1585 if (n >= pics->colors)
1590 sprintf(s, "%*s", 8 - digits, " ");
1594 if (pics->fgcol[n].fgcol >= 0) {
1595 sprintf(s, "%3d #%06X %*d", n,
1596 pics->fgcol[n].fgcol,
1597 digits, pics->fgcol[n].count);
1599 sprintf(s, "%3d (empty) %*d", n,
1600 digits, pics->fgcol[n].count);
1603 if ((s - buffer) > 100)
1606 logmsg("%s", buffer);
1611 * Given the list of colors sorted by the number of times they are used,
1612 * log a short report showing the number of colors for 90%, 99%, 99.9%,
1615 logmsg("Number of colors versus number of cells");
1616 total = pics->high * pics->wide;
1620 for (j = 0; j < pics->colors; ++j) {
1621 accum += pics->fgcol[j].count;
1622 if (accum >= (total * (1.0 - level))) {
1623 int after = (shift > 2) ? shift - 2 : 0;
1624 logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
1626 (100.0 * (j + 1)) / pics->colors,
1628 after, (100.0 * accum) / total);
1638 main(int argc, char *argv[])
1643 const char *palette_path = 0;
1644 const char *rgb_path = RGB_PATH;
1646 while ((n = getopt(argc, argv, "a:dLl:p:qr:s:x:")) != -1) {
1649 if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
1650 || aspect_ratio < 0.1
1651 || aspect_ratio > 10.) {
1652 fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
1656 #if HAVE_USE_DEFAULT_COLORS
1665 if ((logfp = fopen(optarg, "a")) == 0)
1669 palette_path = optarg;
1678 slow_time = atoi(optarg);
1680 #if USE_EXTENDED_COLORS
1687 use_extended_pairs = TRUE;
1690 use_extended_colors = TRUE;
1706 if (optind < argc) {
1707 char **rgb_data = read_file(rgb_path);
1710 rgb_table = parse_rgb(rgb_data);
1712 init_display(palette_path, opt_d);
1714 giveup("expected at least one image filename");
1716 for (n = optind; n < argc; ++n) {
1718 char **data = read_file(argv[n]);
1721 warning("cannot read \"%s\"", argv[n]);
1724 if ((pics = read_picture(argv[n], data)) != 0) {
1730 report_colors(pics);
1733 free_pics_head(pics);
1736 free_data(rgb_data);
1743 cleanup(EXIT_SUCCESS);