Browse Source

Tweak resampler, allow unsafe

Former-commit-id: e77ade827376c5b8391c43f4279e1f49c75d93d1
Former-commit-id: b72fe8bb4e2d417ff6b3a3301a8b7804e6897e8d
Former-commit-id: a79d8c18a2584944f2ee68faf629e9f06d3dba7a
af/merge-core
James Jackson-South 10 years ago
parent
commit
dc0f75e03e
  1. 32
      src/ImageProcessor/Samplers/Resampler.cs
  2. 2
      src/ImageProcessor/Samplers/RotateFlip.cs
  3. 3
      src/ImageProcessor/project.json

32
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<Weight> builder = new List<Weight>();
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)
{

2
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);

3
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": {

Loading…
Cancel
Save