-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr.c
210 lines (156 loc) · 4.55 KB
/
qr.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
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <qrencode.h>
#include "utils.h"
#include "qr.h"
#define ANSI_RESET "\033[0m"
#define ANSI_WHITE_ON_BLACK "\033[40;97;1m"
#define ANSI_WHITE_BG "\033[107;1m"
#define ANSI_BLACK_BG "\033[40;1m"
#define UTF8_BLOCK_UPPER "\xe2\x96\x80" // U+2580
#define UTF8_BLOCK_LOWER "\xe2\x96\x84" // U+2584
#define UTF8_BLOCK_FULL "\xe2\x96\x88" // U+2588
#define QUIET_SIZE 4
static char *memset_p(char *s, char c, size_t n)
{
memset(s, c, n);
s += n;
*s = 0;
return s;
}
static int write_qrcode_utf8(QRcode *qr, void (*print)(const char *line, void *arg), void *arg)
{
AUTOFREE_BUF(char, buf, strlen(ANSI_WHITE_ON_BLACK) + (qr->width + 2 * QUIET_SIZE) * 3 + strlen(ANSI_RESET) + 1);
if (!buf)
return -1;
_Static_assert(QUIET_SIZE % 2 == 0, "QUIET_SIZE needs to be even");
char *p = buf;
p = stpcpy(p, ANSI_WHITE_ON_BLACK);
for (size_t x = 0; x < QUIET_SIZE; x++)
p = stpcpy(p, UTF8_BLOCK_FULL);
char *start = p;
for (size_t x = 0; x < (size_t)qr->width + QUIET_SIZE; x++)
p = stpcpy(p, UTF8_BLOCK_FULL);
stpcpy(p, ANSI_RESET);
for (size_t y = 0; y < QUIET_SIZE / 2; y++)
print(buf, arg);
for (size_t y = 0; y < (size_t)qr->width; y += 2) {
const uint8_t *row1 = qr->data + qr->width * y;
const uint8_t *row2 = row1 + qr->width;
p = start;
for (size_t x = 0; x < (size_t)qr->width; x++) {
bool upper = row1[x] & 1;
bool lower = (y+1) < (size_t)qr->width ? (row2[x] & 1) : false;
if (upper && lower)
p = stpcpy(p, " ");
else if (upper)
p = stpcpy(p, UTF8_BLOCK_LOWER);
else if (lower)
p = stpcpy(p, UTF8_BLOCK_UPPER);
else
p = stpcpy(p, UTF8_BLOCK_FULL);
}
for (size_t x = 0; x < QUIET_SIZE; x++)
p = stpcpy(p, UTF8_BLOCK_FULL);
strcat(buf, ANSI_RESET);
print(buf, arg);
}
p = start;
for (size_t x = 0; x < (size_t)qr->width + QUIET_SIZE; x++)
p = stpcpy(p, UTF8_BLOCK_FULL);
stpcpy(p, ANSI_RESET);
for (size_t y = 0; y < QUIET_SIZE / 2; y++)
print(buf, arg);
return 0;
}
static int write_qrcode_ascii(QRcode *qr, void (*print)(const char *line, void *arg), void *arg)
{
AUTOFREE_BUF(char, buf, 2 * (qr->width + 2 * QUIET_SIZE) + 1);
if (!buf)
return -1;
memset(buf, '#', 2 * (qr->width + 2 * QUIET_SIZE));
buf[2 * (qr->width + 2 * QUIET_SIZE)] = 0;
for (size_t i = 0; i < QUIET_SIZE; i++)
print(buf, arg);
unsigned char *qr_p = qr->data;
for (size_t y = 0; y < (size_t) qr->width; y++) {
char *p = &buf[2 * QUIET_SIZE];
for (size_t x = 0; x < (size_t) qr->width; x++) {
char c = (*qr_p++ & 1) ? ' ' : '#';
*p++ = c;
*p++ = c;
}
print(buf, arg);
}
memset(buf, '#', 2 * (qr->width + 2 * QUIET_SIZE));
for (size_t i = 0; i < QUIET_SIZE; i++)
print(buf, arg);
return 0;
}
static int write_qrcode_ansi(QRcode *qr, void (*print)(const char *line, void *arg), void *arg)
{
/* worst case: one toggle every pixel -> one color sequence + two spaces
* every pixel + final reset */
AUTOFREE_BUF(char, buf, (qr->width + 2 * QUIET_SIZE) * (strlen(ANSI_WHITE_BG) + 2) + strlen(ANSI_RESET) + 1);
if (!buf)
return -1;
*buf = 0;
char *p = buf;
p = stpcpy(p, ANSI_WHITE_BG);
p = memset_p(p, ' ', 2 * QUIET_SIZE);
char *start = p;
p = memset_p(start, ' ', 2 * (qr->width + QUIET_SIZE));
stpcpy(p, ANSI_RESET);
for (size_t i = 0; i < QUIET_SIZE; i++)
print(buf, arg);
unsigned char *qr_p = qr->data;
for (size_t y = 0; y < (size_t) qr->width; y++) {
bool white = true;
p = start;
for (size_t x = 0; x < (size_t) qr->width; x++) {
bool next_white = !(*qr_p++ & 1);
if (!white && next_white)
p = stpcpy(p, ANSI_WHITE_BG);
else if (white && !next_white)
p = stpcpy(p, ANSI_BLACK_BG);
p = stpcpy(p, " ");
white = next_white;
}
if (!white)
p = stpcpy(p, ANSI_WHITE_BG);
p = memset_p(p, ' ', 2 * QUIET_SIZE);
stpcpy(p, ANSI_RESET);
print(buf, arg);
}
p = memset_p(start, ' ', 2 * (qr->width + QUIET_SIZE));
stpcpy(p, ANSI_RESET);
for (size_t i = 0; i < QUIET_SIZE; i++)
print(buf, arg);
return 0;
}
int print_qr(const char *str, enum qr_mode mode, void (*print)(const char *line, void *arg), void *arg)
{
QRcode *qr;
qr = QRcode_encodeString(str, 0, QR_ECLEVEL_L, QR_MODE_8, 1);
if (!qr) {
perror("generating QR code failed");
return -1;
}
int ret = -1;
switch (mode) {
case QR_MODE_UTF8:
ret = write_qrcode_utf8(qr, print, arg);
break;
case QR_MODE_ANSI:
ret = write_qrcode_ansi(qr, print, arg);
break;
case QR_MODE_ASCII:
ret = write_qrcode_ascii(qr, print, arg);
break;
}
QRcode_free(qr);
return ret;
}