| 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 |
#include <libpayload.h> |
| 30 |
#include <cbfs.h> |
| 31 |
#include <fpmath.h> |
| 32 |
#include <sysinfo.h> |
| 33 |
#include "bitmap.h" |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
static struct rect canvas; |
| 42 |
static struct rect screen; |
| 43 |
|
| 44 |
static uint8_t *gfx_buffer; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
static const struct cb_framebuffer *fbinfo; |
| 51 |
|
| 52 |
|
| 53 |
#define REAL_FB ((unsigned char *)phys_to_virt(fbinfo->physical_address)) |
| 54 |
#define FB (gfx_buffer ? gfx_buffer : REAL_FB) |
| 55 |
|
| 56 |
#define LOG(x...) printf("CBGFX: " x) |
| 57 |
#define PIVOT_H_MASK (PIVOT_H_LEFT|PIVOT_H_CENTER|PIVOT_H_RIGHT) |
| 58 |
#define PIVOT_V_MASK (PIVOT_V_TOP|PIVOT_V_CENTER|PIVOT_V_BOTTOM) |
| 59 |
#define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y)) |
| 60 |
|
| 61 |
static char initialized = 0; |
| 62 |
|
| 63 |
static const struct vector vzero = { |
| 64 |
.x = 0, |
| 65 |
.y = 0, |
| 66 |
}; |
| 67 |
|
| 68 |
struct color_transformation { |
| 69 |
uint8_t base; |
| 70 |
int16_t scale; |
| 71 |
}; |
| 72 |
|
| 73 |
struct color_mapping { |
| 74 |
struct color_transformation red; |
| 75 |
struct color_transformation green; |
| 76 |
struct color_transformation blue; |
| 77 |
int enabled; |
| 78 |
}; |
| 79 |
|
| 80 |
static struct color_mapping color_map; |
| 81 |
|
| 82 |
static inline void set_color_trans(struct color_transformation *trans, |
| 83 |
uint8_t bg_color, uint8_t fg_color) |
| 84 |
{ |
| 85 |
trans->base = bg_color; |
| 86 |
trans->scale = fg_color - bg_color; |
| 87 |
} |
| 88 |
|
| 89 |
int set_color_map(const struct rgb_color *background, |
| 90 |
const struct rgb_color *foreground) |
| 91 |
{ |
| 92 |
if (background == NULL || foreground == NULL) |
| 93 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 94 |
|
| 95 |
set_color_trans(&color_map.red, background->red, foreground->red); |
| 96 |
set_color_trans(&color_map.green, background->green, |
| 97 |
foreground->green); |
| 98 |
set_color_trans(&color_map.blue, background->blue, foreground->blue); |
| 99 |
color_map.enabled = 1; |
| 100 |
|
| 101 |
return CBGFX_SUCCESS; |
| 102 |
} |
| 103 |
|
| 104 |
void clear_color_map(void) |
| 105 |
{ |
| 106 |
color_map.enabled = 0; |
| 107 |
} |
| 108 |
|
| 109 |
struct blend_value { |
| 110 |
uint8_t alpha; |
| 111 |
struct rgb_color rgb; |
| 112 |
}; |
| 113 |
|
| 114 |
static struct blend_value blend; |
| 115 |
|
| 116 |
int set_blend(const struct rgb_color *rgb, uint8_t alpha) |
| 117 |
{ |
| 118 |
if (rgb == NULL) |
| 119 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 120 |
|
| 121 |
blend.alpha = alpha; |
| 122 |
blend.rgb = *rgb; |
| 123 |
|
| 124 |
return CBGFX_SUCCESS; |
| 125 |
} |
| 126 |
|
| 127 |
void clear_blend(void) |
| 128 |
{ |
| 129 |
blend.alpha = 0; |
| 130 |
blend.rgb.red = 0; |
| 131 |
blend.rgb.green = 0; |
| 132 |
blend.rgb.blue = 0; |
| 133 |
} |
| 134 |
|
| 135 |
static void add_vectors(struct vector *out, |
| 136 |
const struct vector *v1, const struct vector *v2) |
| 137 |
{ |
| 138 |
out->x = v1->x + v2->x; |
| 139 |
out->y = v1->y + v2->y; |
| 140 |
} |
| 141 |
|
| 142 |
static int fraction_equal(const struct fraction *f1, const struct fraction *f2) |
| 143 |
{ |
| 144 |
return (int64_t)f1->n * f2->d == (int64_t)f2->n * f1->d; |
| 145 |
} |
| 146 |
|
| 147 |
static int is_valid_fraction(const struct fraction *f) |
| 148 |
{ |
| 149 |
return f->d != 0; |
| 150 |
} |
| 151 |
|
| 152 |
static int is_valid_scale(const struct scale *s) |
| 153 |
{ |
| 154 |
return is_valid_fraction(&s->x) && is_valid_fraction(&s->y); |
| 155 |
} |
| 156 |
|
| 157 |
static void reduce_fraction(struct fraction *out, int64_t n, int64_t d) |
| 158 |
{ |
| 159 |
|
| 160 |
int shift = log2(MAX(ABS(n), ABS(d)) >> 31) + 1; |
| 161 |
out->n = n >> shift; |
| 162 |
out->d = d >> shift; |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
static void add_fractions(struct fraction *out, |
| 167 |
const struct fraction *f1, const struct fraction *f2) |
| 168 |
{ |
| 169 |
reduce_fraction(out, |
| 170 |
(int64_t)f1->n * f2->d + (int64_t)f2->n * f1->d, |
| 171 |
(int64_t)f1->d * f2->d); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
static void subtract_fractions(struct fraction *out, |
| 176 |
const struct fraction *f1, |
| 177 |
const struct fraction *f2) |
| 178 |
{ |
| 179 |
reduce_fraction(out, |
| 180 |
(int64_t)f1->n * f2->d - (int64_t)f2->n * f1->d, |
| 181 |
(int64_t)f1->d * f2->d); |
| 182 |
} |
| 183 |
|
| 184 |
static void add_scales(struct scale *out, |
| 185 |
const struct scale *s1, const struct scale *s2) |
| 186 |
{ |
| 187 |
add_fractions(&out->x, &s1->x, &s2->x); |
| 188 |
add_fractions(&out->y, &s1->y, &s2->y); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
static int transform_vector(struct vector *out, |
| 197 |
const struct vector *in, |
| 198 |
const struct scale *a, |
| 199 |
const struct vector *offset) |
| 200 |
{ |
| 201 |
if (!is_valid_scale(a)) |
| 202 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 203 |
out->x = (int64_t)a->x.n * in->x / a->x.d + offset->x; |
| 204 |
out->y = (int64_t)a->y.n * in->y / a->y.d + offset->y; |
| 205 |
return CBGFX_SUCCESS; |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
static int within_box(const struct vector *v, const struct rect *bound) |
| 213 |
{ |
| 214 |
if (v->x > bound->offset.x && |
| 215 |
v->y > bound->offset.y && |
| 216 |
v->x < bound->offset.x + bound->size.width && |
| 217 |
v->y < bound->offset.y + bound->size.height) |
| 218 |
return 1; |
| 219 |
else if (v->x >= bound->offset.x && |
| 220 |
v->y >= bound->offset.y && |
| 221 |
v->x <= bound->offset.x + bound->size.width && |
| 222 |
v->y <= bound->offset.y + bound->size.height) |
| 223 |
return 0; |
| 224 |
else |
| 225 |
return -1; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
static inline uint8_t apply_map(uint8_t color, |
| 230 |
const struct color_transformation *trans) |
| 231 |
{ |
| 232 |
if (!color_map.enabled) |
| 233 |
return color; |
| 234 |
return trans->base + trans->scale * color / UINT8_MAX; |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
static inline uint8_t apply_blend(uint8_t color, uint8_t blend_color) |
| 242 |
{ |
| 243 |
if (blend.alpha == 0 || color == blend_color) |
| 244 |
return color; |
| 245 |
|
| 246 |
return (color * (256 - blend.alpha) + |
| 247 |
blend_color * blend.alpha) / 256; |
| 248 |
} |
| 249 |
|
| 250 |
static inline uint32_t calculate_color(const struct rgb_color *rgb, |
| 251 |
uint8_t invert) |
| 252 |
{ |
| 253 |
uint32_t color = 0; |
| 254 |
|
| 255 |
color |= (apply_blend(apply_map(rgb->red, &color_map.red), |
| 256 |
blend.rgb.red) |
| 257 |
>> (8 - fbinfo->red_mask_size)) |
| 258 |
<< fbinfo->red_mask_pos; |
| 259 |
color |= (apply_blend(apply_map(rgb->green, &color_map.green), |
| 260 |
blend.rgb.green) |
| 261 |
>> (8 - fbinfo->green_mask_size)) |
| 262 |
<< fbinfo->green_mask_pos; |
| 263 |
color |= (apply_blend(apply_map(rgb->blue, &color_map.blue), |
| 264 |
blend.rgb.blue) |
| 265 |
>> (8 - fbinfo->blue_mask_size)) |
| 266 |
<< fbinfo->blue_mask_pos; |
| 267 |
if (invert) |
| 268 |
color ^= 0xffffffff; |
| 269 |
return color; |
| 270 |
} |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
static inline void set_pixel(struct vector *coord, uint32_t color) |
| 277 |
{ |
| 278 |
const int bpp = fbinfo->bits_per_pixel; |
| 279 |
const int bpl = fbinfo->bytes_per_line; |
| 280 |
struct vector rcoord; |
| 281 |
int i; |
| 282 |
|
| 283 |
switch (fbinfo->orientation) { |
| 284 |
case CB_FB_ORIENTATION_NORMAL: |
| 285 |
default: |
| 286 |
rcoord.x = coord->x; |
| 287 |
rcoord.y = coord->y; |
| 288 |
break; |
| 289 |
case CB_FB_ORIENTATION_BOTTOM_UP: |
| 290 |
rcoord.x = screen.size.width - 1 - coord->x; |
| 291 |
rcoord.y = screen.size.height - 1 - coord->y; |
| 292 |
break; |
| 293 |
case CB_FB_ORIENTATION_LEFT_UP: |
| 294 |
rcoord.x = coord->y; |
| 295 |
rcoord.y = screen.size.width - 1 - coord->x; |
| 296 |
break; |
| 297 |
case CB_FB_ORIENTATION_RIGHT_UP: |
| 298 |
rcoord.x = screen.size.height - 1 - coord->y; |
| 299 |
rcoord.y = coord->x; |
| 300 |
break; |
| 301 |
} |
| 302 |
|
| 303 |
uint8_t * const pixel = FB + rcoord.y * bpl + rcoord.x * bpp / 8; |
| 304 |
for (i = 0; i < bpp / 8; i++) |
| 305 |
pixel[i] = (color >> (i * 8)); |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
static int cbgfx_init(void) |
| 313 |
{ |
| 314 |
if (initialized) |
| 315 |
return 0; |
| 316 |
|
| 317 |
fbinfo = &lib_sysinfo.framebuffer; |
| 318 |
|
| 319 |
if (!fbinfo->physical_address) |
| 320 |
return CBGFX_ERROR_FRAMEBUFFER_ADDR; |
| 321 |
|
| 322 |
switch (fbinfo->orientation) { |
| 323 |
default: |
| 324 |
screen.size.width = fbinfo->x_resolution; |
| 325 |
screen.size.height = fbinfo->y_resolution; |
| 326 |
break; |
| 327 |
case CB_FB_ORIENTATION_LEFT_UP: |
| 328 |
case CB_FB_ORIENTATION_RIGHT_UP: |
| 329 |
screen.size.width = fbinfo->y_resolution; |
| 330 |
screen.size.height = fbinfo->x_resolution; |
| 331 |
break; |
| 332 |
} |
| 333 |
screen.offset.x = 0; |
| 334 |
screen.offset.y = 0; |
| 335 |
|
| 336 |
|
| 337 |
if (screen.size.height > screen.size.width) { |
| 338 |
canvas.size.height = screen.size.width; |
| 339 |
canvas.size.width = canvas.size.height; |
| 340 |
canvas.offset.x = 0; |
| 341 |
canvas.offset.y = (screen.size.height - canvas.size.height) / 2; |
| 342 |
} else { |
| 343 |
canvas.size.height = screen.size.height; |
| 344 |
canvas.size.width = canvas.size.height; |
| 345 |
canvas.offset.x = (screen.size.width - canvas.size.width) / 2; |
| 346 |
canvas.offset.y = 0; |
| 347 |
} |
| 348 |
|
| 349 |
initialized = 1; |
| 350 |
LOG("cbgfx initialized: screen:width=%d, height=%d, offset=%d canvas:width=%d, height=%d, offset=%d\n", |
| 351 |
screen.size.width, screen.size.height, screen.offset.x, |
| 352 |
canvas.size.width, canvas.size.height, canvas.offset.x); |
| 353 |
|
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
int draw_box(const struct rect *box, const struct rgb_color *rgb) |
| 358 |
{ |
| 359 |
struct vector top_left; |
| 360 |
struct vector p, t; |
| 361 |
|
| 362 |
if (cbgfx_init()) |
| 363 |
return CBGFX_ERROR_INIT; |
| 364 |
|
| 365 |
const uint32_t color = calculate_color(rgb, 0); |
| 366 |
const struct scale top_left_s = { |
| 367 |
.x = { .n = box->offset.x, .d = CANVAS_SCALE, }, |
| 368 |
.y = { .n = box->offset.y, .d = CANVAS_SCALE, } |
| 369 |
}; |
| 370 |
const struct scale bottom_right_s = { |
| 371 |
.x = { .n = box->offset.x + box->size.x, .d = CANVAS_SCALE, }, |
| 372 |
.y = { .n = box->offset.y + box->size.y, .d = CANVAS_SCALE, } |
| 373 |
}; |
| 374 |
|
| 375 |
transform_vector(&top_left, &canvas.size, &top_left_s, &canvas.offset); |
| 376 |
transform_vector(&t, &canvas.size, &bottom_right_s, &canvas.offset); |
| 377 |
if (within_box(&t, &canvas) < 0) { |
| 378 |
LOG("Box exceeds canvas boundary\n"); |
| 379 |
return CBGFX_ERROR_BOUNDARY; |
| 380 |
} |
| 381 |
|
| 382 |
for (p.y = top_left.y; p.y < t.y; p.y++) |
| 383 |
for (p.x = top_left.x; p.x < t.x; p.x++) |
| 384 |
set_pixel(&p, color); |
| 385 |
|
| 386 |
return CBGFX_SUCCESS; |
| 387 |
} |
| 388 |
|
| 389 |
int draw_rounded_box(const struct scale *pos_rel, const struct scale *dim_rel, |
| 390 |
const struct rgb_color *rgb, |
| 391 |
const struct fraction *thickness, |
| 392 |
const struct fraction *radius) |
| 393 |
{ |
| 394 |
struct scale pos_end_rel; |
| 395 |
struct vector top_left; |
| 396 |
struct vector p, t; |
| 397 |
|
| 398 |
if (cbgfx_init()) |
| 399 |
return CBGFX_ERROR_INIT; |
| 400 |
|
| 401 |
const uint32_t color = calculate_color(rgb, 0); |
| 402 |
|
| 403 |
if (!is_valid_scale(pos_rel) || !is_valid_scale(dim_rel)) |
| 404 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 405 |
|
| 406 |
add_scales(&pos_end_rel, pos_rel, dim_rel); |
| 407 |
transform_vector(&top_left, &canvas.size, pos_rel, &canvas.offset); |
| 408 |
transform_vector(&t, &canvas.size, &pos_end_rel, &canvas.offset); |
| 409 |
if (within_box(&t, &canvas) < 0) { |
| 410 |
LOG("Box exceeds canvas boundary\n"); |
| 411 |
return CBGFX_ERROR_BOUNDARY; |
| 412 |
} |
| 413 |
|
| 414 |
if (!is_valid_fraction(thickness) || !is_valid_fraction(radius)) |
| 415 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 416 |
|
| 417 |
struct scale thickness_scale = { |
| 418 |
.x = { .n = thickness->n, .d = thickness->d }, |
| 419 |
.y = { .n = thickness->n, .d = thickness->d }, |
| 420 |
}; |
| 421 |
struct scale radius_scale = { |
| 422 |
.x = { .n = radius->n, .d = radius->d }, |
| 423 |
.y = { .n = radius->n, .d = radius->d }, |
| 424 |
}; |
| 425 |
struct vector d, r, s; |
| 426 |
transform_vector(&d, &canvas.size, &thickness_scale, &vzero); |
| 427 |
transform_vector(&r, &canvas.size, &radius_scale, &vzero); |
| 428 |
const uint8_t has_thickness = d.x > 0 && d.y > 0; |
| 429 |
if (thickness->n != 0 && !has_thickness) |
| 430 |
LOG("Thickness truncated to 0\n"); |
| 431 |
const uint8_t has_radius = r.x > 0 && r.y > 0; |
| 432 |
if (radius->n != 0 && !has_radius) |
| 433 |
LOG("Radius truncated to 0\n"); |
| 434 |
if (has_radius) { |
| 435 |
if (d.x > r.x || d.y > r.y) { |
| 436 |
LOG("Thickness cannot be greater than radius\n"); |
| 437 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 438 |
} |
| 439 |
if (r.x * 2 > t.x - top_left.x || r.y * 2 > t.y - top_left.y) { |
| 440 |
LOG("Radius cannot be greater than half of the box\n"); |
| 441 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
int32_t x_begin, x_end; |
| 447 |
if (has_thickness) { |
| 448 |
|
| 449 |
for (p.y = top_left.y; p.y < top_left.y + d.y; p.y++) |
| 450 |
for (p.x = top_left.x + r.x; p.x < t.x - r.x; p.x++) |
| 451 |
set_pixel(&p, color); |
| 452 |
|
| 453 |
for (p.y = t.y - d.y; p.y < t.y; p.y++) |
| 454 |
for (p.x = top_left.x + r.x; p.x < t.x - r.x; p.x++) |
| 455 |
set_pixel(&p, color); |
| 456 |
for (p.y = top_left.y + r.y; p.y < t.y - r.y; p.y++) { |
| 457 |
|
| 458 |
for (p.x = top_left.x; p.x < top_left.x + d.x; p.x++) |
| 459 |
set_pixel(&p, color); |
| 460 |
|
| 461 |
for (p.x = t.x - d.x; p.x < t.x; p.x++) |
| 462 |
set_pixel(&p, color); |
| 463 |
} |
| 464 |
} else { |
| 465 |
|
| 466 |
for (p.y = top_left.y; p.y < t.y; p.y++) { |
| 467 |
if (p.y >= top_left.y + r.y && p.y < t.y - r.y) { |
| 468 |
x_begin = top_left.x; |
| 469 |
x_end = t.x; |
| 470 |
} else { |
| 471 |
x_begin = top_left.x + r.x; |
| 472 |
x_end = t.x - r.x; |
| 473 |
} |
| 474 |
for (p.x = x_begin; p.x < x_end; p.x++) |
| 475 |
set_pixel(&p, color); |
| 476 |
} |
| 477 |
} |
| 478 |
|
| 479 |
if (!has_radius) |
| 480 |
return CBGFX_SUCCESS; |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
if (has_thickness) { |
| 488 |
s.x = r.x - d.x; |
| 489 |
s.y = r.y - d.y; |
| 490 |
} else { |
| 491 |
s.x = 0; |
| 492 |
s.y = 0; |
| 493 |
} |
| 494 |
|
| 495 |
|
| 496 |
int32_t x, y; |
| 497 |
uint64_t yy; |
| 498 |
const uint64_t rrx = (uint64_t)r.x * r.x, rry = (uint64_t)r.y * r.y; |
| 499 |
const uint64_t ssx = (uint64_t)s.x * s.x, ssy = (uint64_t)s.y * s.y; |
| 500 |
x_begin = 0; |
| 501 |
x_end = 0; |
| 502 |
for (y = r.y - 1; y >= 0; y--) { |
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
yy = (uint64_t)y * y; |
| 508 |
|
| 509 |
while (yy * ssx + x_begin * x_begin * ssy < ssx * ssy) |
| 510 |
x_begin++; |
| 511 |
|
| 512 |
x = x_begin; |
| 513 |
|
| 514 |
while (x < x_end || yy * rrx + x * x * rry < rrx * rry) { |
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
p.y = top_left.y + r.y - 1 - y; |
| 525 |
p.x = top_left.x + r.x - 1 - x; |
| 526 |
set_pixel(&p, color); |
| 527 |
|
| 528 |
p.y = top_left.y + r.y - 1 - y; |
| 529 |
p.x = t.x - r.x + x; |
| 530 |
set_pixel(&p, color); |
| 531 |
|
| 532 |
p.y = t.y - r.y + y; |
| 533 |
p.x = top_left.x + r.x - 1 - x; |
| 534 |
set_pixel(&p, color); |
| 535 |
|
| 536 |
p.y = t.y - r.y + y; |
| 537 |
p.x = t.x - r.x + x; |
| 538 |
set_pixel(&p, color); |
| 539 |
x++; |
| 540 |
} |
| 541 |
x_end = x; |
| 542 |
|
| 543 |
} |
| 544 |
|
| 545 |
return CBGFX_SUCCESS; |
| 546 |
} |
| 547 |
|
| 548 |
int draw_line(const struct scale *pos1, const struct scale *pos2, |
| 549 |
const struct fraction *thickness, const struct rgb_color *rgb) |
| 550 |
{ |
| 551 |
struct fraction len; |
| 552 |
struct vector top_left; |
| 553 |
struct vector size; |
| 554 |
struct vector p, t; |
| 555 |
|
| 556 |
if (cbgfx_init()) |
| 557 |
return CBGFX_ERROR_INIT; |
| 558 |
|
| 559 |
const uint32_t color = calculate_color(rgb, 0); |
| 560 |
|
| 561 |
if (!is_valid_fraction(thickness)) |
| 562 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 563 |
|
| 564 |
transform_vector(&top_left, &canvas.size, pos1, &canvas.offset); |
| 565 |
if (fraction_equal(&pos1->y, &pos2->y)) { |
| 566 |
|
| 567 |
subtract_fractions(&len, &pos2->x, &pos1->x); |
| 568 |
struct scale dim = { |
| 569 |
.x = { .n = len.n, .d = len.d }, |
| 570 |
.y = { .n = thickness->n, .d = thickness->d }, |
| 571 |
}; |
| 572 |
transform_vector(&size, &canvas.size, &dim, &vzero); |
| 573 |
size.y = MAX(size.y, 1); |
| 574 |
} else if (fraction_equal(&pos1->x, &pos2->x)) { |
| 575 |
|
| 576 |
subtract_fractions(&len, &pos2->y, &pos1->y); |
| 577 |
struct scale dim = { |
| 578 |
.x = { .n = thickness->n, .d = thickness->d }, |
| 579 |
.y = { .n = len.n, .d = len.d }, |
| 580 |
}; |
| 581 |
transform_vector(&size, &canvas.size, &dim, &vzero); |
| 582 |
size.x = MAX(size.x, 1); |
| 583 |
} else { |
| 584 |
LOG("Only support horizontal and vertical lines\n"); |
| 585 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 586 |
} |
| 587 |
|
| 588 |
add_vectors(&t, &top_left, &size); |
| 589 |
if (within_box(&t, &canvas) < 0) { |
| 590 |
LOG("Line exceeds canvas boundary\n"); |
| 591 |
return CBGFX_ERROR_BOUNDARY; |
| 592 |
} |
| 593 |
|
| 594 |
for (p.y = top_left.y; p.y < t.y; p.y++) |
| 595 |
for (p.x = top_left.x; p.x < t.x; p.x++) |
| 596 |
set_pixel(&p, color); |
| 597 |
|
| 598 |
return CBGFX_SUCCESS; |
| 599 |
} |
| 600 |
|
| 601 |
int clear_canvas(const struct rgb_color *rgb) |
| 602 |
{ |
| 603 |
const struct rect box = { |
| 604 |
vzero, |
| 605 |
.size = { |
| 606 |
.width = CANVAS_SCALE, |
| 607 |
.height = CANVAS_SCALE, |
| 608 |
}, |
| 609 |
}; |
| 610 |
|
| 611 |
if (cbgfx_init()) |
| 612 |
return CBGFX_ERROR_INIT; |
| 613 |
|
| 614 |
return draw_box(&box, rgb); |
| 615 |
} |
| 616 |
|
| 617 |
int clear_screen(const struct rgb_color *rgb) |
| 618 |
{ |
| 619 |
if (cbgfx_init()) |
| 620 |
return CBGFX_ERROR_INIT; |
| 621 |
|
| 622 |
int x, y, i; |
| 623 |
uint32_t color = calculate_color(rgb, 0); |
| 624 |
const int bpp = fbinfo->bits_per_pixel; |
| 625 |
const int bpl = fbinfo->bytes_per_line; |
| 626 |
uint8_t *line = malloc(bpl); |
| 627 |
|
| 628 |
if (!line) { |
| 629 |
LOG("Failed to allocate line buffer (%u bytes)\n", bpl); |
| 630 |
return CBGFX_ERROR_UNKNOWN; |
| 631 |
} |
| 632 |
|
| 633 |
|
| 634 |
for (x = 0; x < fbinfo->x_resolution; x++) |
| 635 |
for (i = 0; i < bpp / 8; i++) |
| 636 |
line[x * bpp / 8 + i] = (color >> (i * 8)); |
| 637 |
for (y = 0; y < fbinfo->y_resolution; y++) |
| 638 |
memcpy(FB + y * bpl, line, bpl); |
| 639 |
|
| 640 |
free(line); |
| 641 |
return CBGFX_SUCCESS; |
| 642 |
} |
| 643 |
|
| 644 |
static int pal_to_rgb(uint8_t index, const struct bitmap_palette_element_v3 *pal, |
| 645 |
size_t palcount, struct rgb_color *out) |
| 646 |
{ |
| 647 |
if (index >= palcount) { |
| 648 |
LOG("Color index %d exceeds palette boundary\n", index); |
| 649 |
return CBGFX_ERROR_BITMAP_DATA; |
| 650 |
} |
| 651 |
|
| 652 |
out->red = pal[index].red; |
| 653 |
out->green = pal[index].green; |
| 654 |
out->blue = pal[index].blue; |
| 655 |
return CBGFX_SUCCESS; |
| 656 |
} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
|
| 666 |
|
| 667 |
|
| 668 |
|
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
#if CONFIG(LP_CBGFX_FAST_RESAMPLE) |
| 679 |
#define LNCZ_A 2 |
| 680 |
#else |
| 681 |
#define LNCZ_A 3 |
| 682 |
#endif |
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
#define S0 (LNCZ_A - 1) |
| 694 |
|
| 695 |
|
| 696 |
#define SSZ (LNCZ_A * 2) |
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
static fpmath_t lanczos_weight(fpmath_t in, int off) |
| 707 |
{ |
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
fpmath_t x = fpisub(off - S0, fpsubi(in, fpfloor(in))); |
| 723 |
|
| 724 |
|
| 725 |
fpmath_t x2a = x; |
| 726 |
if (LNCZ_A != 2) |
| 727 |
x2a = fpmul(x, fpfrac(2, LNCZ_A)); |
| 728 |
|
| 729 |
fpmath_t x_times_pi = fpmul(x, fppi()); |
| 730 |
if (fpequals(x_times_pi, fp(0))) |
| 731 |
return fp(1); |
| 732 |
|
| 733 |
|
| 734 |
|
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
fpmath_t tmp = fpmuli(fpdiv(fpsin1(fpmuli(x, 2)), x_times_pi), LNCZ_A); |
| 739 |
return fpdiv(fpmul(tmp, fpsin1(x2a)), x_times_pi); |
| 740 |
} |
| 741 |
|
| 742 |
static int draw_bitmap_v3(const struct vector *top_left, |
| 743 |
const struct vector *dim, |
| 744 |
const struct vector *dim_org, |
| 745 |
const struct bitmap_header_v3 *header, |
| 746 |
const struct bitmap_palette_element_v3 *pal, |
| 747 |
const uint8_t *pixel_array, uint8_t invert) |
| 748 |
{ |
| 749 |
const int bpp = header->bits_per_pixel; |
| 750 |
int32_t dir; |
| 751 |
struct vector p; |
| 752 |
int32_t ox, oy; |
| 753 |
int32_t ix, iy; |
| 754 |
int sx, sy; |
| 755 |
|
| 756 |
if (header->compression) { |
| 757 |
LOG("Compressed bitmaps are not supported\n"); |
| 758 |
return CBGFX_ERROR_BITMAP_FORMAT; |
| 759 |
} |
| 760 |
if (bpp >= 16) { |
| 761 |
LOG("Non-palette bitmaps are not supported\n"); |
| 762 |
return CBGFX_ERROR_BITMAP_FORMAT; |
| 763 |
} |
| 764 |
if (bpp != 8) { |
| 765 |
LOG("Unsupported bits per pixel: %d\n", bpp); |
| 766 |
return CBGFX_ERROR_BITMAP_FORMAT; |
| 767 |
} |
| 768 |
|
| 769 |
const int32_t y_stride = ROUNDUP(dim_org->width * bpp / 8, 4); |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
p.y = top_left->y; |
| 780 |
if (header->height < 0) { |
| 781 |
dir = 1; |
| 782 |
} else { |
| 783 |
p.y += dim->height - 1; |
| 784 |
dir = -1; |
| 785 |
} |
| 786 |
|
| 787 |
|
| 788 |
if (dim_org->width == dim->width && dim_org->height == dim->height) { |
| 789 |
for (oy = 0; oy < dim->height; oy++, p.y += dir) { |
| 790 |
p.x = top_left->x; |
| 791 |
for (ox = 0; ox < dim->width; ox++, p.x++) { |
| 792 |
struct rgb_color rgb; |
| 793 |
if (pal_to_rgb(pixel_array[oy * y_stride + ox], |
| 794 |
pal, header->colors_used, &rgb)) |
| 795 |
return CBGFX_ERROR_BITMAP_DATA; |
| 796 |
set_pixel(&p, calculate_color(&rgb, invert)); |
| 797 |
} |
| 798 |
} |
| 799 |
return CBGFX_SUCCESS; |
| 800 |
} |
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
fpmath_t (*weight_x)[SSZ] = malloc(sizeof(fpmath_t) * SSZ * dim->width); |
| 805 |
if (!weight_x) |
| 806 |
return CBGFX_ERROR_UNKNOWN; |
| 807 |
for (ox = 0; ox < dim->width; ox++) { |
| 808 |
for (sx = 0; sx < SSZ; sx++) { |
| 809 |
fpmath_t ixfp = fpfrac(ox * dim_org->width, dim->width); |
| 810 |
weight_x[ox][sx] = lanczos_weight(ixfp, sx); |
| 811 |
} |
| 812 |
} |
| 813 |
|
| 814 |
|
| 815 |
|
| 816 |
|
| 817 |
|
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
const uint8_t *ypix[SSZ]; |
| 822 |
for (sy = 0; sy < SSZ; sy++) { |
| 823 |
if (sy <= S0) |
| 824 |
ypix[sy] = pixel_array; |
| 825 |
else if (sy - S0 >= dim_org->height) |
| 826 |
ypix[sy] = ypix[sy - 1]; |
| 827 |
else |
| 828 |
ypix[sy] = &pixel_array[y_stride * (sy - S0)]; |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
iy = 0; |
| 833 |
for (oy = 0; oy < dim->height; oy++, p.y += dir) { |
| 834 |
struct rgb_color sample[SSZ][SSZ]; |
| 835 |
|
| 836 |
|
| 837 |
fpmath_t iyfp = fpfrac(oy * dim_org->height, dim->height); |
| 838 |
fpmath_t weight_y[SSZ]; |
| 839 |
for (sy = 0; sy < SSZ; sy++) |
| 840 |
weight_y[sy] = lanczos_weight(iyfp, sy); |
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
while (fpfloor(iyfp) > iy) { |
| 850 |
iy++; |
| 851 |
|
| 852 |
|
| 853 |
for (sy = 0; sy < SSZ - 1; sy++) |
| 854 |
ypix[sy] = ypix[sy + 1]; |
| 855 |
|
| 856 |
|
| 857 |
|
| 858 |
if (iy + LNCZ_A < dim_org->height) |
| 859 |
ypix[SSZ - 1] = &pixel_array[y_stride * |
| 860 |
(iy + LNCZ_A)]; |
| 861 |
} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
int equals = 0; |
| 869 |
uint8_t last_equal = ypix[0][0]; |
| 870 |
for (sx = 0; sx < SSZ; sx++) { |
| 871 |
for (sy = 0; sy < SSZ; sy++) { |
| 872 |
if (sx - S0 >= dim_org->width) { |
| 873 |
sample[sx][sy] = sample[sx - 1][sy]; |
| 874 |
equals++; |
| 875 |
continue; |
| 876 |
} |
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
uint8_t i = ypix[sy][MAX(0, sx - S0)]; |
| 883 |
if (pal_to_rgb(i, pal, header->colors_used, |
| 884 |
&sample[sx][sy])) |
| 885 |
goto bitmap_error; |
| 886 |
if (i == last_equal) { |
| 887 |
equals++; |
| 888 |
} else { |
| 889 |
last_equal = i; |
| 890 |
equals = 1; |
| 891 |
} |
| 892 |
} |
| 893 |
} |
| 894 |
|
| 895 |
ix = 0; |
| 896 |
p.x = top_left->x; |
| 897 |
for (ox = 0; ox < dim->width; ox++, p.x++) { |
| 898 |
|
| 899 |
fpmath_t ixfp = fpfrac(ox * dim_org->width, dim->width); |
| 900 |
while (fpfloor(ixfp) > ix) { |
| 901 |
ix++; |
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
int rx = (SSZ - 1 + ix) % SSZ; |
| 914 |
for (sy = 0; sy < SSZ; sy++) { |
| 915 |
if (ix + LNCZ_A >= dim_org->width) { |
| 916 |
sample[rx][sy] = sample[(SSZ - 2 |
| 917 |
+ ix) % SSZ][sy]; |
| 918 |
equals++; |
| 919 |
continue; |
| 920 |
} |
| 921 |
uint8_t i = ypix[sy][ix + LNCZ_A]; |
| 922 |
if (i == last_equal) { |
| 923 |
if (equals++ >= (SSZ * SSZ)) |
| 924 |
continue; |
| 925 |
} else { |
| 926 |
last_equal = i; |
| 927 |
equals = 1; |
| 928 |
} |
| 929 |
if (pal_to_rgb(i, pal, |
| 930 |
header->colors_used, |
| 931 |
&sample[rx][sy])) |
| 932 |
goto bitmap_error; |
| 933 |
} |
| 934 |
} |
| 935 |
|
| 936 |
|
| 937 |
if (equals >= (SSZ * SSZ)) { |
| 938 |
set_pixel(&p, calculate_color(&sample[0][0], |
| 939 |
invert)); |
| 940 |
continue; |
| 941 |
} |
| 942 |
|
| 943 |
fpmath_t red = fp(0); |
| 944 |
fpmath_t green = fp(0); |
| 945 |
fpmath_t blue = fp(0); |
| 946 |
for (sy = 0; sy < SSZ; sy++) { |
| 947 |
for (sx = 0; sx < SSZ; sx++) { |
| 948 |
int rx = (sx + ix) % SSZ; |
| 949 |
fpmath_t weight = fpmul(weight_x[ox][sx], |
| 950 |
weight_y[sy]); |
| 951 |
red = fpadd(red, fpmuli(weight, |
| 952 |
sample[rx][sy].red)); |
| 953 |
green = fpadd(green, fpmuli(weight, |
| 954 |
sample[rx][sy].green)); |
| 955 |
blue = fpadd(blue, fpmuli(weight, |
| 956 |
sample[rx][sy].blue)); |
| 957 |
} |
| 958 |
} |
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
struct rgb_color rgb = { |
| 966 |
.red = MAX(0, MIN(UINT8_MAX, fpround(red))), |
| 967 |
.green = MAX(0, MIN(UINT8_MAX, fpround(green))), |
| 968 |
.blue = MAX(0, MIN(UINT8_MAX, fpround(blue))), |
| 969 |
}; |
| 970 |
|
| 971 |
set_pixel(&p, calculate_color(&rgb, invert)); |
| 972 |
} |
| 973 |
} |
| 974 |
|
| 975 |
free(weight_x); |
| 976 |
return CBGFX_SUCCESS; |
| 977 |
|
| 978 |
bitmap_error: |
| 979 |
free(weight_x); |
| 980 |
return CBGFX_ERROR_BITMAP_DATA; |
| 981 |
} |
| 982 |
|
| 983 |
static int get_bitmap_file_header(const void *bitmap, size_t size, |
| 984 |
struct bitmap_file_header *file_header) |
| 985 |
{ |
| 986 |
const struct bitmap_file_header *fh; |
| 987 |
|
| 988 |
if (sizeof(*file_header) > size) { |
| 989 |
LOG("Invalid bitmap data\n"); |
| 990 |
return CBGFX_ERROR_BITMAP_DATA; |
| 991 |
} |
| 992 |
fh = (struct bitmap_file_header *)bitmap; |
| 993 |
if (fh->signature[0] != 'B' || fh->signature[1] != 'M') { |
| 994 |
LOG("Bitmap signature mismatch\n"); |
| 995 |
return CBGFX_ERROR_BITMAP_SIGNATURE; |
| 996 |
} |
| 997 |
file_header->file_size = le32toh(fh->file_size); |
| 998 |
if (file_header->file_size != size) { |
| 999 |
LOG("Bitmap file size does not match cbfs file size\n"); |
| 1000 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1001 |
} |
| 1002 |
file_header->bitmap_offset = le32toh(fh->bitmap_offset); |
| 1003 |
|
| 1004 |
return CBGFX_SUCCESS; |
| 1005 |
} |
| 1006 |
|
| 1007 |
static int parse_bitmap_header_v3( |
| 1008 |
const uint8_t *bitmap, |
| 1009 |
size_t size, |
| 1010 |
|
| 1011 |
struct bitmap_header_v3 *header, |
| 1012 |
const struct bitmap_palette_element_v3 **palette, |
| 1013 |
const uint8_t **pixel_array, |
| 1014 |
struct vector *dim_org) |
| 1015 |
{ |
| 1016 |
struct bitmap_file_header file_header; |
| 1017 |
struct bitmap_header_v3 *h; |
| 1018 |
int rv; |
| 1019 |
|
| 1020 |
rv = get_bitmap_file_header(bitmap, size, &file_header); |
| 1021 |
if (rv) |
| 1022 |
return rv; |
| 1023 |
|
| 1024 |
size_t header_offset = sizeof(struct bitmap_file_header); |
| 1025 |
size_t header_size = sizeof(struct bitmap_header_v3); |
| 1026 |
size_t palette_offset = header_offset + header_size; |
| 1027 |
size_t file_size = file_header.file_size; |
| 1028 |
|
| 1029 |
h = (struct bitmap_header_v3 *)(bitmap + header_offset); |
| 1030 |
header->header_size = le32toh(h->header_size); |
| 1031 |
if (header->header_size != header_size) { |
| 1032 |
LOG("Unsupported bitmap format\n"); |
| 1033 |
return CBGFX_ERROR_BITMAP_FORMAT; |
| 1034 |
} |
| 1035 |
|
| 1036 |
header->width = le32toh(h->width); |
| 1037 |
header->height = le32toh(h->height); |
| 1038 |
if (header->width == 0 || header->height == 0) { |
| 1039 |
LOG("Invalid image width or height\n"); |
| 1040 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1041 |
} |
| 1042 |
dim_org->width = header->width; |
| 1043 |
dim_org->height = ABS(header->height); |
| 1044 |
|
| 1045 |
header->bits_per_pixel = le16toh(h->bits_per_pixel); |
| 1046 |
header->compression = le32toh(h->compression); |
| 1047 |
header->size = le32toh(h->size); |
| 1048 |
header->colors_used = le32toh(h->colors_used); |
| 1049 |
size_t palette_size = header->colors_used |
| 1050 |
* sizeof(struct bitmap_palette_element_v3); |
| 1051 |
size_t pixel_offset = file_header.bitmap_offset; |
| 1052 |
if (pixel_offset > file_size) { |
| 1053 |
LOG("Bitmap pixel data exceeds buffer boundary\n"); |
| 1054 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1055 |
} |
| 1056 |
if (palette_offset + palette_size > pixel_offset) { |
| 1057 |
LOG("Bitmap palette data exceeds palette boundary\n"); |
| 1058 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1059 |
} |
| 1060 |
*palette = (struct bitmap_palette_element_v3 *)(bitmap + |
| 1061 |
palette_offset); |
| 1062 |
|
| 1063 |
size_t pixel_size = header->size; |
| 1064 |
if (pixel_size != dim_org->height * |
| 1065 |
ROUNDUP(dim_org->width * header->bits_per_pixel / 8, 4)) { |
| 1066 |
LOG("Bitmap pixel array size does not match expected size\n"); |
| 1067 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1068 |
} |
| 1069 |
if (pixel_offset + pixel_size > file_size) { |
| 1070 |
LOG("Bitmap pixel array exceeds buffer boundary\n"); |
| 1071 |
return CBGFX_ERROR_BITMAP_DATA; |
| 1072 |
} |
| 1073 |
*pixel_array = bitmap + pixel_offset; |
| 1074 |
|
| 1075 |
return CBGFX_SUCCESS; |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
static int calculate_dimension(const struct vector *dim_org, |
| 1084 |
const struct scale *dim_rel, |
| 1085 |
struct vector *dim) |
| 1086 |
{ |
| 1087 |
if (dim_rel->x.n == 0 && dim_rel->y.n == 0) |
| 1088 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1089 |
|
| 1090 |
if (dim_rel->x.n > dim_rel->x.d || dim_rel->y.n > dim_rel->y.d) |
| 1091 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1092 |
|
| 1093 |
if (dim_rel->x.n > 0) { |
| 1094 |
if (!is_valid_fraction(&dim_rel->x)) |
| 1095 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1096 |
dim->width = canvas.size.width * dim_rel->x.n / dim_rel->x.d; |
| 1097 |
} |
| 1098 |
if (dim_rel->y.n > 0) { |
| 1099 |
if (!is_valid_fraction(&dim_rel->y)) |
| 1100 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1101 |
dim->height = canvas.size.height * dim_rel->y.n / dim_rel->y.d; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
if (dim_rel->y.n == 0) |
| 1106 |
dim->height = dim->width * dim_org->height / dim_org->width; |
| 1107 |
|
| 1108 |
if (dim_rel->x.n == 0) |
| 1109 |
dim->width = dim->height * dim_org->width / dim_org->height; |
| 1110 |
|
| 1111 |
return CBGFX_SUCCESS; |
| 1112 |
} |
| 1113 |
|
| 1114 |
static int calculate_position(const struct vector *dim, |
| 1115 |
const struct scale *pos_rel, uint8_t pivot, |
| 1116 |
struct vector *top_left) |
| 1117 |
{ |
| 1118 |
int rv; |
| 1119 |
|
| 1120 |
rv = transform_vector(top_left, &canvas.size, pos_rel, &canvas.offset); |
| 1121 |
if (rv) |
| 1122 |
return rv; |
| 1123 |
|
| 1124 |
switch (pivot & PIVOT_H_MASK) { |
| 1125 |
case PIVOT_H_LEFT: |
| 1126 |
break; |
| 1127 |
case PIVOT_H_CENTER: |
| 1128 |
top_left->x -= dim->width / 2; |
| 1129 |
break; |
| 1130 |
case PIVOT_H_RIGHT: |
| 1131 |
top_left->x -= dim->width; |
| 1132 |
break; |
| 1133 |
default: |
| 1134 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1135 |
} |
| 1136 |
|
| 1137 |
switch (pivot & PIVOT_V_MASK) { |
| 1138 |
case PIVOT_V_TOP: |
| 1139 |
break; |
| 1140 |
case PIVOT_V_CENTER: |
| 1141 |
top_left->y -= dim->height / 2; |
| 1142 |
break; |
| 1143 |
case PIVOT_V_BOTTOM: |
| 1144 |
top_left->y -= dim->height; |
| 1145 |
break; |
| 1146 |
default: |
| 1147 |
return CBGFX_ERROR_INVALID_PARAMETER; |
| 1148 |
} |
| 1149 |
|
| 1150 |
return CBGFX_SUCCESS; |
| 1151 |
} |
| 1152 |
|
| 1153 |
static int check_boundary(const struct vector *top_left, |
| 1154 |
const struct vector *dim, |
| 1155 |
const struct rect *bound) |
| 1156 |
{ |
| 1157 |
struct vector v; |
| 1158 |
add_vectors(&v, dim, top_left); |
| 1159 |
if (top_left->x < bound->offset.x |
| 1160 |
|| top_left->y < bound->offset.y |
| 1161 |
|| within_box(&v, bound) < 0) |
| 1162 |
return CBGFX_ERROR_BOUNDARY; |
| 1163 |
return CBGFX_SUCCESS; |
| 1164 |
} |
| 1165 |
|
| 1166 |
int draw_bitmap(const void *bitmap, size_t size, |
| 1167 |
const struct scale *pos_rel, const struct scale *dim_rel, |
| 1168 |
uint32_t flags) |
| 1169 |
{ |
| 1170 |
struct bitmap_header_v3 header; |
| 1171 |
const struct bitmap_palette_element_v3 *palette; |
| 1172 |
const uint8_t *pixel_array; |
| 1173 |
struct vector top_left, dim, dim_org; |
| 1174 |
int rv; |
| 1175 |
const uint8_t pivot = flags & PIVOT_MASK; |
| 1176 |
const uint8_t invert = (flags & INVERT_COLORS) >> INVERT_SHIFT; |
| 1177 |
|
| 1178 |
if (cbgfx_init()) |
| 1179 |
return CBGFX_ERROR_INIT; |
| 1180 |
|
| 1181 |
|
| 1182 |
rv = parse_bitmap_header_v3(bitmap, size, |
| 1183 |
&header, &palette, &pixel_array, &dim_org); |
| 1184 |
if (rv) |
| 1185 |
return rv; |
| 1186 |
|
| 1187 |
|
| 1188 |
rv = calculate_dimension(&dim_org, dim_rel, &dim); |
| 1189 |
if (rv) |
| 1190 |
return rv; |
| 1191 |
|
| 1192 |
|
| 1193 |
rv = calculate_position(&dim, pos_rel, pivot, &top_left); |
| 1194 |
if (rv) |
| 1195 |
return rv; |
| 1196 |
|
| 1197 |
rv = check_boundary(&top_left, &dim, &canvas); |
| 1198 |
if (rv) { |
| 1199 |
LOG("Bitmap image exceeds canvas boundary\n"); |
| 1200 |
return rv; |
| 1201 |
} |
| 1202 |
|
| 1203 |
return draw_bitmap_v3(&top_left, &dim, &dim_org, |
| 1204 |
&header, palette, pixel_array, invert); |
| 1205 |
} |
| 1206 |
|
| 1207 |
int draw_bitmap_direct(const void *bitmap, size_t size, |
| 1208 |
const struct vector *top_left) |
| 1209 |
{ |
| 1210 |
struct bitmap_header_v3 header; |
| 1211 |
const struct bitmap_palette_element_v3 *palette; |
| 1212 |
const uint8_t *pixel_array; |
| 1213 |
struct vector dim; |
| 1214 |
int rv; |
| 1215 |
|
| 1216 |
if (cbgfx_init()) |
| 1217 |
return CBGFX_ERROR_INIT; |
| 1218 |
|
| 1219 |
|
| 1220 |
rv = parse_bitmap_header_v3(bitmap, size, |
| 1221 |
&header, &palette, &pixel_array, &dim); |
| 1222 |
if (rv) |
| 1223 |
return rv; |
| 1224 |
|
| 1225 |
rv = check_boundary(top_left, &dim, &screen); |
| 1226 |
if (rv) { |
| 1227 |
LOG("Bitmap image exceeds screen boundary\n"); |
| 1228 |
return rv; |
| 1229 |
} |
| 1230 |
|
| 1231 |
return draw_bitmap_v3(top_left, &dim, &dim, |
| 1232 |
&header, palette, pixel_array, 0); |
| 1233 |
} |
| 1234 |
|
| 1235 |
int get_bitmap_dimension(const void *bitmap, size_t sz, struct scale *dim_rel) |
| 1236 |
{ |
| 1237 |
struct bitmap_header_v3 header; |
| 1238 |
const struct bitmap_palette_element_v3 *palette; |
| 1239 |
const uint8_t *pixel_array; |
| 1240 |
struct vector dim, dim_org; |
| 1241 |
int rv; |
| 1242 |
|
| 1243 |
if (cbgfx_init()) |
| 1244 |
return CBGFX_ERROR_INIT; |
| 1245 |
|
| 1246 |
|
| 1247 |
rv = parse_bitmap_header_v3(bitmap, sz, |
| 1248 |
&header, &palette, &pixel_array, &dim_org); |
| 1249 |
if (rv) |
| 1250 |
return rv; |
| 1251 |
|
| 1252 |
|
| 1253 |
rv = calculate_dimension(&dim_org, dim_rel, &dim); |
| 1254 |
if (rv) |
| 1255 |
return rv; |
| 1256 |
|
| 1257 |
|
| 1258 |
dim_rel->x.n = dim.width; |
| 1259 |
dim_rel->x.d = canvas.size.width; |
| 1260 |
dim_rel->y.n = dim.height; |
| 1261 |
dim_rel->y.d = canvas.size.height; |
| 1262 |
|
| 1263 |
return CBGFX_SUCCESS; |
| 1264 |
} |
| 1265 |
|
| 1266 |
int enable_graphics_buffer(void) |
| 1267 |
{ |
| 1268 |
if (gfx_buffer) |
| 1269 |
return CBGFX_SUCCESS; |
| 1270 |
|
| 1271 |
if (cbgfx_init()) |
| 1272 |
return CBGFX_ERROR_INIT; |
| 1273 |
|
| 1274 |
size_t buffer_size = fbinfo->y_resolution * fbinfo->bytes_per_line; |
| 1275 |
gfx_buffer = malloc(buffer_size); |
| 1276 |
if (!gfx_buffer) { |
| 1277 |
LOG("%s: Failed to create graphics buffer (%zu bytes).\n", |
| 1278 |
__func__, buffer_size); |
| 1279 |
return CBGFX_ERROR_GRAPHICS_BUFFER; |
| 1280 |
} |
| 1281 |
|
| 1282 |
return CBGFX_SUCCESS; |
| 1283 |
} |
| 1284 |
|
| 1285 |
int flush_graphics_buffer(void) |
| 1286 |
{ |
| 1287 |
if (!gfx_buffer) |
| 1288 |
return CBGFX_ERROR_GRAPHICS_BUFFER; |
| 1289 |
|
| 1290 |
memcpy(REAL_FB, gfx_buffer, fbinfo->y_resolution * fbinfo->bytes_per_line); |
| 1291 |
return CBGFX_SUCCESS; |
| 1292 |
} |
| 1293 |
|
| 1294 |
void disable_graphics_buffer(void) |
| 1295 |
{ |
| 1296 |
free(gfx_buffer); |
| 1297 |
gfx_buffer = NULL; |
| 1298 |
} |