11import { AppError } from '@agent-device/kernel/errors' ;
22import { decode as decodeJpeg } from 'jpeg-js' ;
3- import { hasPngSignature , PNG } from './png.ts' ;
3+ import { decodePng , hasPngSignature , PNG } from './png.ts' ;
44
55const JPEG_SIGNATURE = Buffer . from ( [ 0xff , 0xd8 , 0xff ] ) ;
66
@@ -13,35 +13,55 @@ export function detectScreenshotImageFormat(bytes: Buffer): ScreenshotImageForma
1313 return undefined ;
1414}
1515
16+ /**
17+ * Decodes a screenshot in whatever container it arrived in into the RGBA rows every pixel reader in
18+ * this repository consumes. The container is sniffed, never trusted from a file name or a caller's
19+ * guess, so a JPEG stored under a `.png` name still decodes.
20+ *
21+ * Synchronous and CPU-bound: daemon request paths reach it through the PNG worker
22+ * (`decodeScreenshotImageAsync` in `png-worker-client.ts`), like every other codec job.
23+ */
24+ export function decodeScreenshotImage ( bytes : Buffer , label : string ) : PNG {
25+ const format = detectScreenshotImageFormat ( bytes ) ;
26+ if ( format === 'png' ) return decodePng ( bytes , label ) ;
27+ if ( format === 'jpeg' ) return decodeJpegImage ( bytes , label ) ;
28+ throw unsupportedContainer ( label , bytes ) ;
29+ }
30+
1631/**
1732 * Returns PNG bytes for a screenshot a provider handed back in whatever container it prefers. PNG
1833 * passes through untouched; JPEG is decoded and re-encoded losslessly from the decoded pixels, so
19- * every PNG-only reader downstream (size, crop, overlay, diff) sees the format the path promises.
34+ * every PNG-only reader downstream (size, crop, overlay) sees the format the path promises. Those
35+ * readers rewrite the file in place, which is why a JPEG cannot be handed to them undecoded.
2036 *
2137 * Synchronous and CPU-bound: daemon request paths reach it through the PNG worker
2238 * (`transcodeScreenshotToPngAsync` in `png-worker-client.ts`), like every other codec job.
2339 */
2440export function transcodeScreenshotToPng ( bytes : Buffer , label : string ) : Buffer {
2541 const format = detectScreenshotImageFormat ( bytes ) ;
2642 if ( format === 'png' ) return bytes ;
27- if ( format === 'jpeg' ) return jpegToPng ( bytes , label ) ;
28- throw new AppError ( 'COMMAND_FAILED' , `${ label } is neither PNG nor JPEG` , {
29- label,
30- leadingBytes : bytes . subarray ( 0 , 4 ) . toString ( 'hex' ) ,
31- } ) ;
43+ if ( format === 'jpeg' ) return PNG . sync . write ( decodeJpegImage ( bytes , label ) ) ;
44+ throw unsupportedContainer ( label , bytes ) ;
3245}
3346
3447/** A JPEG signature does not prove a decodable body; a failure keeps the label and the decoder's reason. */
35- function jpegToPng ( bytes : Buffer , label : string ) : Buffer {
48+ function decodeJpegImage ( bytes : Buffer , label : string ) : PNG {
3649 try {
3750 const decoded = decodeJpeg ( bytes , { useTArray : true , formatAsRGBA : true } ) ;
3851 const png = new PNG ( { width : decoded . width , height : decoded . height } ) ;
3952 png . data = Buffer . from ( decoded . data . buffer , decoded . data . byteOffset , decoded . data . byteLength ) ;
40- return PNG . sync . write ( png ) ;
53+ return png ;
4154 } catch ( error ) {
4255 throw new AppError ( 'COMMAND_FAILED' , `Failed to decode ${ label } as JPEG` , {
4356 label,
4457 reason : error instanceof Error ? error . message : String ( error ) ,
4558 } ) ;
4659 }
4760}
61+
62+ function unsupportedContainer ( label : string , bytes : Buffer ) : AppError {
63+ return new AppError ( 'COMMAND_FAILED' , `${ label } is neither PNG nor JPEG` , {
64+ label,
65+ leadingBytes : bytes . subarray ( 0 , 4 ) . toString ( 'hex' ) ,
66+ } ) ;
67+ }
0 commit comments