/
miniexr.cpp
284 lines (263 loc) · 9.29 KB
/
miniexr.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// miniexr.cpp - v0.2 - public domain - 2013 Aras Pranckevicius / Unity
// Technologies
//
// Writes OpenEXR RGB files out of half-precision RGBA or RGB data.
//
// Only tested on Windows (VS2008) and Mac (clang 3.3), little endian.
// Testing status: "works for me".
//
// History:
// 0.2 Source data can be RGB or RGBA now.
// 0.1 Initial release.
// Compile main() that writes out a test .exr file?
#define COMPILE_TEST_MAIN_ENTRYPOINT 1
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#define ARRAY_SIZE(x) sizeof(x) / sizeof(x[0])
// Writes EXR into a memory buffer.
// Input:
// - (width) x (height) image,
// - channels=4: 8 bytes per pixel (R,G,B,A order, 16 bit float per channel;
// alpha ignored), or
// - channels=3: 6 bytes per pixel (R,G,B order, 16 bit float per channel).
// Returns memory buffer with .EXR contents and buffer size in outSize. free()
// the buffer when done with it.
unsigned char *miniexr_write(unsigned width, unsigned height, unsigned channels,
const void *rgba16f, size_t *outSize) {
const unsigned ww = width - 1;
const unsigned hh = height - 1;
const unsigned char kHeader[] = {
0x76, 0x2f, 0x31,
0x01, // magic
2, 0, 0,
0, // version, scanline
// channels
'c', 'h', 'a',
'n', 'n', 'e',
'l', 's', 0,
'c', 'h', 'l',
'i', 's', 't',
0, 55, 0,
0, 0, 'B',
0, 1, 0,
0, 0, 0,
0, 0, 0,
1, 0, 0,
0, 1, 0,
0, 0, // R, half
'G', 0, 1,
0, 0, 0,
0, 0, 0,
0, 1, 0,
0, 0, 1,
0, 0, 0, // G, half
'R', 0, 1,
0, 0, 0,
0, 0, 0,
0, 1, 0,
0, 0, 1,
0, 0, 0, // B, half
0,
// compression
'c', 'o', 'm',
'p', 'r', 'e',
's', 's', 'i',
'o', 'n', 0,
'c', 'o', 'm',
'p', 'r', 'e',
's', 's', 'i',
'o', 'n', 0,
1, 0, 0,
0,
0, // no compression
// dataWindow
'd', 'a', 't',
'a', 'W', 'i',
'n', 'd', 'o',
'w', 0, 'b',
'o', 'x', '2',
'i', 0, 16,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, ww & 0xFF,
(ww >> 8) & 0xFF, (ww >> 16) & 0xFF, (ww >> 24) & 0xFF,
hh & 0xFF, (hh >> 8) & 0xFF, (hh >> 16) & 0xFF,
(hh >> 24) & 0xFF,
// displayWindow
'd', 'i', 's',
'p', 'l', 'a',
'y', 'W', 'i',
'n', 'd', 'o',
'w', 0, 'b',
'o', 'x', '2',
'i', 0, 16,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0, ww & 0xFF,
(ww >> 8) & 0xFF, (ww >> 16) & 0xFF, (ww >> 24) & 0xFF,
hh & 0xFF, (hh >> 8) & 0xFF, (hh >> 16) & 0xFF,
(hh >> 24) & 0xFF,
// lineOrder
'l', 'i', 'n',
'e', 'O', 'r',
'd', 'e', 'r',
0, 'l', 'i',
'n', 'e', 'O',
'r', 'd', 'e',
'r', 0, 1,
0, 0, 0,
0, // increasing Y
// pixelAspectRatio
'p', 'i', 'x',
'e', 'l', 'A',
's', 'p', 'e',
'c', 't', 'R',
'a', 't', 'i',
'o', 0, 'f',
'l', 'o', 'a',
't', 0, 4,
0, 0, 0,
0, 0, 0x80,
0x3f, // 1.0f
// screenWindowCenter
's', 'c', 'r',
'e', 'e', 'n',
'W', 'i', 'n',
'd', 'o', 'w',
'C', 'e', 'n',
't', 'e', 'r',
0, 'v', '2',
'f', 0, 8,
0, 0, 0,
0, 0, 0,
0, 0, 0,
0, 0,
// screenWindowWidth
's', 'c', 'r',
'e', 'e', 'n',
'W', 'i', 'n',
'd', 'o', 'w',
'W', 'i', 'd',
't', 'h', 0,
'f', 'l', 'o',
'a', 't', 0,
4, 0, 0,
0, 0, 0,
0x80, 0x3f, // 1.0f
// end of header
0,
};
const int kHeaderSize = ARRAY_SIZE(kHeader);
const int kScanlineTableSize = 8 * height;
const unsigned pixelRowSize = width * 3 * 2;
const unsigned fullRowSize = pixelRowSize + 8;
unsigned bufSize = kHeaderSize + kScanlineTableSize + height * fullRowSize;
unsigned char *buf = (unsigned char *)malloc(bufSize);
if (!buf)
return NULL;
// copy in header
memcpy(buf, kHeader, kHeaderSize);
// line offset table
unsigned ofs = kHeaderSize + kScanlineTableSize;
unsigned char *ptr = buf + kHeaderSize;
for (int y = 0; y < height; ++y) {
*ptr++ = ofs & 0xFF;
*ptr++ = (ofs >> 8) & 0xFF;
*ptr++ = (ofs >> 16) & 0xFF;
*ptr++ = (ofs >> 24) & 0xFF;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
*ptr++ = 0;
ofs += fullRowSize;
}
// scanline data
const unsigned char *src = (const unsigned char *)rgba16f;
const int stride = channels * 2;
for (int y = 0; y < height; ++y) {
// coordinate
*ptr++ = y & 0xFF;
*ptr++ = (y >> 8) & 0xFF;
*ptr++ = (y >> 16) & 0xFF;
*ptr++ = (y >> 24) & 0xFF;
// data size
*ptr++ = pixelRowSize & 0xFF;
*ptr++ = (pixelRowSize >> 8) & 0xFF;
*ptr++ = (pixelRowSize >> 16) & 0xFF;
*ptr++ = (pixelRowSize >> 24) & 0xFF;
// B, G, R
memcpy(ptr, src, width * 6);
const unsigned char *chsrc;
chsrc = src + 4;
for (int x = 0; x < width; ++x) {
*ptr++ = chsrc[0];
*ptr++ = chsrc[1];
chsrc += stride;
}
chsrc = src + 2;
for (int x = 0; x < width; ++x) {
*ptr++ = chsrc[0];
*ptr++ = chsrc[1];
chsrc += stride;
}
chsrc = src + 0;
for (int x = 0; x < width; ++x) {
*ptr++ = chsrc[0];
*ptr++ = chsrc[1];
chsrc += stride;
}
src += width * stride;
}
assert(ptr - buf == bufSize);
*outSize = bufSize;
return buf;
}
#if COMPILE_TEST_MAIN_ENTRYPOINT
#include <stdio.h>
#include <math.h>
static unsigned short FloatToHalf(float f) {
union convertf2i {
unsigned int i;
float f;
};
convertf2i f2i;
f2i.f = f;
unsigned int i = f2i.i;
if (i == 0)
return 0;
// Not robust at handling denormals, infinities, ...
return ((i >> 16) & 0x8000) |
((((i & 0x7f800000) - 0x38000000) >> 13) & 0x7c00) |
((i >> 13) & 0x03ff);
}
#if 0
int main()
{
const int kW = 64;
const int kH = 32;
unsigned short rgba[kW*kH*4];
unsigned ofs = 0;
const float kGamma = 2.2f;
for (int y = 0; y < kH; ++y)
{
for (int x = 0; x < kW; ++x)
{
rgba[ofs++] = FloatToHalf(powf(x*2.0f/kW,kGamma)); // horizontal red gradient
rgba[ofs++] = FloatToHalf(powf(y*2.0f/kH,kGamma)); // vertical green gradient
rgba[ofs++] = FloatToHalf((x&y) ? 1.0f : 0.0f); // blue Sierpinski triangle
rgba[ofs++] = 0;
}
}
size_t exrSize;
unsigned char* exr = miniexr_write (kW, kH, 4, rgba, &exrSize);
FILE* f = fopen ("test.exr", "wb");
fwrite (exr, 1, exrSize, f);
fclose (f);
free (exr);
return 0;
}
#endif
#endif