Ejemplo n.º 1
0
        public Image RenderImage(ILUT lut)
        {
            bool render = false;

            if (_bitmap == null)
            {
                System.Drawing.Imaging.PixelFormat format = Components == 4
                                        ? System.Drawing.Imaging.PixelFormat.Format32bppArgb
                                        : System.Drawing.Imaging.PixelFormat.Format32bppRgb;
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                _bitmap = new Bitmap(ScaledData.Width, ScaledData.Height, ScaledData.Width * 4, format, _pixels.Pointer);
                render  = true;
            }
            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }
            _bitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
            if (render)
            {
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);
            }
            _bitmap.RotateFlip(GetRotateFlipType());
            return(_bitmap);
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Sharp the <see cref="PinnedIntArray"/> with mask (-1,-1,-1, -1,9,-1, -1,-1,-1).
 /// </summary>
 /// <param name="width">Pixel width</param>
 /// <param name="height">Pixel height</param>
 /// <param name="pixels">Pixel data which will be changed.</param>
 public static void Sharp(int width, int height, PinnedIntArray pixels)
 {
     int[] original = new int[width * height];
     // ARGB -> B as the PinnedIntArray is color32.
     Parallel.For(0, height, h =>
     {
         var index = h * width;
         for (int i = 0; i < width; i++)
         {
             original[index + i] = new Color32(pixels.Data[index + i]).B;
         }
     });
     // Sharp the gray pixel.
     int[] smooth = Sharp(width, height, original);
     // Gray -> ARGB as the PinnedIntArray is color32.
     Parallel.For(0, height, h =>
     {
         var index = h * width;
         for (int i = 0; i < width; i++)
         {
             var b = new Color32(smooth[index + i]).B;
             pixels.Data[index + i] = new Color32(0xff, b, b, b).Value;
         }
     });
 }
Ejemplo n.º 3
0
        public BitmapSource RenderImageSource(ILUT lut)
        {
            bool render = false;

            if (_bitmap == null)
            {
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                _bitmap = new WriteableBitmap(ScaledData.Width, ScaledData.Height);
                render  = true;
            }
            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }
            if (render)
            {
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);
            }

            MultiThread.For(0, _pixels.Count, delegate(int i) { _bitmap.Pixels[i] = _pixels.Data[i]; });
            _bitmap.Rotate(_rotation);
            if (_flipX)
            {
                _bitmap.Flip(WriteableBitmapExtensions.FlipMode.Horizontal);
            }
            if (_flipY)
            {
                _bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
            }

            _bitmap.Invalidate();

            return(_bitmap);
        }
Ejemplo n.º 4
0
        public BitmapSource RenderImageSource(ILUT lut)
        {
            bool render = false;

            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }

            if (_bitmapSource == null || render)
            {
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);
                _bitmapSource = RenderBitmapSource(ScaledData.Width, ScaledData.Height, _pixels.Data);
            }

            if (_rotation != 0 || _flipX || _flipY)
            {
                TransformGroup rotFlipTransform = new TransformGroup();
                rotFlipTransform.Children.Add(new RotateTransform(_rotation));
                rotFlipTransform.Children.Add(new ScaleTransform(_flipX ? -1 : 1, _flipY ? -1 : 1));
                _bitmapSource = new TransformedBitmap(_bitmapSource, rotFlipTransform);
            }

            return(_bitmapSource);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Initializes an instance of the <see cref="ImageBase{TImage}"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Image object.</param>
 protected ImageBase(int width, int height, PinnedIntArray pixels, TImage image)
 {
     this.width    = width;
     this.height   = height;
     this.pixels   = pixels;
     this.image    = image;
     this.disposed = false;
 }
Ejemplo n.º 6
0
 protected virtual void Dispose(bool disposing)
 {
     if (_pixels != null)
     {
         _pixels.Dispose();
         _pixels = null;
     }
 }
