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.29 2017/05/28 00:19:58 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 assumed palette of terminal
41 * TODO add option for init_color
42 * TODO add option for init_color vs init_extended_color
43 * TODO add option for init_pair vs alloc_pair
44 * TODO use pad to allow pictures larger than screen
46 #include <test.priv.h>
48 #include <sys/types.h>
58 int ch; /* nominal character to display */
59 int fg; /* foreground color */
81 static bool in_curses = FALSE;
82 static RGB_DATA *rgb_table;
85 free_data(char **data)
92 free_pics_head(PICS_HEAD * pics)
100 * Simplify reading xbm/xpm files by first making an array of lines. Blank
101 * lines are filtered out.
104 read_file(const char *filename)
109 printf("** %s\n", filename);
110 if (stat(filename, &sb) == 0
111 && (sb.st_mode & S_IFMT) == S_IFREG
112 && sb.st_size != 0) {
113 size_t size = (size_t) sb.st_size;
114 char *blob = typeMalloc(char, size + 1);
115 bool had_line = TRUE;
119 result = typeCalloc(char *, size + 1);
120 if (blob != 0 && result != 0) {
121 FILE *fp = fopen(filename, "r");
123 if (fread(blob, sizeof(char), size, fp) == size) {
124 for (j = 0; (size_t) j < size; ++j) {
125 if (blob[j] == '\n') {
128 } else if (had_line) {
130 result[k++] = blob + j;
150 static const char *msg[] =
152 "Usage: picsmap [-x rgb-path] [xbm-file [...]]"
157 for (n = 0; n < SIZEOF(msg); n++)
158 fprintf(stderr, "%s\n", msg[n]);
159 ExitProgram(EXIT_FAILURE);
163 giveup(const char *fmt,...)
170 vfprintf(stderr, fmt, ap);
179 int r = (value & 0xff0000) >> 16;
180 int g = (value & 0x00ff00) >> 8;
181 int b = (value & 0x0000ff) >> 0;
182 /* TODO simple mapping into COLOR_BLACK .. COLOR_WHITE */
183 int result = ((r >= 128) << 2) + ((g >= 128) << 1) + (b >= 128);
197 static int match_c(const char *, const char *,...) GCC_SCANFLIKE(2,3);
202 while (isspace(UChar(*s)))
208 skip_cs(const char *s)
210 while (isspace(UChar(*s)))
216 match_c(const char *source, const char *pattern,...)
218 int limit = (int) strlen(source);
219 const char *last_s = source + limit;
226 va_start(ap, pattern);
229 while (*pattern != '\0') {
230 ch = UChar(*pattern++);
231 /* blank in the pattern matches zero-or-more blanks in source */
233 source = skip_cs(source);
236 /* %c, %d, %s are like sscanf except for special treatment of blanks */
237 if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) {
242 cp = va_arg(ap, char *);
245 } while (--limit > 0);
250 ip = va_arg(ap, int *);
251 lv = strtol(source, &cp, ch == 'd' ? 10 : 16);
252 if (cp != 0 && cp != source) {
260 /* not really sscanf... */
261 limit = *va_arg(ap, int *);
265 cp = va_arg(ap, char *);
266 while (*source != '\0') {
270 } else if (found && (ch == *skip_cs(pattern))) {
282 /* other characters are matched literally */
283 if (*source++ != ch) {
292 return (*source || *pattern) ? 0 : 1;
296 match_colors(const char *source, int cpp, char *arg1, char *arg2, char *arg3)
300 /* most files use a quasi-fixed format */
301 if (match_c(source, " \"%n%c %s %s \" , ", &cpp, arg1, arg2, arg3)) {
306 const char *s = skip_cs(source);
307 size_t have = strlen(source);
309 if (*s++ == '"' && have > ((size_t) cpp + 2)) {
310 memcpy(arg1, s, (size_t) cpp);
312 while (*s++ == '\t') {
313 for (t = arg2; (*s != '\0') && strchr("\t\"", *s) == 0;) {
321 for (t = arg3; (*s != '\0') && strchr("\t\"", *s) == 0;) {
325 if (!strcmp(arg2, "c")) {
336 parse_rgb(char **data)
340 unsigned long r, g, b;
344 RGB_DATA *result = 0;
346 for (need = 0; data[need] != 0; ++need) ;
347 result = typeCalloc(RGB_DATA, need + 2);
349 for (n = 0; data[n] != 0; ++n) {
350 if (strlen(t = data[n]) >= sizeof(buf) - 1)
352 if (*(s = skip_s(t)) == '!')
355 r = strtoul(s, &t, 10);
357 g = strtoul(s, &t, 10);
359 b = strtoul(s, &t, 10);
362 result[item].name = s;
364 while (t-- != s && isspace(UChar(*t))) {
367 result[item].value = (int) ((r & 0xff) << 16 | (g & 0xff) << 8 | (b
372 result[item].name = "none";
373 result[item].value = -1;
379 lookup_rgb(const char *name)
381 RGB_DATA *result = 0;
382 if (rgb_table != 0) {
384 for (n = 0; rgb_table[n].name != 0; ++n) {
385 if (!strcasecmp(name, rgb_table[n].name)) {
386 result = &rgb_table[n];
395 parse_xbm(char **data)
404 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
408 for (n = 0; data[n] != 0; ++n) {
409 if (strlen(s = data[n]) >= sizeof(buf) - 1)
415 if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) {
416 if ((t = strstr(buf, "_width")) != 0) {
418 result->wide = bytes_of(num);
419 } else if ((t = strstr(buf, "_height")) != 0) {
425 if (strcmp(result->name, buf)) {
429 result->name = strdup(buf);
434 if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) {
435 if (strcmp(result->name, buf)) {
439 cells = (size_t) (result->wide * result->high);
440 result->cells = typeCalloc(PICS_CELL, cells);
441 if ((s = strchr(s, L_CURLY)) == 0)
449 while (isspace(UChar(*s))) {
452 if (isdigit(UChar(*s))) {
453 long value = strtol(s, &t, 0);
455 if (t != s || value > 255 || value < 0) {
461 /* TODO: which order? */
462 for (b = 0; b < 8; ++b) {
463 if (((1L << b) & value) != 0) {
464 result->cells[which].ch = '*';
465 result->cells[which].fg = 1;
467 result->cells[which].ch = ' ';
468 result->cells[which].fg = 0;
470 if (++which > cells) {
479 } else if (*s == ',') {
491 free_pics_head(result);
496 result->pairs = typeCalloc(PICS_PAIR, 2);
497 result->pairs[1].fg = 0xffffff;
503 parse_xpm(char **data)
506 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
511 int cpp = 1; /* chars per pixel */
523 for (n = 0; data[n] != 0; ++n) {
524 if (strlen(s = data[n]) >= sizeof(buf) - 1)
528 if (match_c(s, " /* XPM */ ")) {
533 if (match_c(s, " static char * %s [] = %c ", arg1, &ch) &&
535 result->name = strdup(arg1);
540 if (match_c(s, " \" %d %d %d %d \" , ",
541 num + 0, num + 1, num + 2, num + 3) ||
542 match_c(s, " \" %d %d %d %d %d %d \" , ",
543 num + 0, num + 1, num + 2, num + 3, num + 4, num + 5)) {
544 result->wide = num[0];
545 result->high = num[1];
546 result->colors = num[2];
547 result->pairs = typeCalloc(PICS_PAIR, result->colors);
548 cells = (size_t) (result->wide * result->high);
549 result->cells = typeCalloc(PICS_CELL, cells);
550 list = typeCalloc(char *, result->colors);
556 if (!match_colors(s, cpp, arg1, arg2, arg3)) {
559 list[color] = strdup(arg1);
560 if ((by_name = lookup_rgb(arg3)) != 0) {
561 result->pairs[color].fg = by_name->value;
562 } else if (*arg3 == '#') {
563 char *rgb = arg3 + 1;
564 unsigned long value = strtoul(rgb, &s, 16);
565 switch ((int) strlen(rgb)) {
569 value = (((value >> 24) & 0xff0000L)
570 | ((value >> 16) & 0xff00L)
571 | ((value >> 8) & 0xffL));
574 printf("unexpected rgb value %s\n", rgb);
577 result->pairs[color].fg = (int) value;
579 result->pairs[color].fg = 0; /* actually an error */
581 if (++color >= result->colors)
585 if (*(cs = skip_cs(s)) == '"') {
587 while (*cs != '\0' && *cs != '"') {
590 for (c = 0; c < result->colors; ++c) {
591 if (!strncmp(cs, list[c], (size_t) cpp)) {
592 result->cells[which].ch = list[c][0];
593 result->cells[which].fg = c;
598 if (result->cells[which].ch == 0) {
599 result->cells[which].ch = '?';
600 result->cells[which].fg = 0;
603 if (++which >= cells) {
607 for (c = cpp; c > 0; --c, ++cs) ;
614 if (result && list) {
615 for (n = 0; n < result->colors; ++n)
621 free_pics_head(result);
629 * The obscurely-named "convert" is provided by ImageMagick
632 parse_img(const char *filename)
634 char *cmd = malloc(strlen(filename) + 80);
638 PICS_HEAD *result = typeCalloc(PICS_HEAD, 1);
640 sprintf(cmd, "convert -thumbnail %dx \"%s\" txt:-", COLS, filename);
641 if ((pp = popen(cmd, "r")) != 0) {
646 while (fgets(buffer, sizeof(buffer), pp) != 0) {
647 if (strlen(buffer) > 160) { /* 80 columns would be enough */
653 "# ImageMagick pixel enumeration: %d,%d,%d,srgba ",
655 result->name = strdup(filename);
658 result->colors = 256;
659 result->pairs = typeCalloc(PICS_PAIR, result->colors);
660 result->cells = typeCalloc(PICS_CELL, (size_t) (col * row));
666 /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */
670 char *s = strchr(buffer, '#');
672 /* after the "#RGB", there are differences - just ignore */
673 while (*s != '\0' && !isspace(UChar(*s)))
678 "%d,%d: (%d,%d,%d,255) #%x ",
685 check != (unsigned) ((r << 16) | (g << 8) | b)) {
689 for (c = 0; c < result->colors; ++c) {
690 if (result->pairs[c].fg == (int) check) {
692 } else if (result->pairs[c].fg == 0) {
693 result->pairs[c].fg = (int) check;
697 if (c >= result->colors) {
698 int more = (result->colors * 3) / 2;
699 PICS_PAIR *p = typeRealloc(PICS_PAIR, more, result->pairs);
701 result->colors = more;
703 result->pairs[c].fg = (int) check;
704 result->pairs[c].bg = 0;
706 result->pairs[c].fg = 0;
707 result->pairs[c].bg = 0;
711 which = col + (row * result->wide);
712 result->cells[which].ch = '#'; /* TODO: space? */
713 result->cells[which].fg = (c < result->colors) ? c : -1;
722 for (len = result->colors; len > 3; len--) {
723 if (result->pairs[len - 1].fg == 0) {
724 result->colors = len - 1;
734 free_pics_head(result);
742 read_picture(const char *filename, char **data)
745 if ((pics = parse_xbm(data)) == 0) {
746 if ((pics = parse_xpm(data)) == 0) {
747 if ((pics = parse_img(filename)) == 0) {
749 giveup("unexpected file-format for \"%s\"", filename);
757 dump_picture(PICS_HEAD * pics)
761 printf("Name %s\n", pics->name);
762 printf("Size %dx%d\n", pics->high, pics->wide);
764 for (y = 0; y < pics->colors; ++y) {
765 if (pics->pairs[y].fg < 0) {
766 printf(" %3d: %d\n", y, pics->pairs[y].fg);
768 printf(" %3d: #%06x\n", y, pics->pairs[y].fg);
771 for (y = 0; y < pics->high; ++y) {
772 for (x = 0; x < pics->wide; ++x) {
773 putchar(pics->cells[y * pics->wide + x].ch);
780 show_picture(PICS_HEAD * pics)
786 for (n = 0; n < pics->colors; ++n) {
787 init_pair((short) (n + 1),
788 (short) map_color(pics->pairs[n].fg),
791 attrset(COLOR_PAIR(1));
794 for (y = 0; y < pics->high; ++y) {
798 for (x = 0; x < pics->wide; ++x) {
801 n = (y * pics->wide + x);
802 attrset(COLOR_PAIR(pics->cells[n].fg + 1));
803 addch((chtype) pics->cells[n].ch);
811 main(int argc, char *argv[])
814 const char *rgb_path = "/etc/X11/rgb.txt";
816 while ((n = getopt(argc, argv, "r:")) != -1) {
828 char **rgb_data = read_file(rgb_path);
831 rgb_table = parse_rgb(rgb_data);
833 if (isatty(fileno(stdout))) {
840 scrollok(stdscr, FALSE);
844 for (n = 1; n < argc; ++n) {
846 char **data = read_file(argv[n]);
849 giveup("cannot read \"%s\"", argv[n]);
851 pics = read_picture(argv[n], data);
858 free_pics_head(pics);
865 ExitProgram(EXIT_SUCCESS);