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
//! Input file types.

use std::io::{Read, Seek};
use std::marker::PhantomData;
use std::ptr;

use libc::c_char;

use openexr_sys::*;

use error::*;
use frame_buffer::FrameBufferMut;
use stream_io::{read_stream, seek_stream};
use Header;

/// Reads any kind of OpenEXR file.
///
/// `InputFile` is a bit unique in that it doesn't care what kind of OpenEXR
/// file is being read.  Regardless of the type being read, it presents an API
/// as if it were a basic scanline OpenEXR file.
///
/// Special features like tiles, mipmaps, and deep image data will not be
/// available even if they are present in the file.  To gain access to those
/// features you need to use the other input file types (not yet implemented,
/// sorry!).
///
/// # Examples
///
/// Load image data from a floating point RGB image file named "input_file.exr".
///
/// ```no_run
/// # use openexr::{InputFile, FrameBufferMut};
/// #
/// // Open file and get its resolution.
/// let mut file = std::fs::File::open("input_file.exr").unwrap();
/// let mut input_file = InputFile::new(&mut file).unwrap();
/// let (width, height) = input_file.header().data_dimensions();
///
/// // Allocate a buffer for the image data and read it in.
/// let mut pixel_data: Vec<[f32; 4]> = vec![[0.0, 0.0, 0.0, 0.0]; (width*height) as usize];
/// let mut fb = FrameBufferMut::new(width, height);
/// fb.insert_channels(&[("R", 0.0), ("G", 0.0), ("B", 0.0), ("A", 0.0)], &mut pixel_data);
/// input_file.read_pixels(&mut fb).unwrap();
/// ```
#[allow(dead_code)]
pub struct InputFile<'a> {
    handle: *mut CEXR_InputFile,
    header_ref: Header,
    istream: *mut CEXR_IStream,
    _phantom_1: PhantomData<CEXR_InputFile>,
    _phantom_2: PhantomData<&'a mut ()>, // Represents the borrowed reader

                                         // NOTE: Because we don't know what type the reader might be, it's important
                                         // that this struct remains neither Sync nor Send.  Please don't implement
                                         // them!
}

impl<'a> InputFile<'a> {
    /// Creates a new `InputFile` from any `Read + Seek` type (typically a
    /// `std::fs::File`).
    ///
    /// Note: this seeks to byte 0 before reading.
    pub fn new<T: 'a>(reader: &mut T) -> Result<InputFile>
    where
        T: Read + Seek,
    {
        let istream_ptr = {
            let read_ptr = read_stream::<T>;
            let seekp_ptr = seek_stream::<T>;

            let mut error_out = ptr::null();
            let mut out = ptr::null_mut();
            let error = unsafe {
                CEXR_IStream_from_reader(
                    reader as *mut T as *mut _,
                    Some(read_ptr),
                    Some(seekp_ptr),
                    &mut out,
                    &mut error_out,
                )
            };

            if error != 0 {
                return Err(Error::take(error_out));
            } else {
                out
            }
        };

        let mut error_out = ptr::null();
        let mut out = ptr::null_mut();
        let error = unsafe { CEXR_InputFile_from_stream(istream_ptr, 1, &mut out, &mut error_out) };
        if error != 0 {
            Err(Error::take(error_out))
        } else {
            Ok(InputFile {
                handle: out,
                header_ref: Header {
                    // NOTE: We're casting to *mut here to satisfy the
                    // field's type, but importantly we only return a
                    // const & of the Header so it retains const semantics.
                    handle: unsafe { CEXR_InputFile_header(out) } as *mut CEXR_Header,
                    owned: false,
                    _phantom: PhantomData,
                },
                istream: istream_ptr,
                _phantom_1: PhantomData,
                _phantom_2: PhantomData,
            })
        }
    }

    /// Creates a new `InputFile` from a slice of bytes, reading from memory.
    ///
    /// Note: although you can do essentially the same thing by passing a
    /// `std::io::Cursor<&[u8]>` to `new()`, using this method is more
    /// efficient because it allows the underlying APIs to avoid reading data
    /// into intermediate buffers.
    pub fn from_slice(slice: &[u8]) -> Result<InputFile> {
        let istream_ptr = unsafe {
            CEXR_IStream_from_memory(
                b"in-memory data\0".as_ptr() as *const c_char,
                slice.as_ptr() as *mut u8 as *mut c_char,
                slice.len(),
            )
        };

        let mut error_out = ptr::null();
        let mut out = ptr::null_mut();
        let error = unsafe { CEXR_InputFile_from_stream(istream_ptr, 1, &mut out, &mut error_out) };
        if error != 0 {
            Err(Error::take(error_out))
        } else {
            Ok(InputFile {
                handle: out,
                header_ref: Header {
                    // NOTE: We're casting to *mut here to satisfy the
                    // field's type, but importantly we only return a
                    // const & of the Header so it retains const semantics.
                    handle: unsafe { CEXR_InputFile_header(out) } as *mut CEXR_Header,
                    owned: false,
                    _phantom: PhantomData,
                },
                istream: istream_ptr,
                _phantom_1: PhantomData,
                _phantom_2: PhantomData,
            })
        }
    }

