oiio 0.3.2

High-level OpenImageIO bindings over oiio-sys
Documentation
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
use crate::{sys, Error, ImageSpec, PixelFormat, Result};

/// One channel of a [`DeepImage`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DeepChannel {
    name: String,
    format: PixelFormat,
}

impl DeepChannel {
    /// The channel's name, such as `"R"`, `"A"` or `"Z"`.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// The type its samples are stored as.
    pub fn format(&self) -> PixelFormat {
        self.format
    }
}

/// A deep image: every pixel holds a list of samples rather than one value.
///
/// Deep images are how a renderer records what is behind what — each sample
/// carries its own depth alongside its colour, so a pixel covered by three
/// overlapping objects keeps all three. The contiguous
/// [`Pixel`](crate::Pixel) API cannot express that and refuses deep files;
/// this is how they are read instead.
///
/// Samples are addressed by pixel coordinate, then channel, then sample
/// index. Every accessor is bounds-checked, so an out-of-range coordinate is
/// an error rather than a read of whatever was next in memory.
///
/// ```no_run
/// use oiio::ImageInput;
/// use std::path::Path;
///
/// # fn main() -> oiio::Result<()> {
/// let mut input = ImageInput::from_path(Path::new("deep.exr"))?;
/// let deep = input.read_deep_image()?;
///
/// let depth = deep.z_channel().expect("a deep image usually has Z");
/// for (x, y) in [(0, 0), (1, 0)] {
///     for sample in 0..deep.sample_count(x, y)? {
///         println!("({x}, {y}) sample {sample} at z {}", deep.value(x, y, depth, sample)?);
///     }
/// }
/// # Ok(())
/// # }
/// ```
pub struct DeepImage {
    inner: cxx::UniquePtr<sys::deepdata::DeepData>,
    origin: [i32; 3],
    dimensions: [u32; 3],
    channels: Vec<DeepChannel>,
}

// The simplest of the crate's `Send` claims, and checked rather than assumed.
// `DeepData::Impl` is plain owned storage: the per-pixel sample counts and
// offsets, one flat data block, and the channel types and names. No pool, no
// arena, no allocator with thread affinity, and no `thread_local` in
// `deepdata.cpp`. The destructor drops those vectors and nothing else, so
// running it on a different thread than the constructor changes nothing. The
// channel names are `ustring`s, whose global table is guarded by a
// `spin_rw_mutex` (`ustring.cpp:37`, read locks on lookup, write lock on
// insert), so sharing those across threads is already synchronised upstream.
// The Rust-side fields beside the pointer are two arrays of integers and a
// `Vec<DeepChannel>` of plain data, all `Send` on their own.
//
// `Sync` is deliberately absent. Several `const` methods are not read-only:
// `all_data` and `get_pointers` (`deepdata.cpp:841-846`, `:851-856`) allocate
// and fill cached state behind a `const` signature, so two threads holding
// `&DeepImage` could race them. Anything that needs one deep image on several
// threads wants `Arc<Mutex<_>>`, or a clone per thread.
unsafe impl Send for DeepImage {}

