1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 2017,2018 Free Software Foundation, Inc. *
5 * Permission is hereby granted, free of charge, to any person obtaining a *
6 * copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, distribute with modifications, sublicense, and/or sell *
10 * copies of the Software, and to permit persons to whom the Software is *
11 * furnished to do so, subject to the following conditions: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
17 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
19 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
21 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
22 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
24 * Except as contained in this notice, the name(s) of the above copyright *
25 * holders shall not be used in advertising or otherwise to promote the *
26 * sale, use or other dealings in this Software without prior written *
28 ****************************************************************************/
30 * $Id: picsmap.c,v 1.132 2020/02/02 23:34:34 tom Exp $
32 * Author: Thomas E. Dickey
34 * A little more interesting than "dots", read a simple image into memory and
35 * measure the time taken to paint it normally vs randomly.
37 * TODO improve use of rgb-names using tsearch.
39 * TODO add option to dump picture in non-optimized mode, e.g., like tput.
40 * TODO write cells/second to stderr (or log)
41 * TODO write picture left-to-right/top-to-bottom
42 * TODO write picture randomly
43 * TODO add one-shot option vs repeat-count before exiting
44 * TODO add option "-xc" for init_color vs init_extended_color
45 * TODO add option "-xa" for init_pair vs alloc_pair
46 * TODO use pad to allow pictures larger than screen
47 * TODO add option to just use convert (which can scale) vs builtin xbm/xpm.
48 * TODO add scr_dump and scr_restore calls
49 * TODO add option for assume_default_colors
51 #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 MaxSCALE 1000 /* input curses ranges 0..1000 */
76 #define MaxRGB 255 /* output color ranges 0..255 */
77 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
78 #define okSCALE(n) ((n) >= 0 && (n) <= MaxSCALE)
79 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / 255)
80 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / scale)
83 #define RGB_PATH "/etc/X11/rgb.txt"
99 #define MAX(a,b) ((a)>(b)?(a):(b))
102 * tfind will return null on failure, so we map subscripts starting at one.
104 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
105 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
107 #define pause_curses() if (in_curses) stop_curses()
109 #define debugmsg if (debugging) logmsg
110 #define debugmsg2 if (debugging) logmsg2
112 static void cleanup(int) GCC_NORETURN;
113 static void giveup(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
114 static void logmsg(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
115 static void logmsg2(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
116 static void warning(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
117 static int gather_c_values(int);
119 static FILE *logfp = 0;
120 static double aspect_ratio = 0.6;
121 static bool in_curses = FALSE;
122 static bool debugging = FALSE;
123 static bool quiet = FALSE;
124 static int slow_time = -1;
125 static RGB_NAME *rgb_table;
126 static RGB_DATA *all_colors;
127 static HOW_MUCH how_much;
129 static int reading_last;
130 static int reading_size;
131 static FG_NODE *reading_ncols;
134 static void *reading_ntree;
137 #if HAVE_ALLOC_PAIR && USE_EXTENDED_COLOR
138 #define USE_EXTENDED_COLORS 1
139 static bool use_extended_pairs = FALSE;
140 static bool use_extended_colors = FALSE;
142 #define USE_EXTENDED_COLORS 0
146 logmsg(const char *fmt, ...)
151 vfprintf(logfp, fmt, ap);
159 logmsg2(const char *fmt, ...)
164 vfprintf(logfp, fmt, ap);
174 logmsg("Allocations:");
175 logmsg("%8ld file", (long) how_much.file);
176 logmsg("%8ld name", (long) how_much.name);
177 logmsg("%8ld list", (long) how_much.list);
178 logmsg("%8ld data", (long) how_much.data);
179 logmsg("%8ld head", (long) how_much.head);
180 logmsg("%8ld pair", (long) how_much.pair);
181 logmsg("%8ld cell", (long) how_much.cell);
182 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
198 failed(const char *msg)
202 logmsg("failed with %s", strerror(save));
203 cleanup(EXIT_FAILURE);
207 warning(const char *fmt, ...)
212 vfprintf(logfp, fmt, ap);
219 vfprintf(stderr, fmt, ap);
222 cleanup(EXIT_FAILURE);
227 free_data(char **data)
236 free_pics_head(PICS_HEAD * pics)
249 begin_c_values(int size)
253 reading_ncols = typeCalloc(FG_NODE, size + 1);
254 how_much.pair += (sizeof(FG_NODE) * (size_t) size);
255 /* black is always the first slot, to work around P2I/I2P logic */
261 compare_c_values(const void *p, const void *q)
263 const int a = P2I(p);
264 const int b = P2I(q);
265 return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
270 check_c_values(int ln)
275 for (n = 0; n < reading_last; ++n) {
276 ft = tfind(I2P(n), &reading_ntree, compare_c_values);
279 if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
280 logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
284 reading_ncols[n].fgcol,
285 reading_ncols[q].fgcol);
288 logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
292 reading_ncols[n].fgcol);
299 #define check_c_values(n) /* nothing */
304 gather_c_values(int fg)
309 int next = reading_last;
311 reading_ncols[next].fgcol = fg;
312 reading_ncols[next].count = 0;
314 check_c_values(__LINE__);
315 if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
318 if (reading_last + 2 >= reading_size) {
319 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
320 int last = reading_last + 1;
321 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
327 memset(reading_ncols + last, 0,
328 sizeof(FG_NODE) * (size_t) (more - last));
329 check_c_values(__LINE__);
332 how_much.pair += sizeof(FG_NODE);
333 if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
336 logmsg("OOPS expected slot %d, got %d", next, found);
337 debugmsg("allocated color #%d as #%06X", next, fg);
338 check_c_values(__LINE__);
344 for (n = 0; n < reading_last; ++n) {
345 if (reading_ncols[n].fgcol == fg) {
351 if (reading_last + 2 >= reading_size) {
352 int more = ((reading_last + 2) * 3) / 2;
353 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
357 how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
358 how_much.pair += (sizeof(FG_NODE) * (size_t) more);
361 memset(reading_ncols + reading_last, 0,
362 sizeof(FG_NODE) * (size_t) (more - reading_last));
364 reading_ncols[reading_last].fgcol = fg;
365 found = reading_last++;
373 finish_c_values(PICS_HEAD * head)
375 head->colors = reading_last;
376 head->fgcol = reading_ncols;
384 dispose_c_values(void)
387 if (reading_ntree != 0) {
389 for (n = 0; n < reading_last; ++n) {
390 tdelete(I2P(n), &reading_ntree, compare_c_values);
395 if (reading_ncols != 0) {
404 is_file(const char *filename, struct stat *sb)
407 if (stat(filename, sb) == 0
408 && (sb->st_mode & S_IFMT) == S_IFREG
409 && sb->st_size != 0) {
412 debugmsg("is_file(%s) %d", filename, result);
417 * Simplify reading xbm/xpm files by first making an array of lines. Blank
418 * lines are filtered out.
421 read_file(const char *filename)
428 printf("** %s\n", filename);
431 if (is_file(filename, &sb)) {
432 size_t size = (size_t) sb.st_size;
433 char *blob = typeCalloc(char, size + 1);
437 result = typeCalloc(char *, size + 1);
438 how_much.file += ((size + 1) * 2);
440 if (blob != 0 && result != 0) {
441 FILE *fp = fopen(filename, "r");
443 logmsg("opened %s", filename);
445 if (fread(blob, sizeof(char), size, fp) == size) {
446 bool had_line = TRUE;
449 for (j = 0; (size_t) j < size; ++j) {
450 if (blob[j] == '\0' ||
451 (UChar(blob[j]) < 32 &&
452 !isspace(UChar(blob[j]))) ||
453 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
456 if (blob[j] == '\n') {
459 debugmsg2("[%5d] %s\n", k, result[k - 1]);
462 } else if (had_line) {
464 result[k++] = blob + j;
469 debugmsg2("[%5d] %s\n", k, result[k - 1]);
474 logmsg("cannot open %s", filename);
478 debugmsg("...file is empty");
483 debugmsg("...file is non-text");
492 static const char *msg[] =
494 "Usage: picsmap [options] [imagefile [...]]"
495 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
498 ," -a ratio aspect-ratio correction for ImageMagick"
499 #if HAVE_USE_DEFAULT_COLORS
500 ," -d invoke use_default_colors"
502 ," -L add debugging information to logfile"
503 ," -l logfile write informational messages to logfile"
504 ," -p palette color-palette file (default \"$TERM.dat\")"
506 ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
507 ," -s SECS pause for SECS seconds after display vs getch"
508 #if USE_EXTENDED_COLORS
509 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
510 ," Either/both extension may be given"
518 for (n = 0; n < SIZEOF(msg); n++)
519 fprintf(stderr, "%s\n", msg[n]);
520 cleanup(EXIT_FAILURE);
524 giveup(const char *fmt, ...)
532 vfprintf(stderr, fmt, ap);
538 vfprintf(logfp, fmt, ap);
548 * Palette files are named for $TERM values. However, there are fewer palette
549 * files than $TERM's. Although there are known problems (some cannot even get
550 * black and white correct), for the purpose of comparison, pretending that
551 * those map into "xterm" is useful.
554 read_palette(const char *filename)
556 static const char *data_dir = DATA_DIR;
558 size_t last = strlen(filename);
559 size_t need = (strlen(data_dir) + 20 + last);
560 char *full_name = malloc(need);
564 if (full_name != 0) {
566 for (tries = 0; tries < 8; ++tries) {
568 *(s = full_name) = '\0';
570 if (strchr(filename, '/') == 0) {
571 _nc_SPRINTF(full_name, _nc_SLIMIT(need) "%s/", data_dir);
577 if (((size_t) (s - full_name) + last + 1) >= need)
580 _nc_STRCAT(full_name, filename, need);
589 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
591 (tc = strchr(t, chr)) != 0 &&
592 !(strncmp) (tc, "color", 5)) {
599 if (found && (t != s)
600 && (strncmp) (s, "xterm", (size_t) (t - s))) {
601 _nc_SPRINTF(s, _nc_SLIMIT(need - (size_t) (s - full_name))
602 "xterm%s", filename + (t - s));
610 int len = (int) strlen(filename);
611 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
612 _nc_STRCAT(full_name, ".dat", need);
617 if (is_file(full_name, &sb))
622 result = read_file(full_name);
630 init_palette(const char *palette_file)
632 if (palette_file != 0) {
633 char **data = read_palette(palette_file);
635 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
636 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
638 #if HAVE_COLOR_CONTENT
641 for (cp = 0; cp < COLORS; ++cp) {
642 color_content((short) cp,
644 &all_colors[cp].green,
645 &all_colors[cp].blue);
649 memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
653 int red, green, blue;
654 int scale = MaxSCALE;
656 for (n = 0; data[n] != 0; ++n) {
657 if (sscanf(data[n], "scale:%d", &c) == 1) {
659 } else if (sscanf(data[n], "%d:%d %d %d",
669 all_colors[c].red = ScaledColor(red);
670 all_colors[c].green = ScaledColor(green);
671 all_colors[c].blue = ScaledColor(blue);
677 } else if (COLORS > 1) {
681 while (power2 < COLORS) {
686 if ((power2 != COLORS) || ((shift % 3) != 0)) {
687 if (all_colors == 0) {
688 init_palette(getenv("TERM"));
689 if (all_colors == 0) {
690 giveup("With %d colors, you need a palette-file", COLORS);
698 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
699 * direct color value.
710 int red = (value & 0xff0000) >> 16;
711 int green = (value & 0x00ff00) >> 8;
712 int blue = (value & 0x0000ff) >> 0;
714 if (all_colors != 0) {
715 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
716 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
721 red = Scaled256(red);
722 green = Scaled256(green);
723 blue = Scaled256(blue);
725 for (result = 0, n = 1; n < COLORS; ++n) {
726 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
732 } else { /* direct color */
736 while (power2 < COLORS) {
746 result = ((red << (2 * (8 - shifts)))
747 + (green << (8 - shifts))
765 static int match_c(const char *, const char *, ...) GCC_SCANFLIKE(2,3);
770 while (isspace(UChar(*s)))
776 skip_cs(const char *s)
778 while (isspace(UChar(*s)))
787 while (isgraph(UChar(*s)))
793 match_c(const char *source, const char *pattern, ...)
795 int limit = (int) strlen(source);
796 const char *last_s = source + limit;
803 va_start(ap, pattern);
806 while (*pattern != '\0') {
807 ch = UChar(*pattern++);
808 /* blank in the pattern matches zero-or-more blanks in source */
810 source = skip_cs(source);
813 /* %c, %d, %s are like sscanf except for special treatment of blanks */
814 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
819 cp = va_arg(ap, char *);
822 } while (--limit > 0);
827 ip = va_arg(ap, int *);
828 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
829 if (cp != 0 && cp != source) {
837 /* not really sscanf... */
838 limit = *va_arg(ap, int *);
842 cp = va_arg(ap, char *);
843 while (*source != '\0') {
847 } else if (found && (ch == *skip_cs(pattern))) {
859 /* other characters are matched literally */
860 if (*source++ != ch) {
869 return (*source || *pattern) ? 0 : 1;
873 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
877 /* most files use a quasi-fixed format */
878 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') {
890 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
898 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
902 if (!strcmp(arg2, "c")) {
913 parse_rgb(char **data)
917 unsigned long r, g, b;
921 RGB_NAME *result = 0;
923 for (need = 0; data[need] != 0; ++need) ;
925 result = typeCalloc(RGB_NAME, need + 2);
926 how_much.name += (sizeof(RGB_NAME) * (need + 2));
928 for (n = 0; data[n] != 0; ++n) {
929 if (strlen(t = data[n]) >= sizeof(buf) - 1)
931 if (*(s = skip_s(t)) == '!')
934 r = strtoul(s, &t, 10);
936 g = strtoul(s, &t, 10);
938 b = strtoul(s, &t, 10);
941 result[item].name = s;
943 while (t-- != s && isspace(UChar(*t))) {
946 result[item].value = (int) ((r & 0xff) << 16 |
952 result[item].name = "none";
953 result[item].value = -1;
959 lookup_rgb(const char *name)
961 RGB_NAME *result = 0;
962 if (rgb_table != 0) {
964 for (n = 0; rgb_table[n].name != 0; ++n) {
965 if (!strcasecmp(name, rgb_table[n].name)) {
966 result = &rgb_table[n];
975 parse_xbm(char **data)
988 debugmsg("called parse_xbm");
990 result = typeCalloc(PICS_HEAD, 1);
991 how_much.head += sizeof(PICS_HEAD);
995 gather_c_values(0xffffff);
997 for (n = 0; data[n] != 0; ++n) {
998 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1004 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
1005 if ((t = strstr(buf, "_width")) != 0) {
1007 result->wide = (short) bytes_of(num);
1008 } else if ((t = strstr(buf, "_height")) != 0) {
1010 result->high = (short) num;
1016 if (strcmp(result->name, buf)) {
1020 result->name = strdup(buf);
1025 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
1026 if (strcmp(result->name, buf)) {
1030 cells = (size_t) (result->wide * result->high);
1032 result->cells = typeCalloc(PICS_CELL, cells);
1033 how_much.cell += (sizeof(PICS_CELL) * cells);
1035 if ((s = strchr(s, L_CURLY)) == 0)
1042 while (*s != '\0') {
1043 while (isspace(UChar(*s))) {
1046 if (isdigit(UChar(*s))) {
1047 long value = strtol(s, &t, 0);
1049 if (t != s || value > MaxRGB || value < 0) {
1055 for (b = 0; b < 8; ++b) {
1056 if (((1L << b) & value) != 0) {
1057 result->cells[which].ch = '*';
1058 result->cells[which].fg = 1;
1059 reading_ncols[1].count++;
1061 result->cells[which].ch = ' ';
1062 result->cells[which].fg = 0;
1063 reading_ncols[0].count++;
1065 if (++which > cells) {
1071 if (*s == R_CURLY) {
1074 } else if (*s == ',') {
1085 debugmsg("...state was only %d", state);
1087 result = free_pics_head(result);
1090 finish_c_values(result);
1096 parse_xpm(char **data)
1103 int cpp = 1; /* chars per pixel */
1117 debugmsg("called parse_xpm");
1119 result = typeCalloc(PICS_HEAD, 1);
1120 how_much.head += sizeof(PICS_HEAD);
1122 for (n = 0; data[n] != 0; ++n) {
1123 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1127 if (match_c(s, " /* XPM */ ")) {
1132 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1134 result->name = strdup(arg1);
1139 if (match_c(s, " \" %d %d %d %d \" , ",
1140 num + 0, num + 1, num + 2, num + 3) ||
1141 match_c(s, " \" %d %d %d %d %d %d \" , ",
1142 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1143 result->wide = (short) num[0];
1144 result->high = (short) num[1];
1145 result->colors = num[2];
1147 begin_c_values(num[2]);
1149 cells = (result->wide * result->high);
1151 result->cells = typeCalloc(PICS_CELL, cells);
1152 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1154 list = typeCalloc(char *, result->colors + 1);
1155 how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
1162 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1166 free(list[reading_last]);
1167 list[reading_last] = strdup(arg1);
1168 if ((by_name = lookup_rgb(arg3)) != 0) {
1169 found = gather_c_values(by_name->value);
1170 } else if (*arg3 == '#') {
1171 char *rgb = arg3 + 1;
1172 unsigned long value = strtoul(rgb, &s, 16);
1173 switch ((int) strlen(rgb)) {
1177 value = (((value >> 24) & 0xff0000L)
1178 | ((value >> 16) & 0xff00L)
1179 | ((value >> 8) & 0xffL));
1182 warning("unexpected rgb value %s", rgb);
1185 found = gather_c_values((int) value);
1187 found = gather_c_values(0); /* actually an error */
1189 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1190 reading_ncols[(found >= 0) ? found : 0].fgcol);
1191 if (num_colors >= result->colors) {
1192 finish_c_values(result);
1195 list[0] = strdup("\033");
1199 if (*(cs = skip_cs(s)) == '"') {
1201 while (*cs != '\0' && *cs != '"') {
1204 /* FIXME - factor out */
1205 for (c = 0; c < result->colors; ++c) {
1207 /* should not happen... */
1210 if (!(strncmp) (cs, list[c], (size_t) cpp)) {
1211 result->cells[which].ch = list[c][0];
1212 result->cells[which].fg = c;
1213 result->fgcol[c].count++;
1218 if (result->cells[which].ch == 0) {
1219 result->cells[which].ch = '?';
1220 result->cells[which].fg = 0;
1223 if (++which >= cells) {
1227 for (c = cpp; c > 0; --c, ++cs) {
1237 if (result && list) {
1238 for (n = 0; n < result->colors; ++n)
1244 debugmsg("...state was only %d", state);
1245 result = free_pics_head(result);
1249 debugmsg("...allocated %d colors", result->colors);
1256 * The obscurely-named "convert" is provided by ImageMagick
1259 parse_img(const char *filename)
1261 size_t need = strlen(filename) + 256;
1262 char *cmd = malloc(need);
1264 char buffer[BUFSIZ];
1270 int width = in_curses ? COLS : 80;
1272 _nc_SPRINTF(cmd, _nc_SLIMIT(need) "identify \"%s\"", filename);
1274 _nc_STRCAT(cmd, " 2>/dev/null", need);
1276 logmsg("...opening pipe to %s", cmd);
1278 result = typeCalloc(PICS_HEAD, 1);
1279 how_much.head += sizeof(PICS_HEAD);
1281 if ((pp = popen(cmd, "r")) != 0) {
1282 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1283 size_t n = strlen(filename);
1284 debugmsg2("...read %s", buffer);
1285 if (strlen(buffer) > n &&
1286 !(strncmp) (buffer, filename, n) &&
1287 isspace(UChar(buffer[n])) &&
1288 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1289 /* distort image to make it show normally on terminal */
1290 pic_x = (int) ((double) pic_x / aspect_ratio);
1297 if (pic_x <= 0 || pic_y <= 0)
1300 _nc_SPRINTF(cmd, _nc_SLIMIT(need)
1301 "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1302 "-define txt:compliance=SVG txt:-",
1303 pic_x, pic_y, width, filename);
1305 _nc_STRCAT(cmd, " 2>/dev/null", need);
1307 logmsg("...opening pipe to %s", cmd);
1308 if ((pp = popen(cmd, "r")) != 0) {
1313 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1314 debugmsg2("[%5d] %s", count + 1, buffer);
1315 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1321 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1322 &col, &row, &len, dummy)) {
1323 result->name = strdup(filename);
1324 result->wide = (short) col;
1325 result->high = (short) row;
1327 begin_c_values(256);
1329 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1330 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1336 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
1337 int r, g, b, nocolor;
1340 char *s = t = strchr(buffer, '#');
1343 /* after the "#RGB", there are differences - just ignore */
1344 while (*s != '\0' && !isspace(UChar(*s)))
1349 "%d,%d: (%d,%d,%d,%d) #%x ",
1351 &r, &g, &b, &nocolor,
1355 if ((s - t) > 8) /* 6 hex digits vs 8 */
1360 check != (unsigned) ((r << 16) | (g << 8) | b)) {
1364 c = gather_c_values((int) check);
1365 which = col + (row * result->wide);
1366 result->cells[which].ch = ((in_curses ||
1370 if (c >= 0 && c < reading_last) {
1371 result->cells[which].fg = c;
1372 reading_ncols[c].count++;
1374 result->cells[which].fg = -1;
1382 finish_c_values(result);
1385 /* FIXME - is this trimming needed? */
1386 for (len = result->colors; len > 3; len--) {
1387 if (result->fgcol[len - 1].fgcol == 0) {
1388 result->colors = len - 1;
1399 result = free_pics_head(result);
1406 read_picture(const char *filename, char **data)
1409 if ((pics = parse_xbm(data)) == 0) {
1411 if ((pics = parse_xpm(data)) == 0) {
1413 if ((pics = parse_img(filename)) == 0) {
1416 warning("unexpected file-format for \"%s\"", filename);
1417 } else if (pics->high == 0 || pics->wide == 0) {
1420 pics = free_pics_head(pics);
1421 warning("no picture found in \"%s\"", filename);
1428 #define fg_color(pics,n) (pics->fgcol[n].fgcol)
1431 dump_picture(PICS_HEAD * pics)
1435 printf("Name %s\n", pics->name);
1436 printf("Size %dx%d\n", pics->high, pics->wide);
1438 for (y = 0; y < pics->colors; ++y) {
1439 if (fg_color(pics, y) < 0) {
1440 printf(" %3d: %d\n", y, fg_color(pics, y));
1442 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1445 for (y = 0; y < pics->high; ++y) {
1446 for (x = 0; x < pics->wide; ++x) {
1447 putchar(pics->cells[y * pics->wide + x].ch);
1453 #ifndef USE_DISPLAY_DRIVER
1455 init_display(const char *palette_path, int opt_d)
1458 if (isatty(fileno(stdout))) {
1466 #if HAVE_USE_DEFAULT_COLORS
1468 use_default_colors();
1470 init_palette(palette_path);
1472 scrollok(stdscr, FALSE);
1478 show_picture(PICS_HEAD * pics)
1483 debugmsg("called show_picture");
1484 logmsg("...using %dx%d screen", LINES, COLS);
1485 #if HAVE_RESET_COLOR_PAIRS
1486 reset_color_pairs();
1492 logmsg("...using %d colors", pics->colors);
1493 for (n = 0; n < pics->colors; ++n) {
1494 int my_pair = (n + 1);
1495 int my_color = map_color(fg_color(pics, n));
1496 #if USE_EXTENDED_COLORS
1497 if (use_extended_pairs) {
1498 init_extended_pair(my_pair, my_color, my_color);
1504 init_pair((short) my_pair, (short) my_color, (short) my_color);
1507 attrset(COLOR_PAIR(1));
1510 for (y = 0; y < pics->high; ++y) {
1515 for (x = 0; x < pics->wide; ++x) {
1520 n = (y * pics->wide + x);
1521 my_pair = pics->cells[n].fg + 1;
1522 #if USE_EXTENDED_COLORS
1523 if (use_extended_pairs) {
1526 wch[0] = (wchar_t) pics->cells[n].ch;
1528 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1533 attrset(COLOR_PAIR(my_pair));
1534 addch((chtype) pics->cells[n].ch);
1538 if (slow_time >= 0) {
1540 if (slow_time > 0) {
1541 #ifdef NCURSES_VERSION
1542 napms(1000 * slow_time);
1544 sleep((unsigned) slow_time);
1548 wmove(stdscr, 0, 0);
1557 compare_fg_counts(const void *a, const void *b)
1559 const FG_NODE *p = (const FG_NODE *) a;
1560 const FG_NODE *q = (const FG_NODE *) b;
1561 return (q->count - p->count);
1565 report_colors(PICS_HEAD * pics)
1577 qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
1579 * For debugging, show a (short) list of the colors used.
1581 if (debugging && (pics->colors < 1000)) {
1585 for (j = pics->colors; j != 0; j /= 10) {
1592 logmsg("These colors were used:");
1593 high = (pics->colors + wide - 1) / wide;
1594 for (j = 0; j < high && j < pics->colors; ++j) {
1598 for (k = 0; k < wide; ++k) {
1599 int n = j + (k * high);
1600 size_t want = (sizeof(buffer) - (size_t) (s - buffer));
1603 if (n >= pics->colors)
1608 _nc_SPRINTF(s, _nc_SLIMIT(want) "%*s", 8 - digits,
1613 if (pics->fgcol[n].fgcol >= 0) {
1614 _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d #%06X %*d", n,
1615 pics->fgcol[n].fgcol,
1616 digits, pics->fgcol[n].count);
1618 _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d (empty) %*d", n,
1619 digits, pics->fgcol[n].count);
1622 if ((s - buffer) > 100)
1625 logmsg("%s", buffer);
1630 * Given the list of colors sorted by the number of times they are used,
1631 * log a short report showing the number of colors for 90%, 99%, 99.9%,
1634 logmsg("Number of colors versus number of cells");
1635 total = pics->high * pics->wide;
1639 for (j = 0; j < pics->colors; ++j) {
1640 accum += pics->fgcol[j].count;
1641 if (accum >= (total * (1.0 - level))) {
1642 int after = (shift > 2) ? shift - 2 : 0;
1643 logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
1645 (100.0 * (j + 1)) / pics->colors,
1647 after, (100.0 * accum) / total);
1657 main(int argc, char *argv[])
1662 const char *palette_path = 0;
1663 const char *rgb_path = RGB_PATH;
1665 while ((n = getopt(argc, argv, "a:dLl:p:qr:s:x:")) != -1) {
1668 if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
1669 || aspect_ratio < 0.1
1670 || aspect_ratio > 10.) {
1671 fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
1675 #if HAVE_USE_DEFAULT_COLORS
1684 if ((logfp = fopen(optarg, "a")) == 0)
1688 palette_path = optarg;
1697 slow_time = atoi(optarg);
1699 #if USE_EXTENDED_COLORS
1706 use_extended_pairs = TRUE;
1709 use_extended_colors = TRUE;
1725 if (optind < argc) {
1726 char **rgb_data = read_file(rgb_path);
1729 rgb_table = parse_rgb(rgb_data);
1731 init_display(palette_path, opt_d);
1733 giveup("expected at least one image filename");
1735 for (n = optind; n < argc; ++n) {
1737 char **data = read_file(argv[n]);
1740 warning("cannot read \"%s\"", argv[n]);
1743 if ((pics = read_picture(argv[n], data)) != 0) {
1749 report_colors(pics);
1752 free_pics_head(pics);
1755 free_data(rgb_data);
1762 cleanup(EXIT_SUCCESS);