public static unsafe bool TryWrapAsPointer(CVMATRIX src, out PointerBitmap wrap) { wrap = default; if (src == null) { return(false); } if (src.Width < 0) { return(false); } // src.IsContinuous = true; ?? // src.IsSubMatrix = false ?? if (!TryGetExactPixelFormat(src.Type(), out var fmt)) { return(false); } var info = new BitmapInfo(src.Width, src.Height, fmt, (int)src.Step()); wrap = new PointerBitmap(src.Data, info); return(true); }
private static unsafe void _Apply(PointerBitmap bmp, Func <Mat, Mat> operation) { using (var srcMat = _Implementation.WrapOrCloneAsMat(bmp)) { using (var dstMat = operation(srcMat)) { if (dstMat == srcMat) { return; } bool isValid = true; if (srcMat.Width != dstMat.Width) { isValid = false; } if (srcMat.Height != dstMat.Height) { isValid = false; } if (!isValid) { throw new ArgumentException("Operation should not change image size.", nameof(operation)); } srcMat.AsSpanBitmap().SetPixels(0, 0, dstMat.AsSpanBitmap()); } } }
public static TResult ReadAsImageSharp <TSelfPixel, TOtherPixel, TResult>(PointerBitmap self, Func <Image <TOtherPixel>, TResult> function) where TSelfPixel : unmanaged where TOtherPixel : unmanaged, IPixel <TOtherPixel> { _Verify <TSelfPixel, TOtherPixel>(self.Info); if (self.Info.IsContinuous) { var m = self.UsingMemory <TOtherPixel>(); var s = self.Size; using (var selfImg = Image.WrapMemory(m, s.Width, s.Height)) { return(Run(selfImg, function)); } } else { using (var selfImg = new Image <TOtherPixel>(self.Width, self.Height)) { _Implementation.CopyPixels(self, selfImg); return(Run(selfImg, function)); } } }
public static void WriteAsImageSharp <TSelfPixel, TOtherPixel>(PointerBitmap self, Action <Image <TOtherPixel> > action) where TSelfPixel : unmanaged where TOtherPixel : unmanaged, IPixel <TOtherPixel> { _Verify <TSelfPixel, TOtherPixel>(self.Info); if (self.Info.IsContinuous) { var m = self.UsingMemory <TOtherPixel>(); var s = self.Size; using (var selfImg = Image.WrapMemory(m, s.Width, s.Height)) { Run(selfImg, action); } } else { using (var selfImg = new Image <TOtherPixel>(self.Width, self.Height)) { _Implementation.CopyPixels(self, selfImg); Run(selfImg, action); _Implementation.CopyPixels(selfImg, self); } } }
private static void _Draw(PointerBitmap ptr, System.Windows.Media.PixelFormat fmt, Action <System.Windows.Media.DrawingContext> onDraw) { // https://stackoverflow.com/questions/88488/getting-a-drawingcontext-for-a-wpf-writeablebitmap // https://social.msdn.microsoft.com/Forums/vstudio/en-US/84299fec-94a1-49a1-b3bc-ec48b8bdf04f/getting-a-drawingcontext-for-a-writeablebitmap?forum=wpf // https://stackoverflow.com/questions/7250282/how-to-draw-directly-on-bitmap-bitmapsource-writeablebitmap-in-wpf var src = BitmapSource.Create(ptr.Info.Width, ptr.Info.Height, 96, 96, fmt, null, ptr.Pointer, ptr.Info.BitmapByteSize, ptr.Info.StepByteSize); var dv = new System.Windows.Media.DrawingVisual(); var dc = dv.RenderOpen(); { // dc.DrawImage(src, new System.Windows.Rect(0, 0, src.PixelWidth, src.PixelHeight)); dc.DrawRectangle(System.Windows.Media.Brushes.Green, null, new System.Windows.Rect(50, 50, 200, 100)); onDraw(dc); } var rt = new RenderTargetBitmap(src.PixelWidth, src.PixelHeight, src.DpiX, src.DpiY, System.Windows.Media.PixelFormats.Pbgra32); rt.Render(dv); // ptr.Bitmap.SetPixels(0, 0, rt.ToMemoryBitmap()); rt.CopyPixels(System.Windows.Int32Rect.Empty, ptr.Pointer, ptr.Info.BitmapByteSize, ptr.Info.StepByteSize); }
private static bool _TryWrap(PointerBitmap src, out GDIBITMAP dst, out string errorMsg) { dst = null; if (src.IsEmpty) { errorMsg = "Source is Empty"; return(false); } if (!TryGetExactPixelFormat(src.Info.PixelFormat, out var fmt)) { errorMsg = $"Invalid format {src.Info.PixelFormat}"; return(false); } if ((src.Info.StepByteSize & 3) != 0) { errorMsg = $"Stride Must be multiple of 4 but found {src.Info.StepByteSize}"; return(false); } errorMsg = null; dst = new GDIBITMAP(src.Info.Width, src.Info.Height, src.Info.StepByteSize, fmt, src.Pointer); return(true); }
protected override void Dispose(bool disposing) { if (disposing) // managed resources handling. { if (_Unmanaged != IntPtr.Zero) { var src = new PointerBitmap(_Unmanaged, _Binfo) .AsSpanBitmap(); _Implementation.CopyPixels(src, _Image); } if (_Owned && _Image != null) { System.Threading.Interlocked.Exchange(ref _Image, null)?.Dispose(); } if (_Unmanaged != IntPtr.Zero) // unamanaged resources handling. { // In theory, we should also release this memory when called from // the finalizer. BUT if we've been called by the finalizer is // because our object has not been disposed properly and our ptr // might still be in use, so if we free the memory we could cause // memory corruption. So it's better to leak memory than to // corrupt memory. System.Runtime.InteropServices.Marshal.FreeHGlobal(_Unmanaged); _Unmanaged = IntPtr.Zero; } } }
public static void FitInputImage(this IModelSession session, PointerBitmap bmp) { if (session.GetInputTensor(0) is IInputImageTensor inputImage) { inputImage.FitPixels(bmp); } }
public OpenCvSharp4MemoryAdapter(MemoryBitmap bmp) { _Handle = bmp.Memory.Pin(); _SourcePointer = new PointerBitmap(_Handle.Value, bmp.Info); _SourceBitmap = bmp; _ProxyBitmap = _Implementation.WrapAsMat(_SourcePointer); }
private static void _Draw(PointerBitmap ptr, Action <System.Drawing.Graphics> onDraw) { using (var src = _Implementation.WrapAsGDIBitmap(ptr)) { using (var gfx = System.Drawing.Graphics.FromImage(src)) { onDraw(gfx); } } }
private static void _OnDraw(PointerBitmap ptr, Action <SkiaSharp.SKCanvas> onDraw) { using (var dst = _Implementation.WrapAsSKBitmap(ptr)) { using (var canvas = new SkiaSharp.SKCanvas(dst)) { onDraw(canvas); } } }
/// <summary> /// Tries to wrap the input bitmap with a <see cref="CVMATRIX"/>. /// If it fails, it creates a <see cref="CVMATRIX"/> clone, using the closest compatible pixel format. /// </summary> /// <param name="src">The bitmap to wrap or clone</param> /// <returns>A <see cref="CVMATRIX"/></returns> public static CVMATRIX WrapOrCloneAsMat(PointerBitmap src) { #pragma warning disable CA2000 // Dispose objects before losing scope if (TryWrapAsMat(src, out var wrap)) { return(wrap); } #pragma warning restore CA2000 // Dispose objects before losing scope return(CloneAsMat(src.AsSpanBitmap())); }
public GDIMemoryAdapter(MemoryBitmap bmp) { _SourceBitmap = bmp; _Handle = bmp.Memory.Pin(); var ptr = new PointerBitmap(_Handle.Value, bmp.Info); _ProxyBitmap = _Implementation.WrapAsGDIBitmap(ptr); _DeviceContext = System.Drawing.Graphics.FromImage(_ProxyBitmap); }
public SkiaMemoryAdapter(MemoryBitmap bmp) { _SourceBitmap = bmp; _Handle = bmp.Memory.Pin(); var ptr = new PointerBitmap(_Handle.Value, bmp.Info); _ProxyBitmap = _Implementation.WrapAsSKBitmap(ptr); _DeviceContext = new SkiaSharp.SKCanvas(_ProxyBitmap); }
public static SkiaSharp.SKBitmap WrapAsSKBitmap(PointerBitmap src) { var wrapInfo = GetSKImageInfo(src.Info, false); var wrap = new SkiaSharp.SKBitmap(); if (!wrap.InstallPixels(wrapInfo, src.Pointer, src.Info.StepByteSize)) { throw new InvalidOperationException("InstallPixels"); } return(wrap); }
public WPFMemoryManager(System.Windows.Media.Imaging.WriteableBitmap bmp) { bmp.Lock(); if (!_Implementation.TryGetExactPixelFormat(bmp.Format, out var bmpFmt)) { throw new Diagnostics.PixelFormatNotSupportedException(bmp.Format, nameof(bmp)); } var nfo = new BitmapInfo(bmp.PixelWidth, bmp.PixelHeight, bmpFmt, bmp.BackBufferStride); var ptr = new PointerBitmap(bmp.BackBuffer, nfo); }
public static void SetPixels(WIC_WRITABLE dstBmp, int dstX, int dstY, PointerBitmap srcPtr) { if (dstX < 0) { throw new ArgumentOutOfRangeException(nameof(dstX)); } if (dstY < 0) { throw new ArgumentOutOfRangeException(nameof(dstY)); } if (dstX + srcPtr.Width > dstBmp.PixelWidth) { throw new ArgumentOutOfRangeException(nameof(srcPtr.Width)); } if (dstY + srcPtr.Height > dstBmp.PixelHeight) { throw new ArgumentOutOfRangeException(nameof(srcPtr.Height)); } if (!TryGetExactPixelFormat(dstBmp.Format, out var dstFmt)) { throw new Diagnostics.PixelFormatNotSupportedException(dstBmp.Format, nameof(dstBmp)); } if (srcPtr.PixelFormat == dstFmt) { var rect = new System.Windows.Int32Rect(dstX, dstY, dstBmp.PixelWidth, dstBmp.PixelHeight); dstBmp.WritePixels(rect, srcPtr.Pointer, srcPtr.Info.BitmapByteSize, srcPtr.Info.StepByteSize); return; } try { dstBmp.Lock(); var nfo = new BitmapInfo(dstBmp.PixelWidth, dstBmp.PixelHeight, dstFmt, dstBmp.BackBufferStride); var dstPtr = new PointerBitmap(dstBmp.BackBuffer, nfo); dstPtr.AsSpanBitmap().SetPixels(0, 0, srcPtr.AsSpanBitmap()); var w = Math.Min(dstBmp.PixelWidth, srcPtr.Width); var h = Math.Min(dstBmp.PixelHeight, srcPtr.Height); var drect = new System.Windows.Int32Rect(0, 0, w, h); dstBmp.AddDirtyRect(drect); } finally { dstBmp.Unlock(); } }
public GDIMemoryManager(System.Drawing.Bitmap bmp) { var rect = new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height); _BitmapSource = bmp; _BitmapData = bmp.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite, bmp.PixelFormat); var info = _Implementation.GetBitmapInfo(_BitmapData); System.Diagnostics.Debug.Assert(info.StepByteSize == _BitmapData.Stride); var ptr = new PointerBitmap(_BitmapData.Scan0, info); Initialize(ptr); }
public static void SetGrayPixels(SpanTensor2 <Single> dst, PointerBitmap src, Single offset, Single scale) { if (src.Size != dst.BitmapSize) { throw new ArgumentException(nameof(dst.BitmapSize)); } var dstSpan = dst.AsSpanBitmap(); var srcSpan = src.AsSpanBitmap(); if (srcSpan.PixelFormat.ByteCount == 1) { SpanBitmap.CopyPixels(srcSpan.OfType <Byte>(), dstSpan, (offset, scale), (0, 1)); } }
public static bool TryWrapAsMat(PointerBitmap src, out CVMATRIX wrap) { if (src.Info.IsEmpty) { wrap = null; return(true); } if (!TryGetExactPixelFormat(src.Info.PixelFormat, out var fmt)) { wrap = null; return(false); } wrap = new CVMATRIX(src.Info.Height, src.Info.Width, fmt, src.Pointer, src.Info.StepByteSize); return(true); }
public unsafe void PushFrame(PointerBitmap inputFrame, long?presentationTimeStamp = null) { if (inputFrame.IsEmpty) { return; } var pts = presentationTimeStamp ?? _FrameCount * 30; if (pts <= _LastPTS) { return; } var(converter, encoder) = GetEncoder(inputFrame.Info); if (!inputFrame.Info.Equals(_Format)) { return; } var frame = new AVFrame { data = new byte_ptrArray8 { [0] = (Byte *)inputFrame.Pointer.ToPointer() }, linesize = new int_array8 { [0] = inputFrame.StepByteSize }, height = inputFrame.Height }; var convertedFrame = converter.Convert(frame); convertedFrame.pts = pts; _LastPTS = pts; encoder.Encode(convertedFrame); ++_FrameCount; }
/// <summary> /// Called by the underlaying device when it receives a new capture bitmap, /// it broadcasts an <see cref="VideoCaptureFrameArgs"/> at <see cref="OnFrameReceived"/>. /// </summary> /// <param name="bmp"></param> protected void RaiseFrameReceived(PointerBitmap bmp) { if (_CheckDisposed()) { return; } var time = DateTime.UtcNow; _FPS.AddFrame(); if (_CurrentFrame.Value.CaptureDevice != this) { throw new InvalidOperationException("Owner mismatch"); } _CurrentFrame.Value._CaptureTime = time; _CurrentFrame.Value._CapturedBitmap = bmp; OnFrameReceived?.Invoke(this, _CurrentFrame.Value); }
private static void _HorizontalFlip <TPixel>(PointerBitmap ptrBmp, bool useMultiThreading = true) where TPixel : unmanaged { if (!useMultiThreading) { var dstBmp = ptrBmp.AsSpanBitmapOfType <TPixel>(); _HorizontalFlip(dstBmp, 0, ptrBmp.Height); return; } const int threads = 4; void _hflip(int idx) { var bmp = ptrBmp.AsSpanBitmapOfType <TPixel>(); int y0 = bmp.Height * (idx + 0) / threads; int y1 = bmp.Height * (idx + 1) / threads; _HorizontalFlip(bmp, y0, y1); } System.Threading.Tasks.Parallel.For(0, threads, _hflip); }
public static CVMATRIX WrapAsMat(PointerBitmap src) { return(TryWrapAsMat(src, out var mat) ? mat : throw new ArgumentException("invalid format", nameof(src))); }
public _PointerDrawingContext(PointerBitmap target, Converter <GDICOLOR, TPixel> converter) : base(target.Width, target.Height, converter) { _Target = target; }
public static GDIBITMAP WrapAsGDIBitmap(PointerBitmap src) { return(_TryWrap(src, out var dst, out var err) ? dst : throw new ArgumentException(err, nameof(src))); }
public static GDIBITMAP WrapOrCloneAsGDIBitmap(PointerBitmap src) { return(_TryWrap(src, out var dst, out _) ? dst : CloneAsGDIBitmap(src)); }
protected void Initialize(PointerBitmap ptrbmp) { _Disposed = false; _PointerBitmap = ptrbmp; }
public static void WarpAffine(this PointerBitmap src, PointerBitmap dst, System.Numerics.Matrix3x2 xform) { _Implementation.TransferCv(src, dst, (s, d) => _Implementation.WarpAffine(s, d, xform)); }
public static OpenCvSharp.Mat WrapAsMat(this PointerBitmap src) { return(_Implementation.WrapAsMat(src)); }