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.90 2017/08/12 17:21:48 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.
37 * TODO when not using tsearch, dispense with intermediate index
38 * TODO count/report uses of each color, giving percentiles.
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>
54 #include <sys/types.h>
61 #undef CUR /* use only the curses interface */
69 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
70 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
71 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
72 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
74 #define RGB_PATH "/etc/X11/rgb.txt"
76 typedef int NUM_COLOR;
79 char ch; /* nominal character to display */
80 NUM_COLOR fg; /* foreground color */
119 #define stop_curses() if (in_curses) endwin()
121 #define debugmsg if (debugging) logmsg
122 #define debugmsg2 if (debugging) logmsg2
124 static void cleanup(int) GCC_NORETURN;
125 static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
126 static void logmsg(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
127 static void logmsg2(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
128 static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
130 static FILE *logfp = 0;
131 static bool in_curses = FALSE;
132 static bool debugging = FALSE;
133 static bool quiet = FALSE;
134 static int slow_time = -1;
135 static RGB_NAME *rgb_table;
136 static RGB_DATA *all_colors;
137 static HOW_MUCH how_much;
139 static int reading_last;
140 static int reading_size;
141 static FG_NODE **reading_ncols;
144 static void *reading_ntree;
147 #if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
148 #define USE_EXTENDED_COLORS 1
149 static bool use_extended_pairs = FALSE;
150 static bool use_extended_colors = FALSE;
152 #define USE_EXTENDED_COLORS 0
156 logmsg(const char *fmt,...)
161 vfprintf(logfp, fmt, ap);
169 logmsg2(const char *fmt,...)
174 vfprintf(logfp, fmt, ap);
184 logmsg("Allocations:");
185 logmsg("%8ld file", (long) how_much.file);
186 logmsg("%8ld name", (long) how_much.name);
187 logmsg("%8ld list", (long) how_much.list);
188 logmsg("%8ld data", (long) how_much.data);
189 logmsg("%8ld head", (long) how_much.head);
190 logmsg("%8ld pair", (long) how_much.pair);
191 logmsg("%8ld cell", (long) how_much.cell);
192 logmsg("%8ld window", LINES * COLS * (long) sizeof(NCURSES_CH_T));
208 failed(const char *msg)
212 logmsg("failed with %s", strerror(save));
213 cleanup(EXIT_FAILURE);
217 warning(const char *fmt,...)
222 vfprintf(logfp, fmt, ap);
229 vfprintf(stderr, fmt, ap);
232 cleanup(EXIT_FAILURE);
237 free_data(char **data)
246 free_pics_head(PICS_HEAD * pics)
249 #if !(HAVE_TSEARCH && HAVE_TDESTROY)
251 if (pics->fgcol != 0) {
252 for (n = 0; n < pics->colors; ++n) {
253 free(pics->fgcol[n]);
267 begin_c_values(int size)
271 reading_ncols = typeCalloc(FG_NODE *, size + 1);
272 how_much.pair += (sizeof(FG_NODE *) * (size_t) size);
277 compare_c_values(const void *p, const void *q)
279 const FG_NODE *a = (const FG_NODE *) p;
280 const FG_NODE *b = (const FG_NODE *) q;
281 return (a->fgcol - b->fgcol);
286 check_c_values(int ln)
293 for (n = 0; n < reading_last; ++n) {
294 if (reading_ncols[n] == 0)
296 ft = tfind(reading_ncols[n], &reading_ntree, compare_c_values);
297 if (ft != 0 && *ft != 0) {
298 if ((*ft)->fgcol != reading_ncols[n]->fgcol) {
299 logmsg("@%d, %d:%d (%d) %p %p fgcol %06X %06X", ln, n,
302 (*ft), reading_ncols[n],
303 reading_ncols[n]->fgcol,
306 if ((*ft)->which != reading_ncols[n]->which) {
307 logmsg("@%d, %d:%d (%d) %p %p which %d %d", ln, n,
310 (*ft), reading_ncols[n],
311 reading_ncols[n]->which,
315 logmsg("@%d, %d:%d (%d) %p %p null %06X %d", ln, n,
318 ft, reading_ncols[n],
319 reading_ncols[n]->fgcol,
320 reading_ncols[n]->which);
325 #define check_c_values(n) /* nothing */
330 #define MAX(a,b) ((a)>(b)?(a):(b))
333 gather_c_values(int fg)
338 FG_NODE *find = typeMalloc(FG_NODE, 1);
340 how_much.pair += sizeof(FG_NODE);
344 check_c_values(__LINE__);
345 if ((ft = tfind(find, &reading_ntree, compare_c_values)) != 0) {
346 found = (*ft)->which;
348 if (reading_last + 1 >= reading_size) {
349 int more = ((MAX(reading_last, reading_size) + 2) * 3) / 2;
350 FG_NODE **p = typeRealloc(FG_NODE *, more, reading_ncols);
354 /* FIXME - this won't reallocate pointers for tsearch */
355 how_much.pair -= (sizeof(FG_NODE *) * (size_t) reading_size);
356 how_much.pair += (sizeof(FG_NODE *) * (size_t) more);
359 memset(reading_ncols + reading_last, 0,
360 sizeof(FG_NODE *) * (size_t) (more - reading_last));
361 check_c_values(__LINE__);
363 reading_ncols[reading_last] = find;
364 find->which = (unsigned short) reading_last++;
365 if ((ft = tsearch(find, &reading_ntree, compare_c_values)) != 0) {
367 check_c_values(__LINE__);
373 for (n = 0; n < reading_last; ++n) {
374 if (reading_ncols[n]->fgcol == fg) {
380 FG_NODE *node = typeMalloc(FG_NODE, 1);
381 how_much.pair += sizeof(FG_NODE);
382 if (reading_last + 2 >= reading_size) {
383 int more = ((reading_last + 2) * 3) / 2;
384 FG_NODE **p = typeRealloc(FG_NODE *, more, reading_ncols);
388 how_much.pair -= (sizeof(FG_NODE *) * reading_size);
389 how_much.pair += (sizeof(FG_NODE *) * more);
392 memset(reading_ncols + reading_last, 0,
393 sizeof(FG_NODE) * (more - reading_last));
396 node->which = reading_last;
397 reading_ncols[reading_last] = node;
398 found = reading_last++;
406 finish_c_values(PICS_HEAD * head)
408 head->colors = reading_last;
409 head->fgcol = reading_ncols;
417 dispose_c_values(void)
421 if (reading_ntree != 0) {
423 tdestroy(reading_ntree, free);
425 for (n = 0; n < reading_last; ++n) {
426 tdelete(reading_ncols[n], &reading_ntree, compare_c_values);
432 if (reading_ncols != 0) {
433 for (n = 0; n < reading_last; ++n) {
434 free(reading_ncols[n]);
444 is_file(const char *filename, struct stat *sb)
447 if (stat(filename, sb) == 0
448 && (sb->st_mode & S_IFMT) == S_IFREG
449 && sb->st_size != 0) {
452 debugmsg("is_file(%s) %d", filename, result);
457 * Simplify reading xbm/xpm files by first making an array of lines. Blank
458 * lines are filtered out.
461 read_file(const char *filename)
468 printf("** %s\n", filename);
471 if (is_file(filename, &sb)) {
472 size_t size = (size_t) sb.st_size;
473 char *blob = typeMalloc(char, size + 1);
474 bool had_line = TRUE;
479 result = typeCalloc(char *, size + 1);
480 how_much.file += ((size + 1) * 2);
482 if (blob != 0 && result != 0) {
483 FILE *fp = fopen(filename, "r");
485 logmsg("opened %s", filename);
486 if (fread(blob, sizeof(char), size, fp) == size) {
487 for (j = 0; (size_t) j < size; ++j) {
488 if (blob[j] == '\0' ||
489 (UChar(blob[j]) < 32 &&
490 !isspace(blob[j])) ||
491 (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160))
493 if (blob[j] == '\n') {
496 debugmsg2("[%5d] %s\n", k, result[k - 1]);
499 } else if (had_line) {
501 result[k++] = blob + j;
506 debugmsg2("[%5d] %s\n", k, result[k - 1]);
511 logmsg("cannot open %s", filename);
515 debugmsg("...file is empty");
520 debugmsg("...file is non-text");
529 static const char *msg[] =
531 "Usage: picsmap [options] [imagefile [...]]"
532 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
535 ," -d add debugging information to logfile"
536 ," -l logfile write informational messages to logfile"
537 ," -p palette color-palette file (default \"$TERM.dat\")"
539 ," -r rgb-path xpm uses X rgb color-names (default \"" RGB_PATH "\")"
540 ," -s SECS pause for SECS seconds after display vs getch"
541 #if USE_EXTENDED_COLORS
542 ," -x [pc] use extension (p=extended-pairs, c=extended-colors)"
543 ," Either/both extension may be given"
551 for (n = 0; n < SIZEOF(msg); n++)
552 fprintf(stderr, "%s\n", msg[n]);
553 cleanup(EXIT_FAILURE);
557 giveup(const char *fmt,...)
565 vfprintf(stderr, fmt, ap);
571 vfprintf(logfp, fmt, ap);
581 * Palette files are named for $TERM values. However, there are fewer palette
582 * files than $TERM's. Although there are known problems (some cannot even get
583 * black and white correct), for the purpose of comparison, pretending that
584 * those map into "xterm" is useful.
587 read_palette(const char *filename)
589 static const char *data_dir = DATA_DIR;
591 char *full_name = malloc(strlen(data_dir) + 20 + strlen(filename));
595 if (full_name != 0) {
597 for (tries = 0; tries < 8; ++tries) {
599 *(s = full_name) = '\0';
601 if (strchr(filename, '/') == 0) {
602 sprintf(full_name, "%s/", data_dir);
617 if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
619 !strncmp(strchr(t, chr), "color", 5)) {
626 if (found && (t != s)
627 && strncmp(s, "xterm", (size_t) (t - s))) {
628 sprintf(s, "xterm%s", filename + (t - s));
636 int len = (int) strlen(filename);
637 if (len <= 4 || strcmp(filename + len - 4, ".dat")) {
643 if (is_file(full_name, &sb))
648 result = read_file(full_name);
656 init_palette(const char *palette_file)
658 if (palette_file != 0) {
659 char **data = read_palette(palette_file);
662 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
663 how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
665 for (cp = 0; cp < COLORS; ++cp) {
666 color_content((short) cp,
668 &all_colors[cp].green,
669 &all_colors[cp].blue);
673 int red, green, blue;
676 for (n = 0; data[n] != 0; ++n) {
677 if (sscanf(data[n], "scale:%d", &c) == 1) {
679 } else if (sscanf(data[n], "%d:%d %d %d",
689 all_colors[c].red = ScaledColor(red);
690 all_colors[c].green = ScaledColor(green);
691 all_colors[c].blue = ScaledColor(blue);
697 } else if (COLORS > 1) {
701 while (power2 < COLORS) {
706 if ((power2 != COLORS) || ((shift % 3) != 0)) {
707 if (all_colors == 0) {
708 init_palette(getenv("TERM"));
710 if (all_colors == 0) {
711 giveup("With %d colors, you need a palette-file", COLORS);
718 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
719 * direct color value.
730 int red = (value & 0xff0000) >> 16;
731 int green = (value & 0x00ff00) >> 8;
732 int blue = (value & 0x0000ff) >> 0;
734 if (all_colors != 0) {
735 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
736 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
741 red = Scaled256(red);
742 green = Scaled256(green);
743 blue = Scaled256(blue);
745 for (result = 0, n = 1; n < COLORS; ++n) {
746 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
752 } else { /* direct color */
756 while (power2 < COLORS) {
766 result = ((red << (2 * (8 - shifts)))
767 + (green << (8 - shifts))
785 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
790 while (isspace(UChar(*s)))
796 skip_cs(const char *s)
798 while (isspace(UChar(*s)))
807 while (isgraph(UChar(*s)))
813 match_c(const char *source, const char *pattern,...)
815 int limit = (int) strlen(source);
816 const char *last_s = source + limit;
823 va_start(ap, pattern);
826 while (*pattern != '\0') {
827 ch = UChar(*pattern++);
828 /* blank in the pattern matches zero-or-more blanks in source */
830 source = skip_cs(source);
833 /* %c, %d, %s are like sscanf except for special treatment of blanks */
834 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
839 cp = va_arg(ap, char *);
842 } while (--limit > 0);
847 ip = va_arg(ap, int *);
848 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
849 if (cp != 0 && cp != source) {
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)) {
903 const char *s = skip_cs(source);
904 size_t have = strlen(source);
906 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
907 memcpy(arg1, s, (size_t) cpp);
909 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;
979 lookup_rgb(const char *name)
981 RGB_NAME *result = 0;
982 if (rgb_table != 0) {
984 for (n = 0; rgb_table[n].name != 0; ++n) {
985 if (!strcasecmp(name, rgb_table[n].name)) {
986 result = &rgb_table[n];
995 parse_xbm(char **data)
1008 debugmsg("called parse_xbm");
1010 result = typeCalloc(PICS_HEAD, 1);
1011 how_much.head += sizeof(PICS_HEAD);
1013 for (n = 0; data[n] != 0; ++n) {
1014 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1020 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
1021 if ((t = strstr(buf, "_width")) != 0) {
1023 result->wide = (short) bytes_of(num);
1024 } else if ((t = strstr(buf, "_height")) != 0) {
1026 result->high = (short) num;
1030 if (strcmp(result->name, buf)) {
1034 result->name = strdup(buf);
1039 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
1040 if (strcmp(result->name, buf)) {
1044 cells = (size_t) (result->wide * result->high);
1046 result->cells = typeCalloc(PICS_CELL, cells);
1047 how_much.cell += (sizeof(PICS_CELL) * cells);
1049 if ((s = strchr(s, L_CURLY)) == 0)
1056 while (*s != '\0') {
1057 while (isspace(UChar(*s))) {
1060 if (isdigit(UChar(*s))) {
1061 long value = strtol(s, &t, 0);
1063 if (t != s || value > 255 || value < 0) {
1069 /* TODO: which order? */
1070 for (b = 0; b < 8; ++b) {
1071 if (((1L << b) & value) != 0) {
1072 result->cells[which].ch = '*';
1073 result->cells[which].fg = 1;
1075 result->cells[which].ch = ' ';
1076 result->cells[which].fg = 0;
1078 if (++which > cells) {
1084 if (*s == R_CURLY) {
1087 } else if (*s == ',') {
1098 debugmsg("...state was only %d", state);
1100 result = free_pics_head(result);
1105 gather_c_values(0xffffff);
1106 finish_c_values(result);
1112 parse_xpm(char **data)
1119 int cpp = 1; /* chars per pixel */
1133 debugmsg("called parse_xpm");
1135 result = typeCalloc(PICS_HEAD, 1);
1136 how_much.head += sizeof(PICS_HEAD);
1138 for (n = 0; data[n] != 0; ++n) {
1139 if (strlen(s = data[n]) >= sizeof(buf) - 1)
1143 if (match_c(s, " /* XPM */ ")) {
1148 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
1150 result->name = strdup(arg1);
1155 if (match_c(s, " \" %d %d %d %d \" , ",
1156 num + 0, num + 1, num + 2, num + 3) ||
1157 match_c(s, " \" %d %d %d %d %d %d \" , ",
1158 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
1159 result->wide = (short) num[0];
1160 result->high = (short) num[1];
1161 result->colors = num[2];
1163 begin_c_values(num[2]);
1165 cells = (result->wide * result->high);
1167 result->cells = typeCalloc(PICS_CELL, cells);
1168 how_much.cell += sizeof(PICS_CELL) * (size_t) cells;
1170 list = typeCalloc(char *, result->colors);
1171 how_much.list += sizeof(char *) * (size_t) result->colors;
1178 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
1182 list[reading_last] = strdup(arg1);
1183 if ((by_name = lookup_rgb(arg3)) != 0) {
1184 found = gather_c_values(by_name->value);
1185 } else if (*arg3 == '#') {
1186 char *rgb = arg3 + 1;
1187 unsigned long value = strtoul(rgb, &s, 16);
1188 switch ((int) strlen(rgb)) {
1192 value = (((value >> 24) & 0xff0000L)
1193 | ((value >> 16) & 0xff00L)
1194 | ((value >> 8) & 0xffL));
1197 warning("unexpected rgb value %s", rgb);
1200 found = gather_c_values((int) value);
1202 found = gather_c_values(0); /* actually an error */
1204 debugmsg(" [%d:%d] %06X", num_colors, result->colors,
1205 reading_ncols[(found >= 0) ? found : 0]->fgcol);
1206 if (num_colors >= result->colors) {
1207 finish_c_values(result);
1212 if (*(cs = skip_cs(s)) == '"') {
1214 while (*cs != '\0' && *cs != '"') {
1217 /* FIXME - factor out */
1218 for (c = 0; c < result->colors; ++c) {
1219 if (!strncmp(cs, list[c], (size_t) cpp)) {
1220 result->cells[which].ch = list[c][0];
1221 result->cells[which].fg = c;
1226 if (result->cells[which].ch == 0) {
1227 result->cells[which].ch = '?';
1228 result->cells[which].fg = 0;
1231 if (++which >= cells) {
1235 for (c = cpp; c > 0; --c, ++cs) ;
1242 if (result && list) {
1243 for (n = 0; n < result->colors; ++n)
1249 debugmsg("...state was only %d", state);
1250 result = free_pics_head(result);
1254 debugmsg("...allocated %d colors", result->colors);
1261 * The obscurely-named "convert" is provided by ImageMagick
1264 parse_img(const char *filename)
1266 char *cmd = malloc(strlen(filename) + 256);
1268 char buffer[BUFSIZ];
1274 int width = in_curses ? COLS : 80;
1276 sprintf(cmd, "identify \"%s\"", filename);
1278 strcat(cmd, " 2>/dev/null");
1280 logmsg("...opening pipe to %s", cmd);
1282 result = typeCalloc(PICS_HEAD, 1);
1283 how_much.head += sizeof(PICS_HEAD);
1285 if ((pp = popen(cmd, "r")) != 0) {
1286 if (fgets(buffer, sizeof(buffer), pp) != 0) {
1287 size_t n = strlen(filename);
1288 debugmsg2("...read %s", buffer);
1289 if (strlen(buffer) > n &&
1290 !strncmp(buffer, filename, n) &&
1291 isspace(UChar(buffer[n])) &&
1292 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
1293 /* distort image to make it show normally on terminal */
1294 pic_x = (166 * pic_x) / 100;
1301 if (pic_x <= 0 || pic_y <= 0)
1304 sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
1305 "-define txt:compliance=SVG txt:-",
1306 pic_x, pic_y, width, filename);
1308 strcat(cmd, " 2>/dev/null");
1310 logmsg("...opening pipe to %s", cmd);
1311 if ((pp = popen(cmd, "r")) != 0) {
1316 while (fgets(buffer, sizeof(buffer), pp) != 0) {
1317 debugmsg2("[%5d] %s", count + 1, buffer);
1318 if (strlen(buffer) > 160) { /* 80 columns would be enough */
1324 "# ImageMagick pixel enumeration: %d,%d,%d,%s ",
1325 &col, &row, &len, dummy)) {
1326 result->name = strdup(filename);
1327 result->wide = (short) col;
1328 result->high = (short) row;
1330 begin_c_values(256);
1332 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
1333 how_much.cell += (sizeof(PICS_CELL) * (size_t) (col * row));
1339 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
1340 int r, g, b, nocolor;
1344 char *s = t = strchr(buffer, '#');
1346 /* after the "#RGB", there are differences - just ignore */
1347 while (*s != '\0' && !isspace(UChar(*s)))
1352 "%d,%d: (%d,%d,%d,%d) #%x ",
1354 &r, &g, &b, &nocolor,
1356 if ((s - t) > 8) /* 6 hex digits vs 8 */
1361 check != (unsigned) ((r << 16) | (g << 8) | b)) {
1365 c = gather_c_values((int) check);
1366 which = col + (row * result->wide);
1367 result->cells[which].ch = ((in_curses ||
1371 result->cells[which].fg = ((c >= 0 && c < reading_last)
1380 finish_c_values(result);
1383 /* FIXME - is this trimming needed? */
1384 for (len = result->colors; len > 3; len--) {
1385 if (result->fgcol[len - 1] == 0) {
1386 result->colors = len - 1;
1397 result = free_pics_head(result);
1404 read_picture(const char *filename, char **data)
1407 if ((pics = parse_xbm(data)) == 0) {
1409 if ((pics = parse_xpm(data)) == 0) {
1411 if ((pics = parse_img(filename)) == 0) {
1414 warning("unexpected file-format for \"%s\"", filename);
1415 } else if (pics->high == 0 || pics->wide == 0) {
1418 pics = free_pics_head(pics);
1419 warning("no picture found in \"%s\"", filename);
1426 #define fg_color(pics,n) (pics->fgcol[n]->fgcol)
1429 dump_picture(PICS_HEAD * pics)
1433 printf("Name %s\n", pics->name);
1434 printf("Size %dx%d\n", pics->high, pics->wide);
1436 for (y = 0; y < pics->colors; ++y) {
1437 if (fg_color(pics, y) < 0) {
1438 printf(" %3d: %d\n", y, fg_color(pics, y));
1440 printf(" %3d: #%06x\n", y, fg_color(pics, y));
1443 for (y = 0; y < pics->high; ++y) {
1444 for (x = 0; x < pics->wide; ++x) {
1445 putchar(pics->cells[y * pics->wide + x].ch);
1452 show_picture(PICS_HEAD * pics)
1456 int my_pair, my_color;
1458 debugmsg("called show_picture");
1459 #if USE_EXTENDED_COLORS
1460 reset_color_pairs();
1466 logmsg("...using %d colors", pics->colors);
1467 for (n = 0; n < pics->colors; ++n) {
1469 my_color = map_color(fg_color(pics, n));
1470 #if USE_EXTENDED_COLORS
1471 if (use_extended_pairs) {
1472 init_extended_pair(my_pair, my_color, my_color);
1478 init_pair((short) my_pair, (short) my_color, (short) my_color);
1481 attrset(COLOR_PAIR(1));
1484 for (y = 0; y < pics->high; ++y) {
1488 for (x = 0; x < pics->wide; ++x) {
1491 n = (y * pics->wide + x);
1492 my_pair = pics->cells[n].fg + 1;
1493 #if USE_EXTENDED_COLORS
1494 if (use_extended_pairs) {
1497 wch[0] = (wchar_t) pics->cells[n].ch;
1499 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1504 attrset(COLOR_PAIR(my_pair));
1505 addch((chtype) pics->cells[n].ch);
1509 if (slow_time >= 0) {
1511 if (slow_time > 0) {
1512 #ifdef NCURSES_VERSION
1513 napms(1000 * slow_time);
1515 sleep((unsigned) slow_time);
1526 main(int argc, char *argv[])
1529 const char *palette_path = 0;
1530 const char *rgb_path = "/etc/X11/rgb.txt";
1532 while ((n = getopt(argc, argv, "dl:p:qr:s:x:")) != -1) {
1538 if ((logfp = fopen(optarg, "a")) == 0)
1542 palette_path = optarg;
1551 slow_time = atoi(optarg);
1553 #if USE_EXTENDED_COLORS
1560 use_extended_pairs = TRUE;
1563 use_extended_colors = TRUE;
1579 if (optind < argc) {
1580 char **rgb_data = read_file(rgb_path);
1583 rgb_table = parse_rgb(rgb_data);
1585 if (isatty(fileno(stdout))) {
1593 init_palette(palette_path);
1595 scrollok(stdscr, FALSE);
1599 giveup("expected at least one image filename");
1601 for (n = optind; n < argc; ++n) {
1603 char **data = read_file(argv[n]);
1606 warning("cannot read \"%s\"", argv[n]);
1609 if ((pics = read_picture(argv[n], data)) != 0) {
1617 free_pics_head(pics);
1620 free_data(rgb_data);
1627 cleanup(EXIT_SUCCESS);