-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsixel.h
81 lines (65 loc) · 1.98 KB
/
sixel.h
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
/* See LICENSE for licence details. */
#include <sixel.h>
enum {
SIXEL_COLORS = 256,
SIXEL_BPP = 3,
};
struct sixel_t {
sixel_output_t *context;
sixel_dither_t *dither;
};
int sixel_write_callback(char *data, int size, void *priv)
{
struct tty_t *tty = (struct tty_t *) priv;
char *ptr;
ssize_t wsize, left;
logging(DEBUG, "callback() data size:%d\n", size);
ptr = data;
left = size;
while (ptr < (data + size)) {
wsize = ewrite(tty->fd, ptr, left);
ptr += wsize;
left -= wsize;
}
return true;
}
bool sixel_init(struct tty_t *tty, struct sixel_t *sixel, struct image *img)
{
/* XXX: libsixel only allows 3 bytes per pixel image,
we should convert bpp when bpp is 1 or 2 or 4 */
if (get_image_channel(img) != SIXEL_BPP)
normalize_bpp(img, SIXEL_BPP, true);
if ((sixel->dither = sixel_dither_create(SIXEL_COLORS)) == NULL) {
logging(ERROR, "couldn't create dither\n");
return false;
}
/* XXX: use first frame for dither initialize */
if (sixel_dither_initialize(sixel->dither, get_current_frame(img),
get_image_width(img), get_image_height(img),
SIXEL_BPP, LARGE_AUTO, REP_AUTO, QUALITY_AUTO) != 0) {
logging(ERROR, "couldn't initialize dither\n");
sixel_dither_unref(sixel->dither);
return false;
}
sixel_dither_set_diffusion_type(sixel->dither, DIFFUSE_AUTO);
//sixel_dither_set_diffusion_type(sixel->dither, DIFFUSE_NONE);
if ((sixel->context = sixel_output_create(sixel_write_callback, (void *) tty)) == NULL) {
logging(ERROR, "couldn't create sixel context\n");
return false;
}
sixel_output_set_8bit_availability(sixel->context, CSIZE_7BIT);
return true;
}
void sixel_die(struct sixel_t *sixel)
{
if (sixel->dither)
sixel_dither_unref(sixel->dither);
if (sixel->context)
sixel_output_unref(sixel->context);
}
void sixel_write(struct tty_t *tty, struct sixel_t *sixel, struct image *img)
{
(void) tty;
sixel_encode(get_current_frame(img), get_image_width(img), get_image_height(img),
get_image_channel(img), sixel->dither, sixel->context);
}