public void Full_Full()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Full), new ImageSize(ImageSizeMode.Max, 1), new ImageRotation(0, false), ImageQuality.@default, ImageFormat.jpg);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX, "Expected x offset does not match calculated x offset");
            Assert.AreEqual(0, result.StartY, "Expected y offset does not match calculated y offset");
            Assert.AreEqual(width, result.RegionWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(height, result.RegionHeight, "Expected region height does not match calculated region height");
        }
        public void ExactSizeShouldEqualExactSize()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Region, 500, 500, 500, 500), new ImageSize(ImageSizeMode.Distort, 1, 500, 500), new ImageRotation(0, false), ImageQuality.@default, ImageFormat.jpg);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, 2000, 2000, false);

            Assert.AreEqual(500, result.StartX);
            Assert.AreEqual(500, result.StartY);
            Assert.AreEqual(500, result.RegionWidth);
            Assert.AreEqual(500, result.RegionHeight);
        }
        public void PercentageRegionFullSize()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.PercentageRegion, 50, 50, 50, 50), new ImageSize(ImageSizeMode.Max, 1), new ImageRotation(0, false));
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, 2000, 2000, false);

            Assert.AreEqual(1000, result.StartX);
            Assert.AreEqual(1000, result.StartY);
            Assert.AreEqual(1000, result.RegionWidth);
            Assert.AreEqual(1000, result.RegionHeight);
        }
        public void Full_Expanded_Yes_size_Above_Full_Max_Height()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Full), new ImageSize(ImageSizeMode.Distort, 1, width * 2, height * 4), new ImageRotation(90, false), ImageQuality.@default, ImageFormat.jpg, int.MaxValue, 300, int.MaxValue);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, width, height, true);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth, "Expected region width does not match calculated width");
            Assert.AreEqual(height, result.RegionHeight, "Expected region height does not match calculated height");
            Assert.AreEqual(225, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(300, result.OutputHeight, "Expected height does not match calculated height");
        }
        public void Pct_region_full_No_size_Above_Full_maxWidth()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.PercentageRegion, 0, 0, 100, 100), new ImageSize(ImageSizeMode.Max, 1), new ImageRotation(0, false), ImageQuality.@default, ImageFormat.jpg, 150, int.MaxValue, int.MaxValue);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth, "Expected width does not match calculated width");
            Assert.AreEqual(height, result.RegionHeight, "Expected height does not match calculated height");
            Assert.AreEqual(150, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(100, result.OutputHeight, "Expected height does not match calculated height");
        }
        public void Full_Max_MaxWidth_MaxHeight_Exact()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Full), new ImageSize(ImageSizeMode.MaintainAspectRatio, 1, 0, 2000), new ImageRotation(0, false), ImageQuality.@default, ImageFormat.jpg, 1000, 1000, int.MaxValue);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, 6640, 4007, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(6640, result.RegionWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(4007, result.RegionHeight, "Expected region height does not match calculated region height");
            Assert.AreEqual(1000, result.OutputWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(604, result.OutputHeight, "Expected region height does not match calculated region height");
        }
        public void Full_Expanded_No_size_Above_Full_Max_Width()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Full), new ImageSize(ImageSizeMode.MaintainAspectRatio, 1, width, height), new ImageRotation(90, false), ImageQuality.@default, ImageFormat.jpg, 200, int.MaxValue, int.MaxValue);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth, "Expected width does not match calculated width");
            Assert.AreEqual(height, result.RegionHeight, "Expected height does not match calculated height");
            Assert.AreEqual(200, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(133, result.OutputHeight, "Expected height does not match calculated height");
        }
        public void Full_Pct_Expanded_Yes_size_Above_Full()
        {
            var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Full), new ImageSize(ImageSizeMode.PercentageScaled, 2), new ImageRotation(90, false), ImageQuality.@default, ImageFormat.jpg);
            var result  = ImageRequestInterpreter.GetInterpretedValues(request, width, height, true);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth);
            Assert.AreEqual(height, result.RegionHeight);
            Assert.AreEqual(width * 2, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(height * 2, result.OutputHeight, "Expected height does not match calculated height");
        }
 public void CheckBounds_InvalidRegion()
 {
     var request = new Image.Common.ImageRequest
                   (
         "",
         new ImageRegion(ImageRegionMode.Region, 0, 0, 0, 100),
         new ImageSize(ImageSizeMode.Distort, 1, 100, 100),
         new ImageRotation(0, false),
         ImageQuality.@default,
         ImageFormat.jpg
                   );
     var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);
 }
        public void Full_Pct_Expanded_No_size_Above_Full()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Full),
                new ImageSize(ImageSizeMode.PercentageScaled, 2),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth);
            Assert.AreEqual(height, result.RegionHeight);
        }
        public void Full_Rotated_90()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Full),
                new ImageSize(ImageSizeMode.Max, 1, 0, 0),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(width, result.RegionWidth);
            Assert.AreEqual(height, result.RegionHeight);
        }
        public void Square_Width()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Square),
                new ImageSize(ImageSizeMode.MaintainAspectRatio, 1, 0, 100),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(50, result.StartX, "Expected x offset does not match calculated x offset");
            Assert.AreEqual(0, result.StartY, "Expected y offset does not match calculated y offset");
            Assert.AreEqual(200, result.RegionWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(200, result.RegionHeight, "Expected region height does not match calculated region height");
            Assert.AreEqual(100, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(100, result.OutputHeight, "Expected height does not match calculated width");
        }
        public void Full_Scaled()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Full),
                new ImageSize(ImageSizeMode.MaintainAspectRatio, 1, 225, 100),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(0, result.StartX, "Expected x position does not match calculated x position");
            Assert.AreEqual(0, result.StartY, "Expected y position does not match calculated y position");
            Assert.AreEqual(width, result.RegionWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(height, result.RegionHeight, "Expected region height does not match calculated region height");
            Assert.AreEqual(150, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(100, result.OutputHeight, "Expected height does not match calculated height");
        }
        public void Square_Width_Height_Distorted()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Square),
                new ImageSize(ImageSizeMode.Distort, 1, 150, 50),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(50, result.StartX, "Expected x position does not match calculated x position");
            Assert.AreEqual(0, result.StartY, "Expected y position does not match calculated y position");
            Assert.AreEqual(200, result.RegionWidth, "Expected region width does not match calculated region width");
            Assert.AreEqual(200, result.RegionHeight, "Expected region height does not match calculated region height");
            Assert.AreEqual(150, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(50, result.OutputHeight, "Expected height does not match calculated height");
        }
        public void Square_Height()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Square),
                new ImageSize(ImageSizeMode.MaintainAspectRatio, 1, 0, 100),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, width, height, false);

            Assert.AreEqual(50, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(height, result.RegionWidth);
            Assert.AreEqual(height, result.RegionHeight);
            Assert.AreEqual(100, result.OutputWidth);
            Assert.AreEqual(100, result.OutputHeight);
        }
        public void Region_And_Size()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Region, 600, 900, 100, 100),
                new ImageSize(ImageSizeMode.Distort, 1, 54, 54),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, 1000, 1000, false);

            Assert.AreEqual(600, result.StartX);
            Assert.AreEqual(900, result.StartY);
            Assert.AreEqual(100, result.RegionWidth);
            Assert.AreEqual(100, result.RegionHeight);
            Assert.AreEqual(54, result.OutputWidth);
            Assert.AreEqual(54, result.OutputHeight);
            Assert.AreEqual(0.54f, result.OutputScale);
        }
        public void Full_With_Specific_Size()
        {
            var request = new Image.Common.ImageRequest
                          (
                "",
                new ImageRegion(ImageRegionMode.Full),
                new ImageSize(ImageSizeMode.Distort, 1, 744, 501),
                new ImageRotation(0, false),
                ImageQuality.@default,
                ImageFormat.jpg
                          );
            var result = ImageRequestInterpreter.GetInterpretedValues(request, 1000, 1000, false);

            Assert.AreEqual(0, result.StartX);
            Assert.AreEqual(0, result.StartY);
            Assert.AreEqual(1000, result.RegionWidth);
            Assert.AreEqual(1000, result.RegionHeight);
            Assert.AreEqual(744, result.OutputWidth, "Expected width does not match calculated width");
            Assert.AreEqual(501, result.OutputHeight, "Expected height does not match calculated height");
            //Assert.AreEqual(0.54f, result.OutputScale);
        }
