Search code examples
c#dicomfo-dicom

Using Grayscale Softcopy Presentation State with fo-dicom


I would like to add markings to a DICOM image (for example, drawings or textual comments) using Grayscale Softcopy Presentation State IOD.

I have created a DICOM image object like this:

Bitmap bitmap = new Bitmap(path);
            bitmap = GetValidImage(bitmap);
            int rows, columns;
            byte[] pixels = GetPixels(bitmap, out rows, out columns);

            MemoryByteBuffer buffer = new MemoryByteBuffer(pixels);
            DicomDataset imageDataset = new DicomDataset();
            FillDataset(imageDataset);
            DicomDataset annotationdataset = new DicomDataset();
            FillAnnotation(imageDataset, annotationdataset);

            imageDataset.Add(DicomTag.PhotometricInterpretation, PhotometricInterpretation.Rgb.Value);
            imageDataset.Add(DicomTag.Rows, (ushort)rows);
            imageDataset.Add(DicomTag.Columns, (ushort)columns);
            imageDataset.Add(DicomTag.BitsAllocated, (ushort)8);

            DicomPixelData pixelData = DicomPixelData.Create(imageDataset, true);
            pixelData.BitsStored = 8;
            pixelData.SamplesPerPixel = 3;
            pixelData.HighBit = 7;
            pixelData.PixelRepresentation = 0;
            pixelData.PlanarConfiguration = 0;
            pixelData.AddFrame(buffer);



            DicomFile dicomfile = new DicomFile(imageDataset);
            if (File.Exists("test.dcm"))
                File.Delete("test.dcm");
            dicomfile.Save("test.dcm");

Then I have created a Grayscale Softcopy Presentation State object like this:

private static void FillAnnotation(DicomDataset imageDataset, DicomDataset annotationDataset)
        {
            //type 1 attributes.
            annotationDataset.Add(DicomTag.SOPClassUID, DicomUID.GrayscaleSoftcopyPresentationStateStorage);
            annotationDataset.Add(DicomTag.StudyInstanceUID, _studyInstanceUid);
            annotationDataset.Add(DicomTag.SeriesInstanceUID, _seriesInstanceUID);
            annotationDataset.Add(DicomTag.SOPInstanceUID, GenerateUid());

            //type 2 attributes
            annotationDataset.Add(DicomTag.PatientID, _patientId);
            annotationDataset.Add(DicomTag.PatientName, _patientName);
            annotationDataset.Add(DicomTag.PatientBirthDate, _patientBirthDate);
            annotationDataset.Add(DicomTag.PatientSex, _patientSex);
            annotationDataset.Add(DicomTag.StudyDate, _studyDateTime);
            annotationDataset.Add(DicomTag.StudyTime, _studyDateTime);
            annotationDataset.Add(DicomTag.AccessionNumber, _accessionNumber);
            annotationDataset.Add(DicomTag.ReferringPhysicianName, _referringPhysicianName);
            annotationDataset.Add(DicomTag.StudyID, _studyID);
            annotationDataset.Add(DicomTag.SeriesNumber, _seriesNumber);
            //annotationDataset.Add(DicomTag.ModalitiesInStudy, "CR");
            annotationDataset.Add(DicomTag.Modality, _modality);
            annotationDataset.Add(DicomTag.Manufacturer, _manufacturer);
            annotationDataset.Add(DicomTag.PresentationCreationDate, _presentationCreationDateTime);
            annotationDataset.Add(DicomTag.PresentationCreationTime, _presentationCreationDateTime);

            DicomDataset serie = new DicomDataset();
            serie.Add(DicomTag.SeriesInstanceUID, _seriesInstanceUID);
            serie.Add(DicomTag.ReferencedImageSequence, imageDataset);

            annotationDataset.Add(DicomTag.ReferencedSeriesSequence, serie);


            DicomDataset displayedArea = new DicomDataset();
            displayedArea.Add(DicomTag.DisplayedAreaTopLeftHandCorner, "50\\50");
            displayedArea.Add(DicomTag.DisplayedAreaBottomRightHandCorner, "100\\100");
            displayedArea.Add(DicomTag.PresentationSizeMode, "SCALE TO FIT");

            annotationDataset.Add(DicomTag.DisplayedAreaSelectionSequence, displayedArea);

            annotationDataset.Add(DicomTag.ICCProfile, Byte.Parse("00000001"));

        }

I do not really understand how this two object are connected with each other?


Solution

  • The Presentation State is connected to the Image by ReferencedSeriesSequence. I see you are filling that attribute in, so this should be ok. Now you also need to save the Presentation State as a separate DICOM file, exactly the way you save the image. In order to check the result, just use fo-dicom to open the PS file and read from it, e.g.:

     var file = DicomFile.Open(your_path, readOption: FileReadOption.ReadAll);
     var dicomDataset = file.Dataset;
     var isItem = dicomDataset.Contains(DicomTag.SOPInstanceUID);
    ....
    

    You can easily check dicomDataset above in the debugger and see that all the attributes you put in the PS should be there.

    I've done the same, but the problem is I couldn't find a free DICOM viewer that visualizes both the image and the Presentation State properly. Still looking for one ....