1 /****************************************************************************
2 * Copyright (c) 2017,2018 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.118 2018/03/24 22:37:42 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 MaxSCALE 1000 /* input curses ranges 0..1000 */
75 #define MaxRGB 255 /* output color ranges 0..255 */
76 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
77 #define okSCALE(n) ((n) >= 0 && (n) <= MaxSCALE)
78 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / 255)
79 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / scale)
82 #define RGB_PATH "/etc/X11/rgb.txt"
98 #define MAX(a,b) ((a)>(b)?(a):(b))
101 * tfind will return null on failure, so we map subscripts starting at one.
103 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
104 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
106 #define stop_curses() if (in_curses) endwin()
108 #define debugmsg if (debugging) logmsg
109 #define debugmsg2 if (debugging) logmsg2
111 static void cleanup(int) GCC_NORETURN;
112 static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
113 static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
114 static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
115 static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
116 static int gather_c_values(int);
118 static FILE *logfp = 0;
119 static double aspect_ratio = 0.6;
120 static bool in_curses = FALSE;
121 static bool debugging = FALSE;
122 static bool quiet = FALSE;
123 static int slow_time = -1;
124 static RGB_NAME *rgb_table;
125 static RGB_DATA *all_colors;
126 static HOW_MUCH how_much;
128 static int reading_last;
129 static int reading_size;
130 static FG_NODE *reading_ncols;
133 static void *reading_ntree;
136 #if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
137 #define USE_EXTENDED_COLORS 1
138 static bool use_extended_pairs = FALSE;
139 static bool use_extended_colors = FALSE;
141 #define USE_EXTENDED_COLORS 0
145 logmsg(const char *fmt,...)
150 vfprintf(logfp, fmt, ap);
158 logmsg2(const char *fmt,...)
163 vfprintf(logfp, fmt, ap);
173 logmsg("Allocations:");
174 logmsg("%8ld file", (long) how_much.file);
175 logmsg("%8ld name", (long) how_much.name);
176 logmsg("%8ld list", (long) how_much.list);
177 logmsg("%8ld data", (long) how_much.data);
178 logmsg("%8ld head", (long) how_much.head);
179 logmsg("%8ld pair", (long) how_much.pair);
180 logmsg("%8ld cell", (long) how_much.cell);
181 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
197 failed(const char *msg)
201 logmsg("failed with %s", strerror(save));
202 cleanup(EXIT_FAILURE);
206 warning(const char *fmt,...)
211 vfprintf(logfp, fmt, ap);
218 vfprintf(stderr, fmt, ap);
221 cleanup(EXIT_FAILURE);
226 free_data(char **data)
235 free_pics_head(PICS_HEAD * pics)
248 begin_c_values(int size)
252 reading_ncols = typeCalloc(FG_NODE, size + 1);
253 how_much.pair += (sizeof(FG_NODE) * (size_t) size);
254 /* black is always the first slot, to work around P2I/I2P logic */
260 compare_c_values(const void *p, const void *q)
262 const int a = P2I(p);
263 const int b = P2I(q);
264 return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
269 check_c_values(int ln)
274 for (n = 0; n < reading_last; ++n) {
275 ft = tfind(I2P(n), &reading_ntree, compare_c_values);
278 if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
279 logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
283 reading_ncols[n].fgcol,
284 reading_ncols[q].fgcol);
287 logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
291 reading_ncols[n].fgcol);
298 #define check_c_values(n) /* nothing */
303 gather_c_values(int fg)
308 int next = reading_last;
310 reading_ncols[next].fgcol = fg;
311 reading_ncols[next].count = 0;
313 check_c_values(__LINE__);
314 if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
317 if (reading_last + 2 >= reading_size) {
318 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
319 int last = reading_last + 1;
320 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
326 memset(reading_ncols + last, 0,
327 sizeof(FG_NODE) * (size_t) (more - last));
328 check_c_values(__LINE__);
331 how_much.pair += sizeof(FG_NODE);
332 if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
335 logmsg("OOPS expected slot %d, got %d", next, found);
336 debugmsg("allocated color #%d as #%06X", next, fg);
337 check_c_values(__LINE__);
343 for (n = 0; n < reading_last; ++n) {
344 if (reading_ncols[n].fgcol == fg) {
350 if (reading_last + 2 >= reading_size) {
351 int more = ((reading_last + 2) * 3) / 2;
352 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
356 how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
357 how_much.pair += (sizeof(FG_NODE) * (size_t) more);
360 memset(reading_ncols + reading_last, 0,
361 sizeof(FG_NODE) * (size_t) (more - reading_last));
363 reading_ncols[reading_last].fgcol = fg;
364 found = reading_last++;
372 finish_c_values(PICS_HEAD * head)
374 head->colors = reading_last;
375 head->fgcol = reading_ncols;
382 #if HAVE_TSEARCH && HAVE_TDESTROY
384 never_free(void *node GCC_UNUSED)
390 dispose_c_values(void)
393 if (reading_ntree != 0) {
395 tdestroy(reading_ntree, never_free);
398 for (n = 0; n < reading_last; ++n) {
399 tdelete(I2P(n), &reading_ntree, compare_c_values);
405 if (reading_ncols != 0) {
414 is_file(const char *filename, struct stat *sb)
417 if (stat(filename, sb) == 0
418 && (sb->st_mode & S_IFMT) == S_IFREG
419 && sb->st_size != 0) {
422 debugmsg("is_file(%s) %d", filename, result);
427 * Simplify reading xbm/xpm files by first making an array of lines. Blank
428 * lines are filtered out.
431 read_file(const char *filename)
438 printf("** %s\n", filename);
441 if (is_file(filename, &sb)) {
442 size_t size = (size_t) sb.st_size;
443 char *blob = typeCalloc(char, size + 1);
444 bool had_line = TRUE;
449 result = typeCalloc(char *, size + 1);
450 how_much.file += ((size + 1) * 2);
452 if (blob != 0 && result != 0) {
453 FILE *fp = fopen(filename, "r");
455 logmsg("opened %s", filename);
456 if (fread(blob, sizeof(char), size, fp) == size) {
457 for (j = 0; (size_t) j < size; ++j) {
458 if (blob[j] == '\0' ||
459 (UChar(blob[j]) < 32 &&
460 !isspace(UChar(blob[j]))) ||
461 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
464 if (blob[j] == '\n') {
467 debugmsg2("[%5d] %s\n", k, result[k - 1]);
470 } else if (had_line) {
472 result[k++] = blob + j;
477 debugmsg2("[%5d] %s\n", k, result[k - 1]);
482 logmsg("cannot open %s", filename);
486 debugmsg("...file is empty");
491 debugmsg("...file is non-text");
500 static const char *msg[] =
502 "Usage: picsmap [options] [imagefile [...]]"
503 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
506 ," -a ratio aspect-ratio correction for ImageMagick"
507 #if HAVE_USE_DEFAULT_COLORS
508 ," -d invoke use_default_colors"
510 ," -L add debugging information to logfile"
511 ," -l logfile write informational messages to logfile"
512 ," -p palette color-palette file (default \"$TERM.dat\")"
514 ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
515 ," -s SECS pause for SECS seconds after display vs getch"
516 #if USE_EXTENDED_COLORS
517 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
518 ," Either/both extension may be given"
526 for (n = 0; n < SIZEOF(msg); n++)
527 fprintf(stderr, "%s\n", msg[n]);
528 cleanup(EXIT_FAILURE);
532 giveup(const char *fmt,...)
540 vfprintf(stderr, fmt, ap);
546 vfprintf(logfp, fmt, ap);
556 * Palette files are named for $TERM values. However, there are fewer palette
557 * files than $TERM's. Although there are known problems (some cannot even get
558 * black and white correct), for the purpose of comparison, pretending that
559 * those map into "xterm" is useful.
562 read_palette(const char *filename)
564 static const char *data_dir = DATA_DIR;
566 char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
570 if (full_name != 0) {
572 for (tries = 0; tries < 8; ++tries) {
574 *(s = full_name) = '\0';
576 if (strchr(filename, '/') == 0) {
577 sprintf(full_name, "%s/", data_dir);
592 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
594 !(strncmp) (strchr(t, chr), "color", 5)) {
601 if (found && (t != s)
602 && (strncmp) (s, "xterm", (size_t) (t - s))) {
603 sprintf(s, "xterm%s", filename + (t - s));
611 int len = (int) strlen(filename);
612 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
618 if (is_file(full_name, &sb))
623 result = read_file(full_name);
631 init_palette(const char *palette_file)
633 if (palette_file != 0) {
634 char **data = read_palette(palette_file);
636 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
637 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
639 #if HAVE_COLOR_CONTENT
642 for (cp = 0; cp < COLORS; ++cp) {
643 color_content((short) cp,
645 &all_colors[cp].green,
646 &all_colors[cp].blue);
650 memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
654 int red, green, blue;
655 int scale = MaxSCALE;
657 for (n = 0; data[n] != 0; ++n) {
658 if (sscanf(data[n], "scale:%d", &c) == 1) {
660 } else if (sscanf(data[n], "%d:%d %d %d",
670 all_colors[c].red = ScaledColor(red);
671 all_colors[c].green = ScaledColor(green);
672 all_colors[c].blue = ScaledColor(blue);
678 } else if (COLORS > 1) {
682 while (power2 < COLORS) {
687 if ((power2 != COLORS) || ((shift % 3) != 0)) {
688 if (all_colors == 0) {
689 init_palette(getenv("TERM"));
691 if (all_colors == 0) {
692 giveup("With %d colors, you need a palette-file", COLORS);
699 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
700 * direct color value.
711 int red = (value & 0xff0000) >> 16;
712 int green = (value & 0x00ff00) >> 8;
713 int blue = (value & 0x0000ff) >> 0;
715 if (all_colors != 0) {
716 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
717 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
722 red = Scaled256(red);
723 green = Scaled256(green);
724 blue = Scaled256(blue);
726 for (result = 0, n = 1; n < COLORS; ++n) {
727 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
733 } else { /* direct color */
737 while (power2 < COLORS) {
747 result = ((red << (2 * (8 - shifts)))
748 + (green << (8 - shifts))
766 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
771 while (isspace(UChar(*s)))
777 skip_cs(const char *s)
779 while (isspace(UChar(*s)))
788 while (isgraph(UChar(*s)))
794 match_c(const char *source, const char *pattern,...)
796 int limit = (int) strlen(source);
797 const char *last_s = source + limit;
804 va_start(ap, pattern);
807 while (*pattern != '\0') {
808 ch = UChar(*pattern++);
809 /* blank in the pattern matches zero-or-more blanks in source */
811 source = skip_cs(source);
814 /* %c, %d, %s are like sscanf except for special treatment of blanks */
815 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
820 cp = va_arg(ap, char *);
823 } while (--limit > 0);
828 ip = va_arg(ap, int *);
829 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
830 if (cp != 0 && cp != source) {
838 /* not really sscanf... */
839 limit = *va_arg(ap, int *);
843 cp = va_arg(ap, char *);
844 while (*source != '\0') {
848 } else if (found && (ch == *skip_cs(pattern))) {
860 /* other characters are matched literally */
861 if (*source++ != ch) {
870 return (*source || *pattern) ? 0 : 1;
874 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
878 /* most files use a quasi-fixed format */
879 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
884 const char *s = skip_cs(source);
885 size_t have = strlen(source);
887 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
888 memcpy(arg1, s, (size_t) cpp);
890 while (*s++ == '\t') {
891 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
899 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
903 if (!strcmp(arg2, "c")) {
914 parse_rgb(char **data)
918 unsigned long r, g, b;
922 RGB_NAME *result = 0;
924 for (need = 0; data[need] != 0; ++need) ;
926 result = typeCalloc(RGB_NAME, need + 2);
927 how_much.name += (sizeof(RGB_NAME) * (need + 2));
929 for (n = 0; data[n] != 0; ++n) {
930 if (strlen(t = data[n]) >= sizeof(buf) - 1)
932 if (*(s = skip_s(t)) == '!')
935 r = strtoul(s, &t, 10);
937 g = strtoul(s, &t, 10);
939 b = strtoul(s, &t, 10);
942 result[item].name = s;
944 while (t-- != s && isspace(UChar(*t))) {
947 result[item].value = (int) ((r & 0xff) << 16 |
953 result[item].name = "none";
954 result[item].value = -1;
960 lookup_rgb(const char *name)
962 RGB_NAME *result = 0;
963 if (rgb_table != 0) {
965 for (n = 0; rgb_table[n].name != 0; ++n) {
966 if (!strcasecmp(name, rgb_table[n].name)) {
967 result = &rgb_table[n];
976 parse_xbm(char **data)
989 debugmsg("called parse_xbm");
991 result = typeCalloc(PICS_HEAD, 1);
992 how_much.head += sizeof(PICS_HEAD);
996 gather_c_values(0xffffff);
998 for (n = 0; data[n] != 0; ++n) {
999 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1005 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
1006 if ((t = strstr(buf, "_width")) != 0) {
1008 result->wide = (short) bytes_of(num);
1009 } else if ((t = strstr(buf, "_height")) != 0) {
1011 result->high = (short) num;
1015 if (strcmp(result->name, buf)) {
1019 result->name = strdup(buf);
1024 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
1025 if (strcmp(result->name, buf)) {
1029 cells = (size_t) (result->wide * result->high);
1031 result->cells = typeCalloc(PICS_CELL, cells);
1032 how_much.cell += (sizeof(PICS_CELL) * cells);
1034 if ((s = strchr(s, L_CURLY)) == 0)
1041 while (*s != '\0') {
1042 while (isspace(UChar(*s))) {
1045 if (isdigit(UChar(*s))) {
1046 long value = strtol(s, &t, 0);
1048 if (t != s || value > MaxRGB || value < 0) {
1054 for (b = 0; b < 8; ++b) {
1055 if (((1L << b) & value) != 0) {
1056 result->cells[which].ch = '*';
1057 result->cells[which].fg = 1;
1058 reading_ncols[1].count++;
1060 result->cells[which].ch = ' ';
1061 result->cells[which].fg = 0;
1062 reading_ncols[0].count++;
1064 if (++which > cells) {
1070 if (*s == R_CURLY) {
1073 } else if (*s == ',') {
1084 debugmsg("...state was only %d", state);
1086 result = free_pics_head(result);
1089 finish_c_values(result);
1095 parse_xpm(char **data)
1102 int cpp = 1; /* chars per pixel */
1116 debugmsg("called parse_xpm");
1118 result = typeCalloc(PICS_HEAD, 1);
1119 how_much.head += sizeof(PICS_HEAD);
1121 for (n = 0; data[n] != 0; ++n) {
1122 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1126 if (match_c(s, " /* XPM */ ")) {
1131 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1133 result->name = strdup(arg1);
1138 if (match_c(s, " \" %d %d %d %d \" , ",
1139 num + 0, num + 1, num + 2, num + 3) ||
1140 match_c(s, " \" %d %d %d %d %d %d \" , ",
1141 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1142 result->wide = (short) num[0];
1143 result->high = (short) num[1];
1144 result->colors = num[2];
1146 begin_c_values(num[2]);
1148 cells = (result->wide * result->high);
1150 result->cells = typeCalloc(PICS_CELL, cells);
1151 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1153 list = typeCalloc(char *, result->colors + 1);
1154 how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
1161 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1165 free(list[reading_last]);
1166 list[reading_last] = strdup(arg1);
1167 if ((by_name = lookup_rgb(arg3)) != 0) {
1168 found = gather_c_values(by_name->value);
1169 } else if (*arg3 == '#') {
1170 char *rgb = arg3 + 1;
1171 unsigned long value = strtoul(rgb, &s, 16);
1172 switch ((int) strlen(rgb)) {
1176 value = (((value >> 24) & 0xff0000L)
1177 | ((value >> 16) & 0xff00L)
1178 | ((value >> 8) & 0xffL));
1181 warning("unexpected rgb value %s", rgb);
1184 found = gather_c_values((int) value);
1186 found = gather_c_values(0); /* actually an error */
1188 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1189 reading_ncols[(found >= 0) ? found : 0].fgcol);
1190 if (num_colors >= result->colors) {
1191 finish_c_values(result);
1193 if (list != 0 && list[0] == 0)
1194 list[0] = strdup("\033");
1198 if (*(cs = skip_cs(s)) == '"') {
1200 while (*cs != '\0' && *cs != '"') {
1203 /* FIXME - factor out */
1204 for (c = 0; c < result->colors; ++c) {
1206 /* should not happen... */
1209 if (!(strncmp) (cs, list[c], (size_t) cpp)) {
1210 result->cells[which].ch = list[c][0];
1211 result->cells[which].fg = c;
1212 result->fgcol[c].count++;
1217 if (result->cells[which].ch == 0) {
1218 result->cells[which].ch = '?';
1219 result->cells[which].fg = 0;
1222 if (++which >= cells) {
1226 for (c = cpp; c > 0; --c, ++cs) {
1236 if (result && list) {
1237 for (n = 0; n < result->colors; ++n)
1243 debugmsg("...state was only %d", state);
1244 result = free_pics_head(result);
1248 debugmsg("...allocated %d colors", result->colors);
1255 * The obscurely-named "convert" is provided by ImageMagick
1258 parse_img(const char *filename)
1260 char *cmd = malloc(strlen(filename) + 256);
1262 char buffer[BUFSIZ];
1268 int width = in_curses ? COLS : 80;
1270 sprintf(cmd, "identify \"%s\"", filename);
1272 strcat(cmd, " 2>/dev/null");
1274 logmsg("...opening pipe to %s", cmd);
1276 result = typeCalloc(PICS_HEAD, 1);
1277 how_much.head += sizeof(PICS_HEAD);
1279 if ((pp = popen(cmd, "r")) != 0) {
1280 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1281 size_t n = strlen(filename);
1282 debugmsg2("...read %s", buffer);
1283 if (strlen(buffer) > n &&
1284 !(strncmp) (buffer, filename, n) &&
1285 isspace(UChar(buffer[n])) &&
1286 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1287 /* distort image to make it show normally on terminal */
1288 pic_x = (int) ((double) pic_x / aspect_ratio);
1295 if (pic_x <= 0 || pic_y <= 0)
1298 sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1299 "-define txt:compliance=SVG txt:-",
1300 pic_x, pic_y, width, filename);
1302 strcat(cmd, " 2>/dev/null");
1304 logmsg("...opening pipe to %s", cmd);
1305 if ((pp = popen(cmd, "r")) != 0) {
1310 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1311 debugmsg2("[%5d] %s", count + 1, buffer);
1312 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1318 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1319 &col, &row, &len, dummy)) {
1320 result->name = strdup(filename);
1321 result->wide = (short) col;
1322 result->high = (short) row;
1324 begin_c_values(256);
1326 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1327 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1333 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
1334 int r, g, b, nocolor;
1338 char *s = t = strchr(buffer, '#');
1340 /* after the "#RGB", there are differences - just ignore */
1341 while (*s != '\0' && !isspace(UChar(*s)))
1346 "%d,%d: (%d,%d,%d,%d) #%x ",
1348 &r, &g, &b, &nocolor,
1350 if ((s - t) > 8) /* 6 hex digits vs 8 */
1355 check != (unsigned) ((r << 16) | (g << 8) | b)) {
1359 c = gather_c_values((int) check);
1360 which = col + (row * result->wide);
1361 result->cells[which].ch = ((in_curses ||
1365 if (c >= 0 && c < reading_last) {
1366 result->cells[which].fg = c;
1367 reading_ncols[c].count++;
1369 result->cells[which].fg = -1;
1377 finish_c_values(result);
1380 /* FIXME - is this trimming needed? */
1381 for (len = result->colors; len > 3; len--) {
1382 if (result->fgcol[len - 1].fgcol == 0) {
1383 result->colors = len - 1;
1394 result = free_pics_head(result);
1401 read_picture(const char *filename, char **data)
1404 if ((pics = parse_xbm(data)) == 0) {
1406 if ((pics = parse_xpm(data)) == 0) {
1408 if ((pics = parse_img(filename)) == 0) {
1411 warning("unexpected file-format for \"%s\"", filename);
1412 } else if (pics->high == 0 || pics->wide == 0) {
1415 pics = free_pics_head(pics);
1416 warning("no picture found in \"%s\"", filename);
1423 #define fg_color(pics,n) (pics->fgcol[n].fgcol)
1426 dump_picture(PICS_HEAD * pics)
1430 printf("Name %s\n", pics->name);
1431 printf("Size %dx%d\n", pics->high, pics->wide);
1433 for (y = 0; y < pics->colors; ++y) {
1434 if (fg_color(pics, y) < 0) {
1435 printf(" %3d: %d\n", y, fg_color(pics, y));
1437 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1440 for (y = 0; y < pics->high; ++y) {
1441 for (x = 0; x < pics->wide; ++x) {
1442 putchar(pics->cells[y * pics->wide + x].ch);
1448 #ifndef USE_DISPLAY_DRIVER
1450 init_display(const char *palette_path, int opt_d)
1452 if (isatty(fileno(stdout))) {
1460 #if HAVE_USE_DEFAULT_COLORS
1462 use_default_colors();
1464 init_palette(palette_path);
1466 scrollok(stdscr, FALSE);
1472 show_picture(PICS_HEAD * pics)
1476 int my_pair, my_color;
1478 debugmsg("called show_picture");
1479 logmsg("...using %dx%d screen", LINES, COLS);
1480 #if HAVE_RESET_COLOR_PAIRS
1481 reset_color_pairs();
1487 logmsg("...using %d colors", pics->colors);
1488 for (n = 0; n < pics->colors; ++n) {
1490 my_color = map_color(fg_color(pics, n));
1491 #if USE_EXTENDED_COLORS
1492 if (use_extended_pairs) {
1493 init_extended_pair(my_pair, my_color, my_color);
1499 init_pair((short) my_pair, (short) my_color, (short) my_color);
1502 attrset(COLOR_PAIR(1));
1505 for (y = 0; y < pics->high; ++y) {
1509 for (x = 0; x < pics->wide; ++x) {
1512 n = (y * pics->wide + x);
1513 my_pair = pics->cells[n].fg + 1;
1514 #if USE_EXTENDED_COLORS
1515 if (use_extended_pairs) {
1518 wch[0] = (wchar_t) pics->cells[n].ch;
1520 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1525 attrset(COLOR_PAIR(my_pair));
1526 addch((chtype) pics->cells[n].ch);
1530 if (slow_time >= 0) {
1532 if (slow_time > 0) {
1533 #ifdef NCURSES_VERSION
1534 napms(1000 * slow_time);
1536 sleep((unsigned) slow_time);
1540 wmove(stdscr, 0, 0);
1549 compare_fg_counts(const void *a, const void *b)
1551 const FG_NODE *p = (const FG_NODE *) a;
1552 const FG_NODE *q = (const FG_NODE *) b;
1553 return (q->count - p->count);
1557 report_colors(PICS_HEAD * pics)
1571 qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
1573 * For debugging, show a (short) list of the colors used.
1575 if (debugging && (pics->colors < 1000)) {
1577 for (j = pics->colors; j != 0; j /= 10) {
1582 logmsg("These colors were used:");
1583 high = (pics->colors + wide - 1) / wide;
1584 for (j = 0; j < high && j < pics->colors; ++j) {
1587 for (k = 0; k < wide; ++k) {
1588 int n = j + (k * high);
1589 if (n >= pics->colors)
1594 sprintf(s, "%*s", 8 - digits, " ");
1598 if (pics->fgcol[n].fgcol >= 0) {
1599 sprintf(s, "%3d #%06X %*d", n,
1600 pics->fgcol[n].fgcol,
1601 digits, pics->fgcol[n].count);
1603 sprintf(s, "%3d (empty) %*d", n,
1604 digits, pics->fgcol[n].count);
1607 if ((s - buffer) > 100)
1610 logmsg("%s", buffer);
1615 * Given the list of colors sorted by the number of times they are used,
1616 * log a short report showing the number of colors for 90%, 99%, 99.9%,
1619 logmsg("Number of colors versus number of cells");
1620 total = pics->high * pics->wide;
1624 for (j = 0; j < pics->colors; ++j) {
1625 accum += pics->fgcol[j].count;
1626 if (accum >= (total * (1.0 - level))) {
1627 int after = (shift > 2) ? shift - 2 : 0;
1628 logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
1630 (100.0 * (j + 1)) / pics->colors,
1632 after, (100.0 * accum) / total);
1642 main(int argc, char *argv[])
1647 const char *palette_path = 0;
1648 const char *rgb_path = RGB_PATH;
1650 while ((n = getopt(argc, argv, "a:dLl:p:qr:s:x:")) != -1) {
1653 if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
1654 || aspect_ratio < 0.1
1655 || aspect_ratio > 10.) {
1656 fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
1660 #if HAVE_USE_DEFAULT_COLORS
1669 if ((logfp = fopen(optarg, "a")) == 0)
1673 palette_path = optarg;
1682 slow_time = atoi(optarg);
1684 #if USE_EXTENDED_COLORS
1691 use_extended_pairs = TRUE;
1694 use_extended_colors = TRUE;
1710 if (optind < argc) {
1711 char **rgb_data = read_file(rgb_path);
1714 rgb_table = parse_rgb(rgb_data);
1716 init_display(palette_path, opt_d);
1718 giveup("expected at least one image filename");
1720 for (n = optind; n < argc; ++n) {
1722 char **data = read_file(argv[n]);
1725 warning("cannot read \"%s\"", argv[n]);
1728 if ((pics = read_picture(argv[n], data)) != 0) {
1734 report_colors(pics);
1737 free_pics_head(pics);
1740 free_data(rgb_data);
1747 cleanup(EXIT_SUCCESS);