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:

UIDNameNotes
1.2.840.10008.1.2Implicit VR Little EndianDefault, most compatible
1.2.840.10008.1.2.1Explicit VR Little EndianRecommended
1.2.840.10008.1.2.2Explicit VR Big EndianRetired

Lossless Compression:

UIDNameRatio
1.2.840.10008.1.2.5RLE Lossless2:1 - 3:1
1.2.840.10008.1.2.4.57JPEG Lossless (Process 14)2:1 - 3:1
1.2.840.10008.1.2.4.70JPEG Lossless SV1 (Recommended)2:1 - 3:1
1.2.840.10008.1.2.4.90JPEG 2000 Lossless Only2:1 - 3:1
1.2.840.10008.1.2.4.80JPEG-LS Lossless2:1 - 3:1

Lossy Compression:

UIDNameRatio
1.2.840.10008.1.2.4.50JPEG Baseline (8-bit)10:1 - 20:1
1.2.840.10008.1.2.4.51JPEG Extended (12-bit)10:1 - 20:1
1.2.840.10008.1.2.4.91JPEG 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:

  1. Value Representation (VR) Encoding: Implicit (VR must be looked up in dictionary) vs. Explicit (VR embedded in data stream)
  2. Byte Ordering: Little Endian (LSB first, x86 native) vs. Big Endian (MSB first)
  3. 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

AlgorithmTypeSpeedCompatibilityBest For
RLELosslessFastUniversalUniform images
JPEG Lossless SV1LosslessMediumWideGeneral use
JPEG 2000 LosslessLosslessSlowerGrowingModern systems
JPEG-LSLosslessFastLimitedMedical imaging
JPEG BaselineLossyFastUniversalWeb display
JPEG 2000LossySlowerGrowingHigh compression

Choosing the Right Transfer Syntax

Use CaseRecommended Transfer Syntax
Maximum compatibilityExplicit VR Little Endian
Archival storageJPEG Lossless SV1 or JPEG 2000 Lossless
Newer systemsJPEG 2000 Lossless
Web/mobile displayJPEG Baseline or JPEG 2000 (lossy)
Network transferImplicit 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:

TagNameValues
(0028,2110)Lossy Image Compression”00” = No, “01” = Yes
(0028,2112)Lossy Image Compression Ratioe.g., “10” for 10:1
(0028,2114)Lossy Image Compression MethodAlgorithm 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!