labwc/src/xbm/parse.c

112 lines
2.1 KiB
C
Raw Normal View History

2021-09-24 21:45:48 +01:00
// SPDX-License-Identifier: GPL-2.0-only
2020-06-29 19:27:59 +01:00
/*
* Parse xbm token to create pixmap
*
* Copyright Johan Malm 2020
*/
2020-06-23 07:17:07 +01:00
#define _POSIX_C_SOURCE 200809L
2020-09-28 20:53:59 +01:00
#include <assert.h>
#include <stdbool.h>
#include <stdint.h>
2020-06-23 07:17:07 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common/mem.h"
#include "xbm/parse.h"
2020-06-23 07:17:07 +01:00
static uint32_t color;
static uint32_t
u32(float *rgba)
{
uint32_t r[4] = { 0 };
for (int i = 0; i < 4; i++) {
r[i] = rgba[i] * 255;
}
return ((r[3] & 0xff) << 24) | ((r[0] & 0xff) << 16) |
((r[1] & 0xff) << 8) | (r[2] & 0xff);
}
void
parse_set_color(float *rgba)
{
color = u32(rgba);
}
static void
process_bytes(struct pixmap *pixmap, struct token *tokens)
{
pixmap->data = (uint32_t *)xzalloc(
pixmap->width * pixmap->height * sizeof(uint32_t));
2020-06-23 07:17:07 +01:00
struct token *t = tokens;
2020-07-03 22:03:44 +01:00
for (int row = 0; row < pixmap->height; row++) {
2020-06-23 07:17:07 +01:00
int byte = 1;
2020-07-03 22:03:44 +01:00
for (int col = 0; col < pixmap->width; col++) {
2020-06-23 07:17:07 +01:00
if (col == byte * 8) {
++byte;
++t;
}
if (!t->type) {
2020-06-23 07:17:07 +01:00
return;
}
if (t->type != TOKEN_INT) {
return;
}
2020-06-23 07:17:07 +01:00
int bit = 1 << (col % 8);
if (t->value & bit) {
pixmap->data[row * pixmap->width + col] = color;
}
2020-06-23 07:17:07 +01:00
}
++t;
}
}
struct pixmap
parse_xbm_tokens(struct token *tokens)
2020-06-23 07:17:07 +01:00
{
2020-07-03 22:03:44 +01:00
struct pixmap pixmap = { 0 };
2020-06-23 07:17:07 +01:00
for (struct token *t = tokens; t->type; t++) {
2020-07-03 22:03:44 +01:00
if (pixmap.width && pixmap.height) {
if (t->type != TOKEN_INT) {
2020-06-23 07:17:07 +01:00
continue;
}
2020-07-03 22:03:44 +01:00
process_bytes(&pixmap, t);
goto out;
2020-06-23 07:17:07 +01:00
}
if (strstr(t->name, "width")) {
2020-07-03 22:03:44 +01:00
pixmap.width = atoi((++t)->name);
} else if (strstr(t->name, "height")) {
2020-07-03 22:03:44 +01:00
pixmap.height = atoi((++t)->name);
}
2020-06-23 07:17:07 +01:00
}
out:
2020-06-29 19:27:59 +01:00
return pixmap;
2020-06-23 07:17:07 +01:00
}
/*
* Openbox built-in icons are not bigger than 8x8, so have only written this
* function to cope wit that max size
*/
#define LABWC_BUILTIN_ICON_MAX_SIZE (8)
struct pixmap
parse_xbm_builtin(const char *button, int size)
{
struct pixmap pixmap = { 0 };
2020-09-28 20:53:59 +01:00
assert(size <= LABWC_BUILTIN_ICON_MAX_SIZE);
pixmap.width = size;
pixmap.height = size;
struct token t[LABWC_BUILTIN_ICON_MAX_SIZE + 1];
for (int i = 0; i < size; i++) {
t[i].value = button[i];
t[i].type = TOKEN_INT;
}
t[size].type = 0;
process_bytes(&pixmap, t);
return pixmap;
}