From 4ac6b92435d345d96fd5c704f31d271c3469f2a8 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Fri, 13 Sep 2024 00:04:11 +0200 Subject: [PATCH] correct colors --- color.c | 33 +++++++++-------- display.c | 108 +++++++++++++++++++++++++++++++++++++++++++++--------- image.c | 2 +- image.h | 2 +- pgm.c | 4 +- ppm.c | 4 +- 6 files changed, 114 insertions(+), 39 deletions(-) diff --git a/color.c b/color.c index 38fb9c3..448f495 100644 --- 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; diff --git a/display.c b/display.c index b097ff7..61dad57 100644 --- a/display.c +++ b/display.c @@ -1,34 +1,108 @@ /* 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 #include +#include #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] \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 81ebbe3..98ff1bb 100644 --- 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 657424f..3d8eb26 100644 --- 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 7743520..9dd11b0 100644 --- 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 538b153..b5efcd3 100644 --- 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; } -- 2.30.2