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
use openexr_sys as sys;
use std::ffi::CString;
use std::path::Path;
use crate::core::{
error::Error,
frame_buffer::{Frame, FrameBuffer, FrameBufferRef},
header::HeaderRef,
};
type Result<T, E = Error> = std::result::Result<T, E>;
#[repr(transparent)]
pub struct InputFile(pub(crate) *mut sys::Imf_InputFile_t);
impl InputFile {
/// Open the file at path `filename` and read the header.
///
/// # Errors
/// * [`Error::Base`] - if the file cannot be opened
///
pub fn new<P: AsRef<Path>>(
filename: P,
num_threads: i32,
) -> Result<InputFile> {
let c_filename = CString::new(
filename
.as_ref()
.to_str()
.expect("Invalid bytes in filename"),
)
.expect("Internal null bytes in filename");
let mut ptr = std::ptr::null_mut();
unsafe {
sys::Imf_InputFile_ctor(&mut ptr, c_filename.as_ptr(), num_threads)
.into_result()?;
}
Ok(InputFile(ptr))
}
/// Access to the file [`Header`](crate::core::header::Header)
///
pub fn header(&self) -> HeaderRef {
unsafe {
let mut ptr = std::ptr::null();
sys::Imf_InputFile_header(self.0, &mut ptr);
if ptr.is_null() {
panic!("Received null ptr from sys::Imf_InputFile_header");
}
HeaderRef::new(ptr)
}
}
/// Access to the file format version
///
pub fn version(&self) -> i32 {
let mut v = 0;
unsafe {
sys::Imf_InputFile_version(self.0, &mut v);
}
v
}
/// Set the current frame buffer -- copies the FrameBuffer
/// object into the InputFile object.
///
/// The current frame buffer is the destination for the pixel
/// data read from the file. The current frame buffer must be
/// set at least once before `read_pixels()` is called.
/// The current frame buffer can be changed after each call
/// to `read_pixels()`.
///
/// # Errors
/// * [`Error::InvalidArgument`] - if the sampling factors do not match or
/// if the frame buffer does not have a sample count slice.
///
pub fn set_frame_buffer(
&mut self,
frame_buffer: &FrameBuffer,
) -> Result<()> {
unsafe {
sys::Imf_InputFile_setFrameBuffer(self.0, frame_buffer.ptr)
.into_result()?;
}
Ok(())
}
/// Access to the current frame buffer
///
pub fn frame_buffer(&self) -> FrameBufferRef {
let mut ptr = std::ptr::null();
unsafe {
sys::Imf_InputFile_frameBuffer(self.0, &mut ptr);
}
FrameBufferRef::new(ptr)
}
/// Check if all pixels in the data window are present in the input file
///
pub fn is_complete(&self) -> bool {
let mut v = false;
unsafe {
sys::Imf_InputFile_isComplete(self.0, &mut v);
}
v
}
/// Check if SSE optimization is enabled
///
/// Call after `set_frame_buffer()` to query whether optimized file decoding
/// is available - decode times will be faster if returns true
///
/// Optimization depends on:
/// * The file type (only scanline data is supported),
/// * The framebuffer channels (RGB/RGBA mono or stereo)
/// * The framebuffer channel types (all channels half-float format only)
/// * The file channels (RGB/RGBA mono or stereo)
/// * The file channel types (all channel half-float format only)
/// * Whether SSE2 instruction support was detected at compile time
///
/// # Errors
/// * [`Error::InvalidArgument`] - if no frame buffer has been set
///
pub fn is_optimization_enabled(&self) -> Result<bool> {
let mut v = false;
unsafe {
sys::Imf_InputFile_isOptimizationEnabled(self.0, &mut v)
.into_result()?;
}
Ok(v)
}
/// Read all scanlines in the range [s1, s2] and put them in the current
/// frame buffer.
///
/// `read_pixel_sample_counts()` must be called before calling this method.
///
/// # Safety
/// You must ensure the the [`FrameBuffer`] attached to this file by
/// [`set_frame_buffer()`](InputFile::set_frame_buffer) has valid slices
/// for the channels to be read.
///
/// # Errors
/// * [`Error::InvalidArgument`] - if no frame buffer has been set, if `s1`
/// or `s2` are outside the data window, or if the sample counts have not been
/// read yet
/// * [`Error::Base`] - if any other error occurs
///
pub unsafe fn read_pixels(&mut self, s1: i32, s2: i32) -> Result<()> {
sys::Imf_InputFile_readPixels(self.0, s1, s2).into_result()?;
Ok(())
}
/// Consume this `InputFile` and convert it to an [`InputFileReader`].
///
/// The [`InputFileReader`] provides a safe API for reading data from the
/// file into memory, by taking ownership of the memory and handling calculating
/// [`Slice`](crate::core::frame_buffer::Slice) offsets internally.
///
/// `frames` is a `Vec` of [`Frame`](crate::core::frame_buffer::Frame) objects, which
/// describe the channels to load from the image, and how they are to be
/// stored in memory.
///
pub fn into_reader(
mut self,
frames: Vec<Frame>,
) -> Result<InputFileReader> {
let mut frame_buffer = FrameBuffer::new();
for frame in frames {
frame_buffer.insert_frame(frame)?;
}
self.set_frame_buffer(&frame_buffer)?;
Ok(InputFileReader {
inner: self.0,
frame_buffer,
})
}
}
/// `InputFileReader` provides a safe API over `InputFile` by taking ownership
/// of the storage into which the channel data is to be read and handling all
/// the [`Slice`](crate::core::frame_buffer::Slice) pointer offset shenanigans internally.
///
pub struct InputFileReader {
inner: *mut sys::Imf_InputFile_t,
frame_buffer: FrameBuffer,
}
impl InputFileReader {
/// Read all scanlines in the range [s1, s2] and put them in the current
/// frame buffer.
///
/// This consumes the `InputFileReader` and returns the original `InputFile`
/// object and the `Vec` of the read [`Frame`](crate::core::frame_buffer::Frame)s .
///
/// # Errors
/// * [`Error::Base`] - if any error occurs
///
pub fn read_pixels(
self,
s1: i32,
s2: i32,
) -> Result<(InputFile, Vec<Frame>)> {
unsafe {
sys::Imf_InputFile_readPixels(self.inner, s1, s2).into_result()?;
}
let InputFileReader {
inner: _,
mut frame_buffer,
} = self;
Ok((InputFile(self.inner), frame_buffer.frames.take().unwrap()))
}
/// Check if SSE optimization is enabled. Decode times will be faster if
/// this returns true.
///
/// Optimization depends on:
/// * The file type (only scanline data is supported),
/// * The framebuffer channels (RGB/RGBA mono or stereo)
/// * The framebuffer channel types (all channels half-float format only)
/// * The file channels (RGB/RGBA mono or stereo)
/// * The file channel types (all channel half-float format only)
/// * Whether SSE2 instruction support was detected at compile time
///
pub fn is_optimization_enabled(&self) -> bool {
let mut v = false;
unsafe {
sys::Imf_InputFile_isOptimizationEnabled(self.inner, &mut v);
}
v
}
}
#[cfg(test)]
#[test]
fn read_input_safe1() -> Result<()> {
use crate::{core::frame_buffer::Frame, rgba::rgba::Rgba};
use std::path::PathBuf;
let path = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR not set"),
)
.join("images")
.join("window.exr");
// Open the `InputFile` and read the header
let file = InputFile::new(&path, 4).unwrap();
// Get the data window from the header so we know how much storage we need
// to allocate
let data_window: [i32; 4] = *file.header().data_window();
// Create a `Frame` to hold the channels we want to read from the file.
// `Frame` handles:
// * Allocating the required memory (or it can use existing storage with the
// `with_vec[_multi]()` constructors).
// * Creating slices with the correct offsets into the storage for the
// requested channels. Note that asking for ["R"] or ["R", "G", "B"] here
// would be a runtime error as the `Pixel` trait we've implemented for `Rgba`
// tells the constructor to expect 4 channels, but we could ask for ["R", "G", "B", "A",
// "diff.R", "diff.G", diff.B", "Z"] and it would pack those 8 channels into
// 2 `Rgba`s per pixel.
let frame_rgba =
Frame::new::<Rgba, _, _>(&["R", "G", "B", "A"], data_window)?;
// Consume the `InputFile` into an `InputFileReader` by giving it the list
// of `Frames` to read into. This internally creates the `FrameBuffer` and
// gives it to the `InputFile` rather than having those as separate operations
// which create lifetime dependencies between all the moving parts.
// `InputFileReader` can have a safe `read_pixels()` method because it owns
// all the data associated with the read operation.
// `read_pixels()` consumes `InputFileReader` and returns an `InputFile` and
// the completed `Frame`s.
let (_file, mut frames) = file
.into_reader(vec![frame_rgba])?
.read_pixels(data_window[1], data_window[3])?;
// Consume the `Frame` into a Vec (or we could just get a reference
// to the data if we wanted)
let pixels: Vec<Rgba> = frames.remove(0).into_vec();
for _rgba in pixels.iter() {
// ... do something with pixels
}
Ok(())
}
#[cfg(all(test, feature = "impl_cgmath"))]
#[test]
fn read_input_safe_cgmath1() -> Result<()> {
use crate::{Frame, Rgba};
use std::path::PathBuf;
let path = PathBuf::from(
std::env::var("CARGO_MANIFEST_DIR")
.expect("CARGO_MANIFEST_DIR not set"),
)
.join("images")
.join("window.exr");
// Use the Box2i implementation of Bound2 which we define as
// struct Box2<T> {
// min: cgmath::Vector2<T>,
// max: cgmath::Vector2<T>,
// }
// when the "impl_cgmath" feature is enabled (which transitively enables the\
// "cgmath" feature in imath-traits.
use cgmath::Vector2;
use imath_traits::impl_cgmath::Box2i;
// Open the `InputFile` and read the header
let file = InputFile::new(&path, 4).unwrap();
// Get the data window from the header so we know how much storage we need
// to allocate
let data_window: Box2i = *file.header().data_window();
// Create a `Frame` to hold the channels we want to read from the file.
// `Frame` handles:
// * Allocating the required memory (or it can use existing storage with the
// `with_vec[_multi]()` constructors).
// * Creating slices with the correct offsets into the storage for the
// requested channels. Note that asking for ["R"] or ["R", "G", "B"] here
// would be a runtime error as the `Pixel` trait we've implemented for `Rgba`
// tells the constructor to expect 4 channels.
let frame_rgba =
Frame::new::<Vector2<f32>, _, _>(&["R", "G", "B", "A"], data_window)?;
// Consume the `InputFile` into an `InputFileReader` by giving it the list
// of `Frames` to read into. This internally creates the `FrameBuffer` and
// gives it to the `InputFile` rather than having those as separate operations
// which create lifetime dependencies between all the moving parts.
// `InputFileReader` can have a safe `read_pixels()` method because it owns
// all the data associated with the read operation.
// `read_pixels()` consumes `InputFileReader` and returns an `InputFile` and
// the completed `Frame`s.
let (_file, mut frames) = file
.into_reader(vec![frame_rgba])?
.read_pixels(data_window.min.y, data_window.max.y)?;
// Consume the `Frame` into a Vec (or we could just get a reference
// to the data if we wanted)
let pixels: Vec<Rgba> = frames.remove(0).into_vec();
for _rgba in pixels.iter() {
// ... do something with pixels
}
Ok(())
}