Search code examples
c#dicomfo-dicom

Add jpg image as jpg image to DICOM file


Good day,

I am reading a jpg image and trying to storing it in the DICOM file as jpg. I want as little manipulation as possible to prevent any loss or ICC profile changes.

I've tried:

...
data.Add(DicomTag.TransferSyntaxUID, DicomUID.JPEGLSLossless);

data.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb);
data.Add(DicomTag.SamplesPerPixel, "3");
data.Add(DicomTag.PlanarConfiguration, "0");
data.Add(DicomTag.BitsAllocated, (ushort)8);
data.Add(DicomTag.BitsStored, (ushort)8);
data.Add(DicomTag.HighBit, (ushort)7);
data.Add(DicomTag.PixelRepresentation, "0");
data.Add(DicomTag.BurnedInAnnotation, "NO");
data.Add(DicomTag.LossyImageCompression, "01");
data.Add(DicomTag.LossyImageCompressionRatio, "10");
data.Add(DicomTag.LossyImageCompressionMethod, "ISO_10918_1");
...

DicomPixelData pixelData = DicomPixelData.Create(data, true);

using (System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(imageFilename))
{
    byte[] pixels = GetPixels(bitmap);
    MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
    pixelData.AddFrame(buffer);
}

and

using (Image image = Image.FromFile(imageFilename))
{
    byte[] pixels = ImageToByteArray(image);
    MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
    pixelData.AddFrame(buffer);
}

It appears to be storing the image as BMP since the size of the DICOM file is ballooning beyond belief.

I have tried different combinations of DicomTag.TransferSyntaxUID:

data.Add(DicomTag.TransferSyntaxUID, DicomUID.JPEGLSLossless);
data.Add(DicomTag.TransferSyntaxUID, DicomUID.JPEGBaseline1);
data.Add(DicomTag.TransferSyntaxUID, DicomUID.JPEGLosslessNonHierarchical14);

Thoughts?

(note: this was posed on the fo-dicom users group as well)


Solution

  • We have found the answer:

    DicomDataset data = new DicomDataset() { };
    

    changed to:

    DicomDataset data = new DicomDataset(DicomTransferSyntax.JPEGProcess1) { };
    

    This is based on this article:

    https://github.com/fo-dicom/fo-dicom/issues/553