Skip to content

Commit

Permalink
modify buffer in place
Browse files Browse the repository at this point in the history
  • Loading branch information
sashankaryal committed Jan 2, 2025
1 parent 4bced93 commit e9e749e
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions app/packages/looker/src/worker/canvas-decoder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit e9e749e

Please sign in to comment.