feat(io): have a fast 2D file writer
Description
We need a fast but compressed image format to be able to store 2D Image efficiently, that will be especially useful for a recorder. Currently, we often use BMP
, TIFF
or PNG
. In short, BMP
is too big and too slow, PNG
is small but far too slow, and tiff is somewhat suitable. However, it still uses the CPU and cannot be directly streamed into a DICOM file, which is can be a requirement.
DICOM support natively JPEG
and JPEG2000 transfer-syntax, so we can easily create a DICOM from them, without transcoding. To fulfil the speed requirement, we can use the GPU and use nvJPEG and nvJPEG2000. JPEG
is not really lossless (although there is a lossless variant JPEG LS
), but can be very close too, depending on the settings. Look at this for a quick introduction. It is intensively used by NVidia for DALI
On the contrary JPEG2000
can be lossless but is slower. We indeed need to investigate if the GPU accelerated version nvJPEG2000 is fast enough.
Proposal
- Find if nvJPEG is fast enough and if the loss of quality acceptable (most if not all US device I encountered stores anyway lossy jpeg)
- Find if nvJPEG2000 is fast enough.
- Implement an Image writer that write a jpeg/jpeg2000 using nvJPEG or nvJPEG2000
Functional specifications
This will be quite simple:
graph TD
A[US IMAGE] -->|FastWriter| B(JPEG or JPEG2000)
B -->|DicomWriter| C(Enhenced US Volume)
Of course, depending on the use case, a DICOM volume can be composed of more than one JPEG
file.
Technical specifications
Will use nvJPEG or nvJPEG2000. This will require CUDA (for nvJPEG) and nvJPEG200 library if JPEG2000 is required. The result file should be lossless or with the best possible quality.
We could implement it as a Sight library, since I can detect at compile time if CUDA and nvJPEG2000 libraries are available and at runtime if there is a CUDA capable GPU.
At build time:
- IF CUDA available at compile time -> build nvJPEG support
- IF nvJPEG2000 available at compile time -> build nvJPEG + nvJPEG2000 support
- IF NO CUDA -> build only libjpeg / libtiff fallback code.
At runtime:
- IF nvJPEG / nvJPEG2000 support has been compiled in and CUDA GPU present:
- Allow to save to jpg, jp2 using nvJPEG and tiff
- ELSE:
- Allow to save to jpg, but use libjpeg-turbo and tiff
Test plan
- Unit tests with JPEG/JPEG2000 encoding (this will not run on CI since there is no GPU on CI hosts...)
- Compute a PSNR for lossy codec and ask IA people if it's good enough. We may compare the result of an IA trained with original lossless image and one with lossy JPG, but I'm quite sure most images we have, are anyway lossy compressed from the beginning.
- Once the DICOM writer is ready, see if the files can be integrated within a DICOM file.