-
Notifications
You must be signed in to change notification settings - Fork 24
/
pngtorgba.c
43 lines (39 loc) · 1.11 KB
/
pngtorgba.c
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
#include <stdio.h>
#include <sys/param.h>
main(int argc, char ** argv) {
int i;
FILE * rgb, * alpha;
int width, height, awidth, aheight;
static char buf[MAXPATHLEN];
if (argc != 2 || *argv[1] == '-') {
fprintf(stderr, "Usage: pngtorgba file.png\n");
exit(1);
}
sprintf(buf, "pngtopnm %s | pnmnoraw", argv[1]);
rgb = popen(buf, "r");
sprintf(buf, "pngtopnm -alpha %s | pnmnoraw", argv[1]);
alpha = popen(buf, "r");
if (!rgb || !alpha) {
fprintf(stderr, "PBMplus package is required\n");
exit(1);
}
if (2 != fscanf(rgb, "P3 %d %d %*d", &width, &height)) {
fprintf(stderr, "Bad RGB stream\n");
exit(1);
}
if (2 != fscanf(alpha, "P2 %d %d %*d", &awidth, &aheight) ||
awidth != width || aheight != height) {
fprintf(stderr, "Bad alpha stream\n");
exit(1);
}
printf("unsigned bk_icon_width = %d,\n\tbk_icon_height = %d,\n\tbk_icon[] = {\n", width, height);
for (i =0; i < width*height; i++) {
int r, g, b, a;
fscanf(rgb, "%d %d %d", &r, &g, &b);
fscanf(alpha, "%d", &a);
printf("%#x%s", r<<24|g<<16|b<<8|a, i!=48*48-1?", ": "");
if (i % 8 == 7) putchar('\n');
}
printf("};\n");
exit(0);
}