-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathsixel-canvas.cc
174 lines (153 loc) · 6.8 KB
/
sixel-canvas.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// -*- mode: c++; c-basic-offset: 4; indent-tabs-mode: nil; -*-
// (c) 2023 Henner Zeller <h.zeller@acm.org>
//
// 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 version 2.
//
// 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://gnu.org/licenses/gpl-2.0.txt>
#include "sixel-canvas.h"
#include <sixel.h>
#include <algorithm>
#include <cassert>
#include <cstring>
#include <functional>
#include <memory>
#include "buffered-write-sequencer.h"
#include "display-options.h"
#include "framebuffer.h"
#include "term-query.h"
#include "terminal-canvas.h"
#include "thread-pool.h"
#include "timg-time.h"
#define CSI "\033["
namespace timg {
SixelCanvas::SixelCanvas(BufferedWriteSequencer *ws, ThreadPool *thread_pool,
const SixelOptions &sixel_options,
const DisplayOptions &display_opts)
: TerminalCanvas(ws),
options_(display_opts),
full_cell_jump_(sixel_options.full_cell_jump),
executor_(thread_pool) {
// Terminals might have different understanding where the curosr is placed
// after an image is sent.
// Apparently the original dec terminal placed it afterwards, but some
// terminals now have also place the cursor in the _next_ line.
//
// There seem to be two DECSET settings 7730 and 8452 which influence that,
// ... and not all terminals implement all of these. Also xterm apparently
// implements CIS 80 backwards ?
//
// So the below is an attempt to get it working on all of these.
// Assuming that xterm is slightly less common these days, but a bunch of
// modern terminals are built based on libvte (e.g. gnome-terminal,
// xfce4-terminal,..), let's go with that in the common case and try
// to detect the exceptions in the graphics query.
//
// To test: test with one animation and some images with --grid=3x2 --title
//
// Also see:
// * https://vt100.net/dec/ek-vt382-rm-001.pdf#page=112
// * https://vt100.net/dec/ek-vt38t-ug-001.pdf#page=132
// Plese send PR or issue if you know a less ugly way to deal with this.
if (!sixel_options.known_broken_cursor_placement) {
//** The default way of doing things; works with most terminals.
// works: konsole, mlterm, libvte-based, alacritty-sixel
// breaks: xterm, wezterm
cursor_move_before_ = CSI "80h" CSI "?7730h" CSI "?8452l";
cursor_move_after_ = "\r";
}
else {
//** The workaround enabled for xterm and wezterm.
// works: xterm, mlterm, wezterm, alacritty-sixel
// break: konsole, libvte-based
cursor_move_before_ = CSI "80l" CSI "?7730l" CSI "?8452h";
cursor_move_after_ = "\n";
}
}
// Char needs to be non-const to be compatible sixel-callback.
static int WriteToOutBuffer(char *data, int size, void *outbuf_param) {
OutBuffer *outbuffer = (OutBuffer *)outbuf_param;
// TODO realloc
memcpy(outbuffer->data + outbuffer->size, data, size);
outbuffer->size += size;
return size;
}
static inline int round_to_sixel(int pixels) {
pixels += 5;
return pixels - pixels % 6;
}
static void WriteStringToOutBuffer(const char *str, OutBuffer *outbuffer) {
WriteToOutBuffer(const_cast<char *>(str), strlen(str), outbuffer);
}
void SixelCanvas::Send(int x, int dy, const Framebuffer &fb_orig,
SeqType seq_type, Duration end_of_frame) {
if (dy < 0) {
MoveCursorDY(cell_height_for_pixels(dy));
}
MoveCursorDX(x / options_.cell_x_px);
// Create copy to be used in threads.
// Round height to next possible sixel cut-off treat the remaining strip
// at the bottom as transparent.
Framebuffer *const fb =
new Framebuffer(fb_orig.width(), round_to_sixel(fb_orig.height()));
// First, make it transparent with whatever choosen (ideally, we only
// do the last couple of rows)
fb->AlphaComposeBackground(
options_.bgcolor_getter, options_.bg_pattern_color,
options_.pattern_size * options_.cell_x_px,
options_.pattern_size * options_.cell_y_px / 2, fb_orig.height());
// .. overwrite with whatever is in the orig.
std::copy(fb_orig.begin(), fb_orig.end(), fb->begin());
// TODO: this should be realloced as needed.
char *const buffer = new char[1024 + fb->width() * fb->height() * 5];
char *const offset = AppendPrefixToBuffer(buffer);
// avoid capture whole 'this', so copy values locally
const char *const cursor_handling_start = cursor_move_before_;
const char *const cursor_handling_end = cursor_move_after_;
const std::function<OutBuffer()> encode_fun =
[fb, buffer, offset, cursor_handling_start, cursor_handling_end]() {
std::unique_ptr<const Framebuffer> auto_delete(fb);
OutBuffer out(buffer, offset - buffer);
WriteStringToOutBuffer(cursor_handling_start, &out);
sixel_output_t *sixel_out = nullptr;
sixel_output_new(&sixel_out, WriteToOutBuffer, &out, nullptr);
sixel_dither_t *sixel_dither = nullptr;
sixel_dither_new(&sixel_dither, 256, nullptr);
sixel_dither_initialize(
sixel_dither, (unsigned char *)fb->begin(), fb->width(),
fb->height(), SIXEL_PIXELFORMAT_RGBA8888, SIXEL_LARGE_LUM,
SIXEL_REP_AVERAGE_COLORS, SIXEL_QUALITY_AUTO);
sixel_encode((unsigned char *)fb->begin(), fb->width(),
fb->height(), 0, sixel_dither, sixel_out);
sixel_dither_destroy(sixel_dither);
sixel_output_destroy(sixel_out);
WriteStringToOutBuffer(cursor_handling_end, &out);
return out;
};
write_sequencer_->WriteBuffer(executor_->ExecAsync(encode_fun), seq_type,
end_of_frame);
}
int SixelCanvas::cell_height_for_pixels(int pixels) const {
assert(pixels <= 0); // Currently only use-case
pixels = -pixels;
if (full_cell_jump_) {
// https://github.com/hzeller/timg/issues/145#issuecomment-2579962760
// As DEC intended.
return -((round_to_sixel(pixels) - 6) / options_.cell_y_px + 1);
}
else {
// Unlike the other exact pixel canvases where we have to round to the
// next even cell_y_px, here we first need to round to the next even 6
// first.
return -((round_to_sixel(pixels) + options_.cell_y_px - 1) /
options_.cell_y_px);
}
}
} // namespace timg