1 Pictures
liquibyte edited this page 2026-11-19 16:23:30 -06:00

Pictures

A program can show pictures in a terminal in three ways: sixel, iTerm2's inline images and the Kitty graphics protocol. This page describes what Hosho takes of each, how a picture sits on the screen, how a host draws it, and how a host takes pictures apart in a jail.

1. How a picture comes to the screen

A program sends a picture to the terminal; the terminal hands its data to the host's functions, which take it apart in the jailed worker; the checked pixels come back and the terminal puts the picture on the screen as cells

The widget's path: every picture is taken apart in Xtk's jailed worker, and the terminal checks what comes back before it puts the picture on the screen. The red border is the jail.

A picture is put on the screen at the cursor as cells: each cell shows one cell-sized part of it. So a picture moves, scrolls and is erased as text is, and a program that writes over it replaces those cells. The size of a cell in pixels is what the host told with hosho_terminal_set_cell_size() (10 by 20 until it does), and the picture keeps the cell size it was put on the screen with.

The text cursor goes to the line below the picture, at the column where the picture starts; the screen scrolls for a tall picture. With sixel display mode (DECSDM, mode 80), a sixel picture goes to the top left of the screen instead, the screen does not scroll, the part below the screen is not shown, and the cursor stays where it was. A picture wider than the screen is cut at the right edge.

2. Sixel

A sixel picture is a device control string: DCS P1 ; P2 ; P3 q, the data, then ST. Each data character is a column of six pixels in one color. Hosho reads the format of the VT330 and VT340: color definitions in RGB and in HLS (#), 256 color registers, repeat counts (!), raster attributes ("; the size there is a hint only, but when the background is filled, the picture is at least that size), carriage return ($) and new line (-).

  • P2 = 1 leaves the pixels the data does not draw transparent; any other value fills them with the color of register 0.
  • The color registers belong to the terminal and stay from one picture to the next, so a program can define colors in one picture and use them in another. A reset gives them the colors a VT340 starts with.
  • A picture has at most 4096 pixels in each direction (HOSHO_SIXEL_SIZE_LIMIT); pixels beyond that are dropped.
  • The terminal says it has sixel in its device attributes (CSI ? 64 ; 4 ; ...), and XTSMGRAPHICS (CSI ? 1 ; 1 S, CSI ? 2 ; 1 S) answers 256 registers and 4096 by 4096 pixels.

3. iTerm2's inline images

OSC 1337 ; File=key=value;key=value:data, where the data is a picture file in base64. Hosho reads these keys:

Key What it does
inline=1 The file is shown. Without it the file is a download, and the terminal does not write files: nothing happens.
width, height N cells, Npx pixels, N% of the screen, or auto. With one of the two, the other follows the picture's shape.
preserveAspectRatio=0 With both sizes, the picture fills them; by default it fits in them and keeps its shape.

The other keys (the name, the size) are not used. The host's image_decode function decodes the file; a host without it shows no inline image. A picture shown in another size is scaled to the nearest pixel, and the result can be at most 4096 pixels each way.

4. The Kitty graphics protocol

APC G keys ; data ST, where the keys are key=value pairs with commas between them and the data is base64. Hosho reads these:

Key What it does
a The action: t sends a picture to keep (the default), T sends one and shows it, p shows a kept picture, q checks a picture and answers without keeping it, d deletes.
i The picture's number: a kept picture is shown again (a=p) and deleted by it.
f The format: 32 RGBA pixels (the default), 24 RGB pixels, 100 a PNG file, which the host's image_decode decodes.
s, v The width and height in pixels, for f=24 and f=32.
o=z The data is compressed with zlib. The host's inflate makes it whole, or the terminal's own reader (hosho_inflate()) when the host has none.
t Only d, data in the command itself. The terminal does not read a file or shared memory a program names, and answers ENOTSUP.
m=1 More parts follow. The first part's keys count; each part adds data, and the part with m=0 ends it. All parts together hold at most 64 MiB of data: when they go past that, the rest of the parts are dropped and the last one gets the error EFBIG. A delete drops a picture that is not complete.
c, r When shown, the size in cells; with one of them, the other follows the picture's shape.
C=1 The cursor does not move when the picture is shown.
q q=1: no OK answers; q=2: no answers at all.

A picture can have a number the program chose (i), or an image number (I): the terminal then gives the picture a number of its own and says it in the answer (APC G i=number,I=image number ; OK ST), and a=p,I= and d=n mean the newest picture with that image number. A command with a picture number or an image number gets an answer, APC G i=number ; OK ST (with I and p when the command had them), or an error: ENOENT (no picture has this number), ENOTSUP (an action or a transmission that is not known), EBADDATA (the data cannot be read), EFBIG (the parts are too large), EBADPNG (the PNG cannot be decoded) or EINVAL (the size and the data do not agree). The terminal keeps at most 32 pictures (HOSHO_KITTY_IMAGE_LIMIT); one more takes the place of the oldest, and a picture sent with the number of a kept one replaces it.

The delete action's d says which pictures leave the screen: a all (the default), i the picture with the number i, c the one at the cursor, p the one at the cell x, y (from 1), x those with a cell in column x, y those with a cell in row y, n the newest picture with the image number I. A capital letter also forgets the kept picture: with C, P, X and Y, each kept picture that was last shown in a place they remove.

5. Drawing a picture

A cell whose character has HOSHO_CELL_IS_IMAGE() true shows a part of a picture: HOSHO_CELL_IMAGE_ID() is the picture's number on the screen, and HOSHO_CELL_IMAGE_COLUMN() and HOSHO_CELL_IMAGE_ROW() say which of its cells this is. hosho_screen_image(screen, number) gives the HoshoImage: its pixels (each 0xAARRGGBB; alpha 0 is "nothing drawn", and the cell's background shows there), its size, the cell size it was placed with, and a serial that no other picture of the screen has had, so a host that keeps pictures ready to draw knows a new picture in an old number. The host draws the part of the pixels at (column times cell width, row times cell height).

