2 unihex2bmp - program to turn a GNU Unifont hex glyph page of 256 code
3 points into a Microsoft Bitmap (.bmp) or Wireless Bitmap file
5 Synopsis: unihex2bmp [-iin_file.hex] [-oout_file.bmp]
6 [-f] [-phex_page_num] [-w]
9 Author: Paul Hardy, unifoundry <at> unifoundry.com, December 2007
11 Copyright (C) 2007, 2008, 2013 Paul Hardy
15 This program is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 2 of the License, or
18 (at your option) any later version.
20 This program is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
37 These are the GNU Unifont hex strings for '0'-'9' and 'A'-'F',
38 for encoding as bit strings in row and column headers.
40 Looking at the final bitmap as a grid of 32*32 bit tiles, the
41 first row contains a hexadecimal character string of the first
42 3 hex digits in a 4 digit Unicode character name; the top column
43 contains a hex character string of the 4th (low-order) hex digit
44 of the Unicode character.
47 "0030:00000000182442424242424224180000", /* Hex digit 0 */
48 "0031:000000000818280808080808083E0000", /* Hex digit 1 */
49 "0032:000000003C4242020C102040407E0000", /* Hex digit 2 */
50 "0033:000000003C4242021C020242423C0000", /* Hex digit 3 */
51 "0034:00000000040C142444447E0404040000", /* Hex digit 4 */
52 "0035:000000007E4040407C020202423C0000", /* Hex digit 5 */
53 "0036:000000001C2040407C424242423C0000", /* Hex digit 6 */
54 "0037:000000007E0202040404080808080000", /* Hex digit 7 */
55 "0038:000000003C4242423C424242423C0000", /* Hex digit 8 */
56 "0039:000000003C4242423E02020204380000", /* Hex digit 9 */
57 "0041:0000000018242442427E424242420000", /* Hex digit A */
58 "0042:000000007C4242427C424242427C0000", /* Hex digit B */
59 "0043:000000003C42424040404042423C0000", /* Hex digit C */
60 "0044:00000000784442424242424244780000", /* Hex digit D */
61 "0045:000000007E4040407C404040407E0000", /* Hex digit E */
62 "0046:000000007E4040407C40404040400000", /* Hex digit F */
63 "0055:000000004242424242424242423C0000", /* Unicode 'U' */
64 "002B:0000000000000808087F080808000000" /* Unicode '+' */
66 unsigned char hexbits[18][32]; /* The above digits converted into bitmap */
68 unsigned unipage=0; /* Unicode page number, 0x00..0xff */
69 int flip=1; /* transpose entire matrix as in Unicode book */
74 main (int argc, char *argv[])
77 int i, j; /* loop variables */
78 unsigned k0; /* temp Unicode char variable */
79 unsigned swap; /* temp variable for swapping values */
80 char inbuf[256]; /* input buffer */
81 unsigned filesize; /* size of file in bytes */
82 unsigned bitmapsize; /* size of bitmap image in bytes */
83 unsigned thischar; /* the current character */
84 unsigned char thischarbyte; /* unsigned char lowest byte of Unicode char */
85 int thischarrow; /* row 0..15 where this character belongs */
86 int thiscol; /* column 0..15 where this character belongs */
87 int toppixelrow; /* pixel row, 0..16*32-1 */
88 unsigned lastpage=0; /* the last Unicode page read in font file */
89 int wbmp=0; /* set to 1 if writing .wbmp format file */
91 unsigned char bitmap[17*32][18*4]; /* final bitmap */
92 unsigned char charbits[32][4]; /* bitmap for one character, 4 bytes/row */
94 char *infile="", *outfile=""; /* names of input and output files */
95 FILE *infp, *outfp; /* file pointers of input and output files */
97 int init(); /* initializes bitmap row/col labeling, &c. */
98 int hex2bit(); /* convert hex string --> bitmap */
100 bitmapsize = 17*32*18*4; /* 17 rows by 18 cols, each 4 bytes */
103 for (i = 1; i < argc; i++) {
104 if (argv[i][0] == '-') { /* this is an option argument */
105 switch (argv[i][1]) {
106 case 'f': /* flip (transpose) glyphs in bitmap as in standard */
109 case 'i': /* name of input file */
110 infile = &argv[i][2];
112 case 'o': /* name of output file */
113 outfile = &argv[i][2];
115 case 'p': /* specify a Unicode page other than default of 0 */
116 sscanf (&argv[i][2], "%x", &unipage); /* Get Unicode page */
118 case 'w': /* write a .wbmp file instead of a .bmp file */
121 default: /* if unrecognized option, print list and exit */
122 fprintf (stderr, "\nSyntax:\n\n");
123 fprintf (stderr, " %s -p<Unicode_Page> ", argv[0]);
124 fprintf (stderr, "-i<Input_File> -o<Output_File> -w\n\n");
125 fprintf (stderr, " -w specifies .wbmp output instead of ");
126 fprintf (stderr, "default Windows .bmp output.\n\n");
127 fprintf (stderr, " -p is followed by 1 to 6 ");
128 fprintf (stderr, "Unicode page hex digits ");
129 fprintf (stderr, "(default is Page 0).\n\n");
130 fprintf (stderr, "\nExample:\n\n");
131 fprintf (stderr, " %s -p83 -iunifont.hex -ou83.bmp\n\n\n",
139 Make sure we can open any I/O files that were specified before
142 if (strlen (infile) > 0) {
143 if ((infp = fopen (infile, "r")) == NULL) {
144 fprintf (stderr, "Error: can't open %s for input.\n", infile);
151 if (strlen (outfile) > 0) {
152 if ((outfp = fopen (outfile, "w")) == NULL) {
153 fprintf (stderr, "Error: can't open %s for output.\n", outfile);
161 (void)init(bitmap); /* initialize bitmap with row/column headers, etc. */
164 Read in the characters in the page
166 while (lastpage <= unipage && fgets (inbuf, MAXBUF-1, infp) != NULL) {
167 sscanf (inbuf, "%x", &thischar);
168 lastpage = thischar >> 8; /* keep Unicode page to see if we can stop */
169 if (lastpage == unipage) {
170 thischarbyte = (unsigned char)(thischar & 0xff);
171 for (k0=0; inbuf[k0] != ':'; k0++);
173 hex2bit (&inbuf[k0], charbits); /* convert hex string to 32*4 bitmap */
176 Now write character bitmap upside-down in page array, to match
177 .bmp file order. In the .wbmp` and .bmp files, white is a '1'
178 bit and black is a '0' bit, so complement charbits[][].
181 thiscol = (thischarbyte & 0xf) + 2; /* column number will be 1..16 */
182 thischarrow = thischarbyte >> 4; /* charcter row number, 0..15 */
183 if (flip) { /* swap row and column placement */
185 thiscol = thischarrow;
187 thiscol += 2; /* column index starts at 1 */
188 thischarrow -= 2; /* row index starts at 0 */
190 toppixelrow = 32 * (thischarrow + 1) - 1; /* from bottom to top */
193 Copy the center of charbits[][] because hex characters only
194 occupy rows 8 to 23 and column byte 2 (and for 16 bit wide
195 characters, byte 3). The charbits[][] array was given 32 rows
196 and 4 column bytes for completeness in the beginning.
198 for (i=8; i<24; i++) {
199 bitmap[toppixelrow + i][(thiscol << 2) | 1] =
200 ~charbits[i][1] & 0xff;
201 bitmap[toppixelrow + i][(thiscol << 2) | 2] =
202 ~charbits[i][2] & 0xff;
207 Now write the appropriate bitmap file format, either
208 Wireless Bitmap or Microsoft Windows bitmap.
210 if (wbmp) { /* Write a Wireless Bitmap .wbmp format file */
214 fprintf (outfp, "%c", 0x00); /* Type of image; always 0 (monochrome) */
215 fprintf (outfp, "%c", 0x00); /* Reserved; always 0 */
216 fprintf (outfp, "%c%c", 0x84, 0x40); /* Width = 576 pixels */
217 fprintf (outfp, "%c%c", 0x84, 0x20); /* Height = 544 pixels */
221 for (toppixelrow=0; toppixelrow <= 17*32-1; toppixelrow++) {
222 for (j=0; j<18; j++) {
223 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) ]);
224 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 1]);
225 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 2]);
226 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 3]);
230 else { /* otherwise, write a Microsoft Windows .bmp format file */
232 Write the .bmp file -- start with the header, then write the bitmap
235 /* 'B', 'M' appears at start of every .bmp file */
236 fprintf (outfp, "%c%c", 0x42, 0x4d);
238 /* Write file size in bytes */
239 filesize = 0x3E + bitmapsize;
240 fprintf (outfp, "%c", (unsigned char)((filesize ) & 0xff));
241 fprintf (outfp, "%c", (unsigned char)((filesize >> 0x08) & 0xff));
242 fprintf (outfp, "%c", (unsigned char)((filesize >> 0x10) & 0xff));
243 fprintf (outfp, "%c", (unsigned char)((filesize >> 0x18) & 0xff));
246 fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
248 /* Offset from start of file to bitmap data */
249 fprintf (outfp, "%c%c%c%c", 0x3E, 0x00, 0x00, 0x00);
251 /* Length of bitmap info header */
252 fprintf (outfp, "%c%c%c%c", 0x28, 0x00, 0x00, 0x00);
254 /* Width of bitmap in pixels */
255 fprintf (outfp, "%c%c%c%c", 0x40, 0x02, 0x00, 0x00);
257 /* Height of bitmap in pixels */
258 fprintf (outfp, "%c%c%c%c", 0x20, 0x02, 0x00, 0x00);
260 /* Planes in bitmap (fixed at 1) */
261 fprintf (outfp, "%c%c", 0x01, 0x00);
263 /* bits per pixel (1 = monochrome) */
264 fprintf (outfp, "%c%c", 0x01, 0x00);
266 /* Compression (0 = none) */
267 fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
269 /* Size of bitmap data in bytes */
270 fprintf (outfp, "%c", (unsigned char)((bitmapsize ) & 0xff));
271 fprintf (outfp, "%c", (unsigned char)((bitmapsize >> 0x08) & 0xff));
272 fprintf (outfp, "%c", (unsigned char)((bitmapsize >> 0x10) & 0xff));
273 fprintf (outfp, "%c", (unsigned char)((bitmapsize >> 0x18) & 0xff));
275 /* Horizontal resolution in pixels per meter */
276 fprintf (outfp, "%c%c%c%c", 0xC4, 0x0E, 0x00, 0x00);
278 /* Vertical resolution in pixels per meter */
279 fprintf (outfp, "%c%c%c%c", 0xC4, 0x0E, 0x00, 0x00);
281 /* Number of colors used */
282 fprintf (outfp, "%c%c%c%c", 0x02, 0x00, 0x00, 0x00);
284 /* Number of important colors */
285 fprintf (outfp, "%c%c%c%c", 0x02, 0x00, 0x00, 0x00);
287 /* The color black: B=0x00, G=0x00, R=0x00, Filler=0xFF */
288 fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
290 /* The color white: B=0xFF, G=0xFF, R=0xFF, Filler=0xFF */
291 fprintf (outfp, "%c%c%c%c", 0xFF, 0xFF, 0xFF, 0x00);
294 Now write the raw data bits. Data is written from the lower
295 left-hand corner of the image to the upper right-hand corner
298 for (toppixelrow=17*32-1; toppixelrow >= 0; toppixelrow--) {
299 for (j=0; j<18; j++) {
300 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) ]);
301 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 1]);
302 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 2]);
304 fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 3]);
312 Convert the portion of a hex string after the ':' into a character bitmap.
314 If string is >= 128 characters, it will fill all 4 bytes per row.
315 If string is >= 64 characters and < 128, it will fill 2 bytes per row.
316 Otherwise, it will fill 1 byte per row.
319 hex2bit (char *instring, unsigned char character[32][4])
322 int i; /* current row in bitmap character */
323 int j; /* current character in input string */
324 int k; /* current byte in bitmap character */
325 int width; /* number of output bytes to fill: 1, 2, or 4 */
327 for (i=0; i<32; i++) /* erase previous character */
328 character[i][0] = character[i][1] = character[i][2] = character[i][3] = 0;
329 j=0; /* current location is at beginning of instring */
331 if (strlen (instring) <= 34) /* 32 + possible '\r', '\n' */
333 else if (strlen (instring) <= 66) /* 64 + possible '\r', '\n' */
338 k = (width > 1) ? 0 : 1; /* if width < 3, start at index 1 else at 0 */
340 for (i=8; i<24; i++) { /* 16 rows per input character, rows 8..23 */
341 sscanf (&instring[j], "%2hhx", &character[i][k]);
343 if (width > 0) { /* add next pair of hex digits to this row */
344 sscanf (&instring[j], "%2hhx", &character[i][k+1]);
347 if (width > 1) { /* add 2 next pairs of hex digits to this row */
348 sscanf (&instring[j], "%2hhx", &character[i][k+2]);
350 sscanf (&instring[j], "%2hhx", &character[i][k+3]);
359 init (unsigned char bitmap[17*32][18*4])
362 unsigned char charbits[32][4]; /* bitmap for one character, 4 bytes/row */
363 unsigned toppixelrow;
365 unsigned char pnybble0, pnybble1, pnybble2, pnybble3;
367 for (i=0; i<18; i++) { /* bitmaps for '0'..'9', 'A'-'F', 'u', '+' */
369 hex2bit (&hex[i][5], charbits); /* convert hex string to 32*4 bitmap */
371 for (j=0; j<32; j++) hexbits[i][j] = ~charbits[j][1];
375 Initialize bitmap to all white.
377 for (toppixelrow=0; toppixelrow < 17*32; toppixelrow++) {
378 for (thiscol=0; thiscol<18; thiscol++) {
379 bitmap[toppixelrow][(thiscol << 2) ] = 0xff;
380 bitmap[toppixelrow][(thiscol << 2) | 1] = 0xff;
381 bitmap[toppixelrow][(thiscol << 2) | 2] = 0xff;
382 bitmap[toppixelrow][(thiscol << 2) | 3] = 0xff;
386 Write the "u+nnnn" table header in the upper left-hand corner,
387 where nnnn is the upper 16 bits of a 32-bit Unicode assignment.
389 pnybble3 = (unipage >> 20);
390 pnybble2 = (unipage >> 16) & 0xf;
391 pnybble1 = (unipage >> 12) & 0xf;
392 pnybble0 = (unipage >> 8) & 0xf;
393 for (i=0; i<32; i++) {
394 bitmap[i][1] = hexbits[16][i]; /* copy 'u' */
395 bitmap[i][2] = hexbits[17][i]; /* copy '+' */
396 bitmap[i][3] = hexbits[pnybble3][i];
397 bitmap[i][4] = hexbits[pnybble2][i];
398 bitmap[i][5] = hexbits[pnybble1][i];
399 bitmap[i][6] = hexbits[pnybble0][i];
402 Write low-order 2 bytes of Unicode number assignments, as hex labels
404 pnybble3 = (unipage >> 4) & 0xf; /* Highest-order hex digit */
405 pnybble2 = (unipage ) & 0xf; /* Next highest-order hex digit */
407 Write the column headers in bitmap[][] (row headers if flipped)
409 toppixelrow = 32 * 17 - 1; /* maximum pixel row number */
411 Label the column headers. The hexbits[][] bytes are split across two
412 bitmap[][] entries to center a the hex digits in a column of 4 bytes.
413 OR highest byte with 0xf0 and lowest byte with 0x0f to make outer
414 nybbles white (0=black, 1-white).
416 for (i=0; i<16; i++) {
417 for (j=0; j<32; j++) {
418 if (flip) { /* transpose matrix */
419 bitmap[j][((i+2) << 2) | 0] = (hexbits[pnybble3][j] >> 4) | 0xf0;
420 bitmap[j][((i+2) << 2) | 1] = (hexbits[pnybble3][j] << 4) |
421 (hexbits[pnybble2][j] >> 4);
422 bitmap[j][((i+2) << 2) | 2] = (hexbits[pnybble2][j] << 4) |
423 (hexbits[i][j] >> 4);
424 bitmap[j][((i+2) << 2) | 3] = (hexbits[i][j] << 4) | 0x0f;
427 bitmap[j][((i+2) << 2) | 1] = (hexbits[i][j] >> 4) | 0xf0;
428 bitmap[j][((i+2) << 2) | 2] = (hexbits[i][j] << 4) | 0x0f;
433 Now use the single hex digit column graphics to label the row headers.
435 for (i=0; i<16; i++) {
436 toppixelrow = 32 * (i + 1) - 1; /* from bottom to top */
438 for (j=0; j<32; j++) {
439 if (!flip) { /* if not transposing matrix */
440 bitmap[toppixelrow + j][4] = hexbits[pnybble3][j];
441 bitmap[toppixelrow + j][5] = hexbits[pnybble2][j];
443 bitmap[toppixelrow + j][6] = hexbits[i][j];
447 Now draw grid lines in bitmap, around characters we just copied.
449 /* draw vertical lines 2 pixels wide */
450 for (i=1*32; i<17*32; i++) {
453 else if ((i & 0x1f) == 14)
455 else if ((i & 0x1f) == 22)
457 for (j=1; j<18; j++) {
458 bitmap[i][(j << 2) | 3] &= 0xfe;
461 /* draw horizontal lines 2 pixels tall */
462 for (i=1*32-1; i<18*32-1; i+=32) {
463 for (j=2; j<18; j++) {
464 bitmap[i][(j << 2) ] = 0x00;
465 bitmap[i][(j << 2) | 1] = 0x81;
466 bitmap[i][(j << 2) | 2] = 0x81;
467 bitmap[i][(j << 2) | 3] = 0x00;
470 /* fill in top left corner pixel of grid */
471 bitmap[31][7] = 0xfe;