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
#include "ffi_imagecache.h"
#include "ffi_pixel.h"

namespace oiio {
namespace {
rust::String
take_cache_error(ImageCache& imagecache)
{
    return rust::String::lossy(imagecache.geterror(true));
}
}  // namespace

std::shared_ptr<ImageCache>
imagecache_create(bool shared)
{
    return OIIO::ImageCache::create(shared);
}

void
imagecache_destroy(std::shared_ptr<ImageCache> imagecache, bool teardown)
{
    OIIO::ImageCache::destroy(imagecache, teardown);
}

bool
imagecache_attribute(ImageCache& imagecache, rust::Str name, TypeDesc type,
                     const uint8_t* val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.attribute(c_name, type, val);
}

bool
imagecache_attribute_int(ImageCache& imagecache, rust::Str name, int val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.attribute(c_name, val);
}

bool
imagecache_attribute_int_with_error(ImageCache& imagecache, rust::Str name,
                                    int val, rust::String& error)
{
    error = rust::String();
    if (imagecache_attribute_int(imagecache, name, val))
        return true;
    error = take_cache_error(imagecache);
    return false;
}

bool
imagecache_attribute_float(ImageCache& imagecache, rust::Str name, float val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.attribute(c_name, val);
}

bool
imagecache_attribute_float_with_error(ImageCache& imagecache, rust::Str name,
                                      float val, rust::String& error)
{
    error = rust::String();
    if (imagecache_attribute_float(imagecache, name, val))
        return true;
    error = take_cache_error(imagecache);
    return false;
}

bool
imagecache_attribute_double(ImageCache& imagecache, rust::Str name, double val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.attribute(c_name, val);
}

bool
imagecache_attribute_str(ImageCache& imagecache, rust::Str name, rust::Str val)
{
    std::string_view c_name(name.data(), name.size());
    std::string_view c_val(val.data(), val.size());
    return imagecache.attribute(c_name, c_val);
}

bool
imagecache_getattribute(ImageCache& imagecache, rust::Str name, TypeDesc type,
                        const uint8_t* val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattribute(c_name, type, (void*)val);
}

bool
imagecache_getattribute_int(ImageCache& imagecache, rust::Str name, int& val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattribute(c_name, val);
}

bool
imagecache_getattribute_float(ImageCache& imagecache, rust::Str name,
                              float& val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattribute(c_name, val);
}

bool
imagecache_getattribute_double(ImageCache& imagecache, rust::Str name,
                               double& val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattribute(c_name, val);
}

bool
imagecache_getattribute_string(ImageCache& imagecache, rust::Str name,
                               std::string& val)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattribute(c_name, val);
}

TypeDesc
imagecache_getattributetype(ImageCache& imagecache, rust::Str name)
{
    std::string_view c_name(name.data(), name.size());
    return imagecache.getattributetype(c_name);
}

Perthread*
imagecache_get_perthread_info(ImageCache& imagecache, Perthread* thread_info)
{
    return imagecache.get_perthread_info(thread_info);
}

Perthread*
imagecache_create_thread_info(ImageCache& imagecache)
{
    return imagecache.create_thread_info();
}

void
imagecache_destroy_thread_info(ImageCache& imagecache, Perthread* thread_info)
{
    imagecache.destroy_thread_info(thread_info);
}

ImageHandle*
imagecache_get_image_handle(ImageCache& imagecache, rust::Str filename,
                            Perthread* thread_info, const TextureOpt* options)
{
    // A malformed UDIM-like name must be stopped before find_file: the
    // regex throw inside the ImageCacheFile constructor leaks a manually
    // held file-cache bin lock (see ffi_pixel.h), so catching alone would
    // leave later queries deadlocking.
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size())))
        return nullptr;
    OIIO::ustring c_filename(filename.data(), filename.size());
    // Regex barrier, belt and braces.
    try {
        return imagecache.get_image_handle(c_filename, thread_info, options);
    } catch (const std::exception&) {
        return nullptr;
    }
}

bool
imagecache_good(ImageCache& imagecache, ImageHandle* file)
{
    return imagecache.good(file);
}

