Example #1
0
        /// <summary>
        /// Initialize a <see cref="DeepZoomGenerator"/> instance with the specified parameters.
        /// </summary>
        /// <param name="image">The OpenSlide image.</param>
        /// <param name="tileSize">The tile size paramter.</param>
        /// <param name="overlap">The overlap paramter.</param>
        /// <param name="limitBounds">Whether image bounds should be respected.</param>
        /// <param name="disposeImage">Whether the OpenSlide image instance should be disposed when this <see cref="DeepZoomGenerator"/> instance is disposed.</param>
        public DeepZoomGenerator(OpenSlideImage image, int tileSize = 254, int overlap = 1, bool limitBounds = true, bool disposeImage = false)
        {
            _image        = image ?? throw new ArgumentNullException(nameof(image));
            _disposeImage = disposeImage;

            long width, height;

            if (limitBounds)
            {
                _boundX = image.BoundsX ?? 0;
                _boundY = image.BoundsY ?? 0;
                _width  = width = image.BoundsWidth ?? image.Width;
                _height = height = image.BoundsHeight ?? image.Height;
            }
            else
            {
                _boundX = 0;
                _boundY = 0;
                _width  = width = image.Width;
                _height = height = image.Height;
            }

            DeepZoomLayer[]            dzLayers = DeepZoomHelper.CalculateDeepZoomLayers(width, height);
            DeepZoomLayerInformation[] layers   = new DeepZoomLayerInformation[dzLayers.Length];

            for (int i = 0; i < layers.Length; i++)
            {
                DeepZoomLayer dzLayer         = dzLayers[i];
                int           layerDownsample = 1 << (dzLayers.Length - i - 1);

                int level = image.GetBestLevelForDownsample(layerDownsample);
                (long levelWidth, long levelHeight) = image.GetLevelDimensions(level);
                layers[i] = new DeepZoomLayerInformation
                {
                    Level           = level,
                    LevelDownsample = image.GetLevelDownsample(level),
                    LevelWidth      = levelWidth,
                    LevelHeight     = levelHeight,
                    LayerDownsample = layerDownsample,
                    LayerWidth      = dzLayer.Width,
                    LayerHeight     = dzLayer.Height
                };
            }

            _layers   = layers;
            _tileSize = tileSize;
            _overlap  = overlap;
        }
Example #2
0
        internal static DeepZoomLayer[] CalculateDeepZoomLayers(long width, long height)
        {
            if (width <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(width));
            }
            if (height <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(height));
            }
            var           layers       = new List <DeepZoomLayer>();
            DeepZoomLayer currentLayer = new DeepZoomLayer(width, height);

            layers.Add(currentLayer);
            while (currentLayer.Width != 1 || currentLayer.Height != 1)
            {
                currentLayer = new DeepZoomLayer((currentLayer.Width + 1) / 2, (currentLayer.Height + 1) / 2);
                layers.Add(currentLayer);
            }
            DeepZoomLayer[] layersArray = layers.ToArray();
            Array.Reverse(layersArray);
            return(layersArray);
        }