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
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
2020-06-26 21:52:38 +01:00
|
|
|
#include <stdint.h>
|
|
|
|
|
#include <stdbool.h>
|
2020-06-23 07:17:07 +01:00
|
|
|
|
|
|
|
|
#include "buf.h"
|
2020-06-29 19:27:59 +01:00
|
|
|
#include "theme/xbm/parse.h"
|
2020-06-23 07:17:07 +01:00
|
|
|
|
2020-06-29 19:27:59 +01:00
|
|
|
/* TODO: should be window.active.button.unpressed.image.color */
|
|
|
|
|
static unsigned char defaultcolor[] = { 255, 255, 255, 255 };
|
2020-06-26 21:52:38 +01:00
|
|
|
|
2020-07-03 22:03:44 +01:00
|
|
|
static uint32_t u32(unsigned char *rbga)
|
2020-06-26 21:52:38 +01:00
|
|
|
{
|
2020-07-03 22:03:44 +01:00
|
|
|
return (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) | rbga[0];
|
2020-06-26 21:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
2020-07-03 22:03:44 +01:00
|
|
|
static void process_bytes(struct pixmap *pixmap, struct token *tokens)
|
2020-06-26 21:52:38 +01:00
|
|
|
{
|
2020-07-03 22:03:44 +01:00
|
|
|
pixmap->data = (uint32_t *)calloc(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)
|
|
|
|
|
return;
|
|
|
|
|
int value = (int)strtol(t->name, NULL, 0);
|
|
|
|
|
int bit = 1 << (col % 8);
|
2020-06-29 19:27:59 +01:00
|
|
|
if (value & bit)
|
2020-07-03 22:03:44 +01:00
|
|
|
pixmap->data[row * pixmap->width + col] = u32(defaultcolor);
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
|
|
|
|
++t;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-29 19:27:59 +01:00
|
|
|
struct pixmap xbm_create_pixmap(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) {
|
2020-06-23 07:17:07 +01:00
|
|
|
if (t->type != TOKEN_INT)
|
|
|
|
|
continue;
|
2020-07-03 22:03:44 +01:00
|
|
|
process_bytes(&pixmap, t);
|
2020-06-26 21:52:38 +01:00
|
|
|
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);
|
2020-06-23 07:17:07 +01:00
|
|
|
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
|
|
|
}
|
2020-06-26 21:52:38 +01:00
|
|
|
out:
|
2020-06-29 19:27:59 +01:00
|
|
|
return pixmap;
|
2020-06-23 07:17:07 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char *xbm_read_file(const char *filename)
|
|
|
|
|
{
|
|
|
|
|
char *line = NULL;
|
|
|
|
|
size_t len = 0;
|
|
|
|
|
FILE *stream = fopen(filename, "r");
|
|
|
|
|
if (!stream) {
|
|
|
|
|
fprintf(stderr, "warn: cannot read '%s'\n", filename);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
struct buf buffer;
|
|
|
|
|
buf_init(&buffer);
|
|
|
|
|
while ((getline(&line, &len, stream) != -1)) {
|
|
|
|
|
char *p = strrchr(line, '\n');
|
|
|
|
|
if (p)
|
|
|
|
|
*p = '\0';
|
|
|
|
|
buf_add(&buffer, line);
|
|
|
|
|
}
|
|
|
|
|
free(line);
|
|
|
|
|
fclose(stream);
|
|
|
|
|
return (buffer.buf);
|
|
|
|
|
}
|