rust::String
imagecache_filename_from_handle(ImageCache& imagecache, ImageHandle* handle)
{
    return rust::String::lossy(imagecache.filename_from_handle(handle).c_str());
}

bool
imagecache_get_image_info(ImageCache& imagecache, rust::Str filename,
                          int subimage, int miplevel, rust::Str dataname,
                          TypeDesc datatype, uint8_t* data)
{
    // Stopped before find_file — the regex throw inside the ImageCacheFile
    // constructor leaks a manually held bin lock (see ffi_pixel.h) — with
    // the catch below as belt and braces.
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size())))
        return false;
    OIIO::ustring c_filename(filename.data(), filename.size());
    OIIO::ustring c_dataname(dataname.data(), dataname.size());
    try {
        return imagecache.get_image_info(c_filename, subimage, miplevel,
                                         c_dataname, datatype, (void*)data);
    } catch (const std::exception&) {
        return false;
    }
}

bool
imagecache_get_image_info_with_handle(ImageCache& imagecache, ImageHandle* file,
                                      Perthread* thread_info, int subimage,
                                      int miplevel, rust::Str dataname,
                                      TypeDesc datatype, uint8_t* data)
{
    OIIO::ustring c_dataname(dataname.data(), dataname.size());
    return imagecache.get_image_info(file, thread_info, subimage, miplevel,
                                     c_dataname, datatype, (void*)data);
}

bool
imagecache_get_imagespec(ImageCache& imagecache, rust::Str filename,
                         ImageSpec& spec, int subimage, int miplevel,
                         bool native)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.get_imagespec(c_filename, spec, subimage, miplevel,
                                    native);
}

std::unique_ptr<ImageSpec>
imagecache_get_imagespec_copy(ImageCache& imagecache, rust::Str filename,
                              int subimage)
{
    auto spec = std::make_unique<ImageSpec>();
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size())))
        return {};
    OIIO::ustring c_filename(filename.data(), filename.size());
    // Regex barrier: see the note in imagecache_get_image_info.
    try {
        if (!imagecache.get_imagespec(c_filename, *spec, subimage))
            return {};
    } catch (const std::exception&) {
        return {};
    }
    return spec;
}

std::unique_ptr<ImageSpec>
imagecache_get_imagespec_copy_with_error(ImageCache& imagecache,
                                         rust::Str filename, int subimage,
                                         rust::String& error)
{
    error = rust::String();
    auto spec = imagecache_get_imagespec_copy(imagecache, filename, subimage);
    if (!spec)
        error = take_cache_error(imagecache);
    return spec;
}

std::unique_ptr<ImageSpec>
imagecache_get_cache_dimensions_copy(ImageCache& imagecache,
                                     rust::Str filename, int subimage,
                                     int miplevel)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    ImageSpec native_spec;
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size())))
        return {};
    // Regex barrier: see the note in imagecache_get_image_info.
    try {
        if (!imagecache.get_imagespec(c_filename, native_spec, subimage))
            return {};

        // get_cache_dimensions only overwrites the compact ImageDims prefix.
        // Seed the object so the untouched format and semantic fields stay
        // valid.
        auto spec = std::make_unique<ImageSpec>(native_spec);
        if (!imagecache.get_cache_dimensions(c_filename, *spec, subimage,
                                             miplevel))
            return {};
        return spec;
    } catch (const std::exception&) {
        return {};
    }
}

std::unique_ptr<ImageSpec>
imagecache_get_image_spec_at_copy_with_error(ImageCache& imagecache,
                                             rust::Str filename, int subimage,
                                             int miplevel,
                                             rust::String& error)
{
    error = rust::String();
    ImageSpec native_spec;
    OIIO::ustring c_filename(filename.data(), filename.size());
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size()))) {
        error = rust::String::lossy(
            "the UDIM-like name cannot form a valid tile pattern");
        return {};
    }
    // Regex barrier: see the note in imagecache_get_image_info.
    bool spec_ok = false;
    try {
        spec_ok = imagecache.get_imagespec(c_filename, native_spec, subimage);
    } catch (const std::exception& thrown) {
        error = rust::String::lossy(thrown.what());
        return {};
    }
    if (!spec_ok) {
        error = take_cache_error(imagecache);
        return {};
    }

    // Start with the native spec because get_cache_dimensions overwrites
    // only the compact, mip-varying ImageDims prefix. This retains the
    // native format, channel names, deep flag, and semantic channel indices.
    auto cache_spec = std::make_unique<ImageSpec>(native_spec);
    if (!imagecache.get_cache_dimensions(c_filename, *cache_spec, subimage,
                                         miplevel)) {
        error = take_cache_error(imagecache);
        return {};
    }
    return cache_spec;
}

