-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm.cpp
48 lines (37 loc) · 1.13 KB
/
ppm.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <algorithm>
#include "image.h"
#include "util.h"
// writePPMImage --
//
// assumes input pixels are float4
// write 3-channel (8 bit --> 24 bits per pixel) ppm
void
writePPMImage(const Image* image, const char *filename)
{
FILE *fp = fopen(filename, "wb");
if (!fp) {
fprintf(stderr, "Error: could not open %s for write\n", filename);
exit(1);
}
// write ppm header
fprintf(fp, "P6\n");
fprintf(fp, "%d %d\n", image->width, image->height);
fprintf(fp, "255\n");
for (int j=image->height-1; j>=0; j--) {
for (int i=0; i<image->width; i++) {
const float* ptr = &image->data[4 * (j*image->width + i)];
char val[3];
val[0] = static_cast<char>(255.f * CLAMP(ptr[0], 0.f, 1.f));
val[1] = static_cast<char>(255.f * CLAMP(ptr[1], 0.f, 1.f));
val[2] = static_cast<char>(255.f * CLAMP(ptr[2], 0.f, 1.f));
fputc(val[0], fp);
fputc(val[1], fp);
fputc(val[2], fp);
}
}
fclose(fp);
printf("Wrote image file %s\n", filename);
}