Skip to content

Kitty graphics - #20872

Open
Matthew Wilding (mbwilding) wants to merge 15 commits into
microsoft:mainfrom
mbwilding:kitty-graphics
Open

Matthew Wilding (mbwilding) wants to merge 15 commits into
microsoft:mainfrom
mbwilding:kitty-graphics

Conversation

@mbwilding

@mbwilding Matthew Wilding (mbwilding) commented Oct 30, 2026 •

Copy link
Copy Markdown

Summary of the Pull Request

This adds support for the kitty graphics protocol to Windows Terminal and conhost. Tools that use the protocol, such as snacks.nvim's image viewer, can display images directly in Windows Terminal, including inline markdown images in Neovim.

terminal neovim

References and Relevant Issues

#8389

Detailed Description of the Pull Request / Additional comments

What's supported

  • Transmission: inline (including chunked), from files, from temporary files, and through shared memory.
  • Formats: PNG, decoded with WIC, and raw RGB/RGBA, optionally zlib-compressed.
  • Placement:
    • cropping, scaling to a cell box with letterboxing, sub-cell offsets, cursor movement control and z-index layering;
    • virtual placements with unicode placeholders, so images move with the text in applications like Neovim.
  • Management: all delete modes, the support query, feature queries, image numbers, and replies that respect the quiet setting.

What isn't supported

Animation, relative placements and the palette-indexed formats (f=8 and f=16) reply with an error.

New dependency

zlib is added to vcpkg.json to decompress zlib-compressed transmissions (o=z).

Change to existing code

ResizeTraditional previously freed rows without destroying them, so image data leaked on resize. This affected sixel images too, so the fix changes existing behaviour.

How it works

  • Parser: APC strings are dispatched to a handler. Conhost passes APC replies back through ConPTY.
  • Buffer: images are stored per buffer with least recently used eviction like kitty. Placements are attached to the rows they cover, so they scroll and reflow with the text and aren't erased by text output. Only images entirely inside the scroll margins move with them. Clearing the screen and a terminal reset remove images.
  • Memory limits: the store has a 320 MB quota, rejects images larger than the quota, and holds at most 4096 images.
  • Cursor sync:
    • The cell footprint and cursor movement use the same virtual cell size as sixel, so ConPTY and the terminal agree on the cursor position.
    • After a placement, conhost marks its cursor position as uncertain, so it rechecks against the real terminal.
    • Conhost only reads the image size rather than decoding it, so images aren't decoded twice.
  • Rendering:
    • AtlasEngine with Direct3D draws images in three layers by z-index: under cell backgrounds, under text, and above text.
    • AtlasEngine with Direct2D draws two layers. Images under coloured cell backgrounds are drawn under the text but over the background.
    • AtlasEngine downscales images larger than the GPU's maximum bitmap size.
    • GDI draws images under or over the text according to their z-index.
  • Security: file paths must be on local drives. Network and device paths are refused, and temporary files are only deleted from the temp directory.

Validation Steps Performed

  • Unit tests: parser, adapter and text buffer tests pass, with new tests for APC parsing, transmission, placement, deletion, chunking, virtual placements, scrolling in margins and reset.
  • Manual: tests were run against every renderer: Direct3D, Direct2D, AtlasEngine in conhost, and GDI. snacks.nvim was also tested with images and inline markdown.

PR Checklist

Written with AI assistance.

Implements the kitty graphics protocol, along with XTVERSION so that
applications can identify the terminal.

* The parser dispatches APC strings to a handler, and the input engine
  passes APC responses through ConPTY.
* Images can be transmitted directly, in chunks, or via files, temporary
  files and shared memory, as PNG or raw RGB/RGBA data, optionally zlib
  compressed. Network and device paths are rejected.
* Placements are attached to the rows they cover, so they scroll and
  reflow with the text and aren't erased by text output. The cell
  footprint uses the virtual cell size so ConPTY and the terminal agree
  on the cursor position.
* Virtual placements and Unicode placeholders are supported, along with
  all delete modes, queries and feature queries.
* AtlasEngine (D3D and D2D) and GDI draw images below backgrounds, below
  text or above text, according to their z-index.
* Exceptions in the graphics command handler no longer escape into the
  state machine.
* Shared memory is only read when its pages are committed.
* Images larger than the device's maximum bitmap size are downscaled
  instead of failing every frame.
* Like kitty, all images count towards the storage quota, and evicted
  images are removed from the screen as well.
* Like kitty, only images entirely within the scroll margins move with
  them, and scrolling the whole region removes its images.
* Deleting by id without an id no longer matches every anonymous image.
* A hard reset removes all placements.
* Reflow keeps the graphics of rows joined onto a previous row.
* GDI caches several images instead of only the most recent one.
* EFBIG replies identify the command that was too large.
* ConPTY no longer keeps the pixel data, since it never renders it.
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Comment thread src/buffer/out/GraphicsImage.cpp Fixed
Comment thread src/renderer/gdi/paint.cpp Fixed
Comment thread src/renderer/gdi/paint.cpp Fixed
@github-actions

This comment has been minimized.

