Skip to content

Incorrect decoding of RLE-compressed DICOM (TransferSyntaxUID 1.2.840.10008.1.2.5) - output image becomes noise #1976

@Anastasia-Kiseleva12

Description

@Anastasia-Kiseleva12

Magick.NET version

Magick.NET-Q16-AnyCPU 14.10.3

Environment (Operating system, version and so on)

Windows 10 x64 .NET Framework 4.8 Running inside a WPF desktop application

Description

I am migrating from an older ImageMagick CLI-based implementation to Magick.NET.

During testing i discovered that some DICOM files that were previously decoded correctly by the old ImageMagick CLI now produce corrupted output when read using Magick.NET.

The issue appears specifically with RLE-compressed DICOM files.

The resulting PNG image appears as random grayscale noise even though the image dimensions and metadata are correct.

The problem reproduces even with a minimal Read(stream) -> Write(Png) pipeline, so it does not appear to be caused by any post-processing in my code.

Steps to Reproduce

  1. Open the attached RLE-compressed DICOM file.
  2. Run the following code:
using System;
using System.IO;
using ImageMagick;
using MagickNetFormat = ImageMagick.MagickFormat;

public static class Repro
{
    public static Stream Convert(Stream inStream, bool onlyFirstPage = true)
    {
        if (inStream is null)
            throw new ArgumentNullException(nameof(inStream));

        if (inStream.CanSeek)
            inStream.Seek(0, SeekOrigin.Begin);

        var read = new MagickReadSettings
        {
            Format = MagickNetFormat.Dcm
        };

        if (onlyFirstPage)
        {
            read.FrameIndex = 0;
            read.FrameCount = 1;
        }

        using (var image = new MagickImage(inStream, read))
        {
            var outStream = new MemoryStream();
            image.Write(outStream, MagickNetFormat.Png);
            outStream.Seek(0, SeekOrigin.Begin);
            return outStream;
        }
    }
}

Save the returned stream as a PNG file and open it.

Result:
The PNG output contains random noise / corrupted pixel data.

Expected result:
The PNG should contain the correct grayscale radiographic image.

Additional notes:

The same source file is decoded correctly by the older ImageMagick CLI-based implementation that I used previously.

The issue appears only for DICOM files with TransferSyntaxUID = 1.2.840.10008.1.2.5 (RLE Lossless).

Uncompressed DICOM (1.2.840.10008.1.2.1) and JPEG-compressed DICOM (1.2.840.10008.1.2.4.70) work correctly.

Example metadata of problematic files:

TransferSyntaxUID = 1.2.840.10008.1.2.5
Compression = RLE
Depth = 16
ColorSpace = Gray
PhotometricInterpretation = MONOCHROME2
Manufacturer = LEAD Technologies

Images

images.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions