#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;
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)
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);
}