oiio-sys 0.4.1

Rust sys bindings for OpenImageIO https://github.com/AcademySoftwareFoundation/OpenImageIO
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
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
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
#include "ffi_imagebuf.h"
#include "ffi_pixel.h"
#include "oiio-sys/src/imagebuf.rs.h"
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <cstddef>
#include <memory>
#include <stdexcept>
#include <stdio.h>

namespace oiio {
#pragma region ImageBuf
std::unique_ptr<ImageBuf>
imagebuf_default()
{
    return std::make_unique<ImageBuf>();
}

std::unique_ptr<ImageBuf>
imagebuf_new_from_file(rust::Str name, int subimage, int miplevel,
                       std::shared_ptr<ImageCache> imagecache,
                       const ImageSpec* config,
                       IOProxy* ioproxy)
{
    std::string_view c_name(name.data(), name.size());
    return std::make_unique<ImageBuf>(c_name, subimage, miplevel,
                                      std::move(imagecache), config, ioproxy);
}

std::unique_ptr<ImageBuf>
imagebuf_new_from_spec(const ImageSpec& spec, InitializePixels zero)
{
    return std::make_unique<ImageBuf>(spec, zero);
}

std::unique_ptr<ImageBuf>
imagebuf_new_from_spec_checked(const ImageSpec& spec, rust::String& error)
{
    error = rust::String();

    // Deliberately InitializePixels::No. When the allocation fails OpenImageIO
    // does not propagate the bad_alloc: ImageBufImpl::new_pixels catches it,
    // records the error on the buffer, leaves the pixels null and comments
    // that it hopes this is "handled well downstream". With Yes, downstream is
    // ImageBufAlgo::zero, which asserts on localpixels() and then divides by
    // zero, so the constructor never returns and there is nothing to check.
    // Allocating first and zeroing afterwards gives us the chance.
    //
    // The flat allocation is caught by new_pixels and reported through
    // localpixels(), but the deep allocation is not: realloc's deep branch
    // calls DeepData::init, which resizes three per-pixel vectors with no
    // try/catch of its own, so a deep spec too large for the machine throws
    // straight out of the constructor. This shim is wrapped noexcept by cxx,
    // so an escaping exception would be std::terminate. Catch it here and turn
    // it into an error, the same courtesy new_pixels does for the flat case.
    try {
        auto buffer = std::make_unique<ImageBuf>(spec, OIIO::InitializePixels::No);

        // A deep image legitimately has no flat storage: realloc passes size 0
        // and the samples live in the DeepData. Only a flat image must have
        // pixels.
        if (!spec.deep && buffer->localpixels() == nullptr) {
            std::string recorded = buffer->geterror(true);
            if (recorded.empty())
                recorded = OIIO::Strutil::fmt::format(
                    "OpenImageIO could not allocate {} bytes for the image",
                    spec.image_bytes());
            error = rust::String::lossy(recorded.data(), recorded.size());
            return {};
        }
        if (buffer->has_error()) {
            const std::string recorded = buffer->geterror(true);
            error = rust::String::lossy(recorded.data(), recorded.size());
            return {};
        }

        if (!spec.deep && !OIIO::ImageBufAlgo::zero(*buffer)) {
            const std::string recorded = buffer->geterror(true);
            error = rust::String::lossy(
                recorded.empty() ? "OpenImageIO could not clear the image"
                                 : recorded.c_str());
            return {};
        }
        return buffer;
    } catch (const std::exception& exception) {
        const std::string recorded = exception.what();
        error = rust::String::lossy(
            recorded.empty() ? "OpenImageIO could not allocate the image"
                             : recorded.c_str());
        return {};
    }
}

std::unique_ptr<ImageBuf>
imagebuf_new_from_buffer(const ImageSpec& spec, uint8_t* buffer,
                         int64_t xstride, int64_t ystride, int64_t zstride)
{
    return std::make_unique<ImageBuf>(spec, buffer, xstride, ystride, zstride);
}

std::unique_ptr<ImageBuf>
imagebuf_clone_checked(const ImageBuf& imagebuf, rust::String& error)
{
    // The copy constructor catches its own allocation failure, records an
    // error on the copy, and returns it anyway — with the source's
    // pixels_valid flag still set and its pixels null, a shape whose first
    // read divides by a zero tile width or dereferences a null cache. Check
    // the error state here so the broken copy never crosses into Rust. The
    // try/catch covers what the constructor does not: a throw while copying
    // the specification's own storage.
    error = rust::String();
    try {
        auto copy = std::make_unique<ImageBuf>(imagebuf);
        if (copy->has_error()) {
            const std::string recorded = copy->geterror(true);
            error = rust::String::lossy(
                recorded.empty() ? "OpenImageIO could not copy the image"
                                 : recorded.c_str());
            return {};
        }
        return copy;
    } catch (const std::exception& exception) {
        const std::string recorded = exception.what();
        error = rust::String::lossy(
            recorded.empty() ? "OpenImageIO could not copy the image"
                             : recorded.c_str());
        return {};
    }
}

void
imagebuf_reset(ImageBuf& imagebuf)
{
    imagebuf.reset();
}

void
imagebuf_reset_from_file(ImageBuf& imagebuf, rust::Str name, int subimage,
                         int miplevel, std::shared_ptr<ImageCache> imagecache,
                         const ImageSpec* config, IOProxy* ioproxy)
{
    std::string_view c_name(name.data(), name.size());
    imagebuf.reset(c_name, subimage, miplevel, std::move(imagecache), config,
                   ioproxy);
}

void
imagebuf_reset_from_spec(ImageBuf& imagebuf, const ImageSpec& spec,
                         InitializePixels zero)
{
    imagebuf.reset(spec, zero);
}

void
imagebuf_reset_from_buffer(ImageBuf& imagebuf, const ImageSpec& spec,
                           uint8_t* buffer, int64_t xstride, int64_t ystride,
                           int64_t zstride)
{
    imagebuf.reset(spec, buffer, xstride, ystride, zstride);
}

bool
imagebuf_make_writeable(ImageBuf& imagebuf, bool keep_cache_type)
{
    return imagebuf.make_writable(keep_cache_type);
}


bool
imagebuf_read(ImageBuf& imagebuf, int subimage, int miplevel, bool force,
              TypeDesc convert)
{
    return imagebuf.read(subimage, miplevel, force, convert);
}

bool
imagebuf_read_subset(ImageBuf& imagebuf, int subimage, int miplevel,
                     int chbegin, int chend, bool force, TypeDesc convert)
{
    return imagebuf.read(subimage, miplevel, chbegin, chend, force, convert);
}

bool
imagebuf_init_spec(ImageBuf& imagebuf, rust::Str filename, int subimage,
                   int miplevel)
{
    std::string_view c_filename(filename.data(), filename.size());
    return imagebuf.init_spec(c_filename, subimage, miplevel);
}

bool
imagebuf_write(ImageBuf& imagebuf, rust::Str filename, TypeDesc dtype,
               rust::Str fileformat)
{
    std::string_view c_filename(filename.data(), filename.size());
    std::string_view c_fileformat(fileformat.data(), fileformat.size());
    return imagebuf.write(c_filename, dtype, c_fileformat);
}

void
imagebuf_set_write_format(ImageBuf& imagebuf, TypeDesc format)
{
    imagebuf.set_write_format(format);
}

void
imagebuf_set_write_format_from_channel_names(ImageBuf& imagebuf,
                                             rust::Slice<const TypeDesc> format)
{
    OIIO::cspan<TypeDesc> c_format(format.data(), format.size());
    imagebuf.set_write_format(c_format);
}

void
imagebuf_set_write_tiles(ImageBuf& imagebuf, int width, int height, int depth)
{
    imagebuf.set_write_tiles(width, height, depth);
}

void
imagebuf_set_write_ioproxy(ImageBuf& imagebuf, IOProxy* ioproxy)
{
    imagebuf.set_write_ioproxy(ioproxy);
}

bool
imagebuf_write_to_imageoutput(ImageBuf& imagebuf, ImageOutput* out)
{
    return imagebuf.write(out);
}

void
imagebuf_copy_metadata(ImageBuf& imagebuf, const ImageBuf& src)
{
    imagebuf.copy_metadata(src);
}

bool
imagebuf_copy_pixels(ImageBuf& imagebuf, const ImageBuf& src)
{
    return imagebuf.copy_pixels(src);
}

bool
imagebuf_copy(ImageBuf& imagebuf, const ImageBuf& src, TypeDesc format)
{
    return imagebuf.copy(src, format);
}

std::unique_ptr<ImageBuf>
imagebuf_copy_to_imagebuf(const ImageBuf& imagebuf, TypeDesc format)
{
    return std::make_unique<ImageBuf>(imagebuf.copy(format));
}

void
imagebuf_swap(ImageBuf& imagebuf, ImageBuf& other)
{
    imagebuf.swap(other);
}

float
imagebuf_getchannel(const ImageBuf& imagebuf, int x, int y, int z, int c,
                    WrapMode wrap)
{
    return imagebuf.getchannel(x, y, z, c, wrap);
}

void
imagebuf_getpixel(const ImageBuf& imagebuf, int x, int y, int z, float* pixel,
                  int maxchannels, WrapMode wrap)
{
    imagebuf.getpixel(x, y, z, pixel, maxchannels, wrap);
}

void
imagebuf_interppixel(const ImageBuf& imagebuf, float x, float y, float* pixel,
                     WrapMode wrap)
{
    imagebuf.interppixel(x, y, pixel, wrap);
}

void
imagebuf_interppixel_NDC(const ImageBuf& imagebuf, float s, float t,
                         float* pixel, WrapMode wrap)
{
    imagebuf.interppixel_NDC(s, t, pixel, wrap);
}

void
imagebuf_interppixel_bicubic(const ImageBuf& imagebuf, float x, float y,
                             float* pixel, WrapMode wrap)
{
    imagebuf.interppixel_bicubic(x, y, pixel, wrap);
}

void
imagebuf_interppixel_bicubic_NDC(const ImageBuf& imagebuf, float s, float t,
                                 float* pixel, WrapMode wrap)
{
    imagebuf.interppixel_bicubic_NDC(s, t, pixel, wrap);
}

void
imagebuf_setpixel(ImageBuf& imagebuf, int x, int y, int z,
                  rust::Slice<const float> pixel)
{
    OIIO::cspan<float> c_pixel(pixel.data(), pixel.size());
    imagebuf.setpixel(x, y, z, c_pixel);
}

void
imagebuf_setpixel_i(ImageBuf& imagebuf, int i, rust::Slice<const float> pixel)
{
    // ImageBuf::setpixel(int i, ..) resolves the linear index with
    // `i % spec().width` and `i / spec().width`, so an image with no pixels
    // divides by zero. Out-of-range coordinates are absorbed by the iterator
    // it delegates to, but the division happens first.
    const OIIO::ImageSpec& spec = imagebuf.spec();
    if (spec.width <= 0 || spec.height <= 0)
        return;
    if (i < 0 || int64_t(i) >= int64_t(spec.width) * int64_t(spec.height))
        return;
    OIIO::cspan<float> c_pixel(pixel.data(), pixel.size());
    imagebuf.setpixel(i, c_pixel);
}

void
imagebuf_setpixel_with_coordinates(ImageBuf& imagebuf, int x, int y, int z,
                                   const float* pixel, int maxchannels)
{
    imagebuf.setpixel(x, y, z, pixel, maxchannels);
}

bool
imagebuf_get_pixels(const ImageBuf& imagebuf, ROI roi, TypeDesc format,
                    uint8_t* result, int64_t xstride, int64_t ystride,
                    int64_t zstride)
{
    return imagebuf.get_pixels(roi, format, result, xstride, ystride, zstride);
}

bool
imagebuf_set_pixels(ImageBuf& imagebuf, ROI roi, TypeDesc format,
                    const uint8_t* data, int64_t xstride, int64_t ystride,
                    int64_t zstride)
{
    return imagebuf.set_pixels(roi, format, data, xstride, ystride, zstride);
}

namespace {
// Shared validation for the bounded pixel calls: the region must be non-empty
// and the buffer must hold exactly one contiguous value per channel, pixel,
// row and slice of it.
inline bool
bounded_roi_layout(const ROI& roi, TypeDesc format, std::size_t buffer_bytes,
                   detail::PixelLayout& layout)
{
    const int64_t width    = static_cast<int64_t>(roi.xend) - roi.xbegin;
    const int64_t height   = static_cast<int64_t>(roi.yend) - roi.ybegin;
    const int64_t depth    = static_cast<int64_t>(roi.zend) - roi.zbegin;
    const int64_t channels = static_cast<int64_t>(roi.chend) - roi.chbegin;
    return detail::bounded_pixel_layout(channels, width, height, depth, format,
                                        buffer_bytes, layout);
}
}  // namespace

bool
imagebuf_get_pixels_span(const ImageBuf& imagebuf, const ROI& roi,
                         TypeDesc format, rust::Slice<uint8_t> result)
{
    detail::PixelLayout layout;
    if (!bounded_roi_layout(roi, format, result.size(), layout))
        return false;
    return imagebuf.get_pixels(roi, format, result.data(), layout.x_stride,
                               layout.y_stride, layout.z_stride);
}

bool
imagebuf_set_pixels_span(ImageBuf& imagebuf, const ROI& roi, TypeDesc format,
                         rust::Slice<const uint8_t> data)
{
    detail::PixelLayout layout;
    if (!bounded_roi_layout(roi, format, data.size(), layout))
        return false;
    return imagebuf.set_pixels(roi, format, data.data(), layout.x_stride,
                               layout.y_stride, layout.z_stride);
}

bool
imagebuf_initialized(const ImageBuf& imagebuf)
{
    return imagebuf.initialized();
}

IBStorage
imagebuf_storage(const ImageBuf& imagebuf)
{
    return imagebuf.storage();
}

const ImageSpec&
imagebuf_spec(const ImageBuf& imagebuf)
{
    return imagebuf.spec();
}

ImageSpec&
imagebuf_specmod(ImageBuf& imagebuf)
{
    return imagebuf.specmod();
}

const ImageSpec&
imagebuf_nativespec(const ImageBuf& imagebuf)
{
    return imagebuf.nativespec();
}

bool
imagebuf_has_thumbnail(const ImageBuf& imagebuf)
{
    return imagebuf.has_thumbnail();
}

std::shared_ptr<ImageBuf>
imagebuf_get_thumbnail(const ImageBuf& imagebuf)
{
    return imagebuf.get_thumbnail();
}

void
imagebuf_set_thumbnail(ImageBuf& imagebuf, const ImageBuf& thumb)
{
    imagebuf.set_thumbnail(thumb);
}

void
imagebuf_clear_thumbnail(ImageBuf& imagebuf)
{
    imagebuf.clear_thumbnail();
}

// Both of these return a string_view. Handing that straight to rust::Str
// selects its std::string constructor, which materialises a temporary and
// leaves the returned Str pointing at freed memory. Constructing from the
// view's own data and size keeps the pointer inside the ImageBuf, whose
// lifetime the Rust signature already borrows.
rust::Str
imagebuf_name(const ImageBuf& imagebuf)
{
    const OIIO::string_view name = imagebuf.name();
    return rust::Str(name.data(), name.size());
}

rust::Str
imagebuf_file_format_name(const ImageBuf& imagebuf)
{
    const OIIO::string_view format = imagebuf.file_format_name();
    return rust::Str(format.data(), format.size());
}

int
imagebuf_subimage(const ImageBuf& imagebuf)
{
    return imagebuf.subimage();
}

int
imagebuf_nsubimages(const ImageBuf& imagebuf)
{
    return imagebuf.nsubimages();
}

int
imagebuf_miplevel(const ImageBuf& imagebuf)
{
    return imagebuf.miplevel();
}

int
imagebuf_nmiplevels(const ImageBuf& imagebuf)
{
    return imagebuf.nmiplevels();
}

int
imagebuf_nchannels(const ImageBuf& imagebuf)
{
    return imagebuf.nchannels();
}

int
imagebuf_xbegin(const ImageBuf& imagebuf)
{
    return imagebuf.xbegin();
}

int
imagebuf_ybegin(const ImageBuf& imagebuf)
{
    return imagebuf.ybegin();
}

int
imagebuf_zbegin(const ImageBuf& imagebuf)
{
    return imagebuf.zbegin();
}

int
imagebuf_xend(const ImageBuf& imagebuf)
{
    return imagebuf.xend();
}

int
imagebuf_yend(const ImageBuf& imagebuf)
{
    return imagebuf.yend();
}

int
imagebuf_zend(const ImageBuf& imagebuf)
{
    return imagebuf.zend();
}

int
imagebuf_xmin(const ImageBuf& imagebuf)
{
    return imagebuf.xmin();
}

int
imagebuf_ymin(const ImageBuf& imagebuf)
{
    return imagebuf.ymin();
}

int
imagebuf_zmin(const ImageBuf& imagebuf)
{
    return imagebuf.zmin();
}

int
imagebuf_xmax(const ImageBuf& imagebuf)
{
    return imagebuf.xmax();
}

int
imagebuf_ymax(const ImageBuf& imagebuf)
{
    return imagebuf.ymax();
}

int
imagebuf_zmax(const ImageBuf& imagebuf)
{
    return imagebuf.zmax();
}

int
imagebuf_orientation(const ImageBuf& imagebuf)
{
    return imagebuf.orientation();
}

void
imagebuf_set_orientation(ImageBuf& imagebuf, int orient)
{
    imagebuf.set_orientation(orient);
}

int
imagebuf_oriented_width(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_width();
}

int
imagebuf_oriented_height(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_height();
}

int
imagebuf_oriented_x(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_x();
}

int
imagebuf_oriented_y(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_y();
}

int
imagebuf_oriented_full_width(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_full_width();
}

int
imagebuf_oriented_full_height(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_full_height();
}

int
imagebuf_oriented_full_x(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_full_x();
}

int
imagebuf_oriented_full_y(const ImageBuf& imagebuf)
{
    return imagebuf.oriented_full_y();
}

void
imagebuf_set_origin(ImageBuf& imagebuf, int x, int y, int z)
{
    imagebuf.set_origin(x, y, z);
}

void
imagebuf_set_full(ImageBuf& imagebuf, int xbegin, int xend, int ybegin,
                  int yend, int zbegin, int zend)
{
    imagebuf.set_full(xbegin, xend, ybegin, yend, zbegin, zend);
}

ROI
imagebuf_roi(const ImageBuf& imagebuf)
{
    return imagebuf.roi();
}

ROI
imagebuf_roi_full(const ImageBuf& imagebuf)
{
    return imagebuf.roi_full();
}

void
imagebuf_set_roi_full(ImageBuf& imagebuf, const ROI& newroi)
{
    imagebuf.set_roi_full(newroi);
}

bool
imagebuf_contains_roi(const ImageBuf& imagebuf, ROI roi)
{
    return imagebuf.contains_roi(roi);
}

bool
imagebuf_pixels_valid(const ImageBuf& imagebuf)
{
    return imagebuf.pixels_valid();
}

TypeDesc
imagebuf_pixeltype(const ImageBuf& imagebuf)
{
    return imagebuf.pixeltype();
}

uint8_t*
imagebuf_localpixels_mut(ImageBuf& imagebuf)
{
    return (uint8_t*)(imagebuf.localpixels());
}

const uint8_t*
imagebuf_localpixels(const ImageBuf& imagebuf)
{
    return (const uint8_t*)(imagebuf.localpixels());
}


int64_t
imagebuf_pixel_stride(const ImageBuf& imagebuf)
{
    return imagebuf.pixel_stride();
}

int64_t
imagebuf_scanline_stride(const ImageBuf& imagebuf)
{
    return imagebuf.scanline_stride();
}

int64_t
imagebuf_z_stride(const ImageBuf& imagebuf)
{
    return imagebuf.z_stride();
}

bool
imagebuf_contiguous(const ImageBuf& imagebuf)
{
    return imagebuf.contiguous();
}

bool
imagebuf_cachedpixels(const ImageBuf& imagebuf)
{
    return imagebuf.cachedpixels();
}

std::shared_ptr<ImageCache>
imagebuf_imagecache(const ImageBuf& imagebuf)
{
    return imagebuf.imagecache();
}

const uint8_t*
imagebuf_pixeladdr(const ImageBuf& imagebuf, int x, int y, int z, int ch)
{
    return (const uint8_t*)(imagebuf.pixeladdr(x, y, z, ch));
}

uint8_t*
imagebuf_pixeladdr_mut(const ImageBuf& imagebuf, int x, int y, int z, int ch)
{
    return (uint8_t*)(imagebuf.pixeladdr(x, y, z, ch));
}

int
imagebuf_pixelindex(const ImageBuf& imagebuf, int x, int y, int z,
                    bool check_range)
{
    return imagebuf.pixelindex(x, y, z, check_range);
}

bool
imagebuf_merge_metadata(ImageBuf& imagebuf, const ImageBuf& src,
                        bool override_existing, rust::Str pattern,
                        rust::String& error)
{
    // The pattern is handed to std::regex, whose constructor throws
    // regex_error on an invalid expression — std::terminate through this
    // noexcept boundary. Catch and report instead.
    error = rust::String();
    try {
        imagebuf.merge_metadata(src, override_existing,
                                OIIO::string_view(pattern.data(),
                                                  pattern.size()));
        return true;
    } catch (const std::exception& exception) {
        const std::string recorded = exception.what();
        error = rust::String::lossy(
            recorded.empty() ? "the metadata pattern is not a valid regex"
                             : recorded.c_str());
        return false;
    }
}

int
imagebuf_threads(const ImageBuf& imagebuf)
{
    return imagebuf.threads();
}

// OpenImageIO declares threads(int) const over a plain mutable int, so the
// setter compiles against a const reference too; the bridge requires the
// exclusive borrow instead, which is what makes the unsynchronized int sound
// from Rust.
void
imagebuf_set_threads(ImageBuf& imagebuf, int count)
{
    imagebuf.threads(count);
}

void
imagebuf_error(const ImageBuf& imagebuf, rust::Str message)
{
    std::string_view c_message(message.data(), message.size());
    imagebuf.error(c_message);
}

bool
imagebuf_has_error(const ImageBuf& imagebuf)
{
    return imagebuf.has_error();
}

rust::String
imagebuf_geterror(const ImageBuf& imagebuf, bool clear)
{
// OpenImageIO builds its error text from the file: a damaged EXR whose
// attribute name is arbitrary bytes comes back quoted verbatim. cxx's
// throwing rust::String constructor would turn that into std::terminate,
// because the shim it is called from is noexcept. Never assume UTF-8.
    return rust::String::lossy(imagebuf.geterror(clear));
}

bool
imagebuf_deep(const ImageBuf& imagebuf)
{
    return imagebuf.deep();
}

int
imagebuf_deep_samples(const ImageBuf& imagebuf, int x, int y, int z)
{
    return imagebuf.deep_samples(x, y, z);
}

const uint8_t*
imagebuf_deep_pixel_ptr(const ImageBuf& imagebuf, int x, int y, int z, int c,
                        int s)
{
    return (const uint8_t*)(imagebuf.deep_pixel_ptr(x, y, z, c, s));
}

float
imagebuf_deep_value(const ImageBuf& imagebuf, int x, int y, int z, int c, int s)
{
    return imagebuf.deep_value(x, y, z, c, s);
}

uint32_t
imagebuf_deep_value_uint(const ImageBuf& imagebuf, int x, int y, int z, int c,
                         int s)
{
    return imagebuf.deep_value_uint(x, y, z, c, s);
}

// The deep mutators below reach DeepData's deferred sample allocation — an
// unguarded vector resize sized from the recorded sample counts — inside
// shims cxx wraps noexcept, so a count too large for the machine would be
// std::terminate. Report it instead, as the DeepData shims do.
template<typename Mutation>
static bool
guarded_deep_buf_mutation(rust::String& error, Mutation&& mutation)
{
    error = rust::String();
    try {
        mutation();
        return true;
    } catch (const std::exception& exception) {
        const std::string recorded = exception.what();
        error = rust::String::lossy(
            recorded.empty() ? "OpenImageIO could not allocate the deep samples"
                             : recorded.c_str());
        return false;
    }
}

bool
imagebuf_set_deep_samples(ImageBuf& imagebuf, int x, int y, int z, int nsamples,
                          rust::String& error)
{
    return guarded_deep_buf_mutation(
        error, [&] { imagebuf.set_deep_samples(x, y, z, nsamples); });
}

bool
imagebuf_deep_insert_samples(ImageBuf& imagebuf, int x, int y, int z,
                             int samplepos, int nsamples, rust::String& error)
{
    return guarded_deep_buf_mutation(error, [&] {
        imagebuf.deep_insert_samples(x, y, z, samplepos, nsamples);
    });
}

void
imagebuf_deep_erase_samples(ImageBuf& imagebuf, int x, int y, int z,
                            int samplepos, int nsamples)
{
    imagebuf.deep_erase_samples(x, y, z, samplepos, nsamples);
}

bool
imagebuf_set_deep_value(ImageBuf& imagebuf, int x, int y, int z, int c, int s,
                        float value, rust::String& error)
{
    return guarded_deep_buf_mutation(
        error, [&] { imagebuf.set_deep_value(x, y, z, c, s, value); });
}

bool
imagebuf_set_deep_value_uint(ImageBuf& imagebuf, int x, int y, int z, int c,
                             int s, uint32_t value, rust::String& error)
{
    return guarded_deep_buf_mutation(
        error, [&] { imagebuf.set_deep_value(x, y, z, c, s, value); });
}

bool
imagebuf_copy_deep_pixel(ImageBuf& imagebuf, int x, int y, int z,
                         const ImageBuf& src, int srcx, int srcy, int srcz)
{
    // Copying sizes the destination pixel, which can reach the deep storage
    // resize; false already means the copy did not happen.
    try {
        return imagebuf.copy_deep_pixel(x, y, z, src, srcx, srcy, srcz);
    } catch (const std::exception&) {
        return false;
    }
}

DeepData*
imagebuf_deepdata_mut(ImageBuf& imagebuf)
{
    return imagebuf.deepdata();
}

const DeepData*
imagebuf_deepdata(const ImageBuf& imagebuf)
{
    return imagebuf.deepdata();
}

void
imagebuf_lock(const ImageBuf& imagebuf)
{
    imagebuf.lock();
}

void
imagebuf_unlock(const ImageBuf& imagebuf)
{
    imagebuf.unlock();
}

WrapMode
wrapmode_from_string(rust::Str name)
{
    std::string_view c_name(name.data(), name.size());
    return OIIO::ImageBuf::WrapMode_from_string(c_name);
}

#pragma endregion ImageBuf

#pragma region ImageBuf::IteratorBase

int
imagebuf_iterator_x(const ImageBufIteratorBase& iterator)
{
    return iterator.x();
}

int
imagebuf_iterator_y(const ImageBufIteratorBase& iterator)
{
    return iterator.y();
}

int
imagebuf_iterator_z(const ImageBufIteratorBase& iterator)
{
    return iterator.z();
}

bool
imagebuf_iterator_valid(const ImageBufIteratorBase& iterator)
{
    return iterator.valid();
}

bool
imagebuf_iterator_valid_coordianates(const ImageBufIteratorBase& iterator,
                                     int x, int y, int z)
{
    return iterator.valid(x, y, z);
}

bool
imagebuf_iterator_exists(const ImageBufIteratorBase& iterator)
{
    return iterator.exists();
}

bool
imagebuf_iterator_done(const ImageBufIteratorBase& iterator)
{
    return iterator.done();
}

int
imagebuf_iterator_deep_samples(const ImageBufIteratorBase& iterator)
{
    return iterator.deep_samples();
}

WrapMode
imagebuf_iterator_wrap(const ImageBufIteratorBase& iterator)
{
    return iterator.wrap();
}

void
imagebuf_iterator_pos(ImageBufIteratorBase& iterator, int x, int y, int z)
{
    iterator.pos(x, y, z);
}

void
imagebuf_iterator_next(ImageBufIteratorBase& iterator)
{
    iterator++;
}

ROI
imagebuf_iterator_range(const ImageBufIteratorBase& iterator)
{
    return iterator.range();
}

void
imagebuf_iterator_rerange(ImageBufIteratorBase& iterator, int xbegin, int xend,
                          int ybegin, int yend, int zbegin, int zend,
                          WrapMode wrap)
{
    iterator.rerange(xbegin, xend, ybegin, yend, zbegin, zend, wrap);
}

const uint8_t*
imagebuf_iterator_rawptr(const ImageBufIteratorBase& iterator)
{
    return (const uint8_t*)(iterator.rawptr());
}

float
imagebuf_iterator_deep_value(const ImageBufIteratorBase& iterator, int c, int s)
{
    return iterator.deep_value(c, s);
}

uint32_t
imagebuf_iterator_deep_value_uint(const ImageBufIteratorBase& iterator, int c,
                                  int s)
{
    return iterator.deep_value_uint(c, s);
}

bool
imagebuf_iterator_localpixels(const ImageBufIteratorBase& iterator)
{
    return iterator.localpixels();
}

bool
imagebuf_iterator_has_error(const ImageBufIteratorBase& iterator)
{
    return iterator.has_error();
}

void
imagebuf_iterator_clear_error(ImageBufIteratorBase& iterator)
{
    iterator.clear_error();
}
#pragma endregion ImageBuf::IteratorBase
}  // namespace oiio