comparison bwsplash/pbm2c.c @ 195:4d9f24c501f3

bwsplash: simple splash screen for FreeCalypso Lite (smallbw)
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 28 Mar 2021 21:36:48 +0000
parents
children
comparison
equal deleted inserted replaced
194:0983c77b8f54 195:4d9f24c501f3
1 /*
2 * This program compiles bwsplash.pbm into a C char array
3 * for inclusion into phone UI firmware source.
4 */
5
6 #include <sys/types.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <strings.h>
11
12 FILE *inf, *outf;
13 char *infname, linebuf[128];
14 int lineno;
15
16 void
17 get_input_line()
18 {
19 char *cp;
20
21 if (!fgets(linebuf, sizeof linebuf, inf)) {
22 fprintf(stderr, "%s: premature EOF\n", infname);
23 exit(1);
24 }
25 lineno++;
26 cp = index(linebuf, '\n');
27 if (!cp) {
28 fprintf(stderr, "%s line %d: too long or missing newline\n",
29 infname, lineno);
30 exit(1);
31 }
32 *cp = '\0';
33 }
34
35 void
36 parse_pixel_line(outbuf, npix)
37 u_char *outbuf;
38 unsigned npix;
39 {
40 char *cp;
41 u_char *dp;
42 unsigned n;
43
44 cp = linebuf;
45 dp = outbuf;
46 for (n = 0; n < npix; n++) {
47 if (*cp != '0' && *cp != '1') {
48 inv: fprintf(stderr,
49 "%s line %d: does not match expected line of %u 0/1 characters\n",
50 infname, lineno, npix);
51 exit(1);
52 }
53 *dp++ = *cp++ - '0';
54 }
55 if (*cp)
56 goto inv;
57 }
58
59 void
60 pack_pixel_byte(bits)
61 u_char *bits;
62 {
63 unsigned byte, mask, n;
64
65 byte = 0;
66 for (n = 0, mask = 0x80; n < 8; n++, mask >>= 1)
67 if (bits[n])
68 byte |= mask;
69 fprintf(outf, "0x%02X,", byte);
70 }
71
72 main(argc, argv)
73 char **argv;
74 {
75 unsigned nrow, ncol;
76 u_char rowbuf[80];
77
78 if (argc != 3) {
79 fprintf(stderr, "usage: %s pbmfile outfile\n", argv[0]);
80 exit(1);
81 }
82 infname = argv[1];
83 inf = fopen(infname, "r");
84 if (!inf) {
85 perror(infname);
86 exit(1);
87 }
88 get_input_line();
89 if (strcmp(linebuf, "P1")) {
90 fprintf(stderr,
91 "error in %s: first line is not the expected \"P1\"\n",
92 infname);
93 exit(1);
94 }
95 get_input_line();
96 if (strcmp(linebuf, "80 30")) {
97 fprintf(stderr,
98 "error in %s: second line is not the expected \"80 30\"\n",
99 infname);
100 exit(1);
101 }
102 outf = fopen(argv[2], "w");
103 if (!outf) {
104 perror(argv[2]);
105 exit(1);
106 }
107 for (nrow = 0; nrow < 30; nrow++) {
108 get_input_line();
109 parse_pixel_line(rowbuf, 80);
110 putc('\t', outf);
111 for (ncol = 0; ncol < 80; ncol += 8)
112 pack_pixel_byte(rowbuf + ncol);
113 putc('\n', outf);
114 }
115 exit(0);
116 }