All files / packages/core/src/utilities convertToGrayscale.ts

0% Statements 0/19
0% Branches 0/6
0% Functions 0/1
0% Lines 0/17

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28                                                       
export default function convertToGrayscale(
  scalarData,
  width: number,
  height: number
) {
  const isRGBA = scalarData.length === width * height * 4;
  const isRGB = scalarData.length === width * height * 3;
  if (isRGBA || isRGB) {
    const newScalarData = new Float32Array(width * height);
    let offset = 0;
    let destOffset = 0;
    const increment = isRGBA ? 4 : 3;
    for (let x = 0; x < width; x++) {
      for (let y = 0; y < height; y++) {
        const r = scalarData[offset];
        const g = scalarData[offset + 1];
        const b = scalarData[offset + 2];
        newScalarData[destOffset] = (r + g + b) / 3;
        offset += increment;
        destOffset++;
      }
    }
    return newScalarData;
  } else {
    return scalarData;
  }
}