Raxol.Terminal.Graphics.GraphicsServer (Raxol v2.0.1)

View Source

Provides unified graphics capabilities for the terminal emulator with automatic protocol detection and fallback support.

This module handles:

  • Automatic graphics protocol detection (Kitty, iTerm2, Sixel)
  • Protocol-agnostic image display interface
  • Graceful fallback between supported protocols
  • Graphics state management and context switching
  • Performance optimization and caching

Supported Protocols

  • Kitty Graphics Protocol - High-performance, supports animations and transparency
  • iTerm2 Inline Images - Good compatibility, limited animation support
  • Sixel Graphics - Wide terminal support, basic image display
  • Fallback Mode - ASCII art conversion for unsupported terminals

Usage

# Automatic protocol detection and image display
{:ok, _} = GraphicsServer.display_image(image_data, %{
  width: 300,
  height: 200,
  format: :png
})

# Query available protocols
graphics_info = GraphicsServer.get_graphics_info()

Summary

Functions

Returns a specification to start this module under a supervisor.

Cleans up resources.

Clears the graphics context.

Closes a graphics context.

Converts an image from one format to another with display optimization.

Creates an animation from multiple image frames.

Creates a new graphics context with the given configuration.

Displays an image using the best available graphics protocol.

Processes and displays multiple images with optimized format conversion.

Gets the active graphics context ID.

Gets the list of all graphics contexts.

Gets information about available graphics protocols and capabilities.

Gets the state of a specific graphics context.

Gets comprehensive information about a processed image.

Manages the image cache for performance optimization.

Creates optimized variants for different terminal capabilities.

Renders graphics data to the specified context.

Sets the active graphics context.

Sets the preferred graphics protocol for future operations.

Updates the graphics manager configuration.

Updates the configuration of a specific graphics context.

Updates properties of an existing graphics element for animation purposes.

Types

display_options()

@type display_options() :: %{
  optional(:x) => non_neg_integer(),
  optional(:y) => non_neg_integer(),
  optional(:width) => non_neg_integer(),
  optional(:height) => non_neg_integer(),
  optional(:scale) => float(),
  optional(:preserve_aspect_ratio) => boolean(),
  optional(:protocol) => graphics_protocol(),
  optional(:quality) => 1..100,
  optional(:dither) => :none | :riemersma | :floyd_steinberg | :ordered,
  optional(:color_optimization) => boolean(),
  optional(:cache_enabled) => boolean(),
  optional(:processing_options) => map()
}

graphics_config()

@type graphics_config() :: %{
  optional(:width) => non_neg_integer(),
  optional(:height) => non_neg_integer(),
  optional(:format) => image_format(),
  optional(:compression) => :none | :zlib | :lz4,
  optional(:quality) => 0..100,
  optional(:protocol) => graphics_protocol(),
  optional(:fallback_enabled) => boolean()
}

graphics_id()

@type graphics_id() :: non_neg_integer()

graphics_protocol()

@type graphics_protocol() :: :kitty | :iterm2 | :sixel | :fallback

graphics_state()

@type graphics_state() :: :active | :inactive | :hidden

image_format()

@type image_format() :: :rgb | :rgba | :png | :jpeg | :webp | :gif

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

cleanup()

@spec cleanup() :: :ok

Cleans up resources.

clear_graphics(graphics_id)

@spec clear_graphics(graphics_id()) :: :ok | {:error, term()}

Clears the graphics context.

close_graphics(graphics_id)

@spec close_graphics(graphics_id()) :: :ok | {:error, term()}

Closes a graphics context.

convert_and_display(image_data, target_format, options \\ %{})

@spec convert_and_display(binary(), image_format(), display_options()) ::
  {:ok, graphics_id()} | {:error, term()}

Converts an image from one format to another with display optimization.

Parameters

  • image_data - Source image data
  • target_format - Target format (:png, :jpeg, :webp, :gif)
  • options - Conversion and display options

Returns

  • {:ok, graphics_id} - Successfully converted and displayed
  • {:error, reason} - Conversion failed

create_animation(frames, options \\ %{})

@spec create_animation([binary()], map()) :: {:ok, graphics_id()} | {:error, term()}

Creates an animation from multiple image frames.

Parameters

  • frames - List of image data for animation frames
  • options - Animation options (frame_delay, loop_count, protocol)

Returns

  • {:ok, graphics_id} - Successfully created animation
  • {:error, reason} - Error with reason

create_graphics(config \\ %{})

@spec create_graphics(map()) :: {:ok, graphics_id()} | {:error, term()}

Creates a new graphics context with the given configuration.

display_image(image_data, options \\ %{})

@spec display_image(binary() | String.t(), display_options()) ::
  {:ok, graphics_id()} | {:error, term()}

Displays an image using the best available graphics protocol.

Automatically detects the optimal protocol and falls back gracefully if the preferred protocol is not supported.

Parameters

  • image_data - Binary image data or file path
  • options - Display options including size, position, format

Returns

  • {:ok, graphics_id} - Successfully displayed image
  • {:error, reason} - Error with reason

Examples

