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
|
#include "sixel.h"
#include <string.h>
#define LOG_MODULE "sixel"
#define LOG_ENABLE_DBG 0
#include "log.h"
#include "render.h"
#define max(x, y) ((x) > (y) ? (x) : (y))
static const size_t COLOR_COUNT = 1024;
static size_t count;
void
sixel_init(struct terminal *term)
{
assert(term->sixel.palette == NULL);
assert(term->sixel.image.data == NULL);
term->sixel.state = SIXEL_SIXEL;
term->sixel.row = 0;
term->sixel.col = 0;
term->sixel.color_idx = 0;
term->sixel.max_col = 0;
term->sixel.param = 0;
term->sixel.param_idx = 0;
memset(term->sixel.params, 0, sizeof(term->sixel.params));
term->sixel.palette = calloc(COLOR_COUNT, sizeof(term->sixel.palette[0]));
term->sixel.image.data = calloc(1 * 6, sizeof(term->sixel.image.data[0]));
term->sixel.image.width = 1;
term->sixel.image.height = 6;
count = 0;
/* TODO: default palette */
}
void
sixel_destroy(struct sixel *sixel)
{
pixman_image_unref(sixel->pix);
free(sixel->data);
sixel->pix = NULL;
sixel->data = NULL;
}
void
sixel_unhook(struct terminal *term)
{
free(term->sixel.palette);
term->sixel.palette = NULL;
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.row++;
term->sixel.col = 0;
struct sixel image = {
.data = term->sixel.image.data,
.width = term->sixel.max_col,
.height = term->sixel.row * 6,
.rows = (term->sixel.row * 6 + term->cell_height - 1) / term->cell_height,
.pos = (struct coord){term->cursor.point.col, term->grid->offset + term->cursor.point.row},
};
LOG_DBG("generating %dx%d pixman image", image.width, image.height);
image.pix = pixman_image_create_bits_no_clear(
PIXMAN_a8r8g8b8,
image.width, image.height,
term->sixel.image.data,
term->sixel.image.width * sizeof(uint32_t));
tll_foreach(term->sixel_images, it) {
if (it->item.pos.row == image.pos.row) {
sixel_destroy(&it->item);
tll_remove(term->sixel_images, it);
}
}
tll_push_back(term->sixel_images, image);
term->sixel.image.data = NULL;
term->sixel.image.width = 0;
term->sixel.image.height = 0;
term->sixel.max_col = 0;
term->sixel.col = 0;
term->sixel.row = 0;
const size_t lines = (image.height + term->cell_height - 1) / term->cell_height;
for (size_t i = 0; i < lines; i++)
term_linefeed(term);
term_formfeed(term);
render_refresh(term);
}
static bool
resize(struct terminal *term, int new_width, int new_height)
{
LOG_DBG("resizing image: %dx%d -> %dx%d",
term->sixel.image.width, term->sixel.image.height,
new_width, new_height);
uint32_t *old_data = term->sixel.image.data;
const int old_width = term->sixel.image.width;
const int old_height = term->sixel.image.height;
assert(new_width >= old_width);
assert(new_height >= old_height);
uint32_t *new_data = NULL;
if (new_width == old_width) {
/* Width (and thus stride) is the same, so we can simply
* re-alloc the existing buffer */
new_data = realloc(old_data, new_width * new_height * sizeof(uint32_t));
if (new_data == NULL) {
LOG_ERRNO("failed to reallocate sixel image buffer");
return false;
}
assert(new_height > old_height);
memset(&new_data[old_height * new_width], 0, (new_height - old_height) * new_width * sizeof(uint32_t));
} else {
/* Width (and thus stride) change - need to allocate a new buffer */
assert(new_width > old_width);
new_data = malloc(new_width * new_height * sizeof(uint32_t));
/* Copy old rows, and zero-initialize the tail of each row */
for (int r = 0; r < old_height; r++) {
memcpy(&new_data[r * new_width], &old_data[r * old_width], old_width * sizeof(uint32_t));
memset(&new_data[r * new_width + old_width], 0, (new_width - old_width) * sizeof(uint32_t));
}
/* Zero-initiailize new rows */
for (int r = old_height; r < new_height; r++)
memset(&new_data[r * new_width], 0, new_width * sizeof(uint32_t));
free(old_data);
}
assert(new_data != NULL);
term->sixel.image.data = new_data;
term->sixel.image.width = new_width;
term->sixel.image.height = new_height;
return true;
}
static void
sixel_add(struct terminal *term, uint32_t color, uint8_t sixel)
{
//LOG_DBG("adding sixel %02hhx using color 0x%06x", sixel, color);
if (term->sixel.col >= term->sixel.image.width ||
term->sixel.row * 6 >= term->sixel.image.height)
{
resize(term,
max(term->sixel.max_col, term->sixel.col + 1),
(term->sixel.row + 1) * 6);
}
for (int i = 0; i < 6; i++, sixel >>= 1) {
if (sixel & 1) {
size_t pixel_row = term->sixel.row * 6 + i;
size_t stride = term->sixel.image.width;
size_t idx = pixel_row * stride + term->sixel.col;
term->sixel.image.data[idx] = term->colors.alpha / 256 << 24 | color;
}
}
assert(sixel == 0);
term->sixel.col++;
}
static void
sixel_sixel(struct terminal *term, uint8_t c)
{
switch (c) {
case '"':
term->sixel.state = SIXEL_RASTER;
term->sixel.param = 0;
term->sixel.param_idx = 0;
break;
case '#':
term->sixel.state = SIXEL_COLOR;
term->sixel.color_idx = 0;
term->sixel.param = 0;
term->sixel.param_idx = 0;
break;
case '$':
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.col = 0;
break;
case '-':
if (term->sixel.col > term->sixel.max_col)
term->sixel.max_col = term->sixel.col;
term->sixel.row++;
term->sixel.col = 0;
break;
case '!':
term->sixel.state = SIXEL_REPEAT;
term->sixel.param = 0;
term->sixel.param_idx = 0;
break;
default:
if (c < '?' || c > '~') {
LOG_ERR("invalid sixel charactwer: '%c' at idx=%zu", c, count);
return;
}
sixel_add(term, term->sixel.palette[term->sixel.color_idx], c - 63);
break;
}
}
static void
sixel_repeat(struct terminal *term, uint8_t c)
{
if (c >= '0' && c <= '9') {
term->sixel.param *= 10;
term->sixel.param += c - '0';
} else {
//LOG_DBG("repeating '%c' %u times", c, term->sixel.param);
term->sixel.state = SIXEL_SIXEL;
for (unsigned i = 0; i < term->sixel.param; i++)
sixel_sixel(term, c);
}
}
static void
sixel_raster(struct terminal *term, uint8_t c)
{
if (c >= '0' && c <= '9') {
term->sixel.param *= 10;
term->sixel.param += c - '0';
} else {
if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0]))
term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
term->sixel.param = 0;
if (c != ';') {
unsigned pan __attribute__((unused)) = term->sixel.params[0];
unsigned pad __attribute__((unused)) = term->sixel.params[1];
unsigned ph __attribute__((unused)) = term->sixel.params[2];
unsigned pv __attribute__((unused)) = term->sixel.params[3];
LOG_DBG("pan=%u, pad=%u (aspect ratio = %u), size=%ux%u",
pan, pad, pan / pad, ph, pv);
if (ph >= term->sixel.image.height && pv >= term->sixel.image.width)
resize(term, ph, pv);
}
switch (c) {
case '#': term->sixel.state = SIXEL_COLOR; break;
case ';': term->sixel.state = SIXEL_RASTER; break;
default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break;
}
}
}
static void
sixel_color(struct terminal *term, uint8_t c)
{
if (c >= '0' && c <= '9') {
term->sixel.param *= 10;
term->sixel.param += c - '0';
} else {
if (term->sixel.param < COLOR_COUNT)
term->sixel.color_idx = term->sixel.param;
else
term->sixel.color_idx = 0;
term->sixel.param_idx = 0;
term->sixel.param = 0;
switch (c) {
case '#': term->sixel.state = SIXEL_COLOR; break;
case ';': term->sixel.state = SIXEL_COLOR_SPEC; break;
default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break;
}
}
}
static void
sixel_color_spec(struct terminal *term, uint8_t c)
{
if (c >= '0' && c <= '9') {
term->sixel.param *= 10;
term->sixel.param += c - '0';
} else {
if (term->sixel.param_idx < sizeof(term->sixel.params) / sizeof(term->sixel.params[0]))
term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
term->sixel.param = 0;
if (c != ';') {
unsigned format = term->sixel.params[0];
unsigned c1 = term->sixel.params[1];
unsigned c2 = term->sixel.params[2];
unsigned c3 = term->sixel.params[3];
if (format == 1) {
assert(false && "HLS color format not implemented");
} else {
uint8_t r = 255 * c1 / 100;
uint8_t g = 255 * c2 / 100;
uint8_t b = 255 * c3 / 100;
LOG_DBG("setting palette #%d = RGB %hhu/%hhu/%hhu",
term->sixel.color_idx, r, g, b);
term->sixel.palette[term->sixel.color_idx] = r << 16 | g << 8 | b;
}
}
switch (c) {
case '#': term->sixel.state = SIXEL_COLOR; break;
case ';': term->sixel.state = SIXEL_COLOR_SPEC; break;
default: term->sixel.state = SIXEL_SIXEL; sixel_sixel(term, c); break;
}
}
}
void
sixel_put(struct terminal *term, uint8_t c)
{
count++;
switch (c) {
case ' ': return;
case '\n': return;
case '\r': return;
}
switch (term->sixel.state) {
case SIXEL_SIXEL: sixel_sixel(term, c); break;
case SIXEL_REPEAT: sixel_repeat(term, c); break;
case SIXEL_RASTER: sixel_raster(term, c); break;
case SIXEL_COLOR: sixel_color(term, c); break;
case SIXEL_COLOR_SPEC: sixel_color_spec(term, c); break;
}
}
|