1 /****************************************************************************
2 * Copyright 2018-2022,2023 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.149 2023/04/23 23:20:37 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>
60 #undef CUR /* use only the curses interface */
68 #define MaxSCALE 1000 /* input curses ranges 0..1000 */
69 #define MaxRGB 255 /* output color ranges 0..255 */
70 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
71 #define okSCALE(n) ((n) >= 0 && (n) <= MaxSCALE)
72 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / 255)
73 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / scale)
76 #define RGB_PATH "/etc/X11/rgb.txt"
92 #define MAX(a,b) ((a)>(b)?(a):(b))
95 * tfind will return null on failure, so we map subscripts starting at one.
97 #define P2I(n) (((int)(my_intptr_t)(n)) - 1)
98 #define I2P(n) (void *)(my_intptr_t)((n) + 1)
100 #define pause_curses() if (in_curses) stop_curses()
102 #define debugmsg if (debugging) logmsg
103 #define debugmsg2 if (debugging) logmsg2
105 static GCC_NORETURN void cleanup(int);
106 static void giveup(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
107 static void logmsg(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
108 static void logmsg2(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
109 static void warning(const char *fmt, ...) GCC_PRINTFLIKE(1, 2);
110 static int gather_c_values(int);
112 static FILE *logfp = 0;
113 static double aspect_ratio = 0.6;
114 static bool in_curses = FALSE;
115 static bool debugging = FALSE;
116 static bool quiet = FALSE;
117 static int slow_time = -1;
118 static RGB_NAME *rgb_table;
119 static RGB_DATA *all_colors;
120 static HOW_MUCH how_much;
122 static int reading_last;
123 static int reading_size;
124 static FG_NODE *reading_ncols;
127 static void *reading_ntree;
130 #if HAVE_ALLOC_PAIR && USE_EXTENDED_COLOR
131 #define USE_EXTENDED_COLORS 1
132 static bool use_extended_pairs = FALSE;
133 static bool use_extended_colors = FALSE;
135 #define USE_EXTENDED_COLORS 0
139 logmsg(const char *fmt, ...)
144 vfprintf(logfp, fmt, ap);
152 logmsg2(const char *fmt, ...)
157 vfprintf(logfp, fmt, ap);
167 logmsg("Allocations:");
168 logmsg("%8ld file", (long) how_much.file);
169 logmsg("%8ld name", (long) how_much.name);
170 logmsg("%8ld list", (long) how_much.list);
171 logmsg("%8ld data", (long) how_much.data);
172 logmsg("%8ld head", (long) how_much.head);
173 logmsg("%8ld pair", (long) how_much.pair);
174 logmsg("%8ld cell", (long) how_much.cell);
175 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
191 failed(const char *msg)
195 logmsg("failed with %s", strerror(save));
196 cleanup(EXIT_FAILURE);
200 warning(const char *fmt, ...)
205 vfprintf(logfp, fmt, ap);
212 vfprintf(stderr, fmt, ap);
215 cleanup(EXIT_FAILURE);
220 free_data(char **data)
229 free_pics_head(PICS_HEAD * pics)
242 begin_c_values(int size)
246 reading_ncols = typeCalloc(FG_NODE, size + 1);
247 how_much.pair += (sizeof(FG_NODE) * (size_t) size);
248 /* black is always the first slot, to work around P2I/I2P logic */
254 compare_c_values(const void *p, const void *q)
256 const int a = P2I(p);
257 const int b = P2I(q);
258 return (reading_ncols[a].fgcol - reading_ncols[b].fgcol);
263 check_c_values(int ln)
268 for (n = 0; n < reading_last; ++n) {
269 ft = tfind(I2P(n), &reading_ntree, compare_c_values);
272 if (reading_ncols[q].fgcol != reading_ncols[n].fgcol) {
273 logmsg("@%d, %d:%d (%d) %d %d fgcol %06X %06X", ln, n,
277 reading_ncols[n].fgcol,
278 reading_ncols[q].fgcol);
281 logmsg("@%d, %d:%d (%d) ? %d null %06X", ln, n,
285 reading_ncols[n].fgcol);
292 #define check_c_values(n) /* nothing */
297 gather_c_values(int fg)
302 int next = reading_last;
304 reading_ncols[next].fgcol = fg;
305 reading_ncols[next].count = 0;
307 check_c_values(__LINE__);
308 if ((ft = tfind(I2P(next), &reading_ntree, compare_c_values)) != 0) {
311 if (reading_last + 2 >= reading_size) {
312 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
313 int last = reading_last + 1;
314 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
320 memset(reading_ncols + last, 0,
321 sizeof(FG_NODE) * (size_t) (more - last));
322 check_c_values(__LINE__);
325 how_much.pair += sizeof(FG_NODE);
326 if ((ft = tsearch(I2P(next), &reading_ntree, compare_c_values)) != 0) {
329 logmsg("OOPS expected slot %d, got %d", next, found);
330 debugmsg("allocated color #%d as #%06X", next, fg);
331 check_c_values(__LINE__);
337 for (n = 0; n < reading_last; ++n) {
338 if (reading_ncols[n].fgcol == fg) {
344 if (reading_last + 2 >= reading_size) {
345 int more = ((reading_last + 2) * 3) / 2;
346 FG_NODE *p = typeRealloc(FG_NODE, more, reading_ncols);
350 how_much.pair -= (sizeof(FG_NODE) * (size_t) reading_size);
351 how_much.pair += (sizeof(FG_NODE) * (size_t) more);
354 memset(reading_ncols + reading_last, 0,
355 sizeof(FG_NODE) * (size_t) (more - reading_last));
357 reading_ncols[reading_last].fgcol = fg;
358 found = reading_last++;
366 finish_c_values(PICS_HEAD * head)
368 head->colors = reading_last;
369 head->fgcol = reading_ncols;
377 dispose_c_values(void)
380 if (reading_ntree != 0) {
382 for (n = 0; n < reading_last; ++n) {
383 tdelete(I2P(n), &reading_ntree, compare_c_values);
388 if (reading_ncols != 0) {
397 is_file(const char *filename, struct stat *sb)
400 if (stat(filename, sb) == 0
401 && (sb->st_mode & S_IFMT) == S_IFREG
402 && sb->st_size != 0) {
405 debugmsg("is_file(%s) %d", filename, result);
410 * Simplify reading xbm/xpm files by first making an array of lines. Blank
411 * lines are filtered out.
414 read_file(const char *filename)
421 printf("** %s\n", filename);
424 if (is_file(filename, &sb)) {
425 size_t size = (size_t) sb.st_size;
426 char *blob = typeCalloc(char, size + 1);
430 result = typeCalloc(char *, size + 1);
431 how_much.file += ((size + 1) * 2);
433 if (blob != 0 && result != 0) {
434 FILE *fp = fopen(filename, "r");
436 logmsg("opened %s", filename);
438 if (fread(blob, sizeof(char), size, fp) == size) {
439 bool had_line = TRUE;
442 for (j = 0; (size_t) j < size; ++j) {
443 if (blob[j] == '\0' ||
444 (UChar(blob[j]) < 32 &&
445 !isspace(UChar(blob[j]))) ||
446 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
449 if (blob[j] == '\n') {
452 debugmsg2("[%5d] %s\n", k, result[k - 1]);
455 } else if (had_line) {
457 result[k++] = blob + j;
462 debugmsg2("[%5d] %s\n", k, result[k - 1]);
467 logmsg("cannot open %s", filename);
471 debugmsg("...file is empty");
476 debugmsg("...file is non-text");
485 static const char *msg[] =
487 "Usage: picsmap [options] [imagefile [...]]"
488 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
492 ," -a ratio aspect-ratio correction for ImageMagick"
493 #if HAVE_USE_DEFAULT_COLORS
494 ," -d invoke use_default_colors"
496 ," -L add debugging information to logfile"
497 ," -l FILE write informational messages to FILE"
498 ," -p FILE color-palette file (default \"$TERM.dat\")"
500 ," -r FILE xpm uses X rgb color-names in FILE (default \"" RGB_PATH "\")"
501 ," -s SECS pause for SECS seconds after display vs getch"
502 #if USE_EXTENDED_COLORS
503 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
504 ," Either/both extension may be given"
512 for (n = 0; n < SIZEOF(msg); n++)
513 fprintf(stderr, "%s\n", msg[n]);
514 cleanup(ok ? EXIT_SUCCESS : EXIT_FAILURE);
518 giveup(const char *fmt, ...)
526 vfprintf(stderr, fmt, ap);
532 vfprintf(logfp, fmt, ap);
542 * Palette files are named for $TERM values. However, there are fewer palette
543 * files than $TERM's. Although there are known problems (some cannot even get
544 * black and white correct), for the purpose of comparison, pretending that
545 * those map into "xterm" is useful.
548 read_palette(const char *filename)
550 static const char *data_dir = DATA_DIR;
552 size_t last = strlen(filename);
553 size_t need = (strlen(data_dir) + 20 + last);
554 char *full_name = malloc(need);
558 if (full_name != 0) {
560 for (tries = 0; tries < 8; ++tries) {
562 *(s = full_name) = '\0';
564 if (strchr(filename, '/') == 0) {
565 _nc_SPRINTF(full_name, _nc_SLIMIT(need) "%s/", data_dir);
571 if (((size_t) (s - full_name) + last + 1) >= need)
574 _nc_STRCAT(full_name, filename, need);
583 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
585 (tc = strchr(t, chr)) != 0 &&
586 !(strncmp) (tc, "color", 5)) {
593 if (found && (t != s)
594 && (strncmp) (s, "xterm", (size_t) (t - s))) {
595 _nc_SPRINTF(s, _nc_SLIMIT(need - (size_t) (s - full_name))
596 "xterm%s", filename + (t - s));
603 int len = (int) strlen(filename);
604 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
605 _nc_STRCAT(full_name, ".dat", need);
610 if (is_file(full_name, &sb))
615 result = read_file(full_name);
623 init_palette(const char *palette_file)
625 if (palette_file != 0) {
626 char **data = read_palette(palette_file);
628 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
629 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
631 #if HAVE_COLOR_CONTENT
634 for (cp = 0; cp < COLORS; ++cp) {
635 color_content((short) cp,
637 &all_colors[cp].green,
638 &all_colors[cp].blue);
642 memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
646 int red, green, blue;
647 int scale = MaxSCALE;
649 for (n = 0; data[n] != 0; ++n) {
650 if (sscanf(data[n], "scale:%d", &c) == 1) {
652 } else if (sscanf(data[n], "%d:%d %d %d",
662 all_colors[c].red = ScaledColor(red);
663 all_colors[c].green = ScaledColor(green);
664 all_colors[c].blue = ScaledColor(blue);
670 } else if (COLORS > 1) {
674 while (power2 < COLORS) {
679 if ((power2 != COLORS) || ((shift % 3) != 0)) {
680 if (all_colors == 0) {
681 init_palette(getenv("TERM"));
682 if (all_colors == 0) {
683 giveup("With %d colors, you need a palette-file", COLORS);
691 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
692 * direct color value.
703 int red = (value & 0xff0000) >> 16;
704 int green = (value & 0x00ff00) >> 8;
705 int blue = (value & 0x0000ff) >> 0;
707 if (all_colors != 0) {
708 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
709 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
714 red = Scaled256(red);
715 green = Scaled256(green);
716 blue = Scaled256(blue);
718 for (result = 0, n = 1; n < COLORS; ++n) {
719 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
725 } else { /* direct color */
729 while (power2 < COLORS) {
739 result = ((red << (2 * (8 - shifts)))
740 + (green << (8 - shifts))
758 static int match_c(const char *, const char *, ...) GCC_SCANFLIKE(2,3);
763 while (isspace(UChar(*s)))
769 skip_cs(const char *s)
771 while (isspace(UChar(*s)))
780 while (isgraph(UChar(*s)))
786 match_c(const char *source, const char *pattern, ...)
788 int limit = (int) strlen(source);
789 const char *last_s = source + limit;
797 va_start(ap, pattern);
800 while (*pattern != '\0') {
801 ch = UChar(*pattern++);
802 /* blank in the pattern matches zero-or-more blanks in source */
804 source = skip_cs(source);
807 /* %c, %d, %s are like sscanf except for special treatment of blanks */
808 if (ch == '%' && *pattern != '\0' && strchr("%cdnfsx", *pattern)) {
816 cp = va_arg(ap, char *);
819 } while (--limit > 0);
824 ip = va_arg(ap, int *);
825 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
826 if (cp != 0 && cp != source) {
834 /* floating point for pixels... */
835 fp = va_arg(ap, float *);
836 lv = strtol(source, &cp, 10);
837 if (cp == 0 || cp == source)
841 if (*source == '.') {
842 lv = strtol(++source, &cp, 10);
843 if (cp == 0 || cp == source)
847 int digits = (int) (cp - source);
848 while (digits-- > 0) {
851 *fp += (float) lv / scale;
857 /* not really sscanf... */
858 limit = *va_arg(ap, int *);
862 cp = va_arg(ap, char *);
863 while (*source != '\0') {
867 } else if (found && (ch == *skip_cs(pattern))) {
879 /* other characters are matched literally */
880 if (*source++ != ch) {
889 return (*source || *pattern) ? 0 : 1;
893 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
897 /* most files use a quasi-fixed format */
898 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
902 const char *s = skip_cs(source);
903 size_t have = strlen(source);
905 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
906 memcpy(arg1, s, (size_t) cpp);
908 while (*s++ == '\t') {
910 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
918 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
922 if (!strcmp(arg2, "c")) {
933 parse_rgb(char **data)
937 unsigned long r, g, b;
941 RGB_NAME *result = 0;
943 for (need = 0; data[need] != 0; ++need) ;
945 result = typeCalloc(RGB_NAME, need + 2);
946 how_much.name += (sizeof(RGB_NAME) * (need + 2));
948 for (n = 0; data[n] != 0; ++n) {
949 if (strlen(t = data[n]) >= sizeof(buf) - 1)
951 if (*(s = skip_s(t)) == '!')
954 r = strtoul(s, &t, 10);
956 g = strtoul(s, &t, 10);
958 b = strtoul(s, &t, 10);
961 result[item].name = s;
963 while (t-- != s && isspace(UChar(*t))) {
966 result[item].value = (int) ((r & 0xff) << 16 |
972 result[item].name = "none";
973 result[item].value = -1;
978 #define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c))
981 CaselessCmp(const char *a, const char *b)
982 { /* strcasecmp isn't portable */
984 int cmp = LOWERCASE(*a) - LOWERCASE(*b);
989 return LOWERCASE(*a) - LOWERCASE(*b);
993 lookup_rgb(const char *name)
995 RGB_NAME *result = 0;
996 if (rgb_table != 0) {
998 for (n = 0; rgb_table[n].name != 0; ++n) {
999 if (!CaselessCmp(name, rgb_table[n].name)) {
1000 result = &rgb_table[n];
1009 parse_xbm(char **data)
1022 debugmsg("called parse_xbm");
1024 result = typeCalloc(PICS_HEAD, 1);
1025 how_much.head += sizeof(PICS_HEAD);
1029 gather_c_values(0xffffff);
1031 for (n = 0; data[n] != 0; ++n) {
1032 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1038 if (sscanf(s, "#define %1024s %d%c", buf, &num, &ch) >= 2) {
1039 if ((t = strstr(buf, "_width")) != 0) {
1041 result->wide = (short) bytes_of(num);
1042 } else if ((t = strstr(buf, "_height")) != 0) {
1044 result->high = (short) num;
1050 if (strcmp(result->name, buf)) {
1054 result->name = strdup(buf);
1059 if (sscanf(s, "static char %1024[^_ ]_bits[]%c", buf, &ch) >= 1) {
1060 if (strcmp(result->name, buf)) {
1064 cells = (size_t) (result->wide * result->high);
1066 result->cells = typeCalloc(PICS_CELL, cells);
1067 how_much.cell += (sizeof(PICS_CELL) * cells);
1069 if ((s = strchr(s, L_CURLY)) == 0)
1076 while (*s != '\0') {
1077 while (isspace(UChar(*s))) {
1080 if (isdigit(UChar(*s))) {
1081 long value = strtol(s, &t, 0);
1083 if (t != s || value > MaxRGB || value < 0) {
1089 for (b = 0; b < 8; ++b) {
1090 if (((1L << b) & value) != 0) {
1091 result->cells[which].ch = '*';
1092 result->cells[which].fg = 1;
1093 reading_ncols[1].count++;
1095 result->cells[which].ch = ' ';
1096 result->cells[which].fg = 0;
1097 reading_ncols[0].count++;
1099 if (++which > cells) {
1105 if (*s == R_CURLY) {
1108 } else if (*s == ',') {
1119 debugmsg("...state was only %d", state);
1121 result = free_pics_head(result);
1124 finish_c_values(result);
1130 parse_xpm(char **data)
1137 int cpp = 1; /* chars per pixel */
1151 debugmsg("called parse_xpm");
1153 result = typeCalloc(PICS_HEAD, 1);
1154 how_much.head += sizeof(PICS_HEAD);
1156 for (n = 0; data[n] != 0; ++n) {
1157 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1161 if (match_c(s, " /* XPM */ ")) {
1166 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1168 result->name = strdup(arg1);
1173 if (match_c(s, " \" %d %d %d %d \" , ",
1174 num + 0, num + 1, num + 2, num + 3) ||
1175 match_c(s, " \" %d %d %d %d %d %d \" , ",
1176 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1177 result->wide = (short) num[0];
1178 result->high = (short) num[1];
1179 result->colors = num[2];
1181 begin_c_values(num[2]);
1183 cells = (result->wide * result->high);
1185 result->cells = typeCalloc(PICS_CELL, cells);
1186 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1188 list = typeCalloc(char *, result->colors + 1);
1189 how_much.list += sizeof(char *) * (size_t) (result->colors + 1);
1196 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1200 free(list[reading_last]);
1201 list[reading_last] = strdup(arg1);
1202 if ((by_name = lookup_rgb(arg3)) != 0) {
1203 found = gather_c_values(by_name->value);
1204 } else if (*arg3 == '#') {
1205 char *rgb = arg3 + 1;
1206 unsigned long value = strtoul(rgb, &s, 16);
1207 switch ((int) strlen(rgb)) {
1211 value = (((value >> 24) & 0xff0000L)
1212 | ((value >> 16) & 0xff00L)
1213 | ((value >> 8) & 0xffL));
1216 warning("unexpected rgb value %s", rgb);
1219 found = gather_c_values((int) value);
1221 found = gather_c_values(0); /* actually an error */
1223 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1224 reading_ncols[(found >= 0) ? found : 0].fgcol);
1225 if (num_colors >= result->colors) {
1226 finish_c_values(result);
1229 list[0] = strdup("\033");
1233 if (*(cs = skip_cs(s)) == '"') {
1235 while (*cs != '\0' && *cs != '"') {
1238 /* FIXME - factor out */
1239 for (c = 0; c < result->colors; ++c) {
1241 /* should not happen... */
1244 if (!(strncmp) (cs, list[c], (size_t) cpp)) {
1245 result->cells[which].ch = list[c][0];
1246 result->cells[which].fg = c;
1247 result->fgcol[c].count++;
1252 if (result->cells[which].ch == 0) {
1253 result->cells[which].ch = '?';
1254 result->cells[which].fg = 0;
1257 if (++which >= cells) {
1261 for (c = cpp; c > 0; --c, ++cs) {
1271 if (result && list) {
1272 for (n = 0; n < result->colors; ++n)
1278 debugmsg("...state was only %d", state);
1279 result = free_pics_head(result);
1283 debugmsg("...allocated %d colors", result->colors);
1290 * The obscurely-named "convert" is provided by ImageMagick
1293 parse_img(const char *filename)
1295 size_t need = strlen(filename) + 256;
1296 char *cmd = malloc(need);
1298 char buffer[BUFSIZ];
1304 int width = in_curses ? COLS : 80;
1306 _nc_SPRINTF(cmd, _nc_SLIMIT(need) "identify \"%s\"", filename);
1308 _nc_STRCAT(cmd, " 2>/dev/null", need);
1310 logmsg("...opening pipe to %s", cmd);
1312 result = typeCalloc(PICS_HEAD, 1);
1313 how_much.head += sizeof(PICS_HEAD);
1315 if ((pp = popen(cmd, "r")) != 0) {
1316 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1317 size_t n = strlen(filename);
1318 debugmsg2("...read %s", buffer);
1319 if (strlen(buffer) > n &&
1320 !(strncmp) (buffer, filename, n) &&
1321 isspace(UChar(buffer[n])) &&
1322 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1323 /* distort image to make it show normally on terminal */
1324 pic_x = (int) ((double) pic_x / aspect_ratio);
1331 if (pic_x <= 0 || pic_y <= 0)
1334 _nc_SPRINTF(cmd, _nc_SLIMIT(need)
1335 "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1336 "-define txt:compliance=SVG txt:-",
1337 pic_x, pic_y, width, filename);
1339 _nc_STRCAT(cmd, " 2>/dev/null", need);
1341 logmsg("...opening pipe to %s", cmd);
1342 if ((pp = popen(cmd, "r")) != 0) {
1347 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1348 debugmsg2("[%5d] %s", count + 1, buffer);
1349 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1355 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1356 &col, &row, &len, dummy)) {
1357 result->name = strdup(filename);
1358 result->wide = (short) col;
1359 result->high = (short) row;
1361 begin_c_values(256);
1363 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1364 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1371 * subsequent lines begin "col,row: (r,g,b,a) #RGB".
1372 * Those r/g/b could be integers (0..255) or float-percentages.
1374 int r, g, b, nocolor;
1378 char *s = t = strchr(buffer, '#');
1379 bool matched = FALSE;
1382 /* after the "#RGB", there are differences - just ignore */
1383 while (*s != '\0' && !isspace(UChar(*s)))
1389 "%d,%d: (%d,%d,%d,%d) #%x ",
1391 &r, &g, &b, &nocolor,
1394 } else if (match_c(buffer,
1395 "%d,%d: (%f%%,%f%%,%f%%,%d) #%x ",
1397 &rf, &gf, &bf, &nocolor,
1400 "%d,%d: (%f%%,%f%%,%f%%) #%x ",
1406 #define fp_fix(n) (int) (MaxRGB * (((n) > 100.0 ? 100.0 : (n)) / 100.0))
1412 if ((s - t) > 8) /* 6 hex digits vs 8 */
1416 int want_r = (check >> 16) & 0xff;
1417 int want_g = (check >> 8) & 0xff;
1418 int want_b = (check >> 0) & 0xff;
1420 #define fp_err(tst,ref) ((tst > MaxRGB) || ((tst - ref)*(tst - ref)) > 4)
1422 if (fp_err(r, want_r) ||
1423 fp_err(g, want_g) ||
1424 fp_err(b, want_b)) {
1428 c = gather_c_values((int) check);
1429 which = col + (row * result->wide);
1430 result->cells[which].ch = ((in_curses ||
1434 if (c >= 0 && c < reading_last) {
1435 result->cells[which].fg = c;
1436 reading_ncols[c].count++;
1438 result->cells[which].fg = -1;
1446 finish_c_values(result);
1449 /* FIXME - is this trimming needed? */
1450 for (len = result->colors; len > 3; len--) {
1451 if (result->fgcol[len - 1].fgcol == 0) {
1452 result->colors = len - 1;
1463 result = free_pics_head(result);
1470 read_picture(const char *filename, char **data)
1473 if ((pics = parse_xbm(data)) == 0) {
1475 if ((pics = parse_xpm(data)) == 0) {
1477 if ((pics = parse_img(filename)) == 0) {
1480 warning("unexpected file-format for \"%s\"", filename);
1481 } else if (pics->high == 0 || pics->wide == 0) {
1484 pics = free_pics_head(pics);
1485 warning("no picture found in \"%s\"", filename);
1492 #define fg_color(pics,n) (pics->fgcol[n].fgcol)
1495 dump_picture(PICS_HEAD * pics)
1499 printf("Name %s\n", pics->name);
1500 printf("Size %dx%d\n", pics->high, pics->wide);
1502 for (y = 0; y < pics->colors; ++y) {
1503 if (fg_color(pics, y) < 0) {
1504 printf(" %3d: %d\n", y, fg_color(pics, y));
1506 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1509 for (y = 0; y < pics->high; ++y) {
1510 for (x = 0; x < pics->wide; ++x) {
1511 putchar(pics->cells[y * pics->wide + x].ch);
1517 #ifndef USE_DISPLAY_DRIVER
1519 init_display(const char *palette_path, int opt_d)
1522 if (isatty(fileno(stdout))) {
1524 setlocale(LC_ALL, "");
1531 #if HAVE_USE_DEFAULT_COLORS
1533 use_default_colors();
1535 init_palette(palette_path);
1537 scrollok(stdscr, FALSE);
1543 show_picture(PICS_HEAD * pics)
1548 debugmsg("called show_picture");
1549 logmsg("...using %dx%d screen", LINES, COLS);
1550 #if HAVE_RESET_COLOR_PAIRS
1551 reset_color_pairs();
1557 logmsg("...using %d colors", pics->colors);
1558 for (n = 0; n < pics->colors; ++n) {
1559 int my_pair = (n + 1);
1560 int my_color = map_color(fg_color(pics, n));
1561 #if USE_EXTENDED_COLORS
1562 if (use_extended_pairs) {
1563 init_extended_pair(my_pair, my_color, my_color);
1569 init_pair((short) my_pair, (short) my_color, (short) my_color);
1572 attrset(COLOR_PAIR(1));
1575 for (y = 0; y < pics->high; ++y) {
1580 for (x = 0; x < pics->wide; ++x) {
1585 n = (y * pics->wide + x);
1586 my_pair = pics->cells[n].fg + 1;
1587 #if USE_EXTENDED_COLORS
1588 if (use_extended_pairs) {
1591 wch[0] = (wchar_t) pics->cells[n].ch;
1593 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1598 attrset(COLOR_PAIR(my_pair));
1599 addch((chtype) pics->cells[n].ch);
1603 if (slow_time >= 0) {
1605 if (slow_time > 0) {
1606 #ifdef NCURSES_VERSION
1607 napms(1000 * slow_time);
1609 sleep((unsigned) slow_time);
1613 wmove(stdscr, 0, 0);
1622 compare_fg_counts(const void *a, const void *b)
1624 const FG_NODE *p = (const FG_NODE *) a;
1625 const FG_NODE *q = (const FG_NODE *) b;
1626 return (q->count - p->count);
1630 report_colors(PICS_HEAD * pics)
1642 qsort(pics->fgcol, (size_t) pics->colors, sizeof(FG_NODE), compare_fg_counts);
1644 * For debugging, show a (short) list of the colors used.
1646 if (debugging && (pics->colors < 1000)) {
1650 for (j = pics->colors; j != 0; j /= 10) {
1657 logmsg("These colors were used:");
1658 high = (pics->colors + wide - 1) / wide;
1659 for (j = 0; j < high && j < pics->colors; ++j) {
1663 for (k = 0; k < wide; ++k) {
1664 int n = j + (k * high);
1665 size_t want = (sizeof(buffer) - (size_t) (s - buffer));
1666 if (want < 100 || want >= sizeof(buffer))
1668 if (n >= pics->colors)
1673 _nc_SPRINTF(s, _nc_SLIMIT(want) "%*s", 8 - digits,
1678 if (pics->fgcol[n].fgcol >= 0) {
1679 _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d #%06X %*d", n,
1680 pics->fgcol[n].fgcol,
1681 digits, pics->fgcol[n].count);
1683 _nc_SPRINTF(s, _nc_SLIMIT(want) "%3d (empty) %*d", n,
1684 digits, pics->fgcol[n].count);
1687 if ((s - buffer) > 100)
1690 logmsg("%s", buffer);
1695 * Given the list of colors sorted by the number of times they are used,
1696 * log a short report showing the number of colors for 90%, 99%, 99.9%,
1699 logmsg("Number of colors versus number of cells");
1700 total = pics->high * pics->wide;
1704 for (j = 0; j < pics->colors; ++j) {
1705 accum += pics->fgcol[j].count;
1706 if (accum >= (total * (1.0 - level))) {
1707 int after = (shift > 2) ? shift - 2 : 0;
1708 logmsg("%8d colors (%.1f%%) in %d cells (%.*f%%)",
1710 (100.0 * (j + 1)) / pics->colors,
1712 after, (100.0 * accum) / total);
1725 main(int argc, char *argv[])
1730 const char *palette_path = 0;
1731 const char *rgb_path = RGB_PATH;
1733 while ((ch = getopt(argc, argv, OPTS_COMMON "a:dLl:p:qr:s:x:")) != -1) {
1736 if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
1737 || aspect_ratio < 0.1
1738 || aspect_ratio > 10.) {
1739 fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
1743 #if HAVE_USE_DEFAULT_COLORS
1752 if ((logfp = fopen(optarg, "a")) == 0)
1756 palette_path = optarg;
1765 slow_time = atoi(optarg);
1767 #if USE_EXTENDED_COLORS
1774 use_extended_pairs = TRUE;
1777 use_extended_colors = TRUE;
1789 ExitProgram(EXIT_SUCCESS);
1791 usage(ch == OPTS_USAGE);
1796 if (optind < argc) {
1797 char **rgb_data = read_file(rgb_path);
1801 rgb_table = parse_rgb(rgb_data);
1803 init_display(palette_path, opt_d);
1805 giveup("expected at least one image filename");
1807 for (n = optind; n < argc; ++n) {
1809 char **data = read_file(argv[n]);
1812 warning("cannot read \"%s\"", argv[n]);
1815 if ((pics = read_picture(argv[n], data)) != 0) {
1821 report_colors(pics);
1824 free_pics_head(pics);
1827 free_data(rgb_data);
1834 cleanup(EXIT_SUCCESS);