/* 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 <stdio.h>
int main (int argc, char *argv[])
{
+ float gamma = 1.;
int i;
for (i = 1; i < argc; i++) {
image_t *image = readppm (argv[i]);
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");
}
}
+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: */
#ifndef __IMAGE_H__
#define __IMAGE_H__
+#include <stdio.h>
+
typedef struct {
float *red;
float *green;
void freeimage (image_t *image);
+int contain (char *str, char c);
+
+int readnumber (FILE *fd);
+
+float getvalue (char *buffer, int maxvalue);
+
#endif /* __IMAGE_H__ */
--- /dev/null
+#include <stdio.h>
+#include <stdlib.h>
+
+#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: */
--- /dev/null
+#ifndef __PGM_H__
+#define __PGM_H__
+
+#include "image.h"
+
+image_t *readpgm (char *filename);
+
+#endif /* __PPM_H__ */
#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;
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++;
}