bool
imagecache_get_imagespec_with_handle(ImageCache& imagecache, ImageHandle* file,
                                     Perthread* thread_info, ImageSpec& spec,
                                     int subimage, int miplevel, bool native)
{
    return imagecache.get_imagespec(file, thread_info, spec, subimage, miplevel,
                                    native);
}

const ImageSpec*
imagecache_imagespec(ImageCache& imagecache, rust::Str filename, int subimage,
                     int miplevel, bool native)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.imagespec(c_filename, subimage, miplevel, native);
}

const ImageSpec*
imagecache_imagespec_with_handle(ImageCache& imagecache, ImageHandle* file,
                                 Perthread* thread_info, int subimage,
                                 int miplevel, bool native)
{
    return imagecache.imagespec(file, thread_info, subimage, miplevel, native);
}

bool
imagecache_get_thumbnail(ImageCache& imagecache, rust::Str filename,
                         ImageBuf& thumbnail, int subimage)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size())))
        return false;
    // Regex barrier: see the note in imagecache_get_image_info.
    try {
        return imagecache.get_thumbnail(c_filename, thumbnail, subimage);
    } catch (const std::exception&) {
        return false;
    }
}

bool
imagecache_get_thumbnail_with_handle(ImageCache& imagecache, ImageHandle* file,
                                     Perthread* thread_info,
                                     ImageBuf& thumbnail, int subimage)
{
    return imagecache.get_thumbnail(file, thread_info, thumbnail, subimage);
}

bool
imagecache_get_pixels(ImageCache& imagecache, rust::Str filename, int subimage,
                      int miplevel, int xbegin, int xend, int ybegin, int yend,
                      int zbegin, int zend, int chbegin, int chend,
                      TypeDesc format, const uint8_t* result, int64_t xstride,
                      int64_t ystride, int64_t zstride, int cache_chbegin,
                      int cache_chend)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.get_pixels(c_filename, subimage, miplevel, xbegin, xend,
                                 ybegin, yend, zbegin, zend, chbegin, chend,
                                 format, (void*)result, xstride, ystride,
                                 zstride, cache_chbegin, cache_chend);
}

bool
imagecache_get_pixels_span(ImageCache& imagecache, rust::Str filename,
                           int subimage, int miplevel, const ROI& roi,
                           TypeDesc format, rust::Slice<uint8_t> result)
{
    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;

    detail::PixelLayout layout;
    if (!detail::bounded_pixel_layout(channels, width, height, depth, format,
                                      result.size(), layout))
        return false;

    OIIO::ustring c_filename(filename.data(), filename.size());
    const auto output = detail::writable_byte_span(result, layout);
    return imagecache.get_pixels(c_filename, subimage, miplevel, roi, format,
                                 output);
}

bool
imagecache_get_pixels_span_with_error(
    ImageCache& imagecache, rust::Str filename, int subimage, int miplevel,
    const ROI& roi, TypeDesc format, rust::Slice<uint8_t> result,
    rust::String& error)
{
    error = rust::String();

    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;
    detail::PixelLayout layout;
    if (!detail::bounded_pixel_layout(channels, width, height, depth, format,
                                      result.size(), layout)) {
        error = rust::String::lossy(
            "invalid pixel layout or destination buffer byte length");
        return false;
    }

    if (detail::malformed_udim_pattern(
            OIIO::string_view(filename.data(), filename.size()))) {
        error = rust::String::lossy(
            "the UDIM-like name cannot form a valid tile pattern");
        return false;
    }
    // Regex barrier: see the note in imagecache_get_image_info.
    try {
        if (imagecache_get_pixels_span(imagecache, filename, subimage,
                                       miplevel, roi, format, result))
            return true;
    } catch (const std::exception& thrown) {
        error = rust::String::lossy(thrown.what());
        return false;
    }
    error = take_cache_error(imagecache);
    return false;
}

