From fd4c72eb929204b896706671f1217c884664ccf8 Mon Sep 17 00:00:00 2001 From: Nithish Anand B Date: Thu, 8 Jan 2026 17:37:44 +0530 Subject: [PATCH] Enhance decodeRect for 16bpp pixel format support Modified decodeRect to handle 16bpp raw pixel format decoding with customizable parameters. --- core/decoders/raw.js | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/core/decoders/raw.js b/core/decoders/raw.js index bd5d2044..26a71514 100644 --- a/core/decoders/raw.js +++ b/core/decoders/raw.js @@ -12,7 +12,7 @@ export default class RawDecoder { this._lines = 0; } - decodeRect(x, y, width, height, sock, display, depth) { + decodeRect(x, y, width, height, sock, display, depth, pixelFormat) { if ((width === 0) || (height === 0)) { return true; } @@ -21,7 +21,7 @@ export default class RawDecoder { this._lines = height; } - const pixelSize = depth == 8 ? 1 : 4; + const pixelSize = depth == 8 ? 1 : (depth == 16 ? 2 : 4); // Modifications const bytesPerLine = width * pixelSize; while (this._lines > 0) { @@ -43,6 +43,36 @@ export default class RawDecoder { newdata[i * 4 + 3] = 255; } data = newdata; + } else if (depth == 16) { // Modifications: decode 16bpp raw + const fmt = pixelFormat || {}; + const redMax = fmt.redMax !== undefined ? fmt.redMax : 31; + const greenMax = fmt.greenMax !== undefined ? fmt.greenMax : 63; + const blueMax = fmt.blueMax !== undefined ? fmt.blueMax : 31; + const redShift = fmt.redShift !== undefined ? fmt.redShift : 11; + const greenShift = fmt.greenShift !== undefined ? fmt.greenShift : 5; + const blueShift = fmt.blueShift !== undefined ? fmt.blueShift : 0; + const bigEndian = !!fmt.bigEndian; + + const newdata = new Uint8Array(width * 4); + for (let i = 0; i < width; i++) { + const idx = i * 2; + let pixel; + if (bigEndian) { + pixel = (data[idx] << 8) | data[idx + 1]; + } else { + pixel = data[idx] | (data[idx + 1] << 8); + } + + const r = (pixel >> redShift) & redMax; + const g = (pixel >> greenShift) & greenMax; + const b = (pixel >> blueShift) & blueMax; + + newdata[i * 4 + 0] = redMax ? (r * 255 / redMax) : 0; + newdata[i * 4 + 1] = greenMax ? (g * 255 / greenMax) : 0; + newdata[i * 4 + 2] = blueMax ? (b * 255 / blueMax) : 0; + newdata[i * 4 + 3] = 255; + } + data = newdata; } // Max sure the image is fully opaque