-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe_buffer.h
More file actions
234 lines (191 loc) · 4.25 KB
/
Copy pathframe_buffer.h
File metadata and controls
234 lines (191 loc) · 4.25 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#pragma once
#include <algorithm>
#include <vector>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <string>
#include "float3.h"
#include <assert.h>
class frame_buffer
{
public:
int width;
int height;
std::vector<float3> pixels;
frame_buffer () :
width (0), height (0)
{}
frame_buffer (int w, int h) :
width (w), height (h), pixels (w*h, float3 {0.0f,0.0f,0.0f})
{}
frame_buffer (const frame_buffer &o) = default;
frame_buffer (frame_buffer &&o) = default;
frame_buffer &operator = (const frame_buffer &o) = default;
frame_buffer &operator = (frame_buffer &&o) = default;
void set (int x, int y, const float3 &v)
{
assert (x >= 0 && x < width && y >= 0 && y < height);
pixels[x+y*width] = v;
}
float3 get (int x, int y) const
{
assert (x >= 0 && x < width && y >= 0 && y < height);
return pixels[x+y*width];
}
bool read_ppm (const char *file);
bool write_tga (const char *file) const;
bool write_ppm (const char *file) const;
};
//#define USE_GAMMA
static inline uint8_t to_image_space (float x)
{
// clamp to 0-1
x = std::max (0.0f, std::min (1.0f, x));
#ifdef USE_GAMMA
// inverse gamma
x = std::pow (x, 1.0f/2.2f);
#endif
// quantize
return uint8_t (std::floor (x * 255.0f));
}
static inline float from_image_space (uint8_t x)
{
#ifdef USE_GAMMA
return std::pow (x/255.0f, 2.2f);
#else
return x/255.0f;
#endif
}
inline bool frame_buffer::write_tga (const char *file) const
{
FILE *f = fopen (file, "wb");
if (f == NULL)
return false;
uint8_t targaheader[18];
// Set unused fields of header to 0
memset (targaheader, 0, sizeof(targaheader));
targaheader[2] = 3; // image type = uncompressed RGB
targaheader[12] = (uint8_t) (width & 0xFF);
targaheader[13] = (uint8_t) (width >> 8);
targaheader[14] = (uint8_t) (height & 0xFF);
targaheader[15] = (uint8_t) (height >> 8);
targaheader[16] = 24; // 3x8-bit per pixel
targaheader[17] = 0x20; // Top-down, non-interlaced
// write header
if (fwrite (targaheader, 18, 1, f) != 1)
{
fclose (f);
return false;
}
const float3 *pixel = pixels.data ();
// write scanlines
std::vector<uint8_t> buffer (width*3);
for (int i = 0; i < height; i++)
{
uint8_t *dest = buffer.data ();
for (int j = 0; j < width; j++)
{
dest[0] = to_image_space (pixel->z);
dest[1] = to_image_space (pixel->y);
dest[2] = to_image_space (pixel->x);
++pixel;
dest += 3;
}
if (fwrite (buffer.data (), width*3, 1, f) != 1)
{
fclose (f);
return false;
}
}
fclose (f);
return true;
}
inline bool frame_buffer::write_ppm (const char *file) const
{
FILE *f = fopen (file, "wb");
if (f == NULL)
return false;
fprintf (f, "P6\n%d %d\n255\n", width, height);
const float3 *pixel = pixels.data ();
// write scanlines
std::vector<uint8_t> buffer (width*3);
for (int i = 0; i < height; i++)
{
uint8_t *dest = buffer.data ();
for (int j = 0; j < width; j++)
{
dest[0] = to_image_space (pixel->x);
dest[1] = to_image_space (pixel->y);
dest[2] = to_image_space (pixel->z);
++pixel;
dest += 3;
}
if (fwrite (buffer.data (), width*3, 1, f) != 1)
{
fclose (f);
return false;
}
}
fclose (f);
return true;
}
inline bool frame_buffer::read_ppm (const char *file)
{
FILE *f = fopen (file, "rb");
if (f == NULL)
return false;
std::string header;
int c;
// read header
while ((c = fgetc (f)) != EOF && c != '\n')
header += c;
if (header != "P6")
{
fclose (f);
return false;
}
do
{
header.clear ();
while ((c = fgetc (f)) != EOF && c != '\n')
header += c;
}
while (header[0] == '#');
if (sscanf (header.c_str (), "%d %d", &width, &height) != 2)
{
fclose (f);
return false;
}
header.clear ();
while ((c = fgetc (f)) != EOF && c != '\n')
header += c;
if (header != "255")
{
fclose (f);
return false;
}
pixels.resize (width*height);
float3 *pixel = pixels.data ();
// write scanlines
std::vector<uint8_t> buffer (width*3);
for (int i = 0; i < height; i++)
{
if (fread (buffer.data (), width*3, 1, f) != 1)
{
fclose (f);
return false;
}
uint8_t *src = buffer.data ();
for (int j = 0; j < width; j++)
{
pixel->x = from_image_space (src[0]);
pixel->y = from_image_space (src[1]);
pixel->z = from_image_space (src[2]);
++pixel;
src += 3;
}
}
fclose (f);
return true;
}