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.35 2017/06/04 00:20:15 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 for init_color
41 * TODO add option for init_color vs init_extended_color
42 * TODO add option for init_pair vs alloc_pair
43 * TODO use pad to allow pictures larger than screen
44 * TODO improve load of image-file's color-table using tsearch.
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;
101 free_data(char **data)
108 free_pics_head(PICS_HEAD * pics)
116 * Simplify reading xbm/xpm files by first making an array of lines. Blank
117 * lines are filtered out.
120 read_file(const char *filename)
128 printf("** %s\n", filename);
130 if (stat(filename, &sb) == 0
131 && (sb.st_mode & S_IFMT) == S_IFREG
132 && sb.st_size != 0) {
133 size_t size = (size_t) sb.st_size;
134 char *blob = typeMalloc(char, size + 1);
135 bool had_line = TRUE;
139 result = typeCalloc(char *, size + 1);
140 if (blob != 0 && result != 0) {
141 FILE *fp = fopen(filename, "r");
143 if (fread(blob, sizeof(char), size, fp) == size) {
144 for (j = 0; (size_t) j < size; ++j) {
145 if (blob[j] == '\n') {
148 } else if (had_line) {
150 result[k++] = blob + j;
170 static const char *msg[] =
172 "Usage: picsmap [options] [imagefile [...]]",
173 "Read/display one or more xbm/xpm files (possibly use \"convert\")",
185 for (n = 0; n < SIZEOF(msg); n++)
186 fprintf(stderr, "%s\n", msg[n]);
187 ExitProgram(EXIT_FAILURE);
191 giveup(const char *fmt,...)
198 vfprintf(stderr, fmt, ap);
205 init_palette(const char *palette_file)
207 if (palette_file != 0) {
210 all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
211 for (cp = 0; cp < COLORS; ++cp) {
214 &all_colors[cp].green,
215 &all_colors[cp].blue);
216 if (palette_file != 0) {
217 char **data = read_file(palette_file);
220 int red, green, blue;
223 for (n = 0; data[n] != 0; ++n) {
224 if (sscanf(data[n], "scale:%d", &c) == 1) {
226 } else if (sscanf(data[n], "%d:%d %d %d",
236 all_colors[c].red = ScaledColor(red);
237 all_colors[c].green = ScaledColor(green);
238 all_colors[c].blue = ScaledColor(blue);
245 } else if (COLORS > 1) {
250 while (power2 < COLORS) {
255 if (power2 != COLORS || (shift % 3) != 0) {
256 giveup("With %d colors, you need a palette-file", COLORS);
262 * Map the 24-bit RGB value to a color index if using a palette, otherwise to a
263 * direct color value.
274 int red = (value & 0xff0000) >> 16;
275 int green = (value & 0x00ff00) >> 8;
276 int blue = (value & 0x0000ff) >> 0;
278 if (all_colors != 0) {
279 #define Diff2(n,m) ((m) - all_colors[n].m) * ((m) - all_colors[n].m)
280 #define Diff2S(n) Diff2(n,red) + Diff2(n,green) + Diff2(n,blue)
285 red = Scaled256(red);
286 green = Scaled256(green);
287 blue = Scaled256(blue);
289 for (result = 0, n = 1; n < COLORS; ++n) {
290 int d = Diff2(n, red) + Diff2(n, green) + Diff2(n, blue);
296 } else { /* direct color */
300 while (power2 < COLORS) {
310 result = ((red << (2 * (8 - shifts)))
311 + (green << (8 - shifts))
329 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
334 while (isspace(UChar(*s)))
340 skip_cs(const char *s)
342 while (isspace(UChar(*s)))
348 match_c(const char *source, const char *pattern,...)
350 int limit = (int) strlen(source);
351 const char *last_s = source + limit;
358 va_start(ap, pattern);
361 while (*pattern != '\0') {
362 ch = UChar(*pattern++);
363 /* blank in the pattern matches zero-or-more blanks in source */
365 source = skip_cs(source);
368 /* %c, %d, %s are like sscanf except for special treatment of blanks */
369 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
374 cp = va_arg(ap, char *);
377 } while (--limit > 0);
382 ip = va_arg(ap, int *);
383 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
384 if (cp != 0 && cp != source) {
392 /* not really sscanf... */
393 limit = *va_arg(ap, int *);
397 cp = va_arg(ap, char *);
398 while (*source != '\0') {
402 } else if (found && (ch == *skip_cs(pattern))) {
414 /* other characters are matched literally */
415 if (*source++ != ch) {
424 return (*source || *pattern) ? 0 : 1;
428 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
432 /* most files use a quasi-fixed format */
433 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
438 const char *s = skip_cs(source);
439 size_t have = strlen(source);
441 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
442 memcpy(arg1, s, (size_t) cpp);
444 while (*s++ == '\t') {
445 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
453 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
457 if (!strcmp(arg2, "c")) {
468 parse_rgb(char **data)
472 unsigned long r, g, b;
476 RGB_NAME *result = 0;
478 for (need = 0; data[need] != 0; ++need) ;
479 result = typeCalloc(RGB_NAME, need + 2);
481 for (n = 0; data[n] != 0; ++n) {
482 if (strlen(t = data[n]) >= sizeof(buf) - 1)
484 if (*(s = skip_s(t)) == '!')
487 r = strtoul(s, &t, 10);
489 g = strtoul(s, &t, 10);
491 b = strtoul(s, &t, 10);
494 result[item].name = s;
496 while (t-- != s && isspace(UChar(*t))) {
499 result[item].value = (int) ((r & 0xff) << 16 | (g & 0xff) << 8 | (b
504 result[item].name = "none";
505 result[item].value = -1;
511 lookup_rgb(const char *name)
513 RGB_NAME *result = 0;
514 if (rgb_table != 0) {
516 for (n = 0; rgb_table[n].name != 0; ++n) {
517 if (!strcasecmp(name, rgb_table[n].name)) {
518 result = &rgb_table[n];
527 parse_xbm(char **data)
536 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
540 for (n = 0; data[n] != 0; ++n) {
541 if (strlen(s = data[n]) >= sizeof(buf) - 1)
547 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
548 if ((t = strstr(buf, "_width")) != 0) {
550 result->wide = bytes_of(num);
551 } else if ((t = strstr(buf, "_height")) != 0) {
557 if (strcmp(result->name, buf)) {
561 result->name = strdup(buf);
566 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
567 if (strcmp(result->name, buf)) {
571 cells = (size_t) (result->wide * result->high);
572 result->cells = typeCalloc(PICS_CELL, cells);
573 if ((s = strchr(s, L_CURLY)) == 0)
581 while (isspace(UChar(*s))) {
584 if (isdigit(UChar(*s))) {
585 long value = strtol(s, &t, 0);
587 if (t != s || value > 255 || value < 0) {
593 /* TODO: which order? */
594 for (b = 0; b < 8; ++b) {
595 if (((1L << b) & value) != 0) {
596 result->cells[which].ch = '*';
597 result->cells[which].fg = 1;
599 result->cells[which].ch = ' ';
600 result->cells[which].fg = 0;
602 if (++which > cells) {
611 } else if (*s == ',') {
623 free_pics_head(result);
628 result->pairs = typeCalloc(PICS_PAIR, 2);
629 result->pairs[1].fg = 0xffffff;
635 parse_xpm(char **data)
638 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
643 int cpp = 1; /* chars per pixel */
655 for (n = 0; data[n] != 0; ++n) {
656 if (strlen(s = data[n]) >= sizeof(buf) - 1)
660 if (match_c(s, " /* XPM */ ")) {
665 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
667 result->name = strdup(arg1);
672 if (match_c(s, " \" %d %d %d %d \" , ",
673 num + 0, num + 1, num + 2, num + 3) ||
674 match_c(s, " \" %d %d %d %d %d %d \" , ",
675 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
676 result->wide = num[0];
677 result->high = num[1];
678 result->colors = num[2];
679 result->pairs = typeCalloc(PICS_PAIR, result->colors);
680 cells = (size_t) (result->wide * result->high);
681 result->cells = typeCalloc(PICS_CELL, cells);
682 list = typeCalloc(char *, result->colors);
688 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
691 list[color] = strdup(arg1);
692 if ((by_name = lookup_rgb(arg3)) != 0) {
693 result->pairs[color].fg = by_name->value;
694 } else if (*arg3 == '#') {
695 char *rgb = arg3 + 1;
696 unsigned long value = strtoul(rgb, &s, 16);
697 switch ((int) strlen(rgb)) {
701 value = (((value >> 24) & 0xff0000L)
702 | ((value >> 16) & 0xff00L)
703 | ((value >> 8) & 0xffL));
706 printf("unexpected rgb value %s\n", rgb);
709 result->pairs[color].fg = (int) value;
711 result->pairs[color].fg = 0; /* actually an error */
713 if (++color >= result->colors)
717 if (*(cs = skip_cs(s)) == '"') {
719 while (*cs != '\0' && *cs != '"') {
722 for (c = 0; c < result->colors; ++c) {
723 if (!strncmp(cs, list[c], (size_t) cpp)) {
724 result->cells[which].ch = list[c][0];
725 result->cells[which].fg = c;
730 if (result->cells[which].ch == 0) {
731 result->cells[which].ch = '?';
732 result->cells[which].fg = 0;
735 if (++which >= cells) {
739 for (c = cpp; c > 0; --c, ++cs) ;
746 if (result && list) {
747 for (n = 0; n < result->colors; ++n)
753 free_pics_head(result);
761 * The obscurely-named "convert" is provided by ImageMagick
764 parse_img(const char *filename)
766 char *cmd = malloc(strlen(filename) + 80);
770 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
772 sprintf(cmd, "convert -thumbnail %dx \"%s\" txt:-", COLS, filename);
773 if ((pp = popen(cmd, "r")) != 0) {
778 while (fgets(buffer, sizeof(buffer), pp) != 0) {
779 if (strlen(buffer) > 160) { /* 80 columns would be enough */
785 "# ImageMagick pixel enumeration: %d,%d,%d,srgba ",
787 result->name = strdup(filename);
790 result->colors = 256;
791 result->pairs = typeCalloc(PICS_PAIR, result->colors);
792 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
798 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
802 char *s = strchr(buffer, '#');
804 /* after the "#RGB", there are differences - just ignore */
805 while (*s != '\0' && !isspace(UChar(*s)))
810 "%d,%d: (%d,%d,%d,255) #%x ",
817 check != (unsigned) ((r << 16) | (g << 8) | b)) {
821 for (c = 0; c < result->colors; ++c) {
822 if (result->pairs[c].fg == (int) check) {
824 } else if (result->pairs[c].fg == 0) {
825 result->pairs[c].fg = (int) check;
829 if (c >= result->colors) {
830 int more = (result->colors * 3) / 2;
831 PICS_PAIR *p = typeRealloc(PICS_PAIR, more, result->pairs);
833 result->colors = more;
835 result->pairs[c].fg = (int) check;
836 result->pairs[c].bg = 0;
838 result->pairs[c].fg = 0;
839 result->pairs[c].bg = 0;
843 which = col + (row * result->wide);
844 result->cells[which].ch = '#'; /* TODO: space? */
845 result->cells[which].fg = (c < result->colors) ? c : -1;
854 for (len = result->colors; len > 3; len--) {
855 if (result->pairs[len - 1].fg == 0) {
856 result->colors = len - 1;
866 free_pics_head(result);
874 read_picture(const char *filename, char **data)
877 if ((pics = parse_xbm(data)) == 0) {
878 if ((pics = parse_xpm(data)) == 0) {
879 if ((pics = parse_img(filename)) == 0) {
881 giveup("unexpected file-format for \"%s\"", filename);
889 dump_picture(PICS_HEAD * pics)
893 printf("Name %s\n", pics->name);
894 printf("Size %dx%d\n", pics->high, pics->wide);
896 for (y = 0; y < pics->colors; ++y) {
897 if (pics->pairs[y].fg < 0) {
898 printf(" %3d: %d\n", y, pics->pairs[y].fg);
900 printf(" %3d: #%06x\n", y, pics->pairs[y].fg);
903 for (y = 0; y < pics->high; ++y) {
904 for (x = 0; x < pics->wide; ++x) {
905 putchar(pics->cells[y * pics->wide + x].ch);
912 show_picture(PICS_HEAD * pics)
918 for (n = 0; n < pics->colors; ++n) {
919 init_pair((short) (n + 1),
920 (short) map_color(pics->pairs[n].fg),
923 attrset(COLOR_PAIR(1));
926 for (y = 0; y < pics->high; ++y) {
930 for (x = 0; x < pics->wide; ++x) {
933 n = (y * pics->wide + x);
934 attrset(COLOR_PAIR(pics->cells[n].fg + 1));
935 addch((chtype) pics->cells[n].ch);
943 main(int argc, char *argv[])
946 const char *palette_path = 0;
947 const char *rgb_path = "/etc/X11/rgb.txt";
949 while ((n = getopt(argc, argv, "p:r:")) != -1) {
952 palette_path = optarg;
964 char **rgb_data = read_file(rgb_path);
967 rgb_table = parse_rgb(rgb_data);
969 if (isatty(fileno(stdout))) {
976 init_palette(palette_path);
978 scrollok(stdscr, FALSE);
982 giveup("expected at least one image filename");
984 for (n = optind; n < argc; ++n) {
986 char **data = read_file(argv[n]);
989 giveup("cannot read \"%s\"", argv[n]);
991 pics = read_picture(argv[n], data);
998 free_pics_head(pics);
1000 free_data(rgb_data);
1005 ExitProgram(EXIT_SUCCESS);