File: wbmp2xpm.c

package info (click to toggle)
wap-wml-tools 0.0.4-5
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k, lenny
  • size: 304 kB
  • ctags: 234
  • sloc: ansic: 1,122; sh: 112; makefile: 104
file content (77 lines) | stat: -rw-r--r-- 1,581 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*
   wml-tools 

   Copyright (C) 1999 Thomas Neill (tneill@pwot.co.uk)

   This file is part of the wml-tools package and it's usage is subject
   to the terms and conditions as given in the license. See the file
   LICENSE in the root directory of the distribution for details.
*/
#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
	FILE *in, *out;
	int c, i, j, width, height, line, block, offset;
	char of[256], *p;

	if(argc != 2) {
		fprintf(stderr, "Usage: %s <file.wbmp>\n", argv[0]);
		exit(1);
	}

	if(!(in = fopen(argv[1], "r"))) {
		fprintf(stderr, "Can't open %s for reading\n", argv[1]);
		exit(1);
	}

	strcpy(of, argv[1]);
	if((p = strrchr(of, '.'))) {
		p++;
		*(p++) = 'x';
		*(p++) = 'p';
		*(p++) = 'm';
		*p = '\0';
	} else
		strcat(of, ".xpm");

	if(!(out = fopen(of, "w"))) {
		fprintf(stderr, "Can't open %s for writing\n", of);
		exit(1);
	}

	i = getc(in);
	j = getc(in);
	width = getc(in);
	height = getc(in);

	fprintf(out, "/* XPM */\nstatic char *xpm[] = {\n");
	fprintf(out, "\"%d %d 2 1\",\n", width, height);
	fprintf(out, "\"a c #000000\",\n");
	fprintf(out, "\"b c #ffffff\",\n");

	for(line = 0; line < height; line++) {
		fputc('"', out);
		for(block = 0; block < width; block += 8) {
			c = getc(in);
			for(offset = 0; (offset<8)&&((offset+block)<width); offset++) {
				if((c & (1 << (7 - offset))))
					fputc('b', out);
				else
					fputc('a', out);
			}
		}
		fputc('"', out);
		if(line != (height - 1))
			fputc(',', out);
		fputc('\n', out);
	}

	fprintf(out, "};\n");

	fclose(out);
	fclose(in);

	return 0;
}