helper function to write the rendered image as PPM file
| 25 | |
| 26 | // helper function to write the rendered image as PPM file |
| 27 | void writePPM( |
| 28 | const char *fileName, int size_x, int size_y, const uint32_t *pixel) |
| 29 | { |
| 30 | FILE *file = fopen(fileName, "wb"); |
| 31 | if (!file) { |
| 32 | fprintf(stderr, "fopen('%s', 'wb') failed: %d", fileName, errno); |
| 33 | return; |
| 34 | } |
| 35 | fprintf(file, "P6\n%i %i\n255\n", size_x, size_y); |
| 36 | unsigned char *out = (unsigned char *)alloca(3 * size_x); |
| 37 | for (int y = 0; y < size_y; y++) { |
| 38 | const unsigned char *in = |
| 39 | (const unsigned char *)&pixel[(size_y - 1 - y) * size_x]; |
| 40 | for (int x = 0; x < size_x; x++) { |
| 41 | out[3 * x + 0] = in[4 * x + 0]; |
| 42 | out[3 * x + 1] = in[4 * x + 1]; |
| 43 | out[3 * x + 2] = in[4 * x + 2]; |
| 44 | } |
| 45 | fwrite(out, 3 * size_x, sizeof(char), file); |
| 46 | } |
| 47 | fprintf(file, "\n"); |
| 48 | fclose(file); |
| 49 | } |
| 50 | |
| 51 | int main(int argc, const char **argv) |
| 52 | { |