summaryrefslogtreecommitdiffstats
path: root/bitmap.c
blob: 8a1ee5d3aca7cad11600fe31e4b4d4075fccff81 (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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
 * \file bitmap.c
 * \brief Bitmap manipulation support
 *
 * \subsection Copyright
 * Copyright (c) 2013 Michael Buesch <m@bues.ch>
 *
 * \subsection License
 * Licensed under the terms of the GNU General Public License version 2.
 */

#include "bitmap.h"
#include "font.h"

#include <avr/pgmspace.h>


/** \brief	Gibt die Breite eines Bitmaps zurueck.
 *		Beruecksichtigt die Rotation.
 */
uint8_t bitmap_width(struct bitmap *bitmap)
{
	if (bitmap->rotation == BITMAP_ROT_0DEG ||
	    bitmap->rotation == BITMAP_ROT_180DEG)
		return bitmap->width;
	return bitmap->height;
}

/** \brief	Gibt die Hoehe eines Bitmaps zurueck.
 *		Beruecksichtigt die Rotation.
 */
uint8_t bitmap_height(struct bitmap *bitmap)
{
	if (bitmap->rotation == BITMAP_ROT_0DEG ||
	    bitmap->rotation == BITMAP_ROT_180DEG)
		return bitmap->height;
	return bitmap->width;
}

/** \brief	Pixel in Bitmap beschreiben.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x		X Koordinate des Pixels.
 *
 * \param y		Y Koordinate des Pixels.
 *
 * \param on		Der neue Pixelwert. (1/0)
 */
void bitmap_pixel_write(struct bitmap *bitmap,
			uint8_t x, uint8_t y, bool on)
{
	uint8_t y_bit, *b, tmp_x, mask;

	/* Koordinaten nach Rotation uebersetzen. */
	switch (bitmap->rotation) {
	case BITMAP_ROT_0DEG:
	default:
		break;
	case BITMAP_ROT_90DEG:
		tmp_x = x;
		x = bitmap->width - y - 1;
		y = tmp_x;
		break;
	case BITMAP_ROT_180DEG:
		x = bitmap->width - x - 1;
		y = bitmap->height - y - 1;
		break;
	case BITMAP_ROT_270DEG:
		tmp_x = x;
		x = y;
		y = bitmap->height - tmp_x - 1;
		break;
	}

	/* Zeiger auf Byte auswaehlen. */
	y_bit = y % 8;
	b = bitmap_column_byte(bitmap, x, y);

	/* Pixel-Bit setzen oder loeschen. */
	mask = BITMASK8(y_bit);
	if (on)
		*b |= mask;
	else
		*b &= ~mask;
}

/** \brief	Rechteckigen Bereich im Bitmap fuellen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param area_x	X Koordinate des Bereichs.
 *
 * \param area_y	Y Koordinate des Bereichs.
 *
 * \param area_width	Breite des Bereichs.
 *
 * \param area_height	Hoehe des Bereichs.
 *
 * \param black		1 -> Schwarz zeichnen. 0 -> Weiss zeichnen.
 */
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)
{
	uint8_t x, y, tmp_x, tmp_width;
	uint8_t *b;

	/* Koordinaten nach Rotation uebersetzen. */
	switch (bitmap->rotation) {
	case BITMAP_ROT_0DEG:
	default:
		break;
	case BITMAP_ROT_90DEG:
		tmp_x = x;
		x = bitmap->width - y - 1;
		y = tmp_x;
		tmp_width = area_width;
		area_width = area_height;
		area_height = tmp_width;
		break;
	case BITMAP_ROT_180DEG:
		x = bitmap->width - x - 1;
		y = bitmap->height - y - 1;
		break;
	case BITMAP_ROT_270DEG:
		tmp_x = x;
		x = y;
		y = bitmap->height - tmp_x - 1;
		tmp_width = area_width;
		area_width = area_height;
		area_height = tmp_width;
		break;
	}

	y = area_y;

	if (y % 8) {
		/* TODO: Spezialbehandlung wenn area_y kein Vielfaches von 8 ist.
		 *	Kommt in diesem Projekt nicht vor.
		 */
	}
	/* Y-Bereich durchlaufen. */
	while (area_height >= 8) {
		/* X-Bereich durchlaufen. */
		for (x = area_x; x < area_x + area_width; x++) {
			b = bitmap_column_byte(bitmap, x, y);
			/* Pixel-Bits loeschen oder setzen. */
			if (black)
				*b = 0xFF;
			else
				*b = 0;
		}
		area_height -= 8;
		y += 8;
	}
	if (area_height) {
		/* TODO: Spezialbehandlung wenn area_height kein Vielfaches von 8 ist.
		 *	Kommt in diesem Projekt nicht vor.
		 */
	}
}

