00001 #include <stdio.h>
00002 #include "image.hh"
00003
00004 extern "C" {
00005 #include <ppm.h>
00006 }
00007
00008 inline pixel PPMImageWriter::makePixel (const Color &c) {
00009 pixel p;
00010 PPM_ASSIGN(p,
00011 (pixval)(c[0] * 255),
00012 (pixval)(c[1] * 255),
00013 (pixval)(c[2] * 255));
00014 return p;
00015 }
00016
00017 void PPMImageWriter::write (const ImageBuffer<2> &image, const char *fname) {
00018 unsigned int w = image.getSize(0), h = image.getSize(1);
00019 FILE *out = fopen(fname, "w");
00020 assert (out != NULL);
00021 ppm_writeppminit (out, w, h, 255, 0);
00022 pixel *row = ppm_allocrow (w);
00023 unsigned int pos[2];
00024 for (pos[1] = 0; pos[1] < h; pos[1] ++) {
00025 for (pos[0] = 0; pos[0] < w; pos[0] ++) {
00026 row[pos[0]] = makePixel (image.getColor(pos));
00027 }
00028 ppm_writeppmrow (out, row, w, PPM_MAXMAXVAL, 0);
00029 }
00030 ppm_freerow (row);
00031 fclose (out);
00032 }
00033
00034