UNPKG

js-dicom

Version:

this is js-dicom

40 lines (39 loc) 1.87 kB
/** * This function transforms stored pixel values into a canvas image data buffer * by using a LUT. This is the most performance sensitive code in cornerstone and * we use a special trick to make this go as fast as possible. Specifically we * use the alpha channel only to control the luminance rather than the red, green and * blue channels which makes it over 3x faster. The canvasImageDataData buffer needs * to be previously filled with white pixels. * * NOTE: Attribution would be appreciated if you use this technique! * * @param pixelData the pixel data * @param lut the lut * @param canvasImageDataData a canvasImgageData.data buffer filled with white pixels */ function storedPixelDataToCanvasImageData(image, lut, canvasImageDataData) { var pixelData = image.getPixelData(); var minPixelValue = image.minPixelValue; var canvasImageDataIndex = 3; var storedPixelDataIndex = 0; var localNumPixels = pixelData.length; var localPixelData = pixelData; var localLut = lut; var localCanvasImageDataData = canvasImageDataData; // NOTE: As of Nov 2014, most javascript engines have lower performance when indexing negative indexes. // We have a special code path for this case that improves performance. Thanks to @jpambrun for this enhancement if(minPixelValue < 0){ while(storedPixelDataIndex < localNumPixels) { localCanvasImageDataData[canvasImageDataIndex] = localLut[localPixelData[storedPixelDataIndex++] + (-minPixelValue)]; // alpha canvasImageDataIndex += 4; } }else{ while(storedPixelDataIndex < localNumPixels) { localCanvasImageDataData[canvasImageDataIndex] = localLut[localPixelData[storedPixelDataIndex++]]; // alpha canvasImageDataIndex += 4; } } } export default storedPixelDataToCanvasImageData;