unifont-6.3.20131215.tar.gz
[unifont.git] / src / unihex2bmp.c
1 /*
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
4
5    Synopsis: unihex2bmp [-iin_file.hex] [-oout_file.bmp]
6                 [-f] [-phex_page_num] [-w]
7
8
9    Author: Paul Hardy, unifoundry <at> unifoundry.com, December 2007
10    
11    Copyright (C) 2007, 2008, 2013 Paul Hardy
12
13    LICENSE:
14
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.
19
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.
24
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/>.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #define MAXBUF 256
34
35
36 /*
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.
39
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.
45 */
46 char *hex[18]= {
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 '+' */
65    };
66 unsigned char hexbits[18][32]; /* The above digits converted into bitmap */
67
68 unsigned unipage=0;        /* Unicode page number, 0x00..0xff */
69 int flip=1;                /* transpose entire matrix as in Unicode book */
70
71
72
73 int
74 main (int argc, char *argv[])
75 {
76
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 */
90
91    unsigned char bitmap[17*32][18*4]; /* final bitmap */
92    unsigned char charbits[32][4];  /* bitmap for one character, 4 bytes/row */
93
94    char *infile="", *outfile="";  /* names of input and output files */
95    FILE *infp, *outfp;      /* file pointers of input and output files */
96
97    int init();                  /* initializes bitmap row/col labeling, &c. */
98    int hex2bit();               /* convert hex string --> bitmap */
99
100    bitmapsize = 17*32*18*4;  /* 17 rows by 18 cols, each 4 bytes */
101
102    if (argc > 1) {
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 */
107                   flip = !flip;
108                   break;
109                case 'i':  /* name of input file */
110                   infile = &argv[i][2];
111                   break;
112                case 'o':  /* name of output file */
113                   outfile = &argv[i][2];
114                   break;
115                case 'p':  /* specify a Unicode page other than default of 0 */
116                   sscanf (&argv[i][2], "%x", &unipage); /* Get Unicode page */
117                   break;
118                case 'w':  /* write a .wbmp file instead of a .bmp file */
119                   wbmp = 1;
120                   break;
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",
132                          argv[0]);
133                   exit (1);
134             }
135          }
136       }
137    }
138    /*
139       Make sure we can open any I/O files that were specified before
140       doing anything else.
141    */
142    if (strlen (infile) > 0) {
143       if ((infp = fopen (infile, "r")) == NULL) {
144          fprintf (stderr, "Error: can't open %s for input.\n", infile);
145          exit (1);
146       }
147    }
148    else {
149       infp = stdin;
150    }
151    if (strlen (outfile) > 0) {
152       if ((outfp = fopen (outfile, "w")) == NULL) {
153          fprintf (stderr, "Error: can't open %s for output.\n", outfile);
154          exit (1);
155       }
156    }
157    else {
158       outfp = stdout;
159    }
160
161    (void)init(bitmap); /* initialize bitmap with row/column headers, etc. */
162
163    /*
164       Read in the characters in the page
165    */
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++);
172          k0++;
173          hex2bit (&inbuf[k0], charbits);  /* convert hex string to 32*4 bitmap */
174
175          /*
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[][].
179          */
180
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 */
184             swap = thiscol;
185             thiscol = thischarrow;
186             thischarrow = swap;
187             thiscol += 2;       /* column index starts at 1 */
188             thischarrow -= 2;   /* row index starts at 0    */
189          }
190          toppixelrow = 32 * (thischarrow + 1) - 1; /* from bottom to top    */
191    
192          /*
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.
197          */
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;
203          }
204       }
205    }
206    /*
207       Now write the appropriate bitmap file format, either
208       Wireless Bitmap or Microsoft Windows bitmap.
209    */
210    if (wbmp) {  /* Write a Wireless Bitmap .wbmp format file */
211       /*
212          Write WBMP header
213       */
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          */
218       /*
219          Write bitmap image
220       */
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]);
227          }
228       }
229    }
230    else {  /* otherwise, write a Microsoft Windows .bmp format file */
231       /*
232          Write the .bmp file -- start with the header, then write the bitmap
233       */
234
235       /* 'B', 'M' appears at start of every .bmp file */
236       fprintf (outfp, "%c%c", 0x42, 0x4d);
237
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));
244
245       /* Reserved - 0's */
246       fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
247
248       /* Offset from start of file to bitmap data */
249       fprintf (outfp, "%c%c%c%c", 0x3E, 0x00, 0x00, 0x00);
250
251       /* Length of bitmap info header */
252       fprintf (outfp, "%c%c%c%c", 0x28, 0x00, 0x00, 0x00);
253
254       /* Width of bitmap in pixels */
255       fprintf (outfp, "%c%c%c%c", 0x40, 0x02, 0x00, 0x00);
256
257       /* Height of bitmap in pixels */
258       fprintf (outfp, "%c%c%c%c", 0x20, 0x02, 0x00, 0x00);
259
260       /* Planes in bitmap (fixed at 1) */
261       fprintf (outfp, "%c%c", 0x01, 0x00);
262
263       /* bits per pixel (1 = monochrome) */
264       fprintf (outfp, "%c%c", 0x01, 0x00);
265
266       /* Compression (0 = none) */
267       fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
268
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));
274
275       /* Horizontal resolution in pixels per meter */
276       fprintf (outfp, "%c%c%c%c", 0xC4, 0x0E, 0x00, 0x00);
277
278       /* Vertical resolution in pixels per meter */
279       fprintf (outfp, "%c%c%c%c", 0xC4, 0x0E, 0x00, 0x00);
280
281       /* Number of colors used */
282       fprintf (outfp, "%c%c%c%c", 0x02, 0x00, 0x00, 0x00);
283
284       /* Number of important colors */
285       fprintf (outfp, "%c%c%c%c", 0x02, 0x00, 0x00, 0x00);
286
287       /* The color black: B=0x00, G=0x00, R=0x00, Filler=0xFF */
288       fprintf (outfp, "%c%c%c%c", 0x00, 0x00, 0x00, 0x00);
289
290       /* The color white: B=0xFF, G=0xFF, R=0xFF, Filler=0xFF */
291       fprintf (outfp, "%c%c%c%c", 0xFF, 0xFF, 0xFF, 0x00);
292
293       /*
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
296          of the image.
297       */
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]);
303
304             fprintf (outfp, "%c", bitmap[toppixelrow][(j<<2) | 3]);
305          }
306       }
307    }
308    exit (0);
309 }
310
311 /*
312    Convert the portion of a hex string after the ':' into a character bitmap.
313
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.
317 */
318 int
319 hex2bit (char *instring, unsigned char character[32][4])
320 {
321
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 */
326
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 */
330
331    if (strlen (instring) <= 34)  /* 32 + possible '\r', '\n' */
332       width = 0;
333    else if (strlen (instring) <= 66)  /* 64 + possible '\r', '\n' */
334       width = 1;
335    else
336       width = 4;
337
338    k = (width > 1) ? 0 : 1;  /* if width < 3, start at index 1 else at 0 */
339
340    for (i=8; i<24; i++) {  /* 16 rows per input character, rows 8..23 */
341       sscanf (&instring[j], "%2hhx", &character[i][k]);
342       j += 2;
343       if (width > 0) { /* add next pair of hex digits to this row */
344          sscanf (&instring[j], "%2hhx", &character[i][k+1]);
345          j += 2;
346       }
347       if (width > 1) { /* add 2 next pairs of hex digits to this row */
348          sscanf (&instring[j], "%2hhx", &character[i][k+2]);
349          j += 2;
350          sscanf (&instring[j], "%2hhx", &character[i][k+3]);
351          j += 2;
352       }
353    }
354
355    return (0);
356 }
357
358 int
359 init (unsigned char bitmap[17*32][18*4])
360 {
361    int i, j;
362    unsigned char charbits[32][4];  /* bitmap for one character, 4 bytes/row */
363    unsigned toppixelrow;
364    unsigned thiscol;
365    unsigned char pnybble0, pnybble1, pnybble2, pnybble3;
366
367    for (i=0; i<18; i++) { /* bitmaps for '0'..'9', 'A'-'F', 'u', '+' */
368
369       hex2bit (&hex[i][5], charbits);  /* convert hex string to 32*4 bitmap */
370
371       for (j=0; j<32; j++) hexbits[i][j] = ~charbits[j][1];
372    }
373
374    /*
375       Initialize bitmap to all white.
376    */
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;
383       }
384    }
385    /*
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.
388    */
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];
400    }
401    /*
402       Write low-order 2 bytes of Unicode number assignments, as hex labels
403    */
404    pnybble3 = (unipage >> 4) & 0xf;  /* Highest-order hex digit */
405    pnybble2 = (unipage     ) & 0xf;  /* Next highest-order hex digit */
406    /*
407       Write the column headers in bitmap[][] (row headers if flipped)
408    */
409    toppixelrow = 32 * 17 - 1; /* maximum pixel row number */
410    /*
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).
415    */
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;
425          }
426          else {
427             bitmap[j][((i+2) << 2) | 1] = (hexbits[i][j] >> 4) | 0xf0;
428             bitmap[j][((i+2) << 2) | 2] = (hexbits[i][j] << 4) | 0x0f;
429          }
430       }
431    }
432    /*
433       Now use the single hex digit column graphics to label the row headers.
434    */
435    for (i=0; i<16; i++) {
436       toppixelrow = 32 * (i + 1) - 1; /* from bottom to top    */
437
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];
442          }
443          bitmap[toppixelrow + j][6] = hexbits[i][j];
444       }
445    }
446    /*
447       Now draw grid lines in bitmap, around characters we just copied.
448    */
449    /* draw vertical lines 2 pixels wide */
450    for (i=1*32; i<17*32; i++) {
451       if ((i & 0x1f) == 7)
452          i++;
453       else if ((i & 0x1f) == 14)
454          i += 2;
455       else if ((i & 0x1f) == 22)
456          i++;
457       for (j=1; j<18; j++) {
458          bitmap[i][(j << 2) | 3] &= 0xfe;
459       }
460    }
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;
468       }
469    }
470    /* fill in top left corner pixel of grid */
471    bitmap[31][7] = 0xfe;
472
473    return (0);
474 }