| 20 | |
| 21 | |
| 22 | export class SixelHandler implements IDcsHandler, IResetHandler { |
| 23 | private _size = 0; |
| 24 | private _aborted = false; |
| 25 | private _dec: Decoder | undefined; |
| 26 | |
| 27 | constructor( |
| 28 | private readonly _opts: IImageAddonOptions, |
| 29 | private readonly _storage: ImageStorage, |
| 30 | private readonly _coreTerminal: ITerminalExt |
| 31 | ) { |
| 32 | DecoderAsync({ |
| 33 | memoryLimit: this._opts.pixelLimit * 4, |
| 34 | palette: DEFAULT_PALETTE, |
| 35 | paletteLimit: this._opts.sixelPaletteLimit |
| 36 | }).then(d => this._dec = d); |
| 37 | } |
| 38 | |
| 39 | public reset(): void { |
| 40 | /** |
| 41 | * reset sixel decoder to defaults: |
| 42 | * - release all memory |
| 43 | * - nullify palette (4096) |
| 44 | * - apply default palette (256) |
| 45 | */ |
| 46 | if (this._dec) { |
| 47 | this._dec.release(); |
| 48 | // FIXME: missing interface on decoder to nullify full palette |
| 49 | (this._dec as any)._palette.fill(0); |
| 50 | this._dec.init(0, DEFAULT_PALETTE, this._opts.sixelPaletteLimit); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | public hook(params: IParams): void { |
| 55 | this._size = 0; |
| 56 | this._aborted = false; |
| 57 | if (this._dec) { |
| 58 | const fillColor = params.params[1] === 1 ? 0 : extractActiveBg( |
| 59 | this._coreTerminal._core._inputHandler._curAttrData, |
| 60 | this._coreTerminal._core._themeService?.colors); |
| 61 | this._dec.init(fillColor, null, this._opts.sixelPaletteLimit); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | public put(data: Uint32Array, start: number, end: number): void { |
| 66 | if (this._aborted || !this._dec) { |
| 67 | return; |
| 68 | } |
| 69 | this._size += end - start; |
| 70 | if (this._size > this._opts.sixelSizeLimit) { |
| 71 | console.warn(`SIXEL: too much data, aborting`); |
| 72 | this._aborted = true; |
| 73 | this._dec.release(); |
| 74 | return; |
| 75 | } |
| 76 | try { |
| 77 | this._dec.decode(data, start, end); |
| 78 | } catch (e) { |
| 79 | console.warn(`SIXEL: error while decoding image - ${e}`); |
nothing calls this directly
no outgoing calls
no test coverage detected