Add support for .xbm bitmap file

.xbm is easy to include in C as source file. It could be viewed
and edited directly without need to be converted between images
and C array.
This commit is contained in:
Dien-Nhung Nguyen-Phu
2024-06-09 12:59:34 +07:00
parent f6d84a1ead
commit 8815700322
8 changed files with 175 additions and 0 deletions

20
src/res/bluetooth.xbm Normal file
View File

@@ -0,0 +1,20 @@
#define bluetooth_width 24
#define bluetooth_height 66
static unsigned char bluetooth_bits[] = {
0x04, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x14, 0x00, 0x00, 0x25, 0x00, 0x00,
0x16, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x16, 0x00, 0x00, 0x25, 0x00, 0x00,
0x14, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x14, 0x00, 0x00, 0x25, 0x00, 0x00, 0x16, 0x01, 0x00,
0x0c, 0x02, 0x00, 0x16, 0x01, 0x00, 0x25, 0x00, 0x00, 0x14, 0x00, 0x00,
0x0c, 0x00, 0x00, 0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x0c, 0x00, 0x00,
0x14, 0x00, 0x00, 0x25, 0x08, 0x00, 0x16, 0x11, 0x00, 0x0c, 0x12, 0x00,
0x16, 0x11, 0x00, 0x25, 0x08, 0x00, 0x14, 0x00, 0x00, 0x0c, 0x00, 0x00,
0x04, 0x00, 0x00, 0x04, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x14, 0x40, 0x00,
0x25, 0x88, 0x00, 0x16, 0x11, 0x01, 0x0c, 0x12, 0x01, 0x16, 0x11, 0x01,
0x25, 0x88, 0x00, 0x14, 0x40, 0x00, 0x0c, 0x00, 0x00, 0x04, 0x00, 0x00,
0x04, 0x00, 0x00, 0x0c, 0x00, 0x02, 0x14, 0x40, 0x04, 0x25, 0x88, 0x08,
0x16, 0x11, 0x09, 0x0c, 0x12, 0x09, 0x16, 0x11, 0x09, 0x25, 0x88, 0x08,
0x14, 0x40, 0x04, 0x0c, 0x00, 0x02, 0x04, 0x00, 0x00, 0x04, 0x00, 0x10,
0x0c, 0x00, 0x22, 0x14, 0x40, 0x44, 0x25, 0x88, 0x88, 0x16, 0x11, 0x89,
0x0c, 0x12, 0x89, 0x16, 0x11, 0x89, 0x25, 0x88, 0x88, 0x14, 0x40, 0x44,
0x0c, 0x00, 0x22, 0x04, 0x00, 0x10 };

14
src/res/foss-asia-1.xbm Normal file
View File

@@ -0,0 +1,14 @@
#define foss_asia_1_width 44
#define foss_asia_1_height 22
static unsigned char foss_asia_1_bits[] = {
0xf8, 0xf3, 0x1f, 0xff, 0xf9, 0x0f, 0xfc, 0xfb, 0xbf, 0xff, 0xfd, 0x0f,
0x0c, 0x18, 0xb0, 0x01, 0x0c, 0x00, 0x0c, 0x18, 0xb0, 0x01, 0x0c, 0x00,
0xff, 0x1b, 0xb0, 0xff, 0xfc, 0x07, 0xff, 0x1b, 0x30, 0xff, 0xf9, 0x0f,
0x0c, 0x18, 0x30, 0x80, 0x01, 0x0c, 0x0c, 0x18, 0x30, 0x80, 0x01, 0x0c,
0x0c, 0xf8, 0xbf, 0xff, 0xfd, 0x0f, 0x0c, 0xf0, 0x9f, 0xff, 0xfc, 0x07,
0x0c, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x00, 0x00,
0xff, 0x81, 0xff, 0x00, 0xfc, 0x07, 0xff, 0xc3, 0xff, 0x30, 0xfc, 0x0f,
0x00, 0xc3, 0x00, 0x30, 0x00, 0x0c, 0x00, 0xc3, 0x00, 0x30, 0x00, 0x0c,
0xfe, 0xc3, 0x7f, 0x30, 0xf8, 0x0f, 0xff, 0x83, 0xff, 0x30, 0xfc, 0x0f,
0x03, 0x03, 0xc0, 0x30, 0x0c, 0x0c, 0x03, 0x03, 0xc0, 0x30, 0x0c, 0x0c,
0xff, 0xc3, 0xff, 0x30, 0xfc, 0x0f, 0xfe, 0xc3, 0x7f, 0x30, 0xf8, 0x0f };

14
src/res/foss-asia-2.xbm Normal file
View File

@@ -0,0 +1,14 @@
#define foss_asia_2_width 44
#define foss_asia_2_height 22
static unsigned char foss_asia_2_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xf9, 0x8f, 0xff, 0xfc, 0x07,
0xfe, 0xfd, 0xdf, 0xff, 0xfe, 0x07, 0x06, 0x0c, 0xd8, 0x00, 0x06, 0x00,
0xff, 0x0d, 0xd8, 0x7f, 0xfe, 0x03, 0xff, 0x0d, 0x98, 0xff, 0xfc, 0x07,
0x06, 0x0c, 0x18, 0xc0, 0x00, 0x06, 0x06, 0xfc, 0xdf, 0xff, 0xfe, 0x07,
0x06, 0xf8, 0xcf, 0x7f, 0xfe, 0x03, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00,
0x06, 0x00, 0x00, 0x18, 0x00, 0x00, 0x06, 0x00, 0x00, 0x18, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x87, 0xff, 0x99, 0xff, 0x01,
0xfe, 0xcf, 0xff, 0x99, 0xff, 0x03, 0x00, 0xcc, 0x00, 0x18, 0x00, 0x03,
0xfc, 0xcf, 0xff, 0x18, 0xff, 0x03, 0xfe, 0x8f, 0xff, 0x99, 0xff, 0x03,
0x06, 0x0c, 0x80, 0x99, 0x01, 0x03, 0xfe, 0xcf, 0xff, 0x99, 0xff, 0x03,
0xfc, 0xcf, 0xff, 0x18, 0xff, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };

14
src/res/fossasia.xbm Normal file
View File

@@ -0,0 +1,14 @@
#define fossasia_width 44
#define fossasia_height 22
static unsigned char fossasia_bits[] = {
0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x9c, 0xc7, 0xf3, 0x1c, 0x2f, 0x07, 0x42, 0x28, 0x08, 0xa0, 0x20, 0x08,
0x42, 0x28, 0x08, 0xa0, 0x20, 0x08, 0x5f, 0xc8, 0x71, 0x3c, 0x27, 0x0f,
0x42, 0x08, 0x82, 0x22, 0xa8, 0x08, 0x42, 0x08, 0x82, 0x22, 0xa8, 0x08,
0x82, 0xe7, 0x79, 0xbc, 0x27, 0x0f, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00,
0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xdf, 0x0f,
0xff, 0xff, 0xff, 0xff, 0xff, 0x0f, 0x63, 0x38, 0x0c, 0xe3, 0xd0, 0x08,
0xbd, 0xd7, 0xf7, 0x5f, 0xdf, 0x07, 0xbd, 0xd7, 0xf7, 0x5f, 0xdf, 0x07,
0xa0, 0x37, 0x8e, 0xc3, 0xd8, 0x00, 0xbd, 0xf7, 0x7d, 0xdd, 0x57, 0x07,
0xbd, 0xf7, 0x7d, 0xdd, 0x57, 0x07, 0x7d, 0x18, 0x86, 0x43, 0xd8, 0x00,
0xfd, 0xff, 0xff, 0xff, 0xff, 0x0f, 0xfd, 0xff, 0xff, 0xff, 0xff, 0x0f };

18
src/resource.c Normal file
View File

@@ -0,0 +1,18 @@
#include "resource.h"
#include "res/bluetooth.xbm"
#include "res/foss-asia-2.xbm"
xbm_t bluetooth = {
.bits = bluetooth_bits,
.w = bluetooth_width,
.h = bluetooth_height,
.fh = 11
};
xbm_t splash = {
.bits = foss_asia_2_bits,
.w = foss_asia_2_width,
.h = foss_asia_2_height,
.fh = 11
};

10
src/resource.h Normal file
View File

@@ -0,0 +1,10 @@
#ifndef __RES_H__
#define __RES_H__
#include <stdint.h>
#include "xbm.h"
extern xbm_t bluetooth;
extern xbm_t splash;
#endif /* __RES_H__ */

67
src/xbm.c Normal file
View File

@@ -0,0 +1,67 @@
#include "xbm.h"
#include <stdlib.h>
#include <memory.h>
#define ALIGNBIT(x) (((x % 8)>0)*8 + (x / 8) * 8)
/**
* Draw bitmap file to fb at (col, row)
* if bitmap file larger than fb, it will be stripped out
*/
void xbm2fb(xbm_t *xbm, uint16_t *fb, int col, int row)
{
int W = ALIGNBIT(xbm->w);
uint16_t *tmpfb = malloc(W * sizeof(uint16_t));
memset(tmpfb, 0, W * sizeof(uint16_t));
if ((xbm->h + row) >= 0 && col >= 0) {
for (int h = 0; h < xbm->h; h++) {
for (int w = 0; w < W; w++) {
tmpfb[w] |= ((xbm->bits[h*(W/8) + w/8] >> (w % 8)) & 0x01)
<< (h+row);
}
}
}
for (int i=0; i < xbm->w; i++) {
fb[col+i] = tmpfb[i];
}
}
void xbm2fb_dirty(xbm_t *xbm, uint16_t *fb, int col, int row)
{
// Byte align for xbm bytes
int W = ALIGNBIT(xbm->w);
for (int h = 0; h < xbm->h; h++) {
for (int w = 0; w < W; w++) {
fb[w] |= ((xbm->bits[h*(W/8) + w/8] >> (w % 8)) & 0x01) << (h+row);
}
}
}
/**
* Crop image horizontaly
* TODO: vertical crop
*
*/
xbm_t *xbm_croph(xbm_t *xbm, xbm_t *frame, int from_row, int to_row)
{
int rb = xbm->w / 8 + ((xbm->w % 8)>0); // Number of bytes in a row
if (to_row * rb > rb * xbm->h)
return NULL;
frame->bits = &xbm->bits[rb * from_row];
frame->w = xbm->w;
frame->h = to_row - from_row;
frame->fh = xbm->fh;
return frame;
}
xbm_t *extract_frame(xbm_t *xbm, xbm_t *frame, int frame_index)
{
int from = frame_index * xbm->fh;
int to = from + xbm->fh;
return xbm_croph(xbm, frame, from, to);
}

18
src/xbm.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef __XBM_H__
#define __XBM_H__
#include <stdint.h>
typedef struct {
uint8_t *bits;
int w; // Width
int h; // Height
int fh; // Frame height
} xbm_t;
void xbm2fb(xbm_t *xbm, uint16_t *fb, int col, int row);
void xbm2fb_dirty(xbm_t *xbm, uint16_t *fb, int col, int row);
xbm_t *xbm_croph(xbm_t *xbm, xbm_t *frame, int from_row, int to_row);
xbm_t *extract_frame(xbm_t *xbm, xbm_t *frame, int frame_index);
#endif /* __XBM_H__ */