From e9e749e1ce811f95c73f4e720d32a72f95c351ff Mon Sep 17 00:00:00 2001 From: Sashank Aryal Date: Thu, 2 Jan 2025 12:50:13 -0600 Subject: [PATCH] modify buffer in place --- .../looker/src/worker/canvas-decoder.ts | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/app/packages/looker/src/worker/canvas-decoder.ts b/app/packages/looker/src/worker/canvas-decoder.ts index 3af00d430a..d0ed29f69f 100644 --- a/app/packages/looker/src/worker/canvas-decoder.ts +++ b/app/packages/looker/src/worker/canvas-decoder.ts @@ -69,11 +69,20 @@ export const decodeWithCanvas = async (blob: Blob) => { if (channels === 1) { // get rid of the G, B, and A channels, new buffer will be 1/4 the size - const data = new Uint8ClampedArray(width * height); - for (let i = 0; i < data.length; i++) { - data[i] = imageData.data[i * 4]; + const rawBuffer = imageData.data; + const totalPixels = width * height; + + let read = 0; + let write = 0; + + while (write < totalPixels) { + rawBuffer[write++] = rawBuffer[read]; + // skip "G,B,A" + read += 4; } - imageData.data.set(data); + + const grayScaleData = rawBuffer.slice(0, totalPixels); + rawBuffer.set(grayScaleData); } return {