]> git.mdlowis.com Git - proto/labwc.git/commitdiff
src/theme/xbm/parse.c: generate cairo_surface
authorJohan Malm <jgm323@gmail.com>
Fri, 26 Jun 2020 20:52:38 +0000 (21:52 +0100)
committerJohan Malm <jgm323@gmail.com>
Fri, 26 Jun 2020 20:52:38 +0000 (21:52 +0100)
include/xbm.h
src/theme/xbm/parse.c
tools/xbm/xbm-parse.c

index 127545daa5b243d7aa14e3f81286ab2cb98d36e1..370f87b605d55afc935e3568e1b4400d6c678e39 100644 (file)
@@ -1,6 +1,8 @@
 #ifndef XBM_H
 #define XBM_H
 
+#include <cairo.h>
+
 enum token_type {
        TOKEN_NONE = 0,
        TOKEN_IDENT,
@@ -20,7 +22,7 @@ struct token {
  * xbm_create_bitmap - parse xbm tokens and create pixmap
  * @tokens: token vector
  */
-void xbm_create_bitmap(struct token *tokens);
+cairo_surface_t *xbm_create_bitmap(struct token *tokens);
 
 /**
  * tokenize - tokenize xbm file
index 6951f256283dc9753bf1f8aba1c20ceaf3856813..0d5820e5b23c3a81a5b57f49296f2f3f23167b44 100644 (file)
@@ -2,12 +2,35 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <stdint.h>
+#include <stdbool.h>
 
 #include "buf.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)
 {
+       init_pixmap(width, height);
        struct token *t = tokens;
        for (int row = 0; row < height; row++) {
                int byte = 1;
@@ -20,31 +43,49 @@ static void process_bytes(int height, int width, struct token *tokens)
                                return;
                        int value = (int)strtol(t->name, NULL, 0);
                        int bit = 1 << (col % 8);
-                       if (value & bit)
+                       if (value & bit) {
+                               add_pixel(row * width + col, defaultcolor);
                                printf(".");
-                       else
+                       } else {
+                               add_pixel(row * width + col, background);
                                printf(" ");
+                       }
                }
                ++t;
                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;
        for (struct token *t = tokens; t->type; t++) {
                if (width && height) {
                        if (t->type != TOKEN_INT)
                                continue;
                        process_bytes(width, height, t);
-                       return;
+                       goto out;
                }
                if (strstr(t->name, "width"))
                        width = atoi((++t)->name);
                else if (strstr(t->name, "height"))
                        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)
index fc9b52d802c52d7a9af52c750c948aca7ff21264..a3fcc162dda9f3fcb40e9b68c75e3881ca84c465 100644 (file)
@@ -18,6 +18,17 @@ int main(int argc, char **argv)
        tokens = xbm_tokenize(buffer);
        free(buffer);
 
-       xbm_create_bitmap(tokens);
+       cairo_surface_t *surface = xbm_create_bitmap(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);
 }