src/theme/xbm/parse.c: refactor

This commit is contained in:
Johan Malm 2020-07-03 22:03:44 +01:00
parent 10f43ca167
commit e1b86555f4

View file

@ -17,30 +17,19 @@
/* TODO: should be window.active.button.unpressed.image.color */ /* TODO: should be window.active.button.unpressed.image.color */
static unsigned char defaultcolor[] = { 255, 255, 255, 255 }; static unsigned char defaultcolor[] = { 255, 255, 255, 255 };
static uint32_t *data; static uint32_t u32(unsigned char *rbga)
static void add_pixel(int position, unsigned char *rbga)
{ {
data[position] = (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) | return (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) | rbga[0];
rbga[0];
} }
static void init_pixmap(int w, int h) static void process_bytes(struct pixmap *pixmap, struct token *tokens)
{ {
static bool has_run; pixmap->data = (uint32_t *)calloc(pixmap->width * pixmap->height,
if (has_run) sizeof(uint32_t));
return;
has_run = true;
data = (uint32_t *)calloc(w * h, sizeof(uint32_t));
}
static void process_bytes(int height, int width, struct token *tokens)
{
init_pixmap(width, height);
struct token *t = tokens; struct token *t = tokens;
for (int row = 0; row < height; row++) { for (int row = 0; row < pixmap->height; row++) {
int byte = 1; int byte = 1;
for (int col = 0; col < width; col++) { for (int col = 0; col < pixmap->width; col++) {
if (col == byte * 8) { if (col == byte * 8) {
++byte; ++byte;
++t; ++t;
@ -50,7 +39,7 @@ static void process_bytes(int height, int width, struct token *tokens)
int value = (int)strtol(t->name, NULL, 0); int value = (int)strtol(t->name, NULL, 0);
int bit = 1 << (col % 8); int bit = 1 << (col % 8);
if (value & bit) if (value & bit)
add_pixel(row * width + col, defaultcolor); pixmap->data[row * pixmap->width + col] = u32(defaultcolor);
} }
++t; ++t;
} }
@ -58,24 +47,21 @@ static void process_bytes(int height, int width, struct token *tokens)
struct pixmap xbm_create_pixmap(struct token *tokens) struct pixmap xbm_create_pixmap(struct token *tokens)
{ {
struct pixmap pixmap; struct pixmap pixmap = { 0 };
int width = 0, height = 0;
for (struct token *t = tokens; t->type; t++) { for (struct token *t = tokens; t->type; t++) {
if (width && height) { if (pixmap.width && pixmap.height) {
if (t->type != TOKEN_INT) if (t->type != TOKEN_INT)
continue; continue;
process_bytes(width, height, t); process_bytes(&pixmap, t);
goto out; goto out;
} }
if (strstr(t->name, "width")) if (strstr(t->name, "width"))
width = atoi((++t)->name); pixmap.width = atoi((++t)->name);
else if (strstr(t->name, "height")) else if (strstr(t->name, "height"))
height = atoi((++t)->name); pixmap.height = atoi((++t)->name);
} }
out: out:
pixmap.data = data;
pixmap.width = width;
pixmap.height = height;
return pixmap; return pixmap;
} }