From 6e4d7abae7d89f320ac14ae77e6ada774a7b7aa7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Wed, 13 Jan 2016 17:57:40 +1100 Subject: [PATCH] Tweak resampler, allow unsafe Former-commit-id: 1fb5ea424210b30b00733a7a3a241b5655e6d830 Former-commit-id: dcbef8a1b6b01d4b142b75f3c1738173e8436856 Former-commit-id: 0747fa709231401ea60953d8f7a4243a1961dafe --- src/ImageProcessor/Samplers/Resampler.cs | 32 ++++++++++++++--------- src/ImageProcessor/Samplers/RotateFlip.cs | 2 +- src/ImageProcessor/project.json | 3 +++ 3 files changed, 24 insertions(+), 13 deletions(-) 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": {