-
Notifications
You must be signed in to change notification settings - Fork 2
/
canvas.hpp
69 lines (58 loc) · 1.17 KB
/
canvas.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#ifndef CANVAS_HPP
#define CANVAS_HPP
#include <stdint.h>
#if __BYTE_ORDER == __LITTLE_ENDIAN
#define SWAP_RGBA
#elif __BYTE_ORDER != __BIG_ENDIAN
#error Unknown endianness
#endif
class Canvas
{
public:
Canvas()
{
}
Canvas(int width, int height, uint32_t* data)
: width(width)
, height(height)
, data(data)
{
}
inline void set(int x, int y, uint32_t rgbx)
{
data[width * y + x] = convert(rgbx);
}
inline void blend(int x, int y, uint32_t rgba)
{
uint8_t alpha = rgba;
uint32_t back = data[width * y + x];
rgba = alphaMultiply(convert(rgba), alpha) + alphaMultiply(back, 255 - alpha);
data[width * y + x] = rgba;
}
unsigned width;
unsigned height;
uint32_t* data;
private:
inline uint32_t alphaMultiply(uint32_t rgbx, uint8_t alpha)
{
#ifndef SWAP_RGBA
rgbx >>= 8;
#endif
uint32_t rb = ((rgbx & 0x00FF00FF) * (alpha + 1)) & 0xFF00FF00;
uint32_t g = ((rgbx & 0xFF00) * (alpha + 1)) & 0x00FF0000;
rgbx = rb | g;
#ifdef SWAP_RGBA
rgbx >>= 8;
#endif
return rgbx;
}
inline uint32_t convert(uint32_t color)
{
#ifdef SWAP_RGBA
return __builtin_bswap32(color);
#else
return color;
#endif
}
};
#endif