forked from uobikiemukot/recterm
-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.c
707 lines (639 loc) · 21 KB
/
main.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
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
/*
* Copyright (C) 2014 haru <uobikiemukot at gmail dot com>
* Copyright (C) 2014 Hayaki Saito <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "yaft.h"
#include "util.h"
#include "pseudo.h"
#include "terminal.h"
#include "function.h"
#include "dcs.h"
#include "parse.h"
#include "gifsave89.h"
#include "color.h"
#include <stdio.h>
#ifdef HAVE_STDLIB_H
# include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#ifdef HAVE_GETOPT_H
# include <getopt.h>
#endif
#ifdef HAVE_ERRNO_H
# include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
# include <fcntl.h>
#endif
#if !defined(HAVE_MEMCPY)
# define memcpy(d, s, n) (bcopy ((s), (d), (n)))
#endif
#if !defined(O_BINARY) && defined(_O_BINARY)
# define O_BINARY _O_BINARY
#endif /* !defined(O_BINARY) && !defined(_O_BINARY) */
struct settings_t {
int width;
int height;
int show_version;
int show_help;
int last_frame_delay;
int foreground_color;
int background_color;
int cursor_color;
int tabwidth;
int cjkwidth;
int repeat;
char *input;
char *output;
int render_interval;
double play_speed;
const uint32_t* palette16;
};
enum cmap_bitfield {
RED_SHIFT = 5,
GREEN_SHIFT = 2,
BLUE_SHIFT = 0,
RED_MASK = 3,
GREEN_MASK = 3,
BLUE_MASK = 2
};
static void pb_init(struct pseudobuffer *pb, int width, int height)
{
pb->width = width;
pb->height = height;
pb->bytes_per_pixel = BYTES_PER_PIXEL;
pb->line_length = pb->width * pb->bytes_per_pixel;
pb->buf = ecalloc(pb->width * pb->height, pb->bytes_per_pixel);
}
static void pb_die(struct pseudobuffer *pb)
{
free(pb->buf);
}
static void set_colormap(int colormap[COLORS * BYTES_PER_PIXEL + 1])
{
int i, ci, r, g, b;
uint8_t index;
/* colormap: terminal 256color
for (i = 0; i < COLORS; i++) {
ci = i * BYTES_PER_PIXEL;
r = (color_list[i] >> 16) & bit_mask[8];
g = (color_list[i] >> 8) & bit_mask[8];
b = (color_list[i] >> 0) & bit_mask[8];
colormap[ci + 0] = r;
colormap[ci + 1] = g;
colormap[ci + 2] = b;
}
*/
/* colormap: red/green: 3bit blue: 2bit
*/
for (i = 0; i < COLORS; i++) {
index = (uint8_t) i;
ci = i * BYTES_PER_PIXEL;
r = (index >> RED_SHIFT) & bit_mask[RED_MASK];
g = (index >> GREEN_SHIFT) & bit_mask[GREEN_MASK];
b = (index >> BLUE_SHIFT) & bit_mask[BLUE_MASK];
colormap[ci + 0] = r * bit_mask[BITS_PER_BYTE] / bit_mask[RED_MASK];
colormap[ci + 1] = g * bit_mask[BITS_PER_BYTE] / bit_mask[GREEN_MASK];
colormap[ci + 2] = b * bit_mask[BITS_PER_BYTE] / bit_mask[BLUE_MASK];
}
colormap[COLORS * BYTES_PER_PIXEL] = -1;
}
static uint32_t pixel2index(uint32_t pixel)
{
/* pixel is always 24bpp */
uint32_t r, g, b;
/* split r, g, b bits */
r = (pixel >> 16) & bit_mask[8];
g = (pixel >> 8) & bit_mask[8];
b = (pixel >> 0) & bit_mask[8];
/* colormap: terminal 256color
if (r == g && r == b) { // 24 gray scale
r = 24 * r / COLORS;
return 232 + r;
} // 6x6x6 color cube
r = 6 * r / COLORS;
g = 6 * g / COLORS;
b = 6 * b / COLORS;
return 16 + (r * 36) + (g * 6) + b;
*/
/* colormap: red/green: 3bit blue: 2bit
*/
// get MSB ..._MASK bits
r = (r >> (8 - RED_MASK)) & bit_mask[RED_MASK];
g = (g >> (8 - GREEN_MASK)) & bit_mask[GREEN_MASK];
b = (b >> (8 - BLUE_MASK)) & bit_mask[BLUE_MASK];
return (r << RED_SHIFT) | (g << GREEN_SHIFT) | (b << BLUE_SHIFT);
}
static void apply_colormap(struct pseudobuffer *pb, unsigned char *img)
{
int w, h;
uint32_t pixel = 0;
for (h = 0; h < pb->height; h++) {
for (w = 0; w < pb->width; w++) {
memcpy(&pixel, pb->buf + h * pb->line_length
+ w * pb->bytes_per_pixel, pb->bytes_per_pixel);
*(img + h * pb->width + w) = pixel2index(pixel) & bit_mask[BITS_PER_BYTE];
}
}
}
static size_t write_gif(unsigned char *gifimage, int size, FILE *f)
{
size_t wsize = 0;
wsize = fwrite(gifimage, sizeof(unsigned char), size, f);
return wsize;
}
static void show_version()
{
printf(PACKAGE_NAME " " PACKAGE_VERSION "\n"
"Copyright (C) 2014 haru <uobikiemukot at gmail dot com>\n"
"Copyright (C) 2012-2014 Hayaki Saito <[email protected]>.\n"
"\n"
"This program is free software; you can redistribute it and/or modify\n"
"it under the terms of the GNU General Public License as published by\n"
"the Free Software Foundation; either version 3 of the License, or\n"
"(at your option) any later version.\n"
"\n"
"This program is distributed in the hope that it will be useful,\n"
"but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"GNU General Public License for more details.\n"
"\n"
"You should have received a copy of the GNU General Public License\n"
"along with this program. If not, see http://www.gnu.org/licenses/.\n"
"%s\n", copyright
);
}
static void show_help()
{
fprintf(stderr,
"Usage: seq2gif [Options] < ttyrecord > record.gif\n"
" seq2gif [Options] -i ttyrecord -o record.gif\n"
"\n"
"Options:\n"
"-w WIDTH, --width=WIDTH specify terminal width in cell size.\n"
" (default: 80)\n"
"-h HEIGHT, --height=HEIGHT specify terminal height in cell size.\n"
" (default: 24)\n"
"-l DELAY, --last-frame-delay=DELAY specify delay in msec which is added\n"
" to the last frame. (default: 300)\n"
"-f COLORNO, --foreground-color=COLORNO specify foreground color palette.\n"
" number.\n"
"-b COLORNO, --background-color=COLORNO specify background color palette\n"
" number.\n"
"-c COLORNO, --cursor-color=COLORNO specify cursor color palette\n"
" number.\n"
"-t TABSTOP, --tabstop=TABSTOP specify hardware tabstop(default: 8)\n"
"-j, --cjkwidth treat East Asian Ambiguous width\n"
" characters (UAX#11) as wide.\n"
"-r COUNT, --repeat=COUNT specify animation repeat count. loop\n"
" infinitely if 0 is given. (default: 0)\n"
"-i FILE, --input=FILE specify input file name. use STDIN\n"
" if '-' is given. (default: '-')\n"
"-o FILE, --output=FILE specify output file name. use STDOUT\n"
" if '-' is given. (default: '-')\n"
"-V, --version show version and license information.\n"
"-H, --help show this help.\n"
"-I DELAY, --render-interval=DELAY skip frames with smaller delays than\n"
" DELAY specified in milliseconds.\n"
" (default: 20)\n"
"-s NUM, --play-speed=NUM specify the factor of the play speed.\n"
" A larger value means faster play.\n"
" (default: 1.0)\n"
"-p NAME, --palette16=NAME specify a color palette name for the\n"
" first 16 colors. NAME is one of the\n"
" following names: 'vga', 'cmd', 'win',\n"
" 'powershell', 'app', 'putty', 'mirc',\n"
" 'xterm', 'ubuntu', and 'solarized',\n"
" 'solarized256'. (default: 'vga').\n"
);
}
static int parse_args(int argc, char *argv[], struct settings_t *psettings)
{
int n;
char const *optstring = "w:h:HVl:f:b:c:t:jr:i:o:I:s:p:";
#ifdef HAVE_GETOPT_LONG
int long_opt;
int option_index;
struct option long_options[] = {
{"width", required_argument, &long_opt, 'w'},
{"height", required_argument, &long_opt, 'h'},
{"last-frame-delay", required_argument, &long_opt, 'l'},
{"foreground-color", required_argument, &long_opt, 'f'},
{"background-color", required_argument, &long_opt, 'b'},
{"cursor-color", required_argument, &long_opt, 'c'},
{"tabstop", required_argument, &long_opt, 't'},
{"cjkwidth", no_argument, &long_opt, 'j'},
{"repeat", required_argument, &long_opt, 'r'},
{"input", required_argument, &long_opt, 'i'},
{"output", required_argument, &long_opt, 'o'},
{"help", no_argument, &long_opt, 'H'},
{"version", no_argument, &long_opt, 'V'},
{"render-interval", required_argument, &long_opt, 'I'},
{"play-speed", required_argument, &long_opt, 's'},
{"palette16", required_argument, &long_opt, 'p'},
{0, 0, 0, 0}
};
#endif /* HAVE_GETOPT_LONG */
for (;;) {
#ifdef HAVE_GETOPT_LONG
n = getopt_long(argc, argv, optstring,
long_options, &option_index);
#else
n = getopt(argc, argv, optstring);
#endif /* HAVE_GETOPT_LONG */
if (n == -1) {
break;
}
#ifdef HAVE_GETOPT_LONG
if (n == 0) {
n = long_opt;
}
#endif /* HAVE_GETOPT_LONG */
switch(n) {
case 'w':
psettings->width = atoi(optarg);
if (psettings->width < 1) {
goto argerr;
}
break;
case 'h':
psettings->height = atoi(optarg);
if (psettings->height < 1) {
goto argerr;
}
break;
case 'l':
psettings->last_frame_delay = atoi(optarg);
if (psettings->last_frame_delay < 0) {
goto argerr;
}
break;
case 'f':
psettings->foreground_color = atoi(optarg);
if (psettings->foreground_color < 0) {
goto argerr;
}
if (psettings->foreground_color > 255) {
goto argerr;
}
break;
case 'b':
psettings->background_color = atoi(optarg);
if (psettings->background_color < 0) {
goto argerr;
}
if (psettings->background_color > 255) {
goto argerr;
}
break;
case 'c':
psettings->cursor_color = atoi(optarg);
if (psettings->cursor_color < 0) {
goto argerr;
}
if (psettings->cursor_color > 255) {
goto argerr;
}
break;
case 't':
psettings->tabwidth = atoi(optarg);
if (psettings->tabwidth < 0) {
goto argerr;
}
if (psettings->tabwidth > 255) {
goto argerr;
}
break;
case 'j':
psettings->cjkwidth = 1;
break;
case 'r':
psettings->repeat = atoi(optarg);
if (psettings->repeat < 0) {
goto argerr;
}
if (psettings->repeat > 0xffff) {
goto argerr;
}
break;
case 'i':
psettings->input = strdup(optarg);
break;
case 'o':
psettings->output = strdup(optarg);
break;
case 'H':
psettings->show_help = 1;
break;
case 'V':
psettings->show_version = 1;
break;
case 'I':
psettings->render_interval = atoi(optarg);
if (psettings->render_interval < 0) {
goto argerr;
}
break;
case 's':
psettings->play_speed = atof(optarg);
if (psettings->play_speed <= 0.0) {
goto argerr;
}
break;
case 'p':
{
const uint32_t* palette16 = color_parse_palette16(optarg);
if (palette16) {
psettings->palette16 = palette16;
}
else {
fprintf(stderr, PACKAGE_NAME ": unknown value for --palette16 (-p) option.\n");
goto argerr;
}
}
break;
default:
goto argerr;
}
}
return 0;
argerr:
show_help();
return 1;
}
static FILE * open_input_file(char const *filename)
{
FILE *f;
if (filename == NULL || strcmp(filename, "-") == 0) {
/* for windows */
#if defined(O_BINARY)
# if HAVE__SETMODE
_setmode(fileno(stdin), O_BINARY);
# elif HAVE_SETMODE
setmode(fileno(stdin), O_BINARY);
# endif /* HAVE_SETMODE */
#endif /* defined(O_BINARY) */
return stdin;
}
f = fopen(filename, "rb");
if (!f) {
#ifdef _ERRNO_H
fprintf(stderr, "fopen('%s') failed.\n" "reason: %s.\n",
filename, strerror(errno));
#endif /* HAVE_ERRNO_H */
return NULL;
}
return f;
}
static FILE * open_output_file(char const *filename)
{
FILE *f;
if (filename == NULL || strcmp(filename, "-") == 0) {
/* for windows */
#if defined(O_BINARY)
# if HAVE__SETMODE
_setmode(fileno(stdout), O_BINARY);
# elif HAVE_SETMODE
setmode(fileno(stdout), O_BINARY);
# endif /* HAVE_SETMODE */
#endif /* defined(O_BINARY) */
return stdout;
}
f = fopen(filename, "wb");
if (!f) {
#ifdef _ERRNO_H
fprintf(stderr, "fopen('%s') failed.\n" "reason: %s.\n",
filename, strerror(errno));
#endif /* HAVE_ERRNO_H */
return NULL;
}
return f;
}
static int32_t readtime(FILE *f, uint8_t *obuf)
{
int nread;
int32_t tv_sec;
int32_t tv_usec;
nread = fread(obuf, 1, sizeof(tv_sec), f);
if (nread != sizeof(tv_sec)) {
return -1;
}
tv_sec = obuf[0] | obuf[1] << 8
| obuf[2] << 16 | obuf[3] << 24;
nread = fread(obuf, 1, sizeof(tv_usec), f);
if (nread != sizeof(tv_usec)) {
return -1;
}
tv_usec = obuf[0] | obuf[1] << 8
| obuf[2] << 16 | obuf[3] << 24;
return tv_sec * 1000000 + tv_usec;
}
static int32_t readlen(FILE *f, uint8_t *obuf)
{
int nread;
uint32_t len;
nread = fread(obuf, 1, sizeof(len), f);
if (nread != sizeof(len)) {
return -1;
}
len = obuf[0]
| obuf[1] << 8
| obuf[2] << 16
| obuf[3] << 24;
return len;
}
int main(int argc, char *argv[])
{
uint8_t *obuf;
ssize_t nread;
struct terminal term;
struct pseudobuffer pb;
uint32_t prev = 0;
uint32_t now = 0;
ssize_t len = 0;
size_t maxlen = 0;
int delay = 0;
int dirty = 0;
FILE *in_file = NULL;
FILE *out_file = NULL;
int nret = EXIT_SUCCESS;
void *gsdata;
unsigned char *gifimage = NULL;
int gifsize, colormap[COLORS * BYTES_PER_PIXEL + 1];
unsigned char *img;
uint32_t gif_unit_time;
uint32_t gif_render_interval;
int is_render_deferred;
struct settings_t settings = {
80, /* width */
24, /* height */
0, /* show_version */
0, /* show_help */
300, /* last_frame_delay */
7, /* foreground_color */
0, /* background_color */
2, /* cursor_color */
8, /* tabwidth */
0, /* cjkwidth */
0, /* repeat */
NULL, /* input */
NULL, /* output */
20, /* render_interval */
1.0, /* play_speed */
NULL, /* palette16 */
};
if (parse_args(argc, argv, &settings) != 0) {
exit(1);
}
if (settings.show_help) {
show_help();
exit(0);
}
if (settings.show_version) {
show_version();
exit(0);
}
if (settings.palette16) {
memcpy(color_list, settings.palette16, sizeof(uint32_t) * 16);
}
/* init */
pb_init(&pb, settings.width * CELL_WIDTH, settings.height * CELL_HEIGHT);
term_init(&term, pb.width, pb.height,
settings.foreground_color,
settings.background_color,
settings.cursor_color,
settings.tabwidth,
settings.cjkwidth);
/* init gif */
img = (unsigned char *) ecalloc(pb.width * pb.height, 1);
set_colormap(colormap);
if (!(gsdata = newgif((void **) &gifimage, pb.width, pb.height, colormap, 0))) {
free(img);
term_die(&term);
pb_die(&pb);
return EXIT_FAILURE;
}
animategif(gsdata, /* repetitions */ settings.repeat, 0,
/* transparent background */ -1, /* disposal */ 2);
in_file = open_input_file(settings.input);
out_file = open_output_file(settings.output);
if (in_file == NULL || out_file == NULL) {
exit(1);
}
maxlen = 2048;
obuf = malloc(maxlen);
prev = now = readtime(in_file, obuf);
/* main loop */
gif_unit_time = (uint32_t)(10000 * settings.play_speed);
gif_render_interval = settings.render_interval / 10;
is_render_deferred = 0;
for(;;) {
len = readlen(in_file, obuf);
if (len <= 0) {
nret = EXIT_FAILURE;
break;
}
if (len > maxlen) {
obuf = realloc(obuf, len);
maxlen = len;
}
nread = fread(obuf, 1, len, in_file);
if (nread != len) {
nret = EXIT_FAILURE;
break;
}
parse(&term, obuf, nread, &dirty);
now = readtime(in_file, obuf);
if (now == -1) {
break;
}
if (term.esc.state != STATE_DCS || dirty) {
delay = (now - prev) / gif_unit_time;
if (is_render_deferred && delay >= gif_render_interval) {
controlgif(gsdata, -1, gif_render_interval, 0, 0);
prev += gif_render_interval * gif_unit_time;
putgif(gsdata, img);
delay -= gif_render_interval;
is_render_deferred = 0;
}
if (!is_render_deferred) {
/* take screenshot */
refresh(&pb, &term);
apply_colormap(&pb, img);
}
is_render_deferred = delay < gif_render_interval;
if (!is_render_deferred) {
controlgif(gsdata, -1, delay, 0, 0);
prev = now;
putgif(gsdata, img);
}
}
dirty = 0;
}
if (is_render_deferred) {
controlgif(gsdata, -1, gif_render_interval, 0, 0);
putgif(gsdata, img);
}
if (settings.last_frame_delay > 0) {
if (nret != EXIT_FAILURE) {
refresh(&pb, &term);
apply_colormap(&pb, img);
}
delay = settings.last_frame_delay / 10;
if (delay < gif_render_interval)
delay = gif_render_interval;
controlgif(gsdata, -1, delay, 0, 0);
putgif(gsdata, img);
}
/* output gif */
gifsize = endgif(gsdata);
if (gifsize > 0) {
write_gif(gifimage, gifsize, out_file);
free(gifimage);
}
/* normal exit */
free(img);
if (settings.input) {
if (fileno(in_file) != STDIN_FILENO) {
fclose(in_file);
}
free(settings.input);
}
if (settings.output) {
if (fileno(out_file) != STDOUT_FILENO) {
fclose(out_file);
}
free(settings.output);
}
term_die(&term);
pb_die(&pb);
free(obuf);
return nret;
}
/* emacs Local Variables: */
/* emacs mode: c */
/* emacs tab-width: 4 */
/* emacs indent-tabs-mode: nil */
/* emacs c-basic-offset: 4 */
/* emacs End: */
/* vim: set expandtab ts=4 : */
/* EOF */