Example #18
0
        /// <summary>
        /// Calculates the region, offset and final output image size
        /// </summary>
        /// <param name="request"></param>
        /// <param name="originalWidth">Original width (pixels) of the source image</param>
        /// <param name="originalHeight">Original height (pixels) of the source image</param>
        /// <param name="allowSizeAboveFull">Allow output image dimensions to exceed that of the source image, but constrained by <see cref="ImageRequest.MaxWidth"/>,<see cref="ImageRequest.MaxHeight"/>,<see cref="ImageRequest.MaxArea"/></param>
        /// <returns></returns>
        static public ProcessState GetInterpretedValues(ImageRequest request, int originalWidth, int originalHeight, bool allowSizeAboveFull)
        {
            request.CheckRequest();
            ProcessState state = new ProcessState();

            state.StartX      = state.StartY = state.RegionHeight = state.RegionWidth = 0;
            state.OutputScale = state.ImageScale = 1;
            switch (request.Region.Mode)
            {
            case ImageRegionMode.PercentageRegion:
                state.StartX       = Convert.ToInt32(request.Region.X / 100 * originalWidth);
                state.StartY       = Convert.ToInt32(request.Region.Y / 100 * originalHeight);
                state.OutputWidth  = state.RegionWidth = Convert.ToInt32(request.Region.Width / 100 * originalWidth);
                state.OutputHeight = state.RegionHeight = Convert.ToInt32(request.Region.Height / 100 * originalHeight);
                break;

            case ImageRegionMode.Region:
                state.StartX       = Convert.ToInt32(request.Region.X);
                state.StartY       = Convert.ToInt32(request.Region.Y);
                state.OutputWidth  = state.RegionWidth = Convert.ToInt32(request.Region.Width);
                state.OutputHeight = state.RegionHeight = Convert.ToInt32(request.Region.Height);
                break;

            case ImageRegionMode.Square:
                state.OutputWidth = state.OutputHeight = state.RegionHeight = state.RegionWidth = Math.Min(originalWidth, originalHeight);
                // pick the middle of the image
                state.StartX = state.RegionWidth == originalWidth ? 0 : Convert.ToInt32(Math.Round((originalWidth - originalHeight) / 2f));
                state.StartY = state.RegionHeight == originalHeight ? 0 : Convert.ToInt32(Math.Round((originalHeight - originalWidth) / 2f));

                break;

            default:
                state.OutputWidth  = state.RegionWidth = originalWidth;
                state.OutputHeight = state.RegionHeight = originalHeight;
                break;
            }

            CheckBounds(state);

            switch (request.Size.Mode)
            {
            case ImageSizeMode.Max:
            case ImageSizeMode.Full:
                // no changes needed as scale and OutputWidth/Height are set
                break;

            case ImageSizeMode.PercentageScaled:
                state.OutputWidth  = Convert.ToInt32(state.RegionWidth * request.Size.Percent.Value);
                state.OutputHeight = Convert.ToInt32(state.RegionHeight * request.Size.Percent.Value);
                state.OutputScale  = request.Size.Percent.Value;
                break;

            case ImageSizeMode.Distort:
                float scaledx, scaledy = 1f;

                scaledy = request.Size.Height / (float)state.RegionHeight;
                scaledx = request.Size.Width / (float)state.RegionWidth;

                state.OutputWidth  = Convert.ToInt32(state.RegionWidth * scaledx);
                state.OutputHeight = Convert.ToInt32(state.RegionHeight * scaledy);
                if (scaledx < scaledy)
                {
                    state.OutputScale = scaledx;
                }
                else
                {
                    state.OutputScale = scaledy;
                }
                if (request.Region.Mode != ImageRegionMode.Full)
                {
                    if (scaledx < scaledy)
                    {
                        state.ImageScale = scaledx;
                    }
                    else
                    {
                        state.ImageScale = scaledy;
                    }
                }
                break;

            case ImageSizeMode.MaintainAspectRatio:
                if (request.Size.Width != 0 && request.Size.Height == 0)
                {
                    var scale = request.Size.Width / (float)state.RegionWidth;
                    state.OutputWidth  = Convert.ToInt32(state.RegionWidth * scale);
                    state.OutputHeight = Convert.ToInt32(state.RegionHeight * scale);
                    state.OutputScale  = scale;
                }
                else if (request.Size.Width == 0 && request.Size.Height != 0)
                {
                    var scale = request.Size.Height / (float)state.RegionHeight;
                    state.OutputWidth  = Convert.ToInt32(state.RegionWidth * scale);
                    state.OutputHeight = Convert.ToInt32(state.RegionHeight * scale);
                    state.OutputScale  = scale;
                }
                else
                {
                    var originalScale = originalWidth / (float)originalHeight;
                    var scale         = Math.Min((request.Size.Height / (float)state.RegionHeight), (request.Size.Width / (float)state.RegionWidth));
                    state.OutputWidth  = Convert.ToInt32(state.RegionWidth * scale);
                    state.OutputHeight = Convert.ToInt32(state.RegionHeight * scale);
                    state.OutputScale  = scale;

                    if (request.Region.Mode != ImageRegionMode.Full)
                    {
                        state.ImageScale = scale;
                    }
                }
                break;
            }

            // final bounds reduction
            float max_scale = 1f;

            (state.OutputWidth, state.OutputHeight, max_scale)
                = ScaleOutput(request.MaxWidth, request.MaxHeight, state.OutputWidth, state.OutputHeight, allowSizeAboveFull);
            state.OutputScale = Math.Min(max_scale, state.ImageScale);

            state.CheckBounds();

            return(state);
        }
 public void RegionShouldNotAllowUnsigned()
 {
     var request = new Image.Common.ImageRequest("", new ImageRegion(ImageRegionMode.Region, -1, 0, 500, 500), new ImageSize(ImageSizeMode.Max, 1), new ImageRotation(0, false));
     var result  = ImageRequestInterpreter.GetInterpretedValues(request, 2000, 2000, false);
 }