public void Apply(IPresentationImage image)
        {
            if (image == ReferenceImage)
            {
                return;
            }

            //Turn off scale to fit and start with scale=1, then adjust it.
            //We do this because images that have been "scaled to fit", but have not been shown yet,
            //have no client rectangle and their scale is often very small.  This is safer
            //and could produce a more accurate result.
            IImageSpatialTransform matchTransform = GetImageTransform(image);

            matchTransform.ScaleToFit = false;
            matchTransform.Scale      = 1;

            //get the displayed width (in mm) for the same size display rectangle in the image to be matched.
            float matchDisplayedWidth = GetDisplayedWidth(image, _referenceDisplayRectangle);
            float rescaleAmount       = matchDisplayedWidth / _referenceDisplayedWidth;

            matchTransform.Scale *= rescaleAmount;

            //ISpatialTransform transform = (ISpatialTransform)_operation.GetOriginator(image);
            matchTransform.TranslationX = this.SelectedSpatialTransformProvider.SpatialTransform.TranslationX;
            matchTransform.TranslationY = this.SelectedSpatialTransformProvider.SpatialTransform.TranslationY;

            IVoiLutLinear selectedLut = (IVoiLutLinear)this.SelectedVoiLutProvider.VoiLutManager.VoiLut;

            IVoiLutProvider provider = ((IVoiLutProvider)image);

            if (!(provider.VoiLutManager.VoiLut is IBasicVoiLutLinear))
            {
                BasicVoiLutLinear installLut = new BasicVoiLutLinear(selectedLut.WindowWidth, selectedLut.WindowCenter);
                provider.VoiLutManager.InstallVoiLut(installLut);
            }

            IBasicVoiLutLinear lut = (IBasicVoiLutLinear)provider.VoiLutManager.VoiLut;

            lut.WindowWidth  = selectedLut.WindowWidth;
            lut.WindowCenter = selectedLut.WindowCenter;
        }
示例#2
0
        public void Apply(IPresentationImage image)
        {
            if (image == ReferenceImage)
            {
                return;
            }

            //Turn off scale to fit and start with scale=1, then adjust it.
            //We do this because images that have been "scaled to fit", but have not been shown yet,
            //have no client rectangle and their scale is often very small.  This is safer
            //and could produce a more accurate result.
            IImageSpatialTransform matchTransform = GetImageTransform(image);

            matchTransform.ScaleToFit = false;
            matchTransform.Scale      = 1;

            //get the displayed width (in mm) for the same size display rectangle in the image to be matched.
            float matchDisplayedWidth = GetDisplayedWidth(image, _referenceDisplayRectangle);
            float rescaleAmount       = matchDisplayedWidth / _referenceDisplayedWidth;

            matchTransform.Scale *= rescaleAmount;
        }
        protected void DeserializeSpatialTransform(SpatialTransformModuleIod module, T image)
        {
            ISpatialTransform spatialTransform = image.SpatialTransform;

            if (spatialTransform is IImageSpatialTransform)
            {
                IImageSpatialTransform iist = (IImageSpatialTransform)spatialTransform;
                iist.ScaleToFit = false;
            }

            if (module.ImageHorizontalFlip == ImageHorizontalFlip.Y)
            {
                spatialTransform.FlipX      = false;
                spatialTransform.FlipY      = true;
                spatialTransform.RotationXY = (360 - module.ImageRotation) % 360;
            }
            else
            {
                spatialTransform.FlipX      = false;
                spatialTransform.FlipY      = false;
                spatialTransform.RotationXY = module.ImageRotation;
            }
        }
