Example #1
0
        public static IBitmapSource FromPointer(IntPtr data, long length)
        {
            var features = new WebPBitstreamFeatures();

            if (LibWebpNative.WebPGetFeaturesInternal(data, (UIntPtr)length, ref features,
                                                      LibWebpNative.WEBP_DECODER_ABI_VERSION) != VP8StatusCode.VP8_STATUS_OK)
            {
                throw new Exception("Failed.");
            }

            var b      = new MemoryBitmapSource(features.width, features.height, 8, 4);
            var config = new WebPDecoderConfig();

            if (LibWebpNative.WebPInitDecoderConfigInternal(ref config, LibWebpNative.WEBP_DECODER_ABI_VERSION) == 0)
            {
                throw new Exception("Failed.");
            }

            config.output.colorspace         = WEBP_CSP_MODE.MODE_BGRA;
            config.output.u.RGBA.rgba        = b.Scan0;
            config.output.u.RGBA.stride      = b.Stride;
            config.output.u.RGBA.size        = (UIntPtr)(b.Stride * features.height);
            config.output.is_external_memory = 1;
            var r = LibWebpNative.WebPDecode(data, (UIntPtr)length, ref config);

            if (r != VP8StatusCode.VP8_STATUS_OK)
            {
                throw new Exception("Failed.");
            }

            return(b);
        }
Example #2
0
        private static MemoryBitmapSource fromBitmapSource(BitmapSource image)
        {
            WriteableBitmap wb;

            if (image.Format == PixelFormats.Bgr24 || image.Format == PixelFormats.Bgra32)
            {
                wb = new WriteableBitmap(image);
            }
            else
            {
                var hasAlpha = image.Format == PixelFormats.Pbgra32 ||
                               image.Format == PixelFormats.Prgba64 ||
                               image.Format == PixelFormats.Rgba64 ||
                               image.Format == PixelFormats.Prgba128Float ||
                               image.Format == PixelFormats.Rgba128Float;

                var converted = new FormatConvertedBitmap();
                converted.BeginInit();
                converted.Source            = image;
                converted.DestinationFormat = hasAlpha ? PixelFormats.Bgra32 : PixelFormats.Bgr24;
                converted.EndInit();

                wb = new WriteableBitmap(converted);
            }

            wb.Freeze();

            var img = new MemoryBitmapSource(wb.PixelWidth, wb.PixelHeight, 8, wb.Format.BitsPerPixel >> 3);

            Misc.CopyFromWritableBitmap(wb, img.FullBitmap);

            return(img);
        }
Example #3
0
        public static IBitmapSource FromPointer(IntPtr data, long length)
        {
            MemoryBitmapSource b;
            var decoder = LibFlifNative.FlifCreateDecoder();

            try {
                if (LibFlifNative.FlifDecoderDecodeMemory(decoder, data, (UIntPtr)length) == 0)
                {
                    return(null);
                }

                var image    = LibFlifNative.FlifDecoderGetImage(decoder, UIntPtr.Zero);
                var width    = LibFlifNative.FlifImageGetWidth(image);
                var height   = LibFlifNative.FlifImageGetHeight(image);
                var bitDepth = LibFlifNative.FlifImageGetDepth(image);
                if (bitDepth > 8)
                {
                    b = new MemoryBitmapSource(width, height, 16, 4);
                    for (var line = 0; line < height; line++)
                    {
                        var position = b.Scan0 + b.Stride * line;
                        LibFlifNative.FlifImageReadRowRgba16(image, line, position, (UIntPtr)b.Stride);
                    }
                }
                else
                {
                    b = new MemoryBitmapSource(width, height, 8, 4);
                    for (var line = 0; line < height; line++)
                    {
                        var position = b.Scan0 + b.Stride * line;
                        LibFlifNative.FlifImageReadRowRgba8(image, line, position, (UIntPtr)b.Stride);
                    }
                    PixelTool.BGRA2RGBA(b.Scan0, (UIntPtr)(width * height));
                }

                LibFlifNative.FlifDestroyImage(image);
            }
            finally {
                // LibFlifNative.FlifDestroyDecoder(decoder);
            }

            return(b);
        }
