Browse Source

Optimize some low hanging fruit

af/merge-core
James Jackson-South 9 years ago
parent
commit
1aa94295ef
  1. 40
      src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs
  2. 4
      src/ImageSharp/Processing/Processors/Binarization/ErrorDiffusionDitherProcessor.cs
  3. 4
      src/ImageSharp/Quantizers/OctreeQuantizer{TPixel}.cs
  4. 4
      src/ImageSharp/Quantizers/PaletteQuantizer{TPixel}.cs
  5. 5
      src/ImageSharp/Quantizers/Quantizer{TPixel}.cs
  6. 4
      src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs

40
src/ImageSharp/Dithering/ErrorDiffusion/ErrorDiffuser.cs

@ -5,6 +5,7 @@
namespace ImageSharp.Dithering
{
using System;
using System.Numerics;
using System.Runtime.CompilerServices;
@ -95,30 +96,33 @@ namespace ImageSharp.Dithering
for (int row = 0; row < this.matrixHeight; row++)
{
int matrixY = y + row;
for (int col = 0; col < this.matrixWidth; col++)
if (matrixY > 0 && matrixY < height)
{
int matrixX = x + (col - this.startingOffset);
Span<TPixel> rowSpan = image.GetRowSpan(matrixY);
if (matrixX > 0 && matrixX < width && matrixY > 0 && matrixY < height)
for (int col = 0; col < this.matrixWidth; col++)
{
float coefficient = this.matrix[row, col];
int matrixX = x + (col - this.startingOffset);
// Good to disable here as we are not comparing mathematical output.
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (coefficient == 0)
if (matrixX > 0 && matrixX < width)
{
continue;
float coefficient = this.matrix[row, col];
// Good to disable here as we are not comparing mathematical output.
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (coefficient == 0)
{
continue;
}
ref TPixel pixel = ref rowSpan[matrixX];
var offsetColor = pixel.ToVector4();
var coefficientVector = new Vector4(coefficient);
Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor;
result.W = offsetColor.W;
pixel.PackFromVector4(result);
}
var coefficientVector = new Vector4(coefficient);
var offsetColor = image[matrixX, matrixY].ToVector4();
Vector4 result = ((error * coefficientVector) / this.divisorVector) + offsetColor;
result.W = offsetColor.W;
var packed = default(TPixel);
packed.PackFromVector4(result);
image[matrixX, matrixY] = packed;
}
}
}

4
src/ImageSharp/Processing/Processors/Binarization/ErrorDiffusionDitherProcessor.cs

@ -88,10 +88,12 @@ namespace ImageSharp.Processing.Processors
for (int y = minY; y < maxY; y++)
{
int offsetY = y - startY;
Span<TPixel> row = source.GetRowSpan(offsetY);
for (int x = minX; x < maxX; x++)
{
int offsetX = x - startX;
TPixel sourceColor = source[offsetX, offsetY];
TPixel sourceColor = row[offsetX];
TPixel transformedColor = sourceColor.ToVector4().X >= this.Threshold ? this.UpperColor : this.LowerColor;
this.Diffuser.Dither(source, sourceColor, transformedColor, offsetX, offsetY, maxX, maxY);
}

4
src/ImageSharp/Quantizers/OctreeQuantizer{TPixel}.cs

@ -78,11 +78,13 @@ namespace ImageSharp.Quantizers
for (int y = 0; y < height; y++)
{
Span<TPixel> row = source.GetRowSpan(y);
// And loop through each column
for (int x = 0; x < width; x++)
{
// Get the pixel.
sourcePixel = source[x, y];
sourcePixel = row[x];
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimization.

4
src/ImageSharp/Quantizers/PaletteQuantizer{TPixel}.cs

@ -84,11 +84,13 @@ namespace ImageSharp.Quantizers
for (int y = 0; y < height; y++)
{
Span<TPixel> row = source.GetRowSpan(y);
// And loop through each column
for (int x = 0; x < width; x++)
{
// Get the pixel.
sourcePixel = source[x, y];
sourcePixel = row[x];
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimization.

5
src/ImageSharp/Quantizers/Quantizer{TPixel}.cs

@ -5,6 +5,7 @@
namespace ImageSharp.Quantizers
{
using System;
using System.Collections.Generic;
using System.Numerics;
using System.Runtime.CompilerServices;
@ -93,11 +94,13 @@ namespace ImageSharp.Quantizers
// Loop through each row
for (int y = 0; y < height; y++)
{
Span<TPixel> row = source.GetRowSpan(y);
// And loop through each column
for (int x = 0; x < width; x++)
{
// Now I have the pixel, call the FirstPassQuantize function...
this.InitialQuantizePixel(source[x, y]);
this.InitialQuantizePixel(row[x]);
}
}
}

4
src/ImageSharp/Quantizers/WuQuantizer{TPixel}.cs

@ -252,11 +252,13 @@ namespace ImageSharp.Quantizers
for (int y = 0; y < height; y++)
{
Span<TPixel> row = source.GetRowSpan(y);
// And loop through each column
for (int x = 0; x < width; x++)
{
// Get the pixel.
sourcePixel = source[x, y];
sourcePixel = row[x];
// Check if this is the same as the last pixel. If so use that value
// rather than calculating it again. This is an inexpensive optimization.

Loading…
Cancel
Save