labwc/tools/xbm/xbm-parse.c

52 lines
1.2 KiB
C
Raw Normal View History

2020-06-23 07:17:07 +01:00
#include <stdio.h>
#include <stdlib.h>
2020-06-29 19:27:59 +01:00
#include <string.h>
#include <cairo.h>
2020-06-23 07:17:07 +01:00
2020-08-31 20:01:08 +01:00
#include "xbm/parse.h"
#include "common/grab-file.h"
2020-06-23 07:17:07 +01:00
2020-08-31 20:01:08 +01:00
static float red[] = { 1.0, 0.0, 0.0, 1.0 };
2020-06-23 07:17:07 +01:00
int main(int argc, char **argv)
{
struct token *tokens;
if (argc != 2) {
fprintf(stderr, "usage: %s <xbm-file>\n", argv[0]);
return 1;
}
char *buffer = grab_file(argv[1]);
2020-06-23 07:17:07 +01:00
if (!buffer)
exit(EXIT_FAILURE);
tokens = tokenize_xbm(buffer);
2020-06-23 07:17:07 +01:00
free(buffer);
2020-08-31 20:01:08 +01:00
parse_set_color(red);
struct pixmap pixmap = parse_xbm_tokens(tokens);
2020-06-23 07:17:07 +01:00
free(tokens);
2020-06-29 19:27:59 +01:00
cairo_surface_t *g_surface;
g_surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32,
pixmap.width, pixmap.height);
if (!g_surface) {
fprintf(stderr, "no surface\n");
2020-06-29 19:27:59 +01:00
exit(EXIT_FAILURE);
}
unsigned char *surface_data = cairo_image_surface_get_data(g_surface);
cairo_surface_flush(g_surface);
memcpy(surface_data, pixmap.data, pixmap.width * pixmap.height * 4);
if (pixmap.data)
free(pixmap.data);
cairo_surface_mark_dirty(g_surface);
char png_name[1024];
snprintf(png_name, sizeof(png_name), "%s.png", argv[1]);
2020-06-29 19:27:59 +01:00
if (cairo_surface_write_to_png(g_surface, png_name)) {
fprintf(stderr, "cannot save png\n");
exit(EXIT_FAILURE);
}
2020-06-29 19:27:59 +01:00
cairo_surface_destroy(g_surface);
exit(EXIT_SUCCESS);
2020-06-23 07:17:07 +01:00
}