    /// Reads the entire image into `framebuffer` at once.
    ///
    /// Any channels in `framebuffer` that are not present in the file will be
    /// filled with their default fill value.
    ///
    /// # Errors
    ///
    /// This function expects `framebuffer` to have the same resolution as the
    /// file, and for any same-named channels to have matching types and
    /// subsampling.
    ///
    /// It will also return an error if there is an I/O error.
    pub fn read_pixels(&mut self, framebuffer: &mut FrameBufferMut) -> Result<()> {
        // ^^^ NOTE: it's not obvious, but this does indeed need to take self as
        // &mut to be safe.  Even though it is not conceptually modifying the
        // thing (typically a file) that it's reading from, it still has a
        // cursor getting incremented etc. during reads, so the reference needs
        // to be unique to avoid unsafe aliasing.

        // Validation
        if self.header().data_dimensions() != framebuffer.dimensions() {
            return Err(Error::Generic(format!(
                "framebuffer size {}x{} does not match \
                 image dimensions {}x{}",
                framebuffer.dimensions().0,
                framebuffer.dimensions().1,
                self.header().data_dimensions().0,
                self.header().data_dimensions().1
            )));
        }

        // Validation
        if self.header().data_origin() != framebuffer.origin() {
            return Err(Error::Generic(format!(
                "framebuffer origin {},{} does not match image origin {},{}",
                framebuffer.origin().0,
                framebuffer.origin().1,
                self.header().data_origin().0,
                self.header().data_origin().1
            )));
        }

        self.header().validate_framebuffer_for_input(framebuffer)?;

        // Set up the framebuffer with the image
        let mut error_out = ptr::null();

        let error = unsafe {
            CEXR_InputFile_set_framebuffer(self.handle, framebuffer.handle_mut(), &mut error_out)
        };
        if error != 0 {
            return Err(Error::take(error_out));
        }

        // Read the image data
        let error = unsafe {
            CEXR_InputFile_read_pixels(
                self.handle,
                self.header().data_window().min.y,
                self.header().data_window().max.y,
                &mut error_out,
            )
        };
        if error != 0 {
            Err(Error::take(error_out))
        } else {
            Ok(())
        }
    }

    /// Reads a contiguous chunk of scanlines into `framebuffer`.
    ///
    /// `framebuffer` may have a different vertical resolution than the image,
    /// but must have the same horizontal resolution.  Scanlines are read from
    /// the image starting at `starting_scanline` and are written to
    /// `framebuffer` until it's filled.
    ///
    /// For example, to read the last 50 scanlines of a 200-pixel-tall image,
    /// you would pass a 50-pixel-tall FrameBufferMut and a starting scanline of
    /// 150.
    ///
    /// Any channels in `framebuffer` that are not present in the file will be
    /// filled with their default fill value.
    ///
    /// # Errors
    ///
    /// This function expects `framebuffer` to have the same _horizontal_
    /// resolution as the file, and for any same-named channels to have
    /// matching types and subsampling.
    ///
    /// It will also return an error if:
    ///
    /// * There aren't enough scanlines starting at `starting_scanline` to fill
    ///   `framebuffer`.
    ///   from `starting_scanline`.
    /// * There is an I/O error.
    pub fn read_pixels_partial(
        &mut self,
        starting_scanline: u32,
        framebuffer: &mut FrameBufferMut,
    ) -> Result<()> {
        // ^^^ NOTE: it's not obvious, but this does indeed need to take self as
        // &mut to be safe.  Even though it is not conceptually modifying the
        // thing (typically a file) that it's reading from, it still has a
        // cursor getting incremented etc. during reads, so the reference needs
        // to be unique to avoid unsafe aliasing.

        // Validation
        assert!(
            starting_scanline < self.header().data_dimensions().1,
            "Cannot start reading past last scanline."
        );

        if framebuffer.dimensions().1 > (self.header().data_dimensions().1 - starting_scanline) {
            return Err(Error::Generic(format!(
                "framebuffer contains {} \
                 scanlines, but only {} scanlines are available to read from \
                 the given starting scanline",
                framebuffer.dimensions().1,
                self.header().data_dimensions().1 - starting_scanline
            )));
        }

        if self.header().data_dimensions().0 != framebuffer.dimensions().0 {
            return Err(Error::Generic(format!(
                "framebuffer width {} does not match\
                 image width {}",
                framebuffer.dimensions().0,
                self.header().data_dimensions().0
            )));
        }

        self.header().validate_framebuffer_for_input(framebuffer)?;

        // Set up the framebuffer with the image
        let start_scanline = self.header().data_window().min.y + starting_scanline as i32;
        let end_scanline = self.header().data_window().min.y
            + (starting_scanline + framebuffer.dimensions().1) as i32
            - 1;

        let mut error_out = ptr::null();

        let error = unsafe {
            let offset_fb = CEXR_FrameBuffer_copy_and_offset_scanlines(
                framebuffer.handle_mut(),
                starting_scanline,
            );
            let err = CEXR_InputFile_set_framebuffer(self.handle, offset_fb, &mut error_out);
            CEXR_FrameBuffer_delete(offset_fb);
            err
        };
        if error != 0 {
            return Err(Error::take(error_out));
        }

        // Read the image data
        let error = unsafe {
            CEXR_InputFile_read_pixels(self.handle, start_scanline, end_scanline, &mut error_out)
        };
        if error != 0 {
            Err(Error::take(error_out))
        } else {
            Ok(())
        }
    }

    /// Access to the file's header.
    pub fn header(&self) -> &Header {
        &self.header_ref
    }
}

impl<'a> Drop for InputFile<'a> {
    fn drop(&mut self) {
        unsafe { CEXR_InputFile_delete(self.handle) };
        unsafe { CEXR_IStream_delete(self.istream) };
    }
}