KNZ624PA7NXVLCB656OUDYTSMLZFAC6RVRYAIVD6IUISP4FFNGCAC TLLVARZXOP2M3B5VTLF4SYDGMIBPHABE6LJFG77IU53QTSYGTKWAC 3XZAHT6PTML33YSBVF5OSL7KLCRARTHRYPT3N7GUYJKSZKWHBIDQC WEY4JL3IDZTK6Y4H5A6MNC6AFBUUE3ZI3CQ6KHN2T3P2T4JHB2XAC ZFMOUTHEMHYYEAGXRQ3L427FVZPNBC7CX6QATVXU2KPDJOVEH2EQC 3JA7HYRMHV57SIMGMGPDOMKQ3NBQS2SKOX3EKDHRBQRP7ZPZGFTQC U6JEEU5O477ZOJ5UMRMOJSGPEJEU6Q7KMPKUSDF56CYVUJWL7QBQC E56RSY75LHUPKXPOPPGFDHPSACMRWPHOAD2GREJN3OFPA6QFM64QC PZHNIV62T77A3VPGPAYURINYRMUJKMNQHTHYD7L22X7WDZSSKQ7QC IFVRAERTCCDICNTYTG3TX2WASB6RXQQEJWWXQMQZJSQDQ3HLE5OQC // 448x448 grayscale image produces >4096 bytes of base64img := image.NewGray(image.Rect(0, 0, 448, 448))for y := range 448 {for x := range 448 {img.SetGray(x, y, color.Gray{Y: uint8((x + y) % 256)})
// 128x128 random noise image is incompressible — produces >4096 bytes of base64 even with proper LZ77rng := rand.New(rand.NewSource(42))img := image.NewGray(image.Rect(0, 0, 128, 128))for y := range 128 {for x := range 128 {img.SetGray(x, y, color.Gray{Y: uint8(rng.Intn(256))})
// Encode to PNGvar pngBuf bytes.Bufferif err := encodePNG(img, &pngBuf); err != nil {return fmt.Errorf("failed to encode PNG: %w", err)}// Base64 encodebase64Data := base64.StdEncoding.EncodeToString(pngBuf.Bytes())// Small payload: send in a single escape sequence (no m= key needed)const chunkSize = 4096if len(base64Data) <= chunkSize {fmt.Fprintf(w, "\x1b_Gf=100,a=T;%s\x1b\\", base64Data)return nil}// Chunked transmission for large payloadsfor offset := 0; offset < len(base64Data); offset += chunkSize {end := min(offset+chunkSize, len(base64Data))chunk := base64Data[offset:end]if offset == 0 {// First chunk: include format and action, m=1 means more chunks followfmt.Fprintf(w, "\x1b_Gf=100,a=T,m=1;%s\x1b\\", chunk)} else if end < len(base64Data) {// Middle chunk: m=1 means more chunks followfmt.Fprintf(w, "\x1b_Gm=1;%s\x1b\\", chunk)} else {// Last chunk: m=0 means no more chunksfmt.Fprintf(w, "\x1b_Gm=0;%s\x1b\\", chunk)}}return nil
return kitty.EncodeGraphics(w, img, &kitty.Options{Format: kitty.PNG,Action: kitty.TransmitAndPut,Transmission: kitty.Direct,Chunk: true,})
}}return result}// encodePNG encodes an image to PNG format.// Detects whether image is grayscale or RGB and uses appropriate encoder.func encodePNG(img image.Image, w io.Writer) error {if _, ok := img.(*image.Gray); ok {return encodeGrayscalePNG(img, w)}return encodeRGBPNG(img, w)}// encodeGrayscalePNG encodes a grayscale image to PNG format.// This is a minimal implementation that works for 8-bit grayscale images.func encodeGrayscalePNG(img image.Image, w io.Writer) error {bounds := img.Bounds()width := bounds.Dx()height := bounds.Dy()// PNG signaturesignature := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}if _, err := w.Write(signature); err != nil {return err}// IHDR chunkihdr := make([]byte, 13)ihdr[0] = byte(width >> 24)ihdr[1] = byte(width >> 16)ihdr[2] = byte(width >> 8)ihdr[3] = byte(width)ihdr[4] = byte(height >> 24)ihdr[5] = byte(height >> 16)ihdr[6] = byte(height >> 8)ihdr[7] = byte(height)ihdr[8] = 8 // bit depthihdr[9] = 0 // color type: grayscaleihdr[10] = 0 // compression: deflateihdr[11] = 0 // filter: adaptiveihdr[12] = 0 // interlace: noneif err := writePNGChunk(w, "IHDR", ihdr); err != nil {return err}// IDAT chunk (image data)rawData := make([]byte, 0, height*(width+1))for y := range height {rawData = append(rawData, 0) // filter byte (none)for x := range width {c := color.GrayModel.Convert(img.At(x+bounds.Min.X, y+bounds.Min.Y)).(color.Gray)rawData = append(rawData, c.Y)}}compressed := deflateCompress(rawData)if err := writePNGChunk(w, "IDAT", compressed); err != nil {return err}// IEND chunkif err := writePNGChunk(w, "IEND", nil); err != nil {return err}return nil}// encodeRGBPNG encodes an RGB image to PNG format.// This is a minimal implementation that works for 8-bit RGB images.func encodeRGBPNG(img image.Image, w io.Writer) error {bounds := img.Bounds()width := bounds.Dx()height := bounds.Dy()// PNG signaturesignature := []byte{0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A}if _, err := w.Write(signature); err != nil {return err}// IHDR chunkihdr := make([]byte, 13)ihdr[0] = byte(width >> 24)ihdr[1] = byte(width >> 16)ihdr[2] = byte(width >> 8)ihdr[3] = byte(width)ihdr[4] = byte(height >> 24)ihdr[5] = byte(height >> 16)ihdr[6] = byte(height >> 8)ihdr[7] = byte(height)ihdr[8] = 8 // bit depthihdr[9] = 2 // color type: RGBihdr[10] = 0 // compression: deflateihdr[11] = 0 // filter: adaptiveihdr[12] = 0 // interlace: noneif err := writePNGChunk(w, "IHDR", ihdr); err != nil {return err}// IDAT chunk (image data)// RGB: 3 bytes per pixel + 1 filter byte per rowrawData := make([]byte, 0, height*(width*3+1))for y := range height {rawData = append(rawData, 0) // filter byte (none)for x := range width {r, g, b, _ := img.At(x+bounds.Min.X, y+bounds.Min.Y).RGBA()rawData = append(rawData, byte(r>>8), byte(g>>8), byte(b>>8))}}compressed := deflateCompress(rawData)if err := writePNGChunk(w, "IDAT", compressed); err != nil {return err}// IEND chunkif err := writePNGChunk(w, "IEND", nil); err != nil {return err}return nil}// writePNGChunk writes a PNG chunk with CRCfunc writePNGChunk(w io.Writer, chunkType string, data []byte) error {// Length (4 bytes, big-endian)length := uint32(len(data))if _, err := w.Write([]byte{byte(length >> 24),byte(length >> 16),byte(length >> 8),byte(length),}); err != nil {return err}// Chunk type (4 bytes)if _, err := w.Write([]byte(chunkType)); err != nil {return err}// Chunk dataif len(data) > 0 {if _, err := w.Write(data); err != nil {return err
// CRC32 of chunk type + datacrc := crc32PNG([]byte(chunkType), data)if _, err := w.Write([]byte{byte(crc >> 24),byte(crc >> 16),byte(crc >> 8),byte(crc),}); err != nil {return err}return nil}// crc32PNG computes CRC32 for PNGfunc crc32PNG(chunkType, data []byte) uint32 {crc := uint32(0xFFFFFFFF)// Process chunk typefor _, b := range chunkType {crc = updateCRC32(crc, b)}// Process datafor _, b := range data {crc = updateCRC32(crc, b)}return crc ^ 0xFFFFFFFF}// updateCRC32 updates CRC with one bytefunc updateCRC32(crc uint32, b byte) uint32 {// CRC32 polynomial table (standard)const poly = 0xEDB88320crc ^= uint32(b)for range 8 {if crc&1 != 0 {crc = (crc >> 1) ^ poly} else {crc >>= 1}}return crc}// deflateCompress performs simple deflate compression// For simplicity, we use uncompressed deflate blocks (not optimal but works)func deflateCompress(data []byte) []byte {result := make([]byte, 0)// Write zlib headerresult = append(result, 0x78, 0x01) // deflate, level 1// Split into uncompressed blocks (max 65535 bytes each)offset := 0for offset < len(data) {blockSize := min(len(data)-offset, 65535)// Block header: final=1 if last block, type=00 (uncompressed)isFinal := byte(0)if offset+blockSize >= len(data) {isFinal = 1}// Store uncompressed blockresult = append(result, isFinal) // BFINAL + BTYPE=00len := uint16(blockSize)nlen := ^lenresult = append(result, byte(len), byte(len>>8))result = append(result, byte(nlen), byte(nlen>>8))result = append(result, data[offset:offset+blockSize]...)offset += blockSize}// Add Adler-32 checksumadler := adler32(data)result = append(result, byte(adler>>24), byte(adler>>16), byte(adler>>8), byte(adler))