-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathbitmap.c
More file actions
78 lines (74 loc) · 2.01 KB
/
bitmap.c
File metadata and controls
78 lines (74 loc) · 2.01 KB
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
#include "sharpdisp/bitmap.h"
#include "string.h"
void bitmap_init(
struct Bitmap* b,
uint8_t* buff,
uint16_t width,
uint16_t height,
uint8_t mode,
uint8_t clear_byte) {
b->data = buff;
b->width = width;
b->height = height;
b->mode = mode;
b->clear_byte = clear_byte;
b->width_bytes = (width + 7) >> 3;
}
void bitmap_copy(struct Bitmap* dest, const struct Bitmap* src) {
if (dest->width_bytes != src->width_bytes) {
return;
}
if (dest->data == src->data) {
return;
}
const uint16_t height = dest->height <= src->height ? dest->height : src->height;
memcpy(dest->data, src->data, height * dest->width_bytes);
}
void bitmap_copy_rect(
struct Bitmap* dest,
const struct Bitmap* src,
int16_t src_x,
int16_t src_y) {
const uint16_t num_columns = dest->width_bytes;
const uint16_t num_rows = dest->height;
uint8_t* dest_data = dest->data;
for (uint16_t row = 0; row < num_rows; ++row) {
for (uint16_t column = 0; column < num_columns; ++column, ++dest_data) {
*dest_data = bitmap_get_stripe(
src,
src_x + (column << 3),
src_y + row
);
}
}
// some extra pixels beyond the actual width may have been grabbed
const uint8_t clear_width = 8 - (dest->width & 0x07);
if (clear_width >= 8) {
// guess not
return;
}
const uint8_t clear_mask = 0xFF << clear_width;
// select the last column
dest_data = dest->data + num_columns - 1;
for (uint16_t row = 0; row < num_rows; ++row,dest_data+=num_columns) {
(*dest_data) &= clear_mask;
}
}
void bitmap_blit(
struct Bitmap* dest,
int16_t dest_x,
int16_t dest_y,
const struct Bitmap* src) {
const uint16_t num_columns = src->width_bytes;
const uint16_t num_rows = src->height;
uint8_t* src_data = src->data;
for (uint16_t row = 0; row < num_rows; ++row) {
for (uint16_t column = 0; column < num_columns; ++column, ++src_data) {
bitmap_apply_stripe(
dest,
dest_x + (column << 3),
dest_y + row,
*src_data);
}
}
}