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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
use openexr_sys as sys;
use crate::{
core::{
error::Error, header::HeaderRef, preview_image::PreviewRgba, LevelMode,
LevelRoundingMode,
},
deep::{
deep_frame_buffer::{DeepFrameBuffer, DeepFrameBufferRef},
deep_tiled_input_file::DeepTiledInputFile,
deep_tiled_input_part::DeepTiledInputPart,
},
multi_part::multi_part_output_file::MultiPartOutputFile,
};
use imath_traits::Bound2;
type Result<T, E = Error> = std::result::Result<T, E>;
use std::marker::PhantomData;
#[repr(transparent)]
pub struct DeepTiledOutputPart<'a> {
pub(crate) inner: sys::Imf_DeepTiledOutputPart_t,
phantom: std::marker::PhantomData<&'a MultiPartOutputFile>,
}
impl<'a> DeepTiledOutputPart<'a> {
/// Get an interface to the part `part_number` of the [`MultiPartOutputFile`]
/// `multi_part_file`.
///
pub fn new(
multi_part_file: &MultiPartOutputFile,
part_number: i32,
) -> Result<DeepTiledOutputPart> {
let mut part = sys::Imf_DeepTiledOutputPart_t::default();
unsafe {
sys::Imf_DeepTiledOutputPart_ctor(
&mut part,
multi_part_file.0,
part_number,
)
.into_result()?;
}
Ok(DeepTiledOutputPart {
inner: part,
phantom: PhantomData,
})
}
/// Define a frame buffer as the pixel data source.
///
/// Pixel (x, y) is at offset x * x_stride + y * y_stride
///
/// ## Errors
/// * [`Error::Base`] - If an error occurs
///
pub fn set_frame_buffer(
&mut self,
frame_buffer: &DeepFrameBuffer,
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_setFrameBuffer(
&mut self.inner,
frame_buffer.ptr,
)
.into_result()?;
}
Ok(())
}
/// Access to the file [`Header`](crate::core::header::Header)
///
pub fn header(&self) -> HeaderRef {
unsafe {
let mut ptr = std::ptr::null();
sys::Imf_DeepTiledOutputPart_header(&self.inner, &mut ptr);
if ptr.is_null() {
panic!("Received null ptr from sys::Imf_DeepTiledOutputPart_header");
}
HeaderRef::new(ptr)
}
}
/// Get the [`DeepFrameBuffer`](crate::deep::deep_frame_buffer::DeepFrameBuffer)
///
pub fn frame_buffer(&self) -> DeepFrameBufferRef {
unsafe {
let mut ptr = std::ptr::null();
sys::Imf_DeepTiledOutputPart_frameBuffer(&self.inner, &mut ptr);
if ptr.is_null() {
panic!("Received null ptr from sys::Imf_DeepTiledOutputPart_frameBuffer");
}
DeepFrameBufferRef::new(ptr)
}
}
/// Get the tiles' x dimension
///
pub fn tile_x_size(&self) -> u32 {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_tileXSize(&self.inner, &mut v).into_result().expect("Unexpected exception from Imf_DeepTiledOutputPart_tileXSize");
}
v
}
/// Get the tiles' y dimension
///
pub fn tile_y_size(&self) -> u32 {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_tileYSize(&self.inner, &mut v).into_result().expect("Unexpected exception from Imf_DeepTiledOutputPart_tileYSize");
}
v
}
/// Get the level mode
///
pub fn level_mode(&self) -> LevelMode {
let mut v = sys::Imf_LevelMode(0);
unsafe {
sys::Imf_DeepTiledOutputPart_levelMode(
&self.inner,
&mut v,
)
.into_result()
.expect(
"Unexpected exception from Imf_DeepTiledOutputPart_levelMode",
);
}
v.into()
}
/// Get the level rounding mode
///
pub fn level_rounding_mode(&self) -> LevelRoundingMode {
let mut v = sys::Imf_LevelRoundingMode(0);
unsafe {
sys::Imf_DeepTiledOutputPart_levelRoundingMode(
&self.inner,
&mut v,
)
.into_result()
.expect(
"Unexpected exception from Imf_DeepTiledOutputPart_levelRoundingMode",
);
}
v.into()
}
/// Get the number of levels in the file
///
/// # Returns
/// * `Ok(1)` if [`level_mode()`](DeepTiledOutputPart::level_mode()) == [`LevelMode::OneLevel`]
/// * `Ok(rfunc (log (max (w, h)) / log (2)) + 1)` if [`level_mode()`](DeepTiledOutputPart::level_mode()) == [`LevelMode::MipmapLevels`]
/// * `Err(Error::Logic)` if [`level_mode()`](DeepTiledOutputPart::level_mode()) == [`LevelMode::RipmapLevels`]
///
/// where `rfunc` is either `floor()` or `ceil()` depending on whether
/// [`level_rounding_mode()`](DeepTiledOutputPart::level_rounding_mode()) is [`LevelRoundingMode::RoundUp`](crate::core::LevelRoundingMode::RoundUp) or [`LevelRoundingMode::RoundDown`](crate::core::LevelRoundingMode::RoundDown)
///
pub fn num_levels(&self) -> Result<i32> {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_numLevels(&self.inner, &mut v)
.into_result()?;
}
Ok(v)
}
/// Get the number of levels in the file in the x axis
///
/// # Returns
/// * `1` if [`level_mode()`](DeepTiledOutputPart::level_mode) == [`LevelMode::OneLevel`]
/// * `rfunc (log (max (w, h)) / log (2)) + 1` if [`level_mode()`](DeepTiledOutputPart::level_mode) == [`LevelMode::MipmapLevels`]
/// * `rfunc (log (w) / log (2)) + 1` if [`level_mode()`](DeepTiledOutputPart::level_mode) == [`LevelMode::RipmapLevels`]
///
/// where `rfunc` is either `floor()` or `ceil()` depending on whether
/// [`level_rounding_mode()`](DeepTiledOutputPart::level_rounding_mode()) is [`LevelRoundingMode::RoundUp`] or [`LevelRoundingMode::RoundDown`]
///
pub fn num_x_levels(&self) -> i32 {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_numXLevels(&self.inner, &mut v).into_result().expect("Unexpected exception from Imf_DeepTiledOutputPart_numXLevels");
}
v
}
/// Get the number of levels in the file in the y axis
///
/// # Returns
/// * `1` if [`level_mode()`](DeepTiledOutputPart::level_mode) == [`LevelMode::OneLevel`]
/// * `rfunc (log (max (w, h)) / log (2)) + 1` if [`level_mode()`](DeepTiledOutputPart::level_mode)`DeepTiledOutputPart::mode()`] == [`LevelMode::MipmapLevels`]
/// * `rfunc (log (h) / log (2)) + 1` if [`level_mode()`](DeepTiledOutputPart::level_mode) == [`LevelMode::RipmapLevels`]
///
/// where `rfunc` is either `floor()` or `ceil()` depending on whether
/// [`level_rounding_mode()`](DeepTiledOutputPart::level_rounding_mode()) is [`LevelRoundingMode::RoundUp`] or [`LevelRoundingMode::RoundDown`]
///
pub fn num_y_levels(&self) -> i32 {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_numYLevels(&self.inner, &mut v).into_result().expect("Unexpected exception from Imf_DeepTiledOutputPart_numYLevels");
}
v
}
/// Returns `true` if the file contains a level with level number `(lx, ly)`, `false`
/// otherwise.
///
pub fn is_valid_level(&self, lx: i32, ly: i32) -> bool {
let mut v = false;
unsafe {
sys::Imf_DeepTiledOutputPart_isValidLevel(&self.inner, &mut v, lx, ly).into_result().expect("Unexpected exception from Imf_DeepTiledOutputPart_isValidLevel");
}
v
}
/// Returns the width of the level with level number `(lx, *)`, where `*` is any number.
///
/// # Returns
/// * `max (1, rfunc (w / pow (2, lx)))`
///
/// where `rfunc` is either `floor()` or `ceil()` depending on whether
/// [`level_rounding_mode()`](DeepTiledOutputPart::level_rounding_mode()) is [`LevelRoundingMode::RoundUp`] or [`LevelRoundingMode::RoundDown`]
///
/// # Errors
/// *[`Error::Base`] - If any error occurs
///
pub fn level_width(&self, lx: i32) -> Result<i32> {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_levelWidth(&self.inner, &mut v, lx)
.into_result()?;
}
Ok(v)
}
/// Returns the height of the level with level number `(*, ly)`, where `*` is any number.
///
/// # Returns
/// * `max (1, rfunc (h / pow (2, ly)))`
///
/// where `rfunc` is either `floor()` or `ceil()` depending on whether
/// [`level_rounding_mode()`](DeepTiledOutputPart::level_rounding_mode()) is [`LevelRoundingMode::RoundUp`] or [`LevelRoundingMode::RoundDown`]
///
/// # Errors
/// *[`Error::Base`] - If any error occurs
///
pub fn level_height(&self, ly: i32) -> Result<i32> {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_levelHeight(&self.inner, &mut v, ly)
.into_result()?;
}
Ok(v)
}
/// Get the number of tiles in the x axis that cover a level with level number `(lx, *)`
/// where `*` is any number
///
/// # Returns
/// *(level_width(lx) + tile_x_size() - 1) / tile_x_size()
///
/// # Errors
/// *[`Error::InvalidArgument`] - If `lx` is not a valid level
///
pub fn num_x_tiles(&self, lx: i32) -> Result<i32> {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_numXTiles(&self.inner, &mut v, lx)
.into_result()?;
}
Ok(v)
}
/// Get the number of tiles in the y axis that cover a level with level number `(*, ly)`
/// where `*` is any number
///
/// # Returns
/// * (level_height(ly) + tile_y_size() - 1) / tile_y_size()
///
/// # Errors
/// *[`Error::InvalidArgument`] - If `lx` is not a valid level
///
pub fn num_y_tiles(&self, ly: i32) -> Result<i32> {
let mut v = 0;
unsafe {
sys::Imf_DeepTiledOutputPart_numYTiles(&self.inner, &mut v, ly)
.into_result()?;
}
Ok(v)
}
/// Returns a 2-dimensional region of valid pixel coordinates for a level with level number `(lx, ly)`
///
/// # Errors
/// *[`Error::Base`] - if any error occurs
///
pub fn data_window_for_level<B: Bound2<i32>>(
&self,
lx: i32,
ly: i32,
) -> Result<B> {
let mut dw = [0i32; 4];
unsafe {
sys::Imf_DeepTiledOutputPart_dataWindowForLevel(
&self.inner,
dw.as_mut_ptr() as *mut sys::Imath_Box2i_t,
lx,
ly,
)
.into_result()?;
}
Ok(B::from_slice(&dw))
}
/// Returns a 2-dimensional region of valid pixel coordinates for a level with tile coordinates `(dx, dy)` and level number `(lx, ly)`
///
/// # Errors
/// * [`Error::InvalidArgument`] - if the passed tile coordinates are invalid
/// * [`Error::Base`] - if any other error occurs
///
pub fn data_window_for_tile<B: Bound2<i32>>(
&self,
dx: i32,
dy: i32,
lx: i32,
ly: i32,
) -> Result<B> {
let mut dw = [0i32; 4];
unsafe {
sys::Imf_DeepTiledOutputPart_dataWindowForTile(
&self.inner,
dw.as_mut_ptr() as *mut sys::Imath_Box2i_t,
dx,
dy,
lx,
ly,
)
.into_result()?;
}
Ok(B::from_slice(&dw))
}
/// Writes the tile with tile
/// coordinates (dx, dy), and level number (lx, ly) to
/// the file.
///
/// # Errors
/// * [`Error::InvalidArgument`] if dx does not lie in the interval [0, numXTiles(lx) - 1]
/// * [`Error::InvalidArgument`] if dy does not lie in the interval [0, numYTiles(ly) - 1]
///
/// * [`Error::InvalidArgument`] if lx does not lie in the interval [0, numXLevels() - 1]
/// * [`Error::InvalidArgument`] if ly does not lie in the inverval [0, numYLevels() - 1]
/// * [`Error::Base`] if any other error occurs
///
///
/// Pixels that are outside the pixel coordinate range for the tile's
/// level, are never accessed by writeTile().
///
/// Each tile in the file must be written exactly once.
///
/// The file's line order attribute determines the order of the tiles
/// in the file:
///
/// ## [`LineOrder::IncreasingY`](crate::core::LineOrder)
/// In the file, the tiles for each level are stored
/// in a contiguous block. The levels are ordered
/// like this:
/// ```c
/// (0, 0) (1, 0) ... (nx-1, 0)
/// (0, 1) (1, 1) ... (nx-1, 1)
/// ...
/// (0,ny-1) (1,ny-1) ... (nx-1,ny-1)
/// ```
///
/// where nx = [`num_x_levels()`](DeepTiledOutputPart::num_x_levels), and ny = [`num_y_levels()`](DeepTiledOutputPart::num_y_levels).
///
/// In an individual level, `(lx, ly)`, the tiles
/// are stored in the following order:
///
/// ```c
/// (0, 0) (1, 0) ... (tx-1, 0)
/// (0, 1) (1, 1) ... (tx-1, 1)
/// ...
/// (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
/// ```
///
/// where tx = [`num_x_tiles(lx)`](DeepTiledOutputPart::num_x_tiles),
/// and ty = [`num_y_tiles(ly)`](DeepTiledOutputPart::num_y_tiles).
///
/// ## [`LineOrder::DecreasingY`](crate::core::LineOrder)
/// As for [`LineOrder::IncreasingY`](crate::core::LineOrder), the tiles
/// for each level are stored in a contiguous block. The levels
/// are ordered the same way as for [`LineOrder::IncreasingY`](crate::core::LineOrder),
/// but within an individual level, the tiles
/// are stored in this order:
///
/// ```c
/// (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
/// ...
/// (0, 1) (1, 1) ... (tx-1, 1)
/// (0, 0) (1, 0) ... (tx-1, 0)
/// ```
///
///
/// ## [`LineOrder::RandomY`](crate::core::LineOrder)
/// The order of the calls to `write_tile()` determines
/// the order of the tiles in the file.
///
pub fn write_tile(
&mut self,
dx: i32,
dy: i32,
lx: i32,
ly: i32,
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_writeTile(
&mut self.inner,
dx,
dy,
lx,
ly,
)
.into_result()?
}
Ok(())
}
/// Writes multiple tiles at once.
///
/// If multi-threading is used multiple tiles are written concurrently.
/// The tile coordinates, dx1, dx2 and dy1, dy2, specify inclusive ranges of tile
/// coordinates. It is valid for dx1 < dx2 or dy1 < dy2; the
/// tiles are always written in the order specified by the line
/// order attribute. Hence, it is not possible to specify an
/// "invalid" or empty tile range.
///
/// Pixels that are outside the pixel coordinate range for the tile's
/// level, are never accessed by writeTile().
///
/// Each tile in the file must be written exactly once.
///
/// The file's line order attribute determines the order of the tiles
/// in the file:
///
/// ## [`LineOrder::IncreasingY`](crate::core::LineOrder)
/// In the file, the tiles for each level are stored
/// in a contiguous block. The levels are ordered
/// like this:
/// ```c
/// (0, 0) (1, 0) ... (nx-1, 0)
/// (0, 1) (1, 1) ... (nx-1, 1)
/// ...
/// (0,ny-1) (1,ny-1) ... (nx-1,ny-1)
/// ```
///
/// where nx = [`num_x_levels()`](DeepTiledOutputPart::num_x_levels), and ny = [`num_y_levels()`](DeepTiledOutputPart::num_y_levels).
///
/// In an individual level, `(lx, ly)`, the tiles
/// are stored in the following order:
///
/// ```c
/// (0, 0) (1, 0) ... (tx-1, 0)
/// (0, 1) (1, 1) ... (tx-1, 1)
/// ...
/// (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
/// ```
///
/// where tx = [`num_x_tiles(lx)`](DeepTiledOutputPart::num_x_tiles),
/// and ty = [`num_y_tiles(ly)`](DeepTiledOutputPart::num_y_tiles).
///
/// ## [`LineOrder::DecreasingY`](crate::core::LineOrder)
/// As for [`LineOrder::IncreasingY`](crate::core::LineOrder), the tiles
/// for each level are stored in a contiguous block. The levels
/// are ordered the same way as for [`LineOrder::IncreasingY`](crate::core::LineOrder),
/// but within an individual level, the tiles
/// are stored in this order:
///
/// ```c
/// (0,ty-1) (1,ty-1) ... (tx-1,ty-1)
/// ...
/// (0, 1) (1, 1) ... (tx-1, 1)
/// (0, 0) (1, 0) ... (tx-1, 0)
/// ```
///
///
/// ## [`LineOrder::RandomY`](crate::core::LineOrder)
/// The order of the calls to `write_tile()` determines
/// the order of the tiles in the file.
///
pub fn write_tiles(
&mut self,
dx1: i32,
dx2: i32,
dy1: i32,
dy2: i32,
lx: i32,
ly: i32,
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_writeTiles(
&mut self.inner,
dx1,
dx2,
dy1,
dy2,
lx,
ly,
)
.into_result()?;
}
Ok(())
}
/// Supplies a new set of pixels for the preview image attribute in the file's header
///
/// Note: update_preview_image() is necessary because images are
/// often stored in a file incrementally, a few tiles at a time,
/// while the image is being generated. Since the preview image
/// is an attribute in the file's header, it gets stored in the
/// file as soon as the file is opened, but we may not know what
/// the preview image should look like until we have written the
/// last tile of the main image.
///
/// # Errors
/// * [`Error::LogicError`] - If the image header does not contain a preview image
/// * [`Error::Base`] - If any other error occurs
///
pub fn update_preview_image(
&mut self,
new_pixels: &[PreviewRgba],
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_updatePreviewImage(
&mut self.inner,
new_pixels.as_ptr() as *const sys::Imf_PreviewRgba_t,
)
.into_result()?;
}
Ok(())
}
/// Shortcut to copy all pixels from an [`DeepTiledInputFile`] into this file,
/// without uncompressing and then recompressing the pixel data.
///
/// This file's header must be compatible with the [`DeepTiledInputFile`]'s
/// header: The two header's "dataWindow", "compression",
/// "lineOrder" and "channels" attributes must be the same.
///
/// # Errors
/// * [`Error::InvalidArgument`] - If the headers do not match
/// * [`Error::LogicError`] - If tiles have already been written to this file.
/// * [`Error::Base`] - If any other error occurs
///
pub fn copy_pixels_from_file(
&mut self,
file: &DeepTiledInputFile,
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_copyPixels_from_file(
&mut self.inner,
file.0,
)
.into_result()?;
}
Ok(())
}
/// Shortcut to copy all pixels from an [`DeepTiledInputPart`] into this file,
/// without uncompressing and then recompressing the pixel data.
///
/// This file's header must be compatible with the [`DeepTiledInputPart`]'s
/// header: The two header's "dataWindow", "compression",
/// "lineOrder" and "channels" attributes must be the same.
///
/// # Errors
/// * [`Error::InvalidArgument`] - If the headers do not match
/// * [`Error::LogicError`] - If tiles have already been written to this file.
/// * [`Error::Base`] - If any other error occurs
///
pub fn copy_pixels_from_part(
&mut self,
file: &mut DeepTiledInputPart,
) -> Result<()> {
unsafe {
sys::Imf_DeepTiledOutputPart_copyPixels_from_part(
&mut self.inner,
&mut file.inner,
)
.into_result()?;
}
Ok(())
}
}