Magick.net: Save image from unmanage pointer

Created on 21 Jun 2019  路  6Comments  路  Source: dlemstra/Magick.NET

I use ffmpeg.autogen for receiving a frame from a video and want to continue to keep this frame in the picture. But I only get a pointer to the frame, how can I save it using Magic.Net? Bitmap was overloading the receive pointer to raw.

Example with Bitmap:

ffmpeg.sws_scale(
                this.pSwsContext,
                (byte*[])this.pDecodedFrame->data,
                (int[])this.pDecodedFrame->linesize,
                0,
                this.Height,
                (byte*[])this.scaledData,
                (int[])this.scaledLinesize);


            var data = new byte_ptrArray8();
            data.UpdateFrom(this.scaledData);
            var linesize = new int_array8();
            linesize.UpdateFrom(this.scaledLinesize);

            var bitmapFrame = new Bitmap(this.Width, this.Height, linesize[0], PixelFormat.Format24bppRgb, (IntPtr)data[0]);
question

All 6 comments

The API only supports managed types. And it looks like this.scaledData is a byte[]? Can you not pass that in instead?

I try copy and load that:

var length = sourceFrame.linesize[0] * sourceFrame.height;
var buff = new byte[length];
Marshal.Copy((IntPtr)data[0], buff, 0, length);
var i = new MagickImage(buff);

but catch next error:

ImageMagick.MagickMissingDelegateErrorException: no decode delegate for this image format `' @ error/blob.c/BlobToImage/458
   at ImageMagick.NativeInstance.CheckException(IntPtr exception, IntPtr result)
   at ImageMagick.MagickImage.NativeMagickImage.ReadBlob(MagickSettings settings, Byte[] data, Int32 offset, Int32 length)
   at ImageMagick.MagickImage.Read(Byte[] data, Int32 offset, Int32 length, MagickReadSettings readSettings, Boolean ping)
   at ImageMagick.MagickImage.Read(Byte[] data, MagickReadSettings readSettings)

maybe I need to specify some settings?

I think you need to call the ReadPixels method of MagickImage instead.

And what I need to specify StorageType and Pixel Mapping?
Possible AVPixelFormat values in attached file, by Bitmap I set AV_PIX_FMT_BGR24.
AVPixelFormat.txt

The storage type should be Char and the pixel mapping BGR.

@dlemstra Thank you, again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cssriraman picture cssriraman  路  7Comments

Anu6is picture Anu6is  路  6Comments

tttrrryyyyeer picture tttrrryyyyeer  路  6Comments

d2phap picture d2phap  路  7Comments

VanillaChai picture VanillaChai  路  9Comments