-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpanoramaPacker.cpp
326 lines (258 loc) · 10.2 KB
/
panoramaPacker.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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#include <unistd.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <limits>
#include <sstream>
#include <OpenImageIO/filter.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/imageio.h>
#include "Color"
#include "Cubemap"
#include "Math"
OIIO_NAMESPACE_USING
bool writeByChannel = false;
struct PanoramaRGBA8 {
int _size; // width
uint8_t* _image;
void init(int size) {
_size = size;
_image = new uint8_t[size * size * 4];
}
void pack(const std::string& file) {
FILE* output = fopen(file.c_str(), "wb");
if (writeByChannel) {
// write by channel
for (int c = 0; c < 4; c++)
for (int b = 0; b < _size * _size; b++) {
uint8_t* p = &(_image[b * 4]);
fwrite(&p[c], 1, 1, output);
}
} else {
fwrite(_image, _size * _size * 4, 1, output);
}
}
};
struct PanoramaRGB32 {
int _size; // width
float* _image;
void init(int size) {
_size = size;
_image = new float[size * size * 3];
}
void pack(const std::string& file) {
FILE* output = fopen(file.c_str(), "wb");
if (writeByChannel) {
// write by channel
for (int c = 0; c < 3; c++)
for (int b = 0; b < _size * _size; b++) {
float* p = &(_image[b * 3]);
fwrite(&p[c], 4, 1, output);
}
} else {
fwrite(_image, _size * _size * 3 * 4, 1, output);
}
}
};
class Packer {
public:
PanoramaRGBA8 _RGBM;
PanoramaRGBA8 _RGBE;
PanoramaRGBA8 _LUV;
PanoramaRGB32 _FLOAT;
std::string _filePattern;
std::string _outputDirectory;
bool _rgbm, _rgbe, _float, _luv;
int _maxLevel;
Packer(const std::string& filenamePattern, int level,
const std::string& outputDirectory) {
_filePattern = filenamePattern;
_maxLevel = level;
_outputDirectory = outputDirectory;
_rgbe = _float = _rgbm = _luv = false;
}
void setRGBE(bool state) { _rgbe = state; }
void setRGBM(bool state) { _rgbm = state; }
void setFloat(bool state) { _float = state; }
void setLUV(bool state) { _luv = state; }
ImageBuf* mipmap() {
int size = int(pow(2, _maxLevel));
ImageSpec specMip(size, size, 3, TypeDesc::FLOAT);
_FLOAT.init(size);
float* data = _FLOAT._image;
ImageBuf* imageMip = new ImageBuf(specMip, data);
uint offset = size / 2;
char str[256];
for (int level = 0; level < _maxLevel + 1; level++) {
int size = int(pow(2, _maxLevel - level));
std::cout << "packing level " << level << " size " << size
<< std::endl;
int strSize = snprintf(str, 255, _filePattern.c_str(), level);
str[strSize + 1] = 0;
ImageBuf src(str);
if (!src.read() || size <= 4) {
std::cout << "skipping file " << str << std::endl;
continue;
}
#if 1
int heightSizeWithoutBorder = size / 2 - 2;
int widthSizeWithoutBorder = size - 2;
ImageSpec specResize(widthSizeWithoutBorder,
heightSizeWithoutBorder, 3, TypeDesc::FLOAT);
ImageBuf imageResized = ImageBuf(specResize);
ImageBufAlgo::resize(imageResized, src);
// handle top / bottom lines
ImageBufAlgo::paste(*imageMip, 1, offset, 0, 0, imageResized,
ROI(0, widthSizeWithoutBorder, 0, 1));
ImageBufAlgo::paste(
*imageMip, 1, offset + size / 2 - 1, 0, 0, imageResized,
ROI(0, widthSizeWithoutBorder, heightSizeWithoutBorder - 1,
heightSizeWithoutBorder));
// left / right
ImageBufAlgo::paste(
*imageMip, 0, offset + 1, 0, 0, imageResized,
ROI(widthSizeWithoutBorder - 1, widthSizeWithoutBorder, 0,
heightSizeWithoutBorder));
ImageBufAlgo::paste(*imageMip, size - 1, offset + 1, 0, 0,
imageResized,
ROI(0, 1, 0, heightSizeWithoutBorder));
// corner
Vec3f pixelTmp;
Vec3f pixel;
// top
imageResized.getpixel(widthSizeWithoutBorder - 1, 0, 0, &pixel[0]);
imageResized.getpixel(0, 0, 0, &pixelTmp[0]);
pixel = (pixelTmp + pixel) * 0.5;
// top left
imageMip->setpixel(0, offset, 0, &pixel[0]);
// top right
imageMip->setpixel(size - 1, offset, 0, &pixel[0]);
// bottom
imageResized.getpixel(widthSizeWithoutBorder - 1,
heightSizeWithoutBorder - 1, 0, &pixel[0]);
imageResized.getpixel(0, heightSizeWithoutBorder - 1, 0,
&pixelTmp[0]);
pixel = (pixelTmp + pixel) * 0.5;
// bottom rigth
imageMip->setpixel(size - 1, offset + size / 2 - 1, 0, &pixel[0]);
// bottom left
imageMip->setpixel(0, offset + size / 2 - 1, 0, &pixel[0]);
// image resized
ImageBufAlgo::paste(*imageMip, 1, offset + 1, 0, 0, imageResized);
#else
ImageBufAlgo::paste(*imageMip, 0, offset, 0, 0, src);
#endif
offset = offset / 2;
}
if (_float) {
_FLOAT.pack(_outputDirectory + "_float.bin");
// imageMip->write("/tmp/debug_panorama_prefilter.tif");
}
return imageMip;
}
void pack(ImageBuf* image) {
int size = image->spec().width;
_RGBE.init(size);
_RGBM.init(size);
_LUV.init(size);
ImageBuf::Iterator<float, float> iteratorSrc(*image, 0, size, 0, size);
ImageSpec specOutRGBE(size, size, 4, TypeDesc::UINT8);
specOutRGBE.attribute("oiio:UnassociatedAlpha", 1);
ImageSpec specOutRGBM(size, size, 4, TypeDesc::UINT8);
specOutRGBM.attribute("oiio:UnassociatedAlpha", 1);
ImageSpec specOutLUV(size, size, 4, TypeDesc::UINT8);
specOutLUV.attribute("oiio:UnassociatedAlpha", 1);
ImageBuf dstRGBE(specOutRGBE, _RGBE._image);
ImageBuf dstRGBM(specOutRGBM, _RGBM._image);
ImageBuf dstLUV(specOutLUV, _LUV._image);
ImageBuf::Iterator<uint8_t, uint8_t> iteratorDstRGBE(dstRGBE, 0, size,
0, size);
ImageBuf::Iterator<uint8_t, uint8_t> iteratorDstRGBM(dstRGBM, 0, size,
0, size);
ImageBuf::Iterator<uint8_t, uint8_t> iteratorDstLUV(dstLUV, 0, size, 0,
size);
float inTmp[3];
float* in;
for (; iteratorDstRGBE.valid(); iteratorSrc++, iteratorDstRGBE++,
iteratorDstRGBM++, iteratorDstLUV++) {
iteratorSrc.pos(iteratorDstRGBE.x(), iteratorDstRGBE.y(),
iteratorDstRGBE.z());
float* inRaw = (float*)iteratorSrc.rawptr();
// we assume to have at least 3 channel in inputs, but it could be
// greyscale
in = inRaw;
if (_rgbe) {
uint8_t* outRGBE = (uint8_t*)iteratorDstRGBE.rawptr();
encodeRGBE(in, outRGBE);
}
if (_rgbm) {
uint8_t* outRGBM = (uint8_t*)iteratorDstRGBM.rawptr();
encodeRGBM(in, outRGBM);
}
if (_luv) {
uint8_t* outLUV = (uint8_t*)iteratorDstLUV.rawptr();
encodeLUV(in, outLUV);
}
#if 0
if ( in[0] != 0 && in[1] != 0 && in[2] != 0) {
decodeLUV(outLUV, inTmp);
Vec3f inVec3 = Vec3f(in[0], in[1], in[2]);
Vec4f result = LogLuvEncode( inVec3 );
Vec3f result2 = LogLuvDecode(result);
Vec3f diff = result2 - inVec3;
std::cout << diff[0] << " " << diff[1] << " " << diff[2] << std::endl;
unsigned char test2[4];
Vec3f outVec3;
encodeLUV( &inVec3[0], test2 );
decodeLUV( test2, &outVec3[0] );
Vec3f diff2 = outVec3 - inVec3;
std::cout << diff2[0] << " " << diff2[1] << " " << diff2[2] << std::endl;
}
#endif
}
if (_rgbe) _RGBE.pack(_outputDirectory + "_rgbe.bin");
if (_luv) _LUV.pack(_outputDirectory + "_luv.bin");
if (_rgbm) _RGBM.pack(_outputDirectory + "_rgbm.bin");
}
};
static int usage(const std::string& name) {
std::cerr
<< "Usage: " << name
<< " [-e encodingFlags] [-c write by channel] level inputPattern output"
<< std::endl;
std::cerr << "eg: " << name
<< " -e luv:rgbm:rgbe:float 5 input_%d.tif /tmp/test/"
<< std::endl;
return 1;
}
int main(int argc, char** argv) {
int level = 0;
int c;
writeByChannel = false;
std::string colorencoding = "luv:rgbm:rgbe:float";
while ((c = getopt(argc, argv, "ce:")) != -1) switch (c) {
case 'e':
colorencoding = std::string(optarg);
break;
case 'c':
writeByChannel = true;
break;
default:
return usage(argv[0]);
}
std::string filePattern = argv[1];
std::string outputDir = argv[3];
if (optind < argc - 2) {
// generate specular ibl
filePattern = std::string(argv[optind]);
level = atof(argv[optind + 1]);
outputDir = std::string(argv[optind + 2]);
}
Packer packer(filePattern, level, outputDir);
if (colorencoding.find("luv") != std::string::npos) packer.setLUV(true);
if (colorencoding.find("rgbe") != std::string::npos) packer.setRGBE(true);
if (colorencoding.find("rgbm") != std::string::npos) packer.setRGBM(true);
if (colorencoding.find("float") != std::string::npos) packer.setFloat(true);
packer.pack(packer.mipmap());
}