add multiple colormaps
authorLaurent Mazet <mazet@softndesign.org>
Mon, 16 Sep 2024 21:37:23 +0000 (23:37 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Mon, 16 Sep 2024 21:37:23 +0000 (23:37 +0200)
color.c
color.h
display.c

diff --git a/color.c b/color.c
index dc56dd7f2e8cf0237ce329eaf7d09d66b2b9d2ca..f80a0d2953b49fb611235fba1241b6c9ba268e3a 100644 (file)
--- a/color.c
+++ b/color.c
@@ -3,7 +3,6 @@
 #include "color.h"
 
 #define nbcolors0 8
-#define nbcolors1 16
 
 color_t colors0[nbcolors0] = {
     { 40, 0.0, 0.0, 0.0 },
@@ -16,30 +15,300 @@ color_t colors0[nbcolors0] = {
     { 47, 0.7, 0.7, 0.7 },
 };
 
+#define nbcolors1 16
+
 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 },
-    { 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 },
-    { 100, 0.4, 0.4, 0.4 },
-    { 101, 1.0, 0.3, 0.3 },
-    { 102, 0.3, 1.0, 0.3 },
-    { 103, 1.0, 1.0, 0.3 },
-    { 104, 0.3, 0.3, 1.0 },
-    { 105, 1.0, 0.3, 1.0 },
-    { 106, 0.3, 1.0, 1.0 },
+    { 41, 0.5, 0.0, 0.0 },
+    { 42, 0.0, 0.5, 0.0 },
+    { 43, 0.5, 0.5, 0.0 },
+    { 44, 0.0, 0.0, 0.5 },
+    { 45, 0.5, 0.0, 0.5 },
+    { 46, 0.0, 0.5, 0.5 },
+    { 47, 0.75, 0.75, 0.75 },
+    { 100, 0.5, 0.5, 0.5 },
+    { 101, 1.0, 0.0, 0.0 },
+    { 102, 0.0, 1.0, 0.0 },
+    { 103, 1.0, 1.0, 0.0 },
+    { 104, 0.0, 0.0, 1.0 },
+    { 105, 1.0, 0.0, 1.0 },
+    { 106, 0.0, 1.0, 1.0 },
     { 107, 1.0, 1.0, 1.0 },
 };
 