Formats the changed files with clang-format, keeping the placeholder diacritic table on a grid, and adds the new API and protocol names to the spelling expect list.
@mbwilding

Copy link
Copy Markdown
Author

Matthew Wilding (Matthew Wilding (@mbwilding)) please read the following Contributor License Agreement(CLA). If you agree with the CLA, please reply with the following information.

@microsoft-github-policy-service agree [company="{your company}"]

Options:

  • (default - no company specified) I have sole ownership of intellectual property rights to my Submissions and I am not making Submissions in the course of work for my employer.
@microsoft-github-policy-service agree
  • (when company given) I am making Submissions in the course of work for my employer (or my employer has intellectual property rights in my Submissions by contract or applicable law). I have permission from my employer to make Submissions and enter into this Agreement on behalf of my employer. By signing below, the defined term “You” includes me and my employer.
@microsoft-github-policy-service agree company="Microsoft"

Contributor License Agreement

@microsoft-github-policy-service agree

* Rows replaced by ResizeTraditional are destroyed, so the images and
  other resources they own are no longer leaked.
* Images are attached to every row they're drawn on, while the cursor
  still moves by the footprint kitty uses.
* Images larger than the storage quota are rejected, the image being
  added is never evicted, and the number of images is limited.
* Freeing an image keeps it while virtual placements still use it.
* Rows joined during reflow keep a single strip of each placement.
* ConPTY marks its cursor position as uncertain after placements, and
  only reads the image dimensions instead of decoding the pixels.
* The D3D renderer draws the cursor above images below the text, and
  doesn't retry bitmaps that failed to be created in every frame.
* GDI clamps the source rectangle, clips images above the text to the
  invalidated area, and limits its bitmap cache by size.
@github-actions

This comment has been minimized.

@DHowett

Copy link
Copy Markdown
Member

XTVERSION

absolutely not :)

@DHowett

Copy link
Copy Markdown
Member

How it works

FWIW, I would have hoped to see something explaining the "placement diacritics" and stuff in this section. It seems like a pretty significant part of how it.. works.. 🙂

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(blocking for direction review)

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Oct 30, 2026
The cursor is drawn after the images below the text, which places its quads within the range that the cursor foreground pass treats as text. That pass now skips everything that isn't text, so it no longer recolors the cursor itself.
@microsoft-github-policy-service microsoft-github-policy-service Bot removed the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label Oct 30, 2026
@mbwilding

Matthew Wilding (mbwilding) commented Oct 30, 2026 •

Copy link
Copy Markdown
Author

How it works

FWIW, I would have hoped to see something explaining the "placement diacritics" and stuff in this section. It seems like a pretty significant part of how it.. works.. 🙂

They're part of the kitty unicode placeholders spec. the app prints U+10EEEE as normal text, the image id goes in the fg colour, the placement id in the underline colour, and the diacritics give the row/column (plus the top byte of the id if it needs it). since it's just text it scrolls and reflows like everything else, so tmux/nvim for example can move images around without knowing anything about the protocol. renderer decodes them per row and draws that part of the image under the text.

@mbwilding

Matthew Wilding (mbwilding) commented Oct 30, 2026 •

Copy link
Copy Markdown
Author

XTVERSION

absolutely not :)

Certain things rely on this for functionality as it identifies the terminal (admittedly some things use this value to apply work arounds for particular terminal emulators). For example the snacks image plugin for neovim will read this value. What's the aversion to having it as part of WT?

@DHowett

Copy link
Copy Markdown
Member

What's the aversion to having it as part of WT?

So, mostly on principle. There's a long discussion in both a PR which attempted to introduce it (#20268) and the original feature request for it (#18382), which I won't go all the way into here.

The TL;DR:

  • But what about User-Agent?
  • You've already had it.
  • We've had one, yes. But what about second User-Agent?

I don't think he knows about second user-agent, Pip.

@mbwilding

Copy link
Copy Markdown
Author

No worries, I have removed the logic for XTVERSION.

@undy-aeon

Copy link
Copy Markdown

If you want to have some good test/benchmark, you can use my music software that support kitty graphic rendering. rustel.cc (ctrl-o for settings > then choose kitty graphics in rendering mode)

@lhecker Leonard Hecker (lhecker) left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I gave this PR a glance and I think this is generally the right direction. It may be worth basing our implementation on this PR out of the 3 we have. Perhaps unsurprisingly it requires significant polishing.

There's also some things that I have personal feelings on. For instance, I think that deflate compressed raw RGB is a particularly bad aspect of the Kitty protocol, to put it mildly.

PNG is literally deflated RGB with a prefilter. Why on god's green earth would you not use it??

I have a strong urge to ask you to remove its support, and similar blasphemous features, but I'm not entirely sure about that yet. 🫥

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

just some thoughts while commit 2917bdc went by my inbox

Comment thread src/terminal/adapter/KittyGraphics.cpp Resolved
Comment thread src/terminal/adapter/KittyGraphics.cpp Resolved
@mbwilding

Copy link
Copy Markdown
Author

just some thoughts while commit 2917bdc went by my inbox

No worries, I sorted those out.

Now, to get this more production ready, what would you like me to sort out in the meantime to move it closer to an approval?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants