Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoBuffer.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoBuffer
3 
4 Written by Michael C. Miller.
5 
6 I invest time and resources providing this open source code,
7 please support me by dontating (see https://github.com/Makuna)
8 
9 -------------------------------------------------------------------------
10 This file is part of the LUMITRONIX_iFlex_Workshop library.
11 
12 LumitronixIFlexBus is free software: you can redistribute it and/or modify
13 it under the terms of the GNU Lesser General Public License as
14 published by the Free Software Foundation, either version 3 of
15 the License, or (at your option) any later version.
16 
17 LumitronixIFlexBus is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU Lesser General Public License for more details.
21 
22 You should have received a copy of the GNU Lesser General Public
23 License along with LumitronixIFlex. If not, see
24 <http://www.gnu.org/licenses/>.
25 -------------------------------------------------------------------------*/
26 #pragma once
27 
28 // T_BUFFER_METHOD - one of
29 // NeoBufferMethod
30 // NeoBufferProgmemMethod
31 //
32 template<typename T_BUFFER_METHOD> class NeoBuffer
33 {
34 public:
35  NeoBuffer(uint16_t width,
36  uint16_t height,
37  PGM_VOID_P pixels = nullptr) :
38  _method(width, height, pixels)
39  {
40  }
41 
43  {
44  }
45 
47  {
48  return _method;
49  }
50 
51  uint16_t PixelCount() const
52  {
53  return _method.PixelCount();
54  };
55 
56  uint16_t Width() const
57  {
58  return _method.Width();
59  };
60 
61  uint16_t Height() const
62  {
63  return _method.Height();
64  };
65 
67  int16_t x,
68  int16_t y,
69  typename T_BUFFER_METHOD::ColorObject color)
70  {
71  _method.SetPixelColor(PixelIndex(x, y), color);
72  };
73 
74  typename T_BUFFER_METHOD::ColorObject GetPixelColor(
75  int16_t x,
76  int16_t y) const
77  {
78  return _method.GetPixelColor(PixelIndex(x, y));
79  };
80 
81  void ClearTo(typename T_BUFFER_METHOD::ColorObject color)
82  {
83  _method.ClearTo(color);
84  };
85 
87  uint16_t indexPixel)
88  {
89  uint16_t destPixelCount = destBuffer.PixelCount();
90  // validate indexPixel
91  if (indexPixel >= destPixelCount)
92  {
93  return;
94  }
95 
96  // calc how many we can copy
97  uint16_t copyCount = destPixelCount - indexPixel;
98  uint16_t srcPixelCount = PixelCount();
99  if (copyCount > srcPixelCount)
100  {
101  copyCount = srcPixelCount;
102  }
103 
104  uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexPixel);
105  _method.CopyPixels(pDest, _method.Pixels(), copyCount);
106  }
107 
109  int16_t xDest,
110  int16_t yDest,
111  int16_t xSrc,
112  int16_t ySrc,
113  int16_t wSrc,
114  int16_t hSrc,
115  LayoutMapCallback layoutMap)
116  {
117  uint16_t destPixelCount = destBuffer.PixelCount();
118 
119  for (int16_t y = 0; y < hSrc; y++)
120  {
121  for (int16_t x = 0; x < wSrc; x++)
122  {
123  uint16_t indexDest = layoutMap(xDest + x, yDest + y);
124 
125  if (indexDest < destPixelCount)
126  {
127  const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), PixelIndex(xSrc + x, ySrc + y));
128  uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexDest);
129 
130  _method.CopyPixels(pDest, pSrc, 1);
131  }
132  }
133  }
134  }
135 
137  int16_t xDest,
138  int16_t yDest,
139  LayoutMapCallback layoutMap)
140  {
141  Blt(destBuffer, xDest, yDest, 0, 0, Width(), Height(), layoutMap);
142  }
143 
144  template <typename T_SHADER> void Render(NeoBufferContext<typename T_BUFFER_METHOD::ColorFeature> destBuffer, T_SHADER& shader)
145  {
146  uint16_t countPixels = destBuffer.PixelCount();
147 
148  if (countPixels > _method.PixelCount())
149  {
150  countPixels = _method.PixelCount();
151  }
152 
153  for (uint16_t indexPixel = 0; indexPixel < countPixels; indexPixel++)
154  {
155  const uint8_t* pSrc = T_BUFFER_METHOD::ColorFeature::getPixelAddress(_method.Pixels(), indexPixel);
156  uint8_t* pDest = T_BUFFER_METHOD::ColorFeature::getPixelAddress(destBuffer.Pixels, indexPixel);
157 
158  shader.Apply(indexPixel, pDest, pSrc);
159  }
160  }
161 
162  uint16_t PixelIndex(
163  int16_t x,
164  int16_t y) const
165  {
166  uint16_t result = PixelIndex_OutOfBounds;
167 
168  if (x >= 0 &&
169  static_cast<uint16_t>(x) < Width() &&
170  y >= 0 &&
171  static_cast<uint16_t>(y) < Height())
172  {
173  result = x + y * Width();
174  }
175  return result;
176  }
177 
178 private:
179  T_BUFFER_METHOD _method;
180 };
std::function< uint16_t(int16_t x, int16_t y)> LayoutMapCallback
Definition: LayoutMapCallback.h:39
const uint16_t PixelIndex_OutOfBounds
Definition: LumitronixIFlexBus.h:33
#define PGM_VOID_P
Definition: NeoUtil.h:42
Definition: NeoBuffer.h:33
void Blt(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, int16_t xDest, int16_t yDest, LayoutMapCallback layoutMap)
Definition: NeoBuffer.h:136
uint16_t PixelIndex(int16_t x, int16_t y) const
Definition: NeoBuffer.h:162
uint16_t Height() const
Definition: NeoBuffer.h:61
uint16_t PixelCount() const
Definition: NeoBuffer.h:51
NeoBuffer(uint16_t width, uint16_t height, PGM_VOID_P pixels=nullptr)
Definition: NeoBuffer.h:35
~NeoBuffer()
Definition: NeoBuffer.h:42
void Render(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, T_SHADER &shader)
Definition: NeoBuffer.h:144
void SetPixelColor(int16_t x, int16_t y, typename T_BUFFER_METHOD::ColorObject color)
Definition: NeoBuffer.h:66
void Blt(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, uint16_t indexPixel)
Definition: NeoBuffer.h:86
T_BUFFER_METHOD::ColorObject GetPixelColor(int16_t x, int16_t y) const
Definition: NeoBuffer.h:74
void Blt(NeoBufferContext< typename T_BUFFER_METHOD::ColorFeature > destBuffer, int16_t xDest, int16_t yDest, int16_t xSrc, int16_t ySrc, int16_t wSrc, int16_t hSrc, LayoutMapCallback layoutMap)
Definition: NeoBuffer.h:108
void ClearTo(typename T_BUFFER_METHOD::ColorObject color)
Definition: NeoBuffer.h:81
uint16_t Width() const
Definition: NeoBuffer.h:56
Definition: NeoBufferContext.h:32
uint8_t * Pixels
Definition: NeoBufferContext.h:43
uint16_t PixelCount() const
Definition: NeoBufferContext.h:40