52 if (_width != 0 && _height != 0)
54 _data = _allocate(_width * _height);
56 for (
auto& v : rhs) *(dest_v++) = v;
62 Data(in_properties,
sizeof(value_type)),
63 _data(_allocate(width * height)),
65 _height(height) {
dirty(); }
67 Array2D(uint32_t width, uint32_t height, value_type* data,
Properties in_properties = {}) :
68 Data(in_properties,
sizeof(value_type)),
71 _height(height) {
dirty(); }
73 Array2D(uint32_t width, uint32_t height,
const value_type& value,
Properties in_properties = {}) :
74 Data(in_properties,
sizeof(value_type)),
75 _data(_allocate(width * height)),
79 for (
auto& v : *
this) v = value;
89 assign(data, offset, stride, width, height, in_properties);
92 template<
typename... Args>
103 size_t sizeofObject() const noexcept
override {
return sizeof(Array2D); }
104 const char* className() const noexcept
override {
return type_name<Array2D>(); }
105 const std::type_info&
type_info() const noexcept
override {
return typeid(*this); }
106 bool is_compatible(
const std::type_info& type)
const noexcept override {
return typeid(
Array2D) == type || Data::is_compatible(type); }
109 void accept(Visitor& visitor)
override;
110 void accept(ConstVisitor& visitor)
const override;
112 void read(Input& input)
override
114 size_t original_size = size();
118 uint32_t w = input.readValue<uint32_t>(
"width");
119 uint32_t h = input.readValue<uint32_t>(
"height");
121 if (
auto data_storage = input.readObject<Data>(
"storage"))
123 uint32_t offset = input.readValue<uint32_t>(
"offset");
128 if (input.matchPropertyName(
"data"))
130 size_t new_size = computeValueCountIncludingMipmaps(w, h, 1,
properties.maxNumMipmaps);
134 if (original_size != new_size)
137 _data = _allocate(new_size);
142 _data = _allocate(new_size);
150 if (_data) input.read(new_size, _data);
156 void write(Output& output)
const override
160 output.writeValue<uint32_t>(
"width", _width);
161 output.writeValue<uint32_t>(
"height", _height);
163 output.writeObject(
"storage", _storage);
166 auto offset = (
reinterpret_cast<uintptr_t
>(_data) -
reinterpret_cast<uintptr_t
>(_storage->dataPointer()));
167 output.writeValue<uint32_t>(
"offset", offset);
171 output.writePropertyName(
"data");
172 output.write(valueCount(), _data);
173 output.writeEndOfLine();
176 size_t size()
const {
return (
properties.maxNumMipmaps <= 1) ?
static_cast<size_t>(_width * _height) : computeValueCountIncludingMipmaps(_width, _height, 1,
properties.maxNumMipmaps); }
178 bool available()
const {
return _data !=
nullptr; }
179 bool empty()
const {
return _data ==
nullptr; }
191 Array2D& operator=(
const Array2D& rhs)
193 if (&rhs ==
this)
return *
this;
199 _height = rhs._height;
201 if (_width != 0 && _height != 0)
203 _data = _allocate(_width * _height);
205 for (
auto& v : rhs) *(dest_v++) = v;
213 void assign(uint32_t width, uint32_t height, value_type* data, Properties in_properties = {})
227 void assign(ref_ptr<Data> storage, uint32_t offset, uint32_t stride, uint32_t width, uint32_t height, Properties in_properties = {})
234 if (_storage && _storage->dataPointer())
236 _data =
reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_storage->dataPointer()) + offset);
252 void* dataRelease()
override
268 size_t valueSize()
const override {
return sizeof(value_type); }
269 size_t valueCount()
const override {
return size(); }
271 bool dataAvailable()
const override {
return available(); }
272 size_t dataSize()
const override {
return size() *
properties.stride; }
274 void* dataPointer()
override {
return _data; }
275 const void* dataPointer()
const override {
return _data; }
277 void* dataPointer(
size_t i)
override {
return data(i); }
278 const void* dataPointer(
size_t i)
const override {
return data(i); }
280 uint32_t dimensions()
const override {
return 2; }
281 uint32_t width()
const override {
return _width; }
282 uint32_t height()
const override {
return _height; }
283 uint32_t depth()
const override {
return 1; }
285 value_type* data() {
return _data; }
286 const value_type* data()
const {
return _data; }
288 inline value_type* data(
size_t i) {
return reinterpret_cast<value_type*
>(
reinterpret_cast<uint8_t*
>(_data) + i *
properties.stride); }
289 inline const value_type* data(
size_t i)
const {
return reinterpret_cast<const value_type*
>(
reinterpret_cast<const uint8_t*
>(_data) + i *
properties.stride); }
291 size_t index(uint32_t i, uint32_t j)
const noexcept {
return static_cast<size_t>(j) * _width + i; }
293 value_type& operator[](
size_t i) {
return *data(i); }
294 const value_type& operator[](
size_t i)
const {
return *data(i); }
296 value_type& at(
size_t i) {
return *data(i); }
297 const value_type& at(
size_t i)
const {
return *data(i); }
299 value_type& operator()(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
300 const value_type& operator()(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
302 value_type& at(uint32_t i, uint32_t j) {
return *data(index(i, j)); }
303 const value_type& at(uint32_t i, uint32_t j)
const {
return *data(index(i, j)); }
305 void set(
size_t i,
const value_type& v) { *data(i) = v; }
306 void set(uint32_t i, uint32_t j,
const value_type& v) { *data(index(i, j)) = v; }
308 Data* storage() {
return _storage; }
309 const Data* storage()
const {
return _storage; }
311 iterator begin() {
return iterator{_data,
properties.stride}; }
312 const_iterator begin()
const {
return const_iterator{_data,
properties.stride}; }
314 iterator end() {
return iterator{data(_width * _height),
properties.stride}; }
315 const_iterator end()
const {
return const_iterator{data(_width * _height),
properties.stride}; }
323 value_type* _allocate(
size_t size)
const
328 return new value_type[size];
330 return new (std::malloc(
sizeof(value_type) * size)) value_type[size];
332 return new (vsg::allocate(
sizeof(value_type) * size, ALLOCATOR_AFFINITY_DATA)) value_type[size];
337 if (!_storage && _data)
344 vsg::deallocate(_data);
352 ref_ptr<Data> _storage;