int
imagecache_handle_is_deep(ImageCache& imagecache, ImageHandle* file,
                          Perthread* thread_info, int subimage)
{
    if (file == nullptr)
        return -1;
    if (thread_info != nullptr)
        thread_info = imagecache.get_perthread_info(thread_info);
    ImageSpec spec;
    if (!imagecache.get_imagespec(file, thread_info, spec, subimage)) {
        // Drain, so the failure is not left pending on the cache for an
        // unrelated call to pick up.
        (void)imagecache.geterror(true);
        return -1;
    }
    return spec.deep ? 1 : 0;
}

bool
imagecache_get_pixels_handle_span_with_error(
    ImageCache& imagecache, ImageHandle* file, Perthread* thread_info,
    int subimage, int miplevel, const ROI& roi, TypeDesc format,
    rust::Slice<uint8_t> result, rust::String& error)
{
    error = rust::String();
    if (file == nullptr) {
        error = rust::String::lossy("null image handle");
        return false;
    }

    // OpenImageIO's header is explicit that a caller-managed Perthread must be
    // passed back through get_perthread_info before each use, because that is
    // where the cache does its housekeeping: an invalidate sets a purge flag on
    // the record, and nothing acts on it until this call. Skipping it leaves
    // the two-tile microcache holding pre-invalidation tiles, which is stale
    // data, and becomes a heap over-read if the replacement file is tiled more
    // coarsely, since the old buffer gets indexed with the new dimensions.
    if (thread_info != nullptr)
        thread_info = imagecache.get_perthread_info(thread_info);

    // The by-name read validates against the spec before crossing over; the
    // handle read used to skip it entirely, so a channel range outside the
    // image reached ImageCacheTile::data, which returns NULL out of range and
    // is dereferenced without a check. Validate here rather than in Rust: the
    // handle is already resolved, so this costs no second name lookup.
    ImageSpec spec;
    if (!imagecache.get_imagespec(file, thread_info, spec, subimage)) {
        error = take_cache_error(imagecache);
        if (error.empty())
            error = rust::String::lossy(
                "the image cache could not describe the image");
        return false;
    }
    if (spec.deep) {
        error = rust::String::lossy(
            "the image cache cannot read flat pixels from a deep image");
        return false;
    }
    if (roi.chbegin < 0 || roi.chend > spec.nchannels) {
        error = rust::String::lossy(
            OIIO::Strutil::fmt::format("channel range {}..{} extends outside "
                                       "the image's {} channels",
                                       roi.chbegin, roi.chend,
                                       spec.nchannels));
        return false;
    }

    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;

    detail::PixelLayout layout;
    if (!detail::bounded_pixel_layout(channels, width, height, depth, format,
                                      result.size(), layout)) {
        error = rust::String::lossy(
            "invalid pixel layout or destination buffer byte length");
        return false;
    }

    const auto output = detail::writable_byte_span(result, layout);
    if (imagecache.get_pixels(file, thread_info, subimage, miplevel, roi,
                              format, output))
        return true;
    error = take_cache_error(imagecache);
    return false;
}

bool
imagecache_get_pixels_with_handle(ImageCache& imagecache, ImageHandle* file,
                                  Perthread* thread_info, int subimage,
                                  int miplevel, int xbegin, int xend,
                                  int ybegin, int yend, int zbegin, int zend,
                                  int chbegin, int chend, TypeDesc format,
                                  const uint8_t* result, int64_t xstride,
                                  int64_t ystride, int64_t zstride,
                                  int cache_chbegin, int cache_chend)
{
    return imagecache.get_pixels(file, thread_info, subimage, miplevel, xbegin,
                                 xend, ybegin, yend, zbegin, zend, chbegin,
                                 chend, format, (void*)result, xstride, ystride,
                                 zstride, cache_chbegin, cache_chend);
}

