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); }
/// <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; } }); }
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); }
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); }
/// <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; }
protected virtual void Dispose(bool disposing) { if (_pixels != null) { _pixels.Dispose(); _pixels = null; } }
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; } }
/// <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; }
/// <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; }
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); }
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); } }
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); }
/// <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) { }
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); }
/// <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) { }
/// <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) { }
/// <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) { }
/// <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) { }
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; } }
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; }
/// <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) { }
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); }
/// <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) { }