public static void CreateSeriesGraphicsForSeg(IPresentationImage presentationImage, Seg seg, SegmentationDocument segmentationDocument, IDicomMessageSopDataSource dicomMessageSopDataSourceSop) { Platform.CheckForNullReference(presentationImage, "presentationImage"); Platform.CheckForNullReference(seg, "seg"); Platform.CheckForNullReference(segmentationDocument, "segmentationDocument"); SegmentImageData segImageData = seg.SegmentImageData; if (segImageData == null) { Platform.Log(LogLevel.Error, "Cannot create segmentation graphics when no segmentation imaging data is provided"); return; } var imageSopProvider = presentationImage as IImageSopProvider; if (imageSopProvider == null) { Platform.Log(LogLevel.Error, "Failed to populate SegFrameGraphics collection. Image is not an ImageSopProvider"); return; } DicomPixelData segmentationPixelData = DicomPixelData.CreateFrom(dicomMessageSopDataSourceSop.SourceMessage); var rawPixelData = (byte[])dicomMessageSopDataSourceSop.SourceMessage.DataSet.GetAttribute(DicomTags.PixelData).Values; var pixelDataGetter = new Func <int, byte[]>(frameIndex => { if (segImageData.BitsStored == 1) { // Do unpacking int frameLength = segImageData.Rows * segImageData.Columns; var overlayData = new OverlayData(frameIndex * frameLength, segImageData.Rows, segImageData.Columns, false, rawPixelData); return(overlayData.Unpack()); } if (segImageData.BitsStored == 8) { return(segmentationPixelData.GetFrame(frameIndex)); } throw new InvalidDataException( "Segmentation objects need to have BitsStored as either 1 or 8"); }); // NOTE: SegmentFrameData was already sorted foreach (SegmentFrameData segmentFrameData in segImageData.SegmentFrameData) { IPresentationImage segPresentationImage = null; // Get the presentation image if we have an image reference string referencedSopInstanceUid = segmentFrameData.ReferencedSopInstanceUid; int referencedImageFrameNumber = segmentFrameData.ReferencedFrameNumber ?? 1; if (!string.IsNullOrEmpty(referencedSopInstanceUid)) { segPresentationImage = presentationImage.ParentDisplaySet.PresentationImages.OfType <IImageSopProvider>() .FirstOrDefault(curImageSopProvider => curImageSopProvider != null && curImageSopProvider.ImageSop.SopInstanceUid == referencedSopInstanceUid && curImageSopProvider.Frame.FrameNumber == referencedImageFrameNumber) as IPresentationImage; } // Location defaults to 0, 0 unless determined otherwise from image position and image orientation var segLocation = new PointF(); // Get the presentation image from the image position and orientation if (segmentFrameData.ImagePositionPatient != null && segmentFrameData.ImagePositionPatient.Count() > 2 && segmentFrameData.ImageOrientationPatient != null && segmentFrameData.ImageOrientationPatient.Count() > 5) { var imagePositionPatient = new ImagePositionPatient( segmentFrameData.ImagePositionPatient[0], segmentFrameData.ImagePositionPatient[1], segmentFrameData.ImagePositionPatient[2]); var imageOrientationPatient = new ImageOrientationPatient( segmentFrameData.ImageOrientationPatient[0], segmentFrameData.ImageOrientationPatient[1], segmentFrameData.ImageOrientationPatient[2], segmentFrameData.ImageOrientationPatient[3], segmentFrameData.ImageOrientationPatient[4], segmentFrameData.ImageOrientationPatient[5]); IDisplaySet displaySet = presentationImage.ParentDisplaySet; if (segPresentationImage == null) { segPresentationImage = PresentationImageFromPositionOrientation( imagePositionPatient, imageOrientationPatient, displaySet, segImageData.FrameOfReferenceUid); } var imageSop = segPresentationImage as IImageSopProvider; if (imageSop != null) { Vector3D segImageLocation = imageSop.Frame.ImagePlaneHelper.ConvertToImagePlane(new Vector3D( (float)imagePositionPatient.X, (float)imagePositionPatient.Y, (float)imagePositionPatient.Z)); PointF?segPixelLocation = imageSop.Frame.ImagePlaneHelper.ConvertToImage( new PointF(segImageLocation.X, segImageLocation.Y)); if (segPixelLocation.HasValue) { segLocation = segPixelLocation.Value; } } } if (segPresentationImage != null) { SegFrameImageGraphic newGraphic = AddSegFrameImageGraphicToPresentationImage( segPresentationImage, segImageData.Rows, segImageData.Columns, segLocation.X, segLocation.Y, seg.Color, referencedImageFrameNumber, seg.Label, seg.Description, pixelDataGetter.Invoke(segmentFrameData.FrameNumber - 1), new SegmentationDocumentReference(segmentationDocument, seg.SegmentationNumber)); if (newGraphic != null && segPresentationImage == segPresentationImage.ParentDisplaySet.ImageBox.TopLeftPresentationImage) { newGraphic.Draw(); } } else { Platform.Log(LogLevel.Error, "Failed to find Presentation Image to display a segment on"); } } }