DICOM Basics using Java - Transfer Syntax and Compression
Introduction
This is part of my series of articles on the DICOM standard. In this tutorial, we'll explore DICOM transfer syntaxes, which define how DICOM data is encoded (value representation, byte ordering, and compression). Understanding transfer syntaxes is essential for interoperability and storage optimization.
Choosing the right transfer syntax affects storage size, transmission speed, and viewer compatibility.
Prerequisites
Before you begin, ensure you have the following:
- Java JDK installed (Java 8 or later)
- PixelMed Java DICOM Toolkit
- Understanding of basic DICOM file structure
- You can find all the code demonstrated in this tutorial on GitHub here
“Time heals what reason cannot.” ~ Seneca
Common Transfer Syntaxes
Uncompressed:
| UID | Name | Notes |
|---|---|---|
| 1.2.840.10008.1.2 | Implicit VR Little Endian | Default, most compatible |
| 1.2.840.10008.1.2.1 | Explicit VR Little Endian | Recommended |
| 1.2.840.10008.1.2.2 | Explicit VR Big Endian | Retired |
Lossless Compression:
| UID | Name | Ratio |
|---|---|---|
| 1.2.840.10008.1.2.5 | RLE Lossless | 2:1 - 3:1 |
| 1.2.840.10008.1.2.4.57 | JPEG Lossless (Process 14) | 2:1 - 3:1 |
| 1.2.840.10008.1.2.4.70 | JPEG Lossless SV1 (Recommended) | 2:1 - 3:1 |
| 1.2.840.10008.1.2.4.90 | JPEG 2000 Lossless Only | 2:1 - 3:1 |
| 1.2.840.10008.1.2.4.80 | JPEG-LS Lossless | 2:1 - 3:1 |
Lossy Compression:
| UID | Name | Ratio |
|---|---|---|
| 1.2.840.10008.1.2.4.50 | JPEG Baseline (8-bit) | 10:1 - 20:1 |
| 1.2.840.10008.1.2.4.51 | JPEG Extended (12-bit) | 10:1 - 20:1 |
| 1.2.840.10008.1.2.4.91 | JPEG 2000 (lossy) | 10:1 - 50:1 |
Transfer Syntax Conversion
package com.saravanansubramanian.dicom.pixelmedtutorial;
import com.pixelmed.dicom.AttributeList;
import com.pixelmed.dicom.FileMetaInformation;
import com.pixelmed.dicom.TagFromName;
import com.pixelmed.dicom.TransferSyntax;
public class TransferSyntaxConversionDemo {
public static void main(String[] args) {
try {
System.out.println("=== DICOM Transfer Syntax Conversion ===\n");
String inputFile = "C:\\path\\to\\input.dcm";
String outputFile = "C:\\path\\to\\output.dcm";
// Read the DICOM file
AttributeList list = new AttributeList();
list.read(inputFile);
// Get current transfer syntax
String currentTS = list.get(TagFromName.TransferSyntaxUID)
.getSingleStringValueOrNull();
System.out.println("Current: " + currentTS);
System.out.println("Description: " + getDescription(currentTS));
// Convert to different transfer syntax
String targetTS = TransferSyntax.ExplicitVRLittleEndian;
System.out.println("\nConverting to: " + targetTS);
// Remove old file meta information
list.removeMetaInformationHeaderAttributes();
// Add new file meta information with target transfer syntax
FileMetaInformation.addFileMetaInformation(
list, targetTS, "OurSourceAET");
// Write the file
list.write(outputFile);
System.out.println("Conversion complete: " + outputFile);
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
public static String getDescription(String tsUID) {
if (tsUID == null) return "Unknown";
switch (tsUID) {
case "1.2.840.10008.1.2":
return "Implicit VR Little Endian";
case "1.2.840.10008.1.2.1":
return "Explicit VR Little Endian";
case "1.2.840.10008.1.2.5":
return "RLE Lossless";
case "1.2.840.10008.1.2.4.50":
return "JPEG Baseline (Lossy, 8-bit)";
case "1.2.840.10008.1.2.4.70":
return "JPEG Lossless SV1 (Recommended)";
case "1.2.840.10008.1.2.4.90":
return "JPEG 2000 Lossless Only";
case "1.2.840.10008.1.2.4.91":
return "JPEG 2000 (Lossy)";
default:
return "Other";
}
}
}
Compression and Decompression
// Read DICOM file (PixelMed automatically decompresses)
AttributeList list = new AttributeList();
list.read(inputFile);
// Save with RLE compression (lossless)
saveWithTransferSyntax(list, TransferSyntax.RLE, "compressed_rle.dcm");
// Save with JPEG Lossless
saveWithTransferSyntax(list, TransferSyntax.JPEGLosslessSV1,
"compressed_jpeg_lossless.dcm");
// Save with JPEG 2000 Lossless
saveWithTransferSyntax(list, TransferSyntax.JPEG2000Lossless,
"compressed_j2k_lossless.dcm");
// Helper method
private static void saveWithTransferSyntax(AttributeList list,
String transferSyntax, String outputPath) throws Exception {
// Clone to avoid modifying original
AttributeList outputList = (AttributeList) list.clone();
// Remove existing file meta information
outputList.removeMetaInformationHeaderAttributes();
// Add new file meta with target transfer syntax
FileMetaInformation.addFileMetaInformation(
outputList, transferSyntax, "OurAET");
// IMPORTANT: In a real implementation, you must also transcode
// (compress/decompress) the pixel data to match the target
// Transfer Syntax. This example only updates the metadata header.
// Write the file
outputList.write(outputPath);
}
Note: This simplified example only updates the Transfer Syntax UID metadata. In a production implementation, you must also transcode the pixel data to match the target Transfer Syntax, otherwise the resulting DICOM file will be invalid.
The Theory Behind Transfer Syntaxes
Transfer syntaxes define the encoding rules for DICOM data. Understanding the theoretical foundations helps in making informed decisions about compression, interoperability, and system design.
The Three Components of Transfer Syntax
Every transfer syntax specifies three independent parameters:
- Value Representation (VR) Encoding: Implicit (VR must be looked up in dictionary) vs. Explicit (VR embedded in data stream)
- Byte Ordering: Little Endian (LSB first, x86 native) vs. Big Endian (MSB first)
- Pixel Data Compression: Uncompressed, RLE, JPEG variants, JPEG 2000, etc.
These are independent axes, though not all combinations exist as registered transfer syntaxes. For example, Implicit VR is always Little Endian in DICOM.
Information Theory and Medical Image Compression
Medical images have unique statistical properties that affect compression efficiency:
- High Bit Depth: 12-16 bits vs. consumer images' 8 bits, meaning more unique values and lower entropy reduction
- Spatial Correlation: Adjacent pixels tend to have similar values (smooth gradients in tissue), exploitable by predictive coding
- Modality-Specific Noise: CT quantum noise, MR thermal noise, US speckle all reduce compressibility
- Dynamic Range: Medical images often use only a portion of the available bit range, creating opportunities for bit-plane optimization
Why Lossless Compression Matters for Medicine
In consumer imaging, lossy compression is universally accepted because humans can't perceive the differences. Medical imaging is different:
- Diagnostic Threshold: A subtle finding might differ from normal by just a few pixel values
- Quantitative Imaging: SUV calculations in PET, Hounsfield units in CT require exact values
- Legal Evidence: Images may be used in litigation where authenticity matters
- Algorithm Input: CAD/AI systems may be sensitive to compression artifacts
However, research has shown that for certain modalities and clinical tasks, carefully controlled lossy compression may be acceptable. The key is validation: any lossy compression scheme must be validated for its specific clinical use case.
The JPEG 2000 Advantage
JPEG 2000 offers significant advantages over older JPEG for medical imaging:
- Native Support for 16-bit: No need for 12-bit extended JPEG workarounds
- Progressive Transmission: Low-resolution preview available before full image loads
- Region of Interest Coding: Encode diagnostically important regions at higher quality
- Unified Lossless/Lossy: Same algorithm scales from lossless to high compression
- No Blocking Artifacts: Wavelet transform avoids JPEG's characteristic 8x8 blocks
Lossless vs Lossy Compression
Lossless Compression:
- Original pixel values preserved exactly
- Required for diagnostic interpretation
- Typical ratio: 2:1 to 3:1
- Examples: RLE, JPEG Lossless, JPEG 2000 Lossless
Lossy Compression:
- Some pixel information is lost
- Acceptable for: teaching files, thumbnails, web display
- Typical ratio: 10:1 to 50:1
- Examples: JPEG Baseline, JPEG 2000 Lossy
Compression Algorithm Comparison
| Algorithm | Type | Speed | Compatibility | Best For |
|---|---|---|---|---|
| RLE | Lossless | Fast | Universal | Uniform images |
| JPEG Lossless SV1 | Lossless | Medium | Wide | General use |
| JPEG 2000 Lossless | Lossless | Slower | Growing | Modern systems |
| JPEG-LS | Lossless | Fast | Limited | Medical imaging |
| JPEG Baseline | Lossy | Fast | Universal | Web display |
| JPEG 2000 | Lossy | Slower | Growing | High compression |
Choosing the Right Transfer Syntax
| Use Case | Recommended Transfer Syntax |
|---|---|
| Maximum compatibility | Explicit VR Little Endian |
| Archival storage | JPEG Lossless SV1 or JPEG 2000 Lossless |
| Newer systems | JPEG 2000 Lossless |
| Web/mobile display | JPEG Baseline or JPEG 2000 (lossy) |
| Network transfer | Implicit VR Little Endian |
Best Practices
- NEVER apply lossy compression to diagnostic images
- Keep original uncompressed copies for legal compliance
- Use JPEG Lossless SV1 for maximum compatibility
- Use JPEG 2000 Lossless for newer systems
- Test viewer compatibility before deployment
- Consider storage vs bandwidth tradeoffs
Lossy Compression Attributes
When lossy compression is applied, these attributes should be updated:
| Tag | Name | Values |
|---|---|---|
| (0028,2110) | Lossy Image Compression | ”00” = No, “01” = Yes |
| (0028,2112) | Lossy Image Compression Ratio | e.g., “10” for 10:1 |
| (0028,2114) | Lossy Image Compression Method | Algorithm identifier |
Conclusion
DICOM transfer syntaxes provide flexibility in how medical images are encoded and compressed. Choosing the appropriate transfer syntax is critical for balancing storage efficiency, transmission speed, and image quality requirements.
Understanding the differences between lossless and lossy compression, and when each is appropriate, is essential for building compliant DICOM applications that maintain image quality for diagnostic purposes while optimizing storage and bandwidth. In the next tutorial in this series, I will cover DICOM character set handling for international text support. See you then!