correct colors
authorLaurent Mazet <mazet@softndesign.org>
Thu, 12 Sep 2024 22:04:11 +0000 (00:04 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Thu, 12 Sep 2024 22:04:11 +0000 (00:04 +0200)
color.c
display.c
image.c
image.h
pgm.c
ppm.c

diff --git a/color.c b/color.c
index 38fb9c38e673fe12e147ae200912f688b5a7b2ae..448f49507a8930343862a85e4192bfdca1d08522 100644 (file)
--- a/color.c
+++ b/color.c
@@ -5,22 +5,22 @@
 #define nbcolors 16
 
 color_t colors[nbcolors] = {
-    { 40, 0, 0, 0 },
-    { 41, 0.5, 0, 0 },
-    { 42, 0, 0.5, 0 },
-    { 43, 0.5, 0.5, 0 },
-    { 44, 0, 0, 0.5 },
-    { 45, 0.5, 0, 0.5 },
-    { 46, 0, 0.5, 0.5 },
-    { 47, 0.5, 0.5, 0.5 },
-    { 100, 0.25, 0.25, 0.25 },
-    { 101, 0.75, 0, 0 },
-    { 102, 0, 0.75, 0 },
-    { 103, 0.75, 0.75, 0 },
-    { 104, 0, 0, 0.75 },
-    { 105, 0.75, 0, 0.75 },
-    { 106, 0, 0.75, 0.75 },
-    { 107, 0.75, 0.75, 0.75 },
+    { 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 },
+    { 107, 1.0, 1.0, 1.0 },
 };
 
 color_t *findcolor (float red, float green, float blue)
@@ -33,6 +33,7 @@ color_t *findcolor (float red, float green, float blue)
         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;
index b097ff78e065e564c346ab963846a7140f3f0e22..61dad57209411e1274137ee5feed552c382ce08e 100644 (file)
--- a/display.c
+++ b/display.c
 /* depend: */
 /* cflags: */
-/* linker: color.o image.o pgm.o ppm.o */
-/* doslnk: color.o image.o pgm.o ppm.o */
-/* winlnk: color.o image.o pgm.o ppm.o */
+/* linker: color.o debug.o image.o pgm.o ppm.o -lm */
+/* doslnk: color.o debug.o image.o pgm.o ppm.o -lm */
+/* winlnk: color.o debug.o image.o pgm.o ppm.o -lm */
 
+#include <math.h>
 #include <stdio.h>
+#include <stdlib.h>
 
 #include "color.h"
+#include "debug.h"
 #include "image.h"
+#include "pgm.h"
 #include "ppm.h"
 
+/* static variables */
+char *progname = NULL;
+char *version = "0.1";
+
+float fgamma = 1.0;
+
+/* help message */
+int usage (int ret)
+{
+    FILE *fd = ret ? stderr : stdout;
+    fprintf (fd, "usage: %s [-g gamma] [-h] <file>\n", progname);
+    fprintf (fd, " -g: gamma correction (%.1f)\n", fgamma);
+    fprintf (fd, " -h: help message\n");
+    fprintf (fd, "%s version %s\n", progname, version);
+
+    return ret;
+}
+
+float correction (float value, float factor)
+{
+    return powf (value, 1. / factor);
+}
+
+/* main function */
 int main (int argc, char *argv[])
 {
-    float gamma = 1.0;
-    int i;
-    for (i = 1; i < argc; i++) {
-       image_t *image = readppm (argv[i]);
-
-       int k, l;
-       for (l = 0; l < image->height; l++) {
-           for (k = 0; k < image->width; k++) {
-               int ind = k + image->width * l;
-               color_t *color = findcolor (gamma * image->red[ind], gamma * image->green[ind], gamma * image->blue[ind]);
-               cprint (color, " ");
-           }
-           printf ("\n");
-       }
+    char *filename = NULL;
+
+    /* get basename */
+    char *pt = progname = argv[0];
+    while (*pt) {
+        if ((*pt == '/') || (*pt == '\\')) {
+           progname = pt + 1;
+        }
+        pt++;
+    }
+
+    /* process argument */
+    while (argc-- > 1) {
+        char *arg = *(++argv);
+        if (arg[0] != '-') {
+            filename = arg;
+            continue;
+        }
+        char c = arg[1];
+        switch (c) {
+        case 'g':
+            arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL;
+            if (arg == NULL) {
+                VERBOSE (ERROR, fprintf (stderr, "%s: no gamma specified\n", progname));
+                return usage (1);
+            }
+            fgamma = strtof (arg, NULL);
+            break;
+        case 'h':
+        default:
+            return usage (c != 'h');
+        }
+    }
+
+    /* check */
+    if (fgamma <= 0) {
+        VERBOSE (ERROR, fprintf (stderr, "incorrect gamma (%.1f)\n", fgamma));
+        return 1;
+    }
+    if (filename == NULL) {
+        VERBOSE (ERROR, fprintf (stderr, "no file specified\n"));
+        return 1;
+    }
+
+    /* main process */
+    image_t *image = readppm (filename);
+
+    int k, l;
+    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], fgamma), correction (image->green[ind], fgamma), correction (image->blue[ind], fgamma));
+            cprint (color, " ");
+        }
+        printf ("\n");
     }
 
     return 0;
 }
 
