mirror of
				https://github.com/labwc/labwc.git
				synced 2025-11-03 09:01:51 -05:00 
			
		
		
		
	
		
			
				
	
	
		
			51 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			51 lines
		
	
	
	
		
			1.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
#include <stdio.h>
 | 
						|
#include <stdlib.h>
 | 
						|
#include <string.h>
 | 
						|
#include <cairo.h>
 | 
						|
 | 
						|
#include "xbm/parse.h"
 | 
						|
#include "common/grab-file.h"
 | 
						|
 | 
						|
static float red[] = { 1.0, 0.0, 0.0, 1.0 };
 | 
						|
 | 
						|
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]);
 | 
						|
	if (!buffer)
 | 
						|
		exit(EXIT_FAILURE);
 | 
						|
	tokens = tokenize_xbm(buffer);
 | 
						|
	free(buffer);
 | 
						|
	parse_set_color(red);
 | 
						|
	struct pixmap pixmap = parse_xbm_tokens(tokens);
 | 
						|
	free(tokens);
 | 
						|
 | 
						|
	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");
 | 
						|
		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]);
 | 
						|
	if (cairo_surface_write_to_png(g_surface, png_name)) {
 | 
						|
		fprintf(stderr, "cannot save png\n");
 | 
						|
		exit(EXIT_FAILURE);
 | 
						|
	}
 | 
						|
	cairo_surface_destroy(g_surface);
 | 
						|
	exit(EXIT_SUCCESS);
 | 
						|
}
 |