As disk space usage has always been a priority when designing/implementing DICOM applications, “What is the best practice for writing DICOM files?” is becoming a very common question asked by our developers.

A sensible idea is to write uncompressed images in lossless-compressed format in order to reduce disk usage, and leave compressed images “as is”.

So check against the “original” transfer syntax of the image (either read from disk or received over network), if it’s one of those uncompressed formats then lossless compress it when you write on disk. And if the input image is already compressed then just leave/store it as is.

The code shows how to write received images on to local disk.

DicomObjects.NET version

 // Event fires when a DICOM Instance is received
 private void Server_InstanceReceived(object Sender, InstanceReceivedArgs e)
	if (e.Instance.OriginalTS == DicomObjects.DicomUIDs.TransferSyntaxes.ImplicitVRLittleEndian
	 || e.Instance.OriginalTS == DicomObjects.DicomUIDs.TransferSyntaxes.ExplicitVRLittleEndian
	 || e.Instance.OriginalTS == DicomObjects.DicomUIDs.TransferSyntaxes.ExplicitVRBigEndian)
	  e.Instance.Write(filePath, DicomObjects.DicomUIDs.TransferSyntaxes.JPEGLossless);
	e.Instance.Write(filePath, e.Instance.OriginalTS);

DicomObjects.COM version

Private Sub  Viewer_ImageReceived(ByVal ReceivedImage As  DicomObjects.DicomImage, ByVal Association As Long,  
                                 isAdded As Boolean,Status As Long)
 If ReceivedImage.ReceivedSyntax = "1.2.840.10008.1.2" 
		Or ReceivedImage.ReceivedSyntax = "1.2.840.10008.1.2.1" 
		Or ReceivedImage.ReceivedSyntax = "1.2.840.10008.1.2.2" Then
 ' if received as uncompressed, then losslessly compress it
     ReceivedImage.WriteFile filePath, True, "1.2.840.10008."
 Else 'otherwise save the image "As Is"
     ReceivedImage.WriteFile filepage, True, ReceivedImage.ReceivedSyntax
 End If
End Sub

Now we have another question here, what if input image is compressed and we want to make it even smaller when writing it out? In DicomObjects, you can actually use a different transfer syntax for “higher” compression, together with a “Quality Factor” to control how you want that image to be compressed. This happens transparently in DicomObjects, it reads in the compressed image, decompresses it and then re-compress it with higher compression and the quality factor the user provided.

Please note, if image’s original Transfer Syntax is same as the transfer syntax for output, Quality Factor will be ignored and the output file size will be the same as the input file size. If you really wish to force a reduction in file size and quality, then use DecompressAll before writing the file using your new quality factor.