-
Notifications
You must be signed in to change notification settings - Fork 663
Expand file tree
/
Copy pathimagebuf.cpp
More file actions
3503 lines (2863 loc) · 102 KB
/
imagebuf.cpp
File metadata and controls
3503 lines (2863 loc) · 102 KB
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
// Copyright Contributors to the OpenImageIO project.
// SPDX-License-Identifier: BSD-3-Clause and Apache-2.0
// https://github.com/AcademySoftwareFoundation/OpenImageIO
#include <iostream>
#include <memory>
#include <OpenImageIO/half.h>
#include <OpenImageIO/dassert.h>
#include <OpenImageIO/deepdata.h>
#include <OpenImageIO/fmath.h>
#include <OpenImageIO/imagebuf.h>
#include <OpenImageIO/imagebufalgo.h>
#include <OpenImageIO/imagebufalgo_util.h>
#include <OpenImageIO/imagecache.h>
#include <OpenImageIO/imageio.h>
#include <OpenImageIO/simd.h>
#include <OpenImageIO/strongparam.h>
#include <OpenImageIO/strutil.h>
#include <OpenImageIO/thread.h>
#include "imageio_pvt.h"
OIIO_NAMESPACE_BEGIN
OIIO_STRONG_PARAM_TYPE(DoLock, bool);
namespace pvt {
int imagebuf_print_uncaught_errors(1);
int imagebuf_use_imagecache(0);
atomic_ll IB_local_mem_current;
atomic_ll IB_local_mem_peak;
std::atomic<float> IB_total_open_time(0.0f);
std::atomic<float> IB_total_image_read_time(0.0f);
} // namespace pvt
ROI
get_roi(const ImageSpec& spec)
{
return ROI(spec.x, spec.x + spec.width, spec.y, spec.y + spec.height,
spec.z, spec.z + spec.depth, 0, spec.nchannels);
}
ROI
get_roi_full(const ImageSpec& spec)
{
return ROI(spec.full_x, spec.full_x + spec.full_width, spec.full_y,
spec.full_y + spec.full_height, spec.full_z,
spec.full_z + spec.full_depth, 0, spec.nchannels);
}
void
set_roi(ImageSpec& spec, const ROI& newroi)
{
spec.x = newroi.xbegin;
spec.y = newroi.ybegin;
spec.z = newroi.zbegin;
spec.width = newroi.width();
spec.height = newroi.height();
spec.depth = newroi.depth();
}
void
set_roi_full(ImageSpec& spec, const ROI& newroi)
{
spec.full_x = newroi.xbegin;
spec.full_y = newroi.ybegin;
spec.full_z = newroi.zbegin;
spec.full_width = newroi.width();
spec.full_height = newroi.height();
spec.full_depth = newroi.depth();
}
span<std::byte>
span_from_buffer(void* data, TypeDesc format, int nchannels, int width,
int height, int depth, stride_t xstride, stride_t ystride,
stride_t zstride)
{
ImageSpec::auto_stride(xstride, ystride, zstride, format.size(), nchannels,
width, height);
// Need to figure out the span based on the origin and strides.
// Start with the span range of one pixel.
std::byte* bufstart = (std::byte*)data;
std::byte* bufend = bufstart + format.size() * nchannels;
// Expand to the span range for one row. Remember negative strides!
if (xstride >= 0) {
bufend += xstride * (width - 1);
} else {
bufstart -= xstride * (width - 1);
}
// Expand to the span range for a whole image plane.
if (ystride >= 0) {
bufend += ystride * (height - 1);
} else {
bufstart -= ystride * (height - 1);
}
// Expand to the span range for a whole volume.
if (depth > 1 && zstride != 0) {
if (zstride >= 0) {
bufend += zstride * (depth - 1);
} else {
bufstart -= zstride * (depth - 1);
}
}
return { bufstart, size_t(bufend - bufstart) };
}
cspan<std::byte>
cspan_from_buffer(const void* data, TypeDesc format, int nchannels, int width,
int height, int depth, stride_t xstride, stride_t ystride,
stride_t zstride)
{
auto s = span_from_buffer(const_cast<void*>(data), format, nchannels, width,
height, depth, xstride, ystride, zstride);
return { s.data(), s.size() };
}
// Expansion of the opaque type that hides all the ImageBuf implementation
// detail.
class ImageBufImpl {
public:
ImageBufImpl(string_view filename, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache = {},
const ImageSpec* spec = nullptr, span<std::byte> bufspan = {},
const void* buforigin = nullptr, bool readonly = false,
const ImageSpec* config = nullptr,
Filesystem::IOProxy* ioproxy = nullptr,
stride_t xstride = AutoStride, stride_t ystride = AutoStride,
stride_t zstride = AutoStride);
ImageBufImpl(const ImageBufImpl& src);
~ImageBufImpl();
void clear();
void reset(string_view name, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache, const ImageSpec* config,
Filesystem::IOProxy* ioproxy);
// Reset the buf to blank, given the spec. If nativespec is also
// supplied, use it for the "native" spec, otherwise make the nativespec
// just copy the regular spec.
void reset(string_view name, const ImageSpec& spec,
const ImageSpec* nativespec = nullptr,
span<std::byte> bufspan = {}, const void* buforigin = {},
bool readonly = false, stride_t xstride = AutoStride,
stride_t ystride = AutoStride, stride_t zstride = AutoStride);
void alloc(const ImageSpec& spec, const ImageSpec* nativespec = nullptr);
void realloc();
bool init_spec(string_view filename, int subimage, int miplevel,
DoLock do_lock);
bool read(int subimage, int miplevel, int chbegin = 0, int chend = -1,
bool force = false, TypeDesc convert = TypeDesc::UNKNOWN,
ProgressCallback progress_callback = nullptr,
void* progress_callback_data = nullptr,
DoLock do_lock = DoLock(true));
void copy_metadata(const ImageBufImpl& src);
// At least one of bufspan or buforigin is supplied. Set this->m_bufspan
// and this->m_localpixels appropriately.
void set_bufspan_localpixels(span<std::byte> bufspan,
const void* buforigin);
// Note: Uses std::format syntax
template<typename... Args>
void error(const char* fmt, const Args&... args) const
{
error(Strutil::fmt::format(fmt, args...));
}
void error(string_view message) const;
ImageBuf::IBStorage storage() const { return m_storage; }
TypeDesc pixeltype() const
{
validate_spec();
return m_localpixels ? m_spec.format : m_cachedpixeltype;
}
DeepData* deepdata()
{
validate_pixels();
return m_spec.deep ? &m_deepdata : NULL;
}
const DeepData* deepdata() const
{
validate_pixels();
return m_spec.deep ? &m_deepdata : NULL;
}
bool initialized() const
{
return m_spec_valid && m_storage != ImageBuf::UNINITIALIZED;
}
bool cachedpixels() const { return m_storage == ImageBuf::IMAGECACHE; }
const void* pixeladdr(int x, int y, int z, int ch) const;
void* pixeladdr(int x, int y, int z, int ch);
const void* retile(int x, int y, int z, ImageCache::Tile*& tile,
int& tilexbegin, int& tileybegin, int& tilezbegin,
int& tilexend, bool& haderror, bool exists,
ImageBuf::WrapMode wrap) const;
bool do_wrap(int& x, int& y, int& z, ImageBuf::WrapMode wrap) const;
const void* blackpixel() const
{
validate_spec();
return &m_blackpixel[0];
}
bool validate_spec(DoLock do_lock = DoLock(true)) const
{
if (m_spec_valid)
return true;
if (!m_name.size())
return false;
lock_t lock(m_mutex, std::defer_lock_t());
if (do_lock)
lock.lock();
if (m_spec_valid)
return true;
ImageBufImpl* imp = const_cast<ImageBufImpl*>(this);
if (imp->m_current_subimage < 0)
imp->m_current_subimage = 0;
if (imp->m_current_miplevel < 0)
imp->m_current_miplevel = 0;
return imp->init_spec(m_name, m_current_subimage, m_current_miplevel,
DoLock(false) /* we already hold the lock */);
}
bool validate_pixels(DoLock do_lock = DoLock(true)) const
{
if (m_pixels_valid)
return true;
if (!m_name.size())
return true;
lock_t lock(m_mutex, std::defer_lock_t());
if (do_lock)
lock.lock();
if (m_pixels_valid)
return true;
ImageBufImpl* imp = const_cast<ImageBufImpl*>(this);
if (imp->m_current_subimage < 0)
imp->m_current_subimage = 0;
if (imp->m_current_miplevel < 0)
imp->m_current_miplevel = 0;
return imp->read(m_current_subimage, m_current_miplevel,
DoLock(false) /* we already hold the lock */);
}
const ImageSpec& spec() const
{
validate_spec();
return m_spec;
}
TypeDesc file_format() const
{
//! TODO: remove m_nativespec and store only the "native format"
return m_nativespec.format;
}
TypeDesc& file_format()
{
//! TODO: remove m_nativespec and store only the "native format"
return m_nativespec.format;
}
std::vector<TypeDesc> file_channelformats() const
{
//! TODO: remove m_nativespec and store only the "native channels format"
return m_nativespec.channelformats;
}
std::vector<TypeDesc>& file_channelformats()
{
//! TODO: remove m_nativespec and store only the "native channels format"
return m_nativespec.channelformats;
}
/// DEPRECTATED
/// TODO: uncomment for backwards compatibility once everything else is in place.
// const ImageSpec& nativespec() const
// {
// validate_spec();
// return m_nativespec;
// }
ImageSpec& specmod()
{
validate_spec();
return m_spec;
}
void threads(int n) const { m_threads = n; }
int threads() const { return m_threads; }
// Allocate m_configspec if not already done
void add_configspec(const ImageSpec* config = NULL)
{
if (!m_configspec)
m_configspec.reset(config ? new ImageSpec(*config) : new ImageSpec);
}
// Return the index of pixel (x,y,z). If check_range is true, return
// -1 for an invalid coordinate that is not within the data window.
int pixelindex(int x, int y, int z, bool check_range = false) const
{
x -= m_spec.x;
y -= m_spec.y;
z -= m_spec.z;
if (check_range
&& (x < 0 || x >= m_spec.width || y < 0 || y >= m_spec.height
|| z < 0 || z >= m_spec.depth))
return -1;
return (z * m_spec.height + y) * m_spec.width + x;
}
// Invalidate the file in our imagecache and the shared one
void invalidate(ustring filename, bool force)
{
auto shared_imagecache = ImageCache::create(true);
OIIO_DASSERT(shared_imagecache);
if (m_imagecache)
m_imagecache->invalidate(filename, force); // *our* IC
if (m_imagecache != shared_imagecache)
shared_imagecache->invalidate(filename, force); // the shared IC
}
void eval_contiguous()
{
m_contiguous = m_localpixels && m_storage == ImageBuf::LOCALBUFFER
&& m_xstride == m_spec.nchannels * m_channel_stride
&& m_ystride == m_xstride * m_spec.width
&& m_zstride == m_ystride * m_spec.height;
}
bool has_thumbnail(DoLock do_lock = DoLock(true)) const;
void clear_thumbnail(DoLock do_lock = DoLock(true));
void set_thumbnail(const ImageBuf& thumb, DoLock do_lock = DoLock(true));
std::shared_ptr<ImageBuf> get_thumbnail(DoLock do_lock = DoLock(true)) const;
private:
ImageBuf::IBStorage m_storage; ///< Pixel storage class
ustring m_name; ///< Filename of the image
ustring m_fileformat; ///< File format name
int m_nsubimages; ///< How many subimages are there?
int m_current_subimage; ///< Current subimage we're viewing
int m_current_miplevel; ///< Current miplevel we're viewing
int m_nmiplevels; ///< # of MIP levels in the current subimage
mutable int m_threads; ///< thread policy for this image
ImageSpec m_spec; ///< Describes the image (size, etc)
//! TODO: remove m_nativespec
ImageSpec m_nativespec; ///< Describes the true native image
std::unique_ptr<char[]> m_pixels; ///< Pixel data, if local and we own it
char* m_localpixels; ///< Pointer to local pixels
span<std::byte> m_bufspan; ///< Bounded buffer for local pixels
typedef std::recursive_mutex mutex_t;
typedef std::unique_lock<mutex_t> lock_t;
mutable mutex_t m_mutex; ///< Thread safety for this ImageBuf
mutable bool m_spec_valid; ///< Is the spec valid
mutable bool m_pixels_valid; ///< Image is valid
bool m_readonly; ///< The bufspan is read-only
bool m_badfile; ///< File not found
float m_pixelaspect; ///< Pixel aspect ratio of the image
stride_t m_xstride;
stride_t m_ystride;
stride_t m_zstride;
stride_t m_channel_stride;
bool m_contiguous;
std::shared_ptr<ImageCache> m_imagecache; ///< ImageCache to use
TypeDesc m_cachedpixeltype; ///< Data type stored in the cache
DeepData m_deepdata; ///< Deep data
size_t m_allocated_size; ///< How much memory we've allocated
std::vector<char> m_blackpixel; ///< Pixel-sized zero bytes
std::vector<TypeDesc> m_write_format; /// Pixel data format to use for write()
int m_write_tile_width;
int m_write_tile_height;
int m_write_tile_depth;
std::unique_ptr<ImageSpec> m_configspec; // Configuration spec
Filesystem::IOProxy* m_rioproxy = nullptr;
Filesystem::IOProxy* m_wioproxy = nullptr;
mutable std::string m_err; ///< Last error message
bool m_has_thumbnail = false;
std::shared_ptr<ImageBuf> m_thumbnail;
// Private reset m_pixels to new allocation of new size, copy if
// data is not nullptr. Return nullptr if an allocation of that size
// was not possible.
char* new_pixels(size_t size, const void* data = nullptr);
// Private release of m_pixels.
void free_pixels();
TypeDesc write_format(int channel = 0) const
{
if (size_t(channel) < m_write_format.size())
return m_write_format[channel];
if (m_write_format.size() == 1)
return m_write_format[0];
return file_format();
}
void lock() const { m_mutex.lock(); }
void unlock() const { m_mutex.unlock(); }
const ImageBufImpl operator=(const ImageBufImpl& src); // unimplemented
friend class ImageBuf;
};
void
ImageBuf::impl_deleter(ImageBufImpl* todel)
{
delete todel;
}
ImageBufImpl::ImageBufImpl(string_view filename, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache,
const ImageSpec* spec, span<std::byte> bufspan,
const void* buforigin, bool readonly,
const ImageSpec* config,
Filesystem::IOProxy* ioproxy, stride_t xstride,
stride_t ystride, stride_t zstride)
: m_storage(ImageBuf::UNINITIALIZED)
, m_name(filename)
, m_nsubimages(0)
, m_current_subimage(subimage)
, m_current_miplevel(miplevel)
, m_nmiplevels(0)
, m_threads(0)
, m_localpixels(NULL)
, m_spec_valid(false)
, m_pixels_valid(false)
, m_readonly(readonly)
, m_badfile(false)
, m_pixelaspect(1)
, m_xstride(0)
, m_ystride(0)
, m_zstride(0)
, m_channel_stride(0)
, m_contiguous(false)
, m_imagecache(imagecache)
, m_allocated_size(0)
, m_write_tile_width(0)
, m_write_tile_height(0)
, m_write_tile_depth(1)
{
if (spec) {
// spec != nullptr means we're constructing an ImageBuf that either
// wraps a buffer or owns its own memory.
m_spec = *spec;
m_nativespec = *spec;
m_channel_stride = stride_t(spec->format.size());
m_xstride = xstride;
m_ystride = ystride;
m_zstride = zstride;
ImageSpec::auto_stride(m_xstride, m_ystride, m_zstride, m_spec.format,
m_spec.nchannels, m_spec.width, m_spec.height);
m_blackpixel.resize(round_to_multiple(spec->pixel_bytes(),
OIIO_SIMD_MAX_SIZE_BYTES),
0);
// NB make it big enough for SSE
if (buforigin || bufspan.size()) {
set_bufspan_localpixels(bufspan, buforigin);
m_storage = ImageBuf::APPBUFFER;
m_pixels_valid = true;
} else {
m_storage = ImageBuf::LOCALBUFFER;
}
m_spec_valid = true;
} else if (filename.length() > 0) {
// filename being nonempty means this ImageBuf refers to a file.
OIIO_DASSERT(buforigin == nullptr);
OIIO_DASSERT(bufspan.empty());
reset(filename, subimage, miplevel, std::move(imagecache), config,
ioproxy);
} else {
OIIO_DASSERT(buforigin == nullptr);
OIIO_DASSERT(bufspan.empty());
}
eval_contiguous();
}
ImageBufImpl::ImageBufImpl(const ImageBufImpl& src)
: m_storage(src.m_storage)
, m_name(src.m_name)
, m_fileformat(src.m_fileformat)
, m_nsubimages(src.m_nsubimages)
, m_current_subimage(src.m_current_subimage)
, m_current_miplevel(src.m_current_miplevel)
, m_nmiplevels(src.m_nmiplevels)
, m_threads(src.m_threads)
, m_spec(src.m_spec)
, m_nativespec(src.m_nativespec)
, m_readonly(src.m_readonly)
, m_badfile(src.m_badfile)
, m_pixelaspect(src.m_pixelaspect)
, m_xstride(src.m_xstride)
, m_ystride(src.m_ystride)
, m_zstride(src.m_zstride)
, m_channel_stride(src.m_channel_stride)
, m_contiguous(src.m_contiguous)
, m_imagecache(src.m_imagecache)
, m_cachedpixeltype(src.m_cachedpixeltype)
, m_deepdata(src.m_deepdata)
, m_allocated_size(0)
, m_blackpixel(src.m_blackpixel) // gets fixed up in the body vvv
, m_write_format(src.m_write_format)
, m_write_tile_width(src.m_write_tile_width)
, m_write_tile_height(src.m_write_tile_height)
, m_write_tile_depth(src.m_write_tile_depth)
// NO -- copy ctr does not transfer proxy , m_rioproxy(src.m_rioproxy)
// NO -- copy ctr does not transfer proxy , m_wioproxy(src.m_wioproxy)
{
m_spec_valid = src.m_spec_valid;
m_pixels_valid = src.m_pixels_valid;
if (src.m_localpixels) {
// Source had the image fully in memory (no cache)
if (m_storage == ImageBuf::APPBUFFER) {
// Source just wrapped the client app's pixels, we do the same
m_localpixels = src.m_localpixels;
m_bufspan = src.m_bufspan;
} else {
// We own our pixels -- copy from source
new_pixels(src.m_spec.image_bytes(), src.m_pixels.get());
// N.B. new_pixels will set m_bufspan
}
} else {
// Source was cache-based or deep
// nothing else to do
m_localpixels = nullptr;
m_bufspan = span<std::byte>();
}
if (m_localpixels || m_spec.deep) {
// A copied ImageBuf is no longer a direct file reference, so clear
// some of the fields that are only meaningful for file references.
m_fileformat.clear();
m_nsubimages = 1;
m_current_subimage = 0;
m_current_miplevel = 0;
m_nmiplevels = 0;
m_spec.erase_attribute("oiio:subimages");
m_nativespec.erase_attribute("oiio:subimages");
}
if (src.m_configspec)
m_configspec.reset(new ImageSpec(*src.m_configspec));
eval_contiguous();
}
ImageBufImpl::~ImageBufImpl()
{
// Do NOT destroy m_imagecache here -- either it was created
// externally and passed to the ImageBuf ctr or reset() method, or
// else init_spec requested the system-wide shared cache, which
// does not need to be destroyed.
clear();
// Upon destruction, print uncaught errors to help users who don't know
// how to properly check for errors.
if (!m_err.empty() /* Note: safe becausethis is the dtr */
&& pvt::imagebuf_print_uncaught_errors) {
OIIO::print(
"An ImageBuf was destroyed with a pending error message that was never\n"
"retrieved via ImageBuf::geterror(). This was the error message:\n{}\n",
m_err);
}
}
ImageBuf::ImageBuf()
: m_impl(new ImageBufImpl(std::string(), -1, -1, NULL), &impl_deleter)
{
}
ImageBuf::ImageBuf(string_view filename, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache,
const ImageSpec* config, Filesystem::IOProxy* ioproxy)
: m_impl(new ImageBufImpl(filename, subimage, miplevel,
std::move(imagecache), nullptr /*spec*/,
{} /*bufspan*/, nullptr /*buforigin*/, true,
config, ioproxy),
&impl_deleter)
{
}
ImageBuf::ImageBuf(const ImageSpec& spec, InitializePixels zero)
: m_impl(new ImageBufImpl("", 0, 0, NULL, &spec), &impl_deleter)
{
m_impl->alloc(spec);
// N.B. alloc will set m_bufspan
if (zero == InitializePixels::Yes && !deep())
ImageBufAlgo::zero(*this);
}
ImageBuf::ImageBuf(const ImageSpec& spec, void* buffer, stride_t xstride,
stride_t ystride, stride_t zstride)
: m_impl(new ImageBufImpl("", 0, 0, nullptr /*imagecache*/, &spec,
{} /*bufspan*/, buffer /*buforigin*/,
false /*readonly*/, nullptr /*config*/,
nullptr /*ioproxy*/, xstride, ystride, zstride),
&impl_deleter)
{
}
ImageBuf::ImageBuf(const ImageSpec& spec, cspan<std::byte> buffer,
void* buforigin, stride_t xstride, stride_t ystride,
stride_t zstride)
: m_impl(new ImageBufImpl("", 0, 0, nullptr /*imagecache*/, &spec,
make_span((std::byte*)buffer.data(),
buffer.size()),
buforigin, true /*readonly*/, nullptr /*config*/,
nullptr /*ioproxy*/, xstride, ystride, zstride),
&impl_deleter)
{
}
ImageBuf::ImageBuf(const ImageSpec& spec, span<std::byte> buffer,
void* buforigin, stride_t xstride, stride_t ystride,
stride_t zstride)
: m_impl(new ImageBufImpl("", 0, 0, NULL, &spec, buffer, buforigin,
false /*readonly*/, nullptr /*config*/,
nullptr /*ioproxy*/, xstride, ystride, zstride),
&impl_deleter)
{
}
ImageBuf::ImageBuf(const ImageBuf& src)
: m_impl(new ImageBufImpl(*src.m_impl), &impl_deleter)
{
}
ImageBuf::ImageBuf(ImageBuf&& src)
: m_impl(std::move(src.m_impl))
{
}
ImageBuf::~ImageBuf() {}
const ImageBuf&
ImageBuf::operator=(const ImageBuf& src)
{
copy(src);
return *this;
}
const ImageBuf&
ImageBuf::operator=(ImageBuf&& src)
{
m_impl = std::move(src.m_impl);
return *this;
}
char*
ImageBufImpl::new_pixels(size_t size, const void* data)
{
if (m_allocated_size)
free_pixels();
try {
m_pixels.reset(size ? new char[size] : nullptr);
// Set bufspan to the allocated memory
m_bufspan = { reinterpret_cast<std::byte*>(m_pixels.get()), size };
} catch (const std::exception& e) {
// Could not allocate enough memory. So don't allocate anything,
// consider this an uninitialized ImageBuf, issue an error, and hope
// it's handled well downstream.
m_pixels.reset();
OIIO::debugfmt("ImageBuf unable to allocate {} bytes ({})\n", size,
e.what());
error("ImageBuf unable to allocate {} bytes ({})\n", size, e.what());
size = 0;
m_bufspan = {};
}
m_allocated_size = size;
pvt::IB_local_mem_current += m_allocated_size;
atomic_max(pvt::IB_local_mem_peak, (long long)pvt::IB_local_mem_current);
if (data && size)
memcpy(m_pixels.get(), data, size);
m_localpixels = m_pixels.get();
m_storage = size ? ImageBuf::LOCALBUFFER : ImageBuf::UNINITIALIZED;
if (pvt::oiio_print_debug > 1)
OIIO::debugfmt("IB allocated {} MB, global IB memory now {} MB\n",
size >> 20, pvt::IB_local_mem_current >> 20);
eval_contiguous();
return m_localpixels;
}
void
ImageBufImpl::free_pixels()
{
if (m_allocated_size) {
if (pvt::oiio_print_debug > 1)
OIIO::debugfmt("IB freed {} MB, global IB memory now {} MB\n",
m_allocated_size >> 20,
pvt::IB_local_mem_current >> 20);
pvt::IB_local_mem_current -= m_allocated_size;
m_allocated_size = 0;
}
m_pixels.reset();
m_deepdata.free();
m_storage = ImageBuf::UNINITIALIZED;
m_blackpixel.clear();
}
static spin_mutex err_mutex; ///< Protect m_err fields
bool
ImageBuf::has_error() const
{
spin_lock lock(err_mutex);
return !m_impl->m_err.empty();
}
std::string
ImageBuf::geterror(bool clear) const
{
spin_lock lock(err_mutex);
std::string e = m_impl->m_err;
if (clear)
m_impl->m_err.clear();
return e;
}
void
ImageBufImpl::error(string_view message) const
{
// Remove a single trailing newline
if (message.size() && message.back() == '\n')
message.remove_suffix(1);
spin_lock lock(err_mutex);
OIIO_ASSERT(
m_err.size() < 1024 * 1024 * 16
&& "Accumulated error messages > 16MB. Try checking return codes!");
// If we are appending to existing error messages, separate them with
// a single newline.
if (m_err.size() && m_err.back() != '\n')
m_err += '\n';
m_err += std::string(message);
}
void
ImageBuf::error(string_view message) const
{
m_impl->error(message);
}
ImageBuf::IBStorage
ImageBuf::storage() const
{
return m_impl->storage();
}
void
ImageBufImpl::clear()
{
if (m_imagecache && !m_name.empty()
&& (cachedpixels() || m_rioproxy)) {
// If we were backed by an ImageCache, invalidate any IC entries we
// might have made. Also do so if we were using an IOProxy, because
// the proxy may not survive long after the ImageBuf is destroyed.
m_imagecache->close(m_name);
m_imagecache->invalidate(m_name, false);
}
free_pixels();
m_name.clear();
m_fileformat.clear();
m_nsubimages = 0;
m_current_subimage = -1;
m_current_miplevel = -1;
m_spec = ImageSpec();
m_nativespec = ImageSpec();
m_pixels.reset();
m_localpixels = nullptr;
m_spec_valid = false;
m_pixels_valid = false;
m_badfile = false;
m_pixelaspect = 1;
m_xstride = 0;
m_ystride = 0;
m_zstride = 0;
m_channel_stride = 0;
m_contiguous = false;
m_imagecache.reset();
m_deepdata.free();
m_blackpixel.clear();
m_write_format.clear();
m_write_tile_width = 0;
m_write_tile_height = 0;
m_write_tile_depth = 0;
m_rioproxy = nullptr;
m_wioproxy = nullptr;
m_configspec.reset();
m_thumbnail.reset();
}
void
ImageBuf::clear()
{
m_impl->clear();
}
void
ImageBufImpl::reset(string_view filename, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache,
const ImageSpec* config, Filesystem::IOProxy* ioproxy)
{
clear();
m_name = ustring(filename);
if (m_imagecache || pvt::imagebuf_use_imagecache) {
// Invalidate the image in cache. Do so unconditionally if there's a
// chance that configuration hints may have changed.
invalidate(m_name, config || m_configspec);
}
m_current_subimage = subimage;
m_current_miplevel = miplevel;
m_imagecache = std::move(imagecache);
if (config)
m_configspec.reset(new ImageSpec(*config));
m_rioproxy = ioproxy;
if (m_rioproxy) {
add_configspec();
m_configspec->attribute("oiio:ioproxy", TypeDesc::PTR, &m_rioproxy);
}
m_bufspan = {};
if (m_name.length() > 0) {
// filename non-empty means this ImageBuf refers to a file.
read(subimage, miplevel);
// FIXME: investigate if the above read is really necessary, or if
// it can be eliminated and done fully lazily.
}
}
void
ImageBuf::reset(string_view filename, int subimage, int miplevel,
std::shared_ptr<ImageCache> imagecache, const ImageSpec* config,
Filesystem::IOProxy* ioproxy)
{
m_impl->reset(filename, subimage, miplevel, std::move(imagecache), config,
ioproxy);
}
void
ImageBufImpl::set_bufspan_localpixels(span<std::byte> bufspan,
const void* buforigin)
{
if (bufspan.size() && !buforigin) {
buforigin = bufspan.data();
} else if (buforigin && (!bufspan.data() || bufspan.empty())) {
bufspan = span_from_buffer(const_cast<void*>(buforigin), m_spec.format,
m_spec.nchannels, m_spec.width,
m_spec.height, m_spec.depth, m_xstride,
m_ystride, m_zstride);
}
m_bufspan = bufspan;
m_localpixels = (char*)buforigin;
OIIO_DASSERT(check_span(m_bufspan, m_localpixels, spec().format));
}
void
ImageBufImpl::reset(string_view filename, const ImageSpec& spec,
const ImageSpec* nativespec, span<std::byte> bufspan,
const void* buforigin, bool readonly, stride_t xstride,
stride_t ystride, stride_t zstride)
{
clear();
if (!spec.image_bytes()) {
m_storage = ImageBuf::UNINITIALIZED;
error(
"Could not initialize ImageBuf: the provided ImageSpec needs a valid width, height, depth, nchannels, format.");
return;
}
m_name = ustring(filename);
m_current_subimage = 0;
m_current_miplevel = 0;
if (buforigin || bufspan.size()) {
m_spec = spec;
//! TODO: remove m_nativespec
m_nativespec = nativespec ? *nativespec : spec;
m_channel_stride = stride_t(spec.format.size());
m_xstride = xstride;
m_ystride = ystride;
m_zstride = zstride;
m_readonly = readonly;
ImageSpec::auto_stride(m_xstride, m_ystride, m_zstride, m_spec.format,
m_spec.nchannels, m_spec.width, m_spec.height);
m_blackpixel.resize(round_to_multiple(spec.pixel_bytes(),
OIIO_SIMD_MAX_SIZE_BYTES),
0);
set_bufspan_localpixels(bufspan, buforigin);
m_storage = ImageBuf::APPBUFFER;
m_pixels_valid = true;
} else {
m_storage = ImageBuf::LOCALBUFFER;
m_readonly = false;
alloc(spec);
// N.B. alloc sets m_bufspan
}
if (nativespec)
m_nativespec = *nativespec;
}
void
ImageBuf::reset(const ImageSpec& spec, InitializePixels zero)
{
m_impl->reset("", spec);
if (zero == InitializePixels::Yes && !deep())
ImageBufAlgo::zero(*this);
}
void
ImageBuf::reset(const ImageSpec& spec, void* buffer, stride_t xstride,
stride_t ystride, stride_t zstride)
{
m_impl->reset("", spec, nullptr, {}, buffer, xstride, ystride, zstride);
}
void
ImageBuf::reset(const ImageSpec& spec, cspan<std::byte> buffer,
const void* buforigin, stride_t xstride, stride_t ystride,
stride_t zstride)
{
m_impl->reset("", spec, nullptr,
{ const_cast<std::byte*>(buffer.data()), buffer.size() },
buforigin, true /*readonly*/, xstride, ystride, zstride);