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(Mask, "dst = dst AND src") \
33  XX(Transparent, "Make nominated colour transparent") \
34  XX(Alpha, "Blend using alpha value")
35 
41 class Blend : public AssetTemplate<AssetType::Blend>
42 {
43 public:
44  enum class Mode {
45 #define XX(name, desc) name,
47 #undef XX
48  };
49 
50  virtual Mode mode() const = 0;
51  virtual void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const = 0;
52  virtual void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const = 0;
53 
54  /* Meta */
55 
56  void write(MetaWriter& meta) const override;
57 };
58 
59 using BlendMode = Blend::Mode;
60 
61 template <class Class, BlendMode blendMode> class BlendTemplate : public Blend
62 {
63 public:
64  Mode mode() const override
65  {
66  return blendMode;
67  }
68 
69  void transform(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length) const override
70  {
71  auto self = static_cast<const Class*>(this);
72  return self->blend(format, src, dstptr, length);
73  }
74 
75  void transform(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const override
76  {
77  auto self = static_cast<const Class*>(this);
78  return self->blend(format, srcptr, dstptr, length);
79  }
80 };
81 
82 class BlendWrite : public BlendTemplate<BlendWrite, BlendMode::Write>
83 {
84 public:
85  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length)
86  {
87  writeColor(dstptr, src, format, length / getBytesPerPixel(format));
88  }
89 
90  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length)
91  {
92  (void)format;
93  memcpy(dstptr, srcptr, length);
94  }
95 };
96 
97 class BlendXor : public BlendTemplate<BlendXor, BlendMode::Xor>
98 {
99 public:
100  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length);
101  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length);
102 };
103 
104 class BlendXNor : public BlendTemplate<BlendXNor, BlendMode::XNor>
105 {
106 public:
107  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length);
108  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length);
109 };
110 
111 class BlendMask : public BlendTemplate<BlendMask, BlendMode::Mask>
112 {
113 public:
114  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length);
115  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length);
116 };
117 
118 class BlendTransparent : public BlendTemplate<BlendTransparent, BlendMode::Transparent>
119 {
120 public:
121  BlendTransparent(Color key) : key(key)
122  {
123  }
124 
125  void write(MetaWriter& meta) const override
126  {
127  Blend::write(meta);
128  meta.write("key", key);
129  }
130 
131  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length)
132  {
133  // Makes no sense for this blender
134  }
135 
136  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length, Color key);
137 
138  void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const
139  {
140  blend(format, srcptr, dstptr, length, key);
141  }
142 
143 private:
145 };
146 
147 class BlendAlpha : public BlendTemplate<BlendAlpha, BlendMode::Alpha>
148 {
149 public:
150  BlendAlpha(uint8_t alpha) : alpha(alpha)
151  {
152  }
153 
154  BlendAlpha(Color color) : alpha(getAlpha(color))
155  {
156  }
157 
158  BlendAlpha(PackedColor color) : alpha(color.alpha)
159  {
160  }
161 
162  void write(MetaWriter& meta) const override
163  {
164  Blend::write(meta);
165  meta.write("alpha", alpha);
166  }
167 
168  static PackedColor blend(PixelFormat format, PackedColor src, PackedColor dst);
169  static void blend(PixelFormat format, PackedColor src, uint8_t* dstptr, size_t length);
170  static void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length, uint8_t alpha);
171  static uint16_t IRAM_ATTR blendRGB565(uint16_t src, uint16_t dst, uint8_t alpha);
172  static void IRAM_ATTR blendRGB565(uint16_t src, uint8_t* dstptr, size_t length, uint8_t alpha);
173  static void IRAM_ATTR blendRGB565(const uint8_t* srcptr, uint8_t* dstptr, size_t length, uint8_t alpha);
174  static uint8_t blendChannel(uint8_t fg, uint8_t bg, uint8_t alpha);
175  static void blendRGB24(PackedColor src, uint8_t* dstptr, size_t length);
176  static PixelBuffer blendColor(PixelBuffer fg, PixelBuffer bg, uint8_t alpha);
177 
178  void blend(PixelFormat format, const uint8_t* srcptr, uint8_t* dstptr, size_t length) const
179  {
180  blend(format, srcptr, dstptr, length, alpha);
181  }
182 
183  Color blendColor(Color fg, Color bg, uint8_t alpha) const
184  {
185  return blendColor(PixelBuffer{fg}, PixelBuffer{bg}, alpha).color;
186  }
187 
188 private:
189  uint16_t blendRGB565(uint16_t src, uint16_t dst) const
190  {
191  return blendRGB565(src, dst, alpha);
192  }
193 
194  uint8_t blendChannel(uint8_t fg, uint8_t bg) const
195  {
196  return blendChannel(fg, bg, alpha);
197  }
198 
200  {
201  return blendColor(fg, bg, alpha);
202  }
203 
204  uint8_t alpha;
205 };
206 
207 } // namespace Graphics
208 
static void blend(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length)
Definition: Blend.h:150
virtual Mode mode() const =0
uint8_t getBytesPerPixel(PixelFormat format)
Get number of bytes required to store a pixel in the given format.
Definition: Colors.h:331
static void blendRGB24(PackedColor src, uint8_t *dstptr, size_t length)
Mode
Definition: Blend.h:63
static void blend(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length)
static uint8_t blendChannel(uint8_t fg, uint8_t bg, uint8_t alpha)
The String class.
Definition: WString.h:136
Writes object content in readable format for debugging.
Definition: Meta.h:66
static void blend(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length)
static PackedColor blend(PixelFormat format, PackedColor src, PackedColor dst)
void write(MetaWriter &meta) const override
Definition: Blend.h:181
std::enable_if< std::is_base_of< Meta, T >::value, void >::type write(const String &name, const T &value)
Definition: Meta.h:74
void write(MetaWriter &meta) const override
constexpr uint8_t getAlpha(Color color)
Definition: Colors.h:257
Definition: Virtual.h:30
static uint16_t blendRGB565(uint16_t src, uint16_t dst, uint8_t alpha)
BlendAlpha(uint8_t alpha)
Definition: Blend.h:169
Definition: Blend.h:137
Blend::Mode BlendMode
Definition: Blend.h:78
String toString(Graphics::BlendMode mode)
size_t writeColor(void *buffer, PackedColor color, PixelFormat format)
Store a packed colour value into memory.
void size_t const void * key
Definition: blake2s.h:33
void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const override
Definition: Blend.h:88
Blend operations.
Definition: Blend.h:60
static void blend(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length)
Definition: Blend.h:104
PixelFormat
Definition: Colors.h:295
Mode mode() const override
Definition: Blend.h:83
#define GRAPHICS_BLENDMODE_MAP(XX)
Definition: Blend.h:47
virtual void transform(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length) const =0
BlendTransparent(Color key)
Definition: Blend.h:140
Structure used to perform pixel format conversions.
Definition: Colors.h:347
Definition: Blend.h:123
Definition: Blend.h:80
static void blend(PixelFormat format, PackedColor src, uint8_t *dstptr, size_t length)
void write(MetaWriter &meta) const override
Definition: Blend.h:144
Colour in device pixel format.
Definition: Colors.h:339
Definition: Blend.h:166
Definition: Blend.h:130
Definition: Blend.h:101
static PixelBuffer blendColor(PixelBuffer fg, PixelBuffer bg, uint8_t alpha)
Color
Standard colour definitions.
Definition: Colors.h:227