From 9bc5c13ed0bbd99f3e017eee7529c94782fa7e05 Mon Sep 17 00:00:00 2001 From: Laurent MAZET Date: Thu, 12 Sep 2024 19:18:09 +0200 Subject: [PATCH] add pgm loader --- display.c | 9 ++++---- image.c | 32 +++++++++++++++++++++++++++ image.h | 8 +++++++ pgm.c | 66 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ pgm.h | 8 +++++++ ppm.c | 38 +++----------------------------- 6 files changed, 122 insertions(+), 39 deletions(-) create mode 100644 pgm.c create mode 100644 pgm.h diff --git a/display.c b/display.c index d0f44c6..c11824b 100644 --- a/display.c +++ b/display.c @@ -1,8 +1,8 @@ /* depend: */ /* cflags: */ -/* linker: color.o image.o ppm.o */ -/* doslnk: color.o image.o ppm.o */ -/* winlnk: color.o image.o ppm.o */ +/* 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 */ #include @@ -12,6 +12,7 @@ int main (int argc, char *argv[]) { + float gamma = 1.; int i; for (i = 1; i < argc; i++) { image_t *image = readppm (argv[i]); @@ -20,7 +21,7 @@ int main (int argc, char *argv[]) for (k = 0; k < image->width; k++) { for (l = 0; l < image->height; l++) { int ind = k + image->width * l; - color_t *color = findcolor (image->red[ind], image->green[ind], image->blue[ind]); + color_t *color = findcolor (gamma * image->red[ind], gamma * image->green[ind], gamma * image->blue[ind]); cprint (color, " "); } printf ("\n"); diff --git a/image.c b/image.c index 9efb3a4..81ebbe3 100644 --- a/image.c +++ b/image.c @@ -22,4 +22,36 @@ void freeimage (image_t *image) } } +int contain (char *str, char c) +{ + while (*str != c) { + if (*str++ == '\0') { + return 0; + } + } + return 1; +} + +int readnumber (FILE *fd) +{ + int num = 0; + fscanf (fd, "%d", &num); + char c = 0; + c = fgetc (fd); + if (!contain (" \t\n\r", c)) { + num = 0; + } + return num; +} + +float getvalue (char *buffer, int maxvalue) +{ + float value = *(buffer++) / (float)maxvalue; + if (maxvalue > 255) { + value *= 256; + value += *buffer / (float)maxvalue; + } + return value; +} + /* vim: set ts=4 sw=4 et: */ diff --git a/image.h b/image.h index 1cc6f2c..657424f 100644 --- a/image.h +++ b/image.h @@ -1,6 +1,8 @@ #ifndef __IMAGE_H__ #define __IMAGE_H__ +#include + typedef struct { float *red; float *green; @@ -13,4 +15,10 @@ image_t *newimage (int width, int height); void freeimage (image_t *image); +int contain (char *str, char c); + +int readnumber (FILE *fd); + +float getvalue (char *buffer, int maxvalue); + #endif /* __IMAGE_H__ */ diff --git a/pgm.c b/pgm.c new file mode 100644 index 0000000..7743520 --- /dev/null +++ b/pgm.c @@ -0,0 +1,66 @@ +#include +#include + +#include "image.h" + +image_t *readpgm (char *filename) +{ + char *buffer = NULL; + image_t *image = NULL; + + /* open file */ + FILE *fd = fopen (filename, "rb"); + if (!fd) { + return NULL; + } + + /* check magic number */ + char magic[3] = {0}; + fread (magic, 3, 1, fd); + if ((!contain ("P", magic[0])) || + (!contain ("5", magic[1])) || + (!contain (" \t\n\r", magic[2]))) { + goto err; + } + + /* get width */ + int width = readnumber (fd); + if (width == 0) { + goto err; + } + + /* get height */ + int height = readnumber (fd); + if (height == 0) { + goto err; + } + + /* get maxvalue */ + int maxvalue = readnumber (fd); + if (maxvalue == 0) { + goto err; + } + + /* read full image */ + int size = width * height * ((maxvalue > 255) ? 2 : 1); + buffer = (char *) calloc (size, 1); + if (!fread (buffer, size, 1, fd)) { + goto err; + } + + /* create image */ + image = newimage (width, height); + int i = 0; + int gap = (maxvalue > 255) ? 2 : 1; + while (i < width * height) { + image->red[i] = image->green[i] = image->blue[i] = getvalue (buffer + gap * i, maxvalue); + i++; + } + +err: + free (buffer); + fclose (fd); + return image; +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/pgm.h b/pgm.h new file mode 100644 index 0000000..b634a8f --- /dev/null +++ b/pgm.h @@ -0,0 +1,8 @@ +#ifndef __PGM_H__ +#define __PGM_H__ + +#include "image.h" + +image_t *readpgm (char *filename); + +#endif /* __PPM_H__ */ diff --git a/ppm.c b/ppm.c index 7d7ad1c..538b153 100644 --- a/ppm.c +++ b/ppm.c @@ -3,38 +3,6 @@ #include "image.h" -int contain (char *str, char c) -{ - while (*str != c) { - if (*str++ == '\0') { - return 0; - } - } - return 1; -} - -int readnumber (FILE *fd) -{ - int num = 0; - fscanf (fd, "%d", &num); - char c = 0; - c = fgetc (fd); - if (!contain (" \t\n\r", c)) { - num = 0; - } - return num; -} - -float getcolor (char *buffer, int maxcolor) -{ - float color = *(buffer++) / (float)maxcolor; - if (maxcolor > 255) { - color *= 256; - color += *buffer / (float)maxcolor; - } - return color; -} - image_t *readppm (char *filename) { char *buffer = NULL; @@ -85,9 +53,9 @@ image_t *readppm (char *filename) int i = 0; int gap = (maxcolor > 255) ? 2 : 1; while (i < width * height) { - image->red[i] = getcolor (buffer + (3 * gap) * i, maxcolor); - image->green[i] = getcolor (buffer + (3 * gap) * i + gap, maxcolor); - image->blue[i] = getcolor (buffer + (3 * gap) * i + 2 * gap, maxcolor); + image->red[i] = getvalue (buffer + (3 * gap) * i, maxcolor); + image->green[i] = getvalue (buffer + (3 * gap) * i + gap, maxcolor); + image->blue[i] = getvalue (buffer + (3 * gap) * i + 2 * gap, maxcolor); i++; } -- 2.30.2