-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkbiosfont.c
231 lines (192 loc) · 5.83 KB
/
mkbiosfont.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if __STDC_VERSION__ < 199901L
#define uint32_t unsigned long
#define int32_t long
#else
#include <stdint.h>
#endif
#define null ((void *)0)
#define GET_LONG(f) (((uint32_t)fgetc(f)) | ((uint32_t)fgetc(f)) << 8 | ((uint32_t)fgetc(f)) << 16 | ((uint32_t)fgetc(f)) << 24)
#define MAX(n,m) ((n > m) ? n : m)
FILE *in_file = null;
FILE *out_file = null;
char filename_buffer[256];
struct CONVERSION_PARAMS
{
uint32_t data_offset;
int32_t bmp_width, bmp_height;
int slice_width, slice_height;
} params;
void print_version_info()
{
printf("\nMKBIOSFONT ver. 0.5\n");
printf("Copyright (c) 2024 by Tobiasz Stamborski\n");
}
void print_usage_info()
{
printf("\nUsage: mkbiosfont font_size bmp_file\n");
printf("Output: bmp_file.dat with valid 1bpp BIOS font\n");
printf("Details: bmp_file should be a bmp file in indexed color mode.\n");
printf(" font_size is size of a single character in pixels.\n");
printf(" Valid font_size should be \"8x8\", \"8x14\" or \"8x16\".\n");
}
void print_info()
{
print_version_info();
print_usage_info();
printf("\n");
}
void print_error(char *description)
{
printf("\nError: %s\n", description);
print_usage_info();
printf("\n");
}
int is_microsoft_bmp(FILE *file)
{
fseek(file, 0, SEEK_SET);
if (fgetc(file) != 'B')
return 0;
if (fgetc(file) != 'M')
return 0;
return 1;
}
int is_uncompressed_bmp(FILE *file)
{
fseek(file, 0x1e, SEEK_SET);
return fgetc(file) == 0 ? 1 : 0;
}
int is_indexed_bmp(FILE *file)
{
fseek(file, 0x1c, SEEK_SET);
return fgetc(file) == 8 ? 1 : 0;
}
int is_valid_font_size(int fontw, int fonth)
{
if (fontw == 8 && (fonth == 8 || fonth == 14 || fonth == 16))
return 1;
else
return 0;
}
void read_bmp_params(FILE *file, struct CONVERSION_PARAMS *params)
{
fseek(file, 0x0a, SEEK_SET);
params->data_offset = GET_LONG(file);
fseek(file, 0x12, SEEK_SET);
params->bmp_width = GET_LONG(file);
params->bmp_height = GET_LONG(file);
}
void err_exit(char *error_desc)
{
print_error(error_desc);
if (in_file != null)
fclose(in_file);
if (out_file != null)
fclose(out_file);
exit(-1);
}
void convert(FILE *input, FILE *output, struct CONVERSION_PARAMS *params)
{
int byte, mask;
int i, j, x, y;
int cx_slice = params->bmp_width / params->slice_width;
int cy_slice = params->bmp_height / params->slice_height;
int slice_offset = params->slice_height*params->bmp_width;
for (i = cy_slice - 1; i >= 0; i--)
{
for (j = 0; j < cx_slice; j++)
{
/* na poczatek ostatniej linii danego slice */
fseek(input, params->data_offset + (i+1)*slice_offset + j*params->slice_width - params->bmp_width, SEEK_SET);
for (y = 0; y < params->slice_height; y++)
{
byte = 0;
mask = 0x80;
for (x = 0; x < params->slice_width; x++)
{
if (fgetc(input) != 0)
byte |= mask >> x;
}
fputc(byte, output);
fseek(input, -(params->bmp_width + params->slice_width), SEEK_CUR); /* na poczatek poprzedniej linii */
}
if ((j+1)*(cy_slice-i) >= 256) /* charset nie powinien byc wiekszy niz 256 znakow */
return;
}
}
}
int last_index_of(char *string, char to_find) {
int i, index;
index = -1;
for (i = 0; i < strlen(string); i++)
if (filename_buffer[i] == to_find)
index = i;
return index;
}
char *get_out_filename(char *in_filename)
{
int len;
int ext_index, sep_index;
if (strlen(in_filename) < 252)
{
strcpy(filename_buffer, in_filename);
ext_index = last_index_of(filename_buffer, '.');
sep_index = MAX(
last_index_of(filename_buffer, '\\'),
last_index_of(filename_buffer, '/')
);
if (ext_index > 0 && ext_index > sep_index)
{
filename_buffer[ext_index] = '.';
filename_buffer[ext_index+1] = 'd';
filename_buffer[ext_index+2] = 'a';
filename_buffer[ext_index+3] = 't';
filename_buffer[ext_index+4] = '\0';
}
else
{
len = strlen(filename_buffer);
filename_buffer[len] = '.';
filename_buffer[len+1] = 'd';
filename_buffer[len+2] = 'a';
filename_buffer[len+3] = 't';
filename_buffer[len+4] = '\0';
}
}
else
{
strcpy(filename_buffer, "output.dat");
}
return filename_buffer;
}
int main(int argc, char **argv)
{
if (argc != 3)
{
print_info();
return 0;
}
in_file = fopen(argv[argc-1], "r");
if (in_file == NULL)
{
print_error("Cannot open given file!");
return -1;
}
if (!is_microsoft_bmp(in_file))
err_exit("This file is not a Microsoft BMP!");
if (!is_uncompressed_bmp(in_file))
err_exit("This is an unsupported compressed bitmap!");
if (!is_indexed_bmp(in_file))
err_exit("This bitmap is not in the indexed color mode!");
read_bmp_params(in_file, ¶ms);
sscanf(argv[1], "%dx%d", ¶ms.slice_width, ¶ms.slice_height);
if (!is_valid_font_size(params.slice_width, params.slice_height))
err_exit("Invalid font size! Possible choices are 8x8, 8x14 or 8x16!");
out_file = fopen(get_out_filename(argv[argc-1]), "w");
convert(in_file, out_file, ¶ms);
fclose(in_file);
fclose(out_file);
return 0;
}