add a minimal colormap
authorLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 16 Sep 2024 08:04:00 +0000 (10:04 +0200)
committerLaurent MAZET <laurent.mazet@thalesgroup.com>
Mon, 16 Sep 2024 08:04:00 +0000 (10:04 +0200)
color.c
color.h
display.c

diff --git a/color.c b/color.c
index 448f49507a8930343862a85e4192bfdca1d08522..f66d365bfaa9e2e5b70882cb04857947dcd7b8c3 100644 (file)
--- a/color.c
+++ b/color.c
@@ -2,9 +2,21 @@
 
 #include "color.h"
 
-#define nbcolors 16
+#define nbcolors0 8
+#define nbcolors1 16
 
-color_t colors[nbcolors] = {
+color_t colors0[nbcolors0] = {
+    { 40, 0.0, 0.0, 0.0 },
+    { 41, 0.7, 0.1, 0.1 },
+    { 42, 0.1, 0.7, 0.1 },
+    { 43, 0.7, 0.4, 0.1 },
+    { 44, 0.1, 0.1, 0.7 },
+    { 45, 0.7, 0.1, 0.7 },
+    { 46, 0.1, 0.7, 0.7 },
+    { 47, 0.7, 0.7, 0.7 },
+};
+
+color_t colors1[nbcolors1] = {
     { 40, 0.0, 0.0, 0.0 },
     { 41, 0.7, 0.1, 0.1 },
     { 42, 0.1, 0.7, 0.1 },
@@ -23,11 +35,24 @@ color_t colors[nbcolors] = {
     { 107, 1.0, 1.0, 1.0 },
 };
 
-color_t *findcolor (float red, float green, float blue)
+color_t *findcolor (float red, float green, float blue, int colormap)
 {
     color_t *color = NULL;
     int i;
     float dist = 3;
+    int nbcolors;
+    color_t *colors;
+    switch (colormap) {
+    case 0:
+        nbcolors = nbcolors0;
+        colors = colors0;
+        break;
+    case 1:
+    default:
+        nbcolors = nbcolors1;
+        colors = colors1;
+        break;
+    }
     for (i = 0; i < nbcolors; i++) {
         float r = (colors + i)->red - red;
         float g = (colors + i)->green - green;
diff --git a/color.h b/color.h
index 65be10e90df04075cb91612a32e63adcee5ebdcb..235253f6520cd5874c3ccb7b1732bde7e0e4f902 100644 (file)
--- a/color.h
+++ b/color.h
@@ -8,7 +8,7 @@ typedef struct {
     float blue;
 } color_t;
 
-color_t *findcolor (float red, float green, float blue);
+color_t *findcolor (float red, float green, float blue, int colormap);
 
 void cprint (color_t *color, char *str);
 
index 428015f35c5c0246d9a6d78676f3f7ba191f1268..28c88accf000616f30450a5ba3eccac155a9ab1f 100644 (file)
--- a/display.c
+++ b/display.c
@@ -17,6 +17,7 @@
 char *progname = NULL;
 char *version = "1.0";
 
+int colormap = 1;
 float gf[3] = { 1.0f, 1.0f, 1.0f };
 int mode = 0;
 
@@ -24,7 +25,8 @@ int mode = 0;
 int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
-    fprintf (fd, "usage: %s [-g gamma] [-h] [-t|-w] [-v int] <file>\n", progname);
+    fprintf (fd, "usage: %s [-c int] [-g gamma] [-h] [-t|-w] [-v int] <file>\n", progname);
+    fprintf (fd, " -c: color map [0..2] (%d)\n", colormap);
     fprintf (fd, " -g: gamma correction (%.1f:%.1f:%.1f)\n", gf[0], gf[1], gf[2]);
     fprintf (fd, " -h: help message\n");
     fprintf (fd, " -t: thin mode (%s)\n", (mode == 0) ? "on" : "off");
@@ -64,6 +66,14 @@ int main (int argc, char *argv[])
         }
         char c = arg[1];
         switch (c) {
+        case 'c':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: missing color map\n", progname));
+                return usage (1);
+            }
+            colormap = atoi (arg);
+            break;
         case 'g':
             arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
             if (arg == NULL) {
@@ -125,7 +135,7 @@ int main (int argc, char *argv[])
     for (l = 0; l < image->height; l++) {
         for (k = 0; k < image->width; k++) {
             int ind = k + image->width * l;
-            color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]));
+            color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap);
             cprint (color, (mode == 0) ? " " : "  ");
         }
         printf ("\n");
@@ -137,6 +147,7 @@ int main (int argc, char *argv[])
 }
 
 /* test: display.exe 2>&1 | grep 'no file specified' */
+/* test: display.exe -c 2>&1 | grep missing */
 /* test: display.exe -g 2>&1 | grep 'no gamma specified' */
 /* test: display.exe -g -1.0 2>&1 | grep 'incorrect gamma' */
 /* test: display.exe -g 1.1:0:1.1 2>&1 | grep 'incorrect gamma' */