ff2braille.c - ff2txt - farbfeld image to plain text visualization
HTML git clone git://bitreich.org/ff2txt git://enlrupgkhuxnvlhsf6lc3fziv5h2hhfrinws65d7roiv6bfj7d652fid.onion/ff2txt
DIR Log
DIR Files
DIR Refs
DIR Tags
DIR README
---
ff2braille.c (2624B)
---
1 /*
2 * Transforming an image into a braille character text is transforming a
3 * matrice this way:
4 *
5 * farbfeld image braille characters
6 *
7 * [[a0 a1 a2 a3 a4 a5] [[[a0 a1 [a2 a3 [a4 a5 \
8 * [b0 b1 b2 b3 b4 b5] b0 b1 / b2 b3 / b4 b5 |<- One braille
9 * [c0 c1 c2 c3 c4 c5] c0 c1 / c2 c3 / c4 c5 | character
10 * [d0 d1 d2 d3 d4 d5] => d0 d1] d2 d3] d4 d5]] /
11 * [e0 e1 e2 e3 e4 e5] [[e0 e1 [e2 e3 [e4 e5
12 * [f0 f1 f2 f3 f4 f5] f0 f1 / f2 f3 / f4 f5
13 * [g0 g1 g2 g3 g4 g5] g0 g1 / g2 g3 / g4 g5
14 * [h0 h1 h2 h3 h4 h5]] h0 h1] h2 h3] h4 h5]]]
15 *
16 * braille characters written as a line:
17 *
18 * [[[a0 a1 b0 b1 c0 c1 d0 d1] <- One braille character
19 * [a2 a3 b2 b3 c2 c3 d2 d3]
20 * [a4 a5 b4 b5 c4 c5 d4 d5]] <- One row of braille characters
21 * [[e0 e1 f0 f1 g0 g1 h0 h1]
22 * [e2 e3 f2 f3 g2 g3 h2 h3]
23 * [e4 e5 f4 f5 g4 g5 h4 h5]]] <- Two row of braille characters
24 *
25 * Although the encoding of braille keeps 1 4
26 * the characters encoded with only six 2 5
27 * dots first, as only these are used for 3 6
28 * encoding letters: 7 8
29 */
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34 #include <stdint.h>
35
36 #include "util.h"
37
38 void
39 print_utf8_3byte(long rune)
40 {
41 printf("%c%c%c",
42 (char)(0xe0 | (0x0f & (rune >> 12))), /* 1110xxxx */
43 (char)(0x80 | (0x3f & (rune >> 6))), /* 10xxxxxx */
44 (char)(0x80 | (0x3f & (rune)))); /* 10xxxxxx */
45 }
46
47 int
48 is_on(struct col *rows[4], uint32_t width, uint32_t height,
49 uint32_t w, uint32_t h)
50 {
51 if (w >= width || h >= height)
52 return 0;
53
54 return col_is_bright(rows[h][w]);
55 }
56
57 void
58 print_4_rows(struct col *rows[4], uint32_t width, uint32_t height)
59 {
60 uint32_t w;
61
62 for (w = 0; w < width; w += 2)
63 print_utf8_3byte(BRAILLE_START +
64 0x01 * is_on(rows, width, height, w + 0, 0) +
65 0x08 * is_on(rows, width, height, w + 1, 0) +
66 0x02 * is_on(rows, width, height, w + 0, 1) +
67 0x10 * is_on(rows, width, height, w + 1, 1) +
68 0x04 * is_on(rows, width, height, w + 0, 2) +
69 0x20 * is_on(rows, width, height, w + 1, 2) +
70 0x40 * is_on(rows, width, height, w + 0, 3) +
71 0x80 * is_on(rows, width, height, w + 1, 3));
72 putchar('\n');
73 }
74
75 int
76 main(void)
77 {
78 struct col buf[MAX_WIDTH * 4], *rows[4];
79 uint32_t width, height, h, r, i;
80
81 read_header(&width, &height);
82
83 for (i = 0; i < 4; i++)
84 rows[i] = buf + width * i;
85
86 for (h = 0; h < height; h += 4) {
87 r = fread(buf, sizeof(*buf), width * 4, stdin);
88 if (r % width != 0)
89 err("invalid line width");
90 if (ferror(stdin))
91 err("fread stdin");
92 print_4_rows(rows, width, r / width);
93 }
94
95 return 0;
96 }