public static MemoryBitmap Read(System.IO.Stream stream, IBitmapDecoder[] decoders, int?bytesToReadHint = null)
        {
            if (decoders.Length == 0)
            {
                decoders = GetDefaultDecoders().ToArray();
            }

            Guard.NotNull(nameof(stream), stream);
            Guard.IsTrue(nameof(stream), stream.CanRead);
            Guard.NotNull(nameof(decoders), decoders);
            Guard.GreaterThan(nameof(decoders), decoders.Length, 0);
            Guard.GreaterThan(nameof(bytesToReadHint), bytesToReadHint ?? 1, 0);

            // it the stream position cannot be reset,
            // and there's more than one decoder,
            // load the source into a temporary memory buffer:

            if (!stream.CanSeek && decoders.Length > 1)
            {
                using var mem = _ToMemoryStream(stream, bytesToReadHint);
                return(Read(mem, decoders, (int)mem.Length));
            }

            System.Diagnostics.Debug.Assert(stream.CanSeek);
            System.Diagnostics.Debug.Assert(stream.CanRead);

            // remember the current stream position in case we need to reset it:
            var startPos = stream.Position;

            // loop over each decoder:
            foreach (var decoder in decoders)
            {
                var context = new BitmapDecoderContext
                {
                    Stream      = stream,
                    BytesToRead = bytesToReadHint
                };

                // try to load the file with the current decoder:
                if (decoder.TryRead(context, out MemoryBitmap bmp))
                {
                    return(bmp);
                }

                #if DEBUG
                stream.Position = 0;
                stream.ReadByte();
                #endif

                // last decoder failed, reset the stream position:
                stream.Position = startPos;
            }

            throw new ArgumentException("invalid format or incompatible decoder.", nameof(stream));
        }
Esempio n. 2
0
 /// <inheritdoc/>
 public bool TryRead(BitmapDecoderContext readContext, out MemoryBitmap bitmap)
 {
     try
     {
         bitmap = ReadRaw(readContext.Stream).First();
         return(true);
     }
     catch (System.IO.IOException)
     {
         bitmap = default;
         return(false);
     }
 }
Esempio n. 3
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            bitmap = default;

            using (var mat = OpenCvSharp.Mat.FromStream(context.Stream, OpenCvSharp.ImreadModes.AnyColor))
            {
                if (mat.Cols == 0)
                {
                    return(false);
                }
                bitmap = mat.AsSpanBitmap().ToMemoryBitmap();
            }

            return(true);
        }
Esempio n. 4
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            try
            {
                var frame = System.Windows.Media.Imaging.BitmapFrame.Create(context.Stream, System.Windows.Media.Imaging.BitmapCreateOptions.PreservePixelFormat, System.Windows.Media.Imaging.BitmapCacheOption.None);

                bitmap = _Implementation.ToMemoryBitmap(frame);

                return(true);
            }
            catch (System.NotSupportedException)
            {
                bitmap = default;
                return(false);
            }
        }
Esempio n. 5
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            try
            {
                using (var img = System.Drawing.Image.FromStream(context.Stream))
                {
                    bitmap = _Implementation.CloneAsMemoryBitmap(img);
                }

                return(true);
            }
            catch (System.ArgumentException)
            {
                bitmap = default;
                return(false);
            }
        }
Esempio n. 6
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            try
            {
                using (var img = SixLabors.ImageSharp.Image.Load(context.Stream))
                {
                    bitmap = img.ToMemoryBitmap();
                }

                return(true);
            }
            catch (SixLabors.ImageSharp.UnknownImageFormatException)
            {
                bitmap = default;
                return(false);
            }
        }
Esempio n. 7
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            try
            {
                bitmap = default;

                var bmp = Android.Graphics.BitmapFactory.DecodeStream(context.Stream);

                bmp.CopyTo(ref bitmap);

                return(true);
            }
            catch (System.ArgumentException)
            {
                bitmap = default;
                return(false);
            }
        }
Esempio n. 8
0
        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            bitmap = default;

            try
            {
                var img = STBREAD.ImageResult.FromStream(context.Stream, STBREAD.ColorComponents.RedGreenBlueAlpha);

                bitmap = _Implementation.AsMemoryBitmap(img);

                return(true);
            }
            catch (Exception ex)
            {
                if (ex.Message == "unknown image type")
                {
                    return(false);
                }
                else
                {
                    throw ex;
                }
            }
        }
Esempio n. 9
0
        // TODO: we should try to operate directly with SkiaSharp.SKCodec

        /// <inheritdoc/>
        public bool TryRead(BitmapDecoderContext context, out MemoryBitmap bitmap)
        {
            bitmap = default;

            if (context.BytesToRead.HasValue)
            {
                using (var data = SkiaSharp.SKData.Create(context.Stream, context.BytesToRead.Value))
                {
                    using (var skc = SkiaSharp.SKCodec.Create(data))
                    {
                        if (skc == null)
                        {
                            bitmap = default; return(false);
                        }

                        var binfo = _Implementation.GetBitmapInfo(skc.Info);
                        bitmap = new MemoryBitmap(skc.Pixels, binfo);
                        return(true);
                    }
                }
            }

            using (var skc = SkiaSharp.SKCodec.Create(context.Stream))
            {
                if (skc == null)
                {
                    bitmap = default; return(false);
                }

                var binfo = _Implementation.GetBitmapInfo(skc.Info);
                bitmap = new MemoryBitmap(skc.Pixels, binfo);
                return(true);
            }

            return(false);
        }