Codebergdnkl
foot
master
1#include "sixel.h"
3#include <string.h>
4#include <limits.h>
6#define LOG_MODULE "sixel"
7#define LOG_ENABLE_DBG 0
8#include "log.h"
9#include "debug.h"
10#include "grid.h"
11#include "hsl.h"
12#include "render.h"
13#include "srgb.h"
14#include "util.h"
15#include "xmalloc.h"
16#include "xsnprintf.h"
18static size_t count;
20static void sixel_put_generic(struct terminal *term, uint8_t c);
21static void sixel_put_ar_11(struct terminal *term, uint8_t c);
23static uint32_t
24color_decode_srgb(const struct terminal *term, uint16_t r, uint16_t g, uint16_t b)
25{
26 if (term->sixel.linear_blending) {
27 if (term->sixel.use_10bit) {
28 r = srgb_decode_8_to_16(r) >> 6;
29 g = srgb_decode_8_to_16(g) >> 6;
30 b = srgb_decode_8_to_16(b) >> 6;
31 } else {
32 r = srgb_decode_8_to_8(r);
33 g = srgb_decode_8_to_8(g);
34 b = srgb_decode_8_to_8(b);
35 }
36 } else {
37 if (term->sixel.use_10bit) {
38 r <<= 2;
39 g <<= 2;
40 b <<= 2;
41 }
42 }
44 uint32_t color;
46 if (term->sixel.use_10bit) {
47 if (PIXMAN_FORMAT_TYPE(term->sixel.pixman_fmt) == PIXMAN_TYPE_ARGB)
48 color = 0x3u << 30 | r << 20 | g << 10 | b;
49 else
50 color = 0x3u << 30 | b << 20 | g << 10 | r;
51 } else
52 color = 0xffu << 24 | r << 16 | g << 8 | b;
54 return color;
55}
57void
58sixel_fini(struct terminal *term)
59{
60 free(term->sixel.image.data);
61 free(term->sixel.private_palette);
62 free(term->sixel.shared_palette);
63}
65sixel_put
66sixel_init(struct terminal *term, int p1, int p2, int p3)
67{
68 /*
69 * P1: pixel aspect ratio
70 * - 0,1 - 2:1
71 * - 2 - 5:1
72 * - 3,4 - 3:1
73 * - 5,6 - 2:1
74 * - 7,8,9 - 1:1
75 *
76 * P2: background color mode
77 * - 0|2: empty pixels use current background color
78 * - 1: empty pixels remain at their current color (i.e. transparent)
79 * P3: horizontal grid size - ignored
80 */
82 xassert(term->sixel.image.data == NULL);
83 xassert(term->sixel.palette_size <= SIXEL_MAX_COLORS);
85 /* Default aspect ratio is 2:1 */
86 const int pad = 1;
87 const int pan =
88 (p1 == 2) ? 5 :
89 (p1 == 3 || p1 == 4) ? 3 :
90 (p1 == 7 || p1 == 8 || p1 == 9) ? 1 : 2;
92 LOG_DBG("initializing sixel with "
93 "p1=%d (pan=%d, pad=%d, aspect-ratio=%d:%d), "
94 "p2=%d (transparent=%s), "
95 "p3=%d (ignored)",
96 p1, pan, pad, pan, pad, p2, p2 == 1 ? "yes" : "no", p3);
98 term->sixel.state = SIXEL_DECSIXEL;
99 term->sixel.pos = (struct coord){0, 0};
100 term->sixel.color_idx = 0;
101 term->sixel.pan = pan;
102 term->sixel.pad = pad;
103 term->sixel.param = 0;
104 term->sixel.param_idx = 0;
105 memset(term->sixel.params, 0, sizeof(term->sixel.params));
106 term->sixel.transparent_bg = p2 == 1;
107 term->sixel.image.data = NULL;
108 term->sixel.image.p = NULL;
109 term->sixel.image.width = 0;
110 term->sixel.image.height = 0;
111 term->sixel.image.alloc_height = 0;
112 term->sixel.image.bottom_pixel = 0;
113 term->sixel.linear_blending = wayl_do_linear_blending(term->wl, term->conf);
114 term->sixel.pixman_fmt = PIXMAN_a8r8g8b8;
116 /*
117 * Use higher-precision sixel surfaces if we're using
118 * higher-precision window surfaces.
119 *
120 * This is to a) get more accurate colors when doing gamma-correct
121 * blending, and b) use the same pixman format as the main
122 * surfaces, for (hopefully) better performance.
123 *
124 * For now, don't support 16-bit surfaces (too much sixel logic
125 * that assumes 32-bit pixels).
126 */
127 if (shm_chain_bit_depth(term->render.chains.grid) >= SHM_BITS_10) {
128 if (term->wl->shm_have_argb2101010) {
129 term->sixel.use_10bit = true;
130 term->sixel.pixman_fmt = PIXMAN_a2r10g10b10;
131 }
133 else if (term->wl->shm_have_abgr2101010) {
134 term->sixel.use_10bit = true;
135 term->sixel.pixman_fmt = PIXMAN_a2b10g10r10;
136 }
137 }
139 const size_t active_palette_entries = min(
140 ALEN(term->conf->colors_dark.sixel), term->sixel.palette_size);
142 if (term->sixel.use_private_palette) {
143 xassert(term->sixel.private_palette == NULL);
144 term->sixel.private_palette = xcalloc(
145 term->sixel.palette_size, sizeof(term->sixel.private_palette[0]));
147 memcpy(
148 term->sixel.private_palette, term->conf->colors_dark.sixel,
149 active_palette_entries * sizeof(term->sixel.private_palette[0]));
151 if (term->sixel.linear_blending || term->sixel.use_10bit) {
152 for (size_t i = 0; i < active_palette_entries; i++) {
153 uint8_t r = (term->sixel.private_palette[i] >> 16) & 0xff;
154 uint8_t g = (term->sixel.private_palette[i] >> 8) & 0xff;
155 uint8_t b = (term->sixel.private_palette[i] >> 0) & 0xff;
156 term->sixel.private_palette[i] = color_decode_srgb(term, r, g, b);
157 }
158 }
160 term->sixel.palette = term->sixel.private_palette;
161 } else {
162 if (term->sixel.shared_palette == NULL) {
163 term->sixel.shared_palette = xcalloc(
164 term->sixel.palette_size, sizeof(term->sixel.shared_palette[0]));
166 memcpy(
167 term->sixel.shared_palette, term->conf->colors_dark.sixel,
168 active_palette_entries * sizeof(term->sixel.shared_palette[0]));
170 if (term->sixel.linear_blending || term->sixel.use_10bit) {
171 for (size_t i = 0; i < active_palette_entries; i++) {
172 uint8_t r = (term->sixel.shared_palette[i] >> 16) & 0xff;
173 uint8_t g = (term->sixel.shared_palette[i] >> 8) & 0xff;
174 uint8_t b = (term->sixel.shared_palette[i] >> 0) & 0xff;
175 term->sixel.shared_palette[i] = color_decode_srgb(term, r, g, b);
176 }
177 }
178 } else {
179 /* Shared palette - do *not* reset palette for new sixels */
180 }
182 term->sixel.palette = term->sixel.shared_palette;
183 }
185 count = 0;
186 return pan == 1 && pad == 1 ? &sixel_put_ar_11 : &sixel_put_generic;
187}
189static void
190sixel_invalidate_cache(struct sixel *sixel)
191{
192 if (sixel->scaled.pix != NULL)
193 pixman_image_unref(sixel->scaled.pix);
195 free(sixel->scaled.data);
196 sixel->scaled.pix = NULL;
197 sixel->scaled.data = NULL;
198 sixel->scaled.width = -1;
199 sixel->scaled.height = -1;
201 sixel->pix = NULL;
202 sixel->width = -1;
203 sixel->height = -1;
204}
206void
207sixel_destroy(struct sixel *sixel)
208{
209 sixel_invalidate_cache(sixel);
211 if (sixel->original.pix != NULL)
212 pixman_image_unref(sixel->original.pix);
214 free(sixel->original.data);
215 sixel->original.pix = NULL;
216 sixel->original.data = NULL;
217}
219void
220sixel_destroy_all(struct terminal *term)
221{
222 tll_foreach(term->normal.sixel_images, it)
223 sixel_destroy(&it->item);
224 tll_foreach(term->alt.sixel_images, it)
225 sixel_destroy(&it->item);
226 tll_free(term->normal.sixel_images);
227 tll_free(term->alt.sixel_images);
228}
230static void
231sixel_erase(struct terminal *term, struct sixel *sixel)
232{
233 for (int i = 0; i < sixel->rows; i++) {
234 int r = (sixel->pos.row + i) & (term->grid->num_rows - 1);
236 struct row *row = term->grid->rows[r];
237 if (row == NULL) {
238 /* A resize/reflow may cause row to now be unallocated */
239 continue;
240 }
242 row->dirty = true;
244 for (int c = sixel->pos.col; c < min(sixel->pos.col + sixel->cols, term->cols); c++)
245 row->cells[c].attrs.clean = 0;
246 }
248 sixel_destroy(sixel);
249}
251/*
252 * Verify the sixels are sorted correctly.
253 *
254 * The sixels are sorted on their *end* row, in descending order. This
255 * invariant means the most recent sixels appear first in the list.
256 */
257static void
258verify_list_order(const struct terminal *term)
259{
260#if defined(_DEBUG)
261 int prev_row = INT_MAX;
262 int prev_col = -1;
263 int prev_col_count = 0;
265 /* To aid debugging */
266 size_t UNUSED idx = 0;
268 tll_foreach(term->grid->sixel_images, it) {
269 int row = grid_row_abs_to_sb(
270 term->grid, term->rows, it->item.pos.row + it->item.rows - 1);
271 int col = it->item.pos.col;
272 int col_count = it->item.cols;
274 xassert(row <= prev_row);
276 if (row == prev_row) {
277 /* Allowed to be on the same row only if their columns
278 * don't overlap */
280 xassert(col + col_count <= prev_col ||
281 prev_col + prev_col_count <= col);
282 }
284 prev_row = row;
285 prev_col = col;
286 prev_col_count = col_count;
287 idx++;
288 }
289#endif
290}
292/*
293 * Verifies there aren't any sixels that cross the scrollback
294 * wrap-around. This invariant means a sixel's absolute row numbers
295 * are strictly increasing.
296 */
297static void
298verify_no_wraparound_crossover(const struct terminal *term)
299{
300#if defined(_DEBUG)
301 tll_foreach(term->grid->sixel_images, it) {
302 const struct sixel *six = &it->item;
304 xassert(six->pos.row >= 0);
305 xassert(six->pos.row < term->grid->num_rows);
307 int end = (six->pos.row + six->rows - 1) & (term->grid->num_rows - 1);
308 xassert(end >= six->pos.row);
309 }
310#endif
311}
313/*
314 * Verify there aren't any sixels that cross the scrollback end. This
315 * invariant means a sixel's rebased row numbers are strictly
316 * increasing.
317 */
318static void
319verify_scrollback_consistency(const struct terminal *term)
320{
321#if defined(_DEBUG)
322 tll_foreach(term->grid->sixel_images, it) {
323 const struct sixel *six = &it->item;
325 int last_row = -1;
326 for (int i = 0; i < six->rows; i++) {
327 int row_no = grid_row_abs_to_sb(
328 term->grid, term->rows, six->pos.row + i);
330 if (last_row != -1)
331 xassert(last_row < row_no);
333 last_row = row_no;
334 }
335 }
336#endif
337}
339/*
340 * Verifies no sixel overlap with any other sixels.
341 */
342static void
343verify_no_overlap(const struct terminal *term)
344{
345#if defined(_DEBUG)
346 tll_foreach(term->grid->sixel_images, it) {
347 const struct sixel *six1 = &it->item;
349 pixman_region32_t rect1;
350 pixman_region32_init_rect(
351 &rect1, six1->pos.col, six1->pos.row, six1->cols, six1->rows);
353 tll_foreach(term->grid->sixel_images, it2) {
354 const struct sixel *six2 = &it2->item;
356 if (six1 == six2)
357 continue;
359 pixman_region32_t rect2;
360 pixman_region32_init_rect(
361 &rect2, six2->pos.col,
362 six2->pos.row, six2->cols, six2->rows);
364 pixman_region32_t intersection;
365 pixman_region32_init(&intersection);
366 pixman_region32_intersect(&intersection, &rect1, &rect2);
368 xassert(!pixman_region32_not_empty(&intersection));
370 pixman_region32_fini(&intersection);
371 pixman_region32_fini(&rect2);
372 }
374 pixman_region32_fini(&rect1);
375 }
376#endif
377}
379static void
380verify_sixels(const struct terminal *term)
381{
382 verify_no_wraparound_crossover(term);
383 verify_scrollback_consistency(term);
384 verify_no_overlap(term);
385 verify_list_order(term);
386}
388static void
389sixel_insert(struct terminal *term, struct sixel sixel)
390{
391 int end_row = grid_row_abs_to_sb(
392 term->grid, term->rows, sixel.pos.row + sixel.rows - 1);
394 tll_foreach(term->grid->sixel_images, it) {
395 int rebased = grid_row_abs_to_sb(
396 term->grid, term->rows, it->item.pos.row + it->item.rows - 1);
398 if (rebased < end_row) {
399 tll_insert_before(term->grid->sixel_images, it, sixel);
400 goto out;
401 }
402 }
404 tll_push_back(term->grid->sixel_images, sixel);
406out:
407#if defined(LOG_ENABLE_DBG) && LOG_ENABLE_DBG
408 LOG_DBG("sixel list after insertion:");
409 tll_foreach(term->grid->sixel_images, it) {
410 LOG_DBG(" rows=%d+%d", it->item.pos.row, it->item.rows);
411 }
412#endif
413 verify_sixels(term);
414}
416void
417sixel_scroll_up(struct terminal *term, int rows)
418{
419 if (likely(tll_length(term->grid->sixel_images) == 0))
420 return;
422 tll_rforeach(term->grid->sixel_images, it) {
423 struct sixel *six = &it->item;
425 int six_start = grid_row_abs_to_sb(term->grid, term->rows, six->pos.row);
427 if (six_start < rows) {
428 sixel_erase(term, six);
429 tll_remove(term->grid->sixel_images, it);
430 } else {
431 /*
432 * Unfortunately, we cannot break here.
433 *
434 * The sixels are sorted on their *end* row. This means
435 * there may be a sixel with a top row that will be
436 * scrolled out *anywhere* in the list (think of a huuuuge
437 * sixel that covers the entire scrollback)
438 */
439 //break;
440 }
441 }
443 term->bits_affecting_ascii_printer.sixels =
444 tll_length(term->grid->sixel_images) > 0;
445 term_update_ascii_printer(term);
446 verify_sixels(term);
447}
449void
450sixel_scroll_down(struct terminal *term, int rows)
451{
452 if (likely(tll_length(term->grid->sixel_images) == 0))
453 return;
455 xassert(term->grid->num_rows >= rows);
457 tll_foreach(term->grid->sixel_images, it) {
458 struct sixel *six = &it->item;
460 int six_end = grid_row_abs_to_sb(
461 term->grid, term->rows, six->pos.row + six->rows - 1);
462 if (six_end >= term->grid->num_rows - rows) {
463 sixel_erase(term, six);
464 tll_remove(term->grid->sixel_images, it);
465 } else
466 break;
467 }
469 term->bits_affecting_ascii_printer.sixels =
470 tll_length(term->grid->sixel_images) > 0;
471 term_update_ascii_printer(term);
472 verify_sixels(term);
473}
475static void
476blend_new_image_over_old(const struct terminal *term,
477 const struct sixel *six, pixman_region32_t *six_rect,
478 int row, int col, pixman_image_t **pix, bool *opaque)
479{
480 xassert(pix != NULL);
481 xassert(opaque != NULL);
483 /*
484 * TODO: handle images being emitted with different cell dimensions
485 */
487 const int six_ofs_x = six->pos.col * six->cell_width;
488 const int six_ofs_y = six->pos.row * six->cell_height;
489 const int img_ofs_x = col * six->cell_width;
490 const int img_ofs_y = row * six->cell_height;
491 const int img_width = pixman_image_get_width(*pix);
492 const int img_height = pixman_image_get_height(*pix);
494 pixman_region32_t pix_rect;
495 pixman_region32_init_rect(
496 &pix_rect, img_ofs_x, img_ofs_y, img_width, img_height);
498 /* Blend the intersection between the old and new images */
499 pixman_region32_t intersection;
500 pixman_region32_init(&intersection);
501 pixman_region32_intersect(&intersection, six_rect, &pix_rect);
503 int n_rects = -1;
504 pixman_box32_t *boxes = pixman_region32_rectangles(
505 &intersection, &n_rects);
507 if (n_rects == 0)
508 goto out;
510 xassert(n_rects == 1);
511 pixman_box32_t *box = &boxes[0];
513 if (!*opaque) {
514 /*
515 * New image is transparent - blend on top of the old
516 * sixel image.
517 */
518 pixman_image_composite32(
519 PIXMAN_OP_OVER_REVERSE,
520 six->original.pix, NULL, *pix,
521 box->x1 - six_ofs_x, box->y1 - six_ofs_y,
522 0, 0,
523 box->x1 - img_ofs_x, box->y1 - img_ofs_y,
524 box->x2 - box->x1, box->y2 - box->y1);
525 }
527 /*
528 * Since the old image is split into sub-tiles on a
529 * per-row basis, we need to enlarge the new image and
530 * copy the old image if the old image extends beyond the
531 * new image.
532 *
533 * The "bounding" coordinates are either the edges of the
534 * old image, or the next cell boundary, whichever comes
535 * first.
536 */
537 int bounding_x = six_ofs_x + six->original.width > img_ofs_x + img_width
538 ? min(
539 six_ofs_x + six->original.width,
540 (box->x2 + six->cell_width - 1) / six->cell_width * six->cell_width)
541 : box->x2;
542 int bounding_y = six_ofs_y + six->original.height > img_ofs_y + img_height
543 ? min(
544 six_ofs_y + six->original.height,
545 (box->y2 + six->cell_height - 1) / six->cell_height * six->cell_height)
546 : box->y2;
548 /* The required size of the new image */
549 const int required_width = bounding_x - img_ofs_x;
550 const int required_height = bounding_y - img_ofs_y;
552 const int new_width = max(img_width, required_width);
553 const int new_height = max(img_height, required_height);
555 if (new_width <= img_width && new_height <= img_height)
556 goto out;
558 //LOG_INFO("enlarging: %dx%d -> %dx%d", img_width, img_height, new_width, new_height);
560 if (!six->opaque) {
561 /* Transparency is viral */
562 *opaque = false;
563 }
565 /* Create a new pixmap */
566 int stride = new_width * sizeof(uint32_t);
567 uint32_t *new_data = xmalloc(stride * new_height);
568 pixman_image_t *pix2 = pixman_image_create_bits_no_clear(
569 term->sixel.pixman_fmt, new_width, new_height, new_data, stride);
571#if defined(_DEBUG)
572 /* Fill new image with an easy-to-recognize color (green) */
573 for (size_t i = 0; i < new_width * new_height; i++)
574 new_data[i] = 0xff00ff00;
575#endif
577 /* Copy the new image, from its old pixmap, to the new pixmap */
578 pixman_image_composite32(
579 PIXMAN_OP_SRC,
580 *pix, NULL, pix2, 0, 0, 0, 0, 0, 0, img_width, img_height);
582 /* Copy the bottom tile of the old sixel image into the new pixmap */
583 pixman_image_composite32(
584 PIXMAN_OP_SRC,
585 six->original.pix, NULL, pix2,
586 box->x1 - six_ofs_x, box->y2 - six_ofs_y,
587 0, 0,
588 box->x1 - img_ofs_x, box->y2 - img_ofs_y,
589 bounding_x - box->x1, bounding_y - box->y2);
591 /* Copy the right tile of the old sixel image into the new pixmap */
592 pixman_image_composite32(
593 PIXMAN_OP_SRC,
594 six->original.pix, NULL, pix2,
595 box->x2 - six_ofs_x, box->y1 - six_ofs_y,
596 0, 0,
597 box->x2 - img_ofs_x, box->y1 - img_ofs_y,
598 bounding_x - box->x2, bounding_y - box->y1);
600 /*
601 * Ensure the newly allocated area is initialized.
602 *
603 * Some of it, or all, will have been initialized above, by the
604 * bottom and right tiles from the old sixel image. However, there
605 * may be areas in the new image that isn't covered by the old
606 * image. These areas need to be made transparent.
607 */
608 pixman_region32_t uninitialized;
609 pixman_region32_init_rects(
610 &uninitialized,
611 (const pixman_box32_t []){
612 /* Extended image area on the right side */
613 {img_ofs_x + img_width, img_ofs_y, img_ofs_x + new_width, img_ofs_y + new_height},
615 /* Bottom */
616 {img_ofs_x, img_ofs_y + img_height, img_ofs_x + new_width, img_ofs_y + new_height}},
617 2);
619 /* Subtract the old sixel image, since the area(s) covered by the
620 * old image has already been copied, and *must* not be
621 * overwritten */
622 pixman_region32_t diff;
623 pixman_region32_init(&diff);
624 pixman_region32_subtract(&diff, &uninitialized, six_rect);
626 if (pixman_region32_not_empty(&diff)) {
627 pixman_image_t *src =
628 pixman_image_create_solid_fill(&(pixman_color_t){0});
630 int count = -1;
631 pixman_box32_t *rects = pixman_region32_rectangles(&diff, &count);
633 for (int i = 0; i < count; i++) {
634 pixman_image_composite32(
635 PIXMAN_OP_SRC,
636 src, NULL, pix2,
637 0, 0, 0, 0,
638 rects[i].x1 - img_ofs_x, rects[i].y1 - img_ofs_y,
639 rects[i].x2 - rects[i].x1,
640 rects[i].y2 - rects[i].y1);
641 }
643 pixman_image_unref(src);
644 *opaque = false;
645 }
647 pixman_region32_fini(&diff);
648 pixman_region32_fini(&uninitialized);
650 /* Use the new pixmap in place of the old one */
651 free(pixman_image_get_data(*pix));
652 pixman_image_unref(*pix);
653 *pix = pix2;
655out:
656 pixman_region32_fini(&intersection);
657 pixman_region32_fini(&pix_rect);
658}
660static void
661sixel_overwrite(struct terminal *term, struct sixel *six,
662 int row, int col, int height, int width,
663 pixman_image_t **pix, bool *opaque)
664{
665 pixman_region32_t six_rect;
666 pixman_region32_init_rect(
667 &six_rect,
668 six->pos.col * six->cell_width, six->pos.row * six->cell_height,
669 six->original.width, six->original.height);
671 pixman_region32_t overwrite_rect;
672 pixman_region32_init_rect(
673 &overwrite_rect,
674 col * six->cell_width, row * six->cell_height,
675 width * six->cell_width, height * six->cell_height);
677#if defined(_DEBUG)
678 pixman_region32_t cell_intersection;
679 pixman_region32_init(&cell_intersection);
680 pixman_region32_intersect(&cell_intersection, &six_rect, &overwrite_rect);
681 xassert(!pixman_region32_not_empty(&six_rect) ||
682 pixman_region32_not_empty(&cell_intersection));
683 pixman_region32_fini(&cell_intersection);
684#endif
686 if (pix != NULL)
687 blend_new_image_over_old(term, six, &six_rect, row, col, pix, opaque);
689 pixman_region32_t diff;
690 pixman_region32_init(&diff);
691 pixman_region32_subtract(&diff, &six_rect, &overwrite_rect);
693 pixman_region32_fini(&six_rect);
694 pixman_region32_fini(&overwrite_rect);
696 int n_rects = -1;
697 pixman_box32_t *boxes = pixman_region32_rectangles(&diff, &n_rects);
699 for (int i = 0; i < n_rects; i++) {
700 LOG_DBG("box #%d: x1=%d, y1=%d, x2=%d, y2=%d", i,
701 boxes[i].x1, boxes[i].y1, boxes[i].x2, boxes[i].y2);
703 xassert(boxes[i].x1 % six->cell_width == 0);
704 xassert(boxes[i].y1 % six->cell_height == 0);
706 /* New image's position, in cells */
707 const int new_col = boxes[i].x1 / six->cell_width;
708 const int new_row = boxes[i].y1 / six->cell_height;
710 xassert(new_row < term->grid->num_rows);
712 /* New image's width and height, in pixels */
713 const int new_width = boxes[i].x2 - boxes[i].x1;
714 const int new_height = boxes[i].y2 - boxes[i].y1;
716 uint32_t *new_data = xmalloc(new_width * new_height * sizeof(uint32_t));
717 const uint32_t *old_data = six->original.data;
719 /* Pixel offsets into old image backing memory */
720 const int x_ofs = boxes[i].x1 - six->pos.col * six->cell_width;
721 const int y_ofs = boxes[i].y1 - six->pos.row * six->cell_height;
723 /* Copy image data, one row at a time */
724 for (size_t j = 0; j < new_height; j++) {
725 memcpy(
726 &new_data[(0 + j) * new_width],
727 &old_data[(y_ofs + j) * six->original.width + x_ofs],
728 new_width * sizeof(uint32_t));
729 }
731 pixman_image_t *new_pix = pixman_image_create_bits_no_clear(
732 term->sixel.pixman_fmt, new_width, new_height, new_data, new_width * sizeof(uint32_t));
734 struct sixel new_six = {
735 .pix = NULL,
736 .width = -1,
737 .height = -1,
738 .pos = {.col = new_col, .row = new_row},
739 .cols = (new_width + six->cell_width - 1) / six->cell_width,
740 .rows = (new_height + six->cell_height - 1) / six->cell_height,
741 .opaque = six->opaque,
742 .cell_width = six->cell_width,
743 .cell_height = six->cell_height,
744 .original = {
745 .data = new_data,
746 .pix = new_pix,
747 .width = new_width,
748 .height = new_height,
749 },
750 .scaled = {
751 .data = NULL,
752 .pix = NULL,
753 .width = -1,
754 .height = -1,
755 },
756 };
758#if defined(_DEBUG)
759 /* Assert we don't cross the scrollback wrap-around */
760 const int new_end = new_six.pos.row + new_six.rows - 1;
761 xassert(new_end < term->grid->num_rows);
762#endif
764 sixel_insert(term, new_six);
765 }
767 pixman_region32_fini(&diff);
768}
770/* Row numbers are absolute */
771static void
772_sixel_overwrite_by_rectangle(
773 struct terminal *term, int row, int col, int height, int width,
774 pixman_image_t **pix, bool *opaque)
775{
776 verify_sixels(term);
778#if defined(_DEBUG)
779 pixman_region32_t overwrite_rect;
780 pixman_region32_init_rect(&overwrite_rect, col, row, width, height);
781#endif
783 const int start = row;
784 const int end = row + height - 1;
786 /* We should never generate scrollback wrapping sixels */
787 xassert(end < term->grid->num_rows);
789 const int scrollback_rel_start = grid_row_abs_to_sb(
790 term->grid, term->rows, start);
792 bool UNUSED would_have_breaked = false;
794 tll_foreach(term->grid->sixel_images, it) {
795 struct sixel *six = &it->item;
797 const int six_start = six->pos.row;
798 const int six_end = (six_start + six->rows - 1);
799 const int six_scrollback_rel_end =
800 grid_row_abs_to_sb(term->grid, term->rows, six_end);
802 /* We should never generate scrollback wrapping sixels */
803 xassert(six_end < term->grid->num_rows);
805 if (six_scrollback_rel_end < scrollback_rel_start) {
806 /* All remaining sixels are *before* our rectangle */
807 would_have_breaked = true;
808 break;
809 }
811#if defined(_DEBUG)
812 pixman_region32_t six_rect;
813 pixman_region32_init_rect(&six_rect, six->pos.col, six->pos.row, six->cols, six->rows);
815 pixman_region32_t intersection;
816 pixman_region32_init(&intersection);
817 pixman_region32_intersect(&intersection, &six_rect, &overwrite_rect);
819 const bool collides = pixman_region32_not_empty(&intersection);
820#else
821 const bool UNUSED collides = false;
822#endif
824 if ((start <= six_start && end >= six_start) || /* Crosses sixel start boundary */
825 (start <= six_end && end >= six_end) || /* Crosses sixel end boundary */
826 (start >= six_start && end <= six_end)) /* Fully within sixel range */
827 {
828 const int col_start = six->pos.col;
829 const int col_end = six->pos.col + six->cols - 1;
831 if ((col <= col_start && col + width - 1 >= col_start) ||
832 (col <= col_end && col + width - 1 >= col_end) ||
833 (col >= col_start && col + width - 1 <= col_end))
834 {
835 xassert(!would_have_breaked);
837 struct sixel to_be_erased = *six;
838 tll_remove(term->grid->sixel_images, it);
840 sixel_overwrite(term, &to_be_erased, start, col, height, width,
841 pix, opaque);
842 sixel_erase(term, &to_be_erased);
843 } else
844 xassert(!collides);
845 } else
846 xassert(!collides);
848#if defined(_DEBUG)
849 pixman_region32_fini(&intersection);
850 pixman_region32_fini(&six_rect);
851#endif
852 }
854#if defined(_DEBUG)
855 pixman_region32_fini(&overwrite_rect);
856#endif
857}
859void
860sixel_overwrite_by_rectangle(
861 struct terminal *term, int row, int col, int height, int width)
862{
863 if (likely(tll_length(term->grid->sixel_images) == 0))
864 return;
866 const int start = (term->grid->offset + row) & (term->grid->num_rows - 1);
867 const int end = (start + height - 1) & (term->grid->num_rows - 1);
868 const bool wraps = end < start;
870 if (wraps) {
871 int rows_to_wrap_around = term->grid->num_rows - start;
872 xassert(height - rows_to_wrap_around > 0);
873 _sixel_overwrite_by_rectangle(term, start, col, rows_to_wrap_around, width, NULL, NULL);
874 _sixel_overwrite_by_rectangle(term, 0, col, height - rows_to_wrap_around, width, NULL, NULL);
875 } else
876 _sixel_overwrite_by_rectangle(term, start, col, height, width, NULL, NULL);
878 term->bits_affecting_ascii_printer.sixels =
879 tll_length(term->grid->sixel_images) > 0;
880 term_update_ascii_printer(term);
881}
883/* Row numbers are relative to grid offset */
884void
885sixel_overwrite_by_row(struct terminal *term, int _row, int col, int width)
886{
887 xassert(col >= 0);
889 xassert(_row >= 0);
890 xassert(_row < term->rows);
891 xassert(col >= 0);
892 xassert(col < term->grid->num_cols);
894 if (likely(tll_length(term->grid->sixel_images) == 0))
895 return;
897 if (col + width > term->grid->num_cols)
898 width = term->grid->num_cols - col;
900 const int row = (term->grid->offset + _row) & (term->grid->num_rows - 1);
901 const int scrollback_rel_row = grid_row_abs_to_sb(term->grid, term->rows, row);
903 tll_foreach(term->grid->sixel_images, it) {
904 struct sixel *six = &it->item;
905 const int six_start = six->pos.row;
906 const int six_end = (six_start + six->rows - 1) & (term->grid->num_rows - 1);
908 /* We should never generate scrollback wrapping sixels */
909 xassert(six_end >= six_start);
911 const int six_scrollback_rel_end =
912 grid_row_abs_to_sb(term->grid, term->rows, six_end);
914 if (six_scrollback_rel_end < scrollback_rel_row) {
915 /* All remaining sixels are *before* "our" row */
916 break;
917 }
919 if (row >= six_start && row <= six_end) {
920 const int col_start = six->pos.col;
921 const int col_end = six->pos.col + six->cols - 1;
923 if ((col <= col_start && col + width - 1 >= col_start) ||
924 (col <= col_end && col + width - 1 >= col_end) ||
925 (col >= col_start && col + width - 1 <= col_end))
926 {
927 struct sixel to_be_erased = *six;
928 tll_remove(term->grid->sixel_images, it);
930 sixel_overwrite(term, &to_be_erased, row, col, 1, width, NULL, NULL);
931 sixel_erase(term, &to_be_erased);
932 }
933 }
934 }
936 term->bits_affecting_ascii_printer.sixels =
937 tll_length(term->grid->sixel_images) > 0;
938 term_update_ascii_printer(term);
939}
941void
942sixel_overwrite_at_cursor(struct terminal *term, int width)
943{
944 if (likely(tll_length(term->grid->sixel_images) == 0))
945 return;
947 sixel_overwrite_by_row(
948 term, term->grid->cursor.point.row, term->grid->cursor.point.col, width);
949}
951void
952sixel_cell_size_changed(struct terminal *term)
953{
954 tll_foreach(term->normal.sixel_images, it)
955 sixel_invalidate_cache(&it->item);
957 tll_foreach(term->alt.sixel_images, it)
958 sixel_invalidate_cache(&it->item);
959}
961void
962sixel_sync_cache(const struct terminal *term, struct sixel *six)
963{
964 if (six->pix != NULL) {
965#if defined(_DEBUG)
966 if (six->cell_width == term->cell_width &&
967 six->cell_height == term->cell_height)
968 {
969 xassert(six->pix == six->original.pix);
970 xassert(six->width == six->original.width);
971 xassert(six->height == six->original.height);
973 xassert(six->scaled.data == NULL);
974 xassert(six->scaled.pix == NULL);
975 xassert(six->scaled.width < 0);
976 xassert(six->scaled.height < 0);
977 } else {
978 xassert(six->pix == six->scaled.pix);
979 xassert(six->width == six->scaled.width);
980 xassert(six->height == six->scaled.height);
982 xassert(six->scaled.data != NULL);
983 xassert(six->scaled.pix != NULL);
985 /* TODO: check ratio */
986 xassert(six->scaled.width >= 0);
987 xassert(six->scaled.height >= 0);
988 }
989#endif
990 return;
991 }
993 /* Cache should be invalid */
994 xassert(six->scaled.data == NULL);
995 xassert(six->scaled.pix == NULL);
996 xassert(six->scaled.width < 0);
997 xassert(six->scaled.height < 0);
999 if (six->cell_width == term->cell_width &&
1000 six->cell_height == term->cell_height)
1001 {
1002 six->pix = six->original.pix;
1003 six->width = six->original.width;
1004 six->height = six->original.height;
1005 } else {
1006 const double width_ratio = (double)term->cell_width / six->cell_width;
1007 const double height_ratio = (double)term->cell_height / six->cell_height;
1009 struct pixman_f_transform scale;
1010 pixman_f_transform_init_scale(
1011 &scale, 1. / width_ratio, 1. / height_ratio);
1013 struct pixman_transform _scale;
1014 pixman_transform_from_pixman_f_transform(&_scale, &scale);
1015 pixman_image_set_transform(six->original.pix, &_scale);
1016 pixman_image_set_filter(six->original.pix, PIXMAN_FILTER_BILINEAR, NULL, 0);
1018 int scaled_width = (double)six->original.width * width_ratio;
1019 int scaled_height = (double)six->original.height * height_ratio;
1020 int scaled_stride = scaled_width * sizeof(uint32_t);
1022 LOG_DBG("scaling sixel: %dx%d -> %dx%d",
1023 six->original.width, six->original.height,
1024 scaled_width, scaled_height);
1026 uint8_t *scaled_data = xmalloc(scaled_height * scaled_stride);
1027 pixman_image_t *scaled_pix = pixman_image_create_bits_no_clear(
1028 term->sixel.pixman_fmt, scaled_width, scaled_height,
1029 (uint32_t *)scaled_data, scaled_stride);
1031 pixman_image_composite32(
1032 PIXMAN_OP_SRC, six->original.pix, NULL, scaled_pix, 0, 0, 0, 0,
1033 0, 0, scaled_width, scaled_height);
1035 pixman_image_set_transform(six->original.pix, NULL);
1037 six->scaled.data = scaled_data;
1038 six->scaled.pix = six->pix = scaled_pix;
1039 six->scaled.width = six->width = scaled_width;
1040 six->scaled.height = six->height = scaled_height;
1041 }
1042}
1044void
1045sixel_reflow_grid(struct terminal *term, struct grid *grid)
1046{
1047 /* Meh - the sixel functions we call use term->grid... */
1048 struct grid *active_grid = term->grid;
1049 term->grid = grid;
1051 /* Need the "real" list to be empty from the beginning */
1052 tll(struct sixel) copy = tll_init();
1053 tll_foreach(grid->sixel_images, it)
1054 tll_push_back(copy, it->item);
1055 tll_free(grid->sixel_images);
1057 tll_rforeach(copy, it) {
1058 struct sixel *six = &it->item;
1059 int start = six->pos.row;
1060 int end = (start + six->rows - 1) & (grid->num_rows - 1);
1062 if (end < start) {
1063 /* Crosses scrollback wrap-around */
1064 /* TODO: split image */
1065 sixel_destroy(six);
1066 continue;
1067 }
1069 if (six->rows > grid->num_rows) {
1070 /* Image too large */
1071 /* TODO: keep bottom part? */
1072 sixel_destroy(six);
1073 continue;
1074 }
1076 /* Drop sixels that now cross the current scrollback end
1077 * border. This is similar to a sixel that have been
1078 * scrolled out */
1079 /* TODO: should be possible to optimize this */
1080 bool sixel_destroyed = false;
1081 int last_row = -1;
1083 for (int j = 0; j < six->rows; j++) {
1084 int row_no = grid_row_abs_to_sb(
1085 term->grid, term->rows, six->pos.row + j);
1086 if (last_row != -1 && last_row >= row_no) {
1087 sixel_destroy(six);
1088 sixel_destroyed = true;
1089 break;
1090 }
1092 last_row = row_no;
1093 }
1095 if (sixel_destroyed) {
1096 LOG_WARN("destroyed sixel that now crossed history");
1097 continue;
1098 }
1100 /* Sixels that didn't overlap may now do so, which isn't
1101 * allowed of course */
1102 _sixel_overwrite_by_rectangle(
1103 term, six->pos.row, six->pos.col, six->rows, six->cols,
1104 &it->item.original.pix, &it->item.opaque);
1106 if (it->item.original.data != pixman_image_get_data(it->item.original.pix)) {
1107 it->item.original.data = pixman_image_get_data(it->item.original.pix);
1108 it->item.original.width = pixman_image_get_width(it->item.original.pix);
1109 it->item.original.height = pixman_image_get_height(it->item.original.pix);
1110 it->item.cols = (it->item.original.width + it->item.cell_width - 1) / it->item.cell_width;
1111 it->item.rows = (it->item.original.height + it->item.cell_height - 1) / it->item.cell_height;
1112 sixel_invalidate_cache(&it->item);
1113 }
1115 sixel_insert(term, it->item);
1116 }
1118 tll_free(copy);
1119 term->grid = active_grid;
1120}
1122void
1123sixel_reflow(struct terminal *term)
1124{
1125 for (size_t i = 0; i < 2; i++) {
1126 struct grid *grid = i == 0 ? &term->normal : &term->alt;
1127 sixel_reflow_grid(term, grid);
1128 }
1129}
1131void
1132sixel_unhook(struct terminal *term)
1133{
1134 if (term->sixel.pos.row < term->sixel.image.height &&
1135 term->sixel.pos.row + 6 * term->sixel.pan >= term->sixel.image.height)
1136 {
1137 /*
1138 * Handle case where image has had its size set by raster
1139 * attributes, and then one or more sixels were printed on the
1140 * last row of the RA area.
1141 *
1142 * In this case, the image height may not be a multiple of
1143 * 6*pan. But the printed sixels may still be outside the RA
1144 * area. In this case, using the size from the RA would
1145 * truncate the image.
1146 *
1147 * So, extend the image to a multiple of 6*pan.
1148 *
1149 * If this is a transparent image, the image may get trimmed
1150 * below (most likely back the size set by RA).
1151 */
1152 term->sixel.image.height = term->sixel.image.alloc_height;
1153 }
1155 /* Strip trailing fully transparent rows, *unless* we *ended* with
1156 * a trailing GNL, in which case we do *not* want to strip all 6
1157 * pixel rows */
1158 if (term->sixel.pos.col > 0) {
1159 const int bits = sizeof(term->sixel.image.bottom_pixel) * 8;
1160 const int leading_zeroes = term->sixel.image.bottom_pixel == 0
1161 ? bits
1162 : __builtin_clz(term->sixel.image.bottom_pixel);
1163 const int rows_to_trim = leading_zeroes + 6 - bits;
1165 LOG_DBG("bottom-pixel: 0x%02x, bits=%d, leading-zeroes=%d, "
1166 "rows-to-trim=%d*%d", term->sixel.image.bottom_pixel,
1167 bits, leading_zeroes, rows_to_trim, term->sixel.pan);
1169 /*
1170 * If the current graphical cursor position is at the last row
1171 * of the image, *and* the image is transparent (P2=1), trim
1172 * the entire image.
1173 *
1174 * If the image is not transparent, then we can't trim the RA
1175 * region (it is supposed to "erase", with the current
1176 * background color.)
1177 *
1178 * We *do* "trim" transparent rows from the graphical cursor
1179 * position, as this affects the positioning of the text
1180 * cursor.
1181 *
1182 * See https://raw.githubusercontent.com/hackerb9/vt340test/main/sixeltests/p2effect.sh
1183 */
1184 if (term->sixel.pos.row + 6 * term->sixel.pan >= term->sixel.image.alloc_height) {
1185 LOG_DBG("trimming image");
1186 const int trimmed_height =
1187 term->sixel.image.alloc_height - rows_to_trim * term->sixel.pan;
1189 if (term->sixel.transparent_bg) {
1190 /* Image is transparent - trim as much as possible */
1191 term->sixel.image.height = trimmed_height;
1192 } else {
1193 /* Image is opaque. We can't trim anything "inside"
1194 the RA region */
1195 if (trimmed_height > term->sixel.image.height) {
1196 /* There are non-empty pixels *outside* the RA
1197 region - trim up to that point */
1198 term->sixel.image.height = trimmed_height;
1199 }
1200 }
1201 } else {
1202 LOG_DBG("only adjusting cursor position");
1203 }
1205 term->sixel.pos.row += 6 * term->sixel.pan;
1206 term->sixel.pos.row -= rows_to_trim * term->sixel.pan;
1207 }
1209 int pixel_row_idx = 0;
1210 int pixel_rows_left = term->sixel.image.height;
1211 const int stride = term->sixel.image.width * sizeof(uint32_t);
1213 /*
1214 * When sixel scrolling is enabled (the default), sixels behave
1215 * pretty much like normal output; the sixel starts at the current
1216 * cursor position and the cursor is moved to a point after the
1217 * sixel.
1218 *
1219 * Furthermore, if the sixel reaches the bottom of the scrolling
1220 * region, the terminal content is scrolled.
1221 *
1222 * When scrolling is disabled, sixels always start at (0,0), the
1223 * cursor is not moved at all, and the terminal content never
1224 * scrolls.
1225 */
1227 const bool do_scroll = term->sixel.scrolling;
1229 /* Number of rows we're allowed to use.
1230 *
1231 * When scrolling is enabled, we always allow the entire sixel to
1232 * be emitted.
1233 *
1234 * When disabled, only the number of screen rows may be used. */
1235 int rows_avail = do_scroll
1236 ? (term->sixel.image.height + term->cell_height - 1) / term->cell_height
1237 : term->scroll_region.end;
1239 /* Initial sixel coordinates */
1240 int start_row = do_scroll ? term->grid->cursor.point.row : 0;
1241 const int start_col = do_scroll ? term->grid->cursor.point.col : 0;
1243 /* Total number of rows needed by image */
1244 const int rows_needed =
1245 (term->sixel.image.height + term->cell_height - 1) / term->cell_height;
1247 bool free_image_data = true;
1249 /* We do not allow sixels to cross the scrollback wrap-around, as
1250 * this makes intersection calculations much more complicated */
1251 while (pixel_rows_left > 0 &&
1252 rows_avail > 0 &&
1253 rows_needed <= term->grid->num_rows)
1254 {
1255 const int cur_row = (term->grid->offset + start_row) & (term->grid->num_rows - 1);
1256 const int rows_left_until_wrap_around = term->grid->num_rows - cur_row;
1257 const int usable_rows = min(rows_avail, rows_left_until_wrap_around);
1259 const int pixel_rows_avail = usable_rows * term->cell_height;
1261 const int width = term->sixel.image.width;
1262 const int height = min(pixel_rows_left, pixel_rows_avail);
1264 uint32_t *img_data;
1265 if (pixel_row_idx == 0 && height == pixel_rows_left) {
1266 /* Entire image will be emitted as a single chunk - reuse
1267 * the source buffer */
1268 img_data = term->sixel.image.data;
1269 free_image_data = false;
1270 } else {
1271 xassert(free_image_data);
1272 img_data = xmalloc(height * stride);
1273 memcpy(
1274 img_data,
1275 &((uint8_t *)term->sixel.image.data)[pixel_row_idx * stride],
1276 height * stride);
1277 }
1279 struct sixel image = {
1280 .pix = NULL,
1281 .width = -1,
1282 .height = -1,
1283 .rows = (height + term->cell_height - 1) / term->cell_height,
1284 .cols = (width + term->cell_width - 1) / term->cell_width,
1285 .pos = (struct coord){start_col, cur_row},
1286 .opaque = !term->sixel.transparent_bg,
1287 .cell_width = term->cell_width,
1288 .cell_height = term->cell_height,
1289 .original = {
1290 .data = img_data,
1291 .pix = NULL,
1292 .width = width,
1293 .height = height,
1294 },
1295 .scaled = {
1296 .data = NULL,
1297 .pix = NULL,
1298 .width = -1,
1299 .height = -1,
1300 },
1301 };
1303 xassert(image.rows <= term->grid->num_rows);
1304 xassert(image.pos.row + image.rows - 1 < term->grid->num_rows);
1306 LOG_DBG("generating %s %dx%d pixman image at %d-%d",
1307 image.opaque ? "opaque" : "transparent",
1308 image.original.width, image.original.height,
1309 image.pos.row, image.pos.row + image.rows);
1311 image.original.pix = pixman_image_create_bits_no_clear(
1312 term->sixel.pixman_fmt, image.original.width, image.original.height,
1313 img_data, stride);
1315 pixel_row_idx += height;
1316 pixel_rows_left -= height;
1317 rows_avail -= image.rows;
1319 if (do_scroll) {
1320 /*
1321 * Linefeeds - always one less than the number of rows
1322 * occupied by the image.
1323 *
1324 * Unless this is *not* the last chunk. In that case,
1325 * linefeed past the chunk, so that the next chunk
1326 * "starts" at a "new" row.
1327 */
1328 const int linefeed_count = rows_avail == 0
1329 ? max(0, image.rows - 1)
1330 : image.rows;
1332 xassert(rows_avail == 0 ||
1333 image.original.height % term->cell_height == 0);
1335 for (size_t i = 0; i < linefeed_count; i++)
1336 term_linefeed(term);
1338 /* Position text cursor if this is the last image chunk */
1339 if (rows_avail == 0) {
1340 int row = term->grid->cursor.point.row;
1342 /*
1343 * Position the text cursor based on the text row
1344 * touched by the last sixel
1345 */
1346 const int pixel_rows = pixel_rows_left > 0
1347 ? image.original.height
1348 : term->sixel.pos.row;
1349 const int term_rows =
1350 (pixel_rows + term->cell_height - 1) / term->cell_height;
1352 xassert(term_rows <= image.rows);
1354 row -= (image.rows - term_rows);
1356 term_cursor_to(
1357 term,
1358 max(0, row),
1359 (term->sixel.cursor_right_of_graphics
1360 ? min(image.pos.col + image.cols, term->cols - 1)
1361 : image.pos.col));
1362 }
1364 term->sixel.pos.row -= image.original.height;
1365 }
1367 /* Dirty touched cells, and scroll terminal content if necessary */
1368 for (size_t i = 0; i < image.rows; i++) {
1369 struct row *row = term->grid->rows[cur_row + i];
1370 row->dirty = true;
1372 for (int col = image.pos.col;
1373 col < min(image.pos.col + image.cols, term->cols);
1374 col++)
1375 {
1376 row->cells[col].attrs.clean = 0;
1377 }
1379 }
1381 _sixel_overwrite_by_rectangle(
1382 term, image.pos.row, image.pos.col, image.rows, image.cols,
1383 &image.original.pix, &image.opaque);
1385 if (image.original.data != pixman_image_get_data(image.original.pix)) {
1386 image.original.data = pixman_image_get_data(image.original.pix);
1387 image.original.width = pixman_image_get_width(image.original.pix);
1388 image.original.height = pixman_image_get_height(image.original.pix);
1389 image.cols = (image.original.width + image.cell_width - 1) / image.cell_width;
1390 image.rows = (image.original.height + image.cell_height - 1) / image.cell_height;
1391 sixel_invalidate_cache(&image);
1392 }
1394 sixel_insert(term, image);
1396 if (do_scroll)
1397 start_row = term->grid->cursor.point.row;
1398 else
1399 start_row -= image.rows;
1400 }
1402 if (free_image_data)
1403 free(term->sixel.image.data);
1405 term->sixel.image.data = NULL;
1406 term->sixel.image.p = NULL;
1407 term->sixel.image.width = 0;
1408 term->sixel.image.height = 0;
1409 term->sixel.pos = (struct coord){0, 0};
1411 free(term->sixel.private_palette);
1412 term->sixel.private_palette = NULL;
1414 LOG_DBG("you now have %zu sixels in current grid",
1415 tll_length(term->grid->sixel_images));
1418 term->bits_affecting_ascii_printer.sixels =
1419 tll_length(term->grid->sixel_images) > 0;
1420 term_update_ascii_printer(term);
1421 render_refresh(term);
1422}
1424static void ALWAYS_INLINE inline
1425memset_u32(uint32_t *data, uint32_t value, size_t count)
1426{
1427 static_assert(sizeof(wchar_t) == 4, "wchar_t is not 4 bytes");
1428 wmemset((wchar_t *)data, (wchar_t)value, count);
1429}
1431static void
1432resize_horizontally(struct terminal *term, int new_width_mutable)
1433{
1434 if (unlikely(new_width_mutable > term->sixel.max_width)) {
1435 LOG_WARN("maximum image dimensions exceeded, truncating");
1436 new_width_mutable = term->sixel.max_width;
1437 }
1439 if (unlikely(term->sixel.image.width >= new_width_mutable))
1440 return;
1442 const int sixel_row_height = 6 * term->sixel.pan;
1444 uint32_t *old_data = term->sixel.image.data;
1445 const int old_width = term->sixel.image.width;
1446 const int new_width = new_width_mutable;
1448 int height;
1449 if (unlikely(term->sixel.image.height == 0)) {
1450 /* Lazy initialize height on first printed sixel */
1451 xassert(old_width == 0);
1452 term->sixel.image.height = height = sixel_row_height;
1453 term->sixel.image.alloc_height = sixel_row_height;
1454 } else
1455 height = term->sixel.image.height;
1457 LOG_DBG("resizing image horizontally: %dx(%d) -> %dx(%d)",
1458 term->sixel.image.width, term->sixel.image.height,
1459 new_width, height);
1461 int alloc_height = (height + sixel_row_height - 1) / sixel_row_height * sixel_row_height;
1463 xassert(new_width >= old_width);
1464 xassert(new_width > 0);
1465 xassert(alloc_height > 0);
1467 /* Width (and thus stride) change - need to allocate a new buffer */
1468 uint32_t *new_data = xmalloc(new_width * alloc_height * sizeof(uint32_t));
1470 uint32_t bg = term->sixel.transparent_bg ? 0 : term->sixel.palette[0];
1472 /* Copy old rows, and initialize new columns to background color */
1473 const uint32_t *end = &new_data[alloc_height * new_width];
1474 for (uint32_t *n = new_data, *o = old_data;
1475 n < end;
1476 n += new_width, o += old_width)
1477 {
1478 memcpy(n, o, old_width * sizeof(uint32_t));
1479 memset_u32(&n[old_width], bg, new_width - old_width);
1480 }
1482 free(old_data);
1484 term->sixel.image.data = new_data;
1485 term->sixel.image.width = new_width;
1487 const int ofs = term->sixel.pos.row * new_width + term->sixel.pos.col;
1488 term->sixel.image.p = &term->sixel.image.data[ofs];
1489}
1491static bool
1492resize_vertically(struct terminal *term, const int new_height)
1493{
1494 LOG_DBG("resizing image vertically: (%d)x%d -> (%d)x%d",
1495 term->sixel.image.width, term->sixel.image.height,
1496 term->sixel.image.width, new_height);
1498 if (unlikely(new_height > term->sixel.max_height)) {
1499 LOG_WARN("maximum image dimensions reached");
1500 return false;
1501 }
1503 uint32_t *old_data = term->sixel.image.data;
1504 const int width = term->sixel.image.width;
1505 const int old_height = term->sixel.image.height;
1506 const int sixel_row_height = 6 * term->sixel.pan;
1508 int alloc_height = (new_height + sixel_row_height - 1) / sixel_row_height * sixel_row_height;
1510 xassert(new_height > 0);
1512 if (unlikely(width == 0)) {
1513 xassert(term->sixel.image.data == NULL);
1514 term->sixel.image.height = new_height;
1515 term->sixel.image.alloc_height = alloc_height;
1516 return true;
1517 }
1519 uint32_t *new_data = realloc(
1520 old_data, width * alloc_height * sizeof(uint32_t));
1522 if (new_data == NULL) {
1523 LOG_ERRNO("failed to reallocate sixel image buffer");
1524 return false;
1525 }
1527 const uint32_t bg = term->sixel.transparent_bg ? 0 : term->sixel.palette[0];
1529 memset_u32(&new_data[old_height * width],
1530 bg,
1531 (alloc_height - old_height) * width);
1533 term->sixel.image.height = new_height;
1534 term->sixel.image.alloc_height = alloc_height;
1536 const int ofs =
1537 term->sixel.pos.row * term->sixel.image.width + term->sixel.pos.col;
1539 term->sixel.image.data = new_data;
1540 term->sixel.image.p = &term->sixel.image.data[ofs];
1542 return true;
1543}
1545static bool
1546resize(struct terminal *term, int new_width_mutable, int new_height_mutable)
1547{
1548 LOG_DBG("resizing image: %dx%d -> %dx%d",
1549 term->sixel.image.width, term->sixel.image.height,
1550 new_width_mutable, new_height_mutable);
1552 if (unlikely(new_width_mutable > term->sixel.max_width)) {
1553 LOG_WARN("maximum image width exceeded, truncating");
1554 new_width_mutable = term->sixel.max_width;
1555 }
1557 if (unlikely(new_height_mutable > term->sixel.max_height)) {
1558 LOG_WARN("maximum image height exceeded, truncating");
1559 new_height_mutable = term->sixel.max_height;
1560 }
1562 if (unlikely(new_height_mutable == 0)) {
1563 new_height_mutable = 6 * term->sixel.pan;
1564 }
1566 uint32_t *old_data = term->sixel.image.data;
1567 const int old_width = term->sixel.image.width;
1568 const int old_height = term->sixel.image.height;
1569 const int new_width = new_width_mutable;
1570 const int new_height = new_height_mutable;
1572 if (unlikely(old_width == new_width && old_height == new_height))
1573 return true;
1575 const int sixel_row_height = 6 * term->sixel.pan;
1576 const int alloc_new_height =
1577 (new_height + sixel_row_height - 1) / sixel_row_height * sixel_row_height;
1579 xassert(alloc_new_height >= new_height);
1580 xassert(alloc_new_height - new_height < sixel_row_height);
1582 uint32_t *new_data = NULL;
1583 const uint32_t bg = term->sixel.transparent_bg ? 0 : term->sixel.palette[0];
1585 /*
1586 * If the image is resized horizontally, or if it's opaque, we
1587 * need to explicitly initialize the "new" pixels.
1588 *
1589 * When the image is *not* resized horizontally, we simply do a
1590 * realloc(). In this case, there's no need to manually copy the
1591 * old pixels. We do however need to initialize the new pixels
1592 * since realloc() returns uninitialized memory.
1593 *
1594 * When the image *is* resized horizontally, we need to allocate
1595 * new memory (when the width changes, the stride changes, and
1596 * thus we cannot simply realloc())
1597 *
1598 * If the default background is transparent, the new pixels need
1599 * to be initialized to 0x0. We do this by using calloc().
1600 *
1601 * If the default background is opaque, then we need to manually
1602 * initialize the new pixels.
1603 */
1604 const bool initialize_bg =
1605 !term->sixel.transparent_bg || new_width == old_width;
1607 if (new_width == old_width) {
1608 /* Width (and thus stride) is the same, so we can simply
1609 * re-alloc the existing buffer */
1611 new_data = realloc(old_data, new_width * alloc_new_height * sizeof(uint32_t));
1612 if (new_data == NULL) {
1613 LOG_ERRNO("failed to reallocate sixel image buffer");
1614 return false;
1615 }
1617 xassert(new_height > old_height);
1619 } else {
1620 /* Width (and thus stride) change - need to allocate a new buffer */
1621 xassert(new_width > old_width);
1622 const size_t pixels = new_width * alloc_new_height;
1624 new_data = !initialize_bg
1625 ? xcalloc(pixels, sizeof(uint32_t))
1626 : xmalloc(pixels * sizeof(uint32_t));
1628 /* Copy old rows, and initialize new columns to background color */
1629 const int row_copy_count = min(old_height, alloc_new_height);
1630 const uint32_t *end = &new_data[row_copy_count * new_width];
1632 for (uint32_t *n = new_data, *o = old_data;
1633 n < end;
1634 n += new_width, o += old_width)
1635 {
1636 memcpy(n, o, old_width * sizeof(uint32_t));
1637 memset_u32(&n[old_width], bg, new_width - old_width);
1638 }
1639 free(old_data);
1640 }
1642 if (initialize_bg) {
1643 memset_u32(&new_data[old_height * new_width],
1644 bg,
1645 (alloc_new_height - old_height) * new_width);
1646 }
1648 xassert(new_data != NULL);
1649 term->sixel.image.data = new_data;
1650 term->sixel.image.width = new_width;
1651 term->sixel.image.height = new_height;
1652 term->sixel.image.alloc_height = alloc_new_height;
1653 term->sixel.image.p = &term->sixel.image.data[term->sixel.pos.row * new_width + term->sixel.pos.col];
1655 return true;
1656}
1658static void
1659sixel_add_generic(struct terminal *term, uint32_t *data, int stride, uint32_t color,
1660 uint8_t sixel)
1661{
1662 const int pan = term->sixel.pan;
1664 for (int i = 0; i < 6; i++, sixel >>= 1) {
1665 if (sixel & 1) {
1666 for (int r = 0; r < pan; r++, data += stride)
1667 *data = color;
1668 } else
1669 data += stride * pan;
1670 }
1672 xassert(sixel == 0);
1673}
1675static void ALWAYS_INLINE inline
1676sixel_add_ar_11(struct terminal *term, uint32_t *data, int stride, uint32_t color,
1677 uint8_t sixel)
1678{
1679 xassert(term->sixel.pan == 1);
1681 if (sixel & 0x01)
1682 *data = color;
1683 data += stride;
1684 if (sixel & 0x02)
1685 *data = color;
1686 data += stride;
1687 if (sixel & 0x04)
1688 *data = color;
1689 data += stride;
1690 if (sixel & 0x08)
1691 *data = color;
1692 data += stride;
1693 if (sixel & 0x10)
1694 *data = color;
1695 data += stride;
1696 if (sixel & 0x20)
1697 *data = color;
1698}
1700static void
1701sixel_add_many_generic(struct terminal *term, uint8_t c, unsigned count)
1702{
1703 int col = term->sixel.pos.col;
1704 int width = term->sixel.image.width;
1706 count *= term->sixel.pad;
1708 if (unlikely(col + count - 1 >= width)) {
1709 resize_horizontally(term, col + count);
1710 width = term->sixel.image.width;
1711 count = min(count, max(width - col, 0));
1713 if (unlikely(count == 0))
1714 return;
1715 }
1717 uint32_t color = term->sixel.color;
1718 uint32_t *data = term->sixel.image.p;
1719 uint32_t *end = data + count;
1721 term->sixel.pos.col = col + count;
1722 term->sixel.image.p = end;
1723 term->sixel.image.bottom_pixel |= c;
1725 for (; data < end; data++)
1726 sixel_add_generic(term, data, width, color, c);
1728}
1730static void ALWAYS_INLINE inline
1731sixel_add_one_ar_11(struct terminal *term, uint8_t c)
1732{
1733 xassert(term->sixel.pan == 1);
1734 xassert(term->sixel.pad == 1);
1736 int col = term->sixel.pos.col;
1737 int width = term->sixel.image.width;
1739 if (unlikely(col >= width)) {
1740 resize_horizontally(term, col + count);
1741 width = term->sixel.image.width;
1742 count = min(count, max(width - col, 0));
1744 if (unlikely(count == 0))
1745 return;
1746 }
1748 uint32_t *data = term->sixel.image.p;
1750 term->sixel.pos.col += 1;
1751 term->sixel.image.p += 1;
1752 term->sixel.image.bottom_pixel |= c;
1754 sixel_add_ar_11(term, data, width, term->sixel.color, c);
1755}
1757static void
1758sixel_add_many_ar_11(struct terminal *term, uint8_t c, unsigned count)
1759{
1760 xassert(term->sixel.pan == 1);
1761 xassert(term->sixel.pad == 1);
1763 int col = term->sixel.pos.col;
1764 int width = term->sixel.image.width;
1766 if (unlikely(col + count - 1 >= width)) {
1767 resize_horizontally(term, col + count);
1768 width = term->sixel.image.width;
1769 count = min(count, max(width - col, 0));
1771 if (unlikely(count == 0))
1772 return;
1773 }
1775 uint32_t color = term->sixel.color;
1776 uint32_t *data = term->sixel.image.p;
1777 uint32_t *end = data + count;
1779 term->sixel.pos.col += count;
1780 term->sixel.image.p = end;
1781 term->sixel.image.bottom_pixel |= c;
1783 for (; data < end; data++)
1784 sixel_add_ar_11(term, data, width, color, c);
1786}
1788IGNORE_WARNING("-Wpedantic")
1790static void
1791decsixel_generic(struct terminal *term, uint8_t c)
1792{
1793 switch (c) {
1794 case '"':
1795 term->sixel.state = SIXEL_DECGRA;
1796 term->sixel.param = 0;
1797 term->sixel.param_idx = 0;
1798 break;
1800 case '!':
1801 term->sixel.state = SIXEL_DECGRI;
1802 term->sixel.param = 0;
1803 term->sixel.param_idx = 0;
1804 term->sixel.repeat_count = 1;
1805 break;
1807 case '#':
1808 term->sixel.state = SIXEL_DECGCI;
1809 term->sixel.color_idx = 0;
1810 term->sixel.param = 0;
1811 term->sixel.param_idx = 0;
1812 break;
1814 case '$':
1815 if (likely(term->sixel.pos.col <= term->sixel.max_width)) {
1816 /*
1817 * We set, and keep, 'col' outside the image boundary when
1818 * we've reached the maximum image height, to avoid also
1819 * having to check the row vs image height in the common
1820 * path in sixel_add().
1821 */
1822 term->sixel.pos.col = 0;
1823 term->sixel.image.p = &term->sixel.image.data[term->sixel.pos.row * term->sixel.image.width];
1824 }
1825 break;
1827 case '-': /* GNL - Graphical New Line */
1828 term->sixel.pos.row += 6 * term->sixel.pan;
1829 term->sixel.pos.col = 0;
1830 term->sixel.image.bottom_pixel = 0;
1831 term->sixel.image.p = &term->sixel.image.data[term->sixel.pos.row * term->sixel.image.width];
1833 if (term->sixel.pos.row >= term->sixel.image.alloc_height) {
1834 if (!resize_vertically(term, term->sixel.pos.row + 6 * term->sixel.pan))
1835 term->sixel.pos.col = term->sixel.max_width + 1 * term->sixel.pad;
1836 }
1837 break;
1839 case '?' ... '~':
1840 sixel_add_many_generic(term, c - 63, 1);
1841 break;
1843 case ' ':
1844 case '\n':
1845 case '\r':
1846 break;
1848 default:
1849 LOG_WARN("invalid sixel character: '%c' at idx=%zu", c, count);
1850 break;
1851 }
1852}
1854UNIGNORE_WARNINGS
1856static void
1857decsixel_ar_11(struct terminal *term, uint8_t c)
1858{
1859 if (likely(c >= '?' && c <= '~'))
1860 sixel_add_one_ar_11(term, c - 63);
1861 else
1862 decsixel_generic(term, c);
1863}
1865static void
1866decgra(struct terminal *term, uint8_t c)
1867{
1868 switch (c) {
1869 case '0': case '1': case '2': case '3': case '4':
1870 case '5': case '6': case '7': case '8': case '9':
1871 term->sixel.param *= 10;
1872 term->sixel.param += c - '0';
1873 break;
1875 case ';':
1876 if (term->sixel.param_idx < ALEN(term->sixel.params))
1877 term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
1878 term->sixel.param = 0;
1879 break;
1881 default: {
1882 if (term->sixel.param_idx < ALEN(term->sixel.params))
1883 term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
1885 int nparams = term->sixel.param_idx;
1886 unsigned pan = nparams > 0 ? term->sixel.params[0] : 0;
1887 unsigned pad = nparams > 1 ? term->sixel.params[1] : 0;
1888 unsigned ph = nparams > 2 ? term->sixel.params[2] : 0;
1889 unsigned pv = nparams > 3 ? term->sixel.params[3] : 0;
1891 pan = pan > 0 ? min(pan, 5) : 1;
1892 pad = pad > 0 ? min(pad, 5) : 1;
1894 if (likely(term->sixel.image.width == 0 &&
1895 term->sixel.image.height == 0))
1896 {
1897 term->sixel.pan = pan;
1898 term->sixel.pad = pad;
1899 } else {
1900 /*
1901 * Unsure what the VT340 does...
1902 *
1903 * We currently do *not* handle changing pan/pad in the
1904 * middle of a sixel, since that means resizing/stretching
1905 * the existing image.
1906 *
1907 * I'm *guessing* the VT340 simply changes the aspect
1908 * ratio of all subsequent sixels. But, given the design
1909 * of our implementation (the entire sixel is written to a
1910 * single pixman image), we can't easily do that.
1911 */
1912 LOG_WARN("sixel: unsupported: pan/pad changed after printing sixels");
1913 pan = term->sixel.pan;
1914 pad = term->sixel.pad;
1915 }
1917 pv *= pan;
1918 ph *= pad;
1920 LOG_DBG("pan=%u, pad=%u (aspect ratio = %d:%d), size=%ux%u",
1921 pan, pad, pan, pad, ph, pv);
1923 /*
1924 * RA really only acts as a rectangular erase - it fills the
1925 * specified area with the sixel background color[^1]. Nothing
1926 * else. It does *not* affect cursor positioning.
1927 *
1928 * This means that if the emitted sixel is *smaller* than the
1929 * RA, the text cursor will be placed "inside" the RA area.
1930 *
1931 * This means it would be more correct to view the RA area as
1932 * a *separate* sixel image, that is then overlaid with the
1933 * actual sixel.
1934 *
1935 * Still, RA _is_ a hint - the final image is _likely_ going
1936 * to be this large. And, treating RA as a separate image
1937 * prevents us from pre-allocating the final sixel image.
1938 *
1939 * So we don't. We use the RA as a hint, and pre-allocates the
1940 * backing image buffer.
1941 *
1942 * [^1]: i.e. it's a NOP if the sixel is transparent
1943 */
1944 if (ph >= term->sixel.image.height && pv >= term->sixel.image.width &&
1945 ph <= term->sixel.max_height && pv <= term->sixel.max_width)
1946 {
1947 /*
1948 * TODO: always resize to a multiple of 6*pan?
1949 *
1950 * We're effectively doing that already, except
1951 * sixel.image.height is set to ph, instead of the
1952 * allocated height (which is always a multiple of 6*pan).
1953 *
1954 * If the user wants to emit a sixel that isn't a multiple
1955 * of 6 pixels, the bottom sixel rows should all be empty,
1956 * and (assuming a transparent sixel), trimmed when the
1957 * final image is generated.
1958 */
1959 resize(term, ph, pv);
1960 }
1962 term->sixel.state = SIXEL_DECSIXEL;
1964 /* Update DCS put handler, since pan/pad may have changed */
1965 term->vt.dcs.put_handler = pan == 1 && pad == 1
1966 ? &sixel_put_ar_11
1967 : &sixel_put_generic;
1969 if (likely(pan == 1 && pad == 1))
1970 decsixel_ar_11(term, c);
1971 else
1972 decsixel_generic(term, c);
1974 break;
1975 }
1976 }
1977}
1979IGNORE_WARNING("-Wpedantic")
1981static void
1982decgri_generic(struct terminal *term, uint8_t c)
1983{
1984 switch (c) {
1985 case '0': case '1': case '2': case '3': case '4':
1986 case '5': case '6': case '7': case '8': case '9': {
1987 unsigned param = term->sixel.param;
1988 param *= 10;
1989 param += c - '0';
1990 term->sixel.repeat_count = term->sixel.param = param;
1991 break;
1992 }
1994 case '?' ... '~': {
1995 unsigned count = term->sixel.repeat_count;
1996 if (unlikely(count == 0)) {
1997 count = 1;
1998 }
2000 sixel_add_many_generic(term, c - 63, count);
2001 term->sixel.state = SIXEL_DECSIXEL;
2002 break;
2003 }
2005 default:
2006 term->sixel.state = SIXEL_DECSIXEL;
2007 term->vt.dcs.put_handler(term, c);
2008 break;
2009 }
2010}
2012UNIGNORE_WARNINGS
2014static void
2015decgri_ar_11(struct terminal *term, uint8_t c)
2016{
2017 if (likely(c >= '?' && c <= '~')) {
2018 unsigned count = term->sixel.repeat_count;
2019 if (unlikely(count == 0)) {
2020 count = 1;
2021 }
2023 sixel_add_many_ar_11(term, c - 63, count);
2024 term->sixel.state = SIXEL_DECSIXEL;
2025 } else
2026 decgri_generic(term, c);
2027}
2029static void
2030decgci(struct terminal *term, uint8_t c)
2031{
2032 switch (c) {
2033 case '0': case '1': case '2': case '3': case '4':
2034 case '5': case '6': case '7': case '8': case '9':
2035 term->sixel.param *= 10;
2036 term->sixel.param += c - '0';
2037 break;
2039 case ';':
2040 if (term->sixel.param_idx < ALEN(term->sixel.params))
2041 term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
2042 term->sixel.param = 0;
2043 break;
2045 default: {
2046 if (term->sixel.param_idx < ALEN(term->sixel.params))
2047 term->sixel.params[term->sixel.param_idx++] = term->sixel.param;
2049 int nparams = term->sixel.param_idx;
2051 if (nparams > 0)
2052 term->sixel.color_idx = min(term->sixel.params[0], term->sixel.palette_size - 1);
2054 if (nparams > 4) {
2055 unsigned format = term->sixel.params[1];
2056 int c1 = term->sixel.params[2];
2057 int c2 = term->sixel.params[3];
2058 int c3 = term->sixel.params[4];
2060 switch (format) {
2061 case 1: { /* HLS */
2062 int hue = min(c1, 360);
2063 int lum = min(c2, 100);
2064 int sat = min(c3, 100);
2066 /*
2067 * Sixel's HLS use the following primary color hues:
2068 * blue: 0°
2069 * red: 120°
2070 * green: 240°
2071 *
2072 * While "standard" HSL uses:
2073 * red: 0°
2074 * green: 120°
2075 * blue: 240°
2076 */
2077 hue = (hue + 240) % 360;
2079 uint32_t rgb = hsl_to_rgb(hue, sat, lum);
2081 LOG_DBG("setting palette #%d = HLS %hhu/%hhu/%hhu (0x%06x)",
2082 term->sixel.color_idx, hue, lum, sat, rgb);
2084 term->sixel.palette[term->sixel.color_idx] = 0xffu << 24 | rgb;
2085 break;
2086 }
2088 case 2: { /* RGB */
2089 uint16_t r = 255 * min(c1, 100) / 100;
2090 uint16_t g = 255 * min(c2, 100) / 100;
2091 uint16_t b = 255 * min(c3, 100) / 100;
2093 LOG_DBG("setting palette #%d = RGB %hu/%hu/%hu",
2094 term->sixel.color_idx, r, g, b);
2096 term->sixel.palette[term->sixel.color_idx] = color_decode_srgb(term, r, g, b);
2097 break;
2098 }
2099 }
2100 } else
2101 term->sixel.color = term->sixel.palette[term->sixel.color_idx];
2103 term->sixel.state = SIXEL_DECSIXEL;
2105 if (likely(term->sixel.pan == 1 && term->sixel.pad == 1))
2106 decsixel_ar_11(term, c);
2107 else
2108 decsixel_generic(term, c);
2109 break;
2110 }
2111 }
2112}
2114static void
2115sixel_put_generic(struct terminal *term, uint8_t c)
2116{
2117 switch (term->sixel.state) {
2118 case SIXEL_DECSIXEL: decsixel_generic(term, c); break;
2119 case SIXEL_DECGRA: decgra(term, c); break;
2120 case SIXEL_DECGRI: decgri_generic(term, c); break;
2121 case SIXEL_DECGCI: decgci(term, c); break;
2122 }
2124 count++;
2125}
2127static void
2128sixel_put_ar_11(struct terminal *term, uint8_t c)
2129{
2130 switch (term->sixel.state) {
2131 case SIXEL_DECSIXEL: decsixel_ar_11(term, c); break;
2132 case SIXEL_DECGRA: decgra(term, c); break;
2133 case SIXEL_DECGRI: decgri_ar_11(term, c); break;
2134 case SIXEL_DECGCI: decgci(term, c); break;
2135 }
2137 count++;
2138}
2140void
2141sixel_colors_report_current(struct terminal *term)
2142{
2143 char reply[24];
2144 size_t n = xsnprintf(reply, sizeof(reply), "\033[?1;0;%uS", term->sixel.palette_size);
2145 term_to_slave(term, reply, n);
2146 LOG_DBG("query response for current color count: %u", term->sixel.palette_size);
2147}
2149void
2150sixel_colors_reset(struct terminal *term)
2151{
2152 LOG_DBG("sixel palette size reset to %u", SIXEL_MAX_COLORS);
2154 free(term->sixel.palette);
2155 term->sixel.palette = NULL;
2157 term->sixel.palette_size = SIXEL_MAX_COLORS;
2158 sixel_colors_report_current(term);
2159}
2161void
2162sixel_colors_set(struct terminal *term, unsigned count)
2163{
2164 unsigned new_palette_size = min(max(2, count), SIXEL_MAX_COLORS);
2165 LOG_DBG("sixel palette size set to %u", new_palette_size);
2167 free(term->sixel.private_palette);
2168 free(term->sixel.shared_palette);
2169 term->sixel.private_palette = NULL;
2170 term->sixel.shared_palette = NULL;
2172 term->sixel.palette_size = new_palette_size;
2173 sixel_colors_report_current(term);
2174}
2176void
2177sixel_colors_report_max(struct terminal *term)
2178{
2179 char reply[24];
2180 size_t n = xsnprintf(reply, sizeof(reply), "\033[?1;0;%uS", SIXEL_MAX_COLORS);
2181 term_to_slave(term, reply, n);
2182 LOG_DBG("query response for max color count: %u", SIXEL_MAX_COLORS);
2183}
2185void
2186sixel_geometry_report_current(struct terminal *term)
2187{
2188 char reply[64];
2189 size_t n = xsnprintf(reply, sizeof(reply), "\033[?2;0;%u;%uS",
2190 min(term->cols * term->cell_width, term->sixel.max_width),
2191 min(term->rows * term->cell_height, term->sixel.max_height));
2192 term_to_slave(term, reply, n);
2194 LOG_DBG("query response for current sixel geometry: %ux%u",
2195 term->sixel.max_width, term->sixel.max_height);
2196}
2198void
2199sixel_geometry_reset(struct terminal *term)
2200{
2201 LOG_DBG("sixel geometry reset to %ux%u", SIXEL_MAX_WIDTH, SIXEL_MAX_HEIGHT);
2202 term->sixel.max_width = SIXEL_MAX_WIDTH;
2203 term->sixel.max_height = SIXEL_MAX_HEIGHT;
2204 sixel_geometry_report_current(term);
2205}
2207void
2208sixel_geometry_set(struct terminal *term, unsigned width, unsigned height)
2209{
2210 LOG_DBG("sixel geometry set to %ux%u", width, height);
2211 term->sixel.max_width = width;
2212 term->sixel.max_height = height;
2213 sixel_geometry_report_current(term);
2214}
2216void
2217sixel_geometry_report_max(struct terminal *term)
2218{
2219 unsigned max_width = term->sixel.max_width;
2220 unsigned max_height = term->sixel.max_height;
2222 char reply[64];
2223 size_t n = xsnprintf(reply, sizeof(reply), "\033[?2;0;%u;%uS", max_width, max_height);
2224 term_to_slave(term, reply, n);
2226 LOG_DBG("query response for max sixel geometry: %ux%u",
2227 max_width, max_height);
2228}
Diagnostics5 git calls · 1.5 s
CallStatusSizeTime01.5 s
discover200178 B3 ms
git.upload-pack discover http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=3 git.response.bytes=178 git.server.agent=git/2.56.1-Linux git.server.object_format=sha1 git.server.caps=agent ls-refs fetch server-option object-format git.server.fetch=shallow wait-for-done filter git.protocol.version=2 git.pkt.frames=9 git.pkt.data=7 git.pkt.flush=2 git.pkt.delim=0 git.pkt.bytes=178 git.pkt.payload.max=35
ls-refs20010 KiB463 ms
git.upload-pack ls-refs http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=ls-refs git.upstream.dur_ms=452 git.response.bytes=10322 git.response.cache=EXPIRED git.response.duration_ms=463 git.protocol.version=2 git.pkt.frames=108 git.pkt.data=107 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=10322 git.pkt.payload.max=106 git.ls_refs.count=107 git.ls_refs.head=refs/heads/master
fetchcombine:blob:none+tree:2 · deepen 12008.6 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=8830 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=combine:blob:none+tree:2 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=1 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=8830 git.pkt.payload.max=8730 git.sideband.band1.bytes=8730 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=14 git.pack.bytes=8730 git.fetch.wide.objects=14 git.fetch.wide.extra_bytes=9909 git.fetch.wide.blobs=0
fetchtree:0 · deepen 102009.3 KiB · hit2 ms
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=2 git.response.bytes=9489 git.response.cache=HIT git.response.duration_ms=2 git.fetch.filter=tree:0 git.fetch.wants=c0d2c3e95d74cb60526ff28aec9f34ee3df545e0 git.fetch.haves.count=0 git.fetch.deepen=10 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=7 git.pkt.data=5 git.pkt.flush=1 git.pkt.delim=1 git.pkt.bytes=9489 git.pkt.payload.max=9389 git.sideband.band1.bytes=9389 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0 git.pack.objects=10 git.pack.bytes=9389 git.pack.ofs_delta=0 git.pack.ref_delta=0 git.pack.chain.max=0 git.pack.trailer=de930681b6073ba5b599a0f956bfbd114762ad5a git.pack.full=10Open pack map
fetch20015 KiB1.0 s
git.upload-pack fetch http.response.status_code=200 git.upstream.cf_ray=- git.upstream.phase=fetch git.upstream.dur_ms=1042 git.response.bytes=15040 git.response.cache=MISS git.response.duration_ms=1042 git.fetch.wants=2bf47b7eef57d65ae2a5b787c5bd29ba838ab5f9 git.fetch.haves.count=0 git.fetch.deepen=0 git.fetch.thin_pack=false git.fetch.ofs_delta=true git.protocol.version=2 git.pkt.frames=4 git.pkt.data=3 git.pkt.flush=1 git.pkt.delim=0 git.pkt.bytes=15040 git.pkt.payload.max=15013 git.sideband.band1.bytes=15013 git.sideband.band2.bytes=0 git.sideband.band3.bytes=0
Each row is one HTTP round trip ply made to the git server for this page, on one timeline. discover asks what the server speaks, ls-refs lists branch tips, fetch brings objects by id. A hit was served by Workers Cache. Tap a row for its wire details.
- Remote
- https://codeberg.org/dnkl/foot
- Ref
- refs/heads/master
- Commit
- c0d2c3e95d74cb60526ff28aec9f34ee3df545e0
- cf-ray
- a5e273a909ecd806
- Request
- 034c97e8…
- Protocol
- v2 · sha1
- Agent
- git/2.56.1-Linux
- Filter
- advertised
- Fetch
- shallow wait-for-done filter
- Capabilities
- agent ls-refs fetch server-option object-format