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
/
MatlabToImf.h
353 lines (294 loc) · 9.18 KB
/
MatlabToImf.h
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
/*============================================================================
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>
============================================================================*/
#pragma once
#include "ImfToMatlab.h"
#include <half.h>
#include <ImfPixelType.h>
#include <ImfCompression.h>
#include <mex.h>
#include <string>
#include <vector>
#include <utility>
#include <cstring>
#include <cassert>
using namespace OPENEXR_IMF_INTERNAL_NAMESPACE;
// Utilities to convert from Matlab types to OpenEXR types
namespace OpenEXRforMatlab
{
///////////////////////////////////////////////////////////////////////////////
// Utilities
///////////////////////////////////////////////////////////////////////////////
inline bool isMatrix(const mxArray * pa, mwSize & outM, mwSize & outN) {
const mwSize numDim = mxGetNumberOfDimensions(pa);
if (numDim != 2) {
return false;
}
const mwSize * dim = mxGetDimensions(pa);
outM = dim[0];
outN = dim[1];
return true;
}
inline bool isVector(const mxArray * pa, mwSize & outNumel) {
mwSize M, N;
if (!isMatrix(pa, M, N)) {
return false;
}
if (M != 1 && N != 1) {
return false;
}
outNumel = mxGetNumberOfElements(pa);
assert(outNumel == M*N);
return true;
}
inline bool isScalar(const mxArray * pa) {
mwSize M, N;
if (!isMatrix(pa, M, N)) {
return false;
}
return (M == 1 && N == 1);
}
///////////////////////////////////////////////////////////////////////////////
// Conversion from Matlab to C++. Return true if the conversion is successful
///////////////////////////////////////////////////////////////////////////////
template <typename T>
inline bool toNative(const mxArray * pa, T & outValue)
{
return false;
}
template <typename T>
inline void toNativeCheck(const mxArray * pa, T & outValue)
{
if (!toNative(pa, outValue)) {
mexErrMsgIdAndTxt("OpenEXR:IllegalConversion",
"Illegal data conversion from Matlab to C++");
}
}
template <>
inline bool toNative(const mxArray * pa, std::string & outValue)
{
char * data = mxArrayToString(pa);
if (data == NULL) {
return false;
}
outValue = data;
mxFree(data);
return true;
}
template<>
inline bool toNative(const mxArray * pa, std::vector<std::string> & outVec)
{
// Be tolerant and accept a single string
if (mxIsChar(pa)) {
std::string result;
if (!toNative(pa, result)) {
return false;
}
outVec.push_back(result);
return true;
}
else if (!mxIsCell(pa)) {
return false;
}
// Handle the cell array
const mwSize numDim = mxGetNumberOfDimensions(pa);
if (numDim != 2) {
mexWarnMsgIdAndTxt("OpenEXR:IllegalConversion", "Tensors not supported.");
return false;
}
const mwSize * dim = mxGetDimensions(pa);
if (dim[0] != 1 && dim[1] != 1) {
mexWarnMsgIdAndTxt("OpenEXR:IllegalConversion", "Not a cell vector.");
return false;
}
const size_t numel = mxGetNumberOfElements(pa);
for (mwIndex i = 0; i != numel; ++i) {
std::string result;
const mxArray * element = mxGetCell(pa, i);
if (!toNative(element, result)) {
return false;
}
outVec.push_back(result);
}
return true;
}
template<>
inline bool toNative(const mxArray * pa,
std::pair<const mxArray *, mxClassID> & pair)
{
if (mxIsNumeric(pa)) {
if (mxIsComplex(pa)) {
mexWarnMsgIdAndTxt("OpenEXR:IllegalConversion",
"Complex data is not supported.");
return false;
}
pair.first = pa;
pair.second = mxGetClassID(pa);
return true;
}
else {
mexWarnMsgIdAndTxt("OpenEXR:IllegalConversion",
"Non numeric type: %s", mxGetClassName(pa));
return false;
}
}
// Converto to a pixel type from a Matlab String
template <>
inline bool toNative(const mxArray * pa, OPENEXR_IMF_INTERNAL_NAMESPACE::PixelType & outType)
{
char * data = mxArrayToString(pa);
if (data == NULL) {
return false;
}
bool result = true;
if (strcmp(data, "half") == 0) {
outType = OPENEXR_IMF_INTERNAL_NAMESPACE::HALF;
}
else if (strcmp(data, "single") == 0 || strcmp(data, "float") == 0) {
outType = OPENEXR_IMF_INTERNAL_NAMESPACE::FLOAT;
}
else {
mexWarnMsgIdAndTxt("OpenEXR:argument",
"Unrecognized pixel type: %s", data);
result = false;
}
mxFree(data);
return result;
}
template <>
inline bool toNative(const mxArray * pa, OPENEXR_IMF_INTERNAL_NAMESPACE::Compression & outCompression)
{
char * data = mxArrayToString(pa);
if (data == NULL) {
return false;
}
bool result = true;
if (strcmp(data, "none") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::NO_COMPRESSION;
}
else if (strcmp(data, "rle") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::RLE_COMPRESSION;
}
else if (strcmp(data, "zips") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::ZIPS_COMPRESSION;
}
else if (strcmp(data, "zip") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::ZIP_COMPRESSION;
}
else if (strcmp(data, "piz") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::PIZ_COMPRESSION;
}
else if (strcmp(data, "pxr24") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::PXR24_COMPRESSION;
}
else if (strcmp(data, "b44") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::B44_COMPRESSION;
}
else if (strcmp(data, "b44a") == 0) {
outCompression = OPENEXR_IMF_INTERNAL_NAMESPACE::B44A_COMPRESSION;
}
else {
mexWarnMsgIdAndTxt("OpenEXR:argument",
"Unrecognized compression: %s", data);
result = false;
}
mxFree(data);
return result;
}
///////////////////////////////////////////////////////////////////////////////
// Bulk array conversion
///////////////////////////////////////////////////////////////////////////////
namespace convert_detail
{
// Helper struct to cast types
template <typename TargetType>
struct type_traits
{
template <typename T>
static TargetType cast (const T & n)
{
return static_cast<TargetType> (n);
}
};
template <>
struct type_traits<half>
{
template <typename T>
static half cast (const T & n)
{
const float f = static_cast<float> (n);
return static_cast<half> (f);
}
};
} // namespace convert_detail
// Convert an array of the given numeric type into another preallocated array.
template <typename SourceType, typename TargetType>
inline void convertData(TargetType * dest, const mxArray * pa, const size_t len)
{
assert(mxGetClassID(pa) == mex_traits<SourceType>::classID);
const SourceType * src = static_cast<const SourceType *>(mxGetData(pa));
for (size_t i = 0; i != len; ++i) {
dest[i] = convert_detail::type_traits<TargetType>::cast (src[i]);
}
}
// Convert from a Matlab numeric array into the given data format
template <typename TargetType>
inline void convertData(TargetType * dest,
const mxArray * pa, mxClassID srcType, const size_t len)
{
assert(srcType == mxGetClassID(pa));
switch(srcType) {
case mxDOUBLE_CLASS:
convertData<real64_T>(dest, pa, len);
break;
case mxSINGLE_CLASS:
convertData<real32_T>(dest, pa, len);
break;
case mxINT8_CLASS:
convertData<int8_T>(dest, pa, len);
break;
case mxUINT8_CLASS:
convertData<uint8_T>(dest, pa, len);
break;
case mxINT16_CLASS:
convertData<int16_T>(dest, pa, len);
break;
case mxUINT16_CLASS:
convertData<uint16_T>(dest, pa, len);
break;
case mxINT32_CLASS:
convertData<int32_T>(dest, pa, len);
break;
case mxUINT32_CLASS:
convertData<uint32_T>(dest, pa, len);
break;
case mxINT64_CLASS:
convertData<int64_T>(dest, pa, len);
break;
case mxUINT64_CLASS:
convertData<uint64_T>(dest, pa, len);
break;
default:
assert("Unsupported mxClassID" == 0);
mexErrMsgIdAndTxt("OpenEXR:unsupported",
"Unsupported mxClassID: %s", mxGetClassName(pa));
}
}
// Convert the Matlab type to a known Imf::Attribute. Returns NULL if
// the conversion fails, most likely because the conversion is not
// implemented yet.
Attribute* toAttribute(const mxArray* pa);
} // namespace OpenEXRforMatlab