mirror of
https://github.com/labwc/labwc.git
synced 2025-10-31 22:25:34 -04:00
src/theme/xbm/parse.c: generate cairo_surface
This commit is contained in:
parent
f86394a997
commit
40e862f3ac
3 changed files with 60 additions and 6 deletions
|
|
@ -1,6 +1,8 @@
|
||||||
#ifndef XBM_H
|
#ifndef XBM_H
|
||||||
#define XBM_H
|
#define XBM_H
|
||||||
|
|
||||||
|
#include <cairo.h>
|
||||||
|
|
||||||
enum token_type {
|
enum token_type {
|
||||||
TOKEN_NONE = 0,
|
TOKEN_NONE = 0,
|
||||||
TOKEN_IDENT,
|
TOKEN_IDENT,
|
||||||
|
|
@ -20,7 +22,7 @@ struct token {
|
||||||
* xbm_create_bitmap - parse xbm tokens and create pixmap
|
* xbm_create_bitmap - parse xbm tokens and create pixmap
|
||||||
* @tokens: token vector
|
* @tokens: token vector
|
||||||
*/
|
*/
|
||||||
void xbm_create_bitmap(struct token *tokens);
|
cairo_surface_t *xbm_create_bitmap(struct token *tokens);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* tokenize - tokenize xbm file
|
* tokenize - tokenize xbm file
|
||||||
|
|
|
||||||
|
|
@ -2,12 +2,35 @@
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
|
||||||
#include "buf.h"
|
#include "buf.h"
|
||||||
#include "xbm.h"
|
#include "xbm.h"
|
||||||
|
|
||||||
|
static unsigned char defaultcolor[] = { 127, 127, 127, 255 };
|
||||||
|
static unsigned char background[] = { 255, 255, 255, 255 };
|
||||||
|
|
||||||
|
static uint32_t *pixmap;
|
||||||
|
|
||||||
|
static void add_pixel(int position, unsigned char *rbga)
|
||||||
|
{
|
||||||
|
pixmap[position] = (rbga[3] << 24) | (rbga[0] << 16) | (rbga[1] << 8) |
|
||||||
|
rbga[0];
|
||||||
|
}
|
||||||
|
|
||||||
|
static void init_pixmap(int w, int h)
|
||||||
|
{
|
||||||
|
static bool has_run;
|
||||||
|
if (has_run)
|
||||||
|
return;
|
||||||
|
has_run = true;
|
||||||
|
pixmap = (uint32_t *)calloc(w * h, sizeof(uint32_t));
|
||||||
|
}
|
||||||
|
|
||||||
static void process_bytes(int height, int width, struct token *tokens)
|
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 < height; row++) {
|
||||||
int byte = 1;
|
int byte = 1;
|
||||||
|
|
@ -20,31 +43,49 @@ static void process_bytes(int height, int width, struct token *tokens)
|
||||||
return;
|
return;
|
||||||
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);
|
||||||
printf(".");
|
printf(".");
|
||||||
else
|
} else {
|
||||||
|
add_pixel(row * width + col, background);
|
||||||
printf(" ");
|
printf(" ");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
++t;
|
++t;
|
||||||
printf("\n");
|
printf("\n");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void xbm_create_bitmap(struct token *tokens)
|
cairo_surface_t *xbm_create_bitmap(struct token *tokens)
|
||||||
{
|
{
|
||||||
|
cairo_surface_t *g_surface;
|
||||||
int width = 0, height = 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 (width && height) {
|
||||||
if (t->type != TOKEN_INT)
|
if (t->type != TOKEN_INT)
|
||||||
continue;
|
continue;
|
||||||
process_bytes(width, height, t);
|
process_bytes(width, height, t);
|
||||||
return;
|
goto out;
|
||||||
}
|
}
|
||||||
if (strstr(t->name, "width"))
|
if (strstr(t->name, "width"))
|
||||||
width = atoi((++t)->name);
|
width = atoi((++t)->name);
|
||||||
else if (strstr(t->name, "height"))
|
else if (strstr(t->name, "height"))
|
||||||
height = atoi((++t)->name);
|
height = atoi((++t)->name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
out:
|
||||||
|
g_surface =
|
||||||
|
cairo_image_surface_create(CAIRO_FORMAT_ARGB32, width, height);
|
||||||
|
if (!g_surface) {
|
||||||
|
fprintf(stderr, "no surface\n");
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
unsigned char *surface_data = cairo_image_surface_get_data(g_surface);
|
||||||
|
cairo_surface_flush(g_surface);
|
||||||
|
memcpy(surface_data, pixmap, width * height * 4);
|
||||||
|
free(pixmap);
|
||||||
|
cairo_surface_mark_dirty(g_surface);
|
||||||
|
return g_surface;
|
||||||
}
|
}
|
||||||
|
|
||||||
char *xbm_read_file(const char *filename)
|
char *xbm_read_file(const char *filename)
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,17 @@ int main(int argc, char **argv)
|
||||||
tokens = xbm_tokenize(buffer);
|
tokens = xbm_tokenize(buffer);
|
||||||
free(buffer);
|
free(buffer);
|
||||||
|
|
||||||
xbm_create_bitmap(tokens);
|
cairo_surface_t *surface = xbm_create_bitmap(tokens);
|
||||||
free(tokens);
|
free(tokens);
|
||||||
|
|
||||||
|
if (!surface)
|
||||||
|
fprintf(stderr, "no surface\n");
|
||||||
|
char png_name[1024];
|
||||||
|
snprintf(png_name, sizeof(png_name), "%s.png", argv[1]);
|
||||||
|
if (cairo_surface_write_to_png(surface, png_name)) {
|
||||||
|
fprintf(stderr, "cannot save png\n");
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
cairo_surface_destroy(surface);
|
||||||
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue