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