Raxol.Terminal.ANSI.SixelGraphics (Raxol v2.0.1)
View SourceComplete Sixel graphics support for terminal rendering.
This module provides comprehensive Sixel (DEC Sixel Graphics) support:
- Full Sixel image encoding and decoding
- Advanced color palette management with quantization
- Image format conversion (PNG, JPEG, GIF -> Sixel)
- Color optimization and dithering algorithms
- Animation frame support
- Terminal compatibility detection
- Performance optimizations for large images
Sixel Format
Sixel is a bitmap graphics format developed by Digital Equipment Corporation for their terminals. Each character represents 6 vertical pixels, allowing efficient transmission of images over serial connections.
Features
- PNG/JPEG/GIF to Sixel conversion
- Color palette optimization (up to 256 colors)
- Floyd-Steinberg dithering
- Transparency support
- Animation support for GIF files
- Compression and size optimization
Summary
Functions
Applies dithering to reduce color banding when quantizing colors.
Decodes an ANSI escape sequence into a Sixel image.
Encodes a Sixel image to ANSI escape sequence.
Converts an image from common formats (PNG, JPEG, GIF) to Sixel format.
Gets the current image data.
Gets the current color palette.
Gets the current position.
Gets the current scale factors.
Creates a new Sixel image with default values.
Creates a new Sixel image with specified dimensions.
Optimizes the color palette using quantization algorithms.
Processes a sequence of Sixel data.
Sets the image data for a Sixel image.
Sets the color palette for a Sixel image.
Sets the position for a Sixel image.
Sets the scale factor for a Sixel image.
Checks if the terminal supports Sixel graphics.
Types
@type color_format() :: :rgb | :rgba | :hsl | :indexed
@type dithering_algorithm() :: :none | :floyd_steinberg | :ordered | :random
@type image_format() :: :png | :jpeg | :gif | :bmp | :raw_rgb | :raw_rgba
@type rgb_color() :: {non_neg_integer(), non_neg_integer(), non_neg_integer()}
@type rgba_color() :: {non_neg_integer(), non_neg_integer(), non_neg_integer(), non_neg_integer()}
@type sixel_options() :: %{ optional(:max_colors) => non_neg_integer(), optional(:dithering) => dithering_algorithm(), optional(:transparent_color) => rgb_color() | nil, optional(:optimize_palette) => boolean(), optional(:target_width) => non_neg_integer() | nil, optional(:target_height) => non_neg_integer() | nil, optional(:preserve_aspect_ratio) => boolean() }
@type sixel_state() :: %{ width: non_neg_integer(), height: non_neg_integer(), data: binary(), palette: map(), current_color: non_neg_integer(), pixel_buffer: map() }
@type t() :: %Raxol.Terminal.ANSI.SixelGraphics{ animation_frames: [t()] | nil, attributes: map(), compression_enabled: boolean(), current_color: non_neg_integer(), data: binary(), dithering_algorithm: dithering_algorithm(), height: non_neg_integer(), original_format: image_format() | nil, palette: map(), pixel_buffer: map(), position: {non_neg_integer(), non_neg_integer()}, scale: {non_neg_integer(), non_neg_integer()}, sixel_cursor_pos: {non_neg_integer(), non_neg_integer()}, transparent_color: rgb_color() | nil, width: non_neg_integer() }
Functions
@spec apply_dithering(map(), dithering_algorithm()) :: map()
Applies dithering to reduce color banding when quantizing colors.
Parameters
image- The Sixel imagealgorithm- Dithering algorithm (:floyd_steinberg, :ordered, :none)
Returns
t()- Image with dithering applied
@spec decode(binary()) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Decodes an ANSI escape sequence into a Sixel image.
Parameters
data- The ANSI escape sequence to decode
Returns
A new Raxol.Terminal.ANSI.SixelGraphics.t/0 struct with the decoded image data.
@spec encode(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()) :: binary()
Encodes a Sixel image to ANSI escape sequence.
Parameters
image- The image to encode
Returns
A binary containing the ANSI escape sequence for the Sixel image.
@spec from_image_data(binary(), image_format(), sixel_options()) :: {:ok, map()} | {:error, term()}
Converts an image from common formats (PNG, JPEG, GIF) to Sixel format.
Parameters
image_data- Binary image dataformat- Image format (:png, :jpeg, :gif)options- Sixel conversion options
Returns
{:ok, sixel_image}- Converted Sixel image{:error, reason}- Conversion error
@spec get_data(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()) :: binary()
Gets the current image data.
Parameters
image- The current image
Returns
The binary image data.
@spec get_palette(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()) :: map()
Gets the current color palette.
Parameters
image- The current image
Returns
A map containing the current color palette.
@spec get_position(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()) :: {non_neg_integer(), non_neg_integer()}
Gets the current position.
Parameters
image- The current image
Returns
A tuple {x, y} with the current position.
@spec get_scale(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()) :: {non_neg_integer(), non_neg_integer()}
Gets the current scale factors.
Parameters
image- The current image
Returns
A tuple {x_scale, y_scale} with the current scale factors.
@spec new() :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Creates a new Sixel image with default values.
Returns
A new Raxol.Terminal.ANSI.SixelGraphics.t/0 struct with default values.
@spec new(pos_integer(), pos_integer()) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Creates a new Sixel image with specified dimensions.
Parameters
width- The image width in pixelsheight- The image height in pixels
Returns
A new Raxol.Terminal.ANSI.SixelGraphics.t/0 struct with the specified dimensions.
@spec optimize_palette(map(), non_neg_integer(), atom()) :: map()
Optimizes the color palette using quantization algorithms.
Parameters
image- The Sixel imagemax_colors- Maximum number of colors (default: 256)algorithm- Quantization algorithm (:median_cut, :octree)
Returns
t()- Image with optimized palette
@spec process_sequence( Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t(), binary() ) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Processes a sequence of Sixel data.
Parameters
state- The current Sixel statedata- The Sixel data to process
Returns
A tuple containing the updated state and a response.
@spec set_data(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t(), binary()) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Sets the image data for a Sixel image.
Parameters
image- The current imagedata- The binary image data
Returns
The updated image with new data.
@spec set_palette(Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t(), map()) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Sets the color palette for a Sixel image.
Parameters
image- The current imagepalette- A map of color indices to RGB values
Returns
The updated image with new palette.
@spec set_position( Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t(), non_neg_integer(), non_neg_integer() ) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Sets the position for a Sixel image.
Parameters
image- The current imagex- The horizontal positiony- The vertical position
Returns
The updated image with new position.
@spec set_scale( Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t(), non_neg_integer(), non_neg_integer() ) :: Raxol.Terminal.ANSI.SixelGraphics.Behaviour.t()
Sets the scale factor for a Sixel image.
Parameters
image- The current imagex_scale- The horizontal scale factory_scale- The vertical scale factor
Returns
The updated image with new scale factors.
@spec supported?() :: boolean()
Checks if the terminal supports Sixel graphics.
Returns
true if Sixel graphics are supported, false otherwise.