+#define nbcolors2 256
+
+color_t colors2[nbcolors2] = {
+    { 0, 0.0, 0.0, 0.0 },
+    { 1, 0.5, 0.0, 0.0 },
+    { 2, 0.0, 0.5, 0.0 },
+    { 3, 0.5, 0.5, 0.0 },
+    { 4, 0.0, 0.0, 0.5 },
+    { 5, 0.5, 0.0, 0.5 },
+    { 6, 0.0, 0.5, 0.5 },
+    { 7, 0.75, 0.75, 0.75 },
+    { 8, 0.5, 0.5, 0.5 },
+    { 9, 1.0, 0.0, 0.0 },
+    { 10, 0.0, 1.0, 0.0 },
+    { 11, 1.0, 1.0, 0.0 },
+    { 12, 0.0, 0.0, 1.0 },
+    { 13, 1.0, 0.0, 1.0 },
+    { 14, 0.0, 1.0, 1.0 },
+    { 15, 1.0, 1.0, 1.0 },
+    { 16, 0.00, 0.00, 0.00 },
+    { 17, 0.00, 0.00, 0.37 },
+    { 18, 0.00, 0.00, 0.53 },
+    { 19, 0.00, 0.00, 0.69 },
+    { 20, 0.00, 0.00, 0.84 },
+    { 21, 0.00, 0.00, 1.00 },
+    { 22, 0.00, 0.37, 0.00 },
+    { 23, 0.00, 0.37, 0.37 },
+    { 24, 0.00, 0.37, 0.53 },
+    { 25, 0.00, 0.37, 0.69 },
+    { 26, 0.00, 0.37, 0.84 },
+    { 27, 0.00, 0.37, 1.00 },
+    { 28, 0.00, 0.53, 0.00 },
+    { 29, 0.00, 0.53, 0.37 },
+    { 30, 0.00, 0.53, 0.53 },
+    { 31, 0.00, 0.53, 0.69 },
+    { 32, 0.00, 0.53, 0.84 },
+    { 33, 0.00, 0.53, 1.00 },
+    { 34, 0.00, 0.69, 0.00 },
+    { 35, 0.00, 0.69, 0.37 },
+    { 36, 0.00, 0.69, 0.53 },
+    { 37, 0.00, 0.69, 0.69 },
+    { 38, 0.00, 0.69, 0.84 },
+    { 39, 0.00, 0.69, 1.00 },
+    { 40, 0.00, 0.84, 0.00 },
+    { 41, 0.00, 0.84, 0.37 },
+    { 42, 0.00, 0.84, 0.53 },
+    { 43, 0.00, 0.84, 0.69 },
+    { 44, 0.00, 0.84, 0.84 },
+    { 45, 0.00, 0.84, 1.00 },
+    { 46, 0.00, 1.00, 0.00 },
+    { 47, 0.00, 1.00, 0.37 },
+    { 48, 0.00, 1.00, 0.53 },
+    { 49, 0.00, 1.00, 0.69 },
+    { 50, 0.00, 1.00, 0.84 },
+    { 51, 0.00, 1.00, 1.00 },
+    { 52, 0.37, 0.00, 0.00 },
+    { 53, 0.37, 0.00, 0.37 },
+    { 54, 0.37, 0.00, 0.53 },
+    { 55, 0.37, 0.00, 0.69 },
+    { 56, 0.37, 0.00, 0.84 },
+    { 57, 0.37, 0.00, 1.00 },
+    { 58, 0.37, 0.37, 0.00 },
+    { 59, 0.37, 0.37, 0.37 },
+    { 60, 0.37, 0.37, 0.53 },
+    { 61, 0.37, 0.37, 0.69 },
+    { 62, 0.37, 0.37, 0.84 },
+    { 63, 0.37, 0.37, 1.00 },
+    { 64, 0.37, 0.53, 0.00 },
+    { 65, 0.37, 0.53, 0.37 },
+    { 66, 0.37, 0.53, 0.53 },
+    { 67, 0.37, 0.53, 0.69 },
+    { 68, 0.37, 0.53, 0.84 },
+    { 69, 0.37, 0.53, 1.00 },
+    { 70, 0.37, 0.69, 0.00 },
+    { 71, 0.37, 0.69, 0.37 },
+    { 72, 0.37, 0.69, 0.53 },
+    { 73, 0.37, 0.69, 0.69 },
+    { 74, 0.37, 0.69, 0.84 },
+    { 75, 0.37, 0.69, 1.00 },
+    { 76, 0.37, 0.84, 0.00 },
+    { 77, 0.37, 0.84, 0.37 },
+    { 78, 0.37, 0.84, 0.53 },
+    { 79, 0.37, 0.84, 0.69 },
+    { 80, 0.37, 0.84, 0.84 },
+    { 81, 0.37, 0.84, 1.00 },
+    { 82, 0.37, 1.00, 0.00 },
+    { 83, 0.37, 1.00, 0.37 },
+    { 84, 0.37, 1.00, 0.53 },
+    { 85, 0.37, 1.00, 0.69 },
+    { 86, 0.37, 1.00, 0.84 },
+    { 87, 0.37, 1.00, 1.00 },
+    { 88, 0.53, 0.00, 0.00 },
+    { 89, 0.53, 0.00, 0.37 },
+    { 90, 0.53, 0.00, 0.53 },
+    { 91, 0.53, 0.00, 0.69 },
+    { 92, 0.53, 0.00, 0.84 },
+    { 93, 0.53, 0.00, 1.00 },
+    { 94, 0.53, 0.37, 0.00 },
+    { 95, 0.53, 0.37, 0.37 },
+    { 96, 0.53, 0.37, 0.53 },
+    { 97, 0.53, 0.37, 0.69 },
+    { 98, 0.53, 0.37, 0.84 },
+    { 99, 0.53, 0.37, 1.00 },
+    { 100, 0.53, 0.53, 0.00 },
+    { 101, 0.53, 0.53, 0.37 },
+    { 102, 0.53, 0.53, 0.53 },
+    { 103, 0.53, 0.53, 0.69 },
+    { 104, 0.53, 0.53, 0.84 },
+    { 105, 0.53, 0.53, 1.00 },
+    { 106, 0.53, 0.69, 0.00 },
+    { 107, 0.53, 0.69, 0.37 },
+    { 108, 0.53, 0.69, 0.53 },
+    { 109, 0.53, 0.69, 0.69 },
+    { 110, 0.53, 0.69, 0.84 },
+    { 111, 0.53, 0.69, 1.00 },
+    { 112, 0.53, 0.84, 0.00 },
+    { 113, 0.53, 0.84, 0.37 },
+    { 114, 0.53, 0.84, 0.53 },
+    { 115, 0.53, 0.84, 0.69 },
+    { 116, 0.53, 0.84, 0.84 },
+    { 117, 0.53, 0.84, 1.00 },
+    { 118, 0.53, 1.00, 0.00 },
+    { 119, 0.53, 1.00, 0.37 },
+    { 120, 0.53, 1.00, 0.53 },
+    { 121, 0.53, 1.00, 0.69 },
+    { 122, 0.53, 1.00, 0.84 },
+    { 123, 0.53, 1.00, 1.00 },
+    { 124, 0.69, 0.00, 0.00 },
+    { 125, 0.69, 0.00, 0.37 },
+    { 126, 0.69, 0.00, 0.53 },
+    { 127, 0.69, 0.00, 0.69 },
+    { 128, 0.69, 0.00, 0.84 },
+    { 129, 0.69, 0.00, 1.00 },
+    { 130, 0.69, 0.37, 0.00 },
+    { 131, 0.69, 0.37, 0.37 },
+    { 132, 0.69, 0.37, 0.53 },
+    { 133, 0.69, 0.37, 0.69 },
+    { 134, 0.69, 0.37, 0.84 },
+    { 135, 0.69, 0.37, 1.00 },
+    { 136, 0.69, 0.53, 0.00 },
+    { 137, 0.69, 0.53, 0.37 },
+    { 138, 0.69, 0.53, 0.53 },
+    { 139, 0.69, 0.53, 0.69 },
+    { 140, 0.69, 0.53, 0.84 },
+    { 141, 0.69, 0.53, 1.00 },
+    { 142, 0.69, 0.69, 0.00 },
+    { 143, 0.69, 0.69, 0.37 },
+    { 144, 0.69, 0.69, 0.53 },
+    { 145, 0.69, 0.69, 0.69 },
+    { 146, 0.69, 0.69, 0.84 },
+    { 147, 0.69, 0.69, 1.00 },
+    { 148, 0.69, 0.84, 0.00 },
+    { 149, 0.69, 0.84, 0.37 },
+    { 150, 0.69, 0.84, 0.53 },
+    { 151, 0.69, 0.84, 0.69 },
+    { 152, 0.69, 0.84, 0.84 },
+    { 153, 0.69, 0.84, 1.00 },
+    { 154, 0.69, 1.00, 0.00 },
+    { 155, 0.69, 1.00, 0.37 },
+    { 156, 0.69, 1.00, 0.53 },
+    { 157, 0.69, 1.00, 0.69 },
+    { 158, 0.69, 1.00, 0.84 },
+    { 159, 0.69, 1.00, 1.00 },
+    { 160, 0.84, 0.00, 0.00 },
+    { 161, 0.84, 0.00, 0.37 },
+    { 162, 0.84, 0.00, 0.53 },
+    { 163, 0.84, 0.00, 0.69 },
+    { 164, 0.84, 0.00, 0.84 },
+    { 165, 0.84, 0.00, 1.00 },
+    { 166, 0.84, 0.37, 0.00 },
+    { 167, 0.84, 0.37, 0.37 },
+    { 168, 0.84, 0.37, 0.53 },
+    { 169, 0.84, 0.37, 0.69 },
+    { 170, 0.84, 0.37, 0.84 },
+    { 171, 0.84, 0.37, 1.00 },
+    { 172, 0.84, 0.53, 0.00 },
+    { 173, 0.84, 0.53, 0.37 },
+    { 174, 0.84, 0.53, 0.53 },
+    { 175, 0.84, 0.53, 0.69 },
+    { 176, 0.84, 0.53, 0.84 },
+    { 177, 0.84, 0.53, 1.00 },
+    { 178, 0.84, 0.69, 0.00 },
+    { 179, 0.84, 0.69, 0.37 },
+    { 180, 0.84, 0.69, 0.53 },
+    { 181, 0.84, 0.69, 0.69 },
+    { 182, 0.84, 0.69, 0.84 },
+    { 183, 0.84, 0.69, 1.00 },
+    { 184, 0.84, 0.84, 0.00 },
+    { 185, 0.84, 0.84, 0.37 },
+    { 186, 0.84, 0.84, 0.53 },
+    { 187, 0.84, 0.84, 0.69 },
+    { 188, 0.84, 0.84, 0.84 },
+    { 189, 0.84, 0.84, 1.00 },
+    { 190, 0.84, 1.00, 0.00 },
+    { 191, 0.84, 1.00, 0.37 },
+    { 192, 0.84, 1.00, 0.53 },
+    { 193, 0.84, 1.00, 0.69 },
+    { 194, 0.84, 1.00, 0.84 },
+    { 195, 0.84, 1.00, 1.00 },
+    { 196, 1.00, 0.00, 0.00 },
+    { 197, 1.00, 0.00, 0.37 },
+    { 198, 1.00, 0.00, 0.53 },
+    { 199, 1.00, 0.00, 0.69 },
+    { 200, 1.00, 0.00, 0.84 },
+    { 201, 1.00, 0.00, 1.00 },
+    { 202, 1.00, 0.37, 0.00 },
+    { 203, 1.00, 0.37, 0.37 },
+    { 204, 1.00, 0.37, 0.53 },
+    { 205, 1.00, 0.37, 0.69 },
+    { 206, 1.00, 0.37, 0.84 },
+    { 207, 1.00, 0.37, 1.00 },
+    { 208, 1.00, 0.53, 0.00 },
+    { 209, 1.00, 0.53, 0.37 },
+    { 210, 1.00, 0.53, 0.53 },
+    { 211, 1.00, 0.53, 0.69 },
+    { 212, 1.00, 0.53, 0.84 },
+    { 213, 1.00, 0.53, 1.00 },
+    { 214, 1.00, 0.69, 0.00 },
+    { 215, 1.00, 0.69, 0.37 },
+    { 216, 1.00, 0.69, 0.53 },
+    { 217, 1.00, 0.69, 0.69 },
+    { 218, 1.00, 0.69, 0.84 },
+    { 219, 1.00, 0.69, 1.00 },
+    { 220, 1.00, 0.84, 0.00 },
+    { 221, 1.00, 0.84, 0.37 },
+    { 222, 1.00, 0.84, 0.53 },
+    { 223, 1.00, 0.84, 0.69 },
+    { 224, 1.00, 0.84, 0.84 },
+    { 225, 1.00, 0.84, 1.00 },
+    { 226, 1.00, 1.00, 0.00 },
+    { 227, 1.00, 1.00, 0.37 },
+    { 228, 1.00, 1.00, 0.53 },
+    { 229, 1.00, 1.00, 0.69 },
+    { 230, 1.00, 1.00, 0.84 },
+    { 231, 1.00, 1.00, 1.00 },
+    { 232, 0.04, 0.04, 0.04 },
+    { 233, 0.08, 0.08, 0.08 },
+    { 234, 0.12, 0.12, 0.12 },
+    { 235, 0.16, 0.16, 0.16 },
+    { 236, 0.20, 0.20, 0.20 },
+    { 237, 0.24, 0.24, 0.24 },
+    { 238, 0.28, 0.28, 0.28 },
+    { 239, 0.32, 0.32, 0.32 },
+    { 240, 0.36, 0.36, 0.36 },
+    { 241, 0.40, 0.40, 0.40 },
+    { 242, 0.44, 0.44, 0.44 },
+    { 243, 0.48, 0.48, 0.48 },
+    { 244, 0.52, 0.52, 0.52 },
+    { 245, 0.56, 0.56, 0.56 },
+    { 246, 0.60, 0.60, 0.60 },
+    { 247, 0.64, 0.64, 0.64 },
+    { 248, 0.68, 0.68, 0.68 },
+    { 249, 0.72, 0.72, 0.72 },
+    { 250, 0.76, 0.76, 0.76 },
+    { 251, 0.80, 0.80, 0.80 },
+    { 252, 0.84, 0.84, 0.84 },
+    { 253, 0.88, 0.88, 0.88 },
+    { 254, 0.92, 0.92, 0.92 },
+    { 255, 0.96, 0.96, 0.96 },
+};
+
+#define nbcolors3 3
+
+color_t colors3[nbcolors3] = {
+    { 0, 0.0, 0.0, 0.0 },
+    { 0, 0.0, 0.0, 0.0 },
+    { 0, 0.0, 0.0, 0.0 },
+};
+
 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) {
@@ -48,22 +317,39 @@ color_t *findcolor (float red, float green, float blue, int colormap)
         colors = colors0;
         break;
     case 1:
-    default:
         nbcolors = nbcolors1;
         colors = colors1;
         break;
+    case 2:
+        nbcolors = nbcolors2;
+        colors = colors2;
+        break;
+    default:
+    case 3:
+        color = colors3;
+        (color + 0)->code = (int)(red * 255);
+        (color + 1)->code = (int)(green * 255);
+        (color + 2)->code = (int)(blue * 255);
+        break;
     }
