Loading...
Searching...
No Matches
bitmap.h
Go to the documentation of this file.
1#ifndef _BBM_BITMAP_H_
2#define _BBM_BITMAP_H_
3
4#include <memory>
5#include <algorithm>
6
7/***********************************************************************/
8/*! \file bitmap.h
9 \brief Minimal bitmap class
10************************************************************************/
11
12namespace bbm {
13
14 template<typename T>
15 class bitmap
16 {
17 public:
18 //! \brief Default constructor
19 bitmap(void) : _width(0), _height(0), _data(nullptr) {}
20
21 //! \brief Construct an empty/unitialized bitmap of size width x height
22 bitmap(size_t width, size_t height)
23 {
25 }
26
27 //! \brief Construct an image of size width x height and set every pixel to 'init'
28 bitmap(size_t width, size_t height, const T& init) : bitmap(width, height)
29 {
30 std::fill(begin(), end(), init);
31 }
32
33 //! \brief Copy constructor
34 bitmap(const bitmap<T>& src) : bitmap(src._width, src._height)
35 {
36 std::copy(src.begin(), src.end(), begin());
37 }
38
39 //! \brief Move constructor
41 {
42 std::swap(_width, src._width);
43 std::swap(_height, src._height);
44 std::swap(_data, src._data);
45 }
46
47 //! \brief Assignment operator
49 {
50 if(this == &src) return *this;
51 reshape(src._width, src._height);
52 std::copy(src.begin(), src.end(), begin());
53 return *this;
54 }
55
56 //! \brief Move assignment operator
58 {
59 if(this == &src) return *this;
60 std::swap(_width, src._width);
61 std::swap(_height, src._height);
62 std::swap(_data, src._data);
63 return *this;
64 }
65
66 //! \brief reshape the bitmap (loss of old data)
67 void reshape(size_t width, size_t height)
68 {
69 _width = width;
71
72 _data.reset();
73 if(_width != 0 && _height != 0)
74 _data = std::make_unique<T[]>(_width*_height);
75 }
76
77 //! @{ \name iterator support
78 const T* begin(void) const { return _data.get(); }
79 const T* end(void) const { return begin() + _width*_height; }
80
81 T* begin(void) { return _data.get(); }
82 T* end(void) { return begin() + _width*_height; }
83 //! @}
84
85 //! @{ \name Inspectors
86 size_t width() const { return _width; }
87 size_t height() const { return _height; }
88
89 const T& operator()(size_t x, size_t y) const { return _data[y*_width + x]; }
90 T& operator()(size_t x, size_t y) { return _data[y*_width + x]; }
91 //! @}
92
93 private:
94 //////////////////
95 // Data Members
96 //////////////////
97 size_t _width, _height;
98 std::unique_ptr<T[]> _data;
99 };
100
101} // end bbm namespace
102
103#endif /* _BBM_BITMAP_H_ */
Definition: bitmap.h:16
T * begin(void)
Definition: bitmap.h:81
bitmap(size_t width, size_t height, const T &init)
Construct an image of size width x height and set every pixel to 'init'.
Definition: bitmap.h:28
T * end(void)
Definition: bitmap.h:82
const T * begin(void) const
Definition: bitmap.h:78
size_t _height
Definition: bitmap.h:97
const T & operator()(size_t x, size_t y) const
Definition: bitmap.h:89
bitmap(size_t width, size_t height)
Construct an empty/unitialized bitmap of size width x height.
Definition: bitmap.h:22
std::unique_ptr< T[]> _data
Definition: bitmap.h:98
bitmap(void)
Default constructor.
Definition: bitmap.h:19
bitmap< T > & operator=(bitmap< T > &&src)
Move assignment operator.
Definition: bitmap.h:57
bitmap(bitmap< T > &&src)
Move constructor.
Definition: bitmap.h:40
size_t _width
Definition: bitmap.h:97
size_t width() const
Definition: bitmap.h:86
bitmap< T > & operator=(const bitmap< T > &src)
Assignment operator.
Definition: bitmap.h:48
const T * end(void) const
Definition: bitmap.h:79
bitmap(const bitmap< T > &src)
Copy constructor.
Definition: bitmap.h:34
size_t height() const
Definition: bitmap.h:87
void reshape(size_t width, size_t height)
reshape the bitmap (loss of old data)
Definition: bitmap.h:67
T & operator()(size_t x, size_t y)
Definition: bitmap.h:90
Definition: aggregatebsdf.h:29