-
Notifications
You must be signed in to change notification settings - Fork 1
/
rle-genops.c
484 lines (404 loc) · 10.8 KB
/
rle-genops.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
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
/*
Run-Length Encoding & Decoding Table Generator
Copyright (c) 2022, Eddy L O Jansson. Licensed under The MIT License.
See https://github.com/eloj/rle-zoo
TODO:
Use bitmap to mark off used RLE_OPS, then bit ops can detect
missing or ambigious encodings.
Output necessary types or include-file when --genc
*/
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <limits.h>
#define UTILITY_IMPLEMENTATION
#include "utility.h"
#define RLE_PARSE_IMPLEMENTATION
#include "rle-parse.h"
#include "build_const.h"
typedef struct rle8 (*rle8_decode_fp)(uint8_t input);
typedef struct rle8 (*rle8_encode_fp)(struct rle8 cmd);
static int opt_usage, opt_genc;
static const char *gen_header = "// Generated by rle-genops from https://github.com/eloj/rle-zoo\n";
struct rle_parser {
const char *name;
rle8_encode_fp rle8_encode;
rle8_decode_fp rle8_decode;
};
static struct rle8 rle8_decode_packbits(uint8_t input) {
struct rle8 cmd;
if (input > 0x80) {
cmd.op = RLE_OP_REP;
cmd.cnt = 1 - (int8_t)input;
} else if (input < 0x80) {
cmd.op = RLE_OP_CPY;
cmd.cnt = input + 1;
} else /* if (input == 0x80) */ {
assert(input == 0x80);
cmd.op = RLE_OP_NOP;
cmd.cnt = 1;
}
assert(
((cmd.op == RLE_OP_REP) && (cmd.cnt <= 128 && cmd.cnt >= 2))
||
((cmd.op == RLE_OP_CPY) && (cmd.cnt <= 128 && cmd.cnt >= 1))
||
((cmd.op == RLE_OP_NOP) && (cmd.cnt == 1))
);
return cmd;
}
static struct rle8 rle8_encode_packbits(struct rle8 cmd) {
struct rle8 res = { RLE_OP_INVALID, 0 };
if (cmd.op == RLE_OP_REP) {
if (cmd.cnt >= 2 && cmd.cnt <= 128) {
res.op = RLE_OP_REP;
res.cnt = 257 - cmd.cnt; // 1 - (int8_t)cmd.cnt;
}
} else if (cmd.op == RLE_OP_CPY) {
if (cmd.cnt >= 1 && cmd.cnt <= 128) {
res.op = RLE_OP_CPY;
res.cnt = cmd.cnt - 1;
}
} else if (cmd.op == RLE_OP_NOP) {
res.cnt = 0x80;
}
return res;
}
static struct rle8 rle8_decode_goldbox(uint8_t input) {
struct rle8 cmd;
if (input > 0x80) {
cmd.op = RLE_OP_REP;
cmd.cnt = (~input) + 1;
} else if (input < 0x7e) {
cmd.op = RLE_OP_CPY;
cmd.cnt = input + 1;
} else {
cmd.op = RLE_OP_INVALID;
cmd.cnt = 0;
}
assert(
((cmd.op == RLE_OP_REP) && (cmd.cnt <= 127 && cmd.cnt > 0))
||
((cmd.op == RLE_OP_CPY) && (cmd.cnt <= 126 && cmd.cnt > 0))
||
((cmd.op == RLE_OP_INVALID && cmd.cnt == 0))
);
return cmd;
}
static struct rle8 rle8_encode_goldbox(struct rle8 cmd) {
struct rle8 res = { RLE_OP_INVALID, 0 };
if (cmd.op == RLE_OP_REP) {
if (cmd.cnt >= 1 && cmd.cnt <= 127) {
res.op = RLE_OP_REP;
res.cnt = 1 + (~cmd.cnt);
}
} else if (cmd.op == RLE_OP_CPY) {
if (cmd.cnt >= 1 && cmd.cnt <= 126) {
res.op = RLE_OP_CPY;
res.cnt = cmd.cnt - 1;
}
} // else if (cmd.op == RLE_OP_INVALID) res.cnt = 0x80; // XXX: Not sure why we'd want this. Add it as NOP in that case.
return res;
}
static struct rle8 rle8_decode_pcx(uint8_t input) {
struct rle8 cmd;
if ((input & 0xC0) == 0xC0) {
cmd.op = RLE_OP_REP;
cmd.cnt = input & 0x3F;
} else {
cmd.op = RLE_OP_LIT;
cmd.cnt = input;
}
assert(
((cmd.op == RLE_OP_LIT) && (cmd.cnt <= 191 && cmd.cnt >= 0))
||
((cmd.op == RLE_OP_REP) && (cmd.cnt <= 63 && cmd.cnt >= 0))
);
return cmd;
}
static struct rle8 rle8_encode_pcx(struct rle8 cmd) {
struct rle8 res = { RLE_OP_INVALID, 0 };
if (cmd.op == RLE_OP_REP) {
if (cmd.cnt <= 63) {
res.op = RLE_OP_REP;
res.cnt = 0xC0 | cmd.cnt;
}
} else if (cmd.op == RLE_OP_LIT) {
if (cmd.cnt <= 191) {
res.op = RLE_OP_LIT;
res.cnt = cmd.cnt;
}
}
return res;
}
static struct rle8 rle8_decode_icns(uint8_t input) {
struct rle8 cmd;
if (input & 0x80) {
cmd.op = RLE_OP_REP;
cmd.cnt = (input & 0x7F) + 3; // 3-130
} else {
cmd.op = RLE_OP_CPY;
cmd.cnt = input + 1; // 1-128
}
assert(
((cmd.op == RLE_OP_REP) && (cmd.cnt <= 130 && cmd.cnt >= 3))
||
((cmd.op == RLE_OP_CPY) && (cmd.cnt <= 128 && cmd.cnt >= 1))
);
return cmd;
}
static struct rle8 rle8_encode_icns(struct rle8 cmd) {
struct rle8 res = { RLE_OP_INVALID, 0 };
if (cmd.op == RLE_OP_REP && (cmd.cnt >= 3 && cmd.cnt <= 130)) {
res.op = RLE_OP_REP;
res.cnt = cmd.cnt + 125;
} else if (cmd.op == RLE_OP_CPY && (cmd.cnt >= 1 && cmd.cnt <= 128)) {
res.op = RLE_OP_CPY;
res.cnt = cmd.cnt - 1;
}
return res;
}
static int rle8_display_ops(struct rle_parser *p) {
printf("// Automatically generated code table for RLE8 variant '%s'\n", p->name);
printf("%s", gen_header);
for (int i=0 ; i < 256 ; ++i) {
uint8_t b = i;
struct rle8 cmd = p->rle8_decode(b);
if (cmd.op != RLE_OP_INVALID) {
struct rle8 b_recode = p->rle8_encode(cmd);
printf("0x%02x (%d/%d) => %s %d\n", b, b, (int8_t)b, rle_op_cstr(cmd.op), cmd.cnt);
if (b != b_recode.cnt) {
printf("ERROR: reencode mismatch: %s %d => 0x%02x\n", rle_op_cstr(cmd.op), cmd.cnt, b_recode.cnt);
return 1;
}
} else {
printf("0x%02x (%d/%d) => %s\n", b, b, (int8_t)b, rle_op_cstr(cmd.op));
}
}
return 0;
}
static void rle8_generate_decode_table(struct rle_parser *p) {
printf("\n// Decode table for RLE8 variant '%s'\n", p->name);
printf("static struct rle8 rle8_tbl_decode_%s[256] = {\n", p->name);
for (int i=0 ; i < 256 ; ++i) {
uint8_t b = i;
struct rle8 cmd = p->rle8_decode(b);
printf(" /* %02X */ { RLE_OP_%s, %3d }", b, rle_op_cstr(cmd.op), cmd.cnt);
if (i < 255) printf(",");
if ((i < 255) && ((i+1) % 4) == 0) printf("\n");
}
printf("\n};\n");
}
static size_t rle8_generate_op_encode_array(struct rle_parser *p, int OP, char *buf, size_t bufsize) {
// TODO: This should be autodetected by max of valid CPY/REP/LIT encoding, rest padded to -1
int max_len = 256;
size_t wp = 0;
int tr = 0;
buf_printf(buf, bufsize, &wp, &tr, "\t\t// RLE_OP_%s 0..%d\n", rle_op_cstr(OP), max_len - 1);
buf_printf(buf, bufsize, &wp, &tr, "\t\t(int16_t[]){ ");
for (int i=0 ; i < max_len ; ++i) {
if (i > 0)
buf_printf(buf, bufsize, &wp, &tr, ",");
struct rle8 cmd = { OP, i };
struct rle8 code = p->rle8_encode(cmd);
if (code.op != RLE_OP_INVALID)
buf_printf(buf, bufsize, &wp, &tr, "0x%02x", code.cnt);
else
buf_printf(buf, bufsize, &wp, &tr, "-1");
}
buf_printf(buf, bufsize, &wp, &tr, " },");
if (tr) {
fprintf(stderr, "INTERNAL ERROR: Buffer truncation detected -- output invalid!\n");
exit(1);
}
return wp;
}
static void rle8_generate_encode_table(struct rle_parser *p) {
// TODO: This should be autodetected by max of valid CPY/REP/LIT encoding, rest padded to -1
// int max_len = 256;
int minmax[3][2] = { { INT_MAX, INT_MIN }, { INT_MAX, INT_MIN }, { INT_MAX, INT_MIN } };
int op_usage[RLE_OP_INVALID + 1] = { 0 };
// Determine REP and CPY limits:
for (int i=0 ; i < 256 ; ++i) {
uint8_t b = i;
struct rle8 cmd = p->rle8_decode(b);
switch (cmd.op) {
case RLE_OP_CPY:
case RLE_OP_REP:
case RLE_OP_LIT:
if (cmd.cnt < minmax[cmd.op][0])
minmax[cmd.op][0] = cmd.cnt;
if (cmd.cnt > minmax[cmd.op][1])
minmax[cmd.op][1] = cmd.cnt;
break;
case RLE_OP_NOP:
case RLE_OP_INVALID:
/* NOP */
break;
}
op_usage[cmd.op]++;
}
char buf[1024+256] = {};
size_t wp = 0;
for (int i = RLE_OP_CPY ; i < RLE_OP_INVALID ; ++i) {
if (op_usage[i] > 0) {
buf_printf(buf, sizeof(buf), &wp, NULL, "%s(1U << RLE_OP_%s) /* %d */", wp == 0 ? "" : " | ", rle_op_cstr(i), op_usage[i]);
}
}
printf("\nstatic struct rle8_tbl rle8_table_%s = {\n", p->name);
printf("\t\"%s\",\n", p->name);
printf("\t%s,\n", buf); // enum RLE_OP op_used;
// SKIP FOR NOW printf("\t%d,\n", max_len);
printf("\t{\n");
for (int i = RLE_OP_CPY ; i < RLE_OP_NOP ; ++i) {
if (op_usage[i] > 0) {
rle8_generate_op_encode_array(p, i, buf, sizeof(buf));
printf("%s\n", buf);
} else {
printf("\t\tNULL,\n");
}
}
printf("\t},\n");
printf("\trle8_tbl_decode_%s,\n", p->name);
printf("\t{\n");
for (int i = RLE_OP_CPY ; i < RLE_OP_NOP ; ++i) {
if (op_usage[i] > 0) {
printf("\t\t{ %d, %d }, // min-max %s\n", minmax[i][0], minmax[i][1], rle_op_cstr(i));
} else {
printf("\t\t{ -1, -1 }, // no %s\n", rle_op_cstr(i));
}
}
printf("\t}\n};\n");
};
static void rle8_generate_c_tables(struct rle_parser *p) {
printf("%s", gen_header);
rle8_generate_decode_table(p);
rle8_generate_encode_table(p);
}
struct rle_parser parsers[] = {
{
"goldbox",
rle8_encode_goldbox,
rle8_decode_goldbox
},
{
"packbits",
rle8_encode_packbits,
rle8_decode_packbits
},
{
"pcx",
rle8_encode_pcx,
rle8_decode_pcx
},
{
"icns",
rle8_encode_icns,
rle8_decode_icns
}
};
static const size_t NUM_VARIANTS = sizeof(parsers)/sizeof(parsers[0]);
static struct rle_parser* get_parser_by_name(const char *name) {
for (size_t i = 0 ; name && i < NUM_VARIANTS ; ++i) {
if (strcmp(name, parsers[i].name) == 0) {
return &parsers[i];
}
}
return NULL;
}
static void print_banner(void) {
printf("rle-genops %s <%.*s>\n", build_version, 8, build_hash);
}
static void print_variants(void) {
printf("\nAvailable variants:\n");
struct rle_parser *p = parsers;
for (size_t i = 0 ; i < NUM_VARIANTS ; ++i) {
printf(" %s\n", p->name);
++p;
}
}
static void usage(const char *argv) {
print_banner();
printf("%s [OPTION] <variant>\n\n", argv);
printf("Options:\n");
printf(" --genc - Generate C tables.\n");
print_variants();
}
static int parse_args(int *argc, char ***argv) {
const char *arg = NULL;
char **l_argv = *argv + 1;
int l_argc = *argc - 1;
int retval = 0;
while ((arg = *l_argv) != NULL) {
// "argv[argc] shall be a null pointer", section 5.1.2.2.1
// const char *value = *l_argv;
if (arg[0] == '-') {
++l_argv;
--l_argc;
++arg;
if (arg[0] == '-') {
// long option
++arg;
if (strcmp(arg, "version") == 0) {
print_banner();
exit(0);
} else if (strcmp(arg, "help") == 0) {
opt_usage = 1;
} else if (strcmp(arg, "genc") == 0) {
opt_genc = 1;
} else {
retval = 1;
break;
}
} else {
// short option
if (strcmp(arg, "v") == 0 || strcmp(arg, "V") == 0) {
print_banner();
exit(0);
} else if (strcmp(arg, "h") == 0) {
opt_usage = 1;
} else {
retval = 1;
break;
}
}
} else {
break;
}
}
if (retval != 0) {
fprintf(stderr, "%s: unrecognized option: --%s\n", *argv[0], arg);
fprintf(stderr, "Try '%s --help' for more information.\n", *argv[0]);
}
*argc = l_argc;
*argv = l_argv;
return retval;
}
int main(int argc, char *argv[]) {
char **org_argv = argv;
if (parse_args(&argc, &argv) != 0) {
return 1;
}
if (opt_usage) {
usage(org_argv[0]);
return 1;
}
char *variant = argv[0];
struct rle_parser *p = argc > 0 ? get_parser_by_name(variant) : NULL;
if (!p) {
if (variant) {
fprintf(stderr, "error: Unknown variant '%s'\n", variant);
print_variants();
} else {
usage(org_argv[0]);
}
return 2;
}
if (opt_genc)
rle8_generate_c_tables(p);
else
rle8_display_ops(p);
return EXIT_SUCCESS;
}