+/* test: display.exe 2>&1 | grep 'no file specified' */
+/* test: display.exe -g 2>&1 | grep 'no gamma specified' */
+/* test: display.exe -g -1.0 2>&1 | grep 'incorrect gamma' */
+/* test: display.exe -h | grep usage */
+
 /* vim: set ts=4 sw=4 et: */
diff --git a/image.c b/image.c
index 81ebbe3a5e15f32f83cb0694562c171e823fa5f1..98ff1bb486b7e671da4a849423333ce0a120fa51 100644 (file)
--- a/image.c
+++ b/image.c
@@ -44,7 +44,7 @@ int readnumber (FILE *fd)
     return num;
 }
 
-float getvalue (char *buffer, int maxvalue)
+float getvalue (unsigned char *buffer, int maxvalue)
 {
     float value = *(buffer++) / (float)maxvalue;
     if (maxvalue > 255) {
diff --git a/image.h b/image.h
index 657424f6f78b90b171f96091b76dbdd19bddb62f..3d8eb26f78b81ea03e41dc1d7dbd0fe6c67cb9b2 100644 (file)
--- a/image.h
+++ b/image.h
@@ -19,6 +19,6 @@ int contain (char *str, char c);
 
 int readnumber (FILE *fd);
 
-float getvalue (char *buffer, int maxvalue);
+float getvalue (unsigned char *buffer, int maxvalue);
 
 #endif /* __IMAGE_H__ */
diff --git a/pgm.c b/pgm.c
index 77435205b25e3ca91822e5d5390dbd3a37384294..9dd11b02acbf1695bb552dd8db7804d38d1ce28b 100644 (file)
--- a/pgm.c
+++ b/pgm.c
@@ -5,7 +5,7 @@
 
 image_t *readpgm (char *filename)
 {
-    char *buffer = NULL;
+    unsigned char *buffer = NULL;
     image_t *image = NULL;
 
     /* open file */
@@ -43,7 +43,7 @@ image_t *readpgm (char *filename)
 
     /* read full image */
     int size = width * height * ((maxvalue > 255) ? 2 : 1);
-    buffer = (char *) calloc (size, 1);
+    buffer = (unsigned char *) calloc (size, 1);
     if (!fread (buffer, size, 1, fd)) {
         goto err;
     }
diff --git a/ppm.c b/ppm.c
index 538b1531668d610fd8779bbed3eb096c964b9ac7..b5efcd36cc31103ec3f61cc3ce132d040a649048 100644 (file)
--- a/ppm.c
+++ b/ppm.c
@@ -5,7 +5,7 @@
 
 image_t *readppm (char *filename)
 {
-    char *buffer = NULL;
+    unsigned char *buffer = NULL;
     image_t *image = NULL;
 
     /* open file */
@@ -43,7 +43,7 @@ image_t *readppm (char *filename)
 
     /* read full image */
     int size = 3 * width * height * ((maxcolor > 255) ? 2 : 1);
-    buffer = (char *) calloc (size, 1);
+    buffer = (unsigned char *) calloc (size, 1);
     if (!fread (buffer, size, 1, fd)) {
         goto err;
     }