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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
|
/*
* Copyright (C) 2020 Mikhail Burakov. This file is part of Pui.
*
* Pui is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pui is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Pui. If not, see <https://www.gnu.org/licenses/>.
*/
#include <ctype.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <err.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <unistd.h>
enum {
kNoSection = 0,
kSectionElements,
kSectionDefaultPalette,
kSectionActivePalette
};
struct PuiElement {
uint8_t x, y, w, h;
};
struct MappedFile {
void* data;
size_t size;
};
struct BitmapHeader {
uint16_t magic;
uint32_t size;
uint16_t reserved[2];
uint32_t offset;
} __attribute__((packed));
struct DibHeader {
uint32_t size;
int32_t width;
int32_t height;
uint16_t planes;
uint16_t bpp;
} __attribute__((packed));
struct Bitmap {
struct MappedFile file;
struct BitmapHeader* bitmap_header;
struct DibHeader* dib_header;
uint8_t* ptr;
int index;
};
static void MapFile(const char* file, struct MappedFile* result) {
int fd = open(file, O_RDONLY);
if (fd == -1) err(1, "Failed to open file");
struct stat buf;
if (fstat(fd, &buf)) err(1, "Failed to stat file");
void* data = mmap(NULL, (size_t)buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
close(fd);
if (data == MAP_FAILED) err(1, "Failed to map file");
result->data = data;
result->size = (size_t)buf.st_size;
}
static void UnmapFile(struct MappedFile* result) {
munmap(result->data, result->size);
}
static int ReadBitmap(struct Bitmap* bitmap) {
uint8_t* buffer = bitmap->file.data;
if (bitmap->ptr < buffer + bitmap->bitmap_header->offset) return -1;
int result = bitmap->ptr[bitmap->index >> 1] >> ((~bitmap->index & 1) << 2);
if (++bitmap->index == bitmap->dib_header->width) {
size_t stride = ((size_t)(bitmap->dib_header->width >> 1) + 3) & ~3u;
bitmap->ptr -= stride;
bitmap->index = 0;
}
return result & 0x7;
}
static void OpenBitmap(const char* fname, struct Bitmap* bitmap) {
MapFile(fname, &bitmap->file);
uint8_t* data = bitmap->file.data;
bitmap->bitmap_header = (struct BitmapHeader*)data;
bitmap->dib_header = (struct DibHeader*)(data + sizeof(struct BitmapHeader));
if (bitmap->bitmap_header->magic != 0x4d42) err(1, "Invalid bitmap magic");
if (bitmap->dib_header->width < 0 || bitmap->dib_header->height < 0 ||
bitmap->dib_header->width > UINT16_MAX ||
bitmap->dib_header->height > UINT16_MAX ||
bitmap->dib_header->planes != 1 || bitmap->dib_header->bpp != 4)
err(1, "Unsupported bitmap format");
size_t stride = ((size_t)(bitmap->dib_header->width >> 1) + 3) & ~3u;
bitmap->ptr = data + bitmap->bitmap_header->offset +
stride * (size_t)(bitmap->dib_header->height - 1);
}
static void Write(int item, FILE* out) {
static int current = -1;
if (current == -1) {
if (item == -1) return;
current = (item & 0xf) << 4;
} else {
if (item == -1) item = 0;
current |= item & 0xf;
fputc(current, out);
current = -1;
}
}
static void WriteCounter(int counter, FILE* out) {
int pos = 0;
while (counter >> (++pos * 3))
;
while (pos-- > 0) Write(0x8 | counter >> (pos * 3), out);
}
static void WriteSequence(int prev, int counter, FILE* out) {
switch (counter) {
case 1:
Write(prev, out);
break;
case 2:
Write(prev, out);
Write(prev, out);
break;
default:
WriteCounter(counter - 2, out);
Write(prev, out);
break;
}
}
static void SelectScale(int32_t size, uint8_t* value, uint8_t* scale) {
if (size < 0) err(1, "Size must be non-negative");
for (int shift = 0, mask = 0;; shift++, mask = mask << 1 | 1) {
if (size & mask) err(1, "Size is not aligned");
if (size < 0x100 << shift) {
*value = (uint8_t)(size >> shift);
*scale = (uint8_t)shift;
return;
}
}
}
static int IsMeaningless(int ch) { return !isgraph(ch); }
static int IsMeaningful(int ch) { return isprint(ch); }
static void SkipGroup(struct MappedFile* file, int (*pred)(int ch)) {
char* ptr = file->data;
size_t size = file->size;
for (; size && pred(*ptr); ptr++, size--)
;
file->data = ptr;
file->size = size;
}
static int ChangeSection(struct MappedFile* file) {
char* begin = file->data;
SkipGroup(file, IsMeaningful);
size_t size = (size_t)((char*)file->data - begin);
static const char kElements[] = {'[', 'E', 'l', 'e', 'm',
'e', 'n', 't', 's', ']'};
static const char kDefaultPalette[] = {'[', 'D', 'e', 'f', 'a', 'u',
'l', 't', 'P', 'a', 'l', 'e',
't', 't', 'e', ']'};
static const char kActivePalette[] = {'[', 'A', 'c', 't', 'i', 'v', 'e', 'P',
'a', 'l', 'e', 't', 't', 'e', ']'};
switch (size) {
case sizeof(kElements):
if (!memcmp(begin, kElements, sizeof(kElements))) return kSectionElements;
break;
case sizeof(kDefaultPalette):
if (!memcmp(begin, kDefaultPalette, sizeof(kDefaultPalette)))
return kSectionDefaultPalette;
break;
case sizeof(kActivePalette):
if (!memcmp(begin, kActivePalette, sizeof(kActivePalette)))
return kSectionActivePalette;
break;
default:
break;
}
return kNoSection;
}
static int GetIndex(char* ptr, size_t size, const char* prefix,
size_t prefix_length) {
if (size < prefix_length + 3 || memcmp(ptr, prefix, prefix_length) ||
!isdigit(ptr[prefix_length]) || !isdigit(ptr[prefix_length + 1]) ||
ptr[prefix_length + 2] != '=')
return -1;
return atoi(ptr + prefix_length);
}
static int ParseElement(struct MappedFile* file, uint8_t scale[2],
struct PuiElement* elements) {
char* ptr = file->data;
SkipGroup(file, IsMeaningful);
size_t size = (size_t)((char*)file->data - ptr);
static const char kElement[] = {'E', 'l', 'e', 'm', 'e', 'n', 't'};
int index = GetIndex(ptr, size, kElement, sizeof(kElement));
if (index == -1 || index > 14) return -1;
ptr += sizeof(kElement) + 3;
size -= sizeof(kElement) + 3;
int values[] = {0, 0, 0, 0};
int *value = values, *end = values + 4;
for (; size; ptr++, size--) {
if ('0' <= *ptr && *ptr <= '9') {
*value = *value * 10 + *ptr - '0';
} else if (*ptr == ',') {
if (++value == end) break;
} else {
break;
}
}
elements[index].x = (uint8_t)(values[0] >> scale[0]);
elements[index].y = (uint8_t)(values[1] >> scale[1]);
elements[index].w = (uint8_t)(values[2] >> scale[0]);
elements[index].h = (uint8_t)(values[3] >> scale[1]);
return index;
}
static int ParsePalette(struct MappedFile* file, uint32_t* palette) {
char* ptr = file->data;
SkipGroup(file, IsMeaningful);
size_t size = (size_t)((char*)file->data - ptr);
static const char kColor[] = {'C', 'o', 'l', 'o', 'r'};
int index = GetIndex(ptr, size, kColor, sizeof(kColor));
if (index == -1 || index > 7) return -1;
ptr += sizeof(kColor) + 3;
size -= sizeof(kColor) + 3;
uint32_t value = 0;
for (; size; ptr++, size--) {
if ('0' <= *ptr && *ptr <= '9') {
value = value << 4 | (unsigned)(*ptr - '0');
} else if ('A' <= *ptr && *ptr <= 'F') {
value = value << 4 | (unsigned)(*ptr - 'A' + 10);
} else if ('a' <= *ptr && *ptr <= 'f') {
value = value << 4 | (unsigned)(*ptr - 'a' + 10);
} else {
break;
}
}
palette[index] = (value >> 24 & 0xff) | (value >> 8 & 0xff00) |
(value << 8 & 0xff0000) | (value << 24 & 0xff000000);
return index;
}
static void CompressDefinition(const struct MappedFile* definition,
uint8_t scale[2], FILE* out) {
struct PuiElement elements[0xf];
memset(elements, 0, sizeof(elements));
uint32_t palettes[2][8];
memset(palettes, 0, sizeof(palettes));
int section = kNoSection;
int max_element = 0, max_color = 0;
for (struct MappedFile def = *definition;;) {
SkipGroup(&def, IsMeaningless);
if (!def.size) break;
switch (*(char*)def.data) {
case '#':
SkipGroup(&def, IsMeaningful);
continue;
case '[':
section = ChangeSection(&def);
continue;
default:
break;
}
switch (section) {
case kSectionElements: {
int index = ParseElement(&def, scale, elements);
if (index > max_element) max_element = index;
break;
}
case kSectionDefaultPalette: {
int index = ParsePalette(&def, palettes[0]);
if (index > max_color) max_color = index;
break;
}
case kSectionActivePalette: {
int index = ParsePalette(&def, palettes[1]);
if (index > max_color) max_color = index;
break;
}
default:
SkipGroup(&def, IsMeaningful);
break;
}
}
int count_elements = max_element + 1;
int count_colors = max_color + 1;
uint8_t counter = (count_elements << 4 & 0xf0) | (count_colors & 0xf);
fwrite(&counter, 1, 1, out);
fwrite(elements, (size_t)count_elements * sizeof(struct PuiElement), 1, out);
fwrite(palettes[0], (size_t)count_colors * sizeof(uint32_t), 1, out);
fwrite(palettes[1], (size_t)count_colors * sizeof(uint32_t), 1, out);
}
int main(int argc, char** argv) {
if (argc < 3) err(1, "Usage: %s [source.bmp] [definition.ini]", argv[0]);
struct Bitmap bitmap;
memset(&bitmap, 0, sizeof(bitmap));
OpenBitmap(argv[1], &bitmap);
uint8_t whs[3], scale[2];
SelectScale(bitmap.dib_header->width, &whs[0], &scale[0]);
SelectScale(bitmap.dib_header->height, &whs[1], &scale[1]);
struct MappedFile definition;
MapFile(argv[2], &definition);
whs[2] = (uint8_t)(scale[0] << 4 | scale[1]);
fwrite(whs, sizeof(whs), 1, stdout);
CompressDefinition(&definition, scale, stdout);
UnmapFile(&definition);
for (int counter = 0, prev = -1;;) {
int current = ReadBitmap(&bitmap);
if (current == prev || prev == -1) {
counter++;
} else {
WriteSequence(prev, counter, stdout);
if (current == -1) break;
counter = 1;
}
prev = current;
}
UnmapFile(&bitmap.file);
Write(-1, stdout);
return 0;
}
|