# Display PNG image with automatic protocol detection
{:ok, id} = GraphicsServer.display_image(png_data, %{
  width: 300,
  height: 200,
  format: :png
})

# Display with specific protocol preference
{:ok, id} = GraphicsServer.display_image(image_data, %{
  width: 400,
  height: 300,
  protocol: :kitty,
  fallback_enabled: true
})

display_images(images, shared_options \\ %{})

@spec display_images(
  [binary() | {binary(), display_options()}],
  display_options()
) :: {:ok, [graphics_id()]} | {:error, term()}

Processes and displays multiple images with optimized format conversion.

Automatically detects image formats, applies appropriate processing, and selects the best display protocol for each image.

Parameters

  • images - List of image data or {data, options} tuples
  • shared_options - Options applied to all images

Returns

  • {:ok, [graphics_id]} - Successfully processed and displayed images
  • {:error, reason} - Processing failed

Examples

# Process multiple images with shared settings
{:ok, ids} = GraphicsServer.display_images([
  png_data,
  {jpeg_data, %{quality: 95}},
  svg_data
], %{
  width: 300,
  height: 200,
  optimize_for_terminal: true
})

get_active_graphics()

@spec get_active_graphics() :: {:ok, graphics_id()} | {:error, :no_active_graphics}

Gets the active graphics context ID.

get_graphics()

@spec get_graphics() :: [graphics_id()]

Gets the list of all graphics contexts.

get_graphics_info()

@spec get_graphics_info() :: map()

Gets information about available graphics protocols and capabilities.

Returns

A map containing:

  • :supported_protocols - List of supported protocols
  • :preferred_protocol - Recommended protocol for best performance
  • :terminal_info - Detected terminal information
  • :capabilities - Protocol-specific capabilities

Examples

iex> GraphicsServer.get_graphics_info()
%{
  supported_protocols: [:kitty, :sixel],
  preferred_protocol: :kitty,
  terminal_info: %{type: :kitty, version: "0.26.5"},
  capabilities: %{
    kitty: %{max_image_size: 100_000_000, supports_animation: true},
    sixel: %{max_image_size: 1_000_000, supports_animation: false}
  }
}

get_graphics_state(graphics_id)

@spec get_graphics_state(graphics_id()) :: {:ok, map()} | {:error, term()}

Gets the state of a specific graphics context.

get_image_info(graphics_id)

@spec get_image_info(graphics_id()) :: {:ok, map()} | {:error, term()}

Gets comprehensive information about a processed image.

Parameters

  • graphics_id - ID of previously processed image

Returns

  • {:ok, image_info} - Image metadata and processing information
  • {:error, reason} - Image not found or error

handle_manager_cast(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_cast/2.

handle_manager_info(msg, state)

Callback implementation for Raxol.Core.Behaviours.BaseManager.handle_manager_info/2.

manage_cache(action, params \\ %{})

@spec manage_cache(atom(), map()) :: term()

Manages the image cache for performance optimization.

Parameters

  • action - Cache action (:get_stats, :clear, :configure)
  • params - Optional parameters for the action

Returns

  • Cache operation result

optimize_and_display(image_data, options \\ %{})

@spec optimize_and_display(binary(), display_options()) ::
  {:ok, graphics_id()} | {:error, term()}

Creates optimized variants for different terminal capabilities.

Generates multiple versions of an image optimized for different terminal types and automatically selects the best one to display.

Parameters

  • image_data - Source image data
  • options - Processing and display options

Returns

  • {:ok, graphics_id} - Successfully optimized and displayed
  • {:error, reason} - Optimization failed

render_graphics(graphics_id, data)

@spec render_graphics(graphics_id(), binary()) :: :ok | {:error, term()}

Renders graphics data to the specified context.

set_active_graphics(graphics_id)

@spec set_active_graphics(graphics_id()) :: :ok | {:error, term()}

Sets the active graphics context.

set_preferred_protocol(protocol, fallback_enabled \\ true)

@spec set_preferred_protocol(graphics_protocol() | :auto, boolean()) ::
  :ok | {:error, term()}

Sets the preferred graphics protocol for future operations.

Parameters

  • protocol - Preferred protocol (:kitty, :iterm2, :sixel, :auto)
  • fallback_enabled - Whether to enable fallback to other protocols

Returns

  • :ok - Protocol preference updated
  • {:error, reason} - Error with reason

start_link(init_opts \\ [])

update_config(config)

@spec update_config(map()) :: :ok

Updates the graphics manager configuration.

update_graphics_config(graphics_id, config)

@spec update_graphics_config(graphics_id(), graphics_config()) ::
  :ok | {:error, term()}

Updates the configuration of a specific graphics context.

update_graphics_properties(graphics_id, properties)

@spec update_graphics_properties(graphics_id(), map()) :: :ok | {:error, term()}

Updates properties of an existing graphics element for animation purposes.

Parameters

  • graphics_id - ID of the graphics element to update
  • properties - Map of properties to update (opacity, position, scale, etc.)

Returns

  • :ok - Properties updated successfully
  • {:error, reason} - Update failed