GNU Unifont: src/unibmp2hex.c Source File

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