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.52 2017/07/15 22:25:06 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 write cells/second to stderr (or log)
37 * TODO write picture left-to-right/top-to-bottom
38 * TODO write picture randomly
39 * TODO add one-shot option vs repeat-count before exiting
40 * TODO add option "-xc" for init_color vs init_extended_color
41 * TODO add option "-xa" for init_pair vs alloc_pair
42 * TODO use pad to allow pictures larger than screen
43 * TODO improve load of image-file's color-table using tsearch.
44 * TODO add option to just use convert (which can scale) vs builtin xbm/xpm.
46 #include <test.priv.h>
48 #include <sys/types.h>
51 #undef CUR /* use only the curses interface */
59 #define okCOLOR(n) ((n) >= 0 && (n) < COLORS)
60 #define okRGB(n) ((n) >= 0 && (n) <= 1000)
61 #define Scaled256(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
62 #define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
65 int ch; /* nominal character to display */
66 int fg; /* foreground color */
94 static void giveup(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
96 static bool in_curses = FALSE;
97 static RGB_NAME *rgb_table;
98 static RGB_DATA *all_colors;
100 #if HAVE_ALLOC_PAIR && HAVE_INIT_EXTENDED_COLOR
101 #define USE_EXTENDED_COLORS 1
102 static bool use_extended_pairs = FALSE;
103 static bool use_extended_colors = FALSE;
105 #define USE_EXTENDED_COLORS 0
109 free_data(char **data)
118 free_pics_head(PICS_HEAD * pics)
128 * Simplify reading xbm/xpm files by first making an array of lines. Blank
129 * lines are filtered out.
132 read_file(const char *filename)
140 printf("** %s\n", filename);
142 if (stat(filename, &sb) == 0
143 && (sb.st_mode & S_IFMT) == S_IFREG
144 && sb.st_size != 0) {
145 size_t size = (size_t) sb.st_size;
146 char *blob = typeMalloc(char, size + 1);
147 bool had_line = TRUE;
151 result = typeCalloc(char *, size + 1);
152 if (blob != 0 && result != 0) {
153 FILE *fp = fopen(filename, "r");
155 if (fread(blob, sizeof(char), size, fp) == size) {
156 for (j = 0; (size_t) j < size; ++j) {
157 if (blob[j] == '\n') {
160 } else if (had_line) {
162 result[k++] = blob + j;
182 static const char *msg[] =
184 "Usage: picsmap [options] [imagefile [...]]"
185 ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
190 #if USE_EXTENDED_COLORS
191 ," -x [p] use extension (p=init_extended_pair)"
200 for (n = 0; n < SIZEOF(msg); n++)
201 fprintf(stderr, "%s\n", msg[n]);
202 ExitProgram(EXIT_FAILURE);
206 giveup(const char *fmt,...)
213 vfprintf(stderr, fmt, ap);
220 init_palette(const char *palette_file)
222 if (palette_file != 0) {
223 char **data = read_file(palette_file);
226 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
227 for (cp = 0; cp < COLORS; ++cp) {
228 color_content((short) cp,
230 &all_colors[cp].green,
231 &all_colors[cp].blue);
233 if (palette_file != 0 && data != 0) {
235 int red, green, blue;
238 for (n = 0; data[n] != 0; ++n) {
239 if (sscanf(data[n], "scale:%d", &c) == 1) {
241 } else if (sscanf(data[n], "%d:%d %d %d",
251 all_colors[c].red = ScaledColor(red);
252 all_colors[c].green = ScaledColor(green);
253 all_colors[c].blue = ScaledColor(blue);
258 } else if (COLORS > 1) {
263 while (power2 < COLORS) {
268 if ((power2 != COLORS) || ((shift % 3) != 0)) {
269 giveup("With %d colors, you need a palette-file", COLORS);
275 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
276 * direct color value.
287 int red = (value & 0xff0000) >> 16;
288 int green = (value & 0x00ff00) >> 8;
289 int blue = (value & 0x0000ff) >> 0;
291 if (all_colors != 0) {
292 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
293 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
298 red = Scaled256(red);
299 green = Scaled256(green);
300 blue = Scaled256(blue);
302 for (result = 0, n = 1; n < COLORS; ++n) {
303 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
309 } else { /* direct color */
313 while (power2 < COLORS) {
323 result = ((red << (2 * (8 - shifts)))
324 + (green << (8 - shifts))
342 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
347 while (isspace(UChar(*s)))
353 skip_cs(const char *s)
355 while (isspace(UChar(*s)))
364 while (isgraph(UChar(*s)))
370 match_c(const char *source, const char *pattern,...)
372 int limit = (int) strlen(source);
373 const char *last_s = source + limit;
380 va_start(ap, pattern);
383 while (*pattern != '\0') {
384 ch = UChar(*pattern++);
385 /* blank in the pattern matches zero-or-more blanks in source */
387 source = skip_cs(source);
390 /* %c, %d, %s are like sscanf except for special treatment of blanks */
391 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
396 cp = va_arg(ap, char *);
399 } while (--limit > 0);
404 ip = va_arg(ap, int *);
405 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
406 if (cp != 0 && cp != source) {
414 /* not really sscanf... */
415 limit = *va_arg(ap, int *);
419 cp = va_arg(ap, char *);
420 while (*source != '\0') {
424 } else if (found && (ch == *skip_cs(pattern))) {
436 /* other characters are matched literally */
437 if (*source++ != ch) {
446 return (*source || *pattern) ? 0 : 1;
450 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
454 /* most files use a quasi-fixed format */
455 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
460 const char *s = skip_cs(source);
461 size_t have = strlen(source);
463 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
464 memcpy(arg1, s, (size_t) cpp);
466 while (*s++ == '\t') {
467 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
475 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
479 if (!strcmp(arg2, "c")) {
490 parse_rgb(char **data)
494 unsigned long r, g, b;
498 RGB_NAME *result = 0;
500 for (need = 0; data[need] != 0; ++need) ;
501 result = typeCalloc(RGB_NAME, need + 2);
503 for (n = 0; data[n] != 0; ++n) {
504 if (strlen(t = data[n]) >= sizeof(buf) - 1)
506 if (*(s = skip_s(t)) == '!')
509 r = strtoul(s, &t, 10);
511 g = strtoul(s, &t, 10);
513 b = strtoul(s, &t, 10);
516 result[item].name = s;
518 while (t-- != s && isspace(UChar(*t))) {
521 result[item].value = (int) ((r & 0xff) << 16 | (g & 0xff) << 8 | (b
526 result[item].name = "none";
527 result[item].value = -1;
533 lookup_rgb(const char *name)
535 RGB_NAME *result = 0;
536 if (rgb_table != 0) {
538 for (n = 0; rgb_table[n].name != 0; ++n) {
539 if (!strcasecmp(name, rgb_table[n].name)) {
540 result = &rgb_table[n];
549 parse_xbm(char **data)
558 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
562 for (n = 0; data[n] != 0; ++n) {
563 if (strlen(s = data[n]) >= sizeof(buf) - 1)
569 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
570 if ((t = strstr(buf, "_width")) != 0) {
572 result->wide = bytes_of(num);
573 } else if ((t = strstr(buf, "_height")) != 0) {
579 if (strcmp(result->name, buf)) {
583 result->name = strdup(buf);
588 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
589 if (strcmp(result->name, buf)) {
593 cells = (size_t) (result->wide * result->high);
594 result->cells = typeCalloc(PICS_CELL, cells);
595 if ((s = strchr(s, L_CURLY)) == 0)
603 while (isspace(UChar(*s))) {
606 if (isdigit(UChar(*s))) {
607 long value = strtol(s, &t, 0);
609 if (t != s || value > 255 || value < 0) {
615 /* TODO: which order? */
616 for (b = 0; b < 8; ++b) {
617 if (((1L << b) & value) != 0) {
618 result->cells[which].ch = '*';
619 result->cells[which].fg = 1;
621 result->cells[which].ch = ' ';
622 result->cells[which].fg = 0;
624 if (++which > cells) {
633 } else if (*s == ',') {
645 free_pics_head(result);
650 result->pairs = typeCalloc(PICS_PAIR, 2);
651 result->pairs[1].fg = 0xffffff;
657 parse_xpm(char **data)
660 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
665 int cpp = 1; /* chars per pixel */
677 for (n = 0; data[n] != 0; ++n) {
678 if (strlen(s = data[n]) >= sizeof(buf) - 1)
682 if (match_c(s, " /* XPM */ ")) {
687 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
689 result->name = strdup(arg1);
694 if (match_c(s, " \" %d %d %d %d \" , ",
695 num + 0, num + 1, num + 2, num + 3) ||
696 match_c(s, " \" %d %d %d %d %d %d \" , ",
697 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
698 result->wide = num[0];
699 result->high = num[1];
700 result->colors = num[2];
701 result->pairs = typeCalloc(PICS_PAIR, result->colors);
702 cells = (result->wide * result->high);
703 result->cells = typeCalloc(PICS_CELL, cells);
704 list = typeCalloc(char *, result->colors);
710 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
713 list[color] = strdup(arg1);
714 if ((by_name = lookup_rgb(arg3)) != 0) {
715 result->pairs[color].fg = by_name->value;
716 } else if (*arg3 == '#') {
717 char *rgb = arg3 + 1;
718 unsigned long value = strtoul(rgb, &s, 16);
719 switch ((int) strlen(rgb)) {
723 value = (((value >> 24) & 0xff0000L)
724 | ((value >> 16) & 0xff00L)
725 | ((value >> 8) & 0xffL));
728 printf("unexpected rgb value %s\n", rgb);
731 result->pairs[color].fg = (int) value;
733 result->pairs[color].fg = 0; /* actually an error */
735 if (++color >= result->colors)
739 if (*(cs = skip_cs(s)) == '"') {
741 while (*cs != '\0' && *cs != '"') {
744 for (c = 0; c < result->colors; ++c) {
745 if (!strncmp(cs, list[c], (size_t) cpp)) {
746 result->cells[which].ch = list[c][0];
747 result->cells[which].fg = c;
752 if (result->cells[which].ch == 0) {
753 result->cells[which].ch = '?';
754 result->cells[which].fg = 0;
757 if (++which >= cells) {
761 for (c = cpp; c > 0; --c, ++cs) ;
768 if (result && list) {
769 for (n = 0; n < result->colors; ++n)
775 free_pics_head(result);
783 * The obscurely-named "convert" is provided by ImageMagick
786 parse_img(const char *filename)
788 char *cmd = malloc(strlen(filename) + 256);
792 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
795 int width = in_curses ? COLS : 80;
797 sprintf(cmd, "identify \"%s\"", filename);
799 if ((pp = popen(cmd, "r")) != 0) {
800 if (fgets(buffer, sizeof(buffer), pp) != 0) {
801 size_t n = strlen(filename);
802 if (strlen(buffer) > n &&
803 !strncmp(buffer, filename, n) &&
804 isspace(UChar(buffer[n])) &&
805 sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
806 /* distort image to make it show normally on terminal */
807 pic_x = (166 * pic_x) / 100;
814 if (pic_x <= 0 || pic_y <= 0)
817 sprintf(cmd, "convert " "-resize %dx%d\\! " "-thumbnail %dx \"%s\" "
818 "-define txt:compliance=SVG txt:-",
819 pic_x, pic_y, width, filename);
821 if ((pp = popen(cmd, "r")) != 0) {
826 while (fgets(buffer, sizeof(buffer), pp) != 0) {
827 if (strlen(buffer) > 160) { /* 80 columns would be enough */
833 "# ImageMagick pixel enumeration: %d,%d,%d,srgba ",
835 result->name = strdup(filename);
838 result->colors = 256;
839 result->pairs = typeCalloc(PICS_PAIR, result->colors);
840 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
846 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
847 int r, g, b, nocolor;
851 char *s = t = strchr(buffer, '#');
853 /* after the "#RGB", there are differences - just ignore */
854 while (*s != '\0' && !isspace(UChar(*s)))
859 "%d,%d: (%d,%d,%d,%d) #%x ",
861 &r, &g, &b, &nocolor,
863 if ((s - t) > 8) /* 6 hex digits vs 8 */
868 check != (unsigned) ((r << 16) | (g << 8) | b)) {
872 for (c = 0; c < result->colors; ++c) {
873 if (result->pairs[c].fg == (int) check) {
875 } else if (result->pairs[c].fg == 0) {
876 result->pairs[c].fg = (int) check;
880 if (c >= result->colors) {
881 int more = (result->colors * 3) / 2;
882 PICS_PAIR *p = typeRealloc(PICS_PAIR, more, result->pairs);
884 result->colors = more;
886 result->pairs[c].fg = (int) check;
887 result->pairs[c].bg = 0;
889 result->pairs[c].fg = 0;
890 result->pairs[c].bg = 0;
894 which = col + (row * result->wide);
895 result->cells[which].ch = ((in_curses ||
899 result->cells[which].fg = (c < result->colors) ? c : -1;
908 for (len = result->colors; len > 3; len--) {
909 if (result->pairs[len - 1].fg == 0) {
910 result->colors = len - 1;
921 free_pics_head(result);
929 read_picture(const char *filename, char **data)
932 if ((pics = parse_xbm(data)) == 0) {
933 if ((pics = parse_xpm(data)) == 0) {
934 if ((pics = parse_img(filename)) == 0) {
936 giveup("unexpected file-format for \"%s\"", filename);
944 dump_picture(PICS_HEAD * pics)
948 printf("Name %s\n", pics->name);
949 printf("Size %dx%d\n", pics->high, pics->wide);
951 for (y = 0; y < pics->colors; ++y) {
952 if (pics->pairs[y].fg < 0) {
953 printf(" %3d: %d\n", y, pics->pairs[y].fg);
955 printf(" %3d: #%06x\n", y, pics->pairs[y].fg);
958 for (y = 0; y < pics->high; ++y) {
959 for (x = 0; x < pics->wide; ++x) {
960 putchar(pics->cells[y * pics->wide + x].ch);
967 show_picture(PICS_HEAD * pics)
971 int my_pair, my_color;
974 for (n = 0; n < pics->colors; ++n) {
976 my_color = map_color(pics->pairs[n].fg);
977 #if USE_EXTENDED_COLORS
978 if (use_extended_pairs) {
979 init_extended_pair(my_pair, my_color, my_color);
985 init_pair((short) my_pair, (short) my_color, (short) my_color);
988 attrset(COLOR_PAIR(1));
991 for (y = 0; y < pics->high; ++y) {
995 for (x = 0; x < pics->wide; ++x) {
998 n = (y * pics->wide + x);
999 my_pair = pics->cells[n].fg + 1;
1000 #if USE_EXTENDED_COLORS
1001 if (use_extended_pairs) {
1004 wch[0] = (wchar_t) pics->cells[n].ch;
1006 setcchar(&temp, wch, A_NORMAL, (short) my_pair, &my_pair);
1011 attrset(COLOR_PAIR(my_pair));
1012 addch((chtype) pics->cells[n].ch);
1021 main(int argc, char *argv[])
1024 const char *palette_path = 0;
1025 const char *rgb_path = "/etc/X11/rgb.txt";
1027 while ((n = getopt(argc, argv, "p:r:x:")) != -1) {
1030 palette_path = optarg;
1035 #if USE_EXTENDED_COLORS
1042 use_extended_pairs = TRUE;
1045 use_extended_colors = TRUE;
1061 if (optind < argc) {
1062 char **rgb_data = read_file(rgb_path);
1065 rgb_table = parse_rgb(rgb_data);
1067 if (isatty(fileno(stdout))) {
1074 init_palette(palette_path);
1076 scrollok(stdscr, FALSE);
1080 giveup("expected at least one image filename");
1082 for (n = optind; n < argc; ++n) {
1084 char **data = read_file(argv[n]);
1087 giveup("cannot read \"%s\"", argv[n]);
1089 pics = read_picture(argv[n], data);
1096 free_pics_head(pics);
1098 free_data(rgb_data);
1103 ExitProgram(EXIT_SUCCESS);