diff --git a/src/ImageProcessor/Samplers/Resampler.cs b/src/ImageProcessor/Samplers/Resampler.cs index df84e562e8..f3c474e759 100644 --- a/src/ImageProcessor/Samplers/Resampler.cs +++ b/src/ImageProcessor/Samplers/Resampler.cs @@ -321,44 +321,52 @@ namespace ImageProcessor.Samplers private Weights[] PrecomputeWeights(int destinationSize, int sourceSize) { IResampler sampler = this.Sampler; - float du = sourceSize / (float)destinationSize; - float scale = du; + float ratio = sourceSize / (float)destinationSize; + float scale = ratio; + // When shrinking, broaden the effective kernel support so that we still + // visit every source pixel. if (scale < 1) { scale = 1; } - float ru = (float)Math.Ceiling(scale * sampler.Radius); + float scaledRadius = (float)Math.Ceiling(scale * sampler.Radius); Weights[] result = new Weights[destinationSize]; + // Make the weights slices, one source for each column or row. Parallel.For( 0, destinationSize, i => { - float fu = ((i + .5f) * du) - 0.5f; - int startU = (int)Math.Ceiling(fu - ru); + float center = ((i + .5f) * ratio) - 0.5f; + int start = (int)Math.Ceiling(center - scaledRadius); - if (startU < 0) + if (start < 0) { - startU = 0; + start = 0; } - int endU = (int)Math.Floor(fu + ru); + int end = (int)Math.Floor(center + scaledRadius); - if (endU > sourceSize - 1) + if (end > sourceSize) { - endU = sourceSize - 1; + end = sourceSize; + + if (end < start) + { + end = start; + } } float sum = 0; result[i] = new Weights(); List builder = new List(); - for (int a = startU; a <= endU; a++) + for (int a = start; a < end; a++) { - float w = sampler.GetValue((a - fu) / scale); + float w = sampler.GetValue((a - center) / scale); if (w < 0 || w > 0) { diff --git a/src/ImageProcessor/Samplers/RotateFlip.cs b/src/ImageProcessor/Samplers/RotateFlip.cs index 62ccff8b15..4163e83eb3 100644 --- a/src/ImageProcessor/Samplers/RotateFlip.cs +++ b/src/ImageProcessor/Samplers/RotateFlip.cs @@ -150,7 +150,7 @@ namespace ImageProcessor.Samplers { int width = target.Width; int height = target.Height; - int halfHeight = (int)Math.Ceiling(target.Height / 2d); + int halfHeight = (int)Math.Ceiling(target.Height * .5); ImageBase temp = new Image(width, height); temp.ClonePixels(width, height, target.Pixels); diff --git a/src/ImageProcessor/project.json b/src/ImageProcessor/project.json index dc44feb0bd..5cb2b12747 100644 --- a/src/ImageProcessor/project.json +++ b/src/ImageProcessor/project.json @@ -7,6 +7,9 @@ "tags": [ "Image Resize Crop Quality Gif Jpg Jpeg Bitmap Png Fluent Animated" ], + "compilationOptions": { + "allowUnsafe": true + }, "projectUrl": "", "licenseUrl": "", "dependencies": {