/** \brief	Linie in Bitmap zeichnen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x_from	X Startkoordinate der Linie.
 *
 * \param y_from	Y Startkoordinate der Linie.
 *
 * \param x_to		X Endkoordinate der Linie.
 *
 * \param y_to		Y Endkoordinate der Linie.
 *
 * \param black		1 -> Schwarz zeichnen. 0 -> Weiss zeichnen.
 */
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)
{
	uint8_t x, y, error = 0;
	uint8_t dist_x, dist_y;
	int8_t x_inc, y_inc;

	/* Richtung und Distanz ermitteln. */
	if (x_to < x_from) {
		dist_x = x_from - x_to;
		x_inc = -1;
	} else {
		dist_x = x_to - x_from;
		x_inc = 1;
	}
	if (y_to < y_from) {
		dist_y = y_from - y_to;
		y_inc = -1;
	} else {
		dist_y = y_to - y_from;
		y_inc = 1;
	}
	x = x_from;
	y = y_from;

	/* Erstes Pixel schreiben. */
	bitmap_pixel_write(bitmap, x, y, black);

	/* Richtung abfragen. */
	if (dist_x >= dist_y) {
		/* Alle Pixel zeichnen. */
		while (x != x_to) {
			x += x_inc;
			error += dist_y * 2;
			if (error > dist_x) {
				y += y_inc;
				error -= dist_x * 2;
			}
			bitmap_pixel_write(bitmap, x, y, black);
		}
	} else {
		/* Alle Pixel zeichnen. */
		while (y != y_to) {
			y += y_inc;
			error += dist_x * 2;
			if (error > dist_y) {
				x += x_inc;
				error -= dist_y * 2;
			}
			bitmap_pixel_write(bitmap, x, y, black);
		}
	}
}

/** \brief	XBM in Bitmap zeichnen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x_pos		X Zielkoordinate.
 *
 * \param y_pos		Y Zielkoordinate.
 *
 * \param xbm		Das XBM Bitmap.
 *
 * \param xbm_width	Breite des XBM Bitmaps.
 *
 * \param xbm_height	Hoehe des XBM Bitmaps.
 */
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)
{
	uint8_t x, y, mask;
	uint8_t bm_width = bitmap_width(bitmap);
	uint8_t bm_height = bitmap_height(bitmap);

	if (x_pos >= bm_width ||
	    y_pos >= bm_height ||
	    x_pos + xbm_width > bm_width ||
	    y_pos + xbm_height > bm_height) {
		/* Ungueltige Koordinaten. */
		return;
	}

	/* Das XBM Pixelweise durchlaufen und Pixel zeichnen. */
	for (y = 0; y < xbm_height; y++) {
		mask = 0x01;
		for (x = 0; x < xbm_width; x++) {
			bitmap_pixel_write(bitmap,
					   x_pos + x, y_pos + y,
					   !!(*xbm & mask));
			mask <<= 1;
			if (!mask) {
				mask = 0x01;
				xbm++;
			}
		}
		if (mask != 0x01)
			xbm++;
	}
}

/** \brief	ASCII Zeichen in Bitmap zeichnen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x_pos		X Zielkoordinate.
 *
 * \param y_pos		Y Zielkoordinate.
 *
 * \param font_desc	Der Font der zum Zeichnen verwendet werden soll.
 *
 * \param ascii		Das ASCII Zeichen das gezeichnet werden soll.
 */
void bitmap_draw_char(struct bitmap *bitmap,
		      uint8_t x_pos, uint8_t y_pos,
		      const struct font_descriptor __flash *font_desc,
		      char ascii)
{
	const uint8_t __flash *xbm;
	uint8_t font_width, font_height, font_xbm_size;

	font_width = font_get_width(font_desc);
	font_height = font_get_height(font_desc);
	font_xbm_size = font_height * (div_round_up(font_width, 8));

	/* ASCII Zeichensatz auswaehlen. */
	if (ascii >= '0' && ascii <= '9') {
		xbm = font_get_numbers(font_desc);
		xbm += font_xbm_size * (ascii - '0');
	} else if (ascii >= 'a' && ascii <= 'z') {
		xbm = font_get_lowercase(font_desc);
		xbm += font_xbm_size * (ascii - 'a');
	} else if (ascii >= 'A' && ascii <= 'Z') {
		xbm = font_get_uppercase(font_desc);
		xbm += font_xbm_size * (ascii - 'A');
	} else
		return;

	/* Zeichen in Bitmap zeichnen. */
	bitmap_draw_xbm(bitmap, x_pos, y_pos, xbm, font_width, font_height);
}

/** \brief	ASCII String in Bitmap zeichnen.
 *
 * \param bitmap	Das Bitmap.
 *
 * \param x_pos		X Zielkoordinate.
 *
 * \param y_pos		Y Zielkoordinate.
 *
 * \param font_desc	Der Font der zum Zeichnen verwendet werden soll.
 *
 * \param string	Der NUL-terminierte String, der gezeichnet werden soll.
 */
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)
{
	uint8_t x = 0, y = 0, font_width, font_height;
	char c;
	uint8_t bm_width = bitmap_width(bitmap);
	uint8_t bm_height = bitmap_height(bitmap);

	font_width = font_get_width(font_desc);
	font_height = font_get_height(font_desc);

	/* Jedes Zeichen im Bitmap zeichnen. */
	while (1) {
		c = *string;
		if (!c) {
			/* Stringende. */
			break;
		}
		/* Das ASCII Zeichen zeichnen. */
		bitmap_draw_char(bitmap, x_pos + x, y_pos + y,
				 font_desc, c);
		string++;
		/* Spalte inkrementieren. */
		x += font_width + 2;
		/* Zeilenumbruch? */
		if (x + font_width > bm_width) {
			x = 0;
			/* Zeile Inkrementieren. */
			y += font_height + 2;
			if (y + font_height > bm_height)
				break;
		}
	}
}
bues.ch cgit interface