/// <summary>
        /// Initialise the tile renderer from input, output, and tile sizes
        /// </summary>
        /// <param name="input">Surface containing the input data (the image to be transformed)</param>
        /// <param name="output">Surface containing the output image</param>
        /// <param name="tileSize">Width and height of a tile</param>
        /// <param name="margin">Outer margin of tiles in percent [0..1]</param>
        public TiledRenderer(ISurface <ColorBgra> input, ISurface <ColorBgra> output, int tileSize, float margin)
        {
            var s = new RangedValue <float>(margin, 0.0f, 1.0f).Value;

            TileSize = tileSize;
            Margin   = (int)(tileSize * s);
            Input    = input;
            Output   = output;
        }
Пример #2
0
        /// <summary>
        /// Return the recommended tile size based on the available RAM
        /// </summary>
        /// <param name="size">Size of the input in pixels</param>
        /// <param name="margin">Tile margin in percent [0, 1.0]</param>
        /// <returns>Maximum tile size that can be processed safely</returns>
        public static Size GetRecommendedTileSize(Size size, float margin)
        {
            // the .NET heap *will* allocate much more than 33% of the available RAM
            // though the algorithm performs better with less (i.e. bigger) tiles in
            // terms of output quality, this will come at the expense of much longer
            // calculation time
            const int THRESHOLD_PERCENT = 33;
            var       threshold         = (AvailableMemory * THRESHOLD_PERCENT) / 100;
            var       required          = GetEstimatedRequiredMemory(size);

            if (required > threshold)
            {
                var factor   = new RangedValue <float>(margin, 0.0f, 1.0f).Value;
                var pixels   = GetMaximumPixelCount(threshold);
                var tileSize = (int)Math.Floor(Math.Sqrt(pixels));
                var n        = (int)(tileSize / (1.0f + factor));
                if (TiledRenderer.GetTiles(size, n).Count > 1)
                {
                    return(new Size(n, n));
                }
            }

            return(size);
        }