summaryrefslogtreecommitdiffstats
path: root/bitmap.h
blob: c707c1347a1d460dd4060e0e9a771cdd71dd3892 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#ifndef BITMAP_H_
#define BITMAP_H_

#include "util.h"
#include "font.h"

#include <stdint.h>
#include <string.h>


enum bitmap_rotation {
	BITMAP_ROT_0DEG,
	BITMAP_ROT_90DEG,
	BITMAP_ROT_180DEG,
	BITMAP_ROT_270DEG,
};

/** Bitmap Deskriptor */
struct bitmap {
	/** Breite des Bitmaps. */
	uint8_t width;
	/** Hoehe des Bitmaps. */
	uint8_t height;
	/** Rotation aller Zeichenoperationen. */
	enum bitmap_rotation rotation;
	/** Datenbereich folgt dem "struct bitmap" unmittelbar. */
	uint8_t data[0];
};

/** \brief	Bitmap und Container definieren.
 *
 * \param _name		Name des Bitmap Containers.
 *
 * \param _width	Breite, in Pixeln.
 *
 * \param _height	Hoehe, in Pixeln. Muss ein Vielfaches von 8 sein.
 */
#define DEFINE_BITMAP(_name, _width, _height)				\
	struct bitmap_container__##_name {				\
		struct bitmap bitmap;					\
		uint8_t data[div_round_up((_height), 8) * (_width)];	\
	} _name##_container = {						\
		.bitmap		= {					\
			.width		= _width,			\
			.height		= _height,			\
			.rotation	= BITMAP_ROT_0DEG,		\
		},							\
		.data = { },						\
	}

/** \brief	Gibt einen "struct bitmap" pointer auf ein bitmap zurueck.
 *
 * \param _name		Name des Bitmap Containers.
 */
#define BITMAP_PTR(_name)	(&(_name##_container.bitmap))

/** \brief	Gibt einen Pointer auf ein Spaltenbyte zurueck.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x		X Koordinate.
 *
 * \param y		Y Koordinate.
 */
#define bitmap_column_byte(bitmap, x, y)				\
		(&(bitmap)->data[(y) / 8 * (bitmap)->width + (x)])

uint8_t bitmap_width(struct bitmap *bitmap);

uint8_t bitmap_height(struct bitmap *bitmap);

/** \brief	Bitmap loeschen.
 *
 * \param bitmap	Das Bitmap.
 */
static inline void bitmap_clear(struct bitmap *bitmap)
{
	memset(bitmap->data, 0,
	       div_round_up(bitmap->height, 8) * bitmap->width);
}

void bitmap_pixel_write(struct bitmap *bitmap,
			uint8_t x, uint8_t y, bool on);

/** \brief	Pixel in Bitmap setzen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x		X Koordinate des Pixels.
 *
 * \param y		Y Koordinate des Pixels.
 */
static inline void bitmap_pixel_set(struct bitmap *bitmap,
				    uint8_t x, uint8_t y)
{
	bitmap_pixel_write(bitmap, x, y, 1);
}

/** \brief	Pixel in Bitmap loeschen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x		X Koordinate des Pixels.
 *
 * \param y		Y Koordinate des Pixels.
 */
static inline void bitmap_pixel_clear(struct bitmap *bitmap,
				      uint8_t x, uint8_t y)
{
	bitmap_pixel_write(bitmap, x, y, 0);
}

/** \brief	Pixelwert aus einem Bitmap auslesen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x		X Koordinate des Pixels.
 *
 * \param y		Y Koordinate des Pixels.
 */
static inline bool bitmap_pixel_get(const struct bitmap *bitmap,
				    uint8_t x, uint8_t y)
{
	uint8_t y_bit = y % 8;
	const uint8_t b = *bitmap_column_byte(bitmap, x, y);

	return !!(b & BITMASK8(y_bit));
}

void bitmap_area_fill(struct bitmap *bitmap,
		      uint8_t area_x, uint8_t area_y,
		      uint8_t area_width, uint8_t area_height,
		      bool black);

void bitmap_draw_line(struct bitmap *bitmap,
		      uint8_t x_from, uint8_t y_from,
		      uint8_t x_to, uint8_t y_to,
		      bool black);

void bitmap_draw_xbm(struct bitmap *bitmap,
		     uint8_t x_pos, uint8_t y_pos,
		     const uint8_t __flash *xbm,
		     unsigned int xbm_width, unsigned int xbm_height);

void bitmap_draw_char(struct bitmap *bitmap,
		      uint8_t x_pos, uint8_t y_pos,
		      const struct font_descriptor __flash *font_desc,
		      char ascii);

void bitmap_draw_string(struct bitmap *bitmap,
			uint8_t x_pos, uint8_t y_pos,
			const struct font_descriptor __flash *font_desc,
			const char __memx *string);

#endif /* BITMAP_H_ */
bues.ch cgit interface