-    for (i = 0; i < nbcolors; i++) {
-        float r = (colors + i)->red - red;
-        float g = (colors + i)->green - green;
-        float b = (colors + i)->blue - blue;
-        float d = r * r + g * g + b * b;
-        //printf ("%.3f/%.3f: %d (%.3f,%.3f,%.3f)\n", dist, d, (colors + i)->code, red, green, blue);
-        if (d < dist) {
-            dist = d;
-            color = colors + i;
+
+    switch (colormap) {
+    case 0:
+    case 1:
+    case 2:
+        float dist = 3;
+        for (i = 0; i < nbcolors; i++) {
+            float r = (colors + i)->red - red;
+            float g = (colors + i)->green - green;
+            float b = (colors + i)->blue - blue;
+            float d = r * r + g * g + b * b;
+            if (d < dist) {
+                dist = d;
+                color = colors + i;
+            }
         }
     }
+
     return color;
 }
 
@@ -72,9 +358,14 @@ void cprint (color_t *color, char *str)
     printf ("\e[%dm%s\e[0m", (color) ? color->code : 30, str);
 }
 
-void cprint24 (float red, float green, float blue, char *str)
+void cprint8 (color_t *color, char *str)
+{
+    printf ("\e[48;5;%dm%s\e[0m", (color) ? color->code : 30, str);
+}
+
+void cprint24 (color_t *color, char *str)
 {
-    printf ("\e[48;2;%d;%d;%dm%s\e[0m", (int)(red * 255), (int)(green * 255), (int)(blue * 255), str);
+    printf ("\e[48;2;%d;%d;%dm%s\e[0m", (color + 0)->code, (color + 1)->code, (color + 2)->code, str);
 }
 
 /* vim: set ts=4 sw=4 et: */
