-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathppm.h
More file actions
36 lines (27 loc) · 685 Bytes
/
ppm.h
File metadata and controls
36 lines (27 loc) · 685 Bytes
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
#ifndef _PPM_H_
#define _PPM_H_
#include <string>
#include <cstdlib>
#include <cstdio>
#include <vector>
#include "material.h"
namespace edupt {
inline double clamp(double x){
if (x < 0.0)
return 0.0;
if (x > 1.0)
return 1.0;
return x;
}
inline int to_int(double x){
return int(pow(clamp(x), 1/2.2) * 255 + 0.5);
}
void save_ppm_file(const std::string &filename, const Color *image, const int width, const int height) {
FILE *f = fopen(filename.c_str(), "wb");
fprintf(f, "P3\n%d %d\n%d\n", width, height, 255);
for (int i = 0; i < width * height; i++)
fprintf(f,"%d %d %d ", to_int(image[i].x), to_int(image[i].y), to_int(image[i].z));
fclose(f);
}
};
#endif