GNU Unifont: src/unibmp2hex.c Source File

GNU Unifont 15.0.02 Pan-Unicode font with complete Unicode Plane 0 coverage and partial coverage of higher planes

unibmp2hex.c
Go to the documentation of this file.
1/**
2 @file unibmp2hex.c
3
4 @brief unibmp2hex - Turn a .bmp or .wbmp glyph matrix into a
5 GNU Unifont hex glyph set of 256 characters
6
7 @author Paul Hardy, unifoundry <at> unifoundry.com, December 2007
8
9 @copyright Copyright (C) 2007, 2008, 2013, 2017, 2019, 2022 Paul Hardy
10
11 Synopsis: unibmp2hex [-iin_file.bmp] [-oout_file.hex] [-phex_page_num] [-w]
12*/
13/*
14
15 LICENSE:
16
17 This program is free software: you can redistribute it and/or modify
18 it under the terms of the GNU General Public License as published by
19 the Free Software Foundation, either version 2 of the License, or
20 (at your option) any later version.
21
22 This program is distributed in the hope that it will be useful,
23 but WITHOUT ANY WARRANTY; without even the implied warranty of
24 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 GNU General Public License for more details.
26
27 You should have received a copy of the GNU General Public License
28 along with this program. If not, see <http://www.gnu.org/licenses/>.
29*/
30
31/*
32 6 September 2021 [Paul Hardy]:
33 - Set U+12F90..U+12FFF (Cypro-Minoan) to be double width.
34 - Set U+1CF00..U+1CFCF (Znamenny Musical Notation) to be double width.
35 - Set U+1AFF0..U+1AFFF (Kana Extended-B) to be double width.
36
37 20 June 2017 [Paul Hardy]:
38 - Modify to allow hard-coding of quadruple-width hex glyphs.
39 The 32nd column (rightmost column) is cleared to zero, because
40 that column contains the vertical cell border.
41 - Set U+9FD8..U+9FE9 (complex CJK) to be quadruple-width.
42 - Set U+011A00..U+011A4F (Masaram Gondi, non-digits) to be wide.
43 - Set U+011A50..U+011AAF (Soyombo) to be wide.
44
45 8 July 2017 [Paul Hardy]:
46 - All CJK glyphs in the range U+4E00..u+9FFF are double width
47 again; commented out the line that sets U+9FD8..U+9FE9 to be
48 quadruple width.
49
50 6 August 2017 [Paul Hardy]:
51 - Remove hard-coding of U+01D200..U+01D24F Ancient Greek Musical
52 Notation to double-width; allow range to be dual-width.
53
54 12 August 2017 [Paul Hardy]:
55 - Remove Miao script from list of wide scripts, so it can contain
56 single-width glyphs.
57
58 26 December 2017 Paul Hardy:
59 - Removed Tibetan from list of wide scripts, so it can contain
60 single-width glyphs.
61 - Added a number of scripts to be explicitly double-width in case
62 they are redrawn.
63 - Added Miao script back as wide, because combining glyphs are
64 added back to font/plane01/plane01-combining.txt.
65
66 05 June 2018 Paul Hardy:
67 - Made U+2329] and U+232A wide.
68 - Added to wide settings for CJK Compatibility Forms over entire range.
69 - Made Kayah Li script double-width.
70 - Made U+232A (Right-pointing Angle Bracket) double-width.
71 - Made U+01F5E7 (Three Rays Right) double-width.
72
73 July 2018 Paul Hardy:
74 - Changed 2017 to 2018 in previous change entry.
75 - Added Dogra (U+011800..U+01184F) as double width.
76 - Added Makasar (U+011EE0..U+011EFF) as dobule width.
77
78 23 February 2019 [Paul Hardy]:
79 - Set U+119A0..U+119FF (Nandinagari) to be wide.
80 - Set U+1E2C0..U+1E2FF (Wancho) to be wide.
81
82 25 May 2019 [Paul Hardy]:
83 - Added support for the case when the original .bmp monochrome
84 file has been converted to a 32 bit per pixel RGB file.
85 - Added support for bitmap images stored from either top to bottom
86 or bottom to top.
87 - Add DEBUG compile flag to print header information, to ease
88 adding support for additional bitmap formats in the future.
89
90 13 March 2022 [Paul Hardy]:
91 - Added support for 24 bits per pixel RGB file.
92
93 12 June 2022 [Paul Hardy]:
94 - Set U+11B00..U+11B5F (Devanagari Extended-A) to be wide.
95 - Set U+11F00..U+11F5F (Kawi) to be wide.
96
97
98*/
99
100#include <stdio.h>
101#include <stdlib.h>
102#include <string.h>
103
104#define MAXBUF 256 ///< Maximum input file line length - 1
105
106
107 unsigned hexdigit[16][4]; ///< 32 bit representation of 16x8 0..F bitmap
108
109 unsigned uniplane=0; ///< Unicode plane number, 0..0xff ff ff
110 unsigned planeset=0; ///< =1: use plane specified with -p parameter
111 unsigned flip=0; ///< =1 if we're transposing glyph matrix
112 unsigned forcewide=0; ///< =1 to set each glyph to 16 pixels wide
113
114/** The six Unicode plane digits, from left-most (0) to right-most (5) */
115 unsigned unidigit[6][4];
116
117
118/** Bitmap Header parameters */
119struct {
120 char filetype[2];
121 int file_size;
122 int image_offset;
123 int info_size;
124 int width;
125 int height;
126 int nplanes;
127 int bits_per_pixel;
128 int compression;
129 int image_size;
130 int x_ppm;
131 int y_ppm;
132 int ncolors;
133 int important_colors;
134} bmp_header;
135
136/** Bitmap Color Table -- maximum of 256 colors in a BMP file */
137 unsigned char color_table[256][4]; /* R, G, B, alpha for up to 256 colors */
138
139// #define DEBUG
140
141/**
142 @brief The main function.
143
144 @param[in] argc The count of command line arguments.
145 @param[in] argv Pointer to array of command line arguments.
146 @return This program exits with status 0.
147*/
148int
149main (int argc, char *argv[])
150{
151
152 int i, j, k; /* loop variables */
153 unsigned char inchar; /* temporary input character */
154 char header[MAXBUF]; /* input buffer for bitmap file header */
155 int wbmp=0; /* =0 for Windows Bitmap (.bmp); 1 for Wireless Bitmap (.wbmp) */
156 int fatal; /* =1 if a fatal error occurred */
157 int match; /* =1 if we're still matching a pattern, 0 if no match */
158 int empty1, empty2; /* =1 if bytes tested are all zeroes */
159 unsigned char thischar1[16], thischar2[16]; /* bytes of hex char */
160 unsigned char thischar0[16], thischar3[16]; /* bytes for quadruple-width */
161 int thisrow; /* index to point into thischar1[] and thischar2[] */
162 int tmpsum; /* temporary sum to see if a character is blank */
163 unsigned this_pixel; /* color of one pixel, if > 1 bit per pixel */
164 unsigned next_pixels; /* pending group of 8 pixels being read */
165 unsigned color_mask = 0x00; /* to invert monochrome bitmap, set to 0xFF */
166
167 unsigned char bitmap[17*32][18*32/8]; /* final bitmap */
168 /* For wide array:
169 0 = don't force glyph to double-width;
170 1 = force glyph to double-width;
171 4 = force glyph to quadruple-width.
172 */
173 char wide[0x200000]={0x200000 * 0};
174
175 char *infile="", *outfile=""; /* names of input and output files */
176 FILE *infp, *outfp; /* file pointers of input and output files */
177
178 if (argc > 1) {
179 for (i = 1; i < argc; i++) {
180 if (argv[i][0] == '-') { /* this is an option argument */
181 switch (argv[i][1]) {
182 case 'i': /* name of input file */
183 infile = &argv[i][2];
184 break;
185 case 'o': /* name of output file */
186 outfile = &argv[i][2];
187 break;
188 case 'p': /* specify a Unicode plane */
189 sscanf (&argv[i][2], "%x", &uniplane); /* Get Unicode plane */
190 planeset = 1; /* Use specified range, not what's in bitmap */
191 break;
192 case 'w': /* force wide (16 pixels) for each glyph */
193 forcewide = 1;
194 break;
195 default: /* if unrecognized option, print list and exit */
196 fprintf (stderr, "\nSyntax:\n\n");
197 fprintf (stderr, " %s -p<Unicode_Page> ", argv[0]);
198 fprintf (stderr, "-i<Input_File> -o<Output_File> -w\n\n");
199 fprintf (stderr, " -w specifies .wbmp output instead of ");
200 fprintf (stderr, "default Windows .bmp output.\n\n");
201 fprintf (stderr, " -p is followed by 1 to 6 ");
202 fprintf (stderr, "Unicode plane hex digits ");
203 fprintf (stderr, "(default is Page 0).\n\n");
204 fprintf (stderr, "\nExample:\n\n");
205 fprintf (stderr, " %s -p83 -iunifont.hex -ou83.bmp\n\n\n",
206 argv[0]);
207 exit (1);
208 }
209 }
210 }
211 }
212 /*
213 Make sure we can open any I/O files that were specified before
214 doing anything else.
215 */
216 if (strlen (infile) > 0) {
217 if ((infp = fopen (infile, "r")) == NULL) {
218 fprintf (stderr, "Error: can't open %s for input.\n", infile);
219 exit (1);
220 }
221 }
222 else {
223 infp = stdin;
224 }
225 if (strlen (outfile) > 0) {
226 if ((outfp = fopen (outfile, "w")) == NULL) {
227 fprintf (stderr, "Error: can't open %s for output.\n", outfile);
228 exit (1);
229 }
230 }
231 else {
232 outfp = stdout;
233 }
234 /*
235 Initialize selected code points for double width (16x16).
236 Double-width is forced in cases where a glyph (usually a combining
237 glyph) only occupies the left-hand side of a 16x16 grid, but must
238 be rendered as double-width to appear properly with other glyphs
239 in a given script. If additions were made to a script after
240 Unicode 5.0, the Unicode version is given in parentheses after
241 the script name.
242 */
243 for (i = 0x0700; i <= 0x074F; i++) wide[i] = 1; /* Syriac */
244 for (i = 0x0800; i <= 0x083F; i++) wide[i] = 1; /* Samaritan (5.2) */
245 for (i = 0x0900; i <= 0x0DFF; i++) wide[i] = 1; /* Indic */
246 for (i = 0x1000; i <= 0x109F; i++) wide[i] = 1; /* Myanmar */
247 for (i = 0x1100; i <= 0x11FF; i++) wide[i] = 1; /* Hangul Jamo */
248 for (i = 0x1400; i <= 0x167F; i++) wide[i] = 1; /* Canadian Aboriginal */
249 for (i = 0x1700; i <= 0x171F; i++) wide[i] = 1; /* Tagalog */
250 for (i = 0x1720; i <= 0x173F; i++) wide[i] = 1; /* Hanunoo */
251 for (i = 0x1740; i <= 0x175F; i++) wide[i] = 1; /* Buhid */
252 for (i = 0x1760; i <= 0x177F; i++) wide[i] = 1; /* Tagbanwa */
253 for (i = 0x1780; i <= 0x17FF; i++) wide[i] = 1; /* Khmer */
254 for (i = 0x18B0; i <= 0x18FF; i++) wide[i] = 1; /* Ext. Can. Aboriginal */
255 for (i = 0x1800; i <= 0x18AF; i++) wide[i] = 1; /* Mongolian */
256 for (i = 0x1900; i <= 0x194F; i++) wide[i] = 1; /* Limbu */
257// for (i = 0x1980; i <= 0x19DF; i++) wide[i] = 1; /* New Tai Lue */
258 for (i = 0x1A00; i <= 0x1A1F; i++) wide[i] = 1; /* Buginese */
259 for (i = 0x1A20; i <= 0x1AAF; i++) wide[i] = 1; /* Tai Tham (5.2) */
260 for (i = 0x1B00; i <= 0x1B7F; i++) wide[i] = 1; /* Balinese */
261 for (i = 0x1B80; i <= 0x1BBF; i++) wide[i] = 1; /* Sundanese (5.1) */
262 for (i = 0x1BC0; i <= 0x1BFF; i++) wide[i] = 1; /* Batak (6.0) */
263 for (i = 0x1C00; i <= 0x1C4F; i++) wide[i] = 1; /* Lepcha (5.1) */
264 for (i = 0x1CC0; i <= 0x1CCF; i++) wide[i] = 1; /* Sundanese Supplement */
265 for (i = 0x1CD0; i <= 0x1CFF; i++) wide[i] = 1; /* Vedic Extensions (5.2) */
266 wide[0x2329] = wide[0x232A] = 1; /* Left- & Right-pointing Angle Brackets */
267 for (i = 0x2E80; i <= 0xA4CF; i++) wide[i] = 1; /* CJK */
268// for (i = 0x9FD8; i <= 0x9FE9; i++) wide[i] = 4; /* CJK quadruple-width */
269 for (i = 0xA900; i <= 0xA92F; i++) wide[i] = 1; /* Kayah Li (5.1) */
270 for (i = 0xA930; i <= 0xA95F; i++) wide[i] = 1; /* Rejang (5.1) */
271 for (i = 0xA960; i <= 0xA97F; i++) wide[i] = 1; /* Hangul Jamo Extended-A */
272 for (i = 0xA980; i <= 0xA9DF; i++) wide[i] = 1; /* Javanese (5.2) */
273 for (i = 0xAA00; i <= 0xAA5F; i++) wide[i] = 1; /* Cham (5.1) */
274 for (i = 0xA9E0; i <= 0xA9FF; i++) wide[i] = 1; /* Myanmar Extended-B */
275 for (i = 0xAA00; i <= 0xAA5F; i++) wide[i] = 1; /* Cham */
276 for (i = 0xAA60; i <= 0xAA7F; i++) wide[i] = 1; /* Myanmar Extended-A */
277 for (i = 0xAAE0; i <= 0xAAFF; i++) wide[i] = 1; /* Meetei Mayek Ext (6.0) */
278 for (i = 0xABC0; i <= 0xABFF; i++) wide[i] = 1; /* Meetei Mayek (5.2) */
279 for (i = 0xAC00; i <= 0xD7AF; i++) wide[i] = 1; /* Hangul Syllables */
280 for (i = 0xD7B0; i <= 0xD7FF; i++) wide[i] = 1; /* Hangul Jamo Extended-B */
281 for (i = 0xF900; i <= 0xFAFF; i++) wide[i] = 1; /* CJK Compatibility */
282 for (i = 0xFE10; i <= 0xFE1F; i++) wide[i] = 1; /* Vertical Forms */
283 for (i = 0xFE30; i <= 0xFE60; i++) wide[i] = 1; /* CJK Compatibility Forms*/
284 for (i = 0xFFE0; i <= 0xFFE6; i++) wide[i] = 1; /* CJK Compatibility Forms*/
285
286 wide[0x303F] = 0; /* CJK half-space fill */
287
288 /* Supplemental Multilingual Plane (Plane 01) */
289 for (i = 0x010A00; i <= 0x010A5F; i++) wide[i] = 1; /* Kharoshthi */
290 for (i = 0x011000; i <= 0x01107F; i++) wide[i] = 1; /* Brahmi */
291 for (i = 0x011080; i <= 0x0110CF; i++) wide[i] = 1; /* Kaithi */
292 for (i = 0x011100; i <= 0x01114F; i++) wide[i] = 1; /* Chakma */
293 for (i = 0x011180; i <= 0x0111DF; i++) wide[i] = 1; /* Sharada */
294 for (i = 0x011200; i <= 0x01124F; i++) wide[i] = 1; /* Khojki */
295 for (i = 0x0112B0; i <= 0x0112FF; i++) wide[i] = 1; /* Khudawadi */
296 for (i = 0x011300; i <= 0x01137F; i++) wide[i] = 1; /* Grantha */
297 for (i = 0x011400; i <= 0x01147F; i++) wide[i] = 1; /* Newa */
298 for (i = 0x011480; i <= 0x0114DF; i++) wide[i] = 1; /* Tirhuta */
299 for (i = 0x011580; i <= 0x0115FF; i++) wide[i] = 1; /* Siddham */
300 for (i = 0x011600; i <= 0x01165F; i++) wide[i] = 1; /* Modi */
301 for (i = 0x011660; i <= 0x01167F; i++) wide[i] = 1; /* Mongolian Suppl. */
302 for (i = 0x011680; i <= 0x0116CF; i++) wide[i] = 1; /* Takri */
303 for (i = 0x011700; i <= 0x01173F; i++) wide[i] = 1; /* Ahom */
304 for (i = 0x011800; i <= 0x01184F; i++) wide[i] = 1; /* Dogra */
305 for (i = 0x011900; i <= 0x01195F; i++) wide[i] = 1; /* Dives Akuru */
306 for (i = 0x0119A0; i <= 0x0119FF; i++) wide[i] = 1; /* Nandinagari */
307 for (i = 0x011A00; i <= 0x011A4F; i++) wide[i] = 1; /* Zanabazar Square */
308 for (i = 0x011A50; i <= 0x011AAF; i++) wide[i] = 1; /* Soyombo */
309 for (i = 0x011B00; i <= 0x011B5F; i++) wide[i] = 1; /* Devanagari Extended-A*/
310 for (i = 0x011F00; i <= 0x011F5F; i++) wide[i] = 1; /* Kawi */
311 for (i = 0x011C00; i <= 0x011C6F; i++) wide[i] = 1; /* Bhaiksuki */
312 for (i = 0x011C70; i <= 0x011CBF; i++) wide[i] = 1; /* Marchen */
313 for (i = 0x011D00; i <= 0x011D5F; i++) wide[i] = 1; /* Masaram Gondi */
314 for (i = 0x011EE0; i <= 0x011EFF; i++) wide[i] = 1; /* Makasar */
315 for (i = 0x012F90; i <= 0x012FFF; i++) wide[i] = 1; /* Cypro-Minoan */
316 /* Make Bassa Vah all single width or all double width */
317 for (i = 0x016AD0; i <= 0x016AFF; i++) wide[i] = 1; /* Bassa Vah */
318 for (i = 0x016B00; i <= 0x016B8F; i++) wide[i] = 1; /* Pahawh Hmong */
319 for (i = 0x016F00; i <= 0x016F9F; i++) wide[i] = 1; /* Miao */
320 for (i = 0x016FE0; i <= 0x016FFF; i++) wide[i] = 1; /* Ideograph Sym/Punct*/
321 for (i = 0x017000; i <= 0x0187FF; i++) wide[i] = 1; /* Tangut */
322 for (i = 0x018800; i <= 0x018AFF; i++) wide[i] = 1; /* Tangut Components */
323 for (i = 0x01AFF0; i <= 0x01AFFF; i++) wide[i] = 1; /* Kana Extended-B */
324 for (i = 0x01B000; i <= 0x01B0FF; i++) wide[i] = 1; /* Kana Supplement */
325 for (i = 0x01B100; i <= 0x01B12F; i++) wide[i] = 1; /* Kana Extended-A */
326 for (i = 0x01B170; i <= 0x01B2FF; i++) wide[i] = 1; /* Nushu */
327 for (i = 0x01CF00; i <= 0x01CFCF; i++) wide[i] = 1; /* Znamenny Musical */
328 for (i = 0x01D100; i <= 0x01D1FF; i++) wide[i] = 1; /* Musical Symbols */
329 for (i = 0x01D800; i <= 0x01DAAF; i++) wide[i] = 1; /* Sutton SignWriting */
330 for (i = 0x01E2C0; i <= 0x01E2FF; i++) wide[i] = 1; /* Wancho */
331 for (i = 0x01E800; i <= 0x01E8DF; i++) wide[i] = 1; /* Mende Kikakui */
332 for (i = 0x01F200; i <= 0x01F2FF; i++) wide[i] = 1; /* Encl Ideograp Suppl*/
333 wide[0x01F5E7] = 1; /* Three Rays Right */
334
335 /*
336 Determine whether or not the file is a Microsoft Windows Bitmap file.
337 If it starts with 'B', 'M', assume it's a Windows Bitmap file.
338 Otherwise, assume it's a Wireless Bitmap file.
339
340 WARNING: There isn't much in the way of error checking here --
341 if you give it a file that wasn't first created by hex2bmp.c,
342 all bets are off.
343 */
344 fatal = 0; /* assume everything is okay with reading input file */
345 if ((header[0] = fgetc (infp)) != EOF) {
346 if ((header[1] = fgetc (infp)) != EOF) {
347 if (header[0] == 'B' && header[1] == 'M') {
348 wbmp = 0; /* Not a Wireless Bitmap -- it's a Windows Bitmap */
349 }
350 else {
351 wbmp = 1; /* Assume it's a Wireless Bitmap */
352 }
353 }
354 else
355 fatal = 1;
356 }
357 else
358 fatal = 1;
359
360 if (fatal) {
361 fprintf (stderr, "Fatal error; end of input file.\n\n");
362 exit (1);
363 }
364 /*
365 If this is a Wireless Bitmap (.wbmp) format file,
366 skip the header and point to the start of the bitmap itself.
367 */
368 if (wbmp) {
369 for (i=2; i<6; i++)
370 header[i] = fgetc (infp);
371 /*
372 Now read the bitmap.
373 */
374 for (i=0; i < 32*17; i++) {
375 for (j=0; j < 32*18/8; j++) {
376 inchar = fgetc (infp);
377 bitmap[i][j] = ~inchar; /* invert bits for proper color */
378 }
379 }
380 }
381 /*
382 Otherwise, treat this as a Windows Bitmap file, because we checked
383 that it began with "BM". Save the header contents for future use.
384 Expect a 14 byte standard BITMAPFILEHEADER format header followed
385 by a 40 byte standard BITMAPINFOHEADER Device Independent Bitmap
386 header, with data stored in little-endian format.
387 */
388 else {
389 for (i = 2; i < 54; i++)
390 header[i] = fgetc (infp);
391
392 bmp_header.filetype[0] = 'B';
393 bmp_header.filetype[1] = 'M';
394
395 bmp_header.file_size =
396 (header[2] & 0xFF) | ((header[3] & 0xFF) << 8) |
397 ((header[4] & 0xFF) << 16) | ((header[5] & 0xFF) << 24);
398
399 /* header bytes 6..9 are reserved */
400
401 bmp_header.image_offset =
402 (header[10] & 0xFF) | ((header[11] & 0xFF) << 8) |
403 ((header[12] & 0xFF) << 16) | ((header[13] & 0xFF) << 24);
404
405 bmp_header.info_size =
406 (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) |
407 ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24);
408
409 bmp_header.width =
410 (header[18] & 0xFF) | ((header[19] & 0xFF) << 8) |
411 ((header[20] & 0xFF) << 16) | ((header[21] & 0xFF) << 24);
412
413 bmp_header.height =
414 (header[22] & 0xFF) | ((header[23] & 0xFF) << 8) |
415 ((header[24] & 0xFF) << 16) | ((header[25] & 0xFF) << 24);
416
417 bmp_header.nplanes =
418 (header[26] & 0xFF) | ((header[27] & 0xFF) << 8);
419
420 bmp_header.bits_per_pixel =
421 (header[28] & 0xFF) | ((header[29] & 0xFF) << 8);
422
423 bmp_header.compression =
424 (header[30] & 0xFF) | ((header[31] & 0xFF) << 8) |
425 ((header[32] & 0xFF) << 16) | ((header[33] & 0xFF) << 24);
426
427 bmp_header.image_size =
428 (header[34] & 0xFF) | ((header[35] & 0xFF) << 8) |
429 ((header[36] & 0xFF) << 16) | ((header[37] & 0xFF) << 24);
430
431 bmp_header.x_ppm =
432 (header[38] & 0xFF) | ((header[39] & 0xFF) << 8) |
433 ((header[40] & 0xFF) << 16) | ((header[41] & 0xFF) << 24);
434
435 bmp_header.y_ppm =
436 (header[42] & 0xFF) | ((header[43] & 0xFF) << 8) |
437 ((header[44] & 0xFF) << 16) | ((header[45] & 0xFF) << 24);
438
439 bmp_header.ncolors =
440 (header[46] & 0xFF) | ((header[47] & 0xFF) << 8) |
441 ((header[48] & 0xFF) << 16) | ((header[49] & 0xFF) << 24);
442
443 bmp_header.important_colors =
444 (header[50] & 0xFF) | ((header[51] & 0xFF) << 8) |
445 ((header[52] & 0xFF) << 16) | ((header[53] & 0xFF) << 24);
446
447 if (bmp_header.ncolors == 0)
448 bmp_header.ncolors = 1 << bmp_header.bits_per_pixel;
449
450 /* If a Color Table exists, read it */
451 if (bmp_header.ncolors > 0 && bmp_header.bits_per_pixel <= 8) {
452 for (i = 0; i < bmp_header.ncolors; i++) {
453 color_table[i][0] = fgetc (infp); /* Red */
454 color_table[i][1] = fgetc (infp); /* Green */
455 color_table[i][2] = fgetc (infp); /* Blue */
456 color_table[i][3] = fgetc (infp); /* Alpha */
457 }
458 /*
459 Determine from the first color table entry whether we
460 are inverting the resulting bitmap image.
461 */
462 if ( (color_table[0][0] + color_table[0][1] + color_table[0][2])
463 < (3 * 128) ) {
464 color_mask = 0xFF;
465 }
466 }
467
468#ifdef DEBUG
469
470 /*
471 Print header info for possibly adding support for
472 additional file formats in the future, to determine
473 how the bitmap is encoded.
474 */
475 fprintf (stderr, "Filetype: '%c%c'\n",
476 bmp_header.filetype[0], bmp_header.filetype[1]);
477 fprintf (stderr, "File Size: %d\n", bmp_header.file_size);
478 fprintf (stderr, "Image Offset: %d\n", bmp_header.image_offset);
479 fprintf (stderr, "Info Header Size: %d\n", bmp_header.info_size);
480 fprintf (stderr, "Image Width: %d\n", bmp_header.width);
481 fprintf (stderr, "Image Height: %d\n", bmp_header.height);
482 fprintf (stderr, "Number of Planes: %d\n", bmp_header.nplanes);
483 fprintf (stderr, "Bits per Pixel: %d\n", bmp_header.bits_per_pixel);
484 fprintf (stderr, "Compression Method: %d\n", bmp_header.compression);
485 fprintf (stderr, "Image Size: %d\n", bmp_header.image_size);
486 fprintf (stderr, "X Pixels per Meter: %d\n", bmp_header.x_ppm);
487 fprintf (stderr, "Y Pixels per Meter: %d\n", bmp_header.y_ppm);
488 fprintf (stderr, "Number of Colors: %d\n", bmp_header.ncolors);
489 fprintf (stderr, "Important Colors: %d\n", bmp_header.important_colors);
490
491#endif
492
493 /*
494 Now read the bitmap.
495 */
496 for (i = 32*17-1; i >= 0; i--) {
497 for (j=0; j < 32*18/8; j++) {
498 next_pixels = 0x00; /* initialize next group of 8 pixels */
499 /* Read a monochrome image -- the original case */
500 if (bmp_header.bits_per_pixel == 1) {
501 next_pixels = fgetc (infp);
502 }
503 /* Read a 32 bit per pixel RGB image; convert to monochrome */
504 else if ( bmp_header.bits_per_pixel == 24 ||
505 bmp_header.bits_per_pixel == 32) {
506 next_pixels = 0;
507 for (k = 0; k < 8; k++) { /* get next 8 pixels */
508 this_pixel = (fgetc (infp) & 0xFF) +
509 (fgetc (infp) & 0xFF) +
510 (fgetc (infp) & 0xFF);
511
512 if (bmp_header.bits_per_pixel == 32) {
513 (void) fgetc (infp); /* ignore alpha value */
514 }
515
516 /* convert RGB color space to monochrome */
517 if (this_pixel >= (128 * 3))
518 this_pixel = 0;
519 else
520 this_pixel = 1;
521
522 /* shift next pixel color into place for 8 pixels total */
523 next_pixels = (next_pixels << 1) | this_pixel;
524 }
525 }
526 if (bmp_header.height < 0) { /* Bitmap drawn top to bottom */
527 bitmap [(32*17-1) - i] [j] = next_pixels;
528 }
529 else { /* Bitmap drawn bottom to top */
530 bitmap [i][j] = next_pixels;
531 }
532 }
533 }
534
535 /*
536 If any bits are set in color_mask, apply it to
537 entire bitmap to invert black <--> white.
538 */
539 if (color_mask != 0x00) {
540 for (i = 32*17-1; i >= 0; i--) {
541 for (j=0; j < 32*18/8; j++) {
542 bitmap [i][j] ^= color_mask;
543 }
544 }
545 }
546
547 }
548
549 /*
550 We've read the entire file. Now close the input file pointer.
551 */
552 fclose (infp);
553 /*
554 We now have the header portion in the header[] array,
555 and have the bitmap portion from top-to-bottom in the bitmap[] array.
556 */
557 /*
558 If no Unicode range (U+nnnnnn00 through U+nnnnnnFF) was specified
559 with a -p parameter, determine the range from the digits in the
560 bitmap itself.
561
562 Store bitmaps for the hex digit patterns that this file uses.
563 */
564 if (!planeset) { /* If Unicode range not specified with -p parameter */
565 for (i = 0x0; i <= 0xF; i++) { /* hex digit pattern we're storing */
566 for (j = 0; j < 4; j++) {
567 hexdigit[i][j] =
568 ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 ][6] << 24 ) |
569 ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 1][6] << 16 ) |
570 ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 2][6] << 8 ) |
571 ((unsigned)bitmap[32 * (i+1) + 4 * j + 8 + 3][6] );
572 }
573 }
574 /*
575 Read the Unicode plane digits into arrays for comparison, to
576 determine the upper four hex digits of the glyph addresses.
577 */
578 for (i = 0; i < 4; i++) {
579 for (j = 0; j < 4; j++) {
580 unidigit[i][j] =
581 ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 1][i + 3] << 24 ) |
582 ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 2][i + 3] << 16 ) |
583 ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 3][i + 3] << 8 ) |
584 ((unsigned)bitmap[32 * 0 + 4 * j + 8 + 4][i + 3] );
585 }
586 }
587
588 tmpsum = 0;
589 for (i = 4; i < 6; i++) {
590 for (j = 0; j < 4; j++) {
591 unidigit[i][j] =
592 ((unsigned)bitmap[32 * 1 + 4 * j + 8 ][i] << 24 ) |
593 ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 1][i] << 16 ) |
594 ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 2][i] << 8 ) |
595 ((unsigned)bitmap[32 * 1 + 4 * j + 8 + 3][i] );
596 tmpsum |= unidigit[i][j];
597 }
598 }
599 if (tmpsum == 0) { /* the glyph matrix is transposed */
600 flip = 1; /* note transposed order for processing glyphs in matrix */
601 /*
602 Get 5th and 6th hex digits by shifting first column header left by
603 1.5 columns, thereby shifting the hex digit right after the leading
604 "U+nnnn" page number.
605 */
606 for (i = 0x08; i < 0x18; i++) {
607 bitmap[i][7] = (bitmap[i][8] << 4) | ((bitmap[i][ 9] >> 4) & 0xf);
608 bitmap[i][8] = (bitmap[i][9] << 4) | ((bitmap[i][10] >> 4) & 0xf);
609 }
610 for (i = 4; i < 6; i++) {
611 for (j = 0; j < 4; j++) {
612 unidigit[i][j] =
613 ((unsigned)bitmap[4 * j + 8 + 1][i + 3] << 24 ) |
614 ((unsigned)bitmap[4 * j + 8 + 2][i + 3] << 16 ) |
615 ((unsigned)bitmap[4 * j + 8 + 3][i + 3] << 8 ) |
616 ((unsigned)bitmap[4 * j + 8 + 4][i + 3] );
617 }
618 }
619 }
620
621 /*
622 Now determine the Unicode plane by comparing unidigit[0..5] to
623 the hexdigit[0x0..0xF] array.
624 */
625 uniplane = 0;
626 for (i=0; i<6; i++) { /* go through one bitmap digit at a time */
627 match = 0; /* haven't found pattern yet */
628 for (j = 0x0; !match && j <= 0xF; j++) {
629 if (unidigit[i][0] == hexdigit[j][0] &&
630 unidigit[i][1] == hexdigit[j][1] &&
631 unidigit[i][2] == hexdigit[j][2] &&
632 unidigit[i][3] == hexdigit[j][3]) { /* we found the digit */
633 uniplane |= j;
634 match = 1;
635 }
636 }
637 uniplane <<= 4;
638 }
639 uniplane >>= 4;
640 }
641 /*
642 Now read each glyph and print it as hex.
643 */
644 for (i = 0x0; i <= 0xf; i++) {
645 for (j = 0x0; j <= 0xf; j++) {
646 for (k = 0; k < 16; k++) {
647 if (flip) { /* transpose glyph matrix */
648 thischar0[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) ];
649 thischar1[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) + 1];
650 thischar2[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) + 2];
651 thischar3[k] = bitmap[32*(j+1) + k + 7][4 * (i+2) + 3];
652 }
653 else {
654 thischar0[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) ];
655 thischar1[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) + 1];
656 thischar2[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) + 2];
657 thischar3[k] = bitmap[32*(i+1) + k + 7][4 * (j+2) + 3];
658 }
659 }
660 /*
661 If the second half of the 16*16 character is all zeroes, this
662 character is only 8 bits wide, so print a half-width character.
663 */
664 empty1 = empty2 = 1;
665 for (k=0; (empty1 || empty2) && k < 16; k++) {
666 if (thischar1[k] != 0) empty1 = 0;
667 if (thischar2[k] != 0) empty2 = 0;
668 }
669 /*
670 Only print this glyph if it isn't blank.
671 */
672 if (!empty1 || !empty2) {
673 /*
674 If the second half is empty, this is a half-width character.
675 Only print the first half.
676 */
677 /*
678 Original GNU Unifont format is four hexadecimal digit character
679 code followed by a colon followed by a hex string. Add support
680 for codes beyond the Basic Multilingual Plane.
681
682 Unicode ranges from U+0000 to U+10FFFF, so print either a
683 4-digit or a 6-digit code point. Note that this software
684 should support up to an 8-digit code point, extending beyond
685 the normal Unicode range, but this has not been fully tested.
686 */
687 if (uniplane > 0xff)
688 fprintf (outfp, "%04X%X%X:", uniplane, i, j); // 6 digit code pt.
689 else
690 fprintf (outfp, "%02X%X%X:", uniplane, i, j); // 4 digit code pt.
691 for (thisrow=0; thisrow<16; thisrow++) {
692 /*
693 If second half is empty and we're not forcing this
694 code point to double width, print as single width.
695 */
696 if (!forcewide &&
697 empty2 && !wide[(uniplane << 8) | (i << 4) | j]) {
698 fprintf (outfp,
699 "%02X",
700 thischar1[thisrow]);
701 }
702 else if (wide[(uniplane << 8) | (i << 4) | j] == 4) {
703 /* quadruple-width; force 32nd pixel to zero */
704 fprintf (outfp,
705 "%02X%02X%02X%02X",
706 thischar0[thisrow], thischar1[thisrow],
707 thischar2[thisrow], thischar3[thisrow] & 0xFE);
708 }
709 else { /* treat as double-width */
710 fprintf (outfp,
711 "%02X%02X",
712 thischar1[thisrow], thischar2[thisrow]);
713 }
714 }
715 fprintf (outfp, "\n");
716 }
717 }
718 }
719 exit (0);
720}
main
int main(int argc, char *argv[])
The main function.
Definition: unibmp2hex.c:149
planeset
unsigned planeset
=1: use plane specified with -p parameter
Definition: unibmp2hex.c:110
unidigit
unsigned unidigit[6][4]
Definition: unibmp2hex.c:115
uniplane
unsigned uniplane
Unicode plane number, 0..0xff ff ff.
Definition: unibmp2hex.c:109
forcewide
unsigned forcewide
=1 to set each glyph to 16 pixels wide
Definition: unibmp2hex.c:112
bmp_header
struct @0 bmp_header
hexdigit
unsigned hexdigit[16][4]
32 bit representation of 16x8 0..F bitmap
Definition: unibmp2hex.c:107
color_table
unsigned char color_table[256][4]
Definition: unibmp2hex.c:137
MAXBUF
#define MAXBUF
Maximum input file line length - 1.
Definition: unibmp2hex.c:104
flip
unsigned flip
=1 if we're transposing glyph matrix
Definition: unibmp2hex.c:111