The screen keeps at most 1023 pictures, numbered from 1 to HOSHO_IMAGE_LIMIT - 1 (number 0 is no picture). When all the numbers are in use, the pictures that no cell shows any more are released and a new picture takes the first of their numbers; when every picture is still shown, the new one is not shown. A picture covers at most 1023 cells in each direction (HOSHO_IMAGE_CELL_LIMIT).

6. Taking pictures apart in a jail

A picture is the most complicated thing a program can send, and the code that takes it apart is where a bug could let a hostile program in. So the host can take every picture apart itself, in a process of its own: the terminal hands picture files to the host's image_decode, sixel data to its sixel_decode, and compressed data to its inflate (see The Host Functions). A host with no sixel_decode or inflate leaves those to the terminal's own readers, in the host's process.

Two things are always done by the terminal itself, in the host's process, because they take nothing apart: it reads the base64 of an iTerm2 file and of a Kitty command, and it copies Kitty's raw pixels (f=24 and f=32 without o=z) into a picture after it checked that the data has as many bytes as the size says. What goes to the host is only the picture file (to image_decode), the sixel data (to sixel_decode) and the compressed data (to inflate).

The terminal treats each answer as possibly hostile too: a sixel picture or a kitty picture larger than 4096 pixels in a direction, and inflated data larger than 64 MiB, are thrown away.

The widget does all three in Xtk's jailed worker (xtk_worker.h): a process of the program's own that can read no file, write nothing, reach no network, and start or signal nothing. It also decodes the theme's background picture there. The program must add the widget's jobs and start the worker at the top of main() (see Embedding the Widget). The jail needs Linux 5.13 or later with Landlock, on x86-64 or ARM64. Without it every request to the worker fails, so the widget shows no picture file, no sixel picture, no compressed picture and no background picture; only Kitty's raw pixels, which need no worker, are still shown.

7. Known limitations

  • An OSC, DCS or APC string holds at most 64 MiB (HOSHO_PARSER_STRING_LIMIT); a longer one is dropped whole. So a sixel picture or an iTerm2 file of more than 64 MiB of data is not shown. The Kitty protocol's parts together hold 64 MiB too.
  • Sixel's pixel aspect ratio is not used: each sixel pixel is one pixel of the display.
  • Of the Kitty graphics protocol, placements by number, layers (z), offsets inside a cell, a part of a picture, animation and Unicode placeholders are not there. A kept picture remembers only the last place it was shown.