Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image support #2503

Closed
wants to merge 162 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
162 commits
Select commit Hold shift + click to select a range
b2352ea
working sixel draft
jerch Oct 24, 2019
11b1458
fix linter and @types/node dep
jerch Oct 24, 2019
4f31ba9
install addon deps in CI
jerch Oct 24, 2019
435e2c4
fix linter error from addon dep
jerch Oct 24, 2019
aa11cfa
Merge branch 'master' into addon-images
jerch Oct 24, 2019
81946b1
Merge branch 'master' into addon-images
jerch Oct 25, 2019
502562e
Merge branch 'addon_build' into addon-images
jerch Oct 25, 2019
02c55d8
revert CI pipeline changes
jerch Oct 25, 2019
912ccb3
restrict linter to addons/*/src files
jerch Oct 25, 2019
9540037
Merge branch 'addon_build' into addon-images
jerch Oct 25, 2019
2a6d4f0
Merge branch 'addon_build' into addon-images
jerch Oct 26, 2019
c65f676
Merge branch 'master' into addon-images
jerch Oct 26, 2019
3c5c8be
Merge branch 'master' into addon-images
jerch Oct 29, 2019
e147673
3x speedup with low level SIXEL handler
jerch Oct 29, 2019
e128e40
Merge branch 'addon-images' of github.com:jerch/xterm.js into addon-i…
jerch Sep 22, 2020
b17dc31
Merge branch 'master' into addon-images
jerch Sep 22, 2020
b1193aa
fix incompatible type declaration
jerch Sep 22, 2020
091c421
make linter happy
jerch Sep 22, 2020
715143c
use ImageBitmap internally, remove different renderer hacks
jerch Sep 27, 2020
ab63ddd
Merge remote-tracking branch 'upstream/master' into addon-images
jerch Sep 27, 2020
750a077
Merge branch 'master' into addon-images
jerch Oct 24, 2020
dfc1ddc
Merge branch 'master' into addon-images
jerch Oct 24, 2020
988c384
remove marker overload
jerch Oct 24, 2020
4a6ba2b
Merge branch 'master' into addon-images
jerch Oct 31, 2020
9c79dff
own imagerenderer layer to work with all render types
jerch Nov 1, 2020
f981f13
- sixel handler init with params
jerch Nov 8, 2020
9f90781
Merge branch 'master' into addon-images
jerch Nov 8, 2020
a67b81b
fix tsconfig issues
jerch Nov 8, 2020
eb52952
refactor to a more modular approach
jerch Nov 8, 2020
9e3f284
multiple changes:
jerch Nov 10, 2020
8954f17
changes:
jerch Nov 11, 2020
fa29fc9
update readme and exported types
jerch Nov 11, 2020
d7a4991
changes:
jerch Nov 11, 2020
3bf8e62
Merge branch 'master' into addon-images
jerch Nov 11, 2020
80e77ef
fix eslint OOM by merging tsconfig lookups
jerch Nov 11, 2020
ba2d27c
skip failing test for now
jerch Nov 11, 2020
9a67407
fix leftover artefacts on image layer
jerch Nov 12, 2020
a32a2d1
add DA1 overload and xterm GA sequence, fix SIXEL bg handling
jerch Nov 12, 2020
1a2066e
implement a max size storage limit with hard eviction rule
jerch Nov 12, 2020
00e8ade
add placeholder option for deleted images
jerch Nov 13, 2020
df48f27
couple of eviction rules:
jerch Nov 14, 2020
896dfd1
expose some settings on demo page
jerch Nov 15, 2020
2399310
add sixel example file
jerch Nov 15, 2020
07a2d24
multiple WIP changes:
jerch Nov 16, 2020
b57646f
quickhack to export image as blob on ctrl+click
jerch Jan 19, 2021
18855d1
Merge branch 'master' into addon-images
jerch Jan 31, 2021
43f84c1
fix changed handler hook
jerch Jan 31, 2021
8a8fcbd
fix linter
jerch Jan 31, 2021
d8fef13
Merge branch 'master' into addon-images
jerch Feb 7, 2021
dbba13b
Merge branch 'master' into addon-images
jerch Mar 19, 2021
6d8bd8a
remove iTerm2 support and own protocol draft
jerch Mar 19, 2021
1ff4799
add example pictures as fixtures
jerch Mar 19, 2021
daf3a58
make linter happy
jerch Mar 19, 2021
82cbbd1
worker based sixel decoding
jerch Mar 21, 2021
eeeb8f6
Merge branch 'master' into addon-images
jerch Mar 21, 2021
411078c
use proper type inference on messages
jerch Mar 21, 2021
4180f6e
better worker integration:
jerch Mar 21, 2021
9514225
basic private accessor test
jerch Mar 21, 2021
ced99a5
update addon requirements, fix bundling
jerch Mar 21, 2021
346e232
remove click hack, doc next steps
jerch Mar 21, 2021
8522b90
fix type exports and README
jerch Mar 21, 2021
db9bfc5
layout few more next steps
jerch Mar 23, 2021
46473ff
remove some of the private hacks
jerch Mar 23, 2021
a0b5fa9
fix tsconfig paths in test project
jerch Mar 24, 2021
c09128a
kiss - remove storage remnants from iterm image protocols
jerch Mar 25, 2021
c9337f8
better typing, optimized ExtendedAttrsImage creation
jerch Mar 25, 2021
0fd64ee
expose cell metrics internally
jerch Mar 25, 2021
7e0e54c
explicit test for buffer and attribute private access
jerch Mar 25, 2021
6993f4d
Merge branch 'master' into addon-images
jerch Mar 26, 2021
4b14446
add comment about garbage on screen
jerch Mar 26, 2021
143f113
Merge branch 'master' into addon-images
jerch May 10, 2021
5a6f3a7
make linter happy
jerch May 10, 2021
e054ec7
change integration test port to 3001
jerch May 10, 2021
0b3aa46
Merge branch 'master' into addon-images
jerch May 16, 2021
7166bf6
fix Promise<void>
jerch May 16, 2021
736fc31
fix mocha timeout error
jerch May 16, 2021
7de2430
Merge branch 'master' into addon-images
jerch May 20, 2021
58e3c6e
fix safari render bug
jerch May 24, 2021
c2c753e
add banding test
jerch May 24, 2021
e0f23d2
custom setting to fit oversized images to viewport width
jerch May 25, 2021
005648c
apply eviction after rescaling to avoid wrong image accounting
jerch May 29, 2021
c98eb8f
growing rectangle test without flickering
jerch May 29, 2021
b98c126
skip undefined image eAttr in render
jerch May 29, 2021
61c5615
fix DOM renderer rescaling issue
jerch May 29, 2021
e0d7b3e
onResize hook for right expansion
jerch May 29, 2021
c145c63
make linter happy
jerch May 29, 2021
8887564
Merge branch 'master' into addon-images
jerch May 30, 2021
0d9a543
fix marker memory leak
jerch Jun 4, 2021
dfa8b84
remove <TypeX>yz casts
jerch Jun 4, 2021
69e9779
cleanup fitOversizedToViewportWidth
jerch Jun 4, 2021
4c0a5d4
use transferable pixel buffer
jerch Jun 4, 2021
124c603
fix type casts
jerch Jun 4, 2021
019986e
Merge branch 'master' into addon-images
jerch Jun 4, 2021
1e2f24e
Merge branch 'master' into addon-images
jerch Jun 10, 2021
f2a8556
Merge branch 'master' into addon-images
jerch Jun 12, 2021
f755ac4
activate windowOptions on addon load, polish docs
jerch Jun 13, 2021
c9fe4f1
Merge branch 'addon-images' of github.com:jerch/xterm.js into addon-i…
jerch Jun 13, 2021
ebc7145
basic image data retrieval API
jerch Jun 13, 2021
18c898b
limit pixels for single image
jerch Jun 13, 2021
84071e3
cleanup FIXMEs
jerch Jun 13, 2021
90864c5
fix integration tests
jerch Jun 13, 2021
28e14b6
multiple mostly cleanup changes:
jerch Jun 14, 2021
708517e
revert false simplification
jerch Jun 14, 2021
b8f6c26
minor fixes
jerch Jun 14, 2021
10ba35f
change eslint project settings
jerch Jun 14, 2021
3eedf92
Merge branch 'master' into addon-images
jerch Jul 8, 2021
4a89f15
Merge branch 'master' into addon-images
jerch Aug 18, 2021
dfc7e23
fix private coreService access
jerch Aug 19, 2021
8c09894
Merge branch 'master' into addon-images
jerch Aug 19, 2021
32adff2
remove leftover .only in test suite
jerch Aug 19, 2021
ded80b3
Merge branch 'master' into addon-images
jerch Aug 21, 2021
652e03a
Merge branch 'master' into addon-images
jerch Aug 28, 2021
db8eb2b
update to new sixel decoder
jerch Aug 31, 2021
73459d6
Merge branch 'addon-images' of github.com:jerch/xterm.js into addon-i…
jerch Aug 31, 2021
27218f6
Merge branch 'master' into addon-images
jerch Aug 31, 2021
16eb95a
fix yarn.lock to point to right version
jerch Sep 2, 2021
5a0939d
Merge branch 'master' into addon-images
jerch Sep 2, 2021
a4587f9
Merge branch 'master' into addon-images
jerch Sep 5, 2021
6676dec
cleanup fixtures
jerch Sep 8, 2021
ce499d1
remove dispose from readme example
jerch Sep 8, 2021
85d8d73
add demo buttons
jerch Sep 8, 2021
7ed3c48
fix xterm dep version
jerch Sep 8, 2021
3ae28d2
simplify worker message types
jerch Sep 8, 2021
cd1448b
add example image to readme
jerch Sep 8, 2021
0277697
use star comments in typings
jerch Sep 8, 2021
07bc129
use import type, fix pixel limit, remove cmdline bookmarks
jerch Sep 8, 2021
3b720fe
Merge branch 'addon-images' of github.com:jerch/xterm.js into addon-i…
jerch Sep 8, 2021
c9f27a8
make linter happy
jerch Sep 8, 2021
f128c04
Merge branch 'master' into addon-images
jerch Sep 28, 2021
87365f1
flip DECSET 80 & 7730 settings
jerch Sep 28, 2021
14b4d4c
fix cursor positioning tests
jerch Sep 28, 2021
09ffed4
fix cursor positioning tests
jerch Sep 28, 2021
276d291
reduce perma held memory for sixel decoding to 4MB
jerch Sep 28, 2021
99e4072
report true window area for sixel geo read if within pixelLimit
jerch Sep 28, 2021
7f003fe
use famous snake sixel example image from libsixel repo
jerch Sep 28, 2021
aaef0fd
widen type to HTMLElement
jerch Sep 28, 2021
bfa0de7
update xterm peer dep version
jerch Sep 28, 2021
e073d8b
make workerPath mandatory
jerch Sep 28, 2021
918113f
fix page eval in api test
jerch Sep 28, 2021
5c64048
remove TODO list from source code
jerch Sep 28, 2021
488cddc
change pixelLimit description
jerch Sep 28, 2021
b9969e9
simplify partial addon options
jerch Sep 28, 2021
f712933
simplify private interface imports from repo base
jerch Sep 28, 2021
ba07f6f
use arrow functions in API tests
jerch Sep 28, 2021
9199b53
use helper for test startup
jerch Sep 28, 2021
ad914a8
shorten private accessor tests
jerch Sep 28, 2021
85c0df0
rename Dim to Dimensions
jerch Sep 28, 2021
8eaeb1e
use const enum for worker message type
jerch Sep 28, 2021
9c2caf0
rename clearCheckerInterval, use shorthand notation for promise return
jerch Sep 28, 2021
c1cb794
fix _poolCheckerInterval name access in test
jerch Sep 28, 2021
dd8050a
fix _poolCheckerInterval name access in test
jerch Sep 28, 2021
336ef5e
Merge branch 'master' into addon-images
jerch Oct 12, 2021
d882cef
fix reported canvas size, rounding fix in XTSMGRAPHICS
jerch Oct 12, 2021
47412ec
Merge branch 'master' into addon-images
jerch Oct 20, 2021
6da04ff
Merge branch 'master' into addon-images
jerch Oct 22, 2021
ba50ec6
Merge branch 'master' into addon-images
jerch Oct 25, 2021
7b1fcad
Merge branch 'master' into addon-images
jerch Nov 2, 2021
bc1f1d4
fix unknown type in catch
jerch Nov 2, 2021
74d12d1
Merge branch 'master' into addon-images
jerch Nov 6, 2021
9468d0d
fix shared palette handling
jerch Nov 7, 2021
55db19c
use enum for ack payload
jerch Nov 7, 2021
414172c
Merge branch 'master' into addon-images
jerch Nov 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 1 addition & 16 deletions .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,7 @@
"src/headless/tsconfig.json",
"test/api/tsconfig.json",
"test/benchmark/tsconfig.json",
"addons/xterm-addon-attach/src/tsconfig.json",
"addons/xterm-addon-attach/test/tsconfig.json",
"addons/xterm-addon-fit/src/tsconfig.json",
"addons/xterm-addon-fit/test/tsconfig.json",
"addons/xterm-addon-ligatures/src/tsconfig.json",
"addons/xterm-addon-search/src/tsconfig.json",
"addons/xterm-addon-search/test/tsconfig.json",
"addons/xterm-addon-serialize/src/tsconfig.json",
"addons/xterm-addon-serialize/test/tsconfig.json",
"addons/xterm-addon-serialize/benchmark/tsconfig.json",
"addons/xterm-addon-unicode11/src/tsconfig.json",
"addons/xterm-addon-unicode11/test/tsconfig.json",
"addons/xterm-addon-web-links/src/tsconfig.json",
"addons/xterm-addon-web-links/test/tsconfig.json",
"addons/xterm-addon-webgl/src/tsconfig.json",
"addons/xterm-addon-webgl/test/tsconfig.json"
"addons/tsconfig.eslint.addons.json"
],
"sourceType": "module"
},
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ node_modules/
lib/
out/
out-test/
out-worker/
.nyc_output/
Makefile.gyp
*.Makefile
Expand All @@ -21,6 +22,7 @@ package-lock.json
# Keep bundled code out of Git
dist/
demo/dist/
demo/workers/

# dont commit benchmark folders
.benchmark/
Expand Down
32 changes: 32 additions & 0 deletions addons/tsconfig.eslint.addons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"compilerOptions": {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need all these compiler options? This might be convenient but it doesn't end up using the TS project tsconfig.json which might lead to future weirdness. perhaps waiting until eslint supports project references is a better way typescript-eslint/typescript-eslint#2094?

Copy link
Member Author

@jerch jerch Sep 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well I basically did a c&p & merge from tsconfigs to make sure, no new rules slip into. Not sure if those rules are needed at all.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently in master eslint will go through all addons with their project references separately, which takes alot of time and memory. With the image addon added, this does not work anymore, eslint runs into OOM. Thus waiting is no option (unless we disable eslint for addons)?

Now the idea of that additional tsconfig file is to give eslint a single project entry for all addons at once. I had to do this, because of your linked issue. With this eslint works again.

The tsconfig file is simply a merger of the single ones at individual addon level.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eslint hits oom 🤯

It's probably fine like this if there's performance issues, not sure if eslint would need anything specific from the tsconfig.json to do its thing anyway.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure either. Tbh I dont like the fact, that the linter operates on a fake project, that never gets used/built by TS, but it was the only way I found avoid the OOM. The linting though works correctly across lib borders (tested manually by doing forth and back changes at various places).

If you have no other ideas how to circumvent the OOM by some simpler settings, I'd leave it that way for now.

"module": "commonjs",
"target": "es2017",
"rootDir": ".",
"outDir": "../out-test",
"sourceMap": true,
"removeComments": true,
"strict": true,
"baseUrl": ".",
"paths": {
"browser/*": [ "../../../src/browser/*" ],
"common/*": [ "../../../src/common/*" ]
},
"types": [
"../node_modules/@types/mocha",
"../node_modules/@types/node",
"../out-test/api/TestUtils"
]
},
"include": [
"../typings/xterm.d.ts",
"./**/src/*",
"./**/src-worker/*",
"./**/test/*",
"./**/benchmark/*"
],
"references": [
{ "path": "../src/browser" },
{ "path": "../src/common" }
]
}
2 changes: 2 additions & 0 deletions addons/xterm-addon-image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lib
node_modules
11 changes: 11 additions & 0 deletions addons/xterm-addon-image/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
**/*.api.js
**/*.api.ts
tsconfig.json
.yarnrc
webpack.config.js

fixture
src
src-worker
test
out-test
19 changes: 19 additions & 0 deletions addons/xterm-addon-image/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright (c) 2019, The xterm.js authors (https://github.com/xtermjs/xterm.js)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
177 changes: 177 additions & 0 deletions addons/xterm-addon-image/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
## xterm-addon-image

Image output in xterm.js.
jerch marked this conversation as resolved.
Show resolved Hide resolved


![](fixture/example.png)


⚠️ This is an experimental addon, that is still under construction. ⚠️


### Install

```bash
npm install --save xterm-addon-image
```

### Usage

```ts
import { Terminal } from 'xterm';
import { ImageAddon, IImageAddonOptions } from 'xterm-addon-image';

const WORKER_PATH = '/path/to/xterm-addon-image-worker.js';

// customize as needed
const customSettings: IImageAddonOptions = {
sixelSupport: true,
...
}

// initialization
const terminal = new Terminal();
const imageAddon = new ImageAddon(WORKER_PATH, customSettings);
terminal.loadAddon(imageAddon);
```

### General Notes

- The image decoding is done with a worker, therefore the addon will only work, if you expose the worker file as well. The worker is distributed under `lib/xterm-addon-image-worker.js`. Place the exported worker path as the first argument of the addon constructor, e.g. `new ImageAddon('/your/path/to/image/worker')`. Additionally make sure, that your main integration has proper read and execution permissions on the worker file, otherwise the addon will log a worker error and disable itself on the first image decoding attempt (lazy evaluated).

- By default the addon will activate these `windowOptions` reports on the terminal:
- getWinSizePixels (CSI 14 t)
- getCellSizePixels (CSI 16 t)
- getWinSizeChars (CSI 18 t)

to help applications getting useful terminal metrics for their image preparations. Set `enableSizeReports` in the constructor options to `false`, if you dont want the addon to alter these terminal settings. This is especially useful, if you have very strict security needs not allowing any terminal reports, or deal with `windowOptions` by other means.


### Operation Modes

- **SIXEL Support**
Set by default, change it with `{sixelSupport: true}`.

- **Scrolling On | Off**
By default scrolling is on, thus an image will advance the cursor at the bottom if needed.
The cursor will move with the image and be placed either right to the image or in the next line
(see cursor positioning).

If scrolling is off, the image gets painted from the top left of the current viewport
and might be truncated if the image exceeds the viewport size.
The cursor position does not change.

You can customize this behavior with the constructor option `{sixelScrolling: false}`
or with `DECSET 80` (off, binary: `\x1b [ ? 80 h`) and
`DECRST 80` (on, binary: `\x1b [ ? 80 l`) during runtime.

- **Cursor Positioning**
If scrolling is set, the cursor will be placed at the beginning of the next row by default.
You can change this behavior with the following terminal sequences:
- `DECSET 8452` (binary: `\x1b [ ? 8452 h`)
For images not overflowing to the right, the cursor will move to the next right cell of the last image cell.
Images overflowing to the right, move the cursor to the next line.
Same as the constructor option `{cursorRight: true}`.

- `DECRST 8452` (binary: `\x1b [ ? 8452 l`)
Always moves the cursor to the next line (default). Same as the constructor option `{cursorRight: false}`.

- `DECRST 7730` (binary: `\x1b [ ? 7730 l`)
Move the cursor on the next line to the image start offset instead of the beginning.
This setting only applies if the cursor will wrap to the next line (thus never for scrolling off,
for `DECSET 8452` only after overflowing to the right). Same as the constructor option `{cursorBelow: true}`.

- `DECSET 7730` (binary: `\x1b [ ? 7730 h`)
Keep the cursor on the next line at the beginning (default). Same as the constructor option `{cursorBelow: false}`.

- **SIXEL Palette Handling**
By default the addon limits the palette size to 256 registers (as demanded by the DEC specification).
The limit can be increased to a maximum of 4096 registers (via `sixelPaletteLimit`).

If `sixelPrivatePalette` is set (default), images are initialized with their own private palette derived from the default palette (`'VT340-COLOR'`). If `sixelPrivatePalette` is not set, the palette of the previous image will be used as initial palette.

Note that the underlying SIXEL library currently applies colors immediately to pixels (*printer mode*),
thus it is technically possible to use more colors in one image than the palette has color slots.
This feature is called *high-color* in libsixel.

In contrast older terminals were always bound to the palette due hardware limitations.
This limitation is mimicked by xterm's shared palette mode, which will re-color previous images from palette changes
treating all sixel images as indexed images. This true shared-palette *terminal mode* is currently not supported by
xterm.js, as it always operates in *printer mode*.

- **SIXEL Raster Attributes Handling**
If raster attributes were found in the SIXEL data (level 2), the image will always be limited to the given height/width extend. We deviate here from the specification on purpose, as it allows several processing optimizations. For level 1 SIXEL data without any raster attributes the image can freely grow in width and height up to the last data byte, which has a much higher processing penalty. In general encoding libraries should not create level 1 data anymore and should not produce pixel information beyond the announced height/width extend. Both is discouraged by the >30 years old specification.

Currently the SIXEL implementation of the addon does not take custom pixel sizes into account, a SIXEL pixel will map 1:1 to a screen pixel.

### Storage and Drawing Settings

The internal storage holds images up to `storageLimit` (in MB, calculated as 4-channel RBGA unpacked, default 100 MB). Once hit images get evicted by FIFO rules. Furthermore images on the alternate buffer will always be erased on buffer changes.

The addon exposes two properties to interact with the storage limits at runtime:
- `storageLimit`
Change the value to your needs at runtime. This is especially useful, if you have multiple terminal
instances running, that all add to one upper memory limit.
- `storageUsage`
Inspect the current memory usage of the image storage.

By default the addon will show a placeholder pattern for evicted images that are still part
of the terminal (e.g. in the scrollback). The pattern can be deactivated by toggling `showPlaceholder`.

### Image Data Retrieval

The addon provides the following API endpoints to retrieve raw image data as canvas:

- `getImageAtBufferCell(x: number, y: number): HTMLCanvasElement | undefined`
Returns the canvas containing the original image data (not resized) at the given buffer position.
The buffer position is the 0-based absolute index (including scrollback at top).

- `extractTileAtBufferCell(x: number, y: number): HTMLCanvasElement | undefined`
Returns a canvas containing the actual single tile image data (maybe resized) at the given buffer position.
The buffer position is the 0-based absolute index (including scrollback at top).
Note that the canvas gets created and data copied over for every call, thus it is not suitable for performance critical actions.

### Memory Usage

The addon does most image processing in Javascript and therefore can occupy a rather big amount of memory. To get an idea where the memory gets eaten, lets look at the data flow and processing steps:
- Incomming image data chunk at `term.write` (terminal)
`term.write` might stock up incoming chunks. To circumvent this, you will need proper flow control (see xterm.js docs). Note that with image output it is more likely to run into this issue, as it can create lots of MBs in very short time.
- Sequence Parser (terminal)
The parser operates on a buffer containing up to 2^17 codepoints (~0.5 MB).
- Sequence Handler - Chunk Processing (addon / mainthread)
Image data chunks are copied over and sent to the decoder worker as transferables with `postMessage`. To avoid a data congestion at the message port, allowed SIXEL data is hard limited by `sixelSizeLimit` (default 25 MB). The transport chunks are pooled, the pool cache may hold up to ~6 MB during active data transmission.
- Image Decoder (addon / worker)
The decoder works chunkwise allocating memory as needed. The allowed image size gets restricted by `pixelLimit` (default 16M pixels), the decoder holds 2 pixel buffers at maximum during decoding (RGBA, ~128 MB for 16M pixels).
After decoding the final pixel buffer is transferred back to the sequence handler.
- Sequence Handler - Image Finalization (addon / mainthread)
The pixel data gets written to a canvas of the same size (~64 MB for 16M pixels) and added to the storage. The pixel buffer is sent back to the worker to be used for the next image.
- Image Storage (addon / mainthread)
The image storage implements a FIFO cache, that will remove old images, if a new one arrives and `storageLimit` is hit (default 128 MB). The storage holds a canvas with the original image, and may additionally hold resized versions of images after a font rescaling. Both are accounted in `storageUsage` as a rough estimation of _pixels x 4 channels_.

Following the steps above, a rough estimation of maximum memory usage by the addon can be calculated with these formulas (in bytes):
```typescript
// storage alone
const storageBytes = storageUsage * storageLimit * 1024 * 1024;
// decoding alone
const decodingBytes = sixelSizeLimit + 2 * (pixelLimit * 4);

// totals
// inactive decoding
const totalInactive = storageBytes;
// active decoding
const totalActive = storageBytes + decodingBytes;
```

Note that browsers have offloading tricks for rarely touched memory segments, esp. `storageBytes` might not directly translate into real memory usage. Usage peaks will happen during active decoding of multiple big images due to the need of 2 full pixel buffers at the same time, which cannot be offloaded. Thus you may want to keep an eye on `pixelLimit` under limited memory conditions.
Further note that the formulas above do not respect the Javascript object's overhead. Compared to the raw buffer needs the book keeping by these objects is rather small (<<5%).

_Why should I care about memory usage at all?_
Well you don't have to, and it probably will just work fine with the addon defaults. But for bigger integrations, where much more data is held in the Javascript context (like multiple terminals on one page), it is likely to hit the engine's memory limit sooner or later under decoding and/or storage pressure.

_How can I adjust the memory usage?_
- `pixelLimit`
A constructor settings, thus you would have to anticipate, whether (multiple) terminals in your page gonna do lots of concurrent decoding. Since this is normally not the case and the memory usage is only temporarily peaking, a rather high value should work even with multiple terminals in one page.
- `storageLimit`
A constructor and a runtime setting. In conjunction with `storageUsage` you can do runtime checks and adjust the limit to your needs. If you have to lower the limit below the current usage, images will be removed and may turn into a placeholder in the terminal's scrollback (if `showPlaceholder` is set). When adjusting keep in mind to leave enough room for memory peaking for decoding.
- `sixelSizeLimit`
A constructor setting. This has only a small direct impact on peaking memory during decoding. It still will avoid processing of overly big or broken sequences at an earlier phase, thus may stop the decoder from entering its memory intensive task for potentially invalid data.
Binary file added addons/xterm-addon-image/fixture/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions addons/xterm-addon-image/fixture/growing_rect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const sixelEncode = require('../node_modules/sixel/lib/SixelEncoder').image2sixel;
const toRGBA8888 = require('../node_modules/sixel/lib/Colors').toRGBA8888;

function createRect(size, color) {
const pixels = new Uint32Array(size * size);
pixels.fill(toRGBA8888(...color));
return sixelEncode(new Uint8ClampedArray(pixels.buffer), size, size);
}

function createRectMinusOne(size, color) {
const pixels = new Uint32Array(size * size);
if (size - 1) {
const sub = new Uint32Array(size - 1);
sub.fill(toRGBA8888(...color));
const last = size * (size - 1);
for (let y = 0; y < last; y += size) {
pixels.set(sub, y);
}
}
return sixelEncode(new Uint8ClampedArray(pixels.buffer), size, size);
}

async function main() {
// clear + cursor and sixelScrolling off
process.stdout.write('\x1b[2J\x1b[?25;80l');

for (let i = 1; i < 300; ++i) {
await new Promise(res => setTimeout(() => {
process.stdout.write(createRect(i, [0, 255, 0]));
res();
}, 5));
}
for (let i = 299; i >= 1; --i) {
await new Promise(res => setTimeout(() => {
process.stdout.write(createRectMinusOne(i, [0, 255, 0]));
res();
}, 5));
}

// re-enable cursor and sixel scrolling
process.stdout.write('\x1b[2J\x1b[?25;80h');
}

main();
Binary file added addons/xterm-addon-image/fixture/palette.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading