Esempio n. 1
0
        public static async Task GetTileAsPngToStreamAsync(this DeepZoomGenerator dz, int level, int locationX, int locationY, Stream stream)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var tileInfo = dz.GetTileInfo(level, locationX, locationY);

            using (var image = new Image <Bgra32>((int)tileInfo.Width, (int)tileInfo.Height))
            {
                WriteImage(dz, image, tileInfo);

                byte[] buffer = ArrayPool <byte> .Shared.Rent(EnsureMinimumSize(tileInfo.TileWidth *tileInfo.TileHeight * 4 * 2));

                try
                {
                    using (var ms = new MemoryStream(buffer))
                    {
                        image.SaveAsPng(ms);
                        ms.SetLength(ms.Position);
                        ms.Position = 0;
                        await ms.CopyToAsync(stream).ConfigureAwait(false);
                    }
                }
                finally
                {
                    ArrayPool <byte> .Shared.Return(buffer);
                }
            }
        }
Esempio n. 2
0
        public static void GetTileAsJpegToStream(this DeepZoomGenerator dz, int level, int locationX, int locationY, Stream stream, int quality = 75)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var tileInfo = dz.GetTileInfo(level, locationX, locationY);

            using (var image = new Image <Bgra32>((int)tileInfo.Width, (int)tileInfo.Height))
            {
                WriteImage(dz, image, tileInfo);

                byte[] buffer = ArrayPool <byte> .Shared.Rent(EnsureMinimumSize(tileInfo.TileWidth *tileInfo.TileHeight * 4 * 2));

                try
                {
                    using (var ms = new MemoryStream(buffer))
                    {
                        image.SaveAsJpeg(ms, new JpegEncoder()
                        {
                            Quality = quality
                        });
                        ms.SetLength(ms.Position);
                        ms.Position = 0;
                        ms.CopyTo(stream);
                    }
                }
                finally
                {
                    ArrayPool <byte> .Shared.Return(buffer);
                }
            }
        }
Esempio n. 3
0
        public static byte[] GetTileAsPng(this DeepZoomGenerator dz, int level, int locationX, int locationY, out TileInfo info)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var tileInfo = dz.GetTileInfo(level, locationX, locationY);

            info = tileInfo;
            using (var image = new Image <Bgra32>((int)tileInfo.Width, (int)tileInfo.Height))
            {
                WriteImage(dz, image, tileInfo);

                byte[] buffer = ArrayPool <byte> .Shared.Rent(EnsureMinimumSize(tileInfo.TileWidth *tileInfo.TileHeight * 4 * 2));

                try
                {
                    using (var ms = new MemoryStream(buffer))
                    {
                        image.SaveAsPng(ms);
                        ms.SetLength(ms.Position);
                        return(ms.ToArray());
                    }
                }
                finally
                {
                    ArrayPool <byte> .Shared.Return(buffer);
                }
            }
        }
Esempio n. 4
0
        public static void GetTileAsPngToStream(this DeepZoomGenerator dz, int level, int locationX, int locationY, MemoryStream stream)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var info = dz.GetTileInfo(level, locationX, locationY);

            using var image = ReadImage(dz, info);
            image.SaveAsPng(stream);
        }
Esempio n. 5
0
        public static byte[] GetTileAsPng(this DeepZoomGenerator dz, int level, int locationX, int locationY, out TileInfo info)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            info            = dz.GetTileInfo(level, locationX, locationY);
            using var image = ReadImage(dz, info);
            using var ms    = PooledMemoryStreamManager.Shared.GetStream();
            image.SaveAsPng(ms);
            return(ms.ToArray());
        }
