Omnidome
Fulldome Mapping Software Toolkit
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Buffer.h
Go to the documentation of this file.
1 /* Copyright (c) 2014-2015 "Omnidome" by cr8tr
2  * Dome Mapping Projection Software (http://omnido.me).
3  * Omnidome was created by Michael Winkelmann aka Wilston Oreo (@WilstonOreo)
4  *
5  * This file is part of Omnidome.
6  *
7  * Omnidome is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Affero General Public License for more details.
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 #ifndef OMNI_BUFFER_H_
20 #define OMNI_BUFFER_H_
21 
22 #include <cstdint>
23 #include <cstddef>
24 #include <QRect>
25 #include <QImage>
26 #include <omni/util.h>
27 #include <omni/PixelConverter.h>
29 
30 namespace omni {
31  /// A buffer holds an w x h pixel array
32  template<typename T>
33  struct Buffer
34  {
35  /// Our pixel type
36  typedef T pixel_type;
37 
38  /// Our data type (a dynamic array)
39  typedef std::vector<pixel_type>data_type;
40 
41  /// Standard constructor
42  Buffer() : width_(0), height_(0) {}
43 
44  /// Constructor with automatic resize
45  Buffer(int _width, int _height) {
46  resize(_width, _height);
47  }
48 
49  /// Write pixel value to position (x,y)
50  void put(int _x, int _y, const pixel_type& _pixel)
51  {
52  put(_y * width() + _x, _pixel);
53  }
54 
55  /// Write pixel value to offset (= x * width + y)
56  void put(size_t _offset, const pixel_type& _pixel)
57  {
58  data_[_offset] = _pixel;
59  }
60 
61  /// Return pixel on position (x,y)
62  pixel_type& operator()(int _x, int _y)
63  {
64  return pixel(_x, _y);
65  }
66 
67  /// Return pixel on position (x,y) (const version)
68  pixel_type const& operator()(int _x, int _y) const
69  {
70  return pixel(_x, _y);
71  }
72 
73  /// Return pixel on offset (= x * width + y)
74  pixel_type& operator()(size_t _offset)
75  {
76  return pixel(_offset);
77  }
78 
79  /// Return pixel on offset (= x * width + y) (const version)
80  pixel_type const& operator()(size_t _offset) const
81  {
82  return pixel(_offset);
83  }
84 
85  /// Subscript operator for accessing elements by index
86  pixel_type& operator[](size_t _offset) {
87  return pixel(_offset);
88  }
89 
90  /// Subscript operator for accessing elements by index (const version)
91  pixel_type const& operator[](size_t _offset) const {
92  return pixel(_offset);
93  }
94 
95  /// Return pixel on position (x,y)
96  pixel_type& pixel(int _x, int _y)
97  {
98  return pixel(_y * width() + _x);
99  }
100 
101  Buffer cropRect(QRect const& _rect) const
102  {
103  Buffer _buffer(_rect.width(), _rect.height());
104 
105  for (int y = 0; y < _rect.height(); ++y)
106  for (int x = 0; x < _rect.width(); ++x)
107  {
108  _buffer(x, y) = pixel(x + _rect.x(), y + _rect.y());
109  }
110 
111  return _buffer;
112  }
113 
114  /// Return pixel on position (x,y) (const version)
115  pixel_type const& pixel(int _x, int _y) const
116  {
117  return pixel(_y * width() + _x);
118  }
119 
120  /// Return pixel on offset (= x * width + y)
121  pixel_type& pixel(size_t _offset) {
122  return data_[_offset];
123  }
124 
125  /// Return pixel on offset (= x * width + y) (const version)
126  pixel_type const& pixel(size_t _offset) const {
127  return data_[_offset];
128  }
129 
130  /// Return width of the buffer
131  int width() const
132  {
133  return width_;
134  }
135 
136  /// Return height of the buffer
137  int height() const
138  {
139  return height_;
140  }
141 
142  /// Return const reference to data
143  data_type const& data() const
144  {
145  return data_;
146  }
147 
148  /// Clear buffer with black color
149  void clear()
150  {
151  data_ = data_type(data_.size(), 0);
152  }
153 
154  /// Clear buffer with specific pixel
155  void clear(const pixel_type& _pixel)
156  {
157  data_ = data_type(data_.size(), _pixel);
158  }
159 
160  /// Returns true if width and height are zero
161  bool empty() const
162  {
163  return data_.empty();
164  }
165 
166  /// Returns size of buffer (= width * height)
167  size_t size() const {
168  return data_.size();
169  }
170 
171  /// Resize buffer to given width and height
172  void resize(int _width, int _height)
173  {
174  if (_width * _height <= 0) return;
175 
176  width_ = _width;
177  height_ = _height;
178  data_.resize(width_ * height_);
179  }
180 
181  /// Convert buffer to QImage
182  QImage toQImage() const
183  {
184  QImage _output(width(), height(), QImage::Format_RGB32);
185 
186  for (int y = 0; y < height(); ++y)
187  for (int x = 0; x < width(); ++x)
188  {
189  auto _p = pixel(x, y);
190  _output.setPixel(x, y, convertPixel<QColor>(_p).rgb());
191  }
192 
193  return _output;
194  }
195 
196  /// Return void pointer to data
197  void* ptr()
198  {
199  return static_cast<void *>(data_.data());
200  }
201 
202  /// Return void pointer to data (const version)
203  void const* ptr() const
204  {
205  return static_cast<void const*>(data_.data());
206  }
207 
208  /// Write blend mask to stream
209  void toStream(QDataStream& _os) const {
210  serialize(_os, width_);
211  serialize(_os, height_);
212  serialize(_os, data_);
213  }
214 
215  /// Read blend mask from stream
216  void fromStream(QDataStream& _is) {
217  deserialize(_is, width_);
218  deserialize(_is, height_);
219  deserialize(_is, data_);
220  }
221 
222  /// Test for equality, buffer is ignored
223  friend bool operator==(Buffer const& _lhs, Buffer const& _rhs) {
224  return
228  }
229 
230  private:
233  };
234 }
235 
236 /// Deserialize buffer from stream
237 template<typename T>
238 QDataStream& operator>>(QDataStream& _is, omni::Buffer<T>& _buf) {
239  _buf.fromStream(_is);
240  return _is;
241 }
242 
243 /// Serialize buffer to stream
244 template<typename T>
245 QDataStream& operator<<(QDataStream& _os, omni::Buffer<T>const& _buf) {
246  _buf.toStream(_os);
247  return _os;
248 }
249 
250 #endif /* OMNI_BUFFER_H_ */
T pixel_type
Our pixel type.
Definition: Buffer.h:36
data_type const & data() const
Return const reference to data.
Definition: Buffer.h:143
void clear()
Clear buffer with black color.
Definition: Buffer.h:149
pixel_type & operator()(int _x, int _y)
Return pixel on position (x,y)
Definition: Buffer.h:62
void put(size_t _offset, const pixel_type &_pixel)
Write pixel value to offset (= x * width + y)
Definition: Buffer.h:56
pixel_type const & operator[](size_t _offset) const
Subscript operator for accessing elements by index (const version)
Definition: Buffer.h:91
int height() const
Return height of the buffer.
Definition: Buffer.h:137
pixel_type const & operator()(size_t _offset) const
Return pixel on offset (= x * width + y) (const version)
Definition: Buffer.h:80
STREAM & serialize(STREAM &_stream, T const &_t)
Serialize object to stream.
Definition: traits.h:140
size_t size() const
Returns size of buffer (= width * height)
Definition: Buffer.h:167
pixel_type & pixel(size_t _offset)
Return pixel on offset (= x * width + y)
Definition: Buffer.h:121
#define OMNI_TEST_MEMBER_EQUAL(member)
Definition: util.h:125
Buffer(int _width, int _height)
Constructor with automatic resize.
Definition: Buffer.h:45
void const * ptr() const
Return void pointer to data (const version)
Definition: Buffer.h:203
STREAM & deserialize(STREAM &_stream, T &_t, ARGS &&..._args)
Deserialize object of type T from stream with optional additional arguments.
Definition: traits.h:125
Buffer cropRect(QRect const &_rect) const
Definition: Buffer.h:101
void put(int _x, int _y, const pixel_type &_pixel)
Write pixel value to position (x,y)
Definition: Buffer.h:50
data_type data_
Definition: Buffer.h:232
bool empty() const
Returns true if width and height are zero.
Definition: Buffer.h:161
void clear(const pixel_type &_pixel)
Clear buffer with specific pixel.
Definition: Buffer.h:155
int height_
Definition: Buffer.h:231
void resize(int _width, int _height)
Resize buffer to given width and height.
Definition: Buffer.h:172
Buffer()
Standard constructor.
Definition: Buffer.h:42
int width_
Definition: Buffer.h:231
QImage toQImage() const
Convert buffer to QImage.
Definition: Buffer.h:182
void toStream(QDataStream &_os) const
Write blend mask to stream.
Definition: Buffer.h:209
std::vector< pixel_type > data_type
Our data type (a dynamic array)
Definition: Buffer.h:39
pixel_type & operator()(size_t _offset)
Return pixel on offset (= x * width + y)
Definition: Buffer.h:74
pixel_type & operator[](size_t _offset)
Subscript operator for accessing elements by index.
Definition: Buffer.h:86
void * ptr()
Return void pointer to data.
Definition: Buffer.h:197
A buffer holds an w x h pixel array.
Definition: Buffer.h:33
pixel_type const & pixel(int _x, int _y) const
Return pixel on position (x,y) (const version)
Definition: Buffer.h:115
QDataStream & operator>>(QDataStream &_is, omni::Buffer< T > &_buf)
Deserialize buffer from stream.
Definition: Buffer.h:238
pixel_type const & operator()(int _x, int _y) const
Return pixel on position (x,y) (const version)
Definition: Buffer.h:68
pixel_type & pixel(int _x, int _y)
Return pixel on position (x,y)
Definition: Buffer.h:96
pixel_type const & pixel(size_t _offset) const
Return pixel on offset (= x * width + y) (const version)
Definition: Buffer.h:126
void fromStream(QDataStream &_is)
Read blend mask from stream.
Definition: Buffer.h:216
int width() const
Return width of the buffer.
Definition: Buffer.h:131
friend bool operator==(Buffer const &_lhs, Buffer const &_rhs)
Test for equality, buffer is ignored.
Definition: Buffer.h:223