bool
imagecache_get_pixels_all_channels(ImageCache& imagecache, rust::Str filename,
                                   int subimage, int miplevel, int xbegin,
                                   int xend, int ybegin, int yend, int zbegin,
                                   int zend, TypeDesc format,
                                   const uint8_t* result)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.get_pixels(c_filename, subimage, miplevel, xbegin, xend,
                                 ybegin, yend, zbegin, zend, format,
                                 (void*)result);
}

bool
imagecache_get_pixels_all_channels_with_handle(
    ImageCache& imagecache, ImageHandle* file, Perthread* thread_info,
    int subimage, int miplevel, int xbegin, int xend, int ybegin, int yend,
    int zbegin, int zend, TypeDesc format, const uint8_t* result)
{
    return imagecache.get_pixels(file, thread_info, subimage, miplevel, xbegin,
                                 xend, ybegin, yend, zbegin, zend, format,
                                 (void*)result);
}

void
imagecache_invalidate(ImageCache& imagecache, rust::Str filename, bool force)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    imagecache.invalidate(c_filename, force);
}

void
imagecache_invalidate_with_handle(ImageCache& imagecache, ImageHandle* file,
                                  bool force)
{
    imagecache.invalidate(file, force);
}

void
imagecache_invalidate_all(ImageCache& imagecache, bool force)
{
    imagecache.invalidate_all(force);
}

void
imagecache_close(ImageCache& imagecache, rust::Str filename)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    imagecache.close(c_filename);
}

void
imagecache_close_all(ImageCache& imagecache)
{
    imagecache.close_all();
}


Tile*
imagecache_get_tile(ImageCache& imagecache, rust::Str filename, int subimage,
                    int miplevel, int x, int y, int z, int chbegin, int chend)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.get_tile(c_filename, subimage, miplevel, x, y, z, chbegin,
                               chend);
}

Tile*
imagecache_get_tile_with_handle(ImageCache& imagecache, ImageHandle* file,
                                Perthread* thread_info, int subimage,
                                int miplevel, int x, int y, int z, int chbegin,
                                int chend)
{
    return imagecache.get_tile(file, thread_info, subimage, miplevel, x, y, z,
                               chbegin, chend);
}

void
imagecache_release_tile(ImageCache& imagecache, Tile* tile)
{
    imagecache.release_tile(tile);
}

TypeDesc
imagecache_tile_format(ImageCache& imagecache, const Tile* tile)
{
    return imagecache.tile_format(tile);
}

ROI
imagecache_tile_roi(ImageCache& imagecache, const Tile* tile)
{
    return imagecache.tile_roi(tile);
}

const uint8_t*
imagecache_tile_pixels(ImageCache& imagecache, Tile* tile, TypeDesc& format)
{
    return (uint8_t*)(imagecache.tile_pixels(tile, format));
}

bool
imagecache_add_file(ImageCache& imagecache, rust::Str filename,
                    OIIO::ImageInput::Creator creator, const ImageSpec* config,
                    bool replace)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.add_file(c_filename, creator, config, replace);
}

bool
imagecache_add_tile_from_coordinates(ImageCache& imagecache, rust::Str filename,
                                     int subimage, int miplevel, int x, int y,
                                     int z, int chbegin, int chend,
                                     TypeDesc format, const uint8_t* buffer,
                                     int64_t xstride, int64_t ystride,
                                     int64_t zstride, bool copy)
{
    OIIO::ustring c_filename(filename.data(), filename.size());
    return imagecache.add_tile(c_filename, subimage, miplevel, x, y, z, chbegin,
                               chend, format, (void*)buffer, xstride, ystride,
                               zstride, copy);
}

bool
imagecache_has_error(ImageCache& imagecache)
{
    return imagecache.has_error();
}

rust::String
imagecache_geterror(ImageCache& imagecache, bool clear)
{
    return rust::String::lossy(imagecache.geterror(clear).c_str());
}

rust::String
imagecache_getstats(ImageCache& imagecache, int level)
{
    return rust::String::lossy(imagecache.getstats(level).c_str());
}

void
imagecache_reset_stats(ImageCache& imagecache)
{
    imagecache.reset_stats();
}
}  // namespace oiio