-
Notifications
You must be signed in to change notification settings - Fork 0
/
gfxboot_canvas.c
208 lines (153 loc) · 5.2 KB
/
gfxboot_canvas.c
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
#include <gfxboot/gfxboot.h>
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// canvas
static char gfx_canvas_pixel2char(canvas_t *c, int x_blk, int y_blk, int x, int y);
static uint32_t gfx_canvas_chksum(canvas_t *c);
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
obj_id_t gfx_obj_canvas_new(int width, int height)
{
obj_id_t id = gfx_obj_alloc(OTYPE_CANVAS, OBJ_CANVAS_SIZE(width, height));
canvas_t *c = gfx_obj_canvas_ptr(id);
if(c) {
c->max_width = c->geo.width = c->region.width = width;
c->max_height = c->geo.height = c->region.height = height;
}
return id;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
canvas_t *gfx_obj_canvas_ptr(obj_id_t id)
{
obj_t *ptr = gfx_obj_ptr(id);
if(!ptr || ptr->base_type != OTYPE_CANVAS) return 0;
return (canvas_t *) ptr->data.ptr;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_obj_canvas_dump(obj_t *ptr, dump_style_t style)
{
if(!ptr) return 1;
canvas_t *c = ptr->data.ptr;
unsigned len = ptr->data.size;
int w, w_max, h, h_max, x_blk, y_blk;
if(
len != OBJ_CANVAS_SIZE(c->max_width, c->max_height) ||
c->geo.width < 0 ||
c->geo.height < 0 ||
c->geo.width > c->max_width ||
c->geo.height > c->max_height
) {
if(style.ref) {
gfxboot_log(" <invalid data>\n");
}
else {
gfxboot_log("<invalid data>");
}
return 1;
}
len -= sizeof (canvas_t);
x_blk = (c->geo.width + 79) / 80;
if(!x_blk) x_blk = 1;
w_max = c->geo.width / x_blk;
y_blk = (c->geo.height + 19) / 20;
if(!y_blk) y_blk = 1;
h_max = c->geo.height / y_blk;
if(!style.ref) {
if(!style.inspect) return 0;
gfxboot_log(
"geo %dx%d_%dx%d, region %dx%d_%dx%d, chk 0x%08x",
c->geo.x, c->geo.y, c->geo.width, c->geo.height,
c->region.x, c->region.y, c->region.width, c->region.height,
gfx_canvas_chksum(c)
);
return 1;
}
if(style.dump) {
gfxboot_log(" cursor %dx%d_%dx%d, draw_mode %d, max %dx%d\n",
c->cursor.x, c->cursor.y, c->cursor.width, c->cursor.height,
c->draw_mode, c->max_width, c->max_height
);
gfxboot_log(" color #%08x, bg_color #%08x, font %s\n", c->color, c->bg_color, gfx_obj_id2str(c->font_id));
gfxboot_log(" unit %dx%d\n", x_blk, y_blk);
if(len) {
for(h = 0; h < h_max; h++) {
gfxboot_log(" |");
for(w = 0; w < w_max; w++) {
gfxboot_log("%c", gfx_canvas_pixel2char(c, x_blk, y_blk, w, h));
}
gfxboot_log("|\n");
}
}
}
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char gfx_canvas_pixel2char(canvas_t *c, int x_blk, int y_blk, int x, int y)
{
const char syms[] = " .,:+ox*%#O@"; // symbols for brightness
color_t col, *cp = (color_t *) ((uint8_t *) c + sizeof (canvas_t));
int i, j;
unsigned val = 0;
x *= x_blk;
y *= y_blk;
cp += x + y * c->geo.width;
for(j = 0; j < y_blk; j++, cp += c->geo.width) {
for(i = 0; i < x_blk; i++) {
col = cp[i];
unsigned alpha = 0xff - ((col >> 24) & 0xff);
val += ((col & 0xff) + ((col >> 8) & 0xff) + ((col >> 16) & 0xff)) * alpha;
}
}
val /= (unsigned) (x_blk * y_blk * 3 * 255 * 255) / (sizeof syms); // yes, size + 1 !!!
if(val > sizeof syms - 2) val = sizeof syms - 2;
return syms[val];
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_canvas_adjust_size(canvas_t *c, int width, int height)
{
if(width < 0 || height < 0 || width > c->max_width || height > c->max_height) return 0;
c->geo.width = width;
c->geo.height = height;
return 1;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_canvas_resize(obj_id_t canvas_id, int width, int height)
{
canvas_t *c = gfx_obj_canvas_ptr(canvas_id);
if(!c) return 0;
if(c->geo.width == width && c->geo.height == height) return 1;
return gfx_obj_realloc(canvas_id, OBJ_CANVAS_SIZE(width, height)) ? 1 : 0;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
uint32_t gfx_canvas_chksum(canvas_t *c)
{
unsigned u, len = (unsigned) c->geo.width * (unsigned) c->geo.height;
uint32_t sum = 0, a = 0;
for(u = 0; u < len; u++) {
a = a * 73 + 19;
sum += a ^ c->ptr[u];
}
return sum;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
unsigned gfx_obj_canvas_gc(obj_t *ptr)
{
if(!ptr) return 0;
canvas_t *canvas = ptr->data.ptr;
unsigned data_size = ptr->data.size;
unsigned more_gc = 0;
if(canvas && data_size == OBJ_CANVAS_SIZE(canvas->max_width, canvas->max_height)) {
more_gc += gfx_obj_ref_dec_delay_gc(canvas->font_id);
}
return more_gc;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int gfx_obj_canvas_contains(obj_t *ptr, obj_id_t id)
{
if(!ptr || !id) return 0;
canvas_t *canvas = ptr->data.ptr;
unsigned data_size = ptr->data.size;
if(canvas && data_size == OBJ_CANVAS_SIZE(canvas->max_width, canvas->max_height)) {
if(id == canvas->font_id) return 1;
}
return 0;
}