package kittyimport ()// 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].typeDecoderstruct {// Uses zlib decompression.Decompressbool// Can be one of [RGB], [RGBA], or [PNG].Formatint// Width of the image in pixels. This can be omitted if the image is [PNG] // formatted.Widthint// Height of the image in pixels. This can be omitted if the image is [PNG] // formatted.Heightint}// Decode decodes the image data from r in the specified format.func ( *Decoder) ( io.Reader) (image.Image, error) {if .Decompress { , := zlib.NewReader()if != nil {returnnil, fmt.Errorf("failed to create zlib reader: %w", ) }defer .Close() //nolint:errcheck = }if .Format == 0 { .Format = RGBA }switch .Format {caseRGBA, RGB:return .decodeRGBA(, .Format == RGBA)casePNG:returnpng.Decode() //nolint:wrapcheckdefault:returnnil, 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 []byteif { = make([]byte, 4) } else { = make([]byte, 3) }for := range .Height {for := range .Width {if , := io.ReadFull(, [:]); != nil {returnnil, 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}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)