Exemplo n.º 1
0
        public static List <OverlayPlaneGraphic> CreateOverlayPlaneGraphics(Frame frame, OverlayPlaneModuleIod overlaysFromPresentationState)
        {
            ISopDataSource        dataSource  = frame.ParentImageSop.DataSource;
            OverlayPlaneModuleIod overlaysIod = new OverlayPlaneModuleIod(dataSource);

            List <OverlayPlaneGraphic> overlayPlaneGraphics = new List <OverlayPlaneGraphic>();

            bool failedOverlays = false;

            foreach (var overlayPlane in overlaysIod)
            {
                // DICOM 2009 PS 3.3 Section C.9.3.1.1 specifies the rule: NumberOfFramesInOverlay+ImageFrameOrigin-1 must be <= NumberOfFrames
                if (!overlayPlane.IsValidMultiFrameOverlay(frame.ParentImageSop.NumberOfFrames))
                {
                    failedOverlays = true;
                    Platform.Log(LogLevel.Warn, new DicomOverlayDeserializationException(overlayPlane.Group, OverlayPlaneSource.Image), "Encoding error encountered while reading overlay from image headers.");
                    continue;
                }

                try
                {
                    byte[] overlayData = dataSource.GetFrameData(frame.FrameNumber).GetNormalizedOverlayData(overlayPlane.Index + 1);
                    overlayPlaneGraphics.Add(new OverlayPlaneGraphic(overlayPlane, overlayData, OverlayPlaneSource.Image));

                    // if overlay data is null, the data source failed to retrieve the overlay data for some reason, so we also treat it as an encoding error
                    // this is different from if the overlay data is zero-length, which indicates that the retrieval succeeded, but that the overlay data for the frame is empty
                    if (overlayData == null)
                    {
                        throw new NullReferenceException();
                    }
                }
                catch (Exception ex)
                {
                    failedOverlays = true;
                    Platform.Log(LogLevel.Warn, new DicomOverlayDeserializationException(overlayPlane.Group, OverlayPlaneSource.Image, ex), "Failed to load overlay from the image header.");
                }
            }

            if (overlaysFromPresentationState != null)
            {
                foreach (var overlayPlane in overlaysFromPresentationState)
                {
                    // if overlay data is missing, treat as an encoding error
                    if (!overlayPlane.HasOverlayData)
                    {
                        failedOverlays = true;
                        Platform.Log(LogLevel.Warn, new DicomOverlayDeserializationException(overlayPlane.Group, OverlayPlaneSource.PresentationState), "Encoding error encountered while reading overlay from softcopy presentation state.");
                        continue;
                    }

                    try
                    {
                        byte[] overlayData;

                        // try to compute the offset in the OverlayData bit stream where we can find the overlay frame that applies to this image frame
                        int overlayFrame, bitOffset;
                        if (overlayPlane.TryGetRelevantOverlayFrame(frame.FrameNumber, frame.ParentImageSop.NumberOfFrames, out overlayFrame) &&
                            overlayPlane.TryComputeOverlayDataBitOffset(overlayFrame, out bitOffset))
                        {
                            // offset found - unpack only that overlay frame
                            var od = new OverlayData(bitOffset,
                                                     overlayPlane.OverlayRows,
                                                     overlayPlane.OverlayColumns,
                                                     overlayPlane.IsBigEndianOW,
                                                     overlayPlane.OverlayData);

                            overlayData = od.Unpack();
                        }
                        else
                        {
                            // no relevant overlay frame found - i.e. the overlay for this image frame is blank
                            overlayData = new byte[0];
                        }

                        overlayPlaneGraphics.Add(new OverlayPlaneGraphic(overlayPlane, overlayData, OverlayPlaneSource.PresentationState));
                    }
                    catch (Exception ex)
                    {
                        failedOverlays = true;
                        Platform.Log(LogLevel.Warn, new DicomOverlayDeserializationException(overlayPlane.Group, OverlayPlaneSource.PresentationState, ex), "Failed to load overlay from softcopy presentation state.");
                    }
                }
            }

            if (failedOverlays)
            {
                // add an error graphic if any overlays are not being displayed due to deserialization errors.
                overlayPlaneGraphics.Add(new ErrorOverlayPlaneGraphic(SR.MessageErrorDisplayingOverlays));
            }

            return(overlayPlaneGraphics);
        }
Exemplo n.º 2
0
 protected override ISopFrameData GetFrameData(int frameNumber)
 {
     return(_sopDataSource.GetFrameData(frameNumber));
 }