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