diff --git a/color.h b/color.h
index 8fe0f2e203f016d011da8dbe29646d0bf2ca65e1..404d2013fa783a9e44ff36d53201b403675068dd 100644 (file)
--- a/color.h
+++ b/color.h
@@ -12,7 +12,9 @@ color_t *findcolor (float red, float green, float blue, int colormap);
 
 void cprint (color_t *color, char *str);
 
-void cprint24 (float red, float green, float blue, char *str);
+void cprint8 (color_t *color, char *str);
+
+void cprint24 (color_t *color, char *str);
 
 #endif /* __COLOR_H__ */
 
index 9cd4ad17a09fab564cf85074bb9605f1d9eceffa..cc03fbc07f093bd2ed09bb7e2bc52be7019eadd9 100644 (file)
--- a/display.c
+++ b/display.c
@@ -26,7 +26,7 @@ int usage (int ret)
 {
     FILE *fd = ret ? stderr : stdout;
     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, " -c: color map [0..4] (%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");
@@ -115,6 +115,10 @@ int main (int argc, char *argv[])
     }
 
     /* check */
+    if ((colormap < 0) || (colormap > 3)) {
+        VERBOSE (ERROR, fprintf (stderr, "incorrect colormap (%d)\n", colormap));
+        return 1;
+    }
     if ((gf[0] <= 0) || (gf[1] <= 0) || (gf[2] <= 0)) {
         VERBOSE (ERROR, fprintf (stderr, "incorrect gamma (%.1f:%.1f;%.1f)\n", gf[0], gf[1], gf[2]));
         return 1;
@@ -135,11 +139,17 @@ 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;
-            if (colormap <= 2) {
-                color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap);
+            color_t *color = findcolor (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), colormap);
+            switch (colormap) {
+            case 0:
+            case 1:
                 cprint (color, (mode == 0) ? " " : "  ");
-            } else {
-                cprint24 (correction (image->red[ind], gf[0]), correction (image->green[ind], gf[1]), correction (image->blue[ind], gf[2]), (mode == 0) ? " " : "  ");
+                break;
+            case 2:
+                cprint8 (color, (mode == 0) ? " " : "  ");
+                break;
+            case 3:
+                cprint24 (color, (mode == 0) ? " " : "  ");
             }
         }
         printf ("\n");
@@ -152,6 +162,8 @@ 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 -c -1 2>&1 | grep 'incorrect colormap' */
+/* test: display.exe -c 4 2>&1 | grep 'incorrect colormap' */
 /* 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' */
@@ -175,6 +187,10 @@ int main (int argc, char *argv[])
 /* test: display.exe image/laurent4.pgm */
 /* test: display.exe image/laurent4b.pgm */
 /* test: display.exe image/laurent4.ppm -g 1.2:3:1 */
+/* test: display.exe image/laurent4.ppm -c 0 */
+/* test: display.exe image/laurent4.ppm -c 1 */
+/* test: display.exe image/laurent4.ppm -c 2 */
+/* test: display.exe image/laurent4.ppm -c 3 */
 /* test: display.exe image/debian.ppm -v 3 */
 /* test: display.exe image/debian.ppm -g 4 */
 /* test: display.exe image/python3.ppm -t */