-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.h
69 lines (55 loc) · 1.44 KB
/
image.h
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Created by ilim on 2018/10/19.
//
#ifndef RENDERER_IMAGE_H
#define RENDERER_IMAGE_H
#include <fstream>
#include <cmath>
#include <algorithm>
#include "vector.h"
struct Image;
namespace image {
void output_ppm(std::string file_name, const Image &image);
}
struct Image {
int width;
int height;
int size;
Vector3 *data;
Image(int width, int height) : width(width), height(height), size(width * height) {
data = new Vector3[size]();
}
~Image() {
// delete data;
}
void set_pixel(int x, int y, const Vector3 v) {
data[coord2index(x, y)] = v;
}
Vector3 get_pixel(int x, int y) const {
return data[coord2index(x, y)];
}
void output_ppm(std::string file_name) const {
image::output_ppm(file_name, *this);
}
private:
int coord2index(int x, int y) const {
return x + y * width;
}
};
namespace image {
double tonemap(double k) {
return std::pow(std::clamp(k, 0., 1.), 1. / 2.2) * 255;
}
Vector3 tonemap(const Vector3 &v) {
return Vector3(tonemap(v.x), tonemap(v.y), tonemap(v.z));
}
void output_ppm(std::string file_name, const Image &image) {
std::ofstream ofs(file_name);
ofs << "P3\n" << image.width << " " << image.height << "\n255\n";
for (int i = 0; i < image.size; ++i) {
ofs << tonemap(image.data[i]);
}
ofs.close();
}
}
#endif //RENDERER_IMAGE_H