-
Notifications
You must be signed in to change notification settings - Fork 142
Expand file tree
/
Copy pathpnm2text.c
More file actions
494 lines (380 loc) · 10.2 KB
/
pnm2text.c
File metadata and controls
494 lines (380 loc) · 10.2 KB
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
485
486
487
488
489
490
491
492
493
494
/* SPDX-License-Identifier: BSD-2-Clause */
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
/*
* NOTE: PNM stands for: portable anymap format (PNM).
* This tool supports only PBM (monochrome) and PPM (24 bpp).
*/
#define PSF1_MAGIC 0x0436
#define PSF2_MAGIC 0x864ab572
struct psf1_header {
uint16_t magic;
uint8_t mode;
uint8_t bytes_per_glyph;
};
struct psf2_header {
uint32_t magic;
uint32_t version;
uint32_t header_size;
uint32_t flags;
uint32_t glyphs_count;
uint32_t bytes_per_glyph;
uint32_t height; /* height in pixels */
uint32_t width; /* width in pixels */
};
static int font_fd = -1;
static size_t font_file_sz;
static void *font;
static uint32_t font_w;
static uint32_t font_h;
static uint32_t font_w_bytes;
static uint32_t font_bytes_per_glyph;
static uint8_t *font_data;
static int pnm_fd = -1;
static size_t pnm_file_sz;
static void *pnm;
static void *pnm_data;
static int pnm_w;
static int pnm_h;
static int pnm_w_bytes;
static int pnm_w_bytes_half;
static int pnm_type;
static int rows;
static int cols;
static bool opt_border = true;
static bool opt_quiet = false;
static uint8_t (*recognize_char)(void *, int, int);
static void (*img_get_char)(int, int, char *);
static void img_p4_get_char8(int row, int col, char *dest);
static void img_p4_get_char16(int row, int col, char *dest);
static uint8_t recognize_char_at_w8(void *img_ptr, int r, int c);
static uint8_t recognize_char_at_w16(void *img_ptr, int r, int c);
static int
open_and_mmap_file(const char *f, void **buf_ref, int *fd_ref, size_t *sz_ref)
{
struct stat statbuf;
int rc, fd = open(f, O_RDONLY);
if (fd < 0)
return -errno;
if (fstat(fd, &statbuf) < 0)
goto err_end;
*buf_ref = mmap(NULL, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0);
if (*buf_ref == (void *)-1)
goto err_end;
*sz_ref = statbuf.st_size;
*fd_ref = fd;
return 0;
err_end:
rc = -errno;
close(fd);
return rc;
}
static int
parse_font_file(void)
{
struct psf1_header *h1 = font;
struct psf2_header *h2 = font;
if (h2->magic == PSF2_MAGIC) {
font = h2;
font_w = h2->width;
font_h = h2->height;
font_w_bytes = h2->bytes_per_glyph / h2->height;
font_data = font + h2->header_size;
font_bytes_per_glyph = h2->bytes_per_glyph;
} else if (h1->magic == PSF1_MAGIC) {
font = h1;
font_w = 8;
font_h = h1->bytes_per_glyph;
font_w_bytes = 1;
font_data = font + sizeof(struct psf1_header);
font_bytes_per_glyph = h1->bytes_per_glyph;
} else {
return -1;
}
if (!opt_quiet) {
fprintf(stderr,
"Detected %s font: ",
h2->magic == PSF2_MAGIC ? "PSF2" : "PSF1");
fprintf(stderr, "%d x %d\n", font_w, font_h);
}
if (font_w != 8 && font_w != 16)
return -1; /* font width not supported */
if (font_w == 8)
recognize_char = &recognize_char_at_w8;
else
recognize_char = &recognize_char_at_w16;
return 0;
}
static int
parse_pnm_file(void)
{
char type[32];
char wstr[16], hstr[16], mvstr[16];
size_t i, n, nl = 2;
sscanf(pnm, "%31s %15s %15s", type, wstr, hstr);
if (!strcmp(type, "P4")) {
pnm_type = 4;
} else if (!strcmp(type, "P6")) {
sscanf(pnm, "%31s %15s %15s %15s", type, wstr, hstr, mvstr);
pnm_type = 6;
nl = 3;
if (atoi(mvstr) != 255)
return -1;
} else {
return -1;
}
pnm_w = atoi(wstr);
pnm_h = atoi(hstr);
if (!pnm_w || !pnm_h)
return -1;
rows = pnm_h / font_h;
cols = pnm_w / font_w;
for (i = 0, n = 0; i < pnm_file_sz && n < nl; i++) {
if (((char *)pnm)[i] == 10)
n++;
}
if (i == pnm_file_sz)
return -1; /* corrupted pnm file */
pnm_data = (char *)pnm + i;
if (pnm_type == 4)
pnm_w_bytes = (pnm_w + 7) / 8;
else if (pnm_type == 6)
pnm_w_bytes = pnm_w * 3;
pnm_w_bytes_half = pnm_w_bytes / 2;
if (!opt_quiet) {
fprintf(stderr, "Detected %s image: %d x %d\n",
pnm_type == 4 ? "PBM" : "PPM", pnm_w, pnm_h);
fprintf(stderr, "Screen size: %d x %d\n", cols, rows);
}
return 0;
}
static uint8_t
recognize_char_at_w8(void *img_ptr, int r, int c)
{
int i, y;
uint8_t *img = img_ptr;
for (i = 0, y = 0; i < 256 && y < font_h; i++) {
uint8_t *g = (void *)(font_data + font_bytes_per_glyph * i);
if (!memcmp(img, g, font_h)) {
i++;
break;
}
for (y = 0; y < font_h; y++)
if ((uint8_t)~img[y] != g[y])
break;
}
return i < 256 ? i-1 : '?';
}
static uint8_t
recognize_char_at_w16(void *img_ptr, int r, int c)
{
uint16_t *img = img_ptr;
int i, y;
for (i = 0, y = 0; i < 256 && y < font_h; i++) {
uint16_t *g = (void *)(font_data + font_bytes_per_glyph * i);
if (!memcmp(img, g, 2 * font_h)) {
i++;
break;
}
for (y = 0; y < font_h; y++)
if ((uint16_t)~img[y] != g[y])
break;
}
return i < 256 ? i-1 : '?';
}
static const char transl[256] = {
[ 1] = '-',
[176] = '#',
[177] = '#',
[179] = '|',
[180] = '+',
[191] = '+',
[192] = '+',
[193] = '+',
[194] = '+',
[195] = '+',
[196] = '-',
[197] = '+',
[217] = '+',
[218] = '+',
[219] = '#',
[223] = '+',
};
static void
img_p4_get_char8(int row, int col, char *dest)
{
void *p = pnm_data + pnm_w_bytes * font_h * row + col;
for (int y = 0; y < font_h; y++)
((uint8_t *)dest)[y] = ((uint8_t *)p)[y * pnm_w_bytes];
}
static void
img_p4_get_char16(int row, int col, char *dest)
{
void *p = pnm_data + pnm_w_bytes * font_h * row + 2 * col;
for (int y = 0; y < font_h; y++)
((uint16_t *)dest)[y] = ((uint16_t *)p)[y * pnm_w_bytes_half];
}
static void
img_p6_get_colors(uint8_t *p8, uint32_t colors[2])
{
colors[0] = *(uint32_t *)p8 & 0x00ffffff;
for (int y = 0; y < font_h; y++, p8 += pnm_w_bytes) {
for (int b = 0; b < font_w_bytes; b++) {
for (int x = 0; x < font_w; x++) {
uint32_t c = *(uint32_t *)(p8 + 3 * (x + b * 8)) & 0x00ffffff;
if (c != colors[0]) {
colors[1] = c;
return;
}
}
}
}
/* No foreground color: alright, it must be a blank character */
colors[1] = 0x00ffffff;
}
static void
img_p6_get_char(int row, int col, char *dest)
{
void *p = pnm_data + pnm_w_bytes * font_h * row + 24 * col * font_w_bytes;
uint8_t *p8 = (uint8_t *)p;
uint32_t colors[2] = {0};
img_p6_get_colors(p8, colors);
for (int y = 0; y < font_h; y++, p8 += pnm_w_bytes) {
for (int b = 0, val = 0; b < font_w_bytes; b++, val = 0) {
for (int x = 0; x < 8; x++, val <<= 1) {
uint32_t c = *(uint32_t *)(p8 + 3 * (x + b * 8)) & 0x00ffffff;
if (c == colors[0]) {
/* do nothing: keep the zero */
} else if (c == colors[1]) {
val |= 1;
} else {
/* Oops: we've found a 3rd color. We cannot recognize this. */
bzero(dest, font_w_bytes * font_h);
return;
}
}
val >>= 1;
((uint8_t *)dest)[font_w_bytes * y + b] = (val & 0xff);
}
}
}
static void
recognize_and_print_char(int row, int col)
{
char imgbuf[font_w_bytes * font_h]; // VLA
bzero(imgbuf, font_w_bytes * font_h);
img_get_char(row, col, imgbuf);
uint8_t c = recognize_char(&imgbuf, row, col);
putchar(
32 <= c && c <= 127
? c
: transl[c] ? transl[c] : '?'
);
}
static void
show_help(FILE *fh)
{
fprintf(fh, "Usage:\n");
fprintf(fh, " pnm2text [-nq] <psf_font> <pnm_screenshot>\n\n");
fprintf(fh, "Options:\n");
fprintf(fh, " -n Don't print any border\n");
fprintf(fh, " -q Quiet: no info messages\n");
}
static void
show_help_and_exit(void)
{
show_help(stderr);
exit(1);
}
static void
print_hline(void)
{
putchar('+');
for (int c = 0; c < cols; c++)
putchar('-');
putchar('+');
putchar('\n');
}
static void
parse_and_dump_screen_with_border(void)
{
print_hline();
for (int r = 0; r < rows; r++) {
putchar('|');
for (int c = 0; c < cols; c++)
recognize_and_print_char(r, c);
putchar('|');
putchar('\n');
}
print_hline();
}
int main(int argc, char **argv)
{
int rc;
while (argc > 1 && argv[1][0] == '-') {
if (!strcmp(argv[1], "-n")) {
opt_border = false;
} else if (!strcmp(argv[1], "-q")) {
opt_quiet = true;
} else if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) {
show_help(stdout);
return 0;
} else {
fprintf(stderr, "ERROR: unknown option '%s'\n", argv[1]);
show_help_and_exit();
}
argc--; argv++;
}
if (argc < 3)
show_help_and_exit();
if ((rc = open_and_mmap_file(argv[1], &font, &font_fd, &font_file_sz)) < 0) {
fprintf(stderr, "ERROR: unable to open and mmap '%s': %s\n",
argv[1], strerror(errno));
return 1;
}
if ((rc = open_and_mmap_file(argv[2], &pnm, &pnm_fd, &pnm_file_sz)) < 0) {
fprintf(stderr, "ERROR: unable to open and mmap '%s': %s\n",
argv[2], strerror(errno));
return 1;
}
if (parse_font_file() < 0) {
fprintf(stderr, "ERROR: invalid font file\n");
close(font_fd);
close(pnm_fd);
return 1;
}
if (parse_pnm_file() < 0) {
fprintf(stderr, "ERROR: invalid file. It must have P4 or P6 as type\n");
return 1;
}
if (pnm_type == 4) {
if (font_w_bytes == 1)
img_get_char = &img_p4_get_char8;
else
img_get_char = &img_p4_get_char16;
} else {
/* png_type == "P6" */
img_get_char = &img_p6_get_char;
}
if (opt_border) {
parse_and_dump_screen_with_border();
} else {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++)
recognize_and_print_char(r, c);
putchar('\n');
}
}
close(font_fd);
close(pnm_fd);
return 0;
}