#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)
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;
/* 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: */
return num;
}
-float getvalue (char *buffer, int maxvalue)
+float getvalue (unsigned char *buffer, int maxvalue)
{
float value = *(buffer++) / (float)maxvalue;
if (maxvalue > 255) {
int readnumber (FILE *fd);
-float getvalue (char *buffer, int maxvalue);
+float getvalue (unsigned char *buffer, int maxvalue);
#endif /* __IMAGE_H__ */
image_t *readpgm (char *filename)
{
- char *buffer = NULL;
+ unsigned char *buffer = NULL;
image_t *image = NULL;
/* open file */
/* 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;
}
image_t *readppm (char *filename)
{
- char *buffer = NULL;
+ unsigned char *buffer = NULL;
image_t *image = NULL;
/* open file */
/* 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;
}