Esempio n. 6
0
 private static Image <Bgra32> ReadImage(DeepZoomGenerator src, TileInfo tileInfo)
 {
     using var buffer = src.Image.ReadImageBuffer(tileInfo.SlideLevel, tileInfo.X, tileInfo.Y, tileInfo.Width, tileInfo.Height);
     if (tileInfo.Width != tileInfo.TileWidth || tileInfo.Height != tileInfo.TileHeight)
     {
         return(buffer.GetImage().Clone(ctx =>
         {
             ctx.Resize(tileInfo.TileWidth, tileInfo.TileHeight);
         }));
     }
     else
     {
         return(buffer.GetImage().Clone());
     }
 }
Esempio n. 7
0
        public static async Task GetTileAsPngToStreamAsync(this DeepZoomGenerator dz, int level, int locationX, int locationY, Stream stream, CancellationToken cancellationToken = default)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var info = dz.GetTileInfo(level, locationX, locationY);

            using var image = ReadImage(dz, info);
            using var ms    = PooledMemoryStreamManager.Shared.GetStream();
            image.SaveAsPng(ms);
            ms.Seek(0, SeekOrigin.Begin);
            await ms.CopyToAsync(stream, 4096, cancellationToken).ConfigureAwait(false);
        }
Esempio n. 8
0
        public static void GetTileAsPngToStream(this DeepZoomGenerator dz, int level, int locationX, int locationY, Stream stream)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var info = dz.GetTileInfo(level, locationX, locationY);

            using var image = ReadImage(dz, info);
            using var ms    = PooledMemoryStreamManager.Shared.GetStream();
            image.SaveAsPng(ms);
            ms.Seek(0, SeekOrigin.Begin);
            ms.CopyTo(stream);
        }
Esempio n. 9
0
        private static void WriteImage(DeepZoomGenerator src, Image <Bgra32> dest, TileInfo tileInfo)
        {
            src.Image.ReadRegion(
                tileInfo.SlideLevel, tileInfo.X, tileInfo.Y, tileInfo.Width, tileInfo.Height,
                ref Unsafe.As <Bgra32, byte>(ref MemoryMarshal.GetReference(dest.GetPixelSpan())));

            dest.Mutate(ctx =>
            {
                if (tileInfo.ResizeNeeded)
                {
                    ctx.Resize(tileInfo.TileWidth, tileInfo.TileHeight);
                    tileInfo.ResizeNeeded = false;
                }
            });
        }
Esempio n. 10
0
        public static void GetTileAsPngToStream(this DeepZoomGenerator dz, int level, int locationX, int locationY, MemoryStream stream)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var tileInfo = dz.GetTileInfo(level, locationX, locationY);

            using (var image = new Image <Bgra32>((int)tileInfo.Width, (int)tileInfo.Height))
            {
                WriteImage(dz, image, tileInfo);

                image.SaveAsPng(stream);
            }
        }
        private static void WriteImage(DeepZoomGenerator src, Image <Bgra32> dest, TileInfo tileInfo)
        {
            var frame = dest.Frames.RootFrame;

            src.Image.DangerousReadRegion(
                tileInfo.SlideLevel, tileInfo.X, tileInfo.Y, tileInfo.Width, tileInfo.Height,
                ref Unsafe.As <Bgra32, byte>(ref frame.DangerousGetPinnableReferenceToPixelBuffer()));

            dest.Mutate(ctx =>
            {
                if (tileInfo.ResizeNeeded)
                {
                    ctx.Resize(tileInfo.TileWidth, tileInfo.TileHeight);
                    tileInfo.ResizeNeeded = false;
                }
            });
        }
Esempio n. 12
0
        public static void GetTileAsJpegToStream(this DeepZoomGenerator dz, int level, int locationX, int locationY, MemoryStream stream, int quality = 75)
        {
            if (dz == null)
            {
                throw new ArgumentNullException(nameof(dz));
            }

            var tileInfo = dz.GetTileInfo(level, locationX, locationY);

            using (var image = new Image <Rgba32>((int)tileInfo.Width, (int)tileInfo.Height))
            {
                WriteImage(dz, image, tileInfo);

                image.SaveAsJpeg(stream, new JpegEncoder()
                {
                    Quality = quality
                });
            }
        }