ImagePrivate.hpp
Go to the documentation of this file.
1 
6 #pragma once
7 
8 #include <etk/types.hpp>
9 #include <etk/math/Vector2D.hpp>
10 #include <etk/Color.hpp>
11 #include <egami/debug.hpp>
12 
13 #include <vector>
14 
15 namespace egami {
16 
17  template<typename T = etk::Color<>>
18  class ImageTemplate : public ImagePrivate {
19  private:
20  ivec2 m_size;
21  std::vector<T> m_data;
22  public:
23  // constructor :
24  ImageTemplate(const ivec2& _size=ivec2(32,32)) :
25  m_size(_size) {
26  // basic element :
27  etk::Color<> tmpBg(0,0,0,0);
28  // preallocate data with a basic bg elements :
29  m_data.resize(m_size.x()*m_size.y(), tmpBg);
30  if ((uint32_t)m_size.x()*m_size.y() > m_data.size()) {
31  //TK_ERROR("Allocation of data buffer in error");
32  return;
33  }
34  }
35 
36  // destructor
37  virtual ~ImageTemplate() { };
38  // EWOL internal API for Texture system :
39  public:
40  void* getTextureDataPointer() {
41  return &m_data[0];
42  };
43  enum colorType getType() const;
44  const ivec2& getSize() {
45  return m_size;
46  };
47  // -----------------------------------------------
48  // -- basic tools :
49  // -----------------------------------------------
50  public :
51  void resize__(const ivec2& _size, const ivec2& _startPos=ivec2(0,0)) {
52  if (_size == m_size) {
53  // same size == > nothing to do ...
54  return;
55  }
56  if ((size_t)(_size.x()*_size.y()) > m_data.size()) {
57  m_data.resize(_size.x()*_size.y());
58  }
59  // grow size :
60  if (_size.x() == m_size.x()) {
61  if (_size.y() < m_size.y()) {
62  // Just remove lines ....
63  } else {
64  // just add lines
65  }
66  } else if (_size.x() < m_size.x()) {
67  if (_size.y() <= m_size.y()) {
68  for (int32_t yyy=0; yyy<_size.y(); ++yyy) {
69  for (int32_t xxx=0; xxx<_size.x(); ++xxx) {
70  m_data[yyy*_size.x()+xxx] = m_data[yyy*m_size.x()+xxx];
71  }
72  }
73  } else {
74  for (int32_t yyy=m_size.y()-1; yyy>=0; --yyy) {
75  for (int32_t xxx=0; xxx<_size.x(); ++xxx) {
76  m_data[yyy*_size.x()+xxx] = m_data[yyy*m_size.x()+xxx];
77  }
78  }
79  }
80  } else { // (_size.x() > m_size.x())
81 
82  if (_size.y() <= m_size.y()) {
83  for (int32_t yyy=0; yyy<_size.y(); ++yyy) {
84  for (int32_t xxx=0; xxx<m_size.x(); ++xxx) {
85  m_data[yyy*_size.x()+xxx] = m_data[yyy*m_size.x()+xxx];
86  }
87  }
88  } else {
89  for (int32_t yyy=m_size.y()-1; yyy>=0; --yyy) {
90  for (int32_t xxx=0; xxx<m_size.x(); ++xxx) {
91  m_data[yyy*_size.x()+xxx] = m_data[yyy*m_size.x()+xxx];
92  }
93  }
94  }
95  }
96  if ((size_t)(_size.x()*_size.y()) < m_data.size()) {
97  m_data.resize(_size.x()*_size.y());
98  }
99  m_size = _size;
100  }
101  void resize__(const ivec2& _size, const T& _color) {
102  m_size=_size;
103  m_data.resize(m_size.x()*m_size.y(), _color);
104  }
105 
106  void resize(const ivec2& _size, const etk::Color<uint8_t, 4>& _color, const ivec2& _startPos) {
107  resize__(_size, _color);
108  }
109  void resize(const ivec2& _size, const etk::Color<float, 4>& _color, const ivec2& _startPos) {
110  resize__(_size, _color);
111  }
112  void resize(const ivec2& _size, const etk::Color<uint16_t, 1>& _color, const ivec2& _startPos) {
113  resize__(_size, _color);
114  }
115  void resize(const ivec2& _size, const etk::Color<uint32_t, 1>& _color, const ivec2& _startPos) {
116  resize__(_size, _color);
117  }
118  void resize(const ivec2& _size, const etk::Color<float, 1>& _color, const ivec2& _startPos) {
119  resize__(_size, _color);
120  }
121  void resize(const ivec2& _size, const etk::Color<double, 1>& _color, const ivec2& _startPos) {
122  resize__(_size, _color);
123  }
124 
125  void resize(const ivec2& _size, const ivec2& _startPos) {
126  resize__(_size);
127  }
128  template<typename TYPE_2> void resize(const ivec2& _size, const TYPE_2& _color) {
129  T tmp(_color);
130  resize__(_size, tmp);
131  }
132  void set(const ivec2& _pos, const etk::Color<>& _newColor) {
133  set__(_pos, _newColor);
134  }
135  void set(const ivec2& _pos, const etk::Color<float>& _newColor) {
136  set__(_pos, _newColor);
137  }
138  void set(const ivec2& _pos, const etk::Color<uint16_t, 1>& _newColor) {
139  set__(_pos, _newColor);
140  }
141  void set(const ivec2& _pos, const etk::Color<uint32_t, 1>& _newColor) {
142  set__(_pos, _newColor);
143  }
144  void set(const ivec2& _pos, const etk::Color<float, 1>& _newColor) {
145  set__(_pos, _newColor);
146  }
147  void set(const ivec2& _pos, const etk::Color<double, 1>& _newColor) {
148  set__(_pos, _newColor);
149  }
150 
151  const ivec2& getSize() const {
152  return m_size;
153  };
154  int32_t getWidth() const {
155  return m_size.x();
156  };
157  int32_t getHeight() const {
158  return m_size.y();
159  };
160  void clearColor(const T& _fill) {
161  for (int32_t iii=0; iii<m_size.x()*m_size.y(); iii++) {
162  m_data[iii] = _fill;
163  }
164  }
165  void clear() {
166  clearColor(T::emptyColor);
167  }
168  etk::Color<> get(const ivec2& _pos) const {
169  return get__(_pos);
170  }
171 
172  const T& get__(const ivec2& _pos) const {
173  static const T errorColor(0x00000000);
174  if( _pos.x()>0 && _pos.x()<m_size.x()
175  && _pos.y()>0 && _pos.y()<m_size.y()) {
176  return m_data[_pos.x()+_pos.y()*m_size.x()];
177  }
178  return errorColor;
179  }
180  void set__(const ivec2& _pos, const T& _newColor) {
181  if( _pos.x()>=0 && _pos.x()<m_size.x()
182  && _pos.y()>=0 && _pos.y()<m_size.y()) {
183  m_data[_pos.x()+_pos.y()*m_size.x()] = _newColor;
184  }
185  }
186  void insert(const ivec2& _pos, const ImageTemplate<T>& _input) {
187  for(int32_t yyy = 0; yyy < _input.getSize().y() && _pos.y() + yyy < m_size.y(); ++yyy) {
188  for(int32_t xxx = 0; xxx < _input.getSize().x() && _pos.x() + xxx < m_size.x(); ++xxx) {
189  set(ivec2(_pos.x()+xxx, _pos.y()+yyy), _input.get(ivec2(xxx, yyy)) );
190  }
191  }
192  }
198  void scale(const ivec2& _size) {
199  // TODO : Add capabilities ...
200  int32_t stepX = m_size.x() / _size.x();
201  int32_t stepY = m_size.y() / _size.y();
202  stepX = std::max(1, stepX);
203  stepY = std::max(1, stepY);
204  EGAMI_VERBOSE("move : " << stepX << " , " << stepY << " from : " << m_size << " ==> " << _size);
205  for (int32_t yyy = 0; yyy < _size.y(); ++yyy) {
206  for (int32_t xxx = 0; xxx < _size.x(); ++xxx) {
207  set(ivec2(xxx, yyy), get(ivec2(xxx*stepX, yyy*stepY)));
208  }
209  }
210  resize(_size);
211  }
212  void set(const std::vector<etk::Color<float,4>>& _data, const ivec2& _size) {
213  m_data.clear();
214  m_size = _size;
215  m_data.resize(_data.size());
216  for (size_t iii=0; iii<m_data.size(); ++iii) {
217  m_data[iii] = _data[iii];
218  }
219  }
220  void set(const std::vector<etk::Color<uint8_t,4>>& _data, const ivec2& _size) {
221  m_data.clear();
222  m_size = _size;
223  m_data.resize(_data.size());
224  for (size_t iii=0; iii<m_data.size(); ++iii) {
225  m_data[iii] = _data[iii];
226  }
227  }
228  };
229  template <> enum colorType ImageTemplate<etk::Color<uint8_t>>::getType() const {
230  return egami::colorType::RGBA8;
231  }
232  template <> enum colorType ImageTemplate<etk::Color<uint8_t, 3>>::getType() const {
233  return egami::colorType::RGB8;
234  }
235  template <> enum colorType ImageTemplate<etk::Color<float>>::getType() const {
236  return egami::colorType::RGBAf;
237  }
238  template <> enum colorType ImageTemplate<etk::Color<float, 3>>::getType() const {
239  return egami::colorType::RGBf;
240  }
241  template <> enum colorType ImageTemplate<etk::Color<uint16_t, 1>>::getType() const {
242  return egami::colorType::unsignedInt16;
243  }
244  template <> enum colorType ImageTemplate<etk::Color<uint32_t, 1>>::getType() const {
245  return egami::colorType::unsignedInt32;
246  }
247  template <> enum colorType ImageTemplate<etk::Color<float, 1>>::getType() const {
248  return egami::colorType::float32;
249  }
250  template <> enum colorType ImageTemplate<etk::Color<double, 1>>::getType() const {
251  return egami::colorType::float64;
252  }
253 };
254 
const T & x() const
Definition: debug.hpp:10
Definition: ImagePrivate.hpp:18
const T & y() const
void scale(const ivec2 &_size)
Scale an image in an other dimention.
Definition: ImagePrivate.hpp:198
Definition: Image.hpp:30