Complete Kitty graphics protocol support for terminal rendering.
This module provides comprehensive Kitty Graphics Protocol support:
- Full image encoding and decoding
- RGB, RGBA, and PNG format support
- Zlib compression support
- Multi-chunk transmission for large images
- Image placement and positioning
- Image deletion and management
- Animation frame support
Kitty Graphics Protocol
The Kitty Graphics Protocol is a modern graphics protocol that enables pixel-level graphics rendering in compatible terminals. It uses APC (Application Program Command) escape sequences and supports:
- Multiple image formats (RGB, RGBA, PNG)
- Compression (zlib)
- Chunked transmission for large images
- Image placement at cell or pixel level
- Z-index layering
- Animation support
Usage
# Create a new image
image = KittyGraphics.new(100, 100)
# Set image data
image = KittyGraphics.set_data(image, pixel_data)
# Encode for transmission
escape_sequence = KittyGraphics.encode(image)
Summary
Functions
Adds an animation frame to the image.
Decodes an APC escape sequence into a Kitty image.
Deletes an image by its ID.
Encodes a Kitty image to APC escape sequence.
Generates a delete command for an image.
Generates a query command for image capabilities.
Gets the current animation frame.
Gets the current image data.
Creates a new Kitty image with default values.
Creates a new Kitty image with specified dimensions.
Advances to the next animation frame.
Places an image at a specific position.
Processes a Kitty graphics protocol sequence.
Queries information about an image.
Sets the compression method.
Sets the image data for a Kitty image.
Sets the image format.
Checks if the terminal supports Kitty graphics.
Transmits an image to the terminal.
Types
@type action() :: :transmit | :transmit_display | :display | :delete | :query | :frame
@type compression() :: :none | :zlib
@type format() :: :rgb | :rgba | :png
@type t() :: %Raxol.Terminal.ANSI.KittyGraphics{ animation_frames: [binary()], cell_position: {non_neg_integer(), non_neg_integer()} | nil, compression: compression(), current_frame: non_neg_integer(), data: binary(), format: format(), height: non_neg_integer(), image_id: non_neg_integer() | nil, pixel_buffer: binary(), placement_id: non_neg_integer() | nil, position: {non_neg_integer(), non_neg_integer()}, width: non_neg_integer(), z_index: integer() }
@type transmission() :: :direct | :file | :temp_file | :shared_memory
Functions
Adds an animation frame to the image.
Parameters
image- The current image stateframe_data- Binary data for the new frame
Returns
The updated image with the new frame added.
Decodes an APC escape sequence into a Kitty image.
Parameters
data- The APC escape sequence to decode
Returns
A new Raxol.Terminal.ANSI.KittyGraphics.t/0 struct with the decoded image data.
Deletes an image by its ID.
Parameters
image- The current image stateimage_id- The ID of the image to delete
Returns
The updated image state (cleared if ID matches).
Encodes a Kitty image to APC escape sequence.
Generates the complete escape sequence for transmitting the image to a Kitty-compatible terminal.
Parameters
image- The image to encode
Returns
A binary containing the APC escape sequence for the Kitty image.
Generates a delete command for an image.
Parameters
image_id- The ID of the image to deleteopts- Delete options::delete_action- What to delete (:all, :id, :placement, :z_index, :cell, :animation)
Returns
The APC escape sequence for the delete command.
Generates a query command for image capabilities.
Returns
The APC escape sequence for querying terminal capabilities.
Gets the current animation frame.
Parameters
image- The current image
Returns
The binary data for the current animation frame, or nil if no frames.
Gets the current image data.
Parameters
image- The current image
Returns
The binary image data.
Creates a new Kitty image with default values.
Returns
A new Raxol.Terminal.ANSI.KittyGraphics.t/0 struct with default values.
Creates a new Kitty image with specified dimensions.
Parameters
width- The image width in pixelsheight- The image height in pixels
Returns
A new Raxol.Terminal.ANSI.KittyGraphics.t/0 struct with the specified dimensions.
Advances to the next animation frame.
Parameters
image- The current image
Returns
The updated image with the next frame selected.
Places an image at a specific position.
Parameters
image- The current image stateopts- Placement options::x- Pixel X offset within cell:y- Pixel Y offset within cell:cell_x- Cell column position:cell_y- Cell row position:z- Z-index for layering
Returns
The updated image state with placement information.
Processes a Kitty graphics protocol sequence.
Parameters
state- The current Kitty graphics statedata- The Kitty graphics data to process (control + payload)
Returns
A tuple containing the updated state and a response:
{updated_state, :ok}- Successful processing{state, {:error, reason}}- Processing error
Queries information about an image.
Parameters
image- The current image stateimage_id- The ID of the image to query
Returns
{:ok, info_map}- Image information if found{:error, :not_found}- Image not found
Sets the compression method.
Parameters
image- The current imagecompression- The compression method (:none, :zlib)
Returns
The updated image with the new compression setting.
Sets the image data for a Kitty image.
Parameters
image- The current imagedata- The binary image data (raw pixels or PNG)
Returns
The updated image with new data.
Sets the image format.
Parameters
image- The current imageformat- The format (:rgb, :rgba, :png)
Returns
The updated image with the new format.
Checks if the terminal supports Kitty graphics.
Returns
true if Kitty graphics are supported, false otherwise.
Transmits an image to the terminal.
Parameters
image- The current image stateopts- Transmission options::format- Image format (:rgb, :rgba, :png):compression- Compression method (:none, :zlib):id- Optional image ID for later reference
Returns
The updated image state.