-
Notifications
You must be signed in to change notification settings - Fork 695
Expand file tree
/
Copy pathPyOpenEXR.h
More file actions
325 lines (270 loc) · 10.2 KB
/
Copy pathPyOpenEXR.h
File metadata and controls
325 lines (270 loc) · 10.2 KB
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
//
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) Contributors to the OpenEXR Project.
//
typedef Array2D<void*> Array2DVoidPtr;
typedef std::map<std::string,std::unique_ptr<Array2DVoidPtr>> SliceDataMap;
//
// PyFile is the object that corresponds to an exr file, either for reading
// or writing, consisting of a simple list of parts.
//
class PyPart;
class PyChannel;
class PyFile
{
public:
PyFile();
PyFile(const std::string& filename, bool separate_channels = false, bool header_only = false, const py::list& part_indices = py::list());
PyFile(const py::dict& header, const py::dict& channels);
PyFile(const py::list& parts);
py::object __enter__();
void __exit__(py::args args);
py::dict& header(int part_index = 0);
py::dict& channels(int part_index = 0);
void write(const char* filename);
std::string filename;
py::list parts;
protected:
bool _header_only;
std::unique_ptr<MultiPartInputFile> _inputFile;
py::object getAttributeObject(const std::string& name, const Attribute* a);
void insertAttribute(Header& header,
const std::string& name,
const py::object& object);
};
//
// PyPart holds the information for a part of an exr file: name, type,
// dimension, compression, the list of attributes (e.g. "header") and the
// list of channels.
//
class PyPart
{
public:
PyPart() {}
PyPart(const py::dict& header, const py::dict& channels, const std::string& name);
std::string name() const;
V2i shape() const;
size_t width() const;
size_t height() const;
Compression compression() const;
exr_storage_t type() const;
std::string typeString() const;
py::dict header;
py::dict channels;
size_t part_index;
void writePixels(MultiPartOutputFile& outfile, const Box2i& dw) const;
void writeDeepPixels(MultiPartOutputFile& outfile, const Box2i& dw) const;
void setDeepSliceData(const ChannelList& channel_list, size_t height, size_t width,
SliceDataMap& sliceDataMap,
std::map<std::string,PyChannel*>& rgbaChannelMap,
const Array2D<unsigned int>& sampleCount);
void readPixels(MultiPartInputFile& infile, const ChannelList& channel_list,
const std::vector<size_t>& shape, const std::set<std::string>& rgbaChannels,
const Box2i& dw, bool separate_channels);
void readDeepPixels(MultiPartInputFile& infile, const std::string& type, const ChannelList& channel_list,
const std::vector<size_t>& shape, const std::set<std::string>& rgbaChannels,
const Box2i& dw, bool separate_channels);
int channelNameToRGBA(const ChannelList& channel_list, const std::string& name,
std::string& py_channel_name, char& channel_name);
};
//
// PyChannel holds information for a channel of a PyPart: name, type, x/y
// sampling, and the array of pixel data.
//
class PyChannel
{
public:
PyChannel()
: xSampling(1), ySampling(1), pLinear(false), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) {}
PyChannel(int xSampling, int ySampling, bool pLinear = false)
: xSampling(xSampling), ySampling(ySampling), pLinear(pLinear), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) {}
PyChannel(const py::array& p)
: xSampling(1), ySampling(1), pLinear(false), pixels(p), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) { validatePixelArray(); }
PyChannel(const py::array& p, int xSampling, int ySampling, bool pLinear = false)
: xSampling(xSampling), ySampling(ySampling), pLinear(pLinear), pixels(p), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) { validatePixelArray(); }
PyChannel(const char* n)
: name(n), xSampling(1), ySampling(1), pLinear(false), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) {}
PyChannel(const char* n, int xSampling, int ySampling, bool pLinear = false)
: name(n), xSampling(xSampling), ySampling(ySampling), pLinear(pLinear), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) {}
PyChannel(const char* n, const py::array& p)
: name(n), xSampling(1), ySampling(1), pLinear(false), pixels(p), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) { validatePixelArray(); }
PyChannel(const char* n, const py::array& p, int xSampling, int ySampling, bool pLinear = false)
: name(n), xSampling(xSampling), ySampling(ySampling), pLinear(pLinear), pixels(p), channel_index(0),
_type(NUM_PIXELTYPES), _nrgba(0) { validatePixelArray(); }
PixelType pixelType() const;
std::string name;
int xSampling;
int ySampling;
int pLinear;
py::array pixels;
size_t channel_index;
mutable PixelType _type;
mutable int _nrgba;
void validatePixelArray();
template<class T> void setSliceDataPtr(Array2DVoidPtr& slice_data,const py::array& a,
size_t y, size_t x,
int channel_offset, PixelType type) const;
void createDeepPixelArrays(size_t height, size_t width,
const Array2D<unsigned int>& sampleCount);
void insertDeepSlice(DeepFrameBuffer& frameBuffer, const std::string& slice_name,
size_t height, size_t width, int nrgba,
int dw_offset, int channel_offset,
Array2D<unsigned int>& sampleCount,
std::vector<std::shared_ptr<Array2DVoidPtr>>& slice_datas) const;
};
class PyPreviewImage
{
public:
static constexpr uint32_t style = py::array::c_style | py::array::forcecast;
static constexpr size_t stride = sizeof(PreviewRgba);
PyPreviewImage() {}
PyPreviewImage(unsigned int width, unsigned int height,
const PreviewRgba* data = nullptr)
: pixels(py::array_t<PreviewRgba,style>(std::vector<size_t>({height, width}),
std::vector<size_t>({stride*width, stride}),
data)) {}
PyPreviewImage(const py::array_t<PreviewRgba>& p) : pixels(p) {}
inline bool operator==(const PyPreviewImage& other) const;
py::array_t<PreviewRgba> pixels;
};
inline std::ostream&
operator<< (std::ostream& s, const PreviewRgba& p)
{
s << " (" << int(p.r)
<< "," << int(p.g)
<< "," << int(p.b)
<< "," << int(p.a)
<< ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const PyPreviewImage& P)
{
auto width = P.pixels.shape(1);
auto height = P.pixels.shape(0);
s << "PreviewImage(" << width
<< ", " << height;
#if PRINT_PIXELS
s << "," << std::endl;
py::buffer_info buf = P.pixels.request();
const PreviewRgba* rgba = static_cast<PreviewRgba*>(buf.ptr);
for (decltype(height) y = 0; y<height; y++)
{
for (decltype(width) x = 0; x<width; x++)
s << rgba[y*width+x];
s << std::endl;
}
#endif
s << ")";
return s;
}
inline bool
PyPreviewImage::operator==(const PyPreviewImage& other) const
{
py::buffer_info buf = pixels.request();
py::buffer_info obuf = other.pixels.request();
const PreviewRgba* apixels = static_cast<PreviewRgba*>(buf.ptr);
const PreviewRgba* bpixels = static_cast<PreviewRgba*>(obuf.ptr);
for (decltype(buf.size) i = 0; i < buf.size; i++)
if (!(apixels[i] == bpixels[i]))
return false;
return true;
}
inline std::ostream&
operator<< (std::ostream& s, const Chromaticities& c)
{
s << "(" << c.red
<< ", " << c.green
<< ", " << c.blue
<< ", " << c.white
<< ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const Rational& v)
{
s << v.n << "/" << v.d;
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const KeyCode& v)
{
s << "(" << v.filmMfcCode()
<< ", " << v.filmType()
<< ", " << v.prefix()
<< ", " << v.count()
<< ", " << v.perfOffset()
<< ", " << v.perfsPerFrame()
<< ", " << v.perfsPerCount()
<< ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const TimeCode& v)
{
s << "(" << v.hours()
<< ", " << v.minutes()
<< ", " << v.seconds()
<< ", " << v.frame()
<< ", " << v.dropFrame()
<< ", " << v.colorFrame()
<< ", " << v.fieldPhase()
<< ", " << v.bgf0()
<< ", " << v.bgf1()
<< ", " << v.bgf2()
<< ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const TileDescription& v)
{
s << "TileDescription(" << v.xSize
<< ", " << v.ySize
<< ", " << py::cast(v.mode)
<< ", " << py::cast(v.roundingMode)
<< ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const Box2i& v)
{
s << "(" << v.min << " " << v.max << ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const Box2f& v)
{
s << "(" << v.min << " " << v.max << ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const PyChannel& C)
{
s << "Channel(\"" << C.name
<< "\", xSampling=" << C.xSampling
<< ", ySampling=" << C.ySampling;
if (C.pLinear)
s << ", pLinear=True";
s << ")";
return s;
}
inline std::ostream&
operator<< (std::ostream& s, const PyPart& P)
{
auto name = P.name();
s << "Part(";
if (name != "")
s << "\"" << name << "\"";
s << ", " << py::cast(P.compression())
<< ", width=" << P.width()
<< ", height=" << P.height()
<< ")";
return s;
}