]> ncurses.scripts.mit.edu Git - ncurses.git/blobdiff - test/picsmap.c
ncurses 6.1 - patch 20180512
[ncurses.git] / test / picsmap.c
index 47a9388bdada24eccf1fc31894522267afb446e6..303bc404a9de56baa0c03b32eeabad2874800476 100644 (file)
@@ -1,5 +1,5 @@
 /****************************************************************************
- * Copyright (c) 2017 Free Software Foundation, Inc.                        *
+ * Copyright (c) 2017,2018 Free Software Foundation, Inc.                   *
  *                                                                          *
  * Permission is hereby granted, free of charge, to any person obtaining a  *
  * copy of this software and associated documentation files (the            *
@@ -26,7 +26,7 @@
  * authorization.                                                           *
  ****************************************************************************/
 /*
- * $Id: picsmap.c,v 1.103 2017/09/06 09:21:38 tom Exp $
+ * $Id: picsmap.c,v 1.121 2018/05/12 16:28:46 tom Exp $
  *
  * Author: Thomas E. Dickey
  *
@@ -52,7 +52,7 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifdef HAVE_STDINT_H
+#if HAVE_STDINT_H
 #include <stdint.h>
 #define my_intptr_t    intptr_t
 #else
 #define  L_CURLY '{'
 #define  R_CURLY '}'
 
+#define MaxSCALE       1000    /* input curses ranges 0..1000 */
+#define MaxRGB         255     /* output color ranges 0..255 */
 #define okCOLOR(n)     ((n) >= 0 && (n) < COLORS)
-#define okRGB(n)       ((n) >= 0 && (n) <= 1000)
-#define Scaled256(n)   (NCURSES_COLOR_T) (int)(((n) * 1000.0) / 256)
-#define ScaledColor(n) (NCURSES_COLOR_T) (int)(((n) * 1000.0) / scale)
+#define okSCALE(n)     ((n) >= 0 && (n) <= MaxSCALE)
+#define Scaled256(n)   (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / 255)
+#define ScaledColor(n) (NCURSES_COLOR_T) (int)(((double)(n) * MaxSCALE) / scale)
 
+#ifndef RGB_PATH
 #define RGB_PATH "/etc/X11/rgb.txt"
+#endif
 
