-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexrinfo.cpp
executable file
·191 lines (148 loc) · 5.56 KB
/
exrinfo.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
/*============================================================================
OpenEXR for Matlab
Distributed under the MIT License (the "License");
see accompanying file LICENSE for details
or copy at http://opensource.org/licenses/MIT
Originated from HDRITools - High Dynamic Range Image Tools
Copyright 2011 Program of Computer Graphics, Cornell University
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
-----------------------------------------------------------------------------
Authors:
Jinwei Gu <jwgu AT cs DOT cornell DOT edu>
Edgar Velazquez-Armendariz <eva5 AT cs DOT cornell DOT edu>
Manuel Leonhardt <leom AT hs-furtwangen DOT de>
============================================================================*/
#include <string>
#include <vector>
#include <cassert>
#include <mex.h>
#include <matrix.h>
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wlong-long"
#pragma clang diagnostic ignored "-Wdeprecated-register"
#pragma clang diagnostic ignored "-Wextra"
#endif
#include <ImfHeader.h>
#include <ImfChannelList.h>
#include <ImfAttribute.h>
#include <ImfInputFile.h>
#include <ImfNamespace.h>
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#include "ImfToMatlab.h"
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
using namespace Imath;
using namespace OpenEXRforMatlab;
namespace {
// Temporaty struct to hold a name and a matlab value
struct Pair
{
const char * name;
mxArray * value;
Pair() : name(NULL), value(NULL) {}
Pair(const char * pName, mxArray * pValue = NULL) :
name(pName), value(pValue)
{}
bool isValid() const {
return name != NULL && value != NULL;
}
};
// Create a cell array with only the name of the channels
mxArray * getChannelNames(const ChannelList & channels)
{
typedef ChannelList::ConstIterator ChannelIterator;
std::vector<mxArray *> channelNames;
for (ChannelIterator it = channels.begin(); it != channels.end(); ++it)
{
mxArray * mStr = mxCreateString(it.name());
channelNames.push_back(mStr);
}
assert(!channelNames.empty());
mxArray * cells = mxCreateCellMatrix(1, channelNames.size());
for (size_t i = 0; i != channelNames.size(); ++i) {
mxSetCell(cells, i, channelNames[i]);
}
return cells;
}
// Create and populate a containters.Map object with the attributes
mxArray * getAttributesMap(const Header& header)
{
std::vector<Pair> attributes;
// Get the full set of attributes
for (Header::ConstIterator it = header.begin(); it != header.end(); ++it) {
const Attribute& attr = it.attribute();
if (!Attribute::knownType(attr.typeName())) {
continue;
}
Pair pair(it.name());
pair.value = toMatlab(attr);
if (pair.isValid()) {
attributes.push_back(pair);
}
}
assert(!attributes.empty());
// Create cell arrays with the attributes' names and values
mxArray * nameCell = mxCreateCellMatrix(1, attributes.size());
mxArray * valueCell = mxCreateCellMatrix(1, attributes.size());
for (size_t i = 0; i != attributes.size(); ++i) {
mxSetCell(nameCell, i, mxCreateString(attributes[i].name));
mxSetCell(valueCell, i, attributes[i].value);
}
// Create the attributes map
mxArray* mapHandle = NULL;
mxArray* mapArgs[2] = {nameCell, valueCell};
if (mexCallMATLAB(1, &mapHandle, 2, &mapArgs[0], "containers.Map") != 0) {
mexErrMsgIdAndTxt("OpenEXR:exception",
"Could not create the attribute map.");
return NULL;
}
return mapHandle;
}
} // namespace
void mexFunction(int nlhs, mxArray* plhs[], int nrhs, const mxArray* prhs[])
{
/* Check for proper number of arguments */
if (nrhs != 1) {
mexErrMsgIdAndTxt("OpenEXR:argument", "The filename is required.");
} else if (nlhs > 1) {
mexErrMsgIdAndTxt("OpenEXR:argument", "Too many output arguments.");
}
char *inputfilePtr = mxArrayToString(prhs[0]);
if (inputfilePtr == NULL) {
mexErrMsgIdAndTxt("OpenEXR:argument", "Invalid filename argument.");
}
// Copy to a string so that the matlab memory may be freed asap
const std::string inputfile(inputfilePtr);
mxFree(inputfilePtr); inputfilePtr = static_cast<char*>(0);
try {
// Open a file, but only read the header
InputFile image(inputfile.c_str());
const Header& header = image.header();
const Box2i& dw = header.dataWindow();
const int width = dw.max.x - dw.min.x + 1;
const int height = dw.max.y - dw.min.y + 1;
// List of channel names
mxArray* channelNames = getChannelNames(header.channels());
// Attributes map
mxArray* attributesMap = getAttributesMap(header);
// Size in matlab style (rows by columns)
const int dataSize[] = {height, width};
mxArray* size = fromArray(dataSize);
// Build the structure
const char* fields[] = {"channels", "size", "attributes"};
mxArray* bStruct = mxCreateStructMatrix(1, 1,
sizeof(fields)/sizeof(const char*), &fields[0]);
mxSetField(bStruct, 0, "channels", channelNames);
mxSetField(bStruct, 0, "size", size);
mxSetField(bStruct, 0, "attributes", attributesMap);
// Assign the result
plhs[0] = bStruct;
}
catch( std::exception& e ) {
mexErrMsgIdAndTxt("OpenEXR:exception", e.what());
}
}