2015-07-30 01:03:52 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the libpayload project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2015 Google, Inc.
|
2015-10-23 13:24:06 +02:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
2015-07-30 01:03:52 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libpayload.h>
|
2015-09-24 21:42:17 +02:00
|
|
|
#include <cbfs.h>
|
2015-07-30 01:03:52 +02:00
|
|
|
#include <sysinfo.h>
|
2015-08-03 19:51:38 +02:00
|
|
|
#include "bitmap.h"
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* 'canvas' is the drawing area located in the center of the screen. It's a
|
|
|
|
* square area, stretching vertically to the edges of the screen, leaving
|
|
|
|
* non-drawing areas on the left and right. The screen is assumed to be
|
|
|
|
* landscape.
|
|
|
|
*/
|
2015-08-26 23:47:06 +02:00
|
|
|
static struct rect canvas;
|
|
|
|
static struct rect screen;
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Framebuffer is assumed to assign a higher coordinate (larger x, y) to
|
|
|
|
* a higher address
|
|
|
|
*/
|
|
|
|
static struct cb_framebuffer *fbinfo;
|
|
|
|
static uint8_t *fbaddr;
|
|
|
|
|
|
|
|
#define LOG(x...) printf("CBGFX: " x)
|
2015-09-28 22:14:43 +02:00
|
|
|
#define PIVOT_H_MASK (PIVOT_H_LEFT|PIVOT_H_CENTER|PIVOT_H_RIGHT)
|
|
|
|
#define PIVOT_V_MASK (PIVOT_V_TOP|PIVOT_V_CENTER|PIVOT_V_BOTTOM)
|
2015-08-26 23:47:06 +02:00
|
|
|
#define ROUNDUP(x, y) ((((x) + ((y) - 1)) / (y)) * (y))
|
2015-08-03 19:51:38 +02:00
|
|
|
#define ABS(x) ((x) < 0 ? -(x) : (x))
|
|
|
|
|
2015-09-28 22:14:43 +02:00
|
|
|
static char initialized = 0;
|
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
static const struct vector vzero = {
|
|
|
|
.x = 0,
|
|
|
|
.y = 0,
|
|
|
|
};
|
|
|
|
|
2015-07-30 01:03:52 +02:00
|
|
|
static void add_vectors(struct vector *out,
|
|
|
|
const struct vector *v1, const struct vector *v2)
|
|
|
|
{
|
|
|
|
out->x = v1->x + v2->x;
|
|
|
|
out->y = v1->y + v2->y;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
static int is_valid_fraction(const struct fraction *f)
|
|
|
|
{
|
|
|
|
return f->d != 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
/*
|
|
|
|
* Transform a vector:
|
|
|
|
* x' = x * a_x + offset_x
|
|
|
|
* y' = y * a_y + offset_y
|
|
|
|
*/
|
2015-10-08 23:03:56 +02:00
|
|
|
static int transform_vector(struct vector *out,
|
|
|
|
const struct vector *in,
|
|
|
|
const struct scale *a,
|
|
|
|
const struct vector *offset)
|
2015-07-30 01:03:52 +02:00
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
if (!is_valid_fraction(&a->x) || !is_valid_fraction(&a->y))
|
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
out->x = a->x.n * in->x / a->x.d + offset->x;
|
|
|
|
out->y = a->y.n * in->y / a->y.d + offset->y;
|
|
|
|
return CBGFX_SUCCESS;
|
2015-07-30 01:03:52 +02:00
|
|
|
}
|
|
|
|
|
2015-08-03 19:51:38 +02:00
|
|
|
/*
|
2015-08-26 23:47:06 +02:00
|
|
|
* Returns 1 if v is exclusively within box, 0 if v is inclusively within box,
|
2015-09-28 22:14:43 +02:00
|
|
|
* or -1 otherwise. Note that only the right and bottom edges are examined.
|
2015-08-03 19:51:38 +02:00
|
|
|
*/
|
2015-08-26 23:47:06 +02:00
|
|
|
static int within_box(const struct vector *v, const struct rect *bound)
|
2015-07-30 01:03:52 +02:00
|
|
|
{
|
2015-08-26 23:47:06 +02:00
|
|
|
if (v->x < bound->offset.x + bound->size.width &&
|
|
|
|
v->y < bound->offset.y + bound->size.height)
|
2015-08-03 19:51:38 +02:00
|
|
|
return 1;
|
2015-08-26 23:47:06 +02:00
|
|
|
else if (v->x <= bound->offset.x + bound->size.width &&
|
|
|
|
v->y <= bound->offset.y + bound->size.height)
|
2015-08-03 19:51:38 +02:00
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return -1;
|
2015-07-30 01:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static inline uint32_t calculate_color(const struct rgb_color *rgb)
|
|
|
|
{
|
|
|
|
uint32_t color = 0;
|
|
|
|
color |= (rgb->red >> (8 - fbinfo->red_mask_size))
|
|
|
|
<< fbinfo->red_mask_pos;
|
|
|
|
color |= (rgb->green >> (8 - fbinfo->green_mask_size))
|
|
|
|
<< fbinfo->green_mask_pos;
|
|
|
|
color |= (rgb->blue >> (8 - fbinfo->blue_mask_size))
|
|
|
|
<< fbinfo->blue_mask_pos;
|
|
|
|
return color;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Plot a pixel in a framebuffer. This is called from tight loops. Keep it slim
|
|
|
|
* and do the validation at callers' site.
|
|
|
|
*/
|
|
|
|
static inline void set_pixel(struct vector *coord, uint32_t color)
|
|
|
|
{
|
|
|
|
const int bpp = fbinfo->bits_per_pixel;
|
2017-04-13 23:27:20 +02:00
|
|
|
const int bpl = fbinfo->bytes_per_line;
|
2015-07-30 01:03:52 +02:00
|
|
|
int i;
|
2017-04-13 23:27:20 +02:00
|
|
|
uint8_t * const pixel = fbaddr + coord->y * bpl + coord->x * bpp / 8;
|
2015-07-30 01:03:52 +02:00
|
|
|
for (i = 0; i < bpp / 8; i++)
|
|
|
|
pixel[i] = (color >> (i * 8));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Initializes the library. Automatically called by APIs. It sets up
|
|
|
|
* the canvas and the framebuffer.
|
|
|
|
*/
|
|
|
|
static int cbgfx_init(void)
|
|
|
|
{
|
|
|
|
if (initialized)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
fbinfo = lib_sysinfo.framebuffer;
|
|
|
|
if (!fbinfo)
|
2015-12-02 20:42:51 +01:00
|
|
|
return CBGFX_ERROR_FRAMEBUFFER_INFO;
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
fbaddr = phys_to_virt((uint8_t *)(uintptr_t)(fbinfo->physical_address));
|
|
|
|
if (!fbaddr)
|
2015-12-02 20:42:51 +01:00
|
|
|
return CBGFX_ERROR_FRAMEBUFFER_ADDR;
|
2015-07-30 01:03:52 +02:00
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
screen.size.width = fbinfo->x_resolution;
|
|
|
|
screen.size.height = fbinfo->y_resolution;
|
|
|
|
screen.offset.x = 0;
|
|
|
|
screen.offset.y = 0;
|
|
|
|
|
|
|
|
/* Calculate canvas size & offset, assuming the screen is landscape */
|
|
|
|
if (screen.size.height > screen.size.width) {
|
2017-04-13 23:27:20 +02:00
|
|
|
const int bpl = fbinfo->bytes_per_line;
|
|
|
|
LOG("Portrait screen not supported, forcing square image!\n");
|
|
|
|
memset(fbaddr + screen.size.width * bpl, 0,
|
|
|
|
(screen.size.height - screen.size.width) * bpl);
|
|
|
|
screen.size.height = screen.size.width;
|
2015-07-30 01:03:52 +02:00
|
|
|
}
|
2015-08-26 23:47:06 +02:00
|
|
|
canvas.size.height = screen.size.height;
|
|
|
|
canvas.size.width = canvas.size.height;
|
|
|
|
canvas.offset.x = (screen.size.width - canvas.size.width) / 2;
|
|
|
|
canvas.offset.y = 0;
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
initialized = 1;
|
2015-08-26 23:47:06 +02:00
|
|
|
LOG("cbgfx initialized: screen:width=%d, height=%d, offset=%d canvas:width=%d, height=%d, offset=%d\n",
|
|
|
|
screen.size.width, screen.size.height, screen.offset.x,
|
|
|
|
canvas.size.width, canvas.size.height, canvas.offset.x);
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
int draw_box(const struct rect *box, const struct rgb_color *rgb)
|
2015-07-30 01:03:52 +02:00
|
|
|
{
|
|
|
|
struct vector top_left;
|
|
|
|
struct vector size;
|
|
|
|
struct vector p, t;
|
|
|
|
const uint32_t color = calculate_color(rgb);
|
2015-08-26 23:47:06 +02:00
|
|
|
const struct scale top_left_s = {
|
2015-10-08 23:03:56 +02:00
|
|
|
.x = { .n = box->offset.x, .d = CANVAS_SCALE, },
|
|
|
|
.y = { .n = box->offset.y, .d = CANVAS_SCALE, }
|
2015-08-26 23:47:06 +02:00
|
|
|
};
|
|
|
|
const struct scale size_s = {
|
2015-10-08 23:03:56 +02:00
|
|
|
.x = { .n = box->size.x, .d = CANVAS_SCALE, },
|
|
|
|
.y = { .n = box->size.y, .d = CANVAS_SCALE, }
|
2015-08-26 23:47:06 +02:00
|
|
|
};
|
2015-07-30 01:03:52 +02:00
|
|
|
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
transform_vector(&top_left, &canvas.size, &top_left_s, &canvas.offset);
|
|
|
|
transform_vector(&size, &canvas.size, &size_s, &vzero);
|
2015-07-30 01:03:52 +02:00
|
|
|
add_vectors(&t, &top_left, &size);
|
2015-08-26 23:47:06 +02:00
|
|
|
if (within_box(&t, &canvas) < 0) {
|
2015-07-30 01:03:52 +02:00
|
|
|
LOG("Box exceeds canvas boundary\n");
|
2015-09-28 22:14:43 +02:00
|
|
|
return CBGFX_ERROR_BOUNDARY;
|
2015-07-30 01:03:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for (p.y = top_left.y; p.y < t.y; p.y++)
|
|
|
|
for (p.x = top_left.x; p.x < t.x; p.x++)
|
|
|
|
set_pixel(&p, color);
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-09-28 22:14:43 +02:00
|
|
|
int clear_canvas(const struct rgb_color *rgb)
|
2015-07-30 01:03:52 +02:00
|
|
|
{
|
2015-08-26 23:47:06 +02:00
|
|
|
const struct rect box = {
|
|
|
|
vzero,
|
|
|
|
.size = {
|
|
|
|
.width = CANVAS_SCALE,
|
|
|
|
.height = CANVAS_SCALE,
|
|
|
|
},
|
2015-07-30 01:03:52 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
|
|
|
|
2015-08-26 23:47:06 +02:00
|
|
|
return draw_box(&box, rgb);
|
|
|
|
}
|
|
|
|
|
2015-09-28 22:14:43 +02:00
|
|
|
int clear_screen(const struct rgb_color *rgb)
|
2015-09-22 02:17:25 +02:00
|
|
|
{
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
|
|
|
|
2016-07-01 07:34:57 +02:00
|
|
|
struct vector p;
|
|
|
|
uint32_t color = calculate_color(rgb);
|
|
|
|
const int bpp = fbinfo->bits_per_pixel;
|
|
|
|
const int bpl = fbinfo->bytes_per_line;
|
|
|
|
|
|
|
|
/* If all significant bytes in color are equal, fastpath through memset.
|
|
|
|
* We assume that for 32bpp the high byte gets ignored anyway. */
|
|
|
|
if ((((color >> 8) & 0xff) == (color & 0xff)) && (bpp == 16 ||
|
|
|
|
(((color >> 16) & 0xff) == (color & 0xff)))) {
|
|
|
|
memset(fbaddr, color & 0xff, screen.size.height * bpl);
|
|
|
|
} else {
|
|
|
|
for (p.y = 0; p.y < screen.size.height; p.y++)
|
|
|
|
for (p.x = 0; p.x < screen.size.width; p.x++)
|
|
|
|
set_pixel(&p, color);
|
|
|
|
}
|
2015-09-22 02:17:25 +02:00
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/*
|
|
|
|
* Bi-linear Interpolation
|
|
|
|
*
|
|
|
|
* It estimates the value of a middle point (tx, ty) using the values from four
|
|
|
|
* adjacent points (q00, q01, q10, q11).
|
|
|
|
*/
|
2015-09-24 21:42:51 +02:00
|
|
|
static uint32_t bli(uint32_t q00, uint32_t q10, uint32_t q01, uint32_t q11,
|
|
|
|
struct fraction *tx, struct fraction *ty)
|
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
uint32_t r0 = (tx->n * q10 + (tx->d - tx->n) * q00) / tx->d;
|
|
|
|
uint32_t r1 = (tx->n * q11 + (tx->d - tx->n) * q01) / tx->d;
|
|
|
|
uint32_t p = (ty->n * r1 + (ty->d - ty->n) * r0) / ty->d;
|
2015-09-24 21:42:51 +02:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2015-08-03 19:51:38 +02:00
|
|
|
static int draw_bitmap_v3(const struct vector *top_left,
|
2015-08-26 23:47:06 +02:00
|
|
|
const struct scale *scale,
|
2015-10-08 23:03:56 +02:00
|
|
|
const struct vector *dim,
|
|
|
|
const struct vector *dim_org,
|
2015-08-03 19:51:38 +02:00
|
|
|
const struct bitmap_header_v3 *header,
|
2015-09-24 21:42:51 +02:00
|
|
|
const struct bitmap_palette_element_v3 *pal,
|
2015-08-03 19:51:38 +02:00
|
|
|
const uint8_t *pixel_array)
|
|
|
|
{
|
|
|
|
const int bpp = header->bits_per_pixel;
|
|
|
|
int32_t dir;
|
|
|
|
struct vector p;
|
|
|
|
|
|
|
|
if (header->compression) {
|
|
|
|
LOG("Compressed bitmaps are not supported\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_FORMAT;
|
|
|
|
}
|
|
|
|
if (bpp >= 16) {
|
|
|
|
LOG("Non-palette bitmaps are not supported\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_FORMAT;
|
|
|
|
}
|
|
|
|
if (bpp != 8) {
|
|
|
|
LOG("Unsupported bits per pixel: %d\n", bpp);
|
|
|
|
return CBGFX_ERROR_BITMAP_FORMAT;
|
|
|
|
}
|
2015-10-08 23:03:56 +02:00
|
|
|
if (scale->x.n == 0 || scale->y.n == 0) {
|
2015-08-03 19:51:38 +02:00
|
|
|
LOG("Scaling out of range\n");
|
|
|
|
return CBGFX_ERROR_SCALE_OUT_OF_RANGE;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
const int32_t y_stride = ROUNDUP(dim_org->width * bpp / 8, 4);
|
2015-08-03 19:51:38 +02:00
|
|
|
/*
|
|
|
|
* header->height can be positive or negative.
|
|
|
|
*
|
|
|
|
* If it's negative, pixel data is stored from top to bottom. We render
|
|
|
|
* image from the lowest row to the highest row.
|
|
|
|
*
|
|
|
|
* If it's positive, pixel data is stored from bottom to top. We render
|
|
|
|
* image from the highest row to the lowest row.
|
|
|
|
*/
|
|
|
|
p.y = top_left->y;
|
|
|
|
if (header->height < 0) {
|
|
|
|
dir = 1;
|
|
|
|
} else {
|
2015-10-08 23:03:56 +02:00
|
|
|
p.y += dim->height - 1;
|
2015-08-03 19:51:38 +02:00
|
|
|
dir = -1;
|
|
|
|
}
|
|
|
|
/*
|
2015-10-08 23:03:56 +02:00
|
|
|
* Plot pixels scaled by the bilinear interpolation. We scan over the
|
|
|
|
* image on canvas (using d) and find the corresponding pixel in the
|
|
|
|
* bitmap data (using s0, s1).
|
|
|
|
*
|
|
|
|
* When d hits the right bottom corner, s0 also hits the right bottom
|
|
|
|
* corner of the pixel array because that's how scale->x and scale->y
|
|
|
|
* have been set. Since the pixel array size is already validated in
|
|
|
|
* parse_bitmap_header_v3, s0 is guranteed not to exceed pixel array
|
|
|
|
* boundary.
|
2015-08-03 19:51:38 +02:00
|
|
|
*/
|
2015-09-24 21:42:51 +02:00
|
|
|
struct vector s0, s1, d;
|
|
|
|
struct fraction tx, ty;
|
2015-10-08 23:03:56 +02:00
|
|
|
for (d.y = 0; d.y < dim->height; d.y++, p.y += dir) {
|
|
|
|
s0.y = d.y * scale->y.d / scale->y.n;
|
2015-09-24 21:42:51 +02:00
|
|
|
s1.y = s0.y;
|
2016-12-13 15:55:26 +01:00
|
|
|
if (s1.y + 1 < dim_org->height)
|
2015-09-24 21:42:51 +02:00
|
|
|
s1.y++;
|
2015-10-08 23:03:56 +02:00
|
|
|
ty.d = scale->y.n;
|
|
|
|
ty.n = (d.y * scale->y.d) % scale->y.n;
|
2015-09-24 21:42:51 +02:00
|
|
|
const uint8_t *data0 = pixel_array + s0.y * y_stride;
|
|
|
|
const uint8_t *data1 = pixel_array + s1.y * y_stride;
|
2015-08-03 19:51:38 +02:00
|
|
|
p.x = top_left->x;
|
2015-10-08 23:03:56 +02:00
|
|
|
for (d.x = 0; d.x < dim->width; d.x++, p.x++) {
|
|
|
|
s0.x = d.x * scale->x.d / scale->x.n;
|
2015-09-24 21:42:51 +02:00
|
|
|
s1.x = s0.x;
|
2015-10-08 23:03:56 +02:00
|
|
|
if (s1.x + 1 < dim_org->width)
|
2015-09-24 21:42:51 +02:00
|
|
|
s1.x++;
|
2015-10-08 23:03:56 +02:00
|
|
|
tx.d = scale->x.n;
|
|
|
|
tx.n = (d.x * scale->x.d) % scale->x.n;
|
2015-09-24 21:42:51 +02:00
|
|
|
uint8_t c00 = data0[s0.x];
|
|
|
|
uint8_t c10 = data0[s1.x];
|
|
|
|
uint8_t c01 = data1[s0.x];
|
|
|
|
uint8_t c11 = data1[s1.x];
|
|
|
|
if (c00 >= header->colors_used
|
|
|
|
|| c10 >= header->colors_used
|
|
|
|
|| c01 >= header->colors_used
|
|
|
|
|| c11 >= header->colors_used) {
|
2015-08-03 19:51:38 +02:00
|
|
|
LOG("Color index exceeds palette boundary\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
const struct rgb_color rgb = {
|
2015-09-24 21:42:51 +02:00
|
|
|
.red = bli(pal[c00].red, pal[c10].red,
|
|
|
|
pal[c01].red, pal[c11].red,
|
|
|
|
&tx, &ty),
|
|
|
|
.green = bli(pal[c00].green, pal[c10].green,
|
|
|
|
pal[c01].green, pal[c11].green,
|
|
|
|
&tx, &ty),
|
|
|
|
.blue = bli(pal[c00].blue, pal[c10].blue,
|
|
|
|
pal[c01].blue, pal[c11].blue,
|
|
|
|
&tx, &ty),
|
2015-08-03 19:51:38 +02:00
|
|
|
};
|
|
|
|
set_pixel(&p, calculate_color(&rgb));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int get_bitmap_file_header(const void *bitmap, size_t size,
|
|
|
|
struct bitmap_file_header *file_header)
|
|
|
|
{
|
|
|
|
const struct bitmap_file_header *fh;
|
|
|
|
|
|
|
|
if (sizeof(*file_header) > size) {
|
|
|
|
LOG("Invalid bitmap data\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
fh = (struct bitmap_file_header *)bitmap;
|
|
|
|
if (fh->signature[0] != 'B' || fh->signature[1] != 'M') {
|
|
|
|
LOG("Bitmap signature mismatch\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_SIGNATURE;
|
|
|
|
}
|
|
|
|
file_header->file_size = le32toh(fh->file_size);
|
|
|
|
if (file_header->file_size != size) {
|
|
|
|
LOG("Bitmap file size does not match cbfs file size\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
file_header->bitmap_offset = le32toh(fh->bitmap_offset);
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
static int parse_bitmap_header_v3(
|
|
|
|
const uint8_t *bitmap,
|
|
|
|
size_t size,
|
2015-08-26 23:47:06 +02:00
|
|
|
/* ^--- IN / OUT ---v */
|
|
|
|
struct bitmap_header_v3 *header,
|
|
|
|
const struct bitmap_palette_element_v3 **palette,
|
2015-10-08 23:03:56 +02:00
|
|
|
const uint8_t **pixel_array,
|
|
|
|
struct vector *dim_org)
|
2015-08-03 19:51:38 +02:00
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
struct bitmap_file_header file_header;
|
2015-08-03 19:51:38 +02:00
|
|
|
struct bitmap_header_v3 *h;
|
2015-10-08 23:03:56 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = get_bitmap_file_header(bitmap, size, &file_header);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2015-08-03 19:51:38 +02:00
|
|
|
size_t header_offset = sizeof(struct bitmap_file_header);
|
|
|
|
size_t header_size = sizeof(struct bitmap_header_v3);
|
|
|
|
size_t palette_offset = header_offset + header_size;
|
2015-10-08 23:03:56 +02:00
|
|
|
size_t file_size = file_header.file_size;
|
2015-08-03 19:51:38 +02:00
|
|
|
|
|
|
|
h = (struct bitmap_header_v3 *)(bitmap + header_offset);
|
|
|
|
header->header_size = le32toh(h->header_size);
|
|
|
|
if (header->header_size != header_size) {
|
|
|
|
LOG("Unsupported bitmap format\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_FORMAT;
|
|
|
|
}
|
2015-10-08 23:03:56 +02:00
|
|
|
|
2015-08-03 19:51:38 +02:00
|
|
|
header->width = le32toh(h->width);
|
|
|
|
header->height = le32toh(h->height);
|
2015-10-08 23:03:56 +02:00
|
|
|
if (header->width == 0 || header->height == 0) {
|
|
|
|
LOG("Invalid image width or height\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
dim_org->width = header->width;
|
|
|
|
dim_org->height = ABS(header->height);
|
|
|
|
|
2015-08-03 19:51:38 +02:00
|
|
|
header->bits_per_pixel = le16toh(h->bits_per_pixel);
|
|
|
|
header->compression = le32toh(h->compression);
|
|
|
|
header->size = le32toh(h->size);
|
|
|
|
header->colors_used = le32toh(h->colors_used);
|
|
|
|
size_t palette_size = header->colors_used
|
|
|
|
* sizeof(struct bitmap_palette_element_v3);
|
2015-10-08 23:03:56 +02:00
|
|
|
size_t pixel_offset = file_header.bitmap_offset;
|
2015-08-03 19:51:38 +02:00
|
|
|
if (pixel_offset > file_size) {
|
|
|
|
LOG("Bitmap pixel data exceeds buffer boundary\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
if (palette_offset + palette_size > pixel_offset) {
|
|
|
|
LOG("Bitmap palette data exceeds palette boundary\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
*palette = (struct bitmap_palette_element_v3 *)(bitmap +
|
|
|
|
palette_offset);
|
|
|
|
|
|
|
|
size_t pixel_size = header->size;
|
2015-10-08 23:03:56 +02:00
|
|
|
if (pixel_size != dim_org->height *
|
|
|
|
ROUNDUP(dim_org->width * header->bits_per_pixel / 8, 4)) {
|
2015-08-03 19:51:38 +02:00
|
|
|
LOG("Bitmap pixel array size does not match expected size\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
if (pixel_offset + pixel_size > file_size) {
|
|
|
|
LOG("Bitmap pixel array exceeds buffer boundary\n");
|
|
|
|
return CBGFX_ERROR_BITMAP_DATA;
|
|
|
|
}
|
|
|
|
*pixel_array = bitmap + pixel_offset;
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/*
|
|
|
|
* This calculates the dimension of the image projected on the canvas from the
|
|
|
|
* dimension relative to the canvas size. If either width or height is zero, it
|
|
|
|
* is derived from the other (non-zero) value to keep the aspect ratio.
|
|
|
|
*/
|
|
|
|
static int calculate_dimension(const struct vector *dim_org,
|
|
|
|
const struct scale *dim_rel,
|
|
|
|
struct vector *dim)
|
2015-09-28 22:14:43 +02:00
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
if (dim_rel->x.n == 0 && dim_rel->y.n == 0)
|
2015-09-28 22:14:43 +02:00
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
if (dim_rel->x.n > dim_rel->x.d || dim_rel->y.n > dim_rel->y.d)
|
2015-09-28 22:14:43 +02:00
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
if (dim_rel->x.n > 0) {
|
|
|
|
if (!is_valid_fraction(&dim_rel->x))
|
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
dim->width = canvas.size.width * dim_rel->x.n / dim_rel->x.d;
|
2015-09-28 22:14:43 +02:00
|
|
|
}
|
2015-10-08 23:03:56 +02:00
|
|
|
if (dim_rel->y.n > 0) {
|
|
|
|
if (!is_valid_fraction(&dim_rel->y))
|
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
dim->height = canvas.size.height * dim_rel->y.n / dim_rel->y.d;
|
2015-09-28 22:14:43 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/* Derive height from width using aspect ratio */
|
|
|
|
if (dim_rel->y.n == 0)
|
|
|
|
dim->height = dim->width * dim_org->height / dim_org->width;
|
|
|
|
/* Derive width from height using aspect ratio */
|
|
|
|
if (dim_rel->x.n == 0)
|
|
|
|
dim->width = dim->height * dim_org->width / dim_org->height;
|
2015-09-28 22:14:43 +02:00
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-10-28 12:24:25 +01:00
|
|
|
static int calculate_position(const struct vector *dim,
|
2015-09-28 22:14:43 +02:00
|
|
|
const struct scale *pos_rel, uint8_t pivot,
|
|
|
|
struct vector *top_left)
|
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
int rv;
|
2015-09-28 22:14:43 +02:00
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
rv = transform_vector(top_left, &canvas.size, pos_rel, &canvas.offset);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
2015-09-28 22:14:43 +02:00
|
|
|
|
|
|
|
switch (pivot & PIVOT_H_MASK) {
|
|
|
|
case PIVOT_H_LEFT:
|
|
|
|
break;
|
|
|
|
case PIVOT_H_CENTER:
|
|
|
|
top_left->x -= dim->width / 2;
|
|
|
|
break;
|
|
|
|
case PIVOT_H_RIGHT:
|
|
|
|
top_left->x -= dim->width;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (pivot & PIVOT_V_MASK) {
|
|
|
|
case PIVOT_V_TOP:
|
|
|
|
break;
|
|
|
|
case PIVOT_V_CENTER:
|
|
|
|
top_left->y -= dim->height / 2;
|
|
|
|
break;
|
|
|
|
case PIVOT_V_BOTTOM:
|
|
|
|
top_left->y -= dim->height;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return CBGFX_ERROR_INVALID_PARAMETER;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_boundary(const struct vector *top_left,
|
|
|
|
const struct vector *dim,
|
|
|
|
const struct rect *bound)
|
|
|
|
{
|
|
|
|
struct vector v;
|
|
|
|
add_vectors(&v, dim, top_left);
|
|
|
|
if (top_left->x < bound->offset.x
|
|
|
|
|| top_left->y < bound->offset.y
|
|
|
|
|| within_box(&v, bound) < 0)
|
|
|
|
return CBGFX_ERROR_BOUNDARY;
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
int draw_bitmap(const void *bitmap, size_t size,
|
|
|
|
const struct scale *pos_rel, uint8_t pivot,
|
|
|
|
const struct scale *dim_rel)
|
2015-08-03 19:51:38 +02:00
|
|
|
{
|
|
|
|
struct bitmap_header_v3 header;
|
|
|
|
const struct bitmap_palette_element_v3 *palette;
|
|
|
|
const uint8_t *pixel_array;
|
2015-10-08 23:03:56 +02:00
|
|
|
struct vector top_left, dim, dim_org;
|
2015-08-26 23:47:06 +02:00
|
|
|
struct scale scale;
|
2015-08-03 19:51:38 +02:00
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/* only v3 is supported now */
|
|
|
|
rv = parse_bitmap_header_v3(bitmap, size,
|
|
|
|
&header, &palette, &pixel_array, &dim_org);
|
2015-08-03 19:51:38 +02:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/* Calculate height and width of the image */
|
|
|
|
rv = calculate_dimension(&dim_org, dim_rel, &dim);
|
2015-08-03 19:51:38 +02:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/* Calculate self scale */
|
|
|
|
scale.x.n = dim.width;
|
|
|
|
scale.x.d = dim_org.width;
|
|
|
|
scale.y.n = dim.height;
|
|
|
|
scale.y.d = dim_org.height;
|
|
|
|
|
|
|
|
/* Calculate coordinate */
|
2015-10-28 12:24:25 +01:00
|
|
|
rv = calculate_position(&dim, pos_rel, pivot, &top_left);
|
2015-10-08 23:03:56 +02:00
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
rv = check_boundary(&top_left, &dim, &canvas);
|
2015-09-28 22:14:43 +02:00
|
|
|
if (rv) {
|
2015-10-08 23:03:56 +02:00
|
|
|
LOG("Bitmap image exceeds canvas boundary\n");
|
2015-09-28 22:14:43 +02:00
|
|
|
return rv;
|
2015-08-03 19:51:38 +02:00
|
|
|
}
|
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
return draw_bitmap_v3(&top_left, &scale, &dim, &dim_org,
|
2015-08-03 19:51:38 +02:00
|
|
|
&header, palette, pixel_array);
|
|
|
|
}
|
2015-09-28 22:14:43 +02:00
|
|
|
|
|
|
|
int draw_bitmap_direct(const void *bitmap, size_t size,
|
2015-10-08 23:03:56 +02:00
|
|
|
const struct vector *top_left)
|
2015-09-28 22:14:43 +02:00
|
|
|
{
|
2015-10-08 23:03:56 +02:00
|
|
|
struct bitmap_header_v3 header;
|
|
|
|
const struct bitmap_palette_element_v3 *palette;
|
|
|
|
const uint8_t *pixel_array;
|
|
|
|
struct vector dim;
|
|
|
|
struct scale scale;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
2015-09-28 22:14:43 +02:00
|
|
|
|
2015-10-08 23:03:56 +02:00
|
|
|
/* only v3 is supported now */
|
|
|
|
rv = parse_bitmap_header_v3(bitmap, size,
|
|
|
|
&header, &palette, &pixel_array, &dim);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* Calculate self scale */
|
|
|
|
scale.x.n = 1;
|
|
|
|
scale.x.d = 1;
|
|
|
|
scale.y.n = 1;
|
|
|
|
scale.y.d = 1;
|
|
|
|
|
|
|
|
rv = check_boundary(top_left, &dim, &screen);
|
|
|
|
if (rv) {
|
|
|
|
LOG("Bitmap image exceeds screen boundary\n");
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return draw_bitmap_v3(top_left, &scale, &dim, &dim,
|
|
|
|
&header, palette, pixel_array);
|
|
|
|
}
|
2015-10-02 00:56:28 +02:00
|
|
|
|
|
|
|
int get_bitmap_dimension(const void *bitmap, size_t sz, struct scale *dim_rel)
|
|
|
|
{
|
|
|
|
struct bitmap_header_v3 header;
|
|
|
|
const struct bitmap_palette_element_v3 *palette;
|
|
|
|
const uint8_t *pixel_array;
|
|
|
|
struct vector dim, dim_org;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (cbgfx_init())
|
|
|
|
return CBGFX_ERROR_INIT;
|
|
|
|
|
|
|
|
/* Only v3 is supported now */
|
|
|
|
rv = parse_bitmap_header_v3(bitmap, sz,
|
|
|
|
&header, &palette, &pixel_array, &dim_org);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* Calculate height and width of the image */
|
|
|
|
rv = calculate_dimension(&dim_org, dim_rel, &dim);
|
|
|
|
if (rv)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
/* Calculate size relative to the canvas */
|
|
|
|
dim_rel->x.n = dim.width;
|
|
|
|
dim_rel->x.d = canvas.size.width;
|
|
|
|
dim_rel->y.n = dim.height;
|
|
|
|
dim_rel->y.d = canvas.size.height;
|
|
|
|
|
|
|
|
return CBGFX_SUCCESS;
|
|
|
|
}
|