PNG generation - RubinLab/epad-ws GitHub Wiki

Dcm4chee and ImageIO

  • We use Dcm4Chee toolkit to read the header tags relevant to the image properties like pixelRepresentation, photometricInterpretation, rescaleSlope, rescaleIntercept, rescaleType, highBit, bitsAllocated, bitsStored, windowWidth (default -100), windowCenter (default -100), windowCenterWidthExplanation, pixelPaddingValue. Rescale properties are retrieved from ModalityLUTSequence if there is one.
  • Then we use Dcm4Chee toolkit to read the pixeldata as an image raster.
  • For creating the png, we go through all the pixel values, convert them to 16 bit grayscale using the pixelRepresentation, bitsStored, adjustment and datamask (which is (1<<bitsStored)-1)). Adjustment is calculated as 1 << (bitsStored - 1) for signed values and is the value to be added to pixel value to make sure all pixel values are zero or positive.
  • We put the adjustment value in the first two pixels of the first row in the blue channel for signed values.
  • Then for each pixel, we split the grayscale pixel value to two 8 bit values: a high part and a low part and put in red channel and green channel respectively. For example the dicom pixel value 26 is converted to grayscale value 32794 with high bits 128 and low 26. The grayscale value 32794 is returned when you ask for raw pixel value from the png. The actual dicom pixel value can be retrieved with the bitwise conversion ((1<<(bitsStored-pixelRepresentation))-1)&pngPixelValue)-(((pngPixelValue>>(bitsStored-pixelRepresentation))^(pixelRepresentation))<<(bitsStored-pixelRepresentation).
  • Finally we use ImageIO to write the png.