HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
deepdata.h
Go to the documentation of this file.
1 // Copyright 2008-present Contributors to the OpenImageIO project.
2 // SPDX-License-Identifier: BSD-3-Clause
3 // https://github.com/OpenImageIO/oiio
4 
5 
6 #pragma once
7 
8 #include <OpenImageIO/export.h>
10 #include <OpenImageIO/span.h>
12 
14 
15 
16 struct TypeDesc;
17 class ImageSpec;
18 
19 
20 
21 /// A `DeepData` holds the contents of an image of ``deep'' pixels (multiple
22 /// depth samples per pixel).
23 
25 public:
26  /// Construct an empty DeepData.
27  DeepData();
28 
29  /// Construct and init from an ImageSpec.
30  DeepData(const ImageSpec& spec);
31 
32  /// Copy constructor
33  DeepData(const DeepData& src);
34 
35  /// Copy constructor with change of channel types
36  DeepData(const DeepData& src, cspan<TypeDesc> channeltypes);
37 
38  /// Move constructor
39  DeepData(DeepData&& src);
40 
41  ~DeepData();
42 
43  /// Copy assignment
44  const DeepData& operator=(const DeepData& d);
45 
46  /// Reset the `DeepData` to be equivalent to its empty initial state.
47  void clear();
48  /// In addition to performing the tasks of `clear()`, also ensure that
49  /// all allocated memory has been truly freed.
50  void free();
51 
52  /// Initialize the `DeepData` with the specified number of pixels,
53  /// channels, channel types, and channel names, and allocate memory for
54  /// all the data.
55  void init(int64_t npix, int nchan, cspan<TypeDesc> channeltypes,
56  cspan<std::string> channelnames);
57 
58  /// Initialize the `DeepData` based on the `ImageSpec`'s total number of
59  /// pixels, number and types of channels. At this stage, all pixels are
60  /// assumed to have 0 samples, and no sample data is allocated.
61  void init(const ImageSpec& spec);
62 
63  /// Is the DeepData initialized?
64  bool initialized() const;
65 
66  /// Has the DeepData fully allocated? If no, it is still very
67  /// inexpensive to call set_capacity().
68  bool allocated() const;
69 
70  /// Retrieve the total number of pixels.
71  int64_t pixels() const;
72  /// Retrieve the number of channels.
73  int channels() const;
74 
75  // Retrieve the index of the Z channel.
76  int Z_channel() const;
77  // Retrieve the index of the Zback channel, which will return the
78  // Z channel if no Zback exists.
79  int Zback_channel() const;
80  // Retrieve the index of the A (alpha) channel.
81  int A_channel() const;
82  // Retrieve the index of the AR channel. If it does not exist, the A
83  // channel (which always exists) will be returned.
84  int AR_channel() const;
85  // Retrieve the index of the AG channel. If it does not exist, the A
86  // channel (which always exists) will be returned.
87  int AG_channel() const;
88  // Retrieve the index of the AB channel. If it does not exist, the A
89  // channel (which always exists) will be returned.
90  int AB_channel() const;
91 
92  /// Return the name of channel c.
93  string_view channelname(int c) const;
94 
95  /// Retrieve the data type of channel `c`.
96  TypeDesc channeltype(int c) const;
97  /// Return the size (in bytes) of one sample datum of channel `c`.
98  size_t channelsize(int c) const;
99  /// Return the size (in bytes) for all channels of one sample.
100  size_t samplesize() const;
101 
102  /// Does this DeepData have the same channel types as `other`?
103  bool same_channeltypes(const DeepData& other) const;
104 
105  /// Retrieve the number of samples for the given pixel index.
106  int samples(int64_t pixel) const;
107 
108  /// Set the number of samples for the given pixel. This must be called
109  /// after init().
110  void set_samples(int64_t pixel, int samps);
111 
112  /// Set the number of samples for all pixels. The samples.size() is
113  /// required to match pixels().
114  void set_all_samples(cspan<unsigned int> samples);
115 
116  /// Set the capacity of samples for the given pixel. This must be called
117  /// after init().
118  void set_capacity(int64_t pixel, int samps);
119 
120  /// Retrieve the capacity (number of allocated samples) for the given
121  /// pixel index.
122  int capacity(int64_t pixel) const;
123 
124  /// Insert `n` samples of the specified pixel, betinning at the sample
125  /// position index. After insertion, the new samples will have
126  /// uninitialized values.
127  void insert_samples(int64_t pixel, int samplepos, int n = 1);
128 
129  /// Erase `n` samples of the specified pixel, betinning at the sample
130  /// position index.
131  void erase_samples(int64_t pixel, int samplepos, int n = 1);
132 
133  /// Retrieve the value of the given pixel, channel, and sample index,
134  /// cast to a `float`.
135  float deep_value(int64_t pixel, int channel, int sample) const;
136  /// Retrieve the value of the given pixel, channel, and sample index,
137  /// cast to a `uint32`.
138  uint32_t deep_value_uint(int64_t pixel, int channel, int sample) const;
139 
140  /// Set the value of the given pixel, channel, and sample index, for
141  /// floating-point channels.
142  void set_deep_value(int64_t pixel, int channel, int sample, float value);
143 
144  /// Set the value of the given pixel, channel, and sample index, for
145  /// integer channels.
146  void set_deep_value(int64_t pixel, int channel, int sample, uint32_t value);
147 
148  /// Retrieve the pointer to a given pixel/channel/sample, or NULL if
149  /// there are no samples for that pixel. Use with care, and note that
150  /// calls to insert_samples and erase_samples can invalidate pointers
151  /// returned by prior calls to data_ptr.
152  void* data_ptr(int64_t pixel, int channel, int sample);
153  const void* data_ptr(int64_t pixel, int channel, int sample) const;
154 
155  cspan<TypeDesc> all_channeltypes() const;
156  cspan<unsigned int> all_samples() const;
157  cspan<char> all_data() const;
158 
159  /// Fill in the vector with pointers to the start of the first
160  /// channel for each pixel.
161  void get_pointers(std::vector<void*>& pointers) const;
162 
163  /// Copy a deep sample from `src` to this `DeepData`. They must have the
164  /// same channel layout. Return `true` if ok, `false` if the operation
165  /// could not be performed.
166  bool copy_deep_sample(int64_t pixel, int sample, const DeepData& src,
167  int64_t srcpixel, int srcsample);
168 
169  /// Copy an entire deep pixel from `src` to this `DeepData`, completely
170  /// replacing any pixel data for that pixel. They must have the same
171  /// channel layout. Return `true` if ok, `false` if the operation could
172  /// not be performed.
173  bool copy_deep_pixel(int64_t pixel, const DeepData& src, int64_t srcpixel);
174 
175  /// Split all samples of that pixel at the given depth zsplit. Samples
176  /// that span z (i.e. z < zsplit < zback) will be split into two samples
177  /// with depth ranges [z,zsplit] and [zsplit,zback] with appropriate
178  /// changes to their color and alpha values. Samples not spanning zsplit
179  /// will remain intact. This operation will have no effect if there are
180  /// not Z and Zback channels present. Return true if any splits
181  /// occurred, false if the pixel was not modified.
182  bool split(int64_t pixel, float depth);
183 
184  /// Sort the samples of the pixel by their `Z` depth.
185  void sort(int64_t pixel);
186 
187  /// Merge any adjacent samples in the pixel that exactly overlap in z
188  /// range. This is only useful if the pixel has previously been split at
189  /// all sample starts and ends, and sorted by Z. Note that this may
190  /// change the number of samples in the pixel.
191  void merge_overlaps(int64_t pixel);
192 
193  /// Merge the samples of `src`'s pixel into this `DeepData`'s pixel.
194  /// Return `true` if ok, `false` if the operation could not be
195  /// performed.
196  void merge_deep_pixels(int64_t pixel, const DeepData& src, int srcpixel);
197 
198  /// Return the z depth at which the pixel reaches full opacity.
199  float opaque_z(int64_t pixel) const;
200 
201  /// Remove any samples hidden behind opaque samples.
202  void occlusion_cull(int64_t pixel);
203 
204 private:
205  class Impl;
206  Impl* m_impl = nullptr; // holds all the nontrivial stuff
207  int64_t m_npixels = 0;
208  int m_nchannels = 0;
209 };
210 
211 
Definition: span.h:73
GLint GLint GLsizei GLint GLenum GLenum const void * pixels
Definition: glcorearb.h:108
GLdouble n
Definition: glcorearb.h:2008
GLsizei samples
Definition: glcorearb.h:1298
GLint GLint GLsizei GLsizei GLsizei depth
Definition: glcorearb.h:476
Definition: core.h:1131
#define OIIO_NAMESPACE_END
Definition: oiioversion.h:94
void OIIO_UTIL_API split(string_view str, std::vector< string_view > &result, string_view sep=string_view(), int maxsplit=-1)
void sort(I begin, I end, const Pred &pred)
Definition: pugixml.cpp:7334
ImageBuf OIIO_API channels(const ImageBuf &src, int nchannels, cspan< int > channelorder, cspan< float > channelvalues={}, cspan< std::string > newchannelnames={}, bool shuffle_channel_names=false, int nthreads=0)
#define OIIO_NAMESPACE_BEGIN
Definition: oiioversion.h:93
#define OIIO_API
Definition: export.h:65
GLenum src
Definition: glcorearb.h:1793