Ejemplo n.º 7
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
            {
                return;
            }

            _scaleFactor = scale;
            if (_bitmap != null)
            {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// Dispose resources.
        /// </summary>
        /// <param name="disposing">Dispose mode?</param>
        protected virtual void Dispose(bool disposing)
        {
            if (this.disposed)
            {
                return;
            }

            this.image = null;

            if (this.pixels != null)
            {
                this.pixels.Dispose();
                this.pixels = null;
            }

            this.disposed = true;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// Dispose resources.
        /// </summary>
        /// <param name="disposing">Dispose mode?</param>
        protected virtual void Dispose(bool disposing)
        {
            if (disposed)
            {
                return;
            }

            image = null;

            if (pixels != null)
            {
                pixels.Dispose();
                pixels = null;
            }

            disposed = true;
        }
Ejemplo n.º 10
0
        public BitmapSource RenderImageSource(ILUT lut)
        {
            var render = false;

            if (_bitmap == null)
            {
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                _bitmap = BitmapFactory.New(ScaledData.Width, ScaledData.Height);
                render  = true;
            }

            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }

            if (render)
            {
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);

                foreach (var overlay in _overlays)
                {
                    overlay.Render(_pixels.Data, ScaledData.Width, ScaledData.Height);
                }
            }

            using (var context = _bitmap.GetBitmapContext())
            {
                Array.Copy(_pixels.Data, context.Pixels, _pixels.Count);

                _bitmap.Rotate(_rotation);
                if (_flipX)
                {
                    _bitmap.Flip(WriteableBitmapExtensions.FlipMode.Horizontal);
                }
                if (_flipY)
                {
                    _bitmap.Flip(WriteableBitmapExtensions.FlipMode.Vertical);
                }
            }

            return(_bitmap);
        }
Ejemplo n.º 11
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
            {
                return;
            }

            _scaleFactor = scale;
            if (_bitmap != null)
            {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }

            foreach (var overlay in _overlays)
            {
                overlay.Scale(scale);
            }
        }
Ejemplo n.º 12
0
        public BitmapSource RenderImageSource(ILUT lut)
        {
            var render = false;

            if (_bitmap == null)
            {
                _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
                render  = true;
            }

            if (_applyLut && lut != null && !lut.IsValid)
            {
                lut.Recalculate();
                render = true;
            }

            if (render)
            {
                ScaledData.Render((_applyLut ? lut : null), _pixels.Data);

                foreach (var overlay in _overlays)
                {
                    overlay.Render(_pixels.Data, ScaledData.Width, ScaledData.Height);
                }
            }

            using (
                var context = new CGBitmapContext(_pixels, ScaledWidth, ScaledHeight, 8, 4 * ScaledWidth,
                                                  CGColorSpace.CreateDeviceRGB(), CGImageAlphaInfo.PremultipliedLast))
            {
                var transform = CGAffineTransform.MakeRotation((float)(_rotation * Math.PI / 180.0));
                transform.Scale(_flipX ? -1.0f : 1.0f, _flipY ? -1.0f : 1.0f);
                transform.Translate(_flipX ? ScaledWidth : 0.0f, _flipY ? ScaledHeight : 0.0f);
                context.ConcatCTM(transform);

                _bitmap = context.ToImage();
            }

            return(_bitmap);
        }
