/* Simple PPM library ---------------------------------------------------------- * Based on specification: http://netpbm.sourceforge.net/doc/ppm.html * -------------------------------------------------------------------------- */ #include "ppm.h" #include #include #define __PPM_FLIP__ 1 /* PPM write ---------------------------------------------------------------- */ int ppm_write(const char *fname, unsigned char *buffer, size_t width, size_t height) { FILE *fs = NULL; int result = 0; fs = fopen(fname, "w+"); if (!fs) goto pgm_write_cleanup; fprintf(fs, "P6\n"); /* Magic Number */ fprintf(fs, "# Simple PPM library\n"); /* Comment */ fprintf(fs, "%lu %lu\n", width, height); /* Image width, height */ fprintf(fs, "255\n"); /* Maximum color value */ /* Pixel values */ #if __PPM_FLIP__ for (size_t r = 0; r < height; r++) fwrite(&buffer[width*(height - r - 1)*3], sizeof(unsigned char), width * 3, fs); #else fwrite(buffer, sizeof(unsigned char), width * height * 3, fs); #endif result = 1; pgm_write_cleanup: if (fs) fclose(fs); return result; } /* vim: set sts=4 sw=4 ts=8 ft=cpp: ----------------------------------------- */