GK SDK 源码库: XMIPCLinuxV100R005C00SPC030 (kernel/tools/open_source excluded)

This commit is contained in:
lai
2026-09-06 03:52:57 +08:00
commit b1928b41c0
21813 changed files with 4413081 additions and 0 deletions
+310
View File
@@ -0,0 +1,310 @@
/*!
*****************************************************************************
** file canvas.c
**
** version 2016-05-08 16:21 heyong -> v1.0
**
** brief the implementation of canvas operations.
**
** attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
** OMMISSIONS
**
** (C) Copyright 2016-2020 BY GOKE MICROELECTRONICS CO.,LTD
**
*****************************************************************************
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
//#include "type.h"
#include "canvas.h"
typedef struct bitmap_file_header_s {
char type[2]; // "BM" (0x4d42)
unsigned int file_size;
unsigned int reserved_zero;
unsigned int off_bits; // data area offset to the file set (unit. byte)
unsigned int info_size;
unsigned int width;
unsigned int height;
unsigned short planes; // 0 - 1
unsigned short bit_count; // 0 - 1
unsigned int compression; // 0 - 1
unsigned int size_image; // 0 - 1
unsigned int xpels_per_meter;
unsigned int ypels_per_meter;
unsigned int clr_used;
unsigned int clr_important;
} __attribute__((packed)) bitmap_file_header_t; //
static canvas_t g_canvas[MAX_CANVAS_NUMS];
canvas_t *canvas_init(int width, int height)
{
int cnt = 0;
canvas_t *canvaTemp = NULL;
/*check input parameters.*/
if ((width <= 0) || (height <= 0)) {
printf("invalid params %d-%d\n", width, height);
return NULL;
}
for (cnt = 0; cnt < MAX_CANVAS_NUMS; cnt++) {
canvaTemp = &(g_canvas[cnt]);
if ((canvaTemp->width == 0) && (canvaTemp->height == 0) && (canvaTemp->pixels == NULL)) {
canvaTemp->width = BIG_ENDIAN_ALIGNED(width, 4); // aligned to 2 pixel
canvaTemp->height = BIG_ENDIAN_ALIGNED(height, 4);
/*canva buffer.*/
canvaTemp->pixels = calloc(canvaTemp->width * canvaTemp->height * sizeof(int), 1);
if(!canvaTemp->pixels)
{
printf("canvas->pixels malloc failed!\n");
canvaTemp->width = 0;
canvaTemp->height = 0;
return NULL;
}
return canvaTemp;
}
}
printf("create canvas failed: not free canvas.\n");
return NULL;
}
int canvas_init_obj(canvas_t *canvas, int width, int height)
{
/*check input parameters.*/
if ((width <= 0) || (height <= 0)) {
printf("invalid params %d-%d\n", width, height);
return -1;
}
canvas->width = BIG_ENDIAN_ALIGNED(width, 2); // aligned to 2 pixel
canvas->height = BIG_ENDIAN_ALIGNED(height, 2);
/*canva buffer.*/
canvas->pixels = calloc(canvas->width * canvas->height * sizeof(int), 1);
return 0;
}
int canvas_deinit(canvas_t *canvas)
{
if (canvas) {
canvas->width = 0;
canvas->height = 0;
if (canvas->pixels) {
/*the canvas buffer calloc form memory, so it need to free in this API.*/
free(canvas->pixels);
canvas->pixels = NULL;
}
}
return 0;
}
int canvas_put_pixel(canvas_t *canvas, int x, int y, canvas_pixel_t pixel)
{
unsigned int *pixelAddr = NULL;
/*check input parameters.*/
if ((canvas == NULL) || ((canvas->pixels == NULL)) ||
((x >= canvas->width) && (y >= canvas->height))) {
printf("input invalid params\n");
return -1;
}
pixelAddr = (unsigned int*)canvas->pixels;
*(pixelAddr + canvas->width * y + x) = pixel.argb8888;
return 0;
}
int canvas_get_pixel(canvas_t *canvas, int x, int y, canvas_pixel_t *pixel)
{
unsigned int *pixelAddr = NULL;
unsigned int argb8888;
/*check input parameters.*/
if ((canvas == NULL) || (canvas->pixels == NULL) || (pixel == NULL) ||
((x >= canvas->width) && (y >= canvas->height))) {
printf("input invalid params.\n");
return -1;
}
pixelAddr = (unsigned int*)canvas->pixels;
argb8888 = *(pixelAddr + canvas->width * y + x);
pixel->argb8888 = argb8888;
return 0;
}
int canvas_match_pixel(canvas_t *canvas, canvas_pixel_t pixel1, canvas_pixel_t pixel2)
{
return (pixel1.argb8888 == pixel2.argb8888);
}
void canvas_clear(canvas_t *canvas)
{
int i = 0;
unsigned int *pixelAddr = (unsigned int *)canvas->pixels;
for (i = 0; i < canvas->height * canvas->width; i++) {
pixelAddr[i] = 0x00000000;// clear canvas.
}
}
void canvas_null_rect(canvas_t *canvas,canvas_pixel_t pixel2)
{
int i = 0,j = 0;
int argb8888 = pixel2.argb8888;
unsigned int *pixelAddr = (unsigned int *)canvas->pixels;
for (i = 0; i < canvas->height ; i++) {
pixelAddr = (unsigned int *)canvas->pixels + (i*canvas->width);
for(j = 0; j < canvas->width; j++)
{
if(i < 2 || j < 2 || (i > (canvas->height -3)) || (j > (canvas->width-3)))
pixelAddr[j] = argb8888;// clear canvas.
else
pixelAddr[j] = 0;// clear canvas.
}
}
}
int canvas_copy(canvas_t *canvas_from, canvas_t *canvas_to)
{
int i ;
int from_width = canvas_from->width*sizeof(int);
int to_width = canvas_to->width*sizeof(int);
char *from = canvas_from->pixels;
char *to = canvas_to->pixels;
for(i = 0 ;i < canvas_from->height; i++)
{
memcpy(to,from,from_width);
to += to_width;
from += from_width;
}
return 0;
}
canvas_t *canvas_init_by_bmp24file(const char *bmp24_path)
{
int ret = 0;
int i = 0, j = 0, bmpWidth = 0, bmpHeight = 0;
unsigned int dataSize = 0;
char *lineOffset = NULL;
FILE *bmpFd = NULL;
char *canvas_cache = NULL;
bitmap_file_header_t bmpHeader;
canvas_t *canvas = NULL;
canvas_pixel_t canvas_pixel;
/*check input parameters.*/
if(bmp24_path == NULL)
{
printf("canvas_init_by_bmp24file: input invalid parameters.\n");
return NULL;
}
printf("canvas_init_by_bmp24file: fileName: %s.\n",bmp24_path);
bmpFd = fopen(bmp24_path, "rb");
if(bmpFd == NULL)
{
printf("canvas_init_by_bmp24file: can not open fileName:%s.\n",bmp24_path);
return NULL;
}
ret = fread(&bmpHeader, 1, sizeof(bmpHeader), bmpFd);
if(sizeof(bmpHeader) != ret)
{
fclose(bmpFd);
printf("canvas_init_by_bmp24file: load fileName:%s error.\n",bmp24_path);
return NULL;
}
if((bmpHeader.type[0] == 'B') && (bmpHeader.type[1] == 'M') &&
((bmpHeader.bit_count == 24) || (bmpHeader.bit_count == 32)))
{
printf("IMAGE %dx%d size=%d offset=%d filesize=%d info=%d\n", \
bmpWidth, bmpHeight, bmpHeader.size_image, \
bmpHeader.off_bits, bmpHeader.file_size,\
bmpHeader.info_size);
bmpWidth = bmpHeader.width;
bmpHeight = bmpHeader.height;
if(bmpHeader.size_image == 0)
{
dataSize = bmpHeader.file_size - bmpHeader.off_bits;
}
else
{
dataSize = bmpHeader.size_image;
}
canvas_cache = calloc(dataSize, 1);
/*load image to buf.*/
if(0 == fseek(bmpFd, bmpHeader.off_bits, SEEK_SET))
{
ret = fread(canvas_cache, 1, dataSize, bmpFd);
}
fclose(bmpFd);
bmpFd = NULL;
/*load to canvas.*/
canvas = canvas_init(bmpWidth, bmpHeight);
for(i = 0; i < bmpHeight; ++i)
{
if(bmpHeader.bit_count == 24)
{
lineOffset = canvas_cache + BIG_ENDIAN_ALIGNED(3 *
bmpWidth, 4) * (bmpHeight - 1 - i) + 2;
for(j = 0; j < bmpWidth; ++j)
{
char *const column_offset = lineOffset + 3 * j;
canvas_pixel.alpha = 0xff;
canvas_pixel.red = column_offset[0];
canvas_pixel.green = column_offset[1];
canvas_pixel.blue = column_offset[2];
canvas_put_pixel(canvas, j, i, canvas_pixel);
}
}
else/*32*/
{
lineOffset = canvas_cache + BIG_ENDIAN_ALIGNED(4 *
bmpWidth, 4) * (bmpHeight - 1 - i) + 2;
for(j = 0; j < bmpWidth; ++j)
{
char *const column_offset = lineOffset + 4 * j;
canvas_pixel.alpha = column_offset[0];
canvas_pixel.red = column_offset[1];
canvas_pixel.green = column_offset[2];
canvas_pixel.blue = column_offset[3];
canvas_put_pixel(canvas, j, i, canvas_pixel);
}
}
}
/*free the canvas cache.*/
free(canvas_cache);
canvas_cache = NULL;
return canvas;
}
fclose(bmpFd);
printf("canvas_init_by_bmp24file: fileName: %s error.\n",bmp24_path);
return NULL;
}
+66
View File
@@ -0,0 +1,66 @@
/*!
*****************************************************************************
** file osd.h
**
** version 2016-05-08 18:43 heyong -> v1.0
**
** brief the canvas operations define.
**
** attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
** OMMISSIONS
**
** (C) Copyright 2016-2020 BY GOKE MICROELECTRONICS CO.,LTD
**
*****************************************************************************
*/
#ifndef _CANVAS_H_
#define _CANVAS_H_
#ifdef __cplusplus
extern "C" {
#endif
#define MAX_CANVAS_NUMS (3)
#define LITTLE_ENDIAN_ALIGNED(__val, __align) ( (__val) & ~((__align)-1u) )
#define BIG_ENDIAN_ALIGNED(__val, __align) LITTLE_ENDIAN_ALIGNED((__val) + (__align) - 1, (__align))
#ifndef ROUND_UP
#define ROUND_UP(x, n) ( ((x)+(n)-1u) & ~((n)-1u) )
#endif
typedef union canvas_pixel_format_s {
int argb8888;
struct {
unsigned char blue, green, red, alpha;
};
} canvas_pixel_t;
typedef struct canvas_s {
int width;
int height;
void* pixels;
int width_min, height_min, height_max;
unsigned char* image;
} canvas_t;
canvas_t *canvas_init(int width, int height);
int canvas_init_obj(canvas_t *canvas, int width, int height);
int canvas_deinit(canvas_t *canvas);
int canvas_put_pixel(canvas_t *canvas, int x, int y, canvas_pixel_t pixel);
int canvas_get_pixel(canvas_t *canvas, int x, int y, canvas_pixel_t *pixel);
int canvas_match_pixel(canvas_t *canvas, canvas_pixel_t pixel1, canvas_pixel_t pixel2);
void canvas_clear(canvas_t *canvas);
canvas_t *canvas_init_by_bmp24file(const char *bmp24_path);
#ifdef __cplusplus
};
#endif
#endif /* _CANVAS_H_ */
@@ -0,0 +1,466 @@
/*!
*****************************************************************************
** file canvas_font.c
**
** version 2015-04-25 14:58 unknow -> v1.0
** 2016-04-25 16:21 heyong -> v2.0
**
** brief the implementation of canvas_font.h.
**
** attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
** OMMISSIONS
**
** (C) Copyright 2016-2020 BY GOKE MICROELECTRONICS CO.,LTD
**
*****************************************************************************
*/
#include "canvas_font.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <strings.h>
#include <sys/types.h>
#include <sys/stat.h>
static unsigned char _def_alpha = 0;
static text_style_t _def_sytle = TEXT_STYLE_DEFAULT;
static unsigned char *_asc_16 = NULL; // bytes 128 x 16 x 1 size 8x16
static unsigned char *_asc_20 = NULL; // bytes 128 x 20 x 2 size 10x20
static unsigned char *_asc_24 = NULL; // bytes 128 x 24 x 2 size 12x24
static unsigned char *_asc_32 = NULL; // bytes 128 x 32 x 2 size 16x32
static unsigned char *_asc_40 = NULL; // bytes 128 x 40 x 3 size 20x40
static unsigned char *_asc_48 = NULL; // bytes 128 x 48 x 3 size 24x48
static unsigned char *_asc_56 = NULL; // bytes 128 x 56 x 4 size 28x56
static unsigned char* _gb2312_16 = NULL; // bytes 87 x 94 x 16 x 2 size 16x16
static unsigned char* _gb2312_20 = NULL; // bytes 87 x 94 x 20 x 3 size 20x20
static unsigned char* _gb2312_24 = NULL; // bytes 87 x 94 x 24 x 3 size 24x24
static unsigned char* _gb2312_32 = NULL; // bytes 87 x 94 x 32 x 4 size 32x32
static unsigned char* _gb2312_40 = NULL; // bytes 87 x 94 x 40 x 5 size 40x40
static unsigned char* _gb2312_48 = NULL; // bytes 87 x 94 x 48 x 6 size 48x48
static unsigned char* _gb2312_56 = NULL; // bytes 87 x 94 x 56 x 7 size 56x56
static int font_offset_byte = 0;
static int font_asc_offset_byte = 0;
static int _canvas_font_load_mem(const char* file_name, unsigned char **ret_mem)
{
struct stat file_stat = {0};
FILE* fid = NULL;
char font=0;
if(0 == stat(file_name, &file_stat)){
fid = fopen(file_name, "rb");
if(NULL != fid){
if(NULL != *ret_mem){
free(*ret_mem);
}
*ret_mem = calloc(file_stat.st_size, 1);
fread(*ret_mem, 1, file_stat.st_size, fid);
fseek(fid,0,SEEK_SET);
fread(&font, 1, 1, fid);
if(font=='M')
{
if(strstr(file_name, "hzk")!=NULL)
{
font_offset_byte = 16;
}
else
font_asc_offset_byte = 16;
}
fclose(fid);
fid = NULL;
return 0;
}
}
return -1;
}
static void _canvas_font_free_mem(unsigned char **mem)
{
if (mem && *mem) {
free(*mem);
*mem = NULL;
}
}
int canvas_font_load(font_size_e font_size, const char* asc_font, const char* gb2312_font)
{
unsigned char **asc_mem = NULL, **gb2312_mem = NULL;
switch(font_size){
case FONT_SIZE_16:
asc_mem = &_asc_16;
gb2312_mem = &_gb2312_16;
break;
case FONT_SIZE_20:
asc_mem = &_asc_20;
gb2312_mem = &_gb2312_20;
break;
case FONT_SIZE_24:
asc_mem = &_asc_24;
gb2312_mem = &_gb2312_24;
break;
case FONT_SIZE_32:
asc_mem = &_asc_32;
gb2312_mem = &_gb2312_32;
break;
case FONT_SIZE_40:
asc_mem = &_asc_40;
gb2312_mem = &_gb2312_40;
break;
case FONT_SIZE_48:
asc_mem = &_asc_48;
gb2312_mem = &_gb2312_48;
break;
case FONT_SIZE_56:
asc_mem = &_asc_56;
gb2312_mem = &_gb2312_56;
break;
default:
return -1;
}
if(0 == _canvas_font_load_mem(asc_font, asc_mem)
&& 0 == _canvas_font_load_mem(gb2312_font, gb2312_mem))
{
return 0;
}
else
{
printf("Load asc_font or gb2313_font fail\n");
}
free(*asc_mem);
free(*gb2312_mem);
return -1;
}
void canvas_font_set_alpha(unsigned char alpha)
{
_def_alpha = alpha;
}
int canvas_font_available(font_size_e font_size)
{
switch(font_size){
case FONT_SIZE_16:
return (NULL != _asc_16 && (NULL != _gb2312_16));
case FONT_SIZE_20:
return (NULL != _asc_20 && (NULL != _gb2312_20));
case FONT_SIZE_24:
return (NULL != _asc_24 && (NULL != _gb2312_24));
case FONT_SIZE_32:
return (NULL != _asc_32 && (NULL != _gb2312_32));
case FONT_SIZE_40:
return (NULL != _asc_40 && (NULL != _gb2312_40));
case FONT_SIZE_48:
return (NULL != _asc_48 && (NULL != _gb2312_48));
case FONT_SIZE_56:
return (NULL != _asc_56 && (NULL != _gb2312_56));
default: ;
}
return 0;
}
int canvas_font_init()
{
char file_path_asc[128] = {0}, file_path_hzk[128] = {0};
snprintf(file_path_asc, sizeof(file_path_asc), "%s/%s%d", CANVAS_FONT_DIR, "asc", FONT_SIZE_32);
snprintf(file_path_hzk, sizeof(file_path_hzk), "%s/%s%d", CANVAS_FONT_DIR, "hzk", FONT_SIZE_32);
canvas_font_load(FONT_SIZE_32, file_path_asc, file_path_hzk);
snprintf(file_path_asc, sizeof(file_path_asc), "%s/%s%d", CANVAS_FONT_DIR, "asc", FONT_SIZE_16);
snprintf(file_path_hzk, sizeof(file_path_hzk), "%s/%s%d", CANVAS_FONT_DIR, "hzk", FONT_SIZE_16);
canvas_font_load(FONT_SIZE_16, file_path_asc, file_path_hzk);
return 0;
}
void canvas_font_deinit()
{
_canvas_font_free_mem(&_asc_16);
_canvas_font_free_mem(&_asc_20);
_canvas_font_free_mem(&_asc_24);
_canvas_font_free_mem(&_asc_32);
_canvas_font_free_mem(&_asc_40);
_canvas_font_free_mem(&_asc_48);
_canvas_font_free_mem(&_asc_56);
_canvas_font_free_mem(&_gb2312_16);
_canvas_font_free_mem(&_gb2312_20);
_canvas_font_free_mem(&_gb2312_24);
_canvas_font_free_mem(&_gb2312_32);
_canvas_font_free_mem(&_gb2312_40);
_canvas_font_free_mem(&_gb2312_48);
_canvas_font_free_mem(&_gb2312_56);
}
int canvas_fill_text(canvas_t *canvas, int x, int y, const char *text,
font_size_e font_size, text_style_t style)
{
int i = 0, ii = 0, iii = 0;
int const x_base = x;
int const y_base = y;
unsigned char *asc = NULL;
unsigned char *gb2312 = NULL;
char *ch = (char *)text; // at the beginning of the text
unsigned int asc_width = 0;
unsigned int gb2312_width = 0;
unsigned int text_width = 0, text_height = 0;
canvas_pixel_t fg_color, bg_color, enclosed_color;
bool is_enclosed = false;
if(!canvas){
return -1;
}
switch(font_size){
case FONT_SIZE_16:
asc_width = 8;
gb2312_width = 16;
asc = _asc_16;
gb2312 = _gb2312_16;
break;
case FONT_SIZE_20:
asc_width = 10;
gb2312_width = 20;
asc = _asc_20;
gb2312 = _gb2312_20;
break;
case FONT_SIZE_24:
asc_width = 12;
gb2312_width = 24;
asc = _asc_24;
gb2312 = _gb2312_24;
break;
case FONT_SIZE_32:
asc_width = 16;
gb2312_width = 32;
asc = _asc_32;
gb2312 = _gb2312_32;
break;
case FONT_SIZE_40:
asc_width = 20;
gb2312_width = 40;
asc = _asc_40;
gb2312 = _gb2312_40;
break;
case FONT_SIZE_48:
asc_width = 24;
gb2312_width = 48;
asc = _asc_48;
gb2312 = _gb2312_48;
break;
case FONT_SIZE_56:
asc_width = 28;
gb2312_width = 56;
asc = _asc_56;
gb2312 = _gb2312_56;
break;
default:
// not support font size
return -1;
}
if(!asc || !gb2312){
return -1;
}
text_height = font_size < canvas->height ? font_size : canvas->height;
if(!style){
style = _def_sytle;
}
// check style
if(style & TEXT_STYLE_BACKGROUND_TRANSPARENT){
RGB_PIXEL_FILL(bg_color, 0, 255, 255, 0);
}else if(style & TEXT_STYLE_BACKGROUND_WHRITE){
RGB_PIXEL_FILL(bg_color, _def_alpha, 255, 255, 255);
}else if(style & TEXT_STYLE_BACKGROUND_BLACK){
RGB_PIXEL_FILL(bg_color, _def_alpha, 0, 0, 0);
}
if(style & TEXT_STYLE_FOREGROUND_TRANSPARENT){
RGB_PIXEL_FILL(fg_color, 0, 255, 0, 255);
}else if(style & TEXT_STYLE_FOREGROUND_WHRITE){
//RGB_PIXEL_FILL(fg_color, _def_alpha ^ 0x11, 255, 255, 255);
RGB_PIXEL_FILL(fg_color, 255, 255, 255, 255);
}else if(style & TEXT_STYLE_FOREGROUND_BLACK){
RGB_PIXEL_FILL(fg_color, _def_alpha ^ 0x11, 0, 0, 0);
}else if(style & TEXT_STYLE_FOREGROUND_RED){
RGB_PIXEL_FILL(fg_color, 255, 255, 0, 0);
}
if(style & TEXT_STYLE_ENCLOSED_TRANSPARENT){
is_enclosed = true;
RGB_PIXEL_FILL(enclosed_color, 0, 0, 255, 255);
}else if(style & TEXT_STYLE_ENCLOSED_WHRITE){
is_enclosed = true;
RGB_PIXEL_FILL(enclosed_color, _def_alpha ^ 0x22, 255, 255, 255);
}else if(style & TEXT_STYLE_ENCLOSED_BLACK){
is_enclosed = true;
RGB_PIXEL_FILL(enclosed_color, _def_alpha ^ 0x22, 0, 0, 0);
}
//printf("bg_color: 0x%08x, fg_color: 0x%08x\n", bg_color.argb8888, fg_color.argb8888);
// the font item stride bytes number, align 8bits / 1byte
#if 0
int convert_the_color = 0;
#endif
while('\0' != *ch)
{
#if 0
if (*ch == '[')
RGB_PIXEL_FILL(fg_color, _def_alpha ^ 0x11, 255, 0, 0);
else if (*ch == ']')
convert_the_color = 2;
convert_the_color--;
if (convert_the_color == 0)
RGB_PIXEL_FILL(fg_color, _def_alpha ^ 0x11, 255, 255, 255);
#endif
if(*ch < 0x7f)
{
// ascii
int const stride_bytes = (asc_width + 7) / 8;
int const asc_code = *ch;
unsigned long offset = 0;
if(font_asc_offset_byte)
{
unsigned long dwCharInfo = 0;
memcpy(&dwCharInfo, &asc[font_asc_offset_byte + asc_code*4], sizeof(dwCharInfo));
offset = dwCharInfo & 0x3ffffff;
}
else
offset = asc_code * font_size * stride_bytes;
// check width limited
if(x + asc_width > canvas->width){
break;
}
for(i = 0; i < font_size; ++i){
int stride_pixel = 0;
//printf("\n");
for(ii = 0; ii < stride_bytes && stride_pixel < asc_width; ++ii){
off_t const offset_byte = offset + i * stride_bytes + ii;
for(iii = 0; iii < 8 && stride_pixel < asc_width; ++iii){
unsigned char const actived_px = 0x80>>iii;//(1<<(8-1-iii));
if(asc[offset_byte] & actived_px){
//printf("1");
canvas_put_pixel(canvas, x + ii * 8 + iii, y + i, fg_color);
}else{
//printf("0");
canvas_put_pixel(canvas, x + ii * 8 + iii, y + i, bg_color);
}
++stride_pixel;
}
}
}
//printf("\n");
x += asc_width;
ch += 1;
}
else if(*ch > 0xa0)
{
int const stride_bytes = (gb2312_width + 7) / 8;
// get qu code and wei code
int const qu_code = ch[0] - 0xa0 - 1; // 87
int const wei_code = ch[1] - 0xa0 - 1; // 94
unsigned long offset = font_offset_byte+ (qu_code * 94 + wei_code) * font_size * stride_bytes;
if(6 == qu_code){
// russian
}
else
{
for(i = 0; i < font_size; ++i)
{
int stride_pixel = 0;
for(ii = 0; ii < stride_bytes && stride_pixel < gb2312_width; ++ii)
{
int const offset_byte = offset + i * stride_bytes + ii;
for(iii = 0; iii < 8 && stride_pixel < gb2312_width; ++iii)
{
unsigned char const actived_px = (0x80>>iii);
if(gb2312[offset_byte] & actived_px){
canvas_put_pixel(canvas, x + ii * 8 + iii, y + i, fg_color);
}else{
canvas_put_pixel(canvas, x + ii * 8 + iii, y + i, bg_color);
}
++stride_pixel;
}
}
}
}
x += gb2312_width;
ch += 2;
}
}
text_width = x - x_base;
if(is_enclosed){
for(i = 0; i < text_height - 1 ; ++i){
for(ii = 0; ii < text_width - 1 ; ++ii){
canvas_pixel_t center_color;
canvas_get_pixel(canvas, x_base + ii, y_base + i, &center_color);
if(0 == i || text_height - 1 == i
|| 0 == ii || text_width - 1 == ii){
// on the edage
if(canvas_match_pixel(canvas, fg_color, center_color)){
canvas_put_pixel(canvas, x_base + ii, y_base + i, enclosed_color);
}
}else{
canvas_pixel_t right_color, bottom_color;
canvas_get_pixel(canvas, x_base + ii + 1, y_base + i, &right_color); // to the right
canvas_get_pixel(canvas, x_base + ii, y_base + i + 1, &bottom_color); // to the bottom
if(canvas_match_pixel(canvas, bg_color, center_color)){
// background
if(canvas_match_pixel(canvas, fg_color, right_color)
|| canvas_match_pixel(canvas, fg_color, bottom_color)){
canvas_put_pixel(canvas, x_base + ii, y_base + i, enclosed_color);
}
}else if(canvas_match_pixel(canvas, fg_color, center_color)){
// forground
if(canvas_match_pixel(canvas, bg_color, right_color)){
canvas_put_pixel(canvas, x_base + ii + 1, y_base + i, enclosed_color);
}
if(canvas_match_pixel(canvas, bg_color, bottom_color)){
canvas_put_pixel(canvas, x_base + ii, y_base + i + 1, enclosed_color);
}
}
}
}
}
}
return x - x_base; // write put size
}
@@ -0,0 +1,101 @@
/*!
*****************************************************************************
** file canvas_font.h
**
** version 2015-04-25 14:58 unknow -> v1.0
** 2016-04-25 16:21 heyong -> v2.0
**
** brief the font operations api define.
**
** attention THIS SAMPLE CODE IS PROVIDED AS IS. GOKE MICROELECTRONICS
** ACCEPTS NO RESPONSIBILITY OR LIABILITY FOR ANY ERRORS OR
** OMMISSIONS
**
** (C) Copyright 2016-2020 BY GOKE MICROELECTRONICS CO.,LTD
**
*****************************************************************************
*/
#ifndef _OSD_FONT_H_
#define _OSD_FONT_H_
#ifdef __cplusplus
extern "C" {
#endif
#include "canvas.h"
#define CANVAS_FONT_DIR "./common/font/fnt"
typedef enum font_size_ {
FONT_SIZE_16 = (16),
FONT_SIZE_20 = (20),
FONT_SIZE_24 = (24),
FONT_SIZE_32 = (32),
FONT_SIZE_40 = (40),
FONT_SIZE_48 = (48),
FONT_SIZE_56 = (56),
FONT_SIZE_64 = (64),
}font_size_e;
#define RGB_PIXEL_FILL(_pixel, _a, _r, _g, _b) \
do{\
_pixel.alpha = (_a), _pixel.red = (_r), _pixel.green = (_g), _pixel.blue = (_b);\
}while(0)
typedef unsigned int text_style_t;
#define TEXT_STYLE_FOREGROUND_TRANSPARENT (1<<0)
#define TEXT_STYLE_FOREGROUND_WHRITE (1<<1)
#define TEXT_STYLE_FOREGROUND_BLACK (1<<2)
#define TEXT_STYLE_FOREGROUND_RED (1<<3)
#define TEXT_STYLE_BACKGROUND_TRANSPARENT (1<<4)
#define TEXT_STYLE_BACKGROUND_WHRITE (1<<5)
#define TEXT_STYLE_BACKGROUND_BLACK (1<<6)
#define TEXT_STYLE_ENCLOSED_TRANSPARENT (1<<8)
#define TEXT_STYLE_ENCLOSED_WHRITE (1<<9)
#define TEXT_STYLE_ENCLOSED_BLACK (1<<10)
#define TEXT_STYLE_ENCLOSED_ENABLE (1<<12)
#define TEXT_STYLE_FOREGROUND_BIT_AREA 0x0007
#define TEXT_STYLE_BACKGROUND_BIT_AREA 0x0070
#define TEXT_STYLE_ENCLOSED_BIT_AREA 0x0700
#define TEXT_STYLE_DEFAULT \
(TEXT_STYLE_FOREGROUND_WHRITE\
|TEXT_STYLE_BACKGROUND_TRANSPARENT\
|TEXT_STYLE_ENCLOSED_BLACK)
void canvas_font_set_alpha(unsigned char alpha);
int canvas_font_available(font_size_e font_size);
int canvas_font_load(font_size_e font_size, const char* asc_font, const char* gb2312_font);
int canvas_font_init();
void canvas_font_deinit();
/*!
*******************************************************************************
** brief load font to canvas.
**
** param[in] canvas the point of dest canvas.
** param[in] x canvas x start address.
** param[in] y canvas y start address.
** param[in] font_size the current font size.
** param[in] text the point of text.
** param[in] style font style.
**
** return
** - #>=0 , successed, and return the write put size.
** - #<0 , failed.
**
*******************************************************************************
*/
int canvas_fill_text(canvas_t *canvas, int x, int y, const char *text,
font_size_e font_size, text_style_t style);
int canvas_get_fill_text_width(canvas_t *canvas, const char *text, int font_size);
#ifdef __cplusplus
};
#endif
#endif /* _OSD_FONT_H_ */