/// <inheritdoc/> public bool TryWrite(Lazy <Stream> stream, CodecFormat format, SpanBitmap bmp) { try { var fmt = GetFormatFromExtension(format); var clr = _Implementation.ToPixelFormat(Pixel.RGBA32.Format); if (bmp.PixelFormat.Components.Any(item => item.IsLuminance)) { clr = _Implementation.ToPixelFormat(Pixel.Luminance8.Format); } if (clr.Color == SkiaSharp.SKColorType.Unknown) { return(false); } using (var skbmp = _Implementation.CloneAsSKImage(bmp, clr)) { var data = skbmp.Encode(fmt, 95); data.SaveTo(stream.Value); } } catch (ArgumentException) { return(false); } return(true); }
public static void Copy(SpanBitmap src, ref XNA.Texture2D dst, bool fit, Func <int, int, XNA.Texture2D> texFactory) { if (dst == null || dst.Width != src.Width || dst.Height != src.Height) { if (dst != null) { dst.Dispose(); } dst = null; } if (dst == null) { dst = texFactory(src.Width, src.Height); } if (dst.Format == XNA.SurfaceFormat.Bgr565) { _Copy16(src, dst, fit); return; } if (dst.Format == XNA.SurfaceFormat.Bgra32) { _Copy32(src, dst, fit); return; } }
public static WIC_WRITABLE ToWritableBitmap(SpanBitmap src) { var dst = ToWritableBitmap(src.Info); dst.SetPixels(0, 0, src); return(dst); }
private static void _Copy16(SpanBitmap src, XNA.Texture2D dst, bool fit) { if (dst == null) { throw new ArgumentNullException(nameof(dst)); } var fmt = ToInteropFormat(dst.Format); if (fmt.ByteCount != 2) { throw new ArgumentException("invalid pixel size", nameof(dst)); } var l = dst.Width * dst.Height; if (_16BitBuffer == null || _16BitBuffer.Length < l) { Array.Resize(ref _16BitBuffer, l); } var dstx = new SpanBitmap <ushort>(_16BitBuffer, dst.Width, dst.Height, fmt); fit &= !(src.Width == dst.Width && src.Height == dst.Height); if (fit) { dstx.AsTypeless().FitPixels(src); } else { dstx.AsTypeless().SetPixels(0, 0, src); } dst.SetData(_16BitBuffer); return; }
public static void Transfer(SpanBitmap src, GDIBITMAP dst, SpanBitmap.Action2 action) { src = src.AsReadOnly(); var rect = new Rectangle(0, 0, dst.Width, dst.Height); GDIPTR dstBits = null; try { dstBits = dst.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, dst.PixelFormat); var dstSpan = dstBits .AsPointerBitmapDangerous() .AsSpanBitmap(); action(src, dstSpan); } finally { if (dstBits != null) { dst.UnlockBits(dstBits); } } }
public static Imaging.BitmapSampler <TPixel> AsBitmapSampler <TPixel>(this SpanBitmap <TPixel> src) where TPixel : unmanaged { var encoding = GetColorEncoding <TPixel>(); return(new Imaging.BitmapSampler <TPixel>(src.ReadableBytes, src.Info.StepByteSize, src.Width, src.Height, encoding)); }
public static void ComposePixels <TSrcPixel, TDstPixel>(SpanBitmap <TSrcPixel> src, SpanBitmap <TDstPixel> dst, TRANSFORM srcXform, bool useBilinear, float opacity) where TSrcPixel : unmanaged, Pixel.IConvertTo where TDstPixel : unmanaged, Pixel.IConvertTo { int opacityQ = (int)(opacity * 256); void _rowProcessorNearest(Span <TDstPixel> dst, SpanQuantized8Sampler <TSrcPixel, Pixel.BGRP32> src, _RowTransformIterator srcIterator) { Pixel.BGRP32 srcPixel = default; Pixel.BGRP32 composer = default; for (int i = 0; i < dst.Length; ++i) { srcIterator.MoveNext(out int sx, out int sy); src.GetSourcePixelOrDefault(sx, sy).CopyTo(ref srcPixel); if (srcPixel.A == 0) { continue; } ref var dstI = ref dst[i]; dstI.CopyTo(ref composer); composer.SetSourceOver(srcPixel, opacityQ); composer.CopyTo(ref dstI); } }
public static void OpaquePixelsConvert <TSrcPixel, TDstPixel>(SpanBitmap <TSrcPixel> src, SpanBitmap <TDstPixel> dst, TRANSFORM srcXform, bool useBilinear) where TSrcPixel : unmanaged, Pixel.IConvertTo where TDstPixel : unmanaged { void _rowProcessorNearest(Span <TDstPixel> dst, SpanQuantized8Sampler <TSrcPixel, TSrcPixel> src, _RowTransformIterator srcIterator) { for (int i = 0; i < dst.Length; ++i) { srcIterator.MoveNext(out int sx, out int sy); src.GetSourcePixelOrDefault(sx, sy).CopyTo(ref dst[i]); } } void _rowProcessorBilinear(Span <TDstPixel> dst, SpanQuantized8Sampler <TSrcPixel, TSrcPixel> src, _RowTransformIterator srcIterator) { for (int i = 0; i < dst.Length; ++i) { srcIterator.MoveNext(out int sx, out int sy, out int rx, out int ry); src.GetSampleOrDefault(sx, sy, rx, ry).CopyTo(ref dst[i]); } } var p = useBilinear ? (_ProcessRowCallback8 <TSrcPixel, TSrcPixel, TDstPixel>)_rowProcessorBilinear : (_ProcessRowCallback8 <TSrcPixel, TSrcPixel, TDstPixel>)_rowProcessorNearest; _ProcessRows(dst, src, srcXform, p); }
public static bool CopyTo(SpanBitmap src, ref ANDROIDBITMAP dst) { var refreshed = Reshape(ref dst, src.Info, null); _PerformCopy(src, dst); return(refreshed); }
// https://www.sciencedirect.com/science/article/pii/S1877705813016007 // https://github.com/justadudewhohacks/opencv4nodejs/issues/448#issuecomment-436341650 // https://stackoverflow.com/questions/17887883/image-sharpness-metric // https://jivp-eurasipjournals.springeropen.com/articles/10.1186/1687-5281-2012-8 // https://www.sciencedirect.com/science/article/pii/S1877705813016007 // https://ieeexplore.ieee.org/document/8073580 // https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.478.82&rep=rep1&type=pdf public static Double Evaluate(SpanBitmap image, double power = 1) { var evaluator = new SharpnessEvaluator(power); evaluator.AddImage(image); return(evaluator.SharpnessFactor); }
private static void _PerformCopy(SpanBitmap src, ANDROIDBITMAP dst) { if (src.Info.IsEmpty) { return; } if (dst == null) { return; } if (dst.IsRecycled || !dst.IsMutable) { throw new ArgumentNullException(nameof(dst)); } if (!TryGetBitmapInfo(dst.GetBitmapInfo(), dst.IsPremultiplied, out var nfo)) { throw new Diagnostics.PixelFormatNotSupportedException((dst.GetBitmapInfo().Format, dst.Premultiplied), nameof(dst)); } var ptr = dst.LockPixels(); if (ptr == IntPtr.Zero) { throw new ArgumentNullException("lock", nameof(dst)); } try { new PointerBitmap(ptr, nfo).AsSpanBitmap().SetPixels(0, 0, src); } finally { dst.UnlockPixels(); } }
public static unsafe void CopyPixels <TSrcPixel, TDstPixel>(SpanBitmap <TSrcPixel> src, Image <TDstPixel> dst) where TSrcPixel : unmanaged where TDstPixel : unmanaged, IPixel <TDstPixel> { if (dst == null) { throw new ArgumentNullException(nameof(dst)); } if (src.Width != dst.Width || src.Height != dst.Height) { throw new ArgumentException("dimensions mismatch", nameof(dst)); } if (sizeof(TSrcPixel) != sizeof(TDstPixel)) { throw new ArgumentException("Pixel size mismatch", typeof(TDstPixel).Name); } src = src.AsReadOnly(); for (int y = 0; y < dst.Height; y++) { var srcRow = src.GetScanlinePixels(y); var dstRow = dst.DangerousGetPixelRowMemory(y).Span; MEMORYMARSHALL .Cast <TSrcPixel, TDstPixel>(srcRow) .CopyTo(dstRow); } }
public static TResult ReadAsSpanBitmap <TSelfPixel, TOtherPixel, TResult>(Image <TSelfPixel> self, SpanBitmap <TOtherPixel> other, SpanBitmap <TOtherPixel> .Function2 <TResult> function) where TSelfPixel : unmanaged, IPixel <TSelfPixel> where TOtherPixel : unmanaged { if (self == null) { throw new ArgumentNullException(nameof(self)); } if (!TryGetExactPixelFormat <TSelfPixel>(out var otherFmt)) { throw new NotImplementedException($"{typeof(TSelfPixel)}"); } if (self.DangerousTryGetSinglePixelMemory(out Memory <TSelfPixel> selfMem)) { var selfBytes = MEMORYMARSHALL.Cast <TSelfPixel, Byte>(selfMem.Span); var selfBmp = new SpanBitmap <TOtherPixel>(selfBytes, self.Width, self.Height, otherFmt); return(function(selfBmp.AsReadOnly(), other)); } else { var tempBmp = ImageSharpToolkit .ToMemoryBitmap <TOtherPixel>(self) .AsSpanBitmap() .AsReadOnly(); return(function(tempBmp, other)); } }
public static SkiaSharp.SKBitmap CloneAsSKBitmap(SpanBitmap bmp) { if (!TryGetPixelFormat(bmp.PixelFormat, out var color, out var alpha)) { throw new ArgumentException("format", nameof(bmp)); } var img = new SkiaSharp.SKBitmap(bmp.Width, bmp.Height, color, alpha); var binfo = GetBitmapInfo(img.Info, img.RowBytes); var ptr = img.GetPixels(); if (ptr == IntPtr.Zero) { throw new ArgumentNullException(); } var dst = new SpanBitmap(ptr, binfo); dst.SetPixels(0, 0, bmp); img.NotifyPixelsChanged(); return(img); }
public static void Transfer(GDIBITMAP src, SpanBitmap dst, SpanBitmap.Action2 action) { var rect = new Rectangle(0, 0, dst.Width, dst.Height); GDIPTR srcBits = null; try { srcBits = src.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadOnly, src.PixelFormat); var srcSpan = srcBits .AsPointerBitmapDangerous() .AsSpanBitmap() .AsReadOnly(); action(srcSpan, dst); } finally { if (srcBits != null) { src.UnlockBits(srcBits); } } }
public readonly bool TryApplyTo(SpanBitmap target) { if (_Rnd == null) { _Rnd = new Random(); } for (int y = 0; y < target.Height; ++y) { var row = target.UseScanlineBytes(y); var fourCount = row.Length & ~3; var rowInts = System.Runtime.InteropServices.MemoryMarshal.Cast <byte, int>(row.Slice(0, fourCount)); for (int x = 0; x < rowInts.Length; ++x) { rowInts[x] = _Rnd.Next(); } for (int x = rowInts.Length * 4; x < row.Length; ++x) { row[x] = (Byte)_Rnd.Next(); } } return(true); }
public static bool TryGetBitmapBGR(IDenseTensor <float> src, out SpanBitmap <Vector3> bmp) { bmp = default; var dims = src.Dimensions; if (dims.Length < 3 || dims.Length > 4) { return(false); } if (dims.Length == 4) { if (dims[0] != 1) { return(false); } dims = dims.Slice(1); } if (dims[2] != 3) { return(false); } var data = System.Runtime.InteropServices.MemoryMarshal.Cast <float, byte>(src.Span); bmp = new SpanBitmap <Vector3>(data, dims[1], dims[0], Pixel.BGR96F.Format); return(true); }
/// <inheritdoc/> public bool TryWrite(Lazy <Stream> stream, CodecFormat format, SpanBitmap bmp) { var fmt = GetFormatFromExtension(format); if (fmt == null) { return(false); } if (!_Implementation.TryGetExactPixelFormat(bmp.PixelFormat, out _)) { using (var tmp = _Implementation.CloneAsGDIBitmap(bmp)) { _WriteBitmap(stream.Value, fmt, tmp); } } else { void _doSave(PointerBitmap ptr) { using (var tmp = _Implementation.WrapOrCloneAsGDIBitmap(ptr)) { _WriteBitmap(stream.Value, fmt, tmp); } } bmp.PinReadablePointer(_doSave); } return(true); }
public static Image CloneToImageSharp(SpanBitmap src) { var dst = CreateImageSharp(src.PixelFormat, src.Width, src.Height); CopyPixels(src, dst); return(dst); }
public static bool CopyTo(this SpanBitmap src, ANDROIDBITMAP dst) { if (dst == null) { throw new ArgumentNullException(nameof(dst)); } return(_Implementation.CopyTo(src, dst)); }
public static void WriteRaw(System.IO.BinaryWriter writer, SpanBitmap src, _InBuiltCodec settings) { _WriteHeader(writer); writer.Write((int)1); // planes count _WritePlane(writer, src, settings); }
public static Image <TPixel> CloneToImageSharp <TPixel>(SpanBitmap <TPixel> src) where TPixel : unmanaged, IPixel <TPixel> { var dst = new Image <TPixel>(src.Width, src.Height); CopyPixels(src, dst); return(dst); }
public static Image <TPixel> CloneToImageSharp <TPixel>(SpanBitmap src) where TPixel : unmanaged, IPixel <TPixel> { TryGetExactPixelType(src.PixelFormat, out var pixType); System.Diagnostics.Debug.Assert(pixType == typeof(TPixel)); return(CloneToImageSharp(src.OfType <TPixel>())); }
public static void CopyPixels(Image src, SpanBitmap dst) { if (src is Image <A8> srcA8) { CopyPixels(srcA8, dst); return; } if (src is Image <L8> srcL8) { CopyPixels(srcL8, dst); return; } if (src is Image <L16> srcL16) { CopyPixels(srcL16, dst); return; } if (src is Image <Bgr565> srcBgr565) { CopyPixels(srcBgr565, dst); return; } if (src is Image <Bgra4444> srcBgra4444) { CopyPixels(srcBgra4444, dst); return; } if (src is Image <Bgra5551> srcBgra5551) { CopyPixels(srcBgra5551, dst); return; } if (src is Image <Rgb24> srcRgb24) { CopyPixels(srcRgb24, dst); return; } if (src is Image <Bgr24> srcBgr24) { CopyPixels(srcBgr24, dst); return; } if (src is Image <Rgba32> srcRgba32) { CopyPixels(srcRgba32, dst); return; } if (src is Image <Bgra32> srcBgra32) { CopyPixels(srcBgra32, dst); return; } if (src is Image <Argb32> srcArgb32) { CopyPixels(srcArgb32, dst); return; } if (src is Image <RgbaVector> srcRgbaVector) { CopyPixels(srcRgbaVector, dst); return; } throw new NotImplementedException(); }
public static void CopyPixels(SpanBitmap src, Image dst) { if (dst is Image <A8> dstA8) { CopyPixels(src, dstA8); return; } if (dst is Image <L8> dstL8) { CopyPixels(src, dstL8); return; } if (dst is Image <L16> dstL16) { CopyPixels(src, dstL16); return; } if (dst is Image <Bgr565> dstBgr565) { CopyPixels(src, dstBgr565); return; } if (dst is Image <Bgra4444> dstBgra4444) { CopyPixels(src, dstBgra4444); return; } if (dst is Image <Bgra5551> dstBgra5551) { CopyPixels(src, dstBgra5551); return; } if (dst is Image <Rgb24> dstRgb24) { CopyPixels(src, dstRgb24); return; } if (dst is Image <Bgr24> dstBgr24) { CopyPixels(src, dstBgr24); return; } if (dst is Image <Rgba32> dstRgba32) { CopyPixels(src, dstRgba32); return; } if (dst is Image <Bgra32> dstBgra32) { CopyPixels(src, dstBgra32); return; } if (dst is Image <Argb32> dstArgb32) { CopyPixels(src, dstArgb32); return; } if (dst is Image <RgbaVector> dstRgbaVector) { CopyPixels(src, dstRgbaVector); return; } throw new NotImplementedException(); }
private static void _CopyBitmap(SpanBitmap src, ref TARGETBITMAP dst) { if (src.IsEmpty) { dst = null; return; } src.WithWPF().CopyTo(ref dst); }
/// <inheritdoc/> public bool TryWrite(Lazy <Stream> stream, CodecFormat format, SpanBitmap bmp) { using (var mat = bmp.WithOpenCv().ToMat()) { mat.WriteToStream(stream.Value, $".{format.ToString().ToLower()}"); } return(true); }
private void _UpdateDirect(SpanBitmap src) { VerifyAccess(); if (src.WithWPF().CopyTo(ref _FrontBuffer)) { OnPropertyChanged(_FrontBufferChanged); } }
public void WriteFrame(SpanBitmap bmp) { _Cache.AsTypeless().SetPixels(0, 0, bmp); if (_Cache.TryGetBuffer(out var segment)) { _Writer.WriteFrame(true, segment.Array, segment.Offset, segment.Count); } }
public static unsafe OpenCvSharp.Mat <TPixel> CloneAsMat <TPixel>(this SpanBitmap <TPixel> srcSpan) where TPixel : unmanaged { var dst = new OpenCvSharp.Mat <TPixel>(srcSpan.Height, srcSpan.Width); var dstSpan = dst.AsSpanBitmap(); dstSpan.SetPixels(0, 0, srcSpan); return(dst); }