impl DeepImage {
    /// Build an empty deep image matching a specification.
    ///
    /// The specification must be marked deep, and describes the size and the
    /// channels. Every pixel starts with no samples; give them some with
    /// [`DeepImage::set_sample_count`], then fill them in with
    /// [`DeepImage::set_value`].
    ///
    /// ```no_run
    /// use oiio::{DeepImage, ImageOutput, ImageSpec, PixelFormat};
    /// use std::path::Path;
    ///
    /// # fn main() -> oiio::Result<()> {
    /// let spec = ImageSpec::new(64, 64, 5, PixelFormat::F32)?
    ///     .with_channel_names(["R", "G", "B", "A", "Z"])?
    ///     .as_deep();
    /// let mut deep = DeepImage::new(&spec)?;
    ///
    /// // One sample at the origin, at depth 10.
    /// deep.set_sample_count(0, 0, 1)?;
    /// deep.set_value(0, 0, 4, 0, 10.0)?;
    ///
    /// let mut output = ImageOutput::create(Path::new("deep.exr"), &spec)?;
    /// output.write_deep_image(&deep)?;
    /// output.close()?;
    /// # Ok(())
    /// # }
    /// ```
    pub fn new(spec: &ImageSpec) -> Result<Self> {
        if !spec.is_deep() {
            return Err(Error::InvalidImageSpec(
                "a deep image needs a specification marked deep; see ImageSpec::as_deep".to_owned(),
            ));
        }

        // DeepData::init indexes its per-pixel sample vectors with an int, so a
        // pixel count past i32::MAX truncates to a negative one and the resize
        // below throws out of a shim cxx wraps noexcept -- std::terminate. This
        // is the same cap ImageBuf::new applies to a deep spec; DeepImage was
        // the other half of it.
        let pixels = spec.pixel_count()?;
        if pixels > i32::MAX as usize {
            return Err(Error::InvalidImageSpec(format!(
                "a deep image is limited to {} pixels, and this spec has {pixels}",
                i32::MAX
            )));
        }

        let native_spec = spec.to_sys()?;
        let Some(native_spec) = native_spec.as_ref() else {
            return Err(Error::InvalidImageSpec(
                "OpenImageIO could not allocate an image specification".to_owned(),
            ));
        };

        let mut inner = sys::deepdata::deepdata_default();
        let Some(pinned) = inner.as_mut() else {
            return Err(Error::operation(
                "create deep image",
                "OpenImageIO could not allocate deep data".to_owned(),
            ));
        };
        let mut error = String::new();
        if !sys::deepdata::deepdata_init_from_spec(pinned, native_spec, &mut error) {
            return Err(Error::operation("create deep image", error));
        }

        Self::from_parts(inner, spec)
    }

    /// Give a pixel a number of samples.
    ///
    /// A pixel's first samples start zeroed. Shrinking a pixel drops the
    /// samples past the new end but keeps the room they occupied, so growing
    /// it again within that room brings their old values back rather than
    /// zeroes — OpenImageIO leaves holes for speed. Write every sample you
    /// intend to read, as [`ImageBuf::set_deep_sample_count`](crate::ImageBuf)
    /// documents for the same underlying storage.
    ///
    /// The storage all the counts describe is allocated on the first value
    /// written, so a total too large for the machine is reported there — or
    /// here, once the image's samples are already allocated and this call has
    /// to grow them.
    pub fn set_sample_count(&mut self, x: i32, y: i32, count: u32) -> Result<()> {
        let pixel = self.pixel_index(x, y)?;
        let count = i32::try_from(count)
            .map_err(|_| Error::InvalidRoi("sample count exceeds i32::MAX".to_owned()))?;
        let mut error = String::new();
        if !sys::deepdata::deepdata_set_samples(self.inner_mut(), pixel, count, &mut error) {
            return Err(Error::operation("set deep sample count", error));
        }
        Ok(())
    }

    /// Set one sample's value.
    ///
    /// The first write allocates the storage every pixel's sample count
    /// describes, so this is where counts too large for the machine are
    /// reported.
    pub fn set_value(
        &mut self,
        x: i32,
        y: i32,
        channel: u32,
        sample: u32,
        value: f32,
    ) -> Result<()> {
        let (pixel, channel, sample) = self.address(x, y, channel, sample)?;
        let mut error = String::new();
        if !sys::deepdata::deepdata_set_deep_value(
            self.inner_mut(),
            pixel,
            channel,
            sample,
            value,
            &mut error,
        ) {
            return Err(Error::operation("set deep value", error));
        }
        Ok(())
    }

    /// Set one sample's value in a channel stored as an unsigned integer.
    pub fn set_value_uint(
        &mut self,
        x: i32,
        y: i32,
        channel: u32,
        sample: u32,
        value: u32,
    ) -> Result<()> {
        let (pixel, channel, sample) = self.address(x, y, channel, sample)?;
        let mut error = String::new();
        if !sys::deepdata::deepdata_set_deep_value_uint(
            self.inner_mut(),
            pixel,
            channel,
            sample,
            value,
            &mut error,
        ) {
            return Err(Error::operation("set deep value", error));
        }
        Ok(())
    }