Ejemplo n.º 13
0
 /// <summary>
 /// Initializes an instance of the <see cref="IOSImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Image object.</param>
 private IOSImage(int width, int height, PinnedIntArray pixels, CGImage image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 14
0
        private void DrawImageBox(DcmImageBox imageBox, Graphics graphics, Point position, int width, int height, int dpiX, int dpiY)
        {
            DcmDataset dataset = imageBox.ImageSequence;

            if (!dataset.Contains(DicomTags.PixelData))
            {
                return;
            }

            DcmPixelData pixelData = new DcmPixelData(dataset);

            PinnedIntArray pixelBuffer = null;
            Bitmap         bitmap      = null;

            if (pixelData.SamplesPerPixel == 3)
            {
                pixelBuffer = new PinnedIntArray(pixelData.GetFrameDataS32(0));
                bitmap      = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight,
                                         pixelData.ImageWidth * sizeof(int), PixelFormat.Format32bppRgb, pixelBuffer.Pointer);
            }
            else
            {
                bool invert = (pixelData.PhotometricInterpretation == "MONOCHROME1");
                if (imageBox.Polarity == "REVERSE")
                {
                    invert = !invert;
                }

                byte[] pixelsOut = null;

                if (pixelData.BitsAllocated == 8)
                {
                    pixelsOut = pixelData.GetFrameDataU8(0);
                }
                else
                {
                    ushort[] pixels = pixelData.GetFrameDataU16(0);
                    pixelsOut = new byte[pixels.Length];
                    double scale = 256.0 / 4096.0;

                    int pixel = 0;
                    for (int y = 0; y < pixelData.ImageHeight; y++)
                    {
                        for (int x = 0; x < pixelData.ImageWidth; x++)
                        {
                            pixelsOut[pixel] = (byte)(pixels[pixel] * scale);
                            pixel++;
                        }
                    }

                    pixels = null;
                }

                bitmap = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight, PixelFormat.Format8bppIndexed);

                if (invert)
                {
                    ColorTable.Apply(bitmap, ColorTable.Monochrome1);
                }
                else
                {
                    ColorTable.Apply(bitmap, ColorTable.Monochrome2);
                }

                BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, pixelData.ImageWidth, pixelData.ImageHeight),
                                                    ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                IntPtr pos = bmData.Scan0;
                for (int i = 0, c = pixelsOut.Length; i < c; i += bmData.Width)
                {
                    Marshal.Copy(pixelsOut, i, pos, bmData.Width);
                    pos = new IntPtr(pos.ToInt64() + bmData.Stride);
                }

                bitmap.UnlockBits(bmData);
            }

            //bitmap.SetResolution(dpiX, dpiY);

            int border = 3;

            double factor = Math.Min((double)height / (double)bitmap.Height,
                                     (double)width / (double)bitmap.Width);

            int drawWidth  = (int)(bitmap.Width * factor) - (border * 2);
            int drawHeight = (int)(bitmap.Height * factor) - (border * 2);

            int drawX = position.X + ((width - drawWidth) / 2);
            int drawY = position.Y + ((height - drawHeight) / 2);

            graphics.DrawImage(bitmap, drawX, drawY, drawWidth, drawHeight);
        }
