X-Git-Url: http://ncurses.scripts.mit.edu/?a=blobdiff_plain;f=test%2Fpicsmap.c;h=09b46ee9cb0b26cbd7f1294ac1411785d36dbc55;hb=79b07cae47a6fa064aa4d2ea70fe930884751eb4;hp=624eda88959080ce6126945e62f1f0982a690160;hpb=02f1dee48fe8af6ce054388fba739aa4f975004e;p=ncurses.git diff --git a/test/picsmap.c b/test/picsmap.c index 624eda88..09b46ee9 100644 --- a/test/picsmap.c +++ b/test/picsmap.c @@ -1,5 +1,6 @@ /**************************************************************************** - * Copyright (c) 2017-2018,2019 Free Software Foundation, Inc. * + * Copyright 2018-2021,2022 Thomas E. Dickey * + * Copyright 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 +27,7 @@ * authorization. * ****************************************************************************/ /* - * $Id: picsmap.c,v 1.131 2019/12/14 23:51:39 tom Exp $ + * $Id: picsmap.c,v 1.142 2022/02/06 01:44:34 tom Exp $ * * Author: Thomas E. Dickey * @@ -108,7 +109,7 @@ typedef struct { #define debugmsg if (debugging) logmsg #define debugmsg2 if (debugging) logmsg2 -static void cleanup(int) GCC_NORETURN; +static GCC_NORETURN void cleanup(int); static void giveup(const char *fmt, ...) GCC_PRINTFLIKE(1, 2); static void logmsg(const char *fmt, ...) GCC_PRINTFLIKE(1, 2); static void logmsg2(const char *fmt, ...) GCC_PRINTFLIKE(1, 2); @@ -603,7 +604,6 @@ read_palette(const char *filename) continue; } } - s += strlen(s); if (tries & 2) { int len = (int) strlen(filename); @@ -797,6 +797,7 @@ match_c(const char *source, const char *pattern, ...) int ch; int *ip; char *cp; + float *fp; long lv; va_start(ap, pattern); @@ -810,10 +811,13 @@ match_c(const char *source, const char *pattern, ...) continue; } /* %c, %d, %s are like sscanf except for special treatment of blanks */ - if (ch == '%' && *pattern != '\0' && strchr("cdnsx", *pattern)) { + if (ch == '%' && *pattern != '\0' && strchr("%cdnfsx", *pattern)) { bool found = FALSE; ch = *pattern++; switch (ch) { + case '%': + source++; + break; case 'c': cp = va_arg(ap, char *); do { @@ -832,6 +836,29 @@ match_c(const char *source, const char *pattern, ...) goto finish; } break; + case 'f': + /* floating point for pixels... */ + fp = va_arg(ap, float *); + lv = strtol(source, &cp, 10); + if (cp == 0 || cp == source) + goto finish; + *fp = (float) lv; + source = cp; + if (*source == '.') { + lv = strtol(++source, &cp, 10); + if (cp == 0 || cp == source) + goto finish; + { + float scale = 1.0f; + int digits = (int) (cp - source); + while (digits-- > 0) { + scale *= 10.0f; + } + *fp += (float) lv / scale; + } + source = cp; + } + break; case 'n': /* not really sscanf... */ limit = *va_arg(ap, int *); @@ -954,6 +981,20 @@ parse_rgb(char **data) return result; } +#define LOWERCASE(c) ((isalpha(UChar(c)) && isupper(UChar(c))) ? tolower(UChar(c)) : (c)) + +static int +CaselessCmp(const char *a, const char *b) +{ /* strcasecmp isn't portable */ + while (*a && *b) { + int cmp = LOWERCASE(*a) - LOWERCASE(*b); + if (cmp != 0) + break; + a++, b++; + } + return LOWERCASE(*a) - LOWERCASE(*b); +} + static RGB_NAME * lookup_rgb(const char *name) { @@ -961,7 +1002,7 @@ lookup_rgb(const char *name) if (rgb_table != 0) { int n; for (n = 0; rgb_table[n].name != 0; ++n) { - if (!strcasecmp(name, rgb_table[n].name)) { + if (!CaselessCmp(name, rgb_table[n].name)) { result = &rgb_table[n]; break; } @@ -975,7 +1016,7 @@ parse_xbm(char **data) { int n; int state = 0; - char buf[BUFSIZ]; + char buf[2048]; int num; char ch; char *s; @@ -1000,7 +1041,7 @@ parse_xbm(char **data) case 0: case 1: case 2: - if (sscanf(s, "#define %s %d%c", buf, &num, &ch) >= 2) { + if (sscanf(s, "#define %1024s %d%c", buf, &num, &ch) >= 2) { if ((t = strstr(buf, "_width")) != 0) { state |= 1; result->wide = (short) bytes_of(num); @@ -1021,7 +1062,7 @@ parse_xbm(char **data) } break; case 3: - if (sscanf(s, "static char %[^_ ]_bits[]%c", buf, &ch) >= 1) { + if (sscanf(s, "static char %1024[^_ ]_bits[]%c", buf, &ch) >= 1) { if (strcmp(result->name, buf)) { goto finish; } @@ -1332,11 +1373,17 @@ parse_img(const char *filename) break; } } else { - /* subsequent lines begin "col,row: (r,g,b,a) #RGB" */ + /* + * subsequent lines begin "col,row: (r,g,b,a) #RGB". + * Those r/g/b could be integers (0..255) or float-percentages. + */ int r, g, b, nocolor; + float rf, gf, bf; unsigned check; char *t; char *s = t = strchr(buffer, '#'); + bool matched = FALSE; + bool blurred = FALSE; if (s != 0) { /* after the "#RGB", there are differences - just ignore */ @@ -1349,14 +1396,30 @@ parse_img(const char *filename) &col, &row, &r, &g, &b, &nocolor, &check)) { + matched = TRUE; + } else if (match_c(buffer, + "%d,%d: (%f%%,%f%%,%f%%,%d) #%x ", + &col, &row, + &rf, &gf, &bf, &nocolor, + &check)) { + matched = TRUE; + blurred = TRUE; /* 6.9.11 scaling is broken... */ +#define fp_fix(n) (int) (MaxRGB * (((n) > 100.0 ? 100.0 : (n)) / 100.0)) + r = fp_fix(rf); + g = fp_fix(gf); + b = fp_fix(bf); + } + if (matched) { int which, c; if ((s - t) > 8) /* 6 hex digits vs 8 */ check /= 256; - if (r > MaxRGB || - g > MaxRGB || - b > MaxRGB || - check != (unsigned) ((r << 16) | (g << 8) | b)) { + if (blurred) { + /* revisit this when ImageMagick is fixed */ + } else if (r > MaxRGB || + g > MaxRGB || + b > MaxRGB || + check != (unsigned) ((r << 16) | (g << 8) | b)) { okay = FALSE; break; } @@ -1597,7 +1660,7 @@ report_colors(PICS_HEAD * pics) for (k = 0; k < wide; ++k) { int n = j + (k * high); size_t want = (sizeof(buffer) - (size_t) (s - buffer)); - if (want < 100) + if (want < 100 || want >= sizeof(buffer)) break; if (n >= pics->colors) break;