    /// Sort one pixel's samples by depth, front to back.
    ///
    /// Needs a `Z` channel, and refuses a pixel whose samples exceed
    /// OpenImageIO's per-call stack scratch — the sort copies the whole pixel
    /// through `alloca`, which no error path can survive.
    pub fn sort_samples(&mut self, x: i32, y: i32) -> Result<()> {
        let pixel = self.pixel_index(x, y)?;
        self.require_z("sort deep samples")?;
        self.require_stack_scratch("sort deep samples", self.samples_at(pixel))?;
        sys::deepdata::deepdata_sort(self.inner_mut(), pixel);
        Ok(())
    }

    /// Merge one pixel's exactly-overlapping samples, per the OpenEXR rules.
    ///
    /// Needs a `Z` channel; OpenImageIO would otherwise return silently
    /// having merged nothing.
    pub fn merge_overlap_samples(&mut self, x: i32, y: i32) -> Result<()> {
        let pixel = self.pixel_index(x, y)?;
        self.require_z("merge overlapping samples")?;
        sys::deepdata::deepdata_merge_overlaps(self.inner_mut(), pixel);
        Ok(())
    }

    /// Merge another image's pixel into one of this image's, splitting and
    /// combining samples by depth.
    ///
    /// The two images must share a channel layout, both need their `Z`
    /// channels, and the combined sample count must fit the same stack
    /// scratch [`DeepImage::sort_samples`] documents, since the merge sorts
    /// the combined pixel.
    pub fn merge_pixel_from(
        &mut self,
        x: i32,
        y: i32,
        src: &DeepImage,
        src_x: i32,
        src_y: i32,
    ) -> Result<()> {
        let pixel = self.pixel_index(x, y)?;
        let src_pixel = src.pixel_index(src_x, src_y)?;
        self.require_z("merge deep pixels")?;
        if src.z_channel().is_none() {
            return Err(Error::operation(
                "merge deep pixels",
                "the source has no Z channel".to_owned(),
            ));
        }
        if !sys::deepdata::deepdata_same_channeltypes(self.inner(), src.inner()) {
            return Err(Error::operation(
                "merge deep pixels",
                "the images disagree on channel types; merging reads the source's \
                 samples with this image's layout"
                    .to_owned(),
            ));
        }
        // The merge concatenates both pixels, splits every sample at every
        // other sample's boundaries, and then sorts the result — so
        // mutually overlapping samples grow quadratically, and the final
        // sort's stack scratch must be bounded by that worst case, not by a
        // linear headroom.
        let before = self
            .samples_at(pixel)
            .saturating_add(src.samples_at(src_pixel));
        let combined = before
            .saturating_mul(before)
            .saturating_mul(2)
            .saturating_add(before);
        self.require_stack_scratch("merge deep pixels", combined)?;
        // The 3.1 series takes the source pixel as an int (widened to
        // int64 upstream only after 3.1); the crate's pixel cap keeps every
        // index within it.
        let src_pixel = i32::try_from(src_pixel)
            .map_err(|_| Error::InvalidRoi("source pixel index exceeds i32::MAX".to_owned()))?;
        sys::deepdata::deepdata_merge_deep_pixels(self.inner_mut(), pixel, src.inner(), src_pixel);
        Ok(())
    }

    /// The depth at which one pixel becomes fully opaque, or `None` when it
    /// never does — no samples, no depth channel, or alpha never reaching
    /// one. OpenImageIO spells that absence `f32::MAX`.
    pub fn opaque_depth(&self, x: i32, y: i32) -> Result<Option<f32>> {
        let pixel = self.pixel_index(x, y)?;
        let depth = sys::deepdata::deepdata_opaque_z(self.inner(), pixel);
        Ok((depth != f32::MAX).then_some(depth))
    }

    /// Drop every sample behind the first fully opaque one in a pixel.
    ///
    /// Needs an alpha channel; OpenImageIO would otherwise return silently
    /// having culled nothing.
    pub fn occlusion_cull_samples(&mut self, x: i32, y: i32) -> Result<()> {
        let pixel = self.pixel_index(x, y)?;
        if sys::deepdata::deepdata_a_channel(self.inner()) < 0 {
            return Err(Error::operation(
                "occlusion cull",
                "the image has no alpha channel to test opacity with".to_owned(),
            ));
        }
        sys::deepdata::deepdata_occlusion_cull(self.inner_mut(), pixel);
        Ok(())
    }

