/
exr.cpp
94 lines (81 loc) · 3.2 KB
/
exr.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
// ======================================================================== //
// Copyright 2009-2016 Intel Corporation //
// //
// Licensed under the Apache License, Version 2.0 (the "License"); //
// you may not use this file except in compliance with the License. //
// You may obtain a copy of the License at //
// //
// http://www.apache.org/licenses/LICENSE-2.0 //
// //
// Unless required by applicable law or agreed to in writing, software //
// distributed under the License is distributed on an "AS IS" BASIS, //
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. //
// See the License for the specific language governing permissions and //
// limitations under the License. //
// ======================================================================== //
#ifdef EMBREE_TUTORIALS_OPENEXR
#include "image.h"
#if defined(__clang__)
#pragma clang diagnostic ignored "-Wdeprecated-register"
#endif
/* include OpenEXR headers */
#ifdef __WIN32__
#include <ImfRgbaFile.h>
#include <ImfArray.h>
#pragma comment (lib, "Half.lib")
#pragma comment (lib, "Iex.lib")
#pragma comment (lib, "IlmImf.lib")
#pragma comment (lib, "IlmThread.lib")
#pragma comment (lib, "Imath.lib")
#pragma comment (lib, "zlib.lib")
#else
#include <ImfRgbaFile.h>
#include <ImfArray.h>
#endif
namespace embree
{
/*! load an EXR file from disk */
Ref<Image> loadExr(const FileName& filename)
{
Imf::RgbaInputFile file (filename.c_str());
Imath::Box2i dw = file.dataWindow();
ssize_t width = dw.max.x - dw.min.x + 1;
ssize_t height = dw.max.y - dw.min.y + 1;
Imf::Array2D<Imf::Rgba> pixels(height, width);
file.setFrameBuffer (&pixels[0][0] - dw.min.x - dw.min.y * width, 1, width);
file.readPixels (dw.min.y, dw.max.y);
Ref<Image> img = new Image3f(width,height,filename);
if (file.lineOrder() == Imf::INCREASING_Y) {
for (ssize_t y=0; y<height; y++) {
for (ssize_t x=0; x<width; x++) {
Imf::Rgba c = pixels[y][x];
img->set(x,y,Color4(c.r,c.g,c.b,c.a));
}
}
}
else {
for (ssize_t y=0; y<height; y++) {
for (ssize_t x=0; x<width; x++) {
Imf::Rgba c = pixels[y][x];
img->set(x,height-y-1,Color4(c.r,c.g,c.b,c.a));
}
}
}
return img;
}
/*! store an EXR file to disk */
void storeExr(const Ref<Image>& img, const FileName& filename)
{
Imf::Array2D<Imf::Rgba> pixels(img->height,img->width);
for (size_t y=0; y<img->height; y++) {
for (size_t x=0; x<img->width; x++) {
const Color4 c = img->get(x,y);
pixels[y][x] = Imf::Rgba(c.r,c.g,c.b,c.a);
}
}
Imf::RgbaOutputFile file(filename.c_str(), img->width, img->height, Imf::WRITE_RGBA);
file.setFrameBuffer(&pixels[0][0], 1, img->width);
file.writePixels(img->height);
}
}
#endif // EMBREE_TUTORIALS_OPENEXR