Blend.h
Go to the documentation of this file.
1 /****
2  * Blend.h
3  *
4  * Copyright 2021 mikee47 <mike@sillyhouse.net>
5  *
6  * This file is part of the Sming-Graphics Library
7  *
8  * This library is free software: you can redistribute it and/or modify it under the terms of the
9  * GNU General Public License as published by the Free Software Foundation, version 3 or later.
10  *
11  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
12  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13  * See the GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along with this library.
16  * If not, see <https://www.gnu.org/licenses/>.
17  *
18  * @author: May 2021 - mikee47 <mike@sillyhouse.net>
19  *
20  ****/
21 
22 #pragma once
23 
24 #include "Asset.h"
25 
26 namespace Graphics
27 {
28 #define GRAPHICS_BLENDMODE_MAP(XX) \
29  XX(Write, "Write normally") \
30  XX(Xor, "dst = dst XOR src0") \
31  XX(XNor, "dst = dst XOR (NOT src)") \
32  XX(Transparent, "Make nominated colour transparent") \
33  XX(Alpha, "Blend using alpha value")
34 
40 class Blend : public AssetTemplate<AssetType::Blend>
41 {
42 public:
43  enum class Mode {
44 #define XX(name, desc) name,
46 #undef XX
47  };
48 
49  virtual Mode mode() const = 0;
50  virtual void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const = 0;
51  virtual void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const = 0;
52 
53  /* Meta */
54 
55  void write(MetaWriter& meta) const override;
56 };
57 
59 
60 template <BlendMode blendMode> class BlendTemplate : public Blend
61 {
62 public:
63  Mode mode() const override
64  {
65  return blendMode;
66  }
67 };
68 
69 class BlendWrite : public BlendTemplate<BlendMode::Write>
70 {
71 public:
72  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override
73  {
74  writeColor(dstptr, src, format, length / getBytesPerPixel(format));
75  }
76 
77  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override
78  {
79  (void)format;
80  memcpy(dstptr, srcptr, length);
81  }
82 };
83 
84 class BlendXor : public BlendTemplate<BlendMode::Xor>
85 {
86 public:
87  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override;
88  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override;
89 };
90 
91 class BlendXNor : public BlendTemplate<BlendMode::XNor>
92 {
93 public:
94  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override;
95  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override;
96 };
97 
98 class BlendTransparent : public BlendTemplate<BlendMode::Transparent>
99 {
100 public:
102  {
103  }
104 
105  void write(MetaWriter& meta) const override
106  {
107  Blend::write(meta);
108  meta.write("key", key);
109  }
110 
111  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override
112  {
113  // Makes no sense for this blender
114  }
115 
116  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override;
117 
118 private:
119  Color key;
120 };
121 
122 class BlendAlpha : public BlendTemplate<BlendMode::Alpha>
123 {
124 public:
125  BlendAlpha(uint8_t alpha) : alpha(alpha)
126  {
127  }
128 
129  BlendAlpha(Color color) : alpha(getAlpha(color))
130  {
131  }
132 
133  BlendAlpha(PackedColor color) : alpha(color.alpha)
134  {
135  }
136 
137  void write(MetaWriter& meta) const override
138  {
139  Blend::write(meta);
140  meta.write("alpha", alpha);
141  }
142 
143  static PackedColor transform(PixelFormat format, PackedColor src, PackedColor dst);
144  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length);
145  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length, uint8_t alpha);
146  static uint16_t IRAM_ATTR blendRGB565(uint16_t src, uint16_t dst, uint8_t alpha);
147  static void IRAM_ATTR blendRGB565(uint16_t src, uint8_t* dstptr, size_t length, uint8_t alpha);
148  static void IRAM_ATTR blendRGB565(const uint8_t* srcptr, uint8_t* dstptr, size_t length, uint8_t alpha);
149  static uint8_t blendChannel(uint8_t fg, uint8_t bg, uint8_t alpha);
150  static void blendRGB24(PackedColor src, uint8_t* dstptr, size_t length);
151  static PixelBuffer blendColor(PixelBuffer fg, PixelBuffer bg, uint8_t alpha);
152 
153  Color blendColor(Color fg, Color bg, uint8_t alpha) const
154  {
155  return blendColor(PixelBuffer{fg}, PixelBuffer{bg}, alpha).color;
156  }
157 
158  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override
159  {
160  return blend(format, src, dstptr, length);
161  }
162 
163  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override
164  {
165  return blend(format, srcptr, dstptr, length, alpha);
166  }
167 
168 private:
169  uint16_t blendRGB565(uint16_t src, uint16_t dst) const
170  {
171  return blendRGB565(src, dst, alpha);
172  }
173 
174  uint8_t blendChannel(uint8_t fg, uint8_t bg) const
175  {
176  return blendChannel(fg, bg, alpha);
177  }
178 
179  PixelBuffer blendColor(PixelBuffer fg, PixelBuffer bg) const
180  {
181  return blendColor(fg, bg, alpha);
182  }
183 
184  uint8_t alpha;
185 };
186 
187 } // namespace Graphics
188 
Definition: Blend.h:60
void transform(PixelFormat format, const uint8_t *srcptr, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:163
Mode mode() const override
Definition: Blend.h:63
Definition: Blend.h:91
Definition: Blend.h:98
void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:111
Mode
Definition: Blend.h:43
void write(MetaWriter &meta) const override
Definition: Blend.h:105
BlendTransparent(Color key)
Definition: Blend.h:101
Colour in device pixel format.
Definition: Colors.h:339
size_t writeColor(void *buffer, PackedColor color, PixelFormat format)
Store a packed colour value into memory.
String toString(Graphics::BlendMode mode)
void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:72
The String class.
Definition: WString.h:136
Color blendColor(Color fg, Color bg, uint8_t alpha) const
Definition: Blend.h:153
void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:158
Definition: Asset.h:125
Definition: Blend.h:84
void write(MetaWriter &meta) const override
Writes object content in readable format for debugging.
Definition: Meta.h:47
void size_t const void * key
Definition: blake2s.h:33
BlendAlpha(PackedColor color)
Definition: Blend.h:133
Color
Standard colour definitions.
Definition: Colors.h:227
BlendAlpha(Color color)
Definition: Blend.h:129
void write(MetaWriter &meta) const override
Definition: Blend.h:137
virtual void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const =0
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
Definition: Blend.h:122
Definition: Blend.h:69
PixelFormat
Definition: Colors.h:295
std::enable_if< std::is_base_of< Meta, T >::value, void >::type write(const String &name, const T &value)
Definition: Meta.h:55
Structure used to perform pixel format conversions.
Definition: Colors.h:347
Blend operations.
Definition: Blend.h:40
BlendAlpha(uint8_t alpha)
Definition: Blend.h:125
#define GRAPHICS_BLENDMODE_MAP(XX)
Definition: Blend.h:28
Definition: Virtual.h:30
virtual Mode mode() const =0
void transform(PixelFormat format, const uint8_t *srcptr, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:77
constexpr uint8_t getAlpha(Color color)
Definition: Colors.h:257