-typedef int NUM_COLOR;
-typedef unsigned short NUM_COUNT;
-
-typedef struct {
-    char ch;                   /* nominal character to display */
-    NUM_COLOR fg;              /* foreground color */
-} PICS_CELL;
-
-typedef struct {
-    NUM_COLOR fgcol;
-    NUM_COUNT count;
-} FG_NODE;
-
-typedef struct {
-    char *name;
-    short high;
-    short wide;
-    int colors;
-    FG_NODE *fgcol;
-    PICS_CELL *cells;
-} PICS_HEAD;
-
-typedef struct {
-    const char *name;
-    int value;
-} RGB_NAME;
-
-typedef struct {
-    short red;
-    short green;
-    short blue;
-} RGB_DATA;
+#include <picsmap.h>
 
 typedef struct {
     size_t file;
@@ -143,6 +116,7 @@ static void warning(const char *fmt,...) GCC_PRINTFLIKE(1, 2);
 static int gather_c_values(int);
 
 static FILE *logfp = 0;
+static double aspect_ratio = 0.6;
 static bool in_curses = FALSE;
 static bool debugging = FALSE;
 static bool quiet = FALSE;
@@ -483,9 +457,10 @@ read_file(const char *filename)
                    for (j = 0; (size_t) j < size; ++j) {
                        if (blob[j] == '\0' ||
                            (UChar(blob[j]) < 32 &&
-                            !isspace(blob[j])) ||
-                           (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160))
+                            !isspace(UChar(blob[j]))) ||
+                           (UChar(blob[j]) >= 128 && UChar(blob[j]) < 160)) {
                            binary = TRUE;
+                       }
                        if (blob[j] == '\n') {
                            blob[j] = '\0';
                            if (k && !binary) {
@@ -528,7 +503,11 @@ usage(void)
        ,"Read/display one or more xbm/xpm files (possibly use \"convert\")"
        ,""
        ,"Options:"
-       ,"  -d           add debugging information to logfile"
+       ,"  -a ratio     aspect-ratio correction for ImageMagick"
+#if HAVE_USE_DEFAULT_COLORS
+       ,"  -d           invoke use_default_colors"
+#endif
+       ,"  -L           add debugging information to logfile"
        ,"  -l logfile   write informational messages to logfile"
        ,"  -p palette   color-palette file (default \"$TERM.dat\")"
        ,"  -q           less verbose"
@@ -605,6 +584,7 @@ read_palette(const char *filename)
            strcpy(s, filename);
            if (tries & 4) {
                char *t = s;
+               char *tc;
                int num;
                char chr;
                int found = 0;
@@ -612,7 +592,8 @@ read_palette(const char *filename)
                    if (*t == '-') {
                        if (sscanf(t, "-%d%c", &num, &chr) == 2 &&
                            chr == 'c' &&
-                           !strncmp(strchr(t, chr), "color", 5)) {
+                           (tc = strchr(t, chr)) != 0 &&
+                           !(strncmp) (tc, "color", 5)) {
                            found = 1;
                        }
                        break;
@@ -620,7 +601,7 @@ read_palette(const char *filename)
                    ++t;
                }
                if (found && (t != s)
-                   && strncmp(s, "xterm", (size_t) (t - s))) {
+                   && (strncmp) (s, "xterm", (size_t) (t - s))) {
                    sprintf(s, "xterm%s", filename + (t - s));
                } else {
                    continue;
@@ -653,17 +634,19 @@ init_palette(const char *palette_file)
 {
     if (palette_file != 0) {
        char **data = read_palette(palette_file);
-       int cp;
 
        all_colors = typeMalloc(RGB_DATA, (unsigned) COLORS);
        how_much.data += (sizeof(RGB_DATA) * (unsigned) COLORS);
 
 #if HAVE_COLOR_CONTENT
-       for (cp = 0; cp < COLORS; ++cp) {
-           color_content((short) cp,
-                         &all_colors[cp].red,
-                         &all_colors[cp].green,
-                         &all_colors[cp].blue);
+       {
+           int cp;
+           for (cp = 0; cp < COLORS; ++cp) {
+               color_content((short) cp,
+                             &all_colors[cp].red,
+                             &all_colors[cp].green,
+                             &all_colors[cp].blue);
+           }
        }
 #else
        memset(all_colors, 0, sizeof(RGB_DATA) * (size_t) COLORS);
@@ -671,7 +654,7 @@ init_palette(const char *palette_file)
        if (data != 0) {
            int n;
            int red, green, blue;
-           int scale = 1000;
+           int scale = MaxSCALE;
            int c;
            for (n = 0; data[n] != 0; ++n) {
                if (sscanf(data[n], "scale:%d", &c) == 1) {
@@ -682,9 +665,9 @@ init_palette(const char *palette_file)
                                  &green,
                                  &blue) == 4
                           && okCOLOR(c)
-                          && okRGB(red)
-                          && okRGB(green)
-                          && okRGB(blue)) {
+                          && okSCALE(red)
+                          && okSCALE(green)
+                          && okSCALE(blue)) {
                    /* *INDENT-EQLS* */
                    all_colors[c].red   = ScaledColor(red);
                    all_colors[c].green = ScaledColor(green);
@@ -1028,6 +1011,8 @@ parse_xbm(char **data)
                } else if ((t = strstr(buf, "_height")) != 0) {
                    state |= 2;
                    result->high = (short) num;
+               } else {
+                   break;
                }
                *t = '\0';
                if (result->name) {
@@ -1064,7 +1049,7 @@ parse_xbm(char **data)
                if (isdigit(UChar(*s))) {
                    long value = strtol(s, &t, 0);
                    int b;
-                   if (t != s || value > 255 || value < 0) {
+                   if (t != s || value > MaxRGB || value < 0) {
                        s = t;
                    } else {
                        state = -1;
@@ -1181,6 +1166,7 @@ parse_xpm(char **data)
                break;
            }
            num_colors++;
+           free(list[reading_last]);
            list[reading_last] = strdup(arg1);
            if ((by_name = lookup_rgb(arg3)) != 0) {
                found = gather_c_values(by_name->value);
@@ -1208,7 +1194,7 @@ parse_xpm(char **data)
            if (num_colors >= result->colors) {
                finish_c_values(result);
                state = 4;
-               if (list != 0 && list[0] == 0)
+               if (list[0] == 0)
                    list[0] = strdup("\033");
            }
            break;
@@ -1224,7 +1210,7 @@ parse_xpm(char **data)
                            /* should not happen... */
                            continue;
                        }
-                       if (!strncmp(cs, list[c], (size_t) cpp)) {
+                       if (!(strncmp) (cs, list[c], (size_t) cpp)) {
                            result->cells[which].ch = list[c][0];
                            result->cells[which].fg = c;
                            result->fgcol[c].count++;
@@ -1299,11 +1285,11 @@ parse_img(const char *filename)
            size_t n = strlen(filename);
            debugmsg2("...read %s", buffer);
            if (strlen(buffer) > n &&
-               !strncmp(buffer, filename, n) &&
+               !(strncmp) (buffer, filename, n) &&
                isspace(UChar(buffer[n])) &&
                sscanf(skip_word(buffer + n), " %dx%d ", &pic_x, &pic_y) == 2) {
                /* distort image to make it show normally on terminal */
-               pic_x = (166 * pic_x) / 100;
+               pic_x = (int) ((double) pic_x / aspect_ratio);
            } else {
                pic_x = pic_y = 0;
            }
@@ -1367,9 +1353,9 @@ parse_img(const char *filename)
                            &check)) {
                    if ((s - t) > 8)    /* 6 hex digits vs 8 */
                        check /= 256;
-                   if (r > 255 ||
-                       g > 255 ||
-                       b > 255 ||
+                   if (r > MaxRGB ||
+                       g > MaxRGB ||
+                       b > MaxRGB ||
                        check != (unsigned) ((r << 16) | (g << 8) | b)) {
                        okay = FALSE;
                        break;
@@ -1463,6 +1449,29 @@ dump_picture(PICS_HEAD * pics)
     }
 }
 
+#ifndef USE_DISPLAY_DRIVER
+static void
+init_display(const char *palette_path, int opt_d)
+{
+    if (isatty(fileno(stdout))) {
+       in_curses = TRUE;
+       initscr();
+       cbreak();
+       noecho();
+       curs_set(0);
+       if (has_colors()) {
+           start_color();
+#if HAVE_USE_DEFAULT_COLORS
+           if (opt_d)
+               use_default_colors();
+#endif
+           init_palette(palette_path);
+       }
+       scrollok(stdscr, FALSE);
+       exit_curses();
+    }
+}
+
 static void
 show_picture(PICS_HEAD * pics)
 {
@@ -1471,7 +1480,8 @@ show_picture(PICS_HEAD * pics)
     int my_pair, my_color;
 
     debugmsg("called show_picture");
-#if USE_EXTENDED_COLORS
+    logmsg("...using %dx%d screen", LINES, COLS);
+#if HAVE_RESET_COLOR_PAIRS
     reset_color_pairs();
 #elif HAVE_CURSCR
     wclear(curscr);
@@ -1537,6 +1547,7 @@ show_picture(PICS_HEAD * pics)
     if (!quiet)
        endwin();
 }
+#endif
 
 static int
 compare_fg_counts(const void *a, const void *b)
@@ -1635,12 +1646,27 @@ int
 main(int argc, char *argv[])
 {
     int n;
+    int opt_d = FALSE;
+    char ignore_ch;
     const char *palette_path = 0;
-    const char *rgb_path = "/etc/X11/rgb.txt";
+    const char *rgb_path = RGB_PATH;
 
-    while ((n = getopt(argc, argv, "dl:p:qr:s:x:")) != -1) {
+    while ((n = getopt(argc, argv, "a:dLl:p:qr:s:x:")) != -1) {
        switch (n) {
+       case 'a':
+           if (sscanf(optarg, "%lf%c", &aspect_ratio, &ignore_ch) != 1
+               || aspect_ratio < 0.1
+               || aspect_ratio > 10.) {
+               fprintf(stderr, "Expected a number in [0.1 to 10.]: %s\n", optarg);
+               usage();
+           }
+           break;
+#if HAVE_USE_DEFAULT_COLORS
        case 'd':
+           opt_d = TRUE;
+           break;
+#endif
+       case 'L':
            debugging = TRUE;
            break;
        case 'l':
@@ -1691,19 +1717,7 @@ main(int argc, char *argv[])
        if (rgb_data)
            rgb_table = parse_rgb(rgb_data);
 
-       if (isatty(fileno(stdout))) {
-           in_curses = TRUE;
-           initscr();
-           cbreak();
-           noecho();
-           curs_set(0);
-           if (has_colors()) {
-               start_color();
-               init_palette(palette_path);
-           }
-           scrollok(stdscr, FALSE);
-           exit_curses();
-       }
+       init_display(palette_path, opt_d);
        if (optind >= argc)
            giveup("expected at least one image filename");