Ejemplo n.º 15
0
 /// <summary>
 /// Initializes an instance of the <see cref="WinFormsImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Pixel array.</param>
 /// <param name="image">Bitmap image.</param>
 private WinFormsImage(int width, int height, PinnedIntArray pixels, Bitmap image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 16
0
 /// <summary>
 /// Initializes an instance of the <see cref="ImageSharpImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Pixel array.</param>
 /// <param name="image">Bitmap image.</param>
 private ImageSharpImage(int width, int height, PinnedIntArray pixels, Image <Bgra32> image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 17
0
 /// <summary>
 /// Initializes an instance of the <see cref="ImageDisposableBase{TImage}"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Image object.</param>
 protected ImageDisposableBase(int width, int height, PinnedIntArray pixels, TImage image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 18
0
 /// <summary>
 /// Initializes an instance of the <see cref="WindowsImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Writeable bitmap image.</param>
 private WindowsImage(int width, int height, PinnedIntArray pixels, WriteableBitmap image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 19
0
        public void Scale(double scale)
        {
            if (Math.Abs(scale - _scaleFactor) <= Double.Epsilon)
                return;

            _scaleFactor = scale;
            if (_bitmap != null) {
                _scaledData = null;
                _pixels.Dispose();
                _pixels = null;
                _bitmap = null;
            }
        }
Ejemplo n.º 20
0
 public Image RenderImage(ILUT lut)
 {
     bool render = false;
     if (_bitmap == null) {
         PixelFormat format = Components == 4 ? PixelFormat.Format32bppArgb : PixelFormat.Format32bppRgb;
         _pixels = new PinnedIntArray(ScaledData.Width * ScaledData.Height);
         _bitmap = new Bitmap(ScaledData.Width, ScaledData.Height, ScaledData.Width * 4, format, _pixels.Pointer);
         render = true;
     }
     if (_applyLut && lut != null && !lut.IsValid) {
         lut.Recalculate();
         render = true;
     }
     _bitmap.RotateFlip(RotateFlipType.RotateNoneFlipNone);
     if (render) {
         ScaledData.Render((_applyLut ? lut : null), _pixels.Data);
     }
     _bitmap.RotateFlip(GetRotateFlipType());
     return _bitmap;
 }
Ejemplo n.º 21
0
 /// <summary>
 /// Initializes an instance of the <see cref="RawImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Raw byte array image.</param>
 private RawImage(int width, int height, PinnedIntArray pixels, byte[] image)
     : base(width, height, pixels, image)
 {
 }
Ejemplo n.º 22
0
        private void DrawImageBox(DcmImageBox imageBox, Graphics graphics, Point position, int width, int height, int dpiX, int dpiY)
        {
            DcmDataset dataset = imageBox.ImageSequence;
            if (!dataset.Contains(DicomTags.PixelData))
                return;

            DcmPixelData pixelData = new DcmPixelData(dataset);

            PinnedIntArray pixelBuffer = null;
            Bitmap bitmap = null;

            if (pixelData.SamplesPerPixel == 3) {
                pixelBuffer = new PinnedIntArray(pixelData.GetFrameDataS32(0));
                bitmap = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight,
                    pixelData.ImageWidth * sizeof(int), PixelFormat.Format32bppRgb, pixelBuffer.Pointer);
            } else {
                bool invert = (pixelData.PhotometricInterpretation == "MONOCHROME1");
                if (imageBox.Polarity == "REVERSE")
                    invert = !invert;

                byte[] pixelsOut = null;

                if (pixelData.BitsAllocated == 8) {
                    pixelsOut = pixelData.GetFrameDataU8(0);
                } else {
                    ushort[] pixels = pixelData.GetFrameDataU16(0);
                    pixelsOut = new byte[pixels.Length];
                    double scale = 256.0 / 4096.0;

                    int pixel = 0;
                    for (int y = 0; y < pixelData.ImageHeight; y++) {
                        for (int x = 0; x < pixelData.ImageWidth; x++) {
                            pixelsOut[pixel] = (byte)(pixels[pixel] * scale);
                            pixel++;
                        }
                    }

                    pixels = null;
                }

                bitmap = new Bitmap(pixelData.ImageWidth, pixelData.ImageHeight, PixelFormat.Format8bppIndexed);

                if (invert)
                    ColorTable.Apply(bitmap, ColorTable.Monochrome1);
                else
                    ColorTable.Apply(bitmap, ColorTable.Monochrome2);

                BitmapData bmData = bitmap.LockBits(new Rectangle(0, 0, pixelData.ImageWidth, pixelData.ImageHeight),
                    ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);

                IntPtr pos = bmData.Scan0;
                for (int i = 0, c = pixelsOut.Length; i < c; i += bmData.Width) {
                    Marshal.Copy(pixelsOut, i, pos, bmData.Width);
                    pos = new IntPtr(pos.ToInt64() + bmData.Stride);
                }

                bitmap.UnlockBits(bmData);
            }

            //bitmap.SetResolution(dpiX, dpiY);

            int border = 3;

            double factor = Math.Min((double)height / (double)bitmap.Height,
                                     (double)width / (double)bitmap.Width);

            int drawWidth = (int)(bitmap.Width * factor) - (border * 2);
            int drawHeight = (int)(bitmap.Height * factor) - (border * 2);

            int drawX = position.X + ((width - drawWidth) / 2);
            int drawY = position.Y + ((height - drawHeight) / 2);

            graphics.DrawImage(bitmap, drawX, drawY, drawWidth, drawHeight);
        }
Ejemplo n.º 23
0
 /// <summary>
 /// Initializes an instance of the <see cref="AndroidImage"/> object.
 /// </summary>
 /// <param name="width">Image width.</param>
 /// <param name="height">Image height.</param>
 /// <param name="pixels">Array of pixels.</param>
 /// <param name="image">Bitmap image.</param>
 private AndroidImage(int width, int height, PinnedIntArray pixels, Bitmap image)
     : base(width, height, pixels, image)
 {
 }