Browse Source

Use correct parallel options

pull/68/head
James Jackson-South 9 years ago
parent
commit
5df852a41d
  1. 8
      src/ImageSharp/Image/PixelAccessor{TColor}.cs
  2. 22
      src/ImageSharp/Quantizers/Quantize.cs
  3. 28
      src/ImageSharp/Quantizers/QuantizedImage.cs
  4. 2
      src/ImageSharp/Quantizers/Wu/WuQuantizer.cs

8
src/ImageSharp/Image/PixelAccessor{TColor}.cs

@ -9,6 +9,7 @@ namespace ImageSharp
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
/// <summary>
/// Provides per-pixel access to generic <see cref="Image{TColor}"/> pixels.
@ -60,6 +61,7 @@ namespace ImageSharp
this.pixelsBase = (byte*)this.dataPointer.ToPointer();
this.PixelSize = Unsafe.SizeOf<TColor>();
this.RowStride = this.Width * this.PixelSize;
this.ParallelOptions = image.Bootstrapper.ParallelOptions;
}
/// <summary>
@ -86,6 +88,7 @@ namespace ImageSharp
this.pixelsBase = (byte*)this.dataPointer.ToPointer();
this.PixelSize = Unsafe.SizeOf<TColor>();
this.RowStride = this.Width * this.PixelSize;
this.ParallelOptions = Bootstrapper.Default.ParallelOptions;
}
/// <summary>
@ -121,6 +124,11 @@ namespace ImageSharp
/// </summary>
public int Height { get; }
/// <summary>
/// Gets the global parallel options for processing tasks in parallel.
/// </summary>
public ParallelOptions ParallelOptions { get; }
/// <summary>
/// Gets or sets the pixel at the specified position.
/// </summary>

22
src/ImageSharp/Quantizers/Quantize.cs

@ -6,6 +6,7 @@
namespace ImageSharp
{
using System;
using System.Threading.Tasks;
using ImageSharp.Quantizers;
@ -55,9 +56,24 @@ namespace ImageSharp
public static Image<TColor> Quantize<TColor>(this Image<TColor> source, IQuantizer<TColor> quantizer, int maxColors)
where TColor : struct, IPackedPixel, IEquatable<TColor>
{
QuantizedImage<TColor> quantizedImage = quantizer.Quantize(source, maxColors);
source.SetPixels(source.Width, source.Height, quantizedImage.ToImage().Pixels);
QuantizedImage<TColor> quantized = quantizer.Quantize(source, maxColors);
int pixelCount = quantized.Pixels.Length;
int palleteCount = quantized.Palette.Length - 1;
TColor[] pixels = new TColor[pixelCount];
Parallel.For(
0,
pixelCount,
source.Bootstrapper.ParallelOptions,
i =>
{
TColor color = quantized.Palette[Math.Min(palleteCount, quantized.Pixels[i])];
pixels[i] = color;
});
source.SetPixels(source.Width, source.Height, pixels);
return source;
}
}
}
}

28
src/ImageSharp/Quantizers/QuantizedImage.cs

@ -59,33 +59,5 @@ namespace ImageSharp.Quantizers
/// Gets the pixels of this <see cref="T:QuantizedImage"/>.
/// </summary>
public byte[] Pixels { get; }
/// <summary>
/// Converts this quantized image to a normal image.
/// </summary>
/// <returns>
/// The <see cref="Image"/>
/// </returns>
public Image<TColor> ToImage()
{
Image<TColor> image = new Image<TColor>(this.Width, this.Height);
int pixelCount = this.Pixels.Length;
int palleteCount = this.Palette.Length - 1;
TColor[] pixels = new TColor[pixelCount];
Parallel.For(
0,
pixelCount,
image.Bootstrapper.ParallelOptions,
i =>
{
TColor color = this.Palette[Math.Min(palleteCount, this.Pixels[i])];
pixels[i] = color;
});
image.SetPixels(this.Width, this.Height, pixels);
return image;
}
}
}

2
src/ImageSharp/Quantizers/Wu/WuQuantizer.cs

@ -773,7 +773,7 @@ namespace ImageSharp.Quantizers
Parallel.For(
0,
height,
Bootstrapper.Default.ParallelOptions,
imagePixels.ParallelOptions,
y =>
{
byte[] rgba = ArrayPool<byte>.Shared.Rent(4);

Loading…
Cancel
Save