Lumitronix_Iflex_Pro_Workshop
Library to interact with the iFlexPro
NeoByteElements.h
Go to the documentation of this file.
1 /*-------------------------------------------------------------------------
2 NeoByteElements provides feature base classes to describe color elements
3 for LumitronixIFlexBus Color Feature template classes
4 
5 Written by Michael C. Miller.
6 
7 I invest time and resources providing this open source code,
8 please support me by dontating (see https://github.com/Makuna)
9 
10 -------------------------------------------------------------------------
11 This file is part of the LUMITRONIX_iFlex_Workshop library.
12 
13 LumitronixIFlexBus is free software: you can redistribute it and/or modify
14 it under the terms of the GNU Lesser General Public License as
15 published by the Free Software Foundation, either version 3 of
16 the License, or (at your option) any later version.
17 
18 LumitronixIFlexBus is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU Lesser General Public License for more details.
22 
23 You should have received a copy of the GNU Lesser General Public
24 License along with LumitronixIFlex. If not, see
25 <http://www.gnu.org/licenses/>.
26 -------------------------------------------------------------------------*/
27 
28 #pragma once
29 
30 // NeoElementsBase contains common methods used by features to map and
31 // copy pixel memory data in native stream format
32 //
33 // V_PIXEL_SIZE - the size in bytes of a pixel in the data stream
34 // T_COLOR_OBJECT - the primary color object used to represent a pixel
35 // T_COPY - (uint8_t/uint16_t/uint32_t) the base type to use when copying/moving
36 template<size_t V_PIXEL_SIZE, typename T_COLOR_OBJECT, typename T_COPY>
38 {
39 public:
40  static const size_t PixelSize = V_PIXEL_SIZE;
41  typedef T_COLOR_OBJECT ColorObject;
42 
43  static uint8_t* getPixelAddress(uint8_t* pPixels, uint16_t indexPixel)
44  {
45  return pPixels + indexPixel * PixelSize;
46  }
47  static const uint8_t* getPixelAddress(const uint8_t* pPixels, uint16_t indexPixel)
48  {
49  return pPixels + indexPixel * PixelSize;
50  }
51 
52  static void replicatePixel(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
53  {
54  T_COPY* pDest = reinterpret_cast<T_COPY*>(pPixelDest);
55  T_COPY* pEnd = pDest + (count * PixelSize / sizeof(T_COPY));
56  const T_COPY* pEndSrc = reinterpret_cast<const T_COPY*>(pPixelSrc) + PixelSize / sizeof(T_COPY);
57 
58  while (pDest < pEnd)
59  {
60  const T_COPY* pSrc = reinterpret_cast<const T_COPY*>(pPixelSrc);
61  while (pSrc < pEndSrc)
62  {
63  *pDest++ = *pSrc++;
64  }
65  }
66  }
67 
68  static void movePixelsInc(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
69  {
70  const T_COPY* pSrc = reinterpret_cast<const T_COPY*>(pPixelSrc);
71  T_COPY* pDest = reinterpret_cast<T_COPY*>(pPixelDest);
72  T_COPY* pEnd = pDest + (count * PixelSize / sizeof(T_COPY));
73 
74  while (pDest < pEnd)
75  {
76  *pDest++ = *pSrc++;
77  }
78  }
79 
80  static void movePixelsDec(uint8_t* pPixelDest, const uint8_t* pPixelSrc, uint16_t count)
81  {
82  const T_COPY* pSrc = reinterpret_cast<const T_COPY*>(pPixelSrc);
83  const T_COPY* pSrcBack = pSrc + (count * PixelSize / sizeof(T_COPY));
84  T_COPY* pDest = reinterpret_cast<T_COPY*>(pPixelDest);
85  T_COPY* pDestBack = pDest + (count * PixelSize / sizeof(T_COPY));
86 
87  while (pDestBack > pDest)
88  {
89  *--pDestBack = *--pSrcBack;
90  }
91  }
92 };
93 
94 // NeoByteElements is used for 8bit color element types and less
95 //
96 // V_PIXEL_SIZE - the size in bytes of a pixel in the data stream
97 // T_COLOR_OBJECT - the primary color object used to represent a pixel
98 // T_COPY - (uint8_t/uint16_t/uint32_t) the base type to use when copying/moving
99 template<size_t V_PIXEL_SIZE, typename T_COLOR_OBJECT, typename T_COPY>
100 class NeoByteElements : public NeoElementsBase<V_PIXEL_SIZE, T_COLOR_OBJECT, T_COPY>
101 {
102 public:
103 
104  static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count)
105  {
106  uint8_t* pEnd = pPixelDest + (count * NeoElementsBase<V_PIXEL_SIZE, T_COLOR_OBJECT, T_COPY>::PixelSize);
107  const uint8_t* pSrc = reinterpret_cast<const uint8_t*>(pPixelSrc);
108 
109  while (pPixelDest < pEnd)
110  {
111  *pPixelDest++ = pgm_read_byte(pSrc++);
112  }
113  }
114 };
115 
116 // NeoWordElements is used for 16bit color element types
117 //
118 // V_PIXEL_SIZE - the size in bytes of a pixel in the data stream
119 // T_COLOR_OBJECT - the primary color object used to represent a pixel
120 // T_COPY - (uint16_t/uint32_t) the base type to use when copying/moving
121 template<size_t V_PIXEL_SIZE, typename T_COLOR_OBJECT, typename T_COPY>
122 class NeoWordElements : public NeoElementsBase<V_PIXEL_SIZE, T_COLOR_OBJECT, T_COPY>
123 {
124 public:
125 
126  static void movePixelsInc_P(uint8_t* pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count)
127  {
128  uint16_t* pDest = reinterpret_cast<uint16_t*>(pPixelDest);
129  uint16_t* pEnd = pDest + (count * NeoElementsBase<V_PIXEL_SIZE, T_COLOR_OBJECT, T_COPY>::PixelSize / sizeof(uint16_t));
130  const uint16_t* pSrc = reinterpret_cast<const uint16_t*>(pPixelSrc);
131 
132  while (pDest < pEnd)
133  {
134  *pDest++ = pgm_read_word(pSrc++);
135  }
136  }
137 };
138 
139 
#define PGM_VOID_P
Definition: NeoUtil.h:42
Definition: NeoByteElements.h:101
static void movePixelsInc_P(uint8_t *pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count)
Definition: NeoByteElements.h:104
Definition: NeoByteElements.h:38
static void replicatePixel(uint8_t *pPixelDest, const uint8_t *pPixelSrc, uint16_t count)
Definition: NeoByteElements.h:52
static const uint8_t * getPixelAddress(const uint8_t *pPixels, uint16_t indexPixel)
Definition: NeoByteElements.h:47
static uint8_t * getPixelAddress(uint8_t *pPixels, uint16_t indexPixel)
Definition: NeoByteElements.h:43
static void movePixelsDec(uint8_t *pPixelDest, const uint8_t *pPixelSrc, uint16_t count)
Definition: NeoByteElements.h:80
T_COLOR_OBJECT ColorObject
Definition: NeoByteElements.h:41
static void movePixelsInc(uint8_t *pPixelDest, const uint8_t *pPixelSrc, uint16_t count)
Definition: NeoByteElements.h:68
static const size_t PixelSize
Definition: NeoByteElements.h:40
Definition: NeoByteElements.h:123
static void movePixelsInc_P(uint8_t *pPixelDest, PGM_VOID_P pPixelSrc, uint16_t count)
Definition: NeoByteElements.h:126