示例#4
0
        private static Bitmap DrawCompleteImageToBitmap(IPresentationImage image, float scale, float dpi)
        {
            IImageSpatialTransform transform = (IImageSpatialTransform)((ISpatialTransformProvider)image).SpatialTransform;
            object restoreMemento            = transform.CreateMemento();

            try
            {
                Rectangle imageRectangle = new Rectangle(new Point(0, 0), image.SceneSize);

                transform.Initialize();
                transform.ScaleToFit = false;
                transform.Scale      = scale;
                RectangleF displayRectangle = transform.ConvertToDestination(imageRectangle);
                int        width            = (int)Math.Round(displayRectangle.Width);
                int        height           = (int)Math.Round(displayRectangle.Height);

                transform.ScaleToFit = true;
                return(ImageDrawToBitmap(image, width, height, dpi));
            }
            finally
            {
                transform.SetMemento(restoreMemento);
            }
        }
        protected void DeserializeDisplayedArea(DisplayedAreaModuleIod dispAreaMod, out RectangleF displayedArea, T image)
        {
            ISpatialTransform spatialTransform = image.SpatialTransform;

            foreach (DisplayedAreaModuleIod.DisplayedAreaSelectionSequenceItem item in dispAreaMod.DisplayedAreaSelectionSequence)
            {
                var dictionary = !DeserializeIgnoreImageRelationship ? new ImageSopInstanceReferenceDictionary(item.ReferencedImageSequence, true) : null;
                if (dictionary == null || dictionary.ReferencesFrame(image.Frame.SopInstanceUid, image.Frame.FrameNumber))
                {
                    // get the displayed area of the image in source coordinates (stored values do not have sub-pixel accuracy)
                    var displayRect = RectangleF.FromLTRB(item.DisplayedAreaTopLeftHandCorner.X,
                                                          item.DisplayedAreaTopLeftHandCorner.Y,
                                                          item.DisplayedAreaBottomRightHandCorner.X,
                                                          item.DisplayedAreaBottomRightHandCorner.Y);
                    displayRect          = RectangleUtilities.ConvertToPositiveRectangle(displayRect);
                    displayRect.Location = displayRect.Location - new SizeF(1, 1);
                    displayRect.Size     = displayRect.Size + new SizeF(1, 1);

                    var centerDisplay = true;

                    switch (item.PresentationSizeMode)
                    {
                    case DisplayedAreaModuleIod.PresentationSizeMode.Magnify:
                        // displays selected area at specified magnification factor
                        spatialTransform.Scale = (float)item.PresentationPixelMagnificationRatio.GetValueOrDefault(1);
                        break;

                    case DisplayedAreaModuleIod.PresentationSizeMode.TrueSize:
                    // currently no support for determining true size, so default to scale area to fit
                    case DisplayedAreaModuleIod.PresentationSizeMode.ScaleToFit:
                    case DisplayedAreaModuleIod.PresentationSizeMode.None:
                    default:
                        if (spatialTransform is IImageSpatialTransform && displayRect.Location == new PointF(0, 0) && displayRect.Size == new SizeF(image.ImageGraphic.Columns, image.ImageGraphic.Rows))
                        {
                            // if the display rect is the whole image, then take advantage of the built-in scale image to fit functionality
                            IImageSpatialTransform iist = (IImageSpatialTransform)spatialTransform;
                            iist.ScaleToFit = true;
                            centerDisplay   = false;                                   // when ScaleToFit is true, the image will automatically be positioned correctly in the client area
                        }
                        else
                        {
                            var clientArea  = image.ClientRectangle.Size;
                            var displaySize = displayRect.Size;

                            // if image is rotated 90 or 270, transpose width/height
                            if (Math.Abs(Math.Round(Math.Sin(spatialTransform.RotationXY * Math.PI / 180))) > 0)
                            {
                                displaySize = new SizeF(displaySize.Height, displaySize.Width);
                            }

                            // compute the maximum magnification that allows the entire displayRect to be visible
                            spatialTransform.Scale = Math.Min(clientArea.Width / displaySize.Width, clientArea.Height / displaySize.Height);
                        }

                        break;
                    }

                    if (centerDisplay)
                    {
                        // compute translation so that the displayRect is centered in the clientRect
                        var displayCentre = GetRectangleCenter(displayRect);
                        var clientCentre  = spatialTransform.ConvertToSource(GetRectangleCenter(image.ClientRectangle));
                        spatialTransform.TranslationX = clientCentre.X - displayCentre.X;
                        spatialTransform.TranslationY = clientCentre.Y - displayCentre.Y;
                    }

                    displayedArea = displayRect;
                    return;
                }
            }
            displayedArea = new RectangleF(0, 0, image.ImageGraphic.Columns, image.ImageGraphic.Rows);
        }
示例#6
0
        public override string GetAnnotationText(IPresentationImage presentationImage)
        {
            if (presentationImage == null)
            {
                return(String.Empty);
            }

            IImageSopProvider imageSopProvider = presentationImage as IImageSopProvider;

            if (imageSopProvider == null)
            {
                return(String.Empty);
            }

            ISpatialTransformProvider spatialTransformProvider = presentationImage as ISpatialTransformProvider;

            if (spatialTransformProvider == null)
            {
                return(String.Empty);
            }

            // 2D DFOV value doesn't make a lot of sense when the "image" is 3D, so we're blanking it out until we define what DFOV means in this context
            if (presentationImage is ISpatialTransform3DProvider)
            {
                return(String.Empty);
            }

            IImageSpatialTransform transform = spatialTransformProvider.SpatialTransform as IImageSpatialTransform;

            if (transform == null)
            {
                return(String.Empty);
            }

            if (transform.RotationXY % 90 != 0)
            {
                return(SR.ValueNotApplicable);
            }

            Frame        frame = imageSopProvider.Frame;
            PixelSpacing normalizedPixelSpacing = frame.NormalizedPixelSpacing;

            if (normalizedPixelSpacing.IsNull)
            {
                return(String.Empty);
            }

            RectangleF sourceRectangle      = new RectangleF(0, 0, frame.Columns, frame.Rows);
            RectangleF destinationRectangle = transform.ConvertToDestination(sourceRectangle);

            destinationRectangle = RectangleUtilities.Intersect(destinationRectangle, presentationImage.ClientRectangle);

            //Convert the displayed width and height to source dimensions
            SizeF widthInSource  = transform.ConvertToSource(new SizeF(destinationRectangle.Width, 0));
            SizeF heightInSource = transform.ConvertToSource(new SizeF(0, destinationRectangle.Height));

            //The displayed FOV is given by the magnitude of each line in source coordinates, but
            //for each of the 2 lines, one of x or y will be zero, so we can optimize.

            float x1 = Math.Abs(widthInSource.Width);
            float y1 = Math.Abs(widthInSource.Height);
            float x2 = Math.Abs(heightInSource.Width);
            float y2 = Math.Abs(heightInSource.Height);

            double displayedFieldOfViewX, displayedFieldOfViewY;

            if (x1 > y1)             //the image is not rotated
            {
                displayedFieldOfViewX = x1 * normalizedPixelSpacing.Column / 10;
                displayedFieldOfViewY = y2 * normalizedPixelSpacing.Row / 10;
            }
            else             //the image is rotated by 90 or 270 degrees
            {
                displayedFieldOfViewX = x2 * normalizedPixelSpacing.Column / 10;
                displayedFieldOfViewY = y1 * normalizedPixelSpacing.Row / 10;
            }

            return(String.Format(SR.FormatCentimeters, String.Format(SR.Format2Dimensions, displayedFieldOfViewX.ToString("F1"), displayedFieldOfViewY.ToString("F1"))));
        }
