-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.cc
344 lines (243 loc) · 7.83 KB
/
calibration.cc
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
// calibration
// Calibrate your terminal for use with Chat-Art tools.
// Copyright(c) 2013 by Christopher Abad | 20 GOTO 10
// email: aempirei@gmail.com aempirei@256.bz
// http://www.256.bz/ http://www.twentygoto10.com/
// git: git@github.com:aempirei/Chat-Art.git
// aim: ambientempire
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <stdint.h>
#include <sys/types.h>
#include <unistd.h>
#include <string>
#include <ios>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <vector>
#include <geometry.hh>
#include <ansi.hh>
#include <tile.hh>
#include <pnm.hh>
#include <rgb.hh>
typedef std::list<int> ints;
typedef std::map<uint8_t, int> histogram;
typedef std::vector<tile> tiles;
struct config {
tiles bg_colors;
tiles fg_colors;
tiles symbols;
};
typedef uint32_t code_t;
static const code_t prefix_code = 0xA55FACED;
static const code_t suffix_code = 0x1DEADFED;
static const int code_sz = sizeof(code_t) * 8;
static const int calibration_lines = 8;
#define BIT_ISSET(n,b) (((n) & (1 << (b))) != 0)
bool try_code(const pnm& snapshot, code_t code, int x0, int y, int dx) {
const rgb_t *c[2] = { NULL, NULL };
for(int column = 0; column < code_sz; column++) {
size_t x = x0 + column * dx;
if(x >= snapshot.width)
return false;
const rgb_t *pixel = snapshot.pixel(y, x);
int bit = BIT_ISSET(code, column);
if(c[bit] == NULL) {
c[bit] = pixel;
if(c[0] != NULL && c[1] != NULL && rgb::equals(*c[0], *c[1]))
return false;
} else if(!rgb::equals(*pixel, *c[bit])) {
return false;
}
}
return true;
}
bool find_code(const pnm& snapshot, int y0, code_t code, pos_t pos, int *tile_width) {
size_t max_tile_width = snapshot.width / code_sz;
for(int dx = max_tile_width; dx > 0; dx--) {
size_t tile_columns = snapshot.width / dx;
size_t x_offset = snapshot.width % dx;
for(size_t column = 0; column + code_sz - 1 < tile_columns; column++) {
for(size_t y = y0; y < snapshot.height; y++) {
int x0 = column * dx + x_offset;
if(try_code(snapshot, code, x0, y, dx)) {
while(try_code(snapshot, code, x0 - 1, y, dx))
x0--;
pos[0] = x0;
pos[1] = y;
*tile_width = dx;
return true;
}
}
}
}
return false;
}
//////////
// PROCESS
//////////
void assign_tiles(tiles& ts, int line, geometry::mode_type mode, int base) {
for(size_t column = 0; column < ts.size(); column++) {
const auto& code = column;
tile& t = ts[column];
t.move(line, column);
t.set_status(mode, code, base);
}
}
void assign_symbolic_tiles(config& cfg, std::list<tiles> tiles_list) {
histogram seen;
for(auto ts : tiles_list)
for(auto t : ts)
if(!seen[t.geo.ratio]++)
cfg.symbols.push_back(t);
}
void print_tiles(const tiles& ts, const char *title) {
printf("#\n# %s\n#\n", title);
for(auto t : ts)
printf("%s\n", t.geo.to_string().c_str());
}
void process_tiles(config& cfg, const pnm& snapshot, const pos_t size, const pos_t origin) {
tile base(&snapshot, size, origin);
tiles solid_tiles( 8, base);
tiles uppercase_tiles(26, base);
tiles lowercase_tiles(26, base);
tiles number_tiles(10, base);
cfg.bg_colors.resize(8, base);
cfg.fg_colors.resize(8, base);
assign_tiles( solid_tiles, 1, geometry::NOP , 0);
assign_tiles( cfg.bg_colors, 2, geometry::BGCOLOR, 0);
assign_tiles( cfg.fg_colors, 3, geometry::FGCOLOR, 1);
assign_tiles(uppercase_tiles, 4, geometry::SYMBOL, 'A');
assign_tiles(lowercase_tiles, 5, geometry::SYMBOL, 'a');
assign_tiles( number_tiles, 6, geometry::SYMBOL, '0');
for(tile t : cfg.bg_colors) {
t.geo.mode = geometry::FGCOLOR;
cfg.fg_colors.push_back(t);
}
tile space_tile = solid_tiles.front();
space_tile.set_status(geometry::SYMBOL, 0, ' ');
assign_symbolic_tiles(cfg, {uppercase_tiles, lowercase_tiles, number_tiles, tiles(1, space_tile)});
print_tiles(cfg.bg_colors, "background colors");
print_tiles(cfg.fg_colors, "foreground colors");
print_tiles(cfg.symbols , "character symbols");
}
void process_calibration() {
pnm snapshot(stdin);
config cfg;
if(snapshot.isloaded()) {
pos_t prefix_pos;
pos_t suffix_pos;
int prefix_tile_width;
int suffix_tile_width;
if( find_code(snapshot, 0, prefix_code, prefix_pos, &prefix_tile_width) &&
find_code(snapshot, prefix_pos[1] + 1, suffix_code, suffix_pos, &suffix_tile_width))
{
int hn = suffix_pos[1] - prefix_pos[1];
if( (prefix_tile_width == suffix_tile_width) &&
(prefix_pos[0] == suffix_pos[0]) &&
(hn % (calibration_lines - 1) == 0))
{
pos_t tile_sz = { prefix_tile_width, hn / (calibration_lines - 1) };
process_tiles(cfg, snapshot, tile_sz, prefix_pos);
}
}
}
}
//////////
// DISPLAY
//////////
void display_code(code_t code) {
for(int bit_n = 0; bit_n < code_sz; bit_n++) {
std::string bgcolor = ansi::bg(BIT_ISSET(code, bit_n) ? ansi::white : ansi::black);
std::cout << bgcolor << ' ';
}
std::cout << ansi::clear << std::endl;
}
void display_calibration() {
/////////////////////////////////////////////
// calibration text is 8 lines and 32 columns
/////////////////////////////////////////////
// 0.32 prefix code
// 1.08 background colors
// 2.08 foreground colors
// 3.08 bold foreground colors
// 4.26 uppercase alphabet
// 5.26 lowercase alphabet
// 6.10 numbers
// 7.32 suffix code
//////////////
// prefix code
display_code(prefix_code);
////////////////////
// background colors
for(int i = ansi::first_color; i <= ansi::last_color; i++)
std::cout << ansi::bg(i) << ' ';
std::cout << std::endl;
////////////////////
// foreground colors
std::cout << ansi::bg(ansi::black);
for(int i = ansi::first_color; i <= ansi::last_color; i++)
std::cout << ansi::fg(i) << '#';
std::cout << std::endl;
/////////////////////////
// bold foreground colors
std::cout << ansi::bg(ansi::black);
for(int i = ansi::first_color; i <= ansi::last_color; i++)
std::cout << ansi::bold << ansi::fg(i) << '#';
std::cout << std::endl;
///////
// font
std::cout << ansi::fg(ansi::white) << ansi::bg(ansi::black);
std::cout << "ABCDEFGHIJKLMNOPQRSTUVWXYZ" << std::endl;
std::cout << "abcdefghijklmnopqrstuvwxyz" << std::endl;
std::cout << "0123456789" << std::endl;
//////////////
// suffix code
display_code(suffix_code);
}
void help(const char *prog) {
std::cerr << "\nusage: " << prog << " [options]\n\n";
std::cerr << "either display the calibration pattern to save a snapshot of, or process a snapshot.\n";
std::cerr << "after displaying the calibration in your terminal, save a snapshot of the pattern.\n";
std::cerr << "this can usually be done via the PRINT SCREEN button. afterward, convert it to the\n";
std::cerr << "PNM file format. netpbm, djpeg or imagemagick can usually do this. finally, pipe\n";
std::cerr << "the PNM file into this program again using the '-p' option and then redirect output\n";
std::cerr << "to a configuration file.\n\n";
std::cerr << "\t-d display the calibration pattern to stdout\n";
std::cerr << "\t-p process calibration pattern image snapshot from stdin\n";
std::cerr << "\t-h show this help\n\n";
std::cerr << "example: djpeg screenshot.jpg | " << prog << " -p > conf.d/ubuntu-mono-12.conf\n\n";
std::cerr << "report bugs to <aempirei@256.bz>\n\n";
}
int main(int argc, char **argv) {
int display = 0;
int process = 0;
int opt;
while ((opt = getopt(argc, argv, "dph")) != -1) {
switch (opt) {
case 'd': display = 1; break;
case 'p': process = 1; break;
case 'h':
default:
help(basename(*argv));
exit(EXIT_SUCCESS);
}
}
if(display == process) {
std::cerr << '\n' << ansi::bold;
std::cerr << "error: please provide exactly one mode of operation, either -d or -p";
std::cerr << ansi::clear << '\n';
help(basename(*argv));
exit(EXIT_SUCCESS);
}
if(display)
display_calibration();
if(process)
process_calibration();
exit(EXIT_SUCCESS);
}