This repository has been archived by the owner on Oct 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathImfToMatlab.cpp
368 lines (306 loc) · 9.9 KB
/
ImfToMatlab.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
/*============================================================================
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 <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 "ImfToMatlab.h"
#include <ImfAttribute.h>
#include <ImfBoxAttribute.h>
#include <ImfChannelListAttribute.h>
#include <ImfChromaticitiesAttribute.h>
#include <ImfCompressionAttribute.h>
#include <ImfDoubleAttribute.h>
#include <ImfEnvmapAttribute.h>
#include <ImfFloatAttribute.h>
#include <ImfIntAttribute.h>
#include <ImfKeyCodeAttribute.h>
#include <ImfLineOrderAttribute.h>
#include <ImfMatrixAttribute.h>
#include <ImfOpaqueAttribute.h>
#include <ImfPreviewImageAttribute.h>
#include <ImfRationalAttribute.h>
#include <ImfStringAttribute.h>
#include <ImfTileDescriptionAttribute.h>
#include <ImfTimeCodeAttribute.h>
#include <ImfVecAttribute.h>
#include <ImfStringVectorAttribute.h>
#include <ImfNamespace.h>
#ifdef __clang__
#pragma clang diagnostic pop
#endif
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
using namespace Imath;
namespace
{
using namespace OpenEXRforMatlab;
// Utility to know if a cast will work
template<typename T>
inline bool canCastTo (const Attribute &a) {
return dynamic_cast<const T*>(&a) != NULL;
}
// Utility to get the value of a typed attribute. It assumes
// that canCastTo<T> returns true!
template<typename T>
inline const T& getValue(const Attribute &a) {
assert(canCastTo<TypedAttribute<T> >(a));
return dynamic_cast<const TypedAttribute<T> *>(&a)->value();
}
///////////////////////////////////////////////////////////////////////////////
// Conversion from Imf enums to a string
///////////////////////////////////////////////////////////////////////////////
inline const char* getCompressionName(Compression c)
{
switch(c) {
case NO_COMPRESSION:
return "none";
case RLE_COMPRESSION:
return "rle";
case ZIPS_COMPRESSION:
return "zips";
case ZIP_COMPRESSION:
return "zip";
case PIZ_COMPRESSION:
return "piz";
case PXR24_COMPRESSION:
return "pxr24";
case B44_COMPRESSION:
return "b44";
case B44A_COMPRESSION:
return "b44a";
default:
return "unknown";
}
}
inline const char * getLineOrderName(LineOrder ord)
{
switch(ord) {
case INCREASING_Y:
return "increasing_y";
case DECREASING_Y:
return "decreasing_y";
case RANDOM_Y:
return "random";
default:
return "unknown";
}
}
inline const char * getPixelTypeName(PixelType p)
{
switch(p) {
case UINT:
return "uint32";
case HALF:
return "half";
case FLOAT:
return "single";
default:
return "unknown";
}
}
///////////////////////////////////////////////////////////////////////////////
// Conversion from C++ types to Matlab versions
///////////////////////////////////////////////////////////////////////////////
// Represent rationals as a struct with 'numerator' and 'denominator' fields
inline mxArray * fromRational(const Rational & value)
{
mxArray *n = fromScalar(value.n);
mxArray *d = fromScalar(value.d);
const char* fields[2] = {"numerator", "denominator"};
mxArray *bStruct = mxCreateStructMatrix(1, 1, 2, &fields[0]);
mxSetField(bStruct, 0, "numerator", n);
mxSetField(bStruct, 0, "denominator", d);
return bStruct;
}
template<typename T, template <typename> class VecType>
inline mxArray * fromVector(const VecType<T> & vec)
{
const int ndim = VecType<T>::dimensions();
mxArray *mArr = mxCreateNumericMatrix(1, ndim, mex_traits<T>::classID, mxREAL);
T * ptr = static_cast<T*>(mxGetData(mArr));
for (int i = 0; i != ndim; ++i) {
ptr[i] = vec[i];
}
return mArr;
}
// Represent boxes as a struct with 'min' and 'max' attributes
template<typename T, template <typename> class VecType>
inline mxArray * fromBox(const Box<VecType<T> > & box)
{
mxArray *minArr = fromVector(box.min);
mxArray *maxArr = fromVector(box.max);
const char* fields[2] = {"min", "max"};
mxArray *bStruct = mxCreateStructMatrix(1, 1, 2, &fields[0]);
mxSetField(bStruct, 0, "min", minArr);
mxSetField(bStruct, 0, "max", maxArr);
return bStruct;
}
template<int ndim, typename T, template <typename> class MatrixType>
inline mxArray * fromMatrix(const MatrixType<T> &m)
{
// Imath has only square, 3x3 or 4x4 matrices
mxArray *mArr = mxCreateNumericMatrix(ndim, ndim, mex_traits<T>::classID, mxREAL);
T * ptr = static_cast<T*>(mxGetData(mArr));
// Matlab uses column-major matrices
for (int j = 0; j < ndim; ++j) {
for (int i = 0; i < ndim; ++i) {
*ptr++ = m[i][j];
}
}
return mArr;
}
inline mxArray * fromStringVector(const StringVector & vec)
{
assert(!vec.empty());
mxArray * cells = mxCreateCellMatrix(1, vec.size());
for (size_t i = 0; i != vec.size(); ++i) {
mxSetCell(cells, i, mxCreateString(vec[i].c_str()));
}
return cells;
}
// Represent channels as structs with each member
inline mxArray * fromChannelList(const ChannelList & channelList)
{
typedef ChannelList::ConstIterator CIter;
// Find out the number of channels
int nChannels = 0;
for (CIter it = channelList.begin(); it != channelList.end(); ++it) {
++nChannels;
}
assert(nChannels > 0);
const char* fields[] =
{"name", "type", "perceptionlinear", "xsampling", "ysampling"};
const size_t nFields = sizeof(fields)/sizeof(const char *);
mxArray *cStruct = mxCreateStructMatrix(1, nChannels, nFields, &fields[0]);
int channelIdx = 0;
for (CIter it = channelList.begin(); it != channelList.end(); ++it) {
mxArray * name = mxCreateString(it.name());
mxArray * type = mxCreateString(getPixelTypeName(it.channel().type));
mxArray * pLinear = fromScalar(it.channel().pLinear);
mxArray * xs = fromScalar(it.channel().xSampling);
mxArray * ys = fromScalar(it.channel().ySampling);
mxSetField(cStruct, channelIdx, "name", name);
mxSetField(cStruct, channelIdx, "type", type);
mxSetField(cStruct, channelIdx, "perceptionlinear", pLinear);
mxSetField(cStruct, channelIdx, "xsampling", xs);
mxSetField(cStruct, channelIdx, "ysampling", ys);
++channelIdx;
}
assert(channelIdx == nChannels);
return cStruct;
}
} // namespace
mxArray* OpenEXRforMatlab::toMatlab(const OPENEXR_IMF_INTERNAL_NAMESPACE::Attribute& attr)
{
// Use the same order as Header.cpp in IlmIfm so that we can compare them
// Boxes
if (canCastTo<Box2fAttribute>(attr)) {
return fromBox(getValue<Box2f>(attr));
} else if (canCastTo<Box2iAttribute>(attr)) {
return fromBox(getValue<Box2i>(attr));
}
// Channel list
else if (canCastTo<ChannelListAttribute>(attr)) {
return fromChannelList(getValue<ChannelList>(attr));
}
// Compression enum
else if (canCastTo<CompressionAttribute>(attr)) {
const char * name = getCompressionName(getValue<Compression>(attr));
return mxCreateString(name);
}
// Chromaticities
else if (canCastTo<ChromaticitiesAttribute>(attr)) {
// TODO Support chromaticities
}
// Primitive types
else if (canCastTo<IntAttribute>(attr)) {
return fromScalar(getValue<int>(attr));
} else if (canCastTo<FloatAttribute>(attr)) {
return fromScalar(getValue<float>(attr));
} else if (canCastTo<DoubleAttribute>(attr)) {
return fromScalar(getValue<double>(attr));
}
// Environment Map
else if (canCastTo<EnvmapAttribute>(attr)) {
// TODO Support EnvmapAttribute
}
// Key code
else if (canCastTo<KeyCodeAttribute>(attr)) {
// TODO Support KeyCodeAttribute
}
// Line order enum
else if (canCastTo<LineOrderAttribute>(attr)) {
const char * name = getLineOrderName(getValue<LineOrder>(attr));
return mxCreateString(name);
}
// Matrices
else if (canCastTo<M33fAttribute>(attr)) {
return fromMatrix<3>(getValue<M33f>(attr));
} else if (canCastTo<M44fAttribute>(attr)) {
return fromMatrix<4>(getValue<M44f>(attr));
}
else if (canCastTo<M33dAttribute>(attr)) {
return fromMatrix<3>(getValue<M33d>(attr));
} else if (canCastTo<M44dAttribute>(attr)) {
return fromMatrix<4>(getValue<M44d>(attr));
}
// Preview Image
else if (canCastTo<PreviewImageAttribute>(attr)) {
// TODO Suppport PreviewImageAttribute
}
// Rational number
else if (canCastTo<RationalAttribute>(attr)) {
return fromRational(getValue<Rational>(attr));
}
// Strings
else if (canCastTo<StringAttribute>(attr)) {
const std::string & value = getValue<std::string>(attr);
return mxCreateString(value.c_str());
}
else if (canCastTo<StringVectorAttribute>(attr)) {
return fromStringVector(getValue<StringVector>(attr));
}
// Tile description
else if (canCastTo<TileDescriptionAttribute>(attr)) {
// TODO Support TileDescriptionAttribute
}
// Time code
else if (canCastTo<TimeCodeAttribute>(attr)) {
// TODO Support TimeCodeAttribute
}
// Vectors
else if (canCastTo<V2fAttribute>(attr)) {
return fromVector(getValue<V2f>(attr));
} else if (canCastTo<V2iAttribute>(attr)) {
return fromVector(getValue<V2i>(attr));
} else if (canCastTo<V3fAttribute>(attr)) {
return fromVector(getValue<V3f>(attr));
} else if (canCastTo<V3iAttribute>(attr)) {
return fromVector(getValue<V3i>(attr));
}
else if (canCastTo<V2dAttribute>(attr)) {
return fromVector(getValue<V2d>(attr));
} else if (canCastTo<V3dAttribute>(attr)) {
return fromVector(getValue<V3d>(attr));
}
return NULL;
}