cornerstonejs / cornerstone

JavaScript library to display interactive medical images including but not limited to DICOM
https://docs.cornerstonejs.org/
MIT License
2.07k stars 598 forks source link

reset Colormap on an image leaves traces of alpha channel #317

Open Zaid-Safadi opened 6 years ago

Zaid-Safadi commented 6 years ago

How to reproduce:

  1. Apply colormap to an image by setting the viewport as in the "falseColorMapping" example:

    viewport.colormap = colormap;
    cornerstone.setViewport(element, viewport);
    cornerstone.updateImage(element, true);

    autumn

  2. Try to reset the colormap by setting the viewport.colormap to empty string *notice that setting to null won't actually reset the colormap since cornerstone copies from existing viewport when a property is null (possible issue related to #304)

viewport.colormap = '';
cornerstone.setViewport(element, viewport);
cornerstone.updateImage(element, true);

*please ignore the colorbar as this doesn't reflect the reset in the example reset

Zaid-Safadi commented 6 years ago

For debugging purposes, setting the "useAlphaChannel" in the renderGrayscaleImage.getRenderCanvas to false would fix the issue since this causes cornerstone to explicitly reset the alpha channel.

dannyrb commented 5 years ago

@Zaid-Safadi, do you have a proposed fix? Would it help if we added an explicit method to reset the alpha channel?

Zaid-Safadi commented 5 years ago

@dannyrb this is what I use externally to fix this:

function resetRenderCanvase(element) { 
    const enabledElement = cornerstone.getEnabledElement(element); 

    enabledElement.renderingTools.colormapId = undefined; 
    enabledElement.renderingTools.colorLut = undefined; 

    const renderCanvas = enabledElement.renderingTools.renderCanvas; 
    const canvasContext = renderCanvas.getContext('2d'); 

    // NOTE - we need to fill the render canvas with white pixels since we 
    // control the luminance using the alpha channel to improve rendering performance. 
    canvasContext.fillStyle = 'white'; 
    canvasContext.fillRect(0, 0, renderCanvas.width, renderCanvas.height); 

    const renderCanvasData = canvasContext.getImageData(0, 0, renderCanvas.width, renderCanvas.height); 

    enabledElement.renderingTools.renderCanvasContext = canvasContext; 
    enabledElement.renderingTools.renderCanvasData = renderCanvasData; 
}

Which is really taken from cornerstone here: https://github.com/cornerstonejs/cornerstone/blob/a1f0caf7e745bda4fd2b2687adc8746546a9a7ca/src/rendering/initializeRenderCanvas.js#L9-L27

So technically we can expose this method externally but maybe adding a method called "resetColomap" might be more suitable?