示例#7
0
        public static Bitmap DrawToBitmap(IPresentationImage image, ExportImageParams exportParams)
        {
            Platform.CheckForNullReference(image, "image");
            Platform.CheckForNullReference(exportParams, "exportParams");

            if (!(image is ISpatialTransformProvider))
            {
                throw new ArgumentException("The image must have a valid ImageSpatialTransform in order to be exported.");
            }

            if (exportParams.ExportOption == ExportOption.TrueSize)
            {
                var imageSopProvider = image as IImageSopProvider;
                var pixelSpacing     = imageSopProvider == null ? null : imageSopProvider.Frame.NormalizedPixelSpacing;
                if (pixelSpacing == null || pixelSpacing.IsNull)
                {
                    throw new ArgumentException("The image does not contain pixel spacing information.  TrueSize export is not possible.");
                }
            }

            IImageSpatialTransform transform = ((ISpatialTransformProvider)image).SpatialTransform as IImageSpatialTransform;

            if (transform == null)
            {
                throw new ArgumentException("The image must have a valid ImageSpatialTransform in order to be exported.");
            }

            if (exportParams.ExportOption == ExportOption.TrueSize)
            {
                return(DrawTrueSizeImageToBitmap(image, exportParams.OutputSize, exportParams.Dpi));
            }

            if (exportParams.SizeMode == SizeMode.Scale)
            {
                // TODO: Refactor ImageExporter, so there only the displayRectangle and OutputRectangle are provided
                //		Scale can be automatically figured out.
                //		A "Padded" option can be provided to distinguish between the current Fixed and ScaleToFit options
                // TODO: Refactor ImageExporter, so there are separate exporters for each ExportOption.
                //		The ExportImageParams is getting too many options and not all of them are applicable to each exporter
                //		Instead, each exporter should have its own parameters.

                if (exportParams.ExportOption == ExportOption.Wysiwyg)
                {
                    return(DrawWysiwygImageToBitmap(image, exportParams.DisplayRectangle, exportParams.Scale, exportParams.Dpi));
                }
                else
                {
                    return(DrawCompleteImageToBitmap(image, exportParams.Scale, exportParams.Dpi));
                }
            }
            else if (exportParams.SizeMode == SizeMode.ScaleToFit)
            {
                if (exportParams.ExportOption == ExportOption.Wysiwyg)
                {
                    var scale = ScaleToFit(exportParams.DisplayRectangle.Size, exportParams.OutputSize);
                    return(DrawWysiwygImageToBitmap(image, exportParams.DisplayRectangle, scale, exportParams.Dpi));
                }
                else
                {
                    var scale = ScaleToFit(image.SceneSize, exportParams.OutputSize);
                    return(DrawCompleteImageToBitmap(image, scale, exportParams.Dpi));
                }
            }
            else
            {
                Bitmap paddedImage = new Bitmap(exportParams.OutputSize.Width, exportParams.OutputSize.Height);
                using (System.Drawing.Graphics graphics = System.Drawing.Graphics.FromImage(paddedImage))
                {
                    // paint background
                    using (Brush b = new SolidBrush(exportParams.BackgroundColor))
                    {
                        graphics.FillRectangle(b, new Rectangle(Point.Empty, exportParams.OutputSize));
                    }

                    // paint image portion
                    Bitmap bmp;
                    if (exportParams.ExportOption == ExportOption.Wysiwyg)
                    {
                        float scale = ScaleToFit(exportParams.DisplayRectangle.Size, exportParams.OutputSize);
                        bmp = DrawWysiwygImageToBitmap(image, exportParams.DisplayRectangle, scale, exportParams.Dpi);
                    }
                    else
                    {
                        float scale = ScaleToFit(image.SceneSize, exportParams.OutputSize);
                        bmp = DrawCompleteImageToBitmap(image, scale, exportParams.Dpi);
                    }
                    graphics.DrawImageUnscaledAndClipped(bmp, new Rectangle(CenterRectangles(bmp.Size, exportParams.OutputSize), bmp.Size));
                    bmp.Dispose();
                }

                return(paddedImage);
            }
        }