Example #4
0
        public static IBitmapSource FromPointer(IntPtr data, long length)
        {
            var bmp = EasyBmpNative.DecodeFromBuffer(data, (UIntPtr)length);
            MemoryBitmapSource b;

            try {
                if (bmp.bmp == IntPtr.Zero)
                {
                    return(null);
                }

                b = new MemoryBitmapSource(bmp.width, bmp.height, 8, 4);

                if (!EasyBmpNative.WriteToMemory(ref bmp, b.Scan0, (UIntPtr)b.Stride))
                {
                    return(null);
                }
            }
            finally {
                EasyBmpNative.FreeBMP(ref bmp);
            }

            return(b);
        }
Example #5
0
        public static IBitmapSource FromPointer(IntPtr data, long length)
        {
            var file = new AvifRoData {
                data = data, size = (UIntPtr)length
            };
            var decoder = LibAvifNative.AvifDecoderCreate();
            MemoryBitmapSource b;

            try {
                var result = LibAvifNative.AvifDecoderParse(ref decoder, ref file);
                if (result != AvifResult.AvifResultOk)
                {
                    return(null);
                }

                result = LibAvifNative.AvifDecoderNextImage(ref decoder);
                if (result != AvifResult.AvifResultOk)
                {
                    return(null);
                }

                var image    = Marshal.PtrToStructure <AvifImage>(decoder.image);
                var width    = image.width;
                var height   = image.height;
                var depth    = image.depth;
                var hasAlpha = image.alphaPlane != IntPtr.Zero;

                AvifRgbFormat chroma;
                int           targetDepth;
                int           targetChannel;
                if (depth <= 8)
                {
                    chroma        = AvifRgbFormat.AvifRgbFormatBgra;
                    targetDepth   = 8;
                    targetChannel = 4;
                }
                else if (hasAlpha)
                {
                    chroma        = AvifRgbFormat.AvifRgbFormatRgba;
                    targetDepth   = 16;
                    targetChannel = 4;
                }
                else
                {
                    chroma        = AvifRgbFormat.AvifRgbFormatRgb;
                    targetDepth   = 16;
                    targetChannel = 3;
                }

                var rgb = new AvifRGBImage();
                LibAvifNative.AvifRGBImageSetDefaults(ref rgb, ref image);

                b = new MemoryBitmapSource(width, height, targetDepth, targetChannel);

                rgb.format   = chroma;
                rgb.depth    = depth <= 8 ? 8 : 16;
                rgb.pixels   = b.Scan0;
                rgb.rowBytes = b.Stride;

                result = LibAvifNative.AvifImageYUVToRGB(ref image, ref rgb);

                if (result != AvifResult.AvifResultOk)
                {
                    return(null);
                }
            }
            finally {
                // LibAvifNative.AvifDecoderDestroy(ref decoder);
            }

            return(b);
        }
