From 664146fd69352ada2380de1e3223d40a70725c6e Mon Sep 17 00:00:00 2001 From: Alexy Date: Sun, 31 Oct 2021 15:35:30 -0400 Subject: [PATCH 1/3] Applying colormap on color images (like ultrasounds) --- .../storedPixelDataToCanvasImageDataPseudocolorLUT.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js index 8f60b193..d7222e80 100644 --- a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js +++ b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js @@ -35,7 +35,10 @@ function storedPixelDataToCanvasImageDataPseudocolorLUT (image, grayscaleLut, co if (minPixelValue < 0) { while (storedPixelDataIndex < numPixels) { - grayscale = grayscaleLut[pixelData[storedPixelDataIndex++] + (-minPixelValue)]; + grayscale = grayscaleLut[pixelData[storedPixelDataIndex] - minPixelValue]; + //Incrementing by 1 pixel only works with original black and white images. RGBA comes in groups of 4 pixels + //(same for the else condition below) + storedPixelDataIndex += (image.color ? 4 : 1); rgba = clut[grayscale]; canvasImageDataData[canvasImageDataIndex++] = rgba[0]; canvasImageDataData[canvasImageDataIndex++] = rgba[1]; @@ -44,7 +47,8 @@ function storedPixelDataToCanvasImageDataPseudocolorLUT (image, grayscaleLut, co } } else { while (storedPixelDataIndex < numPixels) { - grayscale = grayscaleLut[pixelData[storedPixelDataIndex++]]; + grayscale = grayscaleLut[pixelData[storedPixelDataIndex]]; + storedPixelDataIndex += (image.color ? 4 : 1); rgba = clut[grayscale]; canvasImageDataData[canvasImageDataIndex++] = rgba[0]; canvasImageDataData[canvasImageDataIndex++] = rgba[1]; From 0cc3583652d1db91fade0fab718866aa0035d7ed Mon Sep 17 00:00:00 2001 From: Alexy Date: Fri, 12 Nov 2021 15:58:26 -0500 Subject: [PATCH 2/3] Applying color LUT to RGBA images. For RGBA images, it calculates the grayscale pixel value using the luminosity method. --- ...ixelDataToCanvasImageDataPseudocolorLUT.js | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js index d7222e80..79c30161 100644 --- a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js +++ b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js @@ -33,12 +33,23 @@ function storedPixelDataToCanvasImageDataPseudocolorLUT (image, grayscaleLut, co clut = colorLut; } + const grayscalePixelByDefault = () => { + return pixelData[storedPixelDataIndex++]; + }; + var grayscalePixelByLuminosityMethod = () => { + var r = pixelData[storedPixelDataIndex++]; + var g = pixelData[storedPixelDataIndex++]; + var b = pixelData[storedPixelDataIndex++]; + storedPixelDataIndex++;//skip alpha channel + return Math.round(0.299 * r + 0.587 * g + 0.114 * b); + }; + var grayscalePixel = image.color + ? grayscalePixelByLuminosityMethod + : grayscalePixelByDefault; + if (minPixelValue < 0) { while (storedPixelDataIndex < numPixels) { - grayscale = grayscaleLut[pixelData[storedPixelDataIndex] - minPixelValue]; - //Incrementing by 1 pixel only works with original black and white images. RGBA comes in groups of 4 pixels - //(same for the else condition below) - storedPixelDataIndex += (image.color ? 4 : 1); + grayscale = grayscaleLut[grayscalePixel() - minPixelValue]; rgba = clut[grayscale]; canvasImageDataData[canvasImageDataIndex++] = rgba[0]; canvasImageDataData[canvasImageDataIndex++] = rgba[1]; @@ -47,8 +58,7 @@ function storedPixelDataToCanvasImageDataPseudocolorLUT (image, grayscaleLut, co } } else { while (storedPixelDataIndex < numPixels) { - grayscale = grayscaleLut[pixelData[storedPixelDataIndex]]; - storedPixelDataIndex += (image.color ? 4 : 1); + grayscale = grayscaleLut[grayscalePixel()]; rgba = clut[grayscale]; canvasImageDataData[canvasImageDataIndex++] = rgba[0]; canvasImageDataData[canvasImageDataIndex++] = rgba[1]; From 4728410bd15ca3b6c8c80a06de793d5548e7cfb4 Mon Sep 17 00:00:00 2001 From: Alexy Date: Fri, 12 Nov 2021 16:16:47 -0500 Subject: [PATCH 3/3] Some refactoring --- .../storedPixelDataToCanvasImageDataPseudocolorLUT.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js index 79c30161..a0cdb2fe 100644 --- a/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js +++ b/src/internal/storedPixelDataToCanvasImageDataPseudocolorLUT.js @@ -36,14 +36,14 @@ function storedPixelDataToCanvasImageDataPseudocolorLUT (image, grayscaleLut, co const grayscalePixelByDefault = () => { return pixelData[storedPixelDataIndex++]; }; - var grayscalePixelByLuminosityMethod = () => { - var r = pixelData[storedPixelDataIndex++]; - var g = pixelData[storedPixelDataIndex++]; - var b = pixelData[storedPixelDataIndex++]; + const grayscalePixelByLuminosityMethod = () => { + const r = pixelData[storedPixelDataIndex++]; + const g = pixelData[storedPixelDataIndex++]; + const b = pixelData[storedPixelDataIndex++]; storedPixelDataIndex++;//skip alpha channel return Math.round(0.299 * r + 0.587 * g + 0.114 * b); }; - var grayscalePixel = image.color + const grayscalePixel = image.color ? grayscalePixelByLuminosityMethod : grayscalePixelByDefault;