    /// Split every sample of one pixel that spans `depth` into two, with the
    /// colour redistributed per the OpenEXR deep-merging rules. Returns
    /// whether anything was split; an image without both `Z` and `Zback`
    /// channels has no sample extents to split, and reports `false`.
    pub fn split_samples(&mut self, x: i32, y: i32, depth: f32) -> Result<bool> {
        let pixel = self.pixel_index(x, y)?;
        Ok(sys::deepdata::deepdata_split(
            self.inner_mut(),
            pixel,
            depth,
        ))
    }

    fn samples_at(&self, pixel: i64) -> u64 {
        sys::deepdata::deepdata_samples(self.inner(), pixel).max(0) as u64
    }

    fn require_z(&self, operation: &'static str) -> Result<()> {
        if self.z_channel().is_none() {
            return Err(Error::operation(
                operation,
                "the image has no Z channel".to_owned(),
            ));
        }
        Ok(())
    }

    /// The per-pixel operations copy the pixel through OpenImageIO's stack
    /// frame with `alloca`, sized by samples times sample bytes; past the
    /// stack there is no error, only a crash, so the bound lives here.
    fn require_stack_scratch(&self, operation: &'static str, samples: u64) -> Result<()> {
        let sample_bytes = sys::deepdata::deepdata_samplesize(self.inner()).max(1) as u64;
        let bytes = samples.saturating_mul(sample_bytes.saturating_add(4));
        const SCRATCH_LIMIT: u64 = 256 * 1024;
        if bytes > SCRATCH_LIMIT {
            return Err(Error::operation(
                operation,
                format!(
                    "this pixel's samples need {bytes} bytes of per-call stack \
                     scratch, over the {SCRATCH_LIMIT}-byte bound"
                ),
            ));
        }
        Ok(())
    }

    pub(crate) fn native(&self) -> &sys::deepdata::DeepData {
        self.inner()
    }

    fn inner_mut(&mut self) -> std::pin::Pin<&mut sys::deepdata::DeepData> {
        self.inner
            .as_mut()
            .expect("DeepImage invariant violated: null native pointer")
    }

    pub(crate) fn from_parts(
        inner: cxx::UniquePtr<sys::deepdata::DeepData>,
        spec: &ImageSpec,
    ) -> Result<Self> {
        if inner.is_null() {
            return Err(Error::operation(
                "read deep image",
                "OpenImageIO returned no deep data".to_owned(),
            ));
        }

        let channel_count = sys::deepdata::deepdata_channels(inner.as_ref().expect("non-null"));
        let mut channels = Vec::with_capacity(channel_count.max(0) as usize);
        for index in 0..channel_count {
            let native = inner.as_ref().expect("non-null");
            channels.push(DeepChannel {
                name: sys::deepdata::deepdata_channelname(native, index),
                format: PixelFormat::from_sys(&sys::deepdata::deepdata_channeltype(native, index)),
            });
        }

        Ok(Self {
            inner,
            origin: spec.origin(),
            dimensions: spec.dimensions(),
            channels,
        })
    }

    /// Size of the image as `[width, height, depth]`.
    pub fn dimensions(&self) -> [u32; 3] {
        self.dimensions
    }

    /// Origin of the data window as `[x, y, z]`.
    pub fn origin(&self) -> [i32; 3] {
        self.origin
    }

    /// The channels every sample carries.
    pub fn channels(&self) -> &[DeepChannel] {
        &self.channels
    }

    /// Number of channels per sample.
    ///
    /// `u32` to match every other `channel_count` in the crate, and the
    /// channel indices the API takes.
    pub fn channel_count(&self) -> u32 {
        self.channels.len() as u32
    }

    /// Total number of pixels, each of which holds its own sample list.
    pub fn pixel_count(&self) -> u64 {
        sys::deepdata::deepdata_pixels(self.inner()).max(0) as u64
    }

    /// Index of the depth channel, conventionally `Z`.
    pub fn z_channel(&self) -> Option<u32> {
        self.channel_index(sys::deepdata::deepdata_z_channel(self.inner()))
    }

    /// Index of the far-depth channel, conventionally `Zback`.
    pub fn z_back_channel(&self) -> Option<u32> {
        self.channel_index(sys::deepdata::deepdata_z_back_channel(self.inner()))
    }