Example #6
0
        public static unsafe IBitmapSource FromPointer(IntPtr data, long length)
        {
            var ctx         = UIntPtr.Zero;
            var imageHandle = UIntPtr.Zero;
            var options     = UIntPtr.Zero;
            var image       = UIntPtr.Zero;
            MemoryBitmapSource b;

            try {
                ctx = LibHeifNative.HeifContextAlloc();

                var err = LibHeifNative.HeifContextReadFromMemoryWithoutCopy(ctx, data, (UIntPtr)length, UIntPtr.Zero);
                if (CheckErrPtr(err))
                {
                    throw new Exception("Failed.");
                }

                var imageCount = LibHeifNative.HeifContextGetNumberOfTopLevelImages(ctx);
                if (imageCount == 0)
                {
                    throw new Exception("Failed.");
                }

                var imageIDs = new heif_item_id[imageCount];
                fixed(heif_item_id *ptr = imageIDs)
                {
                    imageCount = LibHeifNative.HeifContextGetListOfTopLevelImageIDs(ctx, (UIntPtr)ptr, imageCount);
                }

                err = LibHeifNative.HeifContextGetImageHandle(ctx, imageIDs[0], ref imageHandle);
                if (CheckErrPtr(err))
                {
                    throw new Exception("Failed.");
                }

                var depth = LibHeifNative.HeifImageHandleGetLumaBitsPerPixel(imageHandle);
                if (depth < 0)
                {
                    throw new Exception("Failed.");
                }

                options = LibHeifNative.HeifDecodingOptionsAlloc();

                var hasAlpha = LibHeifNative.HeifImageHandleHasAlphaChannel(imageHandle) != 0;

                HeifChroma chroma;
                int        targetDepth;
                int        targetChannel;
                if (depth <= 8)
                {
                    chroma        = HeifChroma.HeifChromaInterleavedRgba;
                    targetDepth   = 8;
                    targetChannel = 4;
                }
                else if (hasAlpha)
                {
                    chroma        = HeifChroma.HeifChromaInterleavedRrggbbaaLe;
                    targetDepth   = 16;
                    targetChannel = 4;
                }
                else
                {
                    chroma        = HeifChroma.HeifChromaInterleavedRrggbbLe;
                    targetDepth   = 16;
                    targetChannel = 3;
                }
                err = LibHeifNative.HeifDecodeImage(imageHandle, ref image, HeifColorspace.HeifColorspaceRgb,
                                                    chroma, options);

                if (CheckErrPtr(err) || image == UIntPtr.Zero)
                {
                    throw new Exception("Failed.");
                }

                var width  = LibHeifNative.HeifImageGetWidth(image, HeifChannel.HeifChannelInterleaved);
                var height = LibHeifNative.HeifImageGetHeight(image, HeifChannel.HeifChannelInterleaved);
                b = new MemoryBitmapSource(width, height, targetDepth, targetChannel);

                var strideSource = 0;
                var strideDest   = b.Stride;

                var raw = LibHeifNative.HeifImageGetPlaneReadonly(image, HeifChannel.HeifChannelInterleaved, ref strideSource);

                var ptrBegin = b.Scan0;
                if (strideSource != strideDest)
                {
                    for (var line = 0; line < height; line++)
                    {
                        var src = raw + line * strideSource;
                        var dst = ptrBegin + line * strideDest;
                        Buffer.MemoryCopy(src.ToPointer(), dst.ToPointer(), strideSource, strideDest);
                    }
                }
                else
                {
                    Buffer.MemoryCopy(raw.ToPointer(), ptrBegin.ToPointer(), strideSource * height, strideDest * height);
                }

                Console.WriteLine();

                // ReSharper disable once SwitchStatementMissingSomeEnumCasesNoDefault
                switch (chroma)
                {
                case HeifChroma.HeifChromaInterleavedRgba:
                    PixelTool.BGRA2RGBA(b.Scan0, (UIntPtr)(width * height));
                    break;

                case HeifChroma.HeifChromaInterleavedRrggbbaaLe:
                    PixelTool.DEPTHCONVERT(b.Scan0, (UIntPtr)(width * height * 4), (UIntPtr)depth);
                    break;

                case HeifChroma.HeifChromaInterleavedRrggbbLe:
                    PixelTool.DEPTHCONVERT(b.Scan0, (UIntPtr)(width * height * 3), (UIntPtr)depth);
                    break;
                }
            }
            finally {
                if (ctx != UIntPtr.Zero)
                {
                    LibHeifNative.HeifContextFree(ctx);
                }

                if (imageHandle != UIntPtr.Zero)
                {
                    LibHeifNative.HeifImageHandleRelease(imageHandle);
                }

                if (options != UIntPtr.Zero)
                {
                    LibHeifNative.HeifDecodingOptionsFree(options);
                }

                if (image != UIntPtr.Zero)
                {
                    LibHeifNative.HeifImageRelease(image);
                }
            }

            return(b);
        }