// Package kitty provides Kitty terminal graphics protocol functionality.
package kitty import ( ) // Decoder is a decoder for the Kitty graphics protocol. It supports decoding // images in the 24-bit [RGB], 32-bit [RGBA], and [PNG] formats. It can also // decompress data using zlib. // The default format is 32-bit [RGBA]. type Decoder struct { // Uses zlib decompression. Decompress bool // Can be one of [RGB], [RGBA], or [PNG]. Format int // Width of the image in pixels. This can be omitted if the image is [PNG] // formatted. Width int // Height of the image in pixels. This can be omitted if the image is [PNG] // formatted. Height int } // Decode decodes the image data from r in the specified format. func ( *Decoder) ( io.Reader) (image.Image, error) { if .Decompress { , := zlib.NewReader() if != nil { return nil, fmt.Errorf("failed to create zlib reader: %w", ) } defer .Close() //nolint:errcheck = } if .Format == 0 { .Format = RGBA } switch .Format { case RGBA, RGB: return .decodeRGBA(, .Format == RGBA) case PNG: return png.Decode() //nolint:wrapcheck default: return nil, fmt.Errorf("unsupported format: %d", .Format) } } // decodeRGBA decodes the image data in 32-bit RGBA or 24-bit RGB formats. func ( *Decoder) ( io.Reader, bool) (image.Image, error) { := image.NewRGBA(image.Rect(0, 0, .Width, .Height)) var []byte if { = make([]byte, 4) } else { = make([]byte, 3) } for := range .Height { for := range .Width { if , := io.ReadFull(, [:]); != nil { return nil, fmt.Errorf("failed to read pixel data: %w", ) } if { .SetRGBA(, , color.RGBA{[0], [1], [2], [3]}) } else { .SetRGBA(, , color.RGBA{[0], [1], [2], 0xff}) } } } return , nil }