    /// Index of the alpha channel.
    pub fn alpha_channel(&self) -> Option<u32> {
        self.channel_index(sys::deepdata::deepdata_a_channel(self.inner()))
    }

    /// How many samples the pixel at `(x, y)` holds, which may be zero.
    pub fn sample_count(&self, x: i32, y: i32) -> Result<u32> {
        let pixel = self.pixel_index(x, y)?;
        Ok(sys::deepdata::deepdata_samples(self.inner(), pixel).max(0) as u32)
    }

    /// One sample's value, converted to `f32`.
    ///
    /// Use [`DeepImage::value_uint`] for channels whose format is an
    /// unsigned integer, where converting would lose the top bits.
    pub fn value(&self, x: i32, y: i32, channel: u32, sample: u32) -> Result<f32> {
        let (pixel, channel, sample) = self.address(x, y, channel, sample)?;
        Ok(sys::deepdata::deepdata_deep_value(
            self.inner(),
            pixel,
            channel,
            sample,
        ))
    }

    /// One sample's value as an unsigned integer.
    pub fn value_uint(&self, x: i32, y: i32, channel: u32, sample: u32) -> Result<u32> {
        let (pixel, channel, sample) = self.address(x, y, channel, sample)?;
        Ok(sys::deepdata::deepdata_deep_value_uint(
            self.inner(),
            pixel,
            channel,
            sample,
        ))
    }

    /// Every sample of one channel at `(x, y)`, in depth order as stored.
    pub fn samples(&self, x: i32, y: i32, channel: u32) -> Result<Vec<f32>> {
        let count = self.sample_count(x, y)?;
        (0..count)
            .map(|sample| self.value(x, y, channel, sample))
            .collect()
    }

    fn channel_index(&self, index: i32) -> Option<u32> {
        let index = u32::try_from(index).ok()?;
        ((index as usize) < self.channels.len()).then_some(index)
    }

    /// Map an image coordinate onto the linear pixel index OpenImageIO uses.
    fn pixel_index(&self, x: i32, y: i32) -> Result<i64> {
        for (axis, coordinate, start, size) in [
            ("x", x, self.origin[0], self.dimensions[0]),
            ("y", y, self.origin[1], self.dimensions[1]),
        ] {
            let end = i64::from(start) + i64::from(size);
            if i64::from(coordinate) < i64::from(start) || i64::from(coordinate) >= end {
                return Err(Error::InvalidRegion {
                    axis,
                    message: format!(
                        "coordinate {coordinate} lies outside the data window {start}..{end}"
                    ),
                });
            }
        }

        let column = i64::from(x) - i64::from(self.origin[0]);
        let row = i64::from(y) - i64::from(self.origin[1]);
        Ok(row * i64::from(self.dimensions[0]) + column)
    }

    fn address(&self, x: i32, y: i32, channel: u32, sample: u32) -> Result<(i64, i32, i32)> {
        let pixel = self.pixel_index(x, y)?;
        if channel as usize >= self.channels.len() {
            return Err(Error::InvalidRoi(format!(
                "channel {channel} is outside the image's {} channels",
                self.channels.len()
            )));
        }
        let count = sys::deepdata::deepdata_samples(self.inner(), pixel).max(0) as u32;
        if sample >= count {
            return Err(Error::InvalidRoi(format!(
                "sample {sample} is outside the {count} samples at ({x}, {y})"
            )));
        }

        let channel = i32::try_from(channel)
            .map_err(|_| Error::InvalidRoi("channel index exceeds i32::MAX".to_owned()))?;
        let sample = i32::try_from(sample)
            .map_err(|_| Error::InvalidRoi("sample index exceeds i32::MAX".to_owned()))?;
        Ok((pixel, channel, sample))
    }

    fn inner(&self) -> &sys::deepdata::DeepData {
        self.inner
            .as_ref()
            .expect("DeepImage invariant violated: null native pointer")
    }
}

impl std::fmt::Debug for DeepImage {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        formatter
            .debug_struct("DeepImage")
            .field("dimensions", &self.dimensions)
            .field("channels", &self.channels.len())
            .field("pixels", &self.pixel_count())
            .finish_non_exhaustive()
    }
}