public static void AreNotEqual(IMagickColor <QuantumType> notExpected, IMagickImage <QuantumType> image, int x, int y) { using (var collection = image.GetPixelsUnsafe()) { AreNotEqual(notExpected, collection.GetPixel(x, y), $" at position {x}x{y}"); } }
private static void FixAlpha(IMagickImage <QuantumType> image, QuantumType min, QuantumType max) { using (var pixels = image.GetPixelsUnsafe()) { var alphaIndex = pixels.GetIndex(PixelChannel.Alpha); var channels = pixels.Channels; for (int y = 0; y < image.Height; y++) { var row = pixels.GetArea(0, y, image.Width, 1); if (row is null) { continue; } for (int i = alphaIndex; i < row.Length; i += channels) { if (row[i] <= min) { row[i] = 0; } else if (row[i] >= max) { row[i] = Quantum.Max; } } pixels.SetArea(0, y, image.Width, 1, row); } } }
public static void AreEqual(IMagickColor <QuantumType> expected, IMagickImage <QuantumType> image, int x, int y) { using (var pixels = image.GetPixelsUnsafe()) { AreEqual(expected, pixels.GetPixel(x, y), $" at position {x}x{y}"); } }
public static void AreNotEqual(MagickColor notExpected, IMagickImage image, int x, int y) { using (IPixelCollection collection = image.GetPixelsUnsafe()) { AreNotEqual(notExpected, collection.GetPixel(x, y)); } }
public static void AreEqual(MagickColor expected, IMagickImage image, int x, int y) { using (IPixelCollection pixels = image.GetPixelsUnsafe()) { AreEqual(expected, pixels.GetPixel(x, y)); } }
private static Bitmap ToBitmap <TQuantumType>(this IMagickImage <TQuantumType> self, bool useDensity) where TQuantumType : struct { Throw.IfNull(nameof(self), self); IMagickImage <TQuantumType> image = self; var format = PixelFormat.Format24bppRgb; try { if (!IssRGBCompatibleColorspace(image.ColorSpace)) { image = self.Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { format = PixelFormat.Format32bppArgb; } using (var pixels = image.GetPixelsUnsafe()) { var mapping = GetMapping(format); var bitmap = new Bitmap(image.Width, image.Height, format); for (int y = 0; y < image.Height; y++) { var row = new Rectangle(0, y, image.Width, 1); var data = bitmap.LockBits(row, ImageLockMode.WriteOnly, format); var destination = data.Scan0; var bytes = pixels.ToByteArray(0, y, image.Width, 1, mapping); if (bytes != null) { Marshal.Copy(bytes, 0, destination, bytes.Length); } bitmap.UnlockBits(data); } SetBitmapDensity(self, bitmap, useDensity); return(bitmap); } } finally { if (!ReferenceEquals(self, image)) { image.Dispose(); } } }
private static Bitmap ToBitmap <TQuantumType>(this IMagickImage <TQuantumType> self, bool useDensity) where TQuantumType : struct { Throw.IfNull(nameof(self), self); IMagickImage <TQuantumType> image = self; var isGray = image.ChannelCount == 1 && image.ColorSpace == ColorSpace.Gray; var format = isGray ? PixelFormat.Format8bppIndexed : PixelFormat.Format24bppRgb; try { if (!isGray) { if (image.ColorSpace != ColorSpace.sRGB) { image = self.Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { format = PixelFormat.Format32bppArgb; } } using (var pixels = image.GetPixelsUnsafe()) { var bitmap = new Bitmap(image.Width, image.Height, format); if (typeof(TQuantumType) == typeof(byte) && isGray) { CopyGrayPixels(image, pixels, format, bitmap); } else { CopyPixels(image, pixels, format, bitmap); } SetBitmapDensity(self, bitmap, useDensity); return(bitmap); } } finally { if (!ReferenceEquals(self, image)) { image.Dispose(); } } }
private static BitmapSource ToBitmapSource <TQuantumType>(this IMagickImage <TQuantumType> self, bool useDensity) where TQuantumType : struct { Throw.IfNull(nameof(self), self); IMagickImage <TQuantumType> image = self; var mapping = "RGB"; var format = MediaPixelFormats.Rgb24; try { if (self.ColorSpace == ColorSpace.CMYK && !image.HasAlpha) { mapping = "CMYK"; format = MediaPixelFormats.Cmyk32; } else { if (image.ColorSpace != ColorSpace.sRGB) { image = self.Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { mapping = "BGRA"; format = MediaPixelFormats.Bgra32; } } var step = format.BitsPerPixel / 8; var stride = image.Width * step; using (var pixels = image.GetPixelsUnsafe()) { var bytes = pixels.ToByteArray(mapping); var dpi = image.GetDefaultDensity(useDensity ? DensityUnit.PixelsPerInch : DensityUnit.Undefined); return(BitmapSource.Create(image.Width, image.Height, dpi.X, dpi.Y, format, null, bytes, stride)); } } finally { if (!ReferenceEquals(self, image)) { image.Dispose(); } } }
public Bitmap ToBitmap(BitmapDensity bitmapDensity) { IMagickImage image = this; string mapping = "BGR"; var format = PixelFormat.Format24bppRgb; try { if (image.ColorSpace != ColorSpace.sRGB) { image = Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { mapping = "BGRA"; format = PixelFormat.Format32bppArgb; } using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var bitmap = new Bitmap(image.Width, image.Height, format); var data = bitmap.LockBits(new Rectangle(0, 0, image.Width, image.Height), ImageLockMode.ReadWrite, format); var destination = data.Scan0; for (int y = 0; y < Height; y++) { byte[] bytes = pixels.ToByteArray(0, y, Width, 1, mapping); Marshal.Copy(bytes, 0, destination, bytes.Length); destination = new IntPtr(destination.ToInt64() + data.Stride); } bitmap.UnlockBits(data); SetBitmapDensity(bitmap, bitmapDensity); return(bitmap); } } finally { if (!ReferenceEquals(this, image)) { image.Dispose(); } } }
public BitmapSource ToBitmapSource(BitmapDensity bitmapDensity) { IMagickImage image = this; var mapping = "RGB"; var format = MediaPixelFormats.Rgb24; try { if (ColorSpace == ColorSpace.CMYK && !image.HasAlpha) { mapping = "CMYK"; format = MediaPixelFormats.Cmyk32; } else { if (ColorSpace != ColorSpace.sRGB) { image = Clone(); image.ColorSpace = ColorSpace.sRGB; } if (image.HasAlpha) { mapping = "BGRA"; format = MediaPixelFormats.Bgra32; } } var step = format.BitsPerPixel / 8; var stride = Width * step; using (IPixelCollection pixels = image.GetPixelsUnsafe()) { var bytes = pixels.ToByteArray(mapping); var dpi = GetDpi(bitmapDensity); return(BitmapSource.Create(Width, Height, dpi.X, dpi.Y, format, null, bytes, stride)); } } finally { if (!ReferenceEquals(this, image)) { image.Dispose(); } } }