@@ -16,24 +16,23 @@ import {
1616 IKittyCommand ,
1717 IPendingTransmission ,
1818 IKittyImageData ,
19- BYTES_PER_PIXEL_RGB ,
20- BYTES_PER_PIXEL_RGBA ,
21- ALPHA_OPAQUE ,
19+ KittyPixelConstants ,
2220 parseKittyCommand
2321} from './KittyGraphicsTypes' ;
2422
25- // Memory limit for base64 decoder (4MB, same as IIPHandler)
26- const DECODER_KEEP_DATA = 4194304 ;
27- const DECODER_INITIAL_DATA = 4194304 ; // 4MB
28-
29- // Local mirror of const enum (esbuild can't inline const enums from external packages)
30- const DECODER_OK : DecodeStatus . OK = 0 ;
31-
32- // Maximum control data size
33- const MAX_CONTROL_DATA_SIZE = 512 ;
23+ const enum Constants {
24+ // Memory limit for base64 decoder (4MB, same as IIPHandler)
25+ DECODER_KEEP_DATA = 4194304 ,
26+ DECODER_INITIAL_DATA = 4194304 , // 4MB
27+ // Local mirror of const enum (esbuild can't inline const enums from external packages)
28+ DECODER_OK = 0 ,
29+ // Maximum control data size
30+ MAX_CONTROL_DATA_SIZE = 512 ,
31+ // Semicolon codepoint
32+ SEMICOLON = 0x3B
33+ }
3434
35- // Semicolon codepoint
36- const SEMICOLON = 0x3B ;
35+ const DECODER_OK = Constants . DECODER_OK as unknown as DecodeStatus . OK ;
3736
3837// Kitty graphics protocol handler with streaming base64 decoding.
3938export class KittyGraphicsHandler implements IApcHandler , IResetHandler , IDisposable {
@@ -50,7 +49,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
5049 private _inControlData = true ;
5150
5251 // Buffer for control data.
53- private _controlData = new Uint32Array ( MAX_CONTROL_DATA_SIZE ) ;
52+ private _controlData = new Uint32Array ( Constants . MAX_CONTROL_DATA_SIZE ) ;
5453 private _controlLength = 0 ;
5554
5655 // Pre-calculated encoded size limit
@@ -77,7 +76,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
7776 // Convert decoded size limit -> max encoded bytes.
7877 this . _maxEncodedBytes = Math . ceil ( this . _opts . kittySizeLimit * 4 / 3 ) ;
7978 // ensure we preallocate more than configured limit while using 4mb initial size.
80- this . _initialEncodedBytes = Math . min ( DECODER_INITIAL_DATA , this . _maxEncodedBytes ) ;
79+ this . _initialEncodedBytes = Math . min ( Constants . DECODER_INITIAL_DATA , this . _maxEncodedBytes ) ;
8180 }
8281
8382 public reset ( ) : void {
@@ -129,7 +128,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
129128 // Scan for semicolon
130129 let controlEnd = end ;
131130 for ( let i = start ; i < end ; i ++ ) {
132- if ( data [ i ] === SEMICOLON ) {
131+ if ( data [ i ] === Constants . SEMICOLON ) {
133132 this . _inControlData = false ;
134133 controlEnd = i ;
135134 break ;
@@ -138,7 +137,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
138137
139138 // Copy control data
140139 const copyLength = controlEnd - start ;
141- if ( this . _controlLength + copyLength > MAX_CONTROL_DATA_SIZE ) {
140+ if ( this . _controlLength + copyLength > Constants . MAX_CONTROL_DATA_SIZE ) {
142141 this . _aborted = true ;
143142 return ;
144143 }
@@ -201,7 +200,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
201200 this . _activeDecoder = pending . decoder ;
202201 }
203202 if ( ! this . _activeDecoder ) {
204- this . _activeDecoder = new Base64Decoder ( DECODER_KEEP_DATA , this . _maxEncodedBytes , this . _initialEncodedBytes ) ;
203+ this . _activeDecoder = new Base64Decoder ( Constants . DECODER_KEEP_DATA , this . _maxEncodedBytes , this . _initialEncodedBytes ) ;
205204 this . _activeDecoder . init ( ) ;
206205 }
207206
@@ -484,7 +483,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
484483 return true ;
485484 }
486485
487- const bytesPerPixel = format === KittyFormat . RGBA ? BYTES_PER_PIXEL_RGBA : BYTES_PER_PIXEL_RGB ;
486+ const bytesPerPixel = format === KittyFormat . RGBA ? KittyPixelConstants . BYTES_PER_PIXEL_RGBA : KittyPixelConstants . BYTES_PER_PIXEL_RGB ;
488487 const expectedBytes = width * height * bytesPerPixel ;
489488
490489 if ( bytes . length < expectedBytes ) {
@@ -723,7 +722,7 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
723722 throw new Error ( 'Width and height required for raw pixel data' ) ;
724723 }
725724
726- const bytesPerPixel = image . format === KittyFormat . RGBA ? BYTES_PER_PIXEL_RGBA : BYTES_PER_PIXEL_RGB ;
725+ const bytesPerPixel = image . format === KittyFormat . RGBA ? KittyPixelConstants . BYTES_PER_PIXEL_RGBA : KittyPixelConstants . BYTES_PER_PIXEL_RGB ;
727726 const expectedBytes = width * height * bytesPerPixel ;
728727
729728 if ( bytes . length < expectedBytes ) {
@@ -734,13 +733,13 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
734733
735734 if ( image . format === KittyFormat . RGBA ) {
736735 // RGBA: use bytes directly — no copy needed
737- return createImageBitmap ( new ImageData ( new Uint8ClampedArray ( bytes . buffer as ArrayBuffer , bytes . byteOffset , pixelCount * BYTES_PER_PIXEL_RGBA ) , width , height ) ) ;
736+ return createImageBitmap ( new ImageData ( new Uint8ClampedArray ( bytes . buffer as ArrayBuffer , bytes . byteOffset , pixelCount * KittyPixelConstants . BYTES_PER_PIXEL_RGBA ) , width , height ) ) ;
738737 }
739738
740739 // RGB→RGBA: interleave alpha using uint32 block processing (4 pixels per iteration).
741740 // 3 uint32 reads + 4 uint32 writes per 4 pixels vs 28 byte reads/writes — ~6x faster.
742741 // Assumes little-endian (all modern browsers/Node.js).
743- const data = new Uint8ClampedArray ( pixelCount * BYTES_PER_PIXEL_RGBA ) ;
742+ const data = new Uint8ClampedArray ( pixelCount * KittyPixelConstants . BYTES_PER_PIXEL_RGBA ) ;
744743 const src32 = new Uint32Array ( bytes . buffer , bytes . byteOffset , Math . floor ( bytes . byteLength / 4 ) ) ;
745744 const dst32 = new Uint32Array ( data . buffer ) ;
746745 const alignedPixels = pixelCount & ~ 3 ; // round down to multiple of 4
@@ -759,15 +758,15 @@ export class KittyGraphicsHandler implements IApcHandler, IResetHandler, IDispos
759758 }
760759
761760 // Handle remaining 1–3 pixels
762- let srcByte = alignedPixels * BYTES_PER_PIXEL_RGB ;
763- let dstByte = alignedPixels * BYTES_PER_PIXEL_RGBA ;
761+ let srcByte = alignedPixels * KittyPixelConstants . BYTES_PER_PIXEL_RGB ;
762+ let dstByte = alignedPixels * KittyPixelConstants . BYTES_PER_PIXEL_RGBA ;
764763 for ( let i = alignedPixels ; i < pixelCount ; i ++ ) {
765764 data [ dstByte ] = bytes [ srcByte ] ;
766765 data [ dstByte + 1 ] = bytes [ srcByte + 1 ] ;
767766 data [ dstByte + 2 ] = bytes [ srcByte + 2 ] ;
768- data [ dstByte + 3 ] = ALPHA_OPAQUE ;
769- srcByte += BYTES_PER_PIXEL_RGB ;
770- dstByte += BYTES_PER_PIXEL_RGBA ;
767+ data [ dstByte + 3 ] = KittyPixelConstants . ALPHA_OPAQUE ;
768+ srcByte += KittyPixelConstants . BYTES_PER_PIXEL_RGB ;
769+ dstByte += KittyPixelConstants . BYTES_PER_PIXEL_RGBA ;
771770 }
772771
773772 return createImageBitmap ( new ImageData ( data , width , height ) ) ;
0 commit comments