Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResizeProcessor{TPixel}"/> class.
        /// </summary>
        /// <param name="options">The resize options</param>
        /// <param name="sourceSize">The source image size</param>
        public ResizeProcessor(ResizeOptions options, Size sourceSize)
        {
            Guard.NotNull(options, nameof(options));
            Guard.NotNull(options.Sampler, nameof(options.Sampler));

            int targetWidth  = options.Size.Width;
            int targetHeight = options.Size.Height;

            // Ensure size is populated across both dimensions.
            // These dimensions are used to calculate the final dimensions determined by the mode algorithm.
            // If only one of the incoming dimensions is 0, it will be modified here to maintain aspect ratio.
            // If it is not possible to keep aspect ratio, make sure at least the minimum is is kept.
            const int min = 1;

            if (targetWidth == 0 && targetHeight > 0)
            {
                targetWidth = (int)MathF.Max(min, MathF.Round(sourceSize.Width * targetHeight / (float)sourceSize.Height));
            }

            if (targetHeight == 0 && targetWidth > 0)
            {
                targetHeight = (int)MathF.Max(min, MathF.Round(sourceSize.Height * targetWidth / (float)sourceSize.Width));
            }

            Guard.MustBeGreaterThan(targetWidth, 0, nameof(targetWidth));
            Guard.MustBeGreaterThan(targetHeight, 0, nameof(targetHeight));

            (Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options, targetWidth, targetHeight);

            this.Sampler         = options.Sampler;
            this.Width           = size.Width;
            this.Height          = size.Height;
            this.ResizeRectangle = rectangle;
            this.Compand         = options.Compand;
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResizeProcessor{TPixel}"/> class.
        /// </summary>
        /// <param name="options">The resize options</param>
        /// <param name="sourceSize">The source image size</param>
        public ResizeProcessor(ResizeOptions options, Size sourceSize)
        {
            Guard.NotNull(options, nameof(options));
            Guard.NotNull(options.Sampler, nameof(options.Sampler));

            int tempWidth  = options.Size.Width;
            int tempHeight = options.Size.Height;

            // Ensure size is populated across both dimensions.
            // These dimensions are used to calculate the final dimensions determined by the mode algorithm.
            if (tempWidth == 0 && tempHeight > 0)
            {
                tempWidth = (int)MathF.Round(sourceSize.Width * tempHeight / (float)sourceSize.Height);
            }

            if (tempHeight == 0 && tempWidth > 0)
            {
                tempHeight = (int)MathF.Round(sourceSize.Height * tempWidth / (float)sourceSize.Width);
            }

            Guard.MustBeGreaterThan(tempWidth, 0, nameof(tempWidth));
            Guard.MustBeGreaterThan(tempHeight, 0, nameof(tempHeight));

            (Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options, tempWidth, tempHeight);

            this.Sampler         = options.Sampler;
            this.Width           = size.Width;
            this.Height          = size.Height;
            this.ResizeRectangle = rectangle;
            this.Compand         = options.Compand;
        }
Esempio n. 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ResizeProcessor"/> class.
        /// </summary>
        /// <param name="options">The resize options.</param>
        /// <param name="sourceSize">The source image size.</param>
        public ResizeProcessor(ResizeOptions options, Size sourceSize)
        {
            Guard.NotNull(options, nameof(options));
            Guard.NotNull(options.Sampler, nameof(options.Sampler));

            (Size size, Rectangle rectangle) = ResizeHelper.CalculateTargetLocationAndBounds(sourceSize, options);

            this.Sampler         = options.Sampler;
            this.TargetWidth     = size.Width;
            this.TargetHeight    = size.Height;
            this.TargetRectangle = rectangle;
            this.Compand         = options.Compand;
        }